From 7bf462cd170c1013dd96da6f726735d7e4d835e4 Mon Sep 17 00:00:00 2001 From: icodo98 Date: Thu, 12 Sep 2024 12:19:25 +0900 Subject: [PATCH 1/6] [dredd] Introduce new rule for tensorshape Introduce new dredd-rule for check tensors shape. ONE-DCO-1.0-Signed-off-by: JuYoung Lee rsb98759@gmail.com --- compiler/circle-inspect/driver/Driver.cpp | 5 ++- compiler/circle-inspect/src/Dump.cpp | 31 +++++++++++++++++ compiler/circle-inspect/src/Dump.h | 9 +++++ compiler/dredd-rule-lib/rule-lib.sh | 17 ++++++++++ res/TensorFlowLiteRecipes/Pad_002/test.recipe | 33 +++++++++++++++++++ res/TensorFlowLiteRecipes/Pad_002/test.rule | 6 ++++ 6 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 res/TensorFlowLiteRecipes/Pad_002/test.recipe create mode 100644 res/TensorFlowLiteRecipes/Pad_002/test.rule diff --git a/compiler/circle-inspect/driver/Driver.cpp b/compiler/circle-inspect/driver/Driver.cpp index 6371261db87..465f4171824 100644 --- a/compiler/circle-inspect/driver/Driver.cpp +++ b/compiler/circle-inspect/driver/Driver.cpp @@ -37,6 +37,7 @@ int entry(int argc, char **argv) arser.add_argument("--constants").nargs(0).help("Dump constant tensors name"); arser.add_argument("--op_version").nargs(0).help("Dump versions of the operators in circle file"); arser.add_argument("--tensor_dtype").nargs(0).help("Dump dtype of tensors"); + arser.add_argument("--tensor_shape").nargs(0).help("Dump shape of tensors"); arser.add_argument("circle").help("Circle file to inspect"); try @@ -51,7 +52,7 @@ int entry(int argc, char **argv) } if (!arser["--operators"] && !arser["--conv2d_weight"] && !arser["--op_version"] && - !arser["--tensor_dtype"] && !arser["--constants"]) + !arser["--tensor_dtype"] && !arser["--constants"] && !arser["--tensor_shape"]) { std::cout << "At least one option must be specified" << std::endl; std::cout << arser; @@ -70,6 +71,8 @@ int entry(int argc, char **argv) dumps.push_back(std::make_unique()); if (arser["--constants"]) dumps.push_back(std::make_unique()); + if (arser["--tensor_shape"]) + dumps.push_back(std::make_unique()); std::string model_file = arser.get("circle"); diff --git a/compiler/circle-inspect/src/Dump.cpp b/compiler/circle-inspect/src/Dump.cpp index 9d363e1ffa9..aa1f0673274 100644 --- a/compiler/circle-inspect/src/Dump.cpp +++ b/compiler/circle-inspect/src/Dump.cpp @@ -240,3 +240,34 @@ void DumpConstants::run(std::ostream &os, const circle::Model *model, const std: } } // namespace circleinspect + +namespace circleinspect +{ + +void DumpTensorShape::run(std::ostream &os, const circle::Model *model, + const std::vector *data) +{ + mio::circle::Reader reader(model, data); + + const uint32_t subgraph_size = reader.num_subgraph(); + + for (uint32_t g = 0; g < subgraph_size; g++) + { + reader.select_subgraph(g); + auto tensors = reader.tensors(); + + for (uint32_t i = 0; i < tensors->size(); ++i) + { + const auto tensor = tensors->Get(i); + auto shape = tensor->shape_signature() ? tensor->shape_signature() : tensor->shape(); + os << reader.tensor_name(tensor) << " "; + for (int8_t i = 0; i < shape->size(); i++) + { + os << shape->Get(i); + } + os << std::endl; + } + } +} + +} // namespace circleinspect diff --git a/compiler/circle-inspect/src/Dump.h b/compiler/circle-inspect/src/Dump.h index 12a43a71001..4959adcf1db 100644 --- a/compiler/circle-inspect/src/Dump.h +++ b/compiler/circle-inspect/src/Dump.h @@ -78,6 +78,15 @@ class DumpConstants final : public DumpInterface void run(std::ostream &os, const circle::Model *model, const std::vector *data); }; +class DumpTensorShape final : public DumpInterface +{ +public: + DumpTensorShape() = default; + +public: + void run(std::ostream &os, const circle::Model *model, const std::vector *data); +}; + } // namespace circleinspect #endif // __DUMP_H__ diff --git a/compiler/dredd-rule-lib/rule-lib.sh b/compiler/dredd-rule-lib/rule-lib.sh index a920e08abc1..3ee94e6881b 100755 --- a/compiler/dredd-rule-lib/rule-lib.sh +++ b/compiler/dredd-rule-lib/rule-lib.sh @@ -252,4 +252,21 @@ const_count() echo ${ACTUAL} } +tensor_shape() +{ + argc_check $# 1 + file_path_check ${COMPILED_FILE} + file_path_check ${INSPECT_PROG_PATH} + + set -o pipefail + + ACTUAL=`init_error_log ; \ + ${INSPECT_PROG_PATH} --tensor_shape ${COMPILED_FILE} | \ + awk -v tensor_name="$1" '{ if ($1 == tensor_name) print $2}'` + + check_success_exit_code $? 0 + + echo ${ACTUAL} +} + # TODO define more qullity test function diff --git a/res/TensorFlowLiteRecipes/Pad_002/test.recipe b/res/TensorFlowLiteRecipes/Pad_002/test.recipe new file mode 100644 index 00000000000..3e4ba359bf1 --- /dev/null +++ b/res/TensorFlowLiteRecipes/Pad_002/test.recipe @@ -0,0 +1,33 @@ +# padding with dynamic shape, others same as Pad_000 +operand { + name: "ifm" + type: FLOAT32 + shape { dim: 1 dim: 3 dim: 3 dim: 2 } + shape_signature { dim: -1 dim: 3 dim: 3 dim: 2 } +} +operand { + name: "padding" + type: INT64 + shape { dim: 4 dim: 2 } + filler { + tag: "explicit" + arg: "0" arg: "0" + arg: "1" arg: "1" + arg: "2" arg: "2" + arg: "0" arg: "0" + } +} +operand { + name: "ofm" + type: FLOAT32 + shape { dim: 1 dim: 5 dim: 7 dim: 2 } + shape_signature { dim: -1 dim: 5 dim: 7 dim: 2 } +} +operation { + type: "Pad" + input: "ifm" + input: "padding" + output: "ofm" +} +input: "ifm" +output: "ofm" diff --git a/res/TensorFlowLiteRecipes/Pad_002/test.rule b/res/TensorFlowLiteRecipes/Pad_002/test.rule new file mode 100644 index 00000000000..08d53a1d691 --- /dev/null +++ b/res/TensorFlowLiteRecipes/Pad_002/test.rule @@ -0,0 +1,6 @@ +# To check if dynamic dimension properly infered + +RULE "VERIFY_FILE_FORMAT" $(verify_file_format) '=' 1 + +RULE "PAD_EXIST" $(op_count PAD) '=' 1 +RULE "PAD_SHAPE" $(tensor_shape ofm) '=' -1572 From 217822778f4962e8419f700d8dde15cc90e48a45 Mon Sep 17 00:00:00 2001 From: icodo98 Date: Mon, 23 Sep 2024 12:14:58 +0900 Subject: [PATCH 2/6] [dredd] add comma ONE-DCO-1.0-Signed-off-by: JuYoung Lee rsb98759@gmail.com --- compiler/circle-inspect/src/Dump.cpp | 4 ++++ compiler/circle2circle-dredd-recipe-test/test.lst | 1 + res/TensorFlowLiteRecipes/Pad_002/test.recipe | 8 ++++---- res/TensorFlowLiteRecipes/Pad_002/test.rule | 2 +- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/compiler/circle-inspect/src/Dump.cpp b/compiler/circle-inspect/src/Dump.cpp index aa1f0673274..5bd18ea31d5 100644 --- a/compiler/circle-inspect/src/Dump.cpp +++ b/compiler/circle-inspect/src/Dump.cpp @@ -264,6 +264,10 @@ void DumpTensorShape::run(std::ostream &os, const circle::Model *model, for (int8_t i = 0; i < shape->size(); i++) { os << shape->Get(i); + if (i != shape->size() - 1) + { + os << ","; + } } os << std::endl; } diff --git a/compiler/circle2circle-dredd-recipe-test/test.lst b/compiler/circle2circle-dredd-recipe-test/test.lst index 0a8d893c43b..6059effc528 100644 --- a/compiler/circle2circle-dredd-recipe-test/test.lst +++ b/compiler/circle2circle-dredd-recipe-test/test.lst @@ -96,6 +96,7 @@ Add(PadV2_001 PASS substitute_padv2_to_pad) Add(Softmax_001 PASS decompose_softmax) Add(Softmax_002 PASS decompose_softmax) Add(StridedSlice_003 PASS substitute_strided_slice_to_reshape) +Add(Pad_002 PASS) # CSE test diff --git a/res/TensorFlowLiteRecipes/Pad_002/test.recipe b/res/TensorFlowLiteRecipes/Pad_002/test.recipe index 3e4ba359bf1..f22f13510c0 100644 --- a/res/TensorFlowLiteRecipes/Pad_002/test.recipe +++ b/res/TensorFlowLiteRecipes/Pad_002/test.recipe @@ -2,8 +2,8 @@ operand { name: "ifm" type: FLOAT32 - shape { dim: 1 dim: 3 dim: 3 dim: 2 } - shape_signature { dim: -1 dim: 3 dim: 3 dim: 2 } + shape { dim: 1 dim: 1 dim: 3 dim: 2 } + shape_signature { dim: 1 dim: -1 dim: 3 dim: 2 } } operand { name: "padding" @@ -20,8 +20,8 @@ operand { operand { name: "ofm" type: FLOAT32 - shape { dim: 1 dim: 5 dim: 7 dim: 2 } - shape_signature { dim: -1 dim: 5 dim: 7 dim: 2 } + shape { dim: 1 dim: 1 dim: 7 dim: 2 } + shape_signature { dim: 1 dim: -1 dim: 7 dim: 2 } } operation { type: "Pad" diff --git a/res/TensorFlowLiteRecipes/Pad_002/test.rule b/res/TensorFlowLiteRecipes/Pad_002/test.rule index 08d53a1d691..62625045543 100644 --- a/res/TensorFlowLiteRecipes/Pad_002/test.rule +++ b/res/TensorFlowLiteRecipes/Pad_002/test.rule @@ -3,4 +3,4 @@ RULE "VERIFY_FILE_FORMAT" $(verify_file_format) '=' 1 RULE "PAD_EXIST" $(op_count PAD) '=' 1 -RULE "PAD_SHAPE" $(tensor_shape ofm) '=' -1572 +RULE "PAD_SHAPE" $(tensor_shape ofm) '=' 1,-1,7,2 From 98cd1977e79a3625143a02300610c4dfe4135c55 Mon Sep 17 00:00:00 2001 From: icodo98 Date: Tue, 24 Sep 2024 15:52:07 +0900 Subject: [PATCH 3/6] [dredd] add prefix Inf_ to recipe ONE-DCO-1.0-Signed-off-by: JuYoung Lee rsb98759@gmail.com --- compiler/circle2circle-dredd-recipe-test/test.lst | 2 +- res/TensorFlowLiteRecipes/{Pad_002 => Inf_Pad_000}/test.recipe | 0 res/TensorFlowLiteRecipes/{Pad_002 => Inf_Pad_000}/test.rule | 0 3 files changed, 1 insertion(+), 1 deletion(-) rename res/TensorFlowLiteRecipes/{Pad_002 => Inf_Pad_000}/test.recipe (100%) rename res/TensorFlowLiteRecipes/{Pad_002 => Inf_Pad_000}/test.rule (100%) diff --git a/compiler/circle2circle-dredd-recipe-test/test.lst b/compiler/circle2circle-dredd-recipe-test/test.lst index 6059effc528..b1a3c727e6f 100644 --- a/compiler/circle2circle-dredd-recipe-test/test.lst +++ b/compiler/circle2circle-dredd-recipe-test/test.lst @@ -96,7 +96,7 @@ Add(PadV2_001 PASS substitute_padv2_to_pad) Add(Softmax_001 PASS decompose_softmax) Add(Softmax_002 PASS decompose_softmax) Add(StridedSlice_003 PASS substitute_strided_slice_to_reshape) -Add(Pad_002 PASS) +Add(Inf_Pad_000 PASS) # CSE test diff --git a/res/TensorFlowLiteRecipes/Pad_002/test.recipe b/res/TensorFlowLiteRecipes/Inf_Pad_000/test.recipe similarity index 100% rename from res/TensorFlowLiteRecipes/Pad_002/test.recipe rename to res/TensorFlowLiteRecipes/Inf_Pad_000/test.recipe diff --git a/res/TensorFlowLiteRecipes/Pad_002/test.rule b/res/TensorFlowLiteRecipes/Inf_Pad_000/test.rule similarity index 100% rename from res/TensorFlowLiteRecipes/Pad_002/test.rule rename to res/TensorFlowLiteRecipes/Inf_Pad_000/test.rule From 924280309f86a0cd6953a60c1c75080279113a20 Mon Sep 17 00:00:00 2001 From: icodo98 Date: Wed, 25 Sep 2024 09:39:44 +0900 Subject: [PATCH 4/6] [dredd] apply suggestions change rule shape to be enclosed in brackets change type to uint32_t seperate inference test list ONE-DCO-1.0-Signed-off-by: JuYoung Lee rsb98759@gmail.com --- compiler/circle-inspect/src/Dump.cpp | 6 +++--- compiler/circle2circle-dredd-recipe-test/test.lst | 4 +++- res/TensorFlowLiteRecipes/Inf_Pad_000/test.rule | 4 ++-- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/compiler/circle-inspect/src/Dump.cpp b/compiler/circle-inspect/src/Dump.cpp index 5bd18ea31d5..08238736be9 100644 --- a/compiler/circle-inspect/src/Dump.cpp +++ b/compiler/circle-inspect/src/Dump.cpp @@ -260,8 +260,8 @@ void DumpTensorShape::run(std::ostream &os, const circle::Model *model, { const auto tensor = tensors->Get(i); auto shape = tensor->shape_signature() ? tensor->shape_signature() : tensor->shape(); - os << reader.tensor_name(tensor) << " "; - for (int8_t i = 0; i < shape->size(); i++) + os << reader.tensor_name(tensor) << " ["; + for (uint32_t i = 0; i < shape->size(); i++) { os << shape->Get(i); if (i != shape->size() - 1) @@ -269,7 +269,7 @@ void DumpTensorShape::run(std::ostream &os, const circle::Model *model, os << ","; } } - os << std::endl; + os << "]" << std::endl; } } } diff --git a/compiler/circle2circle-dredd-recipe-test/test.lst b/compiler/circle2circle-dredd-recipe-test/test.lst index b1a3c727e6f..0f30bb59c98 100644 --- a/compiler/circle2circle-dredd-recipe-test/test.lst +++ b/compiler/circle2circle-dredd-recipe-test/test.lst @@ -96,7 +96,6 @@ Add(PadV2_001 PASS substitute_padv2_to_pad) Add(Softmax_001 PASS decompose_softmax) Add(Softmax_002 PASS decompose_softmax) Add(StridedSlice_003 PASS substitute_strided_slice_to_reshape) -Add(Inf_Pad_000 PASS) # CSE test @@ -142,3 +141,6 @@ Add(REGRESS_ONNX_Conv_BN_Relu6_001 PASS Add(REGRESS_ONNX_Mul_Mul_000 PASS convert_nchw_to_nhwc) + +# SHAPE INFERENCE test +Add(Inf_Pad_000 PASS) diff --git a/res/TensorFlowLiteRecipes/Inf_Pad_000/test.rule b/res/TensorFlowLiteRecipes/Inf_Pad_000/test.rule index 62625045543..71c1536f2e1 100644 --- a/res/TensorFlowLiteRecipes/Inf_Pad_000/test.rule +++ b/res/TensorFlowLiteRecipes/Inf_Pad_000/test.rule @@ -1,6 +1,6 @@ -# To check if dynamic dimension properly infered +# To check if dynamic dimension properly inferred RULE "VERIFY_FILE_FORMAT" $(verify_file_format) '=' 1 RULE "PAD_EXIST" $(op_count PAD) '=' 1 -RULE "PAD_SHAPE" $(tensor_shape ofm) '=' 1,-1,7,2 +RULE "PAD_SHAPE" $(tensor_shape ofm) '=' [1,-1,7,2] From 2354adc3fc444cb3e942c96c8c7a173f24e55a8e Mon Sep 17 00:00:00 2001 From: sunki Date: Wed, 25 Sep 2024 11:41:07 +0900 Subject: [PATCH 5/6] [docs] Update dredd-rule-lib README ONE-DCO-1.0-Signed-off-by: sunki --- compiler/dredd-rule-lib/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/compiler/dredd-rule-lib/README.md b/compiler/dredd-rule-lib/README.md index 348b0aefba9..4f51aa84258 100644 --- a/compiler/dredd-rule-lib/README.md +++ b/compiler/dredd-rule-lib/README.md @@ -21,6 +21,7 @@ Models (input of test) exist in *model repo*, where The following metric functions are provided: - `all_op_count` : the count of operations inside a compiled tflite file - `file_size` : the size of compiled tflite file +- `tensor_shape` : The shape of a specific node in a compiled tflite file. The format looks like `[1,-1,7,2]`(without spaces). - In addition, `op_count`, `conv2d_weight_not_constant`, etc. - Please , refer to [`rule-lib.sh`](rule-lib.sh) for metric functions From a551138e3dc3dbd97094345c3756d91563923066 Mon Sep 17 00:00:00 2001 From: icodo98 Date: Wed, 25 Sep 2024 13:18:46 +0900 Subject: [PATCH 6/6] [dredd] fix rule remove unneccesary rule for shape inference. ONE-DCO-1.0-Signed-off-by: JuYoung Lee rsb98759@gmail.com --- res/TensorFlowLiteRecipes/Inf_Pad_000/test.rule | 1 - 1 file changed, 1 deletion(-) diff --git a/res/TensorFlowLiteRecipes/Inf_Pad_000/test.rule b/res/TensorFlowLiteRecipes/Inf_Pad_000/test.rule index 71c1536f2e1..dce0a7d5b77 100644 --- a/res/TensorFlowLiteRecipes/Inf_Pad_000/test.rule +++ b/res/TensorFlowLiteRecipes/Inf_Pad_000/test.rule @@ -2,5 +2,4 @@ RULE "VERIFY_FILE_FORMAT" $(verify_file_format) '=' 1 -RULE "PAD_EXIST" $(op_count PAD) '=' 1 RULE "PAD_SHAPE" $(tensor_shape ofm) '=' [1,-1,7,2]