divergent-beamsearch 0.2.1__py3-none-any.whl → 0.2.2__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 +8 -3
- {divergent_beamsearch-0.2.1.dist-info → divergent_beamsearch-0.2.2.dist-info}/METADATA +2 -2
- divergent_beamsearch-0.2.2.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.2.dist-info}/WHEEL +0 -0
- {divergent_beamsearch-0.2.1.dist-info → divergent_beamsearch-0.2.2.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
|
@@ -163,9 +164,11 @@ def divergent_beamsearch(input_ids : torch.Tensor, model : "GPT2LMHeadModel", be
|
|
163
164
|
input_ids_unfinished = torch.cat([input_ids_unfinished[best_tokens_row], best_tokens.unsqueeze(-1)], dim=-1)
|
164
165
|
scores_unfinished = scores_unfinished[best_tokens_row] + best_tokens_logprobs
|
165
166
|
solutions_unfinished = torch.cat([solutions_unfinished[best_tokens_row], best_tokens.unsqueeze(-1)], dim=-1)
|
167
|
+
best_tokens_row = best_tokens_row.tolist()
|
166
168
|
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.
|
169
|
+
for parser, token, row in zip(parsers_unfinished, best_tokens.tolist(), best_tokens_row):
|
170
|
+
if not parser.finished:
|
171
|
+
parser.step(token)
|
169
172
|
|
170
173
|
# Special case of vanilla beam search where all answers are valid
|
171
174
|
# Warning : In this case model will not stop on end_of_sentence token
|
@@ -187,6 +190,7 @@ def divergent_logprob(input_ids : torch.Tensor, attention_mask : torch.Tensor |
|
|
187
190
|
parsers : Parser | list[Parser] | None, batch_size=32,
|
188
191
|
start : int | torch.IntTensor = None, end_symb=DEFAULT_END_SYMB, optimize_gpu_mem=True) -> torch.FloatTensor:
|
189
192
|
if start is None:
|
193
|
+
# Start at 1 because first token logprobs cannot be computed
|
190
194
|
start = 1
|
191
195
|
if isinstance(start, int):
|
192
196
|
start = torch.tensor([start]*input_ids.shape[0])
|
@@ -225,8 +229,9 @@ def divergent_logprob(input_ids : torch.Tensor, attention_mask : torch.Tensor |
|
|
225
229
|
for input_id, att in zip(input_ids[i, start:].tolist(), attention_mask[i, start:].tolist()):
|
226
230
|
if not att:
|
227
231
|
break
|
232
|
+
assert not parser.finished
|
228
233
|
parser.step(input_id)
|
229
|
-
next_tokens =
|
234
|
+
next_tokens = parser.next()
|
230
235
|
try:
|
231
236
|
next_tokens.remove(end_symb)
|
232
237
|
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.2
|
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=sfQSaOcnuN_Pyp69hGZbX66Rmg7V4aQqyUfT0Clltvs,10204
|
3
|
+
divergent_beamsearch-0.2.2.dist-info/METADATA,sha256=l9Wsb45vwcLSNMcsiTuGay5rq9o4TD_Pt0D7ZnipTTg,2790
|
4
|
+
divergent_beamsearch-0.2.2.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
5
|
+
divergent_beamsearch-0.2.2.dist-info/licenses/LICENCE,sha256=gnISbTzmuQC7NwJaGOdjoq26QYgSuKndq5q2JykifKw,1075
|
6
|
+
divergent_beamsearch-0.2.2.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.2.dist-info}/licenses/LICENCE
RENAMED
File without changes
|