From b313a4867c4f20d0049185c1a5ff0e191b3b026b Mon Sep 17 00:00:00 2001 From: LeoBorcherding Date: Fri, 15 May 2026 14:28:03 -0500 Subject: [PATCH] fix: patch torch.distributed stubs in server process for Windows ROCm MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On Windows ROCm, torch.distributed ships without process-group helpers (is_initialized, is_available, get_rank, get_world_size). The worker subprocess already patches these in section 1e, but the main server process calls _determine_attention_impl_for_gpu_estimate() which calls unsloth's resolve_attention_implementation() → is_initialized(), causing: "Could not resolve attention implementation for '...': module 'torch.distributed' has no attribute 'is_initialized'" Fix: patch the missing attrs onto torch.distributed at the top of _determine_attention_impl_for_gpu_estimate, matching the same stubs already applied in worker.py section 1e. No-ops on Linux/CUDA where torch.distributed is fully populated. --- studio/backend/utils/hardware/hardware.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/studio/backend/utils/hardware/hardware.py b/studio/backend/utils/hardware/hardware.py index 747ff194ab..f75c7a9d12 100644 --- a/studio/backend/utils/hardware/hardware.py +++ b/studio/backend/utils/hardware/hardware.py @@ -946,6 +946,24 @@ def _load_config_for_gpu_estimate(model_name: str, hf_token: Optional[str] = Non def _determine_attention_impl_for_gpu_estimate(config) -> str: import copy as _copy + # torch.distributed is incomplete on Windows ROCm — it ships without the + # process-group helpers (is_initialized, is_available, etc.). + # resolve_attention_implementation (unsloth) calls is_initialized() + # unconditionally, so patch any missing attrs before importing it. + try: + import torch.distributed as _td + + for _attr, _stub in ( + ("is_initialized", lambda: False), + ("is_available", lambda: False), + ("get_rank", lambda: 0), + ("get_world_size", lambda: 1), + ): + if not hasattr(_td, _attr): + setattr(_td, _attr, _stub) + except ImportError: + pass + from unsloth.models._utils import resolve_attention_implementation from transformers import AutoModel, AutoModelForCausalLM