blob: 7af2069612c514424ddb472ad488b21377a7a21c [file] [log] [blame]
Eric Kunzee5e26762020-10-13 16:11:07 -07001
Jerry Ge9e94af82022-10-27 09:57:00 -07002// Copyright (c) 2020-2023, 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
Kevin Chengacb550f2021-06-29 15:32:19 -070025TosaReference::Tensor::Tensor(std::string tensorName_, DType tensorDtype_, std::vector<int> shape_)
Eric Kunzee5e26762020-10-13 16:11:07 -070026{
Kevin Chengacb550f2021-06-29 15:32:19 -070027 tensorName = std::string(tensorName_);
28 tensorDtype = tensorDtype_;
29 shape = std::vector<int>(shape_);
30 producer = nullptr;
31 isValid = false;
Eric Kunzee5e26762020-10-13 16:11:07 -070032 consumers.clear();
33 isSubgraphInput = false;
34 isSubgraphOutput = false;
Jerry Ge9e94af82022-10-27 09:57:00 -070035 isParentGraphOutput = false;
Eric Kunzee5e26762020-10-13 16:11:07 -070036}
37
38TosaReference::Tensor::~Tensor()
39{}
40
Jerry Ge9e94af82022-10-27 09:57:00 -070041int TosaReference::Tensor::setIsParentGraphOutput()
42{
43 isParentGraphOutput = true;
44 return 0;
45}
46
Eric Kunzee5e26762020-10-13 16:11:07 -070047int TosaReference::Tensor::setIsSubgraphInput()
48{
49 isSubgraphInput = true;
50 return 0;
51}
52
53int TosaReference::Tensor::setIsSubgraphOutput()
54{
55 isSubgraphOutput = true;
56 return 0;
57}
58
59int TosaReference::Tensor::setProducer(GraphNode* node)
60{
61 ASSERT_MSG(node, "Tensor::setProducer: no node passed in");
62 ASSERT_MSG(!producer, "Tensor::setProducer: producer node already set, tensor %s", tensorName.c_str());
63 producer = node;
64
65 return 0;
66}
67
68int TosaReference::Tensor::addConsumer(GraphNode* node)
69{
70 ASSERT_MSG(node, "Tensor::addConsumer: no node passed in");
71 consumers.push_back(node);
72
73 return 0;
74}
75
76int TosaReference::Tensor::dumpTensorParams(FILE* out) const
77{
Kevin Cheng550ccc52021-03-03 11:21:43 -080078 fprintf(out, "Name: %s DType=%s isValid=%d Rank=%d Shape=%s\n", tensorName.c_str(), EnumNamesDType()[getDtype()],
79 getIsValid(), getRank(), getShapeAsString().c_str());
Eric Kunzee5e26762020-10-13 16:11:07 -070080
81 return 0;
82}
83
84int TosaReference::Tensor::dumpTensorParams(std::ostream& out) const
85{
Kevin Cheng550ccc52021-03-03 11:21:43 -080086 out << "Name: " << getName() << " DType=" << EnumNamesDType()[getDtype()] << " isValid=" << getIsValid()
87 << " Rank=" << getRank() << " Shape=" << getShapeAsString() << "\n";
Eric Kunzee5e26762020-10-13 16:11:07 -070088
89 return 0;
90}
91
92int TosaReference::Tensor::readFromNpyFile(const char* filename)
93{
94 uint32_t elements = getElementCount();
95 float* fdatabuf = nullptr;
James Ward8b390432022-08-12 20:48:56 +010096 half_float::half* f16databuf = nullptr;
Eric Kunzee5e26762020-10-13 16:11:07 -070097 int32_t* i32databuf = nullptr;
98 int64_t* i64databuf = nullptr;
99 bool* bdatabuf = nullptr;
100 NumpyUtilities::NPError nperror;
James Ward24dbc422022-10-19 12:20:31 +0100101 DType dtype = getDtype();
Eric Kunzee5e26762020-10-13 16:11:07 -0700102
James Ward24dbc422022-10-19 12:20:31 +0100103 switch (dtype)
Eric Kunzee5e26762020-10-13 16:11:07 -0700104 {
Jeremy Johnsonbc2a3db2022-09-27 13:50:00 +0100105 case DType_FP32:
James Ward24dbc422022-10-19 12:20:31 +0100106 case DType_BF16:
Eric Kunzee5e26762020-10-13 16:11:07 -0700107 fdatabuf = (float*)calloc(sizeof(float), elements);
108 ASSERT_MEM(fdatabuf);
109
110 nperror = NumpyUtilities::readFromNpyFile(filename, elements, fdatabuf);
111 break;
James Ward8b390432022-08-12 20:48:56 +0100112 case DType_FP16:
113 f16databuf = (half_float::half*)calloc(sizeof(half_float::half), elements);
114 ASSERT_MEM(f16databuf);
115 fdatabuf = (float*)calloc(sizeof(float), elements);
116 ASSERT_MEM(fdatabuf);
117
118 nperror = NumpyUtilities::readFromNpyFile(filename, elements, f16databuf);
119 break;
Eric Kunzee5e26762020-10-13 16:11:07 -0700120 case DType_INT32:
Eric Kunzee5e26762020-10-13 16:11:07 -0700121 case DType_UINT8:
122 case DType_INT4:
123 case DType_INT8:
124 case DType_INT16:
Jeremy Johnsonf7f78ae2022-05-25 15:26:38 +0100125 case DType_UINT16:
Eric Kunzee5e26762020-10-13 16:11:07 -0700126 i32databuf = (int32_t*)calloc(sizeof(int32_t), elements);
127 ASSERT_MEM(i32databuf);
128
129 nperror = NumpyUtilities::readFromNpyFile(filename, elements, i32databuf);
130 break;
131 case DType_INT48:
132 i64databuf = (int64_t*)calloc(sizeof(int64_t), elements);
133 ASSERT_MEM(i64databuf);
134
135 nperror = NumpyUtilities::readFromNpyFile(filename, elements, i64databuf);
136 break;
137 case DType_BOOL:
138 bdatabuf = (bool*)calloc(sizeof(bool), elements);
139 ASSERT_MEM(bdatabuf);
140
141 nperror = NumpyUtilities::readFromNpyFile(filename, elements, bdatabuf);
142 break;
143 default:
144 FATAL_ERROR("unsupported tensor type=%s", EnumNamesDType()[getDtype()]);
145 }
146
147 switch (nperror)
148 {
149 case NumpyUtilities::NO_ERROR:
150 break;
151 case NumpyUtilities::FILE_NOT_FOUND:
152 FATAL_ERROR("readFromNpyFile: Cannot open file %s", filename);
153 case NumpyUtilities::FILE_IO_ERROR:
154 FATAL_ERROR("readFromNpyFile: IO error reading file: %s", filename);
155 case NumpyUtilities::FILE_TYPE_MISMATCH:
156 FATAL_ERROR("readFromNpyFile: Tensor type %s and Numpy file type mismatch for tensor %s filename %s",
157 EnumNamesDType()[getDtype()], getName().c_str(), filename);
158 case NumpyUtilities::HEADER_PARSE_ERROR:
159 FATAL_ERROR("Numpy header parsing error for file: %s", filename);
160 case NumpyUtilities::BUFFER_SIZE_MISMATCH:
161 FATAL_ERROR("Buffer size does not match numpy file size for tensor %s filename %s", getName().c_str(),
162 filename);
163 default:
164 FATAL_ERROR("Unknown error parsing Numpy file: %s", filename);
165 }
166
James Ward24dbc422022-10-19 12:20:31 +0100167 switch (dtype)
Eric Kunzee5e26762020-10-13 16:11:07 -0700168 {
James Ward8b390432022-08-12 20:48:56 +0100169 case DType_FP16:
James Wardee256692022-11-15 11:36:47 +0000170 // Convert from fp16 to fp32 so that fp16 values can be manipulated as float
James Ward8b390432022-08-12 20:48:56 +0100171 for (uint32_t i=0; i < elements; i++) {
172 fdatabuf[i] = half_float::half_cast<float, half_float::half>(f16databuf[i]);
173 }
James Ward24dbc422022-10-19 12:20:31 +0100174 if (setTensorValueFloat(elements, fdatabuf))
175 {
176 free(f16databuf);
177 free(fdatabuf);
178 return 1;
179 }
180 break;
181 case DType_BF16:
182 for (uint32_t i=0; i < elements; i++)
183 {
184 ASSERT_MSG(
185 checkValidBFloat(fdatabuf[i]),
186 "Input float value not a valid bfloat16 value."
187 );
188 }
189 if (setTensorValueFloat(elements, fdatabuf))
190 {
191 free(fdatabuf);
192 return 1;
193 }
194 break;
Jeremy Johnsonbc2a3db2022-09-27 13:50:00 +0100195 case DType_FP32:
Eric Kunzee5e26762020-10-13 16:11:07 -0700196 if (setTensorValueFloat(elements, fdatabuf))
197 {
198 free(fdatabuf);
199 return 1;
200 }
201 break;
202 case DType_INT32:
Eric Kunzee5e26762020-10-13 16:11:07 -0700203 case DType_UINT8:
204 case DType_INT4:
205 case DType_INT8:
206 case DType_INT16:
Jeremy Johnsonf7f78ae2022-05-25 15:26:38 +0100207 case DType_UINT16:
Eric Kunzee5e26762020-10-13 16:11:07 -0700208 if (setTensorValueInt32(elements, i32databuf))
209 {
210 free(i32databuf);
211 return 1;
212 }
213 break;
214 case DType_INT48:
215 if (setTensorValueInt64(elements, i64databuf))
216 {
217 free(i64databuf);
218 return 1;
219 }
220 break;
221 case DType_BOOL:
222 if (setTensorValueBool(elements, bdatabuf))
223 {
224 free(i32databuf);
225 return 1;
226 }
227 break;
228 default:
229 FATAL_ERROR("unsupported tensor type=%s", EnumNamesDType()[getDtype()]);
230 }
231
232 setIsValid();
233
234 if (fdatabuf)
235 free(fdatabuf);
James Ward8b390432022-08-12 20:48:56 +0100236 if (f16databuf)
237 free(f16databuf);
Eric Kunzee5e26762020-10-13 16:11:07 -0700238 if (i32databuf)
239 free(i32databuf);
240 if (i64databuf)
241 free(i64databuf);
242 if (bdatabuf)
243 free(bdatabuf);
244
245 return 0;
246}
247
248int TosaReference::Tensor::writeToNpyFile(const char* filename) const
249{
250 float* fdatabuf = nullptr;
James Ward8b390432022-08-12 20:48:56 +0100251 half_float::half* f16databuf = nullptr;
Eric Kunzee5e26762020-10-13 16:11:07 -0700252 int32_t* i32databuf = nullptr;
253 int64_t* i64databuf = nullptr;
254 bool* bdatabuf = nullptr;
255 NumpyUtilities::NPError nperror;
James Ward8b390432022-08-12 20:48:56 +0100256 uint32_t elements = getElementCount();
James Ward24dbc422022-10-19 12:20:31 +0100257 DType dtype = getDtype();
Eric Kunzee5e26762020-10-13 16:11:07 -0700258
James Ward24dbc422022-10-19 12:20:31 +0100259 switch (dtype)
Eric Kunzee5e26762020-10-13 16:11:07 -0700260 {
Jeremy Johnsonbc2a3db2022-09-27 13:50:00 +0100261 case DType_FP32:
James Ward24dbc422022-10-19 12:20:31 +0100262 case DType_BF16:
Eric Kunzee5e26762020-10-13 16:11:07 -0700263 fdatabuf = (float*)calloc(sizeof(float), elements);
264 ASSERT_MEM(fdatabuf);
265
266 if (getTensorValueFloat(elements, fdatabuf))
267 {
268 free(fdatabuf);
269 return 1;
270 }
Eric Kunzee5e26762020-10-13 16:11:07 -0700271 nperror = NumpyUtilities::writeToNpyFile(filename, shape, fdatabuf);
272
273 free(fdatabuf);
274 break;
James Ward8b390432022-08-12 20:48:56 +0100275 case DType_FP16:
276 fdatabuf = (float*)calloc(sizeof(float), elements);
277 ASSERT_MEM(fdatabuf);
278 f16databuf = (half_float::half*)calloc(sizeof(half_float::half), elements);
279 ASSERT_MEM(f16databuf);
280
281 if (getTensorValueFloat(elements, fdatabuf))
282 {
283 free(fdatabuf);
284 free(f16databuf);
285 return 1;
286 }
James Wardee256692022-11-15 11:36:47 +0000287 // Convert fp32 to fp16 so that output file contains valid fp16 data
James Ward8b390432022-08-12 20:48:56 +0100288 for (uint32_t i=0; i < elements; i++) {
289 f16databuf[i] = half_float::half_cast<half_float::half, float>(fdatabuf[i]);
290 }
291 nperror = NumpyUtilities::writeToNpyFile(filename, shape, f16databuf);
292
293 free(fdatabuf);
294 free(f16databuf);
295 break;
Eric Kunzee5e26762020-10-13 16:11:07 -0700296 case DType_INT32:
Eric Kunzee5e26762020-10-13 16:11:07 -0700297 case DType_UINT8:
298 case DType_INT4:
299 case DType_INT8:
300 case DType_INT16:
Jeremy Johnsonf7f78ae2022-05-25 15:26:38 +0100301 case DType_UINT16:
Eric Kunzee5e26762020-10-13 16:11:07 -0700302 i32databuf = (int32_t*)calloc(sizeof(int32_t), elements);
303 ASSERT_MEM(i32databuf);
304
305 if (getTensorValueInt32(elements, i32databuf))
306 {
307 free(i32databuf);
308 return 1;
309 }
310
311 nperror = NumpyUtilities::writeToNpyFile(filename, shape, i32databuf);
312
313 free(i32databuf);
314 break;
315 case DType_INT48:
316 i64databuf = (int64_t*)calloc(sizeof(int64_t), elements);
317 ASSERT_MEM(i64databuf);
318
319 if (getTensorValueInt64(elements, i64databuf))
320 {
321 free(i64databuf);
322 return 1;
323 }
324
325 nperror = NumpyUtilities::writeToNpyFile(filename, shape, i64databuf);
326
327 free(i64databuf);
328 break;
329 case DType_BOOL:
330 bdatabuf = (bool*)calloc(sizeof(bool), elements);
331 ASSERT_MEM(bdatabuf);
332
333 if (getTensorValueBool(elements, bdatabuf))
334 {
335 free(bdatabuf);
336 return 1;
337 }
338
339 nperror = NumpyUtilities::writeToNpyFile(filename, shape, bdatabuf);
340
341 free(bdatabuf);
342 break;
343 default:
344 FATAL_ERROR("unsupported tensor type=%s", EnumNamesDType()[getDtype()]);
345 }
346
347 switch (nperror)
348 {
349 case NumpyUtilities::NO_ERROR:
350 break;
351 case NumpyUtilities::FILE_NOT_FOUND:
352 FATAL_ERROR("writeToNpyFile: Cannot open output file %s", filename);
353 case NumpyUtilities::FILE_IO_ERROR:
354 FATAL_ERROR("writeToNpyFile: IO error writing file: %s", filename);
355 case NumpyUtilities::FILE_TYPE_MISMATCH:
356 FATAL_ERROR("writeToNpyFile: Tensor type and Numpy file type mismatch for tensor %s filename %s",
357 getName().c_str(), filename);
358 case NumpyUtilities::HEADER_PARSE_ERROR:
359 FATAL_ERROR("Numpy header parsing error for file: %s", filename);
360 case NumpyUtilities::BUFFER_SIZE_MISMATCH:
361 FATAL_ERROR("Buffer size does not match numpy file size for tensor %s filename %s", getName().c_str(),
362 filename);
363 default:
364 FATAL_ERROR("Unknown error writing Numpy file: %s", filename);
365 }
366
367 return 0;
368}
369
370template <class T>
371int TosaReference::TensorTemplate<T>::copyValueFrom(TosaReference::Tensor* src)
372{
373 FATAL_ERROR("TensorTemplate<T>::copyValueFrom should not be called. "
374 "Implement template specialization version.");
375 return 0;
376}
377
378#define DEF_CTENSOR_COPY_VALUE_FROM(RANK, TYPE) \
379 template <> \
380 int TosaReference::Tensor##RANK<TYPE>::copyValueFrom(TosaReference::Tensor* src) \
381 { \
382 TosaReference::Tensor##RANK<TYPE>* t = dynamic_cast<Tensor##RANK<TYPE>*>(src); \
383 if (!t) \
384 { \
385 WARNING("tensor %s templated class does not match %s", src->getName().c_str(), this->getName().c_str()); \
386 return 1; \
387 } \
388 \
389 uint32_t src_rank = src->getRank(); \
390 uint32_t dst_rank = this->getRank(); \
391 DType src_dtype = src->getDtype(); \
392 DType dst_dtype = this->getDtype(); \
393 bool tensor_match = true; \
394 \
395 if ((src_rank != dst_rank) || (src_dtype != dst_dtype)) \
396 { \
397 tensor_match = false; \
398 } \
399 else \
400 { \
401 for (uint32_t i = 0; i < src_rank; i++) \
402 { \
403 int src_dim = src->getShape()[i]; \
404 int dst_dim = this->getShape()[i]; \
405 if (src_dim != dst_dim) \
406 { \
407 tensor_match = false; \
408 } \
409 } \
410 } \
411 \
412 if (!tensor_match) \
413 { \
414 WARNING("source tensor %s (rank=%u, dtype=%s, shape=%s) doesn't match destination tensor %s (rank=%u, " \
415 "dtype=%s, shape=%s)", \
416 src->getName().c_str(), src_rank, EnumNamesDType()[src_dtype], src->getShapeAsString().c_str(), \
417 this->getName().c_str(), dst_rank, EnumNamesDType()[dst_dtype], this->getShapeAsString().c_str()); \
418 return 1; \
419 } \
420 \
421 this->getTensor() = t->getTensor(); \
422 return 0; \
423 }
424
425DEF_CTENSOR_COPY_VALUE_FROM(0, float)
426DEF_CTENSOR_COPY_VALUE_FROM(1, float)
427DEF_CTENSOR_COPY_VALUE_FROM(2, float)
428DEF_CTENSOR_COPY_VALUE_FROM(3, float)
429DEF_CTENSOR_COPY_VALUE_FROM(4, float)
430DEF_CTENSOR_COPY_VALUE_FROM(5, float)
431DEF_CTENSOR_COPY_VALUE_FROM(6, float)
432DEF_CTENSOR_COPY_VALUE_FROM(0, int32_t)
433DEF_CTENSOR_COPY_VALUE_FROM(1, int32_t)
434DEF_CTENSOR_COPY_VALUE_FROM(2, int32_t)
435DEF_CTENSOR_COPY_VALUE_FROM(3, int32_t)
436DEF_CTENSOR_COPY_VALUE_FROM(4, int32_t)
437DEF_CTENSOR_COPY_VALUE_FROM(5, int32_t)
438DEF_CTENSOR_COPY_VALUE_FROM(6, int32_t)
439DEF_CTENSOR_COPY_VALUE_FROM(0, int64_t)
440DEF_CTENSOR_COPY_VALUE_FROM(1, int64_t)
441DEF_CTENSOR_COPY_VALUE_FROM(2, int64_t)
442DEF_CTENSOR_COPY_VALUE_FROM(3, int64_t)
443DEF_CTENSOR_COPY_VALUE_FROM(4, int64_t)
444DEF_CTENSOR_COPY_VALUE_FROM(5, int64_t)
445DEF_CTENSOR_COPY_VALUE_FROM(6, int64_t)
446DEF_CTENSOR_COPY_VALUE_FROM(0, bool)
447DEF_CTENSOR_COPY_VALUE_FROM(1, bool)
448DEF_CTENSOR_COPY_VALUE_FROM(2, bool)
449DEF_CTENSOR_COPY_VALUE_FROM(3, bool)
450DEF_CTENSOR_COPY_VALUE_FROM(4, bool)
451DEF_CTENSOR_COPY_VALUE_FROM(5, bool)
452DEF_CTENSOR_COPY_VALUE_FROM(6, bool)
453
454#undef DEF_CTENSOR_COPY_VALUE_FROM
455
Grant Watson64285a12022-11-16 15:32:39 +0000456int TosaReference::Tensor::readfromVector(const ArrayProxy<float> vals)
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100457{
458 uint32_t elements = getElementCount();
459 switch (getDtype())
460 {
Matthew Sloyan2e4d8892022-10-18 18:02:48 +0100461 case DType_FP16:
Jeremy Johnsonbc2a3db2022-09-27 13:50:00 +0100462 case DType_FP32:
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100463 if (vals.size() != elements)
464 {
465 WARNING("The input size (%ld) doesn't match the number of elements (%d) assigned to the tensor.",
466 vals.size(), elements);
467 return -1;
468 }
469
470 setTensorValueFloat(elements, vals.data());
471 break;
James Ward3d3d45d2022-11-28 16:45:36 +0000472 case DType_BF16:
473 if (vals.size() != elements)
474 {
475 WARNING("The input size (%ld) doesn't match the number of elements (%d) assigned to the tensor.",
476 vals.size(), elements);
477 return -1;
478 }
479
480 for (auto v : vals)
481 {
482 ASSERT_MSG(
483 checkValidBFloat(v),
484 "Input float value not a valid bfloat16 value."
485 );
486 }
487
488 setTensorValueFloat(elements, vals.data());
489 break;
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100490 default:
491 WARNING("The input type (float) doesn't match the data type assigned to the tensor (%s).",
492 EnumNameDType(getDtype()));
493 return -2;
494 }
495 setIsValid();
496 return 0;
497}
498
Grant Watson64285a12022-11-16 15:32:39 +0000499int TosaReference::Tensor::readfromVector(const ArrayProxy<half_float::half> vals)
Matthew Sloyan2e4d8892022-10-18 18:02:48 +0100500{
501 uint32_t elements = getElementCount();
502 std::vector<float> tensor(elements);
503
504 switch (getDtype())
505 {
506 case DType_FP16:
507 if (vals.size() != elements)
508 {
509 WARNING("The input size (%ld) doesn't match the number of elements (%d) assigned to the tensor.",
510 vals.size(), elements);
511 return -1;
512 }
513
514 // Convert from fp16 to fp32
515 for (uint32_t i=0; i < elements; i++)
516 {
517 tensor[i] = half_float::half_cast<float, half_float::half>(vals[i]);
518 }
519
520 setTensorValueFloat(elements, tensor.data());
521 break;
522 default:
523 WARNING("The input type doesn't match the data type assigned to the tensor (%s).",
524 EnumNameDType(getDtype()));
525 return -2;
526 }
527 setIsValid();
528 return 0;
529}
530
Grant Watson64285a12022-11-16 15:32:39 +0000531int TosaReference::Tensor::readfromVector(const ArrayProxy<int32_t> vals)
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100532{
533 uint32_t elements = getElementCount();
534 switch (getDtype())
535 {
536 case DType_INT32:
537 case DType_UINT8:
538 case DType_INT4:
539 case DType_INT8:
540 case DType_INT16:
541 case DType_UINT16:
542 if (vals.size() != elements)
543 {
544 WARNING("The input size (%ld) doesn't match the number of elements (%d) assigned to the tensor.",
545 vals.size(), elements);
546 return -1;
547 }
548
549 setTensorValueInt32(elements, vals.data());
550 break;
551 default:
552 WARNING("The input type doesn't match the data type assigned to the tensor (%s).",
553 EnumNameDType(getDtype()));
554 return -2;
555 }
556 setIsValid();
557 return 0;
558}
559
Grant Watson64285a12022-11-16 15:32:39 +0000560int TosaReference::Tensor::readfromVector(const ArrayProxy<int64_t> vals)
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100561{
562 uint32_t elements = getElementCount();
563 switch (getDtype())
564 {
565 case DType_INT48:
566 if (vals.size() != elements)
567 {
568 WARNING("The input size (%ld) doesn't match the number of elements (%d) assigned to the tensor.",
569 vals.size(), elements);
570 return -1;
571 }
572
573 setTensorValueInt64(elements, vals.data());
574 break;
575 default:
576 WARNING("The input type doesn't match the data type assigned to the tensor (%s).",
577 EnumNameDType(getDtype()));
578 return -2;
579 }
580 setIsValid();
581 return 0;
582}
583
Grant Watson64285a12022-11-16 15:32:39 +0000584int TosaReference::Tensor::readfromVector(const ArrayProxy<unsigned char> vals)
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100585{
586 uint32_t elements = getElementCount();
587
588 switch (getDtype())
589 {
590 case DType_BOOL:
591 if (vals.size() != elements)
592 {
593 WARNING("The input size (%ld) doesn't match the number of elements (%d) assigned to the tensor.",
594 vals.size(), elements);
595 return -1;
596 }
597
598 setTensorValueBool(elements, reinterpret_cast<const bool*>(vals.data()));
599 break;
600 default:
601 WARNING("The input type (bool) doesn't match the data type assigned to the tensor (%s).",
602 EnumNameDType(getDtype()));
603 return -2;
604 }
605 setIsValid();
606 return 0;
607}
608
Grant Watson64285a12022-11-16 15:32:39 +0000609int TosaReference::Tensor::writeToVector(ArrayProxy<float> vals)
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100610{
611 uint32_t elements = getElementCount();
612
613 switch (getDtype())
614 {
Matthew Sloyan2e4d8892022-10-18 18:02:48 +0100615 case DType_FP16:
Jeremy Johnsonbc2a3db2022-09-27 13:50:00 +0100616 case DType_FP32:
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100617 if (vals.size() != elements)
618 {
619 WARNING("The output size (%ld) doesn't match the number of elements (%d) assigned to the tensor.",
620 vals.size(), elements);
621 return -1;
622 }
623
624 getTensorValueFloat(elements, vals.data());
625 break;
James Ward3d3d45d2022-11-28 16:45:36 +0000626 case DType_BF16:
627 if (vals.size() != elements)
628 {
629 WARNING("The output size (%ld) doesn't match the number of elements (%d) assigned to the tensor.",
630 vals.size(), elements);
631 return -1;
632 }
633
634 getTensorValueFloat(elements, vals.data());
635
636 for (auto v : vals)
637 {
638 ASSERT_MSG(
639 checkValidBFloat(v),
640 "Float value not a valid bfloat16 value."
641 );
642 }
643
644 break;
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100645 default:
646 WARNING("The output type (float) doesn't match the data type assigned to the tensor (%s).",
647 EnumNameDType(getDtype()));
648 return -2;
649 }
650 return 0;
651}
652
Grant Watson64285a12022-11-16 15:32:39 +0000653int TosaReference::Tensor::writeToVector(ArrayProxy<half_float::half> vals)
Matthew Sloyan2e4d8892022-10-18 18:02:48 +0100654{
655 uint32_t elements = getElementCount();
656 std::vector<float> tensor(elements);
657
658 switch (getDtype())
659 {
660 case DType_FP16:
661 if (vals.size() != elements)
662 {
663 WARNING("The output size (%ld) doesn't match the number of elements (%d) assigned to the tensor.",
664 vals.size(), elements);
665 return -1;
666 }
667
668 getTensorValueFloat(elements, tensor.data());
669
670 // Convert fp32 to fp16
671 for (uint32_t i=0; i < elements; i++)
672 {
673 vals[i] = half_float::half_cast<half_float::half, float>(tensor[i]);
674 }
675 break;
676 default:
677 WARNING("The output type doesn't match the data type assigned to the tensor (%s).",
678 EnumNameDType(getDtype()));
679 return -2;
680 }
681 return 0;
682}
683
Grant Watson64285a12022-11-16 15:32:39 +0000684int TosaReference::Tensor::writeToVector(ArrayProxy<int32_t> vals)
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100685{
686 uint32_t elements = getElementCount();
687
688 switch (getDtype())
689 {
690 case DType_INT32:
691 case DType_UINT8:
692 case DType_INT4:
693 case DType_INT8:
694 case DType_INT16:
695 case DType_UINT16:
696 if (vals.size() != elements)
697 {
698 WARNING("The output size (%ld) doesn't match the number of elements (%d) assigned to the tensor.",
699 vals.size(), elements);
700 return -1;
701 }
702
703 getTensorValueInt32(elements, vals.data());
704 break;
705 default:
706 WARNING("The output type doesn't match the data type assigned to the tensor (%s).",
707 EnumNameDType(getDtype()));
708 return -2;
709 }
710 return 0;
711}
712
Grant Watson64285a12022-11-16 15:32:39 +0000713int TosaReference::Tensor::writeToVector(ArrayProxy<int64_t> vals)
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100714{
715 uint32_t elements = getElementCount();
716
717 switch (getDtype())
718 {
719 case tosa::DType_INT48:
720 if (vals.size() != elements)
721 {
722 WARNING("The output size (%ld) doesn't match the number of elements (%d) assigned to the tensor.",
723 vals.size(), elements);
724 return -1;
725 }
726
727 getTensorValueInt64(elements, vals.data());
728 break;
729 default:
730 WARNING("The output type doesn't match the data type assigned to the tensor (%s).",
731 EnumNameDType(getDtype()));
732 return -2;
733 }
734 return 0;
735}
736
Grant Watson64285a12022-11-16 15:32:39 +0000737int TosaReference::Tensor::writeToVector(ArrayProxy<unsigned char> vals)
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100738{
739 uint32_t elements = getElementCount();
740
741 switch (getDtype())
742 {
743 case tosa::DType_BOOL:
744 if (vals.size() != elements)
745 {
746 WARNING("The output size (%ld) doesn't match the number of elements (%d) assigned to the tensor.",
747 vals.size(), elements);
748 return -1;
749 }
750
751 getTensorValueBool(elements, reinterpret_cast<bool*>(vals.data()));
752 break;
753 default:
754 WARNING("The output type (bool) doesn't match the data type assigned to the tensor (%s).",
755 EnumNameDType(getDtype()));
756 return -2;
757 }
758 return 0;
759}
760
Eric Kunzee5e26762020-10-13 16:11:07 -0700761template <class T>
762int TosaReference::TensorTemplate<T>::setTensorValueFloat(const size_t buflen, const float* vals)
763{
764 FATAL_ERROR("TensorTemplate<T>::setTensorValueFloat should not be called. "
765 "Implement template specialization version.");
766 return 0;
767}
768
769template <>
770int TosaReference::Tensor0<float>::setTensorValueFloat(const size_t bufLen, const float* vals)
771{
772 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
773
774 (*tensor)(0) = vals[0];
775
776 return 0;
777}
778
779template <>
780int TosaReference::Tensor1<float>::setTensorValueFloat(const size_t bufLen, const float* vals)
781{
782 uint32_t idx = 0;
783
784 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
785
786 for (int i0 = 0; i0 < shape[0]; i0++)
787 {
788 (*tensor)(i0) = vals[idx++];
789 }
790
791 return 0;
792}
793
794template <>
795int TosaReference::Tensor2<float>::setTensorValueFloat(const size_t bufLen, const float* vals)
796{
797 uint32_t idx = 0;
798
799 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
800
801 for (int i0 = 0; i0 < shape[0]; i0++)
802 {
803 for (int i1 = 0; i1 < shape[1]; i1++)
804 {
805 (*tensor)(i0, i1) = vals[idx++];
806 }
807 }
808
809 return 0;
810}
811
812template <>
813int TosaReference::Tensor3<float>::setTensorValueFloat(const size_t bufLen, const float* vals)
814{
815 uint32_t idx = 0;
816
817 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
818
819 for (int i0 = 0; i0 < shape[0]; i0++)
820 {
821 for (int i1 = 0; i1 < shape[1]; i1++)
822 {
823 for (int i2 = 0; i2 < shape[2]; i2++)
824 {
825 (*tensor)(i0, i1, i2) = vals[idx++];
826 }
827 }
828 }
829
830 return 0;
831}
832
833template <>
834int TosaReference::Tensor4<float>::setTensorValueFloat(const size_t bufLen, const float* vals)
835{
836 uint32_t idx = 0;
837
838 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
839
840 for (int i0 = 0; i0 < shape[0]; i0++)
841 {
842 for (int i1 = 0; i1 < shape[1]; i1++)
843 {
844 for (int i2 = 0; i2 < shape[2]; i2++)
845 {
846 for (int i3 = 0; i3 < shape[3]; i3++)
847 {
848 (*tensor)(i0, i1, i2, i3) = vals[idx++];
849 }
850 }
851 }
852 }
853
854 return 0;
855}
856
857template <>
858int TosaReference::Tensor5<float>::setTensorValueFloat(const size_t bufLen, const float* vals)
859{
860 uint32_t idx = 0;
861
862 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
863
864 for (int i0 = 0; i0 < shape[0]; i0++)
865 {
866 for (int i1 = 0; i1 < shape[1]; i1++)
867 {
868 for (int i2 = 0; i2 < shape[2]; i2++)
869 {
870 for (int i3 = 0; i3 < shape[3]; i3++)
871 {
872 for (int i4 = 0; i4 < shape[4]; i4++)
873 {
874 (*tensor)(i0, i1, i2, i3, i4) = vals[idx++];
875 }
876 }
877 }
878 }
879 }
880
881 return 0;
882}
883
884template <>
885int TosaReference::Tensor6<float>::setTensorValueFloat(const size_t bufLen, const float* vals)
886{
887 uint32_t idx = 0;
888
889 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
890
891 for (int i0 = 0; i0 < shape[0]; i0++)
892 {
893 for (int i1 = 0; i1 < shape[1]; i1++)
894 {
895 for (int i2 = 0; i2 < shape[2]; i2++)
896 {
897 for (int i3 = 0; i3 < shape[3]; i3++)
898 {
899 for (int i4 = 0; i4 < shape[4]; i4++)
900 {
901 for (int i5 = 0; i5 < shape[5]; i5++)
902 {
903 (*tensor)(i0, i1, i2, i3, i4, i5) = vals[idx++];
904 }
905 }
906 }
907 }
908 }
909 }
910 return 0;
911}
912
913template <class T>
914int TosaReference::TensorTemplate<T>::setTensorValueInt32(const size_t bufLen, const int32_t* vals)
915{
916 FATAL_ERROR("TensorTemplate<T>::setTensorValueInt32 should not be called. "
917 "Implement template specialization version.");
918 return 0;
919}
920
921template <>
922int TosaReference::Tensor0<int32_t>::setTensorValueInt32(const size_t bufLen, const int32_t* vals)
923{
924 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
925
926 (*tensor)(0) = vals[0];
927
928 return 0;
929}
930
931template <>
932int TosaReference::Tensor1<int32_t>::setTensorValueInt32(const size_t bufLen, const int32_t* vals)
933{
934 uint32_t idx = 0;
935
936 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
937
938 for (int i0 = 0; i0 < shape[0]; i0++)
939 {
940 (*tensor)(i0) = vals[idx++];
941 }
942
943 return 0;
944}
945
946template <>
947int TosaReference::Tensor2<int32_t>::setTensorValueInt32(const size_t bufLen, const int32_t* vals)
948{
949 uint32_t idx = 0;
950
951 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
952
953 for (int i0 = 0; i0 < shape[0]; i0++)
954 {
955 for (int i1 = 0; i1 < shape[1]; i1++)
956 {
957 (*tensor)(i0, i1) = vals[idx++];
958 }
959 }
960
961 return 0;
962}
963
964template <>
965int TosaReference::Tensor3<int32_t>::setTensorValueInt32(const size_t bufLen, const int32_t* vals)
966{
967 uint32_t idx = 0;
968
969 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
970
971 for (int i0 = 0; i0 < shape[0]; i0++)
972 {
973 for (int i1 = 0; i1 < shape[1]; i1++)
974 {
975 for (int i2 = 0; i2 < shape[2]; i2++)
976 {
977 (*tensor)(i0, i1, i2) = vals[idx++];
978 }
979 }
980 }
981
982 return 0;
983}
984
985template <>
986int TosaReference::Tensor4<int32_t>::setTensorValueInt32(const size_t bufLen, const int32_t* vals)
987{
988 uint32_t idx = 0;
989
990 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
991
992 for (int i0 = 0; i0 < shape[0]; i0++)
993 {
994 for (int i1 = 0; i1 < shape[1]; i1++)
995 {
996 for (int i2 = 0; i2 < shape[2]; i2++)
997 {
998 for (int i3 = 0; i3 < shape[3]; i3++)
999 {
1000 (*tensor)(i0, i1, i2, i3) = vals[idx++];
1001 }
1002 }
1003 }
1004 }
1005
1006 return 0;
1007}
1008
1009template <>
1010int TosaReference::Tensor5<int32_t>::setTensorValueInt32(const size_t bufLen, const int32_t* vals)
1011{
1012 uint32_t idx = 0;
1013
1014 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1015
1016 for (int i0 = 0; i0 < shape[0]; i0++)
1017 {
1018 for (int i1 = 0; i1 < shape[1]; i1++)
1019 {
1020 for (int i2 = 0; i2 < shape[2]; i2++)
1021 {
1022 for (int i3 = 0; i3 < shape[3]; i3++)
1023 {
1024 for (int i4 = 0; i4 < shape[4]; i4++)
1025 {
1026 (*tensor)(i0, i1, i2, i3, i4) = vals[idx++];
1027 }
1028 }
1029 }
1030 }
1031 }
1032
1033 return 0;
1034}
1035
1036template <>
1037int TosaReference::Tensor6<int32_t>::setTensorValueInt32(const size_t bufLen, const int32_t* vals)
1038{
1039 uint32_t idx = 0;
1040
1041 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1042
1043 for (int i0 = 0; i0 < shape[0]; i0++)
1044 {
1045 for (int i1 = 0; i1 < shape[1]; i1++)
1046 {
1047 for (int i2 = 0; i2 < shape[2]; i2++)
1048 {
1049 for (int i3 = 0; i3 < shape[3]; i3++)
1050 {
1051 for (int i4 = 0; i4 < shape[4]; i4++)
1052 {
1053 for (int i5 = 0; i5 < shape[5]; i5++)
1054 {
1055 (*tensor)(i0, i1, i2, i3, i4, i5) = vals[idx++];
1056 }
1057 }
1058 }
1059 }
1060 }
1061 }
1062 return 0;
1063}
1064
1065template <class T>
1066int TosaReference::TensorTemplate<T>::setTensorValueInt64(const size_t bufLen, const int64_t* vals)
1067{
1068 FATAL_ERROR("TensorTemplate<T>::setTensorValueInt64 should not be called. "
1069 "Implement template specialization version.");
1070 return 0;
1071}
1072
1073template <>
1074int TosaReference::Tensor0<int64_t>::setTensorValueInt64(const size_t bufLen, const int64_t* vals)
1075{
1076 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1077
1078 (*tensor)(0) = vals[0];
1079
1080 return 0;
1081}
1082
1083template <>
1084int TosaReference::Tensor1<int64_t>::setTensorValueInt64(const size_t bufLen, const int64_t* vals)
1085{
1086 uint32_t idx = 0;
1087
1088 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1089
1090 for (int i0 = 0; i0 < shape[0]; i0++)
1091 {
1092 (*tensor)(i0) = vals[idx++];
1093 }
1094
1095 return 0;
1096}
1097
1098template <>
1099int TosaReference::Tensor2<int64_t>::setTensorValueInt64(const size_t bufLen, const int64_t* vals)
1100{
1101 uint32_t idx = 0;
1102
1103 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1104
1105 for (int i0 = 0; i0 < shape[0]; i0++)
1106 {
1107 for (int i1 = 0; i1 < shape[1]; i1++)
1108 {
1109 (*tensor)(i0, i1) = vals[idx++];
1110 }
1111 }
1112
1113 return 0;
1114}
1115
1116template <>
1117int TosaReference::Tensor3<int64_t>::setTensorValueInt64(const size_t bufLen, const int64_t* vals)
1118{
1119 uint32_t idx = 0;
1120
1121 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1122
1123 for (int i0 = 0; i0 < shape[0]; i0++)
1124 {
1125 for (int i1 = 0; i1 < shape[1]; i1++)
1126 {
1127 for (int i2 = 0; i2 < shape[2]; i2++)
1128 {
1129 (*tensor)(i0, i1, i2) = vals[idx++];
1130 }
1131 }
1132 }
1133
1134 return 0;
1135}
1136
1137template <>
1138int TosaReference::Tensor4<int64_t>::setTensorValueInt64(const size_t bufLen, const int64_t* vals)
1139{
1140 uint32_t idx = 0;
1141
1142 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1143
1144 for (int i0 = 0; i0 < shape[0]; i0++)
1145 {
1146 for (int i1 = 0; i1 < shape[1]; i1++)
1147 {
1148 for (int i2 = 0; i2 < shape[2]; i2++)
1149 {
1150 for (int i3 = 0; i3 < shape[3]; i3++)
1151 {
1152 (*tensor)(i0, i1, i2, i3) = vals[idx++];
1153 }
1154 }
1155 }
1156 }
1157
1158 return 0;
1159}
1160
1161template <>
1162int TosaReference::Tensor5<int64_t>::setTensorValueInt64(const size_t bufLen, const int64_t* vals)
1163{
1164 uint32_t idx = 0;
1165
1166 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1167
1168 for (int i0 = 0; i0 < shape[0]; i0++)
1169 {
1170 for (int i1 = 0; i1 < shape[1]; i1++)
1171 {
1172 for (int i2 = 0; i2 < shape[2]; i2++)
1173 {
1174 for (int i3 = 0; i3 < shape[3]; i3++)
1175 {
1176 for (int i4 = 0; i4 < shape[4]; i4++)
1177 {
1178 (*tensor)(i0, i1, i2, i3, i4) = vals[idx++];
1179 }
1180 }
1181 }
1182 }
1183 }
1184
1185 return 0;
1186}
1187
1188template <>
1189int TosaReference::Tensor6<int64_t>::setTensorValueInt64(const size_t bufLen, const int64_t* vals)
1190{
1191 uint32_t idx = 0;
1192
1193 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1194
1195 for (int i0 = 0; i0 < shape[0]; i0++)
1196 {
1197 for (int i1 = 0; i1 < shape[1]; i1++)
1198 {
1199 for (int i2 = 0; i2 < shape[2]; i2++)
1200 {
1201 for (int i3 = 0; i3 < shape[3]; i3++)
1202 {
1203 for (int i4 = 0; i4 < shape[4]; i4++)
1204 {
1205 for (int i5 = 0; i5 < shape[5]; i5++)
1206 {
1207 (*tensor)(i0, i1, i2, i3, i4, i5) = vals[idx++];
1208 }
1209 }
1210 }
1211 }
1212 }
1213 }
1214 return 0;
1215}
1216
1217template <class T>
1218int TosaReference::TensorTemplate<T>::setTensorValueBool(const size_t buflen, const bool* vals)
1219{
1220 FATAL_ERROR("TensorTemplate<T>::setTensorValueBool should not be called. "
1221 "Implement template specialization version.");
1222 return 0;
1223}
1224
1225template <>
1226int TosaReference::Tensor0<bool>::setTensorValueBool(const size_t bufLen, const bool* vals)
1227{
1228 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1229
1230 (*tensor)(0) = vals[0];
1231
1232 return 0;
1233}
1234
1235template <>
1236int TosaReference::Tensor1<bool>::setTensorValueBool(const size_t bufLen, const bool* vals)
1237{
1238 uint32_t idx = 0;
1239
1240 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1241
1242 for (int i0 = 0; i0 < shape[0]; i0++)
1243 {
1244 (*tensor)(i0) = vals[idx++];
1245 }
1246
1247 return 0;
1248}
1249
1250template <>
1251int TosaReference::Tensor2<bool>::setTensorValueBool(const size_t bufLen, const bool* vals)
1252{
1253 uint32_t idx = 0;
1254
1255 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1256
1257 for (int i0 = 0; i0 < shape[0]; i0++)
1258 {
1259 for (int i1 = 0; i1 < shape[1]; i1++)
1260 {
1261 (*tensor)(i0, i1) = vals[idx++];
1262 }
1263 }
1264
1265 return 0;
1266}
1267
1268template <>
1269int TosaReference::Tensor3<bool>::setTensorValueBool(const size_t bufLen, const bool* vals)
1270{
1271 uint32_t idx = 0;
1272
1273 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1274
1275 for (int i0 = 0; i0 < shape[0]; i0++)
1276 {
1277 for (int i1 = 0; i1 < shape[1]; i1++)
1278 {
1279 for (int i2 = 0; i2 < shape[2]; i2++)
1280 {
1281 (*tensor)(i0, i1, i2) = vals[idx++];
1282 }
1283 }
1284 }
1285
1286 return 0;
1287}
1288
1289template <>
1290int TosaReference::Tensor4<bool>::setTensorValueBool(const size_t bufLen, const bool* vals)
1291{
1292 uint32_t idx = 0;
1293
1294 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1295
1296 for (int i0 = 0; i0 < shape[0]; i0++)
1297 {
1298 for (int i1 = 0; i1 < shape[1]; i1++)
1299 {
1300 for (int i2 = 0; i2 < shape[2]; i2++)
1301 {
1302 for (int i3 = 0; i3 < shape[3]; i3++)
1303 {
1304 (*tensor)(i0, i1, i2, i3) = vals[idx++];
1305 }
1306 }
1307 }
1308 }
1309
1310 return 0;
1311}
1312
1313template <>
1314int TosaReference::Tensor5<bool>::setTensorValueBool(const size_t bufLen, const bool* vals)
1315{
1316 uint32_t idx = 0;
1317
1318 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1319
1320 for (int i0 = 0; i0 < shape[0]; i0++)
1321 {
1322 for (int i1 = 0; i1 < shape[1]; i1++)
1323 {
1324 for (int i2 = 0; i2 < shape[2]; i2++)
1325 {
1326 for (int i3 = 0; i3 < shape[3]; i3++)
1327 {
1328 for (int i4 = 0; i4 < shape[4]; i4++)
1329 {
1330 (*tensor)(i0, i1, i2, i3, i4) = vals[idx++];
1331 }
1332 }
1333 }
1334 }
1335 }
1336
1337 return 0;
1338}
1339
1340template <>
1341int TosaReference::Tensor6<bool>::setTensorValueBool(const size_t bufLen, const bool* vals)
1342{
1343 uint32_t idx = 0;
1344
1345 ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
1346
1347 for (int i0 = 0; i0 < shape[0]; i0++)
1348 {
1349 for (int i1 = 0; i1 < shape[1]; i1++)
1350 {
1351 for (int i2 = 0; i2 < shape[2]; i2++)
1352 {
1353 for (int i3 = 0; i3 < shape[3]; i3++)
1354 {
1355 for (int i4 = 0; i4 < shape[4]; i4++)
1356 {
1357 for (int i5 = 0; i5 < shape[5]; i5++)
1358 {
1359 (*tensor)(i0, i1, i2, i3, i4, i5) = vals[idx++];
1360 }
1361 }
1362 }
1363 }
1364 }
1365 }
1366 return 0;
1367}
1368
1369template <class T>
1370int TosaReference::TensorTemplate<T>::getTensorValueFloat(const size_t bufLen, float* vals) const
1371{
1372 FATAL_ERROR("TensorTemplate<T>::getTensorValueFloat should not be called. "
1373 "Implement template specialization version.");
1374 return 0;
1375}
1376
1377template <>
1378int TosaReference::Tensor0<float>::getTensorValueFloat(const size_t bufLen, float* vals) const
1379{
1380 int totalVals = 1;
1381
1382 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
1383
1384 vals[0] = (*tensor)(0);
1385
1386 return 0;
1387}
1388
1389template <>
1390int TosaReference::Tensor1<float>::getTensorValueFloat(const size_t bufLen, float* vals) const
1391{
1392 uint32_t idx = 0;
1393 int totalVals = 1;
1394
1395 for (size_t i = 0; i < shape.size(); i++)
1396 {
1397 totalVals *= shape[i];
1398 }
1399
1400 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
1401
1402 for (int i0 = 0; i0 < shape[0]; i0++)
1403 {
1404 vals[idx++] = (*tensor)(i0);
1405 }
1406
1407 return 0;
1408}
1409
1410template <>
1411int TosaReference::Tensor2<float>::getTensorValueFloat(const size_t bufLen, float* vals) const
1412{
1413 uint32_t idx = 0;
1414 int totalVals = 1;
1415
1416 for (size_t i = 0; i < shape.size(); i++)
1417 {
1418 totalVals *= shape[i];
1419 }
1420
1421 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
1422
1423 for (int i0 = 0; i0 < shape[0]; i0++)
1424 {
1425 for (int i1 = 0; i1 < shape[1]; i1++)
1426 {
1427 vals[idx++] = (*tensor)(i0, i1);
1428 }
1429 }
1430
1431 return 0;
1432}
1433
1434template <>
1435int TosaReference::Tensor3<float>::getTensorValueFloat(const size_t bufLen, float* vals) const
1436{
1437 uint32_t idx = 0;
1438 int totalVals = 1;
1439
1440 for (size_t i = 0; i < shape.size(); i++)
1441 {
1442 totalVals *= shape[i];
1443 }
1444
1445 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
1446
1447 for (int i0 = 0; i0 < shape[0]; i0++)
1448 {
1449 for (int i1 = 0; i1 < shape[1]; i1++)
1450 {
1451 for (int i2 = 0; i2 < shape[2]; i2++)
1452 {
1453 vals[idx++] = (*tensor)(i0, i1, i2);
1454 }
1455 }
1456 }
1457
1458 return 0;
1459}
1460
1461template <>
1462int TosaReference::Tensor4<float>::getTensorValueFloat(const size_t bufLen, float* vals) const
1463{
1464 uint32_t idx = 0;
1465 int totalVals = 1;
1466
1467 for (size_t i = 0; i < shape.size(); i++)
1468 {
1469 totalVals *= shape[i];
1470 }
1471
1472 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
1473
1474 for (int i0 = 0; i0 < shape[0]; i0++)
1475 {
1476 for (int i1 = 0; i1 < shape[1]; i1++)
1477 {
1478 for (int i2 = 0; i2 < shape[2]; i2++)
1479 {
1480 for (int i3 = 0; i3 < shape[3]; i3++)
1481 {
1482 vals[idx++] = (*tensor)(i0, i1, i2, i3);
1483 }
1484 }
1485 }
1486 }
1487
1488 return 0;
1489}
1490
1491template <>
1492int TosaReference::Tensor5<float>::getTensorValueFloat(const size_t bufLen, float* vals) const
1493{
1494 uint32_t idx = 0;
1495 int totalVals = 1;
1496
1497 for (size_t i = 0; i < shape.size(); i++)
1498 {
1499 totalVals *= shape[i];
1500 }
1501
1502 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
1503
1504 for (int i0 = 0; i0 < shape[0]; i0++)
1505 {
1506 for (int i1 = 0; i1 < shape[1]; i1++)
1507 {
1508 for (int i2 = 0; i2 < shape[2]; i2++)
1509 {
1510 for (int i3 = 0; i3 < shape[3]; i3++)
1511 {
1512 for (int i4 = 0; i4 < shape[4]; i4++)
1513 {
1514 vals[idx++] = (*tensor)(i0, i1, i2, i3, i4);
1515 }
1516 }
1517 }
1518 }
1519 }
1520
1521 return 0;
1522}
1523
1524template <>
1525int TosaReference::Tensor6<float>::getTensorValueFloat(const size_t bufLen, float* vals) const
1526{
1527 uint32_t idx = 0;
1528 int totalVals = 1;
1529
1530 for (size_t i = 0; i < shape.size(); i++)
1531 {
1532 totalVals *= shape[i];
1533 }
1534
1535 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
1536
1537 for (int i0 = 0; i0 < shape[0]; i0++)
1538 {
1539 for (int i1 = 0; i1 < shape[1]; i1++)
1540 {
1541 for (int i2 = 0; i2 < shape[2]; i2++)
1542 {
1543 for (int i3 = 0; i3 < shape[3]; i3++)
1544 {
1545 for (int i4 = 0; i4 < shape[4]; i4++)
1546 {
1547 for (int i5 = 0; i5 < shape[5]; i5++)
1548 {
1549 vals[idx++] = (*tensor)(i0, i1, i2, i3, i4, i5);
1550 }
1551 }
1552 }
1553 }
1554 }
1555 }
1556 return 0;
1557}
1558
1559template <class T>
1560int TosaReference::TensorTemplate<T>::getTensorValueInt32(const size_t bufLen, int32_t* vals) const
1561{
1562 FATAL_ERROR("TensorTemplate<T>::getTensorValueInt32 should not be called. "
1563 "Implement template specialization version.");
1564 return 0;
1565}
1566
1567template <>
1568int TosaReference::Tensor0<int32_t>::getTensorValueInt32(const size_t bufLen, int32_t* vals) const
1569{
1570 int totalVals = 1;
1571
1572 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
1573
1574 vals[0] = (*tensor)(0);
1575
1576 return 0;
1577}
1578
1579template <>
1580int TosaReference::Tensor1<int32_t>::getTensorValueInt32(const size_t bufLen, int32_t* vals) const
1581{
1582 uint32_t idx = 0;
1583 int totalVals = 1;
1584
1585 for (size_t i = 0; i < shape.size(); i++)
1586 {
1587 totalVals *= shape[i];
1588 }
1589
1590 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
1591
1592 for (int i0 = 0; i0 < shape[0]; i0++)
1593 {
1594 vals[idx++] = (*tensor)(i0);
1595 }
1596
1597 return 0;
1598}
1599
1600template <>
1601int TosaReference::Tensor2<int32_t>::getTensorValueInt32(const size_t bufLen, int32_t* vals) const
1602{
1603 uint32_t idx = 0;
1604 int totalVals = 1;
1605
1606 for (size_t i = 0; i < shape.size(); i++)
1607 {
1608 totalVals *= shape[i];
1609 }
1610
1611 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
1612
1613 for (int i0 = 0; i0 < shape[0]; i0++)
1614 {
1615 for (int i1 = 0; i1 < shape[1]; i1++)
1616 {
1617 vals[idx++] = (*tensor)(i0, i1);
1618 }
1619 }
1620
1621 return 0;
1622}
1623
1624template <>
1625int TosaReference::Tensor3<int32_t>::getTensorValueInt32(const size_t bufLen, int32_t* vals) const
1626{
1627 uint32_t idx = 0;
1628 int totalVals = 1;
1629
1630 for (size_t i = 0; i < shape.size(); i++)
1631 {
1632 totalVals *= shape[i];
1633 }
1634
1635 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not 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 vals[idx++] = (*tensor)(i0, i1, i2);
1644 }
1645 }
1646 }
1647
1648 return 0;
1649}
1650
1651template <>
1652int TosaReference::Tensor4<int32_t>::getTensorValueInt32(const size_t bufLen, int32_t* vals) const
1653{
1654 uint32_t idx = 0;
1655 int totalVals = 1;
1656
1657 for (size_t i = 0; i < shape.size(); i++)
1658 {
1659 totalVals *= shape[i];
1660 }
1661
1662 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
1663
1664 for (int i0 = 0; i0 < shape[0]; i0++)
1665 {
1666 for (int i1 = 0; i1 < shape[1]; i1++)
1667 {
1668 for (int i2 = 0; i2 < shape[2]; i2++)
1669 {
1670 for (int i3 = 0; i3 < shape[3]; i3++)
1671 {
1672 vals[idx++] = (*tensor)(i0, i1, i2, i3);
1673 }
1674 }
1675 }
1676 }
1677
1678 return 0;
1679}
1680
1681template <>
1682int TosaReference::Tensor5<int32_t>::getTensorValueInt32(const size_t bufLen, int32_t* vals) const
1683{
1684 uint32_t idx = 0;
1685 int totalVals = 1;
1686
1687 for (size_t i = 0; i < shape.size(); i++)
1688 {
1689 totalVals *= shape[i];
1690 }
1691
1692 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
1693
1694 for (int i0 = 0; i0 < shape[0]; i0++)
1695 {
1696 for (int i1 = 0; i1 < shape[1]; i1++)
1697 {
1698 for (int i2 = 0; i2 < shape[2]; i2++)
1699 {
1700 for (int i3 = 0; i3 < shape[3]; i3++)
1701 {
1702 for (int i4 = 0; i4 < shape[4]; i4++)
1703 {
1704 vals[idx++] = (*tensor)(i0, i1, i2, i3, i4);
1705 }
1706 }
1707 }
1708 }
1709 }
1710
1711 return 0;
1712}
1713
1714template <>
1715int TosaReference::Tensor6<int32_t>::getTensorValueInt32(const size_t bufLen, int32_t* vals) const
1716{
1717 uint32_t idx = 0;
1718 int totalVals = 1;
1719
1720 for (size_t i = 0; i < shape.size(); i++)
1721 {
1722 totalVals *= shape[i];
1723 }
1724
1725 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
1726
1727 for (int i0 = 0; i0 < shape[0]; i0++)
1728 {
1729 for (int i1 = 0; i1 < shape[1]; i1++)
1730 {
1731 for (int i2 = 0; i2 < shape[2]; i2++)
1732 {
1733 for (int i3 = 0; i3 < shape[3]; i3++)
1734 {
1735 for (int i4 = 0; i4 < shape[4]; i4++)
1736 {
1737 for (int i5 = 0; i5 < shape[5]; i5++)
1738 {
1739 vals[idx++] = (*tensor)(i0, i1, i2, i3, i4, i5);
1740 }
1741 }
1742 }
1743 }
1744 }
1745 }
1746 return 0;
1747}
1748
1749template <class T>
1750int TosaReference::TensorTemplate<T>::getTensorValueInt64(const size_t bufLen, int64_t* vals) const
1751{
1752 FATAL_ERROR("TensorTemplate<T>::getTensorValueInt64 should not be called. "
1753 "Implement template specialization version.");
1754 return 0;
1755}
1756
1757template <>
1758int TosaReference::Tensor0<int64_t>::getTensorValueInt64(const size_t bufLen, int64_t* vals) const
1759{
1760 int totalVals = 1;
1761
1762 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
1763
1764 vals[0] = (*tensor)(0);
1765
1766 return 0;
1767}
1768
1769template <>
1770int TosaReference::Tensor1<int64_t>::getTensorValueInt64(const size_t bufLen, int64_t* vals) const
1771{
1772 uint32_t idx = 0;
1773 int totalVals = 1;
1774
1775 for (size_t i = 0; i < shape.size(); i++)
1776 {
1777 totalVals *= shape[i];
1778 }
1779
1780 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
1781
1782 for (int i0 = 0; i0 < shape[0]; i0++)
1783 {
1784 vals[idx++] = (*tensor)(i0);
1785 }
1786
1787 return 0;
1788}
1789
1790template <>
1791int TosaReference::Tensor2<int64_t>::getTensorValueInt64(const size_t bufLen, int64_t* vals) const
1792{
1793 uint32_t idx = 0;
1794 int totalVals = 1;
1795
1796 for (size_t i = 0; i < shape.size(); i++)
1797 {
1798 totalVals *= shape[i];
1799 }
1800
1801 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
1802
1803 for (int i0 = 0; i0 < shape[0]; i0++)
1804 {
1805 for (int i1 = 0; i1 < shape[1]; i1++)
1806 {
1807 vals[idx++] = (*tensor)(i0, i1);
1808 }
1809 }
1810
1811 return 0;
1812}
1813
1814template <>
1815int TosaReference::Tensor3<int64_t>::getTensorValueInt64(const size_t bufLen, int64_t* vals) const
1816{
1817 uint32_t idx = 0;
1818 int totalVals = 1;
1819
1820 for (size_t i = 0; i < shape.size(); i++)
1821 {
1822 totalVals *= shape[i];
1823 }
1824
1825 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
1826
1827 for (int i0 = 0; i0 < shape[0]; i0++)
1828 {
1829 for (int i1 = 0; i1 < shape[1]; i1++)
1830 {
1831 for (int i2 = 0; i2 < shape[2]; i2++)
1832 {
1833 vals[idx++] = (*tensor)(i0, i1, i2);
1834 }
1835 }
1836 }
1837
1838 return 0;
1839}
1840
1841template <>
1842int TosaReference::Tensor4<int64_t>::getTensorValueInt64(const size_t bufLen, int64_t* vals) const
1843{
1844 uint32_t idx = 0;
1845 int totalVals = 1;
1846
1847 for (size_t i = 0; i < shape.size(); i++)
1848 {
1849 totalVals *= shape[i];
1850 }
1851
1852 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
1853
1854 for (int i0 = 0; i0 < shape[0]; i0++)
1855 {
1856 for (int i1 = 0; i1 < shape[1]; i1++)
1857 {
1858 for (int i2 = 0; i2 < shape[2]; i2++)
1859 {
1860 for (int i3 = 0; i3 < shape[3]; i3++)
1861 {
1862 vals[idx++] = (*tensor)(i0, i1, i2, i3);
1863 }
1864 }
1865 }
1866 }
1867
1868 return 0;
1869}
1870
1871template <>
1872int TosaReference::Tensor5<int64_t>::getTensorValueInt64(const size_t bufLen, int64_t* vals) const
1873{
1874 uint32_t idx = 0;
1875 int totalVals = 1;
1876
1877 for (size_t i = 0; i < shape.size(); i++)
1878 {
1879 totalVals *= shape[i];
1880 }
1881
1882 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
1883
1884 for (int i0 = 0; i0 < shape[0]; i0++)
1885 {
1886 for (int i1 = 0; i1 < shape[1]; i1++)
1887 {
1888 for (int i2 = 0; i2 < shape[2]; i2++)
1889 {
1890 for (int i3 = 0; i3 < shape[3]; i3++)
1891 {
1892 for (int i4 = 0; i4 < shape[4]; i4++)
1893 {
1894 vals[idx++] = (*tensor)(i0, i1, i2, i3, i4);
1895 }
1896 }
1897 }
1898 }
1899 }
1900
1901 return 0;
1902}
1903
1904template <>
1905int TosaReference::Tensor6<int64_t>::getTensorValueInt64(const size_t bufLen, int64_t* vals) const
1906{
1907 uint32_t idx = 0;
1908 int totalVals = 1;
1909
1910 for (size_t i = 0; i < shape.size(); i++)
1911 {
1912 totalVals *= shape[i];
1913 }
1914
1915 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
1916
1917 for (int i0 = 0; i0 < shape[0]; i0++)
1918 {
1919 for (int i1 = 0; i1 < shape[1]; i1++)
1920 {
1921 for (int i2 = 0; i2 < shape[2]; i2++)
1922 {
1923 for (int i3 = 0; i3 < shape[3]; i3++)
1924 {
1925 for (int i4 = 0; i4 < shape[4]; i4++)
1926 {
1927 for (int i5 = 0; i5 < shape[5]; i5++)
1928 {
1929 vals[idx++] = (*tensor)(i0, i1, i2, i3, i4, i5);
1930 }
1931 }
1932 }
1933 }
1934 }
1935 }
1936 return 0;
1937}
1938
1939template <class T>
1940int TosaReference::TensorTemplate<T>::getTensorValueBool(const size_t bufLen, bool* vals) const
1941{
1942 FATAL_ERROR("TensorTemplate<T>::getTensorValueBool should not be called. "
1943 "Implement template specialization version.");
1944 return 0;
1945}
1946
1947template <>
1948int TosaReference::Tensor0<bool>::getTensorValueBool(const size_t bufLen, bool* vals) const
1949{
1950 int totalVals = 1;
1951
1952 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
1953
1954 vals[0] = (*tensor)(0);
1955
1956 return 0;
1957}
1958
1959template <>
1960int TosaReference::Tensor1<bool>::getTensorValueBool(const size_t bufLen, bool* vals) const
1961{
1962 uint32_t idx = 0;
1963 int totalVals = 1;
1964
1965 for (size_t i = 0; i < shape.size(); i++)
1966 {
1967 totalVals *= shape[i];
1968 }
1969
1970 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
1971
1972 for (int i0 = 0; i0 < shape[0]; i0++)
1973 {
1974 vals[idx++] = (*tensor)(i0);
1975 }
1976
1977 return 0;
1978}
1979
1980template <>
1981int TosaReference::Tensor2<bool>::getTensorValueBool(const size_t bufLen, bool* vals) const
1982{
1983 uint32_t idx = 0;
1984 int totalVals = 1;
1985
1986 for (size_t i = 0; i < shape.size(); i++)
1987 {
1988 totalVals *= shape[i];
1989 }
1990
1991 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
1992
1993 for (int i0 = 0; i0 < shape[0]; i0++)
1994 {
1995 for (int i1 = 0; i1 < shape[1]; i1++)
1996 {
1997 vals[idx++] = (*tensor)(i0, i1);
1998 }
1999 }
2000
2001 return 0;
2002}
2003
2004template <>
2005int TosaReference::Tensor3<bool>::getTensorValueBool(const size_t bufLen, bool* vals) const
2006{
2007 uint32_t idx = 0;
2008 int totalVals = 1;
2009
2010 for (size_t i = 0; i < shape.size(); i++)
2011 {
2012 totalVals *= shape[i];
2013 }
2014
2015 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
2016
2017 for (int i0 = 0; i0 < shape[0]; i0++)
2018 {
2019 for (int i1 = 0; i1 < shape[1]; i1++)
2020 {
2021 for (int i2 = 0; i2 < shape[2]; i2++)
2022 {
2023 vals[idx++] = (*tensor)(i0, i1, i2);
2024 }
2025 }
2026 }
2027
2028 return 0;
2029}
2030
2031template <>
2032int TosaReference::Tensor4<bool>::getTensorValueBool(const size_t bufLen, bool* vals) const
2033{
2034 uint32_t idx = 0;
2035 int totalVals = 1;
2036
2037 for (size_t i = 0; i < shape.size(); i++)
2038 {
2039 totalVals *= shape[i];
2040 }
2041
2042 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
2043
2044 for (int i0 = 0; i0 < shape[0]; i0++)
2045 {
2046 for (int i1 = 0; i1 < shape[1]; i1++)
2047 {
2048 for (int i2 = 0; i2 < shape[2]; i2++)
2049 {
2050 for (int i3 = 0; i3 < shape[3]; i3++)
2051 {
2052 vals[idx++] = (*tensor)(i0, i1, i2, i3);
2053 }
2054 }
2055 }
2056 }
2057
2058 return 0;
2059}
2060
2061template <>
2062int TosaReference::Tensor5<bool>::getTensorValueBool(const size_t bufLen, bool* vals) const
2063{
2064 uint32_t idx = 0;
2065 int totalVals = 1;
2066
2067 for (size_t i = 0; i < shape.size(); i++)
2068 {
2069 totalVals *= shape[i];
2070 }
2071
2072 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
2073
2074 for (int i0 = 0; i0 < shape[0]; i0++)
2075 {
2076 for (int i1 = 0; i1 < shape[1]; i1++)
2077 {
2078 for (int i2 = 0; i2 < shape[2]; i2++)
2079 {
2080 for (int i3 = 0; i3 < shape[3]; i3++)
2081 {
2082 for (int i4 = 0; i4 < shape[4]; i4++)
2083 {
2084 vals[idx++] = (*tensor)(i0, i1, i2, i3, i4);
2085 }
2086 }
2087 }
2088 }
2089 }
2090
2091 return 0;
2092}
2093
2094template <>
2095int TosaReference::Tensor6<bool>::getTensorValueBool(const size_t bufLen, bool* vals) const
2096{
2097 uint32_t idx = 0;
2098 int totalVals = 1;
2099
2100 for (size_t i = 0; i < shape.size(); i++)
2101 {
2102 totalVals *= shape[i];
2103 }
2104
2105 ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match");
2106
2107 for (int i0 = 0; i0 < shape[0]; i0++)
2108 {
2109 for (int i1 = 0; i1 < shape[1]; i1++)
2110 {
2111 for (int i2 = 0; i2 < shape[2]; i2++)
2112 {
2113 for (int i3 = 0; i3 < shape[3]; i3++)
2114 {
2115 for (int i4 = 0; i4 < shape[4]; i4++)
2116 {
2117 for (int i5 = 0; i5 < shape[5]; i5++)
2118 {
2119 vals[idx++] = (*tensor)(i0, i1, i2, i3, i4, i5);
2120 }
2121 }
2122 }
2123 }
2124 }
2125 }
2126 return 0;
2127}
2128
2129template <>
2130int TosaReference::Tensor0<float>::allocate()
2131{
2132 ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor");
2133 tensor = new ETensor0<float>();
2134
2135 if (tensor)
2136 return 0;
2137 else
2138 return 1;
2139}
2140template <>
2141int TosaReference::Tensor1<float>::allocate()
2142{
2143 ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor");
2144 tensor = new ETensor1<float>(shape[0]);
2145 if (tensor)
2146 return 0;
2147 else
2148 return 1;
2149}
2150template <>
2151int TosaReference::Tensor2<float>::allocate()
2152{
2153 ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor");
2154 tensor = new ETensor2<float>(shape[0], shape[1]);
2155 if (tensor)
2156 return 0;
2157 else
2158 return 1;
2159}
2160
2161template <>
2162int TosaReference::Tensor3<float>::allocate()
2163{
2164 ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor");
2165 tensor = new ETensor3<float>(shape[0], shape[1], shape[2]);
2166 if (tensor)
2167 return 0;
2168 else
2169 return 1;
2170}
2171
2172template <>
2173int TosaReference::Tensor4<float>::allocate()
2174{
2175 ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor");
2176 tensor = new ETensor4<float>(shape[0], shape[1], shape[2], shape[3]);
2177 if (tensor)
2178 return 0;
2179 else
2180 return 1;
2181}
2182
2183template <>
2184int TosaReference::Tensor5<float>::allocate()
2185{
2186 ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor");
2187 tensor = new ETensor5<float>(shape[0], shape[1], shape[2], shape[3], shape[4]);
2188 if (tensor)
2189 return 0;
2190 else
2191 return 1;
2192}
2193
2194template <>
2195int TosaReference::Tensor6<float>::allocate()
2196{
2197 ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor");
2198 tensor = new ETensor6<float>(shape[0], shape[1], shape[2], shape[3], shape[4], shape[5]);
2199 if (tensor)
2200 return 0;
2201 else
2202 return 1;
2203}
2204
2205template <>
2206int TosaReference::Tensor0<int32_t>::allocate()
2207{
2208 ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor");
2209 tensor = new ETensor0<int32_t>();
2210 if (tensor)
2211 return 0;
2212 else
2213 return 1;
2214}
2215template <>
2216int TosaReference::Tensor1<int32_t>::allocate()
2217{
2218 ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor");
2219 tensor = new ETensor1<int32_t>(shape[0]);
2220 if (tensor)
2221 return 0;
2222 else
2223 return 1;
2224}
2225template <>
2226int TosaReference::Tensor2<int32_t>::allocate()
2227{
2228 ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor");
2229 tensor = new ETensor2<int32_t>(shape[0], shape[1]);
2230 if (tensor)
2231 return 0;
2232 else
2233 return 1;
2234}
2235
2236template <>
2237int TosaReference::Tensor3<int32_t>::allocate()
2238{
2239 ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor");
2240 tensor = new ETensor3<int32_t>(shape[0], shape[1], shape[2]);
2241 if (tensor)
2242 return 0;
2243 else
2244 return 1;
2245}
2246
2247template <>
2248int TosaReference::Tensor4<int32_t>::allocate()
2249{
2250 ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor");
2251 tensor = new ETensor4<int32_t>(shape[0], shape[1], shape[2], shape[3]);
2252 if (tensor)
2253 return 0;
2254 else
2255 return 1;
2256}
2257
2258template <>
2259int TosaReference::Tensor5<int32_t>::allocate()
2260{
2261 ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor");
2262 tensor = new ETensor5<int32_t>(shape[0], shape[1], shape[2], shape[3], shape[4]);
2263 if (tensor)
2264 return 0;
2265 else
2266 return 1;
2267}
2268
2269template <>
2270int TosaReference::Tensor6<int32_t>::allocate()
2271{
2272 ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor");
2273 tensor = new ETensor6<int32_t>(shape[0], shape[1], shape[2], shape[3], shape[4], shape[5]);
2274 if (tensor)
2275 return 0;
2276 else
2277 return 1;
2278}
2279
2280template <>
2281int TosaReference::Tensor0<int64_t>::allocate()
2282{
2283 ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor");
2284 tensor = new ETensor0<int64_t>();
2285 if (tensor)
2286 return 0;
2287 else
2288 return 1;
2289}
2290template <>
2291int TosaReference::Tensor1<int64_t>::allocate()
2292{
2293 ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor");
2294 tensor = new ETensor1<int64_t>(shape[0]);
2295 if (tensor)
2296 return 0;
2297 else
2298 return 1;
2299}
2300template <>
2301int TosaReference::Tensor2<int64_t>::allocate()
2302{
2303 ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor");
2304 tensor = new ETensor2<int64_t>(shape[0], shape[1]);
2305 if (tensor)
2306 return 0;
2307 else
2308 return 1;
2309}
2310
2311template <>
2312int TosaReference::Tensor3<int64_t>::allocate()
2313{
2314 ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor");
2315 tensor = new ETensor3<int64_t>(shape[0], shape[1], shape[2]);
2316 if (tensor)
2317 return 0;
2318 else
2319 return 1;
2320}
2321
2322template <>
2323int TosaReference::Tensor4<int64_t>::allocate()
2324{
2325 ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor");
2326 tensor = new ETensor4<int64_t>(shape[0], shape[1], shape[2], shape[3]);
2327 if (tensor)
2328 return 0;
2329 else
2330 return 1;
2331}
2332
2333template <>
2334int TosaReference::Tensor5<int64_t>::allocate()
2335{
2336 ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor");
2337 tensor = new ETensor5<int64_t>(shape[0], shape[1], shape[2], shape[3], shape[4]);
2338 if (tensor)
2339 return 0;
2340 else
2341 return 1;
2342}
2343
2344template <>
2345int TosaReference::Tensor6<int64_t>::allocate()
2346{
2347 ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor");
2348 tensor = new ETensor6<int64_t>(shape[0], shape[1], shape[2], shape[3], shape[4], shape[5]);
2349 if (tensor)
2350 return 0;
2351 else
2352 return 1;
2353}
2354
2355template <>
2356int TosaReference::Tensor0<bool>::allocate()
2357{
2358 ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor");
2359 tensor = new ETensor0<bool>();
2360 if (tensor)
2361 return 0;
2362 else
2363 return 1;
2364}
2365template <>
2366int TosaReference::Tensor1<bool>::allocate()
2367{
2368 ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor");
2369 tensor = new ETensor1<bool>(shape[0]);
2370 if (tensor)
2371 return 0;
2372 else
2373 return 1;
2374}
2375template <>
2376int TosaReference::Tensor2<bool>::allocate()
2377{
2378 ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor");
2379 tensor = new ETensor2<bool>(shape[0], shape[1]);
2380 if (tensor)
2381 return 0;
2382 else
2383 return 1;
2384}
2385
2386template <>
2387int TosaReference::Tensor3<bool>::allocate()
2388{
2389 ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor");
2390 tensor = new ETensor3<bool>(shape[0], shape[1], shape[2]);
2391 if (tensor)
2392 return 0;
2393 else
2394 return 1;
2395}
2396
2397template <>
2398int TosaReference::Tensor4<bool>::allocate()
2399{
2400 ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor");
2401 tensor = new ETensor4<bool>(shape[0], shape[1], shape[2], shape[3]);
2402 if (tensor)
2403 return 0;
2404 else
2405 return 1;
2406}
2407
2408template <>
2409int TosaReference::Tensor5<bool>::allocate()
2410{
2411 ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor");
2412 tensor = new ETensor5<bool>(shape[0], shape[1], shape[2], shape[3], shape[4]);
2413 if (tensor)
2414 return 0;
2415 else
2416 return 1;
2417}
2418
2419template <>
2420int TosaReference::Tensor6<bool>::allocate()
2421{
2422 ASSERT_MSG(tensor == nullptr, "Error: double allocate Eigen tensor");
2423 tensor = new ETensor6<bool>(shape[0], shape[1], shape[2], shape[3], shape[4], shape[5]);
2424 if (tensor)
2425 return 0;
2426 else
2427 return 1;
2428}
2429
2430template <>
2431int TosaReference::Tensor0<float>::dumpTensor(FILE* out) const
2432{
Eric Kunze286f8342022-06-22 11:30:23 -07002433 char fp_fmt[32];
2434 snprintf(fp_fmt, sizeof(fp_fmt), "[ %%%sf ]\n", g_func_config.fp_format.c_str());
Eric Kunzee5e26762020-10-13 16:11:07 -07002435
2436 if (tensor == nullptr)
2437 {
2438 fprintf(out, "<Not allocated>\n");
2439 return 0;
2440 }
2441
2442 fprintf(out, fp_fmt, (*tensor)(0));
2443
2444 return 0;
2445}
2446
2447template <>
2448int TosaReference::Tensor1<float>::dumpTensor(FILE* out) const
2449{
Eric Kunze286f8342022-06-22 11:30:23 -07002450 char fp_fmt[32];
2451 snprintf(fp_fmt, sizeof(fp_fmt), " %%%sf ", g_func_config.fp_format.c_str());
Eric Kunzee5e26762020-10-13 16:11:07 -07002452
2453 if (tensor == nullptr)
2454 {
2455 fprintf(out, "<Not allocated>\n");
2456 return 0;
2457 }
2458
2459 fprintf(out, "[");
2460 for (int i0 = 0; i0 < shape[0]; i0++)
2461 {
2462 fprintf(out, fp_fmt, (*tensor)(i0));
2463 }
2464 fprintf(out, "]\n");
2465
2466 return 0;
2467}
2468
2469template <>
2470int TosaReference::Tensor2<float>::dumpTensor(FILE* out) const
2471{
Eric Kunze286f8342022-06-22 11:30:23 -07002472 char fp_fmt[32];
2473 snprintf(fp_fmt, sizeof(fp_fmt), " %%%sf ", g_func_config.fp_format.c_str());
Eric Kunzee5e26762020-10-13 16:11:07 -07002474
2475 if (tensor == nullptr)
2476 {
2477 fprintf(out, "<Not allocated>\n");
2478 return 0;
2479 }
2480
2481 fprintf(out, "[");
2482 for (int i0 = 0; i0 < shape[0]; i0++)
2483 {
2484 fprintf(out, "[");
2485 for (int i1 = 0; i1 < shape[1]; i1++)
2486 {
2487 fprintf(out, fp_fmt, (*tensor)(i0, i1));
2488 }
2489 fprintf(out, "]\n");
2490 }
2491 fprintf(out, "]\n");
2492
2493 return 0;
2494}
2495
2496template <>
2497int TosaReference::Tensor3<float>::dumpTensor(FILE* out) const
2498{
Eric Kunze286f8342022-06-22 11:30:23 -07002499 char fp_fmt[32];
2500 snprintf(fp_fmt, sizeof(fp_fmt), " %%%sf ", g_func_config.fp_format.c_str());
Eric Kunzee5e26762020-10-13 16:11:07 -07002501
2502 if (tensor == nullptr)
2503 {
2504 fprintf(out, "<Not allocated>\n");
2505 return 0;
2506 }
2507
2508 fprintf(out, "[");
2509 for (int i0 = 0; i0 < shape[0]; i0++)
2510 {
2511 fprintf(out, "[");
2512 for (int i1 = 0; i1 < shape[1]; i1++)
2513 {
2514 fprintf(out, "[");
2515 for (int i2 = 0; i2 < shape[2]; i2++)
2516 {
2517 fprintf(out, fp_fmt, (*tensor)(i0, i1, i2));
2518 }
2519 fprintf(out, "]\n");
2520 }
2521 fprintf(out, "]\n");
2522 }
2523 fprintf(out, "]\n");
2524
2525 return 0;
2526}
2527
2528template <>
2529int TosaReference::Tensor4<float>::dumpTensor(FILE* out) const
2530{
Eric Kunze286f8342022-06-22 11:30:23 -07002531 char fp_fmt[32];
2532 snprintf(fp_fmt, sizeof(fp_fmt), " %%%sf ", g_func_config.fp_format.c_str());
Eric Kunzee5e26762020-10-13 16:11:07 -07002533
2534 if (tensor == nullptr)
2535 {
2536 fprintf(out, "<Not allocated>\n");
2537 return 0;
2538 }
2539
2540 fprintf(out, "[");
2541 for (int i0 = 0; i0 < shape[0]; i0++)
2542 {
2543 fprintf(out, "[");
2544 for (int i1 = 0; i1 < shape[1]; i1++)
2545 {
2546 fprintf(out, "[");
2547 for (int i2 = 0; i2 < shape[2]; i2++)
2548 {
2549 fprintf(out, "[");
2550 for (int i3 = 0; i3 < shape[3]; i3++)
2551 {
2552 fprintf(out, fp_fmt, (*tensor)(i0, i1, i2, i3));
2553 }
2554 fprintf(out, "]\n");
2555 }
2556 fprintf(out, "]\n");
2557 }
2558 fprintf(out, "]\n");
2559 }
2560 fprintf(out, "]\n");
2561
2562 return 0;
2563}
2564
2565template <>
2566int TosaReference::Tensor5<float>::dumpTensor(FILE* out) const
2567{
Eric Kunze286f8342022-06-22 11:30:23 -07002568 char fp_fmt[32];
2569 snprintf(fp_fmt, sizeof(fp_fmt), " %%%sf ", g_func_config.fp_format.c_str());
Eric Kunzee5e26762020-10-13 16:11:07 -07002570
2571 if (tensor == nullptr)
2572 {
2573 fprintf(out, "<Not allocated>\n");
2574 return 0;
2575 }
2576
2577 fprintf(out, "[");
2578 for (int i0 = 0; i0 < shape[0]; i0++)
2579 {
2580 fprintf(out, "[");
2581 for (int i1 = 0; i1 < shape[1]; i1++)
2582 {
2583 fprintf(out, "[");
2584 for (int i2 = 0; i2 < shape[2]; i2++)
2585 {
2586 fprintf(out, "[");
2587 for (int i3 = 0; i3 < shape[3]; i3++)
2588 {
2589 fprintf(out, "[");
2590 for (int i4 = 0; i4 < shape[4]; i4++)
2591 {
2592 fprintf(out, fp_fmt, (*tensor)(i0, i1, i2, i3, i4));
2593 }
2594 fprintf(out, "]\n");
2595 }
2596 fprintf(out, "]\n");
2597 }
2598 fprintf(out, "]\n");
2599 }
2600 fprintf(out, "]\n");
2601 }
2602 fprintf(out, "]\n");
2603
2604 return 0;
2605}
2606
2607template <>
2608int TosaReference::Tensor6<float>::dumpTensor(FILE* out) const
2609{
Eric Kunze286f8342022-06-22 11:30:23 -07002610 char fp_fmt[32];
2611 snprintf(fp_fmt, sizeof(fp_fmt), " %%%sf ", g_func_config.fp_format.c_str());
Eric Kunzee5e26762020-10-13 16:11:07 -07002612
2613 if (tensor == nullptr)
2614 {
2615 fprintf(out, "<Not allocated>\n");
2616 return 0;
2617 }
2618
2619 fprintf(out, "[");
2620 for (int i0 = 0; i0 < shape[0]; i0++)
2621 {
2622 fprintf(out, "[");
2623 for (int i1 = 0; i1 < shape[1]; i1++)
2624 {
2625 fprintf(out, "[");
2626 for (int i2 = 0; i2 < shape[2]; i2++)
2627 {
2628 fprintf(out, "[");
2629 for (int i3 = 0; i3 < shape[3]; i3++)
2630 {
2631 fprintf(out, "[");
2632 for (int i4 = 0; i4 < shape[4]; i4++)
2633 {
2634 fprintf(out, "[");
2635 for (int i5 = 0; i5 < shape[5]; i5++)
2636 {
2637 fprintf(out, fp_fmt, (*tensor)(i0, i1, i2, i3, i4, i5));
2638 }
2639 fprintf(out, "]\n");
2640 }
2641 fprintf(out, "]\n");
2642 }
2643 fprintf(out, "]\n");
2644 }
2645 fprintf(out, "]\n");
2646 }
2647 fprintf(out, "]\n");
2648 }
2649 fprintf(out, "]\n");
2650
2651 return 0;
2652}
2653
2654template <>
2655int TosaReference::Tensor0<int64_t>::dumpTensor(FILE* out) const
2656{
Eric Kunze286f8342022-06-22 11:30:23 -07002657 char i64_fmt[32];
2658 snprintf(i64_fmt, sizeof(i64_fmt), "[ %%ld ]\n");
Eric Kunzee5e26762020-10-13 16:11:07 -07002659
2660 if (tensor == nullptr)
2661 {
2662 fprintf(out, "<Not allocated>\n");
2663 return 0;
2664 }
2665
2666 fprintf(out, i64_fmt, (*tensor)(0));
2667
2668 return 0;
2669}
2670
2671template <>
2672int TosaReference::Tensor1<int64_t>::dumpTensor(FILE* out) const
2673{
Eric Kunze286f8342022-06-22 11:30:23 -07002674 char i64_fmt[32];
2675 snprintf(i64_fmt, sizeof(i64_fmt), " %%ld ");
Eric Kunzee5e26762020-10-13 16:11:07 -07002676
2677 if (tensor == nullptr)
2678 {
2679 fprintf(out, "<Not allocated>\n");
2680 return 0;
2681 }
2682
2683 fprintf(out, "[");
2684 for (int i0 = 0; i0 < shape[0]; i0++)
2685 {
2686 fprintf(out, i64_fmt, (*tensor)(i0));
2687 }
2688 fprintf(out, "]\n");
2689
2690 return 0;
2691}
2692
2693template <>
2694int TosaReference::Tensor2<int64_t>::dumpTensor(FILE* out) const
2695{
Eric Kunze286f8342022-06-22 11:30:23 -07002696 char i64_fmt[32];
2697 snprintf(i64_fmt, sizeof(i64_fmt), " %%ld ");
Eric Kunzee5e26762020-10-13 16:11:07 -07002698
2699 if (tensor == nullptr)
2700 {
2701 fprintf(out, "<Not allocated>\n");
2702 return 0;
2703 }
2704
2705 fprintf(out, "[");
2706 for (int i0 = 0; i0 < shape[0]; i0++)
2707 {
2708 fprintf(out, "[");
2709 for (int i1 = 0; i1 < shape[1]; i1++)
2710 {
2711 fprintf(out, i64_fmt, (*tensor)(i0, i1));
2712 }
2713 fprintf(out, "]\n");
2714 }
2715 fprintf(out, "]\n");
2716
2717 return 0;
2718}
2719
2720template <>
2721int TosaReference::Tensor3<int64_t>::dumpTensor(FILE* out) const
2722{
Eric Kunze286f8342022-06-22 11:30:23 -07002723 char i64_fmt[32];
2724 snprintf(i64_fmt, sizeof(i64_fmt), " %%ld ");
Eric Kunzee5e26762020-10-13 16:11:07 -07002725
2726 if (tensor == nullptr)
2727 {
2728 fprintf(out, "<Not allocated>\n");
2729 return 0;
2730 }
2731
2732 fprintf(out, "[");
2733 for (int i0 = 0; i0 < shape[0]; i0++)
2734 {
2735 fprintf(out, "[");
2736 for (int i1 = 0; i1 < shape[1]; i1++)
2737 {
2738 fprintf(out, "[");
2739 for (int i2 = 0; i2 < shape[2]; i2++)
2740 {
2741 fprintf(out, i64_fmt, (*tensor)(i0, i1, i2));
2742 }
2743 fprintf(out, "]\n");
2744 }
2745 fprintf(out, "]\n");
2746 }
2747 fprintf(out, "]\n");
2748
2749 return 0;
2750}
2751
2752template <>
2753int TosaReference::Tensor4<int64_t>::dumpTensor(FILE* out) const
2754{
Eric Kunze286f8342022-06-22 11:30:23 -07002755 char i64_fmt[32];
2756 snprintf(i64_fmt, sizeof(i64_fmt), " %%ld ");
Eric Kunzee5e26762020-10-13 16:11:07 -07002757
2758 if (tensor == nullptr)
2759 {
2760 fprintf(out, "<Not allocated>\n");
2761 return 0;
2762 }
2763
2764 fprintf(out, "[");
2765 for (int i0 = 0; i0 < shape[0]; i0++)
2766 {
2767 fprintf(out, "[");
2768 for (int i1 = 0; i1 < shape[1]; i1++)
2769 {
2770 fprintf(out, "[");
2771 for (int i2 = 0; i2 < shape[2]; i2++)
2772 {
2773 fprintf(out, "[");
2774 for (int i3 = 0; i3 < shape[3]; i3++)
2775 {
2776 fprintf(out, i64_fmt, (*tensor)(i0, i1, i2, i3));
2777 }
2778 fprintf(out, "]\n");
2779 }
2780 fprintf(out, "]\n");
2781 }
2782 fprintf(out, "]\n");
2783 }
2784 fprintf(out, "]\n");
2785
2786 return 0;
2787}
2788
2789template <>
2790int TosaReference::Tensor5<int64_t>::dumpTensor(FILE* out) const
2791{
Eric Kunze286f8342022-06-22 11:30:23 -07002792 char i64_fmt[32];
2793 snprintf(i64_fmt, sizeof(i64_fmt), " %%ld ");
Eric Kunzee5e26762020-10-13 16:11:07 -07002794
2795 if (tensor == nullptr)
2796 {
2797 fprintf(out, "<Not allocated>\n");
2798 return 0;
2799 }
2800
2801 fprintf(out, "[");
2802 for (int i0 = 0; i0 < shape[0]; i0++)
2803 {
2804 fprintf(out, "[");
2805 for (int i1 = 0; i1 < shape[1]; i1++)
2806 {
2807 fprintf(out, "[");
2808 for (int i2 = 0; i2 < shape[2]; i2++)
2809 {
2810 fprintf(out, "[");
2811 for (int i3 = 0; i3 < shape[3]; i3++)
2812 {
2813 fprintf(out, "[");
2814 for (int i4 = 0; i4 < shape[4]; i4++)
2815 {
2816 fprintf(out, i64_fmt, (*tensor)(i0, i1, i2, i3, i4));
2817 }
2818 fprintf(out, "]\n");
2819 }
2820 fprintf(out, "]\n");
2821 }
2822 fprintf(out, "]\n");
2823 }
2824 fprintf(out, "]\n");
2825 }
2826 fprintf(out, "]\n");
2827
2828 return 0;
2829}
2830
2831template <>
2832int TosaReference::Tensor6<int64_t>::dumpTensor(FILE* out) const
2833{
Eric Kunze286f8342022-06-22 11:30:23 -07002834 char i64_fmt[32];
2835 snprintf(i64_fmt, sizeof(i64_fmt), " %%ld ");
Eric Kunzee5e26762020-10-13 16:11:07 -07002836
2837 if (tensor == nullptr)
2838 {
2839 fprintf(out, "<Not allocated>\n");
2840 return 0;
2841 }
2842
2843 fprintf(out, "[");
2844 for (int i0 = 0; i0 < shape[0]; i0++)
2845 {
2846 fprintf(out, "[");
2847 for (int i1 = 0; i1 < shape[1]; i1++)
2848 {
2849 fprintf(out, "[");
2850 for (int i2 = 0; i2 < shape[2]; i2++)
2851 {
2852 fprintf(out, "[");
2853 for (int i3 = 0; i3 < shape[3]; i3++)
2854 {
2855 fprintf(out, "[");
2856 for (int i4 = 0; i4 < shape[4]; i4++)
2857 {
2858 fprintf(out, "[");
2859 for (int i5 = 0; i5 < shape[5]; i5++)
2860 {
2861 fprintf(out, i64_fmt, (*tensor)(i0, i1, i2, i3, i4, i5));
2862 }
2863 fprintf(out, "]\n");
2864 }
2865 fprintf(out, "]\n");
2866 }
2867 fprintf(out, "]\n");
2868 }
2869 fprintf(out, "]\n");
2870 }
2871 fprintf(out, "]\n");
2872 }
2873 fprintf(out, "]\n");
2874
2875 return 0;
2876}
2877
2878template <>
2879int TosaReference::Tensor0<int32_t>::dumpTensor(FILE* out) const
2880{
Eric Kunze286f8342022-06-22 11:30:23 -07002881 char i32_fmt[32];
2882 snprintf(i32_fmt, sizeof(i32_fmt), "[ %%d ]\n");
Eric Kunzee5e26762020-10-13 16:11:07 -07002883
2884 if (tensor == nullptr)
2885 {
2886 fprintf(out, "<Not allocated>\n");
2887 return 0;
2888 }
2889
2890 fprintf(out, i32_fmt, (*tensor)(0));
2891
2892 return 0;
2893}
2894
2895template <>
2896int TosaReference::Tensor1<int32_t>::dumpTensor(FILE* out) const
2897{
Eric Kunze286f8342022-06-22 11:30:23 -07002898 char i32_fmt[32];
2899 snprintf(i32_fmt, sizeof(i32_fmt), " %%d ");
Eric Kunzee5e26762020-10-13 16:11:07 -07002900
2901 if (tensor == nullptr)
2902 {
2903 fprintf(out, "<Not allocated>\n");
2904 return 0;
2905 }
2906
2907 fprintf(out, "[");
2908 for (int i0 = 0; i0 < shape[0]; i0++)
2909 {
2910 fprintf(out, i32_fmt, (*tensor)(i0));
2911 }
2912 fprintf(out, "]\n");
2913
2914 return 0;
2915}
2916
2917template <>
2918int TosaReference::Tensor2<int32_t>::dumpTensor(FILE* out) const
2919{
Eric Kunze286f8342022-06-22 11:30:23 -07002920 char i32_fmt[32];
2921 snprintf(i32_fmt, sizeof(i32_fmt), " %%d ");
Eric Kunzee5e26762020-10-13 16:11:07 -07002922
2923 if (tensor == nullptr)
2924 {
2925 fprintf(out, "<Not allocated>\n");
2926 return 0;
2927 }
2928
2929 if (tensor == nullptr)
2930 {
2931 fprintf(out, "<Not allocated>\n");
2932 return 0;
2933 }
2934
2935 fprintf(out, "[");
2936 for (int i0 = 0; i0 < shape[0]; i0++)
2937 {
2938 fprintf(out, "[");
2939 for (int i1 = 0; i1 < shape[1]; i1++)
2940 {
2941 fprintf(out, i32_fmt, (*tensor)(i0, i1));
2942 }
2943 fprintf(out, "]\n");
2944 }
2945 fprintf(out, "]\n");
2946
2947 return 0;
2948}
2949
2950template <>
2951int TosaReference::Tensor3<int32_t>::dumpTensor(FILE* out) const
2952{
Eric Kunze286f8342022-06-22 11:30:23 -07002953 char i32_fmt[32];
2954 snprintf(i32_fmt, sizeof(i32_fmt), " %%d ");
Eric Kunzee5e26762020-10-13 16:11:07 -07002955
2956 if (tensor == nullptr)
2957 {
2958 fprintf(out, "<Not allocated>\n");
2959 return 0;
2960 }
2961
2962 if (tensor == nullptr)
2963 {
2964 fprintf(out, "<Not allocated>\n");
2965 return 0;
2966 }
2967
2968 fprintf(out, "[");
2969 for (int i0 = 0; i0 < shape[0]; i0++)
2970 {
2971 fprintf(out, "[");
2972 for (int i1 = 0; i1 < shape[1]; i1++)
2973 {
2974 fprintf(out, "[");
2975 for (int i2 = 0; i2 < shape[2]; i2++)
2976 {
2977 fprintf(out, i32_fmt, (*tensor)(i0, i1, i2));
2978 }
2979 fprintf(out, "]\n");
2980 }
2981 fprintf(out, "]\n");
2982 }
2983 fprintf(out, "]\n");
2984
2985 return 0;
2986}
2987
2988template <>
2989int TosaReference::Tensor4<int32_t>::dumpTensor(FILE* out) const
2990{
Eric Kunze286f8342022-06-22 11:30:23 -07002991 char i32_fmt[32];
2992 snprintf(i32_fmt, sizeof(i32_fmt), " %%d ");
Eric Kunzee5e26762020-10-13 16:11:07 -07002993
2994 if (tensor == nullptr)
2995 {
2996 fprintf(out, "<Not allocated>\n");
2997 return 0;
2998 }
2999
3000 fprintf(out, "[");
3001 for (int i0 = 0; i0 < shape[0]; i0++)
3002 {
3003 fprintf(out, "[");
3004 for (int i1 = 0; i1 < shape[1]; i1++)
3005 {
3006 fprintf(out, "[");
3007 for (int i2 = 0; i2 < shape[2]; i2++)
3008 {
3009 fprintf(out, "[");
3010 for (int i3 = 0; i3 < shape[3]; i3++)
3011 {
3012 fprintf(out, i32_fmt, (*tensor)(i0, i1, i2, i3));
3013 }
3014 fprintf(out, "]\n");
3015 }
3016 fprintf(out, "]\n");
3017 }
3018 fprintf(out, "]\n");
3019 }
3020 fprintf(out, "]\n");
3021
3022 return 0;
3023}
3024
3025template <>
3026int TosaReference::Tensor5<int32_t>::dumpTensor(FILE* out) const
3027{
Eric Kunze286f8342022-06-22 11:30:23 -07003028 char i32_fmt[32];
3029 snprintf(i32_fmt, sizeof(i32_fmt), " %%d ");
Eric Kunzee5e26762020-10-13 16:11:07 -07003030
3031 if (tensor == nullptr)
3032 {
3033 fprintf(out, "<Not allocated>\n");
3034 return 0;
3035 }
3036
3037 fprintf(out, "[");
3038 for (int i0 = 0; i0 < shape[0]; i0++)
3039 {
3040 fprintf(out, "[");
3041 for (int i1 = 0; i1 < shape[1]; i1++)
3042 {
3043 fprintf(out, "[");
3044 for (int i2 = 0; i2 < shape[2]; i2++)
3045 {
3046 fprintf(out, "[");
3047 for (int i3 = 0; i3 < shape[3]; i3++)
3048 {
3049 fprintf(out, "[");
3050 for (int i4 = 0; i4 < shape[4]; i4++)
3051 {
3052 fprintf(out, i32_fmt, (*tensor)(i0, i1, i2, i3, i4));
3053 }
3054 fprintf(out, "]\n");
3055 }
3056 fprintf(out, "]\n");
3057 }
3058 fprintf(out, "]\n");
3059 }
3060 fprintf(out, "]\n");
3061 }
3062 fprintf(out, "]\n");
3063
3064 return 0;
3065}
3066
3067template <>
3068int TosaReference::Tensor6<int32_t>::dumpTensor(FILE* out) const
3069{
Eric Kunze286f8342022-06-22 11:30:23 -07003070 char i32_fmt[32];
3071 snprintf(i32_fmt, sizeof(i32_fmt), " %%d ");
Eric Kunzee5e26762020-10-13 16:11:07 -07003072
3073 if (tensor == nullptr)
3074 {
3075 fprintf(out, "<Not allocated>\n");
3076 return 0;
3077 }
3078
3079 fprintf(out, "[");
3080 for (int i0 = 0; i0 < shape[0]; i0++)
3081 {
3082 fprintf(out, "[");
3083 for (int i1 = 0; i1 < shape[1]; i1++)
3084 {
3085 fprintf(out, "[");
3086 for (int i2 = 0; i2 < shape[2]; i2++)
3087 {
3088 fprintf(out, "[");
3089 for (int i3 = 0; i3 < shape[3]; i3++)
3090 {
3091 fprintf(out, "[");
3092 for (int i4 = 0; i4 < shape[4]; i4++)
3093 {
3094 fprintf(out, "[");
3095 for (int i5 = 0; i5 < shape[5]; i5++)
3096 {
3097 fprintf(out, i32_fmt, (*tensor)(i0, i1, i2, i3, i4, i5));
3098 }
3099 fprintf(out, "]\n");
3100 }
3101 fprintf(out, "]\n");
3102 }
3103 fprintf(out, "]\n");
3104 }
3105 fprintf(out, "]\n");
3106 }
3107 fprintf(out, "]\n");
3108 }
3109 fprintf(out, "]\n");
3110
3111 return 0;
3112}
3113
3114template <>
3115int TosaReference::Tensor0<bool>::dumpTensor(FILE* out) const
3116{
Eric Kunze286f8342022-06-22 11:30:23 -07003117 char bool_fmt[32];
3118 snprintf(bool_fmt, sizeof(bool_fmt), "[ %%s ]\n");
Eric Kunzee5e26762020-10-13 16:11:07 -07003119
3120 if (tensor == nullptr)
3121 {
3122 fprintf(out, "<Not allocated>\n");
3123 return 0;
3124 }
3125
3126 fprintf(out, bool_fmt, bool_to_str((*tensor)(0)));
3127
3128 return 0;
3129}
3130
3131template <>
3132int TosaReference::Tensor1<bool>::dumpTensor(FILE* out) const
3133{
Eric Kunze286f8342022-06-22 11:30:23 -07003134 char bool_fmt[32];
3135 snprintf(bool_fmt, sizeof(bool_fmt), " %%s ");
Eric Kunzee5e26762020-10-13 16:11:07 -07003136
3137 if (tensor == nullptr)
3138 {
3139 fprintf(out, "<Not allocated>\n");
3140 return 0;
3141 }
3142
3143 fprintf(out, "[");
3144 for (int i0 = 0; i0 < shape[0]; i0++)
3145 {
3146 fprintf(out, bool_fmt, bool_to_str((*tensor)(i0)));
3147 }
3148 fprintf(out, "]\n");
3149
3150 return 0;
3151}
3152
3153template <>
3154int TosaReference::Tensor2<bool>::dumpTensor(FILE* out) const
3155{
Eric Kunze286f8342022-06-22 11:30:23 -07003156 char bool_fmt[32];
3157 snprintf(bool_fmt, sizeof(bool_fmt), " %%s ");
Eric Kunzee5e26762020-10-13 16:11:07 -07003158
3159 if (tensor == nullptr)
3160 {
3161 fprintf(out, "<Not allocated>\n");
3162 return 0;
3163 }
3164
3165 fprintf(out, "[");
3166 for (int i0 = 0; i0 < shape[0]; i0++)
3167 {
3168 fprintf(out, "[");
3169 for (int i1 = 0; i1 < shape[1]; i1++)
3170 {
3171 fprintf(out, bool_fmt, bool_to_str((*tensor)(i0, i1)));
3172 }
3173 fprintf(out, "]\n");
3174 }
3175 fprintf(out, "]\n");
3176
3177 return 0;
3178}
3179
3180template <>
3181int TosaReference::Tensor3<bool>::dumpTensor(FILE* out) const
3182{
Eric Kunze286f8342022-06-22 11:30:23 -07003183 char bool_fmt[32];
3184 snprintf(bool_fmt, sizeof(bool_fmt), " %%s ");
Eric Kunzee5e26762020-10-13 16:11:07 -07003185
3186 if (tensor == nullptr)
3187 {
3188 fprintf(out, "<Not allocated>\n");
3189 return 0;
3190 }
3191
3192 fprintf(out, "[");
3193 for (int i0 = 0; i0 < shape[0]; i0++)
3194 {
3195 fprintf(out, "[");
3196 for (int i1 = 0; i1 < shape[1]; i1++)
3197 {
3198 fprintf(out, "[");
3199 for (int i2 = 0; i2 < shape[2]; i2++)
3200 {
3201 fprintf(out, bool_fmt, bool_to_str((*tensor)(i0, i1, i2)));
3202 }
3203 fprintf(out, "]\n");
3204 }
3205 fprintf(out, "]\n");
3206 }
3207 fprintf(out, "]\n");
3208
3209 return 0;
3210}
3211
3212template <>
3213int TosaReference::Tensor4<bool>::dumpTensor(FILE* out) const
3214{
Eric Kunze286f8342022-06-22 11:30:23 -07003215 char bool_fmt[32];
3216 snprintf(bool_fmt, sizeof(bool_fmt), " %%s ");
Eric Kunzee5e26762020-10-13 16:11:07 -07003217
3218 if (tensor == nullptr)
3219 {
3220 fprintf(out, "<Not allocated>\n");
3221 return 0;
3222 }
3223
3224 fprintf(out, "[");
3225 for (int i0 = 0; i0 < shape[0]; i0++)
3226 {
3227 fprintf(out, "[");
3228 for (int i1 = 0; i1 < shape[1]; i1++)
3229 {
3230 fprintf(out, "[");
3231 for (int i2 = 0; i2 < shape[2]; i2++)
3232 {
3233 fprintf(out, "[");
3234 for (int i3 = 0; i3 < shape[3]; i3++)
3235 {
3236 fprintf(out, bool_fmt, bool_to_str((*tensor)(i0, i1, i2, i3)));
3237 }
3238 fprintf(out, "]\n");
3239 }
3240 fprintf(out, "]\n");
3241 }
3242 fprintf(out, "]\n");
3243 }
3244 fprintf(out, "]\n");
3245
3246 return 0;
3247}
3248
3249template <>
3250int TosaReference::Tensor5<bool>::dumpTensor(FILE* out) const
3251{
Eric Kunze286f8342022-06-22 11:30:23 -07003252 char bool_fmt[32];
3253 snprintf(bool_fmt, sizeof(bool_fmt), " %%s ");
Eric Kunzee5e26762020-10-13 16:11:07 -07003254
3255 if (tensor == nullptr)
3256 {
3257 fprintf(out, "<Not allocated>\n");
3258 return 0;
3259 }
3260
3261 fprintf(out, "[");
3262 for (int i0 = 0; i0 < shape[0]; i0++)
3263 {
3264 fprintf(out, "[");
3265 for (int i1 = 0; i1 < shape[1]; i1++)
3266 {
3267 fprintf(out, "[");
3268 for (int i2 = 0; i2 < shape[2]; i2++)
3269 {
3270 fprintf(out, "[");
3271 for (int i3 = 0; i3 < shape[3]; i3++)
3272 {
3273 fprintf(out, "[");
3274 for (int i4 = 0; i4 < shape[4]; i4++)
3275 {
3276 fprintf(out, bool_fmt, bool_to_str((*tensor)(i0, i1, i2, i3, i4)));
3277 }
3278 fprintf(out, "]\n");
3279 }
3280 fprintf(out, "]\n");
3281 }
3282 fprintf(out, "]\n");
3283 }
3284 fprintf(out, "]\n");
3285 }
3286 fprintf(out, "]\n");
3287
3288 return 0;
3289}
3290
3291template <>
3292int TosaReference::Tensor6<bool>::dumpTensor(FILE* out) const
3293{
Eric Kunze286f8342022-06-22 11:30:23 -07003294 char bool_fmt[32];
3295 snprintf(bool_fmt, sizeof(bool_fmt), " %%s ");
Eric Kunzee5e26762020-10-13 16:11:07 -07003296
3297 if (tensor == nullptr)
3298 {
3299 fprintf(out, "<Not allocated>\n");
3300 return 0;
3301 }
3302
3303 fprintf(out, "[");
3304 for (int i0 = 0; i0 < shape[0]; i0++)
3305 {
3306 fprintf(out, "[");
3307 for (int i1 = 0; i1 < shape[1]; i1++)
3308 {
3309 fprintf(out, "[");
3310 for (int i2 = 0; i2 < shape[2]; i2++)
3311 {
3312 fprintf(out, "[");
3313 for (int i3 = 0; i3 < shape[3]; i3++)
3314 {
3315 fprintf(out, "[");
3316 for (int i4 = 0; i4 < shape[4]; i4++)
3317 {
3318 fprintf(out, "[");
3319 for (int i5 = 0; i5 < shape[5]; i5++)
3320 {
3321 fprintf(out, bool_fmt, bool_to_str((*tensor)(i0, i1, i2, i3, i4, i5)));
3322 }
3323 fprintf(out, "]\n");
3324 }
3325 fprintf(out, "]\n");
3326 }
3327 fprintf(out, "]\n");
3328 }
3329 fprintf(out, "]\n");
3330 }
3331 fprintf(out, "]\n");
3332 }
3333 fprintf(out, "]\n");
3334
3335 return 0;
3336}
3337
3338template <class T>
3339int TosaReference::TensorTemplate<T>::dumpTensor(FILE* out) const
3340{
3341 return 0;
3342}
3343
3344// template explicit specialization
3345template class TosaReference::TensorTemplate<Eigen::Tensor<float, 0>>;
3346template class TosaReference::TensorTemplate<Eigen::Tensor<float, 1>>;
3347template class TosaReference::TensorTemplate<Eigen::Tensor<float, 2>>;
3348template class TosaReference::TensorTemplate<Eigen::Tensor<float, 3>>;
3349template class TosaReference::TensorTemplate<Eigen::Tensor<float, 4>>;
3350template class TosaReference::TensorTemplate<Eigen::Tensor<float, 5>>;
3351template class TosaReference::TensorTemplate<Eigen::Tensor<float, 6>>;
3352
3353template class TosaReference::TensorTemplate<Eigen::Tensor<int32_t, 0>>;
3354template class TosaReference::TensorTemplate<Eigen::Tensor<int32_t, 1>>;
3355template class TosaReference::TensorTemplate<Eigen::Tensor<int32_t, 2>>;
3356template class TosaReference::TensorTemplate<Eigen::Tensor<int32_t, 3>>;
3357template class TosaReference::TensorTemplate<Eigen::Tensor<int32_t, 4>>;
3358template class TosaReference::TensorTemplate<Eigen::Tensor<int32_t, 5>>;
3359template class TosaReference::TensorTemplate<Eigen::Tensor<int32_t, 6>>;
3360
3361template class TosaReference::TensorTemplate<Eigen::Tensor<int64_t, 0>>;
3362template class TosaReference::TensorTemplate<Eigen::Tensor<int64_t, 1>>;
3363template class TosaReference::TensorTemplate<Eigen::Tensor<int64_t, 2>>;
3364template class TosaReference::TensorTemplate<Eigen::Tensor<int64_t, 3>>;
3365template class TosaReference::TensorTemplate<Eigen::Tensor<int64_t, 4>>;
3366template class TosaReference::TensorTemplate<Eigen::Tensor<int64_t, 5>>;
3367template class TosaReference::TensorTemplate<Eigen::Tensor<int64_t, 6>>;
3368
3369template class TosaReference::TensorTemplate<Eigen::Tensor<bool, 0>>;
3370template class TosaReference::TensorTemplate<Eigen::Tensor<bool, 1>>;
3371template class TosaReference::TensorTemplate<Eigen::Tensor<bool, 2>>;
3372template class TosaReference::TensorTemplate<Eigen::Tensor<bool, 3>>;
3373template class TosaReference::TensorTemplate<Eigen::Tensor<bool, 4>>;
3374template class TosaReference::TensorTemplate<Eigen::Tensor<bool, 5>>;
3375template class TosaReference::TensorTemplate<Eigen::Tensor<bool, 6>>;