[VitisAI] add new api for models (#20899)

### Description
<!-- Describe your changes. -->
Add new APIs.


### 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. -->
This change is required for satisfying requirement of Microsoft.

---------

Co-authored-by: Zhenze Wang <zhenzew@xilinx.com>
This commit is contained in:
Yueqing Zhang 2024-06-05 13:48:04 +08:00 committed by GitHub
parent 3ecb012337
commit b374ddd704
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 109 additions and 22 deletions

View file

@ -356,10 +356,18 @@ vaip_core::OrtApiForVaip* create_org_api_hook() {
the_global_api.tensor_proto_get_shape_unsafe = vaip::tensor_proto_get_shape;
the_global_api.tensor_proto_data_type = [](const ONNX_NAMESPACE::TensorProto& t) -> int { return t.data_type(); };
the_global_api.tensor_proto_delete = [](ONNX_NAMESPACE::TensorProto* tp) { delete tp; };
the_global_api.tensor_proto_new_floats = vaip::tensor_proto_new_floats;
the_global_api.tensor_proto_new_i8 = vaip::tensor_proto_new_i8;
the_global_api.tensor_proto_new_i16 = vaip::tensor_proto_new_i16;
the_global_api.tensor_proto_new_i32 = vaip::tensor_proto_new_i32;
the_global_api.tensor_proto_new_i64 = vaip::tensor_proto_new_i64;
the_global_api.tensor_proto_new_i8 = vaip::tensor_proto_new_i8;
the_global_api.tensor_proto_new_u8 = vaip::tensor_proto_new_u8;
the_global_api.tensor_proto_new_u16 = vaip::tensor_proto_new_u16;
the_global_api.tensor_proto_new_u32 = vaip::tensor_proto_new_u32;
the_global_api.tensor_proto_new_u64 = vaip::tensor_proto_new_u64;
the_global_api.tensor_proto_new_floats = vaip::tensor_proto_new_floats;
the_global_api.tensor_proto_new_doubles = vaip::tensor_proto_new_doubles;
the_global_api.tensor_proto_new_bf16 = vaip::tensor_proto_new_bf16;
the_global_api.tensor_proto_new_fp16 = vaip::tensor_proto_new_fp16;
the_global_api.tensor_proto_raw_data_size = [](const auto& tensor) { return tensor.raw_data().size(); };
the_global_api.tensor_proto_as_raw = vaip::tensor_proto_as_raw;
the_global_api.tensor_proto_get_name = [](const auto& tensor) -> const std::string& { return tensor.name(); };

View file

@ -50,28 +50,67 @@ static ONNX_NAMESPACE::TensorProto* tensor_proto_new(const std::string& name, co
return tensor_proto.release();
}
ONNX_NAMESPACE::TensorProto* tensor_proto_new_i32(const std::string& name, const std::vector<int64_t>& shape,
const std::vector<int32_t>& data) {
return tensor_proto_new(name, shape, ONNX_NAMESPACE::TensorProto_DataType_INT32,
reinterpret_cast<const char*>(&data[0]), data.size() * sizeof(int32_t));
}
ONNX_NAMESPACE::TensorProto* tensor_proto_new_i64(const std::string& name, const std::vector<int64_t>& shape,
const std::vector<int64_t>& data) {
return tensor_proto_new(name, shape, ONNX_NAMESPACE::TensorProto_DataType_INT64,
reinterpret_cast<const char*>(&data[0]), data.size() * sizeof(int64_t));
}
ONNX_NAMESPACE::TensorProto* tensor_proto_new_i8(const std::string& name, const std::vector<int64_t>& shape,
const std::vector<int8_t>& data) {
return tensor_proto_new(name, shape, ONNX_NAMESPACE::TensorProto_DataType_INT8,
reinterpret_cast<const char*>(&data[0]), data.size() * sizeof(int8_t));
reinterpret_cast<const char*>(&data[0]), data.size() * sizeof(data[0]));
}
ONNX_NAMESPACE::TensorProto* tensor_proto_new_i16(const std::string& name, const std::vector<int64_t>& shape,
const std::vector<int16_t>& data) {
return tensor_proto_new(name, shape, ONNX_NAMESPACE::TensorProto_DataType_INT16,
reinterpret_cast<const char*>(&data[0]), data.size() * sizeof(data[0]));
}
ONNX_NAMESPACE::TensorProto* tensor_proto_new_i32(const std::string& name, const std::vector<int64_t>& shape,
const std::vector<int32_t>& data) {
return tensor_proto_new(name, shape, ONNX_NAMESPACE::TensorProto_DataType_INT32,
reinterpret_cast<const char*>(&data[0]), data.size() * sizeof(data[0]));
}
ONNX_NAMESPACE::TensorProto* tensor_proto_new_i64(const std::string& name, const std::vector<int64_t>& shape,
const std::vector<int64_t>& data) {
return tensor_proto_new(name, shape, ONNX_NAMESPACE::TensorProto_DataType_INT32,
reinterpret_cast<const char*>(&data[0]), data.size() * sizeof(data[0]));
}
ONNX_NAMESPACE::TensorProto* tensor_proto_new_u8(const std::string& name, const std::vector<int64_t>& shape,
const std::vector<uint8_t>& data) {
return tensor_proto_new(name, shape, ONNX_NAMESPACE::TensorProto_DataType_UINT8,
reinterpret_cast<const char*>(&data[0]), data.size() * sizeof(data[0]));
}
ONNX_NAMESPACE::TensorProto* tensor_proto_new_u16(const std::string& name, const std::vector<int64_t>& shape,
const std::vector<uint16_t>& data) {
return tensor_proto_new(name, shape, ONNX_NAMESPACE::TensorProto_DataType_UINT16,
reinterpret_cast<const char*>(&data[0]), data.size() * sizeof(data[0]));
}
ONNX_NAMESPACE::TensorProto* tensor_proto_new_u32(const std::string& name, const std::vector<int64_t>& shape,
const std::vector<uint32_t>& data) {
return tensor_proto_new(name, shape, ONNX_NAMESPACE::TensorProto_DataType_UINT32,
reinterpret_cast<const char*>(&data[0]), data.size() * sizeof(data[0]));
}
ONNX_NAMESPACE::TensorProto* tensor_proto_new_u64(const std::string& name, const std::vector<int64_t>& shape,
const std::vector<uint64_t>& data) {
return tensor_proto_new(name, shape, ONNX_NAMESPACE::TensorProto_DataType_UINT32,
reinterpret_cast<const char*>(&data[0]), data.size() * sizeof(data[0]));
}
ONNX_NAMESPACE::TensorProto* tensor_proto_new_floats(const std::string& name, const std::vector<int64_t>& shape,
const std::vector<float>& data) {
return tensor_proto_new(name, shape, ONNX_NAMESPACE::TensorProto_DataType_FLOAT,
reinterpret_cast<const char*>(&data[0]), data.size() * sizeof(float));
reinterpret_cast<const char*>(&data[0]), data.size() * sizeof(data[0]));
}
ONNX_NAMESPACE::TensorProto* tensor_proto_new_doubles(const std::string& name, const std::vector<int64_t>& shape,
const std::vector<double>& data) {
return tensor_proto_new(name, shape, ONNX_NAMESPACE::TensorProto_DataType_DOUBLE,
reinterpret_cast<const char*>(&data[0]), data.size() * sizeof(data[0]));
}
ONNX_NAMESPACE::TensorProto* tensor_proto_new_bf16(const std::string& name, const std::vector<int64_t>& shape,
const std::vector<int16_t>& data) {
return tensor_proto_new(name, shape, ONNX_NAMESPACE::TensorProto_DataType_BFLOAT16,
reinterpret_cast<const char*>(&data[0]), data.size() * sizeof(data[0]));
}
ONNX_NAMESPACE::TensorProto* tensor_proto_new_fp16(const std::string& name, const std::vector<int64_t>& shape,
const std::vector<int16_t>& data) {
return tensor_proto_new(name, shape, ONNX_NAMESPACE::TensorProto_DataType_FLOAT16,
reinterpret_cast<const char*>(&data[0]), data.size() * sizeof(data[0]));
}
} // namespace vaip

View file

@ -11,10 +11,26 @@ vaip_core::DllSafe<std::vector<int64_t>> tensor_proto_get_shape(const ONNX_NAMES
const std::string& tensor_proto_get_name(const ONNX_NAMESPACE::TensorProto& tensor);
ONNX_NAMESPACE::TensorProto* tensor_proto_new_i8(const std::string& name, const std::vector<int64_t>& shape,
const std::vector<int8_t>& data);
ONNX_NAMESPACE::TensorProto* tensor_proto_new_u8(const std::string& name, const std::vector<int64_t>& shape,
const std::vector<uint8_t>& data);
ONNX_NAMESPACE::TensorProto* tensor_proto_new_i16(const std::string& name, const std::vector<int64_t>& shape,
const std::vector<int16_t>& data);
ONNX_NAMESPACE::TensorProto* tensor_proto_new_u16(const std::string& name, const std::vector<int64_t>& shape,
const std::vector<uint16_t>& data);
ONNX_NAMESPACE::TensorProto* tensor_proto_new_u32(const std::string& name, const std::vector<int64_t>& shape,
const std::vector<uint32_t>& data);
ONNX_NAMESPACE::TensorProto* tensor_proto_new_i32(const std::string& name, const std::vector<int64_t>& shape,
const std::vector<int32_t>& data);
ONNX_NAMESPACE::TensorProto* tensor_proto_new_u64(const std::string& name, const std::vector<int64_t>& shape,
const std::vector<uint64_t>& data);
ONNX_NAMESPACE::TensorProto* tensor_proto_new_i64(const std::string& name, const std::vector<int64_t>& shape,
const std::vector<int64_t>& data);
ONNX_NAMESPACE::TensorProto* tensor_proto_new_floats(const std::string& name, const std::vector<int64_t>& shape,
const std::vector<float>& data);
ONNX_NAMESPACE::TensorProto* tensor_proto_new_bf16(const std::string& name, const std::vector<int64_t>& shape,
const std::vector<int16_t>& data);
ONNX_NAMESPACE::TensorProto* tensor_proto_new_fp16(const std::string& name, const std::vector<int64_t>& shape,
const std::vector<int16_t>& data);
ONNX_NAMESPACE::TensorProto* tensor_proto_new_doubles(const std::string& name, const std::vector<int64_t>& shape,
const std::vector<double>& data);
} // namespace vaip

View file

@ -12,7 +12,7 @@ struct OrtApi;
namespace vaip_core {
#define VAIP_ORT_API_MAJOR (2u)
#define VAIP_ORT_API_MAJOR (3u)
#define VAIP_ORT_API_MINOR (0u)
#define VAIP_ORT_API_PATCH (0u)
struct OrtApiForVaip {
@ -198,6 +198,31 @@ struct OrtApiForVaip {
DllSafe<std::string> (*get_lib_name)(); // [81]
/** new API after 2.0 */
void (*graph_add_initialized_tensor)(Graph& graph, const TensorProto& tensor); // [82]
/** new API after 3.0 */
TensorProto* (*tensor_proto_new_doubles)(
const std::string& name, const std::vector<int64_t>& shape,
const std::vector<double>& data); // [83]
TensorProto* (*tensor_proto_new_i16)(
const std::string& name, const std::vector<int64_t>& shape,
const std::vector<int16_t>& data); // [84
TensorProto* (*tensor_proto_new_u16)(
const std::string& name, const std::vector<int64_t>& shape,
const std::vector<uint16_t>& data); // [84]
TensorProto* (*tensor_proto_new_u32)(
const std::string& name, const std::vector<int64_t>& shape,
const std::vector<uint32_t>& data); // [85]
TensorProto* (*tensor_proto_new_u8)(const std::string& name,
const std::vector<int64_t>& shape,
const std::vector<uint8_t>& data); // [86]
TensorProto* (*tensor_proto_new_u64)(
const std::string& name, const std::vector<int64_t>& shape,
const std::vector<uint64_t>& data); // [87]
TensorProto* (*tensor_proto_new_fp16)(
const std::string& name, const std::vector<int64_t>& shape,
const std::vector<int16_t>& data); // [88]
TensorProto* (*tensor_proto_new_bf16)(
const std::string& name, const std::vector<int64_t>& shape,
const std::vector<int16_t>& data); // [89]
};
#ifndef USE_VITISAI

View file

@ -1066,13 +1066,12 @@ std::unique_ptr<IExecutionProvider> CreateExecutionProviderInstance(
#endif
} else if (type == kVitisAIExecutionProvider) {
#ifdef USE_VITISAI
ProviderOptions info{};
const auto it = provider_options_map.find(type);
if (it == provider_options_map.end()) {
LOGS_DEFAULT(FATAL) << "cannot find provider options for VitisAIExecutionProvider";
if (it != provider_options_map.end()) {
info = it->second;
}
const auto& vitis_option_map = it->second;
return onnxruntime::VitisAIProviderFactoryCreator::Create(vitis_option_map)
->CreateProvider();
return onnxruntime::VitisAIProviderFactoryCreator::Create(info)->CreateProvider();
#endif
} else if (type == kAclExecutionProvider) {
#ifdef USE_ACL