From bd1f86964277b73363c10ccb2199427b26f651f1 Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Thu, 2 Oct 2025 20:43:56 -0700 Subject: [PATCH 01/14] Allow any dimensional tensor for ConstantOfShape Updated enforcement conditions to allow any dimensional tensor with a single element according to the ONNX spec. --- .../core/providers/cpu/generator/constant_of_shape_base.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h b/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h index ffd954f13e568..1bd3f23601568 100644 --- a/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h +++ b/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h @@ -78,8 +78,7 @@ class ConstantOfShapeBase { auto* t_proto_p = t_proto.get(); #endif if (info.GetAttr("value", t_proto_p).IsOK()) { - ORT_ENFORCE(t_proto_p->dims_size() == 1, "Must have a single dimension"); - ORT_ENFORCE(t_proto_p->dims()[0] == 1, "Must have a single dimension of 1"); + // value can be any dimentional. It only needs to be a single-element tensor. SetValueFromTensorProto(*t_proto_p); } else { float f_value = 0.f; From 09415fe2094bd0f48603162080029007bb7017d7 Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Thu, 2 Oct 2025 20:47:40 -0700 Subject: [PATCH 02/14] spelling Signed-off-by: Justin Chu --- .../core/providers/cpu/generator/constant_of_shape_base.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h b/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h index 1bd3f23601568..c734348336396 100644 --- a/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h +++ b/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h @@ -78,7 +78,7 @@ class ConstantOfShapeBase { auto* t_proto_p = t_proto.get(); #endif if (info.GetAttr("value", t_proto_p).IsOK()) { - // value can be any dimentional. It only needs to be a single-element tensor. + // value can be any dimensional. It only needs to be a single-element tensor. SetValueFromTensorProto(*t_proto_p); } else { float f_value = 0.f; From 2bc00123f12cbd817061685f074d42e74c5ebfa1 Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Mon, 6 Oct 2025 13:05:56 -0700 Subject: [PATCH 03/14] update check Signed-off-by: Justin Chu --- .../core/providers/cpu/generator/constant_of_shape_base.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h b/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h index c734348336396..a88662b4ec30a 100644 --- a/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h +++ b/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h @@ -78,7 +78,7 @@ class ConstantOfShapeBase { auto* t_proto_p = t_proto.get(); #endif if (info.GetAttr("value", t_proto_p).IsOK()) { - // value can be any dimensional. It only needs to be a single-element tensor. + ORT_ENFORCE(t_proto_p->dims_size() == 0 || (t_proto_p->dims_size() > 0 && t_proto_p->dims()[0] == 1), "Must have a single element"); SetValueFromTensorProto(*t_proto_p); } else { float f_value = 0.f; From 20381e52d3deb5db35e90e4046cb99131fa96125 Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Mon, 6 Oct 2025 14:24:24 -0700 Subject: [PATCH 04/14] Update validation for single-element tensor in ConstantOfShape --- .../core/providers/cpu/generator/constant_of_shape_base.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h b/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h index a88662b4ec30a..086c37049f2ad 100644 --- a/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h +++ b/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h @@ -78,7 +78,7 @@ class ConstantOfShapeBase { auto* t_proto_p = t_proto.get(); #endif if (info.GetAttr("value", t_proto_p).IsOK()) { - ORT_ENFORCE(t_proto_p->dims_size() == 0 || (t_proto_p->dims_size() > 0 && t_proto_p->dims()[0] == 1), "Must have a single element"); + ORT_ENFORCE(utils::GetTensorShapeFromTensorProto(t_proto_p).Size(), "The value attribute of ConstantOfShape must be a single-element tensor"); SetValueFromTensorProto(*t_proto_p); } else { float f_value = 0.f; From 81cb96d50b2ac61e46b798e955b8d5b56da31189 Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Mon, 6 Oct 2025 15:37:48 -0700 Subject: [PATCH 05/14] Update constant_of_shape_base.h --- .../core/providers/cpu/generator/constant_of_shape_base.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h b/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h index 086c37049f2ad..e4051b663e920 100644 --- a/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h +++ b/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h @@ -78,7 +78,7 @@ class ConstantOfShapeBase { auto* t_proto_p = t_proto.get(); #endif if (info.GetAttr("value", t_proto_p).IsOK()) { - ORT_ENFORCE(utils::GetTensorShapeFromTensorProto(t_proto_p).Size(), "The value attribute of ConstantOfShape must be a single-element tensor"); + ORT_ENFORCE(utils::GetTensorShapeFromTensorProto(t_proto_p).Size() == 1, "The value attribute of ConstantOfShape must be a single-element tensor"); SetValueFromTensorProto(*t_proto_p); } else { float f_value = 0.f; From 4600e2858a1e209114889210ce6c063dc6c4fd92 Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Mon, 6 Oct 2025 15:39:22 -0700 Subject: [PATCH 06/14] Use t_proto Signed-off-by: Justin Chu --- .../core/providers/cpu/generator/constant_of_shape_base.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h b/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h index e4051b663e920..07e022bacda79 100644 --- a/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h +++ b/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h @@ -78,7 +78,7 @@ class ConstantOfShapeBase { auto* t_proto_p = t_proto.get(); #endif if (info.GetAttr("value", t_proto_p).IsOK()) { - ORT_ENFORCE(utils::GetTensorShapeFromTensorProto(t_proto_p).Size() == 1, "The value attribute of ConstantOfShape must be a single-element tensor"); + ORT_ENFORCE(utils::GetTensorShapeFromTensorProto(t_proto).Size() == 1, "The value attribute of ConstantOfShape must be a single-element tensor"); SetValueFromTensorProto(*t_proto_p); } else { float f_value = 0.f; From 61fc4b99a242832d6dc6e01dca86e82aef81975f Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Mon, 6 Oct 2025 15:40:38 -0700 Subject: [PATCH 07/14] deref Signed-off-by: Justin Chu --- .../core/providers/cpu/generator/constant_of_shape_base.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h b/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h index 07e022bacda79..7d1e63b2e4f59 100644 --- a/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h +++ b/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h @@ -78,7 +78,7 @@ class ConstantOfShapeBase { auto* t_proto_p = t_proto.get(); #endif if (info.GetAttr("value", t_proto_p).IsOK()) { - ORT_ENFORCE(utils::GetTensorShapeFromTensorProto(t_proto).Size() == 1, "The value attribute of ConstantOfShape must be a single-element tensor"); + ORT_ENFORCE(utils::GetTensorShapeFromTensorProto(*t_proto_p).Size() == 1, "The value attribute of ConstantOfShape must be a single-element tensor"); SetValueFromTensorProto(*t_proto_p); } else { float f_value = 0.f; From 5686ddd17e0df8f833ece1f9fcbdc1fa50c310c2 Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Mon, 6 Oct 2025 15:41:27 -0700 Subject: [PATCH 08/14] t_proto Signed-off-by: Justin Chu --- .../core/providers/cpu/generator/constant_of_shape_base.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h b/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h index 7d1e63b2e4f59..07e022bacda79 100644 --- a/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h +++ b/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h @@ -78,7 +78,7 @@ class ConstantOfShapeBase { auto* t_proto_p = t_proto.get(); #endif if (info.GetAttr("value", t_proto_p).IsOK()) { - ORT_ENFORCE(utils::GetTensorShapeFromTensorProto(*t_proto_p).Size() == 1, "The value attribute of ConstantOfShape must be a single-element tensor"); + ORT_ENFORCE(utils::GetTensorShapeFromTensorProto(t_proto).Size() == 1, "The value attribute of ConstantOfShape must be a single-element tensor"); SetValueFromTensorProto(*t_proto_p); } else { float f_value = 0.f; From 5a18451ddf1952abcf45a428a3cdfa26638d42f1 Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Mon, 6 Oct 2025 19:27:31 -0700 Subject: [PATCH 09/14] update Signed-off-by: Justin Chu --- .../providers/cpu/generator/constant_of_shape_base.h | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h b/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h index 07e022bacda79..27f15c0fa0764 100644 --- a/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h +++ b/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h @@ -78,7 +78,15 @@ class ConstantOfShapeBase { auto* t_proto_p = t_proto.get(); #endif if (info.GetAttr("value", t_proto_p).IsOK()) { - ORT_ENFORCE(utils::GetTensorShapeFromTensorProto(t_proto).Size() == 1, "The value attribute of ConstantOfShape must be a single-element tensor"); + ORT_ENFORCE(t_proto_p->has_shape(), "The value tensor of ConstantOfShape should have a shape"); + ORT_ENFORCE( + t_proto_p->dims_size() == 0 || + accumulate( + t_proto_p->dims().begin(), + t_proto_p->dims().end(), + 1, + multiplies()) == 1, + "The value attribute of ConstantOfShape must be a single-element tensor"); SetValueFromTensorProto(*t_proto_p); } else { float f_value = 0.f; From 5c6b2f82a5e1e93aeb4af77093d6d2f73b3e6d50 Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Mon, 6 Oct 2025 19:29:13 -0700 Subject: [PATCH 10/14] std Signed-off-by: Justin Chu --- .../core/providers/cpu/generator/constant_of_shape_base.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h b/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h index 27f15c0fa0764..440da5592ec93 100644 --- a/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h +++ b/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h @@ -81,11 +81,11 @@ class ConstantOfShapeBase { ORT_ENFORCE(t_proto_p->has_shape(), "The value tensor of ConstantOfShape should have a shape"); ORT_ENFORCE( t_proto_p->dims_size() == 0 || - accumulate( + std::accumulate( t_proto_p->dims().begin(), t_proto_p->dims().end(), 1, - multiplies()) == 1, + std::multiplies()) == 1, "The value attribute of ConstantOfShape must be a single-element tensor"); SetValueFromTensorProto(*t_proto_p); } else { From 839ebcd0b4f33cf0169ce43cd78a77017f99ebe7 Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Tue, 7 Oct 2025 11:52:18 -0700 Subject: [PATCH 11/14] Update constant_of_shape_base.h --- .../core/providers/cpu/generator/constant_of_shape_base.h | 1 - 1 file changed, 1 deletion(-) diff --git a/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h b/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h index 440da5592ec93..2699211c57e15 100644 --- a/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h +++ b/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h @@ -78,7 +78,6 @@ class ConstantOfShapeBase { auto* t_proto_p = t_proto.get(); #endif if (info.GetAttr("value", t_proto_p).IsOK()) { - ORT_ENFORCE(t_proto_p->has_shape(), "The value tensor of ConstantOfShape should have a shape"); ORT_ENFORCE( t_proto_p->dims_size() == 0 || std::accumulate( From 9df900bc64188f6fd75d3b758007f3e4d01850a7 Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Wed, 8 Oct 2025 14:57:58 -0700 Subject: [PATCH 12/14] update Signed-off-by: Justin Chu --- .../cpu/generator/constant_of_shape_base.h | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h b/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h index 2699211c57e15..98e3777eb6bc8 100644 --- a/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h +++ b/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h @@ -78,14 +78,11 @@ class ConstantOfShapeBase { auto* t_proto_p = t_proto.get(); #endif if (info.GetAttr("value", t_proto_p).IsOK()) { - ORT_ENFORCE( - t_proto_p->dims_size() == 0 || - std::accumulate( - t_proto_p->dims().begin(), - t_proto_p->dims().end(), - 1, - std::multiplies()) == 1, - "The value attribute of ConstantOfShape must be a single-element tensor"); + int64_t size = 1; + for (auto dim : t_proto_p->dims()) { + size *= dim; + } + ORT_ENFORCE(size == 1, "The value attribute of ConstantOfShape must be a single-element tensor"); SetValueFromTensorProto(*t_proto_p); } else { float f_value = 0.f; From a760ff7fe6ed743ac20c00e39b2dd9fdba27b6a1 Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Thu, 9 Oct 2025 09:40:56 -0700 Subject: [PATCH 13/14] Simplify Signed-off-by: Justin Chu --- .../core/providers/cpu/generator/constant_of_shape_base.h | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h b/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h index 98e3777eb6bc8..f08f134d0c080 100644 --- a/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h +++ b/onnxruntime/core/providers/cpu/generator/constant_of_shape_base.h @@ -78,11 +78,9 @@ class ConstantOfShapeBase { auto* t_proto_p = t_proto.get(); #endif if (info.GetAttr("value", t_proto_p).IsOK()) { - int64_t size = 1; for (auto dim : t_proto_p->dims()) { - size *= dim; + ORT_ENFORCE(dim == 1, "The value attribute of ConstantOfShape must be a single-element tensor"); } - ORT_ENFORCE(size == 1, "The value attribute of ConstantOfShape must be a single-element tensor"); SetValueFromTensorProto(*t_proto_p); } else { float f_value = 0.f; From a6be1f61a31ce837c6accdb45f7610f4665ba09f Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Thu, 9 Oct 2025 11:55:14 -0700 Subject: [PATCH 14/14] begin-end Signed-off-by: Justin Chu --- .../core/providers/shared_library/provider_wrappedtypes.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/onnxruntime/core/providers/shared_library/provider_wrappedtypes.h b/onnxruntime/core/providers/shared_library/provider_wrappedtypes.h index 1ab32e649ed40..cdbd0c074f443 100644 --- a/onnxruntime/core/providers/shared_library/provider_wrappedtypes.h +++ b/onnxruntime/core/providers/shared_library/provider_wrappedtypes.h @@ -78,6 +78,8 @@ struct int64s final { const int64_t* data() const { return g_host->int64s__data(this); } const int64_t& operator[](int index) const { return Get(index); } void Reserve(int size) { g_host->int64s__Reserve(this, size); } + const int64_t* begin() const { return data(); } + const int64_t* end() const { return data() + size(); } PROVIDER_DISALLOW_ALL(int64s) };