blob: 6e0ad25c46ad2abd04d7558fc5cce8322fe042cd [file] [log] [blame]
Kshitij Sisodiaf9efe0d2022-09-30 16:42:50 +01001//
Mikael Olsson308e7f12023-06-12 15:00:55 +02002// SPDX-FileCopyrightText: Copyright 2020, 2022-2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
Kshitij Sisodiaf9efe0d2022-09-30 16:42:50 +01003// SPDX-License-Identifier: Apache-2.0
4//
5%module driver
6%{
7#define SWIG_FILE_WITH_INIT
8%}
9
10//typemap definitions and other common stuff
11%include "standard_header.i"
12
13%{
14#include "ethosu.hpp"
15#include <fstream>
16#include <list>
17#include <string>
18#include <cstring>
19#include <sstream>
20#include <linux/ioctl.h>
21
22#define ETHOSU_IOCTL_BASE 0x01
23#define ETHOSU_IO(nr) _IO(ETHOSU_IOCTL_BASE, nr)
24#define ETHOSU_IOCTL_PING ETHOSU_IO(0x00)
25
26%}
27%include <typemaps/buffer.i>
28
29%shared_ptr(EthosU::Buffer);
30%shared_ptr(EthosU::Network);
31
Kshitij Sisodiaf9efe0d2022-09-30 16:42:50 +010032namespace std {
33 %template(UintVector) vector<unsigned int>;
34 %template(SizeTVector) vector<size_t>;
35 %template(SharedBufferVector) vector<shared_ptr<EthosU::Buffer>>;
36}
37
38namespace EthosU
39{
40
Mikael Olssone9c3f072023-06-12 15:58:10 +020041constexpr uint32_t DRIVER_LIBRARY_VERSION_MAJOR;
42constexpr uint32_t DRIVER_LIBRARY_VERSION_MINOR;
43constexpr uint32_t DRIVER_LIBRARY_VERSION_PATCH;
44
Mikael Olsson308e7f12023-06-12 15:00:55 +020045constexpr uint32_t MAX_SUPPORTED_KERNEL_DRIVER_MAJOR_VERSION;
46constexpr uint32_t MIN_SUPPORTED_KERNEL_DRIVER_MAJOR_VERSION;
47
Kshitij Sisodiaf9efe0d2022-09-30 16:42:50 +010048%feature("docstring",
49"
50Semantic Version : major.minor.patch
51") SemanticVersion;
52%nodefaultctor SemanticVersion;
53class SemanticVersion {
54public:
55 SemanticVersion(uint32_t major = 0, uint32_t minor = 0, uint32_t patch = 0);
56
57 uint32_t major;
58 uint32_t minor;
59 uint32_t patch;
60};
61
62%extend SemanticVersion {
63 std::string __str__() const {
64 std::ostringstream out;
65 out << *$self;
66 return out.str();
67 }
68}
69
70%feature("docstring",
71"
Mikael Olssone9c3f072023-06-12 15:58:10 +020072 Return driver library version information.
73
74 Returns:
75 SemanticVersion: driver library version.
76") getLibraryVersion;
77const SemanticVersion getLibraryVersion();
78
79%feature("docstring",
80"
Kshitij Sisodiaf9efe0d2022-09-30 16:42:50 +010081Hardware Identifier which consists of version status, version revision, product revision and architecture revision.
82") HardwareId;
83class HardwareId {
84public:
85 HardwareId(uint32_t versionStatus, SemanticVersion& version, SemanticVersion& product, SemanticVersion& arch);
86
87 uint32_t versionStatus{0};
88 SemanticVersion version{};
89 SemanticVersion product{};
90 SemanticVersion architecture{};
91};
92
93%extend HardwareId {
94 std::string __str__() const {
95 std::ostringstream out;
96 out << "{versionStatus=" << $self->versionStatus <<
97 ", version=" << EthosU_SemanticVersion___str__(&$self->version) <<
98 ", product=" << EthosU_SemanticVersion___str__(&$self->product) <<
99 ", architecture=" << EthosU_SemanticVersion___str__(&$self->architecture) << "}";
100 return out.str();
101 }
102}
103
104%feature("docstring",
105"
106Hardware Configuration object defines specific configuration including MACs per clock cycle and NPU command stream
107version. This also specifies is custom DMA is enabled or not.
108") HardwareConfiguration;
109%nodefaultctor HardwareConfiguration;
110class HardwareConfiguration {
111 public:
112 HardwareConfiguration(uint32_t macs = 0, uint32_t cmdStreamVersion = 0, bool customDma = false);
113
114 uint32_t macsPerClockCycle;
115 uint32_t cmdStreamVersion;
116 bool customDma;
117};
118
119%extend HardwareConfiguration {
120 std::string __str__() const {
121 std::ostringstream out;
122 out << "{macsPerClockCycle=" << $self->macsPerClockCycle <<
123 ", cmdStreamVersion=" << $self->cmdStreamVersion <<
124 ", customDma=" << ($self->customDma? "True": "False") << "}";
125 return out.str();
126 }
127}
128
129%feature("docstring",
130"
131Device capabilities object which specifies capabilities based on hardware ID, configuration and semantic version.
132") Capabilities;
133class Capabilities {
134 public:
135 Capabilities() {}
136 Capabilities(const HardwareId& hwId, const HardwareConfiguration& hwCfg, const SemanticVersion& driverVersion);
137
138 HardwareId hwId;
139 HardwareConfiguration hwCfg;
140 SemanticVersion driver;
141};
142
143%extend Capabilities {
144 std::string __str__() const {
145 std::ostringstream out;
146 out << "{hwId=" << EthosU_HardwareId___str__(&$self->hwId) <<
147 ", hwCfg=" << EthosU_HardwareConfiguration___str__(&$self->hwCfg) <<
148 ", driver=" << EthosU_SemanticVersion___str__(&$self->driver) << "}";
149 return out.str();
150 }
151}
152
153%feature("docstring",
154"
155Device object represents Ethos-U device file descriptor and manages Ethos-U device lifecycle.
156Constructor accepts device name and opens file descriptor with O_RDWR | O_NONBLOCK flags.
157When the object is destroyed - device file descriptor is closed.
158") Device;
159%nodefaultctor Device;
160class Device {
161public:
162 Device(const char *device);
163
164 %feature("docstring",
165 "
166 Performs the I/O control operation on the Ethos-U device.
167
168 Args:
169 cmd: Command code
170 data: Command data
171 Returns:
172 int: Return value depends on command. Usually -1 indicates error.
173 ") ioctl;
174 int ioctl(unsigned long cmd, void *data = nullptr) const;
175
176 %feature("docstring",
177 "
178 Returns the capabilities of the Ethos-U device.
179
180 Returns:
181 Capabilities: Return capabilities of device.
182 ") capabilities;
183 Capabilities capabilities() const;
Mikael Olsson308e7f12023-06-12 15:00:55 +0200184
185 %feature("docstring",
186 "
187 Returns kernel driver version information.
188
189 Returns:
190 SemanticVersion: kernel driver version.
191 ") getDriverVersion;
192 const SemanticVersion &getDriverVersion() const;
Kshitij Sisodiaf9efe0d2022-09-30 16:42:50 +0100193};
194
195%extend Device {
196
197 %feature("docstring",
198 "
199 Sends ping command to the Ethos-U device.
200
201 See ETHOSU_IOCTL_PING from kernel module uapi/ethosu.h
202 ") ping;
203 void ping() {
204 $self->ioctl(ETHOSU_IOCTL_PING);
205 }
206}
207
208%feature("docstring",
209 "
210 Buffer object represents a RW mapping in the virtual address space of the caller.
211
212 Created mapping is shareable, updates to the mapping are visible to other processes mapping the same region.
Mikael Olsson07545152023-10-17 13:05:38 +0200213 Issues ETHOSU_IOCTL_BUFFER_CREATE I/O request to the device with given size.
Kshitij Sisodiaf9efe0d2022-09-30 16:42:50 +0100214
Mikael Olsson07545152023-10-17 13:05:38 +0200215 Buffer could be created for a device with given size or instantiated directly from
Kshitij Sisodiaf9efe0d2022-09-30 16:42:50 +0100216 a file containing binary data.
217
218 Examples:
219 >>> import ethosu_driver as driver
220 >>> # from file:
221 >>> buf = driver.Buffer(device, '/path/to/file')
Mikael Olsson07545152023-10-17 13:05:38 +0200222 >>> # Empty, with size:
Kshitij Sisodiaf9efe0d2022-09-30 16:42:50 +0100223 >>> buf = driver.Buffer(device, 1024)
224 ") Buffer;
225%nodefaultctor Buffer;
226class Buffer {
227public:
Mikael Olsson07545152023-10-17 13:05:38 +0200228 Buffer(const Device &device, const size_t size);
Kshitij Sisodiaf9efe0d2022-09-30 16:42:50 +0100229
230 %feature("docstring",
231 "
232 Sets the size of the device buffer to 0.
233 ") clear;
234 void clear() const;
235
236 %feature("docstring",
237 "
238 Returns a readonly view to the mapped memory.
239
240 Returns:
241 memoryview: readonly memory data.
242 ") data;
243 %driver_buffer_out;
244 char* data() const;
245 %clear_driver_buffer_out;
246
247 %feature("docstring",
248 "
Kshitij Sisodiaf9efe0d2022-09-30 16:42:50 +0100249 Queries device and returns buffer data size.
250
251 Issues ETHOSU_IOCTL_BUFFER_GET I/O request.
252
253 Returns:
254 int: current device buffer size.
255 ") size;
256 size_t size() const;
257
258 %feature("docstring",
259 "
260 Returns buffer file descriptor id.
261
262 Returns:
263 int: file descriptor id.
264 ") getFd;
265 int getFd() const;
266};
267
268%extend Buffer {
269
270 Buffer(const Device& device, const std::string& filename) {
271 std::ifstream stream(filename, std::ios::binary);
272 if (!stream.is_open()) {
273 throw EthosU::Exception(std::string("Failed to open file: ").append(filename).c_str());
274 }
275
276 stream.seekg(0, std::ios_base::end);
277 size_t size = stream.tellg();
278 stream.seekg(0, std::ios_base::beg);
279
280 auto buffer = new EthosU::Buffer(device, size);
Kshitij Sisodiaf9efe0d2022-09-30 16:42:50 +0100281 stream.read(buffer->data(), size);
282
283 return buffer;
284 }
285
286 %feature("docstring",
287 "
288 Fills the buffer from python buffer.
289
290 Copies python buffer data to the mapped memory region.
Kshitij Sisodiaf9efe0d2022-09-30 16:42:50 +0100291
292 Args:
293 buffer: data to be copied to the mapped memory.
294
295 ") from_buffer;
Mikael Olssonc081e592023-10-30 11:10:56 +0100296 %buffer_in(char* buffer, size_t size, BUFFER_FLAG_RW);
Kshitij Sisodiaf9efe0d2022-09-30 16:42:50 +0100297 void from_buffer(char* buffer, size_t size) {
Kshitij Sisodiaf9efe0d2022-09-30 16:42:50 +0100298 char* data = $self->data();
299 std::memcpy(data, buffer, size);
300 }
Mikael Olssonc081e592023-10-30 11:10:56 +0100301 %clear_buffer_in(char* buffer, size_t size);
Kshitij Sisodiaf9efe0d2022-09-30 16:42:50 +0100302}
303
304%feature("docstring",
305 "
306 Represents the neural network file descriptor received from the Ethos-U device.
307
308 `Network` is created providing `Device` object and a `Buffer` containing tflite file data.
309 Network creation issues ETHOSU_IOCTL_NETWORK_CREATE I/O request with buffer file descriptor id.
310 Provided `Buffer` data is parsed into tflite Model object and input/output feature maps sizes are saved.
311
312 Destruction of the object closes network file descriptor.
313 ") Network;
314%nodefaultctor Network;
315class Network {
316public:
317
318 %feature("docstring",
319 "
320 Performs the I/O control operation with network buffer device.
321
322 Args:
323 cmd: Command code
324 data: Command data
325 Returns:
326 int: Return value depends on command. Usually -1 indicates error.
327 ") ioctl;
328 int ioctl(unsigned long cmd, void *data);
329
330 %feature("docstring",
331 "
Kshitij Sisodiaf9efe0d2022-09-30 16:42:50 +0100332 Returns saved sizes of the neural network model input feature maps.
333
334 Returns:
335 list: sizes of all input feature maps
336 ") getIfmDims;
337 const std::vector<size_t> &getIfmDims() const;
338
339 %feature("docstring",
340 "
341 Returns total size of all input feature maps.
342
343 Returns:
344 int: total size of all input feature maps
345 ") getIfmSize;
346 size_t getIfmSize() const;
347
348 %feature("docstring",
349 "
350 Returns saved sizes of the neural network model output feature maps.
351
352 Returns:
353 list: sizes of all output feature maps
354 ") getOfmDims;
355 const std::vector<size_t> &getOfmDims() const;
356
357 %feature("docstring",
358 "
359 Returns total size of all output feature maps.
360
361 Returns:
362 int: total size of all output feature maps
363 ") getOfmSize;
364 size_t getOfmSize() const;
365};
366
367%extend Network {
Kshitij Sisodiaf9efe0d2022-09-30 16:42:50 +0100368
Mikael Olssonc081e592023-10-30 11:10:56 +0100369 Network(const Device &device, const std::string& filename)
370 {
371 std::ifstream stream(filename, std::ios::binary);
372 if (!stream.is_open()) {
373 throw EthosU::Exception(std::string("Failed to open file: ").append(filename).c_str());
374 }
375
376 stream.seekg(0, std::ios_base::end);
377 size_t size = stream.tellg();
378 stream.seekg(0, std::ios_base::beg);
379
380 std::unique_ptr<unsigned char[]> buffer = std::make_unique<unsigned char[]>(size);
381 stream.read(reinterpret_cast<char*>(buffer.get()), size);
382 return new EthosU::Network(device, buffer.get(), size);
383 }
384
385 %buffer_in(const unsigned char* networkData, size_t networkSize, BUFFER_FLAG_RO);
386 Network(const Device &device, const unsigned char* networkData, size_t networkSize)
387 {
388 if(networkData == nullptr){
389 throw EthosU::Exception(std::string("Failed to create the network, networkData is nullptr.").c_str());
390 }
391
392 if(networkSize == 0U){
393 throw EthosU::Exception(std::string("Failed to create the network, networkSize is zero.").c_str());
394 }
395
396 return new EthosU::Network(device, networkData, networkSize);
397 }
398 %clear_buffer_in(const unsigned char* networkData, size_t networkSize);
399
Kshitij Sisodiaf9efe0d2022-09-30 16:42:50 +0100400 Network(const Device &device, const unsigned int index)
401 {
Mikael Olssonc081e592023-10-30 11:10:56 +0100402 return new EthosU::Network(device, index);
Kshitij Sisodiaf9efe0d2022-09-30 16:42:50 +0100403 }
404}
405
406%feature("docstring",
407 "
408 InferenceStatus enumeration
409 ") InferenceStatus;
410enum class InferenceStatus {
411 OK,
412 ERROR,
413 RUNNING,
414 REJECTED,
415 ABORTED,
416 ABORTING
417 };
418
419%feature("docstring",
420 "
421 Represents the inference file descriptor received from the Ethos-U device.
422
423 `Inference` is created providing `Network` object and lists of input and output feature maps buffers.
424 Feature map buffers are copied.
425
426 Inference creation issues ETHOSU_IOCTL_INFERENCE_CREATE I/O request with
427 file descriptor ids for all input and output buffers.
428
429 The number of input/output buffers must not exceed ETHOSU_FD_MAX value defined in the kernel module
430 uapi/ethosu.h.
431
432 Destruction of the object closes inference file descriptor.
433 ") Inference;
434%nodefaultctor Inference;
435class Inference {
436public:
437
438 %feature("docstring",
439 "
440 Polls inference file descriptor for events.
441
442 Args:
443 timeoutNanos (int64_t): polling timeout in nanoseconds.
444
445 Returns:
446 bool: True for success, False otherwise.
447 ") wait;
448 void wait(int64_t timeoutNanos = -1) const;
449
450 %feature("docstring",
451 "
452 Aborts the current inference job.
453
454 Returns:
455 bool: True if gracefully stopped, False otherwise.
456 ") cancel;
457 bool cancel() const;
458
459 %feature("docstring",
460 "
461 Gets the current inference job status.
462
463 Returns:
464 InferenceStatus.
465 ") status;
466 EthosU::InferenceStatus status() const;
467
468 %feature("docstring",
469 "
470 Returns inference file descriptor.
471
472 Returns:
473 int: file descriptor id
474 ") getFd;
475 int getFd() const;
476
477 %feature("docstring",
478 "
479 Returns associated `Network` object.
480
481 Returns:
482 `Network`: network used during initialisation
483 ") getNetwork;
484 std::shared_ptr<Network> getNetwork() const;
485
486 %feature("docstring",
487 "
488 Returns copied input feature maps buffers.
489
490 Returns:
491 list: input feature map buffers
492 ") getIfmBuffers;
493 std::vector<std::shared_ptr<Buffer>> &getIfmBuffers();
494
495 %feature("docstring",
496 "
497 Returns copied output feature maps buffers.
498
499 Returns:
500 list: output feature map buffers
501 ") getOfmBuffers;
502 std::vector<std::shared_ptr<Buffer>> &getOfmBuffers();
503
504 %feature("docstring",
505 "
506 Returns PMU event data.
507
508 Returns:
509 list: PMU event data
510 ") getPmuCounters;
511 const std::vector<uint32_t> getPmuCounters();
512
513 %feature("docstring",
514 "
515 Returns the total cycle count, including idle cycles, as reported by the PMU.
516
517 Returns:
518 int: total cycle count
519 ") getCycleCounter;
520 uint64_t getCycleCounter();
521
522 %feature("docstring",
523 "
524 Returns maximum supported number of PMU events.
525
526 Returns:
527 int: PMU event max
528 ") getMaxPmuEventCounters;
529 static uint32_t getMaxPmuEventCounters();
530};
531
532%extend Inference {
533 Inference(const std::shared_ptr<Network> &network,
534 const std::vector<std::shared_ptr<Buffer>> &ifm,
535 const std::vector<std::shared_ptr<Buffer>> &ofm)
536 {
537 return new EthosU::Inference(network, ifm.begin(), ifm.end(), ofm.begin(), ofm.end());
538 }
539 Inference(const std::shared_ptr<Network> & network,
540 const std::vector<std::shared_ptr<Buffer>> &ifm,
541 const std::vector<std::shared_ptr<Buffer>> &ofm,
542 const std::vector<unsigned int> &enabledCounters,
543 bool enableCycleCounter)
544 {
545 return new EthosU::Inference(network, ifm.begin(), ifm.end(), ofm.begin(), ofm.end(), enabledCounters, enableCycleCounter);
546 }
547}
548
549}
550// Clear exception typemap.
551%exception;