8000 [MPS] Fix sliced cast by malfet · Pull Request #138314 · pytorch/pytorch · GitHub
[go: up one dir, main page]

Skip to content

[MPS] Fix sliced cast #138314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed

[MPS] Fix sliced cast #138314

wants to merge 4 commits into from

Conversation

malfet
Copy link
Contributor
@malfet malfet commented Oct 18, 2024

This fixes internal crash due to the invalid bufer size computation if sliced API is used

Not sure what was the purpose of

IntArrayRef baseShape;
if (src.is_view()) {
  baseShape = src._base().sizes();
} else {
  baseShape = getIMPSAllocator()->getBufferShape(src.storage().data());
}
int flattenedShaped = 1;
for (const auto i : c10::irange(baseShape.size())) {
  flattenedShaped *= baseShape[i];
}

As flattenShaped could be much easier computed as [srcBuf lengh]/src.element_size(), and even if srcBuf is padded it's a safe thing to do.

When someone allocated buffer to hold say uint8 and that view-casted it
to float16, attempt to compute baseShape returned sizes of original
tensor in its data type, rather than size in new dtypes

Fixes #137800

This fixes internal crash due to the invalid bufer size computation if sliced API is used

Not sure what was the purpose of
```c++
IntArrayRef baseShape;
if (src.is_view()) {
  baseShape = src._base().sizes();
} else {
  baseShape = getIMPSAllocator()->getBufferShape(src.storage().data());
}
int flattenedShaped = 1;
for (const auto i : c10::irange(baseShape.size())) {
  flattenedShaped *= baseShape[i];
}
```
As flattenShaped could be much easier computed as `[srcBuf
lengh]/src.element_size()`

When someone allocated buffer to hold say uint8 and that view-casted it
to float16, attempt to compute `baseShape` returned sizes of original
tensor in its data type, rather than size in new dtypes

Fixes #137800
@malfet malfet requested a review from kulinseth as a code owner October 18, 2024 05:34
Copy link
pytorch-bot bot commented Oct 18, 2024

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/138314

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 451e406 with merge base a9014d2 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@pytorch-bot pytorch-bot bot added ciflow/mps Run MPS tests (subset of trunk) release notes: mps Release notes category labels Oct 18, 2024
@malfet malfet added the topic: bug fixes topic category label Oct 18, 2024
Copy link
Collaborator
@albanD albanD left a comment

Choose a reason for hiding this comment

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

Sounds good thanks!
We can use storage().size() as well if we don't want to query the srcBuf.

@DenisVieriu97
Copy link
Collaborator

I think the main idea was that the storage buffer might be bigger than the actual number of elements * element_size since these are aligned, so the flattened shape would be slightly higher than what the original tensor would be. But it seems this also works (tests seem to have passed).

@malfet
Copy link
Contributor Author
malfet commented Oct 19, 2024

@pytorchbot merge -f "MPS changes are green"

@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged immediately since you used the force (-f) flag, bypassing any CI checks (ETA: 1-5 minutes). Please use -f as last resort and instead consider -i/--ignore-current to continue the merge ignoring current failures. This will allow currently pending tests to finish and report signal before the merge.

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@malfet
Copy link
Contributor Author
malfet commented Oct 22, 2024

@pytorchbot cherry-pick --onto release/2.5 -c regression

pytorchbot pushed a commit that referenced this pull request Oct 22, 2024
This fixes internal crash due to the invalid bufer size computation if sliced API is used

Not sure what was the purpose of
```c++
IntArrayRef baseShape;
if (src.is_view()) {
  baseShape = src._base().sizes();
} else {
  baseShape = getIMPSAllocator()->getBufferShape(src.storage().data());
}
int flattenedShaped = 1;
for (const auto i : c10::irange(baseShape.size())) {
  flattenedShaped *= baseShape[i];
}
```
As flattenShaped could be much easier computed as `[srcBuf
lengh]/src.element_size()`, and even if `srcBuf` is padded it's a safe thing to do.

When someone allocated buffer to hold say uint8 and that view-casted it
to float16, attempt to compute `baseShape` returned sizes of original
tensor in its data type, rather than size in new dtypes

Fixes #137800
Pull Request resolved: #138314
Approved by: https://github.com/albanD, https://github.com/DenisVieriu97

(cherry picked from commit de16159)
@pytorchbot
Copy link
Collaborator

Cherry picking #138314

The cherry pick PR is at #138535 and it is recommended to link a regression cherry pick PR with an issue.

Details for Dev Infra team Raised by workflow job

malfet added a commit that referenced this pull request Oct 22, 2024
[MPS] Fix sliced cast (#138314)

This fixes internal crash due to the invalid bufer size computation if sliced API is used

Not sure what was the purpose of
```c++
IntArrayRef baseShape;
if (src.is_view()) {
  baseShape = src._base().sizes();
} else {
  baseShape = getIMPSAllocator()->getBufferShape(src.storage().data());
}
int flattenedShaped = 1;
for (const auto i : c10::irange(baseShape.size())) {
  flattenedShaped *= baseShape[i];
}
```
As flattenShaped could be much easier computed as `[srcBuf
lengh]/src.element_size()`, and even if `srcBuf` is padded it's a safe thing to do.

When someone allocated buffer to hold say uint8 and that view-casted it
to float16, attempt to compute `baseShape` returned sizes of original
tensor in its data type, rather than size in new dtypes

Fixes #137800
Pull Request resolved: #138314
Approved by: https://github.com/albanD, https://github.com/DenisVieriu97

(cherry picked from commit de16159)

Co-authored-by: Nikita Shulga <nikita.shulga@gmail.com>
@github-actions github-actions bot deleted the malfet/mps-fix-sliced-cast branch November 21, 2024 02:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ciflow/mps Run MPS tests (subset of trunk) Merged release notes: mps Release notes category topic: bug fixes topic category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Nightly introduced bug for GGUF in comfy?
5 participants
0