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 | e9c3f07 | 2023-06-12 15:58:10 +0200 | [diff] [blame] | 41 | constexpr uint32_t DRIVER_LIBRARY_VERSION_MAJOR; |
| 42 | constexpr uint32_t DRIVER_LIBRARY_VERSION_MINOR; |
| 43 | constexpr uint32_t DRIVER_LIBRARY_VERSION_PATCH; |
| 44 | |
Mikael Olsson | 308e7f1 | 2023-06-12 15:00:55 +0200 | [diff] [blame] | 45 | constexpr uint32_t MAX_SUPPORTED_KERNEL_DRIVER_MAJOR_VERSION; |
| 46 | constexpr uint32_t MIN_SUPPORTED_KERNEL_DRIVER_MAJOR_VERSION; |
| 47 | |
Kshitij Sisodia | f9efe0d | 2022-09-30 16:42:50 +0100 | [diff] [blame] | 48 | %feature("docstring", |
| 49 | " |
| 50 | Semantic Version : major.minor.patch |
| 51 | ") SemanticVersion; |
| 52 | %nodefaultctor SemanticVersion; |
| 53 | class SemanticVersion { |
| 54 | public: |
| 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 Olsson | e9c3f07 | 2023-06-12 15:58:10 +0200 | [diff] [blame] | 72 | Return driver library version information. |
| 73 | |
| 74 | Returns: |
| 75 | SemanticVersion: driver library version. |
| 76 | ") getLibraryVersion; |
| 77 | const SemanticVersion getLibraryVersion(); |
| 78 | |
| 79 | %feature("docstring", |
| 80 | " |
Kshitij Sisodia | f9efe0d | 2022-09-30 16:42:50 +0100 | [diff] [blame] | 81 | Hardware Identifier which consists of version status, version revision, product revision and architecture revision. |
| 82 | ") HardwareId; |
| 83 | class HardwareId { |
| 84 | public: |
| 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 | " |
| 106 | Hardware Configuration object defines specific configuration including MACs per clock cycle and NPU command stream |
| 107 | version. This also specifies is custom DMA is enabled or not. |
| 108 | ") HardwareConfiguration; |
| 109 | %nodefaultctor HardwareConfiguration; |
| 110 | class 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 | " |
| 131 | Device capabilities object which specifies capabilities based on hardware ID, configuration and semantic version. |
| 132 | ") Capabilities; |
| 133 | class 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 | " |
| 155 | Device object represents Ethos-U device file descriptor and manages Ethos-U device lifecycle. |
| 156 | Constructor accepts device name and opens file descriptor with O_RDWR | O_NONBLOCK flags. |
| 157 | When the object is destroyed - device file descriptor is closed. |
| 158 | ") Device; |
| 159 | %nodefaultctor Device; |
| 160 | class Device { |
| 161 | public: |
| 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 Olsson | 308e7f1 | 2023-06-12 15:00:55 +0200 | [diff] [blame] | 184 | |
| 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 Sisodia | f9efe0d | 2022-09-30 16:42:50 +0100 | [diff] [blame] | 193 | }; |
| 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. |
Mikael Olsson | 0754515 | 2023-10-17 13:05:38 +0200 | [diff] [blame] | 213 | Issues ETHOSU_IOCTL_BUFFER_CREATE I/O request to the device with given size. |
Kshitij Sisodia | f9efe0d | 2022-09-30 16:42:50 +0100 | [diff] [blame] | 214 | |
Mikael Olsson | 0754515 | 2023-10-17 13:05:38 +0200 | [diff] [blame] | 215 | Buffer could be created for a device with given size or instantiated directly from |
Kshitij Sisodia | f9efe0d | 2022-09-30 16:42:50 +0100 | [diff] [blame] | 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') |
Mikael Olsson | 0754515 | 2023-10-17 13:05:38 +0200 | [diff] [blame] | 222 | >>> # Empty, with size: |
Kshitij Sisodia | f9efe0d | 2022-09-30 16:42:50 +0100 | [diff] [blame] | 223 | >>> buf = driver.Buffer(device, 1024) |
| 224 | ") Buffer; |
| 225 | %nodefaultctor Buffer; |
| 226 | class Buffer { |
| 227 | public: |
Mikael Olsson | 0754515 | 2023-10-17 13:05:38 +0200 | [diff] [blame] | 228 | Buffer(const Device &device, const size_t size); |
Kshitij Sisodia | f9efe0d | 2022-09-30 16:42:50 +0100 | [diff] [blame] | 229 | |
| 230 | %feature("docstring", |
| 231 | " |
| 232 | Sets the size of the device buffer to 0. |
| 233 | ") clear; |
| 234 | void clear() const; |
| 235 | |
| 236 | %feature("docstring", |
| 237 | " |
| 238 | Returns a readonly view to the mapped memory. |
| 239 | |
| 240 | Returns: |
| 241 | memoryview: readonly memory data. |
| 242 | ") data; |
| 243 | %driver_buffer_out; |
| 244 | char* data() const; |
| 245 | %clear_driver_buffer_out; |
| 246 | |
| 247 | %feature("docstring", |
| 248 | " |
Kshitij Sisodia | f9efe0d | 2022-09-30 16:42:50 +0100 | [diff] [blame] | 249 | Queries device and returns buffer data size. |
| 250 | |
| 251 | Issues ETHOSU_IOCTL_BUFFER_GET I/O request. |
| 252 | |
| 253 | Returns: |
| 254 | int: current device buffer size. |
| 255 | ") size; |
| 256 | size_t size() const; |
| 257 | |
| 258 | %feature("docstring", |
| 259 | " |
| 260 | Returns buffer file descriptor id. |
| 261 | |
| 262 | Returns: |
| 263 | int: file descriptor id. |
| 264 | ") getFd; |
| 265 | int getFd() const; |
| 266 | }; |
| 267 | |
| 268 | %extend Buffer { |
| 269 | |
| 270 | Buffer(const Device& device, const std::string& filename) { |
| 271 | std::ifstream stream(filename, std::ios::binary); |
| 272 | if (!stream.is_open()) { |
| 273 | throw EthosU::Exception(std::string("Failed to open file: ").append(filename).c_str()); |
| 274 | } |
| 275 | |
| 276 | stream.seekg(0, std::ios_base::end); |
| 277 | size_t size = stream.tellg(); |
| 278 | stream.seekg(0, std::ios_base::beg); |
| 279 | |
| 280 | auto buffer = new EthosU::Buffer(device, size); |
Kshitij Sisodia | f9efe0d | 2022-09-30 16:42:50 +0100 | [diff] [blame] | 281 | stream.read(buffer->data(), size); |
| 282 | |
| 283 | return buffer; |
| 284 | } |
| 285 | |
| 286 | %feature("docstring", |
| 287 | " |
| 288 | Fills the buffer from python buffer. |
| 289 | |
| 290 | Copies python buffer data to the mapped memory region. |
Kshitij Sisodia | f9efe0d | 2022-09-30 16:42:50 +0100 | [diff] [blame] | 291 | |
| 292 | Args: |
| 293 | buffer: data to be copied to the mapped memory. |
| 294 | |
| 295 | ") from_buffer; |
| 296 | %mutable_buffer(char* buffer, size_t size); |
| 297 | void from_buffer(char* buffer, size_t size) { |
Kshitij Sisodia | f9efe0d | 2022-09-30 16:42:50 +0100 | [diff] [blame] | 298 | char* data = $self->data(); |
| 299 | std::memcpy(data, buffer, size); |
| 300 | } |
| 301 | %clear_mutable_buffer(char* buffer, size_t size); |
| 302 | } |
| 303 | |
| 304 | %feature("docstring", |
| 305 | " |
| 306 | Represents the neural network file descriptor received from the Ethos-U device. |
| 307 | |
| 308 | `Network` is created providing `Device` object and a `Buffer` containing tflite file data. |
| 309 | Network creation issues ETHOSU_IOCTL_NETWORK_CREATE I/O request with buffer file descriptor id. |
| 310 | Provided `Buffer` data is parsed into tflite Model object and input/output feature maps sizes are saved. |
| 311 | |
| 312 | Destruction of the object closes network file descriptor. |
| 313 | ") Network; |
| 314 | %nodefaultctor Network; |
| 315 | class Network { |
| 316 | public: |
| 317 | |
| 318 | %feature("docstring", |
| 319 | " |
| 320 | Performs the I/O control operation with network buffer device. |
| 321 | |
| 322 | Args: |
| 323 | cmd: Command code |
| 324 | data: Command data |
| 325 | Returns: |
| 326 | int: Return value depends on command. Usually -1 indicates error. |
| 327 | ") ioctl; |
| 328 | int ioctl(unsigned long cmd, void *data); |
| 329 | |
| 330 | %feature("docstring", |
| 331 | " |
| 332 | Returns associated memory buffer. |
| 333 | |
| 334 | Returns: |
| 335 | `Buffer`: buffer object used during initialisation. |
| 336 | ") getBuffer; |
| 337 | std::shared_ptr<Buffer> getBuffer(); |
| 338 | |
| 339 | %feature("docstring", |
| 340 | " |
| 341 | Returns saved sizes of the neural network model input feature maps. |
| 342 | |
| 343 | Returns: |
| 344 | list: sizes of all input feature maps |
| 345 | ") getIfmDims; |
| 346 | const std::vector<size_t> &getIfmDims() const; |
| 347 | |
| 348 | %feature("docstring", |
| 349 | " |
| 350 | Returns total size of all input feature maps. |
| 351 | |
| 352 | Returns: |
| 353 | int: total size of all input feature maps |
| 354 | ") getIfmSize; |
| 355 | size_t getIfmSize() const; |
| 356 | |
| 357 | %feature("docstring", |
| 358 | " |
| 359 | Returns saved sizes of the neural network model output feature maps. |
| 360 | |
| 361 | Returns: |
| 362 | list: sizes of all output feature maps |
| 363 | ") getOfmDims; |
| 364 | const std::vector<size_t> &getOfmDims() const; |
| 365 | |
| 366 | %feature("docstring", |
| 367 | " |
| 368 | Returns total size of all output feature maps. |
| 369 | |
| 370 | Returns: |
| 371 | int: total size of all output feature maps |
| 372 | ") getOfmSize; |
| 373 | size_t getOfmSize() const; |
| 374 | }; |
| 375 | |
| 376 | %extend Network { |
| 377 | Network(const Device &device, std::shared_ptr<Buffer> &buffer) |
| 378 | { |
| 379 | if(buffer == nullptr){ |
| 380 | throw EthosU::Exception(std::string("Failed to create the network, buffer is nullptr.").c_str()); |
| 381 | } |
| 382 | auto network = new EthosU::Network(device, buffer); |
| 383 | return network; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | %extend Network { |
| 388 | Network(const Device &device, const unsigned int index) |
| 389 | { |
| 390 | auto network = new EthosU::Network(device, index); |
| 391 | return network; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | %feature("docstring", |
| 396 | " |
| 397 | InferenceStatus enumeration |
| 398 | ") InferenceStatus; |
| 399 | enum class InferenceStatus { |
| 400 | OK, |
| 401 | ERROR, |
| 402 | RUNNING, |
| 403 | REJECTED, |
| 404 | ABORTED, |
| 405 | ABORTING |
| 406 | }; |
| 407 | |
| 408 | %feature("docstring", |
| 409 | " |
| 410 | Represents the inference file descriptor received from the Ethos-U device. |
| 411 | |
| 412 | `Inference` is created providing `Network` object and lists of input and output feature maps buffers. |
| 413 | Feature map buffers are copied. |
| 414 | |
| 415 | Inference creation issues ETHOSU_IOCTL_INFERENCE_CREATE I/O request with |
| 416 | file descriptor ids for all input and output buffers. |
| 417 | |
| 418 | The number of input/output buffers must not exceed ETHOSU_FD_MAX value defined in the kernel module |
| 419 | uapi/ethosu.h. |
| 420 | |
| 421 | Destruction of the object closes inference file descriptor. |
| 422 | ") Inference; |
| 423 | %nodefaultctor Inference; |
| 424 | class Inference { |
| 425 | public: |
| 426 | |
| 427 | %feature("docstring", |
| 428 | " |
| 429 | Polls inference file descriptor for events. |
| 430 | |
| 431 | Args: |
| 432 | timeoutNanos (int64_t): polling timeout in nanoseconds. |
| 433 | |
| 434 | Returns: |
| 435 | bool: True for success, False otherwise. |
| 436 | ") wait; |
| 437 | void wait(int64_t timeoutNanos = -1) const; |
| 438 | |
| 439 | %feature("docstring", |
| 440 | " |
| 441 | Aborts the current inference job. |
| 442 | |
| 443 | Returns: |
| 444 | bool: True if gracefully stopped, False otherwise. |
| 445 | ") cancel; |
| 446 | bool cancel() const; |
| 447 | |
| 448 | %feature("docstring", |
| 449 | " |
| 450 | Gets the current inference job status. |
| 451 | |
| 452 | Returns: |
| 453 | InferenceStatus. |
| 454 | ") status; |
| 455 | EthosU::InferenceStatus status() const; |
| 456 | |
| 457 | %feature("docstring", |
| 458 | " |
| 459 | Returns inference file descriptor. |
| 460 | |
| 461 | Returns: |
| 462 | int: file descriptor id |
| 463 | ") getFd; |
| 464 | int getFd() const; |
| 465 | |
| 466 | %feature("docstring", |
| 467 | " |
| 468 | Returns associated `Network` object. |
| 469 | |
| 470 | Returns: |
| 471 | `Network`: network used during initialisation |
| 472 | ") getNetwork; |
| 473 | std::shared_ptr<Network> getNetwork() const; |
| 474 | |
| 475 | %feature("docstring", |
| 476 | " |
| 477 | Returns copied input feature maps buffers. |
| 478 | |
| 479 | Returns: |
| 480 | list: input feature map buffers |
| 481 | ") getIfmBuffers; |
| 482 | std::vector<std::shared_ptr<Buffer>> &getIfmBuffers(); |
| 483 | |
| 484 | %feature("docstring", |
| 485 | " |
| 486 | Returns copied output feature maps buffers. |
| 487 | |
| 488 | Returns: |
| 489 | list: output feature map buffers |
| 490 | ") getOfmBuffers; |
| 491 | std::vector<std::shared_ptr<Buffer>> &getOfmBuffers(); |
| 492 | |
| 493 | %feature("docstring", |
| 494 | " |
| 495 | Returns PMU event data. |
| 496 | |
| 497 | Returns: |
| 498 | list: PMU event data |
| 499 | ") getPmuCounters; |
| 500 | const std::vector<uint32_t> getPmuCounters(); |
| 501 | |
| 502 | %feature("docstring", |
| 503 | " |
| 504 | Returns the total cycle count, including idle cycles, as reported by the PMU. |
| 505 | |
| 506 | Returns: |
| 507 | int: total cycle count |
| 508 | ") getCycleCounter; |
| 509 | uint64_t getCycleCounter(); |
| 510 | |
| 511 | %feature("docstring", |
| 512 | " |
| 513 | Returns maximum supported number of PMU events. |
| 514 | |
| 515 | Returns: |
| 516 | int: PMU event max |
| 517 | ") getMaxPmuEventCounters; |
| 518 | static uint32_t getMaxPmuEventCounters(); |
| 519 | }; |
| 520 | |
| 521 | %extend Inference { |
| 522 | Inference(const std::shared_ptr<Network> &network, |
| 523 | const std::vector<std::shared_ptr<Buffer>> &ifm, |
| 524 | const std::vector<std::shared_ptr<Buffer>> &ofm) |
| 525 | { |
| 526 | return new EthosU::Inference(network, ifm.begin(), ifm.end(), ofm.begin(), ofm.end()); |
| 527 | } |
| 528 | Inference(const std::shared_ptr<Network> & network, |
| 529 | const std::vector<std::shared_ptr<Buffer>> &ifm, |
| 530 | const std::vector<std::shared_ptr<Buffer>> &ofm, |
| 531 | const std::vector<unsigned int> &enabledCounters, |
| 532 | bool enableCycleCounter) |
| 533 | { |
| 534 | return new EthosU::Inference(network, ifm.begin(), ifm.end(), ofm.begin(), ofm.end(), enabledCounters, enableCycleCounter); |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | } |
| 539 | // Clear exception typemap. |
| 540 | %exception; |