Skip to content

Conversation

@emlin
Copy link
Contributor

@emlin emlin commented Nov 24, 2025

Differential Revision: D87734295

@meta-cla meta-cla bot added the cla signed label Nov 24, 2025
@meta-codesync
Copy link
Contributor

meta-codesync bot commented Nov 24, 2025

@emlin has exported this pull request. If you are a Meta employee, you can view the originating Diff in D87734295.

Summary:

X-link: facebookresearch/FBGEMM#2166

In edge cases, the weight input_stride and output_stride could be different. 
In this case, out will move based on out_stride but copy data with size input_stride. If input_stride is larger than output_stride, the memcpy could overflow.
this fix guarantees the safe memcpy, and is backward compatible for cases that input_stride <= output_stride.

Differential Revision: D87734295
q10 added a commit to q10/FBGEMM that referenced this pull request Nov 26, 2025
…and input_stride > output_stride (pytorch#5169)

Summary:
X-link: facebookresearch/FBGEMM#2170


X-link: facebookresearch/FBGEMM#2166

In edge cases, the weight input_stride and output_stride could be different. 
In this case, out will move based on out_stride but copy data with size input_stride. If input_stride is larger than output_stride, the memcpy could overflow.
this fix guarantees the safe memcpy, and is backward compatible for cases that input_stride <= output_stride.

====

The context is S592128, where the predictor crashes every so often bc of bad input being provided to memcpy, which causes memory overflow. The issues show up when nobag=true, OutType=uint8_t, and input_stride > output_stride.

From what I can tell, if input_stride and output_stride are not provided to us (i.e. a -1 is passed), we compute it ourselves. But it is possible for users to have users pass in custom-sized tables to us, where input_stride is greater than output_stride.

The code already has an assert beforehand to check for this case, but apparently, assert() can be disabled when -DNDEBUG is provided during compilation, which is probably what is being passed in when building the package and why we see the crash.

Emma provided the initial patch, which is to copy just the min(input_stride, output_stride) amount of buffer per row. It provides backward compatibility with existing behavior, and just adjusts it so that the corner case doesnt crash the predictor.

I have added the unit test to the patch, based on the existing EmbeddingSpMDMTest.basicTest. I initially tried to fit it into EmbeddingSpMDMTest.basicTest, but figured it would be better to have a standalone test bc it is testing a corner case, not adding more coordinates to the test matrix.

Reviewed By: emlin, spcyppt

Differential Revision: D87734295
q10 added a commit to q10/FBGEMM that referenced this pull request Nov 26, 2025
…and input_stride > output_stride (pytorch#5169)

Summary:
X-link: facebookresearch/FBGEMM#2170


X-link: facebookresearch/FBGEMM#2166

In edge cases, the weight input_stride and output_stride could be different. 
In this case, out will move based on out_stride but copy data with size input_stride. If input_stride is larger than output_stride, the memcpy could overflow.
this fix guarantees the safe memcpy, and is backward compatible for cases that input_stride <= output_stride.

====

The context is S592128, where the predictor crashes every so often bc of bad input being provided to memcpy, which causes memory overflow. The issues show up when nobag=true, OutType=uint8_t, and input_stride > output_stride.

From what I can tell, if input_stride and output_stride are not provided to us (i.e. a -1 is passed), we compute it ourselves. But it is possible for users to have users pass in custom-sized tables to us, where input_stride is greater than output_stride.

The code already has an assert beforehand to check for this case, but apparently, assert() can be disabled when -DNDEBUG is provided during compilation, which is probably what is being passed in when building the package and why we see the crash.

Emma provided the initial patch, which is to copy just the min(input_stride, output_stride) amount of buffer per row. It provides backward compatibility with existing behavior, and just adjusts it so that the corner case doesnt crash the predictor.

I have added the unit test to the patch, based on the existing EmbeddingSpMDMTest.basicTest. I initially tried to fit it into EmbeddingSpMDMTest.basicTest, but figured it would be better to have a standalone test bc it is testing a corner case, not adding more coordinates to the test matrix.

Reviewed By: emlin, spcyppt

Differential Revision: D87734295
@meta-codesync meta-codesync bot closed this in c7910c4 Nov 26, 2025
@meta-codesync
Copy link
Contributor

meta-codesync bot commented Nov 26, 2025

This pull request has been merged in c7910c4.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants