aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Cheng <kevin.cheng@arm.com>2021-06-17 16:01:59 -0700
committerKevin Cheng <kevin.cheng@arm.com>2021-06-24 16:37:58 -0700
commit82507d77056dd5510547438ba2064c1ee8bebc2c (patch)
treeca5c4bd49029430b1153091f30680866e97ccd2f
parent2d60f0063eb91f6514b20a1817663ce0ddd3ff4a (diff)
downloadreference_model-82507d77056dd5510547438ba2064c1ee8bebc2c.tar.gz
Update to use new serialization_lib API.
- Constant tensors are now initialized from embedded u8 array instead from numpy. - Python unit test generator and built-in test hasn't been updated. Signed-off-by: Kevin Cheng <kevin.cheng@arm.com> Change-Id: I5cb86f8e5ec8f23fee5dcbf257874a0f204ede04
-rw-r--r--examples/test_add_1x4x4x4_f32/flatbuffer-tf/desc.json6
-rw-r--r--examples/test_add_1x4x4x4_f32/flatbuffer-tf/test_add_1x4x4x4_f32.tosabin492 -> 516 bytes
-rw-r--r--examples/test_add_1x4x4x4_f32/flatbuffer-tflite/desc.json6
-rw-r--r--examples/test_add_1x4x4x4_f32/flatbuffer-tflite/test_add_1x4x4x4_f32.tosabin516 -> 516 bytes
-rw-r--r--examples/test_add_1x4x4x4_f32/model.pb2
-rw-r--r--examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/flatbuffer-tf/desc.json4
-rw-r--r--examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/flatbuffer-tf/layer_1.npybin144 -> 0 bytes
-rw-r--r--examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/flatbuffer-tf/layer_2.npybin192 -> 0 bytes
-rw-r--r--examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/flatbuffer-tf/layer_3.npybin640 -> 0 bytes
-rw-r--r--examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/flatbuffer-tf/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11.tosabin964 -> 1556 bytes
-rw-r--r--examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/flatbuffer-tflite/desc.json4
-rw-r--r--examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/flatbuffer-tflite/layer_1.npybin192 -> 0 bytes
-rw-r--r--examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/flatbuffer-tflite/layer_2.npybin640 -> 0 bytes
-rw-r--r--examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/flatbuffer-tflite/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11.tosabin744 -> 1312 bytes
-rw-r--r--examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/model.pb2
-rw-r--r--examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/model.tflitebin1272 -> 1272 bytes
-rw-r--r--examples/test_conv2d_1x1_1x32x32x8_qi8_st11_padSAME_dilat11/flatbuffer-tflite/desc.json4
-rw-r--r--examples/test_conv2d_1x1_1x32x32x8_qi8_st11_padSAME_dilat11/flatbuffer-tflite/layer_1.npybin192 -> 0 bytes
-rw-r--r--examples/test_conv2d_1x1_1x32x32x8_qi8_st11_padSAME_dilat11/flatbuffer-tflite/layer_2.npybin640 -> 0 bytes
-rw-r--r--examples/test_conv2d_1x1_1x32x32x8_qi8_st11_padSAME_dilat11/flatbuffer-tflite/test_conv2d_1x1_1x32x32x8_qi8_st11_padSAME_dilat11.tosabin1072 -> 1264 bytes
-rw-r--r--examples/test_conv2d_1x1_1x32x32x8_qi8_st11_padSAME_dilat11/model.tflitebin1408 -> 1408 bytes
-rw-r--r--reference_model/src/subgraph_traverser.cc67
m---------thirdparty/serialization_lib0
-rw-r--r--verif/tosa_serializer.py82
24 files changed, 138 insertions, 39 deletions
diff --git a/examples/test_add_1x4x4x4_f32/flatbuffer-tf/desc.json b/examples/test_add_1x4x4x4_f32/flatbuffer-tf/desc.json
index 1a86713..423772a 100644
--- a/examples/test_add_1x4x4x4_f32/flatbuffer-tf/desc.json
+++ b/examples/test_add_1x4x4x4_f32/flatbuffer-tf/desc.json
@@ -1,15 +1,15 @@
{
"tosa_file": "test_add_1x4x4x4_f32.tosa",
"ifm_name": [
- "InputTensor-tf0",
- "InputTensor-tf1"
+ "TosaInput_0",
+ "TosaInput_1"
],
"ifm_file": [
"../placeholder_0.npy",
"../placeholder_1.npy"
],
"ofm_name": [
- "Result0"
+ "TosaOutput_0"
],
"ofm_file": [
"ref_model_Result0.npy"
diff --git a/examples/test_add_1x4x4x4_f32/flatbuffer-tf/test_add_1x4x4x4_f32.tosa b/examples/test_add_1x4x4x4_f32/flatbuffer-tf/test_add_1x4x4x4_f32.tosa
index 003d1bb..8d004e8 100644
--- a/examples/test_add_1x4x4x4_f32/flatbuffer-tf/test_add_1x4x4x4_f32.tosa
+++ b/examples/test_add_1x4x4x4_f32/flatbuffer-tf/test_add_1x4x4x4_f32.tosa
Binary files differ
diff --git a/examples/test_add_1x4x4x4_f32/flatbuffer-tflite/desc.json b/examples/test_add_1x4x4x4_f32/flatbuffer-tflite/desc.json
index 30a447b..423772a 100644
--- a/examples/test_add_1x4x4x4_f32/flatbuffer-tflite/desc.json
+++ b/examples/test_add_1x4x4x4_f32/flatbuffer-tflite/desc.json
@@ -1,15 +1,15 @@
{
"tosa_file": "test_add_1x4x4x4_f32.tosa",
"ifm_name": [
- "InputTensor-tflite0",
- "InputTensor-tflite1"
+ "TosaInput_0",
+ "TosaInput_1"
],
"ifm_file": [
"../placeholder_0.npy",
"../placeholder_1.npy"
],
"ofm_name": [
- "Result0"
+ "TosaOutput_0"
],
"ofm_file": [
"ref_model_Result0.npy"
diff --git a/examples/test_add_1x4x4x4_f32/flatbuffer-tflite/test_add_1x4x4x4_f32.tosa b/examples/test_add_1x4x4x4_f32/flatbuffer-tflite/test_add_1x4x4x4_f32.tosa
index 864aaac..8d004e8 100644
--- a/examples/test_add_1x4x4x4_f32/flatbuffer-tflite/test_add_1x4x4x4_f32.tosa
+++ b/examples/test_add_1x4x4x4_f32/flatbuffer-tflite/test_add_1x4x4x4_f32.tosa
Binary files differ
diff --git a/examples/test_add_1x4x4x4_f32/model.pb b/examples/test_add_1x4x4x4_f32/model.pb
index 930928d..c8b634c 100644
--- a/examples/test_add_1x4x4x4_f32/model.pb
+++ b/examples/test_add_1x4x4x4_f32/model.pb
@@ -92,5 +92,5 @@ node {
}
}
versions {
- producer: 772
+ producer: 790
}
diff --git a/examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/flatbuffer-tf/desc.json b/examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/flatbuffer-tf/desc.json
index b62c528..e59bac3 100644
--- a/examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/flatbuffer-tf/desc.json
+++ b/examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/flatbuffer-tf/desc.json
@@ -1,13 +1,13 @@
{
"tosa_file": "test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11.tosa",
"ifm_name": [
- "InputTensor-tf0"
+ "TosaInput_0"
],
"ifm_file": [
"../placeholder_0.npy"
],
"ofm_name": [
- "Result0"
+ "TosaOutput_0"
],
"ofm_file": [
"ref_model_Result0.npy"
diff --git a/examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/flatbuffer-tf/layer_1.npy b/examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/flatbuffer-tf/layer_1.npy
deleted file mode 100644
index 42ff6d7..0000000
--- a/examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/flatbuffer-tf/layer_1.npy
+++ /dev/null
Binary files differ
diff --git a/examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/flatbuffer-tf/layer_2.npy b/examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/flatbuffer-tf/layer_2.npy
deleted file mode 100644
index ec9d526..0000000
--- a/examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/flatbuffer-tf/layer_2.npy
+++ /dev/null
Binary files differ
diff --git a/examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/flatbuffer-tf/layer_3.npy b/examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/flatbuffer-tf/layer_3.npy
deleted file mode 100644
index 79bf0ec..0000000
--- a/examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/flatbuffer-tf/layer_3.npy
+++ /dev/null
Binary files differ
diff --git a/examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/flatbuffer-tf/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11.tosa b/examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/flatbuffer-tf/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11.tosa
index 0204756..71564f2 100644
--- a/examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/flatbuffer-tf/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11.tosa
+++ b/examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/flatbuffer-tf/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11.tosa
Binary files differ
diff --git a/examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/flatbuffer-tflite/desc.json b/examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/flatbuffer-tflite/desc.json
index 2849934..e59bac3 100644
--- a/examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/flatbuffer-tflite/desc.json
+++ b/examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/flatbuffer-tflite/desc.json
@@ -1,13 +1,13 @@
{
"tosa_file": "test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11.tosa",
"ifm_name": [
- "InputTensor-tflite0"
+ "TosaInput_0"
],
"ifm_file": [
"../placeholder_0.npy"
],
"ofm_name": [
- "Result0"
+ "TosaOutput_0"
],
"ofm_file": [
"ref_model_Result0.npy"
diff --git a/examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/flatbuffer-tflite/layer_1.npy b/examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/flatbuffer-tflite/layer_1.npy
deleted file mode 100644
index ec9d526..0000000
--- a/examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/flatbuffer-tflite/layer_1.npy
+++ /dev/null
Binary files differ
diff --git a/examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/flatbuffer-tflite/layer_2.npy b/examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/flatbuffer-tflite/layer_2.npy
deleted file mode 100644
index 207be76..0000000
--- a/examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/flatbuffer-tflite/layer_2.npy
+++ /dev/null
Binary files differ
diff --git a/examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/flatbuffer-tflite/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11.tosa b/examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/flatbuffer-tflite/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11.tosa
index f549d12..0dc565a 100644
--- a/examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/flatbuffer-tflite/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11.tosa
+++ b/examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/flatbuffer-tflite/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11.tosa
Binary files differ
diff --git a/examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/model.pb b/examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/model.pb
index 6e8a215..e4f47e2 100644
--- a/examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/model.pb
+++ b/examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/model.pb
@@ -137,5 +137,5 @@ node {
}
}
versions {
- producer: 772
+ producer: 790
}
diff --git a/examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/model.tflite b/examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/model.tflite
index 8b5bdf6..02ad2fb 100644
--- a/examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/model.tflite
+++ b/examples/test_conv2d_1x1_1x32x32x8_f32_st11_padSAME_dilat11/model.tflite
Binary files differ
diff --git a/examples/test_conv2d_1x1_1x32x32x8_qi8_st11_padSAME_dilat11/flatbuffer-tflite/desc.json b/examples/test_conv2d_1x1_1x32x32x8_qi8_st11_padSAME_dilat11/flatbuffer-tflite/desc.json
index c935e0a..360394e 100644
--- a/examples/test_conv2d_1x1_1x32x32x8_qi8_st11_padSAME_dilat11/flatbuffer-tflite/desc.json
+++ b/examples/test_conv2d_1x1_1x32x32x8_qi8_st11_padSAME_dilat11/flatbuffer-tflite/desc.json
@@ -1,13 +1,13 @@
{
"tosa_file": "test_conv2d_1x1_1x32x32x8_qi8_st11_padSAME_dilat11.tosa",
"ifm_name": [
- "InputTensor-tflite0"
+ "TosaInput_0"
],
"ifm_file": [
"../placeholder_0.npy"
],
"ofm_name": [
- "Result0"
+ "TosaOutput_0"
],
"ofm_file": [
"ref_model_Result0.npy"
diff --git a/examples/test_conv2d_1x1_1x32x32x8_qi8_st11_padSAME_dilat11/flatbuffer-tflite/layer_1.npy b/examples/test_conv2d_1x1_1x32x32x8_qi8_st11_padSAME_dilat11/flatbuffer-tflite/layer_1.npy
deleted file mode 100644
index 67c2421..0000000
--- a/examples/test_conv2d_1x1_1x32x32x8_qi8_st11_padSAME_dilat11/flatbuffer-tflite/layer_1.npy
+++ /dev/null
Binary files differ
diff --git a/examples/test_conv2d_1x1_1x32x32x8_qi8_st11_padSAME_dilat11/flatbuffer-tflite/layer_2.npy b/examples/test_conv2d_1x1_1x32x32x8_qi8_st11_padSAME_dilat11/flatbuffer-tflite/layer_2.npy
deleted file mode 100644
index 7494934..0000000
--- a/examples/test_conv2d_1x1_1x32x32x8_qi8_st11_padSAME_dilat11/flatbuffer-tflite/layer_2.npy
+++ /dev/null
Binary files differ
diff --git a/examples/test_conv2d_1x1_1x32x32x8_qi8_st11_padSAME_dilat11/flatbuffer-tflite/test_conv2d_1x1_1x32x32x8_qi8_st11_padSAME_dilat11.tosa b/examples/test_conv2d_1x1_1x32x32x8_qi8_st11_padSAME_dilat11/flatbuffer-tflite/test_conv2d_1x1_1x32x32x8_qi8_st11_padSAME_dilat11.tosa
index 248dace..600590c 100644
--- a/examples/test_conv2d_1x1_1x32x32x8_qi8_st11_padSAME_dilat11/flatbuffer-tflite/test_conv2d_1x1_1x32x32x8_qi8_st11_padSAME_dilat11.tosa
+++ b/examples/test_conv2d_1x1_1x32x32x8_qi8_st11_padSAME_dilat11/flatbuffer-tflite/test_conv2d_1x1_1x32x32x8_qi8_st11_padSAME_dilat11.tosa
Binary files differ
diff --git a/examples/test_conv2d_1x1_1x32x32x8_qi8_st11_padSAME_dilat11/model.tflite b/examples/test_conv2d_1x1_1x32x32x8_qi8_st11_padSAME_dilat11/model.tflite
index 840fdb9..93dd386 100644
--- a/examples/test_conv2d_1x1_1x32x32x8_qi8_st11_padSAME_dilat11/model.tflite
+++ b/examples/test_conv2d_1x1_1x32x32x8_qi8_st11_padSAME_dilat11/model.tflite
Binary files differ
diff --git a/reference_model/src/subgraph_traverser.cc b/reference_model/src/subgraph_traverser.cc
index b615feb..d64cb38 100644
--- a/reference_model/src/subgraph_traverser.cc
+++ b/reference_model/src/subgraph_traverser.cc
@@ -98,7 +98,6 @@ TosaReference::Tensor* SubgraphTraverser::getOutputTensorByName(const std::strin
int SubgraphTraverser::initializeGraph()
{
- char tensor_fullname[1000];
int idx = 0;
for (auto op : block->GetOperators())
{
@@ -227,20 +226,70 @@ int SubgraphTraverser::initializeGraph()
TosaReference::Tensor* tensor =
TensorFactory::newTensor(ts->GetName(), ts->GetDtype(), ts->GetShape(), ts->GetShape().size());
- if (!ts->GetNpyFilePtr().empty())
+ if (!ts->GetData().empty())
{
if (tensor->allocate())
{
- FATAL_ERROR("Fail to allocate Eigen tensor %s", tensor->getName().c_str());
+ WARNING("Fail to allocate tensor %s", tensor->getName().c_str());
+ return 1;
}
- bzero(tensor_fullname, sizeof(tensor_fullname));
- snprintf(tensor_fullname, sizeof(tensor_fullname), "%s/%s", g_func_config.flatbuffer_dir,
- ts->GetNpyFilePtr().c_str());
- if (tensor->readFromNpyFile(tensor_fullname))
+ switch (ts->GetDtype())
{
- FATAL_ERROR("Cannot read input data into graph tensor %s from block %s", tensor->getName().c_str(),
- block->GetName().c_str());
+ case DType_INT8:
+ {
+ std::vector<int8_t> i8_data;
+ TosaSerializationHandler::ConvertU8toI8(ts->GetData(), tensor->getElementCount(), i8_data);
+ std::vector<int32_t> i32_data(i8_data.begin(), i8_data.end());
+ tensor->setTensorValueInt32(i32_data.size(), i32_data.data());
+ }
+ break;
+ case DType_INT16:
+ {
+ std::vector<int16_t> i16_data;
+ TosaSerializationHandler::ConvertU8toI16(ts->GetData(), tensor->getElementCount(), i16_data);
+ std::vector<int32_t> i32_data(i16_data.begin(), i16_data.end());
+ tensor->setTensorValueInt32(i32_data.size(), i32_data.data());
+ }
+ break;
+ case DType_INT32:
+ {
+ std::vector<int32_t> i32_data;
+ TosaSerializationHandler::ConvertU8toI32(ts->GetData(), tensor->getElementCount(), i32_data);
+ tensor->setTensorValueInt32(i32_data.size(), i32_data.data());
+ }
+ break;
+ case DType_INT48:
+ {
+ std::vector<int64_t> i64_data;
+ TosaSerializationHandler::ConvertU8toI48(ts->GetData(), tensor->getElementCount(), i64_data);
+ tensor->setTensorValueInt64(i64_data.size(), i64_data.data());
+ }
+ break;
+ case DType_FLOAT:
+ {
+ std::vector<float> fp32_data;
+ TosaSerializationHandler::ConvertU8toF32(ts->GetData(), tensor->getElementCount(), fp32_data);
+ tensor->setTensorValueFloat(fp32_data.size(), fp32_data.data());
+ }
+ break;
+ case DType_BOOL:
+ {
+ std::vector<bool> bool_data;
+ TosaSerializationHandler::ConvertU8toBool(ts->GetData(), tensor->getElementCount(), bool_data);
+
+ // std::vector<bool>::data() will return bit mask instead of array of bool array.
+ // Need to translate manually.
+ bool* bool_array = (bool*)calloc(bool_data.size(), sizeof(bool));
+ for (size_t i = 0; i < bool_data.size(); i++)
+ {
+ bool_array[i] = bool_data[i];
+ }
+ tensor->setTensorValueBool(bool_data.size(), bool_array);
+ }
+ break;
+ default:
+ FATAL_ERROR("Unsupported tensor type %s.", EnumNamesDType()[ts->GetDtype()]);
}
}
diff --git a/thirdparty/serialization_lib b/thirdparty/serialization_lib
-Subproject a8b4eafda31fe41b99a46c09c131ac729538257
+Subproject 3bb1bc1189bfa14670d2f7839d708f443f5b594
diff --git a/verif/tosa_serializer.py b/verif/tosa_serializer.py
index 3b03252..c4de2a2 100644
--- a/verif/tosa_serializer.py
+++ b/verif/tosa_serializer.py
@@ -19,6 +19,7 @@ import sys
import json
import flatbuffers
import numpy as np
+import struct
from enum import Enum, IntEnum, unique
from tosa import (
TosaGraph,
@@ -41,6 +42,7 @@ import tosa
# With the way flatc generates its python types, there is no programatic way
# to get string names for the integer types. Manually maintain a string table
# here.
+DType = tosa.DType.DType()
DTypeNames = [
"UNKNOWN",
"BOOL",
@@ -53,6 +55,7 @@ DTypeNames = [
"FLOAT",
]
+ByteMask = np.uint64(0xFF)
def dtype_str_to_val(name):
@@ -337,7 +340,7 @@ class TosaSerializerTensor:
name,
shape,
dtype,
- filename=None,
+ data=None,
placeholderFilename=None,
):
self.name = name
@@ -349,8 +352,12 @@ class TosaSerializerTensor:
self.shape = shape
self.dtype = dtype
- # Filename for const tensors. This gets written to the .tosa serialization
- self.filename = filename
+ if isinstance(data, np.ndarray):
+ data = data.flatten().astype(int).tolist()
+ data = list(map(int, data))
+ self.data = data
+ else:
+ self.data = None
# Filename for placeholder tensors. These get generated by the test generation
# process and are written to disk, but are considered input tensors by the network
@@ -359,11 +366,10 @@ class TosaSerializerTensor:
self.placeholderFilename = placeholderFilename
def __str__(self):
- str = "TosaSerializerTensor name: {} shape: {} dtype: {} filename: {}".format(
+ str = "TosaSerializerTensor name: {} shape: {} dtype: {}".format(
self.name,
self.shape,
DTypeNames[self.dtype],
- self.filename,
)
return str
@@ -372,16 +378,56 @@ class TosaSerializerTensor:
def serialize(self, builder):
fb_name = builder.CreateString(self.name)
- if self.filename:
- fb_filename = builder.CreateString(self.filename)
fb_shapes = TosaSerializer.serializeInt32Vec(builder, self.shape)
+ if self.data:
+ u8_data = list()
+ # little endianess
+ if self.dtype == DType.BOOL:
+ for val in self.data:
+ val_u8 = np.uint8(val)
+ u8_data.append(val_u8)
+ elif self.dtype == DType.INT8:
+ for val in self.data:
+ val_u8 = np.uint8(val)
+ u8_data.append(val_u8)
+ elif self.dtype == DType.INT16:
+ for val in self.data:
+ val_u16 = np.uint16(val)
+ b0 = val_u16 & ByteMask
+ b1 = (val_u16 >> np.uint16(8)) & ByteMask
+ u8_data.extend([b0, b1])
+ elif self.dtype == DType.INT32:
+ for val in self.data:
+ val_u32 = np.uint32(val)
+ b0 = val_u32 & ByteMask
+ b1 = (val_u32 >> np.uint32(8)) & ByteMask
+ b2 = (val_u32 >> np.uint32(16)) & ByteMask
+ b3 = (val_u32 >> np.uint32(32)) & ByteMask
+ u8_data.extend([b0, b1, b2, b3])
+ elif self.dtype == DType.INT48:
+ for val in self.data:
+ val_u64 = np.uint64(val)
+ b0 = val_u64 & ByteMask
+ b1 = (val_u64 >> np.uint64(8)) & ByteMask
+ b2 = (val_u64 >> np.uint64(16)) & ByteMask
+ b3 = (val_u64 >> np.uint64(24)) & ByteMask
+ b4 = (val_u64 >> np.uint64(32)) & ByteMask
+ b5 = (val_u64 >> np.uint64(40)) & ByteMask
+ u8_data.extend([b0, b1, b2, b3, b4, b5])
+ elif self.dtype == DType.FLOAT:
+ for val in self.data:
+ b = struct.pack('!f', val)
+ u8_data.extend([b[3], b[2], b[1], b[0]])
+ else:
+ raise Exception("unsupported data type {}".format(DTypeNames[self.dtype]))
+ fb_data = TosaSerializer.serializeUint8Vec(builder, u8_data)
TosaTensor.TosaTensorStart(builder)
TosaTensor.TosaTensorAddName(builder, fb_name)
TosaTensor.TosaTensorAddShape(builder, fb_shapes)
TosaTensor.TosaTensorAddType(builder, self.dtype)
- if self.filename:
- TosaTensor.TosaTensorAddNpyFilename(builder, fb_filename)
+ if self.data:
+ TosaTensor.TosaTensorAddData(builder, fb_data)
return TosaTensor.TosaTensorEnd(builder)
@@ -448,7 +494,7 @@ class TosaSerializerBasicBlock:
name,
shape,
dtype,
- filename=None,
+ data=None,
placeholderFilename=None,
):
try:
@@ -456,7 +502,7 @@ class TosaSerializerBasicBlock:
tens = self.tensors[name]
except KeyError:
self.tensors[name] = TosaSerializerTensor(
- name, shape, dtype, filename, placeholderFilename
+ name, shape, dtype, data, placeholderFilename
)
return self.tensors[name]
@@ -562,12 +608,10 @@ class TosaSerializer:
filename = "{}.npy".format(name)
self.currInputIdx = self.currInputIdx + 1
- tens = self.currBasicBlock.addTensor(name, shape, dtype, filename)
+ tens = self.currBasicBlock.addTensor(name, shape, dtype, vals)
# Add the operator now
self.currBasicBlock.addOperator(tosa.Op.Op().CONST, [], name)
- if vals is not None:
- np.save(os.path.join(self.pathPrefix, filename), vals, False)
return tens
def addIntermediate(self, shape, dtype):
@@ -576,10 +620,9 @@ class TosaSerializer:
raise Exception("addTensor called without valid basic block")
name = "layer-{}".format(self.currLayerIdx)
- filename = None # No file, so no filename
self.currLayerIdx = self.currLayerIdx + 1
- tens = self.currBasicBlock.addTensor(name, shape, dtype, filename)
+ tens = self.currBasicBlock.addTensor(name, shape, dtype, None)
return tens
@@ -683,6 +726,13 @@ class TosaSerializer:
return builder.EndVector(len(fb_strs))
@staticmethod
+ def serializeUint8Vec(builder, vec):
+ builder.StartVector(1, len(vec), 8)
+ for v in vec[::-1]:
+ builder.PrependUint8(v)
+ return builder.EndVector(len(vec))
+
+ @staticmethod
def serializeInt32Vec(builder, vec):
builder.StartVector(4, len(vec), 4)
for v in vec[::-1]: