blob: 12f831e81e5ea5dfb56a221b0f33616ff2812e70 [file] [log] [blame]
Matthew Sloyanba5fad32022-09-26 13:31:43 +01001
2// Copyright (c) 2022, 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 GENERAL_UTILS_H_
17#define GENERAL_UTILS_H_
18
19#include "func_debug.h"
20
21#include "numpy_utils.h"
22
23namespace TosaReference
24{
25
26const uint32_t getElementCount(std::vector<int32_t>& shape)
27{
28 uint32_t elements = 1;
29 for (size_t i = 0; i < shape.size(); i++)
30 {
31 elements *= shape[i];
32 }
33
34 return elements;
35}
36
37template <typename T>
38std::vector<T> readFromNpyFile(const char* filename, std::vector<int32_t>& shape)
39{
40 uint32_t elements = getElementCount(shape);
41 std::vector<T> data(elements, 0);
42
43 NumpyUtilities::NPError nperror = NumpyUtilities::readFromNpyFile(filename, elements, data.data());
44
45 switch (nperror)
46 {
47 case NumpyUtilities::NO_ERROR:
48 break;
49 case NumpyUtilities::FILE_NOT_FOUND:
50 FATAL_ERROR("readFromNpyFile: Cannot open file %s", filename);
51 case NumpyUtilities::FILE_IO_ERROR:
52 FATAL_ERROR("readFromNpyFile: IO error reading file: %s", filename);
53 case NumpyUtilities::FILE_TYPE_MISMATCH:
54 FATAL_ERROR("readFromNpyFile: Tensor type and Numpy file type mismatch for filename %s", filename);
55 case NumpyUtilities::HEADER_PARSE_ERROR:
56 FATAL_ERROR("Numpy header parsing error for file: %s", filename);
57 case NumpyUtilities::BUFFER_SIZE_MISMATCH:
58 FATAL_ERROR("Buffer size does not match numpy file size for filename %s", filename);
59 default:
60 FATAL_ERROR("Unknown error parsing Numpy file: %s", filename);
61 }
62
63 return data;
64}
65
66}; // namespace TosaReference
67
68#endif