Line data Source code
1 : // SPDX-License-Identifier: Apache-2.0
2 : /**
3 : * @file char_tensor.h
4 : * @date 02 April 2024
5 : * @brief This is CharTensor class for 8-bit integer calculation
6 : * @see https://github.com/nnstreamer/nntrainer
7 : * @author Donghyeon Jeong <dhyeon.jeong@samsung.com>
8 : * @bug No known bugs except for NYI items
9 : */
10 :
11 : #ifndef __CHAR_TENSOR_H__
12 : #define __CHAR_TENSOR_H__
13 : #ifdef __cplusplus
14 :
15 : #include <quantizer.h>
16 : #include <tensor_base.h>
17 :
18 : namespace nntrainer {
19 :
20 : /**
21 : * @class CharTensor class
22 : * @brief CharTensor class for 8-bit integer calculation
23 : */
24 : class CharTensor : public TensorBase {
25 : public:
26 : /**
27 : * @brief Basic Constructor of Tensor
28 : */
29 : CharTensor(std::string name_ = "", Tformat fm = Tformat::NCHW,
30 : QScheme qscheme_ = QScheme::PER_TENSOR_AFFINE);
31 :
32 : /**
33 : * @brief Construct a new CharTensor object
34 : *
35 : * @param d Tensor dim for this float tensor
36 : * @param alloc_now Allocate memory to this tensor or not
37 : * @param init Initializer for the tensor
38 : * @param name Name of the tensor
39 : * @param qscheme_ Quantization scheme of the tensor
40 : */
41 : CharTensor(const TensorDim &d, bool alloc_now,
42 : Initializer init = Initializer::NONE, std::string name = "",
43 : QScheme qscheme_ = QScheme::PER_TENSOR_AFFINE);
44 :
45 : /**
46 : * @brief Construct a new CharTensor object
47 : *
48 : * @param d Tensor dim for this tensor
49 : * @param buf buffer
50 : * @param qscheme_ quantization scheme of the tensor
51 : */
52 : CharTensor(const TensorDim &d, const void *buf = nullptr,
53 : QScheme qscheme_ = QScheme::PER_TENSOR_AFFINE);
54 :
55 : /**
56 : * @brief Construct a new CharTensor object
57 : *
58 : * @param d data for the Tensor
59 : * @param scales scale factors for the Tensor
60 : * @param fm format for the Tensor
61 : * @param qscheme_ quantization scheme of the tensor
62 : */
63 : CharTensor(
64 : std::vector<std::vector<std::vector<std::vector<int8_t>>>> const &d,
65 : std::vector<float> const &scales, Tformat fm, QScheme qscheme_);
66 :
67 : /**
68 : * @brief Construct a new CharTensor object
69 : * @param rhs TensorBase object to copy
70 : */
71 3 : CharTensor(TensorBase &rhs) :
72 3 : TensorBase(rhs), qscheme(QScheme::PER_TENSOR_AFFINE) {}
73 :
74 : /**
75 : * @brief Basic Destructor
76 : */
77 62 : ~CharTensor() {}
78 :
79 : /**
80 : * @brief Comparison operator overload
81 : * @param[in] rhs Tensor to be compared with
82 : * @note Only compares Tensor data
83 : */
84 : bool operator==(const CharTensor &rhs) const;
85 :
86 : /**
87 : * @brief Comparison operator overload
88 : * @param[in] rhs Tensor to be compared with
89 : * @note Only compares Tensor data
90 : */
91 : bool operator!=(const CharTensor &rhs) const { return !(*this == rhs); }
92 :
93 : /**
94 : * @copydoc Tensor::allocate()
95 : */
96 : void allocate() override;
97 :
98 : /**
99 : * @copydoc Tensor::deallocate()
100 : */
101 : void deallocate() override;
102 :
103 : /**
104 : * @copydoc Tensor::getData()
105 : */
106 : void *getData() const override;
107 :
108 : /**
109 : * @copydoc Tensor::getData(size_t idx)
110 : */
111 : void *getData(size_t idx) const override;
112 :
113 : /**
114 : * @copydoc Tensor::getScale()
115 : */
116 : void *getScale() const override;
117 :
118 : /**
119 : * @copydoc Tensor::getScale(size_t idx)
120 : */
121 : void *getScale(size_t idx) const override;
122 :
123 : /**
124 : * @brief i data index
125 : * @retval address of ith data
126 : */
127 : void *getAddress(unsigned int i) override;
128 :
129 : /**
130 : * @brief i data index
131 : * @retval address of ith data
132 : */
133 : const void *getAddress(unsigned int i) const override;
134 :
135 : /**
136 : * @brief return value at specific location
137 : * @param[in] i index
138 : */
139 : const int8_t &getValue(unsigned int i) const;
140 :
141 : /**
142 : * @brief return value at specific location
143 : * @param[in] i index
144 : */
145 : int8_t &getValue(unsigned int i);
146 :
147 : /**
148 : * @brief return value at specific location
149 : * @param[in] b batch location
150 : * @param[in] c channel location
151 : * @param[in] h height location
152 : * @param[in] w width location
153 : */
154 : const int8_t &getValue(unsigned int b, unsigned int c, unsigned int h,
155 : unsigned int w) const;
156 :
157 : /**
158 : * @brief return value at specific location
159 : * @param[in] b batch location
160 : * @param[in] c channel location
161 : * @param[in] h height location
162 : * @param[in] w width location
163 : */
164 : int8_t &getValue(unsigned int b, unsigned int c, unsigned int h,
165 : unsigned int w);
166 :
167 : /**
168 : * @copydoc Tensor::setValue(float value)
169 : */
170 : void setValue(float value) override;
171 :
172 : /**
173 : * @copydoc Tensor::setValue(b, c, h, w, value)
174 : */
175 : void setValue(unsigned int b, unsigned int c, unsigned int h, unsigned int w,
176 : float value) override;
177 :
178 : /**
179 : * @copydoc Tensor::addValue(b, c, h, w, value, beta)
180 : */
181 : void addValue(unsigned int b, unsigned int c, unsigned int h, unsigned int w,
182 : float value, float beta) override;
183 :
184 : /**
185 : * @copydoc Tensor::setZero()
186 : */
187 : void setZero() override;
188 :
189 : /**
190 : * @copydoc Tensor::initialize()
191 : */
192 : void initialize() override;
193 :
194 : /**
195 : * @copydoc Tensor::initialize(Initializer init)
196 : */
197 : void initialize(Initializer init) override;
198 :
199 : /**
200 : * @copydoc Tensor::multiply_i(float const &value)
201 : */
202 : int multiply_i(float const &value) override;
203 :
204 : /**
205 : * @copydoc Tensor::multiply(Tensor const &m, Tensor &output, const
206 : * float scale = 0.0)
207 : *
208 : * @note multiply only works under the following conditions.
209 : * 1. appropriate scale must be provided (feature to automatically determine
210 : * the scale factor will be added in the future update.)
211 : * 2. should have same data type QINT8.
212 : * 3. should have same size (broadcasting is currently not supported)
213 : * 4. only per-tensor quantization qscheme is supported
214 : */
215 : Tensor &multiply(Tensor const &m, Tensor &output,
216 : const float scale = 0.0) const override;
217 : /**
218 : * @copydoc Tensor::add(Tensor const &m, Tensor &output, float const
219 : * alpha)
220 : */
221 : Tensor &add(Tensor const &m, Tensor &output,
222 : float const scale) const override;
223 :
224 : /**
225 : * @copydoc Tensor::copy(const Tensor &from)
226 : */
227 : void copy(const Tensor &from) override;
228 :
229 : /**
230 : * @copydoc Tensor::copyData(const Tensor &from)
231 : */
232 : void copyData(const Tensor &from) override;
233 :
234 : /**
235 : * @copydoc Tensor::copy_with_stride()
236 : */
237 : void copy_with_stride(const Tensor &input, Tensor &output) override;
238 :
239 : /**
240 : * @copydoc Tensor::save(std::ostream &file)
241 : */
242 : void save(std::ostream &file) override;
243 :
244 : /**
245 : * @copydoc Tensor::read(std::ifstream &file)
246 : */
247 : void read(std::ifstream &file, size_t start_offset,
248 : bool read_from_offset) override;
249 :
250 : /**
251 : * @copydoc Tensor::argmax()
252 : */
253 : std::vector<unsigned int> argmax() const override;
254 :
255 : /**
256 : * @copydoc Tensor::argmin()
257 : */
258 : std::vector<unsigned int> argmin() const override;
259 :
260 : /**
261 : * @copydoc Tensor::max_abs()
262 : */
263 : float max_abs() const override;
264 :
265 : /**
266 : * @copydoc Tensor::maxValue()
267 : */
268 : float maxValue() const override;
269 :
270 : /**
271 : * @copydoc Tensor::minValue()
272 : */
273 : float minValue() const override;
274 :
275 : /**
276 : * @copydoc Tensor::print(std::ostream &out)
277 : */
278 : void print(std::ostream &out) const override;
279 :
280 : /**
281 : * @copydoc Tensor::getMemoryBytes()
282 : */
283 : size_t getMemoryBytes() const override;
284 :
285 : /**
286 : * @copydoc TensorBase::save_quantization_info()
287 : */
288 : void save_quantization_info(std::ostream &file) override;
289 :
290 : /**
291 : * @copydoc TensorBase::read_quantization_info()
292 : */
293 : void read_quantization_info(std::ifstream &file, size_t start_offset,
294 : bool read_from_offset) override;
295 :
296 : /**
297 : * @copydoc Tensor::scale_size()
298 : */
299 : size_t scale_size() const override;
300 :
301 : /**
302 : * @copydoc Tensor::scale_size()
303 : */
304 : QScheme q_scheme() const override;
305 :
306 : private:
307 : /**
308 : * @brief quantization scheme
309 : */
310 : QScheme qscheme;
311 :
312 : /**
313 : * @brief copy a buffer to @a this, the caller has to ensure that @a this is
314 : * initialized otherwise undefined behavior
315 : *
316 : * @param buf buffer to copy from
317 : */
318 : void copy(const void *buf);
319 :
320 : /**
321 : * @brief Get the Data Type String object
322 : * @return std::string of tensor data type (QINT8)
323 : */
324 1 : std::string getStringDataType() const override { return "QINT8"; }
325 :
326 : /**
327 : * @copydoc Tensor::isValid()
328 : */
329 0 : bool isValid() const override { return true; }; // NYI
330 : };
331 :
332 : } // namespace nntrainer
333 :
334 : #endif /* __cplusplus */
335 : #endif /* __CHAR_TENSOR_H__ */
|