fastbook icon indicating copy to clipboard operation
fastbook copied to clipboard

Apple Silicon (MPS) Issues for Chapter 1 Code

Open FahimF opened this issue 3 years ago • 8 comments
trafficstars

Now that PyTorch supports Apple Silicon devices, it should be possible to run the Jupyter Notebooks on an Apple Silicon device, right?

I tried and ran into the following issues with the very first training exercise - the #CLICK ME cell. I was able to fix all of them and so mentioning the solutions here in case you want to add the fixes to future releases 🙂

There were three issue if I recall correctly:

  1. There's an issues currently on MPS devices where you'll get the following error due to a bug which is in PR state for PyTorch:
RuntimeError: view size is not compatible with input tensor's size and stride (at least one dimension spans across two contiguous subspaces). Use .reshape(...) instead.

The fix is to pip install fastcore and then run the following before you execute the # CLICK ME cell:

from diffusers.models.attention import BasicTransformerBlock
from fastcore.basics import patch

@patch
def forward(self:BasicTransformerBlock, x, context=None):
    x = self.attn1(self.norm1(x.contiguous())) + x
    x = self.attn2(self.norm2(x), context=context) + x
    x = self.ff(self.norm3(x)) + x
    return x
  1. Running the training will then throw the following error:
[W ParallelNative.cpp:229] Warning: Cannot set number of intraop threads after parallel work has started or after set_num_threads call when using native parallel backend (function set_num_threads)

The above required adding the parameter num_workers=0 to the ImageDataLoader.

  1. Once you get past the above and the fine_tune(1) line executes, you'll get an error like this:
Exception occured in `ProgressCallback` when calling event `after_batch`:
	unsupported format string passed to TensorBase.__format__

There might be a better solution but I solved it by changing line 33 of fastai/callback/progress.py from:

if hasattr(self, 'smooth_loss'): self.pbar.comment = f'{self.smooth_loss:.4f}'

To:

if hasattr(self, 'smooth_loss'): self.pbar.comment = f'{self.smooth_loss.item():.4f}'

Basically, I added an item() to the the self.smooth_loss parameter.

That let me complete the chapter and do everything on an Apple Silicon device.

FahimF avatar Sep 06 '22 03:09 FahimF

How long did it take to train the model? Because my M2 just lags very badly when I tried training a model. I stopped it because it could be trained faster on a cloud server

Tanujshriyan avatar Sep 10 '22 17:09 Tanujshriyan

@Tanujshriyan I don't believe it took too long on an M1 Mac but unfortunately, I've been running on very little sleep and have also been doing some other stuff and so have very little recollection now of what happened several days ago. But I do believe it didn't take inordinately long from my hazy recollections ...

FahimF avatar Sep 10 '22 22:09 FahimF

@FahimF Okay, Thank you!

Tanujshriyan avatar Sep 11 '22 11:09 Tanujshriyan

@FahimF , how did you change the code in fastai/callback/progress.py ? Did you fork the library and build a customized one after the change?

zhiyanshao avatar Sep 17 '22 19:09 zhiyanshao

@zhiyanshao Nope, did all the modifications in the Jupyter notebook. No changes were needed (or at least made) to the fastai core code.

FahimF avatar Sep 18 '22 00:09 FahimF

@FahimF , do you have an example of how to changing line 33 of fastai/callback/progress.py ? This file is in the fastai library, right? Thanks!

zhiyanshao avatar Sep 18 '22 00:09 zhiyanshao

@zhiyanshao Ah, yes, sorry 😄 Been a while since I did all that and have been working on a bunch of other things since then. I probably modified it in place. Just find where it's located in your Python package location on your hard drive and modify it in place ...

The pip show command should tell you where the package is.

FahimF avatar Sep 18 '22 00:09 FahimF

works for me.thanks a alot.

h4rk8s avatar Oct 29 '22 07:10 h4rk8s