DronePilot / core /model.js
edgarmoreau's picture
SkyJEPA task card: write the goals and costs, a 5.6k-param world model flies them in the browser
c6f6a31 verified
Raw History Blame Contribute Delete
8.81 kB
// SkyJEPA in plain JS: dronepilot/bodies/skyjepa_quad/world_model.py (rollout) with models/{encoders,predictor,prober}.py.
// Encode the state history with a causal TCN, encode past + candidate rotor commands with another, unroll a GRU in
// latent space, and let a small prober turn each predicted latent into residual accelerations that correct a nominal
// rigid-body quadrotor integrated on SO(3). About 5.6k parameters in the encoders + predictor, 1.6k in the prober.
//
// State (18) = [p, v, R columns, w] (z up, metres). Action (4) = rotor thrusts in newtons, LeDrone's order:
// M0 front-left, M1 back-right, M2 front-right, M3 back-left.
export const G = 9.81, MASS = 1.3, DT = 0.05;
export const HOVER = MASS * G / 4, A_MIN = 0, A_MAX = 4 * MASS * G / 4;
const J = [0.011, 0.011, 0.021], DRAG = [0.2, 0.2, 0.2], ALPHA = 0.03, CQ = 0.016, ARM = 0.17;
const d = ARM / Math.SQRT2;
// nominal allocation f -> [tau_roll, tau_pitch, tau_yaw] (rows), sim/quadrotor.py allocation_matrix()[1:4]
export const B_TORQUE = [[d, -d, -d, d], [-d, d, -d, d], [-CQ, -CQ, CQ, CQ]];
const f64 = a => Float64Array.from(a.flat(Infinity));
function conv(w, b) { const [O, I, K] = [w.length, w[0].length, w[0][0].length]; return { O, I, K, w: f64(w), b: f64(b) }; }
function tcn(sd, pre) {
return { layers: [0, 2, 4].map(i => conv(sd[`${pre}.tcn.${i}.conv.weight`], sd[`${pre}.tcn.${i}.conv.bias`])),
out: conv(sd[`${pre}.readout.weight`], sd[`${pre}.readout.bias`]) };
}
function lin(w, b) { return { O: w.length, I: w[0].length, w: f64(w), b: f64(b) }; }
export function loadModel(js) {
const j = js.jepa, p = js.prober, n = js.norm;
return {
H: js.history,
sEnc: tcn(j, "state_enc"), aEnc: tcn(j, "action_enc"),
gru: { Wi: f64(j["predictor.cell.weight_ih"]), Wh: f64(j["predictor.cell.weight_hh"]),
bi: f64(j["predictor.cell.bias_ih"]), bh: f64(j["predictor.cell.bias_hh"]), Hd: 24 },
rd: lin(j["predictor.readout.weight"], j["predictor.readout.bias"]),
pr: [lin(p["net.0.weight"], p["net.0.bias"]), lin(p["net.2.weight"], p["net.2.bias"]), lin(p["net.4.weight"], p["net.4.bias"])],
sm: f64(n.state_mean), ss: f64(n.state_std), am: f64(n.action_mean), as: f64(n.action_std),
source: js.source,
};
}
/** Causal TCN over x (L, C) row-major -> per-step latents (L, out). Zero left padding, ReLU after each conv. */
function runTCN(enc, x, L) {
let h = x, C = enc.layers[0].I;
for (const c of enc.layers) {
const y = new Float64Array(L * c.O);
for (let t = 0; t < L; t++)
for (let o = 0; o < c.O; o++) {
let s = c.b[o];
for (let i = 0; i < c.I; i++)
for (let k = 0; k < c.K; k++) {
const tt = t - (c.K - 1) + k;
if (tt >= 0) s += c.w[(o * c.I + i) * c.K + k] * h[tt * C + i];
}
y[t * c.O + o] = s > 0 ? s : 0;
}
h = y; C = c.O;
}
const r = enc.out, z = new Float64Array(L * r.O);
for (let t = 0; t < L; t++)
for (let o = 0; o < r.O; o++) {
let s = r.b[o];
for (let i = 0; i < r.I; i++) s += r.w[o * r.I + i] * h[t * C + i];
z[t * r.O + o] = s;
}
return z;
}
function dense(l, x, y, relu) {
for (let o = 0; o < l.O; o++) {
let s = l.b[o];
for (let i = 0; i < l.I; i++) s += l.w[o * l.I + i] * x[i];
y[o] = relu && s < 0 ? 0 : s;
}
return y;
}
const sig = x => 1 / (1 + Math.exp(-x));
// R = R @ exp(hat(w h)), R stored as rows-major 3x3 in r[9]
function rotStep(r, wx, wy, wz, h) {
const px = wx * h, py = wy * h, pz = wz * h, th2 = px * px + py * py + pz * pz, th = Math.sqrt(th2);
let a, b;
if (th < 1e-6) { a = 1 - th2 / 6; b = 0.5 - th2 / 24; } else { a = Math.sin(th) / th; b = (1 - Math.cos(th)) / th2; }
// E = I + a K + b K^2, K = hat(p)
const E = [1 + b * (-py * py - pz * pz), -a * pz + b * px * py, a * py + b * px * pz,
a * pz + b * px * py, 1 + b * (-px * px - pz * pz), -a * px + b * py * pz,
-a * py + b * px * pz, a * px + b * py * pz, 1 + b * (-px * px - py * py)];
const o = new Float64Array(9);
for (let i = 0; i < 3; i++) for (let j = 0; j < 3; j++)
o[i * 3 + j] = r[i * 3] * E[j] + r[i * 3 + 1] * E[3 + j] + r[i * 3 + 2] * E[6 + j];
r.set(o);
}
/** Encode the state history once per plan: stateHist (H+1, 18) array of arrays -> latent s0 (16). */
export function encodeState(m, stateHist) {
const L = stateHist.length, x = new Float64Array(L * 18);
for (let t = 0; t < L; t++) for (let i = 0; i < 18; i++) x[t * 18 + i] = (stateHist[t][i] - m.sm[i]) / m.ss[i];
const z = runTCN(m.sEnc, x, L);
return z.slice((L - 1) * 16, L * 16);
}
/** rollout(m, stateHist (H+1)x18, actionHist (>=H)x4, plans Float32Array (S*T*4), S, T) -> Float32Array (S*T*18). */
export function rollout(m, stateHist, actionHist, plans, S, T, s0 = null) {
const H = m.H, past = actionHist.slice(-H), L = H + T;
s0 = s0 || encodeState(m, stateHist);
const x0 = stateHist[stateHist.length - 1];
const out = new Float32Array(S * T * 18);
const seq = new Float64Array(L * 4);
for (let t = 0; t < H; t++) for (let i = 0; i < 4; i++) seq[t * 4 + i] = (past[t][i] - m.am[i]) / m.as[i];
const g = m.gru, Hd = g.Hd, gi = new Float64Array(72), gh = new Float64Array(72);
const inp = new Float64Array(24), hid = new Float64Array(Hd), s = new Float64Array(16);
const p1 = new Float64Array(32), p2 = new Float64Array(32), po = new Float64Array(15);
const R = new Float64Array(9), fa = new Float64Array(4), lag = Math.exp(-(DT / 5) / ALPHA), h = DT / 5;
for (let n = 0; n < S; n++) {
const base = n * T * 4;
for (let t = 0; t < T; t++) for (let i = 0; i < 4; i++) seq[(H + t) * 4 + i] = (plans[base + t * 4 + i] - m.am[i]) / m.as[i];
const z = runTCN(m.aEnc, seq, L); // (L, 8); the rollout uses steps H..H+T-1
// GRU unroll from s0
hid.fill(0); s.set(s0);
let px = x0[0], py = x0[1], pz = x0[2], vx = x0[3], vy = x0[4], vz = x0[5];
for (let i = 0; i < 3; i++) for (let j = 0; j < 3; j++) R[i * 3 + j] = x0[6 + j * 3 + i]; // columns -> rows-major
let wx = x0[15], wy = x0[16], wz = x0[17];
for (let i = 0; i < 4; i++) fa[i] = plans[base + i];
for (let k = 0; k < T; k++) {
inp.set(s); for (let i = 0; i < 8; i++) inp[16 + i] = z[(H + k) * 8 + i];
for (let r = 0; r < 72; r++) {
let a = g.bi[r], b = g.bh[r];
for (let i = 0; i < 24; i++) a += g.Wi[r * 24 + i] * inp[i];
for (let i = 0; i < Hd; i++) b += g.Wh[r * Hd + i] * hid[i];
gi[r] = a; gh[r] = b;
}
for (let i = 0; i < Hd; i++) {
const rr = sig(gi[i] + gh[i]), zz = sig(gi[Hd + i] + gh[Hd + i]);
const nn = Math.tanh(gi[2 * Hd + i] + rr * gh[2 * Hd + i]);
hid[i] = (1 - zz) * nn + zz * hid[i];
}
dense(m.rd, hid, s, false);
// prober: latent -> dvdot (3), K (3x4)
dense(m.pr[2], dense(m.pr[1], dense(m.pr[0], s, p1, true), p2, true), po, false);
const a0 = plans[base + k * 4], a1 = plans[base + k * 4 + 1], a2 = plans[base + k * 4 + 2], a3 = plans[base + k * 4 + 3];
const Ka0 = po[3] * a0 + po[4] * a1 + po[5] * a2 + po[6] * a3;
const Ka1 = po[7] * a0 + po[8] * a1 + po[9] * a2 + po[10] * a3;
const Ka2 = po[11] * a0 + po[12] * a1 + po[13] * a2 + po[14] * a3;
for (let sub = 0; sub < 5; sub++) {
fa[0] = a0 + (fa[0] - a0) * lag; fa[1] = a1 + (fa[1] - a1) * lag;
fa[2] = a2 + (fa[2] - a2) * lag; fa[3] = a3 + (fa[3] - a3) * lag;
const thr = (fa[0] + fa[1] + fa[2] + fa[3]) / MASS;
const ax = thr * R[2] - (DRAG[0] / MASS) * vx + po[0];
const ay = thr * R[5] - (DRAG[1] / MASS) * vy + po[1];
const az = thr * R[8] - G - (DRAG[2] / MASS) * vz + po[2];
const t0 = B_TORQUE[0][0] * fa[0] + B_TORQUE[0][1] * fa[1] + B_TORQUE[0][2] * fa[2] + B_TORQUE[0][3] * fa[3];
const t1 = B_TORQUE[1][0] * fa[0] + B_TORQUE[1][1] * fa[1] + B_TORQUE[1][2] * fa[2] + B_TORQUE[1][3] * fa[3];
const t2 = B_TORQUE[2][0] * fa[0] + B_TORQUE[2][1] * fa[1] + B_TORQUE[2][2] * fa[2] + B_TORQUE[2][3] * fa[3];
const Jx = J[0] * wx, Jy = J[1] * wy, Jz = J[2] * wz;
const gx = wy * Jz - wz * Jy, gy = wz * Jx - wx * Jz, gz = wx * Jy - wy * Jx;
const dwx = (t0 - gx) / J[0] + Ka0, dwy = (t1 - gy) / J[1] + Ka1, dwz = (t2 - gz) / J[2] + Ka2;
px += vx * h; py += vy * h; pz += vz * h;
vx += ax * h; vy += ay * h; vz += az * h;
rotStep(R, wx, wy, wz, h);
wx += dwx * h; wy += dwy * h; wz += dwz * h;
}
const o = (n * T + k) * 18;
out[o] = px; out[o + 1] = py; out[o + 2] = pz; out[o + 3] = vx; out[o + 4] = vy; out[o + 5] = vz;
for (let j = 0; j < 3; j++) for (let i = 0; i < 3; i++) out[o + 6 + j * 3 + i] = R[i * 3 + j];
out[o + 15] = wx; out[o + 16] = wy; out[o + 17] = wz;
}
}
return out;
}