aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorgios Pinitas <georgios.pinitas@arm.com>2023-12-06 18:52:30 +0000
committerGeorgios Pinitas <georgios.pinitas@arm.com>2023-12-06 19:15:48 +0000
commite9059775c0486de4a96d42b41104496f4aefe8e8 (patch)
tree728fed98b82fd4508f0df14a4e4df3836b154e79
parent438ad7ff03bee24fdd2fa09909cdec01affac399 (diff)
downloadreference_model-e9059775c0486de4a96d42b41104496f4aefe8e8.tar.gz
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 <georgios.pinitas@arm.com> Change-Id: I3752d98531c859002539bf1cb65413ceeff05e95
-rw-r--r--reference_model/src/model_runner_impl.cc12
-rw-r--r--reference_model/src/tensor.cc392
-rw-r--r--reference_model/src/tensor.h36
-rw-r--r--reference_model/test/model_runner_tests.cpp30
4 files changed, 470 insertions, 0 deletions
diff --git a/reference_model/src/model_runner_impl.cc b/reference_model/src/model_runner_impl.cc
index 447ee26..311db7c 100644
--- a/reference_model/src/model_runner_impl.cc
+++ b/reference_model/src/model_runner_impl.cc
@@ -197,6 +197,12 @@ int ModelRunnerImpl::setInput(std::string input_name, uint8_t* raw_ptr, size_t s
status = setInput(input_name, ArrayProxy(elements, typed_ptr));
break;
}
+ case TOSA_REF_TYPE_INT16: {
+ auto typed_ptr = reinterpret_cast<int16_t*>(raw_ptr);
+ const int elements = size / sizeof(int16_t);
+ status = setInput(input_name, ArrayProxy(elements, typed_ptr));
+ break;
+ }
case TOSA_REF_TYPE_INT32: {
auto typed_ptr = reinterpret_cast<int*>(raw_ptr);
const int elements = size / sizeof(int);
@@ -281,6 +287,12 @@ int ModelRunnerImpl::getOutput(std::string output_name, uint8_t* raw_ptr, size_t
status = tensor->writeToVector(ArrayProxy(elements, typed_ptr));
break;
}
+ case TOSA_REF_TYPE_INT16: {
+ auto typed_ptr = reinterpret_cast<int16_t*>(raw_ptr);
+ const int elements = size / sizeof(int16_t);
+ status = tensor->writeToVector(ArrayProxy(elements, typed_ptr));
+ break;
+ }
case TOSA_REF_TYPE_INT32: {
auto typed_ptr = reinterpret_cast<int*>(raw_ptr);
const int elements = size / sizeof(int);
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<half_float::half> val
return 0;
}
+int TosaReference::Tensor::readfromVector(const ArrayProxy<int16_t> 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<int32_t> vals)
{
uint32_t elements = getElementCount();
@@ -822,6 +847,31 @@ int TosaReference::Tensor::writeToVector(ArrayProxy<half_float::half> vals)
return 0;
}
+int TosaReference::Tensor::writeToVector(ArrayProxy<int16_t> 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<int32_t> vals)
{
uint32_t elements = getElementCount();
@@ -1205,6 +1255,158 @@ int TosaReference::Tensor6<float>::setTensorValueFloat(const size_t bufLen, cons
}
template <class T>
+int TosaReference::TensorTemplate<T>::setTensorValueInt16(const size_t bufLen, const int16_t* vals)
+{
+ FATAL_ERROR("TensorTemplate<T>::setTensorValueInt32 should not be called. "
+ "Implement template specialization version.");
+ return 0;
+}
+
+template <>
+int TosaReference::Tensor0<int32_t>::setTensorValueInt16(const size_t bufLen, const int16_t* vals)
+{
+ ASSERT_MSG(bufLen == getElementCount(), "Total elements must match");
+
+ (*tensor)(0) = static_cast<int32_t>(vals[0]);
+
+ return 0;
+}
+
+template <>
+int TosaReference::Tensor1<int32_t>::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<int32_t>(vals[idx++]);
+ }
+
+ return 0;
+}
+
+template <>
+int TosaReference::Tensor2<int32_t>::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<int32_t>(vals[idx++]);
+ }
+ }
+
+ return 0;
+}
+
+template <>
+int TosaReference::Tensor3<int32_t>::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<int32_t>(vals[idx++]);
+ }
+ }
+ }
+
+ return 0;
+}
+
+template <>
+int TosaReference::Tensor4<int32_t>::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<int32_t>(vals[idx++]);
+ }
+ }
+ }
+ }
+
+ return 0;
+}
+
+template <>
+int TosaReference::Tensor5<int32_t>::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<int32_t>(vals[idx++]);
+ }
+ }
+ }
+ }
+ }
+
+ return 0;
+}
+
+template <>
+int TosaReference::Tensor6<int32_t>::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<int32_t>(vals[idx++]);
+ }
+ }
+ }
+ }
+ }
+ }
+ return 0;
+}
+
+template <class T>
int TosaReference::TensorTemplate<T>::setTensorValueInt32(const size_t bufLen, const int32_t* vals)
{
FATAL_ERROR("TensorTemplate<T>::setTensorValueInt32 should not be called. "
@@ -2041,6 +2243,196 @@ int TosaReference::Tensor6<float>::getTensorValueFloat(const size_t bufLen, floa
}
template <class T>
+int TosaReference::TensorTemplate<T>::getTensorValueInt16(const size_t bufLen, int16_t* vals) const
+{
+ FATAL_ERROR("TensorTemplate<T>::getTensorValueInt32 should not be called. "
+ "Implement template specialization version.");
+ return 0;
+}
+
+template <>
+int TosaReference::Tensor0<int32_t>::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<int32_t>::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<int32_t>::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<int32_t>::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<int32_t>::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<int32_t>::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<int32_t>::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 <class T>
int TosaReference::TensorTemplate<T>::getTensorValueInt32(const size_t bufLen, int32_t* vals) const
{
FATAL_ERROR("TensorTemplate<T>::getTensorValueInt32 should not be called. "
diff --git a/reference_model/src/tensor.h b/reference_model/src/tensor.h
index 203cfec..5bcd1b2 100644
--- a/reference_model/src/tensor.h
+++ b/reference_model/src/tensor.h
@@ -241,11 +241,13 @@ public:
virtual int setTensorValueDouble(const size_t bufLen, const double* vals) = 0;
virtual int setTensorValueFloat(const size_t bufLen, const float* vals) = 0;
+ virtual int setTensorValueInt16(const size_t bufLen, const int16_t* vals) = 0;
virtual int setTensorValueInt32(const size_t bufLen, const int32_t* vals) = 0;
virtual int setTensorValueInt64(const size_t bufLen, const int64_t* vals) = 0;
virtual int setTensorValueBool(const size_t bufLen, const bool* vals) = 0;
virtual int getTensorValueDouble(const size_t bufLen, double* fbuf) const = 0;
virtual int getTensorValueFloat(const size_t bufLen, float* fbuf) const = 0;
+ virtual int getTensorValueInt16(const size_t bufLen, int16_t* ibuf) const = 0;
virtual int getTensorValueInt32(const size_t bufLen, int32_t* ibuf) const = 0;
virtual int getTensorValueInt64(const size_t bufLen, int64_t* ibuf) const = 0;
virtual int getTensorValueBool(const size_t bufLen, bool* ibuf) const = 0;
@@ -257,6 +259,7 @@ public:
virtual int readfromVector(const ArrayProxy<double> vals);
virtual int readfromVector(const ArrayProxy<float> vals);
virtual int readfromVector(const ArrayProxy<half_float::half> vals);
+ virtual int readfromVector(const ArrayProxy<int16_t> vals);
virtual int readfromVector(const ArrayProxy<int32_t> vals);
virtual int readfromVector(const ArrayProxy<int64_t> vals);
virtual int readfromVector(const ArrayProxy<unsigned char> vals);
@@ -264,6 +267,7 @@ public:
virtual int writeToVector(ArrayProxy<double> vals);
virtual int writeToVector(ArrayProxy<float> vals);
virtual int writeToVector(ArrayProxy<half_float::half> vals);
+ virtual int writeToVector(ArrayProxy<int16_t> vals);
virtual int writeToVector(ArrayProxy<int32_t> vals);
virtual int writeToVector(ArrayProxy<int64_t> vals);
virtual int writeToVector(ArrayProxy<unsigned char> vals);
@@ -357,12 +361,14 @@ public:
virtual int setTensorValueDouble(const size_t bufLen, const double* vals);
virtual int setTensorValueFloat(const size_t bufLen, const float* vals);
+ virtual int setTensorValueInt16(const size_t bufLen, const int16_t* vals);
virtual int setTensorValueInt32(const size_t bufLen, const int32_t* vals);
virtual int setTensorValueInt64(const size_t bufLen, const int64_t* vals);
virtual int setTensorValueBool(const size_t bufLen, const bool* vals);
virtual int getTensorValueDouble(const size_t bufLen, double* fbuf) const;
virtual int getTensorValueFloat(const size_t bufLen, float* fbuf) const;
+ virtual int getTensorValueInt16(const size_t bufLen, int16_t* ibuf) const;
virtual int getTensorValueInt32(const size_t bufLen, int32_t* ibuf) const;
virtual int getTensorValueInt64(const size_t bufLen, int64_t* ibuf) const;
virtual int getTensorValueBool(const size_t bufLen, bool* bbuf) const;
@@ -526,6 +532,21 @@ template <>
int Tensor6<bool>::copyValueFrom(Tensor* src);
template <>
+int Tensor0<int32_t>::setTensorValueInt16(const size_t bufLen, const int16_t* vals);
+template <>
+int Tensor1<int32_t>::setTensorValueInt16(const size_t bufLen, const int16_t* vals);
+template <>
+int Tensor2<int32_t>::setTensorValueInt16(const size_t bufLen, const int16_t* vals);
+template <>
+int Tensor3<int32_t>::setTensorValueInt16(const size_t bufLen, const int16_t* vals);
+template <>
+int Tensor4<int32_t>::setTensorValueInt16(const size_t bufLen, const int16_t* vals);
+template <>
+int Tensor5<int32_t>::setTensorValueInt16(const size_t bufLen, const int16_t* vals);
+template <>
+int Tensor6<int32_t>::setTensorValueInt16(const size_t bufLen, const int16_t* vals);
+
+template <>
int Tensor0<int32_t>::setTensorValueInt32(const size_t bufLen, const int32_t* vals);
template <>
int Tensor1<int32_t>::setTensorValueInt32(const size_t bufLen, const int32_t* vals);
@@ -541,6 +562,21 @@ template <>
int Tensor6<int32_t>::setTensorValueInt32(const size_t bufLen, const int32_t* vals);
template <>
+int Tensor0<int32_t>::getTensorValueInt16(const size_t bufLen, int16_t* vals) const;
+template <>
+int Tensor1<int32_t>::getTensorValueInt16(const size_t bufLen, int16_t* vals) const;
+template <>
+int Tensor2<int32_t>::getTensorValueInt16(const size_t bufLen, int16_t* vals) const;
+template <>
+int Tensor3<int32_t>::getTensorValueInt16(const size_t bufLen, int16_t* vals) const;
+template <>
+int Tensor4<int32_t>::getTensorValueInt16(const size_t bufLen, int16_t* vals) const;
+template <>
+int Tensor5<int32_t>::getTensorValueInt16(const size_t bufLen, int16_t* vals) const;
+template <>
+int Tensor6<int32_t>::getTensorValueInt16(const size_t bufLen, int16_t* vals) const;
+
+template <>
int Tensor0<int32_t>::getTensorValueInt32(const size_t bufLen, int32_t* vals) const;
template <>
int Tensor1<int32_t>::getTensorValueInt32(const size_t bufLen, int32_t* vals) const;
diff --git a/reference_model/test/model_runner_tests.cpp b/reference_model/test/model_runner_tests.cpp
index 6580774..35e3aa2 100644
--- a/reference_model/test/model_runner_tests.cpp
+++ b/reference_model/test/model_runner_tests.cpp
@@ -122,6 +122,36 @@ TEST_SUITE("model_runner")
compareOutput(dstData, expectedData, expectedData.size());
}
+ TEST_CASE("op_entry_cast")
+ {
+ // Inputs/Outputs
+ std::vector<int32_t> shape = { 1, 2, 2, 1 };
+ std::vector<int16_t> srcData = { 15, 13, 5, -51 };
+ std::vector<float> dstData(4, 0.f);
+
+ tosa_tensor_t input;
+ input.shape = shape.data();
+ input.num_dims = shape.size();
+ input.data_type = tosa_datatype_int16_t;
+ input.data = reinterpret_cast<uint8_t*>(srcData.data());
+ input.size = srcData.size() * sizeof(int16_t);
+
+ tosa_tensor_t output;
+ output.shape = shape.data();
+ output.num_dims = shape.size();
+ output.data_type = tosa_datatype_fp32_t;
+ output.data = reinterpret_cast<uint8_t*>(dstData.data());
+ output.size = dstData.size() * sizeof(float);
+
+ // Execution
+ auto status = tosa_run_cast(input, output, {});
+ CHECK((status == tosa_status_valid));
+
+ // Compare results
+ std::vector<float> expectedData = { 15.f, 13.f, 5.f, -51.f };
+ compareOutput(dstData, expectedData, expectedData.size());
+ }
+
TEST_CASE("op_entry_conv2d")
{
// Conv parameters