divergent-beamsearch 0.2.1__tar.gz → 0.2.3__tar.gz

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.
@@ -1,10 +1,10 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: divergent-beamsearch
3
- Version: 0.2.1
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.9.72
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
 
@@ -1,11 +1,11 @@
1
1
  [project]
2
2
  name = "divergent-beamsearch"
3
- version = "0.2.1"
3
+ version = "0.2.3"
4
4
  description = "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
  readme = "README.md"
6
6
  requires-python = ">=3.11"
7
7
  dependencies = [
8
- "multi-choices-parser>=0.9.72",
8
+ "multi-choices-parser>=0.10.0",
9
9
  "torch>=2.0.0",
10
10
  ]
11
11
 
@@ -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.step(token)
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 = list(parser.next())
235
+ next_tokens = parser.next()
230
236
  try:
231
237
  next_tokens.remove(end_symb)
232
238
  except ValueError:
@@ -217,9 +217,8 @@ def test_element_wise_equivalence_divergent_logprob(fakemodel_and_tokenizer, dev
217
217
  'Google is owned by Alphabet'
218
218
  ]
219
219
 
220
- multi_choices_parser = MultiChoicesParser([texts])
221
-
222
220
  inputs = tokenizer(texts, return_tensors='pt', padding=True).to(device)
221
+ multi_choices_parser = MultiChoicesParser([[x[1:] for x in tokenizer(texts).input_ids]])
223
222
 
224
223
  logprobs_global = divergent_logprob(inputs.input_ids, inputs.attention_mask, model, multi_choices_parser)
225
224
 
@@ -73,28 +73,30 @@ wheels = [
73
73
 
74
74
  [[package]]
75
75
  name = "divergent-beamsearch"
76
- version = "0.2.0"
76
+ version = "0.2.2"
77
77
  source = { editable = "." }
78
78
  dependencies = [
79
79
  { name = "multi-choices-parser" },
80
80
  { name = "torch" },
81
- { name = "transformers" },
82
81
  ]
83
82
 
84
83
  [package.dev-dependencies]
85
84
  dev = [
86
85
  { name = "pytest" },
86
+ { name = "transformers" },
87
87
  ]
88
88
 
89
89
  [package.metadata]
90
90
  requires-dist = [
91
- { name = "multi-choices-parser", specifier = ">=0.9.72" },
91
+ { name = "multi-choices-parser", specifier = ">=0.10.0" },
92
92
  { name = "torch", specifier = ">=2.0.0" },
93
- { name = "transformers", specifier = ">=4.47.1" },
94
93
  ]
95
94
 
96
95
  [package.metadata.requires-dev]
97
- dev = [{ name = "pytest", specifier = ">=8.3.4" }]
96
+ dev = [
97
+ { name = "pytest", specifier = ">=8.3.4" },
98
+ { name = "transformers", specifier = ">=4.47.1" },
99
+ ]
98
100
 
99
101
  [[package]]
100
102
  name = "filelock"
@@ -221,18 +223,18 @@ wheels = [
221
223
 
222
224
  [[package]]
223
225
  name = "multi-choices-parser"
224
- version = "0.9.72"
225
- source = { registry = "https://pypi.org/simple" }
226
- wheels = [
227
- { url = "https://files.pythonhosted.org/packages/ab/14/7a99908c455ed355563c1a59c3953fd2e1e0b8bd3699f616adf44f31c019/multi_choices_parser-0.9.72-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:7554b9928b663952d50dad2be070b33eac12a7cf0a5d0237ca273f075e598d09", size = 99165 },
228
- { url = "https://files.pythonhosted.org/packages/aa/3c/fb13affb1061050fb0f2988d1fdd0f37943e17abf1644ac681d6cda45615/multi_choices_parser-0.9.72-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:4700eab86215bd4f0da9fcea0650e5336bc22d62a77625b2d3d1b1a83081b0d4", size = 139545 },
229
- { url = "https://files.pythonhosted.org/packages/47/c0/5b47daed1dd6cff64c602cdcefda285eacfdf71f43d0452ed2f68e17ae9e/multi_choices_parser-0.9.72-cp311-cp311-win_amd64.whl", hash = "sha256:65725c593363b8c207748478ca966e5fc0288118b95c2e6b7cc338003417a185", size = 105544 },
230
- { url = "https://files.pythonhosted.org/packages/51/98/10331d2da4c0c036720f1cd41a60f33cf35a4ac2aad963dd58e486d97ccb/multi_choices_parser-0.9.72-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:277ed1e6e4c7044313281caa5c20cb09eab518f27271001afea748793acc26a9", size = 99360 },
231
- { url = "https://files.pythonhosted.org/packages/98/6a/5c90c3b19013aa02b40ed6ef193213bfb4ad92e4500c8e1009e712c0d6db/multi_choices_parser-0.9.72-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:441ef6dd784c9d7fbf0effe66f2e910ad308604749924e0dccec79fd24cfdf2e", size = 138117 },
232
- { url = "https://files.pythonhosted.org/packages/07/13/4c601c9336b7a83e762937c2d75823964a9a9773903cba2696ec59107dbf/multi_choices_parser-0.9.72-cp312-cp312-win_amd64.whl", hash = "sha256:28ac8cea47639b434fc88e143f5d38a0bd5ab4ce9a040a036e532896185d672b", size = 105658 },
233
- { url = "https://files.pythonhosted.org/packages/db/9d/8ea1f8a87282da07b0d5044c682566a68eced933bb675c8936400bb72a54/multi_choices_parser-0.9.72-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:3dfd87c968a6e5618a0cacc3d29fa244cf427208829eeda82802fd60250ea1b3", size = 99398 },
234
- { url = "https://files.pythonhosted.org/packages/8b/8d/f0a244c59e13e4591e5be9f0793a22e3cde6b631801f9473e96fe44c76bb/multi_choices_parser-0.9.72-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:c99dae2ba228c9362648ed66bd9790df07a71176f22938595487605b489a8dc8", size = 138403 },
235
- { url = "https://files.pythonhosted.org/packages/f3/f2/73e929b894fe379be5e1b8d373c9acf66c5b34da3f886edbe22ff8725593/multi_choices_parser-0.9.72-cp313-cp313-win_amd64.whl", hash = "sha256:9576300f71ba688f799832e8b86b3cb24ea74cde29aa4e70ac63ec7545e32790", size = 105658 },
226
+ version = "0.10.0"
227
+ source = { registry = "https://pypi.org/simple" }
228
+ wheels = [
229
+ { url = "https://files.pythonhosted.org/packages/25/59/233da6ab703cf3243dffd2180d082a91a45caf720723309090cee3353da7/multi_choices_parser-0.10.0-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:3019671e9ed6daa0fb8c746cd9f52557da280d57a3ba938f72a34db336671980", size = 99282 },
230
+ { url = "https://files.pythonhosted.org/packages/30/b0/82b5ea3ebb500df180cf15e2d7d43bbcef1d58b122206f0b4616bf1dabf5/multi_choices_parser-0.10.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:bebb99bf5096e8f40ee584e5860efa10e9484a50c7747360f313c761e16ed5c5", size = 139668 },
231
+ { url = "https://files.pythonhosted.org/packages/2d/f4/b7e12764e7366b01d7fa5fdd177480967492219b1d7ffd5c6a35f8117247/multi_choices_parser-0.10.0-cp311-cp311-win_amd64.whl", hash = "sha256:bee4d14b626fa9f8290670047bc0cc358c0a3dddd0dc104e9e844ed4b1b43203", size = 105669 },
232
+ { url = "https://files.pythonhosted.org/packages/d6/08/f6eacab1476d99b64443433e5a683afaf79f8ae6798edf12a7535a7a02af/multi_choices_parser-0.10.0-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:818d292bfa2d35e0cbee4608cb2f4f9e223c7a8b8d3f94a83dc83d05f4dd71ef", size = 99485 },
233
+ { url = "https://files.pythonhosted.org/packages/02/6a/6ecfcf3b14972807cf3eb34d960691116bbdd655ba5466905543b0fb0a53/multi_choices_parser-0.10.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:4e5ed7468834cf9502d3aa3fe71eaf9b2a28e3e40eb60744ded7a7605eed3612", size = 138236 },
234
+ { url = "https://files.pythonhosted.org/packages/62/04/7ab6935f99d275fba202cd21b2cd0fb2f775237c6b57ad247cbb95e4db53/multi_choices_parser-0.10.0-cp312-cp312-win_amd64.whl", hash = "sha256:fbc8c4b6f8bbf9e2ead1c228b6a5be9fac4d91854797b430fcb05d91ba96f8dd", size = 105784 },
235
+ { url = "https://files.pythonhosted.org/packages/27/88/8fb06ff9341e4a09b714939d515e583678e1620f2d3a4536e4776a4ad92a/multi_choices_parser-0.10.0-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:541f5ff7da3cb7fb1b4ef2d114ee02637322c058c84345283c9c3194c7207e31", size = 99523 },
236
+ { url = "https://files.pythonhosted.org/packages/e6/da/7f853bb1e676d74c85d25a1023674bcb0407d9a222ce9f65d56de4025dd5/multi_choices_parser-0.10.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:ead4849057c50608a48eb498a95a622a2b0151e20c871ed9ec27ed27eb20108d", size = 138524 },
237
+ { url = "https://files.pythonhosted.org/packages/ca/2f/7b8baffc032b503fc1075fa0be19c8ab3b56265b8c3a763bfac6c27b835f/multi_choices_parser-0.10.0-cp313-cp313-win_amd64.whl", hash = "sha256:ad17159761164672895efe07c75e1d872c8c40e74e89aa9cb6ff74fd9a81362d", size = 105781 },
236
238
  ]
237
239
 
238
240
  [[package]]