blob: 8c2ed718eb0dc76b409e79b93c1d29da63464f72 [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
Eric Kunze2364dcd2021-04-26 11:06:57 -070048 static NPError readFromNpyFile(const char* filename, const uint32_t elems, int32_t* databuf);
49
50 static NPError readFromNpyFile(const char* filename, const uint32_t elems, int64_t* databuf);
51
52 static NPError readFromNpyFile(const char* filename, const uint32_t elems, bool* databuf);
53
54 static NPError writeToNpyFile(const char* filename, const std::vector<int32_t>& shape, const bool* databuf);
55
56 static NPError writeToNpyFile(const char* filename, const uint32_t elems, const bool* databuf);
57
James Ward485a11d2022-08-05 13:48:37 +010058 static NPError
59 writeToNpyFile(const char* filename, const std::vector<int32_t>& shape, const half_float::half* databuf);
60
Eric Kunze2364dcd2021-04-26 11:06:57 -070061 static NPError writeToNpyFile(const char* filename, const std::vector<int32_t>& shape, const int32_t* databuf);
62
63 static NPError writeToNpyFile(const char* filename, const uint32_t elems, const int32_t* databuf);
64
65 static NPError writeToNpyFile(const char* filename, const std::vector<int32_t>& shape, const int64_t* databuf);
66
67 static NPError writeToNpyFile(const char* filename, const uint32_t elems, const int64_t* databuf);
68
69 static NPError writeToNpyFile(const char* filename, const std::vector<int32_t>& shape, const float* databuf);
70
71 static NPError writeToNpyFile(const char* filename, const uint32_t elems, const float* databuf);
72
Tai Ly3ef34fb2023-04-04 20:34:05 +000073 static NPError writeToNpyFile(const char* filename, const std::vector<int32_t>& shape, const double* databuf);
74
75 static NPError writeToNpyFile(const char* filename, const uint32_t elems, const double* databuf);
76
Eric Kunze2364dcd2021-04-26 11:06:57 -070077private:
78 static NPError writeToNpyFileCommon(const char* filename,
79 const char* dtype_str,
80 const size_t elementsize,
81 const std::vector<int32_t>& shape,
82 const void* databuf,
83 bool bool_translate);
84 static NPError readFromNpyFileCommon(const char* filename,
85 const char* dtype_str,
86 const size_t elementsize,
87 const uint32_t elems,
88 void* databuf,
89 bool bool_translate);
90 static NPError checkNpyHeader(FILE* infile, const uint32_t elems, const char* dtype_str);
91 static NPError writeNpyHeader(FILE* outfile, const std::vector<int32_t>& shape, const char* dtype_str);
92};
93
94#endif // _TOSA_NUMPY_UTILS_H