ptrendx commented on a change in pull request #15118: Conversion from FP32 
model to Mixed Precision model
URL: https://github.com/apache/incubator-mxnet/pull/15118#discussion_r291415957
 
 

 ##########
 File path: src/c_api/c_api_symbolic.cc
 ##########
 @@ -810,6 +810,156 @@ int MXQuantizeSymbol(SymbolHandle sym_handle,
   API_END_HANDLE_ERROR(delete s);
 }
 
+int MXReducePrecisionSymbol(SymbolHandle sym_handle,
+                            SymbolHandle *ret_sym_handle,
+                            mx_uint num_args,
+                            const int *arg_type_data,
+                            mx_uint num_ind_ptr,
+                            const int* ind_ptr,
+                            const int *target_dtype,
+                            const mx_uint num_target_dtype_op_names,
+                            const mx_uint num_fp32_op_names,
+                            const mx_uint num_widest_dtype_op_names,
+                            const mx_uint num_conditional_fp32_op_names,
+                            const mx_uint num_excluded_symbols,
+                            const mx_uint num_model_params,
+                            const char **target_dtype_op_names,
+                            const char **fp32_op_names,
+                            const char **widest_dtype_op_names,
+                            const char **conditional_fp32_op_names,
+                            const char **excluded_symbols,
+                            const char **param_names,
+                            const char **param_vals,
+                            const char **model_param_names,
+                            const char **arg_names) {
+  nnvm::Symbol *s = new nnvm::Symbol();
+  API_BEGIN();
+  nnvm::Symbol *sym = static_cast<nnvm::Symbol *>(sym_handle);
+  nnvm::Graph g = Symbol2Graph(*sym);
+  std::unordered_set<std::string> target_dtype_ops;
+  std::unordered_set<std::string> fp32_ops;
+  std::unordered_set<std::string> widest_dtype_ops;
+  std::unordered_set<std::string> excluded_syms;
+  std::unordered_set<std::string> model_params;
+  std::unordered_map<std::string,
+                     std::unordered_map<std::string,
+                                        std::vector<std::string>>> 
conditional_fp32_ops;
+  int target_dt = *target_dtype;
+
+  for (size_t i = 0; i < num_target_dtype_op_names; ++i) {
+    target_dtype_ops.emplace(target_dtype_op_names[i]);
+  }
+  for (size_t i = 0; i < num_fp32_op_names; ++i) {
+    fp32_ops.emplace(fp32_op_names[i]);
+  }
+  for (size_t i = 0; i < num_widest_dtype_op_names; ++i) {
+    widest_dtype_ops.emplace(widest_dtype_op_names[i]);
+  }
+  for (size_t i = 0; i < num_excluded_symbols; ++i) {
+    excluded_syms.emplace(excluded_symbols[i]);
+  }
+  for (size_t i = 0; i < num_model_params; ++i) {
+    model_params.emplace(model_param_names[i]);
+  }
+
+  for (size_t i = 0; i < num_ind_ptr - 1; ++i) {
+    for (int j = ind_ptr[i]; j < ind_ptr[i + 1]; ++j) {
+      conditional_fp32_ops[conditional_fp32_op_names[i]][param_names[i]]
+          .emplace_back(std::string(param_vals[j]));
+    }
+  }
+
+  std::unordered_map<std::string, int> kwargs;
+  std::unordered_map<std::string, int> node_name_dtype_map, 
node_without_dtype_map;
+  nnvm::DTypeVector arg_types(g.indexed_graph().input_nodes().size(), -1);
+  for (mx_uint i = 0; i < num_args; ++i) {
+    kwargs[arg_names[i]] = arg_type_data[i];
+    node_name_dtype_map[arg_names[i]] = arg_type_data[i];
+  }
+  mxnet::MatchArguments(g.indexed_graph(), kwargs, &arg_types, "InferType");
+
+  g.attrs["target_dtype_ops"] =
+      std::make_shared<nnvm::any>(std::move(target_dtype_ops));
+  g.attrs["fp32_ops"] = std::make_shared<nnvm::any>(std::move(fp32_ops));
+  g.attrs["widest_dtype_ops"] =
+      std::make_shared<nnvm::any>(std::move(widest_dtype_ops));
+  g.attrs["conditional_fp32_ops"] =
+      std::make_shared<nnvm::any>(std::move(conditional_fp32_ops));
+  g.attrs["excluded_syms"] =
+      std::make_shared<nnvm::any>(std::move(excluded_syms));
+  g.attrs["target_dtype"] = std::make_shared<nnvm::any>(target_dt);
+
+  g = ApplyPass(std::move(g), "ReducePrecision");
+  // Need to run type inference since it is possible that inferred
+  // type of some inputs has changed
+  g = mxnet::exec::InferType(std::move(g), std::move(arg_types), "");
 
 Review comment:
   If you already did InferType before on the original graph then I guess you 
should remove the dtype AttrVector from the graph, right?
   Also, I'm not sure if indexed graph is recreated after your pass.

----------------------------------------------------------------
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

Reply via email to