|
| 1 | +import torch |
| 2 | + |
| 3 | +from invokeai.backend.model_manager.load.model_cache.cached_model.cached_model_only_full_load import ( |
| 4 | + CachedModelOnlyFullLoad, |
| 5 | +) |
| 6 | +from tests.backend.model_manager.load.model_cache.cached_model.utils import DummyModule, parameterize_mps_and_cuda |
| 7 | + |
| 8 | + |
| 9 | +class NonTorchModel: |
| 10 | + """A model that does not sub-class torch.nn.Module.""" |
| 11 | + |
| 12 | + def __init__(self): |
| 13 | + self.linear = torch.nn.Linear(10, 32) |
| 14 | + |
| 15 | + def run_inference(self, x: torch.Tensor) -> torch.Tensor: |
| 16 | + return self.linear(x) |
| 17 | + |
| 18 | + |
| 19 | +@parameterize_mps_and_cuda |
| 20 | +def test_cached_model_total_bytes(device: str): |
| 21 | + model = DummyModule() |
| 22 | + cached_model = CachedModelOnlyFullLoad(model=model, compute_device=torch.device(device), total_bytes=100) |
| 23 | + assert cached_model.total_bytes() == 100 |
| 24 | + |
| 25 | + |
| 26 | +@parameterize_mps_and_cuda |
| 27 | +def test_cached_model_is_in_vram(device: str): |
| 28 | + model = DummyModule() |
| 29 | + cached_model = CachedModelOnlyFullLoad(model=model, compute_device=torch.device(device), total_bytes=100) |
| 30 | + assert not cached_model.is_in_vram() |
| 31 | + assert cached_model.cur_vram_bytes() == 0 |
| 32 | + |
| 33 | + cached_model.full_load_to_vram() |
| 34 | + assert cached_model.is_in_vram() |
| 35 | + assert cached_model.cur_vram_bytes() == 100 |
| 36 | + |
| 37 | + cached_model.full_unload_from_vram() |
| 38 | + assert not cached_model.is_in_vram() |
| 39 | + assert cached_model.cur_vram_bytes() == 0 |
| 40 | + |
| 41 | + |
| 42 | +@parameterize_mps_and_cuda |
| 43 | +def test_cached_model_full_load_and_unload(device: str): |
| 44 | + model = DummyModule() |
| 45 | + cached_model = CachedModelOnlyFullLoad(model=model, compute_device=torch.device(device), total_bytes=100) |
| 46 | + assert cached_model.full_load_to_vram() == 100 |
| 47 | + assert cached_model.is_in_vram() |
| 48 | + assert all(p.device.type == device for p in cached_model.model.parameters()) |
| 49 | + |
| 50 | + assert cached_model.full_unload_from_vram() == 100 |
| 51 | + assert not cached_model.is_in_vram() |
| 52 | + assert all(p.device.type == "cpu" for p in cached_model.model.parameters()) |
| 53 | + |
| 54 | + |
| 55 | +@parameterize_mps_and_cuda |
| 56 | +def test_cached_model_get_cpu_state_dict(device: str): |
| 57 | + model = DummyModule() |
| 58 | + cached_model = CachedModelOnlyFullLoad(model=model, compute_device=torch.device(device), total_bytes=100) |
| 59 | + assert not cached_model.is_in_vram() |
| 60 | + |
| 61 | + # The CPU state dict can be accessed and has the expected properties. |
| 62 | + cpu_state_dict = cached_model.get_cpu_state_dict() |
| 63 | + assert cpu_state_dict is not None |
| 64 | + assert len(cpu_state_dict) == len(model.state_dict()) |
| 65 | + assert all(p.device.type == "cpu" for p in cpu_state_dict.values()) |
| 66 | + |
| 67 | + # Full load the model into VRAM. |
| 68 | + cached_model.full_load_to_vram() |
| 69 | + assert cached_model.is_in_vram() |
| 70 | + |
| 71 | + # The CPU state dict is still available, and still on the CPU. |
| 72 | + cpu_state_dict = cached_model.get_cpu_state_dict() |
| 73 | + assert cpu_state_dict is not None |
| 74 | + assert len(cpu_state_dict) == len(model.state_dict()) |
| 75 | + assert all(p.device.type == "cpu" for p in cpu_state_dict.values()) |
| 76 | + |
| 77 | + |
| 78 | +@parameterize_mps_and_cuda |
| 79 | +def test_cached_model_full_load_and_inference(device: str): |
| 80 | + model = DummyModule() |
| 81 | + cached_model = CachedModelOnlyFullLoad(model=model, compute_device=torch.device(device), total_bytes=100) |
| 82 | + assert not cached_model.is_in_vram() |
| 83 | + |
| 84 | + # Run inference on the CPU. |
| 85 | + x = torch.randn(1, 10) |
| 86 | + output1 = model(x) |
| 87 | + assert output1.device.type == "cpu" |
| 88 | + |
| 89 | + # Full load the model into VRAM. |
| 90 | + cached_model.full_load_to_vram() |
| 91 | + assert cached_model.is_in_vram() |
| 92 | + |
| 93 | + # Run inference on the GPU. |
| 94 | + output2 = model(x.to(device)) |
| 95 | + assert output2.device.type == device |
| 96 | + |
| 97 | + # The outputs should be the same for both runs. |
| 98 | + assert torch.allclose(output1, output2.to("cpu")) |
| 99 | + |
| 100 | + |
| 101 | +@parameterize_mps_and_cuda |
| 102 | +def test_non_torch_model(device: str): |
| 103 | + model = NonTorchModel() |
| 104 | + cached_model = CachedModelOnlyFullLoad(model=model, compute_device=torch.device(device), total_bytes=100) |
| 105 | + assert not cached_model.is_in_vram() |
| 106 | + |
| 107 | + # The model does not have a CPU state dict. |
| 108 | + assert cached_model.get_cpu_state_dict() is None |
| 109 | + |
| 110 | + # Attempting to load the model into VRAM should have no effect. |
| 111 | + cached_model.full_load_to_vram() |
| 112 | + assert not cached_model.is_in_vram() |
| 113 | + assert cached_model.cur_vram_bytes() == 0 |
| 114 | + |
| 115 | + # Attempting to unload the model from VRAM should have no effect. |
| 116 | + cached_model.full_unload_from_vram() |
| 117 | + assert not cached_model.is_in_vram() |
| 118 | + assert cached_model.cur_vram_bytes() == 0 |
| 119 | + |
| 120 | + # Running inference on the CPU should work. |
| 121 | + output1 = model.run_inference(torch.randn(1, 10)) |
| 122 | + assert output1.device.type == "cpu" |
0 commit comments