From 8eb05a8d4cbe7ad8024be9dcecfb53eaefa38a28 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=84=B8=ED=9D=AC/On-Device=20Lab=28SR=29/Princip?= =?utf8?q?al=20Engineer/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Mon, 21 Oct 2019 14:26:59 +0900 Subject: [PATCH] [moco] Import Nodes (#8336) This will add Import GraphBuilder Nodes to import TensorFlow NodeDef Signed-off-by: SaeHie Park --- compiler/moco/import/include/moco/Import/Nodes.h | 49 ++++++ .../moco/import/include/moco/Import/Nodes/Add.h | 37 +++++ .../import/include/moco/Import/Nodes/AvgPool.h | 34 ++++ .../import/include/moco/Import/Nodes/BiasAdd.h | 34 ++++ .../moco/import/include/moco/Import/Nodes/Concat.h | 34 ++++ .../moco/import/include/moco/Import/Nodes/Const.h | 34 ++++ .../moco/import/include/moco/Import/Nodes/Conv2D.h | 34 ++++ .../moco/Import/Nodes/Conv2DBackpropInput.h | 37 +++++ .../moco/Import/Nodes/DepthwiseConv2dNative.h | 37 +++++ .../include/moco/Import/Nodes/FusedBatchNorm.h | 37 +++++ .../import/include/moco/Import/Nodes/Identity.h | 34 ++++ .../import/include/moco/Import/Nodes/MaxPool.h | 34 ++++ .../moco/import/include/moco/Import/Nodes/Mean.h | 37 +++++ .../moco/import/include/moco/Import/Nodes/Mul.h | 37 +++++ .../moco/import/include/moco/Import/Nodes/Pad.h | 37 +++++ .../import/include/moco/Import/Nodes/Placeholder.h | 37 +++++ .../import/include/moco/Import/Nodes/RealDiv.h | 37 +++++ .../moco/import/include/moco/Import/Nodes/Relu.h | 37 +++++ .../moco/import/include/moco/Import/Nodes/Relu6.h | 37 +++++ .../import/include/moco/Import/Nodes/Reshape.h | 37 +++++ .../moco/import/include/moco/Import/Nodes/Rsqrt.h | 37 +++++ .../moco/import/include/moco/Import/Nodes/Shape.h | 37 +++++ .../import/include/moco/Import/Nodes/Softmax.h | 37 +++++ .../moco/import/include/moco/Import/Nodes/Sqrt.h | 37 +++++ .../include/moco/Import/Nodes/SquaredDifference.h | 37 +++++ .../import/include/moco/Import/Nodes/Squeeze.h | 37 +++++ .../include/moco/Import/Nodes/StopGradient.h | 37 +++++ .../moco/import/include/moco/Import/Nodes/Sub.h | 37 +++++ .../moco/import/include/moco/Import/Nodes/Tanh.h | 37 +++++ compiler/moco/import/src/Nodes/Add.cpp | 91 ++++++++++ compiler/moco/import/src/Nodes/Add.test.cpp | 58 +++++++ compiler/moco/import/src/Nodes/AvgPool.cpp | 136 +++++++++++++++ compiler/moco/import/src/Nodes/BiasAdd.cpp | 118 +++++++++++++ compiler/moco/import/src/Nodes/Concat.cpp | 114 +++++++++++++ compiler/moco/import/src/Nodes/Const.cpp | 184 +++++++++++++++++++++ compiler/moco/import/src/Nodes/Conv2D.cpp | 133 +++++++++++++++ .../moco/import/src/Nodes/Conv2DBackpropInput.cpp | 139 ++++++++++++++++ .../import/src/Nodes/DepthwiseConv2dNative.cpp | 139 ++++++++++++++++ compiler/moco/import/src/Nodes/FusedBatchNorm.cpp | 105 ++++++++++++ compiler/moco/import/src/Nodes/Identity.cpp | 98 +++++++++++ compiler/moco/import/src/Nodes/MaxPool.cpp | 130 +++++++++++++++ compiler/moco/import/src/Nodes/Mean.cpp | 102 ++++++++++++ compiler/moco/import/src/Nodes/Mul.cpp | 91 ++++++++++ compiler/moco/import/src/Nodes/Pad.cpp | 94 +++++++++++ compiler/moco/import/src/Nodes/Placeholder.cpp | 84 ++++++++++ compiler/moco/import/src/Nodes/RealDiv.cpp | 92 +++++++++++ compiler/moco/import/src/Nodes/Relu.cpp | 89 ++++++++++ compiler/moco/import/src/Nodes/Relu6.cpp | 83 ++++++++++ compiler/moco/import/src/Nodes/Reshape.cpp | 105 ++++++++++++ compiler/moco/import/src/Nodes/Rsqrt.cpp | 87 ++++++++++ compiler/moco/import/src/Nodes/Shape.cpp | 102 ++++++++++++ compiler/moco/import/src/Nodes/Softmax.cpp | 88 ++++++++++ compiler/moco/import/src/Nodes/Sqrt.cpp | 86 ++++++++++ .../moco/import/src/Nodes/SquaredDifference.cpp | 98 +++++++++++ compiler/moco/import/src/Nodes/Squeeze.cpp | 107 ++++++++++++ compiler/moco/import/src/Nodes/StopGradient.cpp | 89 ++++++++++ compiler/moco/import/src/Nodes/Sub.cpp | 91 ++++++++++ compiler/moco/import/src/Nodes/Tanh.cpp | 86 ++++++++++ 58 files changed, 4083 insertions(+) create mode 100644 compiler/moco/import/include/moco/Import/Nodes.h create mode 100644 compiler/moco/import/include/moco/Import/Nodes/Add.h create mode 100644 compiler/moco/import/include/moco/Import/Nodes/AvgPool.h create mode 100644 compiler/moco/import/include/moco/Import/Nodes/BiasAdd.h create mode 100644 compiler/moco/import/include/moco/Import/Nodes/Concat.h create mode 100644 compiler/moco/import/include/moco/Import/Nodes/Const.h create mode 100644 compiler/moco/import/include/moco/Import/Nodes/Conv2D.h create mode 100644 compiler/moco/import/include/moco/Import/Nodes/Conv2DBackpropInput.h create mode 100644 compiler/moco/import/include/moco/Import/Nodes/DepthwiseConv2dNative.h create mode 100644 compiler/moco/import/include/moco/Import/Nodes/FusedBatchNorm.h create mode 100644 compiler/moco/import/include/moco/Import/Nodes/Identity.h create mode 100644 compiler/moco/import/include/moco/Import/Nodes/MaxPool.h create mode 100644 compiler/moco/import/include/moco/Import/Nodes/Mean.h create mode 100644 compiler/moco/import/include/moco/Import/Nodes/Mul.h create mode 100644 compiler/moco/import/include/moco/Import/Nodes/Pad.h create mode 100644 compiler/moco/import/include/moco/Import/Nodes/Placeholder.h create mode 100644 compiler/moco/import/include/moco/Import/Nodes/RealDiv.h create mode 100644 compiler/moco/import/include/moco/Import/Nodes/Relu.h create mode 100644 compiler/moco/import/include/moco/Import/Nodes/Relu6.h create mode 100644 compiler/moco/import/include/moco/Import/Nodes/Reshape.h create mode 100644 compiler/moco/import/include/moco/Import/Nodes/Rsqrt.h create mode 100644 compiler/moco/import/include/moco/Import/Nodes/Shape.h create mode 100644 compiler/moco/import/include/moco/Import/Nodes/Softmax.h create mode 100644 compiler/moco/import/include/moco/Import/Nodes/Sqrt.h create mode 100644 compiler/moco/import/include/moco/Import/Nodes/SquaredDifference.h create mode 100644 compiler/moco/import/include/moco/Import/Nodes/Squeeze.h create mode 100644 compiler/moco/import/include/moco/Import/Nodes/StopGradient.h create mode 100644 compiler/moco/import/include/moco/Import/Nodes/Sub.h create mode 100644 compiler/moco/import/include/moco/Import/Nodes/Tanh.h create mode 100644 compiler/moco/import/src/Nodes/Add.cpp create mode 100644 compiler/moco/import/src/Nodes/Add.test.cpp create mode 100644 compiler/moco/import/src/Nodes/AvgPool.cpp create mode 100644 compiler/moco/import/src/Nodes/BiasAdd.cpp create mode 100644 compiler/moco/import/src/Nodes/Concat.cpp create mode 100644 compiler/moco/import/src/Nodes/Const.cpp create mode 100644 compiler/moco/import/src/Nodes/Conv2D.cpp create mode 100644 compiler/moco/import/src/Nodes/Conv2DBackpropInput.cpp create mode 100644 compiler/moco/import/src/Nodes/DepthwiseConv2dNative.cpp create mode 100644 compiler/moco/import/src/Nodes/FusedBatchNorm.cpp create mode 100644 compiler/moco/import/src/Nodes/Identity.cpp create mode 100644 compiler/moco/import/src/Nodes/MaxPool.cpp create mode 100644 compiler/moco/import/src/Nodes/Mean.cpp create mode 100644 compiler/moco/import/src/Nodes/Mul.cpp create mode 100644 compiler/moco/import/src/Nodes/Pad.cpp create mode 100644 compiler/moco/import/src/Nodes/Placeholder.cpp create mode 100644 compiler/moco/import/src/Nodes/RealDiv.cpp create mode 100644 compiler/moco/import/src/Nodes/Relu.cpp create mode 100644 compiler/moco/import/src/Nodes/Relu6.cpp create mode 100644 compiler/moco/import/src/Nodes/Reshape.cpp create mode 100644 compiler/moco/import/src/Nodes/Rsqrt.cpp create mode 100644 compiler/moco/import/src/Nodes/Shape.cpp create mode 100644 compiler/moco/import/src/Nodes/Softmax.cpp create mode 100644 compiler/moco/import/src/Nodes/Sqrt.cpp create mode 100644 compiler/moco/import/src/Nodes/SquaredDifference.cpp create mode 100644 compiler/moco/import/src/Nodes/Squeeze.cpp create mode 100644 compiler/moco/import/src/Nodes/StopGradient.cpp create mode 100644 compiler/moco/import/src/Nodes/Sub.cpp create mode 100644 compiler/moco/import/src/Nodes/Tanh.cpp diff --git a/compiler/moco/import/include/moco/Import/Nodes.h b/compiler/moco/import/include/moco/Import/Nodes.h new file mode 100644 index 0000000..eda191f --- /dev/null +++ b/compiler/moco/import/include/moco/Import/Nodes.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2019 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. + */ + +#ifndef __MOCO_IMPORT_NODES_H__ +#define __MOCO_IMPORT_NODES_H__ + +#include "Nodes/Add.h" +#include "Nodes/AvgPool.h" +#include "Nodes/BiasAdd.h" +#include "Nodes/Concat.h" +#include "Nodes/Const.h" +#include "Nodes/Conv2DBackpropInput.h" +#include "Nodes/Conv2D.h" +#include "Nodes/DepthwiseConv2dNative.h" +#include "Nodes/FusedBatchNorm.h" +#include "Nodes/Identity.h" +#include "Nodes/MaxPool.h" +#include "Nodes/Mean.h" +#include "Nodes/Mul.h" +#include "Nodes/Pad.h" +#include "Nodes/Placeholder.h" +#include "Nodes/RealDiv.h" +#include "Nodes/Relu6.h" +#include "Nodes/Relu.h" +#include "Nodes/Reshape.h" +#include "Nodes/Rsqrt.h" +#include "Nodes/Shape.h" +#include "Nodes/Softmax.h" +#include "Nodes/Sqrt.h" +#include "Nodes/SquaredDifference.h" +#include "Nodes/Squeeze.h" +#include "Nodes/StopGradient.h" +#include "Nodes/Sub.h" +#include "Nodes/Tanh.h" + +#endif // __MOCO_IMPORT_NODES_H__ diff --git a/compiler/moco/import/include/moco/Import/Nodes/Add.h b/compiler/moco/import/include/moco/Import/Nodes/Add.h new file mode 100644 index 0000000..3d0d0f3 --- /dev/null +++ b/compiler/moco/import/include/moco/Import/Nodes/Add.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2019 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. + */ + +#ifndef __MOCO_IMPORT_OP_ADD_H__ +#define __MOCO_IMPORT_OP_ADD_H__ + +#include "moco/Import/GraphBuilder.h" + +namespace moco +{ + +/** + * @brief GraphBuilder for Add node + */ +class AddGraphBuilder final : public GraphBuilder +{ +public: + bool validate(const tensorflow::NodeDef &) const override; + void build(const tensorflow::NodeDef &, GraphBuilderContext *) const override; +}; + +} // namespace moco + +#endif // __MOCO_IMPORT_OP_ADD_H__ diff --git a/compiler/moco/import/include/moco/Import/Nodes/AvgPool.h b/compiler/moco/import/include/moco/Import/Nodes/AvgPool.h new file mode 100644 index 0000000..4c8087a --- /dev/null +++ b/compiler/moco/import/include/moco/Import/Nodes/AvgPool.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2019 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. + */ + +#ifndef __MOCO_IMPORT_OP_AVG_POOL_H__ +#define __MOCO_IMPORT_OP_AVG_POOL_H__ + +#include "moco/Import/GraphBuilder.h" + +namespace moco +{ + +class AvgPoolGraphBuilder : public GraphBuilder +{ +public: + bool validate(const tensorflow::NodeDef &) const final; + void build(const tensorflow::NodeDef &, GraphBuilderContext *) const final; +}; + +} // namespace moco + +#endif // __MOCO_IMPORT_OP_AVG_POOL_H__ diff --git a/compiler/moco/import/include/moco/Import/Nodes/BiasAdd.h b/compiler/moco/import/include/moco/Import/Nodes/BiasAdd.h new file mode 100644 index 0000000..214df03 --- /dev/null +++ b/compiler/moco/import/include/moco/Import/Nodes/BiasAdd.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2019 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. + */ + +#ifndef __MOCO_IMPORT_OP_BIAS_ADD_H__ +#define __MOCO_IMPORT_OP_BIAS_ADD_H__ + +#include "moco/Import/GraphBuilder.h" + +namespace moco +{ + +class BiasAddGraphBuilder : public GraphBuilder +{ +public: + bool validate(const tensorflow::NodeDef &) const final; + void build(const tensorflow::NodeDef &, GraphBuilderContext *) const final; +}; + +} // namespace moco + +#endif // __MOCO_IMPORT_OP_BIAS_ADD_H__ diff --git a/compiler/moco/import/include/moco/Import/Nodes/Concat.h b/compiler/moco/import/include/moco/Import/Nodes/Concat.h new file mode 100644 index 0000000..2341fb0 --- /dev/null +++ b/compiler/moco/import/include/moco/Import/Nodes/Concat.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2019 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. + */ + +#ifndef __MOCO_IMPORT_OP_CONCAT_H__ +#define __MOCO_IMPORT_OP_CONCAT_H__ + +#include "moco/Import/GraphBuilder.h" + +namespace moco +{ + +class ConcatV2GraphBuilder : public GraphBuilder +{ +public: + bool validate(const tensorflow::NodeDef &) const final; + void build(const tensorflow::NodeDef &, GraphBuilderContext *) const final; +}; + +} // namespace moco + +#endif // __MOCO_IMPORT_OP_CONCAT_H__ diff --git a/compiler/moco/import/include/moco/Import/Nodes/Const.h b/compiler/moco/import/include/moco/Import/Nodes/Const.h new file mode 100644 index 0000000..1ce3782 --- /dev/null +++ b/compiler/moco/import/include/moco/Import/Nodes/Const.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2019 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. + */ + +#ifndef __MOCO_IMPORT_OP_CONST_H__ +#define __MOCO_IMPORT_OP_CONST_H__ + +#include "moco/Import/GraphBuilder.h" + +namespace moco +{ + +class ConstGraphBuilder : public GraphBuilder +{ +public: + bool validate(const tensorflow::NodeDef &) const final; + void build(const tensorflow::NodeDef &, GraphBuilderContext *) const final; +}; + +} // namespace moco + +#endif // __MOCO_IMPORT_OP_CONST_H__ diff --git a/compiler/moco/import/include/moco/Import/Nodes/Conv2D.h b/compiler/moco/import/include/moco/Import/Nodes/Conv2D.h new file mode 100644 index 0000000..3bd3dc7 --- /dev/null +++ b/compiler/moco/import/include/moco/Import/Nodes/Conv2D.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2019 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. + */ + +#ifndef __MOCO_IMPORT_OP_CONV_2D_H__ +#define __MOCO_IMPORT_OP_CONV_2D_H__ + +#include "moco/Import/GraphBuilder.h" + +namespace moco +{ + +class Conv2DGraphBuilder : public GraphBuilder +{ +public: + bool validate(const tensorflow::NodeDef &) const final; + void build(const tensorflow::NodeDef &, GraphBuilderContext *) const final; +}; + +} // namespace moco + +#endif // __MOCO_IMPORT_OP_CONV_2D_H__ diff --git a/compiler/moco/import/include/moco/Import/Nodes/Conv2DBackpropInput.h b/compiler/moco/import/include/moco/Import/Nodes/Conv2DBackpropInput.h new file mode 100644 index 0000000..262a443 --- /dev/null +++ b/compiler/moco/import/include/moco/Import/Nodes/Conv2DBackpropInput.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2019 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. + */ + +#ifndef __MOCO_IMPORT_OP_CONV_2D_BACKPROP_INPUT_H__ +#define __MOCO_IMPORT_OP_CONV_2D_BACKPROP_INPUT_H__ + +#include "moco/Import/GraphBuilder.h" + +namespace moco +{ + +/** + * @brief GraphBuilder for Conv2DBackpropInput node + */ +class Conv2DBackpropInputGraphBuilder final : public GraphBuilder +{ +public: + bool validate(const tensorflow::NodeDef &) const override; + void build(const tensorflow::NodeDef &, GraphBuilderContext *) const override; +}; + +} // namespace moco + +#endif // __MOCO_IMPORT_OP_CONV_2D_BACKPROP_INPUT_H__ diff --git a/compiler/moco/import/include/moco/Import/Nodes/DepthwiseConv2dNative.h b/compiler/moco/import/include/moco/Import/Nodes/DepthwiseConv2dNative.h new file mode 100644 index 0000000..1dcbba1 --- /dev/null +++ b/compiler/moco/import/include/moco/Import/Nodes/DepthwiseConv2dNative.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2019 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. + */ + +#ifndef __MOCO_IMPORT_OP_DEPTHWISE_CONV_2D_NATIVE_H__ +#define __MOCO_IMPORT_OP_DEPTHWISE_CONV_2D_NATIVE_H__ + +#include "moco/Import/GraphBuilder.h" + +namespace moco +{ + +/** + * @brief GraphBuilder for DepthwiseConv2dNative node + */ +class DepthwiseConv2dNativeGraphBuilder final : public GraphBuilder +{ +public: + bool validate(const tensorflow::NodeDef &) const override; + void build(const tensorflow::NodeDef &, GraphBuilderContext *) const override; +}; + +} // namespace moco + +#endif // __MOCO_IMPORT_OP_DEPTHWISE_CONV_2D_NATIVE_H__ diff --git a/compiler/moco/import/include/moco/Import/Nodes/FusedBatchNorm.h b/compiler/moco/import/include/moco/Import/Nodes/FusedBatchNorm.h new file mode 100644 index 0000000..38d1d56 --- /dev/null +++ b/compiler/moco/import/include/moco/Import/Nodes/FusedBatchNorm.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2019 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. + */ + +#ifndef __MOCO_IMPORT_OP_FUSED_BATCH_NORM_H__ +#define __MOCO_IMPORT_OP_FUSED_BATCH_NORM_H__ + +#include "moco/Import/GraphBuilder.h" + +namespace moco +{ + +/** + * @brief GraphBuilder for FusedBatchNorm node + */ +class FusedBatchNormGraphBuilder final : public GraphBuilder +{ +public: + bool validate(const tensorflow::NodeDef &) const override; + void build(const tensorflow::NodeDef &, GraphBuilderContext *) const override; +}; + +} // namespace moco + +#endif // __MOCO_IMPORT_OP_FUSED_BATCH_NORM_H__ diff --git a/compiler/moco/import/include/moco/Import/Nodes/Identity.h b/compiler/moco/import/include/moco/Import/Nodes/Identity.h new file mode 100644 index 0000000..29e0480 --- /dev/null +++ b/compiler/moco/import/include/moco/Import/Nodes/Identity.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2019 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. + */ + +#ifndef __MOCO_IMPORT_OP_IDENTITY_H__ +#define __MOCO_IMPORT_OP_IDENTITY_H__ + +#include "moco/Import/GraphBuilder.h" + +namespace moco +{ + +class IdentityGraphBuilder : public GraphBuilder +{ +public: + bool validate(const tensorflow::NodeDef &) const final; + void build(const tensorflow::NodeDef &, GraphBuilderContext *) const final; +}; + +} // namespace moco + +#endif // __MOCO_IMPORT_OP_IDENTITY_H__ diff --git a/compiler/moco/import/include/moco/Import/Nodes/MaxPool.h b/compiler/moco/import/include/moco/Import/Nodes/MaxPool.h new file mode 100644 index 0000000..696fa71 --- /dev/null +++ b/compiler/moco/import/include/moco/Import/Nodes/MaxPool.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2019 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. + */ + +#ifndef __MOCO_IMPORT_OP_MAX_POOL_H__ +#define __MOCO_IMPORT_OP_MAX_POOL_H__ + +#include "moco/Import/GraphBuilder.h" + +namespace moco +{ + +class MaxPoolGraphBuilder : public GraphBuilder +{ +public: + bool validate(const tensorflow::NodeDef &) const final; + void build(const tensorflow::NodeDef &, GraphBuilderContext *) const final; +}; + +} // namespace moco + +#endif // __MOCO_IMPORT_OP_MAX_POOL_H__ diff --git a/compiler/moco/import/include/moco/Import/Nodes/Mean.h b/compiler/moco/import/include/moco/Import/Nodes/Mean.h new file mode 100644 index 0000000..7bae1bb --- /dev/null +++ b/compiler/moco/import/include/moco/Import/Nodes/Mean.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2019 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. + */ + +#ifndef __MOCO_IMPORT_OP_MEAN_H__ +#define __MOCO_IMPORT_OP_MEAN_H__ + +#include "moco/Import/GraphBuilder.h" + +namespace moco +{ + +/** + * @brief GraphBuilder for Mean node + */ +class MeanGraphBuilder final : public GraphBuilder +{ +public: + bool validate(const tensorflow::NodeDef &) const override; + void build(const tensorflow::NodeDef &, GraphBuilderContext *) const override; +}; + +} // namespace moco + +#endif // __MOCO_IMPORT_OP_MEAN_H__ diff --git a/compiler/moco/import/include/moco/Import/Nodes/Mul.h b/compiler/moco/import/include/moco/Import/Nodes/Mul.h new file mode 100644 index 0000000..667c819 --- /dev/null +++ b/compiler/moco/import/include/moco/Import/Nodes/Mul.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2019 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. + */ + +#ifndef __MOCO_IMPORT_OP_MUL_H__ +#define __MOCO_IMPORT_OP_MUL_H__ + +#include "moco/Import/GraphBuilder.h" + +namespace moco +{ + +/** + * @brief GraphBuilder for Mul node + */ +class MulGraphBuilder final : public GraphBuilder +{ +public: + bool validate(const tensorflow::NodeDef &) const override; + void build(const tensorflow::NodeDef &, GraphBuilderContext *) const override; +}; + +} // namespace moco + +#endif // __MOCO_IMPORT_OP_MUL_H__ diff --git a/compiler/moco/import/include/moco/Import/Nodes/Pad.h b/compiler/moco/import/include/moco/Import/Nodes/Pad.h new file mode 100644 index 0000000..22eab32 --- /dev/null +++ b/compiler/moco/import/include/moco/Import/Nodes/Pad.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2019 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. + */ + +#ifndef __MOCO_IMPORT_OP_PAD_H__ +#define __MOCO_IMPORT_OP_PAD_H__ + +#include "moco/Import/GraphBuilder.h" + +namespace moco +{ + +/** + * @brief GraphBuilder for Pad node + */ +class PadGraphBuilder final : public GraphBuilder +{ +public: + bool validate(const tensorflow::NodeDef &) const override; + void build(const tensorflow::NodeDef &, GraphBuilderContext *) const override; +}; + +} // namespace moco + +#endif // __MOCO_IMPORT_OP_PAD_H__ diff --git a/compiler/moco/import/include/moco/Import/Nodes/Placeholder.h b/compiler/moco/import/include/moco/Import/Nodes/Placeholder.h new file mode 100644 index 0000000..4586009 --- /dev/null +++ b/compiler/moco/import/include/moco/Import/Nodes/Placeholder.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2019 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. + */ + +#ifndef __MOCO_IMPORT_OP_PLACEHOLDER_H__ +#define __MOCO_IMPORT_OP_PLACEHOLDER_H__ + +#include "moco/Import/GraphBuilder.h" + +namespace moco +{ + +/** + * @brief GraphBuilder for Placeholder node + */ +class PlaceholderGraphBuilder final : public GraphBuilder +{ +public: + bool validate(const tensorflow::NodeDef &) const override; + void build(const tensorflow::NodeDef &, GraphBuilderContext *) const override; +}; + +} // namespace moco + +#endif // __MOCO_IMPORT_OP_PLACEHOLDER_H__ diff --git a/compiler/moco/import/include/moco/Import/Nodes/RealDiv.h b/compiler/moco/import/include/moco/Import/Nodes/RealDiv.h new file mode 100644 index 0000000..142e8b5 --- /dev/null +++ b/compiler/moco/import/include/moco/Import/Nodes/RealDiv.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2019 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. + */ + +#ifndef __MOCO_IMPORT_OP_REALDIV_H__ +#define __MOCO_IMPORT_OP_REALDIV_H__ + +#include "moco/Import/GraphBuilder.h" + +namespace moco +{ + +/** + * @brief GraphBuilder for RealDiv node + */ +class RealDivGraphBuilder final : public GraphBuilder +{ +public: + bool validate(const tensorflow::NodeDef &) const override; + void build(const tensorflow::NodeDef &, GraphBuilderContext *) const override; +}; + +} // namespace moco + +#endif // __MOCO_IMPORT_OP_REALDIV_H__ diff --git a/compiler/moco/import/include/moco/Import/Nodes/Relu.h b/compiler/moco/import/include/moco/Import/Nodes/Relu.h new file mode 100644 index 0000000..0bd9cff --- /dev/null +++ b/compiler/moco/import/include/moco/Import/Nodes/Relu.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2019 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. + */ + +#ifndef __MOCO_IMPORT_OP_RELU_H__ +#define __MOCO_IMPORT_OP_RELU_H__ + +#include "moco/Import/GraphBuilder.h" + +namespace moco +{ + +/** + * @brief GraphBuilder for Relu node + */ +class ReluGraphBuilder : public GraphBuilder +{ +public: + bool validate(const tensorflow::NodeDef &) const final; + void build(const tensorflow::NodeDef &, GraphBuilderContext *) const final; +}; + +} // namespace moco + +#endif // __MOCO_IMPORT_OP_RELU_H__ diff --git a/compiler/moco/import/include/moco/Import/Nodes/Relu6.h b/compiler/moco/import/include/moco/Import/Nodes/Relu6.h new file mode 100644 index 0000000..d211b05 --- /dev/null +++ b/compiler/moco/import/include/moco/Import/Nodes/Relu6.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2019 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. + */ + +#ifndef __MOCO_IMPORT_OP_RELU6_H__ +#define __MOCO_IMPORT_OP_RELU6_H__ + +#include "moco/Import/GraphBuilder.h" + +namespace moco +{ + +/** + * @brief GraphBuilder for Relu6 node + */ +class Relu6GraphBuilder : public GraphBuilder +{ +public: + bool validate(const tensorflow::NodeDef &) const final; + void build(const tensorflow::NodeDef &, GraphBuilderContext *) const final; +}; + +} // namespace moco + +#endif // __MOCO_IMPORT_OP_RELU6_H__ diff --git a/compiler/moco/import/include/moco/Import/Nodes/Reshape.h b/compiler/moco/import/include/moco/Import/Nodes/Reshape.h new file mode 100644 index 0000000..e8bfeee --- /dev/null +++ b/compiler/moco/import/include/moco/Import/Nodes/Reshape.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2019 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. + */ + +#ifndef __MOCO_IMPORT_OP_RESHAPE_H__ +#define __MOCO_IMPORT_OP_RESHAPE_H__ + +#include "moco/Import/GraphBuilder.h" + +namespace moco +{ + +/** + * @brief GraphBuilder for Reshape node + */ +class ReshapeGraphBuilder final : public GraphBuilder +{ +public: + bool validate(const tensorflow::NodeDef &) const override; + void build(const tensorflow::NodeDef &, GraphBuilderContext *) const override; +}; + +} // namespace moco + +#endif // __MOCO_IMPORT_OP_RESHAPE_H__ diff --git a/compiler/moco/import/include/moco/Import/Nodes/Rsqrt.h b/compiler/moco/import/include/moco/Import/Nodes/Rsqrt.h new file mode 100644 index 0000000..dedc523 --- /dev/null +++ b/compiler/moco/import/include/moco/Import/Nodes/Rsqrt.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2019 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. + */ + +#ifndef __MOCO_IMPORT_OP_RSQRT_H__ +#define __MOCO_IMPORT_OP_RSQRT_H__ + +#include "moco/Import/GraphBuilder.h" + +namespace moco +{ + +/** + * @brief GraphBuilder for Rsqrt node + */ +class RsqrtGraphBuilder final : public GraphBuilder +{ +public: + bool validate(const tensorflow::NodeDef &) const override; + void build(const tensorflow::NodeDef &, GraphBuilderContext *) const override; +}; + +} // namespace moco + +#endif // __MOCO_IMPORT_OP_RSQRT_H__ diff --git a/compiler/moco/import/include/moco/Import/Nodes/Shape.h b/compiler/moco/import/include/moco/Import/Nodes/Shape.h new file mode 100644 index 0000000..e36e1d5 --- /dev/null +++ b/compiler/moco/import/include/moco/Import/Nodes/Shape.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2019 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. + */ + +#ifndef __MOCO_IMPORT_OP_SHAPE_H__ +#define __MOCO_IMPORT_OP_SHAPE_H__ + +#include "moco/Import/GraphBuilder.h" + +namespace moco +{ + +/** + * @brief GraphBuilder for Shape node + */ +class ShapeGraphBuilder final : public GraphBuilder +{ +public: + bool validate(const tensorflow::NodeDef &) const override; + void build(const tensorflow::NodeDef &, GraphBuilderContext *) const override; +}; + +} // namespace moco + +#endif // __MOCO_IMPORT_OP_SHAPE_H__ diff --git a/compiler/moco/import/include/moco/Import/Nodes/Softmax.h b/compiler/moco/import/include/moco/Import/Nodes/Softmax.h new file mode 100644 index 0000000..43fbb88 --- /dev/null +++ b/compiler/moco/import/include/moco/Import/Nodes/Softmax.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2019 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. + */ + +#ifndef __MOCO_IMPORT_OP_SOFTMAX_H__ +#define __MOCO_IMPORT_OP_SOFTMAX_H__ + +#include "moco/Import/GraphBuilder.h" + +namespace moco +{ + +/** +* @brief GraphBuilder for Softmax node +*/ +class SoftmaxGraphBuilder final : public GraphBuilder +{ +public: + bool validate(const tensorflow::NodeDef &) const override; + void build(const tensorflow::NodeDef &, GraphBuilderContext *) const override; +}; + +} // namespace moco + +#endif // __MOCO_IMPORT_OP_SOFTMAX_H__ diff --git a/compiler/moco/import/include/moco/Import/Nodes/Sqrt.h b/compiler/moco/import/include/moco/Import/Nodes/Sqrt.h new file mode 100644 index 0000000..d17dc34 --- /dev/null +++ b/compiler/moco/import/include/moco/Import/Nodes/Sqrt.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2019 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. + */ + +#ifndef __MOCO_IMPORT_OP_SQRT_H__ +#define __MOCO_IMPORT_OP_SQRT_H__ + +#include "moco/Import/GraphBuilder.h" + +namespace moco +{ + +/** + * @brief GraphBuilder for Sqrt node + */ +class SqrtGraphBuilder final : public GraphBuilder +{ +public: + bool validate(const tensorflow::NodeDef &) const override; + void build(const tensorflow::NodeDef &, GraphBuilderContext *) const override; +}; + +} // namespace moco + +#endif // __MOCO_IMPORT_OP_SQRT_H__ diff --git a/compiler/moco/import/include/moco/Import/Nodes/SquaredDifference.h b/compiler/moco/import/include/moco/Import/Nodes/SquaredDifference.h new file mode 100644 index 0000000..501464d --- /dev/null +++ b/compiler/moco/import/include/moco/Import/Nodes/SquaredDifference.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2019 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. + */ + +#ifndef __MOCO_IMPORT_OP_SQUARED_DIFFERENCE_H__ +#define __MOCO_IMPORT_OP_SQUARED_DIFFERENCE_H__ + +#include "moco/Import/GraphBuilder.h" + +namespace moco +{ + +/** + * @brief GraphBuilder for SquaredDifference node + */ +class SquaredDifferenceGraphBuilder final : public GraphBuilder +{ +public: + bool validate(const tensorflow::NodeDef &) const override; + void build(const tensorflow::NodeDef &, GraphBuilderContext *) const override; +}; + +} // namespace moco + +#endif // __MOCO_IMPORT_OP_SQUARED_DIFFERENCE_H__ diff --git a/compiler/moco/import/include/moco/Import/Nodes/Squeeze.h b/compiler/moco/import/include/moco/Import/Nodes/Squeeze.h new file mode 100644 index 0000000..64ead07 --- /dev/null +++ b/compiler/moco/import/include/moco/Import/Nodes/Squeeze.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2019 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. + */ + +#ifndef __MOCO_IMPORT_OP_SQUEEZE_H__ +#define __MOCO_IMPORT_OP_SQUEEZE_H__ + +#include "moco/Import/GraphBuilder.h" + +namespace moco +{ + +/** + * @brief GraphBuilder for Squeeze node + */ +class SqueezeGraphBuilder final : public GraphBuilder +{ +public: + bool validate(const tensorflow::NodeDef &) const override; + void build(const tensorflow::NodeDef &, GraphBuilderContext *) const override; +}; + +} // namespace moco + +#endif // __MOCO_IMPORT_OP_SQUEEZE_H__ diff --git a/compiler/moco/import/include/moco/Import/Nodes/StopGradient.h b/compiler/moco/import/include/moco/Import/Nodes/StopGradient.h new file mode 100644 index 0000000..e547a8a --- /dev/null +++ b/compiler/moco/import/include/moco/Import/Nodes/StopGradient.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2019 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. + */ + +#ifndef __MOCO_IMPORT_OP_STOP_GRADIENT_H__ +#define __MOCO_IMPORT_OP_STOP_GRADIENT_H__ + +#include "moco/Import/GraphBuilder.h" + +namespace moco +{ + +/** + * @brief GraphBuilder for StopGradient node + */ +class StopGradientGraphBuilder final : public GraphBuilder +{ +public: + bool validate(const tensorflow::NodeDef &) const override; + void build(const tensorflow::NodeDef &, GraphBuilderContext *) const override; +}; + +} // namespace moco + +#endif // __MOCO_IMPORT_OP_STOP_GRADIENT_H__ diff --git a/compiler/moco/import/include/moco/Import/Nodes/Sub.h b/compiler/moco/import/include/moco/Import/Nodes/Sub.h new file mode 100644 index 0000000..d6351e3 --- /dev/null +++ b/compiler/moco/import/include/moco/Import/Nodes/Sub.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2019 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. + */ + +#ifndef __MOCO_IMPORT_OP_SUB_H__ +#define __MOCO_IMPORT_OP_SUB_H__ + +#include "moco/Import/GraphBuilder.h" + +namespace moco +{ + +/** + * @brief GraphBuilder for Sub node + */ +class SubGraphBuilder final : public GraphBuilder +{ +public: + bool validate(const tensorflow::NodeDef &) const override; + void build(const tensorflow::NodeDef &, GraphBuilderContext *) const override; +}; + +} // namespace moco + +#endif // __MOCO_IMPORT_OP_SUB_H__ diff --git a/compiler/moco/import/include/moco/Import/Nodes/Tanh.h b/compiler/moco/import/include/moco/Import/Nodes/Tanh.h new file mode 100644 index 0000000..183e117 --- /dev/null +++ b/compiler/moco/import/include/moco/Import/Nodes/Tanh.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2019 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. + */ + +#ifndef __MOCO_IMPORT_OP_TANH_H__ +#define __MOCO_IMPORT_OP_TANH_H__ + +#include "moco/Import/GraphBuilder.h" + +namespace moco +{ + +/** + * @brief GraphBuilder for Tanh node + */ +class TanhGraphBuilder final : public GraphBuilder +{ +public: + bool validate(const tensorflow::NodeDef &) const override; + void build(const tensorflow::NodeDef &, GraphBuilderContext *) const override; +}; + +} // namespace moco + +#endif // __MOCO_IMPORT_OP_TANH_H__ diff --git a/compiler/moco/import/src/Nodes/Add.cpp b/compiler/moco/import/src/Nodes/Add.cpp new file mode 100644 index 0000000..f7e599b --- /dev/null +++ b/compiler/moco/import/src/Nodes/Add.cpp @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2019 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 "moco/Import/Nodes/Add.h" + +#include + +#include +#include + +namespace +{ + +using namespace moco; + +/** + * @brief GraphUpdate for TF Add node + */ +class TFAddGraphUpdate final : public GraphUpdate +{ +public: + TFAddGraphUpdate(TFAdd *node, std::vector names) : _node(node), _names(names) {} + + void input(const SymbolTable *) const override; + +private: + TFAdd *_node; + std::vector _names; +}; + +void TFAddGraphUpdate::input(const SymbolTable *tensor_names) const +{ + int num_inputs = _names.size(); + assert(num_inputs == 2); + + _node->x(tensor_names->node(_names[0])); + _node->y(tensor_names->node(_names[1])); +} + +} // namespace + +namespace moco +{ + +bool AddGraphBuilder::validate(const tensorflow::NodeDef &node) const +{ + assert(node.input_size() == 2); + + return true; +} + +void AddGraphBuilder::build(const tensorflow::NodeDef &node, GraphBuilderContext *context) const +{ + assert(context != nullptr); + + loco::Graph *graph = context->graph(); + SymbolTable *tensor_names = context->tensor_names(); + UpdateQueue *updates = context->updates(); + + // creating TF dialect Add node + auto tf_add = graph->nodes()->create(); + + TensorName output_name(node.name(), 0); + tensor_names->enroll(output_name, tf_add); + + std::vector add_input_names; + add_input_names.push_back(TensorName(node.input(0))); // x + add_input_names.push_back(TensorName(node.input(1))); // y + + auto tf_add_update = stdex::make_unique(tf_add, add_input_names); + updates->enroll(std::move(tf_add_update)); +} + +} // namespace moco + +#include "moco/Import/GraphBuilderRegistry.h" + +REGISTER_OP_BUILDER(Add, AddGraphBuilder) diff --git a/compiler/moco/import/src/Nodes/Add.test.cpp b/compiler/moco/import/src/Nodes/Add.test.cpp new file mode 100644 index 0000000..ace2b08 --- /dev/null +++ b/compiler/moco/import/src/Nodes/Add.test.cpp @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2019 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 "moco/Import/Nodes/Add.h" +#include "TestHelper.h" + +#include + +using namespace moco::test; + +namespace +{ +// clang-format off +const char *add_basic_pbtxt = STRING_CONTENT( + name: "ADD_01" + op: "Add" + input: "input_01" + input: "input_02" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +); +// clang-format on + +} // namespace + +TEST(TensorFlowImport, tf_add_basic) +{ + TFNodeBuildTester tester; + moco::AddGraphBuilder graphbuilder; + tensorflow::NodeDef nodedef; + + EXPECT_TRUE(plier::tf::parse_nodedef(add_basic_pbtxt, nodedef)); + + // what to test: + // - TFAdd node should exist + // - both inputs x() and y() should not be null + + tester.inputs({"input_01", "input_02"}); + tester.output("ADD_01"); + tester.run(nodedef, graphbuilder); +} diff --git a/compiler/moco/import/src/Nodes/AvgPool.cpp b/compiler/moco/import/src/Nodes/AvgPool.cpp new file mode 100644 index 0000000..219940f --- /dev/null +++ b/compiler/moco/import/src/Nodes/AvgPool.cpp @@ -0,0 +1,136 @@ +/* + * Copyright (c) 2019 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 "moco/Import/Nodes/AvgPool.h" + +#include + +#include + +#include "Convert.h" +#include +#include +#include + +#include +#include + +using namespace plier::tf; + +namespace +{ + +using namespace moco; + +class TFAvgPoolGraphUpdate final : public GraphUpdate +{ +public: + TFAvgPoolGraphUpdate(TFAvgPool *node, const TensorName &name) + : _avgpool_node(node), _value_name(name) + { + } + + void input(const SymbolTable *) const override; + +private: + TFAvgPool *_avgpool_node; + const TensorName _value_name; +}; + +void TFAvgPoolGraphUpdate::input(const SymbolTable *node_table) const +{ + loco::Node *value_node = node_table->node(_value_name); + _avgpool_node->value(value_node); +} + +} // namespace + +namespace moco +{ + +bool AvgPoolGraphBuilder::validate(const tensorflow::NodeDef &node) const +{ + // note: even though "data_format" is not entered when a model is written, + // TF seems to generate "data_format" field into a pb file + if (!plier::tf::has_attrs(node, {"T", "data_format", "ksize", "padding", "strides"})) + return false; + + if (node.input_size() != 1) + return false; + + return true; +} + +void AvgPoolGraphBuilder::build(const tensorflow::NodeDef &node, GraphBuilderContext *context) const +{ + assert(context != nullptr); + + loco::Graph *graph = context->graph(); + SymbolTable *tensor_names = context->tensor_names(); + UpdateQueue *updates = context->updates(); + + // name of loco nodes + ::std::string avgPool2d_name = node.name(); + + // tensorflow data_format: one of NHWC or NCHW. + auto data_layout = get_string_attr(node, "data_format"); + auto avgPool_node = graph->nodes()->create(); + avgPool_node->data_layout(data_layout); + + // padding + auto padding = moco::str_toupper(get_string_attr(node, "padding")); + avgPool_node->padding(padding); + + // ksize + auto tf_ksize = get_list_attr(node, "ksize"); + auto ksize = as_int64_list(tf_ksize); + if (ksize.size() != 4) + { + // TODO support ksize length for 1 and 2 + throw std::runtime_error("AvgPool only supports ksize length 4"); + } + avgPool_node->ksize(ksize); + + // strides + auto tf_strides = get_list_attr(node, "strides"); + auto strides = as_int64_list(tf_strides); + if (strides.size() != 4) + { + // TODO support strides length for 1 and 2 + throw std::runtime_error("AvgPool only supports strides length 4"); + } + avgPool_node->strides(strides); + + // To set the input node of encode_node with avgPool2d_name + TensorName output_name(avgPool2d_name, 0); + tensor_names->enroll(output_name, avgPool_node); + + // Record ifm inputs to featureEncode_node + auto update = stdex::make_unique(avgPool_node, TensorName(node.input(0))); + + updates->enroll(std::move(update)); +} + +} // namespace moco + +#include "moco/Import/GraphBuilderRegistry.h" + +REGISTER_OP_BUILDER(AvgPool, AvgPoolGraphBuilder) + +// TODO Consider a case when TF AvgPool is for 3D. +// AvgPool works for 2D and other Dimensions, such as 3D +// So, in future, some other GraphBuilder decide if AvgPoolGraphBuilder is used or +// other GraphBuilder is used for TF AvgPool diff --git a/compiler/moco/import/src/Nodes/BiasAdd.cpp b/compiler/moco/import/src/Nodes/BiasAdd.cpp new file mode 100644 index 0000000..4811cea --- /dev/null +++ b/compiler/moco/import/src/Nodes/BiasAdd.cpp @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2019 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 "moco/Import/Nodes/BiasAdd.h" + +#include + +#include + +#include +#include +#include +#include + +#include +#include + +namespace +{ +using namespace moco; + +class TFBiasAddGraphUpdate final : public GraphUpdate +{ +public: + TFBiasAddGraphUpdate(TFBiasAdd *biasadd, std::vector &names) + : _biasadd(biasadd), _names(names) + { + } + + void input(const SymbolTable *) const override; + +private: + TFBiasAdd *_biasadd; + std::vector _names; +}; + +void TFBiasAddGraphUpdate::input(const SymbolTable *node_table) const +{ + assert(_names.size() == 2); + + auto value_node = node_table->node(_names[0]); + auto bias_node = node_table->node(_names[1]); + assert(value_node != nullptr); + assert(bias_node != nullptr); + + _biasadd->value(value_node); + _biasadd->bias(bias_node); +} + +} // namespace + +namespace moco +{ + +bool BiasAddGraphBuilder::validate(const tensorflow::NodeDef &node) const +{ + assert(node.input_size() == 2); + + // note: even though "data_format" is not entered when a model is written, + // TF seems to generate "data_format" field into a pb file + if (!plier::tf::has_attrs(node, {"T", "data_format"})) + return false; + + // TODO add type check + // type of input and bias should be same (except using quantization) + + // Note In case of TF.nn.bias_add, + // "value may have any number of dimensions." ... + // but "data_format: A string. 'NHWC' and 'NCHW' are supported." + // Not sure if value should be 4-D tensor. Let's skip this check for now. + + return true; +} + +void BiasAddGraphBuilder::build(const tensorflow::NodeDef &node, GraphBuilderContext *context) const +{ + assert(context != nullptr); + + loco::Graph *graph = context->graph(); + SymbolTable *tensor_names = context->tensor_names(); + UpdateQueue *updates = context->updates(); + + // tensorflow data_format: one of NHWC or NCHW. + auto data_layout = plier::tf::get_string_attr(node, "data_format"); + auto tf_bias_add = graph->nodes()->create(); + + tf_bias_add->data_layout(data_layout); + + // To set the input node of encode_node with biasAdd_name + TensorName output_name(node.name(), 0); + tensor_names->enroll(output_name, tf_bias_add); + + std::vector input_names; + input_names.push_back(TensorName(node.input(0))); + input_names.push_back(TensorName(node.input(1))); + + auto update = stdex::make_unique(tf_bias_add, input_names); + updates->enroll(std::move(update)); +} + +} // namespace moco + +#include "moco/Import/GraphBuilderRegistry.h" + +REGISTER_OP_BUILDER(BiasAdd, BiasAddGraphBuilder) diff --git a/compiler/moco/import/src/Nodes/Concat.cpp b/compiler/moco/import/src/Nodes/Concat.cpp new file mode 100644 index 0000000..775a6c2 --- /dev/null +++ b/compiler/moco/import/src/Nodes/Concat.cpp @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2019 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 "moco/Import/Nodes/Concat.h" + +#include + +#include + +#include +#include +#include + +#include + +namespace +{ + +using namespace moco; + +class TFConcatV2GraphUpdate final : public GraphUpdate +{ +public: + TFConcatV2GraphUpdate(TFConcatV2 *node, std::vector names) + : _node(node), _names(names) + { + } + + void input(const SymbolTable *) const override; + +private: + TFConcatV2 *_node; + std::vector _names; +}; + +void TFConcatV2GraphUpdate::input(const SymbolTable *tensor_names) const +{ + uint32_t num_values = _names.size() - 1; // exclude axis + assert(num_values >= 1); + + for (uint32_t i = 0; i < num_values; ++i) + { + auto input_node = tensor_names->node(_names[i]); + assert(input_node != nullptr); + _node->values(i, input_node); + } + auto axis_node = tensor_names->node(_names[num_values]); + assert(axis_node != nullptr); + _node->axis(axis_node); +} + +} // namespace + +namespace moco +{ + +bool ConcatV2GraphBuilder::validate(const tensorflow::NodeDef &node) const +{ + if (!plier::tf::has_attrs(node, {"T", "N", "Tidx"})) + return false; + + // Concat node SHOULD have 3 or more inputs, that is 2 + axis + const int num_inputs = node.input_size() - 1; + assert(num_inputs >= 2); + assert(num_inputs == plier::tf::get_int_attr(node, "N")); + return (num_inputs >= 2) && (num_inputs == plier::tf::get_int_attr(node, "N")); +} + +void ConcatV2GraphBuilder::build(const tensorflow::NodeDef &node, + GraphBuilderContext *context) const +{ + assert(context != nullptr); + + auto graph = context->graph(); + auto tensor_names = context->tensor_names(); + auto updates = context->updates(); + + const int num_inputs = node.input_size() - 1; + std::vector input_names; + auto concat_node = graph->nodes()->create(num_inputs); + + for (int ni = 0; ni < num_inputs; ++ni) + { + input_names.push_back(TensorName(node.input(ni))); + } + // last one is the axis + input_names.push_back(TensorName(node.input(num_inputs))); + + // register string-name to the last node as output of concat(s) + TensorName output_name(node.name(), 0); + tensor_names->enroll(output_name, concat_node); + + auto update = stdex::make_unique(concat_node, input_names); + updates->enroll(std::move(update)); +} + +} // namespace moco + +#include "moco/Import/GraphBuilderRegistry.h" + +REGISTER_OP_BUILDER(ConcatV2, ConcatV2GraphBuilder) diff --git a/compiler/moco/import/src/Nodes/Const.cpp b/compiler/moco/import/src/Nodes/Const.cpp new file mode 100644 index 0000000..7454b35 --- /dev/null +++ b/compiler/moco/import/src/Nodes/Const.cpp @@ -0,0 +1,184 @@ +/* + * Copyright (c) 2019 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 "moco/Import/Nodes/Const.h" + +#include +#include + +#include +#include + +#include +#include +#include + +namespace +{ + +using namespace moco; + +void read_value_int32(TFConst *const_node, int num_elements, + const tensorflow::TensorProto &input_tensor) +{ + const_node->size(num_elements); + + int32_t input_elements = input_tensor.int_val_size(); + + if (input_tensor.tensor_content().size() == num_elements * sizeof(int32_t)) + { + const std::string &str_content = input_tensor.tensor_content(); + const int32_t *s32_ptr = reinterpret_cast(str_content.c_str()); + for (int32_t i = 0; i < num_elements; i++) + { + const_node->at(i) = *(s32_ptr + i); + } + } + else if (0 < input_elements && input_elements <= num_elements) + { + for (int32_t i = 0; i < input_elements; i++) + { + const_node->at(i) = input_tensor.int_val(i); + } + + for (int32_t i = input_elements; i < num_elements; i++) + { + const_node->at(i) = input_tensor.int_val(input_elements - 1); + } + } + else + { + throw std::runtime_error("Error: Invalid Const values"); + } +} + +void read_value_float32(TFConst *const_node, int num_elements, + const tensorflow::TensorProto &input_tensor) +{ + const_node->size(num_elements); + + int32_t input_elements = input_tensor.float_val_size(); + + if (input_tensor.tensor_content().size() == num_elements * sizeof(float)) + { + const std::string &str_content = input_tensor.tensor_content(); + const float *float_ptr = reinterpret_cast(str_content.c_str()); + for (int32_t i = 0; i < num_elements; i++) + { + const_node->at(i) = *(float_ptr + i); + } + } + else if (0 < input_elements && input_elements <= num_elements) + { + for (int32_t i = 0; i < input_elements; i++) + { + const_node->at(i) = input_tensor.float_val(i); + } + + for (int32_t i = input_elements; i < num_elements; i++) + { + const_node->at(i) = input_tensor.float_val(input_elements - 1); + } + } + else + { + throw std::runtime_error("Error: Invalid Const values"); + } +} + +} // namespace + +namespace moco +{ + +bool ConstGraphBuilder::validate(const tensorflow::NodeDef &node) const +{ + return plier::tf::has_attrs(node, {"dtype", "value"}); +} + +void ConstGraphBuilder::build(const tensorflow::NodeDef &node, GraphBuilderContext *context) const +{ + loco::Graph *graph = context->graph(); + SymbolTable *tensor_names = context->tensor_names(); + + // Create a "TFConstant" node for Const + auto const_node = graph->nodes()->create(); + + // set dtype + auto dtype = plier::tf::as_loco_datatype(plier::tf::get_datatype_attr(node, "dtype")); + const_node->dtype(dtype); + + // import shape and value + const auto &input_tensor = plier::tf::get_tensor_attr(node, "value"); + const auto &input_shape = input_tensor.tensor_shape(); + const auto &input_dims = input_shape.dim(); + assert(input_shape.dim_size() <= 6); + const_node->rank(input_shape.dim_size()); + int index = 0; + bool zero_sized_shape = false; + for (auto &d : input_dims) + { + if (d.size() > std::numeric_limits::max()) + throw std::runtime_error("Shape element overflows"); + if (d.size() == 0) + zero_sized_shape = true; + + if (d.size() >= 0) + const_node->dim(index++) = d.size(); + else + throw std::runtime_error{"Error: Unknown dim size for " + node.name()}; + } + + int num_elements = 1; + if (zero_sized_shape) + { + const_node->rank(0); + num_elements = 0; + } + else + { + for (uint32_t d = 0; d < const_node->rank(); d++) + { + num_elements *= const_node->dim(d).value(); + } + } + + switch (dtype) + { + case loco::DataType::S32: + read_value_int32(const_node, num_elements, input_tensor); + break; + + case loco::DataType::FLOAT32: + read_value_float32(const_node, num_elements, input_tensor); + break; + + // TODO support other types + + default: + throw std::runtime_error{"Error: Unsupported data type for " + node.name()}; + } + + // register string-name to node + TensorName output_name(node.name(), 0); + tensor_names->enroll(output_name, const_node); +} + +} // namespace moco + +#include "moco/Import/GraphBuilderRegistry.h" + +REGISTER_OP_BUILDER(Const, ConstGraphBuilder) diff --git a/compiler/moco/import/src/Nodes/Conv2D.cpp b/compiler/moco/import/src/Nodes/Conv2D.cpp new file mode 100644 index 0000000..4341273 --- /dev/null +++ b/compiler/moco/import/src/Nodes/Conv2D.cpp @@ -0,0 +1,133 @@ +/* + * Copyright (c) 2019 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 "moco/Import/Nodes/Conv2D.h" + +#include + +#include + +#include "Convert.h" + +#include +#include +#include +#include + +#include +#include +#include + +namespace +{ +using namespace moco; + +class TFConv2DGraphUpdate final : public GraphUpdate +{ +public: + TFConv2DGraphUpdate(TFConv2D *node, std::vector names) : _node(node), _names(names) {} + + void input(const SymbolTable *) const override; + +private: + TFConv2D *_node; + std::vector _names; +}; + +void TFConv2DGraphUpdate::input(const SymbolTable *node_table) const +{ + assert(_names.size() == 2); + + auto input_node = node_table->node(_names[0]); + auto filter_node = node_table->node(_names[1]); + assert(input_node != nullptr); + assert(filter_node != nullptr); + + _node->input(input_node); + _node->filter(filter_node); +} + +} // namespace + +namespace moco +{ + +bool Conv2DGraphBuilder::validate(const tensorflow::NodeDef &node) const +{ + assert(node.input_size() == 2); + + // note: even though "data_format" is not entered when a model is written, + // TF seems to generate "data_format" field into a pb file + bool has_mandatory_attrs = plier::tf::has_attrs(node, {"T", "data_format", "padding", "strides"}); + // dilation attribute is not fully supported + bool supported_dilations = true; + if (plier::tf::has_attr(node, "dilations")) + { + auto dilation = plier::tf::get_list_attr(node, "dilations").i(); + supported_dilations = + std::all_of(dilation.begin(), dilation.end(), [](std::int64_t dil) { return dil == 1; }); + } + return has_mandatory_attrs && supported_dilations; +} + +void Conv2DGraphBuilder::build(const tensorflow::NodeDef &node, GraphBuilderContext *context) const +{ + assert(context != nullptr); + + loco::Graph *graph = context->graph(); + SymbolTable *tensor_names = context->tensor_names(); + UpdateQueue *updates = context->updates(); + + // name of loco nodes + std::string conv2d_name = node.name(); + + auto conv2d = graph->nodes()->create(); + + // read attributes + auto data_layout = plier::tf::get_string_attr(node, "data_format"); + if (!(data_layout == "NHWC" || data_layout == "NCHW")) + { + throw std::runtime_error("Not yet supported"); + } + conv2d->data_layout(data_layout); + + auto tf_strides = plier::tf::get_list_attr(node, "strides"); + auto strides = plier::tf::as_int64_list(tf_strides); + conv2d->strides(strides); + + auto padding = moco::str_toupper(plier::tf::get_string_attr(node, "padding")); + assert(padding == "VALID" || padding == "SAME"); + conv2d->padding(padding); + + // save the name for graph link updates + TensorName output_name(conv2d_name, 0); + tensor_names->enroll(output_name, conv2d); + + std::vector input_names; + input_names.push_back(TensorName(node.input(0))); // input + input_names.push_back(TensorName(node.input(1))); // kernel + + // Record ifm inputs to featureEncode_node + auto tfconv2d_update = stdex::make_unique(conv2d, input_names); + + updates->enroll(std::move(tfconv2d_update)); +} + +} // namespace moco + +#include "moco/Import/GraphBuilderRegistry.h" + +REGISTER_OP_BUILDER(Conv2D, Conv2DGraphBuilder) diff --git a/compiler/moco/import/src/Nodes/Conv2DBackpropInput.cpp b/compiler/moco/import/src/Nodes/Conv2DBackpropInput.cpp new file mode 100644 index 0000000..a0036df --- /dev/null +++ b/compiler/moco/import/src/Nodes/Conv2DBackpropInput.cpp @@ -0,0 +1,139 @@ +/* + * Copyright (c) 2019 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 "moco/Import/Nodes/Conv2DBackpropInput.h" + +#include + +#include "Convert.h" + +#include +#include +#include + +namespace +{ +using namespace moco; + +/// @brief GraphUpdate for Conv2DBackpropInput node +class Conv2DBackpropInputGraphUpdate final : public GraphUpdate +{ +public: + Conv2DBackpropInputGraphUpdate(TFConv2DBackpropInput *node, std::vector names) + : _node(node), _input_names(names) + { + // DO NOTHING + } + + void input(const SymbolTable *) const override; + +private: + TFConv2DBackpropInput *_node; + std::vector _input_names; +}; + +void Conv2DBackpropInputGraphUpdate::input(const SymbolTable *table) const +{ + assert(_input_names.size() == 3); + + auto input_sizes_node = table->node(_input_names[0]); + auto filter_node = table->node(_input_names[1]); + auto out_backprop_node = table->node(_input_names[2]); + + assert(input_sizes_node != nullptr); + assert(filter_node != nullptr); + assert(out_backprop_node != nullptr); + + _node->input_sizes(input_sizes_node); + _node->filter(filter_node); + _node->out_backprop(out_backprop_node); +} + +} // namespace + +namespace moco +{ + +bool Conv2DBackpropInputGraphBuilder::validate(const tensorflow::NodeDef &node) const +{ + assert(node.input_size() == 3); + + auto assert_default_dilations = [](std::vector dilations) { + assert(dilations.size() == 4); + for (auto d : dilations) + assert(d == 1); + }; + + if (plier::tf::has_attr(node, "dilations")) + { + auto tf_dilations = plier::tf::get_list_attr(node, "dilations"); + auto dilations = plier::tf::as_int64_list(tf_dilations); + + // TODO Support non-default dilations + assert_default_dilations(dilations); + } + // Else, dilations are automatically set to default [1,1,1,1] which we assumes now + + return plier::tf::has_attrs(node, {"T", "data_format", "padding", "strides"}); +} + +void Conv2DBackpropInputGraphBuilder::build(const tensorflow::NodeDef &node, + GraphBuilderContext *context) const +{ + loco::Graph *graph = context->graph(); + SymbolTable *tensor_names = context->tensor_names(); + UpdateQueue *updates = context->updates(); + + // name of loco nodes + std::string conv2d_backprop_name = node.name(); + + auto conv2d_backprop = graph->nodes()->create(); + + // read attributes + auto data_layout = plier::tf::get_string_attr(node, "data_format"); + if (!(data_layout == "NHWC" || data_layout == "NCHW")) + throw std::runtime_error("Not yet supported"); + conv2d_backprop->data_layout(data_layout); + + auto tf_strides = plier::tf::get_list_attr(node, "strides"); + auto strides = plier::tf::as_int64_list(tf_strides); + conv2d_backprop->strides(strides); + + auto padding = moco::str_toupper(plier::tf::get_string_attr(node, "padding")); + assert(padding == "VALID" || padding == "SAME"); + conv2d_backprop->padding(padding); + + // save the name for graph link updates + TensorName output_name(conv2d_backprop_name, 0); + tensor_names->enroll(output_name, conv2d_backprop); + + std::vector input_names; + input_names.push_back(TensorName(node.input(0))); // input_sizes + input_names.push_back(TensorName(node.input(1))); // filter + input_names.push_back(TensorName(node.input(2))); // out_backprop + + // update + auto conv2d_backprop_update = + stdex::make_unique(conv2d_backprop, input_names); + + updates->enroll(std::move(conv2d_backprop_update)); +} + +} // namespace moco + +#include "moco/Import/GraphBuilderRegistry.h" + +REGISTER_OP_BUILDER(Conv2DBackpropInput, Conv2DBackpropInputGraphBuilder) diff --git a/compiler/moco/import/src/Nodes/DepthwiseConv2dNative.cpp b/compiler/moco/import/src/Nodes/DepthwiseConv2dNative.cpp new file mode 100644 index 0000000..8ef44cc --- /dev/null +++ b/compiler/moco/import/src/Nodes/DepthwiseConv2dNative.cpp @@ -0,0 +1,139 @@ +/* + * Copyright (c) 2019 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 "moco/Import/Nodes/DepthwiseConv2dNative.h" + +#include + +#include + +#include "Convert.h" + +#include +#include +#include + +#include + +using namespace plier::tf; + +namespace +{ +using namespace moco; + +class TFDepthwiseConv2dNativeGraphUpdate final : public GraphUpdate +{ +public: + TFDepthwiseConv2dNativeGraphUpdate(TFDepthwiseConv2dNative *node, std::vector names) + : _node(node), _names(names) + { + } + + void input(const SymbolTable *) const override; + +private: + TFDepthwiseConv2dNative *_node; + std::vector _names; +}; + +void TFDepthwiseConv2dNativeGraphUpdate::input(const SymbolTable *node_table) const +{ + assert(_names.size() == 2); + + auto input_node = node_table->node(_names[0]); + auto filter_node = node_table->node(_names[1]); + assert(input_node != nullptr); + assert(filter_node != nullptr); + + _node->input(input_node); + _node->filter(filter_node); +} + +} // namespace + +namespace moco +{ + +bool DepthwiseConv2dNativeGraphBuilder::validate(const tensorflow::NodeDef &node) const +{ + assert(node.input_size() == 2); + + auto data_layout = get_string_attr(node, "data_format"); + if (!(data_layout == "NHWC" || data_layout == "NCHW")) + { + throw std::runtime_error("Not yet supported"); + } + + auto padding = moco::str_toupper(get_string_attr(node, "padding")); + assert(padding == "VALID" || padding == "SAME"); + + auto tf_strides = get_list_attr(node, "strides"); + auto strides = as_int64_list(tf_strides); + assert(strides.size() == 4); + auto stride_n = strides.at(0); + auto stride_h = strides.at(1); + auto stride_w = strides.at(2); + auto stride_c = strides.at(3); + assert(stride_n == 1 && stride_c == 1); + assert(stride_h == stride_w); + + // note: even though "data_format" and "dilations" are not entered when a model is written, + // TF seems to generate those field into a pb file. + return has_attrs(node, {"T", "data_format", "dilations", "padding", "strides"}); +} + +void DepthwiseConv2dNativeGraphBuilder::build(const tensorflow::NodeDef &node, + GraphBuilderContext *context) const +{ + assert(context != nullptr); + + loco::Graph *graph = context->graph(); + SymbolTable *tensor_names = context->tensor_names(); + UpdateQueue *updates = context->updates(); + + auto depthwiseconv2d_native_node = graph->nodes()->create(); + + // read attributes + auto data_layout = get_string_attr(node, "data_format"); + depthwiseconv2d_native_node->data_layout(data_layout); + + auto tf_strides = get_list_attr(node, "strides"); + auto strides = as_int64_list(tf_strides); + depthwiseconv2d_native_node->strides(strides); + + auto padding = moco::str_toupper(get_string_attr(node, "padding")); + depthwiseconv2d_native_node->padding(padding); + + // save the name for graph link updates + TensorName output_name(node.name(), 0); + tensor_names->enroll(output_name, depthwiseconv2d_native_node); + + std::vector input_names; + input_names.push_back(TensorName(node.input(0))); // input + input_names.push_back(TensorName(node.input(1))); // kernel + + // Record ifm inputs to featureEncode_node + auto tfdepthwiseconv2dnative_update = stdex::make_unique( + depthwiseconv2d_native_node, input_names); + + updates->enroll(std::move(tfdepthwiseconv2dnative_update)); +} + +} // namespace moco + +#include "moco/Import/GraphBuilderRegistry.h" + +REGISTER_OP_BUILDER(DepthwiseConv2dNative, DepthwiseConv2dNativeGraphBuilder) diff --git a/compiler/moco/import/src/Nodes/FusedBatchNorm.cpp b/compiler/moco/import/src/Nodes/FusedBatchNorm.cpp new file mode 100644 index 0000000..3dd7f07 --- /dev/null +++ b/compiler/moco/import/src/Nodes/FusedBatchNorm.cpp @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2019 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 "moco/Import/Nodes/FusedBatchNorm.h" + +#include + +#include +#include +#include + +namespace +{ + +using namespace moco; + +/** + * @brief GraphUpdate for FusedBatchNorm node + */ +class FusedBatchNormGraphUpdate final : public GraphUpdate +{ +public: + FusedBatchNormGraphUpdate(TFFusedBatchNorm *node, std::vector names) + : _node(node), _names(names) + { + } + + void input(const SymbolTable *) const override; + +private: + TFFusedBatchNorm *_node; + std::vector _names; +}; + +void FusedBatchNormGraphUpdate::input(const SymbolTable *tensor_names) const +{ + int num_inputs = _names.size(); + assert(num_inputs == 5); + + _node->x(tensor_names->node(_names[0])); + _node->scale(tensor_names->node(_names[1])); + _node->offset(tensor_names->node(_names[2])); + _node->mean(tensor_names->node(_names[3])); + _node->variance(tensor_names->node(_names[4])); +} + +} // namespace + +namespace moco +{ + +bool FusedBatchNormGraphBuilder::validate(const tensorflow::NodeDef &node) const +{ + assert(node.input_size() == 5); + + return plier::tf::has_attrs(node, {"epsilon"}); +} + +void FusedBatchNormGraphBuilder::build(const tensorflow::NodeDef &node, + GraphBuilderContext *context) const +{ + assert(context != nullptr); + + loco::Graph *graph = context->graph(); + SymbolTable *tensor_names = context->tensor_names(); + UpdateQueue *updates = context->updates(); + + float epsilon = plier::tf::get_float_attr(node, "epsilon"); + + // creating TF dialect FusedBatchNorm node + auto tf_fbn = graph->nodes()->create(); + tf_fbn->epsilon(epsilon); + + TensorName output_name(node.name(), 0); + tensor_names->enroll(output_name, tf_fbn); + + std::vector fbn_input_names; + fbn_input_names.push_back(TensorName(node.input(0))); // input + fbn_input_names.push_back(TensorName(node.input(1))); // scale + fbn_input_names.push_back(TensorName(node.input(2))); // offset + fbn_input_names.push_back(TensorName(node.input(3))); // mean + fbn_input_names.push_back(TensorName(node.input(4))); // variance + + auto tf_fbn_update = stdex::make_unique(tf_fbn, fbn_input_names); + updates->enroll(std::move(tf_fbn_update)); +} + +} // namespace moco + +#include "moco/Import/GraphBuilderRegistry.h" + +REGISTER_OP_BUILDER(FusedBatchNorm, FusedBatchNormGraphBuilder) diff --git a/compiler/moco/import/src/Nodes/Identity.cpp b/compiler/moco/import/src/Nodes/Identity.cpp new file mode 100644 index 0000000..650fa66 --- /dev/null +++ b/compiler/moco/import/src/Nodes/Identity.cpp @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2019 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 "moco/Import/Nodes/Identity.h" + +#include + +#include +#include +#include + +#include + +namespace +{ + +using namespace moco; + +class TFIdentityGraphUpdate final : public GraphUpdate +{ +public: + TFIdentityGraphUpdate(TFIdentity *node, const std::vector &names) + : _node(node), _names(names) + { + } + + void input(const SymbolTable *) const override; + +private: + TFIdentity *_node; + const std::vector _names; +}; + +void TFIdentityGraphUpdate::input(const SymbolTable *tensor_names) const +{ + for (auto &name : _names) + { + loco::Node *target = tensor_names->node(name); + _node->input(target); + } +} + +} // namespace + +namespace moco +{ + +bool IdentityGraphBuilder::validate(const tensorflow::NodeDef &node) const +{ + if (node.input_size() < 1) // from TensorFlow lite toco + return false; + + return true; +} + +void IdentityGraphBuilder::build(const tensorflow::NodeDef &node, + GraphBuilderContext *context) const +{ + loco::Graph *graph = context->graph(); + SymbolTable *tensor_names = context->tensor_names(); + UpdateQueue *updates = context->updates(); + + // Create a Identity node + auto identity_node = graph->nodes()->create(); + + // register string-name to node + TensorName output_name(node.name(), 0); + tensor_names->enroll(output_name, identity_node); + + // Queue node input update + // TODO: Check if we really need multiple input handlings + std::vector names; + for (int i = 0; i < node.input_size(); ++i) + { + names.emplace_back(TensorName(node.input(i))); + } + auto update = stdex::make_unique(identity_node, names); + updates->enroll(std::move(update)); +} + +} // namespace moco + +#include "moco/Import/GraphBuilderRegistry.h" + +REGISTER_OP_BUILDER(Identity, IdentityGraphBuilder) diff --git a/compiler/moco/import/src/Nodes/MaxPool.cpp b/compiler/moco/import/src/Nodes/MaxPool.cpp new file mode 100644 index 0000000..976284f --- /dev/null +++ b/compiler/moco/import/src/Nodes/MaxPool.cpp @@ -0,0 +1,130 @@ +/* + * Copyright (c) 2019 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 "moco/Import/Nodes/MaxPool.h" + +#include + +#include + +#include "Convert.h" + +#include +#include +#include +#include + +#include +#include + +namespace +{ + +using namespace moco; + +class TFMaxPoolGraphUpdate final : public GraphUpdate +{ +public: + TFMaxPoolGraphUpdate(TFMaxPool *node, const TensorName &name) + : _maxpool_node(node), _input_name(name) + { + } + + void input(const SymbolTable *) const override; + +private: + TFMaxPool *_maxpool_node; + const TensorName _input_name; +}; + +void TFMaxPoolGraphUpdate::input(const SymbolTable *node_table) const +{ + loco::Node *input_node = node_table->node(_input_name); + _maxpool_node->input(input_node); +} + +} // namespace + +namespace moco +{ + +bool MaxPoolGraphBuilder::validate(const tensorflow::NodeDef &node) const +{ + // note: even though "data_format" is not entered when a model is written, + // TF seems to generate "data_format" field into a pb file + return plier::tf::has_attrs(node, {"T", "data_format", "ksize", "padding", "strides"}); +} + +void MaxPoolGraphBuilder::build(const tensorflow::NodeDef &node, GraphBuilderContext *context) const +{ + assert(context != nullptr); + + loco::Graph *graph = context->graph(); + SymbolTable *tensor_names = context->tensor_names(); + UpdateQueue *updates = context->updates(); + + // name of loco nodes + ::std::string node_name = node.name(); + + // tensorflow data_format: one of NHWC or NCHW. + auto data_layout = plier::tf::get_string_attr(node, "data_format"); + auto maxPool_node = graph->nodes()->create(); + maxPool_node->data_layout(data_layout); + + // padding + auto padding = moco::str_toupper(plier::tf::get_string_attr(node, "padding")); + maxPool_node->padding(padding); + + // ksize + auto tf_ksize = plier::tf::get_list_attr(node, "ksize"); + auto ksize = plier::tf::as_int64_list(tf_ksize); + if (ksize.size() != 4) + { + // TODO support ksize length for 1 and 2 + throw std::runtime_error("MaxPool only supports ksize length 4"); + } + maxPool_node->ksize(ksize); + + // strides + auto tf_strides = plier::tf::get_list_attr(node, "strides"); + auto strides = plier::tf::as_int64_list(tf_strides); + if (strides.size() != 4) + { + // TODO support strides length for 1 and 2 + throw std::runtime_error("MaxPool only supports strides length 4"); + } + maxPool_node->strides(strides); + + // To set the input node of encode_node with node_name + TensorName output_name(node_name, 0); + tensor_names->enroll(output_name, maxPool_node); + + // Record ifm inputs to featureEncode_node + auto update = stdex::make_unique(maxPool_node, TensorName(node.input(0))); + + updates->enroll(std::move(update)); +} + +} // namespace moco + +#include "moco/Import/GraphBuilderRegistry.h" + +REGISTER_OP_BUILDER(MaxPool, MaxPoolGraphBuilder) + +// TODO Consider a case when TF MaxPool is for 3D. +// MaxPool works for 2D and other Dimensions, such as 3D +// So, in future, some other GraphBuilder decide if MaxPoolGraphBuilder is used or +// other GraphBuilder is used for TF MaxPool diff --git a/compiler/moco/import/src/Nodes/Mean.cpp b/compiler/moco/import/src/Nodes/Mean.cpp new file mode 100644 index 0000000..b7664b3 --- /dev/null +++ b/compiler/moco/import/src/Nodes/Mean.cpp @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2019 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 "moco/Import/Nodes/Mean.h" + +#include + +#include +#include +#include + +namespace +{ +using namespace moco; + +/** + * @brief GraphUpdate for Mean node + */ +class MeanGraphUpdate final : public GraphUpdate +{ +public: + MeanGraphUpdate(TFMean *node, const TensorName &&input_name, + const TensorName &&reduction_indices_name) + : _node(node), _input_name(input_name), _reduction_indices_name(reduction_indices_name) + { + // DO NOTHING + } + + void input(const SymbolTable *) const override; + +private: + TFMean *_node; + const TensorName _input_name; + const TensorName _reduction_indices_name; +}; + +void MeanGraphUpdate::input(const SymbolTable *table) const +{ + loco::Node *input_node = table->node(_input_name); + loco::Node *reduction_indices_node = table->node(_reduction_indices_name); + _node->input(input_node); + _node->reduction_indices(reduction_indices_node); +} + +} // namespace + +namespace moco +{ + +bool MeanGraphBuilder::validate(const tensorflow::NodeDef &node) const +{ + if (node.input_size() != 2) + return false; + + if (plier::tf::has_attrs(node, {"T", "Tidx", "keep_dims"}) == false) + return false; + + auto dtype = plier::tf::get_datatype_attr(node, "Tidx"); + if (dtype != tensorflow::DataType::DT_INT32 && dtype != tensorflow::DataType::DT_INT64) + return false; + + return true; +} + +void MeanGraphBuilder::build(const tensorflow::NodeDef &node, GraphBuilderContext *context) const +{ + assert(context != nullptr); + + loco::Graph *graph = context->graph(); + SymbolTable *tensor_names = context->tensor_names(); + UpdateQueue *updates = context->updates(); + + // creating TF dialect Mean node + auto tf_mean = graph->nodes()->create(); + tf_mean->keep_dims(plier::tf::get_bool_attr(node, "keep_dims")); + + TensorName output_name(node.name(), 0); + tensor_names->enroll(output_name, tf_mean); + + auto update = stdex::make_unique(tf_mean, TensorName(node.input(0)), + TensorName(node.input(1))); + updates->enroll(std::move(update)); +} + +} // namespace moco + +#include "moco/Import/GraphBuilderRegistry.h" + +REGISTER_OP_BUILDER(Mean, MeanGraphBuilder) diff --git a/compiler/moco/import/src/Nodes/Mul.cpp b/compiler/moco/import/src/Nodes/Mul.cpp new file mode 100644 index 0000000..8176ee4 --- /dev/null +++ b/compiler/moco/import/src/Nodes/Mul.cpp @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2019 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 "moco/Import/Nodes/Mul.h" + +#include + +#include +#include + +namespace +{ + +using namespace moco; + +/** + * @brief GraphUpdate for TF Mul node + */ +class TFMulGraphUpdate final : public GraphUpdate +{ +public: + TFMulGraphUpdate(TFMul *node, std::vector names) : _node(node), _names(names) {} + + void input(const SymbolTable *) const override; + +private: + TFMul *_node; + std::vector _names; +}; + +void TFMulGraphUpdate::input(const SymbolTable *tensor_names) const +{ + int num_inputs = _names.size(); + assert(num_inputs == 2); + + _node->x(tensor_names->node(_names[0])); + _node->y(tensor_names->node(_names[1])); +} + +} // namespace + +namespace moco +{ + +bool MulGraphBuilder::validate(const tensorflow::NodeDef &node) const +{ + assert(node.input_size() == 2); + + return true; +} + +void MulGraphBuilder::build(const tensorflow::NodeDef &node, GraphBuilderContext *context) const +{ + assert(context != nullptr); + + loco::Graph *graph = context->graph(); + SymbolTable *tensor_names = context->tensor_names(); + UpdateQueue *updates = context->updates(); + + // creating TF dialect Mul node + auto tf_mul = graph->nodes()->create(); + + TensorName output_name(node.name(), 0); + tensor_names->enroll(output_name, tf_mul); + + std::vector add_input_names; + add_input_names.push_back(TensorName(node.input(0))); // x + add_input_names.push_back(TensorName(node.input(1))); // y + + auto tf_mul_update = stdex::make_unique(tf_mul, add_input_names); + updates->enroll(std::move(tf_mul_update)); +} + +} // namespace moco + +#include "moco/Import/GraphBuilderRegistry.h" + +REGISTER_OP_BUILDER(Mul, MulGraphBuilder) diff --git a/compiler/moco/import/src/Nodes/Pad.cpp b/compiler/moco/import/src/Nodes/Pad.cpp new file mode 100644 index 0000000..1798cac --- /dev/null +++ b/compiler/moco/import/src/Nodes/Pad.cpp @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2019 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 "moco/Import/Nodes/Pad.h" + +#include + +#include +#include +#include + +namespace +{ + +using namespace moco; + +/** + * @brief GraphUpdate for TF Pad node + */ +class TFPadGraphUpdate final : public GraphUpdate +{ +public: + TFPadGraphUpdate(TFPad *node, std::vector names) : _node(node), _names(names) {} + + void input(const SymbolTable *) const override; + +private: + TFPad *_node; + std::vector _names; +}; + +void TFPadGraphUpdate::input(const SymbolTable *table) const +{ + int num_inputs = _names.size(); + assert(num_inputs == 2); + + _node->input(table->node(_names[0])); + _node->paddings(table->node(_names[1])); +} + +} // namespace + +namespace moco +{ + +bool PadGraphBuilder::validate(const tensorflow::NodeDef &node) const +{ + assert(node.input_size() == 2); + + return plier::tf::has_attrs(node, {"T", "Tpaddings"}); +} + +void PadGraphBuilder::build(const tensorflow::NodeDef &node, GraphBuilderContext *context) const +{ + assert(context != nullptr); + + loco::Graph *graph = context->graph(); + SymbolTable *tensor_names = context->tensor_names(); + UpdateQueue *updates = context->updates(); + + // creating TF dialect Pad node + auto tf_pad = graph->nodes()->create(); + + // register string-name to node + TensorName output_name(node.name(), 0); + tensor_names->enroll(output_name, tf_pad); + + std::vector add_input_names; + add_input_names.push_back(TensorName(node.input(0))); // input + add_input_names.push_back(TensorName(node.input(1))); // paddings + + // Queue node input update + auto tf_pad_update = stdex::make_unique(tf_pad, add_input_names); + updates->enroll(std::move(tf_pad_update)); +} + +} // namespace moco + +#include "moco/Import/GraphBuilderRegistry.h" + +REGISTER_OP_BUILDER(Pad, PadGraphBuilder) diff --git a/compiler/moco/import/src/Nodes/Placeholder.cpp b/compiler/moco/import/src/Nodes/Placeholder.cpp new file mode 100644 index 0000000..78bf775 --- /dev/null +++ b/compiler/moco/import/src/Nodes/Placeholder.cpp @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2019 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 "moco/Import/Nodes/Placeholder.h" + +#include +#include + +#include +#include + +namespace moco +{ + +bool PlaceholderGraphBuilder::validate(const tensorflow::NodeDef &node) const +{ + return plier::tf::has_attrs(node, {"dtype", "shape"}); +} + +void PlaceholderGraphBuilder::build(const tensorflow::NodeDef &node, + GraphBuilderContext *context) const +{ + assert(context != nullptr); + + loco::Graph *graph = context->graph(); + SymbolTable *tensor_names = context->tensor_names(); + + loco::DataType dtype = plier::tf::as_loco_datatype(plier::tf::get_datatype_attr(node, "dtype")); + const auto &shape = plier::tf::get_shape_attr(node, "shape"); + // TODO handle for unknown rank + assert(!shape.unknown_rank()); + int64_t num_dims = shape.dim_size(); + + // TODO support other types + assert(dtype == loco::DataType::FLOAT32); + + // Create a "pull" node as an input + auto pull_node = graph->nodes()->create(); + + pull_node->dtype(dtype); + + // Setting shape info. + pull_node->rank(num_dims); + for (int64_t d = 0; d < num_dims; d++) + { + assert(shape.dim(d).size() < std::numeric_limits::max()); + int64_t dim_value = shape.dim(d).size(); + if (dim_value >= 0) + { + uint32_t dim_value32 = static_cast(dim_value); + pull_node->dim(d) = dim_value32; + } + else + { + pull_node->dim(d).unset(); + // TODO Remove assert() and do implement + // NOTE Current implementation assumes dim is all know + assert(false); + } + } + + // register string-name to node + TensorName output_name(node.name(), 0); + tensor_names->enroll(output_name, pull_node); +} + +} // namespace moco + +#include "moco/Import/GraphBuilderRegistry.h" + +REGISTER_OP_BUILDER(Placeholder, PlaceholderGraphBuilder) diff --git a/compiler/moco/import/src/Nodes/RealDiv.cpp b/compiler/moco/import/src/Nodes/RealDiv.cpp new file mode 100644 index 0000000..d5114ec --- /dev/null +++ b/compiler/moco/import/src/Nodes/RealDiv.cpp @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2019 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 "moco/Import/Nodes/RealDiv.h" + +#include + +#include +#include + +namespace +{ + +using namespace moco; + +/** + * @brief GraphUpdate for TF RealDiv node + */ +class TFRealDivGraphUpdate final : public GraphUpdate +{ +public: + TFRealDivGraphUpdate(TFRealDiv *node, std::vector names) : _node(node), _names(names) + { + } + + void input(const SymbolTable *) const override; + +private: + TFRealDiv *_node; + std::vector _names; +}; + +void TFRealDivGraphUpdate::input(const SymbolTable *tensor_names) const +{ + int num_inputs = _names.size(); + assert(num_inputs == 2); + + _node->x(tensor_names->node(_names[0])); + _node->y(tensor_names->node(_names[1])); +} + +} // namespace + +namespace moco +{ +bool RealDivGraphBuilder::validate(const tensorflow::NodeDef &node) const +{ + assert(node.input_size() == 2); + + return true; +} + +void RealDivGraphBuilder::build(const tensorflow::NodeDef &node, GraphBuilderContext *context) const +{ + assert(context != nullptr); + + loco::Graph *graph = context->graph(); + SymbolTable *tensor_names = context->tensor_names(); + UpdateQueue *updates = context->updates(); + + // creating TF dialect RealDiv node + auto tf_div = graph->nodes()->create(); + + TensorName output_name(node.name(), 0); + tensor_names->enroll(output_name, tf_div); + + std::vector div_input_names; + div_input_names.push_back(TensorName(node.input(0))); // x + div_input_names.push_back(TensorName(node.input(1))); // y + + auto tf_div_update = stdex::make_unique(tf_div, div_input_names); + updates->enroll(std::move(tf_div_update)); +} + +} // namespace moco + +#include "moco/Import/GraphBuilderRegistry.h" + +REGISTER_OP_BUILDER(RealDiv, RealDivGraphBuilder) diff --git a/compiler/moco/import/src/Nodes/Relu.cpp b/compiler/moco/import/src/Nodes/Relu.cpp new file mode 100644 index 0000000..1736eba --- /dev/null +++ b/compiler/moco/import/src/Nodes/Relu.cpp @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2019 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 "moco/Import/Nodes/Relu.h" + +#include + +#include +#include +#include + +#include +#include + +namespace +{ + +using namespace moco; + +class TFReluGraphUpdate final : public GraphUpdate +{ +public: + TFReluGraphUpdate(TFRelu *node, const TensorName &&name) : _node(node), _name(name) {} + + void input(const SymbolTable *) const override; + +private: + TFRelu *_node; + const TensorName _name; +}; + +void TFReluGraphUpdate::input(const SymbolTable *table) const +{ + loco::Node *target = table->node(_name); + _node->features(target); +} + +} // namespace + +namespace moco +{ + +bool ReluGraphBuilder::validate(const tensorflow::NodeDef &node) const +{ + // ReLU node SHOULD have only one input + if (node.input_size() != 1) + return false; + + return true; +} + +void ReluGraphBuilder::build(const tensorflow::NodeDef &node, GraphBuilderContext *context) const +{ + assert(context != nullptr); + + loco::Graph *graph = context->graph(); + SymbolTable *tensor_names = context->tensor_names(); + UpdateQueue *updates = context->updates(); + + // Create a "TFRelu" node for Relu + auto relu_node = graph->nodes()->create(); + + // register string-name to node + TensorName output_name(node.name(), 0); + tensor_names->enroll(output_name, relu_node); + + // Queue node input update + auto update = stdex::make_unique(relu_node, TensorName(node.input(0))); + updates->enroll(std::move(update)); +} + +} // namespace moco + +#include "moco/Import/GraphBuilderRegistry.h" + +REGISTER_OP_BUILDER(Relu, ReluGraphBuilder) diff --git a/compiler/moco/import/src/Nodes/Relu6.cpp b/compiler/moco/import/src/Nodes/Relu6.cpp new file mode 100644 index 0000000..9fbbf1d --- /dev/null +++ b/compiler/moco/import/src/Nodes/Relu6.cpp @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2019 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 "moco/Import/Nodes/Relu6.h" + +#include + +#include + +namespace +{ + +using namespace moco; + +class TFRelu6GraphUpdate final : public GraphUpdate +{ +public: + TFRelu6GraphUpdate(TFRelu6 *node, const TensorName &&name) : _node(node), _name(name) {} + + void input(const SymbolTable *) const override; + +private: + TFRelu6 *_node; + const TensorName _name; +}; + +void TFRelu6GraphUpdate::input(const SymbolTable *table) const +{ + loco::Node *target = table->node(_name); + _node->features(target); +} + +} // namespace + +namespace moco +{ + +bool Relu6GraphBuilder::validate(const tensorflow::NodeDef &node) const +{ + // ReLU6 node SHOULD have only one input + if (node.input_size() != 1) + return false; + return true; +} + +void Relu6GraphBuilder::build(const tensorflow::NodeDef &node, GraphBuilderContext *context) const +{ + assert(context != nullptr); + + loco::Graph *graph = context->graph(); + SymbolTable *tensor_names = context->tensor_names(); + UpdateQueue *updates = context->updates(); + + // Create a "TFRelu6" node for Relu + auto relu_node = graph->nodes()->create(); + + // register string-name to node + TensorName output_name(node.name(), 0); + tensor_names->enroll(output_name, relu_node); + + // Queue node input update + auto update = stdex::make_unique(relu_node, TensorName(node.input(0))); + updates->enroll(std::move(update)); +} + +} // namespace moco + +#include "moco/Import/GraphBuilderRegistry.h" + +REGISTER_OP_BUILDER(Relu6, Relu6GraphBuilder) diff --git a/compiler/moco/import/src/Nodes/Reshape.cpp b/compiler/moco/import/src/Nodes/Reshape.cpp new file mode 100644 index 0000000..60bc72d --- /dev/null +++ b/compiler/moco/import/src/Nodes/Reshape.cpp @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2019 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 "moco/Import/Nodes/Reshape.h" + +#include + +#include +#include +#include +#include + +#include +#include + +namespace +{ +using namespace moco; + +class ReshapeGraphUpdate final : public GraphUpdate +{ +public: + ReshapeGraphUpdate(TFReshape *node, std::vector names) : _node(node), _names(names) {} + + void input(const SymbolTable *) const override; + +private: + TFReshape *_node; + std::vector _names; +}; + +void ReshapeGraphUpdate::input(const SymbolTable *node_table) const +{ + assert(_names.size() == 2); + + auto tensor_node = node_table->node(_names[0]); + auto shape_node = node_table->node(_names[1]); + + assert(tensor_node != nullptr); + assert(shape_node != nullptr); + + _node->tensor(tensor_node); + _node->shape(shape_node); +} + +} // namespace + +namespace moco +{ + +bool ReshapeGraphBuilder::validate(const tensorflow::NodeDef &node) const +{ + // Tensorflow Reshape has 2 inputs: tensor & shape + if (node.input_size() != 2) + return false; + + // TODO Assert Tshape value is DT_INT32? + return plier::tf::has_attrs(node, {"T", "Tshape"}); +} + +void ReshapeGraphBuilder::build(const tensorflow::NodeDef &node, GraphBuilderContext *context) const +{ + assert(context != nullptr); + + loco::Graph *graph = context->graph(); + SymbolTable *tensor_names = context->tensor_names(); + UpdateQueue *updates = context->updates(); + + // name of loco nodes + std::string reshape_name = node.name(); + + auto reshape = graph->nodes()->create(); + + // save the name for graph link updates + TensorName output_name(reshape_name, 0); + tensor_names->enroll(output_name, reshape); + + std::vector input_names; + input_names.push_back(TensorName(node.input(0))); // tensor + input_names.push_back(TensorName(node.input(1))); // shape + + // Queue node input update + auto update = stdex::make_unique(reshape, input_names); + + updates->enroll(std::move(update)); +} + +} // namespace moco + +#include "moco/Import/GraphBuilderRegistry.h" + +REGISTER_OP_BUILDER(Reshape, ReshapeGraphBuilder) diff --git a/compiler/moco/import/src/Nodes/Rsqrt.cpp b/compiler/moco/import/src/Nodes/Rsqrt.cpp new file mode 100644 index 0000000..87eccae --- /dev/null +++ b/compiler/moco/import/src/Nodes/Rsqrt.cpp @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2019 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 "moco/Import/Nodes/Rsqrt.h" + +#include + +#include +#include + +namespace +{ + +using namespace moco; + +/** + * @brief GraphUpdate for TF Rsqrt node + */ +class TFRsqrtGraphUpdate final : public GraphUpdate +{ +public: + TFRsqrtGraphUpdate(TFRsqrt *node, TensorName &&name) : _node(node), _name(name) {} + + void input(const SymbolTable *) const override; + +private: + TFRsqrt *_node; + TensorName _name; +}; + +void TFRsqrtGraphUpdate::input(const SymbolTable *table) const +{ + loco::Node *target = table->node(_name); + _node->x(target); +} + +} // namespace + +namespace moco +{ + +bool RsqrtGraphBuilder::validate(const tensorflow::NodeDef &node) const +{ + assert(node.input_size() == 1); + + return true; +} + +void RsqrtGraphBuilder::build(const tensorflow::NodeDef &node, GraphBuilderContext *context) const +{ + assert(context != nullptr); + + loco::Graph *graph = context->graph(); + SymbolTable *tensor_names = context->tensor_names(); + UpdateQueue *updates = context->updates(); + + // creating TF dialect Rsqrt node + auto tf_rsqrt = graph->nodes()->create(); + + // register string-name to node + TensorName output_name(node.name(), 0); + tensor_names->enroll(output_name, tf_rsqrt); + + // Queue node input update + auto tf_rsqrt_update = + stdex::make_unique(tf_rsqrt, TensorName(node.input(0))); + updates->enroll(std::move(tf_rsqrt_update)); +} + +} // namespace moco + +#include "moco/Import/GraphBuilderRegistry.h" + +REGISTER_OP_BUILDER(Rsqrt, RsqrtGraphBuilder) diff --git a/compiler/moco/import/src/Nodes/Shape.cpp b/compiler/moco/import/src/Nodes/Shape.cpp new file mode 100644 index 0000000..b055b87 --- /dev/null +++ b/compiler/moco/import/src/Nodes/Shape.cpp @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2019 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 "moco/Import/Nodes/Shape.h" + +#include + +#include +#include +#include + +namespace +{ +using namespace moco; + +/** + * @brief GraphUpdate for Shape node + */ +class ShapeGraphUpdate final : public GraphUpdate +{ +public: + ShapeGraphUpdate(TFShape *node, const TensorName &&input_name) + : _node(node), _input_name(input_name) + { + // DO NOTHING + } + + void input(const SymbolTable *) const override; + +private: + TFShape *_node; + const TensorName _input_name; +}; + +void ShapeGraphUpdate::input(const SymbolTable *table) const +{ + loco::Node *input_node = table->node(_input_name); + _node->input(input_node); +} + +} // namespace + +namespace moco +{ + +bool ShapeGraphBuilder::validate(const tensorflow::NodeDef &node) const +{ + assert(node.input_size() == 1); + + return plier::tf::has_attrs(node, {"T"}); +} + +void ShapeGraphBuilder::build(const tensorflow::NodeDef &node, GraphBuilderContext *context) const +{ + assert(context != nullptr); + + loco::Graph *graph = context->graph(); + SymbolTable *tensor_names = context->tensor_names(); + UpdateQueue *updates = context->updates(); + + // create TF dialect Shape node + auto tf_shape = graph->nodes()->create(); + + if (plier::tf::has_attrs(node, {"out_type"})) + { + auto dtype = plier::tf::as_loco_datatype(plier::tf::get_datatype_attr(node, "out_type")); + // TODO Support other dtype like S64 + assert(dtype == loco::DataType::S32); + + tf_shape->dtype(dtype); + } + else + { + // Set to S32, TF-documented default value for 'out_type' + tf_shape->dtype(loco::DataType::S32); + } + + TensorName output_name(node.name(), 0); + tensor_names->enroll(output_name, tf_shape); + + auto update = stdex::make_unique(tf_shape, TensorName(node.input(0))); + updates->enroll(std::move(update)); +} + +} // namespace moco + +#include "moco/Import/GraphBuilderRegistry.h" + +REGISTER_OP_BUILDER(Shape, ShapeGraphBuilder) diff --git a/compiler/moco/import/src/Nodes/Softmax.cpp b/compiler/moco/import/src/Nodes/Softmax.cpp new file mode 100644 index 0000000..e606ef7 --- /dev/null +++ b/compiler/moco/import/src/Nodes/Softmax.cpp @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2019 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 "moco/Import/Nodes/Softmax.h" + +#include + +#include +#include +#include + +namespace +{ +using namespace moco; + +/** +* @brief GraphUpdate for Softmax node +*/ +class SoftmaxGraphUpdate final : public GraphUpdate +{ +public: + SoftmaxGraphUpdate(TFSoftmax *node, const TensorName &&input_name) + : _node(node), _input_name(input_name) + { + // DO NOTHING + } + + void input(const SymbolTable *) const override; + +private: + TFSoftmax *_node; + const TensorName _input_name; +}; + +void SoftmaxGraphUpdate::input(const SymbolTable *table) const +{ + loco::Node *input_node = table->node(_input_name); + _node->logits(input_node); +} + +} // namespace + +namespace moco +{ + +bool SoftmaxGraphBuilder::validate(const tensorflow::NodeDef &node) const +{ + assert(node.input_size() == 1); + + return plier::tf::has_attrs(node, {"T"}); +} + +void SoftmaxGraphBuilder::build(const tensorflow::NodeDef &node, GraphBuilderContext *context) const +{ + assert(context != nullptr); + + loco::Graph *graph = context->graph(); + SymbolTable *tensor_names = context->tensor_names(); + UpdateQueue *updates = context->updates(); + + // creating TF dialect Softmax node + auto tf_softmax = graph->nodes()->create(); + + TensorName output_name(node.name(), 0); + tensor_names->enroll(output_name, tf_softmax); + + auto update = stdex::make_unique(tf_softmax, TensorName(node.input(0))); + updates->enroll(std::move(update)); +} + +} // namespace moco + +#include "moco/Import/GraphBuilderRegistry.h" + +REGISTER_OP_BUILDER(Softmax, SoftmaxGraphBuilder) diff --git a/compiler/moco/import/src/Nodes/Sqrt.cpp b/compiler/moco/import/src/Nodes/Sqrt.cpp new file mode 100644 index 0000000..6880054 --- /dev/null +++ b/compiler/moco/import/src/Nodes/Sqrt.cpp @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2019 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 "moco/Import/Nodes/Sqrt.h" + +#include + +#include +#include + +namespace +{ + +using namespace moco; + +/** + * @brief GraphUpdate for TF Sqrt node + */ +class TFSqrtGraphUpdate final : public GraphUpdate +{ +public: + TFSqrtGraphUpdate(TFSqrt *node, TensorName &&name) : _node(node), _name(name) {} + + void input(const SymbolTable *) const override; + +private: + TFSqrt *_node; + TensorName _name; +}; + +void TFSqrtGraphUpdate::input(const SymbolTable *table) const +{ + loco::Node *target = table->node(_name); + _node->x(target); +} + +} // namespace + +namespace moco +{ + +bool SqrtGraphBuilder::validate(const tensorflow::NodeDef &node) const +{ + assert(node.input_size() == 1); + + return true; +} + +void SqrtGraphBuilder::build(const tensorflow::NodeDef &node, GraphBuilderContext *context) const +{ + assert(context != nullptr); + + loco::Graph *graph = context->graph(); + SymbolTable *tensor_names = context->tensor_names(); + UpdateQueue *updates = context->updates(); + + // creating TF dialect Sqrt node + auto tf_sqrt = graph->nodes()->create(); + + // register string-name to node + TensorName output_name(node.name(), 0); + tensor_names->enroll(output_name, tf_sqrt); + + // Queue node input update + auto tf_sqrt_update = stdex::make_unique(tf_sqrt, TensorName(node.input(0))); + updates->enroll(std::move(tf_sqrt_update)); +} + +} // namespace moco + +#include "moco/Import/GraphBuilderRegistry.h" + +REGISTER_OP_BUILDER(Sqrt, SqrtGraphBuilder) diff --git a/compiler/moco/import/src/Nodes/SquaredDifference.cpp b/compiler/moco/import/src/Nodes/SquaredDifference.cpp new file mode 100644 index 0000000..5f80128 --- /dev/null +++ b/compiler/moco/import/src/Nodes/SquaredDifference.cpp @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2019 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 "moco/Import/Nodes/SquaredDifference.h" + +#include + +#include +#include + +namespace +{ + +using namespace moco; + +/** + * @brief GraphUpdate for TF SquaredDifference node + */ +class TFSquaredDifferenceGraphUpdate final : public GraphUpdate +{ +public: + TFSquaredDifferenceGraphUpdate(TFSquaredDifference *node, std::vector names) + : _node(node), _names(names) + { + } + + void input(const SymbolTable *) const override; + +private: + TFSquaredDifference *_node; + std::vector _names; +}; + +void TFSquaredDifferenceGraphUpdate::input(const SymbolTable *table) const +{ + int num_inputs = _names.size(); + assert(num_inputs == 2); + + _node->x(table->node(_names[0])); + _node->y(table->node(_names[1])); +} + +} // namespace + +namespace moco +{ + +bool SquaredDifferenceGraphBuilder::validate(const tensorflow::NodeDef &node) const +{ + assert(node.input_size() == 2); + + return true; +} + +void SquaredDifferenceGraphBuilder::build(const tensorflow::NodeDef &node, + GraphBuilderContext *context) const +{ + assert(context != nullptr); + + loco::Graph *graph = context->graph(); + SymbolTable *tensor_names = context->tensor_names(); + UpdateQueue *updates = context->updates(); + + // creating TF dialect SquaredDifference node + auto tf_sqdiff = graph->nodes()->create(); + + // register string-name to node + TensorName output_name(node.name(), 0); + tensor_names->enroll(output_name, tf_sqdiff); + + std::vector add_input_names; + add_input_names.push_back(TensorName(node.input(0))); // x + add_input_names.push_back(TensorName(node.input(1))); // y + + // Queue node input update + auto tf_sqrt_update = + stdex::make_unique(tf_sqdiff, add_input_names); + updates->enroll(std::move(tf_sqrt_update)); +} + +} // namespace moco + +#include "moco/Import/GraphBuilderRegistry.h" + +REGISTER_OP_BUILDER(SquaredDifference, SquaredDifferenceGraphBuilder) diff --git a/compiler/moco/import/src/Nodes/Squeeze.cpp b/compiler/moco/import/src/Nodes/Squeeze.cpp new file mode 100644 index 0000000..90d189b --- /dev/null +++ b/compiler/moco/import/src/Nodes/Squeeze.cpp @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2019 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 "moco/Import/Nodes/Squeeze.h" + +#include + +#include + +#include +#include +#include + +namespace +{ +using namespace moco; + +/** + * @brief GraphUpdate for Squeeze node + */ +class SqueezeGraphUpdate final : public GraphUpdate +{ +public: + SqueezeGraphUpdate(TFSqueeze *node, const TensorName &&input_name) + : _node(node), _input_name(input_name) + { + // DO NOTHING + } + + void input(const SymbolTable *) const override; + +private: + TFSqueeze *_node; + const TensorName _input_name; +}; + +void SqueezeGraphUpdate::input(const SymbolTable *table) const +{ + loco::Node *input_node = table->node(_input_name); + _node->input(input_node); +} + +} // namespace + +namespace moco +{ + +bool SqueezeGraphBuilder::validate(const tensorflow::NodeDef &node) const +{ + assert(node.input_size() == 1); + + return plier::tf::has_attrs(node, {"T"}); +} + +void SqueezeGraphBuilder::build(const tensorflow::NodeDef &node, GraphBuilderContext *context) const +{ + assert(context != nullptr); + + loco::Graph *graph = context->graph(); + SymbolTable *tensor_names = context->tensor_names(); + UpdateQueue *updates = context->updates(); + + if (plier::tf::has_attrs(node, {"axis"})) + { + // TODO support 'axis' attribute + std::runtime_error("Import Squeeze: 'axis' attribute is not supported yet"); + } + + std::vector squeeze_dims; + if (plier::tf::has_attrs(node, {"squeeze_dims"})) + { + auto squeeze_dim_list = plier::tf::get_list_attr(node, {"squeeze_dims"}); + // TODO assert squeeze_dims are mutually different? + squeeze_dims = plier::tf::as_int64_list(squeeze_dim_list); + } + // Note that it is possible that NodeDef does not have squeeze_dims attribute. + // In that case, TFSqueeze also has empty squeeze_dims, + + // creating TF dialect Squeeze node + auto tf_squeeze = graph->nodes()->create(); + tf_squeeze->squeeze_dims(squeeze_dims); + + TensorName output_name(node.name(), 0); + tensor_names->enroll(output_name, tf_squeeze); + + auto update = stdex::make_unique(tf_squeeze, TensorName(node.input(0))); + updates->enroll(std::move(update)); +} + +} // namespace moco + +#include "moco/Import/GraphBuilderRegistry.h" + +REGISTER_OP_BUILDER(Squeeze, SqueezeGraphBuilder) diff --git a/compiler/moco/import/src/Nodes/StopGradient.cpp b/compiler/moco/import/src/Nodes/StopGradient.cpp new file mode 100644 index 0000000..9b924b8 --- /dev/null +++ b/compiler/moco/import/src/Nodes/StopGradient.cpp @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2019 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 "moco/Import/Nodes/StopGradient.h" + +#include + +#include +#include +#include + +namespace +{ + +using namespace moco; + +/** + * @brief GraphUpdate for TF StopGradient node + */ +class TFStopGradientGraphUpdate final : public GraphUpdate +{ +public: + TFStopGradientGraphUpdate(TFStopGradient *node, TensorName &&name) : _node(node), _name(name) {} + + void input(const SymbolTable *) const override; + +private: + TFStopGradient *_node; + TensorName _name; +}; + +void TFStopGradientGraphUpdate::input(const SymbolTable *table) const +{ + loco::Node *target = table->node(_name); + _node->input(target); +} + +} // namespace + +namespace moco +{ + +bool StopGradientGraphBuilder::validate(const tensorflow::NodeDef &node) const +{ + assert(node.input_size() == 1); + + return plier::tf::has_attrs(node, {"T"}); +} + +void StopGradientGraphBuilder::build(const tensorflow::NodeDef &node, + GraphBuilderContext *context) const +{ + assert(context != nullptr); + + loco::Graph *graph = context->graph(); + SymbolTable *tensor_names = context->tensor_names(); + UpdateQueue *updates = context->updates(); + + // creating TF dialect StopGradient node + auto tf_stopgradient = graph->nodes()->create(); + + // register string-name to node + TensorName output_name(node.name(), 0); + tensor_names->enroll(output_name, tf_stopgradient); + + // Queue node input update + auto tf_stopgradient_update = + stdex::make_unique(tf_stopgradient, TensorName(node.input(0))); + updates->enroll(std::move(tf_stopgradient_update)); +} + +} // namespace moco + +#include "moco/Import/GraphBuilderRegistry.h" + +REGISTER_OP_BUILDER(StopGradient, StopGradientGraphBuilder) diff --git a/compiler/moco/import/src/Nodes/Sub.cpp b/compiler/moco/import/src/Nodes/Sub.cpp new file mode 100644 index 0000000..8cd4f08 --- /dev/null +++ b/compiler/moco/import/src/Nodes/Sub.cpp @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2019 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 "moco/Import/Nodes/Sub.h" + +#include + +#include +#include + +namespace +{ + +using namespace moco; + +/** + * @brief GraphUpdate for TF Sub node + */ +class TFSubGraphUpdate final : public GraphUpdate +{ +public: + TFSubGraphUpdate(TFSub *node, std::vector names) : _node(node), _names(names) {} + + void input(const SymbolTable *) const override; + +private: + TFSub *_node; + std::vector _names; +}; + +void TFSubGraphUpdate::input(const SymbolTable *tensor_names) const +{ + int num_inputs = _names.size(); + assert(num_inputs == 2); + + _node->x(tensor_names->node(_names[0])); + _node->y(tensor_names->node(_names[1])); +} + +} // namespace + +namespace moco +{ + +bool SubGraphBuilder::validate(const tensorflow::NodeDef &node) const +{ + assert(node.input_size() == 2); + + return true; +} + +void SubGraphBuilder::build(const tensorflow::NodeDef &node, GraphBuilderContext *context) const +{ + assert(context != nullptr); + + loco::Graph *graph = context->graph(); + SymbolTable *tensor_names = context->tensor_names(); + UpdateQueue *updates = context->updates(); + + // creating TF dialect Sub node + auto tf_sub = graph->nodes()->create(); + + TensorName output_name(node.name(), 0); + tensor_names->enroll(output_name, tf_sub); + + std::vector sub_input_names; + sub_input_names.push_back(TensorName(node.input(0))); // x + sub_input_names.push_back(TensorName(node.input(1))); // y + + auto tf_sub_update = stdex::make_unique(tf_sub, sub_input_names); + updates->enroll(std::move(tf_sub_update)); +} + +} // namespace moco + +#include "moco/Import/GraphBuilderRegistry.h" + +REGISTER_OP_BUILDER(Sub, SubGraphBuilder) diff --git a/compiler/moco/import/src/Nodes/Tanh.cpp b/compiler/moco/import/src/Nodes/Tanh.cpp new file mode 100644 index 0000000..4ab2c4f --- /dev/null +++ b/compiler/moco/import/src/Nodes/Tanh.cpp @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2019 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 "moco/Import/Nodes/Tanh.h" + +#include + +#include +#include + +namespace +{ + +using namespace moco; + +/** + * @brief GraphUpdate for TF Tanh node + */ +class TFTanhGraphUpdate final : public GraphUpdate +{ +public: + TFTanhGraphUpdate(TFTanh *node, TensorName &&name) : _node(node), _name(name) {} + + void input(const SymbolTable *) const override; + +private: + TFTanh *_node; + TensorName _name; +}; + +void TFTanhGraphUpdate::input(const SymbolTable *table) const +{ + loco::Node *target = table->node(_name); + _node->x(target); +} + +} // namespace + +namespace moco +{ + +bool TanhGraphBuilder::validate(const tensorflow::NodeDef &node) const +{ + assert(node.input_size() == 1); + + return true; +} + +void TanhGraphBuilder::build(const tensorflow::NodeDef &node, GraphBuilderContext *context) const +{ + assert(context != nullptr); + + loco::Graph *graph = context->graph(); + SymbolTable *tensor_names = context->tensor_names(); + UpdateQueue *updates = context->updates(); + + // creating TF dialect Tanh node + auto tf_tanh = graph->nodes()->create(); + + // register string-name to node + TensorName output_name(node.name(), 0); + tensor_names->enroll(output_name, tf_tanh); + + // Queue node input update + auto tf_tanh_update = stdex::make_unique(tf_tanh, TensorName(node.input(0))); + updates->enroll(std::move(tf_tanh_update)); +} + +} // namespace moco + +#include "moco/Import/GraphBuilderRegistry.h" + +REGISTER_OP_BUILDER(Tanh, TanhGraphBuilder) -- 2.7.4