Skip to content

[Tests] fix sharding tests #8764

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/diffusers/models/embeddings.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,9 +415,10 @@ def __init__(

if set_W_to_weight:
# to delete later
del self.weight
self.W = nn.Parameter(torch.randn(embedding_size) * scale, requires_grad=False)

self.weight = self.W
del self.W
Comment on lines +418 to +421
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Necessary to mitigate the "tied weight tensors" error from safetensors.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sayakpaul curious about codes here, may i ask for more details if possible? ;)


def forward(self, x):
if self.log:
Expand Down
7 changes: 4 additions & 3 deletions tests/models/autoencoders/test_models_vae.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,10 @@ class ConsistencyDecoderVAETests(ModelTesterMixin, unittest.TestCase):
forward_requires_fresh_args = True

def inputs_dict(self, seed=None):
generator = torch.Generator("cpu")
if seed is not None:
generator.manual_seed(0)
Comment on lines -365 to -366
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was likely a mistake because if a seed is provided you would want to use that to do: generator.manual_seed(seed) and not generator.manual_seed(0) using a fixed seed.

if seed is None:
generator = torch.Generator("cpu").manual_seed(0)
else:
generator = torch.Generator("cpu").manual_seed(seed)
image = randn_tensor((4, 3, 32, 32), generator=generator, device=torch.device(torch_device))

return {"sample": image, "generator": generator}
Expand Down
5 changes: 4 additions & 1 deletion tests/models/test_modeling_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -905,11 +905,13 @@ def test_sharded_checkpoints(self):
actual_num_shards = len([file for file in os.listdir(tmp_dir) if file.endswith(".safetensors")])
self.assertTrue(actual_num_shards == expected_num_shards)

new_model = self.model_class.from_pretrained(tmp_dir)
new_model = self.model_class.from_pretrained(tmp_dir).eval()
new_model = new_model.to(torch_device)

torch.manual_seed(0)
_, inputs_dict = self.prepare_init_args_and_inputs_for_common()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise the generator (which is required as an input when using the ConsistencyDecoderVAE will be consumed and its states will change in-place. So, we need to re-initialize here.

new_output = new_model(**inputs_dict)

self.assertTrue(torch.allclose(base_output[0], new_output[0], atol=1e-5))

@require_torch_gpu
Expand Down Expand Up @@ -940,6 +942,7 @@ def test_sharded_checkpoints_device_map(self):
new_model = new_model.to(torch_device)

torch.manual_seed(0)
_, inputs_dict = self.prepare_init_args_and_inputs_for_common()
new_output = new_model(**inputs_dict)
self.assertTrue(torch.allclose(base_output[0], new_output[0], atol=1e-5))

Expand Down
Loading