divergent-beamsearch 0.2.1__py3-none-any.whl → 0.2.3__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.
- divergent_beamsearch/algorithm.py +9 -3
- {divergent_beamsearch-0.2.1.dist-info → divergent_beamsearch-0.2.3.dist-info}/METADATA +2 -2
- divergent_beamsearch-0.2.3.dist-info/RECORD +6 -0
- divergent_beamsearch-0.2.1.dist-info/RECORD +0 -6
- {divergent_beamsearch-0.2.1.dist-info → divergent_beamsearch-0.2.3.dist-info}/WHEEL +0 -0
- {divergent_beamsearch-0.2.1.dist-info → divergent_beamsearch-0.2.3.dist-info}/licenses/LICENCE +0 -0
@@ -1,4 +1,5 @@
|
|
1
1
|
import math
|
2
|
+
import multi_choices_parser
|
2
3
|
import torch
|
3
4
|
try:
|
4
5
|
from transformers import GPT2LMHeadModel
|
@@ -74,6 +75,7 @@ class AcceptEverythingParser(Parser):
|
|
74
75
|
def __init__(self, vocab_size : int):
|
75
76
|
self.vocab_size = vocab_size
|
76
77
|
self.tokens = tuple(range(vocab_size))
|
78
|
+
self.finished = False
|
77
79
|
|
78
80
|
def step(self, token):
|
79
81
|
pass
|
@@ -163,9 +165,11 @@ def divergent_beamsearch(input_ids : torch.Tensor, model : "GPT2LMHeadModel", be
|
|
163
165
|
input_ids_unfinished = torch.cat([input_ids_unfinished[best_tokens_row], best_tokens.unsqueeze(-1)], dim=-1)
|
164
166
|
scores_unfinished = scores_unfinished[best_tokens_row] + best_tokens_logprobs
|
165
167
|
solutions_unfinished = torch.cat([solutions_unfinished[best_tokens_row], best_tokens.unsqueeze(-1)], dim=-1)
|
168
|
+
best_tokens_row = best_tokens_row.tolist()
|
166
169
|
parsers_unfinished = [parsers_unfinished[row].copy() for row in best_tokens_row]
|
167
|
-
for parser, token in zip(parsers_unfinished, best_tokens.tolist()):
|
168
|
-
parser.
|
170
|
+
for parser, token, row in zip(parsers_unfinished, best_tokens.tolist(), best_tokens_row):
|
171
|
+
if not parser.finished:
|
172
|
+
parser.step(token)
|
169
173
|
|
170
174
|
# Special case of vanilla beam search where all answers are valid
|
171
175
|
# Warning : In this case model will not stop on end_of_sentence token
|
@@ -187,6 +191,7 @@ def divergent_logprob(input_ids : torch.Tensor, attention_mask : torch.Tensor |
|
|
187
191
|
parsers : Parser | list[Parser] | None, batch_size=32,
|
188
192
|
start : int | torch.IntTensor = None, end_symb=DEFAULT_END_SYMB, optimize_gpu_mem=True) -> torch.FloatTensor:
|
189
193
|
if start is None:
|
194
|
+
# Start at 1 because first token logprobs cannot be computed
|
190
195
|
start = 1
|
191
196
|
if isinstance(start, int):
|
192
197
|
start = torch.tensor([start]*input_ids.shape[0])
|
@@ -225,8 +230,9 @@ def divergent_logprob(input_ids : torch.Tensor, attention_mask : torch.Tensor |
|
|
225
230
|
for input_id, att in zip(input_ids[i, start:].tolist(), attention_mask[i, start:].tolist()):
|
226
231
|
if not att:
|
227
232
|
break
|
233
|
+
assert not parser.finished
|
228
234
|
parser.step(input_id)
|
229
|
-
next_tokens =
|
235
|
+
next_tokens = parser.next()
|
230
236
|
try:
|
231
237
|
next_tokens.remove(end_symb)
|
232
238
|
except ValueError:
|
@@ -1,10 +1,10 @@
|
|
1
1
|
Metadata-Version: 2.4
|
2
2
|
Name: divergent-beamsearch
|
3
|
-
Version: 0.2.
|
3
|
+
Version: 0.2.3
|
4
4
|
Summary: A variant of the beam search algorithm that focuses on finding answers that maximize the probability of generating an answer before diverging into another subject.
|
5
5
|
License-File: LICENCE
|
6
6
|
Requires-Python: >=3.11
|
7
|
-
Requires-Dist: multi-choices-parser>=0.
|
7
|
+
Requires-Dist: multi-choices-parser>=0.10.0
|
8
8
|
Requires-Dist: torch>=2.0.0
|
9
9
|
Description-Content-Type: text/markdown
|
10
10
|
|
@@ -0,0 +1,6 @@
|
|
1
|
+
divergent_beamsearch/__init__.py,sha256=qrpVRoT3d-q1N9fJnzHI2X13e71LDY4-6eLOQ_gwCqQ,62
|
2
|
+
divergent_beamsearch/algorithm.py,sha256=n41WYmcT9UwuCBHbdKm3-SgO3NgJBenTlueVxpF1dV8,10234
|
3
|
+
divergent_beamsearch-0.2.3.dist-info/METADATA,sha256=o33zn3fuy96QWws-0ALVXo9euVKug5vXbtWDZ4eF6t8,2790
|
4
|
+
divergent_beamsearch-0.2.3.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
5
|
+
divergent_beamsearch-0.2.3.dist-info/licenses/LICENCE,sha256=gnISbTzmuQC7NwJaGOdjoq26QYgSuKndq5q2JykifKw,1075
|
6
|
+
divergent_beamsearch-0.2.3.dist-info/RECORD,,
|
@@ -1,6 +0,0 @@
|
|
1
|
-
divergent_beamsearch/__init__.py,sha256=qrpVRoT3d-q1N9fJnzHI2X13e71LDY4-6eLOQ_gwCqQ,62
|
2
|
-
divergent_beamsearch/algorithm.py,sha256=GKFwi6aKNmJRu9SR6X96JT93SbOpy84fxyKJ5Pq5vQs,9961
|
3
|
-
divergent_beamsearch-0.2.1.dist-info/METADATA,sha256=0JAVae-tlHYFQkaEqBOE9ZDtExKsS-gpFFFb9oNTRdg,2790
|
4
|
-
divergent_beamsearch-0.2.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
5
|
-
divergent_beamsearch-0.2.1.dist-info/licenses/LICENCE,sha256=gnISbTzmuQC7NwJaGOdjoq26QYgSuKndq5q2JykifKw,1075
|
6
|
-
divergent_beamsearch-0.2.1.dist-info/RECORD,,
|
File without changes
|
{divergent_beamsearch-0.2.1.dist-info → divergent_beamsearch-0.2.3.dist-info}/licenses/LICENCE
RENAMED
File without changes
|