@@ -3036,7 +3036,7 @@ ORT_API_STATUS_IMPL(OrtApis::Node_GetAttributeByName, _In_ const OrtNode* node,
30363036 API_IMPL_END
30373037}
30383038
3039- ORT_API_STATUS_IMPL (OrtApis::Node_GetTensorAttributeAsOrtValue, _In_ const OrtNode* node , _In_ const OrtOpAttr* attribute, _Outptr_result_maybenull_ OrtValue** attr_tensor) {
3039+ ORT_API_STATUS_IMPL (OrtApis::OpAttr_GetTensorAttributeAsOrtValue , _In_ const OrtOpAttr* attribute, _Outptr_result_maybenull_ OrtValue** attr_tensor) {
30403040 API_IMPL_BEGIN
30413041 if (attr_tensor == nullptr ) {
30423042 return OrtApis::CreateStatus (ORT_INVALID_ARGUMENT, " attr_tensor argument is null" );
@@ -3045,7 +3045,39 @@ ORT_API_STATUS_IMPL(OrtApis::Node_GetTensorAttributeAsOrtValue, _In_ const OrtNo
30453045 return OrtApis::CreateStatus (ORT_INVALID_ARGUMENT, " attribute argument is null" );
30463046 }
30473047
3048- ORT_API_RETURN_IF_STATUS_NOT_OK (node->GetTensorAttributeAsOrtValue (attribute, *attr_tensor));
3048+ const auto * attr_proto = reinterpret_cast <const ONNX_NAMESPACE::AttributeProto*>(attribute);
3049+
3050+ if (attr_proto->type () != onnx::AttributeProto::TENSOR) {
3051+ return OrtApis::CreateStatus (OrtErrorCode::ORT_INVALID_ARGUMENT, " This OrtOpAttr instance is not a 'TENSOR' attribute" );
3052+ }
3053+
3054+ const auto & tensor_proto = attr_proto->t ();
3055+
3056+ // Check that TensorProto is valid.
3057+ if (!utils::HasDataType (tensor_proto)) {
3058+ return OrtApis::CreateStatus (OrtErrorCode::ORT_INVALID_ARGUMENT, " Tensor proto doesn't have data type." );
3059+ }
3060+
3061+ if (!ONNX_NAMESPACE::TensorProto::DataType_IsValid (tensor_proto.data_type ())) {
3062+ return OrtApis::CreateStatus (OrtErrorCode::ORT_INVALID_ARGUMENT, " Tensor proto has invalid data type." );
3063+ }
3064+
3065+ if (utils::HasExternalData (tensor_proto)) {
3066+ return OrtApis::CreateStatus (OrtErrorCode::ORT_INVALID_ARGUMENT,
3067+ " Tensor proto with external data for value attribute is not supported." );
3068+ }
3069+
3070+ // Initialize OrtValue for tensor attribute.
3071+ auto tensor_attribute_value = std::make_unique<OrtValue>();
3072+ AllocatorPtr tensor_attribute_allocator = CPUAllocator::DefaultInstance ();
3073+ // The tensor in the 'Tensor' attribute's TensorProto is stored inline, not in an external file.
3074+ // Therefore, the 'model_path' passed to TensorProtoToOrtValue() may be an empty path.
3075+ std::filesystem::path model_path;
3076+ ORT_API_RETURN_IF_STATUS_NOT_OK (utils::TensorProtoToOrtValue (Env::Default (), model_path, tensor_proto,
3077+ tensor_attribute_allocator, *tensor_attribute_value));
3078+
3079+ *attr_tensor = tensor_attribute_value.release ();
3080+
30493081 return nullptr ;
30503082 API_IMPL_END
30513083}
@@ -4134,7 +4166,7 @@ static constexpr OrtApi ort_api_1_to_23 = {
41344166 &OrtApis::Node_GetNumAttributes,
41354167 &OrtApis::Node_GetAttributes,
41364168 &OrtApis::Node_GetAttributeByName,
4137- &OrtApis::Node_GetTensorAttributeAsOrtValue ,
4169+ &OrtApis::OpAttr_GetTensorAttributeAsOrtValue ,
41384170 &OrtApis::OpAttr_GetType,
41394171 &OrtApis::OpAttr_GetName,
41404172 &OrtApis::Node_GetNumSubgraphs,
0 commit comments