Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 1 | |
| 2 | // Copyright (c) 2020-2021, ARM Limited. |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | #ifndef _TOSA_NUMPY_UTILS_H |
| 17 | #define _TOSA_NUMPY_UTILS_H |
| 18 | |
| 19 | #include <cassert> |
| 20 | #include <cctype> |
| 21 | #include <cstdint> |
| 22 | #include <cstdio> |
| 23 | #include <cstdlib> |
| 24 | #include <cstring> |
| 25 | #include <vector> |
| 26 | |
James Ward | 485a11d | 2022-08-05 13:48:37 +0100 | [diff] [blame] | 27 | #include "half.hpp" |
| 28 | |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 29 | class NumpyUtilities |
| 30 | { |
| 31 | public: |
| 32 | enum NPError |
| 33 | { |
| 34 | NO_ERROR = 0, |
| 35 | FILE_NOT_FOUND, |
| 36 | FILE_IO_ERROR, |
| 37 | FILE_TYPE_MISMATCH, |
| 38 | HEADER_PARSE_ERROR, |
| 39 | BUFFER_SIZE_MISMATCH, |
| 40 | }; |
| 41 | |
| 42 | static NPError readFromNpyFile(const char* filename, const uint32_t elems, float* databuf); |
| 43 | |
Tai Ly | 3ef34fb | 2023-04-04 20:34:05 +0000 | [diff] [blame] | 44 | static NPError readFromNpyFile(const char* filename, const uint32_t elems, double* databuf); |
| 45 | |
James Ward | 485a11d | 2022-08-05 13:48:37 +0100 | [diff] [blame] | 46 | static NPError readFromNpyFile(const char* filename, const uint32_t elems, half_float::half* databuf); |
| 47 | |
Jerry Ge | c93cb4a | 2023-05-18 21:16:22 +0000 | [diff] [blame] | 48 | static NPError readFromNpyFile(const char* filename, const uint32_t elems, uint8_t* databuf); |
| 49 | |
| 50 | static NPError readFromNpyFile(const char* filename, const uint32_t elems, int8_t* databuf); |
| 51 | |
| 52 | static NPError readFromNpyFile(const char* filename, const uint32_t elems, uint16_t* databuf); |
| 53 | |
| 54 | static NPError readFromNpyFile(const char* filename, const uint32_t elems, int16_t* databuf); |
| 55 | |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 56 | static NPError readFromNpyFile(const char* filename, const uint32_t elems, int32_t* databuf); |
| 57 | |
| 58 | static NPError readFromNpyFile(const char* filename, const uint32_t elems, int64_t* databuf); |
| 59 | |
| 60 | static NPError readFromNpyFile(const char* filename, const uint32_t elems, bool* databuf); |
| 61 | |
| 62 | static NPError writeToNpyFile(const char* filename, const std::vector<int32_t>& shape, const bool* databuf); |
| 63 | |
| 64 | static NPError writeToNpyFile(const char* filename, const uint32_t elems, const bool* databuf); |
| 65 | |
James Ward | 485a11d | 2022-08-05 13:48:37 +0100 | [diff] [blame] | 66 | static NPError |
| 67 | writeToNpyFile(const char* filename, const std::vector<int32_t>& shape, const half_float::half* databuf); |
| 68 | |
Jerry Ge | c93cb4a | 2023-05-18 21:16:22 +0000 | [diff] [blame] | 69 | static NPError writeToNpyFile(const char* filename, const std::vector<int32_t>& shape, const uint8_t* databuf); |
| 70 | |
| 71 | static NPError writeToNpyFile(const char* filename, const uint32_t elems, const uint8_t* databuf); |
| 72 | |
| 73 | static NPError writeToNpyFile(const char* filename, const std::vector<int32_t>& shape, const int8_t* databuf); |
| 74 | |
| 75 | static NPError writeToNpyFile(const char* filename, const uint32_t elems, const int8_t* databuf); |
| 76 | |
| 77 | static NPError writeToNpyFile(const char* filename, const std::vector<int32_t>& shape, const uint16_t* databuf); |
| 78 | |
| 79 | static NPError writeToNpyFile(const char* filename, const uint32_t elems, const uint16_t* databuf); |
| 80 | |
| 81 | static NPError writeToNpyFile(const char* filename, const std::vector<int32_t>& shape, const int16_t* databuf); |
| 82 | |
| 83 | static NPError writeToNpyFile(const char* filename, const uint32_t elems, const int16_t* databuf); |
| 84 | |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 85 | static NPError writeToNpyFile(const char* filename, const std::vector<int32_t>& shape, const int32_t* databuf); |
| 86 | |
| 87 | static NPError writeToNpyFile(const char* filename, const uint32_t elems, const int32_t* databuf); |
| 88 | |
| 89 | static NPError writeToNpyFile(const char* filename, const std::vector<int32_t>& shape, const int64_t* databuf); |
| 90 | |
| 91 | static NPError writeToNpyFile(const char* filename, const uint32_t elems, const int64_t* databuf); |
| 92 | |
| 93 | static NPError writeToNpyFile(const char* filename, const std::vector<int32_t>& shape, const float* databuf); |
| 94 | |
| 95 | static NPError writeToNpyFile(const char* filename, const uint32_t elems, const float* databuf); |
| 96 | |
Tai Ly | 3ef34fb | 2023-04-04 20:34:05 +0000 | [diff] [blame] | 97 | static NPError writeToNpyFile(const char* filename, const std::vector<int32_t>& shape, const double* databuf); |
| 98 | |
| 99 | static NPError writeToNpyFile(const char* filename, const uint32_t elems, const double* databuf); |
| 100 | |
Eric Kunze | 2364dcd | 2021-04-26 11:06:57 -0700 | [diff] [blame] | 101 | private: |
| 102 | static NPError writeToNpyFileCommon(const char* filename, |
| 103 | const char* dtype_str, |
| 104 | const size_t elementsize, |
| 105 | const std::vector<int32_t>& shape, |
| 106 | const void* databuf, |
| 107 | bool bool_translate); |
| 108 | static NPError readFromNpyFileCommon(const char* filename, |
| 109 | const char* dtype_str, |
| 110 | const size_t elementsize, |
| 111 | const uint32_t elems, |
| 112 | void* databuf, |
| 113 | bool bool_translate); |
| 114 | static NPError checkNpyHeader(FILE* infile, const uint32_t elems, const char* dtype_str); |
| 115 | static NPError writeNpyHeader(FILE* outfile, const std::vector<int32_t>& shape, const char* dtype_str); |
| 116 | }; |
| 117 | |
| 118 | #endif // _TOSA_NUMPY_UTILS_H |