Kernel-Trap-Cards / cases.mjs
alphabell-research's picture
Clarify quiz feedback and stride input (#2)
f56c33a
Raw History Blame Contribute Delete
6.19 kB
export const cases = [
{
slug: 'tail-mask',
title: 'The missing tail',
task: 'Double every element of a vector.',
fixed: '1,024 elements, all values checked',
code: `for (start = 0; start + 64 <= n; start += 64) {
write(out[start : start + 64], 2 * x[start : start + 64]);
}`,
question: 'Which second test exposes the bug?',
choices: [
{ label: 'Another 2,048-element vector', input: { n: 2048 } },
{ label: 'A 1,030-element vector', input: { n: 1030 } },
{ label: 'Different values, still 1,024 elements', input: { n: 1024, offset: 7 } },
],
reference({ n, offset = 0 }) {
return Array.from({ length: n }, (_, i) => 2 * (i + offset));
},
candidate({ n, offset = 0 }) {
const out = Array(n).fill(null);
for (let start = 0; start + 64 <= n; start += 64) {
for (let i = start; i < start + 64; i++) out[i] = 2 * (i + offset);
}
return out;
},
lesson: 'The loop never launches a partial block. Tests whose lengths are multiples of 64 cannot reveal the unwritten tail.',
},
{
slug: 'row-stride',
title: 'The wrong row stride',
task: 'Triple every value in a 2D view of a backing buffer numbered 0, 1, 2, ...',
fixed: '4 by 3 contiguous view, all values checked',
code: `for (row = 0; row < height; row++)
for (col = 0; col < width; col++)
out[row, col] = 3 * storage[row * width + col];`,
question: 'Which input breaks the candidate?',
choices: [
{ label: 'Contiguous 8 by 3 view', input: { width: 8, height: 3, stride: 8 } },
{ label: 'Contiguous 4 by 3 view with negatives', input: { width: 4, height: 3, stride: 4, offset: -20 } },
{ label: '4 by 3 view with row stride 6', input: { width: 4, height: 3, stride: 6 } },
],
reference({ width, height, stride, offset = 0 }) {
return Array.from({ length: width * height }, (_, i) => 3 * (Math.floor(i / width) * stride + (i % width) + offset));
},
candidate({ width, height, offset = 0 }) {
return Array.from({ length: width * height }, (_, i) => 3 * (Math.floor(i / width) * width + (i % width) + offset));
},
lesson: 'Width is not a memory stride. A sliced or padded view can keep the same visible shape while changing where each row begins.',
},
{
slug: 'half-accumulator',
title: 'The half-precision accumulator',
task: 'Sum values and return a 32-bit result.',
fixed: '32 copies of 0.25, expected result 8',
code: `half total = 0;
for (i = 0; i < n; i++) total = half(total + x[i]);
return float32(total);`,
question: 'Which values expose the overflow?',
choices: [
{ label: '64 copies of 0.25', input: { values: Array(64).fill(0.25) } },
{ label: '60,000 and 60,000', input: { values: [60000, 60000] } },
{ label: '1, 2, and 3', input: { values: [1, 2, 3] } },
],
reference({ values }) {
return [values.reduce((sum, value) => sum + value, 0)];
},
candidate({ values }) {
let total = 0;
for (const value of values) {
total += value;
if (Math.abs(total) > 65504) total = total < 0 ? -Infinity : Infinity;
}
return [total];
},
lesson: 'The simplified half accumulator overflows above 65,504 even though the requested 32-bit output can represent the correct sum.',
},
{
slug: 'single-row',
title: 'The one-row illusion',
task: 'Add a column bias to every matrix row.',
fixed: '1 by 4 matrix, all values checked',
code: `out = copy(matrix);
for (col = 0; col < width; col++)
out[0, col] += bias[col];`,
question: 'Which shape exposes the missing work?',
choices: [
{ label: '1 by 8 matrix', input: { rows: 1, width: 8 } },
{ label: '2 by 4 matrix', input: { rows: 2, width: 4 } },
{ label: '1 by 4 matrix with a different bias', input: { rows: 1, width: 4, biasOffset: 10 } },
],
reference({ rows, width, biasOffset = 0 }) {
return Array.from({ length: rows * width }, (_, i) => i + 1 + (i % width) + biasOffset);
},
candidate({ rows, width, biasOffset = 0 }) {
const out = Array.from({ length: rows * width }, (_, i) => i + 1);
for (let col = 0; col < width; col++) out[col] += col + biasOffset;
return out;
},
lesson: 'The candidate only writes row zero. A one-row correctness test can check every value and still miss the missing rows.',
},
{
slug: 'aliasing',
title: 'The aliasing trap',
task: 'For each element after the first, add its left neighbor from the original input.',
fixed: 'Separate input and output buffers',
code: `for (i = 1; i < n; i++)
out[i] = x[i] + x[i - 1];`,
question: 'Which buffer setup changes the result?',
choices: [
{ label: 'Separate buffers with larger values', input: { values: [10, 20, 30, 40], alias: false } },
{ label: 'Output aliases input', input: { values: [1, 2, 3, 4], alias: true } },
{ label: 'Separate buffers with more elements', input: { values: [1, 2, 3, 4, 5, 6], alias: false } },
],
reference({ values }) {
const out = values.slice();
for (let i = 1; i < values.length; i++) out[i] = values[i] + values[i - 1];
return out;
},
candidate({ values, alias }) {
const input = values.slice();
const out = alias ? input : input.slice();
for (let i = 1; i < input.length; i++) out[i] = input[i] + input[i - 1];
return out;
},
lesson: 'When output aliases input, each write changes the left neighbor read by the next step. Separate-buffer tests never exercise that dependency.',
},
];
export function compare(reference, candidate) {
if (reference.length !== candidate.length) return { index: -1, expected: reference.length, observed: candidate.length };
for (let index = 0; index < reference.length; index++) {
if (!Object.is(reference[index], candidate[index])) return { index, expected: reference[index], observed: candidate[index] };
}
return null;
}
export function runTest(testCase, choice) {
const expected = testCase.reference(choice.input);
const observed = testCase.candidate(choice.input);
return compare(expected, observed);
}