blob: 29d7e11661b10b59b588dd0e0c7fdeb1e575b61a [file] [log] [blame]
Eric Kunze2364dcd2021-04-26 11:06:57 -07001
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 Ward485a11d2022-08-05 13:48:37 +010027#include "half.hpp"
28
Eric Kunze2364dcd2021-04-26 11:06:57 -070029class NumpyUtilities
30{
31public:
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 Ly3ef34fb2023-04-04 20:34:05 +000044 static NPError readFromNpyFile(const char* filename, const uint32_t elems, double* databuf);
45
James Ward485a11d2022-08-05 13:48:37 +010046 static NPError readFromNpyFile(const char* filename, const uint32_t elems, half_float::half* databuf);
47
Jerry Gec93cb4a2023-05-18 21:16:22 +000048 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 Kunze2364dcd2021-04-26 11:06:57 -070056 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 Ward485a11d2022-08-05 13:48:37 +010066 static NPError
67 writeToNpyFile(const char* filename, const std::vector<int32_t>& shape, const half_float::half* databuf);
68
Jerry Gec93cb4a2023-05-18 21:16:22 +000069 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 Kunze2364dcd2021-04-26 11:06:57 -070085 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 Ly3ef34fb2023-04-04 20:34:05 +000097 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 Kunze2364dcd2021-04-26 11:06:57 -0700101private:
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