Skip to content
Open
Changes from all 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
10 changes: 7 additions & 3 deletions python_coreml_stable_diffusion/activation_quantization.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,8 @@ def hook(_, args, kwargs):
if len(timestep.shape) == 0:
timestep = timestep[None]
timestep = timestep.expand(sample.shape[0])
encoder_hidden_states = kwargs["encoder_hidden_states"]
encoder_hidden_states = encoder_hidden_states.permute((0, 2, 1)).unsqueeze(2)
# In-place permutation and unsqueeze by using out-of-place ops (as before)
encoder_hidden_states = kwargs["encoder_hidden_states"].permute(0, 2, 1).unsqueeze(2)
modified_args = (sample, timestep, encoder_hidden_states)
return (modified_args, {})

Expand All @@ -301,8 +301,12 @@ def prepare_pipe(pipe, unet):
"""
Create a new pipeline from `pipe` with `unet` as the noise predictor
"""
# Use shallow copy for pipe except for .unet assignment for significant memory savings
# This assumes downstream code only requires deep copy if necessary for other fields. But as
# behavior preservation is critical, keep deepcopy but move .to() before deepcopy to save time.
device = pipe.unet.device
unet.to(device)
new_pipe = deepcopy(pipe)
unet.to(new_pipe.unet.device)
new_pipe.unet = unet
pre_hook_handle = register_input_preprocessing_hook(new_pipe)
return new_pipe, pre_hook_handle
Expand Down