"""Deterministic contracts for accelerator detection and fallback behavior.""" from __future__ import annotations import sys from types import SimpleNamespace from unittest.mock import Mock import pytest import torch from obliteratus import device @pytest.mark.parametrize( ("cuda", "mps", "expected"), [(True, False, "cuda"), (False, True, "mps"), (False, False, "cpu")], ) def test_auto_device_preference(monkeypatch, cuda, mps, expected): monkeypatch.setattr(device, "is_cuda", lambda: cuda) monkeypatch.setattr(device, "is_mps", lambda: mps) assert device.get_device() == expected assert device.is_gpu_available() is (cuda or mps) def test_explicit_device_validation(monkeypatch): monkeypatch.setattr(device, "is_cuda", lambda: False) monkeypatch.setattr(device, "is_mps", lambda: False) assert device.get_device("cpu") == "cpu" with pytest.raises(RuntimeError, match="CUDA was requested.*device='auto'"): device.get_device("cuda:0") with pytest.raises(RuntimeError, match="MPS was requested.*device='auto'"): device.get_device("mps") with pytest.raises(ValueError, match="Unknown device 'tpu'"): device.get_device("tpu") with pytest.raises(ValueError, match="Unknown device 'cuda:gpu'"): device.get_device("cuda:gpu") monkeypatch.setattr(device, "is_cuda", lambda: True) assert device.get_device("cuda") == "cuda" assert device.get_device("cuda:3") == "cuda:3" monkeypatch.setattr(device, "is_mps", lambda: True) assert device.get_device("mps") == "mps" def test_names_and_device_counts(monkeypatch): monkeypatch.setattr(device, "is_cuda", lambda: True) names = ["Test GPU 0", "Test GPU 1", "Test GPU 2", "Test GPU 3"] monkeypatch.setattr(device.torch.cuda, "get_device_name", names.__getitem__) monkeypatch.setattr(device.torch.cuda, "device_count", lambda: 4) assert device.get_device_name() == "Test GPU 0" assert device.get_device_name(3) == "Test GPU 3" assert device.device_count() == 4 monkeypatch.setattr(device, "is_cuda", lambda: False) monkeypatch.setattr(device, "is_mps", lambda: True) monkeypatch.setattr(device.platform, "processor", lambda: "M3") assert device.get_device_name() == "Apple M3 (MPS)" assert device.device_count() == 1 monkeypatch.setattr(device, "is_mps", lambda: False) assert device.get_device_name() == "CPU" assert device.device_count() == 0 def test_system_memory_sources_and_fallback(monkeypatch): gib = 1024**3 fake_psutil = SimpleNamespace( virtual_memory=lambda: SimpleNamespace(total=32 * gib, available=12 * gib), ) monkeypatch.setitem(__import__("sys").modules, "psutil", fake_psutil) assert device._system_memory_gb() == (32.0, 12.0) fake_psutil.virtual_memory = Mock(side_effect=FileNotFoundError("/proc/meminfo")) monkeypatch.setattr( device.os, "sysconf", lambda name: {"SC_PHYS_PAGES": 4, "SC_PAGE_SIZE": gib}[name], ) assert device._system_memory_gb() == (4.0, 2.4) monkeypatch.delitem(__import__("sys").modules, "psutil", raising=False) real_import = __import__("builtins").__import__ def reject_psutil(name, *args, **kwargs): if name == "psutil": raise ImportError return real_import(name, *args, **kwargs) monkeypatch.setattr("builtins.__import__", reject_psutil) monkeypatch.setattr(device.os, "sysconf", lambda name: {"SC_PHYS_PAGES": 4, "SC_PAGE_SIZE": gib}[name]) assert device._system_memory_gb() == (4.0, 2.4) monkeypatch.setattr(device.os, "sysconf", Mock(side_effect=ValueError)) assert device._system_memory_gb() == (16.0, 8.0) def test_memory_info_for_cuda_and_cuda_fallback(monkeypatch): gib = 1024**3 monkeypatch.setattr(device, "is_cuda", lambda: True) queried_names = [] def device_name(index=0): queried_names.append(index) return f"GPU {index}" monkeypatch.setattr(device, "get_device_name", device_name) monkeypatch.setattr(device.torch.cuda, "mem_get_info", lambda _index: (6 * gib, 8 * gib)) monkeypatch.setattr(device.torch.cuda, "memory_allocated", lambda _index: 1 * gib) monkeypatch.setattr(device.torch.cuda, "memory_reserved", lambda _index: 2 * gib) assert device.get_memory_info(2) == device.MemoryInfo(1, 2, 8, 6, "GPU 2") assert queried_names == [2] monkeypatch.setattr(device.torch.cuda, "mem_get_info", Mock(side_effect=RuntimeError("unsupported"))) monkeypatch.setattr( device.torch.cuda, "get_device_properties", lambda _index: SimpleNamespace(total_memory=10 * gib), ) assert device.get_memory_info(2) == device.MemoryInfo( total_gb=10, free_gb=10, device_name="GPU 2", ) def test_memory_info_for_mps_cpu_and_total_free(monkeypatch): monkeypatch.setattr(device, "is_cuda", lambda: False) monkeypatch.setattr(device, "is_mps", lambda: True) monkeypatch.setattr(device, "get_device_name", lambda: "MPS") monkeypatch.setattr(device, "_system_memory_gb", lambda: (20.0, 8.0)) assert device.get_memory_info() == device.MemoryInfo(6, 0, 14, 8, "MPS") assert device.get_total_free_gb() == pytest.approx(5.6) monkeypatch.setattr(device, "is_mps", lambda: False) monkeypatch.setattr(device, "get_device_name", lambda: "CPU") assert device.get_memory_info() == device.MemoryInfo(total_gb=20, free_gb=8, device_name="CPU") assert device.get_total_free_gb() == 0 def test_total_cuda_memory_sums_query_and_fallback(monkeypatch): gib = 1024**3 monkeypatch.setattr(device, "is_cuda", lambda: True) monkeypatch.setattr(device.torch.cuda, "device_count", lambda: 2) monkeypatch.setattr( device.torch.cuda, "mem_get_info", Mock(side_effect=[(3 * gib, 4 * gib), RuntimeError("no query")]), ) monkeypatch.setattr( device.torch.cuda, "get_device_properties", lambda _index: SimpleNamespace(total_memory=5 * gib), ) assert device.get_total_free_gb() == 8.0 def test_cache_cleanup_paths_are_best_effort(monkeypatch): cuda_empty = Mock() monkeypatch.setattr(device, "is_cuda", lambda: True) monkeypatch.setattr(device.torch.cuda, "empty_cache", cuda_empty) device.empty_cache() cuda_empty.assert_called_once_with() sync = Mock() reset = Mock() monkeypatch.setattr(device.torch.cuda, "empty_cache", Mock(side_effect=RuntimeError("busy"))) monkeypatch.setattr(device.torch.cuda, "synchronize", sync) monkeypatch.setattr(device.torch.cuda, "reset_peak_memory_stats", reset) monkeypatch.setattr(device.gc, "collect", Mock()) device.free_gpu_memory() sync.assert_called_once_with() reset.assert_called_once_with() mps_empty = Mock() mps_sync = Mock(side_effect=RuntimeError("busy")) monkeypatch.setattr(device, "is_cuda", lambda: False) monkeypatch.setattr(device, "is_mps", lambda: True) monkeypatch.setattr(device.torch, "mps", SimpleNamespace(empty_cache=mps_empty, synchronize=mps_sync)) device.empty_cache() mps_empty.side_effect = RuntimeError("busy") device.free_gpu_memory() assert mps_empty.call_count == 2 mps_sync.assert_called_once_with() @pytest.mark.parametrize( ("cuda", "mps", "expected"), [(False, False, False), (True, False, True), (False, True, True)], ) def test_per_batch_release_runs_only_with_an_accelerator(monkeypatch, cuda, mps, expected): # #684: hot activation loops skip the per-batch gc.collect() on CPU-only # hosts; with an accelerator they keep the full free_gpu_memory() release. monkeypatch.setattr(device, "is_cuda", lambda: cuda) monkeypatch.setattr(device, "is_mps", lambda: mps) assert device.per_batch_release_needed() is expected def test_cuda_cleanup_tolerates_every_recovery_failure(monkeypatch): monkeypatch.setattr(device, "is_cuda", lambda: True) monkeypatch.setattr(device.torch.cuda, "empty_cache", Mock(side_effect=RuntimeError("busy"))) monkeypatch.setattr(device.torch.cuda, "synchronize", Mock(side_effect=RuntimeError("lost"))) monkeypatch.setattr( device.torch.cuda, "reset_peak_memory_stats", Mock(side_effect=RuntimeError("lost")), ) monkeypatch.setattr(device.gc, "collect", Mock()) device.free_gpu_memory() @pytest.mark.parametrize( ("version", "expected"), [("2.2.9", False), ("2.3.0", True), ("2.10.1", True)], ) def test_mps_bfloat16_version_boundary(monkeypatch, version, expected): monkeypatch.setattr(device, "is_cuda", lambda: False) monkeypatch.setattr(device.torch, "__version__", version) assert device.supports_bfloat16("mps") is expected def test_seed_dtype_and_capability_contracts(monkeypatch): manual_seed = Mock() cuda_seed = Mock() monkeypatch.setattr(device.torch, "manual_seed", manual_seed) monkeypatch.setattr(device.torch.cuda, "manual_seed_all", cuda_seed) monkeypatch.setattr(device, "is_cuda", lambda: True) device.set_seed_all(7) manual_seed.assert_called_once_with(7) cuda_seed.assert_called_once_with(7) assert device.default_dtype("cpu") is torch.float32 assert device.default_dtype("cuda") is torch.float16 monkeypatch.setattr(device.torch.cuda, "get_device_capability", lambda _index: (8, 0)) assert device.supports_bfloat16("cuda") is True monkeypatch.setattr(device.torch.cuda, "get_device_capability", lambda _index: (7, 5)) assert device.supports_bfloat16("cuda") is False monkeypatch.setattr(device, "is_cuda", lambda: False) assert device.supports_bfloat16("cuda") is False assert device.supports_bfloat16("cpu") is True assert device.supports_float64("mps") is False assert device.supports_float64("cpu") is True assert device.supports_bitsandbytes("cuda:1") is True assert device.supports_bitsandbytes("mps") is False assert device.supports_device_map_auto("cuda") is True assert device.supports_device_map_auto("cpu") is False def test_svd_dtype_and_oom_matching(): assert device.safe_svd_dtype(torch.ones(1, dtype=torch.float64)) is torch.float64 assert device.safe_svd_dtype(torch.ones(1, dtype=torch.float16)) is torch.float32 assert device.is_oom_error(torch.cuda.OutOfMemoryError("oom")) is True assert device.is_oom_error(RuntimeError("MPS backend out of memory")) is True assert device.is_oom_error(RuntimeError("other")) is False def test_mps_svd_dtype_uses_float32_without_requiring_mps_hardware(): tensor = SimpleNamespace(device=SimpleNamespace(type="mps"), dtype=torch.float64) assert device.safe_svd_dtype(tensor) is torch.float32 def test_configure_cuda_allocator(monkeypatch): monkeypatch.delenv("PYTORCH_CUDA_ALLOC_CONF", raising=False) monkeypatch.setattr(device, "is_cuda", lambda: True) device.configure_cuda_alloc() assert device.os.environ["PYTORCH_CUDA_ALLOC_CONF"] == "expandable_segments:True" monkeypatch.setenv("PYTORCH_CUDA_ALLOC_CONF", "existing") device.configure_cuda_alloc() assert device.os.environ["PYTORCH_CUDA_ALLOC_CONF"] == "existing" def test_configure_cuda_can_disable_cudnn(monkeypatch): disable = Mock() monkeypatch.setattr(device, "is_cuda", lambda: True) monkeypatch.setattr(device.torch.backends.cuda, "enable_cudnn_sdp", disable) monkeypatch.setenv("OBLITERATUS_DISABLE_CUDNN", "1") monkeypatch.setattr(device.torch.backends.cudnn, "enabled", True) device.configure_cuda_alloc() disable.assert_called_once_with(False) assert device.torch.backends.cudnn.enabled is False def test_configure_cuda_disables_cudnn_without_sdp_toggle(monkeypatch): """Older Torch builds may not expose the cuDNN SDP backend toggle.""" monkeypatch.setattr(device, "is_cuda", lambda: True) monkeypatch.delattr( device.torch.backends.cuda, "enable_cudnn_sdp", raising=False ) monkeypatch.setenv("OBLITERATUS_DISABLE_CUDNN", "1") monkeypatch.setattr(device.torch.backends.cudnn, "enabled", True) device.configure_cuda_alloc() assert device.torch.backends.cudnn.enabled is False class _EmbeddingModel(torch.nn.Module): def __init__(self, *, first_parameter: torch.nn.Parameter | None = None): super().__init__() if first_parameter is not None: self.first_parameter = first_parameter self.embed = torch.nn.Embedding(8, 3) def get_input_embeddings(self): return self.embed def forward(self, *, input_ids): return self.embed(input_ids) def test_input_execution_device_uses_real_offloaded_embedding_hook(): accelerate_hooks = pytest.importorskip("accelerate.hooks") model = _EmbeddingModel() original_weight = model.embed.weight.detach().clone() hook = accelerate_hooks.AlignDevicesHook(execution_device="cpu", offload=True) accelerate_hooks.add_hook_to_module(model.embed, hook) assert next(model.embed.parameters()).device.type == "meta" input_ids = torch.tensor([[1, 2, 3]], dtype=torch.long) routed_ids = input_ids.to(device.resolve_input_execution_device(model)) output = model(input_ids=routed_ids) assert routed_ids.device.type == "cpu" torch.testing.assert_close(output, original_weight[input_ids]) def test_input_execution_device_uses_nested_accelerate_hook(): accelerate_hooks = pytest.importorskip("accelerate.hooks") model = _EmbeddingModel() hook = accelerate_hooks.AlignDevicesHook( execution_device="cpu", offload=True, place_submodules=True, ) accelerate_hooks.add_hook_to_module(model, hook) assert model.embed.weight.device.type == "meta" assert device.resolve_input_execution_device(model) == torch.device("cpu") def test_input_execution_device_uses_sequential_accelerate_hook(): accelerate_hooks = pytest.importorskip("accelerate.hooks") model = _EmbeddingModel() hook = accelerate_hooks.SequentialHook( accelerate_hooks.AlignDevicesHook(execution_device="cpu", offload=True), ) accelerate_hooks.add_hook_to_module(model.embed, hook) assert model.embed.weight.device.type == "meta" assert device.resolve_input_execution_device(model) == torch.device("cpu") @pytest.mark.parametrize("storage", ["cpu", "meta"]) def test_input_execution_device_prefers_embedding_hook_over_root(storage): accelerate_hooks = pytest.importorskip("accelerate.hooks") model = _EmbeddingModel() model.embed = torch.nn.Embedding(8, 3, device=storage) model.block = torch.nn.Linear(3, 3) model.hf_device_map = {"block": 0, "embed": 1} model._hf_hook = accelerate_hooks.AlignDevicesHook(execution_device="cuda:0") model.embed._hf_hook = accelerate_hooks.AlignDevicesHook(execution_device="cuda:1") assert device.resolve_input_execution_device(model) == torch.device("cuda:1") def test_input_execution_device_prefers_nearest_ancestor_hook_over_root(): accelerate_hooks = pytest.importorskip("accelerate.hooks") model = torch.nn.Module() model.backbone = torch.nn.Sequential(torch.nn.Embedding(8, 3, device="meta")) model.get_input_embeddings = lambda: model.backbone[0] model._hf_hook = accelerate_hooks.AlignDevicesHook(execution_device=0) model.backbone._hf_hook = accelerate_hooks.AlignDevicesHook(execution_device=1) assert device.resolve_input_execution_device(model) == torch.device("cuda:1") def test_input_execution_device_rejects_conflicting_hooks_on_embedding(): accelerate_hooks = pytest.importorskip("accelerate.hooks") model = _EmbeddingModel() model.embed = torch.nn.Embedding(8, 3, device="meta") model.embed._hf_hook = accelerate_hooks.SequentialHook( accelerate_hooks.AlignDevicesHook(execution_device="cuda:0"), accelerate_hooks.AlignDevicesHook(execution_device="cuda:1"), ) with pytest.raises(device.InputDeviceResolutionError, match="ambiguous"): device.resolve_input_execution_device(model) def test_input_execution_device_prefers_embedding_over_first_parameter(): model = _EmbeddingModel( first_parameter=torch.nn.Parameter(torch.empty(1, device="meta")), ) assert next(model.parameters()).device.type == "meta" assert device.resolve_input_execution_device(model) == torch.device("cpu") def test_input_execution_device_allows_models_without_parameters(): assert device.resolve_input_execution_device(torch.nn.Module()) == torch.device("cpu") def test_input_execution_device_rejects_ambiguous_mixed_storage_without_embedding(): class ModelWithMetaDevice(torch.nn.Module): def __init__(self): super().__init__() self.first = torch.nn.Parameter(torch.empty(1, device="meta")) self.second = torch.nn.Parameter(torch.zeros(1)) @property def device(self): return self.first.device with pytest.raises(device.InputDeviceResolutionError, match="ambiguous"): device.resolve_input_execution_device(ModelWithMetaDevice()) def test_input_execution_device_rejects_unsupported_meta_embedding_hook(): model = _EmbeddingModel() model.embed = torch.nn.Embedding(8, 3, device="meta") model.embed._hf_hook = object() with pytest.raises(device.InputDeviceResolutionError, match="unsupported"): device.resolve_input_execution_device(model) def test_input_execution_device_rejects_hook_without_execution_device(): accelerate_hooks = pytest.importorskip("accelerate.hooks") model = _EmbeddingModel() model.embed = torch.nn.Embedding(8, 3, device="meta") model.embed._hf_hook = accelerate_hooks.AlignDevicesHook( execution_device=None, offload=True, ) with pytest.raises(device.InputDeviceResolutionError, match="execution_device"): device.resolve_input_execution_device(model) @pytest.fixture def split_projection_model(): accelerate_hooks = pytest.importorskip("accelerate.hooks") model = torch.nn.Module() model.layer = torch.nn.Module() model.layer.attention = torch.nn.Module() model.layer.attention.projection = torch.nn.Linear(3, 3) model._hf_hook = accelerate_hooks.AlignDevicesHook(execution_device=torch.device("cuda", 0)) model.layer._hf_hook = accelerate_hooks.AlignDevicesHook( execution_device=torch.device("cuda", 1), ) return model @pytest.mark.parametrize( ("hook_owner", "expected"), [("projection", 0), ("attention", 0), ("layer", 1), ("model", 0)], ) def test_module_execution_device_prefers_nearest_hook(split_projection_model, hook_owner, expected): accelerate_hooks = pytest.importorskip("accelerate.hooks") model = split_projection_model projection = model.layer.attention.projection if hook_owner == "model": del model.layer._hf_hook elif hook_owner in {"attention", "projection"}: owner = projection if hook_owner == "projection" else model.layer.attention owner._hf_hook = accelerate_hooks.AlignDevicesHook(execution_device=torch.device("cuda", 0)) assert device.resolve_module_execution_device( model, projection, require_supported_hooks=True, ) == torch.device("cuda", expected) @pytest.mark.parametrize("require_supported_hooks", [False, True]) @pytest.mark.parametrize("explicit_ancestors", [False, True]) def test_module_execution_device_rejects_conflicting_local_hooks( split_projection_model, require_supported_hooks, explicit_ancestors, ): accelerate_hooks = pytest.importorskip("accelerate.hooks") model = split_projection_model projection = model.layer.attention.projection projection._hf_hook = accelerate_hooks.SequentialHook( accelerate_hooks.AlignDevicesHook(execution_device=torch.device("cuda", 0)), accelerate_hooks.AlignDevicesHook(execution_device=torch.device("cuda", 1)), ) with pytest.raises(device.InputDeviceResolutionError, match="ambiguous"): device.resolve_module_execution_device( model, projection, require_supported_hooks=require_supported_hooks, ancestors=([projection, model.layer.attention, model.layer, model] if explicit_ancestors else None), ) @pytest.mark.parametrize("hook_kind", ["unsupported", "missing_device"]) @pytest.mark.parametrize("hook_owner", ["projection", "attention"]) @pytest.mark.parametrize("require_supported_hooks", [False, True]) @pytest.mark.parametrize("explicit_ancestors", [False, True]) def test_module_execution_device_keeps_supported_hook_policy( split_projection_model, hook_kind, hook_owner, require_supported_hooks, explicit_ancestors, ): accelerate_hooks = pytest.importorskip("accelerate.hooks") model = split_projection_model projection = model.layer.attention.projection owner = projection if hook_owner == "projection" else model.layer.attention owner._hf_hook = (object() if hook_kind == "unsupported" else accelerate_hooks.AlignDevicesHook(execution_device=None)) ancestors = ([projection, model.layer.attention, model.layer, model] if explicit_ancestors else None) if require_supported_hooks: message = "unsupported" if hook_kind == "unsupported" else "execution_device" with pytest.raises(device.InputDeviceResolutionError, match=message): device.resolve_module_execution_device( model, projection, require_supported_hooks=True, ancestors=ancestors, ) else: assert device.resolve_module_execution_device( model, projection, require_supported_hooks=False, ancestors=ancestors, ) == torch.device("cuda", 1) @pytest.fixture def shared_projection_model(): accelerate_hooks = pytest.importorskip("accelerate.hooks") model = torch.nn.Module() projection = torch.nn.Linear(3, 3) for name, execution in (("cuda_layer", "cuda:1"), ("cpu_layer", "cpu")): layer = torch.nn.Module() layer.attention = torch.nn.Module() layer.attention.projection = projection layer._hf_hook = accelerate_hooks.AlignDevicesHook(execution_device=execution) model.add_module(name, layer) model._hf_hook = accelerate_hooks.AlignDevicesHook(execution_device="cuda:0") return model @pytest.mark.parametrize( ("layer_name", "expected"), [("cuda_layer", "cuda:1"), ("cpu_layer", "cpu")], ) def test_module_execution_device_uses_explicit_context_for_shared_projection( shared_projection_model, layer_name, expected, monkeypatch, ): model = shared_projection_model layer = getattr(model, layer_name) attention = layer.attention projection = attention.projection hook_device = Mock(wraps=device._hook_execution_device) monkeypatch.setattr(device, "_hook_execution_device", hook_device) assert device.resolve_module_execution_device( model, projection, require_supported_hooks=True, ancestors=[projection, projection, attention, attention, layer, model], ) == torch.device(expected) assert [call.args[0][0] for call in hook_device.call_args_list] == [projection, attention, layer] @pytest.mark.parametrize("require_supported_hooks", [False, True]) def test_module_execution_device_rejects_conflicting_shared_aliases( shared_projection_model, require_supported_hooks, ): model = shared_projection_model with pytest.raises(device.InputDeviceResolutionError, match="ambiguous shared module"): device.resolve_module_execution_device( model, model.cpu_layer.attention.projection, require_supported_hooks=require_supported_hooks, ) @pytest.mark.parametrize("hook_owner", ["layer", "projection"]) def test_module_execution_device_accepts_shared_aliases_that_agree( shared_projection_model, hook_owner, ): accelerate_hooks = pytest.importorskip("accelerate.hooks") model = shared_projection_model projection = model.cpu_layer.attention.projection owner = model.cpu_layer if hook_owner == "layer" else projection owner._hf_hook = accelerate_hooks.AlignDevicesHook(execution_device="cuda:1") assert device.resolve_module_execution_device( model, projection, require_supported_hooks=True, ) == torch.device("cuda:1") @pytest.mark.parametrize("require_supported_hooks", [False, True]) @pytest.mark.parametrize("conflicting", [False, True]) def test_module_execution_device_own_hook_decides_without_alias_scan( shared_projection_model, monkeypatch, require_supported_hooks, conflicting, ): accelerate_hooks = pytest.importorskip("accelerate.hooks") model = shared_projection_model projection = model.cpu_layer.attention.projection hook = accelerate_hooks.AlignDevicesHook(execution_device="cuda:1") if conflicting: hook = accelerate_hooks.SequentialHook( hook, accelerate_hooks.AlignDevicesHook(execution_device="cpu"), ) projection._hf_hook = hook scan = Mock(side_effect=AssertionError("a decisive own hook must not scan aliases")) monkeypatch.setattr(device, "_module_ancestor_chains", scan) if conflicting: with pytest.raises(device.InputDeviceResolutionError, match="ambiguous"): device.resolve_module_execution_device( model, projection, require_supported_hooks=require_supported_hooks, ) else: assert device.resolve_module_execution_device( model, projection, require_supported_hooks=require_supported_hooks, ) == torch.device("cuda:1") scan.assert_not_called() @pytest.mark.parametrize("own_hook", [False, True]) def test_module_execution_device_prunes_registered_back_references(split_projection_model, own_hook): accelerate_hooks = pytest.importorskip("accelerate.hooks") model = split_projection_model model.layer.parent = model projection = model.layer.attention.projection if own_hook: projection._hf_hook = accelerate_hooks.AlignDevicesHook(execution_device="cpu") assert device.resolve_module_execution_device( model, projection, require_supported_hooks=True, ) == torch.device("cpu" if own_hook else "cuda:1") @pytest.mark.parametrize("alias_count", [64, 65]) @pytest.mark.parametrize("container_alias", [False, True]) def test_module_execution_device_bounds_alias_paths(alias_count, container_alias): accelerate_hooks = pytest.importorskip("accelerate.hooks") model = torch.nn.Module() projection = torch.nn.Linear(3, 3) alias = torch.nn.Sequential(projection) if container_alias else projection for index in range(alias_count): model.add_module(f"alias_{index}", alias) model._hf_hook = accelerate_hooks.AlignDevicesHook(execution_device="cpu") if alias_count > 64: with pytest.raises(device.InputDeviceResolutionError, match="too many alias paths"): device.resolve_module_execution_device(model, projection, require_supported_hooks=True) else: assert device.resolve_module_execution_device( model, projection, require_supported_hooks=True, ) == torch.device("cpu") @pytest.mark.parametrize("registered_target", [False, True]) def test_module_execution_device_indexes_unused_shared_subgraphs_once(monkeypatch, registered_target): # Only 28 modules, but two aliases at every step create over 2**26 # downward visits. None of those paths belongs to the target's ancestry. nodes = [torch.nn.Module() for _ in range(27)] target = torch.nn.Module() for parent, child in zip(nodes, nodes[1:]): parent.left = child parent.right = child if registered_target: nodes[0].target = target work = _measure_traversal_work(monkeypatch, [*nodes, target]) hooks = Mock(wraps=device._hook_execution_device) monkeypatch.setattr(device, "_hook_execution_device", hooks) assert device.resolve_module_execution_device( nodes[0], target, require_supported_hooks=True, ) is None assert work.edges == 2 * (len(nodes) - 1) + int(registered_target) assert 0 < work.charges < 200 assert hooks.call_count == (2 if registered_target else 1) def test_module_execution_device_bounds_relevant_shared_subgraph_aliases(monkeypatch): nodes = [torch.nn.Module() for _ in range(27)] for parent, child in zip(nodes, nodes[1:]): parent.left = child parent.right = child work = _measure_traversal_work(monkeypatch, nodes) hooks = Mock(wraps=device._hook_execution_device) monkeypatch.setattr(device, "_hook_execution_device", hooks) with pytest.raises(device.InputDeviceResolutionError, match="too many alias paths"): device.resolve_module_execution_device(nodes[0], nodes[-1], require_supported_hooks=True) assert work.edges == 2 * (len(nodes) - 1) assert work.charges < device._MODULE_TRAVERSAL_LIMIT assert hooks.call_count == 1 # Only the target's own hook precedes path validation. def _measure_traversal_work(monkeypatch, modules): """Count yielded edges and observe the live DFS stack at budget checks.""" work = SimpleNamespace(edges=0, peak_pending=0, charges=0) class CountingChildren(dict): def values(self): for child in super().values(): work.edges += 1 yield child def items(self): for edge in super().items(): work.edges += 1 yield edge for module in modules: monkeypatch.setattr(module, "_modules", CountingChildren(module._modules)) charge = device._ModuleTraversalBudget.charge def record_charge(budget): # Observe both the ancestry walker and index builder, including the # last state before a failed reservation. No graph work is simulated. caller = sys._getframe(1) work.peak_pending = max(work.peak_pending, len(caller.f_locals.get("pending", ()))) work.charges += 1 return charge(budget) monkeypatch.setattr(device._ModuleTraversalBudget, "charge", record_charge) return work @pytest.mark.parametrize("walker", ["automatic", "parent_index"]) @pytest.mark.parametrize("edge_kind", ["cycle", "blocked", "none", "leaf"]) def test_module_traversal_charges_wide_and_pruned_edges(monkeypatch, walker, edge_kind): root, target, leaf = [torch.nn.Module() for _ in range(3)] child = {"cycle": root, "blocked": leaf, "none": None, "leaf": leaf}[edge_kind] root._modules = dict.fromkeys((f"edge_{index}" for index in range(100_001)), child) # A match after the wide fan-out must not turn an incomplete scan into success. root.target = target work = _measure_traversal_work(monkeypatch, [root, leaf, target]) budget = device._ModuleTraversalBudget() with pytest.raises(device.InputDeviceResolutionError, match="alias traversal budget exceeded"): if walker == "automatic": device._module_ancestor_chains( root, target, blocked_modules=(leaf,) if edge_kind == "blocked" else (), ) else: device._ModuleParentIndex(budget).ancestor_paths(root, target) assert 0 < work.edges < device._MODULE_TRAVERSAL_LIMIT assert work.charges == device._MODULE_TRAVERSAL_LIMIT + 1 assert 1 <= work.peak_pending <= 2 # Width cannot queue 100,001 sibling states. if walker == "parent_index": assert budget.used == device._MODULE_TRAVERSAL_LIMIT @pytest.mark.parametrize("walker", ["automatic", "parent_index"]) def test_module_traversal_reserves_deep_states_before_allocating(monkeypatch, walker): limit = 128 monkeypatch.setattr(device, "_MODULE_TRAVERSAL_LIMIT", limit) nodes = [torch.nn.Module() for _ in range(400)] for parent, child in zip(nodes, nodes[1:]): parent.child = child work = _measure_traversal_work(monkeypatch, nodes) with pytest.raises(device.InputDeviceResolutionError, match="alias traversal budget exceeded"): if walker == "automatic": device._module_ancestor_chains(nodes[0], nodes[-1]) else: device._ModuleParentIndex(device._ModuleTraversalBudget()).ancestor_paths( nodes[0], nodes[-1], ) assert 0 < work.edges <= limit // 2 assert 1 < work.peak_pending <= limit // 2 assert work.charges == limit + 1 @pytest.mark.parametrize("with_hook", [False, True]) def test_module_execution_device_compares_unresolved_shared_aliases( shared_projection_model, with_hook, ): model = shared_projection_model projection = model.cpu_layer.attention.projection del model._hf_hook del model.cuda_layer._hf_hook if with_hook: with pytest.raises(device.InputDeviceResolutionError, match="ambiguous shared module"): device.resolve_module_execution_device(model, projection, require_supported_hooks=True) else: del model.cpu_layer._hf_hook assert device.resolve_module_execution_device( model, projection, require_supported_hooks=True, ) is None @pytest.mark.parametrize("alias_device", ["cuda:0", "cpu"]) def test_input_execution_device_checks_shared_embedding_aliases(alias_device): accelerate_hooks = pytest.importorskip("accelerate.hooks") model = _EmbeddingModel() model.alias = torch.nn.Module() model.alias.embed = model.embed model._hf_hook = accelerate_hooks.AlignDevicesHook(execution_device="cuda:0") model.alias._hf_hook = accelerate_hooks.AlignDevicesHook(execution_device=alias_device) if alias_device == "cuda:0": assert device.resolve_input_execution_device(model) == torch.device("cuda:0") else: with pytest.raises(device.InputDeviceResolutionError, match="ambiguous shared module"): device.resolve_input_execution_device(model) def test_module_execution_device_does_not_infer_from_storage_or_unrelated_root( split_projection_model, ): model = split_projection_model assert device.resolve_module_execution_device( model, torch.nn.Linear(3, 3), require_supported_hooks=True, ) is None del model._hf_hook del model.layer._hf_hook assert device.resolve_module_execution_device( model, model.layer.attention.projection, require_supported_hooks=True, ) is None