見出し画像

xformersからtritonのimport要求条件を削除する&各種更新


xformersからtritonのimport要求条件を削除する

LoRAを作成するKohya's GUIには、オプションとしてtritonのインストールオプション(※Danger!という警告付きで)がありますが、

なぜ、そんなモノがあるかと言えば、xformersの構造的宿痾というべきもので、以下の記事でも既に解説しています。

では、何故Kohya's GUIの作者殿は「これ」を危険物扱いにしているかと言えば、これが正式なライブラリーではなく個人の有志が開発した私製ライブラリーであること、更には致命的なエラーの原因にすらなりうるからです。

で、実際に最新のxformers 0.0.28.post2では遂に「この手」が利かなくなり、A1111やLama-Cleaner等に対して致命的なエラーを引き起こすようになってしまいました。
(※致命的と言っても、tritonを削除すれば直る程度の話ですが)

しかし、tritonを削除するとA1111では出ないのですが、Lama-Cleaner等では「tritonがインストールされていません」的なエラー表示が出て、正直不快なので、何とかこれを消せないか…と考えました。

そもそも、tritonとはLinux上でxformersを使用する時に必要なライブラリーですが(※だからGoogle Colab等では勝手にインストールされる)、Windowsではそもそも必要ないのです。
(※必要があるならとっくに開発されている筈で、Windows用tritonが存在しないのは、不要だからと解釈する以外に合理的な理由がない)

※2025年1月、本当に今度こそtritonが必要になる状況が生じました。以下で解説するWaveSpeedがそれです。

このエラー…というか警告が出る理由は、xformersを構成するファイル内の「_init_.py」に原因があり、以下がその原因になっています。

@compute_once
def _is_triton_available():
    if os.environ.get("XFORMERS_ENABLE_TRITON", "0") == "1":
        return True
    if not torch.cuda.is_available():
        return False
    if os.environ.get("XFORMERS_FORCE_DISABLE_TRITON", "0") == "1":
        return False
    # We have many errors on V100 with recent triton versions
    # Let's just drop support for triton kernels below A100
    if torch.cuda.get_device_capability("cuda") < (8, 0):
        return False
    try:
        import triton  # noqa

        return True
    except (ImportError, AttributeError):
        logger.warning(
            "A matching Triton is not available, some optimizations will not be enabled",
            exc_info=True,
        )
        return False

単純に、Windows上で真にtritonが不要であるならば、そもそもtritonのインポートを要求するロジックそのものを殺してしまえばいい、そういう理屈になります。

そこで、毎度おなじみCursor先生に、tritonのインポート要求を削除したバージョンを作成してもらいました。

コードそのものも長くはないので、以下全文掲載します。

# Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
#
# This source code is licensed under the BSD license found in the
# LICENSE file in the root directory of this source tree.

import logging
import os

import torch

from . import _cpp_lib
from .checkpoint import (  # noqa: E402, F401
    checkpoint,
    get_optimal_checkpoint_policy,
    list_operators,
    selective_checkpoint_wrapper,
)

try:
    from .version import __version__  # noqa: F401
except ImportError:
    __version__ = "0.0.0"


logger = logging.getLogger("xformers")

_has_cpp_library: bool = _cpp_lib._cpp_library_load_exception is None

_is_opensource: bool = True


def compute_once(func):
    value = None

    def func_wrapper():
        nonlocal value
        if value is None:
            value = func()
        return value

    return func_wrapper


@compute_once
def _is_triton_available():
    if os.environ.get("XFORMERS_ENABLE_TRITON", "0") == "1":
        return True
    if not torch.cuda.is_available():
        return False
    if os.environ.get("XFORMERS_FORCE_DISABLE_TRITON", "0") == "1":
        return False
    # We have many errors on V100 with recent triton versions
    # Let's just drop support for triton kernels below A100
    if torch.cuda.get_device_capability("cuda") < (8, 0):
        return False
    # Triton import and availability check removed
    return False  # Always return False as Triton is not imported


@compute_once
def get_python_lib():
    return torch.library.Library("xformers_python", "DEF")

# end of file

この結果、下図のようにLama-Cleanerにxformers 0.0.28.post2を適用し、かつtritonはインストールしていない状態でも、「trtitonがインストールされていません」警告文は出ないようになりました。

別な視点としては、以下の記事はPytorch 2.4.1+cu12.4とxformers 0.0.28.post1までにしか通用しないロジック…ということになります。一応、記事自体は残しておきます。

一方で、2024年12月現在、Pytorch2.5.1には対応できる私製tritonとしてVer3.0.0と3.1.0がそれぞれ公開されており、以下記事で解説しています。

各種更新

何気に近日来、ComfyUIの基本仕様がそこそこ更新されているんですよね…そのために却って以前作成したワークフローが使えなくなっていたりしまして以下、現行の仕様に合わせる形で更新しています。


いいなと思ったら応援しよう!