Commit 9f1c89f
feat(llm): min_p and repetition_penalty sampling, per-model defaults, letterbox vision (#1099)
## Description
Adds `min_p` and `repetition_penalty` sampling parameters to
`GenerationConfig`, plumbs them through the full stack (`Sampler` →
`TextDecoderRunner` → `TextTokenGenerator` → `BaseLLMRunner` /
`TextRunner` / `MultimodalRunner` → JSI bindings → `LLMController`),
introduces a per-model default `generationConfig` that gets applied
automatically on load (populated for Qwen3 and LFM2-VL from their
upstream recommendations), and replaces the distorting `cv::resize` in
`VisionEncoder` with the existing `resizePadded` helper so multimodal
inputs keep their aspect ratio. Also fixes three silent pre-existing
bugs surfaced along the way: an xorshift PRNG seeded with `0` that made
sampling deterministic, a `Sampler::apply_min_p` renormalization gap,
and inline `{}` no-op overrides in `MultimodalRunner` that would desync
in future refactors.
### Introduces a breaking change?
- [ ] Yes
- [x] No
### Type of change
- [x] Bug fix (change which fixes an issue)
- [x] New feature (change which adds functionality)
- [ ] Documentation update (improves or adds clarity to existing
documentation)
- [ ] Other (chores, tests, code style improvements etc.)
### Tested on
- [x] iOS
- [ ] Android
### Testing instructions
**Sampling parameter plumbing**
1. Open `apps/llm`, load any supported model (e.g.
`LFM2_VL_450M_QUANTIZED`).
2. Without any manual `configure()` call, send a prompt. The model card
defaults are applied automatically — for LFM2-VL you should now see
coherent, non-repetitive descriptions (previously the model often
produced generic or looping replies at the library's default
`temperature=0.8, topp=0.9`).
3. Optionally override via `useLLM(...)`'s `configure({
generationConfig: { temperature: 0.7, minP: 0.1, repetitionPenalty: 1.05
} })` and confirm the generation style changes.
**Letterbox preprocessing**
1. With a multimodal model loaded in `apps/llm` → `multimodal_llm`
screen, attach a photo with a non-square aspect ratio (e.g. 3000×2250
from your camera roll).
2. Ask the model to describe it. Before this PR the image was stretched
into the PTE's square input shape — the model would sometimes
misidentify subjects in wide/tall photos. After, the image is
letterboxed so proportions are preserved.
### Screenshots
<!-- none -->
### Related issues
<!-- none -->
### Checklist
- [x] I have performed a self-review of my code
- [x] I have commented my code, particularly in hard-to-understand areas
- [x] I have updated the documentation accordingly
- [x] My changes generate no new warnings
### Additional notes
**Per-model recommended defaults**
Model presets gain an optional `generationConfig` field;
`LLMController.load` applies it before flipping `isReady`, so users see
sensible sampling out of the box. User `configure()` calls still
override per-field. Populated for:
- **Qwen3** family (`temperature=0.6, topp=0.95`, from
`generation_config.json`)
- **LFM2-VL** family (`temperature=0.1, minP=0.15,
repetitionPenalty=1.05`, from the LiquidAI model card)
Other presets (Llama, SmolLM2, Hammer, Phi-4, Qwen2.5, LFM2 text) keep
the library defaults — these model cards don't publish sampling
recommendations, so adding arbitrary values would be guessing.
---------
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>1 parent 04852be commit 9f1c89f
29 files changed
Lines changed: 514 additions & 178 deletions
File tree
- apps/llm/app/multimodal_llm
- docs/docs
- 03-hooks/01-natural-language-processing
- 04-typescript-api/01-natural-language-processing
- packages/react-native-executorch
- common
- rnexecutorch
- host_objects
- models/llm
- tests
- integration
- stubs
- unit
- runner
- encoders
- src
- constants
- controllers
- hooks/natural_language_processing
- types
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
| 17 | + | |
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| |||
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
53 | | - | |
| 53 | + | |
54 | 54 | | |
55 | 55 | | |
56 | 56 | | |
| |||
Lines changed: 16 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
211 | 211 | | |
212 | 212 | | |
213 | 213 | | |
214 | | - | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
215 | 223 | | |
216 | 224 | | |
217 | 225 | | |
| |||
282 | 290 | | |
283 | 291 | | |
284 | 292 | | |
285 | | - | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
286 | 296 | | |
287 | 297 | | |
288 | 298 | | |
| |||
491 | 501 | | |
492 | 502 | | |
493 | 503 | | |
494 | | - | |
| 504 | + | |
495 | 505 | | |
496 | | - | |
| 506 | + | |
497 | 507 | | |
498 | 508 | | |
499 | 509 | | |
| |||
514 | 524 | | |
515 | 525 | | |
516 | 526 | | |
517 | | - | |
| 527 | + | |
518 | 528 | | |
519 | 529 | | |
520 | 530 | | |
| |||
537 | 547 | | |
538 | 548 | | |
539 | 549 | | |
540 | | - | |
| 550 | + | |
541 | 551 | | |
542 | 552 | | |
543 | 553 | | |
| |||
Lines changed: 11 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
107 | 107 | | |
108 | 108 | | |
109 | 109 | | |
110 | | - | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
111 | 119 | | |
112 | 120 | | |
113 | 121 | | |
114 | 122 | | |
115 | 123 | | |
116 | 124 | | |
117 | | - | |
| 125 | + | |
118 | 126 | | |
119 | 127 | | |
120 | | - | |
| 128 | + | |
121 | 129 | | |
122 | 130 | | |
123 | 131 | | |
| |||
Lines changed: 9 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
140 | 140 | | |
141 | 141 | | |
142 | 142 | | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
143 | 152 | | |
144 | 153 | | |
145 | 154 | | |
| |||
Lines changed: 24 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
250 | 250 | | |
251 | 251 | | |
252 | 252 | | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
253 | 277 | | |
254 | 278 | | |
255 | 279 | | |
| |||
Lines changed: 2 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
| 41 | + | |
| 42 | + | |
41 | 43 | | |
42 | 44 | | |
43 | 45 | | |
| |||
Lines changed: 6 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
151 | 151 | | |
152 | 152 | | |
153 | 153 | | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
154 | 160 | | |
155 | 161 | | |
156 | 162 | | |
| |||
Lines changed: 25 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
110 | 110 | | |
111 | 111 | | |
112 | 112 | | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
113 | 138 | | |
114 | 139 | | |
115 | 140 | | |
| |||
Lines changed: 0 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | 21 | | |
26 | 22 | | |
27 | 23 | | |
28 | 24 | | |
29 | 25 | | |
30 | 26 | | |
31 | 27 | | |
32 | | - | |
33 | 28 | | |
Lines changed: 13 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
65 | | - | |
| 65 | + | |
66 | 66 | | |
67 | 67 | | |
68 | 68 | | |
69 | | - | |
70 | 69 | | |
71 | 70 | | |
72 | 71 | | |
| |||
89 | 88 | | |
90 | 89 | | |
91 | 90 | | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
0 commit comments