@hackbuild/motion-net 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,15 @@
1
+ import { type WasmSource } from "./load.js";
2
+ export type { WasmSource } from "./load.js";
3
+ export * from "./layout.js";
4
+ export * from "./weights.js";
5
+ export * from "./engine.js";
6
+ /** Loads the wasm for the page side helpers (golden trace). The predictor loads its own copy. */
7
+ export declare function load(source?: WasmSource): Promise<void>;
8
+ export declare function loadSync(source: ArrayBuffer | ArrayBufferView | WebAssembly.Module): void;
9
+ /** The seeded random network trace that the native and wasm builds must agree on. */
10
+ export declare function goldenTrace(seed: number): Float64Array;
11
+ /** Uniform numbers in [0, 1); the family contract. */
12
+ export type Rng = () => number;
13
+ /** The linear congruential generator of the specification's test vectors. */
14
+ export declare function lcg(seed: number): Rng;
15
+ export * from "./host.js";
package/dist/index.js ADDED
@@ -0,0 +1,31 @@
1
+ // @hackbuild/motion-net: a runtime for the categorical codebook matching
2
+ // quadruped network and a gait host that drives a dog from it. The network
3
+ // runs in Rust on wasm SIMD128 inside a worker, or in the page when a
4
+ // worker is refused. Weights are CC BY-NC 4.0 and are fetched at run time.
5
+ import { assertReady, loadWasm, loadWasmSync } from "./load.js";
6
+ import * as wasm from "./wasm/motion_net.js";
7
+ export * from "./layout.js";
8
+ export * from "./weights.js";
9
+ export * from "./engine.js";
10
+ const PKG = "@hackbuild/motion-net";
11
+ /** Loads the wasm for the page side helpers (golden trace). The predictor loads its own copy. */
12
+ export async function load(source) {
13
+ await loadWasm(source);
14
+ }
15
+ export function loadSync(source) {
16
+ loadWasmSync(source);
17
+ }
18
+ /** The seeded random network trace that the native and wasm builds must agree on. */
19
+ export function goldenTrace(seed) {
20
+ assertReady(PKG);
21
+ return wasm.goldenTrace(seed);
22
+ }
23
+ /** The linear congruential generator of the specification's test vectors. */
24
+ export function lcg(seed) {
25
+ let s = seed >>> 0;
26
+ return () => {
27
+ s = (Math.imul(s, 1664525) + 1013904223) >>> 0;
28
+ return s / 4294967296;
29
+ };
30
+ }
31
+ export * from "./host.js";
@@ -0,0 +1,43 @@
1
+ export declare const BONES: readonly ["Hips", "Spine", "Spine1", "Neck", "Head", "HeadSite", "LeftShoulder", "LeftArm", "LeftForeArm", "LeftHand", "LeftHandSite", "RightShoulder", "RightArm", "RightForeArm", "RightHand", "RightHandSite", "LeftUpLeg", "LeftLeg", "LeftFoot", "LeftFootSite", "RightUpLeg", "RightLeg", "RightFoot", "RightFootSite", "Tail", "Tail1", "Tail1Site"];
2
+ export type BoneName = (typeof BONES)[number];
3
+ export declare const BONE_COUNT = 27;
4
+ /** Parent index per bone, -1 for the root. */
5
+ export declare const PARENTS: readonly number[];
6
+ export declare const STYLES: readonly ["Canter", "Idle", "Jump", "Lie", "Pace", "Sit", "Stand", "Trot", "Walk"];
7
+ export type StyleLabel = (typeof STYLES)[number];
8
+ /** Paws in contact order: front left, front right, hind left, hind right. */
9
+ export declare const PAWS: readonly ["front left", "front right", "hind left", "hind right"];
10
+ /** Per paw: the end site, the paw bone, the lower and upper limb bones. */
11
+ export declare const PAW_CHAINS: readonly {
12
+ site: number;
13
+ paw: number;
14
+ lower: number;
15
+ upper: number;
16
+ }[];
17
+ /** Input vector offsets. */
18
+ export declare const IN: {
19
+ readonly positions: 0;
20
+ readonly velocities: 81;
21
+ readonly trajPositions: 162;
22
+ readonly trajDirections: 194;
23
+ readonly trajVelocities: 226;
24
+ readonly style: 258;
25
+ readonly length: 339;
26
+ };
27
+ /** Output vector offsets, per frame. */
28
+ export declare const OUT: {
29
+ readonly dx: 0;
30
+ readonly dyaw: 1;
31
+ readonly dz: 2;
32
+ readonly positions: 3;
33
+ readonly axisZ: 84;
34
+ readonly axisY: 165;
35
+ readonly velocities: 246;
36
+ readonly contacts: 327;
37
+ readonly style: 331;
38
+ readonly length: 412;
39
+ };
40
+ export declare const FRAMES = 16;
41
+ /** Seconds between predicted frames and between trajectory samples. */
42
+ export declare const FRAME_DT: number;
43
+ export declare const TRAJ_SAMPLES = 16;
package/dist/layout.js ADDED
@@ -0,0 +1,80 @@
1
+ // Data facts of the published quadruped network: bone order, input and
2
+ // output layout, style labels. Fixed by training; the host reads them from
3
+ // here and checks the manifest against them.
4
+ export const BONES = [
5
+ "Hips",
6
+ "Spine",
7
+ "Spine1",
8
+ "Neck",
9
+ "Head",
10
+ "HeadSite",
11
+ "LeftShoulder",
12
+ "LeftArm",
13
+ "LeftForeArm",
14
+ "LeftHand",
15
+ "LeftHandSite",
16
+ "RightShoulder",
17
+ "RightArm",
18
+ "RightForeArm",
19
+ "RightHand",
20
+ "RightHandSite",
21
+ "LeftUpLeg",
22
+ "LeftLeg",
23
+ "LeftFoot",
24
+ "LeftFootSite",
25
+ "RightUpLeg",
26
+ "RightLeg",
27
+ "RightFoot",
28
+ "RightFootSite",
29
+ "Tail",
30
+ "Tail1",
31
+ "Tail1Site",
32
+ ];
33
+ export const BONE_COUNT = 27;
34
+ /** Parent index per bone, -1 for the root. */
35
+ export const PARENTS = BONES.map((name, i) => {
36
+ if (i === 0)
37
+ return -1;
38
+ if (name === "Spine" || name === "LeftUpLeg" || name === "RightUpLeg" || name === "Tail")
39
+ return 0;
40
+ if (name === "Neck" || name === "LeftShoulder" || name === "RightShoulder")
41
+ return 2;
42
+ return i - 1;
43
+ });
44
+ export const STYLES = ["Canter", "Idle", "Jump", "Lie", "Pace", "Sit", "Stand", "Trot", "Walk"];
45
+ /** Paws in contact order: front left, front right, hind left, hind right. */
46
+ export const PAWS = ["front left", "front right", "hind left", "hind right"];
47
+ /** Per paw: the end site, the paw bone, the lower and upper limb bones. */
48
+ export const PAW_CHAINS = [
49
+ { site: 10, paw: 9, lower: 8, upper: 7 },
50
+ { site: 15, paw: 14, lower: 13, upper: 12 },
51
+ { site: 19, paw: 18, lower: 17, upper: 16 },
52
+ { site: 23, paw: 22, lower: 21, upper: 20 },
53
+ ];
54
+ /** Input vector offsets. */
55
+ export const IN = {
56
+ positions: 0,
57
+ velocities: 81,
58
+ trajPositions: 162,
59
+ trajDirections: 194,
60
+ trajVelocities: 226,
61
+ style: 258,
62
+ length: 339,
63
+ };
64
+ /** Output vector offsets, per frame. */
65
+ export const OUT = {
66
+ dx: 0,
67
+ dyaw: 1,
68
+ dz: 2,
69
+ positions: 3,
70
+ axisZ: 84,
71
+ axisY: 165,
72
+ velocities: 246,
73
+ contacts: 327,
74
+ style: 331,
75
+ length: 412,
76
+ };
77
+ export const FRAMES = 16;
78
+ /** Seconds between predicted frames and between trajectory samples. */
79
+ export const FRAME_DT = 1 / 30;
80
+ export const TRAJ_SAMPLES = 16;
package/dist/load.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ export type WasmSource = URL | string | Response | ArrayBuffer | ArrayBufferView | WebAssembly.Module;
2
+ export declare function wasmReady(): boolean;
3
+ /** Instantiates the wasm. Later calls return the first load. */
4
+ export declare function loadWasm(source?: WasmSource): Promise<void>;
5
+ /** Synchronous load from bytes or a compiled module. */
6
+ export declare function loadWasmSync(source: ArrayBuffer | ArrayBufferView | WebAssembly.Module): void;
7
+ export declare function assertReady(pkg: string): void;
package/dist/load.js ADDED
@@ -0,0 +1,42 @@
1
+ // Loads the motion-net wasm once. The source can be a URL (fetched), bytes
2
+ // (ArrayBuffer or a typed array), a compiled WebAssembly.Module, or nothing,
3
+ // in which case the base64 copy in ./wasm/inline.js is decoded, which works
4
+ // from file:// where fetch of a local file is refused.
5
+ import initAsync, { initSync as initFromBytes } from "./wasm/motion_net.js";
6
+ let pending = null;
7
+ let ready = false;
8
+ const isBytes = (s) => s instanceof ArrayBuffer || ArrayBuffer.isView(s) || (typeof WebAssembly !== "undefined" && s instanceof WebAssembly.Module);
9
+ export function wasmReady() {
10
+ return ready;
11
+ }
12
+ /** Instantiates the wasm. Later calls return the first load. */
13
+ export function loadWasm(source) {
14
+ if (pending)
15
+ return pending;
16
+ pending = (async () => {
17
+ let input = source;
18
+ if (input === undefined)
19
+ input = (await import("./wasm/inline.js")).wasmBytes();
20
+ if (isBytes(input))
21
+ initFromBytes({ module: input });
22
+ else
23
+ await initAsync({ module_or_path: input });
24
+ ready = true;
25
+ })();
26
+ pending.catch(() => {
27
+ pending = null;
28
+ });
29
+ return pending;
30
+ }
31
+ /** Synchronous load from bytes or a compiled module. */
32
+ export function loadWasmSync(source) {
33
+ if (ready)
34
+ return;
35
+ initFromBytes({ module: source });
36
+ ready = true;
37
+ pending = Promise.resolve();
38
+ }
39
+ export function assertReady(pkg) {
40
+ if (!ready)
41
+ throw new Error(`${pkg}: wasm not loaded. call load() first.`);
42
+ }