You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2020/09/10 05:49:28 UTC

[GitHub] [incubator-mxnet] mseth10 commented on a change in pull request #18690: [WIP] optimize graph in presence of dynamic shape ops

mseth10 commented on a change in pull request #18690:
URL: https://github.com/apache/incubator-mxnet/pull/18690#discussion_r486080444



##########
File path: src/operator/subgraph/static_shape_subgraph_property.cc
##########
@@ -0,0 +1,181 @@
+/*
+ * 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.
+ */
+
+#include "./common.h"
+#include "./subgraph_property.h"
+#include "../../imperative/cached_op.h"
+
+namespace mxnet {
+namespace op {
+
+/*
+ * This selects nodes for a subgraph that only contains static shape operators
+ * and it visits nodes via both input and output links.
+ */
+class StaticShapeOpSelector: public SubgraphSelector {
+ public:
+  // select nodes with FInferShape attribute
+  bool Select(const nnvm::Node &seed_node) override {
+    const auto& infershape = nnvm::Op::GetAttr<mxnet::FInferShape>("FInferShape");
+    return !seed_node.is_variable() && infershape.count(seed_node.op()) &&
+           !unsupported_op_names_.count(seed_node.op()->name);
+  }
+
+  bool SelectInput(const nnvm::Node &cur_node, const nnvm::Node &input_node) override {
+    return Select(input_node);
+  }
+
+  bool SelectOutput(const nnvm::Node &cur_node, const nnvm::Node &output_node) override {
+    return Select(output_node);
+  }
+
+  // reject single node subgraph
+  std::vector<nnvm::Node*> Filter(const std::vector<nnvm::Node*>& candidates) override {
+    if (candidates.size() == 1) {
+      return std::vector<nnvm::Node*>();
+    }
+    return candidates;
+  }
+
+ private:
+    // static shape ops that fail backward pass inside subgraph CachedOp
+    // GitHub issue: https://github.com/apache/incubator-mxnet/issues/18823
+    std::unordered_set<std::string> unsupported_op_names_ {"Reshape", "_np_reshape", "transpose",
+                                                           "_npi_dstack", "_npi_hstack"};
+};
+
+/*
+ * This subgraph property finds a subgraph whose nodes have only static shape operators.
+ * The operators in the subgraph will be executed by _CachedOp.
+ */
+class StaticShapeSubgraphProperty: public SubgraphProperty {
+ public:
+  StaticShapeSubgraphProperty() {
+    // flag to recursively partition dynamic shape op nodes containing subgraphs
+    attrs_["recursive_partition"] = std::make_shared<dmlc::any>(true);
+    // flag to ensure subgraph CachedOp has at least one external input
+    // as required by CachedOp::Forward
+    attrs_["ensure_CachedOp_input"] = std::make_shared<dmlc::any>(true);
+  }
+  static SubgraphPropertyPtr Create() { return std::make_shared<StaticShapeSubgraphProperty>(); }
+
+  SubgraphSelectorPtr CreateSubgraphSelector() const override {
+    return std::make_shared<StaticShapeOpSelector>();
+  }
+
+  void PrePartition(const nnvm::Graph& g,
+                    const std::unordered_map<std::string, std::string>& options_map) override {
+    // update static_alloc and static_shape flags
+    if (options_map_.size() == 0) {
+      for (auto& kv : options_map) {
+        options_map_.emplace_back(kv);
+      }
+    }
+    if (param_name_set_.size() > 0) {
+      param_name_set_.clear();
+    }
+    // generate param_name_set_ for data_indices and param_indices
+    if (g.HasAttr("param_indices_list")) {
+      const std::vector<int>& param_indices_list
+          = g.GetAttr<std::vector<int>>("param_indices_list");
+      const auto& indexed_graph = g.indexed_graph();
+      for (const auto index : param_indices_list) {
+          auto nid = indexed_graph.input_nodes()[index];
+          nnvm::Node n = *(indexed_graph[nid].source);
+          param_name_set_.emplace(n.attrs.name);
+      }
+    }
+  }
+
+  void InitSubgraphInputs(std::vector<nnvm::NodeEntry*>* input_entries,
+                          std::vector<nnvm::NodeEntry>* orig_input_entries) override {

Review comment:
       add `const` ?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org