multimodalart HF Staff commited on
Commit
db1dccd
·
verified ·
1 Parent(s): 19b83f5

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. app.py +38 -3
  2. requirements.txt +1 -1
app.py CHANGED
@@ -5,6 +5,7 @@ import spaces
5
  import torch
6
  import torch.nn.functional as F
7
  import gradio as gr
 
8
  from transformers import AutoModelForCausalLM, AutoTokenizer
9
 
10
  MODEL_ID = "WhirlwindAI/Arithmetic-SLM"
@@ -16,12 +17,46 @@ CTX_LEN = 2048
16
 
17
  STOP_STRINGS = [IM_END, IM_START]
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  # ---------------------------------------------------------------------------
20
  # Load model + tokenizer once, at module scope, moved eagerly to CUDA so
21
  # ZeroGPU can pack the weights and stream them into VRAM on the first call.
22
- # The model uses custom code (TinyGPTForCausalLM) with the pure-torch attention
23
- # backend (config: attention_backend="torch", torch_fallback=True) so no flash
24
- # kernels are needed at runtime.
25
  # ---------------------------------------------------------------------------
26
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
27
  model = AutoModelForCausalLM.from_pretrained(
 
5
  import torch
6
  import torch.nn.functional as F
7
  import gradio as gr
8
+ import transformers.modeling_utils as _mu
9
  from transformers import AutoModelForCausalLM, AutoTokenizer
10
 
11
  MODEL_ID = "WhirlwindAI/Arithmetic-SLM"
 
17
 
18
  STOP_STRINGS = [IM_END, IM_START]
19
 
20
+ # ---------------------------------------------------------------------------
21
+ # Compatibility shim: the model ships custom code (TinyGPTForCausalLM) written
22
+ # for transformers 4.57.x, whose custom `tie_weights(self)` takes no kwargs.
23
+ # transformers 5.x calls `self.tie_weights(recompute_mapping=False)` from
24
+ # `init_weights`, which raises TypeError on the custom override. We can't pin
25
+ # transformers 4.x (its huggingface-hub<1.0 conflicts with the Gradio 6 SDK's
26
+ # hub>=1.2), so instead we make the base `tie_weights` swallow the new kwarg
27
+ # for any subclass override that doesn't accept it. This does not change the
28
+ # author's inference logic — the model has tied embeddings either way.
29
+ # ---------------------------------------------------------------------------
30
+ _orig_init_weights = _mu.PreTrainedModel.init_weights
31
+
32
+
33
+ def _safe_init_weights(self, *args, **kwargs):
34
+ _tie = self.tie_weights
35
+
36
+ def _tolerant_tie(*a, **k):
37
+ try:
38
+ return _tie(*a, **k)
39
+ except TypeError:
40
+ return _tie()
41
+
42
+ self.tie_weights = _tolerant_tie
43
+ try:
44
+ return _orig_init_weights(self, *args, **kwargs)
45
+ finally:
46
+ try:
47
+ del self.tie_weights
48
+ except AttributeError:
49
+ pass
50
+
51
+
52
+ _mu.PreTrainedModel.init_weights = _safe_init_weights
53
+
54
  # ---------------------------------------------------------------------------
55
  # Load model + tokenizer once, at module scope, moved eagerly to CUDA so
56
  # ZeroGPU can pack the weights and stream them into VRAM on the first call.
57
+ # The model uses the pure-torch attention backend (config:
58
+ # attention_backend="torch", torch_fallback=True) so no flash kernels are
59
+ # needed at runtime.
60
  # ---------------------------------------------------------------------------
61
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
62
  model = AutoModelForCausalLM.from_pretrained(
requirements.txt CHANGED
@@ -1,2 +1,2 @@
1
- transformers==4.57.1
2
  safetensors
 
1
+ transformers
2
  safetensors