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