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