ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Common.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 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#ifndef _NNC_CORE_BACKEND_INTERPRETER_COMMON_
18#define _NNC_CORE_BACKEND_INTERPRETER_COMMON_
19
20#include "mir/Tensor.h"
21#include "mir/TensorVariant.h"
22#include "mir/DataType.h"
23#include "mir/Shape.h"
24#include "mir/Index.h"
25
26namespace mir_interpreter
27{
28
29template <template <typename> class F, typename... Args>
30void dispatch(mir::DataType dt, Args &&...args)
31{
32 switch (dt)
33 {
34 case mir::DataType::FLOAT32:
35 return F<float>::run(std::forward<Args>(args)...);
36 case mir::DataType::FLOAT64:
37 return F<double>::run(std::forward<Args>(args)...);
38 case mir::DataType::INT32:
39 return F<int32_t>::run(std::forward<Args>(args)...);
40 case mir::DataType::INT64:
41 return F<int64_t>::run(std::forward<Args>(args)...);
42 case mir::DataType::UINT8:
43 return F<uint8_t>::run(std::forward<Args>(args)...);
44 case mir::DataType::UNKNOWN:
45 throw std::runtime_error{"Unknown datatype met during operation execution"};
46 default:
47 throw std::runtime_error{"mir::DataType enum mismatch"};
48 }
49}
50
51template <typename T> void erase(mir::TensorVariant &tv)
52{
53 size_t element_count = tv.getShape().numElements();
54 for (size_t i = 0; i < element_count; ++i)
55 {
56 auto ptr = tv.atOffset(i);
57 *reinterpret_cast<T *>(ptr) = 0;
58 }
59}
60
61mir::Index shift(const mir::Index &in_index, const mir::Shape &shift_from);
62
63} // namespace mir_interpreter
64
65#endif // _NNC_CORE_BACKEND_INTERPRETER_COMMON_
int32_t numElements() const
Definition Shape.cpp:30
char * atOffset(int32_t offset) const
const Shape & getShape() const
void erase(mir::TensorVariant &tv)
Definition Common.h:51
Index shift(const Index &in_index, const Shape &shift_from)
Definition Common.cpp:26
void dispatch(mir::DataType dt, Args &&...args)
Definition Common.h:30
DataType
Definition DataType.h:27