Skip to content

Commit b3c316f

Browse files
authored
[CUDA] Fix clip operator (#25057)
### Description The result of Clip op is not expected when min > max for CUDA. This fixes the implementation to align with operator spec: https://onnx.ai/onnx/operators/onnx__Clip.html ### Motivation and Context ONNX backend test failure with onnx 1.18: python onnx_backend_test_series.py ====================================================================== FAIL: test_clip_min_greater_than_max_cuda (__main__.OnnxBackendNodeModelTest) ---------------------------------------------------------------------- DESIRED: array([1., 1., 1.], dtype=float32) ACTUAL: array([2., 2., 1.], dtype=float32)
1 parent e7c9a6c commit b3c316f

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

onnxruntime/core/providers/cuda/math/clip_impl.cu

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@ namespace onnxruntime {
88
namespace cuda {
99
template <typename T>
1010
__global__ void _Clip(const T* input, T* output, const T* min, const T* max, T min_default, T max_default, size_t N) {
11-
auto min_val = (min) ? *min : min_default;
12-
auto max_val = (max) ? *max : max_default;
11+
auto min_val = (min) ? *min : min_default;
12+
auto max_val = (max) ? *max : max_default;
1313
CALCULATE_ELEMENTWISE_INDEX_OR_EXIT(id, N);
14-
output[id] = (input[id] < min_val) ? min_val : ((input[id] > max_val) ? max_val : input[id]);
14+
15+
// output = Min(max, Max(input, min)). Note that min might be larger than max, so we need to compute in two steps.
16+
auto value = input[id];
17+
value = (value < min_val) ? min_val : value;
18+
output[id] = (value > max_val) ? max_val : value;
1519
}
1620

1721
template <typename T>

0 commit comments

Comments
 (0)