blob: 721e4d03928c8eb315a97ab60175b5113266b4c7 [file] [log] [blame]
Kshitij Sisodiaf9efe0d2022-09-30 16:42:50 +01001//
Mikael Olssondc18cea2024-02-16 11:04:46 +01002// SPDX-FileCopyrightText: Copyright 2020, 2022-2024 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:
Mikael Olssondc18cea2024-02-16 11:04:46 +0100120 HardwareConfiguration(uint32_t macs = 0, uint32_t cmdStreamVersion = 0, uint32_t type = static_cast<uint32_t>(DeviceType::UNKNOWN), bool customDma = false);
121
122 %feature("docstring",
123 "
124 DeviceType enumeration
125 ") DeviceType;
126 enum class DeviceType {
127 UNKNOWN = 0,
128 SUBSYSTEM,
129 DIRECT,
130 };
Kshitij Sisodiaf9efe0d2022-09-30 16:42:50 +0100131
132 uint32_t macsPerClockCycle;
133 uint32_t cmdStreamVersion;
Mikael Olssondc18cea2024-02-16 11:04:46 +0100134 DeviceType type;
Kshitij Sisodiaf9efe0d2022-09-30 16:42:50 +0100135 bool customDma;
136};
137
138%extend HardwareConfiguration {
139 std::string __str__() const {
140 std::ostringstream out;
141 out << "{macsPerClockCycle=" << $self->macsPerClockCycle <<
142 ", cmdStreamVersion=" << $self->cmdStreamVersion <<
Mikael Olssondc18cea2024-02-16 11:04:46 +0100143 ", type=" << $self->type <<
Kshitij Sisodiaf9efe0d2022-09-30 16:42:50 +0100144 ", customDma=" << ($self->customDma? "True": "False") << "}";
145 return out.str();
146 }
147}
148
149%feature("docstring",
150"
151Device capabilities object which specifies capabilities based on hardware ID, configuration and semantic version.
152") Capabilities;
153class Capabilities {
154 public:
155 Capabilities() {}
156 Capabilities(const HardwareId& hwId, const HardwareConfiguration& hwCfg, const SemanticVersion& driverVersion);
157
158 HardwareId hwId;
159 HardwareConfiguration hwCfg;
160 SemanticVersion driver;
161};
162
163%extend Capabilities {
164 std::string __str__() const {
165 std::ostringstream out;
166 out << "{hwId=" << EthosU_HardwareId___str__(&$self->hwId) <<
167 ", hwCfg=" << EthosU_HardwareConfiguration___str__(&$self->hwCfg) <<
168 ", driver=" << EthosU_SemanticVersion___str__(&$self->driver) << "}";
169 return out.str();
170 }
171}
172
173%feature("docstring",
174"
175Device object represents Ethos-U device file descriptor and manages Ethos-U device lifecycle.
176Constructor accepts device name and opens file descriptor with O_RDWR | O_NONBLOCK flags.
177When the object is destroyed - device file descriptor is closed.
178") Device;
179%nodefaultctor Device;
180class Device {
181public:
182 Device(const char *device);
183
184 %feature("docstring",
185 "
186 Performs the I/O control operation on the Ethos-U device.
187
188 Args:
189 cmd: Command code
190 data: Command data
191 Returns:
192 int: Return value depends on command. Usually -1 indicates error.
193 ") ioctl;
194 int ioctl(unsigned long cmd, void *data = nullptr) const;
195
196 %feature("docstring",
197 "
198 Returns the capabilities of the Ethos-U device.
199
200 Returns:
201 Capabilities: Return capabilities of device.
202 ") capabilities;
203 Capabilities capabilities() const;
Mikael Olsson308e7f12023-06-12 15:00:55 +0200204
205 %feature("docstring",
206 "
207 Returns kernel driver version information.
208
209 Returns:
210 SemanticVersion: kernel driver version.
211 ") getDriverVersion;
212 const SemanticVersion &getDriverVersion() const;
Kshitij Sisodiaf9efe0d2022-09-30 16:42:50 +0100213};
214
215%extend Device {
216
217 %feature("docstring",
218 "
219 Sends ping command to the Ethos-U device.
220
221 See ETHOSU_IOCTL_PING from kernel module uapi/ethosu.h
222 ") ping;
223 void ping() {
224 $self->ioctl(ETHOSU_IOCTL_PING);
225 }
226}
227
228%feature("docstring",
229 "
230 Buffer object represents a RW mapping in the virtual address space of the caller.
231
232 Created mapping is shareable, updates to the mapping are visible to other processes mapping the same region.
Mikael Olsson07545152023-10-17 13:05:38 +0200233 Issues ETHOSU_IOCTL_BUFFER_CREATE I/O request to the device with given size.
Kshitij Sisodiaf9efe0d2022-09-30 16:42:50 +0100234
Mikael Olsson07545152023-10-17 13:05:38 +0200235 Buffer could be created for a device with given size or instantiated directly from
Kshitij Sisodiaf9efe0d2022-09-30 16:42:50 +0100236 a file containing binary data.
237
238 Examples:
239 >>> import ethosu_driver as driver
240 >>> # from file:
241 >>> buf = driver.Buffer(device, '/path/to/file')
Mikael Olsson07545152023-10-17 13:05:38 +0200242 >>> # Empty, with size:
Kshitij Sisodiaf9efe0d2022-09-30 16:42:50 +0100243 >>> buf = driver.Buffer(device, 1024)
244 ") Buffer;
245%nodefaultctor Buffer;
246class Buffer {
247public:
Mikael Olsson07545152023-10-17 13:05:38 +0200248 Buffer(const Device &device, const size_t size);
Kshitij Sisodiaf9efe0d2022-09-30 16:42:50 +0100249
250 %feature("docstring",
251 "
252 Sets the size of the device buffer to 0.
253 ") clear;
254 void clear() const;
255
256 %feature("docstring",
257 "
258 Returns a readonly view to the mapped memory.
259
260 Returns:
261 memoryview: readonly memory data.
262 ") data;
263 %driver_buffer_out;
264 char* data() const;
265 %clear_driver_buffer_out;
266
267 %feature("docstring",
268 "
Kshitij Sisodiaf9efe0d2022-09-30 16:42:50 +0100269 Queries device and returns buffer data size.
270
271 Issues ETHOSU_IOCTL_BUFFER_GET I/O request.
272
273 Returns:
274 int: current device buffer size.
275 ") size;
276 size_t size() const;
277
278 %feature("docstring",
279 "
280 Returns buffer file descriptor id.
281
282 Returns:
283 int: file descriptor id.
284 ") getFd;
285 int getFd() const;
286};
287
288%extend Buffer {
289
290 Buffer(const Device& device, const std::string& filename) {
291 std::ifstream stream(filename, std::ios::binary);
292 if (!stream.is_open()) {
293 throw EthosU::Exception(std::string("Failed to open file: ").append(filename).c_str());
294 }
295
296 stream.seekg(0, std::ios_base::end);
297 size_t size = stream.tellg();
298 stream.seekg(0, std::ios_base::beg);
299
300 auto buffer = new EthosU::Buffer(device, size);
Kshitij Sisodiaf9efe0d2022-09-30 16:42:50 +0100301 stream.read(buffer->data(), size);
302
303 return buffer;
304 }
305
306 %feature("docstring",
307 "
308 Fills the buffer from python buffer.
309
310 Copies python buffer data to the mapped memory region.
Kshitij Sisodiaf9efe0d2022-09-30 16:42:50 +0100311
312 Args:
313 buffer: data to be copied to the mapped memory.
314
315 ") from_buffer;
Mikael Olssonc081e592023-10-30 11:10:56 +0100316 %buffer_in(char* buffer, size_t size, BUFFER_FLAG_RW);
Kshitij Sisodiaf9efe0d2022-09-30 16:42:50 +0100317 void from_buffer(char* buffer, size_t size) {
Kshitij Sisodiaf9efe0d2022-09-30 16:42:50 +0100318 char* data = $self->data();
319 std::memcpy(data, buffer, size);
320 }
Mikael Olssonc081e592023-10-30 11:10:56 +0100321 %clear_buffer_in(char* buffer, size_t size);
Kshitij Sisodiaf9efe0d2022-09-30 16:42:50 +0100322}
323
324%feature("docstring",
325 "
326 Represents the neural network file descriptor received from the Ethos-U device.
327
328 `Network` is created providing `Device` object and a `Buffer` containing tflite file data.
329 Network creation issues ETHOSU_IOCTL_NETWORK_CREATE I/O request with buffer file descriptor id.
330 Provided `Buffer` data is parsed into tflite Model object and input/output feature maps sizes are saved.
331
332 Destruction of the object closes network file descriptor.
333 ") Network;
334%nodefaultctor Network;
335class Network {
336public:
337
338 %feature("docstring",
339 "
340 Performs the I/O control operation with network buffer device.
341
342 Args:
343 cmd: Command code
344 data: Command data
345 Returns:
346 int: Return value depends on command. Usually -1 indicates error.
347 ") ioctl;
348 int ioctl(unsigned long cmd, void *data);
349
350 %feature("docstring",
351 "
Kshitij Sisodiaf9efe0d2022-09-30 16:42:50 +0100352 Returns saved sizes of the neural network model input feature maps.
353
354 Returns:
355 list: sizes of all input feature maps
356 ") getIfmDims;
357 const std::vector<size_t> &getIfmDims() const;
358
359 %feature("docstring",
360 "
361 Returns total size of all input feature maps.
362
363 Returns:
364 int: total size of all input feature maps
365 ") getIfmSize;
366 size_t getIfmSize() const;
367
368 %feature("docstring",
369 "
370 Returns saved sizes of the neural network model output feature maps.
371
372 Returns:
373 list: sizes of all output feature maps
374 ") getOfmDims;
375 const std::vector<size_t> &getOfmDims() const;
376
377 %feature("docstring",
378 "
379 Returns total size of all output feature maps.
380
381 Returns:
382 int: total size of all output feature maps
383 ") getOfmSize;
384 size_t getOfmSize() const;
385};
386
387%extend Network {
Kshitij Sisodiaf9efe0d2022-09-30 16:42:50 +0100388
Mikael Olssonc081e592023-10-30 11:10:56 +0100389 Network(const Device &device, const std::string& filename)
390 {
391 std::ifstream stream(filename, std::ios::binary);
392 if (!stream.is_open()) {
393 throw EthosU::Exception(std::string("Failed to open file: ").append(filename).c_str());
394 }
395
396 stream.seekg(0, std::ios_base::end);
397 size_t size = stream.tellg();
398 stream.seekg(0, std::ios_base::beg);
399
400 std::unique_ptr<unsigned char[]> buffer = std::make_unique<unsigned char[]>(size);
401 stream.read(reinterpret_cast<char*>(buffer.get()), size);
402 return new EthosU::Network(device, buffer.get(), size);
403 }
404
405 %buffer_in(const unsigned char* networkData, size_t networkSize, BUFFER_FLAG_RO);
406 Network(const Device &device, const unsigned char* networkData, size_t networkSize)
407 {
408 if(networkData == nullptr){
409 throw EthosU::Exception(std::string("Failed to create the network, networkData is nullptr.").c_str());
410 }
411
412 if(networkSize == 0U){
413 throw EthosU::Exception(std::string("Failed to create the network, networkSize is zero.").c_str());
414 }
415
416 return new EthosU::Network(device, networkData, networkSize);
417 }
418 %clear_buffer_in(const unsigned char* networkData, size_t networkSize);
419
Kshitij Sisodiaf9efe0d2022-09-30 16:42:50 +0100420 Network(const Device &device, const unsigned int index)
421 {
Mikael Olssonc081e592023-10-30 11:10:56 +0100422 return new EthosU::Network(device, index);
Kshitij Sisodiaf9efe0d2022-09-30 16:42:50 +0100423 }
424}
425
426%feature("docstring",
427 "
428 InferenceStatus enumeration
429 ") InferenceStatus;
430enum class InferenceStatus {
431 OK,
432 ERROR,
433 RUNNING,
434 REJECTED,
435 ABORTED,
436 ABORTING
437 };
438
439%feature("docstring",
440 "
441 Represents the inference file descriptor received from the Ethos-U device.
442
443 `Inference` is created providing `Network` object and lists of input and output feature maps buffers.
444 Feature map buffers are copied.
445
446 Inference creation issues ETHOSU_IOCTL_INFERENCE_CREATE I/O request with
447 file descriptor ids for all input and output buffers.
448
449 The number of input/output buffers must not exceed ETHOSU_FD_MAX value defined in the kernel module
450 uapi/ethosu.h.
451
452 Destruction of the object closes inference file descriptor.
453 ") Inference;
454%nodefaultctor Inference;
455class Inference {
456public:
457
458 %feature("docstring",
459 "
460 Polls inference file descriptor for events.
461
462 Args:
463 timeoutNanos (int64_t): polling timeout in nanoseconds.
464
465 Returns:
466 bool: True for success, False otherwise.
467 ") wait;
468 void wait(int64_t timeoutNanos = -1) const;
469
470 %feature("docstring",
471 "
472 Aborts the current inference job.
473
474 Returns:
475 bool: True if gracefully stopped, False otherwise.
476 ") cancel;
477 bool cancel() const;
478
479 %feature("docstring",
480 "
481 Gets the current inference job status.
482
483 Returns:
484 InferenceStatus.
485 ") status;
486 EthosU::InferenceStatus status() const;
487
488 %feature("docstring",
489 "
490 Returns inference file descriptor.
491
492 Returns:
493 int: file descriptor id
494 ") getFd;
495 int getFd() const;
496
497 %feature("docstring",
498 "
499 Returns associated `Network` object.
500
501 Returns:
502 `Network`: network used during initialisation
503 ") getNetwork;
504 std::shared_ptr<Network> getNetwork() const;
505
506 %feature("docstring",
507 "
508 Returns copied input feature maps buffers.
509
510 Returns:
511 list: input feature map buffers
512 ") getIfmBuffers;
513 std::vector<std::shared_ptr<Buffer>> &getIfmBuffers();
514
515 %feature("docstring",
516 "
517 Returns copied output feature maps buffers.
518
519 Returns:
520 list: output feature map buffers
521 ") getOfmBuffers;
522 std::vector<std::shared_ptr<Buffer>> &getOfmBuffers();
523
524 %feature("docstring",
525 "
526 Returns PMU event data.
527
528 Returns:
529 list: PMU event data
530 ") getPmuCounters;
Mikael Olssone87446c2023-12-15 17:17:06 +0100531 const std::vector<uint64_t> getPmuCounters();
Kshitij Sisodiaf9efe0d2022-09-30 16:42:50 +0100532
533 %feature("docstring",
534 "
535 Returns the total cycle count, including idle cycles, as reported by the PMU.
536
537 Returns:
538 int: total cycle count
539 ") getCycleCounter;
540 uint64_t getCycleCounter();
541
542 %feature("docstring",
543 "
544 Returns maximum supported number of PMU events.
545
546 Returns:
547 int: PMU event max
548 ") getMaxPmuEventCounters;
549 static uint32_t getMaxPmuEventCounters();
550};
551
552%extend Inference {
553 Inference(const std::shared_ptr<Network> &network,
554 const std::vector<std::shared_ptr<Buffer>> &ifm,
555 const std::vector<std::shared_ptr<Buffer>> &ofm)
556 {
557 return new EthosU::Inference(network, ifm.begin(), ifm.end(), ofm.begin(), ofm.end());
558 }
559 Inference(const std::shared_ptr<Network> & network,
560 const std::vector<std::shared_ptr<Buffer>> &ifm,
561 const std::vector<std::shared_ptr<Buffer>> &ofm,
562 const std::vector<unsigned int> &enabledCounters,
563 bool enableCycleCounter)
564 {
565 return new EthosU::Inference(network, ifm.begin(), ifm.end(), ofm.begin(), ofm.end(), enabledCounters, enableCycleCounter);
566 }
567}
568
569}
570// Clear exception typemap.
571%exception;