[Refactor] Replace and ban a few torch.cuda functions in favor of torch.accelerator replacements. (#3365)

Signed-off-by: Nick Cao <ncao@redhat.com>
This commit is contained in:
Nick Cao authored and GitHub committed 2026-05-07 23:23:59 +08:00
1 parent 54277a8dd0
commit e6466cf0e6
40 files changed
+98 -90

No files matched your search

+1 -1
View File
@@ -230,7 +230,7 @@ def _build_accuracy_server_config(
run_level: str,
model_prefix: str,
) -> AccuracyServerConfig:
if torch.cuda.device_count() < 1:
if torch.accelerator.device_count() < 1:
pytest.skip("Need at least 1 CUDA GPU for accuracy benchmark smoke tests.")
if not generate_model:
@@ -160,7 +160,7 @@ def _run_diffusers_wan22_i2v(*, model: str, output_path: Path, conditioning_imag
del pipe
gc.collect()
if torch.cuda.is_available():
torch.cuda.empty_cache()
torch.accelerator.empty_cache()
run_post_test_cleanup(enable_force=True)
@@ -240,7 +240,7 @@ def _run_diffusers_qwen_image(*, model: str, output_path: Path) -> tuple[Image.I
del pipe
gc.collect()
if torch.cuda.is_available():
torch.cuda.empty_cache()
torch.accelerator.empty_cache()
run_post_test_cleanup(enable_force=True)
@@ -199,7 +199,7 @@ def _run_diffusers_baseline(model: str, output_dir: Path) -> list[Image.Image]:
del pipe
gc.collect()
if torch.cuda.is_available():
torch.cuda.empty_cache()
torch.accelerator.empty_cache()
run_post_test_cleanup(enable_force=True)
@@ -278,7 +278,7 @@ def _run_with_custom_transformer(model: str, output_dir: Path) -> list[Image.Ima
del pipe, result, our_transformer
gc.collect()
if torch.cuda.is_available():
torch.cuda.empty_cache()
torch.accelerator.empty_cache()
return frames
+1 -1
View File
@@ -98,7 +98,7 @@ def _run_diffusers_qwen_image(*, model: str, output_path: Path) -> Image.Image:
del pipe
gc.collect()
if torch.cuda.is_available():
torch.cuda.empty_cache()
torch.accelerator.empty_cache()
run_post_test_cleanup(enable_force=True)
+1 -1
View File
@@ -109,7 +109,7 @@ def _run_diffusers_image_edit(
del pipe
gc.collect()
if torch.cuda.is_available():
torch.cuda.empty_cache()
torch.accelerator.empty_cache()
run_post_test_cleanup(enable_force=True)
@@ -125,7 +125,7 @@ def _run_diffusers_qwen_image_layered(*, model: str, input_image: Image.Image, o
del pipe
gc.collect()
if torch.cuda.is_available():
torch.cuda.empty_cache()
torch.accelerator.empty_cache()
run_post_test_cleanup(enable_force=True)
@@ -572,7 +572,7 @@ def test_wan22_i2v_online_serving_generates_video(
wan22_i2v_image_source: str | None,
wan22_i2v_online_timeout_seconds: int,
) -> None:
if not torch.cuda.is_available() or torch.cuda.device_count() < 2:
if not torch.cuda.is_available() or torch.accelerator.device_count() < 2:
pytest.skip("Wan2.2 I2V similarity e2e test requires >= 2 CUDA GPUs.")
_probe_binary("ffprobe")
@@ -597,7 +597,7 @@ def test_wan22_i2v_online_serving_generates_video(
def test_wan22_i2v_serving_matches_diffusers_video_similarity(
wan22_i2v_image_source: str | None,
) -> None:
if not torch.cuda.is_available() or torch.cuda.device_count() < 2:
if not torch.cuda.is_available() or torch.accelerator.device_count() < 2:
pytest.skip("Wan2.2 I2V similarity e2e test requires >= 2 CUDA GPUs.")
_probe_binary("ffmpeg")
@@ -327,7 +327,7 @@ def _generate_image(omni: Omni, use_system_prompt: str | None) -> Image.Image:
return _extract_generated_image(outputs)
@pytest.mark.skipif(torch.cuda.device_count() < 8, reason="Need at least 8 CUDA GPUs for this test.")
@pytest.mark.skipif(torch.accelerator.device_count() < 8, reason="Need at least 8 CUDA GPUs for this test.")
@pytest.mark.parametrize("system_prompt_name,use_system_prompt,expected_embedding", SYSTEM_PROMPT_CASES)
def test_system_prompt_scores(
omni: Omni,
@@ -26,7 +26,7 @@ def get_ack_info(ack, key, default=None):
def get_dynamic_devices(stage_idx, num_stages, tp_size):
total_gpus = torch.cuda.device_count()
total_gpus = torch.accelerator.device_count()
gpus_per_stage = tp_size
start_idx = stage_idx * gpus_per_stage
if start_idx + gpus_per_stage > total_gpus:
@@ -42,7 +42,7 @@ def get_dynamic_devices(stage_idx, num_stages, tp_size):
@hardware_test(res={"cuda": "H100", "rocm": "MI325"}, num_cards=1)
@pytest.mark.asyncio
async def test_diffusion_model_sleep_tp(tp_size):
num_gpus = torch.cuda.device_count()
num_gpus = torch.accelerator.device_count()
if num_gpus < tp_size:
pytest.skip(f"Skipping TP={tp_size}")
@@ -88,7 +88,7 @@ async def test_diffusion_model_sleep_tp(tp_size):
@hardware_test(res={"cuda": "H100", "rocm": "MI325"}, num_cards=2)
@pytest.mark.asyncio
async def test_multistage_sleep_h100(tp_size):
num_gpus = torch.cuda.device_count()
num_gpus = torch.accelerator.device_count()
if num_gpus < tp_size * 2:
pytest.skip("Not enough GPUs")
@@ -67,7 +67,7 @@ def _generate_single_stage_image(
omni_kwargs["quantization"] = quantization
with OmniRunner(model, **omni_kwargs) as runner:
torch.cuda.reset_peak_memory_stats()
torch.accelerator.reset_peak_memory_stats()
generator = torch.Generator(device=current_omni_platform.device_type).manual_seed(seed)
outputs = runner.omni.generate(
@@ -81,7 +81,7 @@ def _generate_single_stage_image(
),
)
peak_mem = torch.cuda.max_memory_allocated() / (1024**3)
peak_mem = torch.accelerator.max_memory_allocated() / (1024**3)
first_output = outputs[0]
assert first_output.final_output_type == "image"
@@ -116,7 +116,7 @@ def _generate_bagel_image(
model_name = omni_kwargs.pop("model")
with OmniRunner(model_name, **omni_kwargs) as runner:
omni = runner.omni
torch.cuda.reset_peak_memory_stats()
torch.accelerator.reset_peak_memory_stats()
params_list = omni.default_sampling_params_list
if len(params_list) > 1:
@@ -134,7 +134,7 @@ def _generate_bagel_image(
)
)
peak_mem = torch.cuda.max_memory_allocated() / (1024**3)
peak_mem = torch.accelerator.max_memory_allocated() / (1024**3)
# Extract image
generated_image = None
@@ -190,7 +190,7 @@ def test_single_stage_zimage_fp8_uses_less_memory():
model="Tongyi-MAI/Z-Image-Turbo",
quantization=None,
)
torch.cuda.empty_cache()
torch.accelerator.empty_cache()
_, mem_fp8 = _generate_single_stage_image(
model="Tongyi-MAI/Z-Image-Turbo",
@@ -241,7 +241,7 @@ def test_single_stage_flux_fp8_uses_less_memory():
width=512,
num_inference_steps=4,
)
torch.cuda.empty_cache()
torch.accelerator.empty_cache()
_, mem_fp8 = _generate_single_stage_image(
model="black-forest-labs/FLUX.1-dev",