Skip to content

Commit

Permalink
[VitisAI] add new api for models (#20899)
Browse files Browse the repository at this point in the history
### 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>
  • Loading branch information
BoarQing and Zhenze Wang committed Jun 5, 2024
1 parent 3ecb012 commit b374ddd
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 18 deletions.
12 changes: 10 additions & 2 deletions onnxruntime/core/providers/vitisai/imp/global_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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(); };
Expand Down
59 changes: 49 additions & 10 deletions onnxruntime/core/providers/vitisai/imp/tensor_proto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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_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(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(int32_t));
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_INT64,
reinterpret_cast<const char*>(&data[0]), data.size() * sizeof(int64_t));
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_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));
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
16 changes: 16 additions & 0 deletions onnxruntime/core/providers/vitisai/imp/tensor_proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 26 additions & 1 deletion onnxruntime/core/providers/vitisai/include/vaip/vaip_ort_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions onnxruntime/python/onnxruntime_pybind_state.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b374ddd

Please sign in to comment.