動作保証できかねます。使用は自己責任で。
本家
フォーク(スクリプトから使用)
使用例(ファイルを流用)
関連記事
- https://gigazine.net/news/20240213-zluda-nvidia-amd/
- https://gigazine.net/news/20240305-nvidia-cuda/
- https://www.hpcwire.jp/archives/88912
- https://www.hpcwire.jp/archives/90197
AMD の HIP でサポートされた GPU を用意して、HIP と Python 3.10 をインストールします。
作業用のディレクトリを作って、Python の仮想環境を構築します。
python -m venv zluda
zluda\Scripts\activate.bat
指定されたバージョンの torch をインストールします。
pip install torch==2.3.0 torchvision --index-url https://download.pytorch.org/whl/cu118
modules ディレクトリを作成します。
md modules
SD.Next から以下のファイルをダウンロードして、modules ディレクトリに入れます。
- https://github.com/vladmandic/automatic/blob/master/modules/rocm.py
- https://github.com/vladmandic/automatic/blob/master/modules/zluda_installer.py
作業ディレクトリ直下に以下の内容で zluda.py を作成します。(コメントは引用元)
# https://github.com/vladmandic/automatic/blob/master/cli/zluda-python.py
import os
from modules import zluda_installer
zluda_path = zluda_installer.get_path()
if not os.path.exists(zluda_path):
zluda_installer.install(zluda_path)
zluda_installer.make_copy(zluda_path)
zluda_installer.load(zluda_path)
# https://github.com/lshqqytiger/ZLUDA
import torch
torch.backends.cudnn.enabled = False
torch.backends.cuda.enable_flash_sdp(False)
torch.backends.cuda.enable_math_sdp(True)
torch.backends.cuda.enable_mem_efficient_sdp(False)
def jit_script(f, *_, **__):
f.graph = torch._C.Graph() # pylint: disable=protected-access
return f
torch.jit.script = jit_script
_topk = torch.topk
def topk(tensor: torch.Tensor, *args, **kwargs):
device = tensor.device
values, indices = _topk(tensor.cpu(), *args, **kwargs)
return torch.return_types.topk((values.to(device), indices.to(device),))
torch.topk = topk
これで torch が ZLUDA 対応になります。
zluda を torch より先に import
します。
Python の REPL での使用例です。
>>> import zluda, torch
>>> torch.cuda.is_available()
True
>>> torch.cuda.get_device_name(0)
'AMD Radeon RX 7600 XT [ZLUDA]'
ZLUDA を使用して画像生成を行います。
pip install diffusers transformers accelerate
サンプルでは以下のモデルファイルを使用します。
import zluda, torch, diffusers
pipe = diffusers.StableDiffusionPipeline.from_single_file(
"nai-anime-v1-full.safetensors", torch_dtype=torch.float16)
pipe.scheduler = diffusers.EulerDiscreteScheduler.from_config(pipe.scheduler.config)
pipe = pipe.to("cuda")
torch.manual_seed(2870305590)
from datetime import datetime
start = datetime.now()
for i in range(10):
print(i + 1, "/", 10)
result = pipe(
prompt = "masterpiece, best quality, masterpiece, asuka langley sitting cross legged on a chair",
negative_prompt = "lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts,signature, watermark, username, blurry, artist name",
width = 512, height = 512, num_inference_steps = 28, guidance_scale = 12, clip_skip = 2)
result.images[0].save(f"hello-asuka-{i + 1:02d}.png")
print(datetime.now() - start)
初回起動時はコンパイルが行われるため時間がかかります。
Compiling in progress. Please wait...
何回か実行すればコンパイルが収まって安定します。所要時間の例を示します。
回目 | 所要時間 |
---|---|
1 | 0:15:48.617763 |
2 | 0:00:55.395871 |
3 | 0:02:31.159087 |
4 | 0:00:56.689619 |
5 | 0:00:55.430992 |
コンパイル結果は zluda.db にキャッシュされます。場所の探し方を示します。
dir /s %LOCALAPPDATA%\zluda.db