SriRamanaYash commited on
Commit
8bd5532
Β·
verified Β·
1 Parent(s): 4092587

Update handler.py

Browse files

The shim adds back the single missing method (get_max_length) that transformers β‰₯ 4.38 removed, returning None which is safe β€” the internal code that calls it only uses the value for optional length checks, not critical logic.

Without cache (old): token 1 β†’ recompute all attention for N tokens
token 2 β†’ recompute all attention for N+1 tokens
token K β†’ recompute all attention for N+K tokens ← O(nΒ²)

With cache (new): token 1 β†’ compute, store KV in DynamicCache
token 2 β†’ only compute for new token, reuse cache ← O(n)

Files changed (1) hide show
  1. handler.py +6 -1
handler.py CHANGED
@@ -6,7 +6,12 @@ Fix: DynamicCache.get_max_length() removed in transformers>=4.38.
6
  Solution: use_cache=False bypasses DynamicCache entirely.
7
  Slightly slower but fully compatible without any version pinning.
8
  """
 
 
9
 
 
 
 
10
  from transformers import AutoTokenizer, AutoModelForCausalLM
11
  import torch
12
 
@@ -66,7 +71,7 @@ class EndpointHandler:
66
  # use_cache=False bypasses DynamicCache entirely β€”
67
  # avoids get_max_length() removal in transformers>=4.38
68
  # without requiring any version pinning.
69
- use_cache=False,
70
  )
71
 
72
  if do_sample:
 
6
  Solution: use_cache=False bypasses DynamicCache entirely.
7
  Slightly slower but fully compatible without any version pinning.
8
  """
9
+ # ── DynamicCache compatibility shim (transformers >= 4.38) ──────────────────
10
+ from transformers import DynamicCache
11
 
12
+ if not hasattr(DynamicCache, "get_max_length"):
13
+ DynamicCache.get_max_length = lambda self: None
14
+ # ───────────────────────────────────────────────────────────────────────────
15
  from transformers import AutoTokenizer, AutoModelForCausalLM
16
  import torch
17
 
 
71
  # use_cache=False bypasses DynamicCache entirely β€”
72
  # avoids get_max_length() removal in transformers>=4.38
73
  # without requiring any version pinning.
74
+ # use_cache=False,
75
  )
76
 
77
  if do_sample: