From e9059775c0486de4a96d42b41104496f4aefe8e8 Mon Sep 17 00:00:00 2001 From: Georgios Pinitas Date: Wed, 6 Dec 2023 18:52:30 +0000 Subject: Add support for int16_t inputs in eager interface Implement support of int16_t inputs through int32_t underlying storage buffers. Values are upcasted/downcasted as needed through explicit copy. Signed-off-by: Georgios Pinitas Change-Id: I3752d98531c859002539bf1cb65413ceeff05e95 --- reference_model/src/tensor.cc | 392 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 392 insertions(+) (limited to 'reference_model/src/tensor.cc') diff --git a/reference_model/src/tensor.cc b/reference_model/src/tensor.cc index 5fffa8a..645b55f 100644 --- a/reference_model/src/tensor.cc +++ b/reference_model/src/tensor.cc @@ -647,6 +647,31 @@ int TosaReference::Tensor::readfromVector(const ArrayProxy val return 0; } +int TosaReference::Tensor::readfromVector(const ArrayProxy vals) +{ + uint32_t elements = getElementCount(); + switch (getDtype()) + { + case TOSA_REF_TYPE_INT16: + case TOSA_REF_TYPE_UINT16: + if (vals.size() != elements) + { + WARNING("The input size (%ld) doesn't match the number of elements (%d) assigned to the tensor.", + vals.size(), elements); + return -1; + } + + setTensorValueInt16(elements, vals.data()); + break; + default: + WARNING("The input type doesn't match the data type assigned to the tensor (%s).", + EnumNameTOSAREFTYPE(getDtype())); + return -2; + } + setIsValid(); + return 0; +} + int TosaReference::Tensor::readfromVector(const ArrayProxy vals) { uint32_t elements = getElementCount(); @@ -822,6 +847,31 @@ int TosaReference::Tensor::writeToVector(ArrayProxy vals) return 0; } +int TosaReference::Tensor::writeToVector(ArrayProxy vals) +{ + uint32_t elements = getElementCount(); + + switch (getDtype()) + { + case TOSA_REF_TYPE_INT16: + case TOSA_REF_TYPE_UINT16: + if (vals.size() != elements) + { + WARNING("The output size (%ld) doesn't match the number of elements (%d) assigned to the tensor.", + vals.size(), elements); + return -1; + } + + getTensorValueInt16(elements, vals.data()); + break; + default: + WARNING("The output type doesn't match the data type assigned to the tensor (%s).", + EnumNameTOSAREFTYPE(getDtype())); + return -2; + } + return 0; +} + int TosaReference::Tensor::writeToVector(ArrayProxy vals) { uint32_t elements = getElementCount(); @@ -1204,6 +1254,158 @@ int TosaReference::Tensor6::setTensorValueFloat(const size_t bufLen, cons return 0; } +template +int TosaReference::TensorTemplate::setTensorValueInt16(const size_t bufLen, const int16_t* vals) +{ + FATAL_ERROR("TensorTemplate::setTensorValueInt32 should not be called. " + "Implement template specialization version."); + return 0; +} + +template <> +int TosaReference::Tensor0::setTensorValueInt16(const size_t bufLen, const int16_t* vals) +{ + ASSERT_MSG(bufLen == getElementCount(), "Total elements must match"); + + (*tensor)(0) = static_cast(vals[0]); + + return 0; +} + +template <> +int TosaReference::Tensor1::setTensorValueInt16(const size_t bufLen, const int16_t* vals) +{ + uint32_t idx = 0; + + ASSERT_MSG(bufLen == getElementCount(), "Total elements must match"); + + for (int i0 = 0; i0 < shape[0]; i0++) + { + (*tensor)(i0) = static_cast(vals[idx++]); + } + + return 0; +} + +template <> +int TosaReference::Tensor2::setTensorValueInt16(const size_t bufLen, const int16_t* vals) +{ + uint32_t idx = 0; + + ASSERT_MSG(bufLen == getElementCount(), "Total elements must match"); + + for (int i0 = 0; i0 < shape[0]; i0++) + { + for (int i1 = 0; i1 < shape[1]; i1++) + { + (*tensor)(i0, i1) = static_cast(vals[idx++]); + } + } + + return 0; +} + +template <> +int TosaReference::Tensor3::setTensorValueInt16(const size_t bufLen, const int16_t* vals) +{ + uint32_t idx = 0; + + ASSERT_MSG(bufLen == getElementCount(), "Total elements must match"); + + for (int i0 = 0; i0 < shape[0]; i0++) + { + for (int i1 = 0; i1 < shape[1]; i1++) + { + for (int i2 = 0; i2 < shape[2]; i2++) + { + (*tensor)(i0, i1, i2) = static_cast(vals[idx++]); + } + } + } + + return 0; +} + +template <> +int TosaReference::Tensor4::setTensorValueInt16(const size_t bufLen, const int16_t* vals) +{ + uint32_t idx = 0; + + ASSERT_MSG(bufLen == getElementCount(), "Total elements must match"); + + for (int i0 = 0; i0 < shape[0]; i0++) + { + for (int i1 = 0; i1 < shape[1]; i1++) + { + for (int i2 = 0; i2 < shape[2]; i2++) + { + for (int i3 = 0; i3 < shape[3]; i3++) + { + (*tensor)(i0, i1, i2, i3) = static_cast(vals[idx++]); + } + } + } + } + + return 0; +} + +template <> +int TosaReference::Tensor5::setTensorValueInt16(const size_t bufLen, const int16_t* vals) +{ + uint32_t idx = 0; + + ASSERT_MSG(bufLen == getElementCount(), "Total elements must match"); + + for (int i0 = 0; i0 < shape[0]; i0++) + { + for (int i1 = 0; i1 < shape[1]; i1++) + { + for (int i2 = 0; i2 < shape[2]; i2++) + { + for (int i3 = 0; i3 < shape[3]; i3++) + { + for (int i4 = 0; i4 < shape[4]; i4++) + { + (*tensor)(i0, i1, i2, i3, i4) = static_cast(vals[idx++]); + } + } + } + } + } + + return 0; +} + +template <> +int TosaReference::Tensor6::setTensorValueInt16(const size_t bufLen, const int16_t* vals) +{ + uint32_t idx = 0; + + ASSERT_MSG(bufLen == getElementCount(), "Total elements must match"); + + for (int i0 = 0; i0 < shape[0]; i0++) + { + for (int i1 = 0; i1 < shape[1]; i1++) + { + for (int i2 = 0; i2 < shape[2]; i2++) + { + for (int i3 = 0; i3 < shape[3]; i3++) + { + for (int i4 = 0; i4 < shape[4]; i4++) + { + for (int i5 = 0; i5 < shape[5]; i5++) + { + (*tensor)(i0, i1, i2, i3, i4, i5) = static_cast(vals[idx++]); + } + } + } + } + } + } + return 0; +} + template int TosaReference::TensorTemplate::setTensorValueInt32(const size_t bufLen, const int32_t* vals) { @@ -2040,6 +2242,196 @@ int TosaReference::Tensor6::getTensorValueFloat(const size_t bufLen, floa return 0; } +template +int TosaReference::TensorTemplate::getTensorValueInt16(const size_t bufLen, int16_t* vals) const +{ + FATAL_ERROR("TensorTemplate::getTensorValueInt32 should not be called. " + "Implement template specialization version."); + return 0; +} + +template <> +int TosaReference::Tensor0::getTensorValueInt16(const size_t bufLen, int16_t* vals) const +{ + int totalVals = 1; + + ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match"); + + vals[0] = (*tensor)(0); + + return 0; +} + +template <> +int TosaReference::Tensor1::getTensorValueInt16(const size_t bufLen, int16_t* vals) const +{ + uint32_t idx = 0; + int totalVals = 1; + + for (size_t i = 0; i < shape.size(); i++) + { + totalVals *= shape[i]; + } + + ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match"); + + for (int i0 = 0; i0 < shape[0]; i0++) + { + vals[idx++] = (*tensor)(i0); + } + + return 0; +} + +template <> +int TosaReference::Tensor2::getTensorValueInt16(const size_t bufLen, int16_t* vals) const +{ + uint32_t idx = 0; + int totalVals = 1; + + for (size_t i = 0; i < shape.size(); i++) + { + totalVals *= shape[i]; + } + + ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match"); + + for (int i0 = 0; i0 < shape[0]; i0++) + { + for (int i1 = 0; i1 < shape[1]; i1++) + { + vals[idx++] = (*tensor)(i0, i1); + } + } + + return 0; +} + +template <> +int TosaReference::Tensor3::getTensorValueInt16(const size_t bufLen, int16_t* vals) const +{ + uint32_t idx = 0; + int totalVals = 1; + + for (size_t i = 0; i < shape.size(); i++) + { + totalVals *= shape[i]; + } + + ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match"); + + for (int i0 = 0; i0 < shape[0]; i0++) + { + for (int i1 = 0; i1 < shape[1]; i1++) + { + for (int i2 = 0; i2 < shape[2]; i2++) + { + vals[idx++] = (*tensor)(i0, i1, i2); + } + } + } + + return 0; +} + +template <> +int TosaReference::Tensor4::getTensorValueInt16(const size_t bufLen, int16_t* vals) const +{ + uint32_t idx = 0; + int totalVals = 1; + + for (size_t i = 0; i < shape.size(); i++) + { + totalVals *= shape[i]; + } + + ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match"); + + for (int i0 = 0; i0 < shape[0]; i0++) + { + for (int i1 = 0; i1 < shape[1]; i1++) + { + for (int i2 = 0; i2 < shape[2]; i2++) + { + for (int i3 = 0; i3 < shape[3]; i3++) + { + vals[idx++] = (*tensor)(i0, i1, i2, i3); + } + } + } + } + + return 0; +} + +template <> +int TosaReference::Tensor5::getTensorValueInt16(const size_t bufLen, int16_t* vals) const +{ + uint32_t idx = 0; + int totalVals = 1; + + for (size_t i = 0; i < shape.size(); i++) + { + totalVals *= shape[i]; + } + + ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match"); + + for (int i0 = 0; i0 < shape[0]; i0++) + { + for (int i1 = 0; i1 < shape[1]; i1++) + { + for (int i2 = 0; i2 < shape[2]; i2++) + { + for (int i3 = 0; i3 < shape[3]; i3++) + { + for (int i4 = 0; i4 < shape[4]; i4++) + { + vals[idx++] = (*tensor)(i0, i1, i2, i3, i4); + } + } + } + } + } + + return 0; +} + +template <> +int TosaReference::Tensor6::getTensorValueInt16(const size_t bufLen, int16_t* vals) const +{ + uint32_t idx = 0; + int totalVals = 1; + + for (size_t i = 0; i < shape.size(); i++) + { + totalVals *= shape[i]; + } + + ASSERT_MSG((size_t)totalVals == bufLen, "Output buffer and tensor size do not match"); + + for (int i0 = 0; i0 < shape[0]; i0++) + { + for (int i1 = 0; i1 < shape[1]; i1++) + { + for (int i2 = 0; i2 < shape[2]; i2++) + { + for (int i3 = 0; i3 < shape[3]; i3++) + { + for (int i4 = 0; i4 < shape[4]; i4++) + { + for (int i5 = 0; i5 < shape[5]; i5++) + { + vals[idx++] = (*tensor)(i0, i1, i2, i3, i4, i5); + } + } + } + } + } + } + return 0; +} + template int TosaReference::TensorTemplate::getTensorValueInt32(const size_t bufLen, int32_t* vals) const { -- cgit v1.2.1