[locoex] TypeInferenceRule for custom op (#6170)
author윤현식/On-Device Lab(SR)/Principal Engineer/삼성전자 <hyunsik.yoon@samsung.com>
Mon, 5 Aug 2019 00:14:31 +0000 (09:14 +0900)
committer박종현/On-Device Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Mon, 5 Aug 2019 00:14:31 +0000 (09:14 +0900)
TypeInferenceRule and a test for custom op was committed.

Signed-off-by: Hyun Sik Yoon <hyunsik.yoon@samsung.com>
compiler/locoex-customop/include/locoex/Service/COpTypeInference.h [new file with mode: 0644]
compiler/locoex-customop/src/Service/COpTypeInference.cpp [new file with mode: 0644]
compiler/locoex-customop/src/Service/COpTypeInference.test.cpp [new file with mode: 0644]

diff --git a/compiler/locoex-customop/include/locoex/Service/COpTypeInference.h b/compiler/locoex-customop/include/locoex/Service/COpTypeInference.h
new file mode 100644 (file)
index 0000000..13163a5
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * 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 __LOCOEX_SERVICE_TYPE_INFERENCE_H__
+#define __LOCOEX_SERVICE_TYPE_INFERENCE_H__
+
+#include <loco/Service/TypeInference.h>
+
+namespace locoex
+{
+
+/**
+ * @brief Type Inference Rule for COpDialect
+ */
+struct COpTypeInferenceRule final : public loco::TypeInferenceRule
+{
+  bool recognize(const loco::Dialect *) const final;
+  bool infer(const loco::Node *, loco::DataType &) const final;
+};
+
+} // namespace locoex
+
+#endif // __LOCOEX_SERVICE_TYPE_INFERENCE_H__
diff --git a/compiler/locoex-customop/src/Service/COpTypeInference.cpp b/compiler/locoex-customop/src/Service/COpTypeInference.cpp
new file mode 100644 (file)
index 0000000..b41454e
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * 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 "locoex/Service/COpTypeInference.h"
+
+#include "locoex/COpDialect.h"
+#include "locoex/COpCall.h"
+
+#include <cassert>
+
+namespace locoex
+{
+
+bool COpTypeInferenceRule::recognize(const loco::Dialect *d) const
+{
+  // This rule recognizes only "COpDialect" dialect!
+  return COpDialect::get() == d;
+}
+
+bool COpTypeInferenceRule::infer(const loco::Node *node, loco::DataType &dtype) const
+{
+  assert(node->dialect() == COpDialect::get());
+
+  auto customop = dynamic_cast<const COpCall *>(node);
+
+  assert(customop != nullptr);
+  assert(customop->dtype() != loco::DataType::Unknown);
+
+  dtype = customop->dtype();
+
+  return true;
+}
+
+} // namespace locoex
diff --git a/compiler/locoex-customop/src/Service/COpTypeInference.test.cpp b/compiler/locoex-customop/src/Service/COpTypeInference.test.cpp
new file mode 100644 (file)
index 0000000..90ef1f2
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * 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 <locoex/Service/COpTypeInference.h>
+#include <locoex/COpCall.h>
+
+#include <gtest/gtest.h>
+
+TEST(TypeInferenceRuleTest, COpTypeInference)
+{
+  // Create a simple Relu6 network
+  auto g = loco::make_graph();
+
+  auto pull_node = g->nodes()->create<loco::Pull>();
+  pull_node->dtype(loco::DataType::FLOAT32);
+
+  auto call_node = g->nodes()->create<locoex::COpCall>(1);
+  call_node->input(0, pull_node);
+  call_node->dtype(loco::DataType::FLOAT32);
+
+  auto push_node = g->nodes()->create<loco::Push>();
+  push_node->from(call_node);
+
+  auto graph_input = g->inputs()->create();
+
+  graph_input->name("input");
+  graph_input->node(pull_node);
+
+  auto graph_output = g->outputs()->create();
+
+  graph_output->name("output");
+  graph_output->node(push_node);
+
+  // Run Type Inference
+  locoex::COpTypeInferenceRule rule;
+
+  loco::apply(&rule).to(g.get());
+
+  // Verify!
+  ASSERT_TRUE(loco::dtype_known(call_node));
+  ASSERT_EQ(loco::dtype_get(call_node), loco::DataType::FLOAT32);
+}