| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| import os |
| import unittest |
| from tempfile import TemporaryDirectory |
|
|
| import torch |
| import torch.nn as nn |
|
|
| from accelerate.utils import ( |
| OffloadedWeightsLoader, |
| extract_submodules_state_dict, |
| is_torch_version, |
| load_offloaded_weight, |
| offload_state_dict, |
| offload_weight, |
| ) |
|
|
|
|
| class ModelForTest(nn.Module): |
| def __init__(self): |
| super().__init__() |
| self.linear1 = nn.Linear(3, 4) |
| self.batchnorm = nn.BatchNorm1d(4) |
| self.linear2 = nn.Linear(4, 5) |
|
|
| def forward(self, x): |
| return self.linear2(self.batchnorm(self.linear1(x))) |
|
|
|
|
| class OffloadTester(unittest.TestCase): |
| def test_offload_state_dict(self): |
| model = ModelForTest() |
| with TemporaryDirectory() as tmp_dir: |
| offload_state_dict(tmp_dir, model.state_dict()) |
| index_file = os.path.join(tmp_dir, "index.json") |
| self.assertTrue(os.path.isfile(index_file)) |
| |
|
|
| for key in ["linear1.weight", "linear1.bias", "linear2.weight", "linear2.bias"]: |
| weight_file = os.path.join(tmp_dir, f"{key}.dat") |
| self.assertTrue(os.path.isfile(weight_file)) |
| |
|
|
| def test_offload_weight(self): |
| dtypes = [torch.float16, torch.float32] |
| if is_torch_version(">=", "1.10"): |
| dtypes.append(torch.bfloat16) |
|
|
| for dtype in dtypes: |
| weight = torch.randn(2, 3, dtype=dtype) |
| with TemporaryDirectory() as tmp_dir: |
| index = offload_weight(weight, "weight", tmp_dir, {}) |
| weight_file = os.path.join(tmp_dir, "weight.dat") |
| self.assertTrue(os.path.isfile(weight_file)) |
| self.assertDictEqual(index, {"weight": {"shape": [2, 3], "dtype": str(dtype).split(".")[1]}}) |
|
|
| new_weight = load_offloaded_weight(weight_file, index["weight"]) |
| self.assertTrue(torch.equal(weight, new_weight)) |
|
|
| def test_offload_weights_loader(self): |
| model = ModelForTest() |
| state_dict = model.state_dict() |
| cpu_part = {k: v for k, v in state_dict.items() if "linear2" not in k} |
| disk_part = {k: v for k, v in state_dict.items() if "linear2" in k} |
|
|
| with TemporaryDirectory() as tmp_dir: |
| offload_state_dict(tmp_dir, disk_part) |
| weight_map = OffloadedWeightsLoader(state_dict=cpu_part, save_folder=tmp_dir) |
|
|
| |
| self.assertEqual(sorted(weight_map), sorted(state_dict.keys())) |
| for key, param in state_dict.items(): |
| self.assertTrue(torch.allclose(param, weight_map[key])) |
|
|
| cpu_part = {k: v for k, v in state_dict.items() if "weight" in k} |
| disk_part = {k: v for k, v in state_dict.items() if "weight" not in k} |
|
|
| with TemporaryDirectory() as tmp_dir: |
| offload_state_dict(tmp_dir, disk_part) |
| weight_map = OffloadedWeightsLoader(state_dict=cpu_part, save_folder=tmp_dir) |
|
|
| |
| self.assertEqual(sorted(weight_map), sorted(state_dict.keys())) |
| for key, param in state_dict.items(): |
| self.assertTrue(torch.allclose(param, weight_map[key])) |
|
|
| with TemporaryDirectory() as tmp_dir: |
| offload_state_dict(tmp_dir, state_dict) |
| |
| weight_map = OffloadedWeightsLoader(state_dict=cpu_part, save_folder=tmp_dir) |
|
|
| |
| self.assertEqual(sorted(weight_map), sorted(state_dict.keys())) |
| for key, param in state_dict.items(): |
| self.assertTrue(torch.allclose(param, weight_map[key])) |
|
|
| def test_extract_submodules_state_dict(self): |
| state_dict = {"a.1": 0, "a.10": 1, "a.2": 2} |
| extracted = extract_submodules_state_dict(state_dict, ["a.1", "a.2"]) |
| self.assertDictEqual(extracted, {"a.1": 0, "a.2": 2}) |
|
|
| state_dict = {"a.1.a": 0, "a.10.a": 1, "a.2.a": 2} |
| extracted = extract_submodules_state_dict(state_dict, ["a.1", "a.2"]) |
| self.assertDictEqual(extracted, {"a.1.a": 0, "a.2.a": 2}) |
|
|