Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { tensor } from '../../src/core/tensor';
import { normalize } from '../../src/extensions/cv/ops/image';

describe('normalize — options merging', () => {
it('falls back to the defaults for options passed as undefined', () => {
const src = tensor('float32', [1, 1, 2], new Float32Array([255, 51]));
const dst = tensor('float32', [1, 1, 2]);
try {
normalize(src, dst, { alpha: undefined, beta: undefined });
const [first, second] = dst.getData(new Float32Array(2));
expect(first).toBeCloseTo(1);
expect(second).toBeCloseTo(0.2);
} finally {
src.dispose();
dst.dispose();
}
});
});
12 changes: 6 additions & 6 deletions packages/react-native-executorch/__tests__/support/fakeOps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,14 +299,14 @@ export const cv = {
opts: { alpha?: number | readonly number[]; beta?: number | readonly number[] }
): FakeTensor {
expectShape(dst, src.shape, 'normalize: dst');
// Native requires both coefficients, so the TS wrapper must resolve them.
if (opts.alpha === undefined || opts.beta === undefined) {
throw new Error('normalize: alpha and beta are required');
}
const channels = src.shape[0]!;
const perChannel = src.numel / channels;
const coefficient = (
value: number | readonly number[] | undefined,
c: number,
fallback: number
) =>
value === undefined ? fallback : typeof value === 'number' ? value : (value[c] ?? fallback);
const coefficient = (value: number | readonly number[], c: number, fallback: number) =>
typeof value === 'number' ? value : (value[c] ?? fallback);

for (let i = 0; i < src.numel; i++) {
const c = Math.floor(i / perChannel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,23 @@ describe('createLLMChatSession — construction', () => {
expect(promptsSent().join('')).not.toContain('<think>');
});

it('keeps the session default for a per-turn option passed as undefined', async () => {
writeTokenizerConfig({
// eslint-disable-next-line camelcase
chat_template: CHAT_TEMPLATE.replace(
'<|assistant|>',
'<|assistant|>{% if enable_thinking is not defined or enable_thinking %}<think>\n{% else %}DIRECT{% endif %}'
),
});
const session = tracked(
await createLLMChatSession(config, { generationConfig: { enableThinking: false } })
);

await session.sendMessage('hi', undefined, { enableThinking: undefined });

expect(promptsSent()).toContain('<|assistant|>DIRECT');
});

it.each(SPECIAL_TOKEN_CONFIGS)(
'passes configured special tokens to the template (%#)',
async (tokens) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,26 @@ describe('createFsmnVoiceActivityDetector', () => {
expect(await vad.detectVoice(new Float32Array(100))).toEqual([]);
vad.dispose();
});

it('falls back to the default options for fields passed as undefined', async () => {
// Every frame scores 0 non-speech probability, so the defaults find speech.
fakeJsi.registerModel(VAD_PATH, { schema: VAD_SCHEMA });

const vad = await createFsmnVoiceActivityDetector(VAD_CONFIG);
const waveform = new Float32Array(16000);
const expected = await vad.detectVoice(waveform);
expect(expected.length).toBeGreaterThan(0);
expect(
await vad.detectVoice(waveform, {
speechThreshold: undefined,
minSpeechDurationMs: undefined,
minSilenceDurationMs: undefined,
speechPadMs: undefined,
mergeGapMs: undefined,
})
).toEqual(expected);
vad.dispose();
});
});

// ============================================================================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,10 @@ export function toChannelsLast(src: Tensor, dst: Tensor): Tensor {
*/
export function normalize(src: Tensor, dst: Tensor, options?: NormalizeOptions): Tensor {
'worklet';
const defaultNormalizeOptions = {
alpha: 1 / 255.0,
beta: 0.0,
} as const;
return rnexecutorchJsi.cv.normalize(src, dst, { ...defaultNormalizeOptions, ...options });
return rnexecutorchJsi.cv.normalize(src, dst, {
alpha: options?.alpha ?? 1 / 255.0,
beta: options?.beta ?? 0.0,
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,11 @@ export async function createLLMChatSession(
throw RnExecuTorchError('RESOURCE_BUSY', 'sendMessage: A turn is already in progress.');
}

const turnGenConfig = { ...defaultGenerationConfig, ...genConfig };
// Drop fields passed as `undefined` so they keep the session default.
const turnOverrides = Object.fromEntries(
Object.entries(genConfig ?? {}).filter(([, value]) => value !== undefined)
);
const turnGenConfig: LLMGenerationConfig = { ...defaultGenerationConfig, ...turnOverrides };
const prefillOptions = { enableThinking: turnGenConfig.enableThinking, addGenPrompt: false };
const renderOptions = { ...prefillOptions, addGenPrompt: true };
const generationOpts = { genConfig: turnGenConfig, stopTokens, stopRegex, onToken };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,13 @@ export async function createFsmnVoiceActivityDetector(

const detectVoiceWorklet = (waveform: Float32Array, options?: VadOptions): VadSegment[] => {
'worklet';
const mergedOpts: Required<VadOptions> = { ...defaultOptions, ...options };
const mergedOpts: Required<VadOptions> = {
speechThreshold: options?.speechThreshold ?? defaultOptions.speechThreshold,
minSpeechDurationMs: options?.minSpeechDurationMs ?? defaultOptions.minSpeechDurationMs,
minSilenceDurationMs: options?.minSilenceDurationMs ?? defaultOptions.minSilenceDurationMs,
speechPadMs: options?.speechPadMs ?? defaultOptions.speechPadMs,
mergeGapMs: options?.mergeGapMs ?? defaultOptions.mergeGapMs,
};
const numFrames = Math.floor((waveform.length - FRAME_LENGTH) / HOP_LENGTH);
if (numFrames <= 0) return [];

Expand Down
Loading