From 09ffda8e55ab946d07dee8337f90cfddce6c86c1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EA=B9=80=EC=9A=A9=EC=84=AD/On-Device=20Lab=28SR=29/Enginee?= =?utf8?q?r/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Thu, 7 Feb 2019 16:46:00 +0900 Subject: [PATCH] Introduce Subgraph as Node (#4373) * Introduce Subgraph as Node Introduce Subgraph as Node. This Subgraph would be handled as extrordinay Node so that running such as group could be possible. Signed-off-by: Yongseop Kim * Remove unnecessary header * Change addOperation name and index of Element of Subgraph --- .../neurun/src/backend/interface/IStageGenerator.h | 2 + runtimes/neurun/src/model/operation/NodeVisitor.h | 6 ++ runtimes/neurun/src/model/operation/Subgraph.cc | 61 +++++++++++++++++ runtimes/neurun/src/model/operation/Subgraph.h | 79 ++++++++++++++++++++++ 4 files changed, 148 insertions(+) create mode 100644 runtimes/neurun/src/model/operation/Subgraph.cc create mode 100644 runtimes/neurun/src/model/operation/Subgraph.h diff --git a/runtimes/neurun/src/backend/interface/IStageGenerator.h b/runtimes/neurun/src/backend/interface/IStageGenerator.h index 878a50e..20fe3c2 100644 --- a/runtimes/neurun/src/backend/interface/IStageGenerator.h +++ b/runtimes/neurun/src/backend/interface/IStageGenerator.h @@ -51,6 +51,8 @@ protected: virtual void visit(const model::operation::InternalName &) override {} #include "model/operation/Op.lst" #undef OP + // TODO: Fill this + virtual void visit(const model::operation::Subgraph &) override {} protected: void returnStage(const Stage &stage) { _return = stage; } diff --git a/runtimes/neurun/src/model/operation/NodeVisitor.h b/runtimes/neurun/src/model/operation/NodeVisitor.h index 8420de9..5a4ce23 100644 --- a/runtimes/neurun/src/model/operation/NodeVisitor.h +++ b/runtimes/neurun/src/model/operation/NodeVisitor.h @@ -26,6 +26,8 @@ namespace model namespace operation { +class Subgraph; + struct NodeVisitor { virtual ~NodeVisitor() = default; @@ -34,6 +36,10 @@ struct NodeVisitor virtual void visit(const InternalName &) {} #include "model/operation/Op.lst" #undef OP + + // This Subgraph node should be handled specially so that + // Op.lst doesn't have Subgraph + virtual void visit(const Subgraph &) {} }; } // namespace operation diff --git a/runtimes/neurun/src/model/operation/Subgraph.cc b/runtimes/neurun/src/model/operation/Subgraph.cc new file mode 100644 index 0000000..93883e0 --- /dev/null +++ b/runtimes/neurun/src/model/operation/Subgraph.cc @@ -0,0 +1,61 @@ +/* + * 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 "Subgraph.h" +#include "NodeVisitor.h" +#include + +namespace neurun +{ +namespace model +{ +namespace operation +{ + +Subgraph::Subgraph() : Node{OperandConstraint::createAny()} +{ + // DO NOTHING +} + +void Subgraph::accept(NodeVisitor &&v) const { v.visit(*this); } + +// TODO: Impl Dumper instead of this method +std::string Subgraph::getStr() const +{ + // " subgraph IN(xx,xx,xx) -> { op0, op1, op2 } -> OUT(yy,yy,yy)" + std::stringstream ss; + ss << " subgraph IN("; + for (const auto &index : getInputs()) + { + ss << " " << index.value(); + } + ss << " ) -> {"; + for (const auto &elem : _operations) + { + ss << " " << elem.index.value() << "(" << elem.node->getName() << ")"; + } + ss << " } -> OUT("; + for (const auto &index : getOutputs()) + { + ss << " " << index.value(); + } + ss << " )"; + return ss.str(); +} + +} // namespace operation +} // namespace model +} // neurun diff --git a/runtimes/neurun/src/model/operation/Subgraph.h b/runtimes/neurun/src/model/operation/Subgraph.h new file mode 100644 index 0000000..62d91a8 --- /dev/null +++ b/runtimes/neurun/src/model/operation/Subgraph.h @@ -0,0 +1,79 @@ +/* + * 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 __NEURUN_MODEL_OPERATION_SUBGRAPH_H__ +#define __NEURUN_MODEL_OPERATION_SUBGRAPH_H__ + +#include +#include +#include + +#include "Index.h" +#include "Node.h" + +namespace neurun +{ +namespace model +{ +namespace operation +{ + +struct Element +{ + const Index index; + const Node *node; + + Element(const Index *i, const Node *n) : index{*i}, node{n} + { + // DO NOTHING + } +}; + +class Subgraph : public Node +{ +public: + Subgraph(); + Subgraph(const Subgraph &) = delete; + +public: + virtual void accept(NodeVisitor &&visitor) const override; + + virtual std::string getName(void) const override { return "Subgraph"; } + +public: + void appendOperation(const Index &index, const Node &node) + { + _operations.emplace_back(&index, &node); + } + + std::vector &operations(void) { return _operations; } + + const std::vector &operations(void) const { return _operations; } + + uint32_t size(void) const { return _operations.size(); } + + // TODO: Impl Dumper instead of this method + std::string getStr(void) const; + +private: + std::vector _operations; +}; + +} // namespace operation +} // namespace model +} // namespace neurun + +#endif // __NEURUN_MODEL_OPERATION_SUBGRAPH_H__ -- 2.7.4