blob: 1ff8ded41e5dfdc6cb45cbc27b3fe0ff45f385b1 [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.
213 Issues ETHOSU_IOCTL_BUFFER_CREATE I/O request to the device with given Maximum capacity.
214
215 Buffer could be created for a device with given maximum capacity or instantiated directly from
216 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')
222 >>> # Empty, with maximum capacity:
223 >>> buf = driver.Buffer(device, 1024)
224 ") Buffer;
225%nodefaultctor Buffer;
226class Buffer {
227public:
228 Buffer(const Device &device, const size_t capacity);
229
230 %feature("docstring",
231 "
232 Returns maximum buffer capacity set during initialisation.
233
234 Returns:
235 int: maximum buffer capacity.
236 ") capacity;
237 size_t capacity() const;
238
239 %feature("docstring",
240 "
241 Sets the size of the device buffer to 0.
242 ") clear;
243 void clear() const;
244
245 %feature("docstring",
246 "
247 Returns a readonly view to the mapped memory.
248
249 Returns:
250 memoryview: readonly memory data.
251 ") data;
252 %driver_buffer_out;
253 char* data() const;
254 %clear_driver_buffer_out;
255
256 %feature("docstring",
257 "
258 Sets a size of the memory buffer for the device.
259
260 'offset + size' must not exceed the capacity of the buffer.
261 Does not change the size of the mapped region.
262
263 Issues ETHOSU_IOCTL_BUFFER_SET I/O request with a given size and offset.
264
265 Args:
266 size (int): Device buffer size.
267 offset (int): Offset to where the data starts.
268 ") resize;
269 void resize(size_t size, size_t offset = 0) const;
270
271 %feature("docstring",
272 "
273 Queries device and returns buffer data offset.
274
275 Issues ETHOSU_IOCTL_BUFFER_GET I/O request.
276
277 Returns:
278 int: data offset
279 ") offset;
280 size_t offset() const;
281
282 %feature("docstring",
283 "
284 Queries device and returns buffer data size.
285
286 Issues ETHOSU_IOCTL_BUFFER_GET I/O request.
287
288 Returns:
289 int: current device buffer size.
290 ") size;
291 size_t size() const;
292
293 %feature("docstring",
294 "
295 Returns buffer file descriptor id.
296
297 Returns:
298 int: file descriptor id.
299 ") getFd;
300 int getFd() const;
301};
302
303%extend Buffer {
304
305 Buffer(const Device& device, const std::string& filename) {
306 std::ifstream stream(filename, std::ios::binary);
307 if (!stream.is_open()) {
308 throw EthosU::Exception(std::string("Failed to open file: ").append(filename).c_str());
309 }
310
311 stream.seekg(0, std::ios_base::end);
312 size_t size = stream.tellg();
313 stream.seekg(0, std::ios_base::beg);
314
315 auto buffer = new EthosU::Buffer(device, size);
316 buffer->resize(size);
317 stream.read(buffer->data(), size);
318
319 return buffer;
320 }
321
322 %feature("docstring",
323 "
324 Fills the buffer from python buffer.
325
326 Copies python buffer data to the mapped memory region.
327 Input buffer size must be within `Buffer` maximum capacity.
328
329 Args:
330 buffer: data to be copied to the mapped memory.
331
332 ") from_buffer;
333 %mutable_buffer(char* buffer, size_t size);
334 void from_buffer(char* buffer, size_t size) {
335 self->resize(size);
336 char* data = $self->data();
337 std::memcpy(data, buffer, size);
338 }
339 %clear_mutable_buffer(char* buffer, size_t size);
340}
341
342%feature("docstring",
343 "
344 Represents the neural network file descriptor received from the Ethos-U device.
345
346 `Network` is created providing `Device` object and a `Buffer` containing tflite file data.
347 Network creation issues ETHOSU_IOCTL_NETWORK_CREATE I/O request with buffer file descriptor id.
348 Provided `Buffer` data is parsed into tflite Model object and input/output feature maps sizes are saved.
349
350 Destruction of the object closes network file descriptor.
351 ") Network;
352%nodefaultctor Network;
353class Network {
354public:
355
356 %feature("docstring",
357 "
358 Performs the I/O control operation with network buffer device.
359
360 Args:
361 cmd: Command code
362 data: Command data
363 Returns:
364 int: Return value depends on command. Usually -1 indicates error.
365 ") ioctl;
366 int ioctl(unsigned long cmd, void *data);
367
368 %feature("docstring",
369 "
370 Returns associated memory buffer.
371
372 Returns:
373 `Buffer`: buffer object used during initialisation.
374 ") getBuffer;
375 std::shared_ptr<Buffer> getBuffer();
376
377 %feature("docstring",
378 "
379 Returns saved sizes of the neural network model input feature maps.
380
381 Returns:
382 list: sizes of all input feature maps
383 ") getIfmDims;
384 const std::vector<size_t> &getIfmDims() const;
385
386 %feature("docstring",
387 "
388 Returns total size of all input feature maps.
389
390 Returns:
391 int: total size of all input feature maps
392 ") getIfmSize;
393 size_t getIfmSize() const;
394
395 %feature("docstring",
396 "
397 Returns saved sizes of the neural network model output feature maps.
398
399 Returns:
400 list: sizes of all output feature maps
401 ") getOfmDims;
402 const std::vector<size_t> &getOfmDims() const;
403
404 %feature("docstring",
405 "
406 Returns total size of all output feature maps.
407
408 Returns:
409 int: total size of all output feature maps
410 ") getOfmSize;
411 size_t getOfmSize() const;
412};
413
414%extend Network {
415 Network(const Device &device, std::shared_ptr<Buffer> &buffer)
416 {
417 if(buffer == nullptr){
418 throw EthosU::Exception(std::string("Failed to create the network, buffer is nullptr.").c_str());
419 }
420 auto network = new EthosU::Network(device, buffer);
421 return network;
422 }
423}
424
425%extend Network {
426 Network(const Device &device, const unsigned int index)
427 {
428 auto network = new EthosU::Network(device, index);
429 return network;
430 }
431}
432
433%feature("docstring",
434 "
435 InferenceStatus enumeration
436 ") InferenceStatus;
437enum class InferenceStatus {
438 OK,
439 ERROR,
440 RUNNING,
441 REJECTED,
442 ABORTED,
443 ABORTING
444 };
445
446%feature("docstring",
447 "
448 Represents the inference file descriptor received from the Ethos-U device.
449
450 `Inference` is created providing `Network` object and lists of input and output feature maps buffers.
451 Feature map buffers are copied.
452
453 Inference creation issues ETHOSU_IOCTL_INFERENCE_CREATE I/O request with
454 file descriptor ids for all input and output buffers.
455
456 The number of input/output buffers must not exceed ETHOSU_FD_MAX value defined in the kernel module
457 uapi/ethosu.h.
458
459 Destruction of the object closes inference file descriptor.
460 ") Inference;
461%nodefaultctor Inference;
462class Inference {
463public:
464
465 %feature("docstring",
466 "
467 Polls inference file descriptor for events.
468
469 Args:
470 timeoutNanos (int64_t): polling timeout in nanoseconds.
471
472 Returns:
473 bool: True for success, False otherwise.
474 ") wait;
475 void wait(int64_t timeoutNanos = -1) const;
476
477 %feature("docstring",
478 "
479 Aborts the current inference job.
480
481 Returns:
482 bool: True if gracefully stopped, False otherwise.
483 ") cancel;
484 bool cancel() const;
485
486 %feature("docstring",
487 "
488 Gets the current inference job status.
489
490 Returns:
491 InferenceStatus.
492 ") status;
493 EthosU::InferenceStatus status() const;
494
495 %feature("docstring",
496 "
497 Returns inference file descriptor.
498
499 Returns:
500 int: file descriptor id
501 ") getFd;
502 int getFd() const;
503
504 %feature("docstring",
505 "
506 Returns associated `Network` object.
507
508 Returns:
509 `Network`: network used during initialisation
510 ") getNetwork;
511 std::shared_ptr<Network> getNetwork() const;
512
513 %feature("docstring",
514 "
515 Returns copied input feature maps buffers.
516
517 Returns:
518 list: input feature map buffers
519 ") getIfmBuffers;
520 std::vector<std::shared_ptr<Buffer>> &getIfmBuffers();
521
522 %feature("docstring",
523 "
524 Returns copied output feature maps buffers.
525
526 Returns:
527 list: output feature map buffers
528 ") getOfmBuffers;
529 std::vector<std::shared_ptr<Buffer>> &getOfmBuffers();
530
531 %feature("docstring",
532 "
533 Returns PMU event data.
534
535 Returns:
536 list: PMU event data
537 ") getPmuCounters;
538 const std::vector<uint32_t> getPmuCounters();
539
540 %feature("docstring",
541 "
542 Returns the total cycle count, including idle cycles, as reported by the PMU.
543
544 Returns:
545 int: total cycle count
546 ") getCycleCounter;
547 uint64_t getCycleCounter();
548
549 %feature("docstring",
550 "
551 Returns maximum supported number of PMU events.
552
553 Returns:
554 int: PMU event max
555 ") getMaxPmuEventCounters;
556 static uint32_t getMaxPmuEventCounters();
557};
558
559%extend Inference {
560 Inference(const std::shared_ptr<Network> &network,
561 const std::vector<std::shared_ptr<Buffer>> &ifm,
562 const std::vector<std::shared_ptr<Buffer>> &ofm)
563 {
564 return new EthosU::Inference(network, ifm.begin(), ifm.end(), ofm.begin(), ofm.end());
565 }
566 Inference(const std::shared_ptr<Network> & network,
567 const std::vector<std::shared_ptr<Buffer>> &ifm,
568 const std::vector<std::shared_ptr<Buffer>> &ofm,
569 const std::vector<unsigned int> &enabledCounters,
570 bool enableCycleCounter)
571 {
572 return new EthosU::Inference(network, ifm.begin(), ifm.end(), ofm.begin(), ofm.end(), enabledCounters, enableCycleCounter);
573 }
574}
575
576}
577// Clear exception typemap.
578%exception;