autoregressive-language-model-generate 0.1.0a0__py2.py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- autoregressive_language_model_generate-0.1.0a0.dist-info/METADATA +66 -0
- autoregressive_language_model_generate-0.1.0a0.dist-info/RECORD +6 -0
- autoregressive_language_model_generate-0.1.0a0.dist-info/WHEEL +6 -0
- autoregressive_language_model_generate-0.1.0a0.dist-info/licenses/LICENSE +21 -0
- autoregressive_language_model_generate-0.1.0a0.dist-info/top_level.txt +1 -0
- autoregressive_language_model_generate.py +53 -0
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: autoregressive-language-model-generate
|
|
3
|
+
Version: 0.1.0a0
|
|
4
|
+
Summary: A generator-based, stateless autoregressive inference loop for language models compatible with HuggingFace's Transformers API.
|
|
5
|
+
Author-email: Jifeng Wu <jifengwu2k@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/jifengwu2k/autoregressive-language-model-generate
|
|
8
|
+
Project-URL: Bug Tracker, https://github.com/jifengwu2k/autoregressive-language-model-generate/issues
|
|
9
|
+
Classifier: Programming Language :: Python :: 2
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Requires-Python: >=2
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
Requires-Dist: torch
|
|
16
|
+
Requires-Dist: typing; python_version < "3.5"
|
|
17
|
+
Dynamic: license-file
|
|
18
|
+
|
|
19
|
+
# `autoregressive-language-model-generate`
|
|
20
|
+
|
|
21
|
+
A generator-based, stateless autoregressive inference loop for language models compatible with HuggingFace's Transformers API. At each step, it yields logits from the model and expects the caller to send back the predicted next tokens. Easily integrates into custom sampling strategies (greedy, beam, top-k/p, etc).
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
Assume you have:
|
|
26
|
+
|
|
27
|
+
- `model`
|
|
28
|
+
- `input_ids` and `attention_mask`, shape `(batch_size, seq_len)`
|
|
29
|
+
|
|
30
|
+
```python
|
|
31
|
+
import torch
|
|
32
|
+
from autoregressive_language_model_generate import autoregressive_language_model_generate
|
|
33
|
+
|
|
34
|
+
model = ...
|
|
35
|
+
input_ids = ...
|
|
36
|
+
attention_mask = ...
|
|
37
|
+
|
|
38
|
+
gen = autoregressive_language_model_generate(
|
|
39
|
+
model,
|
|
40
|
+
input_ids,
|
|
41
|
+
attention_mask
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
logits = next(gen)
|
|
45
|
+
|
|
46
|
+
# Implement your sampling logic here
|
|
47
|
+
next_token_logits = logits[:, -1, :]
|
|
48
|
+
top_k = 50
|
|
49
|
+
indices_to_remove = next_token_logits < torch.topk(next_token_logits, top_k)[0][..., -1, None]
|
|
50
|
+
next_token_scores = next_token_logits.masked_fill(indices_to_remove, -float('Inf'))
|
|
51
|
+
probs = torch.nn.functional.softmax(next_token_scores, dim=-1)
|
|
52
|
+
|
|
53
|
+
# `next_tokens` has shape `(batch_size,)`
|
|
54
|
+
next_tokens = torch.multinomial(probs, num_samples=1).squeeze(1)
|
|
55
|
+
|
|
56
|
+
# Send `next_tokens` to generator, receive `logits`
|
|
57
|
+
logits = gen.send(next_tokens)
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Contributing
|
|
61
|
+
|
|
62
|
+
Contributions are welcome! Please submit pull requests or open issues on the GitHub repository.
|
|
63
|
+
|
|
64
|
+
## License
|
|
65
|
+
|
|
66
|
+
This project is licensed under the [MIT License](LICENSE).
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
autoregressive_language_model_generate.py,sha256=Rmw96uGVuaF1WujlbQTwzBo2gBR9NuAYnuxNxkEsSQA,2164
|
|
2
|
+
autoregressive_language_model_generate-0.1.0a0.dist-info/licenses/LICENSE,sha256=hvfX-ADssuMYgrXDUAjMMut4l8W3meA31ZAYfpdlJKY,1066
|
|
3
|
+
autoregressive_language_model_generate-0.1.0a0.dist-info/METADATA,sha256=y3hiny3mNOEwbhULoGXDjPFnt5-3eCTtrxgU7bp4rmU,2254
|
|
4
|
+
autoregressive_language_model_generate-0.1.0a0.dist-info/WHEEL,sha256=Q6xS052dXadQWXcEVKSI037R6NoyqhUlJ5BcYz2iMP4,110
|
|
5
|
+
autoregressive_language_model_generate-0.1.0a0.dist-info/top_level.txt,sha256=aUG3Zpl9vhYtzmVk7BvZc0vOJYy4_SMPwDdFBwQG2hM,39
|
|
6
|
+
autoregressive_language_model_generate-0.1.0a0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Jifeng Wu
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
autoregressive_language_model_generate
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# Copyright (c) 2026 Jifeng Wu
|
|
2
|
+
# Licensed under the MIT License. See LICENSE file in the project root for full license information.
|
|
3
|
+
from typing import Generator
|
|
4
|
+
import torch
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@torch.no_grad()
|
|
8
|
+
def autoregressive_language_model_generate(
|
|
9
|
+
model, # type: torch.nn.Module
|
|
10
|
+
input_ids, # type: torch.LongTensor
|
|
11
|
+
attention_mask, # type: torch.BoolTensor
|
|
12
|
+
):
|
|
13
|
+
# type: (...) -> Generator[torch.Tensor, torch.Tensor, None]
|
|
14
|
+
"""
|
|
15
|
+
A generator-based, stateless autoregressive inference loop for language models compatible with HuggingFace's Transformers API. At each step, it yields logits from the model and expects the caller to send back the predicted next tokens. Easily integrates into custom sampling strategies (greedy, beam, top-k/p, etc).
|
|
16
|
+
|
|
17
|
+
Args:
|
|
18
|
+
`model` (`torch.nn.Module`): A language model compatible with HuggingFace's Transformers API. Must accept `input_ids`, `attention_mask`, and `position_ids`.
|
|
19
|
+
`input_ids` (`torch.LongTensor`): Token indices to start generation. Shape `(batch_size, seq_len)`.
|
|
20
|
+
`attention_mask` (`torch.BoolTensor`): Attention mask indicating which indices are valid input (1) vs padding (0). Shape `(batch_size, seq_len)`.
|
|
21
|
+
|
|
22
|
+
Yields:
|
|
23
|
+
`torch.FloatTensor`: Logits from the model for next token prediction. Shape `(batch_size, seq_len, vocab_size)`.
|
|
24
|
+
|
|
25
|
+
Note:
|
|
26
|
+
The caller is responsible for sending back the predicted next tokens. Shape `(batch_size,)`.
|
|
27
|
+
"""
|
|
28
|
+
while True:
|
|
29
|
+
# Fully recompute position_ids for new length
|
|
30
|
+
position_ids = attention_mask.long().cumsum(-1) - 1
|
|
31
|
+
position_ids.masked_fill_(attention_mask == 0, 1)
|
|
32
|
+
|
|
33
|
+
logits = model(
|
|
34
|
+
input_ids=input_ids,
|
|
35
|
+
attention_mask=attention_mask,
|
|
36
|
+
position_ids=position_ids,
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
next_tokens = yield logits
|
|
40
|
+
|
|
41
|
+
input_ids = torch.cat(
|
|
42
|
+
[input_ids, next_tokens[:, None]],
|
|
43
|
+
dim=-1
|
|
44
|
+
)
|
|
45
|
+
attention_mask = torch.cat(
|
|
46
|
+
[
|
|
47
|
+
attention_mask,
|
|
48
|
+
attention_mask.new_ones(
|
|
49
|
+
(attention_mask.shape[0], 1)
|
|
50
|
+
)
|
|
51
|
+
],
|
|
52
|
+
dim=-1
|
|
53
|
+
)
|