はじめに
M1Macを使用していて、PyTorchを実行すると、diffusersモジュールの実行時に表題のエラーが発生しました。
解消方法をまとめました
エラー発生の経緯
環境
-
- M1Macbook
-
- Python3.9
- venv
上記の環境で仮想環境内でPyTorchとdiffusersモジュールをインストールしてPythonアプリを実行しました。
するとdiffusersの関数実行時にエラーが発生
Torch not compiled with CUDA enabled
ちなみに実行したコードはこちら
import torch
from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler
from diffusers.utils import export_to_video
pipe = DiffusionPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5",
torch_dtype=torch.float16,
)
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
pipe.enable_model_cpu_offload()
# ビデオを作成
prompt = "A bustling city street in Tokyo during cherry blossom season"
video_frames = pipe(prompt, num_inference_steps=25).frames
video_path = export_to_video(video_frames)
解消法
エラーを調べているとM1Mac環境だとAnacondaの環境を使って上手くいったという記事を見て
Anacondaをインストール
仮想環境を作成して有効化します
conda create --name sampleapp python=3.9
conda activate sampleapp
pip install diffusersを実行すると
Rustが必要というメッセージが出たのでRustをインストールします
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Rustがインストールされていることを確認します
rustc -V
M1Mac用のRustのtargetをインストールします
rustup target add x86_64-apple-darwin
diffusersをインストールします
pip install diffusers
インストールが成功しました
そしてPythonアプリの実行も成功しました
めでたしめでたし
ちなみにdffusersについて
diffusersはPython3.11は対応していないようなので3.8や3.9を使うのがいいです
PyTorchは環境要因があるのでGoogle Colaboratoryを使うと楽です
ランタイムをCPUからGPUに変えるのをお忘れなく