| 326 | // Please see write_header() in the above file for more details. |
| 327 | template<typename T> |
| 328 | inline void WriteToNumpyFile(const std::string& outputTensorFileName, |
| 329 | const T* const array, |
| 330 | const unsigned int numElements, |
| 331 | armnn::DataType dataType, |
| 332 | const armnn::TensorShape& shape) |
| 333 | { |
| 334 | std::ofstream out(outputTensorFileName, std::ofstream::binary); |
| 335 | |
| 336 | // write header |
| 337 | { |
| 338 | // Setup string of tensor shape in format (x0, x1, x2, ..) |
| 339 | std::string shapeStr = "("; |
| 340 | for (uint32_t i = 0; i < shape.GetNumDimensions()-1; i++) |
| 341 | { |
| 342 | shapeStr = shapeStr + std::to_string(shape[i]) + ", "; |
| 343 | } |
| 344 | shapeStr = shapeStr + std::to_string(shape[shape.GetNumDimensions()-1]) + ")"; |
| 345 | |
| 346 | int i = 1; |
| 347 | std::string endianChar = (*(reinterpret_cast<char *>(&i))) ? "<" : ">"; |
| 348 | std::string dataTypeStr = getNumpyDescr(dataType); |
| 349 | std::string fortranOrder = "False"; |
| 350 | std::string headerStr = "{'descr': '" + endianChar + dataTypeStr + |
| 351 | "', 'fortran_order': " + fortranOrder + |
| 352 | ", 'shape': " + shapeStr + ", }"; |
| 353 | |
| 354 | armnnNumpy::HeaderInfo headerInfo; |
| 355 | |
| 356 | // Header is composed of: |
| 357 | // - 6 byte magic string |
| 358 | // - 2 byte major and minor version |
| 359 | // - 2 byte (v1.0) / 4 byte (v2.0) little-endian unsigned int |
| 360 | // - headerStr.length() bytes |
| 361 | // - 1 byte for newline termination (\n) |
| 362 | size_t length = headerInfo.m_MagicStringLength + 2 + 2 + headerStr.length() + 1; |
| 363 | uint8_t major_version = 1; |
| 364 | |
| 365 | // for numpy major version 2, add extra 2 bytes for little-endian int (total 4 bytes) |
| 366 | if (length >= 255 * 255) |
| 367 | { |
| 368 | length += 2; |
| 369 | major_version = 2; |
| 370 | } |
| 371 | |
| 372 | // Pad with spaces so header length is modulo 16 bytes. |
| 373 | size_t padding_length = 16 - length % 16; |
| 374 | std::string padding(padding_length, ' '); |
| 375 | |
| 376 | // write magic string |
| 377 | out.write(headerInfo.m_MagicString, headerInfo.m_MagicStringLength); |
| 378 | out.put(static_cast<char>(major_version)); |
| 379 | out.put(0); // minor version |
| 380 | |
| 381 | // write header length |
| 382 | if (major_version == 1) |
| 383 | { |
| 384 | auto header_len = static_cast<uint16_t>(headerStr.length() + padding.length() + 1); |
| 385 |
no test coverage detected