Skip to content

Commit

Permalink
[VitisAI] Cache node subgraph when necessary (#22073)
Browse files Browse the repository at this point in the history
### Description
<!-- Describe your changes. -->

[VitisAI] Cache node subgraph when necessary

### Motivation and Context
<!-- - Why is this change required? What problem does it solve?
- If it fixes an open issue, please link to the issue here. -->

---------

Co-authored-by: Zhenze Wang <[email protected]>
Co-authored-by: zhenzew <[email protected]>
  • Loading branch information
3 people authored Nov 9, 2024
1 parent ef281f8 commit d3ad76b
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 9 deletions.
1 change: 1 addition & 0 deletions cmake/onnxruntime_providers_vitisai.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
file(GLOB onnxruntime_providers_vitisai_cc_srcs CONFIGURE_DEPENDS
"${ONNXRUNTIME_ROOT}/core/providers/vitisai/*.cc"
"${ONNXRUNTIME_ROOT}/core/providers/vitisai/*.h"
"${ONNXRUNTIME_ROOT}/core/providers/vitisai/include/vaip/*.h"
"${ONNXRUNTIME_ROOT}/core/providers/vitisai/imp/*.cc"
"${ONNXRUNTIME_ROOT}/core/providers/vitisai/imp/*.h"
"${ONNXRUNTIME_ROOT}/core/providers/shared_library/*.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,7 @@ struct ProviderHost {
bool include_outer_scope_args,
int execution_order) noexcept = 0;
virtual const Node* GraphViewer__GetProducerNode(const GraphViewer* p, const std::string& node_arg_name) const = 0;
virtual IOnnxRuntimeOpSchemaCollectionPtr GraphViewer__GetSchemaRegistry(const GraphViewer* p) const = 0;

// OpKernel
virtual const Node& OpKernel__Node(const OpKernel* p) = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,7 @@ class GraphViewer final {
g_host->GraphViewer__ToProto(this, graph_proto, include_initializers, include_outer_scope_args, execution_order);
}
const Node* GetProducerNode(const std::string& node_arg_name) const { return g_host->GraphViewer__GetProducerNode(this, node_arg_name); }
IOnnxRuntimeOpSchemaCollectionPtr GetSchemaRegistry() const { return g_host->GraphViewer__GetSchemaRegistry(this); }

GraphViewer() = delete;
GraphViewer(const GraphViewer&) = delete;
Expand Down
5 changes: 5 additions & 0 deletions onnxruntime/core/providers/vitisai/imp/global_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,11 @@ vaip_core::OrtApiForVaip* create_org_api_hook() {
}
};
the_global_api.node_arg_external_location = vaip::node_arg_external_location;
the_global_api.model_to_proto = [](onnxruntime::Model& model) { return model.ToProto().release(); };
the_global_api.model_proto_serialize_as_string = [](ONNX_NAMESPACE::ModelProto& model_proto) {
return vaip_core::DllSafe(model_proto.SerializeAsString());
};
the_global_api.model_proto_delete = [](ONNX_NAMESPACE::ModelProto* p) { delete p; };
if (!s_library_vitisaiep.vaip_get_version) {
return reinterpret_cast<vaip_core::OrtApiForVaip*>(&(the_global_api.host_));
} else {
Expand Down
12 changes: 6 additions & 6 deletions onnxruntime/core/providers/vitisai/include/vaip/custom_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ class ExecutionProvider {
virtual DllSafe<std::vector<std::string>> get_meta_def_nodes() const = 0;
virtual DllSafe<std::vector<std::string>>
get_meta_def_constant_initializer() const = 0;
virtual bool get_meta_def_fallback_CPU() const { return false; };
virtual std::unique_ptr<CustomOp> compile() const = 0;

public:
inline void set_fused_node(const onnxruntime::Node* fused_node) {
fused_node_ = fused_node;
}
inline const onnxruntime::Node* get_fused_node() const {
return fused_node_;
}
inline void set_fused_node(const onnxruntime::Node* fused_node) { fused_node_ = fused_node; }
inline const onnxruntime::Node* get_fused_node() const { return fused_node_; }
inline void set_model(onnxruntime::Model* model) { model_ = model; }
inline onnxruntime::Model* get_model() const { return model_; }

private:
const onnxruntime::Node* fused_node_ = nullptr;
onnxruntime::Model* model_ = nullptr;
};

class CustomOp {
Expand Down
2 changes: 2 additions & 0 deletions onnxruntime/core/providers/vitisai/include/vaip/my_ort.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ struct NodeAttributes;
namespace ONNX_NAMESPACE {
struct AttributeProto;
struct TensorProto;
struct ModelProto;
#ifndef USE_VITISAI
enum TensorProto_DataType : int {
TensorProto_DataType_UNDEFINED = 0,
Expand Down Expand Up @@ -70,6 +71,7 @@ enum AttributeProto_AttributeType : int {
namespace vaip_core {
class GraphHolder;
using ONNX_NAMESPACE::AttributeProto;
using ONNX_NAMESPACE::ModelProto;
using ONNX_NAMESPACE::TensorProto;
using onnxruntime::Graph;
using onnxruntime::GraphViewer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct OrtApi;

namespace vaip_core {

#define VAIP_ORT_API_MAJOR (10u)
#define VAIP_ORT_API_MAJOR (11u)
#define VAIP_ORT_API_MINOR (0u)
#define VAIP_ORT_API_PATCH (0u)
struct OrtApiForVaip {
Expand Down Expand Up @@ -231,6 +231,9 @@ struct OrtApiForVaip {
gsl::span<const NodeArg* const> inputs); // [92]
int (*node_arg_external_location)(const Graph& graph, const NodeArg& node_arg, std::string& file, size_t& offset, size_t& size, size_t& checksum); // [93]
void (*session_option_configuration)(void* mmap, void* session_option, void (*push)(void* mmap, const char* name, const char* value)); // [94]
ModelProto* (*model_to_proto)(Model& model); // [95]
DllSafe<std::string> (*model_proto_serialize_as_string)(ModelProto& model_proto); // [96]
void (*model_proto_delete)(ModelProto* p); // [97]
};

#ifndef USE_VITISAI
Expand Down
12 changes: 11 additions & 1 deletion onnxruntime/core/providers/vitisai/vitisai_execution_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,17 @@ common::Status VitisAIExecutionProvider::Compile(const std::vector<FusedNodeAndG
auto& attrs = fused_node_graph.fused_node.get().GetAttributes();
assert(attrs.count("index"));
size_t index = attrs.at("index").i();
(**this->execution_providers_)[index]->set_fused_node(&fused_node_graph.fused_node.get());
auto& ep = (**this->execution_providers_)[index];
ep->set_fused_node(&fused_node_graph.fused_node.get());
if (ep->get_meta_def_fallback_CPU()) {
auto& subgraph = fused_node_graph.filtered_graph.get();
auto& logger = logging::LoggingManager::DefaultLogger();
auto model_proto = subgraph.CreateModel(logger)->ToProto();
subgraph.ToProto(*model_proto->mutable_graph(), true, true);
auto local_registries = IOnnxRuntimeOpSchemaRegistryList{subgraph.GetSchemaRegistry()};
auto model = Model::Create(std::move(*model_proto), subgraph.ModelPath(), &local_registries, logger);
ep->set_model(model.release());
}
compute_info.create_state_func = [this, index](ComputeContext* context, FunctionState* state) {
auto* p = (**this->execution_providers_)[index]->compile().release();
*state = p;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ class VitisAIExecutionProvider : public IExecutionProvider {
ProviderOptions info_;
std::vector<OrtCustomOpDomain*> custom_op_domains_;
std::shared_ptr<KernelRegistry> registry_;
std::set<std::string> vitisai_optypes_;
// EP context related.
bool ep_ctx_enabled_ = false;
bool ep_ctx_embed_mode_ = true;
Expand Down
1 change: 1 addition & 0 deletions onnxruntime/core/session/provider_bridge_ort.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1212,6 +1212,7 @@ struct ProviderHostImpl : ProviderHost {
GraphViewerToProto(*p, graph_proto, include_initializers, include_outer_scope_args, static_cast<ExecutionOrder>(execution_order));
}
const Node* GraphViewer__GetProducerNode(const GraphViewer* p, const std::string& node_arg_name) const override { return p->GetProducerNode(node_arg_name); }
IOnnxRuntimeOpSchemaCollectionPtr GraphViewer__GetSchemaRegistry(const GraphViewer* p) const override { return p->GetSchemaRegistry(); }

// OpKernel (direct)
const Node& OpKernel__Node(const OpKernel* p) override { return p->OpKernel::Node(); }
Expand Down

0 comments on commit d3ad76b

Please sign in to comment.