Skip to content

modeling_phi3 errors with AttributeError: 'DynamicCache' object has no attribute 'get_max_length' #36071

Closed

Description

@doctorpangloss

System Info

  • transformers version: 4.49.0.dev0 (315a9f4~1)
  • Platform: Windows-10-10.0.20348-SP0
  • Python version: 3.11.7
  • Huggingface_hub version: 0.28.1
  • Safetensors version: 0.5.2
  • Accelerate version: 1.3.0
  • Accelerate config: not found
  • PyTorch version (GPU?): 2.6.0+cu124 (True)
  • Tensorflow version (GPU?): not installed (NA)
  • Flax version (CPU?/GPU?/TPU?): 0.10.2 (cpu)
  • Jax version: 0.5.0
  • JaxLib version: 0.5.0
  • Using distributed or parallel set-up in script?: no
  • Using GPU in script?: no
  • GPU type: NVIDIA RTX A5000

Who can help?

@ArthurZucker

Information

  • The official example scripts
  • My own modified scripts

Tasks

  • An officially supported task in the examples folder (such as GLUE/SQuAD, ...)
  • My own task or dataset (give details below)

Reproduction

Use Phi3 with any cache configuration, including default (DynamicCache)

I think get_max_length is probably declared on a mixin that isn't on the cache classes yet?

comfy_extras\nodes\nodes_language.py:361: in execute
    return model.generate(tokens, max_new_tokens, repetition_penalty, seed, sampler),
comfy\language\transformers_model_management.py:228: in generate
    output_ids = transformers_model.generate(
..\..\.venv\Lib\site-packages\torch\utils\_contextlib.py:116: in decorate_context
    return func(*args, **kwargs)
..\..\.venv\Lib\site-packages\transformers\generation\utils.py:2224: in generate
    result = self._sample(
..\..\.venv\Lib\site-packages\transformers\generation\utils.py:3198: in _sample
    model_inputs = self.prepare_inputs_for_generation(input_ids, **model_kwargs)
C:\Users\bberman\.cache\huggingface\modules\transformers_modules\c1358f8a35e6d2af81890deffbbfa575b978c62f\modeling_phi3.py:1292: in prepare_inputs_for_generation
    max_cache_length = past_key_values.get_max_length()

Expected behavior

related to #35168?

I'm not sure why this is only coming up with phi-3 so far

Activity

  1. zucchini-nlp commented on Feb 7, 2025

    @zucchini-nlp
    Member

    Hey @doctorpangloss !

    The get_max_length() has been deprecated for several releases and is removed in the last v4.48. It should be cache.get_max_cache_shape().

    Since the source of error is the remote code stored on hub, you need to open an issue in model repo and ask authors to fix that. Also you can downgrade transformers version a a workaround

  2. jillianmclements commented on Feb 8, 2025

    @jillianmclements

    I need to upgrade to v4.49 to use GRPO, so downgrading the transformers version isn't an option. Since the issue is with the HF hub's modeling_phi3.py, is there an easy way to get it to use the transformers' modeling_phi3.py instead (where it looks like the issue is fixed)? My current workaround is to download the transformers version and modify it to use absolute imports instead of relative imports.

  3. zucchini-nlp commented on Feb 9, 2025

    @zucchini-nlp
    Member

    @doctorpangloss you can set trust_remote_code=False if you are using one of the supported checkpoints. Check out #32365 (comment)

  4. kylesayrs commented on Feb 12, 2025

    @kylesayrs
    Contributor

    Unfortunately the same workaround doesn't seem to exist for phi3_v. @zucchini-nlp Are you aware of any limitations that would prevent a HF-maintained version of this model as well? I'd normally open a PR on HF, but the project hasn't been updated in 7 months.

  5. zucchini-nlp commented on Feb 12, 2025

    @zucchini-nlp
    Member

    @kylesayrs yeah, we didn't ship Phi3-Vision to transformers and it is supported only by hub code. You can still run it by cloning the repo and changing the problematic line. Not sure if authors are actively updating the model code

  6. alothomas commented on Feb 13, 2025

    @alothomas

    Hey I have the same error, with Phi2 backbone, did you manage to fix it ?

  7. ArthurZucker commented on Feb 13, 2025

    @ArthurZucker
    Collaborator

    @kylesayrs limitation is just time 🤣 happy to have it in transformers

  8. github-actions commented on Mar 10, 2025

    @github-actions
    Contributor

    This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

    Please note that issues that do not follow the contributing guidelines are likely to be ignored.

  9. Rocketknight1 commented on Mar 10, 2025

    @Rocketknight1
    Member

    We currently have a user PR to add Phi-3.5-vision to the main library, which should resolve this: #36036

  10. jranaraki commented on Mar 13, 2025

    @jranaraki

    System Info

    • transformers version: 4.49.0.dev0 (315a9f4~1)
    • Platform: Windows-10-10.0.20348-SP0
    • Python version: 3.11.7
    • Huggingface_hub version: 0.28.1
    • Safetensors version: 0.5.2
    • Accelerate version: 1.3.0
    • Accelerate config: not found
    • PyTorch version (GPU?): 2.6.0+cu124 (True)
    • Tensorflow version (GPU?): not installed (NA)
    • Flax version (CPU?/GPU?/TPU?): 0.10.2 (cpu)
    • Jax version: 0.5.0
    • JaxLib version: 0.5.0
    • Using distributed or parallel set-up in script?: no
    • Using GPU in script?: no
    • GPU type: NVIDIA RTX A5000

    Who can help?

    @ArthurZucker

    Information

    • The official example scripts[ ] My own modified scripts

    Tasks

    • An officially supported task in the examples folder (such as GLUE/SQuAD, ...)[x] My own task or dataset (give details below)

    Reproduction

    Use Phi3 with any cache configuration, including default (DynamicCache)

    I think get_max_length is probably declared on a mixin that isn't on the cache classes yet?

    comfy_extras\nodes\nodes_language.py:361: in execute
        return model.generate(tokens, max_new_tokens, repetition_penalty, seed, sampler),
    comfy\language\transformers_model_management.py:228: in generate
        output_ids = transformers_model.generate(
    ..\..\.venv\Lib\site-packages\torch\utils\_contextlib.py:116: in decorate_context
        return func(*args, **kwargs)
    ..\..\.venv\Lib\site-packages\transformers\generation\utils.py:2224: in generate
        result = self._sample(
    ..\..\.venv\Lib\site-packages\transformers\generation\utils.py:3198: in _sample
        model_inputs = self.prepare_inputs_for_generation(input_ids, **model_kwargs)
    C:\Users\bberman\.cache\huggingface\modules\transformers_modules\c1358f8a35e6d2af81890deffbbfa575b978c62f\modeling_phi3.py:1292: in prepare_inputs_for_generation
        max_cache_length = past_key_values.get_max_length()
    

    Expected behavior

    related to #35168?

    I'm not sure why this is only coming up with phi-3 so far

    Updating the line 1292 of modeling_phi3.py from max_cache_length = past_key_values.get_max_length() to max_cache_length = past_key_values.get_max_cache_shape() seems to be fixing the issue.

  11. kevalmorabia97 commented on Mar 20, 2025

    @kevalmorabia97
  12. github-actions commented on Apr 14, 2025

    @github-actions
    Contributor

    This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

    Please note that issues that do not follow the contributing guidelines are likely to be ignored.

  13. Efim27 commented on Jun 7, 2025

    @Efim27

    Just use:
    pip install transformers==4.47.0

  14. leonmayer commented on Jun 17, 2025

    @leonmayer

    Setting use_cache=False in the generation_args fixes the issue for me. It slows everything down, however.

  15. chipwilson commented on Jul 23, 2025

    @chipwilson

    Just use: pip install transformers==4.47.0

    I tried pip install transformers==4.47.0 and restarted the kernel, but it did not fix the error for me, but adding use_cache=False to my Pipeline args did fix it.

  16. KanishkNoir commented on Dec 17, 2025

    @KanishkNoir

    Setting use_cache=False in the generation_args fixes the issue for me. It slows everything down, however.

    hey did, you find the workaround for this issue, i am running in same problem and currently doing same in generation_args to fix but latency is my concern

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions