divergent-beamsearch 0.1.0__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ from .algorithm import divergent_beamsearch
@@ -0,0 +1,112 @@
1
+ import math
2
+ import torch
3
+ from transformers import GPT2LMHeadModel
4
+ from multi_choices_parser import MultiChoicesParser, end_symb
5
+
6
+
7
+ def get_parsers_tokens(parsers : list[MultiChoicesParser]) -> tuple[list, list[int]]:
8
+ parsers_tokens = []
9
+ can_end = []
10
+ for parser in parsers:
11
+ tokens = list(parser.next())
12
+ if end_symb in tokens:
13
+ can_end.append(True)
14
+ tokens.remove(end_symb)
15
+ else:
16
+ can_end.append(False)
17
+ parsers_tokens.append(tokens)
18
+ return parsers_tokens, can_end
19
+
20
+ def apply_mask_tokens(pred : torch.Tensor, parsers_tokens):
21
+ mask = torch.ones_like(pred, dtype=torch.bool)
22
+ for tokens in parsers_tokens:
23
+ mask[:, tokens] = False
24
+ pred[mask] = -float('inf')
25
+ return pred[~pred.isinf().all(dim=-1)]
26
+
27
+
28
+ def batched_inference_logits(model : GPT2LMHeadModel, input_ids : torch.Tensor, batch_size : int = 32) -> torch.Tensor:
29
+ logits = []
30
+ for i in range(0, input_ids.shape[0], batch_size):
31
+ logits.append(model(input_ids[i:i+batch_size]).logits)
32
+ return torch.cat(logits, dim=0)
33
+
34
+ def select_mask(source : list, mask : list[bool]) -> list:
35
+ assert len(source) == len(mask)
36
+ return [x for x, m in zip(source, mask) if m]
37
+
38
+
39
+ def log1mexp(x: torch.Tensor) -> torch.Tensor:
40
+ """Numerically accurate evaluation of log(1 - exp(x)) for x < 0.
41
+ See [Maechler2012accurate]_ for details.
42
+ """
43
+ mask = -math.log(2) < x # x < 0
44
+ return torch.where(
45
+ mask,
46
+ (-x.expm1()).log(),
47
+ (-x.exp()).log1p(),
48
+ )
49
+
50
+ @torch.no_grad()
51
+ def divergent_beamsearch(input_ids : torch.Tensor, model : GPT2LMHeadModel, beam_size : int, max_length : int, multi_choices_parser : MultiChoicesParser, pad_token_id : int, batch_size=32, num_solutions = None) -> tuple[torch.Tensor, torch.Tensor]:
52
+ assert input_ids.shape[0] == 1, "Batch size must be 1"
53
+
54
+ if num_solutions is None:
55
+ num_solutions = beam_size
56
+
57
+ parsers_unfinished = [multi_choices_parser]
58
+ scores_finished = torch.tensor([], dtype=torch.float)
59
+ solutions_finished = torch.tensor([], dtype=torch.long).view(0,0)
60
+
61
+ input_ids_unfinished = input_ids
62
+ scores_unfinished = torch.tensor([0.0], dtype=torch.float)
63
+ solutions_unfinished = torch.tensor([], dtype=torch.long).view(1,0)
64
+
65
+
66
+ for _ in range(max_length):
67
+ if len(input_ids_unfinished) == 0:
68
+ break
69
+ pred = batched_inference_logits(model, input_ids_unfinished, batch_size)[:, -1].cpu()
70
+ parsers_tokens, can_end = get_parsers_tokens(parsers_unfinished)
71
+ # input_ids_unfinished = input_ids_unfinished[~torch.tensor(can_only_end)]
72
+ logprobs = torch.log_softmax(pred, dim=-1)
73
+ logprobs_filtered = apply_mask_tokens(logprobs, parsers_tokens)
74
+ if len(logprobs_filtered):
75
+ topk = torch.topk(logprobs_filtered, beam_size, dim=-1) # shape (batch_size, beam_size)
76
+ topk_global = topk.values.flatten().topk(beam_size)
77
+ best_tokens_row = topk_global.indices // beam_size
78
+ best_tokens, best_tokens_logprobs = topk.indices[best_tokens_row, topk_global.indices % beam_size], topk_global.values
79
+ notinf = ~best_tokens_logprobs.isinf()
80
+ best_tokens, best_tokens_row, best_tokens_logprobs = best_tokens[notinf], best_tokens_row[notinf], best_tokens_logprobs[notinf]
81
+ else:
82
+ best_tokens = torch.tensor([], dtype=torch.long)
83
+ best_tokens_row = torch.tensor([], dtype=torch.long)
84
+ best_tokens_logprobs = torch.tensor([], dtype=torch.float)
85
+
86
+
87
+ scores_finished_current = scores_unfinished[can_end]
88
+ solutions_finished_current = solutions_unfinished[can_end]
89
+ scores_finished_current = scores_finished_current + log1mexp(logprobs[can_end, select_mask(parsers_tokens, can_end)].logsumexp(dim=-1)).squeeze(-1)
90
+ scores_finished = torch.cat([scores_finished, scores_finished_current])
91
+ if len(solutions_finished_current):
92
+ pad = torch.full((len(scores_finished_current), solutions_finished_current.shape[1] - solutions_finished.shape[1]), pad_token_id, dtype=torch.long)
93
+ solutions_finished = torch.cat([solutions_finished.view(-1, solutions_finished_current.shape[1]+pad.shape[1]), torch.cat([solutions_finished_current, pad], dim=1)], dim=0)
94
+ if solutions_finished.numel():
95
+ # Keep num_solutions best solutions in finished
96
+ order = scores_finished.argsort(descending=True)
97
+ solutions_finished = solutions_finished[order][:num_solutions]
98
+ scores_finished = scores_finished[order][:num_solutions]
99
+
100
+
101
+ input_ids_unfinished = torch.cat([input_ids_unfinished[best_tokens_row], best_tokens.unsqueeze(-1)], dim=-1)
102
+ scores_unfinished = scores_unfinished[best_tokens_row] + best_tokens_logprobs
103
+ solutions_unfinished = torch.cat([solutions_unfinished[best_tokens_row], best_tokens.unsqueeze(-1)], dim=-1)
104
+ parsers_unfinished = [parsers_unfinished[row].copy() for row in best_tokens_row]
105
+ for parser, token in zip(parsers_unfinished, best_tokens.tolist()):
106
+ parser.step(token)
107
+
108
+ return scores_finished, solutions_finished
109
+
110
+
111
+
112
+
@@ -0,0 +1,78 @@
1
+ Metadata-Version: 2.4
2
+ Name: divergent-beamsearch
3
+ Version: 0.1.0
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
+ License-File: LICENCE
6
+ Requires-Python: >=3.11
7
+ Requires-Dist: multi-choices-parser>=0.9.57
8
+ Requires-Dist: torch>=2.5.1
9
+ Requires-Dist: transformers>=4.47.1
10
+ Description-Content-Type: text/markdown
11
+
12
+ # Divergent Beam Search
13
+ ## Overview
14
+
15
+ Divergent Beam Search is a variant of the beam search algorithm. Unlike the beam search where answers are constrained, which aims to find the answers with the highest probability of appearing, Divergent Beam Search focuses on finding answers that are not likely to be continued with another answer. Essentially, it finds the answers that maximize the probability of generating an answer before diverging into another subject given the prompt.
16
+
17
+ The core idea of this algorithm can be roughly summarized in the following optimization problem:
18
+
19
+ $$\max_{ans \in A} P(ans + diverging\ into\ another\ subject \mid prompt)$$
20
+
21
+ It is important that the set of answers $A$ is sufficiently exhaustive for this method to work. Otherwise, the algorithm could unjustifiably conclude that an answer is not being followed by the answer while this longer answer exists but is not included in the set $A$.
22
+
23
+ ## Installation
24
+
25
+ To install the package, use the following command:
26
+
27
+ ```bash
28
+ pip install divergent-beamsearch
29
+ ```
30
+
31
+ ## Usage
32
+
33
+ Here's a brief example of how to use `divergent-beamsearch`:
34
+
35
+ ```python
36
+ import torch
37
+ from transformers import GPT2LMHeadModel, GPT2Tokenizer
38
+ from multi_choices_parser import MultiChoicesParser
39
+ from divergent_beamsearch import divergent_beamsearch
40
+
41
+ # Load model and tokenizer
42
+ model = GPT2LMHeadModel.from_pretrained("gpt2")
43
+ tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
44
+
45
+ # Define input prompt
46
+ prompt = "The capital of France is"
47
+ input_ids = tokenizer.encode(prompt, return_tensors="pt")
48
+
49
+ # Define beam search parameters
50
+ beam_size = 5
51
+ max_length = 10
52
+ pad_token_id = tokenizer.eos_token_id
53
+
54
+ # Define possible answers
55
+ possible_answers = [' Paris', ' Paris Hilton']
56
+ tokenized_answers = tokenizer(possible_answers).input_ids
57
+ multi_choices_parser = MultiChoicesParser([tokenized_answers])
58
+
59
+ # Perform beam search
60
+ scores, solutions = divergent_beamsearch(
61
+ input_ids=input_ids,
62
+ model=model,
63
+ beam_size=beam_size,
64
+ max_length=max_length,
65
+ multi_choices_parser=multi_choices_parser,
66
+ pad_token_id=pad_token_id,
67
+ num_solutions=2
68
+ )
69
+
70
+ # Decode solutions
71
+ decoded_solutions = [tokenizer.decode(solution, skip_special_tokens=True) for solution in solutions]
72
+ print("Scores:", scores)
73
+ print("Solutions:", decoded_solutions)
74
+ ```
75
+
76
+ ## License
77
+
78
+ This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
@@ -0,0 +1,6 @@
1
+ divergent_beamsearch/__init__.py,sha256=Z2R1pkj4EEHMKWVZX0upeE_Jtfb6joxgYHuRNxWc8Zo,43
2
+ divergent_beamsearch/algorithm.py,sha256=6cWp6XHepSn1rjqQFkASxd8k3OEUarKNAiqPKfrA78k,5324
3
+ divergent_beamsearch-0.1.0.dist-info/METADATA,sha256=UCkp3rgFZ89kmwFVy_N_dEy45NTGN_yFhf-J6WCCR4U,2826
4
+ divergent_beamsearch-0.1.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
5
+ divergent_beamsearch-0.1.0.dist-info/licenses/LICENCE,sha256=jDQOOFKJxgrQwcEyipwKcKzj5IX_paD_41c3iOjH3qw,1095
6
+ divergent_beamsearch-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.27.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Hichem Ammar Khodja
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.