From 73a2eb82eb9364b4dea8df2cd6a46affd008b15c Mon Sep 17 00:00:00 2001 From: Wanming Lin Date: Fri, 1 Dec 2023 08:19:22 +0800 Subject: [PATCH] Fixed bug in Flatten's axis (#18645) Flatten's axis is in the range [-r, r] rather than [-r, r-1]. --- .../providers/webnn/builders/impl/flatten_op_builder.cc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/onnxruntime/core/providers/webnn/builders/impl/flatten_op_builder.cc b/onnxruntime/core/providers/webnn/builders/impl/flatten_op_builder.cc index f0df27b523..31b1bd92a9 100644 --- a/onnxruntime/core/providers/webnn/builders/impl/flatten_op_builder.cc +++ b/onnxruntime/core/providers/webnn/builders/impl/flatten_op_builder.cc @@ -36,7 +36,11 @@ Status FlattenOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder, int64_t rank = input_shape.size(); NodeAttrHelper helper(node); int64_t axis = helper.Get("axis", 1); - axis = HandleNegativeAxis(axis, rank); + ORT_ENFORCE(axis >= -rank && axis <= rank, "axis ", axis, + " is not in valid range [-", rank, ",", rank, "]"); + if (axis < 0) { + axis += rank; + } // Use WebNN's reshape to implement Flatten. int64_t num_pre_axis_elements = std::accumulate(