From a6e219deff9ad90fa4afc664af2cb5f90a93b6bc Mon Sep 17 00:00:00 2001 From: Vincent Wang Date: Wed, 2 Sep 2020 21:54:40 +0800 Subject: [PATCH] Pass Model Path to TensorProtoToMLValue from Constant Folding for External Inputs (#5000) * Don't constant fold external inputs. * pass model_path to TensorProtoToMLValue Co-authored-by: Vincent Wang --- onnxruntime/core/optimizer/constant_folding.cc | 3 +-- .../core/optimizer/optimizer_execution_frame.cc | 10 +++++++--- onnxruntime/core/optimizer/optimizer_execution_frame.h | 4 +++- onnxruntime/test/optimizer/optimizer_test.cc | 2 +- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/onnxruntime/core/optimizer/constant_folding.cc b/onnxruntime/core/optimizer/constant_folding.cc index 0a7062434b..0777462aa6 100644 --- a/onnxruntime/core/optimizer/constant_folding.cc +++ b/onnxruntime/core/optimizer/constant_folding.cc @@ -104,8 +104,7 @@ Status ConstantFolding::ApplyImpl(Graph& graph, bool& modified, int graph_level, } // Create execution frame for executing constant nodes. - // Create execution frame for executing constant nodes. - OptimizerExecutionFrame::Info info({node}, constant_inputs, execution_provider_); + OptimizerExecutionFrame::Info info({node}, constant_inputs, graph.ModelPath(), execution_provider_); std::vector fetch_mlvalue_idxs; for (const auto* node_out : node->OutputDefs()) { diff --git a/onnxruntime/core/optimizer/optimizer_execution_frame.cc b/onnxruntime/core/optimizer/optimizer_execution_frame.cc index 3c7a93da01..fcbe59d67d 100644 --- a/onnxruntime/core/optimizer/optimizer_execution_frame.cc +++ b/onnxruntime/core/optimizer/optimizer_execution_frame.cc @@ -19,6 +19,7 @@ namespace onnxruntime { OptimizerExecutionFrame::Info::Info(const std::vector& nodes, const InitializedTensorSet& initialized_tensor_set, + const Path& model_path, const IExecutionProvider& execution_provider) : execution_provider_(execution_provider) { allocator_ptr_ = execution_provider_.GetAllocator(device_id_, mem_type_); @@ -27,7 +28,7 @@ OptimizerExecutionFrame::Info::Info(const std::vector& nodes, data_transfer_mgr_.RegisterDataTransfer(onnxruntime::make_unique()); // Create MLValues related maps - auto initialize_maps = [this, &initialized_tensor_set](const NodeArg& arg, size_t /*index*/) -> Status { + auto initialize_maps = [this, &initialized_tensor_set, &model_path](const NodeArg& arg, size_t /*index*/) -> Status { int idx = ort_value_name_idx_map_.Add(arg.Name()); ort_value_idx_nodearg_map_[idx] = &arg; @@ -41,9 +42,12 @@ OptimizerExecutionFrame::Info::Info(const std::vector& nodes, std::unique_ptr data(new char[cpu_tensor_length]); std::unique_ptr p_tensor; OrtCallback d; - ORT_RETURN_IF_ERROR(utils::TensorProtoToMLValue(Env::Default(), nullptr, tensor_proto, + ORT_RETURN_IF_ERROR(utils::TensorProtoToMLValue(Env::Default(), + model_path.IsEmpty() ? nullptr : model_path.ToPathString().c_str(), + tensor_proto, MemBuffer(data.get(), cpu_tensor_length, allocator_ptr_->Info()), - ort_value, d)); + ort_value, + d)); initializers_[idx] = ort_value; buffer_for_initialized_tensors_[idx] = std::move(data); diff --git a/onnxruntime/core/optimizer/optimizer_execution_frame.h b/onnxruntime/core/optimizer/optimizer_execution_frame.h index ba97bbd28a..00c3d2419d 100644 --- a/onnxruntime/core/optimizer/optimizer_execution_frame.h +++ b/onnxruntime/core/optimizer/optimizer_execution_frame.h @@ -20,7 +20,9 @@ class OptimizerExecutionFrame final : public IExecutionFrame { public: class Info { public: - Info(const std::vector& nodes, const InitializedTensorSet& initialized_tensor_set, + Info(const std::vector& nodes, + const InitializedTensorSet& initialized_tensor_set, + const Path& model_path, const IExecutionProvider& execution_provider); ~Info() { for (auto& kvp : deleter_for_initialized_tensors_) { diff --git a/onnxruntime/test/optimizer/optimizer_test.cc b/onnxruntime/test/optimizer/optimizer_test.cc index 8b76fb2fc3..92656cacc8 100644 --- a/onnxruntime/test/optimizer/optimizer_test.cc +++ b/onnxruntime/test/optimizer/optimizer_test.cc @@ -66,7 +66,7 @@ TEST(OptimizerTest, Basic) { std::unique_ptr cpu_execution_provider = onnxruntime::make_unique(CPUExecutionProviderInfo()); - OptimizerExecutionFrame::Info info(nodes, initialized_tensor_set, *cpu_execution_provider.get()); + OptimizerExecutionFrame::Info info(nodes, initialized_tensor_set, graph.ModelPath(), *cpu_execution_provider.get()); std::vector fetch_mlvalue_idxs{info.GetMLValueIndex("out")}; OptimizerExecutionFrame frame(info, fetch_mlvalue_idxs); const logging::Logger& logger = DefaultLoggingManager().DefaultLogger();