Skip to content
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

can't deepcopy quantized pipeline #358

Open
noskill opened this issue Dec 4, 2024 · 2 comments
Open

can't deepcopy quantized pipeline #358

noskill opened this issue Dec 4, 2024 · 2 comments

Comments

@noskill
Copy link

noskill commented Dec 4, 2024

It seems that quantized models can't be deepcopied anymore

optimum.quanto.version
'0.2.6'

from diffusers import FluxPipeline
from optimum.quanto import freeze, qfloat8, quantize as _quantize


def quantize(pipe, dtype=qfloat8):
    components = ['unet', 'transformer', 'text_encoder', 'text_encoder_2', 'vae']
    
    for component in components:
        if hasattr(pipe, component):
            component_obj = getattr(pipe, component)
            _quantize(component_obj, weights=dtype)
            freeze(component_obj)
            # Add attributes to indicate quantization
            component_obj._is_quantized = True
            component_obj._quantization_dtype = dtype


pipe0 = FluxPipeline.from_pretrained("./models/flux-1-dev", torch_dtype=torch.bfloat16).to("cpu")
quantize(pipe0)
cp.deepcopy(pipe0)

error:

    cp.deepcopy(pipe0)
  File "/usr/lib/python3.10/copy.py", line 172, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "/usr/lib/python3.10/copy.py", line 271, in _reconstruct
    state = deepcopy(state, memo)
  File "/usr/lib/python3.10/copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "/usr/lib/python3.10/copy.py", line 231, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/usr/lib/python3.10/copy.py", line 172, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "/usr/lib/python3.10/copy.py", line 271, in _reconstruct
    state = deepcopy(state, memo)
  File "/usr/lib/python3.10/copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "/usr/lib/python3.10/copy.py", line 231, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/usr/lib/python3.10/copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "/usr/lib/python3.10/copy.py", line 231, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/usr/lib/python3.10/copy.py", line 172, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "/usr/lib/python3.10/copy.py", line 271, in _reconstruct
    state = deepcopy(state, memo)
  File "/usr/lib/python3.10/copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "/usr/lib/python3.10/copy.py", line 231, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/usr/lib/python3.10/copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "/usr/lib/python3.10/copy.py", line 231, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/usr/lib/python3.10/copy.py", line 172, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "/usr/lib/python3.10/copy.py", line 271, in _reconstruct
    state = deepcopy(state, memo)
  File "/usr/lib/python3.10/copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "/usr/lib/python3.10/copy.py", line 231, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/usr/lib/python3.10/copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "/usr/lib/python3.10/copy.py", line 231, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/usr/lib/python3.10/copy.py", line 153, in deepcopy
    y = copier(memo)
  File "/tester/_work/_tool/Python/3.10.15/x64/lib/python3.10/site-packages/torch/_tensor.py", line 83, in __deepcopy__
    return handle_torch_function(Tensor.__deepcopy__, (self,), self, memo)
  File "/tester/_work/_tool/Python/3.10.15/x64/lib/python3.10/site-packages/torch/overrides.py", line 1739, in handle_torch_function
    result = torch_func_method(public_api, types, args, kwargs)
  File "/tester/_work/_tool/Python/3.10.15/x64/lib/python3.10/site-packages/optimum/quanto/tensor/weights/qbytes.py", line 272, in __torch_function__
    return func(*args, **kwargs)
  File "/tester/_work/_tool/Python/3.10.15/x64/lib/python3.10/site-packages/torch/_tensor.py", line 112, in __deepcopy__
    raise RuntimeError(
RuntimeError: The default implementation of __deepcopy__() for wrapper subclasses only works for subclass types that implement clone() and for which cloning returns another instance of the same subclass. You should either properly implement clone() for your subclass or override __deepcopy__() if it is intended behavior for clone() to return an instance of a different type.

if i downgrade to 0.2.4 code works

@noskill noskill changed the title can't move quantized pipeline can't deepcopy quantized pipeline Dec 4, 2024
Copy link

github-actions bot commented Jan 4, 2025

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions github-actions bot added the Stale label Jan 4, 2025
@noskill
Copy link
Author

noskill commented Jan 8, 2025

not stale!

@dacorvo dacorvo removed the Stale label Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants