From 27a2acc5da51ccf8d042a4134d7a7c02aed3681e Mon Sep 17 00:00:00 2001 From: SeungHui Lee Date: Mon, 30 Oct 2023 01:18:52 -0400 Subject: [PATCH] [tflchef] Introduce a op util function to check custom codes This introduces a function that check codes of CustomOp to avoid duplication. ONE-DCO-1.0-Signed-off-by: SeungHui Lee --- compiler/tflchef/core/src/CustomOp/AddV2.cpp | 3 +- compiler/tflchef/core/src/CustomOp/All.cpp | 3 +- .../core/src/CustomOp/BatchMatMulV2.cpp | 3 +- .../tflchef/core/src/CustomOp/BroadcastTo.cpp | 3 +- compiler/tflchef/core/src/CustomOp/Erf.cpp | 3 +- compiler/tflchef/core/src/CustomOp/MatMul.cpp | 3 +- .../core/src/CustomOp/MatrixBandPart.cpp | 3 +- .../core/src/CustomOp/MaxPoolWithArgmax.cpp | 3 +- compiler/tflchef/core/src/OpUtils.cpp | 29 +++++++++++++++++++ compiler/tflchef/core/src/OpUtils.h | 28 ++++++++++++++++++ 10 files changed, 73 insertions(+), 8 deletions(-) create mode 100644 compiler/tflchef/core/src/OpUtils.cpp create mode 100644 compiler/tflchef/core/src/OpUtils.h diff --git a/compiler/tflchef/core/src/CustomOp/AddV2.cpp b/compiler/tflchef/core/src/CustomOp/AddV2.cpp index 557c20bce11..f3d0b5e9472 100644 --- a/compiler/tflchef/core/src/CustomOp/AddV2.cpp +++ b/compiler/tflchef/core/src/CustomOp/AddV2.cpp @@ -16,6 +16,7 @@ */ #include "AddV2.h" +#include "OpUtils.h" #include @@ -29,7 +30,7 @@ AddV2Chef::custom_value(flatbuffers::FlatBufferBuilder &fbb) const { auto &operation = (*_operation); - assert(operation.type() == "AddV2"); + check_custom_op_value(operation, "AddV2"); /** * REGISTER_OP("AddV2") diff --git a/compiler/tflchef/core/src/CustomOp/All.cpp b/compiler/tflchef/core/src/CustomOp/All.cpp index bbef5ecaa37..4af447dc338 100644 --- a/compiler/tflchef/core/src/CustomOp/All.cpp +++ b/compiler/tflchef/core/src/CustomOp/All.cpp @@ -16,6 +16,7 @@ */ #include "All.h" +#include "OpUtils.h" #include @@ -29,7 +30,7 @@ AllChef::custom_value(flatbuffers::FlatBufferBuilder &fbb) const { auto &operation = (*_operation); - assert(operation.type() == "All"); + check_custom_op_value(operation, "All"); /** * REGISTER_OP("All") diff --git a/compiler/tflchef/core/src/CustomOp/BatchMatMulV2.cpp b/compiler/tflchef/core/src/CustomOp/BatchMatMulV2.cpp index 6d2c5b13b61..f7ce49035a1 100644 --- a/compiler/tflchef/core/src/CustomOp/BatchMatMulV2.cpp +++ b/compiler/tflchef/core/src/CustomOp/BatchMatMulV2.cpp @@ -16,6 +16,7 @@ */ #include "BatchMatMulV2.h" +#include "OpUtils.h" #include @@ -29,7 +30,7 @@ BatchMatMulV2Chef::custom_value(flatbuffers::FlatBufferBuilder &fbb) const { auto &operation = (*_operation); - assert(operation.type() == "BatchMatMulV2"); + check_custom_op_value(operation, "BatchMatMulV2"); /** * REGISTER_OP("BatchMatMulV2") diff --git a/compiler/tflchef/core/src/CustomOp/BroadcastTo.cpp b/compiler/tflchef/core/src/CustomOp/BroadcastTo.cpp index dd458b376c7..4ce82077d72 100644 --- a/compiler/tflchef/core/src/CustomOp/BroadcastTo.cpp +++ b/compiler/tflchef/core/src/CustomOp/BroadcastTo.cpp @@ -16,6 +16,7 @@ */ #include "BroadcastTo.h" +#include "OpUtils.h" #include @@ -29,7 +30,7 @@ BroadcastToChef::custom_value(flatbuffers::FlatBufferBuilder &fbb) const { auto &operation = (*_operation); - assert(operation.type() == "BroadcastTo"); + check_custom_op_value(operation, "BroadcastTo"); /** * REGISTER_OP("BroadcastTo") diff --git a/compiler/tflchef/core/src/CustomOp/Erf.cpp b/compiler/tflchef/core/src/CustomOp/Erf.cpp index f611b68e1dc..ac2e9b3136f 100644 --- a/compiler/tflchef/core/src/CustomOp/Erf.cpp +++ b/compiler/tflchef/core/src/CustomOp/Erf.cpp @@ -16,6 +16,7 @@ */ #include "Erf.h" +#include "OpUtils.h" #include @@ -29,7 +30,7 @@ ErfChef::custom_value(flatbuffers::FlatBufferBuilder &fbb) const { auto &operation = (*_operation); - assert(operation.type() == "Erf"); + check_custom_op_value(operation, "Erf"); /** * REGISTER_OP("Erf") diff --git a/compiler/tflchef/core/src/CustomOp/MatMul.cpp b/compiler/tflchef/core/src/CustomOp/MatMul.cpp index e7c707d3722..d8282f77f1f 100644 --- a/compiler/tflchef/core/src/CustomOp/MatMul.cpp +++ b/compiler/tflchef/core/src/CustomOp/MatMul.cpp @@ -16,6 +16,7 @@ */ #include "MatMul.h" +#include "OpUtils.h" #include @@ -29,7 +30,7 @@ MatMulChef::custom_value(flatbuffers::FlatBufferBuilder &fbb) const { auto &operation = (*_operation); - assert(operation.type() == "MatMul"); + check_custom_op_value(operation, "MatMul"); /** * REGISTER_OP("MatMul") diff --git a/compiler/tflchef/core/src/CustomOp/MatrixBandPart.cpp b/compiler/tflchef/core/src/CustomOp/MatrixBandPart.cpp index b2500322789..1c7987a1972 100644 --- a/compiler/tflchef/core/src/CustomOp/MatrixBandPart.cpp +++ b/compiler/tflchef/core/src/CustomOp/MatrixBandPart.cpp @@ -16,6 +16,7 @@ */ #include "MatrixBandPart.h" +#include "OpUtils.h" #include @@ -29,7 +30,7 @@ MatrixBandPartChef::custom_value(flatbuffers::FlatBufferBuilder &fbb) const { auto &operation = (*_operation); - assert(operation.type() == "MatrixBandPart"); + check_custom_op_value(operation, "MatrixBandPart"); /** * REGISTER_OP("MatrixBandPart") diff --git a/compiler/tflchef/core/src/CustomOp/MaxPoolWithArgmax.cpp b/compiler/tflchef/core/src/CustomOp/MaxPoolWithArgmax.cpp index 290d3c2cace..64c1a6c24d4 100644 --- a/compiler/tflchef/core/src/CustomOp/MaxPoolWithArgmax.cpp +++ b/compiler/tflchef/core/src/CustomOp/MaxPoolWithArgmax.cpp @@ -16,6 +16,7 @@ */ #include "MaxPoolWithArgmax.h" +#include "OpUtils.h" #include @@ -29,7 +30,7 @@ MaxPoolWithArgmaxChef::custom_value(flatbuffers::FlatBufferBuilder &fbb) const { auto &operation = (*_operation); - assert(operation.type() == "MaxPoolWithArgmax"); + check_custom_op_value(operation, "MaxPoolWithArgmax"); /** * REGISTER_OP("MaxPoolWithArgmax") diff --git a/compiler/tflchef/core/src/OpUtils.cpp b/compiler/tflchef/core/src/OpUtils.cpp new file mode 100644 index 00000000000..008f37fb1f0 --- /dev/null +++ b/compiler/tflchef/core/src/OpUtils.cpp @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "OpUtils.h" + +#include + +void check_custom_op_value(const tflchef::Operation operation, std::string op_type) +{ + if ((operation.has_extype() && operation.extype() == "Custom") || + (operation.has_type() && operation.type() == "Custom")) + { + assert(operation.has_custom_code()); + assert(operation.custom_code() == op_type); + } +} diff --git a/compiler/tflchef/core/src/OpUtils.h b/compiler/tflchef/core/src/OpUtils.h new file mode 100644 index 00000000000..f0b1df09064 --- /dev/null +++ b/compiler/tflchef/core/src/OpUtils.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file OpUtils.h + * @brief This header declares various op_utils functions + */ +#ifndef __OPUTILS_H__ +#define __OPUTILS_H__ + +#include + +void check_custom_op_value(const tflchef::Operation operation, std::string op_type); + +#endif // __OPUTILS_H__