Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Error when passing model to deepcopy in llama_cpp_python>=0.3.0 #1769

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

Closed
sergey21000 opened this issue Sep 28, 2024 · 3 comments
Closed

Error when passing model to deepcopy in llama_cpp_python>=0.3.0 #1769

sergey21000 opened this issue Sep 28, 2024 · 3 comments

Comments

@sergey21000
Copy link

sergey21000 commented Sep 28, 2024

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 sergey21000 changed the title Error when passing model to deepcopy in llama_cpp_python==0.3.0 Error when passing model to deepcopy in llama_cpp_python>=0.3.0 Oct 1, 2024
@gil-frenkel-HH
Copy link

gil-frenkel-HH commented Nov 27, 2024

Hi, any follow up on this?
E.g., why is it happening or why was this attribute removed?
thanks!!

@sergey21000
Copy link
Author

Hi, any follow up on this? E.g., why is it happening or why was this attribute removed? thanks!!

It seems they forgot to add it here
https://github.com/abetlen/llama-cpp-python/blob/main/llama_cpp/llama.py#L312

self.context_params = llama_cpp.llama_context_default_params()
self.context_params.n_ctx = n_ctx

need to add it like this

self.context_params = llama_cpp.llama_context_default_params()
self.context_params.seed = seed
self.context_params.n_ctx = n_ctx

@sergey21000
Copy link
Author

Fixed in version 0.3.3

pip install "llama-cpp-python>=0.3.3"

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

Successfully merging a pull request may close this issue.

2 participants