🐙

Stable Diffusionによる画像生成(Python ローカル実行版)

2022/09/10に公開

概要

Stable Diffusionによる画像生成(Google Colab版) ができたので、ローカル実行を試してみます。

自動運転のOSSや、仮想通貨のマイニング(結局やっていない)等を試すために用意したビデオボードがあるので、こちらを活用したいという気持ちもありました。

なお、使っているのは ZOTAC GAMING GeForce RTX 2060 Twin Fan (Memory 6GB) です。

下記のページを参考にしました。

参考ページ1

参考というよりは公式ページ

参考ページ2

セットアップ

pipでのインストールとPyhonスクリプトの実行ができればよいようなので、Windows版のPython環境で実行してみます。

versionは3.7.3です。

>python --version
Python 3.7.3

結果的には下記を実行しました。

pip install --upgrade diffusers transformers scipy
pip install torch==1.11.0+cu113 torchvision==0.12.0+cu113 torchaudio==0.11.0 --extra-index-url https://download.pytorch.org/whl/cu113
huggingface-cli login

スクリプト

結果的には test2.py を使用して画像生成できました。

test.py

※参考ページ1から引用させていただいています。

import torch
from torch import autocast
from diffusers import StableDiffusionPipeline

model_id = "CompVis/stable-diffusion-v1-4"
device = "cuda"


pipe = StableDiffusionPipeline.from_pretrained(model_id, use_auth_token=True)
pipe = pipe.to(device)

prompt = "a photo of an astronaut riding a horse on mars"
with autocast("cuda"):
    image = pipe(prompt, guidance_scale=7.5)["sample"][0]  
    
image.save("astronaut_rides_horse.png")

test1.py

※参考ページ1の情報を基に、GPU RAM が 10GB よりも少ない場合、デフォルトの float32 精度の替わりに float16 precision を設定してみたもの

import torch
from torch import autocast
from diffusers import StableDiffusionPipeline

model_id = "CompVis/stable-diffusion-v1-4"
device = "cuda"


pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16, revision="fp16", use_auth_token=True)
pipe = pipe.to(device)

prompt = "a photo of an astronaut riding a horse on mars"
with autocast("cuda"):
    image = pipe(prompt, guidance_scale=7.5)["sample"][0]  
    
image.save("astronaut_rides_horse.png")

test2.py

※参考ページ2から引用させていただいています。(空行のみ一部削除)

from diffusers import StableDiffusionPipeline, LMSDiscreteScheduler
import torch
from torch import autocast

access_tokens="HuggingFaceで作ったAccessTokenをここに入れます" # @param {type:"string"}
model_id = "CompVis/stable-diffusion-v1-4"

# Use the K-LMS scheduler here instead
scheduler = LMSDiscreteScheduler(beta_start=0.00085, beta_end=0.012, beta_schedule="scaled_linear", num_train_timesteps=1000)

pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16, revision="fp16", use_auth_token=access_tokens)
pipe = pipe.to("cuda")

prompt = "A silver-haired girl standing in front of a giant Gundam-style robot"

with autocast("cuda"):
    image = pipe(prompt, guidance_scale=7.5)["sample"][0]  

image.save("test.png")

履歴

pip で diffusers transformers scipy をインストール

>pip install --upgrade diffusers transformers scipy
Collecting diffusers
  Downloading diffusers-0.2.4-py3-none-any.whl (112 kB)
     ---------------------------------------- 113.0/113.0 kB 3.3 MB/s eta 0:00:00
Collecting transformers
  Downloading transformers-4.21.2-py3-none-any.whl (4.7 MB)
     ---------------------------------------- 4.7/4.7 MB 545.7 kB/s eta 0:00:00
Collecting scipy
  Downloading scipy-1.7.3-cp37-cp37m-win_amd64.whl (34.1 MB)
     ---------------------------------------- 34.1/34.1 MB 415.9 kB/s eta 0:00:00
Collecting regex!=2019.12.17
  Downloading regex-2022.8.17-cp37-cp37m-win_amd64.whl (263 kB)
     ---------------------------------------- 263.3/263.3 kB 954.8 kB/s eta 0:00:00
Collecting filelock
  Downloading filelock-3.8.0-py3-none-any.whl (10 kB)
Requirement already satisfied: requests in c:\usr\python37\lib\site-packages (from diffusers) (2.27.1)
Collecting torch>=1.4
  Downloading torch-1.12.1-cp37-cp37m-win_amd64.whl (161.9 MB)
     ---------------------------------------- 161.9/161.9 MB 676.2 kB/s eta 0:00:00
Collecting huggingface-hub<1.0,>=0.8.1
  Downloading huggingface_hub-0.9.1-py3-none-any.whl (120 kB)
     ---------------------------------------- 120.7/120.7 kB 1.0 MB/s eta 0:00:00
Collecting importlib-metadata
  Downloading importlib_metadata-4.12.0-py3-none-any.whl (21 kB)
Requirement already satisfied: numpy in c:\usr\python37\lib\site-packages (from diffusers) (1.17.4)
Collecting Pillow
  Downloading Pillow-9.2.0-cp37-cp37m-win_amd64.whl (3.3 MB)
     ---------------------------------------- 3.3/3.3 MB 743.8 kB/s eta 0:00:00
Collecting tqdm>=4.27
  Downloading tqdm-4.64.0-py2.py3-none-any.whl (78 kB)
     ---------------------------------------- 78.4/78.4 kB 483.7 kB/s eta 0:00:00
Collecting tokenizers!=0.11.3,<0.13,>=0.11.1
  Downloading tokenizers-0.12.1-cp37-cp37m-win_amd64.whl (3.3 MB)
     ---------------------------------------- 3.3/3.3 MB 675.6 kB/s eta 0:00:00
Collecting pyyaml>=5.1
  Downloading PyYAML-6.0-cp37-cp37m-win_amd64.whl (153 kB)
     ---------------------------------------- 153.2/153.2 kB 830.9 kB/s eta 0:00:00
Collecting packaging>=20.0
  Downloading packaging-21.3-py3-none-any.whl (40 kB)
     ---------------------------------------- 40.8/40.8 kB 984.2 kB/s eta 0:00:00
Collecting typing-extensions>=3.7.4.3
  Using cached typing_extensions-4.3.0-py3-none-any.whl (25 kB)
Requirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in c:\usr\python37\lib\site-packages (from packaging>=20.0->transformers) (2.4.0)
Collecting colorama
  Downloading colorama-0.4.5-py2.py3-none-any.whl (16 kB)
Collecting zipp>=0.5
  Downloading zipp-3.8.1-py3-none-any.whl (5.6 kB)
Requirement already satisfied: charset-normalizer~=2.0.0 in c:\usr\python37\lib\site-packages (from requests->diffusers) (2.0.12)
Requirement already satisfied: idna<4,>=2.5 in c:\usr\python37\lib\site-packages (from requests->diffusers) (3.3)
Requirement already satisfied: certifi>=2017.4.17 in c:\usr\python37\lib\site-packages (from requests->diffusers) (2021.10.8)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in c:\usr\python37\lib\site-packages (from requests->diffusers) (1.26.9)
Installing collected packages: tokenizers, zipp, typing-extensions, scipy, regex, pyyaml, Pillow, packaging, filelock, colorama, tqdm, torch, importlib-metadata, huggingface-hub, transformers, diffusers
Successfully installed Pillow-9.2.0 colorama-0.4.5 diffusers-0.2.4 filelock-3.8.0 huggingface-hub-0.9.1 importlib-metadata-4.12.0 packaging-21.3 pyyaml-6.0 regex-2022.8.17 scipy-1.7.3 tokenizers-0.12.1 torch-1.12.1 tqdm-4.64.0 transformers-4.21.2 typing-extensions-4.3.0 zipp-3.8.1

[notice] A new release of pip available: 22.1.2 -> 22.2.2
[notice] To update, run: python.exe -m pip install --upgrade pip

おまけ pip のアップグレード

>python.exe -m pip install --upgrade pip
Requirement already satisfied: pip in c:\usr\python37\lib\site-packages (22.1.2)
Collecting pip
  Downloading pip-22.2.2-py3-none-any.whl (2.0 MB)
     ---------------------------------------- 2.0/2.0 MB 1.2 MB/s eta 0:00:00
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 22.1.2
    Uninstalling pip-22.1.2:
      Successfully uninstalled pip-22.1.2
Successfully installed pip-22.2.2

huggingface-cli login

>huggingface-cli login

        _|    _|  _|    _|    _|_|_|    _|_|_|  _|_|_|  _|      _|    _|_|_|      _|_|_|_|    _|_|      _|_|_|  _|_|_|_|
        _|    _|  _|    _|  _|        _|          _|    _|_|    _|  _|            _|        _|    _|  _|        _|
        _|_|_|_|  _|    _|  _|  _|_|  _|  _|_|    _|    _|  _|  _|  _|  _|_|      _|_|_|    _|_|_|_|  _|        _|_|_|
        _|    _|  _|    _|  _|    _|  _|    _|    _|    _|    _|_|  _|    _|      _|        _|    _|  _|        _|
        _|    _|    _|_|      _|_|_|    _|_|_|  _|_|_|  _|      _|    _|_|_|      _|        _|    _|    _|_|_|  _|_|_|_|

        To login, `huggingface_hub` now requires a token generated from https://huggingface.co/settings/tokens .

Token:
Login successful
Your token has been saved to D:\home\wurly/.huggingface/token
Authenticated through git-credential store but this isn't the helper defined on your machine.
You might have to re-authenticate when pushing to the Hugging Face Hub. Run the following command in your terminal in case you want to set this credential helper as the default

git config --global credential.helper store

git config --global credential.helper store

言われた通り実行

>git config --global credential.helper store

huggingface-cli login (再度実行)

ログインできた

>huggingface-cli login

        _|    _|  _|    _|    _|_|_|    _|_|_|  _|_|_|  _|      _|    _|_|_|      _|_|_|_|    _|_|      _|_|_|  _|_|_|_|
        _|    _|  _|    _|  _|        _|          _|    _|_|    _|  _|            _|        _|    _|  _|        _|
        _|_|_|_|  _|    _|  _|  _|_|  _|  _|_|    _|    _|  _|  _|  _|  _|_|      _|_|_|    _|_|_|_|  _|        _|_|_|
        _|    _|  _|    _|  _|    _|  _|    _|    _|    _|    _|_|  _|    _|      _|        _|    _|  _|        _|
        _|    _|    _|_|      _|_|_|    _|_|_|  _|_|_|  _|      _|    _|_|_|      _|        _|    _|    _|_|_|  _|_|_|_|

        To login, `huggingface_hub` now requires a token generated from https://huggingface.co/settings/tokens .

Token:
Login successful
Your token has been saved to D:\home\wurly/.huggingface/token

スクリプトを実行してみる

OSError が発生する。エラーメッセージを見ると、Developers Modeを有効にするか、管理者権限で実行することが必要とのこと。

>python test.py
Downloading: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████| 1.34k/1.34k [00:00<00:00, 669kB/s]
Traceback (most recent call last):
  File "C:\usr\Python37\lib\site-packages\huggingface_hub\file_download.py", line 837, in _create_relative_symlink
    os.symlink(relative_src, dst)
OSError: symbolic link privilege not held

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 9, in <module>
    pipe = StableDiffusionPipeline.from_pretrained(model_id, use_auth_token=True)
  File "C:\usr\Python37\lib\site-packages\diffusers\pipeline_utils.py", line 161, in from_pretrained
    revision=revision,
  File "C:\usr\Python37\lib\site-packages\huggingface_hub\utils\_deprecation.py", line 93, in inner_f
    return f(*args, **kwargs)
  File "C:\usr\Python37\lib\site-packages\huggingface_hub\_snapshot_download.py", line 204, in snapshot_download
    use_auth_token=use_auth_token,
  File "C:\usr\Python37\lib\site-packages\huggingface_hub\file_download.py", line 1274, in hf_hub_download
    _create_relative_symlink(blob_path, pointer_path)
  File "C:\usr\Python37\lib\site-packages\huggingface_hub\file_download.py", line 842, in _create_relative_symlink
    "Windows requires Developer Mode to be activated, or to run Python as "
OSError: Windows requires Developer Mode to be activated, or to run Python as an administrator, in order to create symlinks.
In order to activate Developer Mode, see this article: https://docs.microsoft.com/en-us/windows/apps/get-started/enable-your-device-for-development

管理者権限で実行する

管理者権限で実行する

test.py は "AssertionError: Torch not compiled with CUDA enabled" と言われる。

>python test.py
Downloading: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 14.3k/14.3k [00:00<00:00, 78.8kB/s]
Downloading: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 342/342 [00:00<00:00, 342kB/s]
Downloading: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 543/543 [00:00<00:00, 545kB/s]
Downloading: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 4.56k/4.56k [00:00<00:00, 4.58MB/s]
Downloading: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1.22G/1.22G [06:31<00:00, 3.11MB/s]
Downloading: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 209/209 [00:00<00:00, 104kB/s]
Downloading: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 592/592 [00:00<00:00, 592kB/s]
Downloading: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 492M/492M [00:51<00:00, 9.60MB/s]
Downloading: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 525k/525k [00:03<00:00, 149kB/s]
Downloading: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 472/472 [00:00<00:00, 236kB/s]
Downloading: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 806/806 [00:00<00:00, 804kB/s]
Downloading: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1.06M/1.06M [00:07<00:00, 136kB/s]
Downloading: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 743/743 [00:00<00:00, 248kB/s]
Downloading: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 3.44G/3.44G [08:35<00:00, 6.67MB/s]
Downloading: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 71.2k/71.2k [00:00<00:00, 99.0kB/s]
Downloading: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 522/522 [00:00<00:00, 504kB/s]
Downloading: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 335M/335M [00:34<00:00, 9.68MB/s]
ftfy or spacy is not installed using BERT BasicTokenizer instead of ftfy.
Traceback (most recent call last):
  File "test.py", line 10, in <module>
    pipe = pipe.to(device)
  File "C:\usr\Python37\lib\site-packages\diffusers\pipeline_utils.py", line 126, in to
    module.to(torch_device)
  File "C:\usr\Python37\lib\site-packages\torch\nn\modules\module.py", line 927, in to
    return self._apply(convert)
  File "C:\usr\Python37\lib\site-packages\torch\nn\modules\module.py", line 579, in _apply
    module._apply(fn)
  File "C:\usr\Python37\lib\site-packages\torch\nn\modules\module.py", line 579, in _apply
    module._apply(fn)
  File "C:\usr\Python37\lib\site-packages\torch\nn\modules\module.py", line 579, in _apply
    module._apply(fn)
  [Previous line repeated 1 more time]
  File "C:\usr\Python37\lib\site-packages\torch\nn\modules\module.py", line 602, in _apply
    param_applied = fn(param)
  File "C:\usr\Python37\lib\site-packages\torch\nn\modules\module.py", line 925, in convert
    return t.to(device, dtype if t.is_floating_point() or t.is_complex() else None, non_blocking)
  File "C:\usr\Python37\lib\site-packages\torch\cuda\__init__.py", line 211, in _lazy_init
    raise AssertionError("Torch not compiled with CUDA enabled")
AssertionError: Torch not compiled with CUDA enabled

別のスクリプトで試してみる

test2.py でも同じく "AssertionError: Torch not compiled with CUDA enabled" と言われる。

>python test2.py
Downloading: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 12.5k/12.5k [00:00<00:00, 12.5MB/s]
Downloading: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 543/543 [00:00<00:00, 544kB/s]
Downloading: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 4.63k/4.63k [00:00<00:00, 4.89MB/s]
Downloading: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 608M/608M [01:01<00:00, 9.87MB/s]
Downloading: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 209/209 [00:00<00:00, 211kB/s]
Downloading: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 572/572 [00:00<00:00, 572kB/s]
Downloading: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 246M/246M [00:35<00:00, 6.88MB/s]
Downloading: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 788/788 [00:00<00:00, 752kB/s]
Downloading: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 772/772 [00:00<00:00, 776kB/s]
Downloading: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1.72G/1.72G [02:55<00:00, 9.82MB/s]
Downloading: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 550/550 [00:00<00:00, 550kB/s]
Downloading: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 167M/167M [00:19<00:00, 8.68MB/s]
ftfy or spacy is not installed using BERT BasicTokenizer instead of ftfy.
Traceback (most recent call last):
  File "test2.py", line 21, in <module>
    pipe = pipe.to("cuda")
  File "C:\usr\Python37\lib\site-packages\diffusers\pipeline_utils.py", line 126, in to
    module.to(torch_device)
  File "C:\usr\Python37\lib\site-packages\torch\nn\modules\module.py", line 927, in to
    return self._apply(convert)
  File "C:\usr\Python37\lib\site-packages\torch\nn\modules\module.py", line 579, in _apply
    module._apply(fn)
  File "C:\usr\Python37\lib\site-packages\torch\nn\modules\module.py", line 579, in _apply
    module._apply(fn)
  File "C:\usr\Python37\lib\site-packages\torch\nn\modules\module.py", line 579, in _apply
    module._apply(fn)
  [Previous line repeated 1 more time]
  File "C:\usr\Python37\lib\site-packages\torch\nn\modules\module.py", line 602, in _apply
    param_applied = fn(param)
  File "C:\usr\Python37\lib\site-packages\torch\nn\modules\module.py", line 925, in convert
    return t.to(device, dtype if t.is_floating_point() or t.is_complex() else None, non_blocking)
  File "C:\usr\Python37\lib\site-packages\torch\cuda\__init__.py", line 211, in _lazy_init
    raise AssertionError("Torch not compiled with CUDA enabled")
AssertionError: Torch not compiled with CUDA enabled

torch を cuda付きで再インストール

torch を cuda付きでインストールし直す。
(一般ユーザ権限で実行する)

>pip install torch==1.11.0+cu113 torchvision==0.12.0+cu113 torchaudio==0.11.0 --extra-index-url https://download.pytorch.org/whl/cu113
Looking in indexes: https://pypi.org/simple, https://download.pytorch.org/whl/cu113
Collecting torch==1.11.0+cu113
  Downloading https://download.pytorch.org/whl/cu113/torch-1.11.0%2Bcu113-cp37-cp37m-win_amd64.whl (2186.0 MB)
     ---------------------------------------- 2.2/2.2 GB 1.4 MB/s eta 0:00:00
Collecting torchvision==0.12.0+cu113
  Downloading https://download.pytorch.org/whl/cu113/torchvision-0.12.0%2Bcu113-cp37-cp37m-win_amd64.whl (5.4 MB)
     ---------------------------------------- 5.4/5.4 MB 7.8 MB/s eta 0:00:00
Collecting torchaudio==0.11.0
  Downloading https://download.pytorch.org/whl/cu113/torchaudio-0.11.0%2Bcu113-cp37-cp37m-win_amd64.whl (573 kB)
     ---------------------------------------- 573.4/573.4 kB 8.9 MB/s eta 0:00:00
Requirement already satisfied: typing-extensions in c:\usr\python37\lib\site-packages (from torch==1.11.0+cu113) (4.3.0)
Requirement already satisfied: pillow!=8.3.*,>=5.3.0 in c:\usr\python37\lib\site-packages (from torchvision==0.12.0+cu113) (9.2.0)
Requirement already satisfied: requests in c:\usr\python37\lib\site-packages (from torchvision==0.12.0+cu113) (2.27.1)
Requirement already satisfied: numpy in c:\usr\python37\lib\site-packages (from torchvision==0.12.0+cu113) (1.17.4)
Requirement already satisfied: idna<4,>=2.5 in c:\usr\python37\lib\site-packages (from requests->torchvision==0.12.0+cu113) (3.3)
Requirement already satisfied: charset-normalizer~=2.0.0 in c:\usr\python37\lib\site-packages (from requests->torchvision==0.12.0+cu113) (2.0.12)
Requirement already satisfied: certifi>=2017.4.17 in c:\usr\python37\lib\site-packages (from requests->torchvision==0.12.0+cu113) (2021.10.8)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in c:\usr\python37\lib\site-packages (from requests->torchvision==0.12.0+cu113) (1.26.9)
Installing collected packages: torch, torchvision, torchaudio
  Attempting uninstall: torch
    Found existing installation: torch 1.12.1
    Uninstalling torch-1.12.1:
      Successfully uninstalled torch-1.12.1
Successfully installed torch-1.11.0+cu113 torchaudio-0.11.0+cu113 torchvision-0.12.0+cu113

スクリプトの再実行

スクリプトを再度実行する。

test.py、test1.py の場合には管理者権限での実行が必要だが、
いずれにしても"CUDA out of memory" になる。

>python test.py
ftfy or spacy is not installed using BERT BasicTokenizer instead of ftfy.
Traceback (most recent call last):
  File "test.py", line 14, in <module>
    image = pipe(prompt, guidance_scale=7.5)["sample"][0]
  File "C:\usr\Python37\lib\site-packages\torch\autograd\grad_mode.py", line 27, in decorate_context
    return func(*args, **kwargs)
  File "C:\usr\Python37\lib\site-packages\diffusers\pipelines\stable_diffusion\pipeline_stable_diffusion.py", line 82, in __call__
    text_embeddings = self.text_encoder(text_input.input_ids.to(self.device))[0]
  File "C:\usr\Python37\lib\site-packages\torch\nn\modules\module.py", line 1110, in _call_impl
    return forward_call(*input, **kwargs)
  File "C:\usr\Python37\lib\site-packages\transformers\models\clip\modeling_clip.py", line 734, in forward
    return_dict=return_dict,
  File "C:\usr\Python37\lib\site-packages\torch\nn\modules\module.py", line 1110, in _call_impl
    return forward_call(*input, **kwargs)
  File "C:\usr\Python37\lib\site-packages\transformers\models\clip\modeling_clip.py", line 655, in forward
    return_dict=return_dict,
  File "C:\usr\Python37\lib\site-packages\torch\nn\modules\module.py", line 1110, in _call_impl
    return forward_call(*input, **kwargs)
  File "C:\usr\Python37\lib\site-packages\transformers\models\clip\modeling_clip.py", line 582, in forward
    output_attentions=output_attentions,
  File "C:\usr\Python37\lib\site-packages\torch\nn\modules\module.py", line 1110, in _call_impl
    return forward_call(*input, **kwargs)
  File "C:\usr\Python37\lib\site-packages\transformers\models\clip\modeling_clip.py", line 331, in forward
    hidden_states = self.mlp(hidden_states)
  File "C:\usr\Python37\lib\site-packages\torch\nn\modules\module.py", line 1110, in _call_impl
    return forward_call(*input, **kwargs)
  File "C:\usr\Python37\lib\site-packages\transformers\models\clip\modeling_clip.py", line 288, in forward
    hidden_states = self.fc2(hidden_states)
  File "C:\usr\Python37\lib\site-packages\torch\nn\modules\module.py", line 1110, in _call_impl
    return forward_call(*input, **kwargs)
  File "C:\usr\Python37\lib\site-packages\torch\nn\modules\linear.py", line 103, in forward
    return F.linear(input, self.weight, self.bias)
RuntimeError: CUDA out of memory. Tried to allocate 20.00 MiB (GPU 0; 6.00 GiB total capacity; 5.30 GiB already allocated; 0 bytes free; 5.30 GiB reserved in total by PyTorch) If reserved memory is >> allocated memory try setting max_split_size_mb to avoid fragmentation.  See documentation for Memory Management and PYTORCH_CUDA_ALLOC_CONF

test2.py を実行してみる。

こちらは管理者権限での実行は不要。

>python test2.py
ftfy or spacy is not installed using BERT BasicTokenizer instead of ftfy.
51it [01:07,  1.32s/it]

画像が生成された!

確かに実行中、タスクマネージャーで GPUの使用量はMaxになっていたのでローカルでGPUを使用して生成されたようだ。

その他のリンク

Discussion