Skip to content

Instantly share code, notes, and snippets.

@7shi
Last active October 22, 2024 18:59
Show Gist options
  • Save 7shi/d21aafc66ebfeacbdf64aab833c3aa6c to your computer and use it in GitHub Desktop.
Save 7shi/d21aafc66ebfeacbdf64aab833c3aa6c to your computer and use it in GitHub Desktop.
ZLUDA メモ

ZLUDA メモ

動作保証できかねます。使用は自己責任で。

本家

フォーク(スクリプトから使用)

使用例(ファイルを流用)

関連記事

準備

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 ディレクトリに入れます。

作業ディレクトリ直下に以下の内容で 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]'

diffusers

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment