Download shape-math.test.mjs from tensorviz-org/pytorch-shape-lab: direct link, hf CLI and curl.
- Browser
- Download file 2.18 kB
-
https://huggingface.co/spaces/tensorviz-org/pytorch-shape-lab/resolve/main/shape-math.test.mjs
- Command line
-
hf download hf://spaces/tensorviz-org/pytorch-shape-lab/shape-math.test.mjs
-
curl -L -o shape-math.test.mjs https://huggingface.co/spaces/tensorviz-org/pytorch-shape-lab/resolve/main/shape-math.test.mjs
2.18 kB
| import test from 'node:test'; | |
| import assert from 'node:assert/strict'; | |
| import { inspectShapes } from './shape-math.mjs'; | |
| test('the canonical mismatch keeps two images and locates the feature error', () => { | |
| const r = inspectShapes({ batch: 2, height: 16, width: 16 }); | |
| assert.deepEqual(r.flattened, [2, 512]); | |
| assert.deepEqual(r.weight, [5, 128]); | |
| assert.equal(r.error, 'mat1 and mat2 shapes cannot be multiplied (2x512 and 128x5)'); | |
| assert.equal(r.output, null); | |
| }); | |
| test('a fixed-width repair preserves batch but does not promise arbitrary inputs', () => { | |
| for (const batch of [1, 2, 5]) { | |
| assert.deepEqual(inspectShapes({ batch, height: 16, width: 16, mode: 'fixed' }).output, [batch, 5]); | |
| } | |
| assert.equal(inspectShapes({ batch: 2, height: 16, width: 20, mode: 'fixed' }).matches, false); | |
| }); | |
| test('odd dimensions use floor pooling and the explicit adaptive stage changes the head', () => { | |
| const r = inspectShapes({ batch: 3, height: 17, width: 19, mode: 'pooled' }); | |
| assert.deepEqual(r.pool, [3, 8, 8, 9]); | |
| assert.deepEqual(r.adaptive, [3, 8, 1, 1]); | |
| assert.deepEqual(r.flattened, [3, 8]); | |
| assert.deepEqual(r.output, [3, 5]); | |
| }); | |
| test('making Linear run by reshape must not be mistaken for preserving samples', () => { | |
| const r = inspectShapes({ batch: 2, height: 16, width: 16 }); | |
| assert.deepEqual(r.unsafeReshape.shape, [8, 128]); | |
| assert.deepEqual(r.unsafeReshape.output, [8, 5]); | |
| assert.equal(r.unsafeReshape.preservesBatch, false); | |
| assert.equal(inspectShapes({ batch: 1, height: 3, width: 3 }).unsafeReshape.possible, false); | |
| assert.equal(inspectShapes({ batch: 2, height: 8, width: 8 }).unsafeReshape.preservesBatch, true); | |
| }); | |
| test('invalid inputs are rejected instead of showing stale or impossible shapes', () => { | |
| const base = { batch: 2, height: 16, width: 16 }; | |
| for (const [name, value] of [['batch', 0], ['batch', 65], ['height', 1], ['width', 257], ['width', 2.5], ['height', NaN], ['batch', Infinity], ['mode', 'other']]) { | |
| const r = inspectShapes({ ...base, [name]: value }); | |
| assert.equal(r.valid, false, `${name}=${value}`); | |
| assert.ok(r.message); | |
| assert.equal(r.flattened, undefined); | |
| } | |
| }); | |