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