[VitisAI] Cache node subgraph when necessary (#22073)

### 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 <zhenzew@xilinx.com>
Co-authored-by: zhenzew <zhenzew@amd.com>
This commit is contained in:
zz002 2024-11-08 23:17:16 -08:00 committed by GitHub
parent ef281f850a
commit d3ad76b2cf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 32 additions and 9 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -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,
@ -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;

View file

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

View file

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

View file

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

View file

@ -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(); }