Harley-ml commited on
Commit
849c8d6
·
verified ·
1 Parent(s): 113830c

Upload 4 files

Browse files
config.json CHANGED
@@ -17,6 +17,16 @@
17
  "longest_word_chars",
18
  "vocab_size"
19
  ],
 
 
 
 
 
 
 
 
 
 
20
  "feature_dim": 19,
21
  "num_layers": 8,
22
  "hidden_dim": 32,
@@ -27,5 +37,47 @@
27
  "use_log1p_features": true,
28
  "use_log1p_target": false,
29
  "center_target": false,
30
- "target_offset": 0.0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  }
 
17
  "longest_word_chars",
18
  "vocab_size"
19
  ],
20
+ "base_feature_names": [
21
+ "chars",
22
+ "words",
23
+ "avg_chars_per_word",
24
+ "punctuation_ratio",
25
+ "symbol_ratio",
26
+ "longest_word_chars",
27
+ "vocab_size"
28
+ ],
29
+ "base_feature_dim": 7,
30
  "feature_dim": 19,
31
  "num_layers": 8,
32
  "hidden_dim": 32,
 
37
  "use_log1p_features": true,
38
  "use_log1p_target": false,
39
  "center_target": false,
40
+ "target_offset": 0.0,
41
+ "feature_mean": [
42
+ 4971.99609375,
43
+ 750.8612670898438,
44
+ 5.387211799621582,
45
+ 0.038271043449640274,
46
+ 0.00981982797384262,
47
+ 24.875553131103516,
48
+ 62374.8046875,
49
+ 6.58632230758667,
50
+ 0.15978458523750305,
51
+ 7.920589923858643,
52
+ 6.06857967376709,
53
+ 9.939688682556152,
54
+ 216.70751953125,
55
+ 61.361202239990234,
56
+ 4028.412353515625,
57
+ 29.0120792388916,
58
+ 1.2395837306976318,
59
+ 32.29179382324219,
60
+ 278.0736999511719
61
+ ],
62
+ "feature_std": [
63
+ 10841.10546875,
64
+ 1556.9542236328125,
65
+ 1.7252269983291626,
66
+ 0.028954673558473587,
67
+ 0.020614376291632652,
68
+ 540.275390625,
69
+ 76498.015625,
70
+ 2.147888660430908,
71
+ 0.028698621317744255,
72
+ 0.9891781806945801,
73
+ 0.963409960269928,
74
+ 1.7922862768173218,
75
+ 818.908935546875,
76
+ 420.1692199707031,
77
+ 8589.4033203125,
78
+ 90.29910278320312,
79
+ 5.135631084442139,
80
+ 561.3566284179688,
81
+ 1134.063232421875
82
+ ]
83
  }
configuration_jetoncount.py CHANGED
@@ -9,6 +9,8 @@ class JetonCountConfig(PretrainedConfig):
9
  def __init__(
10
  self,
11
  feature_names=None,
 
 
12
  feature_dim=19,
13
  num_layers=8,
14
  hidden_dim=32,
@@ -20,9 +22,21 @@ class JetonCountConfig(PretrainedConfig):
20
  use_log1p_target=False,
21
  center_target=False,
22
  target_offset=0.0,
 
 
23
  **kwargs,
24
  ):
25
- self.feature_names = feature_names or []
 
 
 
 
 
 
 
 
 
 
26
  self.feature_dim = int(feature_dim)
27
  self.num_layers = int(num_layers)
28
  self.hidden_dim = int(hidden_dim)
@@ -34,4 +48,6 @@ class JetonCountConfig(PretrainedConfig):
34
  self.use_log1p_target = bool(use_log1p_target)
35
  self.center_target = bool(center_target)
36
  self.target_offset = float(target_offset)
 
 
37
  super().__init__(**kwargs)
 
9
  def __init__(
10
  self,
11
  feature_names=None,
12
+ base_feature_names=None,
13
+ base_feature_dim=7,
14
  feature_dim=19,
15
  num_layers=8,
16
  hidden_dim=32,
 
22
  use_log1p_target=False,
23
  center_target=False,
24
  target_offset=0.0,
25
+ feature_mean=None,
26
+ feature_std=None,
27
  **kwargs,
28
  ):
29
+ self.base_feature_names = base_feature_names or [
30
+ "chars",
31
+ "words",
32
+ "avg_chars_per_word",
33
+ "punctuation_ratio",
34
+ "symbol_ratio",
35
+ "longest_word_chars",
36
+ "vocab_size",
37
+ ]
38
+ self.feature_names = feature_names or list(self.base_feature_names)
39
+ self.base_feature_dim = int(base_feature_dim)
40
  self.feature_dim = int(feature_dim)
41
  self.num_layers = int(num_layers)
42
  self.hidden_dim = int(hidden_dim)
 
48
  self.use_log1p_target = bool(use_log1p_target)
49
  self.center_target = bool(center_target)
50
  self.target_offset = float(target_offset)
51
+ self.feature_mean = feature_mean
52
+ self.feature_std = feature_std
53
  super().__init__(**kwargs)
model.safetensors CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:4c048d0728fd46f7bff885866ffdcbe742789ff633eb7fb6468cf11f5fda3a34
3
- size 29148
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f8871d7515ae031717c953aa4008f2ebd6d5b4d2f04ed8c15b6f61f7db11628c
3
+ size 29276
modeling_jetoncount.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  from __future__ import annotations
2
 
3
  from dataclasses import dataclass
@@ -32,8 +34,8 @@ def _get_activation(name: str) -> nn.Module:
32
 
33
  def _engineer_features_tensor(base: torch.Tensor) -> torch.Tensor:
34
  """
35
- base must be shape [B, 7] or [7].
36
- Output is shape [B, 19] or [19].
37
  Matches train_mlp_token_regressor.py.
38
  """
39
  squeeze = False
@@ -52,34 +54,20 @@ def _engineer_features_tensor(base: torch.Tensor) -> torch.Tensor:
52
  vocab_size = base[:, 6]
53
 
54
  eps = 1e-6
55
- ratio_chars_words = chars / torch.clamp(words, min=1.0)
56
- ratio_words_chars = words / torch.clamp(chars, min=1.0)
57
- log_chars = torch.log1p(torch.clamp(chars, min=0.0))
58
- log_words = torch.log1p(torch.clamp(words, min=0.0))
59
- log_vocab = torch.log1p(torch.clamp(vocab_size, min=0.0))
60
-
61
- chars_x_punct = chars * punctuation_ratio
62
- chars_x_symbol = chars * symbol_ratio
63
- words_x_avg = words * avg_chars_per_word
64
- words_x_punct = words * punctuation_ratio
65
- longest_x_punct = longest_word_chars * punctuation_ratio
66
- complexity_proxy = (avg_chars_per_word + longest_word_chars) * (1.0 + punctuation_ratio + symbol_ratio)
67
- density_proxy = (chars + eps) * (punctuation_ratio + symbol_ratio + eps)
68
-
69
  extra = torch.stack(
70
  [
71
- ratio_chars_words,
72
- ratio_words_chars,
73
- log_chars,
74
- log_words,
75
- log_vocab,
76
- chars_x_punct,
77
- chars_x_symbol,
78
- words_x_avg,
79
- words_x_punct,
80
- longest_x_punct,
81
- complexity_proxy,
82
- density_proxy,
83
  ],
84
  dim=-1,
85
  )
@@ -130,9 +118,8 @@ class JetonCountForRegression(PreTrainedModel):
130
  if not self.config.standardize_features:
131
  return x
132
 
133
- mean = self.config.feature_mean
134
- std = self.config.feature_std
135
-
136
  if mean is None or std is None:
137
  return x
138
 
@@ -165,24 +152,21 @@ class JetonCountForRegression(PreTrainedModel):
165
  def _remap_state_dict_keys(self, state_dict):
166
  """
167
  Accepts several historical layouts:
168
- - 0.weight / 0.bias
169
- - net.0.weight / net.0.bias
170
- - mlp.net.0.weight / mlp.net.0.bias
171
  """
172
  if not state_dict:
173
  return state_dict
174
 
175
- if any(k.startswith("mlp.net.") or k.startswith("net.") for k in state_dict):
176
- return state_dict
177
-
178
  remapped = {}
179
  for k, v in state_dict.items():
180
  if k.startswith("mlp.net."):
181
- remapped[k[len("mlp.") :]] = v # -> net.0.weight
182
- elif k.startswith("mlp."):
183
- remapped[k[len("mlp.") :]] = v
184
- elif k[0].isdigit():
185
- remapped[f"net.{k}"] = v
186
  else:
187
  remapped[k] = v
188
  return remapped
@@ -210,4 +194,4 @@ class JetonCountForRegression(PreTrainedModel):
210
  labels = labels.to(logits.dtype)
211
  loss = torch.nn.functional.mse_loss(logits, labels)
212
 
213
- return RegressionOutput(loss=loss, logits=logits)
 
1
+ """JetonCount MLP regression model."""
2
+
3
  from __future__ import annotations
4
 
5
  from dataclasses import dataclass
 
34
 
35
  def _engineer_features_tensor(base: torch.Tensor) -> torch.Tensor:
36
  """
37
+ base shape: [7] or [B, 7]
38
+ output shape: [19] or [B, 19]
39
  Matches train_mlp_token_regressor.py.
40
  """
41
  squeeze = False
 
54
  vocab_size = base[:, 6]
55
 
56
  eps = 1e-6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  extra = torch.stack(
58
  [
59
+ chars / torch.clamp(words, min=1.0),
60
+ words / torch.clamp(chars, min=1.0),
61
+ torch.log1p(torch.clamp(chars, min=0.0)),
62
+ torch.log1p(torch.clamp(words, min=0.0)),
63
+ torch.log1p(torch.clamp(vocab_size, min=0.0)),
64
+ chars * punctuation_ratio,
65
+ chars * symbol_ratio,
66
+ words * avg_chars_per_word,
67
+ words * punctuation_ratio,
68
+ longest_word_chars * punctuation_ratio,
69
+ (avg_chars_per_word + longest_word_chars) * (1.0 + punctuation_ratio + symbol_ratio),
70
+ (chars + eps) * (punctuation_ratio + symbol_ratio + eps),
71
  ],
72
  dim=-1,
73
  )
 
118
  if not self.config.standardize_features:
119
  return x
120
 
121
+ mean = getattr(self.config, "feature_mean", None)
122
+ std = getattr(self.config, "feature_std", None)
 
123
  if mean is None or std is None:
124
  return x
125
 
 
152
  def _remap_state_dict_keys(self, state_dict):
153
  """
154
  Accepts several historical layouts:
155
+ - mlp.net.0.weight
156
+ - net.0.weight
157
+ - 0.weight
158
  """
159
  if not state_dict:
160
  return state_dict
161
 
 
 
 
162
  remapped = {}
163
  for k, v in state_dict.items():
164
  if k.startswith("mlp.net."):
165
+ remapped[k] = v
166
+ elif k.startswith("net."):
167
+ remapped[f"mlp.{k}"] = v
168
+ elif k and k[0].isdigit():
169
+ remapped[f"mlp.net.{k}"] = v
170
  else:
171
  remapped[k] = v
172
  return remapped
 
194
  labels = labels.to(logits.dtype)
195
  loss = torch.nn.functional.mse_loss(logits, labels)
196
 
197
+ return RegressionOutput(loss=loss, logits=logits)