Skip to content
Closed

WIP #24837

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion onnxruntime/core/providers/webgpu/math/softmax.cc
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ Status SoftmaxProgram::GenerateShaderCode(ShaderHelper& shader) const {

// Calculate the final value for each element in the row
<< " for (var col = lindex; col < cols; col += wg) {\n"
<< " let value = exp(getValue(row, col, row_stride) - row_max_shared) / row_sum_shared;\n"
<< " var value = exp(getValue(row, col, row_stride) - row_max_shared) / row_sum_shared;\n"
<< " // max operation protects against NaN since all values should be >=0\n"
<< " value = max(value, x_value_t(0.0));\n"
<< " setValue(row, col, row_stride, value);\n"
<< " }\n";

Expand Down
16 changes: 16 additions & 0 deletions onnxruntime/test/providers/cpu/math/softmax_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ TEST(SoftmaxOperator, Simple) {
RunTest(x_vals, expected_vals, dimensions);
}

#ifdef USE_WEBGPU
TEST(SoftmaxOperator, webgpu_nan) {
OpTester test("Softmax", 13); // axis default is -1

Comment on lines +53 to +55
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
TEST(SoftmaxOperator, webgpu_nan) {
OpTester test("Softmax", 13); // axis default is -1
TEST(SoftmaxOperator, webgpu_nan) {
OpTester test("Softmax", 13); // axis default is -1

std::vector<float> x_vals = {-INFINITY, -INFINITY, -INFINITY};
std::vector<float> expected_result = {0.0f, 0.0f, 0.0f};
std::vector<int64_t> dimensions = {1, 3};

test.AddInput<float>("X", dimensions, x_vals);
test.AddOutput<float>("Y", dimensions, expected_result);

// explicitly disable CPU EP for this test since CPU implementation does not handle NaN
Comment on lines +61 to +63
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
test.AddOutput<float>("Y", dimensions, expected_result);
// explicitly disable CPU EP for this test since CPU implementation does not handle NaN
test.AddOutput<float>("Y", dimensions, expected_result);
// explicitly disable CPU EP for this test since CPU implementation does not handle NaN

test.Run(OpTester::ExpectResult::kExpectSuccess, "", {kCpuExecutionProvider});
}
#endif

#if defined(USE_CUDA) || defined(USE_ROCM) || defined(USE_XNNPACK)
TEST(SoftmaxOperator, Simple_fp16) {
#ifdef USE_CUDA
Expand Down
Loading