Fix arithmetic generation cache default
Browse files- README.md +32 -1
- config.json +1 -0
- generation_config.json +2 -1
- model.py +4 -3
README.md
CHANGED
|
@@ -80,9 +80,34 @@ text = "12 + 34 ="
|
|
| 80 |
inputs = tokenizer(text, return_tensors="pt", add_special_tokens=False)
|
| 81 |
|
| 82 |
with torch.no_grad():
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
```
|
| 85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
## Evaluation
|
| 87 |
|
| 88 |
### ArithMark 2.0
|
|
@@ -121,6 +146,12 @@ implementation can score this slightly longer harness window; reduce batch size
|
|
| 121 |
or set `max_length` to the longest sequence found if a task variant contains
|
| 122 |
longer continuations.
|
| 123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
## Results
|
| 125 |
|
| 126 |
| Benchmark | Metric | Value |
|
|
|
|
| 80 |
inputs = tokenizer(text, return_tensors="pt", add_special_tokens=False)
|
| 81 |
|
| 82 |
with torch.no_grad():
|
| 83 |
+
output_ids = model.generate(
|
| 84 |
+
**inputs,
|
| 85 |
+
max_new_tokens=3,
|
| 86 |
+
do_sample=False,
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
print(tokenizer.decode(output_ids[0], skip_special_tokens=True))
|
| 90 |
+
# 12 + 34 = 46
|
| 91 |
```
|
| 92 |
|
| 93 |
+
### Generation Cache
|
| 94 |
+
|
| 95 |
+
Atom2.7m derives arithmetic `place_ids` and `role_ids` from the full token sequence. During arithmetic generation, result digits need those features to be recomputed from the full current prefix. For this reason, the checkpoint defaults to `use_cache=False` for `model.generate(...)`. This is slower than KV-cache generation, but preserves the arithmetic feature annotations used by the model.
|
| 96 |
+
|
| 97 |
+
You can opt into faster cached generation when exact arithmetic-aware generation is not required:
|
| 98 |
+
|
| 99 |
+
```python
|
| 100 |
+
output_ids = model.generate(
|
| 101 |
+
**inputs,
|
| 102 |
+
max_new_tokens=32,
|
| 103 |
+
use_cache=True,
|
| 104 |
+
)
|
| 105 |
+
```
|
| 106 |
+
|
| 107 |
+
Cached generation remains supported, but for arithmetic continuations it may
|
| 108 |
+
produce lower-quality results unless the caller supplies correctly updated
|
| 109 |
+
`place_ids` and `role_ids`.
|
| 110 |
+
|
| 111 |
## Evaluation
|
| 112 |
|
| 113 |
### ArithMark 2.0
|
|
|
|
| 146 |
or set `max_length` to the longest sequence found if a task variant contains
|
| 147 |
longer continuations.
|
| 148 |
|
| 149 |
+
For multiple-choice or benchmark-style evaluation, no special generation cache
|
| 150 |
+
setting is required. Log-likelihood scoring runs full `context + continuation`
|
| 151 |
+
forward passes, so arithmetic features are derived from the complete sequence.
|
| 152 |
+
This is the path used by the included ArithMark benchmark script and by
|
| 153 |
+
lm-evaluation-harness log-likelihood tasks.
|
| 154 |
+
|
| 155 |
## Results
|
| 156 |
|
| 157 |
| Benchmark | Metric | Value |
|
config.json
CHANGED
|
@@ -68,6 +68,7 @@
|
|
| 68 |
"role_vocab_size": 12,
|
| 69 |
"rope_theta": 5000.0,
|
| 70 |
"transformers_version": "4.57.6",
|
|
|
|
| 71 |
"use_place_embeddings": true,
|
| 72 |
"use_role_embeddings": true,
|
| 73 |
"vocab_size": 4096,
|
|
|
|
| 68 |
"role_vocab_size": 12,
|
| 69 |
"rope_theta": 5000.0,
|
| 70 |
"transformers_version": "4.57.6",
|
| 71 |
+
"use_cache": false,
|
| 72 |
"use_place_embeddings": true,
|
| 73 |
"use_role_embeddings": true,
|
| 74 |
"vocab_size": 4096,
|
generation_config.json
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
{
|
| 2 |
"_from_model_config": true,
|
| 3 |
-
"transformers_version": "4.57.6"
|
|
|
|
| 4 |
}
|
|
|
|
| 1 |
{
|
| 2 |
"_from_model_config": true,
|
| 3 |
+
"transformers_version": "4.57.6",
|
| 4 |
+
"use_cache": false
|
| 5 |
}
|
model.py
CHANGED
|
@@ -341,7 +341,8 @@ class GPTForCausalLM(GPTPreTrainedModel, GenerationMixin):
|
|
| 341 |
return embeddings
|
| 342 |
|
| 343 |
def prepare_inputs_for_generation(self, input_ids, past_key_values=None, attention_mask=None, **kwargs):
|
| 344 |
-
|
|
|
|
| 345 |
input_ids = input_ids[:, -1:]
|
| 346 |
if kwargs.get("place_ids") is not None:
|
| 347 |
kwargs["place_ids"] = kwargs["place_ids"][:, -1:]
|
|
@@ -352,8 +353,8 @@ class GPTForCausalLM(GPTPreTrainedModel, GenerationMixin):
|
|
| 352 |
"place_ids": kwargs.get("place_ids"),
|
| 353 |
"role_ids": kwargs.get("role_ids"),
|
| 354 |
"attention_mask": attention_mask,
|
| 355 |
-
"past_key_values": past_key_values,
|
| 356 |
-
"use_cache":
|
| 357 |
}
|
| 358 |
|
| 359 |
def _get_freqs_cis(self, seq_len, device):
|
|
|
|
| 341 |
return embeddings
|
| 342 |
|
| 343 |
def prepare_inputs_for_generation(self, input_ids, past_key_values=None, attention_mask=None, **kwargs):
|
| 344 |
+
use_cache = bool(kwargs.get("use_cache", False))
|
| 345 |
+
if use_cache and past_key_values is not None and past_key_values.get_seq_length() > 0:
|
| 346 |
input_ids = input_ids[:, -1:]
|
| 347 |
if kwargs.get("place_ids") is not None:
|
| 348 |
kwargs["place_ids"] = kwargs["place_ids"][:, -1:]
|
|
|
|
| 353 |
"place_ids": kwargs.get("place_ids"),
|
| 354 |
"role_ids": kwargs.get("role_ids"),
|
| 355 |
"attention_mask": attention_mask,
|
| 356 |
+
"past_key_values": past_key_values if use_cache else None,
|
| 357 |
+
"use_cache": use_cache,
|
| 358 |
}
|
| 359 |
|
| 360 |
def _get_freqs_cis(self, seq_len, device):
|