Skip to content

Commit dbad41c

Browse files
kiya00t-viBorda
authored
Diffuser test (#2141)
Co-authored-by: Thomas Viehmann <tv.code@beamnet.de> Co-authored-by: Jirka Borovec <6035284+Borda@users.noreply.github.com>
1 parent 76c8cea commit dbad41c

7 files changed

Lines changed: 173 additions & 44 deletions

File tree

.azure/gpu-coverage.yml

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
trigger:
2+
tags:
3+
include: ["*"]
4+
paths:
5+
include:
6+
- ".azure/gpu-coverage.yml"
7+
- "requirements/coverage.txt"
8+
- "thunder/tests/coverage/**"
9+
branches:
10+
include:
11+
- "main"
12+
- "release/*"
13+
- "refs/tags/*"
14+
15+
pr:
16+
branches:
17+
include: ["*"]
18+
19+
jobs:
20+
- job: coverage
21+
strategy:
22+
matrix:
23+
"w/ torch 2.7.1":
24+
docker-image: "ubuntu24.04-cuda12.6.3-cudnn-fe1.10.0-py3.10-pt_2.7.1-dev"
25+
# how much time to give 'run always even if cancelled tasks' before stopping them
26+
cancelTimeoutInMinutes: "2"
27+
pool: "lit-rtx-3090"
28+
variables:
29+
DEVICES: $( python -c 'name = "$(Agent.Name)" ; gpus = name.split("_")[-1] if "_" in name else "0"; print(gpus)' )
30+
TORCH_HOME: "/var/tmp/torch"
31+
PIP_CACHE_DIR: "/var/tmp/pip"
32+
PYTHONHASHSEED: "0"
33+
NCCL_DEBUG: "INFO"
34+
ALLOW_COVERAGE_TRACE: "1"
35+
container:
36+
image: "pytorchlightning/lightning-thunder:$(docker-image)"
37+
options: "--gpus=all --shm-size=16g -v /var/tmp:/var/tmp"
38+
workspace:
39+
clean: all
40+
steps:
41+
- bash: |
42+
echo $(DEVICES)
43+
lspci | egrep 'VGA|3D'
44+
dpkg-query -W -f='${Package} ${Version}\n' libnccl2 libnccl-dev
45+
whereis nvidia
46+
nvidia-smi
47+
which python && which pip
48+
python --version
49+
pip --version
50+
pip list
51+
echo "##vso[task.setvariable variable=CUDA_VISIBLE_DEVICES]$(DEVICES)"
52+
displayName: "Image info & NVIDIA"
53+
54+
- bash: |
55+
set -ex
56+
# drop pt from requirements so not to interfere with the existing one
57+
bash scripts/remove-torch-lines.sh requirements/base.txt
58+
cat requirements/base.txt
59+
60+
# double check on test requirements
61+
pip install -U -r requirements/base.txt -r requirements/coverage.txt
62+
63+
# https://docs.codecov.com/docs/codecov-uploader
64+
curl -Os https://uploader.codecov.io/latest/linux/codecov
65+
chmod +x codecov
66+
67+
# install this package
68+
python setup.py develop
69+
displayName: "Install package & ..."
70+
71+
- bash: bash scripts/sanity-check.sh
72+
displayName: "Sanity check / details"
73+
74+
- bash: |
75+
PYTHONPATH=$(pwd)/thunder/tests pytest thunder/tests/coverage_tests
76+
timeoutInMinutes: "45"
77+
displayName: "Testing: coverage_tests"

.github/workflows/coverage_test.yml

Lines changed: 0 additions & 43 deletions
This file was deleted.

requirements/coverage.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
coverage ~=7.9.1
2+
pytest ==8.3.5
3+
pytest-cov ==6.2.1
4+
pytest-benchmark ==5.1.0
5+
transformers ==4.52.4
6+
lightning_sdk
7+
diffusers==0.34.0
8+
accelerate
9+
bitsandbytes==0.46.1

requirements/test.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ jsonargparse # thunder/benchmarks/benchmark_litgpt.py
2020
bitsandbytes==0.46.1; 'arm' not in platform_machine and 'aarch' not in platform_machine
2121
bitsandbytes>=0.42,<0.43; 'arm' in platform_machine or 'aarch' in platform_machine
2222
transformers==4.52.4 # for test_networks.py
23+
diffusers==0.34.0 # for test_networks.py
24+
accelerate # for test_networks.py
2325

2426
# Installs JAX on Linux and MacOS
2527
jaxlib; sys_platform == 'linux' or sys_platform == 'darwin' # required for jax, see https://github.com/google/jax#installation

thunder/tests/coverage_tests/__init__.py

Whitespace-only changes.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import torch
2+
import os
3+
import pytest
4+
5+
6+
if os.getenv("ALLOW_COVERAGE_TRACE") != "1":
7+
pytest.skip("Skipping test_coverage_hf_diffusers.py in regular CI", allow_module_level=True)
8+
9+
hf_diffusers_unet2d_condition_model_ids = [
10+
"runwayml/stable-diffusion-v1-5",
11+
"CompVis/stable-diffusion-v1-4",
12+
"ionet-official/bc8-alpha",
13+
"stabilityai/sd-turbo",
14+
"runwayml/stable-diffusion-inpainting",
15+
"stabilityai/stable-diffusion-xl-base-1.0",
16+
"stabilityai/stable-diffusion-xl-refiner-1.0",
17+
"diffusers/stable-diffusion-xl-1.0-inpainting-0.1",
18+
]
19+
20+
from thunder.tests.framework import requiresCUDA
21+
22+
23+
@requiresCUDA
24+
@pytest.mark.parametrize("model_id", hf_diffusers_unet2d_condition_model_ids)
25+
def test_hf_diffusers(model_id):
26+
from thunder.dynamo import thunderfx
27+
from diffusers import UNet2DConditionModel
28+
29+
unet_config = UNet2DConditionModel.load_config(model_id, subfolder="unet", torch_dtype=torch.bfloat16)
30+
unet = UNet2DConditionModel(unet_config)
31+
in_channels = unet.config.in_channels
32+
cross_attention_dim = unet.config.cross_attention_dim
33+
addition_embed_type = unet.config.addition_embed_type
34+
35+
sample_size = 4
36+
batch_size = 1
37+
seq_length = 4
38+
39+
if "xl" in model_id:
40+
time_ids_dim = 6
41+
text_embeds_dim = 4
42+
if "refiner" in model_id:
43+
time_ids_dim = 2
44+
text_embeds_dim = 4
45+
else:
46+
time_ids_dim = None
47+
text_embeds_dim = None
48+
49+
input_shape = (batch_size, in_channels, sample_size, sample_size)
50+
hidden_states_shape = (batch_size, seq_length, cross_attention_dim)
51+
52+
unet = unet.to("cuda", dtype=torch.bfloat16).requires_grad_(True)
53+
compiled_model = thunderfx(unet)
54+
55+
def make_inputs(dtype=torch.bfloat16):
56+
added_cond_kwargs = {}
57+
with torch.device("cuda"):
58+
input = torch.randn(input_shape, dtype=dtype)
59+
hidden_states = torch.randn(hidden_states_shape, dtype=dtype)
60+
timestep = torch.ones(batch_size, dtype=torch.long)
61+
if addition_embed_type is not None:
62+
assert text_embeds_dim is not None and time_ids_dim is not None
63+
time_ids_shape = (batch_size, time_ids_dim)
64+
text_embeds_shape = (batch_size, text_embeds_dim)
65+
added_cond_kwargs["time_ids"] = torch.randn(time_ids_shape, device="cuda", dtype=dtype)
66+
added_cond_kwargs["text_embeds"] = torch.randn(text_embeds_shape, device="cuda", dtype=dtype)
67+
return (input, timestep, hidden_states), {"added_cond_kwargs": added_cond_kwargs}
68+
69+
compiled_args, compiled_kwargs = make_inputs(torch.bfloat16)
70+
compiled_output = compiled_model(*compiled_args, **compiled_kwargs)
71+
72+
ref_output = unet(*compiled_args, **compiled_kwargs)
73+
74+
ref_output = ref_output.sample
75+
compiled_output = compiled_output.sample
76+
77+
torch.testing.assert_close(compiled_output, ref_output, rtol=1e-2, atol=2e-1)
78+
79+
# TODO: Currently fails, needs investigation https://github.com/Lightning-AI/lightning-thunder/issues/2153
80+
# loss_grad = torch.randn_like(compiled_output)
81+
# grads_ref = torch.autograd.grad(ref_output, unet.parameters(), grad_outputs=loss_grad)
82+
# grads_compiled = torch.autograd.grad(compiled_output, unet.parameters(), grad_outputs=loss_grad)
83+
# torch.testing.assert_close(grads_ref, grads_compiled, rtol=1e-1, atol=1e-1)

thunder/tests/test_coverage_trace.py renamed to thunder/tests/coverage_tests/test_coverage_trace.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
AutoModelForSeq2SeqLM,
1818
AutoModelForImageClassification,
1919
)
20-
from test_core import run_prologue
20+
from thunder.tests.test_core import run_prologue
2121

2222

2323
MODEL_LIST = [
@@ -59,6 +59,7 @@ def get_dummy_input(model_name, config):
5959
return {"input_ids": torch.randint(0, 1000, (1, 16), device="cpu")}
6060

6161

62+
@pytest.mark.skip(reason="https://github.com/Lightning-AI/lightning-thunder/issues/2436")
6263
@pytest.mark.parametrize("model_name", MODEL_LIST)
6364
def test_model_trace(model_name):
6465
print(f"\n=== Testing {model_name} ===")

0 commit comments

Comments
 (0)