fastbook
fastbook copied to clipboard
Apple Silicon (MPS) Issues for Chapter 1 Code
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:
- 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
- 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.
- 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.
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 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 Okay, Thank you!
@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 Nope, did all the modifications in the Jupyter notebook. No changes were needed (or at least made) to the fastai core code.
@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 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.
works for me.thanks a alot.