ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Constant.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 __SOUSCHEF_DATA_CONSTANT_H__
18#define __SOUSCHEF_DATA_CONSTANT_H__
19
20#include "souschef/DataChef.h"
22
23#include <stdexcept>
24
25namespace souschef
26{
27
28template <typename T> class ConstantDataChef final : public DataChef
29{
30public:
31 ConstantDataChef(const T &value) : _value{value}
32 {
33 // DO NOTHING
34 }
35
36public:
37 std::vector<uint8_t> generate(int32_t count) const override
38 {
39 std::vector<uint8_t> res;
40
41 for (uint32_t n = 0; n < count; ++n)
42 {
43 const uint8_t *arr = reinterpret_cast<const uint8_t *>(&_value);
44
45 for (uint32_t b = 0; b < sizeof(T); ++b)
46 {
47 res.emplace_back(arr[b]);
48 }
49 }
50
51 return res;
52 }
53
54private:
55 T _value;
56};
57
58template <typename T> struct ConstantDataChefFactory : public DataChefFactory
59{
60 std::unique_ptr<DataChef> create(const Arguments &args) const
61 {
62 auto const value = to_number<T>(args.value(0));
63 return std::unique_ptr<DataChef>{new ConstantDataChef<T>{value}};
64 }
65};
66
67class ConstantInt4DataChef final : public DataChef
68{
69public:
70 ConstantInt4DataChef(const int8_t &value) : _value{value}
71 {
72 // DO NOTHING
73 }
74
75public:
76 // int4 constant is saved as int8 (extra 4 bits are filled with sign bits).
77 // Callers must cast each element to int8 before using it.
78 // Example)
79 // ConstInt4DataChef chef(-5)
80 // auto values = chef.generate(3);
81 // for (uint8_t value: values) {
82 // int8_t real_value = static_cast<int8_t>(value);
83 // assert(value == 251 and real_value == -5);
84 std::vector<uint8_t> generate(int32_t count) const override
85 {
86 std::vector<uint8_t> res;
87
88 if (_value < -8 || 7 < _value)
89 throw std::runtime_error("Constant value out of range.");
90
91 for (uint32_t n = 0; n < count; ++n)
92 {
93 const uint8_t data = static_cast<const uint8_t>(_value);
94 res.emplace_back(data);
95 }
96
97 return res;
98 }
99
100private:
101 int8_t _value;
102};
103
105{
106 std::unique_ptr<DataChef> create(const Arguments &args) const
107 {
108 auto const value = to_number<int8_t>(args.value(0));
109 return std::unique_ptr<DataChef>{new ConstantInt4DataChef{value}};
110 }
111};
112
113class ConstantUint4DataChef final : public DataChef
114{
115public:
116 ConstantUint4DataChef(const uint8_t &value) : _value{value}
117 {
118 // DO NOTHING
119 }
120
121public:
122 std::vector<uint8_t> generate(int32_t count) const override
123 {
124 std::vector<uint8_t> res;
125
126 if (15 < _value)
127 throw std::runtime_error("Constant value out of range.");
128
129 for (uint32_t n = 0; n < count; ++n)
130 {
131 res.emplace_back(_value);
132 }
133
134 return res;
135 }
136
137private:
138 uint8_t _value;
139};
140
142{
143 std::unique_ptr<DataChef> create(const Arguments &args) const
144 {
145 auto const value = to_number<uint8_t>(args.value(0));
146 return std::unique_ptr<DataChef>{new ConstantUint4DataChef{value}};
147 }
148};
149
150} // namespace souschef
151
152#endif // __SOUSCHEF_DATA_CONSTANT_H__
std::vector< uint8_t > generate(int32_t count) const override
Generate a sequence of 'count' elements as a byte sequence.
Definition Constant.h:37
ConstantDataChef(const T &value)
Definition Constant.h:31
ConstantInt4DataChef(const int8_t &value)
Definition Constant.h:70
std::vector< uint8_t > generate(int32_t count) const override
Generate a sequence of 'count' elements as a byte sequence.
Definition Constant.h:84
std::vector< uint8_t > generate(int32_t count) const override
Generate a sequence of 'count' elements as a byte sequence.
Definition Constant.h:122
ConstantUint4DataChef(const uint8_t &value)
Definition Constant.h:116
This file provides string <-> number cast helpers.
Definition Arguments.h:24
Read-only string sequence view.
Definition Arguments.h:30
std::unique_ptr< DataChef > create(const Arguments &args) const
Definition Constant.h:60
std::unique_ptr< DataChef > create(const Arguments &args) const
Definition Constant.h:106
std::unique_ptr< DataChef > create(const Arguments &args) const
Definition Constant.h:143
Data Generator Factory.
Definition DataChef.h:53
Data Generator.
Definition DataChef.h:35