mbrookhart commented on a change in pull request #12502: [NGRAPH] MXNet - nGraph initial integration URL: https://github.com/apache/incubator-mxnet/pull/12502#discussion_r220925700
########## File path: src/operator/contrib/ngraph.cc ########## @@ -0,0 +1,266 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/*! + * Copyright (c) 2018 Intel Corporation + * \file ngraph.cc + * \brief ngraph subgraph property for mxnet +*/ + +#if MXNET_USE_NGRAPH +#include <mxnet/ndarray.h> +#include <ngraph_imperative.h> +#include <ngraph_graph.h> +#include <ngraph_nnvm_ops.h> + +#include "./ngraph-inl.h" +#include "../subgraph/common.h" +#include "../subgraph/subgraph_property.h" + +namespace mxnet { +namespace op { + +std::shared_ptr<ngraph_bridge::Graph> get_ngraph(const NodeAttrs& attrs) { + auto compiler = + nnvm::get<std::shared_ptr<ngraph_bridge::Compiler>>(attrs.parsed); + return compiler->GetNgraph(); +} + +class NgraphSubgraphOperator { + public: + explicit NgraphSubgraphOperator(std::shared_ptr<ngraph_bridge::Graph> ngraph) + : ngraph_(ngraph) {} + void Forward(const OpContext& ctx, const std::vector<NDArray>& inputs, + const std::vector<OpReqType>& req, + const std::vector<NDArray>& outputs); + void Backward(const OpContext& ctx, const std::vector<NDArray>& inputs, + const std::vector<OpReqType>& req, + const std::vector<NDArray>& outputs); + + private: + std::shared_ptr<ngraph_bridge::Graph> ngraph_; +}; + +void NgraphSubgraphOperator::Forward(const OpContext& ctx, + const std::vector<NDArray>& inputs, + const std::vector<OpReqType>& req, + const std::vector<NDArray>& outputs) { + compute_forward(ctx, ngraph_, inputs, req, outputs); +} + +void NgraphSubgraphOperator::Backward(const OpContext& ctx, + const std::vector<NDArray>& inputs, + const std::vector<OpReqType>& req, + const std::vector<NDArray>& outputs) { + compute_backward(ctx, ngraph_, inputs, req, outputs); +} + +OpStatePtr CreateNgraphSubgraphOpState(const NodeAttrs& attrs, Context ctx, + const std::vector<TShape>& in_shapes, + const std::vector<int>& in_types) { + return OpStatePtr::Create<NgraphSubgraphOperator>(get_ngraph(attrs)); +} + +void NgraphSubgraphOpForward(const OpStatePtr& state_ptr, const OpContext& ctx, + const std::vector<NDArray>& inputs, + const std::vector<OpReqType>& req, + const std::vector<NDArray>& outputs) { + NgraphSubgraphOperator& op = state_ptr.get_state<NgraphSubgraphOperator>(); + op.Forward(ctx, inputs, req, outputs); +} + +void NgraphSubgraphOpBackward(const OpStatePtr& state_ptr, const OpContext& ctx, + const std::vector<NDArray>& inputs, + const std::vector<OpReqType>& req, + const std::vector<NDArray>& outputs) { + NgraphSubgraphOperator& op = state_ptr.get_state<NgraphSubgraphOperator>(); + op.Backward(ctx, inputs, req, outputs); +} + +std::vector<nnvm::NodeEntry> NgraphSubgraphGradient( + const nnvm::NodePtr& n, const std::vector<nnvm::NodeEntry>& ograds) { + auto graph = get_ngraph(n->attrs); + const bool zero_grad = check_zero_grad(graph); + graph->zero_grad = zero_grad; + auto is_loss = graph->is_loss; + auto p = nnvm::Node::Create(); + p->attrs.op = nnvm::Op::Get("_backward_ngraph_subgraph_op"); + p->attrs.parsed = n->attrs.parsed; + if (std::find(begin(is_loss), end(is_loss), true) == end(is_loss) && + zero_grad && graph->num_outputs_ == 1) { + return mxnet::op::MakeZeroGradNodes(n, ograds); + } + p->attrs.name = n->attrs.name + "_backward"; + p->attrs.dict = n->attrs.dict; + p->control_deps.emplace_back(n); + if (p->op()->attr_parser != nullptr) { + p->op()->attr_parser(&(p->attrs)); + } + if (!zero_grad) { + for (size_t i = 0; i < ograds.size(); ++i) { + if (!is_loss[i]) { + p->inputs.push_back(ograds[i]); + } + } + } + p->inputs.insert(p->inputs.end(), n->inputs.begin(), n->inputs.end()); + std::vector<nnvm::NodeEntry> ret; + for (unsigned i = 0; i < p->num_outputs(); ++i) { + ret.emplace_back(nnvm::NodeEntry{p, i, 0}); + } + return ret; +} + +NNVM_REGISTER_OP(_ngraph_subgraph_op) + .describe(R"code(_ngraph_subgraph_op)code" ADD_FILELINE) + .set_num_inputs([](const NodeAttrs& attrs) { + auto graph = get_ngraph(attrs); + return graph->inputs_.size(); + }) + .set_num_outputs([](const NodeAttrs& attrs) { + auto graph = get_ngraph(attrs); + return graph->outputs_.size(); + }) + .set_attr<nnvm::FListInputNames>("FListInputNames", + [](const nnvm::NodeAttrs& attrs) { + auto graph = get_ngraph(attrs); + std::vector<std::string> input_names; + + for (auto n : graph->inputs_) { + input_names.emplace_back(n->name_); + } + return input_names; Review comment: Can do. Thanks. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
