samskalicky commented on a change in pull request #17623: Dynamic subgraph
compile support
URL: https://github.com/apache/incubator-mxnet/pull/17623#discussion_r393486989
##########
File path: src/operator/subgraph/partitioner/custom_subgraph_property.h
##########
@@ -188,31 +278,133 @@ class CustomSubgraphProperty: public SubgraphProperty {
}
std::string subgraph_json = nnvm::pass::SaveJSON(g);
- CHECK(call_review_subgraph_(review_subgraph_, subgraph_json.c_str(),
- subgraph_id, &accept, opt_keys_.data(),
- opt_vals_.data(), opt_keys_.size(),
- &attr_keys, &attr_vals, &num_attr))
+ CHECK(call_review_subgraph_(review_subgraph_, subgraph_json.c_str(),
subgraph_id,
+ &accept, opt_keys_.data(), opt_vals_.data(),
+ opt_keys_.size(), &attr_keys, &attr_vals,
&num_attr,
+ arg_names.data(), arg_names.size(),
arg_data.data(),
+ arg_shapes.data(), arg_dims.data(),
arg_types.data(),
+ arg_verIDs.data(), arg_dev_type.data(),
+ arg_dev_id.data(), aux_names.data(),
aux_names.size(),
+ aux_data.data(), aux_shapes.data(),
aux_dims.data(),
+ aux_types.data(), aux_verIDs.data(),
+ aux_dev_type.data(), aux_dev_id.data()))
<< "Error calling review_subgraph for '" << subgraph_prop << "'";
+
+ if (num_attr > 0) {
+ // set user specified attributes
+ for (int i=0; i < num_attr; i++) {
+ user_attrs[attr_keys[i]] = attr_vals[i];
+ call_free_(attr_vals[i]);
+ call_free_(attr_keys[i]);
+ }
+ // free memory used by custom op to allocate attributes
+ call_free_(attr_vals);
+ call_free_(attr_keys);
+ }
}
+
if (accept) {
nnvm::ObjectPtr n = nnvm::Node::Create();
n->attrs.op = Op::Get(subgraph_op_name);
n->attrs.name = "_op" + std::to_string(subgraph_id);
n->attrs.subgraphs.push_back(std::make_shared<nnvm::Symbol>(sym));
- // set user specified attributes
- for (int i=0; i < num_attr; i++) {
- n->attrs.dict[attr_keys[i]] = attr_vals[i];
- call_free_(attr_vals[i]);
- call_free_(attr_keys[i]);
+
+ // set shapes
+ {
+ std::stringstream ss;
+ ss << "[";
+ for (unsigned i=0; i < sym.outputs.size(); i++) {
+ nnvm::Node* n = sym.outputs[i].node.get();
+ if (n->attrs.dict.count("__shape__") > 0) {
+ std::string& shape = n->attrs.dict["__shape__"];
+ ss << shape.substr(1, shape.length()-2); // strip off outer
square brackets []
+ }
+ if (i < sym.outputs.size()-1)
+ ss << ",";
+ }
+ ss << "]";
+ n->attrs.dict["__shape__"] = ss.str();
}
- // free memory used by custom op to allocate attributes
- call_free_(attr_vals);
- call_free_(attr_keys);
+ // set dtypes
+ {
+ std::stringstream ss;
+ ss << "[";
+ for (unsigned i=0; i < sym.outputs.size(); i++) {
+ nnvm::Node* n = sym.outputs[i].node.get();
+ if (n->attrs.dict.count("__dtype__") > 0) {
+ std::string& dtype = n->attrs.dict["__dtype__"];
+ ss << dtype.substr(1, dtype.length()-2); // strip off outer
square brackets []
+ }
+ if (i < sym.outputs.size()-1)
+ ss << ",";
+ }
+ ss << "]";
+ n->attrs.dict["__dtype__"] = ss.str();
+ }
+ // set user specified attributes
+ for (auto attr : user_attrs)
+ n->attrs.dict[attr.first] = attr.second;
+
return n;
} else {
return nullptr;
}
}
+
+ virtual void InitSubgraphInputs(std::vector<nnvm::NodeEntry*>* input_entries,
+ std::vector<nnvm::NodeEntry>*
orig_input_entries) const {
+ for (size_t i = 0; i < input_entries->size(); ++i) {
+ nnvm::NodeEntry *e = input_entries->at(i);
+ nnvm::NodeEntry& orig = orig_input_entries->at(i);
+
+ // set attribute for subgraph input to indicate if it is from an
arg/param to model
+ if (orig.node->is_variable()) {
+ // get name of original output entry
+ nnvm::Symbol sym;
+ sym.outputs.push_back(orig);
+ const auto output_names = sym.ListOutputNames();
+ CHECK_EQ(output_names.size(), 1U);
+ const std::string& var_name = output_names[0];
+
+ e->node->attrs.dict["isArg"] = "True";
+ e->node->attrs.dict["argName"] = var_name;
+ } else {
+ e->node->attrs.dict["isArg"] = "False";
+ }
+
+ // pass down other attributes if available
+ if (orig.node->attrs.dict.count("__dtype__") > 0) {
+ // get dtype string from other node
+ std::string& dtype = orig.node->attrs.dict["__dtype__"];
+ int idx = 0;
+ // find the beginning of the output dtype for the particular output
index
+ for (unsigned x=0; x < orig.index; x++)
+ idx = dtype.find("[", idx+1);
+ if (idx == 0) idx++; // if output index is 0, start after first
square bracket [
+ int stop = dtype.find("]", idx); // find stop index for this output
dtype
+ std::stringstream ss;
+ // create new dtype string for this node
+ ss << "[" << dtype.substr(idx, stop-idx+1) << "]";
+ e->node->attrs.dict["__dtype__"] = ss.str();
+ }
+
+ if (orig.node->attrs.dict.count("__shape__") > 0) {
+ // get shape string from other node
+ std::string& shape = orig.node->attrs.dict["__shape__"];
+ int idx = 0;
+ // find the beginning of the output shape for the particular output
index
+ for (unsigned x=0; x < orig.index; x++)
+ idx = shape.find("[", idx+1);
Review comment:
fixed
----------------------------------------------------------------
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:
[email protected]
With regards,
Apache Git Services