Skip to content

[CUDA] Special case for K==0 in CUDA MatMul - #21525

Merged
Dmitri Smirnov (yuslepukhin) merged 5 commits into
mainfrom
yuslepukhin/matmul_zero_k
Aug 13, 2024
Merged

Dmitri Smirnov (yuslepukhin) merged 5 commits into
mainfrom
yuslepukhin/matmul_zero_k

Conversation

@yuslepukhin

@yuslepukhin Dmitri Smirnov (yuslepukhin) commented Jul 26, 2024 •

Copy link
Copy Markdown
Contributor

Description

This change addresses a case where we multiply two matrices, and their inner dimension is 0.
numpy and Eigen which is being used in our CPU EP implementation correctly handle this case
and output a [M, N] matrix filled with zeros.

Motivation and Context

This is required to support GenAI empty input Lora implementation.

Addresses: #21483

@tianleiwu

Copy link
Copy Markdown
Contributor

/azp run Windows GPU CI Pipeline

@azure-pipelines

Copy link
Copy Markdown
No pipelines are associated with this pull request.

@yuslepukhin
Dmitri Smirnov (yuslepukhin) deleted the yuslepukhin/matmul_zero_k branch August 13, 2024 18:27
@ranjitshs

Ranjit Ranjan (ranjitshs) commented Aug 14, 2024 •

Copy link
Copy Markdown
Contributor

Dmitri Smirnov (@yuslepukhin) Tianlei Wu (@tianleiwu)
FYI. Our local AIX CI reported the below newly introduced test case failure. I will try to debug.
Please let me know if you have any suggestion.

1: [ RUN      ] MathOpTest.MatMul_ZeroK
1: /home/buildusr/jenkins/workspace/onnxruntime-openxl/onnxruntime/onnxruntime/test/providers/checkers.cc:390: Failure
1: The difference between cur_expected[i] and cur_actual[i] is NaNQ, which exceeds tolerance, where
1: cur_expected[i] evaluates to 0,
1: cur_actual[i] evaluates to -NaNQ, and
1: tolerance evaluates to 9.9999997473787516e-06.
1: i:3
1: Google Test trace:
1: /home/buildusr/jenkins/workspace/onnxruntime-openxl/onnxruntime/onnxruntime/test/providers/checkers.cc:566: provider type: CPUExecutionProvider
1: /home/buildusr/jenkins/workspace/onnxruntime-openxl/onnxruntime/onnxruntime/test/providers/base_tester.cc:827: registered execution providers: CPUExecutionProvider
1: 
1: /home/buildusr/jenkins/workspace/onnxruntime-openxl/onnxruntime/onnxruntime/test/providers/checkers.cc:390: Failure
1: The difference between cur_expected[i] and cur_actual[i] is NaNQ, which exceeds tolerance, where
1: cur_expected[i] evaluates to 0,
1: cur_actual[i] evaluates to -NaNQ, and
1: tolerance evaluates to 9.9999997473787516e-06.
1: i:5
1: Google Test trace:
1: /home/buildusr/jenkins/workspace/onnxruntime-openxl/onnxruntime/onnxruntime/test/providers/checkers.cc:566: provider type: CPUExecutionProvider
1: /home/buildusr/jenkins/workspace/onnxruntime-openxl/onnxruntime/onnxruntime/test/providers/base_tester.cc:827: registered execution providers: CPUExecutionProvider
1: 
1:

@yuslepukhin

Copy link
Copy Markdown
Contributor Author

We do not have AIX build, however, my hunch is that std::fill() with the actual datatype(0) such as float would do better than memset. Can you try it locally and let us know? I would provide a fix.

@ranjitshs

Copy link
Copy Markdown
Contributor

Hi Dmitri Smirnov (@yuslepukhin)
This test failure is intermittent and not happening every time, so it's okay for time being.
I will try to see the frequency of failure in local CI system.
Thanks for the std::fill suggestion. I haven't tried because failure is intermittent.
is it more reliable than memset() for data type other than integers family ?

@yuslepukhin

Copy link
Copy Markdown
Contributor Author

It depends on the floating-point format used on AIX. I have not worked with AIX for nearly 20 years. memset() assumes that all zeros are fine all datatypes. std::fill() would rely on explicitly constructing a given data type constant that represents zero on any given platform with any given compiler, and, therefore, is more portable.

Dmitri Smirnov (yuslepukhin) added a commit that referenced this pull request Aug 15, 2024
### Description
Replace `memset(0)` with `std::fill(T{})`. This would ensure that all
the types are initialized in a portable way.

### Motivation and Context
Some platforms exhibit intermittent failures with NaN results.
Follow up to: #21525

Cc: @ranjitshs
Rohan Jha (rohan11235813) pushed a commit to quadric-io/onnxruntime that referenced this pull request Aug 19, 2025
### Description
Replace `memset(0)` with `std::fill(T{})`. This would ensure that all
the types are initialized in a portable way.

### Motivation and Context
Some platforms exhibit intermittent failures with NaN results.
Follow up to: microsoft/onnxruntime#21525

Cc: @ranjitshs
Rohan Jha (rohan11235813) pushed a commit to quadric-io/onnxruntime that referenced this pull request Sep 15, 2025
### Description
Replace `memset(0)` with `std::fill(T{})`. This would ensure that all
the types are initialized in a portable way.

### Motivation and Context
Some platforms exhibit intermittent failures with NaN results.
Follow up to: microsoft/onnxruntime#21525

Cc: @ranjitshs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants