blob: f9ec9373fccf144d4ed3d725afdfb835aa1bea48 [file] [log] [blame]
Eric Kunzee5e26762020-10-13 16:11:07 -07001
Fabrizio Indirli72038352023-12-11 11:15:32 +00002// Copyright (c) 2020-2024, ARM Limited.
Eric Kunzee5e26762020-10-13 16:11:07 -07003//
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#include "tensor.h"
17#include "arith_util.h"
Grant Watson64285a12022-11-16 15:32:39 +000018#include "array_proxy.h"
James Ward8b390432022-08-12 20:48:56 +010019#include "half.hpp"
Eric Kunzee5e26762020-10-13 16:11:07 -070020
21using namespace TosaReference;
22using namespace Eigen;
23using namespace tosa;
24
Tai Lya4d748b2023-03-28 22:06:56 +000025TosaReference::Tensor::Tensor(const std::string tensorName_,
26 const DType serializationDtype_,
27 const std::vector<int> shape_)
28 : tensorName(tensorName_)
29 , serializationDtype(serializationDtype_)
30 , shape(shape_)
31 , tensorDtype(ConvertDType(serializationDtype_))
Eric Kunzee5e26762020-10-13 16:11:07 -070032{
Jerry Ge9c9c8da2023-07-19 23:08:16 +000033 producer = nullptr;
34 isValid = false;
Eric Kunzee5e26762020-10-13 16:11:07 -070035 consumers.clear();
Jerry Ge9c9c8da2023-07-19 23:08:16 +000036 isSubgraphInput = false;
37 isSubgraphOutput = false;
Jerry Ge9e94af82022-10-27 09:57:00 -070038 isParentGraphOutput = false;
Tai Lycf84bc92023-09-07 20:49:09 +000039 isVariable = false;
Eric Kunzee5e26762020-10-13 16:11:07 -070040}
41
42TosaReference::Tensor::~Tensor()
43{}
44
Jerry Ge9e94af82022-10-27 09:57:00 -070045int TosaReference::Tensor::setIsParentGraphOutput()
46{
47 isParentGraphOutput = true;
48 return 0;
49}
50
Eric Kunzee5e26762020-10-13 16:11:07 -070051int TosaReference::Tensor::setIsSubgraphInput()
52{
53 isSubgraphInput = true;
54 return 0;
55}
56
57int TosaReference::Tensor::setIsSubgraphOutput()
58{
59 isSubgraphOutput = true;
60 return 0;
61}
62
Tai Lycf84bc92023-09-07 20:49:09 +000063int TosaReference::Tensor::setIsVariable()
64{
65 isVariable = true;
66 return 0;
67}
68
Eric Kunzee5e26762020-10-13 16:11:07 -070069int TosaReference::Tensor::setProducer(GraphNode* node)
70{
71 ASSERT_MSG(node, "Tensor::setProducer: no node passed in");
72 ASSERT_MSG(!producer, "Tensor::setProducer: producer node already set, tensor %s", tensorName.c_str());
73 producer = node;
74
75 return 0;
76}
77
78int TosaReference::Tensor::addConsumer(GraphNode* node)
79{
80 ASSERT_MSG(node, "Tensor::addConsumer: no node passed in");
81 consumers.push_back(node);
82
83 return 0;
84}
85
86int TosaReference::Tensor::dumpTensorParams(FILE* out) const
87{
Tai Lya4d748b2023-03-28 22:06:56 +000088 fprintf(out, "Name: %s DType=%s isValid=%d Rank=%d Shape=%s\n", tensorName.c_str(), EnumNameTOSAREFTYPE(getDtype()),
Kevin Cheng550ccc52021-03-03 11:21:43 -080089 getIsValid(), getRank(), getShapeAsString().c_str());
Eric Kunzee5e26762020-10-13 16:11:07 -070090
91 return 0;
92}
93
94int TosaReference::Tensor::dumpTensorParams(std::ostream& out) const
95{
Tai Lya4d748b2023-03-28 22:06:56 +000096 out << "Name: " << getName() << " DType=" << EnumNameTOSAREFTYPE(getDtype()) << " isValid=" << getIsValid()
Kevin Cheng550ccc52021-03-03 11:21:43 -080097 << " Rank=" << getRank() << " Shape=" << getShapeAsString() << "\n";
Eric Kunzee5e26762020-10-13 16:11:07 -070098
99 return 0;
100}
101
102int TosaReference::Tensor::readFromNpyFile(const char* filename)
103{
Jerry Ge9c9c8da2023-07-19 23:08:16 +0000104 uint32_t elements = getElementCount();
Tai Lya4d748b2023-03-28 22:06:56 +0000105 double* f64databuf = nullptr;
106 float* f32databuf = nullptr;
James Ward8b390432022-08-12 20:48:56 +0100107 half_float::half* f16databuf = nullptr;
Jerry Ge9c9c8da2023-07-19 23:08:16 +0000108 int32_t* i32databuf = nullptr;
109 int64_t* i64databuf = nullptr;
110 bool* bdatabuf = nullptr;
Eric Kunzee5e26762020-10-13 16:11:07 -0700111 NumpyUtilities::NPError nperror;
Jerry Ge9c9c8da2023-07-19 23:08:16 +0000112 TOSA_REF_TYPE dtype = getDtype();
113 DType serialization_dtype = getSerializationDtype();
Eric Kunzee5e26762020-10-13 16:11:07 -0700114
Tai Lya4d748b2023-03-28 22:06:56 +0000115 assert(dtype == ConvertDType(serialization_dtype));
116 // if dtype is FP64, serialization_dtype must be one of FP32, FP16, BF16
117 assert(dtype != TOSA_REF_TYPE_FP64 || serialization_dtype == DType_FP32 || serialization_dtype == DType_FP16 ||
118 serialization_dtype == DType_BF16);
119
120 switch (serialization_dtype)
Eric Kunzee5e26762020-10-13 16:11:07 -0700121 {
Jeremy Johnsonbc2a3db2022-09-27 13:50:00 +0100122 case DType_FP32:
James Ward24dbc422022-10-19 12:20:31 +0100123 case DType_BF16:
Tai Lya4d748b2023-03-28 22:06:56 +0000124 f32databuf = (float*)calloc(sizeof(float), elements);
125 ASSERT_MEM(f32databuf);
Eric Kunzee5e26762020-10-13 16:11:07 -0700126
Tai Lya4d748b2023-03-28 22:06:56 +0000127 nperror = NumpyUtilities::readFromNpyFile(filename, elements, f32databuf);
Eric Kunzee5e26762020-10-13 16:11:07 -0700128 break;
James Ward8b390432022-08-12 20:48:56 +0100129 case DType_FP16:
130 f16databuf = (half_float::half*)calloc(sizeof(half_float::half), elements);
131 ASSERT_MEM(f16databuf);
James Ward8b390432022-08-12 20:48:56 +0100132
133 nperror = NumpyUtilities::readFromNpyFile(filename, elements, f16databuf);
134 break;
Eric Kunzee5e26762020-10-13 16:11:07 -0700135 case DType_INT32:
Eric Kunzee5e26762020-10-13 16:11:07 -0700136 case DType_UINT8:
137 case DType_INT4:
138 case DType_INT8:
139 case DType_INT16:
Jeremy Johnsonf7f78ae2022-05-25 15:26:38 +0100140 case DType_UINT16:
Eric Kunzee5e26762020-10-13 16:11:07 -0700141 i32databuf = (int32_t*)calloc(sizeof(int32_t), elements);
142 ASSERT_MEM(i32databuf);
143
144 nperror = NumpyUtilities::readFromNpyFile(filename, elements, i32databuf);
145 break;
146 case DType_INT48:
Won Jeona21b2e82023-08-10 10:33:01 +0000147 case DType_SHAPE:
Eric Kunzee5e26762020-10-13 16:11:07 -0700148 i64databuf = (int64_t*)calloc(sizeof(int64_t), elements);
149 ASSERT_MEM(i64databuf);
150
151 nperror = NumpyUtilities::readFromNpyFile(filename, elements, i64databuf);
152 break;
153 case DType_BOOL:
154 bdatabuf = (bool*)calloc(sizeof(bool), elements);
155 ASSERT_MEM(bdatabuf);
156
157 nperror = NumpyUtilities::readFromNpyFile(filename, elements, bdatabuf);
158 break;
159 default:
Tai Lya4d748b2023-03-28 22:06:56 +0000160 FATAL_ERROR("unknown tensor type=%s", EnumNameDType(serialization_dtype));
Eric Kunzee5e26762020-10-13 16:11:07 -0700161 }
162
163 switch (nperror)
164 {
165 case NumpyUtilities::NO_ERROR:
166 break;
167 case NumpyUtilities::FILE_NOT_FOUND:
168 FATAL_ERROR("readFromNpyFile: Cannot open file %s", filename);
169 case NumpyUtilities::FILE_IO_ERROR:
170 FATAL_ERROR("readFromNpyFile: IO error reading file: %s", filename);
171 case NumpyUtilities::FILE_TYPE_MISMATCH:
172 FATAL_ERROR("readFromNpyFile: Tensor type %s and Numpy file type mismatch for tensor %s filename %s",
Tai Lya4d748b2023-03-28 22:06:56 +0000173 EnumNameTOSAREFTYPE(getDtype()), getName().c_str(), filename);
Eric Kunzee5e26762020-10-13 16:11:07 -0700174 case NumpyUtilities::HEADER_PARSE_ERROR:
175 FATAL_ERROR("Numpy header parsing error for file: %s", filename);
176 case NumpyUtilities::BUFFER_SIZE_MISMATCH:
177 FATAL_ERROR("Buffer size does not match numpy file size for tensor %s filename %s", getName().c_str(),
178 filename);
179 default:
180 FATAL_ERROR("Unknown error parsing Numpy file: %s", filename);
181 }
182
James Ward24dbc422022-10-19 12:20:31 +0100183 switch (dtype)
Eric Kunzee5e26762020-10-13 16:11:07 -0700184 {
Tai Lya4d748b2023-03-28 22:06:56 +0000185 case TOSA_REF_TYPE_FP16:
James Wardee256692022-11-15 11:36:47 +0000186 // Convert from fp16 to fp32 so that fp16 values can be manipulated as float
Tai Lya4d748b2023-03-28 22:06:56 +0000187 f32databuf = (float*)calloc(sizeof(float), elements);
188 ASSERT_MEM(f32databuf);
Jerry Ge9c9c8da2023-07-19 23:08:16 +0000189 for (uint32_t i = 0; i < elements; i++)
190 {
Tai Lya4d748b2023-03-28 22:06:56 +0000191 f32databuf[i] = half_float::half_cast<float, half_float::half>(f16databuf[i]);
James Ward8b390432022-08-12 20:48:56 +0100192 }
Tai Lya4d748b2023-03-28 22:06:56 +0000193 if (setTensorValueFloat(elements, f32databuf))
James Ward24dbc422022-10-19 12:20:31 +0100194 {
195 free(f16databuf);
Tai Lya4d748b2023-03-28 22:06:56 +0000196 free(f32databuf);
James Ward24dbc422022-10-19 12:20:31 +0100197 return 1;
198 }
199 break;
Tai Lya4d748b2023-03-28 22:06:56 +0000200 case TOSA_REF_TYPE_BF16:
Jerry Ge9c9c8da2023-07-19 23:08:16 +0000201 for (uint32_t i = 0; i < elements; i++)
James Ward24dbc422022-10-19 12:20:31 +0100202 {
Jerry Ge9c9c8da2023-07-19 23:08:16 +0000203 ASSERT_MSG(checkValidBFloat(f32databuf[i]), "Input float value not a valid bfloat16 value.");
James Ward24dbc422022-10-19 12:20:31 +0100204 }
Tai Lya4d748b2023-03-28 22:06:56 +0000205 if (setTensorValueFloat(elements, f32databuf))
James Ward24dbc422022-10-19 12:20:31 +0100206 {
Tai Lya4d748b2023-03-28 22:06:56 +0000207 free(f32databuf);
James Ward24dbc422022-10-19 12:20:31 +0100208 return 1;
209 }
210 break;
Tai Lya4d748b2023-03-28 22:06:56 +0000211 case TOSA_REF_TYPE_FP32:
212 if (setTensorValueFloat(elements, f32databuf))
Eric Kunzee5e26762020-10-13 16:11:07 -0700213 {
Tai Lya4d748b2023-03-28 22:06:56 +0000214 free(f32databuf);
Eric Kunzee5e26762020-10-13 16:11:07 -0700215 return 1;
216 }
217 break;
Tai Lya4d748b2023-03-28 22:06:56 +0000218 case TOSA_REF_TYPE_INT32:
219 case TOSA_REF_TYPE_UINT8:
220 case TOSA_REF_TYPE_INT4:
221 case TOSA_REF_TYPE_INT8:
222 case TOSA_REF_TYPE_INT16:
223 case TOSA_REF_TYPE_UINT16:
Eric Kunzee5e26762020-10-13 16:11:07 -0700224 if (setTensorValueInt32(elements, i32databuf))
225 {
226 free(i32databuf);
227 return 1;
228 }
229 break;
Tai Lya4d748b2023-03-28 22:06:56 +0000230 case TOSA_REF_TYPE_INT48:
Won Jeona21b2e82023-08-10 10:33:01 +0000231 case TOSA_REF_TYPE_SHAPE:
Eric Kunzee5e26762020-10-13 16:11:07 -0700232 if (setTensorValueInt64(elements, i64databuf))
233 {
234 free(i64databuf);
235 return 1;
236 }
237 break;
Tai Lya4d748b2023-03-28 22:06:56 +0000238 case TOSA_REF_TYPE_BOOL:
Eric Kunzee5e26762020-10-13 16:11:07 -0700239 if (setTensorValueBool(elements, bdatabuf))
240 {
241 free(i32databuf);
242 return 1;
243 }
244 break;
Tai Lya4d748b2023-03-28 22:06:56 +0000245 case TOSA_REF_TYPE_FP64:
246 switch (serialization_dtype)
247 {
248 case DType_FP16:
249 // FP16 -> FP64
250 f64databuf = (double*)calloc(sizeof(double), elements);
251 ASSERT_MEM(f64databuf);
252 for (uint32_t i = 0; i < elements; i++)
253 {
254 f64databuf[i] = half_float::half_cast<double, half_float::half>(f16databuf[i]);
255 }
256 if (setTensorValueDouble(elements, f64databuf))
257 {
258 free(f16databuf);
259 free(f64databuf);
260 return 1;
261 }
262 break;
263 case DType_BF16:
264 // BF16 -> FP64
265 f64databuf = (double*)calloc(sizeof(double), elements);
266 ASSERT_MEM(f64databuf);
267 for (uint32_t i = 0; i < elements; i++)
268 {
269 ASSERT_MSG(checkValidBFloat(f32databuf[i]), "Input float value not a valid bfloat16 value.");
270 f64databuf[i] = static_cast<double>(f32databuf[i]);
271 }
272 if (setTensorValueDouble(elements, f64databuf))
273 {
274 free(f32databuf);
275 free(f64databuf);
276 return 1;
277 }
278 break;
279 case DType_FP32:
280 // FP32 -> FP64
281 f64databuf = (double*)calloc(sizeof(double), elements);
282 ASSERT_MEM(f64databuf);
283 for (uint32_t i = 0; i < elements; i++)
284 {
285 f64databuf[i] = static_cast<double>(f32databuf[i]);
286 }
287 if (setTensorValueDouble(elements, f64databuf))
288 {
289 free(f32databuf);
290 free(f64databuf);
291 return 1;
292 }
293 break;
294 default:
295 FATAL_ERROR("unexpected tensor type=%s and original tensor type=%s", EnumNameTOSAREFTYPE(dtype),
296 EnumNameDType(serialization_dtype));
297 }
298 break;
Eric Kunzee5e26762020-10-13 16:11:07 -0700299 default:
Tai Lya4d748b2023-03-28 22:06:56 +0000300 FATAL_ERROR("unsupported tensor type=%s", EnumNameTOSAREFTYPE(dtype));
Eric Kunzee5e26762020-10-13 16:11:07 -0700301 }
302
303 setIsValid();
304
Tai Lya4d748b2023-03-28 22:06:56 +0000305 if (f32databuf)
306 free(f32databuf);
James Ward8b390432022-08-12 20:48:56 +0100307 if (f16databuf)
308 free(f16databuf);
Tai Lya4d748b2023-03-28 22:06:56 +0000309 if (f64databuf)
310 free(f64databuf);
Eric Kunzee5e26762020-10-13 16:11:07 -0700311 if (i32databuf)
312 free(i32databuf);
313 if (i64databuf)
314 free(i64databuf);
315 if (bdatabuf)
316 free(bdatabuf);
317
318 return 0;
319}
320
321int TosaReference::Tensor::writeToNpyFile(const char* filename) const
322{
Jerry Ge9c9c8da2023-07-19 23:08:16 +0000323 float* f32databuf = nullptr;
324 double* f64databuf = nullptr;
325 half_float::half* f16databuf = nullptr;
Jerry Gec5291692024-01-02 22:29:08 +0000326 uint8_t* ui8databuf = nullptr;
327 int8_t* i8databuf = nullptr;
Jerry Ge9c9c8da2023-07-19 23:08:16 +0000328 int32_t* i32databuf = nullptr;
329 int64_t* i64databuf = nullptr;
330 bool* bdatabuf = nullptr;
Eric Kunzeedac6ab2023-06-28 13:29:38 -0700331 NumpyUtilities::NPError nperror = NumpyUtilities::NO_ERROR;
Jerry Ge9c9c8da2023-07-19 23:08:16 +0000332 uint32_t elements = getElementCount();
333 const TOSA_REF_TYPE dtype = getDtype();
Eric Kunzee5e26762020-10-13 16:11:07 -0700334
James Ward24dbc422022-10-19 12:20:31 +0100335 switch (dtype)
Eric Kunzee5e26762020-10-13 16:11:07 -0700336 {
Tai Lya4d748b2023-03-28 22:06:56 +0000337 case TOSA_REF_TYPE_FP32:
338 case TOSA_REF_TYPE_BF16:
339 f32databuf = (float*)calloc(sizeof(float), elements);
340 ASSERT_MEM(f32databuf);
Eric Kunzee5e26762020-10-13 16:11:07 -0700341
Tai Lya4d748b2023-03-28 22:06:56 +0000342 if (getTensorValueFloat(elements, f32databuf))
Eric Kunzee5e26762020-10-13 16:11:07 -0700343 {
Tai Lya4d748b2023-03-28 22:06:56 +0000344 free(f32databuf);
Eric Kunzee5e26762020-10-13 16:11:07 -0700345 return 1;
346 }
Tai Lya4d748b2023-03-28 22:06:56 +0000347 nperror = NumpyUtilities::writeToNpyFile(filename, shape, f32databuf);
Eric Kunzee5e26762020-10-13 16:11:07 -0700348
Tai Lya4d748b2023-03-28 22:06:56 +0000349 free(f32databuf);
Eric Kunzee5e26762020-10-13 16:11:07 -0700350 break;
Tai Lya4d748b2023-03-28 22:06:56 +0000351 case TOSA_REF_TYPE_FP16:
352 f32databuf = (float*)calloc(sizeof(float), elements);
353 ASSERT_MEM(f32databuf);
James Ward8b390432022-08-12 20:48:56 +0100354 f16databuf = (half_float::half*)calloc(sizeof(half_float::half), elements);
355 ASSERT_MEM(f16databuf);
356
Tai Lya4d748b2023-03-28 22:06:56 +0000357 if (getTensorValueFloat(elements, f32databuf))
James Ward8b390432022-08-12 20:48:56 +0100358 {
Tai Lya4d748b2023-03-28 22:06:56 +0000359 free(f32databuf);
James Ward8b390432022-08-12 20:48:56 +0100360 free(f16databuf);
361 return 1;
362 }
James Wardee256692022-11-15 11:36:47 +0000363 // Convert fp32 to fp16 so that output file contains valid fp16 data
Jerry Ge9c9c8da2023-07-19 23:08:16 +0000364 for (uint32_t i = 0; i < elements; i++)
365 {
Tai Lya4d748b2023-03-28 22:06:56 +0000366 f16databuf[i] = half_float::half_cast<half_float::half, float>(f32databuf[i]);
James Ward8b390432022-08-12 20:48:56 +0100367 }
368 nperror = NumpyUtilities::writeToNpyFile(filename, shape, f16databuf);
369
Tai Lya4d748b2023-03-28 22:06:56 +0000370 free(f32databuf);
James Ward8b390432022-08-12 20:48:56 +0100371 free(f16databuf);
372 break;
Tai Lya4d748b2023-03-28 22:06:56 +0000373 case TOSA_REF_TYPE_INT32:
Jerry Gec5291692024-01-02 22:29:08 +0000374 i32databuf = (int32_t*)calloc(sizeof(int32_t), elements);
375 ASSERT_MEM(i32databuf);
376
377 if (getTensorValueInt32(elements, i32databuf))
378 {
379 free(i32databuf);
380 return 1;
381 }
382
383 nperror = NumpyUtilities::writeToNpyFile(filename, shape, i32databuf);
384
385 free(i32databuf);
386 break;
Tai Lya4d748b2023-03-28 22:06:56 +0000387 case TOSA_REF_TYPE_UINT8:
Jerry Gec5291692024-01-02 22:29:08 +0000388 ui8databuf = (uint8_t*)calloc(sizeof(uint8_t), elements);
389 ASSERT_MEM(ui8databuf);
390
391 if (getTensorValueUInt8(elements, ui8databuf))
392 {
393 free(ui8databuf);
394 return 1;
395 }
396
397 nperror = NumpyUtilities::writeToNpyFile(filename, shape, ui8databuf);
398
399 free(ui8databuf);
400 break;
Tai Lya4d748b2023-03-28 22:06:56 +0000401 case TOSA_REF_TYPE_INT4:
402 case TOSA_REF_TYPE_INT8:
Jerry Gec5291692024-01-02 22:29:08 +0000403 i8databuf = (int8_t*)calloc(sizeof(int8_t), elements);
404 ASSERT_MEM(i8databuf);
405
406 if (getTensorValueInt8(elements, i8databuf))
407 {
408 free(i8databuf);
409 return 1;
410 }
411
412 nperror = NumpyUtilities::writeToNpyFile(filename, shape, i8databuf);
413
414 free(i8databuf);
415 break;
Tai Lya4d748b2023-03-28 22:06:56 +0000416 case TOSA_REF_TYPE_INT16:
417 case TOSA_REF_TYPE_UINT16:
Eric Kunzee5e26762020-10-13 16:11:07 -0700418 i32databuf = (int32_t*)calloc(sizeof(int32_t), elements);
419 ASSERT_MEM(i32databuf);
420
421 if (getTensorValueInt32(elements, i32databuf))
422 {
423 free(i32databuf);
424 return 1;
425 }
426
427 nperror = NumpyUtilities::writeToNpyFile(filename, shape, i32databuf);
428
429 free(i32databuf);
430 break;
Tai Lya4d748b2023-03-28 22:06:56 +0000431 case TOSA_REF_TYPE_INT48:
Won Jeona21b2e82023-08-10 10:33:01 +0000432 case TOSA_REF_TYPE_SHAPE:
Eric Kunzee5e26762020-10-13 16:11:07 -0700433 i64databuf = (int64_t*)calloc(sizeof(int64_t), elements);
434 ASSERT_MEM(i64databuf);
435
436 if (getTensorValueInt64(elements, i64databuf))
437 {
438 free(i64databuf);
439 return 1;
440 }
441
442 nperror = NumpyUtilities::writeToNpyFile(filename, shape, i64databuf);
443
444 free(i64databuf);
445 break;
Tai Lya4d748b2023-03-28 22:06:56 +0000446 case TOSA_REF_TYPE_BOOL:
Eric Kunzee5e26762020-10-13 16:11:07 -0700447 bdatabuf = (bool*)calloc(sizeof(bool), elements);
448 ASSERT_MEM(bdatabuf);
449
450 if (getTensorValueBool(elements, bdatabuf))
451 {
452 free(bdatabuf);
453 return 1;
454 }
455
456 nperror = NumpyUtilities::writeToNpyFile(filename, shape, bdatabuf);
457
458 free(bdatabuf);
459 break;
Tai Lya4d748b2023-03-28 22:06:56 +0000460 case TOSA_REF_TYPE_FP64:
461 // @todo : support FP64 dtype
462 f64databuf = (double*)calloc(sizeof(double), elements);
463 ASSERT_MEM(f64databuf);
464
465 if (getTensorValueDouble(elements, f64databuf))
466 {
467 free(f64databuf);
468 return 1;
469 }
470 nperror = NumpyUtilities::writeToNpyFile(filename, shape, f64databuf);
471
472 free(f64databuf);
473 break;
474 case TOSA_REF_TYPE_UNKNOWN:
475 FATAL_ERROR("unsupported tensor type=%s", EnumNameTOSAREFTYPE(getDtype()));
Eric Kunzee5e26762020-10-13 16:11:07 -0700476 }
477
478 switch (nperror)
479 {
480 case NumpyUtilities::NO_ERROR:
481 break;
482 case NumpyUtilities::FILE_NOT_FOUND:
483 FATAL_ERROR("writeToNpyFile: Cannot open output file %s", filename);
484 case NumpyUtilities::FILE_IO_ERROR:
485 FATAL_ERROR("writeToNpyFile: IO error writing file: %s", filename);
486 case NumpyUtilities::FILE_TYPE_MISMATCH:
487 FATAL_ERROR("writeToNpyFile: Tensor type and Numpy file type mismatch for tensor %s filename %s",
488 getName().c_str(), filename);
489 case NumpyUtilities::HEADER_PARSE_ERROR:
490 FATAL_ERROR("Numpy header parsing error for file: %s", filename);
491 case NumpyUtilities::BUFFER_SIZE_MISMATCH:
492 FATAL_ERROR("Buffer size does not match numpy file size for tensor %s filename %s", getName().c_str(),
493 filename);
494 default:
495 FATAL_ERROR("Unknown error writing Numpy file: %s", filename);
496 }
497
498 return 0;
499}
500
501template <class T>
502int TosaReference::TensorTemplate<T>::copyValueFrom(TosaReference::Tensor* src)
503{
504 FATAL_ERROR("TensorTemplate<T>::copyValueFrom should not be called. "
505 "Implement template specialization version.");
506 return 0;
507}
508
509#define DEF_CTENSOR_COPY_VALUE_FROM(RANK, TYPE) \
510 template <> \
511 int TosaReference::Tensor##RANK<TYPE>::copyValueFrom(TosaReference::Tensor* src) \
512 { \
513 TosaReference::Tensor##RANK<TYPE>* t = dynamic_cast<Tensor##RANK<TYPE>*>(src); \
514 if (!t) \
515 { \
516 WARNING("tensor %s templated class does not match %s", src->getName().c_str(), this->getName().c_str()); \
517 return 1; \
518 } \
519 \
Tai Lya4d748b2023-03-28 22:06:56 +0000520 const uint32_t src_rank = src->getRank(); \
521 const uint32_t dst_rank = this->getRank(); \
522 const TOSA_REF_TYPE src_dtype = src->getDtype(); \
523 const TOSA_REF_TYPE dst_dtype = this->getDtype(); \
524 bool tensor_match = true; \
Eric Kunzee5e26762020-10-13 16:11:07 -0700525 \
526 if ((src_rank != dst_rank) || (src_dtype != dst_dtype)) \
527 { \
528 tensor_match = false; \
529 } \
530 else \
531 { \
532 for (uint32_t i = 0; i < src_rank; i++) \
533 { \
534 int src_dim = src->getShape()[i]; \
535 int dst_dim = this->getShape()[i]; \
536 if (src_dim != dst_dim) \
537 { \
538 tensor_match = false; \
539 } \
540 } \
541 } \
542 \
543 if (!tensor_match) \
544 { \
545 WARNING("source tensor %s (rank=%u, dtype=%s, shape=%s) doesn't match destination tensor %s (rank=%u, " \
546 "dtype=%s, shape=%s)", \
Tai Lya4d748b2023-03-28 22:06:56 +0000547 src->getName().c_str(), src_rank, EnumNameTOSAREFTYPE(src_dtype), src->getShapeAsString().c_str(), \
548 this->getName().c_str(), dst_rank, EnumNameTOSAREFTYPE(dst_dtype), \
549 this->getShapeAsString().c_str()); \
Eric Kunzee5e26762020-10-13 16:11:07 -0700550 return 1; \
551 } \
552 \
553 this->getTensor() = t->getTensor(); \
554 return 0; \
555 }
556
557DEF_CTENSOR_COPY_VALUE_FROM(0, float)
558DEF_CTENSOR_COPY_VALUE_FROM(1, float)
559DEF_CTENSOR_COPY_VALUE_FROM(2, float)
560DEF_CTENSOR_COPY_VALUE_FROM(3, float)
561DEF_CTENSOR_COPY_VALUE_FROM(4, float)
562DEF_CTENSOR_COPY_VALUE_FROM(5, float)
563DEF_CTENSOR_COPY_VALUE_FROM(6, float)
Tai Lya4d748b2023-03-28 22:06:56 +0000564DEF_CTENSOR_COPY_VALUE_FROM(0, double)
565DEF_CTENSOR_COPY_VALUE_FROM(1, double)
566DEF_CTENSOR_COPY_VALUE_FROM(2, double)
567DEF_CTENSOR_COPY_VALUE_FROM(3, double)
568DEF_CTENSOR_COPY_VALUE_FROM(4, double)
569DEF_CTENSOR_COPY_VALUE_FROM(5, double)
570DEF_CTENSOR_COPY_VALUE_FROM(6, double)
Eric Kunzee5e26762020-10-13 16:11:07 -0700571DEF_CTENSOR_COPY_VALUE_FROM(0, int32_t)
572DEF_CTENSOR_COPY_VALUE_FROM(1, int32_t)
573DEF_CTENSOR_COPY_VALUE_FROM(2, int32_t)
574DEF_CTENSOR_COPY_VALUE_FROM(3, int32_t)
575DEF_CTENSOR_COPY_VALUE_FROM(4, int32_t)
576DEF_CTENSOR_COPY_VALUE_FROM(5, int32_t)
577DEF_CTENSOR_COPY_VALUE_FROM(6, int32_t)
578DEF_CTENSOR_COPY_VALUE_FROM(0, int64_t)
579DEF_CTENSOR_COPY_VALUE_FROM(1, int64_t)
580DEF_CTENSOR_COPY_VALUE_FROM(2, int64_t)
581DEF_CTENSOR_COPY_VALUE_FROM(3, int64_t)
582DEF_CTENSOR_COPY_VALUE_FROM(4, int64_t)
583DEF_CTENSOR_COPY_VALUE_FROM(5, int64_t)
584DEF_CTENSOR_COPY_VALUE_FROM(6, int64_t)
585DEF_CTENSOR_COPY_VALUE_FROM(0, bool)
586DEF_CTENSOR_COPY_VALUE_FROM(1, bool)
587DEF_CTENSOR_COPY_VALUE_FROM(2, bool)
588DEF_CTENSOR_COPY_VALUE_FROM(3, bool)
589DEF_CTENSOR_COPY_VALUE_FROM(4, bool)
590DEF_CTENSOR_COPY_VALUE_FROM(5, bool)
591DEF_CTENSOR_COPY_VALUE_FROM(6, bool)
592
593#undef DEF_CTENSOR_COPY_VALUE_FROM
594
Tai Lya4d748b2023-03-28 22:06:56 +0000595int TosaReference::Tensor::readfromVector(const ArrayProxy<double> vals)
596{
597 uint32_t elements = getElementCount();
598 switch (getDtype())
599 {
600 case TOSA_REF_TYPE_FP64:
601 if (vals.size() != elements)
602 {
603 WARNING("The input size (%ld) doesn't match the number of elements (%d) assigned to the tensor.",
604 vals.size(), elements);
605 return -1;
606 }
607
608 setTensorValueDouble(elements, vals.data());
609 break;
610 default:
611 WARNING("The input type (float) doesn't match the data type assigned to the tensor (%s).",
612 EnumNameTOSAREFTYPE(getDtype()));
613 return -2;
614 }
615 setIsValid();
616 return 0;
617}
618
Grant Watson64285a12022-11-16 15:32:39 +0000619int TosaReference::Tensor::readfromVector(const ArrayProxy<float> vals)
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100620{
621 uint32_t elements = getElementCount();
622 switch (getDtype())
623 {
Fabrizio Indirli72038352023-12-11 11:15:32 +0000624 case TOSA_REF_TYPE_FP64:
625 if (!g_func_config.precise_mode)
626 {
627 WARNING("The input type (float) doesn't match the data type assigned to the tensor (%s).",
628 EnumNameTOSAREFTYPE(getDtype()));
629 return -2;
630 }
631 // continue with setting float vals in the tensor
Tai Lya4d748b2023-03-28 22:06:56 +0000632 case TOSA_REF_TYPE_FP16:
633 case TOSA_REF_TYPE_FP32:
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100634 if (vals.size() != elements)
635 {
636 WARNING("The input size (%ld) doesn't match the number of elements (%d) assigned to the tensor.",
637 vals.size(), elements);
638 return -1;
639 }
640
641 setTensorValueFloat(elements, vals.data());
642 break;
Tai Lya4d748b2023-03-28 22:06:56 +0000643 case TOSA_REF_TYPE_BF16:
James Ward3d3d45d2022-11-28 16:45:36 +0000644 if (vals.size() != elements)
645 {
646 WARNING("The input size (%ld) doesn't match the number of elements (%d) assigned to the tensor.",
647 vals.size(), elements);
648 return -1;
649 }
650
651 for (auto v : vals)
652 {
Jerry Ge9c9c8da2023-07-19 23:08:16 +0000653 ASSERT_MSG(checkValidBFloat(v), "Input float value not a valid bfloat16 value.");
James Ward3d3d45d2022-11-28 16:45:36 +0000654 }
655
656 setTensorValueFloat(elements, vals.data());
657 break;
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100658 default:
659 WARNING("The input type (float) doesn't match the data type assigned to the tensor (%s).",
Tai Lya4d748b2023-03-28 22:06:56 +0000660 EnumNameTOSAREFTYPE(getDtype()));
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100661 return -2;
662 }
663 setIsValid();
664 return 0;
665}
666
Grant Watson64285a12022-11-16 15:32:39 +0000667int TosaReference::Tensor::readfromVector(const ArrayProxy<half_float::half> vals)
Matthew Sloyan2e4d8892022-10-18 18:02:48 +0100668{
669 uint32_t elements = getElementCount();
670 std::vector<float> tensor(elements);
671
672 switch (getDtype())
673 {
Fabrizio Indirli72038352023-12-11 11:15:32 +0000674 case TOSA_REF_TYPE_FP64:
675 if (!g_func_config.precise_mode)
676 {
677 WARNING("The input type (float) doesn't match the data type assigned to the tensor (%s).",
678 EnumNameTOSAREFTYPE(getDtype()));
679 return -2;
680 }
681 // continue with setting float vals in the tensor
Tai Lya4d748b2023-03-28 22:06:56 +0000682 case TOSA_REF_TYPE_FP16:
Matthew Sloyan2e4d8892022-10-18 18:02:48 +0100683 if (vals.size() != elements)
684 {
685 WARNING("The input size (%ld) doesn't match the number of elements (%d) assigned to the tensor.",
686 vals.size(), elements);
687 return -1;
688 }
689
690 // Convert from fp16 to fp32
Jerry Ge9c9c8da2023-07-19 23:08:16 +0000691 for (uint32_t i = 0; i < elements; i++)
Matthew Sloyan2e4d8892022-10-18 18:02:48 +0100692 {
693 tensor[i] = half_float::half_cast<float, half_float::half>(vals[i]);
694 }
695
696 setTensorValueFloat(elements, tensor.data());
697 break;
698 default:
699 WARNING("The input type doesn't match the data type assigned to the tensor (%s).",
Tai Lya4d748b2023-03-28 22:06:56 +0000700 EnumNameTOSAREFTYPE(getDtype()));
Matthew Sloyan2e4d8892022-10-18 18:02:48 +0100701 return -2;
702 }
703 setIsValid();
704 return 0;
705}
706
Jerry Gec5291692024-01-02 22:29:08 +0000707int TosaReference::Tensor::readfromVector(const ArrayProxy<int8_t> vals)
708{
709 uint32_t elements = getElementCount();
710 switch (getDtype())
711 {
712 case TOSA_REF_TYPE_INT8:
713 case TOSA_REF_TYPE_UINT8:
714 if (vals.size() != elements)
715 {
716 WARNING("The input size (%ld) doesn't match the number of elements (%d) assigned to the tensor.",
717 vals.size(), elements);
718 return -1;
719 }
720
721 setTensorValueInt8(elements, vals.data());
722 break;
723 default:
724 WARNING("The input type doesn't match the data type assigned to the tensor (%s).",
725 EnumNameTOSAREFTYPE(getDtype()));
726 return -2;
727 }
728 setIsValid();
729 return 0;
730}
731
Georgios Pinitase9059772023-12-06 18:52:30 +0000732int TosaReference::Tensor::readfromVector(const ArrayProxy<int16_t> vals)
733{
734 uint32_t elements = getElementCount();
735 switch (getDtype())
736 {
737 case TOSA_REF_TYPE_INT16:
738 case TOSA_REF_TYPE_UINT16:
739 if (vals.size() != elements)
740 {
741 WARNING("The input size (%ld) doesn't match the number of elements (%d) assigned to the tensor.",
742 vals.size(), elements);
743 return -1;
744 }
745
746 setTensorValueInt16(elements, vals.data());
747 break;
748 default:
749 WARNING("The input type doesn't match the data type assigned to the tensor (%s).",
750 EnumNameTOSAREFTYPE(getDtype()));
751 return -2;
752 }
753 setIsValid();
754 return 0;
755}
756
Grant Watson64285a12022-11-16 15:32:39 +0000757int TosaReference::Tensor::readfromVector(const ArrayProxy<int32_t> vals)
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100758{
759 uint32_t elements = getElementCount();
760 switch (getDtype())
761 {
Tai Lya4d748b2023-03-28 22:06:56 +0000762 case TOSA_REF_TYPE_INT32:
763 case TOSA_REF_TYPE_UINT8:
764 case TOSA_REF_TYPE_INT4:
765 case TOSA_REF_TYPE_INT8:
766 case TOSA_REF_TYPE_INT16:
767 case TOSA_REF_TYPE_UINT16:
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100768 if (vals.size() != elements)
769 {
770 WARNING("The input size (%ld) doesn't match the number of elements (%d) assigned to the tensor.",
771 vals.size(), elements);
772 return -1;
773 }
774
775 setTensorValueInt32(elements, vals.data());
776 break;
777 default:
778 WARNING("The input type doesn't match the data type assigned to the tensor (%s).",
Tai Lya4d748b2023-03-28 22:06:56 +0000779 EnumNameTOSAREFTYPE(getDtype()));
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100780 return -2;
781 }
782 setIsValid();
783 return 0;
784}
785
Grant Watson64285a12022-11-16 15:32:39 +0000786int TosaReference::Tensor::readfromVector(const ArrayProxy<int64_t> vals)
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100787{
788 uint32_t elements = getElementCount();
789 switch (getDtype())
790 {
Tai Lya4d748b2023-03-28 22:06:56 +0000791 case TOSA_REF_TYPE_INT48:
Won Jeona21b2e82023-08-10 10:33:01 +0000792 case TOSA_REF_TYPE_SHAPE:
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100793 if (vals.size() != elements)
794 {
795 WARNING("The input size (%ld) doesn't match the number of elements (%d) assigned to the tensor.",
796 vals.size(), elements);
797 return -1;
798 }
799
800 setTensorValueInt64(elements, vals.data());
801 break;
802 default:
803 WARNING("The input type doesn't match the data type assigned to the tensor (%s).",
Tai Lya4d748b2023-03-28 22:06:56 +0000804 EnumNameTOSAREFTYPE(getDtype()));
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100805 return -2;
806 }
807 setIsValid();
808 return 0;
809}
810
Grant Watson64285a12022-11-16 15:32:39 +0000811int TosaReference::Tensor::readfromVector(const ArrayProxy<unsigned char> vals)
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100812{
813 uint32_t elements = getElementCount();
814
815 switch (getDtype())
816 {
Tai Lya4d748b2023-03-28 22:06:56 +0000817 case TOSA_REF_TYPE_BOOL:
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100818 if (vals.size() != elements)
819 {
820 WARNING("The input size (%ld) doesn't match the number of elements (%d) assigned to the tensor.",
821 vals.size(), elements);
822 return -1;
823 }
824
825 setTensorValueBool(elements, reinterpret_cast<const bool*>(vals.data()));
826 break;
827 default:
828 WARNING("The input type (bool) doesn't match the data type assigned to the tensor (%s).",
Tai Lya4d748b2023-03-28 22:06:56 +0000829 EnumNameTOSAREFTYPE(getDtype()));
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100830 return -2;
831 }
832 setIsValid();
833 return 0;
834}
835
Tai Lya4d748b2023-03-28 22:06:56 +0000836int TosaReference::Tensor::writeToVector(ArrayProxy<double> vals)
837{
838 uint32_t elements = getElementCount();
839
840 switch (getDtype())
841 {
842 case TOSA_REF_TYPE_FP64:
843 if (vals.size() != elements)
844 {
845 WARNING("The output size (%ld) doesn't match the number of elements (%d) assigned to the tensor.",
846 vals.size(), elements);
847 return -1;
848 }
849
850 getTensorValueDouble(elements, vals.data());
851 break;
852 default:
853 WARNING("The output type (float) doesn't match the data type assigned to the tensor (%s).",
854 EnumNameTOSAREFTYPE(getDtype()));
855 return -2;
856 }
857 return 0;
858}
859
Grant Watson64285a12022-11-16 15:32:39 +0000860int TosaReference::Tensor::writeToVector(ArrayProxy<float> vals)
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100861{
862 uint32_t elements = getElementCount();
863
864 switch (getDtype())
865 {
Tai Lya4d748b2023-03-28 22:06:56 +0000866 case TOSA_REF_TYPE_FP16:
867 case TOSA_REF_TYPE_FP32:
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100868 if (vals.size() != elements)
869 {
870 WARNING("The output size (%ld) doesn't match the number of elements (%d) assigned to the tensor.",
871 vals.size(), elements);
872 return -1;
873 }
874
875 getTensorValueFloat(elements, vals.data());
876 break;
Tai Lya4d748b2023-03-28 22:06:56 +0000877 case TOSA_REF_TYPE_BF16:
James Ward3d3d45d2022-11-28 16:45:36 +0000878 if (vals.size() != elements)
879 {
880 WARNING("The output size (%ld) doesn't match the number of elements (%d) assigned to the tensor.",
881 vals.size(), elements);
882 return -1;
883 }
884
885 getTensorValueFloat(elements, vals.data());
886
887 for (auto v : vals)
888 {
Jerry Ge9c9c8da2023-07-19 23:08:16 +0000889 ASSERT_MSG(checkValidBFloat(v), "Float value not a valid bfloat16 value.");
James Ward3d3d45d2022-11-28 16:45:36 +0000890 }
891
892 break;
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100893 default:
894 WARNING("The output type (float) doesn't match the data type assigned to the tensor (%s).",
Tai Lya4d748b2023-03-28 22:06:56 +0000895 EnumNameTOSAREFTYPE(getDtype()));
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100896 return -2;
897 }
898 return 0;
899}
900
Grant Watson64285a12022-11-16 15:32:39 +0000901int TosaReference::Tensor::writeToVector(ArrayProxy<half_float::half> vals)
Matthew Sloyan2e4d8892022-10-18 18:02:48 +0100902{
903 uint32_t elements = getElementCount();
904 std::vector<float> tensor(elements);
905
906 switch (getDtype())
907 {
Tai Lya4d748b2023-03-28 22:06:56 +0000908 case TOSA_REF_TYPE_FP16:
Matthew Sloyan2e4d8892022-10-18 18:02:48 +0100909 if (vals.size() != elements)
910 {
911 WARNING("The output size (%ld) doesn't match the number of elements (%d) assigned to the tensor.",
912 vals.size(), elements);
913 return -1;
914 }
915
916 getTensorValueFloat(elements, tensor.data());
917
918 // Convert fp32 to fp16
Jerry Ge9c9c8da2023-07-19 23:08:16 +0000919 for (uint32_t i = 0; i < elements; i++)
Matthew Sloyan2e4d8892022-10-18 18:02:48 +0100920 {
921 vals[i] = half_float::half_cast<half_float::half, float>(tensor[i]);
922 }
923 break;
924 default:
925 WARNING("The output type doesn't match the data type assigned to the tensor (%s).",
Tai Lya4d748b2023-03-28 22:06:56 +0000926 EnumNameTOSAREFTYPE(getDtype()));
Matthew Sloyan2e4d8892022-10-18 18:02:48 +0100927 return -2;
928 }
929 return 0;
930}
931
Jerry Gec5291692024-01-02 22:29:08 +0000932int TosaReference::Tensor::writeToVector(ArrayProxy<int8_t> vals)
933{
934 uint32_t elements = getElementCount();
935 switch (getDtype())
936 {
937 case TOSA_REF_TYPE_INT8:
938 case TOSA_REF_TYPE_UINT8:
939 if (vals.size() != elements)
940 {
941 WARNING("The output size (%ld) doesn't match the number of elements (%d) assigned to the tensor.",
942 vals.size(), elements);
943 return -1;
944 }
945
946 getTensorValueInt8(elements, vals.data());
947 break;
948 default:
949 WARNING("The output type doesn't match the data type assigned to the tensor (%s).",
950 EnumNameTOSAREFTYPE(getDtype()));
951 return -2;
952 }
953 return 0;
954}
955
Georgios Pinitase9059772023-12-06 18:52:30 +0000956int TosaReference::Tensor::writeToVector(ArrayProxy<int16_t> vals)
957{
958 uint32_t elements = getElementCount();
959
960 switch (getDtype())
961 {
962 case TOSA_REF_TYPE_INT16:
963 case TOSA_REF_TYPE_UINT16:
964 if (vals.size() != elements)
965 {
966 WARNING("The output size (%ld) doesn't match the number of elements (%d) assigned to the tensor.",
967 vals.size(), elements);
968 return -1;
969 }
970
971 getTensorValueInt16(elements, vals.data());
972 break;
973 default:
974 WARNING("The output type doesn't match the data type assigned to the tensor (%s).",
975 EnumNameTOSAREFTYPE(getDtype()));
976 return -2;
977 }
978 return 0;
979}
980
Grant Watson64285a12022-11-16 15:32:39 +0000981int TosaReference::Tensor::writeToVector(ArrayProxy<int32_t> vals)
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100982{
983 uint32_t elements = getElementCount();
984
985 switch (getDtype())
986 {
Tai Lya4d748b2023-03-28 22:06:56 +0000987 case TOSA_REF_TYPE_INT32:
988 case TOSA_REF_TYPE_UINT8:
989 case TOSA_REF_TYPE_INT4:
990 case TOSA_REF_TYPE_INT8:
991 case TOSA_REF_TYPE_INT16:
992 case TOSA_REF_TYPE_UINT16:
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100993 if (vals.size() != elements)
994 {
995 WARNING("The output size (%ld) doesn't match the number of elements (%d) assigned to the tensor.",
996 vals.size(), elements);
997 return -1;
998 }
999
1000 getTensorValueInt32(elements, vals.data());
1001 break;
1002 default:
1003 WARNING("The output type doesn't match the data type assigned to the tensor (%s).",
Tai Lya4d748b2023-03-28 22:06:56 +00001004 EnumNameTOSAREFTYPE(getDtype()));
Matthew Sloyanba5fad32022-09-26 13:31:43 +01001005 return -2;
1006 }
1007 return 0;
1008}
1009
Grant Watson64285a12022-11-16 15:32:39 +00001010int TosaReference::Tensor::writeToVector(ArrayProxy<int64_t> vals)
Matthew Sloyanba5fad32022-09-26 13:31:43 +01001011{
1012 uint32_t elements = getElementCount();
1013
1014 switch (getDtype())
1015 {
Tai Lya4d748b2023-03-28 22:06:56 +00001016 case TOSA_REF_TYPE_INT48:
Won Jeona21b2e82023-08-10 10:33:01 +00001017 case TOSA_REF_TYPE_SHAPE:
Matthew Sloyanba5fad32022-09-26 13:31:43 +01001018 if (vals.size() != elements)
1019 {
1020 WARNING("The output size (%ld) doesn't match the number of elements (%d) assigned to the tensor.",
1021 vals.size(), elements);
1022 return -1;
1023 }
1024
1025 getTensorValueInt64(elements, vals.data());
1026 break;
1027 default:
1028 WARNING("The output type doesn't match the data type assigned to the tensor (%s).",
Tai Lya4d748b2023-03-28 22:06:56 +00001029 EnumNameTOSAREFTYPE(getDtype()));
Matthew Sloyanba5fad32022-09-26 13:31:43 +01001030 return -2;
1031 }
1032 return 0;
1033}
1034
Grant Watson64285a12022-11-16 15:32:39 +00001035int TosaReference::Tensor::writeToVector(ArrayProxy<unsigned char> vals)
Matthew Sloyanba5fad32022-09-26 13:31:43 +01001036{
1037 uint32_t elements = getElementCount();
1038
1039 switch (getDtype())
1040 {
Tai Lya4d748b2023-03-28 22:06:56 +00001041 case TOSA_REF_TYPE_BOOL:
Matthew Sloyanba5fad32022-09-26 13:31:43 +01001042 if (vals.size() != elements)
1043 {
1044 WARNING("The output size (%ld) doesn't match the number of elements (%d) assigned to the tensor.",
1045 vals.size(), elements);
1046 return -1;
1047 }
1048
1049 getTensorValueBool(elements, reinterpret_cast<bool*>(vals.data()));
1050 break;
1051 default:
1052 WARNING("The output type (bool) doesn't match the data type assigned to the tensor (%s).",
Tai Lya4d748b2023-03-28 22:06:56 +00001053 EnumNameTOSAREFTYPE(getDtype()));
Matthew Sloyanba5fad32022-09-26 13:31:43 +01001054 return -2;
1055 }
1056 return 0;
1057}
1058
Eric Kunzee5e26762020-10-13 16:11:07 -07001059template <class T>
Tai Lya4d748b2023-03-28 22:06:56 +00001060int TosaReference::TensorTemplate<T>::setTensorValueDouble(const size_t buflen, const double* vals)
1061{
Fabrizio Indirli72038352023-12-11 11:15:32 +00001062 FATAL_ERROR("TensorTemplate<T>::setTensorValueDouble should not be called. "
Tai Lya4d748b2023-03-28 22:06:56 +00001063 "Implement template specialization version.");
1064 return 0;
1065}
1066
1067template <>
1068int TosaReference::Tensor0<double>::setTensorValueDouble(const size_t bufLen, const double* vals)
1069{
1070 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1071
1072 (*tensor)(0) = vals[0];
1073
1074 return 0;
1075}
1076
1077template <>
1078int TosaReference::Tensor1<double>::setTensorValueDouble(const size_t bufLen, const double* vals)
1079{
1080 uint32_t idx = 0;
1081
1082 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1083
1084 for (int i0 = 0; i0 < shape[0]; i0++)
1085 {
1086 (*tensor)(i0) = vals[idx++];
1087 }
1088
1089 return 0;
1090}
1091
1092template <>
1093int TosaReference::Tensor2<double>::setTensorValueDouble(const size_t bufLen, const double* vals)
1094{
1095 uint32_t idx = 0;
1096
1097 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1098
1099 for (int i0 = 0; i0 < shape[0]; i0++)
1100 {
1101 for (int i1 = 0; i1 < shape[1]; i1++)
1102 {
1103 (*tensor)(i0, i1) = vals[idx++];
1104 }
1105 }
1106
1107 return 0;
1108}
1109
1110template <>
1111int TosaReference::Tensor3<double>::setTensorValueDouble(const size_t bufLen, const double* vals)
1112{
1113 uint32_t idx = 0;
1114
1115 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1116
1117 for (int i0 = 0; i0 < shape[0]; i0++)
1118 {
1119 for (int i1 = 0; i1 < shape[1]; i1++)
1120 {
1121 for (int i2 = 0; i2 < shape[2]; i2++)
1122 {
1123 (*tensor)(i0, i1, i2) = vals[idx++];
1124 }
1125 }
1126 }
1127
1128 return 0;
1129}
1130
1131template <>
1132int TosaReference::Tensor4<double>::setTensorValueDouble(const size_t bufLen, const double* vals)
1133{
1134 uint32_t idx = 0;
1135
1136 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1137
1138 for (int i0 = 0; i0 < shape[0]; i0++)
1139 {
1140 for (int i1 = 0; i1 < shape[1]; i1++)
1141 {
1142 for (int i2 = 0; i2 < shape[2]; i2++)
1143 {
1144 for (int i3 = 0; i3 < shape[3]; i3++)
1145 {
1146 (*tensor)(i0, i1, i2, i3) = vals[idx++];
1147 }
1148 }
1149 }
1150 }
1151
1152 return 0;
1153}
1154
1155template <>
1156int TosaReference::Tensor5<double>::setTensorValueDouble(const size_t bufLen, const double* vals)
1157{
1158 uint32_t idx = 0;
1159
1160 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1161
1162 for (int i0 = 0; i0 < shape[0]; i0++)
1163 {
1164 for (int i1 = 0; i1 < shape[1]; i1++)
1165 {
1166 for (int i2 = 0; i2 < shape[2]; i2++)
1167 {
1168 for (int i3 = 0; i3 < shape[3]; i3++)
1169 {
1170 for (int i4 = 0; i4 < shape[4]; i4++)
1171 {
1172 (*tensor)(i0, i1, i2, i3, i4) = vals[idx++];
1173 }
1174 }
1175 }
1176 }
1177 }
1178
1179 return 0;
1180}
1181
1182template <>
1183int TosaReference::Tensor6<double>::setTensorValueDouble(const size_t bufLen, const double* vals)
1184{
1185 uint32_t idx = 0;
1186
1187 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1188
1189 for (int i0 = 0; i0 < shape[0]; i0++)
1190 {
1191 for (int i1 = 0; i1 < shape[1]; i1++)
1192 {
1193 for (int i2 = 0; i2 < shape[2]; i2++)
1194 {
1195 for (int i3 = 0; i3 < shape[3]; i3++)
1196 {
1197 for (int i4 = 0; i4 < shape[4]; i4++)
1198 {
1199 for (int i5 = 0; i5 < shape[5]; i5++)
1200 {
1201 (*tensor)(i0, i1, i2, i3, i4, i5) = vals[idx++];
1202 }
1203 }
1204 }
1205 }
1206 }
1207 }
1208 return 0;
1209}
1210
1211template <class T>
Eric Kunzee5e26762020-10-13 16:11:07 -07001212int TosaReference::TensorTemplate<T>::setTensorValueFloat(const size_t buflen, const float* vals)
1213{
1214 FATAL_ERROR("TensorTemplate<T>::setTensorValueFloat should not be called. "
1215 "Implement template specialization version.");
1216 return 0;
1217}
1218
1219template <>
1220int TosaReference::Tensor0<float>::setTensorValueFloat(const size_t bufLen, const float* vals)
1221{
1222 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1223
1224 (*tensor)(0) = vals[0];
1225
1226 return 0;
1227}
1228
1229template <>
1230int TosaReference::Tensor1<float>::setTensorValueFloat(const size_t bufLen, const float* vals)
1231{
1232 uint32_t idx = 0;
1233
1234 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1235
1236 for (int i0 = 0; i0 < shape[0]; i0++)
1237 {
1238 (*tensor)(i0) = vals[idx++];
1239 }
1240
1241 return 0;
1242}
1243
1244template <>
1245int TosaReference::Tensor2<float>::setTensorValueFloat(const size_t bufLen, const float* vals)
1246{
1247 uint32_t idx = 0;
1248
1249 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1250
1251 for (int i0 = 0; i0 < shape[0]; i0++)
1252 {
1253 for (int i1 = 0; i1 < shape[1]; i1++)
1254 {
1255 (*tensor)(i0, i1) = vals[idx++];
1256 }
1257 }
1258
1259 return 0;
1260}
1261
1262template <>
1263int TosaReference::Tensor3<float>::setTensorValueFloat(const size_t bufLen, const float* vals)
1264{
1265 uint32_t idx = 0;
1266
1267 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1268
1269 for (int i0 = 0; i0 < shape[0]; i0++)
1270 {
1271 for (int i1 = 0; i1 < shape[1]; i1++)
1272 {
1273 for (int i2 = 0; i2 < shape[2]; i2++)
1274 {
1275 (*tensor)(i0, i1, i2) = vals[idx++];
1276 }
1277 }
1278 }
1279
1280 return 0;
1281}
1282
1283template <>
1284int TosaReference::Tensor4<float>::setTensorValueFloat(const size_t bufLen, const float* vals)
1285{
1286 uint32_t idx = 0;
1287
1288 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1289
1290 for (int i0 = 0; i0 < shape[0]; i0++)
1291 {
1292 for (int i1 = 0; i1 < shape[1]; i1++)
1293 {
1294 for (int i2 = 0; i2 < shape[2]; i2++)
1295 {
1296 for (int i3 = 0; i3 < shape[3]; i3++)
1297 {
1298 (*tensor)(i0, i1, i2, i3) = vals[idx++];
1299 }
1300 }
1301 }
1302 }
1303
1304 return 0;
1305}
1306
1307template <>
1308int TosaReference::Tensor5<float>::setTensorValueFloat(const size_t bufLen, const float* vals)
1309{
1310 uint32_t idx = 0;
1311
1312 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1313
1314 for (int i0 = 0; i0 < shape[0]; i0++)
1315 {
1316 for (int i1 = 0; i1 < shape[1]; i1++)
1317 {
1318 for (int i2 = 0; i2 < shape[2]; i2++)
1319 {
1320 for (int i3 = 0; i3 < shape[3]; i3++)
1321 {
1322 for (int i4 = 0; i4 < shape[4]; i4++)
1323 {
1324 (*tensor)(i0, i1, i2, i3, i4) = vals[idx++];
1325 }
1326 }
1327 }
1328 }
1329 }
1330
1331 return 0;
1332}
1333
1334template <>
1335int TosaReference::Tensor6<float>::setTensorValueFloat(const size_t bufLen, const float* vals)
1336{
1337 uint32_t idx = 0;
1338
1339 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1340
1341 for (int i0 = 0; i0 < shape[0]; i0++)
1342 {
1343 for (int i1 = 0; i1 < shape[1]; i1++)
1344 {
1345 for (int i2 = 0; i2 < shape[2]; i2++)
1346 {
1347 for (int i3 = 0; i3 < shape[3]; i3++)
1348 {
1349 for (int i4 = 0; i4 < shape[4]; i4++)
1350 {
1351 for (int i5 = 0; i5 < shape[5]; i5++)
1352 {
1353 (*tensor)(i0, i1, i2, i3, i4, i5) = vals[idx++];
1354 }
1355 }
1356 }
1357 }
1358 }
1359 }
1360 return 0;
1361}
1362
Fabrizio Indirli72038352023-12-11 11:15:32 +00001363template <>
1364int TosaReference::Tensor0<double>::setTensorValueFloat(const size_t bufLen, const float* vals)
1365{
1366 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1367
1368 (*tensor)(0) = vals[0];
1369
1370 return 0;
1371}
1372
1373template <>
1374int TosaReference::Tensor1<double>::setTensorValueFloat(const size_t bufLen, const float* vals)
1375{
1376 uint32_t idx = 0;
1377
1378 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1379
1380 for (int i0 = 0; i0 < shape[0]; i0++)
1381 {
1382 (*tensor)(i0) = vals[idx++];
1383 }
1384
1385 return 0;
1386}
1387
1388template <>
1389int TosaReference::Tensor2<double>::setTensorValueFloat(const size_t bufLen, const float* vals)
1390{
1391 uint32_t idx = 0;
1392
1393 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1394
1395 for (int i0 = 0; i0 < shape[0]; i0++)
1396 {
1397 for (int i1 = 0; i1 < shape[1]; i1++)
1398 {
1399 (*tensor)(i0, i1) = vals[idx++];
1400 }
1401 }
1402
1403 return 0;
1404}
1405
1406template <>
1407int TosaReference::Tensor3<double>::setTensorValueFloat(const size_t bufLen, const float* vals)
1408{
1409 uint32_t idx = 0;
1410
1411 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1412
1413 for (int i0 = 0; i0 < shape[0]; i0++)
1414 {
1415 for (int i1 = 0; i1 < shape[1]; i1++)
1416 {
1417 for (int i2 = 0; i2 < shape[2]; i2++)
1418 {
1419 (*tensor)(i0, i1, i2) = vals[idx++];
1420 }
1421 }
1422 }
1423
1424 return 0;
1425}
1426
1427template <>
1428int TosaReference::Tensor4<double>::setTensorValueFloat(const size_t bufLen, const float* vals)
1429{
1430 uint32_t idx = 0;
1431
1432 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1433
1434 for (int i0 = 0; i0 < shape[0]; i0++)
1435 {
1436 for (int i1 = 0; i1 < shape[1]; i1++)
1437 {
1438 for (int i2 = 0; i2 < shape[2]; i2++)
1439 {
1440 for (int i3 = 0; i3 < shape[3]; i3++)
1441 {
1442 (*tensor)(i0, i1, i2, i3) = vals[idx++];
1443 }
1444 }
1445 }
1446 }
1447
1448 return 0;
1449}
1450
1451template <>
1452int TosaReference::Tensor5<double>::setTensorValueFloat(const size_t bufLen, const float* vals)
1453{
1454 uint32_t idx = 0;
1455
1456 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1457
1458 for (int i0 = 0; i0 < shape[0]; i0++)
1459 {
1460 for (int i1 = 0; i1 < shape[1]; i1++)
1461 {
1462 for (int i2 = 0; i2 < shape[2]; i2++)
1463 {
1464 for (int i3 = 0; i3 < shape[3]; i3++)
1465 {
1466 for (int i4 = 0; i4 < shape[4]; i4++)
1467 {
1468 (*tensor)(i0, i1, i2, i3, i4) = vals[idx++];
1469 }
1470 }
1471 }
1472 }
1473 }
1474
1475 return 0;
1476}
1477
1478template <>
1479int TosaReference::Tensor6<double>::setTensorValueFloat(const size_t bufLen, const float* vals)
1480{
1481 uint32_t idx = 0;
1482
1483 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1484
1485 for (int i0 = 0; i0 < shape[0]; i0++)
1486 {
1487 for (int i1 = 0; i1 < shape[1]; i1++)
1488 {
1489 for (int i2 = 0; i2 < shape[2]; i2++)
1490 {
1491 for (int i3 = 0; i3 < shape[3]; i3++)
1492 {
1493 for (int i4 = 0; i4 < shape[4]; i4++)
1494 {
1495 for (int i5 = 0; i5 < shape[5]; i5++)
1496 {
1497 (*tensor)(i0, i1, i2, i3, i4, i5) = vals[idx++];
1498 }
1499 }
1500 }
1501 }
1502 }
1503 }
1504 return 0;
1505}
1506
Eric Kunzee5e26762020-10-13 16:11:07 -07001507template <class T>
Jerry Gec5291692024-01-02 22:29:08 +00001508int TosaReference::TensorTemplate<T>::setTensorValueUInt8(const size_t bufLen, const uint8_t* vals)
1509{
1510 FATAL_ERROR("TensorTemplate<T>::setTensorValueUInt8 should not be called. "
1511 "Implement template specialization version.");
1512 return 0;
1513}
1514
1515template <>
1516int TosaReference::Tensor0<int32_t>::setTensorValueUInt8(const size_t bufLen, const uint8_t* vals)
1517{
1518 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1519
1520 (*tensor)(0) = static_cast<int32_t>(vals[0]);
1521
1522 return 0;
1523}
1524
1525template <>
1526int TosaReference::Tensor1<int32_t>::setTensorValueUInt8(const size_t bufLen, const uint8_t* vals)
1527{
1528 uint32_t idx = 0;
1529
1530 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1531
1532 for (int i0 = 0; i0 < shape[0]; i0++)
1533 {
1534 (*tensor)(i0) = static_cast<int32_t>(vals[idx++]);
1535 }
1536
1537 return 0;
1538}
1539
1540template <>
1541int TosaReference::Tensor2<int32_t>::setTensorValueUInt8(const size_t bufLen, const uint8_t* vals)
1542{
1543 uint32_t idx = 0;
1544
1545 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1546
1547 for (int i0 = 0; i0 < shape[0]; i0++)
1548 {
1549 for (int i1 = 0; i1 < shape[1]; i1++)
1550 {
1551 (*tensor)(i0, i1) = static_cast<int32_t>(vals[idx++]);
1552 }
1553 }
1554
1555 return 0;
1556}
1557
1558template <>
1559int TosaReference::Tensor3<int32_t>::setTensorValueUInt8(const size_t bufLen, const uint8_t* vals)
1560{
1561 uint32_t idx = 0;
1562
1563 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1564
1565 for (int i0 = 0; i0 < shape[0]; i0++)
1566 {
1567 for (int i1 = 0; i1 < shape[1]; i1++)
1568 {
1569 for (int i2 = 0; i2 < shape[2]; i2++)
1570 {
1571 (*tensor)(i0, i1, i2) = static_cast<int32_t>(vals[idx++]);
1572 }
1573 }
1574 }
1575
1576 return 0;
1577}
1578
1579template <>
1580int TosaReference::Tensor4<int32_t>::setTensorValueUInt8(const size_t bufLen, const uint8_t* vals)
1581{
1582 uint32_t idx = 0;
1583
1584 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1585
1586 for (int i0 = 0; i0 < shape[0]; i0++)
1587 {
1588 for (int i1 = 0; i1 < shape[1]; i1++)
1589 {
1590 for (int i2 = 0; i2 < shape[2]; i2++)
1591 {
1592 for (int i3 = 0; i3 < shape[3]; i3++)
1593 {
1594 (*tensor)(i0, i1, i2, i3) = static_cast<int32_t>(vals[idx++]);
1595 }
1596 }
1597 }
1598 }
1599
1600 return 0;
1601}
1602
1603template <>
1604int TosaReference::Tensor5<int32_t>::setTensorValueUInt8(const size_t bufLen, const uint8_t* vals)
1605{
1606 uint32_t idx = 0;
1607
1608 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1609
1610 for (int i0 = 0; i0 < shape[0]; i0++)
1611 {
1612 for (int i1 = 0; i1 < shape[1]; i1++)
1613 {
1614 for (int i2 = 0; i2 < shape[2]; i2++)
1615 {
1616 for (int i3 = 0; i3 < shape[3]; i3++)
1617 {
1618 for (int i4 = 0; i4 < shape[4]; i4++)
1619 {
1620 (*tensor)(i0, i1, i2, i3, i4) = static_cast<int32_t>(vals[idx++]);
1621 }
1622 }
1623 }
1624 }
1625 }
1626
1627 return 0;
1628}
1629
1630template <>
1631int TosaReference::Tensor6<int32_t>::setTensorValueUInt8(const size_t bufLen, const uint8_t* vals)
1632{
1633 uint32_t idx = 0;
1634
1635 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1636
1637 for (int i0 = 0; i0 < shape[0]; i0++)
1638 {
1639 for (int i1 = 0; i1 < shape[1]; i1++)
1640 {
1641 for (int i2 = 0; i2 < shape[2]; i2++)
1642 {
1643 for (int i3 = 0; i3 < shape[3]; i3++)
1644 {
1645 for (int i4 = 0; i4 < shape[4]; i4++)
1646 {
1647 for (int i5 = 0; i5 < shape[5]; i5++)
1648 {
1649 (*tensor)(i0, i1, i2, i3, i4, i5) = static_cast<int32_t>(vals[idx++]);
1650 }
1651 }
1652 }
1653 }
1654 }
1655 }
1656 return 0;
1657}
1658
1659template <class T>
1660int TosaReference::TensorTemplate<T>::setTensorValueInt8(const size_t bufLen, const int8_t* vals)
1661{
1662 FATAL_ERROR("TensorTemplate<T>::setTensorValueInt8 should not be called. "
1663 "Implement template specialization version.");
1664 return 0;
1665}
1666
1667template <>
1668int TosaReference::Tensor0<int32_t>::setTensorValueInt8(const size_t bufLen, const int8_t* vals)
1669{
1670 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1671
1672 (*tensor)(0) = static_cast<int32_t>(vals[0]);
1673
1674 return 0;
1675}
1676
1677template <>
1678int TosaReference::Tensor1<int32_t>::setTensorValueInt8(const size_t bufLen, const int8_t* vals)
1679{
1680 uint32_t idx = 0;
1681
1682 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1683
1684 for (int i0 = 0; i0 < shape[0]; i0++)
1685 {
1686 (*tensor)(i0) = static_cast<int32_t>(vals[idx++]);
1687 }
1688
1689 return 0;
1690}
1691
1692template <>
1693int TosaReference::Tensor2<int32_t>::setTensorValueInt8(const size_t bufLen, const int8_t* vals)
1694{
1695 uint32_t idx = 0;
1696
1697 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1698
1699 for (int i0 = 0; i0 < shape[0]; i0++)
1700 {
1701 for (int i1 = 0; i1 < shape[1]; i1++)
1702 {
1703 (*tensor)(i0, i1) = static_cast<int32_t>(vals[idx++]);
1704 }
1705 }
1706
1707 return 0;
1708}
1709
1710template <>
1711int TosaReference::Tensor3<int32_t>::setTensorValueInt8(const size_t bufLen, const int8_t* vals)
1712{
1713 uint32_t idx = 0;
1714
1715 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1716
1717 for (int i0 = 0; i0 < shape[0]; i0++)
1718 {
1719 for (int i1 = 0; i1 < shape[1]; i1++)
1720 {
1721 for (int i2 = 0; i2 < shape[2]; i2++)
1722 {
1723 (*tensor)(i0, i1, i2) = static_cast<int32_t>(vals[idx++]);
1724 }
1725 }
1726 }
1727
1728 return 0;
1729}
1730
1731template <>
1732int TosaReference::Tensor4<int32_t>::setTensorValueInt8(const size_t bufLen, const int8_t* vals)
1733{
1734 uint32_t idx = 0;
1735
1736 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1737
1738 for (int i0 = 0; i0 < shape[0]; i0++)
1739 {
1740 for (int i1 = 0; i1 < shape[1]; i1++)
1741 {
1742 for (int i2 = 0; i2 < shape[2]; i2++)
1743 {
1744 for (int i3 = 0; i3 < shape[3]; i3++)
1745 {
1746 (*tensor)(i0, i1, i2, i3) = static_cast<int32_t>(vals[idx++]);
1747 }
1748 }
1749 }
1750 }
1751
1752 return 0;
1753}
1754
1755template <>
1756int TosaReference::Tensor5<int32_t>::setTensorValueInt8(const size_t bufLen, const int8_t* vals)
1757{
1758 uint32_t idx = 0;
1759
1760 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1761
1762 for (int i0 = 0; i0 < shape[0]; i0++)
1763 {
1764 for (int i1 = 0; i1 < shape[1]; i1++)
1765 {
1766 for (int i2 = 0; i2 < shape[2]; i2++)
1767 {
1768 for (int i3 = 0; i3 < shape[3]; i3++)
1769 {
1770 for (int i4 = 0; i4 < shape[4]; i4++)
1771 {
1772 (*tensor)(i0, i1, i2, i3, i4) = static_cast<int32_t>(vals[idx++]);
1773 }
1774 }
1775 }
1776 }
1777 }
1778
1779 return 0;
1780}
1781
1782template <>
1783int TosaReference::Tensor6<int32_t>::setTensorValueInt8(const size_t bufLen, const int8_t* vals)
1784{
1785 uint32_t idx = 0;
1786
1787 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1788
1789 for (int i0 = 0; i0 < shape[0]; i0++)
1790 {
1791 for (int i1 = 0; i1 < shape[1]; i1++)
1792 {
1793 for (int i2 = 0; i2 < shape[2]; i2++)
1794 {
1795 for (int i3 = 0; i3 < shape[3]; i3++)
1796 {
1797 for (int i4 = 0; i4 < shape[4]; i4++)
1798 {
1799 for (int i5 = 0; i5 < shape[5]; i5++)
1800 {
1801 (*tensor)(i0, i1, i2, i3, i4, i5) = static_cast<int32_t>(vals[idx++]);
1802 }
1803 }
1804 }
1805 }
1806 }
1807 }
1808 return 0;
1809}
1810
1811template <class T>
Georgios Pinitase9059772023-12-06 18:52:30 +00001812int TosaReference::TensorTemplate<T>::setTensorValueInt16(const size_t bufLen, const int16_t* vals)
1813{
1814 FATAL_ERROR("TensorTemplate<T>::setTensorValueInt32 should not be called. "
1815 "Implement template specialization version.");
1816 return 0;
1817}
1818
1819template <>
1820int TosaReference::Tensor0<int32_t>::setTensorValueInt16(const size_t bufLen, const int16_t* vals)
1821{
1822 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1823
1824 (*tensor)(0) = static_cast<int32_t>(vals[0]);
1825
1826 return 0;
1827}
1828
1829template <>
1830int TosaReference::Tensor1<int32_t>::setTensorValueInt16(const size_t bufLen, const int16_t* vals)
1831{
1832 uint32_t idx = 0;
1833
1834 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1835
1836 for (int i0 = 0; i0 < shape[0]; i0++)
1837 {
1838 (*tensor)(i0) = static_cast<int32_t>(vals[idx++]);
1839 }
1840
1841 return 0;
1842}
1843
1844template <>
1845int TosaReference::Tensor2<int32_t>::setTensorValueInt16(const size_t bufLen, const int16_t* vals)
1846{
1847 uint32_t idx = 0;
1848
1849 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1850
1851 for (int i0 = 0; i0 < shape[0]; i0++)
1852 {
1853 for (int i1 = 0; i1 < shape[1]; i1++)
1854 {
1855 (*tensor)(i0, i1) = static_cast<int32_t>(vals[idx++]);
1856 }
1857 }
1858
1859 return 0;
1860}
1861
1862template <>
1863int TosaReference::Tensor3<int32_t>::setTensorValueInt16(const size_t bufLen, const int16_t* vals)
1864{
1865 uint32_t idx = 0;
1866
1867 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1868
1869 for (int i0 = 0; i0 < shape[0]; i0++)
1870 {
1871 for (int i1 = 0; i1 < shape[1]; i1++)
1872 {
1873 for (int i2 = 0; i2 < shape[2]; i2++)
1874 {
1875 (*tensor)(i0, i1, i2) = static_cast<int32_t>(vals[idx++]);
1876 }
1877 }
1878 }
1879
1880 return 0;
1881}
1882
1883template <>
1884int TosaReference::Tensor4<int32_t>::setTensorValueInt16(const size_t bufLen, const int16_t* vals)
1885{
1886 uint32_t idx = 0;
1887
1888 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1889
1890 for (int i0 = 0; i0 < shape[0]; i0++)
1891 {
1892 for (int i1 = 0; i1 < shape[1]; i1++)
1893 {
1894 for (int i2 = 0; i2 < shape[2]; i2++)
1895 {
1896 for (int i3 = 0; i3 < shape[3]; i3++)
1897 {
1898 (*tensor)(i0, i1, i2, i3) = static_cast<int32_t>(vals[idx++]);
1899 }
1900 }
1901 }
1902 }
1903
1904 return 0;
1905}
1906
1907template <>
1908int TosaReference::Tensor5<int32_t>::setTensorValueInt16(const size_t bufLen, const int16_t* vals)
1909{
1910 uint32_t idx = 0;
1911
1912 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1913
1914 for (int i0 = 0; i0 < shape[0]; i0++)
1915 {
1916 for (int i1 = 0; i1 < shape[1]; i1++)
1917 {
1918 for (int i2 = 0; i2 < shape[2]; i2++)
1919 {
1920 for (int i3 = 0; i3 < shape[3]; i3++)
1921 {
1922 for (int i4 = 0; i4 < shape[4]; i4++)
1923 {
1924 (*tensor)(i0, i1, i2, i3, i4) = static_cast<int32_t>(vals[idx++]);
1925 }
1926 }
1927 }
1928 }
1929 }
1930
1931 return 0;
1932}
1933
1934template <>
1935int TosaReference::Tensor6<int32_t>::setTensorValueInt16(const size_t bufLen, const int16_t* vals)
1936{
1937 uint32_t idx = 0;
1938
1939 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1940
1941 for (int i0 = 0; i0 < shape[0]; i0++)
1942 {
1943 for (int i1 = 0; i1 < shape[1]; i1++)
1944 {
1945 for (int i2 = 0; i2 < shape[2]; i2++)
1946 {
1947 for (int i3 = 0; i3 < shape[3]; i3++)
1948 {
1949 for (int i4 = 0; i4 < shape[4]; i4++)
1950 {
1951 for (int i5 = 0; i5 < shape[5]; i5++)
1952 {
1953 (*tensor)(i0, i1, i2, i3, i4, i5) = static_cast<int32_t>(vals[idx++]);
1954 }
1955 }
1956 }
1957 }
1958 }
1959 }
1960 return 0;
1961}
1962
1963template <class T>
Eric Kunzee5e26762020-10-13 16:11:07 -07001964int TosaReference::TensorTemplate<T>::setTensorValueInt32(const size_t bufLen, const int32_t* vals)
1965{
1966 FATAL_ERROR("TensorTemplate<T>::setTensorValueInt32 should not be called. "
1967 "Implement template specialization version.");
1968 return 0;
1969}
1970
1971template <>
1972int TosaReference::Tensor0<int32_t>::setTensorValueInt32(const size_t bufLen, const int32_t* vals)
1973{
1974 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1975
1976 (*tensor)(0) = vals[0];
1977
1978 return 0;
1979}
1980
1981template <>
1982int TosaReference::Tensor1<int32_t>::setTensorValueInt32(const size_t bufLen, const int32_t* vals)
1983{
1984 uint32_t idx = 0;
1985
1986 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1987
1988 for (int i0 = 0; i0 < shape[0]; i0++)
1989 {
1990 (*tensor)(i0) = vals[idx++];
1991 }
1992
1993 return 0;
1994}
1995
1996template <>
1997int TosaReference::Tensor2<int32_t>::setTensorValueInt32(const size_t bufLen, const int32_t* vals)
1998{
1999 uint32_t idx = 0;
2000
2001 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
2002
2003 for (int i0 = 0; i0 < shape[0]; i0++)
2004 {
2005 for (int i1 = 0; i1 < shape[1]; i1++)
2006 {
2007 (*tensor)(i0, i1) = vals[idx++];
2008 }
2009 }
2010
2011 return 0;
2012}
2013
2014template <>
2015int TosaReference::Tensor3<int32_t>::setTensorValueInt32(const size_t bufLen, const int32_t* vals)
2016{
2017 uint32_t idx = 0;
2018
2019 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
2020
2021 for (int i0 = 0; i0 < shape[0]; i0++)
2022 {
2023 for (int i1 = 0; i1 < shape[1]; i1++)
2024 {
2025 for (int i2 = 0; i2 < shape[2]; i2++)
2026 {
2027 (*tensor)(i0, i1, i2) = vals[idx++];
2028 }
2029 }
2030 }
2031
2032 return 0;
2033}
2034
2035template <>
2036int TosaReference::Tensor4<int32_t>::setTensorValueInt32(const size_t bufLen, const int32_t* vals)
2037{
2038 uint32_t idx = 0;
2039
2040 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
2041
2042 for (int i0 = 0; i0 < shape[0]; i0++)
2043 {
2044 for (int i1 = 0; i1 < shape[1]; i1++)
2045 {
2046 for (int i2 = 0; i2 < shape[2]; i2++)
2047 {
2048 for (int i3 = 0; i3 < shape[3]; i3++)
2049 {
2050 (*tensor)(i0, i1, i2, i3) = vals[idx++];
2051 }
2052 }
2053 }
2054 }
2055
2056 return 0;
2057}
2058
2059template <>
2060int TosaReference::Tensor5<int32_t>::setTensorValueInt32(const size_t bufLen, const int32_t* vals)
2061{
2062 uint32_t idx = 0;
2063
2064 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
2065
2066 for (int i0 = 0; i0 < shape[0]; i0++)
2067 {
2068 for (int i1 = 0; i1 < shape[1]; i1++)
2069 {
2070 for (int i2 = 0; i2 < shape[2]; i2++)
2071 {
2072 for (int i3 = 0; i3 < shape[3]; i3++)
2073 {
2074 for (int i4 = 0; i4 < shape[4]; i4++)
2075 {
2076 (*tensor)(i0, i1, i2, i3, i4) = vals[idx++];
2077 }
2078 }
2079 }
2080 }
2081 }
2082
2083 return 0;
2084}
2085
2086template <>
2087int TosaReference::Tensor6<int32_t>::setTensorValueInt32(const size_t bufLen, const int32_t* vals)
2088{
2089 uint32_t idx = 0;
2090
2091 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
2092
2093 for (int i0 = 0; i0 < shape[0]; i0++)
2094 {
2095 for (int i1 = 0; i1 < shape[1]; i1++)
2096 {
2097 for (int i2 = 0; i2 < shape[2]; i2++)
2098 {
2099 for (int i3 = 0; i3 < shape[3]; i3++)
2100 {
2101 for (int i4 = 0; i4 < shape[4]; i4++)
2102 {
2103 for (int i5 = 0; i5 < shape[5]; i5++)
2104 {
2105 (*tensor)(i0, i1, i2, i3, i4, i5) = vals[idx++];
2106 }
2107 }
2108 }
2109 }
2110 }
2111 }
2112 return 0;
2113}
2114
2115template <class T>
2116int TosaReference::TensorTemplate<T>::setTensorValueInt64(const size_t bufLen, const int64_t* vals)
2117{
2118 FATAL_ERROR("TensorTemplate<T>::setTensorValueInt64 should not be called. "
2119 "Implement template specialization version.");
2120 return 0;
2121}
2122
2123template <>
2124int TosaReference::Tensor0<int64_t>::setTensorValueInt64(const size_t bufLen, const int64_t* vals)
2125{
2126 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
2127
2128 (*tensor)(0) = vals[0];
2129
2130 return 0;
2131}
2132
2133template <>
2134int TosaReference::Tensor1<int64_t>::setTensorValueInt64(const size_t bufLen, const int64_t* vals)
2135{
2136 uint32_t idx = 0;
2137
2138 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
2139
2140 for (int i0 = 0; i0 < shape[0]; i0++)
2141 {
2142 (*tensor)(i0) = vals[idx++];
2143 }
2144
2145 return 0;
2146}
2147
2148template <>
2149int TosaReference::Tensor2<int64_t>::setTensorValueInt64(const size_t bufLen, const int64_t* vals)
2150{
2151 uint32_t idx = 0;
2152
2153 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
2154
2155 for (int i0 = 0; i0 < shape[0]; i0++)
2156 {
2157 for (int i1 = 0; i1 < shape[1]; i1++)
2158 {
2159 (*tensor)(i0, i1) = vals[idx++];
2160 }
2161 }
2162
2163 return 0;
2164}
2165
2166template <>
2167int TosaReference::Tensor3<int64_t>::setTensorValueInt64(const size_t bufLen, const int64_t* vals)
2168{
2169 uint32_t idx = 0;
2170
2171 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
2172
2173 for (int i0 = 0; i0 < shape[0]; i0++)
2174 {
2175 for (int i1 = 0; i1 < shape[1]; i1++)
2176 {
2177 for (int i2 = 0; i2 < shape[2]; i2++)
2178 {
2179 (*tensor)(i0, i1, i2) = vals[idx++];
2180 }
2181 }
2182 }
2183
2184 return 0;
2185}
2186
2187template <>
2188int TosaReference::Tensor4<int64_t>::setTensorValueInt64(const size_t bufLen, const int64_t* vals)
2189{
2190 uint32_t idx = 0;
2191
2192 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
2193
2194 for (int i0 = 0; i0 < shape[0]; i0++)
2195 {
2196 for (int i1 = 0; i1 < shape[1]; i1++)
2197 {
2198 for (int i2 = 0; i2 < shape[2]; i2++)
2199 {
2200 for (int i3 = 0; i3 < shape[3]; i3++)
2201 {
2202 (*tensor)(i0, i1, i2, i3) = vals[idx++];
2203 }
2204 }
2205 }
2206 }
2207
2208 return 0;
2209}
2210
2211template <>
2212int TosaReference::Tensor5<int64_t>::setTensorValueInt64(const size_t bufLen, const int64_t* vals)
2213{
2214 uint32_t idx = 0;
2215
2216 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
2217
2218 for (int i0 = 0; i0 < shape[0]; i0++)
2219 {
2220 for (int i1 = 0; i1 < shape[1]; i1++)
2221 {
2222 for (int i2 = 0; i2 < shape[2]; i2++)
2223 {
2224 for (int i3 = 0; i3 < shape[3]; i3++)
2225 {
2226 for (int i4 = 0; i4 < shape[4]; i4++)
2227 {
2228 (*tensor)(i0, i1, i2, i3, i4) = vals[idx++];
2229 }
2230 }
2231 }
2232 }
2233 }
2234
2235 return 0;
2236}
2237
2238template <>
2239int TosaReference::Tensor6<int64_t>::setTensorValueInt64(const size_t bufLen, const int64_t* vals)
2240{
2241 uint32_t idx = 0;
2242
2243 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
2244
2245 for (int i0 = 0; i0 < shape[0]; i0++)
2246 {
2247 for (int i1 = 0; i1 < shape[1]; i1++)
2248 {
2249 for (int i2 = 0; i2 < shape[2]; i2++)
2250 {
2251 for (int i3 = 0; i3 < shape[3]; i3++)
2252 {
2253 for (int i4 = 0; i4 < shape[4]; i4++)
2254 {
2255 for (int i5 = 0; i5 < shape[5]; i5++)
2256 {
2257 (*tensor)(i0, i1, i2, i3, i4, i5) = vals[idx++];
2258 }
2259 }
2260 }
2261 }
2262 }
2263 }
2264 return 0;
2265}
2266
2267template <class T>
2268int TosaReference::TensorTemplate<T>::setTensorValueBool(const size_t buflen, const bool* vals)
2269{
2270 FATAL_ERROR("TensorTemplate<T>::setTensorValueBool should not be called. "
2271 "Implement template specialization version.");
2272 return 0;
2273}
2274
2275template <>
2276int TosaReference::Tensor0<bool>::setTensorValueBool(const size_t bufLen, const bool* vals)
2277{
2278 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
2279
2280 (*tensor)(0) = vals[0];
2281
2282 return 0;
2283}
2284
2285template <>
2286int TosaReference::Tensor1<bool>::setTensorValueBool(const size_t bufLen, const bool* vals)
2287{
2288 uint32_t idx = 0;
2289
2290 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
2291
2292 for (int i0 = 0; i0 < shape[0]; i0++)
2293 {
2294 (*tensor)(i0) = vals[idx++];
2295 }
2296
2297 return 0;
2298}
2299
2300template <>
2301int TosaReference::Tensor2<bool>::setTensorValueBool(const size_t bufLen, const bool* vals)
2302{
2303 uint32_t idx = 0;
2304
2305 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
2306
2307 for (int i0 = 0; i0 < shape[0]; i0++)
2308 {
2309 for (int i1 = 0; i1 < shape[1]; i1++)
2310 {
2311 (*tensor)(i0, i1) = vals[idx++];
2312 }
2313 }
2314
2315 return 0;
2316}
2317
2318template <>
2319int TosaReference::Tensor3<bool>::setTensorValueBool(const size_t bufLen, const bool* vals)
2320{
2321 uint32_t idx = 0;
2322
2323 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
2324
2325 for (int i0 = 0; i0 < shape[0]; i0++)
2326 {
2327 for (int i1 = 0; i1 < shape[1]; i1++)
2328 {
2329 for (int i2 = 0; i2 < shape[2]; i2++)
2330 {
2331 (*tensor)(i0, i1, i2) = vals[idx++];
2332 }
2333 }
2334 }
2335
2336 return 0;
2337}
2338
2339template <>
2340int TosaReference::Tensor4<bool>::setTensorValueBool(const size_t bufLen, const bool* vals)
2341{
2342 uint32_t idx = 0;
2343
2344 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
2345
2346 for (int i0 = 0; i0 < shape[0]; i0++)
2347 {
2348 for (int i1 = 0; i1 < shape[1]; i1++)
2349 {
2350 for (int i2 = 0; i2 < shape[2]; i2++)
2351 {
2352 for (int i3 = 0; i3 < shape[3]; i3++)
2353 {
2354 (*tensor)(i0, i1, i2, i3) = vals[idx++];
2355 }
2356 }
2357 }
2358 }
2359
2360 return 0;
2361}
2362
2363template <>
2364int TosaReference::Tensor5<bool>::setTensorValueBool(const size_t bufLen, const bool* vals)
2365{
2366 uint32_t idx = 0;
2367
2368 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
2369
2370 for (int i0 = 0; i0 < shape[0]; i0++)
2371 {
2372 for (int i1 = 0; i1 < shape[1]; i1++)
2373 {
2374 for (int i2 = 0; i2 < shape[2]; i2++)
2375 {
2376 for (int i3 = 0; i3 < shape[3]; i3++)
2377 {
2378 for (int i4 = 0; i4 < shape[4]; i4++)
2379 {
2380 (*tensor)(i0, i1, i2, i3, i4) = vals[idx++];
2381 }
2382 }
2383 }
2384 }
2385 }
2386
2387 return 0;
2388}
2389
2390template <>
2391int TosaReference::Tensor6<bool>::setTensorValueBool(const size_t bufLen, const bool* vals)
2392{
2393 uint32_t idx = 0;
2394
2395 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
2396
2397 for (int i0 = 0; i0 < shape[0]; i0++)
2398 {
2399 for (int i1 = 0; i1 < shape[1]; i1++)
2400 {
2401 for (int i2 = 0; i2 < shape[2]; i2++)
2402 {
2403 for (int i3 = 0; i3 < shape[3]; i3++)
2404 {
2405 for (int i4 = 0; i4 < shape[4]; i4++)
2406 {
2407 for (int i5 = 0; i5 < shape[5]; i5++)
2408 {
2409 (*tensor)(i0, i1, i2, i3, i4, i5) = vals[idx++];
2410 }
2411 }
2412 }
2413 }
2414 }
2415 }
2416 return 0;
2417}
2418
2419template <class T>
Tai Lya4d748b2023-03-28 22:06:56 +00002420int TosaReference::TensorTemplate<T>::getTensorValueDouble(const size_t bufLen, double* vals) const
2421{
2422 FATAL_ERROR("TensorTemplate<T>::getTensorValueDouble should not be called. "
2423 "Implement template specialization version.");
2424 return 0;
2425}
2426
2427template <>
2428int TosaReference::Tensor0<double>::getTensorValueDouble(const size_t bufLen, double* vals) const
2429{
2430 int totalVals = 1;
2431
2432 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
2433
2434 vals[0] = (*tensor)(0);
2435
2436 return 0;
2437}
2438
2439template <>
2440int TosaReference::Tensor1<double>::getTensorValueDouble(const size_t bufLen, double* vals) const
2441{
2442 uint32_t idx = 0;
2443 int totalVals = 1;
2444
2445 for (size_t i = 0; i < shape.size(); i++)
2446 {
2447 totalVals *= shape[i];
2448 }
2449
2450 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
2451
2452 for (int i0 = 0; i0 < shape[0]; i0++)
2453 {
2454 vals[idx++] = (*tensor)(i0);
2455 }
2456
2457 return 0;
2458}
2459
2460template <>
2461int TosaReference::Tensor2<double>::getTensorValueDouble(const size_t bufLen, double* vals) const
2462{
2463 uint32_t idx = 0;
2464 int totalVals = 1;
2465
2466 for (size_t i = 0; i < shape.size(); i++)
2467 {
2468 totalVals *= shape[i];
2469 }
2470
2471 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
2472
2473 for (int i0 = 0; i0 < shape[0]; i0++)
2474 {
2475 for (int i1 = 0; i1 < shape[1]; i1++)
2476 {
2477 vals[idx++] = (*tensor)(i0, i1);
2478 }
2479 }
2480
2481 return 0;
2482}
2483
2484template <>
2485int TosaReference::Tensor3<double>::getTensorValueDouble(const size_t bufLen, double* vals) const
2486{
2487 uint32_t idx = 0;
2488 int totalVals = 1;
2489
2490 for (size_t i = 0; i < shape.size(); i++)
2491 {
2492 totalVals *= shape[i];
2493 }
2494
2495 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
2496
2497 for (int i0 = 0; i0 < shape[0]; i0++)
2498 {
2499 for (int i1 = 0; i1 < shape[1]; i1++)
2500 {
2501 for (int i2 = 0; i2 < shape[2]; i2++)
2502 {
2503 vals[idx++] = (*tensor)(i0, i1, i2);
2504 }
2505 }
2506 }
2507
2508 return 0;
2509}
2510
2511template <>
2512int TosaReference::Tensor4<double>::getTensorValueDouble(const size_t bufLen, double* vals) const
2513{
2514 uint32_t idx = 0;
2515 int totalVals = 1;
2516
2517 for (size_t i = 0; i < shape.size(); i++)
2518 {
2519 totalVals *= shape[i];
2520 }
2521
2522 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
2523
2524 for (int i0 = 0; i0 < shape[0]; i0++)
2525 {
2526 for (int i1 = 0; i1 < shape[1]; i1++)
2527 {
2528 for (int i2 = 0; i2 < shape[2]; i2++)
2529 {
2530 for (int i3 = 0; i3 < shape[3]; i3++)
2531 {
2532 vals[idx++] = (*tensor)(i0, i1, i2, i3);
2533 }
2534 }
2535 }
2536 }
2537
2538 return 0;
2539}
2540
2541template <>
2542int TosaReference::Tensor5<double>::getTensorValueDouble(const size_t bufLen, double* vals) const
2543{
2544 uint32_t idx = 0;
2545 int totalVals = 1;
2546
2547 for (size_t i = 0; i < shape.size(); i++)
2548 {
2549 totalVals *= shape[i];
2550 }
2551
2552 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
2553
2554 for (int i0 = 0; i0 < shape[0]; i0++)
2555 {
2556 for (int i1 = 0; i1 < shape[1]; i1++)
2557 {
2558 for (int i2 = 0; i2 < shape[2]; i2++)
2559 {
2560 for (int i3 = 0; i3 < shape[3]; i3++)
2561 {
2562 for (int i4 = 0; i4 < shape[4]; i4++)
2563 {
2564 vals[idx++] = (*tensor)(i0, i1, i2, i3, i4);
2565 }
2566 }
2567 }
2568 }
2569 }
2570
2571 return 0;
2572}
2573
2574template <>
2575int TosaReference::Tensor6<double>::getTensorValueDouble(const size_t bufLen, double* vals) const
2576{
2577 uint32_t idx = 0;
2578 int totalVals = 1;
2579
2580 for (size_t i = 0; i < shape.size(); i++)
2581 {
2582 totalVals *= shape[i];
2583 }
2584
2585 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
2586
2587 for (int i0 = 0; i0 < shape[0]; i0++)
2588 {
2589 for (int i1 = 0; i1 < shape[1]; i1++)
2590 {
2591 for (int i2 = 0; i2 < shape[2]; i2++)
2592 {
2593 for (int i3 = 0; i3 < shape[3]; i3++)
2594 {
2595 for (int i4 = 0; i4 < shape[4]; i4++)
2596 {
2597 for (int i5 = 0; i5 < shape[5]; i5++)
2598 {
2599 vals[idx++] = (*tensor)(i0, i1, i2, i3, i4, i5);
2600 }
2601 }
2602 }
2603 }
2604 }
2605 }
2606 return 0;
2607}
2608
2609template <class T>
Eric Kunzee5e26762020-10-13 16:11:07 -07002610int TosaReference::TensorTemplate<T>::getTensorValueFloat(const size_t bufLen, float* vals) const
2611{
2612 FATAL_ERROR("TensorTemplate<T>::getTensorValueFloat should not be called. "
2613 "Implement template specialization version.");
2614 return 0;
2615}
2616
2617template <>
2618int TosaReference::Tensor0<float>::getTensorValueFloat(const size_t bufLen, float* vals) const
2619{
2620 int totalVals = 1;
2621
2622 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
2623
2624 vals[0] = (*tensor)(0);
2625
2626 return 0;
2627}
2628
2629template <>
2630int TosaReference::Tensor1<float>::getTensorValueFloat(const size_t bufLen, float* vals) const
2631{
2632 uint32_t idx = 0;
2633 int totalVals = 1;
2634
2635 for (size_t i = 0; i < shape.size(); i++)
2636 {
2637 totalVals *= shape[i];
2638 }
2639
2640 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
2641
2642 for (int i0 = 0; i0 < shape[0]; i0++)
2643 {
2644 vals[idx++] = (*tensor)(i0);
2645 }
2646
2647 return 0;
2648}
2649
2650template <>
2651int TosaReference::Tensor2<float>::getTensorValueFloat(const size_t bufLen, float* vals) const
2652{
2653 uint32_t idx = 0;
2654 int totalVals = 1;
2655
2656 for (size_t i = 0; i < shape.size(); i++)
2657 {
2658 totalVals *= shape[i];
2659 }
2660
2661 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
2662
2663 for (int i0 = 0; i0 < shape[0]; i0++)
2664 {
2665 for (int i1 = 0; i1 < shape[1]; i1++)
2666 {
2667 vals[idx++] = (*tensor)(i0, i1);
2668 }
2669 }
2670
2671 return 0;
2672}
2673
2674template <>
2675int TosaReference::Tensor3<float>::getTensorValueFloat(const size_t bufLen, float* vals) const
2676{
2677 uint32_t idx = 0;
2678 int totalVals = 1;
2679
2680 for (size_t i = 0; i < shape.size(); i++)
2681 {
2682 totalVals *= shape[i];
2683 }
2684
2685 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
2686
2687 for (int i0 = 0; i0 < shape[0]; i0++)
2688 {
2689 for (int i1 = 0; i1 < shape[1]; i1++)
2690 {
2691 for (int i2 = 0; i2 < shape[2]; i2++)
2692 {
2693 vals[idx++] = (*tensor)(i0, i1, i2);
2694 }
2695 }
2696 }
2697
2698 return 0;
2699}
2700
2701template <>
2702int TosaReference::Tensor4<float>::getTensorValueFloat(const size_t bufLen, float* vals) const
2703{
2704 uint32_t idx = 0;
2705 int totalVals = 1;
2706
2707 for (size_t i = 0; i < shape.size(); i++)
2708 {
2709 totalVals *= shape[i];
2710 }
2711
2712 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
2713
2714 for (int i0 = 0; i0 < shape[0]; i0++)
2715 {
2716 for (int i1 = 0; i1 < shape[1]; i1++)
2717 {
2718 for (int i2 = 0; i2 < shape[2]; i2++)
2719 {
2720 for (int i3 = 0; i3 < shape[3]; i3++)
2721 {
2722 vals[idx++] = (*tensor)(i0, i1, i2, i3);
2723 }
2724 }
2725 }
2726 }
2727
2728 return 0;
2729}
2730
2731template <>
2732int TosaReference::Tensor5<float>::getTensorValueFloat(const size_t bufLen, float* vals) const
2733{
2734 uint32_t idx = 0;
2735 int totalVals = 1;
2736
2737 for (size_t i = 0; i < shape.size(); i++)
2738 {
2739 totalVals *= shape[i];
2740 }
2741
2742 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
2743
2744 for (int i0 = 0; i0 < shape[0]; i0++)
2745 {
2746 for (int i1 = 0; i1 < shape[1]; i1++)
2747 {
2748 for (int i2 = 0; i2 < shape[2]; i2++)
2749 {
2750 for (int i3 = 0; i3 < shape[3]; i3++)
2751 {
2752 for (int i4 = 0; i4 < shape[4]; i4++)
2753 {
2754 vals[idx++] = (*tensor)(i0, i1, i2, i3, i4);
2755 }
2756 }
2757 }
2758 }
2759 }
2760
2761 return 0;
2762}
2763
2764template <>
2765int TosaReference::Tensor6<float>::getTensorValueFloat(const size_t bufLen, float* vals) const
2766{
2767 uint32_t idx = 0;
2768 int totalVals = 1;
2769
2770 for (size_t i = 0; i < shape.size(); i++)
2771 {
2772 totalVals *= shape[i];
2773 }
2774
2775 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
2776
2777 for (int i0 = 0; i0 < shape[0]; i0++)
2778 {
2779 for (int i1 = 0; i1 < shape[1]; i1++)
2780 {
2781 for (int i2 = 0; i2 < shape[2]; i2++)
2782 {
2783 for (int i3 = 0; i3 < shape[3]; i3++)
2784 {
2785 for (int i4 = 0; i4 < shape[4]; i4++)
2786 {
2787 for (int i5 = 0; i5 < shape[5]; i5++)
2788 {
2789 vals[idx++] = (*tensor)(i0, i1, i2, i3, i4, i5);
2790 }
2791 }
2792 }
2793 }
2794 }
2795 }
2796 return 0;
2797}
2798
2799template <class T>
Jerry Gec5291692024-01-02 22:29:08 +00002800int TosaReference::TensorTemplate<T>::getTensorValueUInt8(const size_t bufLen, uint8_t* vals) const
2801{
2802 std::cout << "T is: " << typeid(T).name() << std::endl;
2803 FATAL_ERROR("TensorTemplate<T>::getTensorValueUInt8 should not be called. "
2804 "Implement template specialization version.");
2805 return 0;
2806}
2807
2808template <>
2809int TosaReference::Tensor0<int32_t>::getTensorValueUInt8(const size_t bufLen, uint8_t* vals) const
2810{
2811 int totalVals = 1;
2812
2813 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
2814
2815 vals[0] = (*tensor)(0);
2816
2817 return 0;
2818}
2819
2820template <>
2821int TosaReference::Tensor1<int32_t>::getTensorValueUInt8(const size_t bufLen, uint8_t* vals) const
2822{
2823 uint32_t idx = 0;
2824 int totalVals = 1;
2825
2826 for (size_t i = 0; i < shape.size(); i++)
2827 {
2828 totalVals *= shape[i];
2829 }
2830
2831 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
2832
2833 for (int i0 = 0; i0 < shape[0]; i0++)
2834 {
2835 vals[idx++] = (*tensor)(i0);
2836 }
2837
2838 return 0;
2839}
2840
2841template <>
2842int TosaReference::Tensor2<int32_t>::getTensorValueUInt8(const size_t bufLen, uint8_t* vals) const
2843{
2844 uint32_t idx = 0;
2845 int totalVals = 1;
2846
2847 for (size_t i = 0; i < shape.size(); i++)
2848 {
2849 totalVals *= shape[i];
2850 }
2851
2852 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
2853
2854 for (int i0 = 0; i0 < shape[0]; i0++)
2855 {
2856 for (int i1 = 0; i1 < shape[1]; i1++)
2857 {
2858 vals[idx++] = (*tensor)(i0, i1);
2859 }
2860 }
2861
2862 return 0;
2863}
2864
2865template <>
2866int TosaReference::Tensor3<int32_t>::getTensorValueUInt8(const size_t bufLen, uint8_t* vals) const
2867{
2868 uint32_t idx = 0;
2869 int totalVals = 1;
2870
2871 for (size_t i = 0; i < shape.size(); i++)
2872 {
2873 totalVals *= shape[i];
2874 }
2875
2876 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
2877
2878 for (int i0 = 0; i0 < shape[0]; i0++)
2879 {
2880 for (int i1 = 0; i1 < shape[1]; i1++)
2881 {
2882 for (int i2 = 0; i2 < shape[2]; i2++)
2883 {
2884 vals[idx++] = (*tensor)(i0, i1, i2);
2885 }
2886 }
2887 }
2888
2889 return 0;
2890}
2891
2892template <>
2893int TosaReference::Tensor4<int32_t>::getTensorValueUInt8(const size_t bufLen, uint8_t* vals) const
2894{
2895 uint32_t idx = 0;
2896 int totalVals = 1;
2897
2898 for (size_t i = 0; i < shape.size(); i++)
2899 {
2900 totalVals *= shape[i];
2901 }
2902
2903 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
2904
2905 for (int i0 = 0; i0 < shape[0]; i0++)
2906 {
2907 for (int i1 = 0; i1 < shape[1]; i1++)
2908 {
2909 for (int i2 = 0; i2 < shape[2]; i2++)
2910 {
2911 for (int i3 = 0; i3 < shape[3]; i3++)
2912 {
2913 vals[idx++] = (*tensor)(i0, i1, i2, i3);
2914 }
2915 }
2916 }
2917 }
2918
2919 return 0;
2920}
2921
2922template <>
2923int TosaReference::Tensor5<int32_t>::getTensorValueUInt8(const size_t bufLen, uint8_t* vals) const
2924{
2925 uint32_t idx = 0;
2926 int totalVals = 1;
2927
2928 for (size_t i = 0; i < shape.size(); i++)
2929 {
2930 totalVals *= shape[i];
2931 }
2932
2933 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
2934
2935 for (int i0 = 0; i0 < shape[0]; i0++)
2936 {
2937 for (int i1 = 0; i1 < shape[1]; i1++)
2938 {
2939 for (int i2 = 0; i2 < shape[2]; i2++)
2940 {
2941 for (int i3 = 0; i3 < shape[3]; i3++)
2942 {
2943 for (int i4 = 0; i4 < shape[4]; i4++)
2944 {
2945 vals[idx++] = (*tensor)(i0, i1, i2, i3, i4);
2946 }
2947 }
2948 }
2949 }
2950 }
2951
2952 return 0;
2953}
2954
2955template <>
2956int TosaReference::Tensor6<int32_t>::getTensorValueUInt8(const size_t bufLen, uint8_t* vals) const
2957{
2958 uint32_t idx = 0;
2959 int totalVals = 1;
2960
2961 for (size_t i = 0; i < shape.size(); i++)
2962 {
2963 totalVals *= shape[i];
2964 }
2965
2966 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
2967
2968 for (int i0 = 0; i0 < shape[0]; i0++)
2969 {
2970 for (int i1 = 0; i1 < shape[1]; i1++)
2971 {
2972 for (int i2 = 0; i2 < shape[2]; i2++)
2973 {
2974 for (int i3 = 0; i3 < shape[3]; i3++)
2975 {
2976 for (int i4 = 0; i4 < shape[4]; i4++)
2977 {
2978 for (int i5 = 0; i5 < shape[5]; i5++)
2979 {
2980 vals[idx++] = (*tensor)(i0, i1, i2, i3, i4, i5);
2981 }
2982 }
2983 }
2984 }
2985 }
2986 }
2987 return 0;
2988}
2989
2990template <class T>
2991int TosaReference::TensorTemplate<T>::getTensorValueInt8(const size_t bufLen, int8_t* vals) const
2992{
2993 std::cout << "T is: " << typeid(T).name() << std::endl;
2994 FATAL_ERROR("TensorTemplate<T>::getTensorValueInt8 should not be called. "
2995 "Implement template specialization version.");
2996 return 0;
2997}
2998
2999template <>
3000int TosaReference::Tensor0<int32_t>::getTensorValueInt8(const size_t bufLen, int8_t* vals) const
3001{
3002 int totalVals = 1;
3003
3004 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
3005
3006 vals[0] = (*tensor)(0);
3007
3008 return 0;
3009}
3010
3011template <>
3012int TosaReference::Tensor1<int32_t>::getTensorValueInt8(const size_t bufLen, int8_t* vals) const
3013{
3014 uint32_t idx = 0;
3015 int totalVals = 1;
3016
3017 for (size_t i = 0; i < shape.size(); i++)
3018 {
3019 totalVals *= shape[i];
3020 }
3021
3022 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
3023
3024 for (int i0 = 0; i0 < shape[0]; i0++)
3025 {
3026 vals[idx++] = (*tensor)(i0);
3027 }
3028
3029 return 0;
3030}
3031
3032template <>
3033int TosaReference::Tensor2<int32_t>::getTensorValueInt8(const size_t bufLen, int8_t* vals) const
3034{
3035 uint32_t idx = 0;
3036 int totalVals = 1;
3037
3038 for (size_t i = 0; i < shape.size(); i++)
3039 {
3040 totalVals *= shape[i];
3041 }
3042
3043 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
3044
3045 for (int i0 = 0; i0 < shape[0]; i0++)
3046 {
3047 for (int i1 = 0; i1 < shape[1]; i1++)
3048 {
3049 vals[idx++] = (*tensor)(i0, i1);
3050 }
3051 }
3052
3053 return 0;
3054}
3055
3056template <>
3057int TosaReference::Tensor3<int32_t>::getTensorValueInt8(const size_t bufLen, int8_t* vals) const
3058{
3059 uint32_t idx = 0;
3060 int totalVals = 1;
3061
3062 for (size_t i = 0; i < shape.size(); i++)
3063 {
3064 totalVals *= shape[i];
3065 }
3066
3067 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
3068
3069 for (int i0 = 0; i0 < shape[0]; i0++)
3070 {
3071 for (int i1 = 0; i1 < shape[1]; i1++)
3072 {
3073 for (int i2 = 0; i2 < shape[2]; i2++)
3074 {
3075 vals[idx++] = (*tensor)(i0, i1, i2);
3076 }
3077 }
3078 }
3079
3080 return 0;
3081}
3082
3083template <>
3084int TosaReference::Tensor4<int32_t>::getTensorValueInt8(const size_t bufLen, int8_t* vals) const
3085{
3086 uint32_t idx = 0;
3087 int totalVals = 1;
3088
3089 for (size_t i = 0; i < shape.size(); i++)
3090 {
3091 totalVals *= shape[i];
3092 }
3093
3094 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
3095
3096 for (int i0 = 0; i0 < shape[0]; i0++)
3097 {
3098 for (int i1 = 0; i1 < shape[1]; i1++)
3099 {
3100 for (int i2 = 0; i2 < shape[2]; i2++)
3101 {
3102 for (int i3 = 0; i3 < shape[3]; i3++)
3103 {
3104 vals[idx++] = (*tensor)(i0, i1, i2, i3);
3105 }
3106 }
3107 }
3108 }
3109
3110 return 0;
3111}
3112
3113template <>
3114int TosaReference::Tensor5<int32_t>::getTensorValueInt8(const size_t bufLen, int8_t* vals) const
3115{
3116 uint32_t idx = 0;
3117 int totalVals = 1;
3118
3119 for (size_t i = 0; i < shape.size(); i++)
3120 {
3121 totalVals *= shape[i];
3122 }
3123
3124 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
3125
3126 for (int i0 = 0; i0 < shape[0]; i0++)
3127 {
3128 for (int i1 = 0; i1 < shape[1]; i1++)
3129 {
3130 for (int i2 = 0; i2 < shape[2]; i2++)
3131 {
3132 for (int i3 = 0; i3 < shape[3]; i3++)
3133 {
3134 for (int i4 = 0; i4 < shape[4]; i4++)
3135 {
3136 vals[idx++] = (*tensor)(i0, i1, i2, i3, i4);
3137 }
3138 }
3139 }
3140 }
3141 }
3142
3143 return 0;
3144}
3145
3146template <>
3147int TosaReference::Tensor6<int32_t>::getTensorValueInt8(const size_t bufLen, int8_t* vals) const
3148{
3149 uint32_t idx = 0;
3150 int totalVals = 1;
3151
3152 for (size_t i = 0; i < shape.size(); i++)
3153 {
3154 totalVals *= shape[i];
3155 }
3156
3157 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
3158
3159 for (int i0 = 0; i0 < shape[0]; i0++)
3160 {
3161 for (int i1 = 0; i1 < shape[1]; i1++)
3162 {
3163 for (int i2 = 0; i2 < shape[2]; i2++)
3164 {
3165 for (int i3 = 0; i3 < shape[3]; i3++)
3166 {
3167 for (int i4 = 0; i4 < shape[4]; i4++)
3168 {
3169 for (int i5 = 0; i5 < shape[5]; i5++)
3170 {
3171 vals[idx++] = (*tensor)(i0, i1, i2, i3, i4, i5);
3172 }
3173 }
3174 }
3175 }
3176 }
3177 }
3178 return 0;
3179}
3180
3181template <class T>
Georgios Pinitase9059772023-12-06 18:52:30 +00003182int TosaReference::TensorTemplate<T>::getTensorValueInt16(const size_t bufLen, int16_t* vals) const
3183{
3184 FATAL_ERROR("TensorTemplate<T>::getTensorValueInt32 should not be called. "
3185 "Implement template specialization version.");
3186 return 0;
3187}
3188
3189template <>
3190int TosaReference::Tensor0<int32_t>::getTensorValueInt16(const size_t bufLen, int16_t* vals) const
3191{
3192 int totalVals = 1;
3193
3194 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
3195
3196 vals[0] = (*tensor)(0);
3197
3198 return 0;
3199}
3200
3201template <>
3202int TosaReference::Tensor1<int32_t>::getTensorValueInt16(const size_t bufLen, int16_t* vals) const
3203{
3204 uint32_t idx = 0;
3205 int totalVals = 1;
3206
3207 for (size_t i = 0; i < shape.size(); i++)
3208 {
3209 totalVals *= shape[i];
3210 }
3211
3212 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
3213
3214 for (int i0 = 0; i0 < shape[0]; i0++)
3215 {
3216 vals[idx++] = (*tensor)(i0);
3217 }
3218
3219 return 0;
3220}
3221
3222template <>
3223int TosaReference::Tensor2<int32_t>::getTensorValueInt16(const size_t bufLen, int16_t* vals) const
3224{
3225 uint32_t idx = 0;
3226 int totalVals = 1;
3227
3228 for (size_t i = 0; i < shape.size(); i++)
3229 {
3230 totalVals *= shape[i];
3231 }
3232
3233 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
3234
3235 for (int i0 = 0; i0 < shape[0]; i0++)
3236 {
3237 for (int i1 = 0; i1 < shape[1]; i1++)
3238 {
3239 vals[idx++] = (*tensor)(i0, i1);
3240 }
3241 }
3242
3243 return 0;
3244}
3245
3246template <>
3247int TosaReference::Tensor3<int32_t>::getTensorValueInt16(const size_t bufLen, int16_t* vals) const
3248{
3249 uint32_t idx = 0;
3250 int totalVals = 1;
3251
3252 for (size_t i = 0; i < shape.size(); i++)
3253 {
3254 totalVals *= shape[i];
3255 }
3256
3257 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
3258
3259 for (int i0 = 0; i0 < shape[0]; i0++)
3260 {
3261 for (int i1 = 0; i1 < shape[1]; i1++)
3262 {
3263 for (int i2 = 0; i2 < shape[2]; i2++)
3264 {
3265 vals[idx++] = (*tensor)(i0, i1, i2);
3266 }
3267 }
3268 }
3269
3270 return 0;
3271}
3272
3273template <>
3274int TosaReference::Tensor4<int32_t>::getTensorValueInt16(const size_t bufLen, int16_t* vals) const
3275{
3276 uint32_t idx = 0;
3277 int totalVals = 1;
3278
3279 for (size_t i = 0; i < shape.size(); i++)
3280 {
3281 totalVals *= shape[i];
3282 }
3283
3284 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
3285
3286 for (int i0 = 0; i0 < shape[0]; i0++)
3287 {
3288 for (int i1 = 0; i1 < shape[1]; i1++)
3289 {
3290 for (int i2 = 0; i2 < shape[2]; i2++)
3291 {
3292 for (int i3 = 0; i3 < shape[3]; i3++)
3293 {
3294 vals[idx++] = (*tensor)(i0, i1, i2, i3);
3295 }
3296 }
3297 }
3298 }
3299
3300 return 0;
3301}
3302
3303template <>
3304int TosaReference::Tensor5<int32_t>::getTensorValueInt16(const size_t bufLen, int16_t* vals) const
3305{
3306 uint32_t idx = 0;
3307 int totalVals = 1;
3308
3309 for (size_t i = 0; i < shape.size(); i++)
3310 {
3311 totalVals *= shape[i];
3312 }
3313
3314 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
3315
3316 for (int i0 = 0; i0 < shape[0]; i0++)
3317 {
3318 for (int i1 = 0; i1 < shape[1]; i1++)
3319 {
3320 for (int i2 = 0; i2 < shape[2]; i2++)
3321 {
3322 for (int i3 = 0; i3 < shape[3]; i3++)
3323 {
3324 for (int i4 = 0; i4 < shape[4]; i4++)
3325 {
3326 vals[idx++] = (*tensor)(i0, i1, i2, i3, i4);
3327 }
3328 }
3329 }
3330 }
3331 }
3332
3333 return 0;
3334}
3335
3336template <>
3337int TosaReference::Tensor6<int32_t>::getTensorValueInt16(const size_t bufLen, int16_t* vals) const
3338{
3339 uint32_t idx = 0;
3340 int totalVals = 1;
3341
3342 for (size_t i = 0; i < shape.size(); i++)
3343 {
3344 totalVals *= shape[i];
3345 }
3346
3347 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
3348
3349 for (int i0 = 0; i0 < shape[0]; i0++)
3350 {
3351 for (int i1 = 0; i1 < shape[1]; i1++)
3352 {
3353 for (int i2 = 0; i2 < shape[2]; i2++)
3354 {
3355 for (int i3 = 0; i3 < shape[3]; i3++)
3356 {
3357 for (int i4 = 0; i4 < shape[4]; i4++)
3358 {
3359 for (int i5 = 0; i5 < shape[5]; i5++)
3360 {
3361 vals[idx++] = (*tensor)(i0, i1, i2, i3, i4, i5);
3362 }
3363 }
3364 }
3365 }
3366 }
3367 }
3368 return 0;
3369}
3370
3371template <class T>
Eric Kunzee5e26762020-10-13 16:11:07 -07003372int TosaReference::TensorTemplate<T>::getTensorValueInt32(const size_t bufLen, int32_t* vals) const
3373{
3374 FATAL_ERROR("TensorTemplate<T>::getTensorValueInt32 should not be called. "
3375 "Implement template specialization version.");
3376 return 0;
3377}
3378
3379template <>
3380int TosaReference::Tensor0<int32_t>::getTensorValueInt32(const size_t bufLen, int32_t* vals) const
3381{
3382 int totalVals = 1;
3383
3384 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
3385
3386 vals[0] = (*tensor)(0);
3387
3388 return 0;
3389}
3390
3391template <>
3392int TosaReference::Tensor1<int32_t>::getTensorValueInt32(const size_t bufLen, int32_t* vals) const
3393{
3394 uint32_t idx = 0;
3395 int totalVals = 1;
3396
3397 for (size_t i = 0; i < shape.size(); i++)
3398 {
3399 totalVals *= shape[i];
3400 }
3401
3402 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
3403
3404 for (int i0 = 0; i0 < shape[0]; i0++)
3405 {
3406 vals[idx++] = (*tensor)(i0);
3407 }
3408
3409 return 0;
3410}
3411
3412template <>
3413int TosaReference::Tensor2<int32_t>::getTensorValueInt32(const size_t bufLen, int32_t* vals) const
3414{
3415 uint32_t idx = 0;
3416 int totalVals = 1;
3417
3418 for (size_t i = 0; i < shape.size(); i++)
3419 {
3420 totalVals *= shape[i];
3421 }
3422
3423 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
3424
3425 for (int i0 = 0; i0 < shape[0]; i0++)
3426 {
3427 for (int i1 = 0; i1 < shape[1]; i1++)
3428 {
3429 vals[idx++] = (*tensor)(i0, i1);
3430 }
3431 }
3432
3433 return 0;
3434}
3435
3436template <>
3437int TosaReference::Tensor3<int32_t>::getTensorValueInt32(const size_t bufLen, int32_t* vals) const
3438{
3439 uint32_t idx = 0;
3440 int totalVals = 1;
3441
3442 for (size_t i = 0; i < shape.size(); i++)
3443 {
3444 totalVals *= shape[i];
3445 }
3446
3447 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
3448
3449 for (int i0 = 0; i0 < shape[0]; i0++)
3450 {
3451 for (int i1 = 0; i1 < shape[1]; i1++)
3452 {
3453 for (int i2 = 0; i2 < shape[2]; i2++)
3454 {
3455 vals[idx++] = (*tensor)(i0, i1, i2);
3456 }
3457 }
3458 }
3459
3460 return 0;
3461}
3462
3463template <>
3464int TosaReference::Tensor4<int32_t>::getTensorValueInt32(const size_t bufLen, int32_t* vals) const
3465{
3466 uint32_t idx = 0;
3467 int totalVals = 1;
3468
3469 for (size_t i = 0; i < shape.size(); i++)
3470 {
3471 totalVals *= shape[i];
3472 }
3473
3474 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
3475
3476 for (int i0 = 0; i0 < shape[0]; i0++)
3477 {
3478 for (int i1 = 0; i1 < shape[1]; i1++)
3479 {
3480 for (int i2 = 0; i2 < shape[2]; i2++)
3481 {
3482 for (int i3 = 0; i3 < shape[3]; i3++)
3483 {
3484 vals[idx++] = (*tensor)(i0, i1, i2, i3);
3485 }
3486 }
3487 }
3488 }
3489
3490 return 0;
3491}
3492
3493template <>
3494int TosaReference::Tensor5<int32_t>::getTensorValueInt32(const size_t bufLen, int32_t* vals) const
3495{
3496 uint32_t idx = 0;
3497 int totalVals = 1;
3498
3499 for (size_t i = 0; i < shape.size(); i++)
3500 {
3501 totalVals *= shape[i];
3502 }
3503
3504 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
3505
3506 for (int i0 = 0; i0 < shape[0]; i0++)
3507 {
3508 for (int i1 = 0; i1 < shape[1]; i1++)
3509 {
3510 for (int i2 = 0; i2 < shape[2]; i2++)
3511 {
3512 for (int i3 = 0; i3 < shape[3]; i3++)
3513 {
3514 for (int i4 = 0; i4 < shape[4]; i4++)
3515 {
3516 vals[idx++] = (*tensor)(i0, i1, i2, i3, i4);
3517 }
3518 }
3519 }
3520 }
3521 }
3522
3523 return 0;
3524}
3525
3526template <>
3527int TosaReference::Tensor6<int32_t>::getTensorValueInt32(const size_t bufLen, int32_t* vals) const
3528{
3529 uint32_t idx = 0;
3530 int totalVals = 1;
3531
3532 for (size_t i = 0; i < shape.size(); i++)
3533 {
3534 totalVals *= shape[i];
3535 }
3536
3537 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
3538
3539 for (int i0 = 0; i0 < shape[0]; i0++)
3540 {
3541 for (int i1 = 0; i1 < shape[1]; i1++)
3542 {
3543 for (int i2 = 0; i2 < shape[2]; i2++)
3544 {
3545 for (int i3 = 0; i3 < shape[3]; i3++)
3546 {
3547 for (int i4 = 0; i4 < shape[4]; i4++)
3548 {
3549 for (int i5 = 0; i5 < shape[5]; i5++)
3550 {
3551 vals[idx++] = (*tensor)(i0, i1, i2, i3, i4, i5);
3552 }
3553 }
3554 }
3555 }
3556 }
3557 }
3558 return 0;
3559}
3560
3561template <class T>
3562int TosaReference::TensorTemplate<T>::getTensorValueInt64(const size_t bufLen, int64_t* vals) const
3563{
3564 FATAL_ERROR("TensorTemplate<T>::getTensorValueInt64 should not be called. "
3565 "Implement template specialization version.");
3566 return 0;
3567}
3568
3569template <>
3570int TosaReference::Tensor0<int64_t>::getTensorValueInt64(const size_t bufLen, int64_t* vals) const
3571{
3572 int totalVals = 1;
3573
3574 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
3575
3576 vals[0] = (*tensor)(0);
3577
3578 return 0;
3579}
3580
3581template <>
3582int TosaReference::Tensor1<int64_t>::getTensorValueInt64(const size_t bufLen, int64_t* vals) const
3583{
3584 uint32_t idx = 0;
3585 int totalVals = 1;
3586
3587 for (size_t i = 0; i < shape.size(); i++)
3588 {
3589 totalVals *= shape[i];
3590 }
3591
3592 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
3593
3594 for (int i0 = 0; i0 < shape[0]; i0++)
3595 {
3596 vals[idx++] = (*tensor)(i0);
3597 }
3598
3599 return 0;
3600}
3601
3602template <>
3603int TosaReference::Tensor2<int64_t>::getTensorValueInt64(const size_t bufLen, int64_t* vals) const
3604{
3605 uint32_t idx = 0;
3606 int totalVals = 1;
3607
3608 for (size_t i = 0; i < shape.size(); i++)
3609 {
3610 totalVals *= shape[i];
3611 }
3612
3613 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
3614
3615 for (int i0 = 0; i0 < shape[0]; i0++)
3616 {
3617 for (int i1 = 0; i1 < shape[1]; i1++)
3618 {
3619 vals[idx++] = (*tensor)(i0, i1);
3620 }
3621 }
3622
3623 return 0;
3624}
3625
3626template <>
3627int TosaReference::Tensor3<int64_t>::getTensorValueInt64(const size_t bufLen, int64_t* vals) const
3628{
3629 uint32_t idx = 0;
3630 int totalVals = 1;
3631
3632 for (size_t i = 0; i < shape.size(); i++)
3633 {
3634 totalVals *= shape[i];
3635 }
3636
3637 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
3638
3639 for (int i0 = 0; i0 < shape[0]; i0++)
3640 {
3641 for (int i1 = 0; i1 < shape[1]; i1++)
3642 {
3643 for (int i2 = 0; i2 < shape[2]; i2++)
3644 {
3645 vals[idx++] = (*tensor)(i0, i1, i2);
3646 }
3647 }
3648 }
3649
3650 return 0;
3651}
3652
3653template <>
3654int TosaReference::Tensor4<int64_t>::getTensorValueInt64(const size_t bufLen, int64_t* vals) const
3655{
3656 uint32_t idx = 0;
3657 int totalVals = 1;
3658
3659 for (size_t i = 0; i < shape.size(); i++)
3660 {
3661 totalVals *= shape[i];
3662 }
3663
3664 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
3665
3666 for (int i0 = 0; i0 < shape[0]; i0++)
3667 {
3668 for (int i1 = 0; i1 < shape[1]; i1++)
3669 {
3670 for (int i2 = 0; i2 < shape[2]; i2++)
3671 {
3672 for (int i3 = 0; i3 < shape[3]; i3++)
3673 {
3674 vals[idx++] = (*tensor)(i0, i1, i2, i3);
3675 }
3676 }
3677 }
3678 }
3679
3680 return 0;
3681}
3682
3683template <>
3684int TosaReference::Tensor5<int64_t>::getTensorValueInt64(const size_t bufLen, int64_t* vals) const
3685{
3686 uint32_t idx = 0;
3687 int totalVals = 1;
3688
3689 for (size_t i = 0; i < shape.size(); i++)
3690 {
3691 totalVals *= shape[i];
3692 }
3693
3694 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
3695
3696 for (int i0 = 0; i0 < shape[0]; i0++)
3697 {
3698 for (int i1 = 0; i1 < shape[1]; i1++)
3699 {
3700 for (int i2 = 0; i2 < shape[2]; i2++)
3701 {
3702 for (int i3 = 0; i3 < shape[3]; i3++)
3703 {
3704 for (int i4 = 0; i4 < shape[4]; i4++)
3705 {
3706 vals[idx++] = (*tensor)(i0, i1, i2, i3, i4);
3707 }
3708 }
3709 }
3710 }
3711 }
3712
3713 return 0;
3714}
3715
3716template <>
3717int TosaReference::Tensor6<int64_t>::getTensorValueInt64(const size_t bufLen, int64_t* vals) const
3718{
3719 uint32_t idx = 0;
3720 int totalVals = 1;
3721
3722 for (size_t i = 0; i < shape.size(); i++)
3723 {
3724 totalVals *= shape[i];
3725 }
3726
3727 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
3728
3729 for (int i0 = 0; i0 < shape[0]; i0++)
3730 {
3731 for (int i1 = 0; i1 < shape[1]; i1++)
3732 {
3733 for (int i2 = 0; i2 < shape[2]; i2++)
3734 {
3735 for (int i3 = 0; i3 < shape[3]; i3++)
3736 {
3737 for (int i4 = 0; i4 < shape[4]; i4++)
3738 {
3739 for (int i5 = 0; i5 < shape[5]; i5++)
3740 {
3741 vals[idx++] = (*tensor)(i0, i1, i2, i3, i4, i5);
3742 }
3743 }
3744 }
3745 }
3746 }
3747 }
3748 return 0;
3749}
3750
3751template <class T>
3752int TosaReference::TensorTemplate<T>::getTensorValueBool(const size_t bufLen, bool* vals) const
3753{
3754 FATAL_ERROR("TensorTemplate<T>::getTensorValueBool should not be called. "
3755 "Implement template specialization version.");
3756 return 0;
3757}
3758
3759template <>
3760int TosaReference::Tensor0<bool>::getTensorValueBool(const size_t bufLen, bool* vals) const
3761{
3762 int totalVals = 1;
3763
3764 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
3765
3766 vals[0] = (*tensor)(0);
3767
3768 return 0;
3769}
3770
3771template <>
3772int TosaReference::Tensor1<bool>::getTensorValueBool(const size_t bufLen, bool* vals) const
3773{
3774 uint32_t idx = 0;
3775 int totalVals = 1;
3776
3777 for (size_t i = 0; i < shape.size(); i++)
3778 {
3779 totalVals *= shape[i];
3780 }
3781
3782 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
3783
3784 for (int i0 = 0; i0 < shape[0]; i0++)
3785 {
3786 vals[idx++] = (*tensor)(i0);
3787 }
3788
3789 return 0;
3790}
3791
3792template <>
3793int TosaReference::Tensor2<bool>::getTensorValueBool(const size_t bufLen, bool* vals) const
3794{
3795 uint32_t idx = 0;
3796 int totalVals = 1;
3797
3798 for (size_t i = 0; i < shape.size(); i++)
3799 {
3800 totalVals *= shape[i];
3801 }
3802
3803 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
3804
3805 for (int i0 = 0; i0 < shape[0]; i0++)
3806 {
3807 for (int i1 = 0; i1 < shape[1]; i1++)
3808 {
3809 vals[idx++] = (*tensor)(i0, i1);
3810 }
3811 }
3812
3813 return 0;
3814}
3815
3816template <>
3817int TosaReference::Tensor3<bool>::getTensorValueBool(const size_t bufLen, bool* vals) const
3818{
3819 uint32_t idx = 0;
3820 int totalVals = 1;
3821
3822 for (size_t i = 0; i < shape.size(); i++)
3823 {
3824 totalVals *= shape[i];
3825 }
3826
3827 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
3828
3829 for (int i0 = 0; i0 < shape[0]; i0++)
3830 {
3831 for (int i1 = 0; i1 < shape[1]; i1++)
3832 {
3833 for (int i2 = 0; i2 < shape[2]; i2++)
3834 {
3835 vals[idx++] = (*tensor)(i0, i1, i2);
3836 }
3837 }
3838 }
3839
3840 return 0;
3841}
3842
3843template <>
3844int TosaReference::Tensor4<bool>::getTensorValueBool(const size_t bufLen, bool* vals) const
3845{
3846 uint32_t idx = 0;
3847 int totalVals = 1;
3848
3849 for (size_t i = 0; i < shape.size(); i++)
3850 {
3851 totalVals *= shape[i];
3852 }
3853
3854 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
3855
3856 for (int i0 = 0; i0 < shape[0]; i0++)
3857 {
3858 for (int i1 = 0; i1 < shape[1]; i1++)
3859 {
3860 for (int i2 = 0; i2 < shape[2]; i2++)
3861 {
3862 for (int i3 = 0; i3 < shape[3]; i3++)
3863 {
3864 vals[idx++] = (*tensor)(i0, i1, i2, i3);
3865 }
3866 }
3867 }
3868 }
3869
3870 return 0;
3871}
3872
3873template <>
3874int TosaReference::Tensor5<bool>::getTensorValueBool(const size_t bufLen, bool* vals) const
3875{
3876 uint32_t idx = 0;
3877 int totalVals = 1;
3878
3879 for (size_t i = 0; i < shape.size(); i++)
3880 {
3881 totalVals *= shape[i];
3882 }
3883
3884 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
3885
3886 for (int i0 = 0; i0 < shape[0]; i0++)
3887 {
3888 for (int i1 = 0; i1 < shape[1]; i1++)
3889 {
3890 for (int i2 = 0; i2 < shape[2]; i2++)
3891 {
3892 for (int i3 = 0; i3 < shape[3]; i3++)
3893 {
3894 for (int i4 = 0; i4 < shape[4]; i4++)
3895 {
3896 vals[idx++] = (*tensor)(i0, i1, i2, i3, i4);
3897 }
3898 }
3899 }
3900 }
3901 }
3902
3903 return 0;
3904}
3905
3906template <>
3907int TosaReference::Tensor6<bool>::getTensorValueBool(const size_t bufLen, bool* vals) const
3908{
3909 uint32_t idx = 0;
3910 int totalVals = 1;
3911
3912 for (size_t i = 0; i < shape.size(); i++)
3913 {
3914 totalVals *= shape[i];
3915 }
3916
3917 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
3918
3919 for (int i0 = 0; i0 < shape[0]; i0++)
3920 {
3921 for (int i1 = 0; i1 < shape[1]; i1++)
3922 {
3923 for (int i2 = 0; i2 < shape[2]; i2++)
3924 {
3925 for (int i3 = 0; i3 < shape[3]; i3++)
3926 {
3927 for (int i4 = 0; i4 < shape[4]; i4++)
3928 {
3929 for (int i5 = 0; i5 < shape[5]; i5++)
3930 {
3931 vals[idx++] = (*tensor)(i0, i1, i2, i3, i4, i5);
3932 }
3933 }
3934 }
3935 }
3936 }
3937 }
3938 return 0;
3939}
3940
Eric Kunze9a367552023-07-11 13:27:36 -07003941#define TOSAREF_ZERORANK_TENSOR_ALLOCATE(dtype) \
3942 template <> \
3943 int TosaReference::Tensor0<dtype>::allocate() \
3944 { \
3945 ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor"); \
3946 tensor = new ETensor0<dtype>(); \
3947 \
3948 if (tensor) \
3949 return 0; \
3950 else \
3951 return 1; \
3952 }
Tai Lya4d748b2023-03-28 22:06:56 +00003953
Eric Kunze9a367552023-07-11 13:27:36 -07003954#define TOSAREF_TENSOR_ALLOCATE(rank, dtype) \
3955 template <> \
3956 int TosaReference::Tensor##rank<dtype>::allocate() \
3957 { \
3958 ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor"); \
3959 std::array<Eigen::DenseIndex, rank> arrshape; \
3960 std::copy_n(shape.begin(), rank, arrshape.begin()); \
3961 tensor = new ETensor##rank<dtype>(arrshape); \
3962 \
3963 if (tensor) \
3964 return 0; \
3965 else \
3966 return 1; \
3967 }
Tai Lya4d748b2023-03-28 22:06:56 +00003968
Eric Kunze9a367552023-07-11 13:27:36 -07003969TOSAREF_ZERORANK_TENSOR_ALLOCATE(double)
3970TOSAREF_TENSOR_ALLOCATE(1, double)
3971TOSAREF_TENSOR_ALLOCATE(2, double)
3972TOSAREF_TENSOR_ALLOCATE(3, double)
3973TOSAREF_TENSOR_ALLOCATE(4, double)
3974TOSAREF_TENSOR_ALLOCATE(5, double)
3975TOSAREF_TENSOR_ALLOCATE(6, double)
3976TOSAREF_ZERORANK_TENSOR_ALLOCATE(float)
3977TOSAREF_TENSOR_ALLOCATE(1, float)
3978TOSAREF_TENSOR_ALLOCATE(2, float)
3979TOSAREF_TENSOR_ALLOCATE(3, float)
3980TOSAREF_TENSOR_ALLOCATE(4, float)
3981TOSAREF_TENSOR_ALLOCATE(5, float)
3982TOSAREF_TENSOR_ALLOCATE(6, float)
3983TOSAREF_ZERORANK_TENSOR_ALLOCATE(int32_t)
3984TOSAREF_TENSOR_ALLOCATE(1, int32_t)
3985TOSAREF_TENSOR_ALLOCATE(2, int32_t)
3986TOSAREF_TENSOR_ALLOCATE(3, int32_t)
3987TOSAREF_TENSOR_ALLOCATE(4, int32_t)
3988TOSAREF_TENSOR_ALLOCATE(5, int32_t)
3989TOSAREF_TENSOR_ALLOCATE(6, int32_t)
3990TOSAREF_ZERORANK_TENSOR_ALLOCATE(int64_t)
3991TOSAREF_TENSOR_ALLOCATE(1, int64_t)
3992TOSAREF_TENSOR_ALLOCATE(2, int64_t)
3993TOSAREF_TENSOR_ALLOCATE(3, int64_t)
3994TOSAREF_TENSOR_ALLOCATE(4, int64_t)
3995TOSAREF_TENSOR_ALLOCATE(5, int64_t)
3996TOSAREF_TENSOR_ALLOCATE(6, int64_t)
3997TOSAREF_ZERORANK_TENSOR_ALLOCATE(bool)
3998TOSAREF_TENSOR_ALLOCATE(1, bool)
3999TOSAREF_TENSOR_ALLOCATE(2, bool)
4000TOSAREF_TENSOR_ALLOCATE(3, bool)
4001TOSAREF_TENSOR_ALLOCATE(4, bool)
4002TOSAREF_TENSOR_ALLOCATE(5, bool)
4003TOSAREF_TENSOR_ALLOCATE(6, bool)
Eric Kunzee5e26762020-10-13 16:11:07 -07004004
4005template <>
Tai Lya4d748b2023-03-28 22:06:56 +00004006int TosaReference::Tensor0<double>::dumpTensor(FILE* out) const
4007{
4008 char fp_fmt[32];
4009 snprintf(fp_fmt, sizeof(fp_fmt), "[ %%%sf ]\n", g_func_config.fp_format.c_str());
4010
4011 if (tensor == nullptr)
4012 {
4013 fprintf(out, "<Not allocated>\n");
4014 return 0;
4015 }
4016
4017 fprintf(out, fp_fmt, (*tensor)(0));
4018
4019 return 0;
4020}
4021
4022template <>
4023int TosaReference::Tensor1<double>::dumpTensor(FILE* out) const
4024{
4025 char fp_fmt[32];
4026 snprintf(fp_fmt, sizeof(fp_fmt), " %%%sf ", g_func_config.fp_format.c_str());
4027
4028 if (tensor == nullptr)
4029 {
4030 fprintf(out, "<Not allocated>\n");
4031 return 0;
4032 }
4033
4034 fprintf(out, "[");
4035 for (int i0 = 0; i0 < shape[0]; i0++)
4036 {
4037 fprintf(out, fp_fmt, (*tensor)(i0));
4038 }
4039 fprintf(out, "]\n");
4040
4041 return 0;
4042}
4043
4044template <>
4045int TosaReference::Tensor2<double>::dumpTensor(FILE* out) const
4046{
4047 char fp_fmt[32];
4048 snprintf(fp_fmt, sizeof(fp_fmt), " %%%sf ", g_func_config.fp_format.c_str());
4049
4050 if (tensor == nullptr)
4051 {
4052 fprintf(out, "<Not allocated>\n");
4053 return 0;
4054 }
4055
4056 fprintf(out, "[");
4057 for (int i0 = 0; i0 < shape[0]; i0++)
4058 {
4059 fprintf(out, "[");
4060 for (int i1 = 0; i1 < shape[1]; i1++)
4061 {
4062 fprintf(out, fp_fmt, (*tensor)(i0, i1));
4063 }
4064 fprintf(out, "]\n");
4065 }
4066 fprintf(out, "]\n");
4067
4068 return 0;
4069}
4070
4071template <>
4072int TosaReference::Tensor3<double>::dumpTensor(FILE* out) const
4073{
4074 char fp_fmt[32];
4075 snprintf(fp_fmt, sizeof(fp_fmt), " %%%sf ", g_func_config.fp_format.c_str());
4076
4077 if (tensor == nullptr)
4078 {
4079 fprintf(out, "<Not allocated>\n");
4080 return 0;
4081 }
4082
4083 fprintf(out, "[");
4084 for (int i0 = 0; i0 < shape[0]; i0++)
4085 {
4086 fprintf(out, "[");
4087 for (int i1 = 0; i1 < shape[1]; i1++)
4088 {
4089 fprintf(out, "[");
4090 for (int i2 = 0; i2 < shape[2]; i2++)
4091 {
4092 fprintf(out, fp_fmt, (*tensor)(i0, i1, i2));
4093 }
4094 fprintf(out, "]\n");
4095 }
4096 fprintf(out, "]\n");
4097 }
4098 fprintf(out, "]\n");
4099
4100 return 0;
4101}
4102
4103template <>
4104int TosaReference::Tensor4<double>::dumpTensor(FILE* out) const
4105{
4106 char fp_fmt[32];
4107 snprintf(fp_fmt, sizeof(fp_fmt), " %%%sf ", g_func_config.fp_format.c_str());
4108
4109 if (tensor == nullptr)
4110 {
4111 fprintf(out, "<Not allocated>\n");
4112 return 0;
4113 }
4114
4115 fprintf(out, "[");
4116 for (int i0 = 0; i0 < shape[0]; i0++)
4117 {
4118 fprintf(out, "[");
4119 for (int i1 = 0; i1 < shape[1]; i1++)
4120 {
4121 fprintf(out, "[");
4122 for (int i2 = 0; i2 < shape[2]; i2++)
4123 {
4124 fprintf(out, "[");
4125 for (int i3 = 0; i3 < shape[3]; i3++)
4126 {
4127 fprintf(out, fp_fmt, (*tensor)(i0, i1, i2, i3));
4128 }
4129 fprintf(out, "]\n");
4130 }
4131 fprintf(out, "]\n");
4132 }
4133 fprintf(out, "]\n");
4134 }
4135 fprintf(out, "]\n");
4136
4137 return 0;
4138}
4139
4140template <>
4141int TosaReference::Tensor5<double>::dumpTensor(FILE* out) const
4142{
4143 char fp_fmt[32];
4144 snprintf(fp_fmt, sizeof(fp_fmt), " %%%sf ", g_func_config.fp_format.c_str());
4145
4146 if (tensor == nullptr)
4147 {
4148 fprintf(out, "<Not allocated>\n");
4149 return 0;
4150 }
4151
4152 fprintf(out, "[");
4153 for (int i0 = 0; i0 < shape[0]; i0++)
4154 {
4155 fprintf(out, "[");
4156 for (int i1 = 0; i1 < shape[1]; i1++)
4157 {
4158 fprintf(out, "[");
4159 for (int i2 = 0; i2 < shape[2]; i2++)
4160 {
4161 fprintf(out, "[");
4162 for (int i3 = 0; i3 < shape[3]; i3++)
4163 {
4164 fprintf(out, "[");
4165 for (int i4 = 0; i4 < shape[4]; i4++)
4166 {
4167 fprintf(out, fp_fmt, (*tensor)(i0, i1, i2, i3, i4));
4168 }
4169 fprintf(out, "]\n");
4170 }
4171 fprintf(out, "]\n");
4172 }
4173 fprintf(out, "]\n");
4174 }
4175 fprintf(out, "]\n");
4176 }
4177 fprintf(out, "]\n");
4178
4179 return 0;
4180}
4181
4182template <>
4183int TosaReference::Tensor6<double>::dumpTensor(FILE* out) const
4184{
4185 char fp_fmt[32];
4186 snprintf(fp_fmt, sizeof(fp_fmt), " %%%sf ", g_func_config.fp_format.c_str());
4187
4188 if (tensor == nullptr)
4189 {
4190 fprintf(out, "<Not allocated>\n");
4191 return 0;
4192 }
4193
4194 fprintf(out, "[");
4195 for (int i0 = 0; i0 < shape[0]; i0++)
4196 {
4197 fprintf(out, "[");
4198 for (int i1 = 0; i1 < shape[1]; i1++)
4199 {
4200 fprintf(out, "[");
4201 for (int i2 = 0; i2 < shape[2]; i2++)
4202 {
4203 fprintf(out, "[");
4204 for (int i3 = 0; i3 < shape[3]; i3++)
4205 {
4206 fprintf(out, "[");
4207 for (int i4 = 0; i4 < shape[4]; i4++)
4208 {
4209 fprintf(out, "[");
4210 for (int i5 = 0; i5 < shape[5]; i5++)
4211 {
4212 fprintf(out, fp_fmt, (*tensor)(i0, i1, i2, i3, i4, i5));
4213 }
4214 fprintf(out, "]\n");
4215 }
4216 fprintf(out, "]\n");
4217 }
4218 fprintf(out, "]\n");
4219 }
4220 fprintf(out, "]\n");
4221 }
4222 fprintf(out, "]\n");
4223 }
4224 fprintf(out, "]\n");
4225
4226 return 0;
4227}
4228
4229template <>
Eric Kunzee5e26762020-10-13 16:11:07 -07004230int TosaReference::Tensor0<float>::dumpTensor(FILE* out) const
4231{
Eric Kunze286f8342022-06-22 11:30:23 -07004232 char fp_fmt[32];
4233 snprintf(fp_fmt, sizeof(fp_fmt), "[ %%%sf ]\n", g_func_config.fp_format.c_str());
Eric Kunzee5e26762020-10-13 16:11:07 -07004234
4235 if (tensor == nullptr)
4236 {
4237 fprintf(out, "<Not allocated>\n");
4238 return 0;
4239 }
4240
4241 fprintf(out, fp_fmt, (*tensor)(0));
4242
4243 return 0;
4244}
4245
4246template <>
4247int TosaReference::Tensor1<float>::dumpTensor(FILE* out) const
4248{
Eric Kunze286f8342022-06-22 11:30:23 -07004249 char fp_fmt[32];
4250 snprintf(fp_fmt, sizeof(fp_fmt), " %%%sf ", g_func_config.fp_format.c_str());
Eric Kunzee5e26762020-10-13 16:11:07 -07004251
4252 if (tensor == nullptr)
4253 {
4254 fprintf(out, "<Not allocated>\n");
4255 return 0;
4256 }
4257
4258 fprintf(out, "[");
4259 for (int i0 = 0; i0 < shape[0]; i0++)
4260 {
4261 fprintf(out, fp_fmt, (*tensor)(i0));
4262 }
4263 fprintf(out, "]\n");
4264
4265 return 0;
4266}
4267
4268template <>
4269int TosaReference::Tensor2<float>::dumpTensor(FILE* out) const
4270{
Eric Kunze286f8342022-06-22 11:30:23 -07004271 char fp_fmt[32];
4272 snprintf(fp_fmt, sizeof(fp_fmt), " %%%sf ", g_func_config.fp_format.c_str());
Eric Kunzee5e26762020-10-13 16:11:07 -07004273
4274 if (tensor == nullptr)
4275 {
4276 fprintf(out, "<Not allocated>\n");
4277 return 0;
4278 }
4279
4280 fprintf(out, "[");
4281 for (int i0 = 0; i0 < shape[0]; i0++)
4282 {
4283 fprintf(out, "[");
4284 for (int i1 = 0; i1 < shape[1]; i1++)
4285 {
4286 fprintf(out, fp_fmt, (*tensor)(i0, i1));
4287 }
4288 fprintf(out, "]\n");
4289 }
4290 fprintf(out, "]\n");
4291
4292 return 0;
4293}
4294
4295template <>
4296int TosaReference::Tensor3<float>::dumpTensor(FILE* out) const
4297{
Eric Kunze286f8342022-06-22 11:30:23 -07004298 char fp_fmt[32];
4299 snprintf(fp_fmt, sizeof(fp_fmt), " %%%sf ", g_func_config.fp_format.c_str());
Eric Kunzee5e26762020-10-13 16:11:07 -07004300
4301 if (tensor == nullptr)
4302 {
4303 fprintf(out, "<Not allocated>\n");
4304 return 0;
4305 }
4306
4307 fprintf(out, "[");
4308 for (int i0 = 0; i0 < shape[0]; i0++)
4309 {
4310 fprintf(out, "[");
4311 for (int i1 = 0; i1 < shape[1]; i1++)
4312 {
4313 fprintf(out, "[");
4314 for (int i2 = 0; i2 < shape[2]; i2++)
4315 {
4316 fprintf(out, fp_fmt, (*tensor)(i0, i1, i2));
4317 }
4318 fprintf(out, "]\n");
4319 }
4320 fprintf(out, "]\n");
4321 }
4322 fprintf(out, "]\n");
4323
4324 return 0;
4325}
4326
4327template <>
4328int TosaReference::Tensor4<float>::dumpTensor(FILE* out) const
4329{
Eric Kunze286f8342022-06-22 11:30:23 -07004330 char fp_fmt[32];
4331 snprintf(fp_fmt, sizeof(fp_fmt), " %%%sf ", g_func_config.fp_format.c_str());
Eric Kunzee5e26762020-10-13 16:11:07 -07004332
4333 if (tensor == nullptr)
4334 {
4335 fprintf(out, "<Not allocated>\n");
4336 return 0;
4337 }
4338
4339 fprintf(out, "[");
4340 for (int i0 = 0; i0 < shape[0]; i0++)
4341 {
4342 fprintf(out, "[");
4343 for (int i1 = 0; i1 < shape[1]; i1++)
4344 {
4345 fprintf(out, "[");
4346 for (int i2 = 0; i2 < shape[2]; i2++)
4347 {
4348 fprintf(out, "[");
4349 for (int i3 = 0; i3 < shape[3]; i3++)
4350 {
4351 fprintf(out, fp_fmt, (*tensor)(i0, i1, i2, i3));
4352 }
4353 fprintf(out, "]\n");
4354 }
4355 fprintf(out, "]\n");
4356 }
4357 fprintf(out, "]\n");
4358 }
4359 fprintf(out, "]\n");
4360
4361 return 0;
4362}
4363
4364template <>
4365int TosaReference::Tensor5<float>::dumpTensor(FILE* out) const
4366{
Eric Kunze286f8342022-06-22 11:30:23 -07004367 char fp_fmt[32];
4368 snprintf(fp_fmt, sizeof(fp_fmt), " %%%sf ", g_func_config.fp_format.c_str());
Eric Kunzee5e26762020-10-13 16:11:07 -07004369
4370 if (tensor == nullptr)
4371 {
4372 fprintf(out, "<Not allocated>\n");
4373 return 0;
4374 }
4375
4376 fprintf(out, "[");
4377 for (int i0 = 0; i0 < shape[0]; i0++)
4378 {
4379 fprintf(out, "[");
4380 for (int i1 = 0; i1 < shape[1]; i1++)
4381 {
4382 fprintf(out, "[");
4383 for (int i2 = 0; i2 < shape[2]; i2++)
4384 {
4385 fprintf(out, "[");
4386 for (int i3 = 0; i3 < shape[3]; i3++)
4387 {
4388 fprintf(out, "[");
4389 for (int i4 = 0; i4 < shape[4]; i4++)
4390 {
4391 fprintf(out, fp_fmt, (*tensor)(i0, i1, i2, i3, i4));
4392 }
4393 fprintf(out, "]\n");
4394 }
4395 fprintf(out, "]\n");
4396 }
4397 fprintf(out, "]\n");
4398 }
4399 fprintf(out, "]\n");
4400 }
4401 fprintf(out, "]\n");
4402
4403 return 0;
4404}
4405
4406template <>
4407int TosaReference::Tensor6<float>::dumpTensor(FILE* out) const
4408{
Eric Kunze286f8342022-06-22 11:30:23 -07004409 char fp_fmt[32];
4410 snprintf(fp_fmt, sizeof(fp_fmt), " %%%sf ", g_func_config.fp_format.c_str());
Eric Kunzee5e26762020-10-13 16:11:07 -07004411
4412 if (tensor == nullptr)
4413 {
4414 fprintf(out, "<Not allocated>\n");
4415 return 0;
4416 }
4417
4418 fprintf(out, "[");
4419 for (int i0 = 0; i0 < shape[0]; i0++)
4420 {
4421 fprintf(out, "[");
4422 for (int i1 = 0; i1 < shape[1]; i1++)
4423 {
4424 fprintf(out, "[");
4425 for (int i2 = 0; i2 < shape[2]; i2++)
4426 {
4427 fprintf(out, "[");
4428 for (int i3 = 0; i3 < shape[3]; i3++)
4429 {
4430 fprintf(out, "[");
4431 for (int i4 = 0; i4 < shape[4]; i4++)
4432 {
4433 fprintf(out, "[");
4434 for (int i5 = 0; i5 < shape[5]; i5++)
4435 {
4436 fprintf(out, fp_fmt, (*tensor)(i0, i1, i2, i3, i4, i5));
4437 }
4438 fprintf(out, "]\n");
4439 }
4440 fprintf(out, "]\n");
4441 }
4442 fprintf(out, "]\n");
4443 }
4444 fprintf(out, "]\n");
4445 }
4446 fprintf(out, "]\n");
4447 }
4448 fprintf(out, "]\n");
4449
4450 return 0;
4451}
4452
4453template <>
4454int TosaReference::Tensor0<int64_t>::dumpTensor(FILE* out) const
4455{
Eric Kunze286f8342022-06-22 11:30:23 -07004456 char i64_fmt[32];
4457 snprintf(i64_fmt, sizeof(i64_fmt), "[ %%ld ]\n");
Eric Kunzee5e26762020-10-13 16:11:07 -07004458
4459 if (tensor == nullptr)
4460 {
4461 fprintf(out, "<Not allocated>\n");
4462 return 0;
4463 }
4464
4465 fprintf(out, i64_fmt, (*tensor)(0));
4466
4467 return 0;
4468}
4469
4470template <>
4471int TosaReference::Tensor1<int64_t>::dumpTensor(FILE* out) const
4472{
Eric Kunze286f8342022-06-22 11:30:23 -07004473 char i64_fmt[32];
4474 snprintf(i64_fmt, sizeof(i64_fmt), " %%ld ");
Eric Kunzee5e26762020-10-13 16:11:07 -07004475
4476 if (tensor == nullptr)
4477 {
4478 fprintf(out, "<Not allocated>\n");
4479 return 0;
4480 }
4481
4482 fprintf(out, "[");
4483 for (int i0 = 0; i0 < shape[0]; i0++)
4484 {
4485 fprintf(out, i64_fmt, (*tensor)(i0));
4486 }
4487 fprintf(out, "]\n");
4488
4489 return 0;
4490}
4491
4492template <>
4493int TosaReference::Tensor2<int64_t>::dumpTensor(FILE* out) const
4494{
Eric Kunze286f8342022-06-22 11:30:23 -07004495 char i64_fmt[32];
4496 snprintf(i64_fmt, sizeof(i64_fmt), " %%ld ");
Eric Kunzee5e26762020-10-13 16:11:07 -07004497
4498 if (tensor == nullptr)
4499 {
4500 fprintf(out, "<Not allocated>\n");
4501 return 0;
4502 }
4503
4504 fprintf(out, "[");
4505 for (int i0 = 0; i0 < shape[0]; i0++)
4506 {
4507 fprintf(out, "[");
4508 for (int i1 = 0; i1 < shape[1]; i1++)
4509 {
4510 fprintf(out, i64_fmt, (*tensor)(i0, i1));
4511 }
4512 fprintf(out, "]\n");
4513 }
4514 fprintf(out, "]\n");
4515
4516 return 0;
4517}
4518
4519template <>
4520int TosaReference::Tensor3<int64_t>::dumpTensor(FILE* out) const
4521{
Eric Kunze286f8342022-06-22 11:30:23 -07004522 char i64_fmt[32];
4523 snprintf(i64_fmt, sizeof(i64_fmt), " %%ld ");
Eric Kunzee5e26762020-10-13 16:11:07 -07004524
4525 if (tensor == nullptr)
4526 {
4527 fprintf(out, "<Not allocated>\n");
4528 return 0;
4529 }
4530
4531 fprintf(out, "[");
4532 for (int i0 = 0; i0 < shape[0]; i0++)
4533 {
4534 fprintf(out, "[");
4535 for (int i1 = 0; i1 < shape[1]; i1++)
4536 {
4537 fprintf(out, "[");
4538 for (int i2 = 0; i2 < shape[2]; i2++)
4539 {
4540 fprintf(out, i64_fmt, (*tensor)(i0, i1, i2));
4541 }
4542 fprintf(out, "]\n");
4543 }
4544 fprintf(out, "]\n");
4545 }
4546 fprintf(out, "]\n");
4547
4548 return 0;
4549}
4550
4551template <>
4552int TosaReference::Tensor4<int64_t>::dumpTensor(FILE* out) const
4553{
Eric Kunze286f8342022-06-22 11:30:23 -07004554 char i64_fmt[32];
4555 snprintf(i64_fmt, sizeof(i64_fmt), " %%ld ");
Eric Kunzee5e26762020-10-13 16:11:07 -07004556
4557 if (tensor == nullptr)
4558 {
4559 fprintf(out, "<Not allocated>\n");
4560 return 0;
4561 }
4562
4563 fprintf(out, "[");
4564 for (int i0 = 0; i0 < shape[0]; i0++)
4565 {
4566 fprintf(out, "[");
4567 for (int i1 = 0; i1 < shape[1]; i1++)
4568 {
4569 fprintf(out, "[");
4570 for (int i2 = 0; i2 < shape[2]; i2++)
4571 {
4572 fprintf(out, "[");
4573 for (int i3 = 0; i3 < shape[3]; i3++)
4574 {
4575 fprintf(out, i64_fmt, (*tensor)(i0, i1, i2, i3));
4576 }
4577 fprintf(out, "]\n");
4578 }
4579 fprintf(out, "]\n");
4580 }
4581 fprintf(out, "]\n");
4582 }
4583 fprintf(out, "]\n");
4584
4585 return 0;
4586}
4587
4588template <>
4589int TosaReference::Tensor5<int64_t>::dumpTensor(FILE* out) const
4590{
Eric Kunze286f8342022-06-22 11:30:23 -07004591 char i64_fmt[32];
4592 snprintf(i64_fmt, sizeof(i64_fmt), " %%ld ");
Eric Kunzee5e26762020-10-13 16:11:07 -07004593
4594 if (tensor == nullptr)
4595 {
4596 fprintf(out, "<Not allocated>\n");
4597 return 0;
4598 }
4599
4600 fprintf(out, "[");
4601 for (int i0 = 0; i0 < shape[0]; i0++)
4602 {
4603 fprintf(out, "[");
4604 for (int i1 = 0; i1 < shape[1]; i1++)
4605 {
4606 fprintf(out, "[");
4607 for (int i2 = 0; i2 < shape[2]; i2++)
4608 {
4609 fprintf(out, "[");
4610 for (int i3 = 0; i3 < shape[3]; i3++)
4611 {
4612 fprintf(out, "[");
4613 for (int i4 = 0; i4 < shape[4]; i4++)
4614 {
4615 fprintf(out, i64_fmt, (*tensor)(i0, i1, i2, i3, i4));
4616 }
4617 fprintf(out, "]\n");
4618 }
4619 fprintf(out, "]\n");
4620 }
4621 fprintf(out, "]\n");
4622 }
4623 fprintf(out, "]\n");
4624 }
4625 fprintf(out, "]\n");
4626
4627 return 0;
4628}
4629
4630template <>
4631int TosaReference::Tensor6<int64_t>::dumpTensor(FILE* out) const
4632{
Eric Kunze286f8342022-06-22 11:30:23 -07004633 char i64_fmt[32];
4634 snprintf(i64_fmt, sizeof(i64_fmt), " %%ld ");
Eric Kunzee5e26762020-10-13 16:11:07 -07004635
4636 if (tensor == nullptr)
4637 {
4638 fprintf(out, "<Not allocated>\n");
4639 return 0;
4640 }
4641
4642 fprintf(out, "[");
4643 for (int i0 = 0; i0 < shape[0]; i0++)
4644 {
4645 fprintf(out, "[");
4646 for (int i1 = 0; i1 < shape[1]; i1++)
4647 {
4648 fprintf(out, "[");
4649 for (int i2 = 0; i2 < shape[2]; i2++)
4650 {
4651 fprintf(out, "[");
4652 for (int i3 = 0; i3 < shape[3]; i3++)
4653 {
4654 fprintf(out, "[");
4655 for (int i4 = 0; i4 < shape[4]; i4++)
4656 {
4657 fprintf(out, "[");
4658 for (int i5 = 0; i5 < shape[5]; i5++)
4659 {
4660 fprintf(out, i64_fmt, (*tensor)(i0, i1, i2, i3, i4, i5));
4661 }
4662 fprintf(out, "]\n");
4663 }
4664 fprintf(out, "]\n");
4665 }
4666 fprintf(out, "]\n");
4667 }
4668 fprintf(out, "]\n");
4669 }
4670 fprintf(out, "]\n");
4671 }
4672 fprintf(out, "]\n");
4673
4674 return 0;
4675}
4676
4677template <>
4678int TosaReference::Tensor0<int32_t>::dumpTensor(FILE* out) const
4679{
Eric Kunze286f8342022-06-22 11:30:23 -07004680 char i32_fmt[32];
4681 snprintf(i32_fmt, sizeof(i32_fmt), "[ %%d ]\n");
Eric Kunzee5e26762020-10-13 16:11:07 -07004682
4683 if (tensor == nullptr)
4684 {
4685 fprintf(out, "<Not allocated>\n");
4686 return 0;
4687 }
4688
4689 fprintf(out, i32_fmt, (*tensor)(0));
4690
4691 return 0;
4692}
4693
4694template <>
4695int TosaReference::Tensor1<int32_t>::dumpTensor(FILE* out) const
4696{
Eric Kunze286f8342022-06-22 11:30:23 -07004697 char i32_fmt[32];
4698 snprintf(i32_fmt, sizeof(i32_fmt), " %%d ");
Eric Kunzee5e26762020-10-13 16:11:07 -07004699
4700 if (tensor == nullptr)
4701 {
4702 fprintf(out, "<Not allocated>\n");
4703 return 0;
4704 }
4705
4706 fprintf(out, "[");
4707 for (int i0 = 0; i0 < shape[0]; i0++)
4708 {
4709 fprintf(out, i32_fmt, (*tensor)(i0));
4710 }
4711 fprintf(out, "]\n");
4712
4713 return 0;
4714}
4715
4716template <>
4717int TosaReference::Tensor2<int32_t>::dumpTensor(FILE* out) const
4718{
Eric Kunze286f8342022-06-22 11:30:23 -07004719 char i32_fmt[32];
4720 snprintf(i32_fmt, sizeof(i32_fmt), " %%d ");
Eric Kunzee5e26762020-10-13 16:11:07 -07004721
4722 if (tensor == nullptr)
4723 {
4724 fprintf(out, "<Not allocated>\n");
4725 return 0;
4726 }
4727
4728 if (tensor == nullptr)
4729 {
4730 fprintf(out, "<Not allocated>\n");
4731 return 0;
4732 }
4733
4734 fprintf(out, "[");
4735 for (int i0 = 0; i0 < shape[0]; i0++)
4736 {
4737 fprintf(out, "[");
4738 for (int i1 = 0; i1 < shape[1]; i1++)
4739 {
4740 fprintf(out, i32_fmt, (*tensor)(i0, i1));
4741 }
4742 fprintf(out, "]\n");
4743 }
4744 fprintf(out, "]\n");
4745
4746 return 0;
4747}
4748
4749template <>
4750int TosaReference::Tensor3<int32_t>::dumpTensor(FILE* out) const
4751{
Eric Kunze286f8342022-06-22 11:30:23 -07004752 char i32_fmt[32];
4753 snprintf(i32_fmt, sizeof(i32_fmt), " %%d ");
Eric Kunzee5e26762020-10-13 16:11:07 -07004754
4755 if (tensor == nullptr)
4756 {
4757 fprintf(out, "<Not allocated>\n");
4758 return 0;
4759 }
4760
4761 if (tensor == nullptr)
4762 {
4763 fprintf(out, "<Not allocated>\n");
4764 return 0;
4765 }
4766
4767 fprintf(out, "[");
4768 for (int i0 = 0; i0 < shape[0]; i0++)
4769 {
4770 fprintf(out, "[");
4771 for (int i1 = 0; i1 < shape[1]; i1++)
4772 {
4773 fprintf(out, "[");
4774 for (int i2 = 0; i2 < shape[2]; i2++)
4775 {
4776 fprintf(out, i32_fmt, (*tensor)(i0, i1, i2));
4777 }
4778 fprintf(out, "]\n");
4779 }
4780 fprintf(out, "]\n");
4781 }
4782 fprintf(out, "]\n");
4783
4784 return 0;
4785}
4786
4787template <>
4788int TosaReference::Tensor4<int32_t>::dumpTensor(FILE* out) const
4789{
Eric Kunze286f8342022-06-22 11:30:23 -07004790 char i32_fmt[32];
4791 snprintf(i32_fmt, sizeof(i32_fmt), " %%d ");
Eric Kunzee5e26762020-10-13 16:11:07 -07004792
4793 if (tensor == nullptr)
4794 {
4795 fprintf(out, "<Not allocated>\n");
4796 return 0;
4797 }
4798
4799 fprintf(out, "[");
4800 for (int i0 = 0; i0 < shape[0]; i0++)
4801 {
4802 fprintf(out, "[");
4803 for (int i1 = 0; i1 < shape[1]; i1++)
4804 {
4805 fprintf(out, "[");
4806 for (int i2 = 0; i2 < shape[2]; i2++)
4807 {
4808 fprintf(out, "[");
4809 for (int i3 = 0; i3 < shape[3]; i3++)
4810 {
4811 fprintf(out, i32_fmt, (*tensor)(i0, i1, i2, i3));
4812 }
4813 fprintf(out, "]\n");
4814 }
4815 fprintf(out, "]\n");
4816 }
4817 fprintf(out, "]\n");
4818 }
4819 fprintf(out, "]\n");
4820
4821 return 0;
4822}
4823
4824template <>
4825int TosaReference::Tensor5<int32_t>::dumpTensor(FILE* out) const
4826{
Eric Kunze286f8342022-06-22 11:30:23 -07004827 char i32_fmt[32];
4828 snprintf(i32_fmt, sizeof(i32_fmt), " %%d ");
Eric Kunzee5e26762020-10-13 16:11:07 -07004829
4830 if (tensor == nullptr)
4831 {
4832 fprintf(out, "<Not allocated>\n");
4833 return 0;
4834 }
4835
4836 fprintf(out, "[");
4837 for (int i0 = 0; i0 < shape[0]; i0++)
4838 {
4839 fprintf(out, "[");
4840 for (int i1 = 0; i1 < shape[1]; i1++)
4841 {
4842 fprintf(out, "[");
4843 for (int i2 = 0; i2 < shape[2]; i2++)
4844 {
4845 fprintf(out, "[");
4846 for (int i3 = 0; i3 < shape[3]; i3++)
4847 {
4848 fprintf(out, "[");
4849 for (int i4 = 0; i4 < shape[4]; i4++)
4850 {
4851 fprintf(out, i32_fmt, (*tensor)(i0, i1, i2, i3, i4));
4852 }
4853 fprintf(out, "]\n");
4854 }
4855 fprintf(out, "]\n");
4856 }
4857 fprintf(out, "]\n");
4858 }
4859 fprintf(out, "]\n");
4860 }
4861 fprintf(out, "]\n");
4862
4863 return 0;
4864}
4865
4866template <>
4867int TosaReference::Tensor6<int32_t>::dumpTensor(FILE* out) const
4868{
Eric Kunze286f8342022-06-22 11:30:23 -07004869 char i32_fmt[32];
4870 snprintf(i32_fmt, sizeof(i32_fmt), " %%d ");
Eric Kunzee5e26762020-10-13 16:11:07 -07004871
4872 if (tensor == nullptr)
4873 {
4874 fprintf(out, "<Not allocated>\n");
4875 return 0;
4876 }
4877
4878 fprintf(out, "[");
4879 for (int i0 = 0; i0 < shape[0]; i0++)
4880 {
4881 fprintf(out, "[");
4882 for (int i1 = 0; i1 < shape[1]; i1++)
4883 {
4884 fprintf(out, "[");
4885 for (int i2 = 0; i2 < shape[2]; i2++)
4886 {
4887 fprintf(out, "[");
4888 for (int i3 = 0; i3 < shape[3]; i3++)
4889 {
4890 fprintf(out, "[");
4891 for (int i4 = 0; i4 < shape[4]; i4++)
4892 {
4893 fprintf(out, "[");
4894 for (int i5 = 0; i5 < shape[5]; i5++)
4895 {
4896 fprintf(out, i32_fmt, (*tensor)(i0, i1, i2, i3, i4, i5));
4897 }
4898 fprintf(out, "]\n");
4899 }
4900 fprintf(out, "]\n");
4901 }
4902 fprintf(out, "]\n");
4903 }
4904 fprintf(out, "]\n");
4905 }
4906 fprintf(out, "]\n");
4907 }
4908 fprintf(out, "]\n");
4909
4910 return 0;
4911}
4912
4913template <>
4914int TosaReference::Tensor0<bool>::dumpTensor(FILE* out) const
4915{
Eric Kunze286f8342022-06-22 11:30:23 -07004916 char bool_fmt[32];
4917 snprintf(bool_fmt, sizeof(bool_fmt), "[ %%s ]\n");
Eric Kunzee5e26762020-10-13 16:11:07 -07004918
4919 if (tensor == nullptr)
4920 {
4921 fprintf(out, "<Not allocated>\n");
4922 return 0;
4923 }
4924
4925 fprintf(out, bool_fmt, bool_to_str((*tensor)(0)));
4926
4927 return 0;
4928}
4929
4930template <>
4931int TosaReference::Tensor1<bool>::dumpTensor(FILE* out) const
4932{
Eric Kunze286f8342022-06-22 11:30:23 -07004933 char bool_fmt[32];
4934 snprintf(bool_fmt, sizeof(bool_fmt), " %%s ");
Eric Kunzee5e26762020-10-13 16:11:07 -07004935
4936 if (tensor == nullptr)
4937 {
4938 fprintf(out, "<Not allocated>\n");
4939 return 0;
4940 }
4941
4942 fprintf(out, "[");
4943 for (int i0 = 0; i0 < shape[0]; i0++)
4944 {
4945 fprintf(out, bool_fmt, bool_to_str((*tensor)(i0)));
4946 }
4947 fprintf(out, "]\n");
4948
4949 return 0;
4950}
4951
4952template <>
4953int TosaReference::Tensor2<bool>::dumpTensor(FILE* out) const
4954{
Eric Kunze286f8342022-06-22 11:30:23 -07004955 char bool_fmt[32];
4956 snprintf(bool_fmt, sizeof(bool_fmt), " %%s ");
Eric Kunzee5e26762020-10-13 16:11:07 -07004957
4958 if (tensor == nullptr)
4959 {
4960 fprintf(out, "<Not allocated>\n");
4961 return 0;
4962 }
4963
4964 fprintf(out, "[");
4965 for (int i0 = 0; i0 < shape[0]; i0++)
4966 {
4967 fprintf(out, "[");
4968 for (int i1 = 0; i1 < shape[1]; i1++)
4969 {
4970 fprintf(out, bool_fmt, bool_to_str((*tensor)(i0, i1)));
4971 }
4972 fprintf(out, "]\n");
4973 }
4974 fprintf(out, "]\n");
4975
4976 return 0;
4977}
4978
4979template <>
4980int TosaReference::Tensor3<bool>::dumpTensor(FILE* out) const
4981{
Eric Kunze286f8342022-06-22 11:30:23 -07004982 char bool_fmt[32];
4983 snprintf(bool_fmt, sizeof(bool_fmt), " %%s ");
Eric Kunzee5e26762020-10-13 16:11:07 -07004984
4985 if (tensor == nullptr)
4986 {
4987 fprintf(out, "<Not allocated>\n");
4988 return 0;
4989 }
4990
4991 fprintf(out, "[");
4992 for (int i0 = 0; i0 < shape[0]; i0++)
4993 {
4994 fprintf(out, "[");
4995 for (int i1 = 0; i1 < shape[1]; i1++)
4996 {
4997 fprintf(out, "[");
4998 for (int i2 = 0; i2 < shape[2]; i2++)
4999 {
5000 fprintf(out, bool_fmt, bool_to_str((*tensor)(i0, i1, i2)));
5001 }
5002 fprintf(out, "]\n");
5003 }
5004 fprintf(out, "]\n");
5005 }
5006 fprintf(out, "]\n");
5007
5008 return 0;
5009}
5010
5011template <>
5012int TosaReference::Tensor4<bool>::dumpTensor(FILE* out) const
5013{
Eric Kunze286f8342022-06-22 11:30:23 -07005014 char bool_fmt[32];
5015 snprintf(bool_fmt, sizeof(bool_fmt), " %%s ");
Eric Kunzee5e26762020-10-13 16:11:07 -07005016
5017 if (tensor == nullptr)
5018 {
5019 fprintf(out, "<Not allocated>\n");
5020 return 0;
5021 }
5022
5023 fprintf(out, "[");
5024 for (int i0 = 0; i0 < shape[0]; i0++)
5025 {
5026 fprintf(out, "[");
5027 for (int i1 = 0; i1 < shape[1]; i1++)
5028 {
5029 fprintf(out, "[");
5030 for (int i2 = 0; i2 < shape[2]; i2++)
5031 {
5032 fprintf(out, "[");
5033 for (int i3 = 0; i3 < shape[3]; i3++)
5034 {
5035 fprintf(out, bool_fmt, bool_to_str((*tensor)(i0, i1, i2, i3)));
5036 }
5037 fprintf(out, "]\n");
5038 }
5039 fprintf(out, "]\n");
5040 }
5041 fprintf(out, "]\n");
5042 }
5043 fprintf(out, "]\n");
5044
5045 return 0;
5046}
5047
5048template <>
5049int TosaReference::Tensor5<bool>::dumpTensor(FILE* out) const
5050{
Eric Kunze286f8342022-06-22 11:30:23 -07005051 char bool_fmt[32];
5052 snprintf(bool_fmt, sizeof(bool_fmt), " %%s ");
Eric Kunzee5e26762020-10-13 16:11:07 -07005053
5054 if (tensor == nullptr)
5055 {
5056 fprintf(out, "<Not allocated>\n");
5057 return 0;
5058 }
5059
5060 fprintf(out, "[");
5061 for (int i0 = 0; i0 < shape[0]; i0++)
5062 {
5063 fprintf(out, "[");
5064 for (int i1 = 0; i1 < shape[1]; i1++)
5065 {
5066 fprintf(out, "[");
5067 for (int i2 = 0; i2 < shape[2]; i2++)
5068 {
5069 fprintf(out, "[");
5070 for (int i3 = 0; i3 < shape[3]; i3++)
5071 {
5072 fprintf(out, "[");
5073 for (int i4 = 0; i4 < shape[4]; i4++)
5074 {
5075 fprintf(out, bool_fmt, bool_to_str((*tensor)(i0, i1, i2, i3, i4)));
5076 }
5077 fprintf(out, "]\n");
5078 }
5079 fprintf(out, "]\n");
5080 }
5081 fprintf(out, "]\n");
5082 }
5083 fprintf(out, "]\n");
5084 }
5085 fprintf(out, "]\n");
5086
5087 return 0;
5088}
5089
5090template <>
5091int TosaReference::Tensor6<bool>::dumpTensor(FILE* out) const
5092{
Eric Kunze286f8342022-06-22 11:30:23 -07005093 char bool_fmt[32];
5094 snprintf(bool_fmt, sizeof(bool_fmt), " %%s ");
Eric Kunzee5e26762020-10-13 16:11:07 -07005095
5096 if (tensor == nullptr)
5097 {
5098 fprintf(out, "<Not allocated>\n");
5099 return 0;
5100 }
5101
5102 fprintf(out, "[");
5103 for (int i0 = 0; i0 < shape[0]; i0++)
5104 {
5105 fprintf(out, "[");
5106 for (int i1 = 0; i1 < shape[1]; i1++)
5107 {
5108 fprintf(out, "[");
5109 for (int i2 = 0; i2 < shape[2]; i2++)
5110 {
5111 fprintf(out, "[");
5112 for (int i3 = 0; i3 < shape[3]; i3++)
5113 {
5114 fprintf(out, "[");
5115 for (int i4 = 0; i4 < shape[4]; i4++)
5116 {
5117 fprintf(out, "[");
5118 for (int i5 = 0; i5 < shape[5]; i5++)
5119 {
5120 fprintf(out, bool_fmt, bool_to_str((*tensor)(i0, i1, i2, i3, i4, i5)));
5121 }
5122 fprintf(out, "]\n");
5123 }
5124 fprintf(out, "]\n");
5125 }
5126 fprintf(out, "]\n");
5127 }
5128 fprintf(out, "]\n");
5129 }
5130 fprintf(out, "]\n");
5131 }
5132 fprintf(out, "]\n");
5133
5134 return 0;
5135}
5136
5137template <class T>
5138int TosaReference::TensorTemplate<T>::dumpTensor(FILE* out) const
5139{
5140 return 0;
5141}
5142
5143// template explicit specialization
Tai Lya4d748b2023-03-28 22:06:56 +00005144template class TosaReference::TensorTemplate<Eigen::Tensor<double, 0>>;
5145template class TosaReference::TensorTemplate<Eigen::Tensor<double, 1>>;
5146template class TosaReference::TensorTemplate<Eigen::Tensor<double, 2>>;
5147template class TosaReference::TensorTemplate<Eigen::Tensor<double, 3>>;
5148template class TosaReference::TensorTemplate<Eigen::Tensor<double, 4>>;
5149template class TosaReference::TensorTemplate<Eigen::Tensor<double, 5>>;
5150template class TosaReference::TensorTemplate<Eigen::Tensor<double, 6>>;
5151
Eric Kunzee5e26762020-10-13 16:11:07 -07005152template class TosaReference::TensorTemplate<Eigen::Tensor<float, 0>>;
5153template class TosaReference::TensorTemplate<Eigen::Tensor<float, 1>>;
5154template class TosaReference::TensorTemplate<Eigen::Tensor<float, 2>>;
5155template class TosaReference::TensorTemplate<Eigen::Tensor<float, 3>>;
5156template class TosaReference::TensorTemplate<Eigen::Tensor<float, 4>>;
5157template class TosaReference::TensorTemplate<Eigen::Tensor<float, 5>>;
5158template class TosaReference::TensorTemplate<Eigen::Tensor<float, 6>>;
5159
5160template class TosaReference::TensorTemplate<Eigen::Tensor<int32_t, 0>>;
5161template class TosaReference::TensorTemplate<Eigen::Tensor<int32_t, 1>>;
5162template class TosaReference::TensorTemplate<Eigen::Tensor<int32_t, 2>>;
5163template class TosaReference::TensorTemplate<Eigen::Tensor<int32_t, 3>>;
5164template class TosaReference::TensorTemplate<Eigen::Tensor<int32_t, 4>>;
5165template class TosaReference::TensorTemplate<Eigen::Tensor<int32_t, 5>>;
5166template class TosaReference::TensorTemplate<Eigen::Tensor<int32_t, 6>>;
5167
5168template class TosaReference::TensorTemplate<Eigen::Tensor<int64_t, 0>>;
5169template class TosaReference::TensorTemplate<Eigen::Tensor<int64_t, 1>>;
5170template class TosaReference::TensorTemplate<Eigen::Tensor<int64_t, 2>>;
5171template class TosaReference::TensorTemplate<Eigen::Tensor<int64_t, 3>>;
5172template class TosaReference::TensorTemplate<Eigen::Tensor<int64_t, 4>>;
5173template class TosaReference::TensorTemplate<Eigen::Tensor<int64_t, 5>>;
5174template class TosaReference::TensorTemplate<Eigen::Tensor<int64_t, 6>>;
5175
5176template class TosaReference::TensorTemplate<Eigen::Tensor<bool, 0>>;
5177template class TosaReference::TensorTemplate<Eigen::Tensor<bool, 1>>;
5178template class TosaReference::TensorTemplate<Eigen::Tensor<bool, 2>>;
5179template class TosaReference::TensorTemplate<Eigen::Tensor<bool, 3>>;
5180template class TosaReference::TensorTemplate<Eigen::Tensor<bool, 4>>;
5181template class TosaReference::TensorTemplate<Eigen::Tensor<bool, 5>>;
5182template class TosaReference::TensorTemplate<Eigen::Tensor<bool, 6>>;