LycheeAI commited on
Commit
f3f95d1
·
verified ·
1 Parent(s): 89bea82

Read prompts whole up to 262,144 tokens

Browse files
Files changed (3) hide show
  1. README.md +5 -4
  2. lychee/model.py +34 -16
  3. lychee_config.json +1 -1
README.md CHANGED
@@ -19,9 +19,9 @@ Lychee-27B is a decision model fine-tuned from Qwen3.8-27B. Given a state and ty
19
  | Knowledge & Reasoning | **53.9** | 51.8 |
20
  | Language Understanding | 59.2 | **66.2** |
21
  | Retrieval & Classification | 55.4 | **63.9** |
22
- | Tools & Automation | 75.1 | **80.7** |
23
- | Arts & Human Taste | 39.1 | **46.9** |
24
- | Public index | 58.0 | **62.7** |
25
 
26
  Bold marks the better score in each row. Decision Index 0.3, public suite: Jev's figures are the board's,
27
  Lychee-27B's were measured with the board's kit against the server in this repository.
@@ -54,7 +54,8 @@ print(answers["team"]) # the chosen option and a probability for every option
54
  ```
55
 
56
  A `noul` question gives the probability of yes; a `score` question takes its levels as a list, lowest first. Prompts
57
- over 8,192 tokens are refused, never truncated.
 
58
 
59
  ## License
60
 
 
19
  | Knowledge & Reasoning | **53.9** | 51.8 |
20
  | Language Understanding | 59.2 | **66.2** |
21
  | Retrieval & Classification | 55.4 | **63.9** |
22
+ | Tools & Automation | 75.1 | **80.8** |
23
+ | Arts & Human Taste | 39.1 | **47.0** |
24
+ | Public index | 58.0 | **62.8** |
25
 
26
  Bold marks the better score in each row. Decision Index 0.3, public suite: Jev's figures are the board's,
27
  Lychee-27B's were measured with the board's kit against the server in this repository.
 
54
  ```
55
 
56
  A `noul` question gives the probability of yes; a `score` question takes its levels as a list, lowest first. Prompts
57
+ are read whole up to 262,144 tokens, the base model's context; longer ones, or ones that do not fit in the GPU's
58
+ memory, are refused, never truncated.
59
 
60
  ## License
61
 
lychee/model.py CHANGED
@@ -24,6 +24,7 @@ SYSTEM_PROMPT = (
24
  DEFAULT_INSTRUCTIONS = "Choose the best matching option."
25
  CLOSING_LINE = "Return only the letter code of the best option."
26
  QUESTIONS_PER_PASS = 8
 
27
 
28
 
29
  class RequestError(ValueError):
@@ -139,6 +140,28 @@ class Lychee:
139
  chat_messages(state, question, self.codes), tokenize=False, add_generation_prompt=True,
140
  enable_thinking=False)
141
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
  @torch.inference_mode()
143
  def decide(self, state, questions: dict) -> tuple[dict, int]:
144
  """Answers for every named question about one state, and the input tokens read."""
@@ -148,22 +171,17 @@ class Lychee:
148
  answers, input_tokens = {}, 0
149
  for start in range(0, len(named), QUESTIONS_PER_PASS):
150
  part = named[start:start + QUESTIONS_PER_PASS]
151
- counts = [len(option_table(question)[0]) for _, question in part]
152
  texts = [self.prompt(state, question) for _, question in part]
153
  encoded = self.tokenizer(texts, padding=True, return_tensors="pt")
154
- if encoded["input_ids"].shape[1] > self.max_input_tokens:
155
- raise RequestError(f"the prompt exceeds the maximum context length of {self.max_input_tokens} "
156
- "tokens; nothing was truncated")
157
- input_tokens += int(encoded["attention_mask"].sum())
158
- encoded = {name: tensor.to(self.device) for name, tensor in encoded.items()}
159
- last = self.backbone(**encoded, use_cache=False).last_hidden_state[:, -1]
160
- logits = self.readout(last).float()
161
- beyond = (torch.arange(logits.shape[1], device=logits.device)[None]
162
- >= torch.tensor(counts, device=logits.device)[:, None])
163
- p = (logits.masked_fill(beyond, -1e9) / self.temperature).softmax(-1).cpu().tolist()
164
- for (name, question), row, count in zip(part, p, counts):
165
- values = row[:count]
166
- if not all(math.isfinite(v) for v in values):
167
- raise RequestError(f"question {name!r} produced a non-finite distribution")
168
- answers[name] = shaped_answer(question, values)
169
  return answers, input_tokens
 
24
  DEFAULT_INSTRUCTIONS = "Choose the best matching option."
25
  CLOSING_LINE = "Return only the letter code of the best option."
26
  QUESTIONS_PER_PASS = 8
27
+ BATCHED_PROMPT_TOKENS = 8192 # passes whose padded prompts are longer than this read one question at a time
28
 
29
 
30
  class RequestError(ValueError):
 
140
  chat_messages(state, question, self.codes), tokenize=False, add_generation_prompt=True,
141
  enable_thinking=False)
142
 
143
+ def _read(self, part: list[tuple[str, dict]], encoded) -> dict:
144
+ """One forward pass over the tokenized prompts of the questions in `part`."""
145
+ counts = [len(option_table(question)[0]) for _, question in part]
146
+ encoded = {name: tensor.to(self.device) for name, tensor in encoded.items()}
147
+ try:
148
+ last = self.backbone(**encoded, use_cache=False).last_hidden_state[:, -1]
149
+ except torch.OutOfMemoryError:
150
+ torch.cuda.empty_cache()
151
+ raise RequestError("the prompt does not fit in this GPU's memory, so it exceeds the maximum context length "
152
+ "this device can read; nothing was truncated") from None
153
+ logits = self.readout(last).float()
154
+ beyond = (torch.arange(logits.shape[1], device=logits.device)[None]
155
+ >= torch.tensor(counts, device=logits.device)[:, None])
156
+ p = (logits.masked_fill(beyond, -1e9) / self.temperature).softmax(-1).cpu().tolist()
157
+ answers = {}
158
+ for (name, question), row, count in zip(part, p, counts):
159
+ values = row[:count]
160
+ if not all(math.isfinite(v) for v in values):
161
+ raise RequestError(f"question {name!r} produced a non-finite distribution")
162
+ answers[name] = shaped_answer(question, values)
163
+ return answers
164
+
165
  @torch.inference_mode()
166
  def decide(self, state, questions: dict) -> tuple[dict, int]:
167
  """Answers for every named question about one state, and the input tokens read."""
 
171
  answers, input_tokens = {}, 0
172
  for start in range(0, len(named), QUESTIONS_PER_PASS):
173
  part = named[start:start + QUESTIONS_PER_PASS]
 
174
  texts = [self.prompt(state, question) for _, question in part]
175
  encoded = self.tokenizer(texts, padding=True, return_tensors="pt")
176
+ if encoded["input_ids"].shape[1] <= BATCHED_PROMPT_TOKENS:
177
+ passes = [(part, encoded)]
178
+ else: # long prompts are read one question at a time, which bounds the memory a pass needs
179
+ passes = [([q], self.tokenizer([text], return_tensors="pt")) for q, text in zip(part, texts)]
180
+ for questions_in_pass, tokens in passes:
181
+ if tokens["input_ids"].shape[1] > self.max_input_tokens:
182
+ raise RequestError(f"the prompt exceeds the maximum context length of {self.max_input_tokens} "
183
+ "tokens; nothing was truncated")
184
+ for questions_in_pass, tokens in passes:
185
+ answers.update(self._read(questions_in_pass, tokens))
186
+ input_tokens += int(tokens["attention_mask"].sum())
 
 
 
 
187
  return answers, input_tokens
lychee_config.json CHANGED
@@ -1,6 +1,6 @@
1
  {
2
  "base_model": "Qwen/Qwen3.8-27B",
3
- "max_input_tokens": 8192,
4
  "temperature": 1.0,
5
  "codes": [
6
  "A",
 
1
  {
2
  "base_model": "Qwen/Qwen3.8-27B",
3
+ "max_input_tokens": 262144,
4
  "temperature": 1.0,
5
  "codes": [
6
  "A",