import { existsSync } from 'node:fs'; import { describe, expect, it } from 'vitest'; import { ContextDrafter, FeatureDraftHead, TrainedContextDrafter, targetDistributionFromTopK, type TrainedContextModel, type FeatureDraftHeadModel, } from '../../packages/runtime/src/speculative.ts'; import { sampleResidual, verifySampledChain, type SparseDistribution, } from '../../packages/kernels/src/reference/speculative.ts'; const profile = { topP: 0.95, minP: 0.05, temperature: 0.3, minCandidates: 1 }; describe('context drafter and speculative verification', () => { it('replays an observed suffix and emits normalized draft distributions', () => { const drafter = new ContextDrafter(42n, [1, 2, 3, 4, 1, 2, 3, 9, 1, 2]); const draft = drafter.propose(2); expect(draft.proposals[0]).toBe(3); expect([4, 9]).toContain(draft.proposals[1]); for (const distribution of draft.distributions) { const total = distribution.probabilities.reduce((sum, value) => sum + value, 0); expect(total).toBeCloseTo(1, 12); } }); it('matches the sampler filter over a merged top-k vector', () => { const distribution = targetDistributionFromTopK( { tokens: [5, 9, 2, 0xffffffff], logits: [4, 3, 1, -Infinity] }, profile, ); expect([...distribution.tokens]).toEqual([5, 9]); expect(distribution.probabilities[0]!).toBeGreaterThan(distribution.probabilities[1]!); expect(distribution.probabilities.reduce((sum, value) => sum + value, 0)).toBeCloseTo(1, 12); }); it('accepts a chain and samples a bonus from the target', () => { const target: SparseDistribution = { tokens: Uint32Array.from([1, 2]), probabilities: Float64Array.from([0.6, 0.4]), }; const draft: SparseDistribution = { tokens: Uint32Array.from([1, 2]), probabilities: Float64Array.from([0.5, 0.5]), }; const uniforms = [0.1, 0.99]; let index = 0; const result = verifySampledChain( Uint32Array.from([1]), [draft], [target, target], { uniforms: () => uniforms[index++]!, targetDistributions: [target, target] }, ); expect(result.accepted).toEqual([1]); expect(result.corrected).toBeNull(); expect(result.bonus).toBe(2); }); it('uses the residual law after rejection', () => { const target: SparseDistribution = { tokens: Uint32Array.from([1, 2]), probabilities: Float64Array.from([0.9, 0.1]), }; const draft: SparseDistribution = { tokens: Uint32Array.from([1, 2]), probabilities: Float64Array.from([0.1, 0.9]), }; let draw = 0; const result = verifySampledChain( Uint32Array.from([2]), [draft], [target, target], { uniforms: () => (draw++ === 0 ? 0.5 : 0.95), targetDistributions: [target, target] }, ); expect(result.accepted).toEqual([]); expect(result.corrected).toBe(1); expect(sampleResidual(target, draft, () => 0.0)).toBe(1); }); }); describe('trained context drafter artifact', () => { it.skipIf(!existsSync(new URL('../../artifacts/draft-context.json', import.meta.url)))('loads the trained transition model and produces normalized proposals', async () => { const path = new URL('../../artifacts/draft-context.json', import.meta.url); const { readFileSync } = await import('node:fs'); const model = JSON.parse(readFileSync(path, 'utf8')) as TrainedContextModel; const drafter = new TrainedContextDrafter(model, 42n, [9707, 279, 264]); const draft = drafter.propose(2); expect(draft.proposals.length).toBeGreaterThan(0); for (const distribution of draft.distributions) { expect(distribution.probabilities.reduce((sum, value) => sum + value, 0)).toBeCloseTo(1, 12); } }); }); describe('target-feature draft head artifact', () => { it.skipIf(!existsSync(new URL('../../artifacts/draft-head.json', import.meta.url)))('loads the trained head and normalizes candidate probabilities', async () => { const { readFileSync } = await import('node:fs'); const model = JSON.parse( readFileSync(new URL('../../artifacts/draft-head.json', import.meta.url), 'utf8'), ) as FeatureDraftHeadModel; const head = new FeatureDraftHead(model); const hidden = new Float64Array(model.dimension); for (let i = 0; i < hidden.length; i++) hidden[i] = Math.sin(i * 0.01) * 0.1; const distribution = head.distribution(hidden); expect(distribution.tokens.length).toBe(model.candidates.length); expect(distribution.probabilities.reduce((sum, value) => sum + value, 0)).toBeCloseTo(1, 12); }); });