File size: 2,179 Bytes
d6c87b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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);
  }
});