ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Utils.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 Samsung Electronics Co., Ltd. All Rights Reserved
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
17#include "Utils.h"
18
19#include <luci/IR/CircleNodes.h>
21
22#include <vector>
23#include <string>
24#include <fstream>
25
26namespace record_minmax
27{
28
30{
31 assert(input); // FIX_CALLER_UNLESS
32
33 for (uint32_t i = 0; i < input->rank(); i++)
34 if (!input->dim(i).known())
35 throw std::runtime_error(input->name() + " has unknown dimension");
36
37 if (numElements(input) == 0)
38 throw std::runtime_error(input->name() + " is a zero-sized input");
39}
40
41uint32_t numElements(const luci::CircleNode *node)
42{
43 assert(node); // FIX_CALLER_UNLESS
44
45 uint32_t num_elements = 1;
46 for (uint32_t i = 0; i < node->rank(); i++)
47 {
48 if (not node->dim(i).known())
49 throw std::runtime_error("Unknown dimension found in " + node->name());
50
51 num_elements *= node->dim(i).value();
52 }
53
54 return num_elements;
55}
56
58{
59 assert(node); // FIX_CALLER_UNLESS
60
61 uint32_t elem_size = luci::size(node->dtype());
62 return numElements(node) * elem_size;
63}
64
65void readDataFromFile(const std::string &filename, std::vector<char> &data, size_t data_size)
66{
67 assert(data.size() == data_size); // FIX_CALLER_UNLESS
68
69 std::ifstream fs(filename, std::ifstream::binary);
70 if (fs.fail())
71 throw std::runtime_error("Cannot open file \"" + filename + "\".\n");
72 if (fs.read(data.data(), data_size).fail())
73 throw std::runtime_error("Failed to read data from file \"" + filename + "\".\n");
74 if (fs.peek() != EOF)
75 throw std::runtime_error("Input tensor size mismatches with \"" + filename + "\".\n");
76}
77
78} // namespace record_minmax
CircleNode used for Input of the Graph.
Definition CircleInput.h:36
uint32_t size(loco::DataType data_type)
Returns the size of the data type.
void checkInputDimension(const luci::CircleInput *input)
Definition Utils.cpp:29
size_t getTensorSize(const luci::CircleNode *node)
Definition Utils.cpp:57
void readDataFromFile(const std::string &filename, std::vector< char > &data, size_t data_size)
Definition Utils.cpp:65
uint32_t numElements(const luci::CircleNode *node)
Definition Utils.cpp:41
NodeName name(void) const