mirror of
https://github.com/saymrwulf/onnxruntime.git
synced 2026-07-30 20:18:08 +00:00
Remove duplicated constant initializer copies for TensorRT nodes (#10105)
* add new field constant_initializers in metadef and remove constant initializers from trt node inputs * remove redundancy * use GetConstantInitializer() to get constant initializers * add ORT_ENFORCE check Co-authored-by: Ubuntu <azureuser@orteplinuxdev.bxgbzpva45kedp3rhbsbit4phb.jx.internal.cloudapp.net>
This commit is contained in:
parent
ce1a9ca618
commit
05d20343ee
6 changed files with 28 additions and 0 deletions
|
|
@ -39,6 +39,7 @@ struct IndexedSubGraph {
|
|||
|
||||
std::vector<std::string> inputs; ///< Inputs of customized SubGraph/FunctionProto.
|
||||
std::vector<std::string> outputs; ///< Outputs of customized SubGraph/FunctionProto.
|
||||
std::vector<std::string> constant_initializers; ///< Constant initializers of customized SubGraph/FunctionProto.
|
||||
NodeAttributes attributes; ///< Attributes of customized SubGraph/FunctionProto.
|
||||
|
||||
std::string doc_string; ///< Doc string of customized SubGraph/FunctionProto.
|
||||
|
|
|
|||
|
|
@ -457,6 +457,16 @@ FunctionImpl::FunctionImpl(const onnxruntime::Graph& graph,
|
|||
}
|
||||
}
|
||||
|
||||
for (const auto& constant_initializer : meta_def->constant_initializers) {
|
||||
const ONNX_NAMESPACE::TensorProto* initializer = graph.GetConstantInitializer(constant_initializer, true);
|
||||
ORT_ENFORCE(initializer != nullptr, "Initializer " + constant_initializer + " is not found or is not constant initializer.");
|
||||
// meta_def->constant_initializers could have duplicates so make sure we only add once
|
||||
const ONNX_NAMESPACE::TensorProto* subgraph_initializer = nullptr;
|
||||
if (!function_body_graph.GetInitializedTensor(constant_initializer, subgraph_initializer)) {
|
||||
function_body_graph.AddInitializedTensor(*initializer);
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: if we reuse the nodes in parent graph, maybe we don't need to resolve it.
|
||||
auto status = function_body_graph.Resolve();
|
||||
ORT_ENFORCE(status.IsOK(), status.ErrorMessage());
|
||||
|
|
|
|||
|
|
@ -429,6 +429,7 @@ struct ProviderHost {
|
|||
virtual ONNX_NAMESPACE::OperatorStatus& IndexedSubGraph_MetaDef__status(IndexedSubGraph_MetaDef* p) = 0;
|
||||
virtual std::vector<std::string>& IndexedSubGraph_MetaDef__inputs(IndexedSubGraph_MetaDef* p) = 0;
|
||||
virtual std::vector<std::string>& IndexedSubGraph_MetaDef__outputs(IndexedSubGraph_MetaDef* p) = 0;
|
||||
virtual std::vector<std::string>& IndexedSubGraph_MetaDef__constant_initializers(IndexedSubGraph_MetaDef* p) = 0;
|
||||
virtual NodeAttributes& IndexedSubGraph_MetaDef__attributes(IndexedSubGraph_MetaDef* p) = 0;
|
||||
virtual std::string& IndexedSubGraph_MetaDef__doc_string(IndexedSubGraph_MetaDef* p) = 0;
|
||||
|
||||
|
|
|
|||
|
|
@ -348,6 +348,8 @@ struct IndexedSubGraph_MetaDef final {
|
|||
const std::vector<std::string>& inputs() const { return g_host->IndexedSubGraph_MetaDef__inputs(const_cast<IndexedSubGraph_MetaDef*>(this)); }
|
||||
std::vector<std::string>& inputs() { return g_host->IndexedSubGraph_MetaDef__inputs(this); }
|
||||
const std::vector<std::string>& outputs() const { return g_host->IndexedSubGraph_MetaDef__outputs(const_cast<IndexedSubGraph_MetaDef*>(this)); }
|
||||
const std::vector<std::string>& constant_initializers() const { return g_host->IndexedSubGraph_MetaDef__constant_initializers(const_cast<IndexedSubGraph_MetaDef*>(this)); }
|
||||
std::vector<std::string>& constant_initializers() { return g_host->IndexedSubGraph_MetaDef__constant_initializers(this); }
|
||||
std::vector<std::string>& outputs() { return g_host->IndexedSubGraph_MetaDef__outputs(this); }
|
||||
NodeAttributes& attributes() { return g_host->IndexedSubGraph_MetaDef__attributes(this); }
|
||||
|
||||
|
|
|
|||
|
|
@ -714,10 +714,15 @@ std::unique_ptr<IndexedSubGraph> TensorrtExecutionProvider::GetSubGraph(SubGraph
|
|||
int input_order = 0;
|
||||
int output_order = 0;
|
||||
|
||||
std::vector<std::string> initializers;
|
||||
for (const auto& index : graph_nodes_index.first) {
|
||||
sub_graph->Nodes().push_back(node_index[index]);
|
||||
const auto& node = graph.GetNode(node_index[index]);
|
||||
for (const auto& input : node->InputDefs()) {
|
||||
if (graph.IsConstantInitializer(input->Name(), true)) {
|
||||
initializers.push_back(input->Name());
|
||||
continue;
|
||||
}
|
||||
const auto& it = fused_outputs.find(input);
|
||||
if (it != fused_outputs.end()) {
|
||||
fused_outputs.erase(it);
|
||||
|
|
@ -729,6 +734,10 @@ std::unique_ptr<IndexedSubGraph> TensorrtExecutionProvider::GetSubGraph(SubGraph
|
|||
}
|
||||
|
||||
for (const auto& input : node->ImplicitInputDefs()) {
|
||||
if (graph.IsConstantInitializer(input->Name(), true)) {
|
||||
initializers.push_back(input->Name());
|
||||
continue;
|
||||
}
|
||||
const auto& it = fused_outputs.find(input);
|
||||
if (it != fused_outputs.end()) {
|
||||
fused_outputs.erase(it);
|
||||
|
|
@ -810,6 +819,10 @@ std::unique_ptr<IndexedSubGraph> TensorrtExecutionProvider::GetSubGraph(SubGraph
|
|||
}
|
||||
}
|
||||
|
||||
for (const auto& initializer : initializers) {
|
||||
meta_def->constant_initializers().push_back(initializer);
|
||||
}
|
||||
|
||||
for (const auto& output : outputs) {
|
||||
if (output.second->Exists()) {
|
||||
meta_def->outputs().push_back(output.second->Name());
|
||||
|
|
|
|||
|
|
@ -509,6 +509,7 @@ struct ProviderHostImpl : ProviderHost {
|
|||
ONNX_NAMESPACE::OperatorStatus& IndexedSubGraph_MetaDef__status(IndexedSubGraph_MetaDef* p) override { return p->status; }
|
||||
std::vector<std::string>& IndexedSubGraph_MetaDef__inputs(IndexedSubGraph_MetaDef* p) override { return p->inputs; }
|
||||
std::vector<std::string>& IndexedSubGraph_MetaDef__outputs(IndexedSubGraph_MetaDef* p) override { return p->outputs; }
|
||||
std::vector<std::string>& IndexedSubGraph_MetaDef__constant_initializers(IndexedSubGraph_MetaDef* p) override { return p->constant_initializers; }
|
||||
NodeAttributes& IndexedSubGraph_MetaDef__attributes(IndexedSubGraph_MetaDef* p) override { return p->attributes; }
|
||||
std::string& IndexedSubGraph_MetaDef__doc_string(IndexedSubGraph_MetaDef* p) override { return p->doc_string; }
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue