Thanks to visit codestin.com
Credit goes to github.com

Skip to content

buffer is not large enough when running pytorch on Mac M1 mps #77886

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
xiaoouwang opened this issue May 19, 2022 · 34 comments
Closed

buffer is not large enough when running pytorch on Mac M1 mps #77886

xiaoouwang opened this issue May 19, 2022 · 34 comments
Labels
module: memory usage PyTorch is using more memory than it should, or it is leaking memory module: mps Related to Apple Metal Performance Shaders framework triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Comments

@xiaoouwang
Copy link

xiaoouwang commented May 19, 2022

πŸ› Describe the bug

The bug seems related to #77851

To reproduce the bug:

from transformers import AutoModel
from transformers import AutoTokenizer
import torch
model_ckpt = "distilbert-base-uncased"
device = torch.device("mps")
model = AutoModel.from_pretrained(model_ckpt).to(device)
model_ckpt = "distilbert-base-uncased"
tokenizer = AutoTokenizer.from_pretrained(model_ckpt)
text = "this is a test"
inputs = tokenizer(text, return_tensors="pt")
inputs = {k:v.to(device) for k,v in inputs.items()}
with torch.no_grad():
    outputs = model(**inputs)

The error message:

/AppleInternal/Library/BuildRoots/8d3bda53-8d9c-11ec-abd7-fa6a1964e34e/Library/Caches/com.apple.xbs/Sources/MetalPerformanceShaders/MPSCore/Types/MPSNDArray.mm:782: failed assertion `[MPSNDArray, initWithBuffer:descriptor:] Error: buffer is not large enough. Must be 432 bytes
'
[1] 75519 abort python 02.py
/Users/xiaoou/opt/anaconda3/lib/python3.9/multiprocessing/resource_tracker.py:216: UserWarning: resource_tracker: There appear to be 1 leaked semaphore objects to clean up at shutdown
warnings.warn('resource_tracker: There appear to be %d '

Versions

1.12.0.dev20220519

@albanD albanD added module: memory usage PyTorch is using more memory than it should, or it is leaking memory triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module module: mps Related to Apple Metal Performance Shaders framework labels May 19, 2022
@glenn-jocher
Copy link

glenn-jocher commented May 20, 2022

Same issue with YOLOv5 on MPS noted in #77748 (comment). I see buffer is not large enough. Must be 25600 bytes

@Willian-Zhang

This comment was marked as off-topic.

@XinBow99
Copy link

same issue
ζˆͺεœ– 2022-05-22 δΈ‹εˆ8 21 14

@razarmehr
Copy link
Collaborator

This issue is fixed in PR #78496 (nightly build 1.13.0.dev20220531 or later).

facebook-github-bot pushed a commit that referenced this issue Jun 1, 2022
…#78496)

Summary:
Fixes #78247, #77886

Pull Request resolved: #78496
Approved by: https://github.com/albanD, https://github.com/malfet

Test Plan: contbuild & OSS CI, see https://hud.pytorch.org/commit/pytorch/pytorch/017b0ae9431ae3780a4eb9bf6d8865dfcd02cd92

Reviewed By: seemethere

Differential Revision: D36784418

Pulled By: seemethere

fbshipit-source-id: 3558273f2fa3342f4e028fe1186c733bc5a370a8
@crayon7442
Copy link

Same issue with YOLOv5 on device "mps"
MPSNDArray.mm:782: failed assertion `[MPSNDArray, initWithBuffer:descriptor:] Error: buffer is not large enough. Must be 19200 bytes' #78492

@crayon7442
Copy link

This issue is fixed in PR #78496 (nightly build 1.13.0.dev20220531 or later).
This issue has not been fixed..

@glenn-jocher
Copy link

@jerjer1223 @GerardWalsh can you please reinstall nightly and see if this resolves ultralytics/yolov5#8102

malfet pushed a commit that referenced this issue Jun 7, 2022
@csmetzner
Copy link

csmetzner commented Jun 8, 2022

Hello I am still receiving this error. What do I have to do to resolve this bug/issue? Thanks.

pytorch nightly version: torch-1.13.0.dev20220607

/AppleInternal/Library/BuildRoots/b6051351-c030-11ec-96e9-3e7866fcf3a1/Library/Caches/com.apple.xbs/Sources/MetalPerformanceShaders/MPSCore/Types/MPSNDArray.mm:782: failed assertion `[MPSNDArray, initWithBuffer:descriptor:] Error: buffer is not large enough. Must be 160000 bytes

@jerjer1223
Copy link

jerjer1223 commented Jun 8, 2022

I'm also still receiving this error, a fix would be appreciated.

PyTorch version 1.13.0.dev20220607

Fusing layers...
YOLOv5s summary: 213 layers, 7225885 parameters, 0 gradients
/AppleInternal/Library/BuildRoots/b6051351-c030-11ec-96e9-3e7866fcf3a1/Library/Caches/com.apple.xbs/Sources/MetalPerformanceShaders/MPSCore/Types/MPSNDArray.mm:782: failed assertion `[MPSNDArray, initWithBuffer:descriptor:] Error: buffer is not large enough. Must be 25600 bytes

@GerardWalsh
Copy link

GerardWalsh commented Jun 8, 2022

@glenn-jocher

can you please reinstall nightly and see if this resolves ultralytics/yolov5#8102

No it does not

python yolov5/detect.py --source 0 --device='mps'                                       
detect: weights=yolov5/yolov5s.pt, source=0, data=yolov5/data/coco128.yaml, imgsz=[640, 640], conf_thres=0.25, iou_thres=0.45, max_det=1000, device=mps, view_img=False, save_txt=False, save_conf=False, save_crop=False, nosave=False, classes=None, agnostic_nms=False, augment=False, visualize=False, update=False, project=yolov5/runs/detect, name=exp, exist_ok=False, line_thickness=3, hide_labels=False, hide_conf=False, half=False, dnn=False
YOLOv5 πŸš€ v6.1-246-g2dd3db0 Python-3.8.13 torch-1.13.0.dev20220607 MPS

Fusing layers... 
YOLOv5s summary: 213 layers, 7225885 parameters, 0 gradients
1/1: 0...  Success (inf frames 1280x720 at 30.00 FPS)

/AppleInternal/Library/BuildRoots/b6051351-c030-11ec-96e9-3e7866fcf3a1/Library/Caches/com.apple.xbs/Sources/MetalPerformanceShaders/MPSCore/Types/MPSNDArray.mm:782: failed assertion `[MPSNDArray, initWithBuffer:descriptor:] Error: buffer is not large enough. Must be 25600 bytes
'
zsh: abort      python yolov5/detect.py --source 0 --device='mps'
Collecting environment information...
PyTorch version: 1.13.0.dev20220607
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: macOS 12.4 (arm64)
GCC version: Could not collect
Clang version: 13.1.6 (clang-1316.0.21.2.5)
CMake version: Could not collect
Libc version: N/A

Python version: 3.8.13 | packaged by conda-forge | (default, Mar 25 2022, 06:04:14)  [Clang 12.0.1 ] (64-bit runtime)
Python platform: macOS-12.4-arm64-arm-64bit
Is CUDA available: False
CUDA runtime version: No CUDA
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

Versions of relevant libraries:
[pip3] numpy==1.22.4
[pip3] torch==1.13.0.dev20220607
[pip3] torchvision==0.14.0.dev20220608
[conda] numpy                     1.22.4           py38he1fcd3f_0    conda-forge
[conda] pytorch                   1.13.0.dev20220607         py3.8_0    pytorch-nightly
[conda] torchvision               0.14.0.dev20220603          pypi_0    pypi

@giuseppebrb
Copy link

giuseppebrb commented Jun 12, 2022

Same issue try running YOLOv5s with mps on M1 Pro

YOLOv5 πŸš€ 2022-6-12 Python-3.9.12 torch-1.13.0.dev20220612 MPS

/AppleInternal/Library/BuildRoots/b6051351-c030-11ec-96e9-3e7866fcf3a1/Library/Caches/com.apple.xbs/Sources/MetalPerformanceShaders/MPSCore/Types/MPSNDArray.mm:782: failed assertion [MPSNDArray, initWithBuffer:descriptor:] Error: buffer is not large enough. Must be 19200 bytes

@mclean-connor
Copy link

mclean-connor commented Jun 13, 2022

same issue when running with stable baselines 3 contrib PPO recurrent

/AppleInternal/Library/BuildRoots/b6051351-c030-11ec-96e9-3e7866fcf3a1/Library/Caches/com.apple.xbs/Sources/MetalPerformanceShaders/MPSCore/Types/MPSNDArray.mm:782: failed assertion `[MPSNDArray, initWithBuffer:descriptor:] Error: buffer is not large enough. Must be 576 bytes
'

@Darkfeast
Copy link

YOLOv5 πŸš€ v6.1-253-g75bbaa8 Python-3.10.4 torch-1.13.0.dev20220616 MPS

image

/AppleInternal/Library/BuildRoots/b6051351-c030-11ec-96e9-3e7866fcf3a1/Library/Caches/com.apple.xbs/Sources/MetalPerformanceShaders/MPSCore/Types/MPSNDArray.mm:782: failed assertion `[MPSNDArray, initWithBuffer:descriptor:] Error: buffer is not large enough. Must be 25600 bytes

glenn-jocher added a commit to ultralytics/yolov5 that referenced this issue Jul 2, 2022
Require explicit request for MPS, i.e.
```bash
python detect.py --device mps
```

Reverts #8210 for preferring MPS if available. 

Note that torch MPS is experiencing ongoing compatibility issues in pytorch/pytorch#77886
@glenn-jocher
Copy link

glenn-jocher commented Jul 2, 2022

I confirm I'm experiencing the same YOLOv5 Apple MPS bug with torch 1.12 on MacBook M1: Error: buffer is not large enough. Must be 25600 bytes

$ glennjocher@Glenns-MacBook-Air yolov5 % python detect.py --device mps

detect: weights=yolov5s.pt, source=data/images, data=data/coco128.yaml, imgsz=[640, 640], conf_thres=0.25, iou_thres=0.45, max_det=1000, device=mps, view_img=False, save_txt=False, save_conf=False, save_crop=False, nosave=False, classes=None, agnostic_nms=False, augment=False, visualize=False, update=False, project=runs/detect, name=exp, exist_ok=False, line_thickness=3, hide_labels=False, hide_conf=False, half=False, dnn=False
YOLOv5 πŸš€ v6.1-386-g858a1a3 Python-3.9.13 torch-1.12.0 MPS

Fusing layers... 
YOLOv5s summary: 213 layers, 7225885 parameters, 0 gradients
/AppleInternal/Library/BuildRoots/b6051351-c030-11ec-96e9-3e7866fcf3a1/Library/Caches/com.apple.xbs/Sources/MetalPerformanceShaders/MPSCore/Types/MPSNDArray.mm:782: failed assertion `[MPSNDArray, initWithBuffer:descriptor:] Error: buffer is not large enough. Must be 25600 bytes

@glenn-jocher
Copy link

glenn-jocher commented Jul 2, 2022

This issue is fixed in PR #78496 (nightly build 1.13.0.dev20220531 or later).

@albanD @razarmehr is this error supposed to exist in torch 1.12? I see the fix was was in a 1.13 nightly.

glenn-jocher added a commit to ultralytics/yolov5 that referenced this issue Jul 2, 2022
Require explicit request for MPS, i.e.
```bash
python detect.py --device mps
```

Reverts #8210 for preferring MPS if available. 

Note that torch MPS is experiencing ongoing compatibility issues in pytorch/pytorch#77886
@albanD
Copy link
Collaborator

albanD commented Jul 4, 2022

Hi,

I'm not sure if this made it for the release no.
If you're using MPS a lot, I would recommend using the nightly though as we did quite a few fixes that didn't make it to 1.12.

@malfet
Copy link
Contributor

malfet commented Jul 4, 2022

I'm not sure if this made it for the release no. If you're using MPS a lot, I would recommend using the nightly though as we did quite a few fixes that didn't make it to 1.12.

Hmm #78496 was picked into release branch as e3e7531

@malfet malfet reopened this Jul 4, 2022
@malfet
Copy link
Contributor

malfet commented Jul 4, 2022

Reopening to investigate if it still crashes on trunk, and if it is not, why Pytorch-1.12 is still affected

Shivvrat pushed a commit to Shivvrat/epic-yolov5 that referenced this issue Jul 12, 2022
Require explicit request for MPS, i.e.
```bash
python detect.py --device mps
```

Reverts ultralytics#8210 for preferring MPS if available. 

Note that torch MPS is experiencing ongoing compatibility issues in pytorch/pytorch#77886
@daniwnwd
Copy link

I'm still getting the issue on 1.13.0.dev20220712

Is there any fix?

@DenisVieriu97
Copy link
Collaborator

@glenn-jocher, @daniwnwd this should be fixed in the latest PyTorch nightly (1.13.0.dev20220722). Could you please let me know if you are still seeing the issue on your end?

@crayon7442
Copy link

@DenisVieriu97
NotImplementedError: The operator 'aten::index.Tensor_out' is not current implemented for the MPS device. #82034

@glenn-jocher
Copy link

@DenisVieriu97 I confirm that the original buffer is not large enough error is now resolved in latest nightly.

YOLOv5 inference still fails on operator 'aten::index.Tensor_out' is not current implemented for the MPS device as mentioned by @crayon7442, but that's a separate issue, so I believe this issue can be closed now.

@DenisVieriu97
Copy link
Collaborator

Thanks a lot @crayon7442 and @glenn-jocher for checking this!
index.Tensor_out is already part of https://github.com/kulinseth/pytorch and we hope to get it soon in PyTorch master

@glenn-jocher
Copy link

@DenisVieriu97 awesome! Thanks for the update.

ctjanuhowski pushed a commit to ctjanuhowski/yolov5 that referenced this issue Sep 8, 2022
Require explicit request for MPS, i.e.
```bash
python detect.py --device mps
```

Reverts ultralytics#8210 for preferring MPS if available. 

Note that torch MPS is experiencing ongoing compatibility issues in pytorch/pytorch#77886
@astrowonk
Copy link

astrowonk commented Oct 3, 2022

I'm on the latest nightly torch-1.13.0.dev20221003 and still getting this error. Could it be cause aten::repeat_interleave.self_int fell back to CPU?

/usr/local/opt/miniforge3/lib/python3.9/site-packages/whisper/decoding.py:628:
/UserWarning: The operator 'aten::repeat_interleave.self_int' is not currently
/supported on the MPS backend and will fall back to run on the CPU. This may
/have performance implications. (Triggered internally at
//Users/runner/work/pytorch/pytorch/pytorch/aten/src/ATen/mps/MPSFallback.mm:11
/.)
  audio_features = audio_features.repeat_interleave(self.n_group, dim=0)
/AppleInternal/Library/BuildRoots/a0876c02-1788-11ed-b9c4-96898e02b808/Library/
/Caches/com.apple.xbs/Sources/MetalPerformanceShaders/MPSCore/Types/MPSNDArray.
/mm:782: failed assertion `[MPSNDArray, initWithBuffer:descriptor:] Error:
/buffer is not large enough. Must be 201452 bytes
'

Edited to hardwrap the long lines

@glenn-jocher
Copy link

@astrowonk that's not an error, that's a warning. As the message states not all torch ops are fully converted to MPS yet.

@astrowonk
Copy link

@astrowonk that's not an error, that's a warning. As the message states not all torch ops are fully converted to MPS yet.

I think you have have to side scroll to see everything @glenn-jocher, the formatting wasn't great. " Error: buffer is not large enough. Must be 201452 bytes" is there on the last line.

and then the kernel dies (in Ipython or Jupyter)

@kulinseth
Copy link
Collaborator

@astrowonk , can you create a new issue with the network and command line and we will take a look. Thanks.

@glenn-jocher
Copy link

@astrowonk ah yes, I stand corrected!

@astrowonk
Copy link

@astrowonk , can you create a new issue with the network and command line and we will take a look. Thanks.

@kulinseth Opened #86152

@joannercsheppard
Copy link

I just had this issue and I found the solution. It turns out the error was because my MacOS was too old and PyTorch mps was not compatible. If nothing else it working try updating the operating system.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module: memory usage PyTorch is using more memory than it should, or it is leaking memory module: mps Related to Apple Metal Performance Shaders framework triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

No branches or pull requests