Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt to Ascend NPU #81

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions environment_npu.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: DiT
channels:
- pytorch
dependencies:
- python >= 3.8
- pytorch >= 1.13
- torchvision
- pip:
- timm
- diffusers
- accelerate
- protobuf
- decorator
- scipy
- attrs
3 changes: 3 additions & 0 deletions sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
from download import find_model
from models import DiT_models
import argparse
from utils.device_utils import is_npu_available
if is_npu_available():
from torch_npu.contrib import transfer_to_npu


def main(args):
Expand Down
3 changes: 3 additions & 0 deletions train.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
from models import DiT_models
from diffusion import create_diffusion
from diffusers.models import AutoencoderKL
from utils.device_utils import is_npu_available
if is_npu_available():
from torch_npu.contrib import transfer_to_npu


#################################################################################
Expand Down
17 changes: 17 additions & 0 deletions utils/device_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import torch
import importlib


def is_npu_available():
"Checks if `torch_npu` is installed and potentially if a NPU is in the environment"
if importlib.util.find_spec("torch") is None or importlib.util.find_spec("torch_npu") is None:
return False

import torch_npu

try:
# Will raise a RuntimeError if no NPU is found
_ = torch.npu.device_count()
return torch.npu.is_available()
except RuntimeError:
return False