From 3f6033b96e0fba8482c7b8dbbfbe3080ac671f7f Mon Sep 17 00:00:00 2001 From: Raphael Fakhri <153192858+RaphaelFakhri@users.noreply.github.com> Date: Thu, 5 Nov 2026 09:52:25 +0000 Subject: [PATCH] fix(llm): fall back to sampling defaults for options passed as undefined --- .../__tests__/extensions/sampler.test.ts | 39 +++++++++++++++++++ .../src/extensions/llm/sampler.ts | 20 ++++++++-- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/packages/react-native-executorch/__tests__/extensions/sampler.test.ts b/packages/react-native-executorch/__tests__/extensions/sampler.test.ts index 3bcc2eafea..113593a22c 100644 --- a/packages/react-native-executorch/__tests__/extensions/sampler.test.ts +++ b/packages/react-native-executorch/__tests__/extensions/sampler.test.ts @@ -67,4 +67,43 @@ describe('sample — options merging', () => { logits.dispose(); } }); + + it('falls back to the defaults for options passed as undefined', () => { + const logits = tensor('float32', [1, 10]); + try { + sample( + logits, + { generatedTokens: new Int32Array(0) }, + { + temperature: undefined, + topK: undefined, + topP: undefined, + minP: undefined, + repetitionPenalty: undefined, + penaltyLastN: undefined, + dry: { multiplier: 0.8, base: undefined, allowedLength: undefined }, + xtc: { probability: 0.5, threshold: undefined }, + } + ); + + expect(llm.lastSampleOptions()).toMatchObject({ + temperature: 0.8, + repetitionPenalty: 1.0, + penaltyLastN: -1, + topK: 40, + topP: 0.95, + minP: 0.05, + dry: { + multiplier: 0.8, + base: 1.75, + allowedLength: 2, + penaltyLastN: -1, + sequenceBreakers: new Int32Array(0), + }, + xtc: { probability: 0.5, threshold: 0.1 }, + }); + } finally { + logits.dispose(); + } + }); }); diff --git a/packages/react-native-executorch/src/extensions/llm/sampler.ts b/packages/react-native-executorch/src/extensions/llm/sampler.ts index 3f9a159002..3a421e26c4 100644 --- a/packages/react-native-executorch/src/extensions/llm/sampler.ts +++ b/packages/react-native-executorch/src/extensions/llm/sampler.ts @@ -171,11 +171,25 @@ const DEFAULT_XTC_OPTIONS = { */ export function sample(logits: Tensor, ctx: SamplingContext, config: SamplingConfig = {}): number { 'worklet'; + // `??` rather than spreading the defaults, so a field passed as `undefined` + // falls back to its default instead of reaching the native sampler unset. + const { dry, xtc } = config; const options = { - ...DEFAULT_SAMPLING_OPTIONS, ...config, - dry: config.dry && { ...DEFAULT_DRY_OPTIONS, ...config.dry }, - xtc: config.xtc && { ...DEFAULT_XTC_OPTIONS, ...config.xtc }, + temperature: config.temperature ?? DEFAULT_SAMPLING_OPTIONS.temperature, + repetitionPenalty: config.repetitionPenalty ?? DEFAULT_SAMPLING_OPTIONS.repetitionPenalty, + penaltyLastN: config.penaltyLastN ?? DEFAULT_SAMPLING_OPTIONS.penaltyLastN, + topK: config.topK ?? DEFAULT_SAMPLING_OPTIONS.topK, + topP: config.topP ?? DEFAULT_SAMPLING_OPTIONS.topP, + minP: config.minP ?? DEFAULT_SAMPLING_OPTIONS.minP, + dry: dry && { + ...dry, + base: dry.base ?? DEFAULT_DRY_OPTIONS.base, + allowedLength: dry.allowedLength ?? DEFAULT_DRY_OPTIONS.allowedLength, + penaltyLastN: dry.penaltyLastN ?? DEFAULT_DRY_OPTIONS.penaltyLastN, + sequenceBreakers: dry.sequenceBreakers ?? DEFAULT_DRY_OPTIONS.sequenceBreakers, + }, + xtc: xtc && { ...xtc, threshold: xtc.threshold ?? DEFAULT_XTC_OPTIONS.threshold }, }; return rnexecutorchJsi.llm.sample(logits, ctx, options); }