llama-cpp-python icon indicating copy to clipboard operation
llama-cpp-python copied to clipboard

Error when passing model to deepcopy in llama_cpp_python>=0.3.0

Open sergey21000 opened this issue 1 year ago • 0 comments

Environment

  • OS Linux (Google Colab and other)
  • Python 3.10.12
  • llama_cpp_python>=0.3.0

Steps to Reproduce

# pip install llama_cpp_python 
from copy import copy, deepcopy
from llama_cpp import Llama

model = Llama.from_pretrained(
    repo_id="bartowski/gemma-2-2b-it-GGUF",
    filename="*8_0.gguf",
    local_dir='./models',
    verbose=False,
)
deepcopy(model)

Error Message

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-11-b9be7995a25d> in <cell line: 11>()
      9 )
     10 
---> 11 deepcopy(model)

1 frames
/usr/lib/python3.10/copy.py in deepcopy(x, memo, _nil)
    159                     reductor = getattr(x, "__reduce_ex__", None)
    160                     if reductor is not None:
--> 161                         rv = reductor(4)
    162                     else:
    163                         reductor = getattr(x, "__reduce__", None)

/usr/local/lib/python3.10/dist-packages/llama_cpp/llama.py in __getstate__(self)
   2073             kv_overrides=self.kv_overrides,
   2074             # Context Params
-> 2075             seed=self.context_params.seed,
   2076             n_ctx=self.context_params.n_ctx,
   2077             n_batch=self.n_batch,

AttributeError: 'llama_context_params' object has no attribute 'seed'

Additional Information

This issue prevents the Llama model from being passed to Gradio's State. The following code results in a similar error:

# pip install gradio 
import gradio as gr

model_state = gr.State({'model': model})

Error code

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-12-9d791615643d> in <cell line: 2>()
      1 import gradio as gr
----> 2 model_state = gr.State({'model': model})

5 frames
/usr/local/lib/python3.10/dist-packages/gradio/component_meta.py in wrapper(*args, **kwargs)
    165             return None
    166         else:
--> 167             return fn(self, **kwargs)
    168 
    169     return wrapper

/usr/local/lib/python3.10/dist-packages/gradio/components/state.py in __init__(self, value, render, time_to_live, delete_callback)
     46         self.delete_callback = delete_callback or (lambda a: None)  # noqa: ARG005
     47         try:
---> 48             self.value = deepcopy(value)
     49         except TypeError as err:
     50             raise TypeError(

/usr/lib/python3.10/copy.py in deepcopy(x, memo, _nil)
    144     copier = _deepcopy_dispatch.get(cls)
    145     if copier is not None:
--> 146         y = copier(x, memo)
    147     else:
    148         if issubclass(cls, type):

/usr/lib/python3.10/copy.py in _deepcopy_dict(x, memo, deepcopy)
    229     memo[id(x)] = y
    230     for key, value in x.items():
--> 231         y[deepcopy(key, memo)] = deepcopy(value, memo)
    232     return y
    233 d[dict] = _deepcopy_dict

/usr/lib/python3.10/copy.py in deepcopy(x, memo, _nil)
    159                     reductor = getattr(x, "__reduce_ex__", None)
    160                     if reductor is not None:
--> 161                         rv = reductor(4)
    162                     else:
    163                         reductor = getattr(x, "__reduce__", None)

/usr/local/lib/python3.10/dist-packages/llama_cpp/llama.py in __getstate__(self)
   2073             kv_overrides=self.kv_overrides,
   2074             # Context Params
-> 2075             seed=self.context_params.seed,
   2076             n_ctx=self.context_params.n_ctx,
   2077             n_batch=self.n_batch,

AttributeError: 'llama_context_params' object has no attribute 'seed'

Workaround

This issue doesn't occur with versions of llama_cpp_python below 0.3.0

sergey21000 avatar Sep 28 '24 12:09 sergey21000