forwardfig 0.1.0 → 0.1.2
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.
- package/README.md +8 -5
- package/dist/api.d.ts +13 -1
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +24 -0
- package/dist/forwardfig.iife.js +7 -7
- package/dist/forwardfig.js +7 -7
- package/dist/index.d.ts +1 -0
- package/dist/layout.d.ts +2 -0
- package/dist/tex.d.ts +4 -2
- package/dist/types.d.ts +7 -0
- package/package.json +15 -2
package/README.md
CHANGED
|
@@ -53,7 +53,7 @@ add(W(x), B(A(x).edge('$r$')))
|
|
|
53
53
|
|
|
54
54
|
All layers take `(label?, options?)` or `(options?)`; `image(src, …)` and `vector(values, …)` take their data first.
|
|
55
55
|
|
|
56
|
-
- Basic: `block` `thin` `text` `circle` `op('+' | '*' | '~')` `enc` `dec` `tensor({ size: [w,h,d] })` `stack({ n, size, offset })` `neurons({ n })` `vector(values, label?, options?)`
|
|
56
|
+
- Basic: `block` `thin` `text` `empty` (empty space, for arrows from / into the void) `circle` `op('+' | '*' | '~')` `enc` `dec` `tensor({ size: [w,h,d] })` `stack({ n, size, offset })` `neurons({ n })` `vector(values, label?, options?)`
|
|
57
57
|
- ML presets: `mha` `attention` `ffn` `mlp` `norm` `embed` `linear` `softmax` `matmul` `concat` `conv` `pool` `act` `dropout` `loss`
|
|
58
58
|
- Functional: `add(...xs)` (⊕) and `mul(...xs)` (⊗) create a new node on each call.
|
|
59
59
|
- Grids: `matrix({ rows, cols, cells, cellSize })` (2D) and `voxels({ rows, cols, depth, cells, cellSize })` (3D, the front, top and right faces as cells). `cells` is a 2D array or `(r, c) => cell` (`(r, c, d) => cell` for voxels, with d = 0 in front). A cell is a number in 0–1 (a shade of the block's colour), a colour string, or `{ value | color, texture, label, crossed }`. When `cells` is omitted, varied shades are used. Textures inside cells use the lightweight vector looks. `axes: { rows, cols, depth }` names the extent along each axis (TeX works). `cols` goes under the bottom edge, `rows` to the left and `depth` beside the receding edge; any that would sit on an arrow are moved off its line.
|
|
@@ -85,6 +85,7 @@ Common options: `color` `texture` `frozen` `trainable` `crossed` `dashed` `bold`
|
|
|
85
85
|
|
|
86
86
|
When both are set, `texture` decides the surface, and the ❄/🔥 badge follows `freeze()` / `trainable()` (for example `block({ texture: 'quantized' }).freeze()` in QLoRA). Without WebGL, glass, metal and noise fall back to lightweight vector versions.
|
|
87
87
|
|
|
88
|
+
- `new Figure({ legend: true })` adds a key under the figure for the looks it uses (❄ frozen, 🔥 trainable, each texture, ✕ crossed). `legend: { position: 'right', labels: { quantized: '4-bit' } }` moves it beside the figure and renames entries.
|
|
88
89
|
- Placement is automatic by default. To override it, use `lane` (fractions allowed), `align: ref`, `leftOf` / `rightOf` / `above` / `below: ref` (with `dist`), `at: [x, y]` or `nudge: [dx, dy]`.
|
|
89
90
|
- `freeze()` / `trainable()` can be called on a layer or on a module. On a module they apply to every block inside it, unless a layer is marked explicitly.
|
|
90
91
|
- A 🧊 or 🔥 in a label works the same way.
|
|
@@ -123,7 +124,7 @@ Like matplotlib's rcParams, you can set defaults globally and narrow them down b
|
|
|
123
124
|
|
|
124
125
|
```ts
|
|
125
126
|
configure({ style: { fontSize: 14 }, kinds: { mha: { color: 'purple' } } }) // whole library
|
|
126
|
-
new Figure({ style: { color: 'gray' } })
|
|
127
|
+
new Figure({ style: { color: 'gray', fontFamily: 'Times New Roman, serif' } }) // this figure
|
|
127
128
|
new Encoder({ style: { fontSize: 11, texture: 'glass' } }) // blocks made inside this module
|
|
128
129
|
block('x', { fontSize: 20 }) // the block itself wins
|
|
129
130
|
configure(null) // reset
|
|
@@ -131,9 +132,9 @@ configure(null) // r
|
|
|
131
132
|
|
|
132
133
|
- Precedence, strongest first: the block's own options > inner Module > outer Module > Figure > `configure()` > the preset's defaults.
|
|
133
134
|
- At each level, `kinds[kind]` (for one preset) beats `style` (for all blocks).
|
|
134
|
-
- `style` can set `color` `texture` `fontSize` `dashed` `bold`. At the Figure / `configure` level, `fontSize` also sizes edge labels, group titles and captions.
|
|
135
|
+
- `style` can set `color` `texture` `fontSize` `fontFamily` `dashed` `bold`. At the Figure / `configure` level, `fontSize` also sizes edge labels, group titles and captions, and `fontFamily` (any CSS font family) sets the font of the whole figure, e.g. to match a paper's body text. Math (`$...$`) keeps its TeX font.
|
|
135
136
|
- A `texture` set through style doesn't override `freeze()` / `trainable()` (ice and magma win). A texture set on the block itself does.
|
|
136
|
-
- `configure` also takes `theme` `flow` `gap` `materials`.
|
|
137
|
+
- `configure` also takes `theme` `flow` `gap` `materials` `legend`.
|
|
137
138
|
|
|
138
139
|
## Export
|
|
139
140
|
|
|
@@ -147,6 +148,8 @@ fig.svg() // string (works in Node too)
|
|
|
147
148
|
- Raster formats (png / jpeg / webp) need a browser (canvas). External image URLs inside are inlined as data URLs before export. JPEG gets a white background by default.
|
|
148
149
|
- PDF isn't supported. For papers, an SVG export (vector) can go into LaTeX via Inkscape or `svg2pdf`.
|
|
149
150
|
|
|
151
|
+
From the command line (e.g. in a Makefile), `npx forwardfig model.ts -o fig.svg` writes the module's default export (a `Figure`, or a function returning one). The format follows the extension: `.svg`, or `.png` / `.jpg` / `.jpeg` / `.webp`, which need the optional peer dependency `sharp` (`npm i -D sharp`) and take `--scale` (default 2) and `--background`. Without a default export, each exported `Figure` becomes `<name>.svg` in the `-o` directory. `--theme dark` picks the theme. `.ts` files need Node ≥ 22.6 (`NODE_OPTIONS=--experimental-strip-types` before 23.6) or Bun.
|
|
152
|
+
|
|
150
153
|
## Drawing core
|
|
151
154
|
|
|
152
155
|
The layout and rendering engine is independent of the API above. `fig.diagram()` returns plain `Diagram` data (nodes, edges, groups, ranks), which `draw(diagram, { theme })` turns into SVG. Anything that can generate a Diagram, from another language or as JSON, can reuse the same engine.
|
|
@@ -157,4 +160,4 @@ The layout and rendering engine is independent of the API above. `fig.diagram()`
|
|
|
157
160
|
|
|
158
161
|
To develop locally: run `bun install && bun test && bun run build`, then `python3 -m http.server` and open `demo/`. `bun run docs:dev` builds the docs in `doc/` and serves them at http://localhost:4321. Pushing to `main` deploys the demo to GitHub Pages.
|
|
159
162
|
|
|
160
|
-
Known limits: crossings are not minimized (
|
|
163
|
+
Known limits: crossings are reduced by reordering lanes, not minimized (a greedy pass that stops at a local minimum, and never moves a `lane` you set). TeX is a subset.
|
package/dist/api.d.ts
CHANGED
|
@@ -204,7 +204,7 @@ export declare class Tensor {
|
|
|
204
204
|
/** Style the edge(s) this tensor flows into next: x.edge('$r$'), x.edge({ dashed: true }) */
|
|
205
205
|
edge(style: EdgeOptions | string): Tensor;
|
|
206
206
|
}
|
|
207
|
-
export type Shape = 'block' | 'thin' | 'text' | 'circle' | 'op' | 'enc' | 'dec' | 'tensor' | 'stack' | 'neurons' | 'vector' | 'matrix' | 'voxels' | 'image' | 'magnify' | 'gaussian' | 'space' | 'sphere' | 'pointcloud' | 'waveform';
|
|
207
|
+
export type Shape = 'block' | 'thin' | 'text' | 'circle' | 'op' | 'enc' | 'dec' | 'tensor' | 'stack' | 'neurons' | 'empty' | 'vector' | 'matrix' | 'voxels' | 'image' | 'magnify' | 'gaussian' | 'space' | 'sphere' | 'pointcloud' | 'waveform';
|
|
208
208
|
export interface Layer<O extends NodeOptions = NodeOptions> {
|
|
209
209
|
(...inputs: Tensor[]): Tensor;
|
|
210
210
|
readonly shape: Shape;
|
|
@@ -245,6 +245,8 @@ export declare const dec: Factory<NodeOptions>;
|
|
|
245
245
|
export declare const tensor: Factory<TensorOptions>;
|
|
246
246
|
export declare const stack: Factory<StackOptions>;
|
|
247
247
|
export declare const neurons: Factory<NeuronOptions>;
|
|
248
|
+
/** Empty space: a point that draws nothing, for arrows that come from or go into the void. */
|
|
249
|
+
export declare const empty: Factory<NodeOptions>;
|
|
248
250
|
export declare function vector(values: string[], a?: string | NodeOptions, b?: NodeOptions): Layer<NodeOptions>;
|
|
249
251
|
export declare const mha: Factory<NodeOptions>;
|
|
250
252
|
export declare const attention: Factory<NodeOptions>;
|
|
@@ -295,6 +297,8 @@ export interface Style {
|
|
|
295
297
|
texture?: Texture;
|
|
296
298
|
/** label size (px); at figure / configure level it also sizes edge, group and caption text */
|
|
297
299
|
fontSize?: number;
|
|
300
|
+
/** CSS font family; at figure / configure level it sets the whole figure's font */
|
|
301
|
+
fontFamily?: string;
|
|
298
302
|
dashed?: boolean;
|
|
299
303
|
bold?: boolean;
|
|
300
304
|
}
|
|
@@ -345,6 +349,14 @@ export interface FigureOptions extends StyleConfig {
|
|
|
345
349
|
gap?: V2;
|
|
346
350
|
/** bake WebGL ice / magma when available (default true) */
|
|
347
351
|
materials?: boolean;
|
|
352
|
+
/** a key to the looks used: ❄ frozen, 🔥 trainable, each texture, ✕ crossed */
|
|
353
|
+
legend?: boolean | LegendOptions;
|
|
354
|
+
}
|
|
355
|
+
export interface LegendOptions {
|
|
356
|
+
/** default 'bottom' */
|
|
357
|
+
position?: 'bottom' | 'right';
|
|
358
|
+
/** names to show instead of the defaults, e.g. { trainable: 'Trained', quantized: '4-bit' } */
|
|
359
|
+
labels?: Partial<Record<'frozen' | 'trainable' | Exclude<Texture, 'ice' | 'magma'> | 'crossed', string>>;
|
|
348
360
|
}
|
|
349
361
|
export type { TexEngine };
|
|
350
362
|
/** configure() also takes the TeX engine: MathJax is used automatically when the page has it */
|
package/dist/cli.d.ts
ADDED
package/dist/cli.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import{writeFileSync as d}from"node:fs";import{join as S,resolve as E}from"node:path";import{pathToFileURL as R}from"node:url";import{parseArgs as L}from"node:util";var l=(t)=>{let e={},r=t.split(" ");for(let i=0;i<r.length;i+=2)e[r[i]]=r[i+1];return e},N=l("alpha α beta β gamma γ delta δ epsilon ϵ varepsilon ε zeta ζ eta η theta θ vartheta ϑ iota ι kappa κ lambda λ mu μ nu ν xi ξ pi π rho ρ sigma σ tau τ upsilon υ phi ϕ varphi φ chi χ psi ψ omega ω Gamma Γ Delta Δ Theta Θ Lambda Λ Xi Ξ Pi Π Sigma Σ Phi Φ Psi Ψ Omega Ω"),C=l("cdot · times × oplus ⊕ otimes ⊗ odot ⊙ to → rightarrow → leftarrow ← gets ← Rightarrow ⇒ leftrightarrow ↔ mapsto ↦ sum ∑ prod ∏ int ∫ infty ∞ partial ∂ nabla ∇ in ∈ notin ∉ sim ∼ approx ≈ propto ∝ leq ≤ le ≤ geq ≥ ge ≥ neq ≠ ne ≠ pm ± mp ∓ ldots … dots … cdots ⋯ vdots ⋮ mid ∣ | ‖ ast ∗ star ⋆ circ ∘ bullet • forall ∀ exists ∃ emptyset ∅ cup ∪ cap ∩ subset ⊂ subseteq ⊆ top ⊤ perp ⊥ langle ⟨ rangle ⟩ ell ℓ hbar ℏ dagger † prime ′ { { } } % % _ _ # # & & $ $"),f={};for(let t of"+−×·±∓⊕⊗⊙∘∗⋆∪∩")f[t]="bin";for(let t of"=<>≤≥≠≈∼∝∈∉⊂⊆→←⇒↔↦∣")f[t]="rel";for(let t of",;")f[t]="punct";f["|"]="mid";for(let t of"([⟨")f[t]="open";for(let t of")]⟩")f[t]="close";var P=new Set("log exp ln sin cos tanh max min arg argmax argmin softmax lim det tr sup inf Pr KL diag sign".split(" ")),I=l("hat ̂ widehat ̂ tilde ̃ widetilde ̃ bar ̄ overline ̅ dot ̇ ddot ̈ vec ⃗"),j=l(", : ; quad qquad ! "),T=l("B ℬ E ℰ F ℱ H ℋ I ℐ L ℒ M ℳ R ℛ"),B=l("C ℂ H ℍ N ℕ P ℙ Q ℚ R ℝ Z ℤ");var F=new Set("left right big Big bigl bigr Bigl Bigr displaystyle".split(" "));var u={light:{none:["#e2e5e9","#7f8794"],bg:"none",text:"#1d2430",sub:"#5b6474",edge:"#39414f",halo:"#fff",group:["#f5f6f9","#d5dae2"],shadow:"rgba(30,40,60,.10)",dark:!1,ice:["#f7fdff","#d2effb","#aedcf2","#6fb9de"],fire:["#fff6e8","#ffcf9f","#ee7d36"],c:{white:["#ffffff","#8b95a5"],gray:["#eceff3","#9aa3b2"],pink:["#fde1e4","#e58c98"],orange:["#fde4c8","#e69a50"],yellow:["#f4f5cc","#c0c064"],green:["#d8f1de","#68b880"],blue:["#d6eafc","#5f9fd8"],purple:["#e4defa","#9584db"],red:["#ffd7d2","#dd6559"],teal:["#d1f0ec","#48a99f"]}},dark:{none:["#2a2f37","#8a93a2"],bg:"#0e1117",text:"#e8edf5",sub:"#9aa4b5",edge:"#c3cad6",halo:"#0e1117",group:["#161a22","#2b323f"],shadow:"rgba(0,0,0,.45)",dark:!0,ice:["#29506a","#163a50","#0f2c3e","#7fd4ff"],fire:["#4a2a12","#33180a","#ff9447"],c:{white:["#1b2029","#7d8799"],gray:["#1f242d","#8792a4"],pink:["#3a1e29","#ff8ea2"],orange:["#3a2916","#ffae5c"],yellow:["#2f3018","#e3da66"],green:["#142e22","#5ad08a"],blue:["#13273c","#5ab1ff"],purple:["#241e3f","#a892ff"],red:["#3c1b19","#ff786a"],teal:["#11312f","#4cd3c3"]}}};var v=-0.65,y=0.45,et=Math.cos(v),ot=Math.sin(v),rt=Math.cos(y),st=Math.sin(y);var m=`Usage: forwardfig <file> [-o out.svg|.png|.jpg|.webp] [--theme light|dark]
|
|
3
|
+
|
|
4
|
+
Renders the module's default export (a Figure, or a function returning one) to the
|
|
5
|
+
-o file, in the format of its extension, or as SVG to stdout. Without a default export,
|
|
6
|
+
every exported Figure is written as <name>.svg into the -o directory (default: the
|
|
7
|
+
current directory).
|
|
8
|
+
|
|
9
|
+
Options:
|
|
10
|
+
-o, --out <path> output file (default export) or directory (named exports)
|
|
11
|
+
--theme <name> ${Object.keys(u).join(" | ")} (default: the figure's own)
|
|
12
|
+
--scale <n> raster resolution multiplier (default 2)
|
|
13
|
+
--background <css> raster background (default transparent; white for JPEG)
|
|
14
|
+
-h, --help show this help
|
|
15
|
+
|
|
16
|
+
.ts files need Node >= 22.6 (with --experimental-strip-types before 23.6) or Bun.
|
|
17
|
+
PNG / JPEG / WebP need sharp: npm i -D sharp`,g=(t)=>typeof t?.svg==="function",b={png:"png",jpg:"jpeg",jpeg:"jpeg",webp:"webp"};function n(t){process.stderr.write(`forwardfig: ${t}
|
|
18
|
+
`),process.exit(1)}async function A(){let t;try{t=L({allowPositionals:!0,options:{out:{type:"string",short:"o"},theme:{type:"string"},scale:{type:"string"},background:{type:"string"},help:{type:"boolean",short:"h"}}})}catch(o){n(`${o.message}
|
|
19
|
+
|
|
20
|
+
${m}`)}let{values:e,positionals:r}=t;if(e.help)return void process.stdout.write(m+`
|
|
21
|
+
`);if(r.length!==1)n(`expected one input file
|
|
22
|
+
|
|
23
|
+
${m}`);if(e.theme&&!(e.theme in u))n(`unknown theme "${e.theme}" (${Object.keys(u).join(", ")})`);let i=Number(e.scale??2);if(!(i>0))n(`--scale must be a positive number, got "${e.scale}"`);let s=e.out&&/\.(\w+)$/.exec(e.out)?.[1].toLowerCase();if(s&&s!=="svg"&&!(s in b))n(`unknown output format ".${s}" (svg, png, jpg, jpeg, webp)`);let a;try{a=await import(R(E(r[0])).href)}catch(o){n(`cannot import ${r[0]}: ${o.message}`)}let c=(o)=>o.svg({theme:e.theme});if(a.default!==void 0){let o=typeof a.default==="function"&&!g(a.default)?await a.default():a.default;if(!g(o))n(`the default export of ${r[0]} is not a Figure`);if(!e.out)process.stdout.write(c(o));else if(s&&s in b)d(e.out,await z(c(o),b[s],i,e.background));else d(e.out,c(o));return}let h=Object.entries(a).filter(([,o])=>g(o));if(!h.length)n(`${r[0]} exports no Figure (export default a Figure, or export named Figures)`);for(let[o,p]of h){let x=S(e.out??".",`${o}.svg`);d(x,c(p)),process.stderr.write(`wrote ${x}
|
|
24
|
+
`)}}async function z(t,e,r,i){let s;try{s=(await import("sharp")).default}catch{n(`${e.toUpperCase()} output needs sharp: npm i -D sharp`)}for(let h of new Set([...t.matchAll(/<image href="(https?:[^"]+)"/g)].map((o)=>o[1])))try{let o=await fetch(h.replace(/&/g,"&")),p=`data:${o.headers.get("content-type")??""};base64,${Buffer.from(await o.arrayBuffer()).toString("base64")}`;t=t.split(`href="${h}"`).join(`href="${p}"`)}catch{}let a=i??(e==="jpeg"?"#ffffff":void 0),c=s(Buffer.from(t),{density:72*r});return(a?c.flatten({background:a}):c).toFormat(e,{quality:92}).toBuffer()}A().catch((t)=>n(t?.stack??String(t)));
|
package/dist/forwardfig.iife.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
(()=>{var{defineProperty:RV,getOwnPropertyNames:i0,getOwnPropertyDescriptor:a0}=Object,t0=Object.prototype.hasOwnProperty;function r0(V){return this[V]}var s0=(V)=>{var X=(fV??=new WeakMap).get(V),K;if(X)return X;if(X=RV({},"__esModule",{value:!0}),V&&typeof V==="object"||typeof V==="function"){for(var Y of i0(V))if(!t0.call(X,Y))RV(X,Y,{get:r0.bind(V,Y),enumerable:!(K=a0(V,Y))||K.enumerable})}return fV.set(V,X),X},fV;var e0=(V)=>V;function n0(V,X){this[V]=e0.bind(null,X)}var hV=(V,X)=>{for(var K in X)RV(V,K,{get:X[K],enumerable:!0,configurable:!0,set:n0.bind(X,K)})};var R4={};hV(R4,{waveform:()=>k0,voxels:()=>b0,vector:()=>D0,thin:()=>K0,text:()=>kV,tensor:()=>O0,stack:()=>Z0,sphere:()=>u0,space:()=>w0,softmax:()=>R0,preset:()=>n,pool:()=>j0,pointcloud:()=>T0,op:()=>GV,norm:()=>W0,neurons:()=>F0,mul:()=>g0,mlp:()=>C0,mha:()=>M0,matrix:()=>h0,matmul:()=>E0,magnify:()=>y0,loss:()=>v0,linear:()=>H0,layout:()=>tJ,layer:()=>LJ,image:()=>m0,gaussian:()=>z0,figure:()=>c0,ffn:()=>G0,enc:()=>I0,embed:()=>A0,dropout:()=>S0,draw:()=>p0,default:()=>H4,dec:()=>_0,conv:()=>P0,configure:()=>l0,concat:()=>q0,circle:()=>Q0,block:()=>B0,attention:()=>N0,add:()=>x0,act:()=>L0,Tensor:()=>PJ,THEMES:()=>XV,Module:()=>mV,Graph:()=>NV,Figure:()=>CV});var yV={};hV(yV,{waveform:()=>k0,voxels:()=>b0,vector:()=>D0,thin:()=>K0,text:()=>kV,tensor:()=>O0,stack:()=>Z0,sphere:()=>u0,space:()=>w0,softmax:()=>R0,preset:()=>n,pool:()=>j0,pointcloud:()=>T0,op:()=>GV,norm:()=>W0,neurons:()=>F0,mul:()=>g0,mlp:()=>C0,mha:()=>M0,matrix:()=>h0,matmul:()=>E0,magnify:()=>y0,loss:()=>v0,linear:()=>H0,layer:()=>LJ,image:()=>m0,gaussian:()=>z0,figure:()=>c0,ffn:()=>G0,enc:()=>I0,embed:()=>A0,dropout:()=>S0,dec:()=>_0,conv:()=>P0,configure:()=>l0,concat:()=>q0,circle:()=>Q0,block:()=>B0,attention:()=>N0,add:()=>x0,act:()=>L0,Tensor:()=>PJ,Module:()=>mV,Graph:()=>NV,Figure:()=>CV});var oV='Inter,"Helvetica Neue",Helvetica,Arial,sans-serif',iV='"Latin Modern Math","STIX Two Math","Cambria Math","Times New Roman",serif',JV;function qV(V){JV=V,EV.clear()}var aV=()=>{let V=globalThis.MathJax;return V?.tex2svg?(X)=>V.tex2svg(X,{display:!1}).querySelector("svg")?.outerHTML??"":null},J4=(V,X)=>{let K=JV===void 0?aV():JV;if(!K)return null;let Y;try{Y=K(V)}catch{return null}let I=/viewBox="([^"]+)"/.exec(Y)?.[1];if(!I)return null;let[,O,D,F]=I.trim().split(/[\s,]+/).map(Number),E=X/1000;return{inner:Y.replace(/^<svg[^>]*>/,"").replace(/<\/svg>\s*$/,""),viewBox:I,w:D*E,asc:-O*E,desc:(F+O)*E}},aJ=(V)=>{let X={},K=V.split(" ");for(let Y=0;Y<K.length;Y+=2)X[K[Y]]=K[Y+1];return X},bV=aJ("alpha α beta β gamma γ delta δ epsilon ϵ varepsilon ε zeta ζ eta η theta θ vartheta ϑ iota ι kappa κ lambda λ mu μ nu ν xi ξ pi π rho ρ sigma σ tau τ upsilon υ phi ϕ varphi φ chi χ psi ψ omega ω Gamma Γ Delta Δ Theta Θ Lambda Λ Xi Ξ Pi Π Sigma Σ Phi Φ Psi Ψ Omega Ω"),xV=aJ("cdot · times × oplus ⊕ otimes ⊗ odot ⊙ to → rightarrow → leftarrow ← gets ← Rightarrow ⇒ leftrightarrow ↔ mapsto ↦ sum ∑ prod ∏ int ∫ infty ∞ partial ∂ nabla ∇ in ∈ notin ∉ sim ∼ approx ≈ propto ∝ leq ≤ le ≤ geq ≥ ge ≥ neq ≠ ne ≠ pm ± mp ∓ ldots … dots … cdots ⋯ vdots ⋮ mid ∣ | ‖ ast ∗ star ⋆ circ ∘ bullet • forall ∀ exists ∃ emptyset ∅ cup ∪ cap ∩ subset ⊂ subseteq ⊆ top ⊤ perp ⊥ langle ⟨ rangle ⟩ ell ℓ hbar ℏ dagger † prime ′ { { } } % % _ _ # # & & $ $"),xJ={};for(let V of"+−×·±∓⊕⊗⊙∘∗⋆∪∩")xJ[V]="bin";for(let V of"=<>≤≥≠≈∼∝∈∉⊂⊆→←⇒↔↦∣")xJ[V]="rel";for(let V of",;")xJ[V]="punct";xJ["|"]="mid";for(let V of"([⟨")xJ[V]="open";for(let V of")]⟩")xJ[V]="close";var V4=new Set("log exp ln sin cos tanh max min arg argmax argmin softmax lim det tr sup inf Pr KL diag sign".split(" ")),gV=aJ("hat ̂ widehat ̂ tilde ̃ widetilde ̃ bar ̄ overline ̅ dot ̇ ddot ̈ vec ⃗"),lV=aJ(", : ; quad qquad ! "),$4=aJ("B ℬ E ℰ F ℱ H ℋ I ℐ L ℒ M ℳ R ℛ"),U4=aJ("C ℂ H ℍ N ℕ P ℙ Q ℚ R ℝ Z ℤ"),cV=(V)=>$4[V]||(/[A-Z]/.test(V)?String.fromCodePoint(119964+V.charCodeAt(0)-65):V),X4=(V)=>U4[V]||(/[A-Z]/.test(V)?String.fromCodePoint(120120+V.charCodeAt(0)-65):/\d/.test(V)?String.fromCodePoint(120792+ +V):V),pV={mathbf:{up:!0,b:!0},bf:{up:!0,b:!0},boldsymbol:{b:!0},bm:{b:!0},mathrm:{up:!0},rm:{up:!0},operatorname:{up:!0},mathit:{},text:{up:!0,text:!0},textrm:{up:!0,text:!0},mathsf:{up:!0,sans:!0},textbf:{up:!0,b:!0,text:!0},mathcal:{up:!0,map:cV},mathscr:{up:!0,map:cV},mathbb:{up:!0,map:X4}},Y4=new Set("left right big Big bigl bigr Bigl Bigr displaystyle".split(" "));function B4(V){let X=0,K=(O,D,F=!1)=>{if(D.text)return{t:O,it:!1,b:!!D.b,serif:!D.sans};if(O==="-")O="−";return{t:O,it:F&&!D.up,b:!!D.b,serif:!D.sans,cls:xJ[O]}};function Y(O,D){let F=[];while(X<V.length){let E=V[X];if(E===D){X++;break}if(E==="^"||E==="_"){X++;let S=E==="^"?"sup":"sub",j=F[F.length-1];if(!j||"t"in j||j[S])F.push(j={});j[S]=I(O);continue}if(E===" "&&!O.text){X++;continue}F.push(...I(O))}return F}function I(O){while(V[X]===" "&&!O.text)X++;let D=V[X++];if(D===void 0)return[];if(D==="{")return Y(O,"}");if(D!=="\\")return[K(O.map?O.map(D):D,O,/[A-Za-z]/.test(D)&&!O.map)];let F=/^[A-Za-z]+/.exec(V.slice(X))?.[0]||V[X]||"";if(X+=F.length,bV[F])return[K(bV[F],O,/^[a-z]/.test(F))];if(xV[F])return[K(xV[F],O)];if(lV[F])return[K(lV[F],O)];if(V4.has(F))return[K(F,{...O,up:!0})];if(pV[F])return I({...O,...pV[F]});if(gV[F]){let E=I(O),S=E.find((j)=>("t"in j));if(S)S.t+=gV[F];return E}if(F==="frac"){let E=I(O),S=I(O);return[...E,K("/",O),...S]}if(F==="sqrt")return[K("√",O),...I(O)];if(Y4.has(F))return[];return[K(F,{...O,up:!0})]}return Y({})}var EV=new Map,nJ,dV=new Map;function K4(V,X,K,Y){return`${X?"italic ":""}${K===!0?700:K||400} ${V}px ${Y?'"Latin Modern Math","STIX Two Math","Cambria Math","Times New Roman",serif':'Inter,"Helvetica Neue",Helvetica,Arial,sans-serif'}`}function Q4(V){if(/[iljI.,:;'|! ]/.test(V))return 0.28;if(/[mwMW]/.test(V))return 0.86;if(/[A-Z]/.test(V))return 0.68;if(/[̀-ͯ⃗]/.test(V))return 0;if(/[ -鿿-]/.test(V))return 1;if(V===" ")return 0.28;return 0.56}function I4(V,X,K){let Y=X+"\x00"+V,I=dV.get(Y);if(I!==void 0)return I;if(nJ===void 0)nJ=typeof document<"u"?document.createElement("canvas").getContext("2d"):null;if(nJ)nJ.font=X,I=nJ.measureText(V).width;else{I=0;for(let O of V)I+=Q4(O)*K}return dV.set(Y,I),I}function qJ(V,X,K=400){let Y=V+"\x00"+X+"\x00"+K+"\x00"+(JV===void 0?!!aV():!!JV),I=EV.get(Y);if(I)return I;let O=[],D=[];for(let S of V.split(/\n|\\\\|<br\s*\/?>/)){let j=[],f=0,v=(d,h,k,MJ=!1)=>{let UJ="",s=!1,OJ="",VJ=!1;for(let t of d)if("t"in t){let IJ=t.cls??"ord",ZJ=!UJ||UJ==="bin"||UJ==="rel"||UJ==="open"||UJ==="punct";if(IJ==="bin"&&ZJ)IJ="ord";else if(IJ==="mid"&&(ZJ||VJ))IJ=VJ?"close":"open",VJ=!VJ;let NJ=0;if(MJ&&UJ&&IJ!=="punct"&&IJ!=="close"&&UJ!=="open"){if(IJ==="rel"!==(UJ==="rel"))NJ=0.2777777777777778;else if(IJ==="bin"||UJ==="bin")NJ=0.2222222222222222;else if(UJ==="punct"||IJ==="mid"||UJ==="mid")NJ=0.16666666666666666}if(s&&!t.it&&IJ!=="punct")NJ+=0.08;f+=NJ*k;let EJ=I4(t.t,K4(k,t.it,t.b||(t.serif?!1:K),t.serif),k),WJ=j[j.length-1];if(!NJ&&WJ&&WJ.dy===h&&WJ.size===k&&WJ.it===t.it&&WJ.b===t.b&&WJ.serif===t.serif)WJ.t+=t.t;else j.push({t:t.t,x:f,dy:h,size:k,it:t.it,b:t.b,serif:t.serif});f+=EJ,UJ=IJ,s=t.it,OJ=t.t.slice(-1)}else{let IJ=f,ZJ=Math.max(k*0.72,Math.min(k,8.5)),NJ=IJ,EJ=IJ;if(t.sup)f=IJ+(s?(OJ==="f"?0.2:0.08)*k:0),v(t.sup,h-k*0.38,ZJ),NJ=f,f=IJ;if(t.sub)v(t.sub,h+k*0.24,ZJ),EJ=f;f=Math.max(NJ,EJ)+0.06*k,s=!1}};S.split("$").forEach((d,h)=>{let k=h%2?J4(d,X*1.1):null;if(k)j.push({t:"",x:f,dy:0,size:X,it:!1,b:!1,serif:!0,math:k}),f+=k.w;else if(h%2)v(B4(d),0,X*1.1,!0);else if(d)v([{t:d,it:!1,b:!1,serif:!1}],0,X)}),O.push(j),D.push(f)}let F=X*1.3,E={lines:O,widths:D,w:Math.max(0,...D),h:O.length*F,lh:F,size:X};return EV.set(Y,E),E}var jV=13,LV=new Set(["tensor","stack","matrix","voxels","image","magnify","neurons","gaussian","space","sphere","pointcloud","waveform"]),SV=(V)=>JSON.parse(V.attrs.values??"[]"),vV=(V,X)=>+V.attrs.fontSize||(V.shape==="thin"||V.shape==="vector"||LV.has(V.shape)?X-1:X),OV=(V)=>{let X=(+V.tilt||0)*Math.PI/180,K=(+V.rotate||0)*Math.PI/180,[Y,I]=[Math.cos(X),-Math.sin(X)*0.35],[O,D]=[Math.cos(K),Math.sin(K)];return[O*Y-D*I,D*Y+O*I,-D,O]},GJ=(V,X)=>V?V.split(",").map(Number):X,PV={"left-of":[-1,0],"right-of":[1,0],above:[0,-1],below:[0,1]},VV=14;function _4(V,X,K){let Y=vV(V,K),I=qJ(V.label,Y,500),O,D;switch(V.shape){case"thin":O=Math.max(I.w+26,64),D=I.h+9;break;case"op":O=D=22;break;case"circle":O=D=Math.max(42,I.w+18,I.h+16);break;case"tensor":{let[F,E,S]=GJ(V.attrs.size,[36,64,20]);O=F+S*0.6,D=E+S*0.6;break}case"stack":{let[F,E]=GJ(V.attrs.size,[44,60]),S=+V.attrs.n||4,j=+V.attrs.offset||7;O=F+(S-1)*j,D=E+(S-1)*j;break}case"enc":case"dec":if(X)O=I.w+40,D=Math.max(84,I.h+44);else O=Math.max(I.w+64,120),D=Math.max(52,I.h+26);break;case"neurons":{let F=(+V.attrs.n||4)*20+4;[O,D]=X?[22,F]:[F,22];break}case"vector":{let F=SV(V),E=Math.max(36,...F.map((j)=>qJ(j.trim(),Y).w+14)),S=Math.max(20,Y+8);[O,D]=X?[E,F.length*S+4]:[F.length*E+4,S+2];break}case"image":{let[F,E]=GJ(V.attrs.size,[64,64]),S=+V.attrs.n||1,j=F+(S-1)*6,f=E+(S-1)*6,[v,d,h,k]=OV(V.attrs);O=Math.abs(v)*j+Math.abs(h)*f,D=Math.abs(d)*j+Math.abs(k)*f;break}case"magnify":[O,D]=GJ(V.attrs.size,[64,64]);break;case"matrix":{let F=+V.attrs.cellSize||16;O=+V.attrs.cols*F,D=+V.attrs.rows*F;break}case"voxels":{let F=+V.attrs.cellSize||16,E=+V.attrs.depth*F*0.5;O=+V.attrs.cols*F+E,D=+V.attrs.rows*F+E;break}case"gaussian":[O,D]=GJ(V.attrs.size,[120,84]);break;case"space":[O,D]=GJ(V.attrs.size,[110,100]);break;case"sphere":[O,D]=GJ(V.attrs.size,[84,84]);break;case"pointcloud":[O,D]=GJ(V.attrs.size,[100,84]);break;case"waveform":[O,D]=GJ(V.attrs.size,[120,44]);break;case"text":O=I.w+8,D=I.h+4;break;default:O=Math.max(I.w+30,76),D=I.h+18}V.w=+V.attrs.w||O,V.h=+V.attrs.h||D}function tJ(V){let X=V.dir==="LR"||V.dir==="RL",[K,Y]=V.gap??(X?[56,36]:[30,44]),I=[...V.nodes.values()],O=V.fontSize??jV;for(let $ of I)_4($,X,O);let D=($)=>X?$.w:$.h,F=($)=>X?$.h:$.w,E=($,B)=>V.dir==="TB"?[B,$]:V.dir==="BT"?[B,-$]:V.dir==="LR"?[$,B]:[-$,B],S=($,B)=>V.dir==="TB"?[B,$]:V.dir==="BT"?[-B,$]:V.dir==="LR"?[$,B]:[-$,B],j=($)=>("at"in $.attrs)||Object.keys(PV).some((B)=>(B in $.attrs)),f=I.filter(($)=>!j($)),v=new Set(f.map(($)=>$.id)),d=new Map(f.map(($)=>[$.id,$.id])),h=($)=>{while(d.get($)!==$)$=d.get($);return $};for(let $ of V.ranks)for(let B of $.slice(1))if(v.has(B)&&v.has($[0]))d.set(h(B),h($[0]));let k=new Map;for(let $ of V.edges){if(!v.has($.from)||!v.has($.to))continue;let B=h($.from),_=h($.to);if(B===_)continue;if(!k.has(B))k.set(B,[]);k.get(B).push({to:_,e:$})}let MJ=new Set,UJ=new Map,s=[],OJ=($)=>{UJ.set($,1);for(let{to:B,e:_}of k.get($)??[]){let G=UJ.get(B);if(G===1)MJ.add(_);else if(!G)OJ(B)}UJ.set($,2),s.push($)};for(let $ of f)if(!UJ.has(h($.id)))OJ(h($.id));let VJ=new Map;for(let $ of s.reverse()){let B=VJ.get($)??0;VJ.set($,B);for(let{to:_,e:G}of k.get($)??[])if(!MJ.has(G))VJ.set(_,Math.max(VJ.get(_)??0,B+1))}let t=($)=>VJ.get(h($.id)),IJ=new Map;for(let $ of V.edges)if(v.has($.from)&&v.has($.to)&&!MJ.has($)&&!$.flags.has("skip")&&!$.flags.has("join")){if(!IJ.has($.to))IJ.set($.to,[]);IJ.get($.to).push(V.nodes.get($.from))}let ZJ=new Map,NJ=new Set,EJ=new Set,WJ=0;for(let $ of[...f].sort((B,_)=>t(B)-t(_)||B.order-_.order)){let B=t($),_=(u)=>!EJ.has(B+":"+u),G=$.attrs.lane!==void 0?+$.attrs.lane:$.attrs.align?ZJ.get($.attrs.align):void 0;if(G===void 0){let u=(IJ.get($.id)??[]).filter((J)=>t(J)<B),W=u.map((J)=>ZJ.get(J.id));if(u.length>1&&u.every((J)=>t(J)===t(u[0]))&&new Set(W).size===W.length){let J=W.reduce((U,Q)=>U+Q)/W.length;if(_(J))G=J}else for(let J of u){let U=ZJ.get(J.id);if(!NJ.has(J.id)&&_(U)){G=U,NJ.add(J.id);break}}if(G===void 0&&!u.length)for(let J of V.edges){if(J.from!==$.id||!MJ.has(J)||!ZJ.has(J.to)||NJ.has(J.to))continue;let U=ZJ.get(J.to);if(_(U)){G=U,NJ.add(J.to);break}}}if(G===void 0||!_(G)){while(!_(WJ))WJ++;G=WJ}WJ=Math.max(WJ,Math.floor(G)+1),EJ.add(B+":"+G),ZJ.set($.id,G)}let SJ=Math.max(0,...[...ZJ.values()].map(Math.ceil))+1,cJ=Math.max(0,...f.map(t))+1,vJ=Array(SJ).fill(0),zJ=Array(cJ).fill(0),kJ=Array.from({length:SJ},()=>new Set),mJ=Array.from({length:cJ},()=>new Set);for(let $ of f){let B=ZJ.get($.id),_=t($),G=$.groups.join();if(Number.isInteger(B))vJ[B]=Math.max(vJ[B],F($)),kJ[B].add(G);zJ[_]=Math.max(zJ[_],D($)),$.groups.forEach((u)=>mJ[_].add(u))}let BV=($,B)=>$.size!==B.size||[...$].some((_)=>!B.has(_)),pJ=Array(cJ).fill(K);for(let $ of V.edges){let B=V.nodes.get($.from),_=V.nodes.get($.to);if(!$.label||!v.has(B.id)||!v.has(_.id)||t(_)<=t(B))continue;let G=qJ($.label,O-1);pJ[t(_)]=Math.max(pJ[t(_)],X?G.w+24:G.h+16)}let yJ=[vJ[0]/2],fJ=[zJ[0]/2];for(let $=1;$<SJ;$++)yJ[$]=yJ[$-1]+vJ[$-1]/2+Y+(BV(kJ[$-1],kJ[$])?VV*2:0)+vJ[$]/2;for(let $=1;$<cJ;$++)fJ[$]=fJ[$-1]+zJ[$-1]/2+pJ[$]+(BV(mJ[$-1],mJ[$])?VV*1.5:0)+zJ[$]/2;let WV=($)=>{let B=Math.floor($),_=Math.ceil($);return yJ[B]+(yJ[_]-yJ[B])*($-B)},sJ=($)=>{let[B,_]=GJ($.attrs.nudge,[0,0]);$.x+=B,$.y+=_};for(let $ of f)[$.x,$.y]=E(fJ[t($)],WV(ZJ.get($.id))),sJ($);let hJ=I.filter(j),dJ=new Set(v);for(let $=0;hJ.length&&$<=I.length;$++)hJ=hJ.filter((B)=>{if(B.attrs.at)return[B.x,B.y]=GJ(B.attrs.at,[0,0]),sJ(B),dJ.add(B.id),!1;let _=Object.keys(PV).find((U)=>(U in B.attrs)),G=V.nodes.get(B.attrs[_]);if(!G)throw Error(`${B.id}: ${_} refers to unknown node "${B.attrs[_]}"`);if(!dJ.has(G.id))return!0;let[u,W]=PV[_],J=+B.attrs.dist||22;return B.x=G.x+u*(G.w/2+B.w/2+J),B.y=G.y+W*(G.h/2+B.h/2+J),sJ(B),dJ.add(B.id),!1});if(hJ.length)throw Error(`circular placement: ${hJ.map(($)=>$.id).join(", ")}`);let RJ=new Map;for(let $ of I){let[B,_]=S($.x,$.y);RJ.set($.id,{u:B,v:_,hu:D($)/2,hv:F($)/2})}let oJ=($,B,_)=>{for(let G=0;G+1<$.length;G++){let[u,W]=[$[G],$[G+1]],J=Math.min(u[0],W[0]),U=Math.max(u[0],W[0]),Q=Math.min(u[1],W[1]),Z=Math.max(u[1],W[1]);for(let[N,H]of RJ)if(N!==B&&N!==_&&J<H.u+H.hu+3&&U>H.u-H.hu-3&&Q<H.v+H.hv+3&&Z>H.v-H.hv-3)return!0}return!1},KV=[],bJ=($,B,_,G,u)=>{let W=$>0?u:G;for(let J=!0;J;){J=!1;for(let U of RJ.values())if(U.u-U.hu<_&&U.u+U.hu>B&&U.v+U.hv>G-1&&U.v-U.hv<u+1){let Q=$>0?U.v+U.hv:U.v-U.hv;if($>0?Q>W:Q<W)W=Q,J=!0,$>0?u=W:G=W}}W+=$*16;for(let J=!0;J;){J=!1;for(let U of KV)if(U.side===$&&U.a<_&&U.b>B&&Math.abs(U.v-W)<9)W+=$*9,J=!0}return KV.push({side:$,a:B,b:_,v:W}),W},QV=[],jJ=[];for(let $ of V.edges){let B=RJ.get($.from),_=RJ.get($.to),G=V.nodes.get($.from),u=V.nodes.get($.to);if($.op==="=>"&&G.shape==="neurons"&&u.shape==="neurons"){$.fc=!0;continue}if($.from!==$.to&&!$.flags.has("skip")&&_.u-_.hu>=B.u+B.hu+2){let W=Math.max(B.v-B.hv,_.v-_.hv)+6,J=Math.min(B.v+B.hv,_.v+_.hv)-6,U=W<=J?Math.min(J,Math.max(W,B.hv<=_.hv?B.v:_.v)):NaN;QV.push({e:$,S:B,T:_,ps:isNaN(U)?B.v:U,pt:isNaN(U)?_.v:U})}else jJ.push($)}let eJ=({e:$,S:B,T:_,ps:G,pt:u})=>{let W=B.u+B.hu,J=_.u-_.hu,U=(W+J)/2,Q=(N)=>[[W,G],[N,G],[N,u],[J,u]],Z=Math.abs(G-u)<0.5?[[[W,u],[J,u]]]:[Q(Math.max(U,J-K/2)),Q(Math.min(U,W+K/2))];if($.flags.has("join")&&Math.abs(G-_.v)>_.hv){let N=G>_.v?1:-1;Z.unshift([[W,G],[_.u,G],[_.u,_.v+N*_.hv]])}return Z.find((N)=>!oJ(N,$.from,$.to))},IV=QV.filter(($)=>eJ($)||(jJ.push($.e),!1)),iJ=new Map;for(let $ of IV){if(!iJ.has($.e.to))iJ.set($.e.to,[]);iJ.get($.e.to).push($)}let _V=($,B,_)=>{let G=$.map((J)=>J.pt),u=B?$.indexOf(B):0;for(let J=u+1;J<$.length;J++)G[J]=Math.max(G[J],G[J-1]+12);for(let J=u-1;J>=0;J--)G[J]=Math.min(G[J],G[J+1]-12);let W=Math.max(0,G[G.length-1]-(_.v+_.hv-5))-Math.max(0,_.v-_.hv+5-G[0]);return{pt:G.map((J)=>J-W),moved:W!==0}};for(let $ of iJ.values()){if($.length<2)continue;let B=$[0].T;$.sort((W,J)=>W.pt-J.pt||W.S.v-J.S.v);let _=$.filter((W)=>Math.abs(W.ps-W.pt)<0.5).sort((W,J)=>Math.abs(W.pt-B.v)-Math.abs(J.pt-B.v))[0],G=$,u=_V(G,_,B);if(_&&u.moved)G=$.filter((W)=>{if(W===_||Math.abs(W.S.v-B.v)<=B.hv)return!0;let J=W.S.v>B.v?1:-1,U=[[W.S.u+W.S.hu,W.S.v],[B.u,W.S.v],[B.u,B.v+J*B.hv]];if(oJ(U,W.e.from,W.e.to))return!0;return W.alt=U,!1}),u=_V(G,_,B);G.forEach((W,J)=>{W.pt=u.pt[J]});for(let W of G)if(Math.abs(W.pt-W.S.v)<W.S.hv-4)W.ps=W.pt}let wJ=($,B,_=0)=>{if($.pts=B.map(([G,u])=>E(G,u)),$.flags.has("curve")&&B.length===4)$.curve=_};for(let $ of IV){let B=$.alt??eJ($);if(B)wJ($.e,B,1);else jJ.push($.e)}let AV=($)=>V.edges.some((B)=>B!==$&&(B.from===$.from&&B.to===$.to||B.from===$.to&&B.to===$.from));for(let $=jJ.length-1;$>=0;$--){let B=jJ[$],_=RJ.get(B.from),G=RJ.get(B.to);if(B.from===B.to||B.flags.has("skip")||G.u+G.hu>_.u-_.hu-2||AV(B))continue;let u=_.u-_.hu,W=G.u+G.hu,J=G.v>_.v?1:-1,U=(u+W)/2,Q=Math.max(_.v-_.hv,G.v-G.hv)+6,Z=Math.min(_.v+_.hv,G.v+G.hv)-6,N=[];if(Q<=Z){let L=Math.min(Z,Math.max(Q,_.hv<=G.hv?_.v:G.v));N.push([[u,L],[W,L]])}else N.push([[_.u,_.v+J*_.hv],[_.u,G.v],[W,G.v]],[[u,_.v],[U,_.v],[U,G.v],[W,G.v]]);let H=N.find((L)=>!oJ(L,B.from,B.to));if(H)wJ(B,H,1),jJ.splice($,1)}for(let $ of jJ){let B=RJ.get($.from),_=RJ.get($.to),G=/^(left|top|-)/.test($.attrs.side??"")?-1:1,u=Math.min(B.v-B.hv,_.v-_.hv),W=Math.max(B.v+B.hv,_.v+_.hv);if($.from===$.to){let J=B.v+G*(B.hv+18);wJ($,[[B.u+B.hu*0.4,B.v+G*B.hv],[B.u+B.hu*0.4,J],[B.u-B.hu*0.4,J],[B.u-B.hu*0.4,B.v+G*B.hv]],1.33)}else if(!$.flags.has("skip")&&B.u-B.hu<_.u+_.hu&&_.u-_.hu<B.u+B.hu){let J=(Math.max(B.u-B.hu,_.u-_.hu)+Math.min(B.u+B.hu,_.u+_.hu))/2,U=_.v>B.v?1:-1;wJ($,[[J,B.v+U*B.hv],[J,_.v-U*_.hv]])}else if(_.u>B.u){let J=B.u+B.hu,U=Math.min(K/2,(_.u-_.hu-J)/2),Q=V.nodes.get($.from);if(V.nodes.get($.to).groups.some((H)=>!Q.groups.includes(H))){let H=Math.min(...[...RJ.values()].filter((L)=>L.u-L.hu>J+1&&L.u<_.u&&L.v+L.hv>u&&L.v-L.hv<W).map((L)=>L.u-L.hu));if(H-VV+6-J>6)U=H-VV+6-J}let N=bJ(G,J+U,_.u+_.hu,u,W);wJ($,[[J,B.v],[J+U,B.v],[J+U,N],[_.u,N],[_.u,_.v+G*_.hv]])}else{let J=bJ(G,Math.min(B.u-B.hu,_.u-_.hu),Math.max(B.u+B.hu,_.u+_.hu),u,W);wJ($,[[B.u,B.v+G*B.hv],[B.u,J],[_.u,J],[_.u,_.v+G*_.hv]],1.33)}}for(let $ of V.edges){if(!$.label||$.pts.length<2)continue;let B=qJ($.label,O-1),_=0,G=0;for(let N=0;N+1<$.pts.length;N++){let H=Math.hypot($.pts[N+1][0]-$.pts[N][0],$.pts[N+1][1]-$.pts[N][1]);if(H>_)_=H,G=N}if($.flags.has("join")&&$.pts.length===3)G=1;let[u,W]=[$.pts[G],$.pts[G+1]],J=$.pts[0],U=$.pts[$.pts.length-1],Q=(u[0]+W[0])/2,Z=(u[1]+W[1])/2;if(Math.abs(u[1]-W[1])<Math.abs(u[0]-W[0])){let N=Z>Math.max(J[1],U[1])+1;$.lx=Q,$.ly=Z+(N?1:-1)*(B.h/2+5)}else{let N=Q<Math.min(J[0],U[0])-1;$.lx=Q+(N?-1:1)*(B.w/2+7),$.ly=Z}}for(let $ of[...V.groups].reverse()){let B=1/0,_=1/0,G=-1/0,u=-1/0,W=(Q,Z,N=VV)=>{B=Math.min(B,Q-N),_=Math.min(_,Z-N),G=Math.max(G,Q+N),u=Math.max(u,Z+N)},J=new Set(I.filter((Q)=>Q.groups.includes($.id)).map((Q)=>Q.id));for(let Q of J){let Z=V.nodes.get(Q);W(Z.x-Z.w/2,Z.y-Z.h/2),W(Z.x+Z.w/2,Z.y+Z.h/2)}for(let Q of V.edges)if(J.has(Q.from)&&J.has(Q.to))for(let[Z,N]of Q.pts)W(Z,N);else if(J.has(Q.to)&&Q.pts.length===5)for(let[Z,N]of Q.pts.slice(2))W(Z,N,6);for(let Q of V.groups)if(Q.parent===$.id&&Q.w)W(Q.x,Q.y),W(Q.x+Q.w,Q.y+Q.h);if(B===1/0)continue;let U=$.label?18:0;Object.assign($,{x:B,y:_-U,w:G-B,h:u-_+U})}return V}var uJ,UV,rJ={},tV=new Map;function rV(V,X,K){let Y=V.createShader(X);if(V.shaderSource(Y,K),V.compileShader(Y),!V.getShaderParameter(Y,V.COMPILE_STATUS))throw Error(V.getShaderInfoLog(Y)??"shader");return Y}function $V(V,X){let K=V.createProgram();if(V.attachShader(K,rV(V,V.VERTEX_SHADER,`
|
|
1
|
+
(()=>{var{defineProperty:Ae,getOwnPropertyNames:or,getOwnPropertyDescriptor:rr}=Object,sr=Object.prototype.hasOwnProperty;function nr(o){return this[o]}var ar=(o)=>{var n=(_e??=new WeakMap).get(o),i;if(n)return n;if(n=Ae({},"__esModule",{value:!0}),o&&typeof o==="object"||typeof o==="function"){for(var a of or(o))if(!sr.call(n,a))Ae(n,a,{get:nr.bind(o,a),enumerable:!(i=rr(o,a))||i.enumerable})}return _e.set(o,n),n},_e;var ir=(o)=>o;function cr(o,n){this[o]=ir.bind(null,n)}var He=(o,n)=>{for(var i in n)Ae(o,i,{get:n[i],enumerable:!0,configurable:!0,set:cr.bind(n,i)})};var Nr={};He(Nr,{Figure:()=>ze,Graph:()=>Re,Module:()=>Ge,THEMES:()=>ue,Tensor:()=>Mt,act:()=>Po,add:()=>Zo,attention:()=>So,block:()=>ho,circle:()=>go,concat:()=>Co,configure:()=>Xo,conv:()=>To,dec:()=>xo,default:()=>zr,draw:()=>Qo,dropout:()=>jo,embed:()=>Lo,empty:()=>wo,enc:()=>bo,ffn:()=>Oo,figure:()=>Ko,gaussian:()=>Bo,image:()=>Do,layer:()=>Rt,layout:()=>Zt,linear:()=>zo,loss:()=>Fo,magnify:()=>_o,matmul:()=>Ao,matrix:()=>Wo,mha:()=>Mo,mlp:()=>Eo,mul:()=>Yo,neurons:()=>$o,norm:()=>Ro,op:()=>Le,pointcloud:()=>Vo,pool:()=>Io,preset:()=>tt,softmax:()=>No,space:()=>qo,sphere:()=>Uo,stack:()=>vo,tensor:()=>yo,text:()=>Ve,thin:()=>mo,vector:()=>ko,voxels:()=>Jo,waveform:()=>Go});var De={};He(De,{Figure:()=>ze,Graph:()=>Re,Module:()=>Ge,Tensor:()=>Mt,act:()=>Po,add:()=>Zo,attention:()=>So,block:()=>ho,circle:()=>go,concat:()=>Co,configure:()=>Xo,conv:()=>To,dec:()=>xo,dropout:()=>jo,embed:()=>Lo,empty:()=>wo,enc:()=>bo,ffn:()=>Oo,figure:()=>Ko,gaussian:()=>Bo,image:()=>Do,layer:()=>Rt,linear:()=>zo,loss:()=>Fo,magnify:()=>_o,matmul:()=>Ao,matrix:()=>Wo,mha:()=>Mo,mlp:()=>Eo,mul:()=>Yo,neurons:()=>$o,norm:()=>Ro,op:()=>Le,pointcloud:()=>Vo,pool:()=>Io,preset:()=>tt,softmax:()=>No,space:()=>qo,sphere:()=>Uo,stack:()=>vo,tensor:()=>yo,text:()=>Ve,thin:()=>mo,vector:()=>ko,voxels:()=>Jo,waveform:()=>Go});var ye='Inter,"Helvetica Neue",Helvetica,Arial,sans-serif',Ct=400,to='"Latin Modern Math","STIX Two Math","Cambria Math","Times New Roman",serif',ie;function Te(o){ie=o,Ce.clear()}var eo=()=>{let o=globalThis.MathJax;return o?.tex2svg?(n)=>o.tex2svg(n,{display:!1}).querySelector("svg")?.outerHTML??"":null},lr=(o,n)=>{let i=ie===void 0?eo():ie;if(!i)return null;let a;try{a=i(o)}catch{return null}let u=/viewBox="([^"]+)"/.exec(a)?.[1];if(!u)return null;let[,h,x,g]=u.trim().split(/[\s,]+/).map(Number),v=n/1000;return{inner:a.replace(/^<svg[^>]*>/,"").replace(/<\/svg>\s*$/,""),viewBox:u,w:x*v,asc:-h*v,desc:(g+h)*v}},Jt=(o)=>{let n={},i=o.split(" ");for(let a=0;a<i.length;a+=2)n[i[a]]=i[a+1];return n},We=Jt("alpha α beta β gamma γ delta δ epsilon ϵ varepsilon ε zeta ζ eta η theta θ vartheta ϑ iota ι kappa κ lambda λ mu μ nu ν xi ξ pi π rho ρ sigma σ tau τ upsilon υ phi ϕ varphi φ chi χ psi ψ omega ω Gamma Γ Delta Δ Theta Θ Lambda Λ Xi Ξ Pi Π Sigma Σ Phi Φ Psi Ψ Omega Ω"),Je=Jt("cdot · times × oplus ⊕ otimes ⊗ odot ⊙ to → rightarrow → leftarrow ← gets ← Rightarrow ⇒ leftrightarrow ↔ mapsto ↦ sum ∑ prod ∏ int ∫ infty ∞ partial ∂ nabla ∇ in ∈ notin ∉ sim ∼ approx ≈ propto ∝ leq ≤ le ≤ geq ≥ ge ≥ neq ≠ ne ≠ pm ± mp ∓ ldots … dots … cdots ⋯ vdots ⋮ mid ∣ | ‖ ast ∗ star ⋆ circ ∘ bullet • forall ∀ exists ∃ emptyset ∅ cup ∪ cap ∩ subset ⊂ subseteq ⊆ top ⊤ perp ⊥ langle ⟨ rangle ⟩ ell ℓ hbar ℏ dagger † prime ′ { { } } % % _ _ # # & & $ $"),Ut={};for(let o of"+−×·±∓⊕⊗⊙∘∗⋆∪∩")Ut[o]="bin";for(let o of"=<>≤≥≠≈∼∝∈∉⊂⊆→←⇒↔↦∣")Ut[o]="rel";for(let o of",;")Ut[o]="punct";Ut["|"]="mid";for(let o of"([⟨")Ut[o]="open";for(let o of")]⟩")Ut[o]="close";var fr=new Set("log exp ln sin cos tanh max min arg argmax argmin softmax lim det tr sup inf Pr KL diag sign".split(" ")),Ze=Jt("hat ̂ widehat ̂ tilde ̃ widetilde ̃ bar ̄ overline ̅ dot ̇ ddot ̈ vec ⃗"),Ye=Jt(", : ; quad qquad ! "),ur=Jt("B ℬ E ℰ F ℱ H ℋ I ℐ L ℒ M ℳ R ℛ"),pr=Jt("C ℂ H ℍ N ℕ P ℙ Q ℚ R ℝ Z ℤ"),Xe=(o)=>ur[o]||(/[A-Z]/.test(o)?String.fromCodePoint(119964+o.charCodeAt(0)-65):o),hr=(o)=>pr[o]||(/[A-Z]/.test(o)?String.fromCodePoint(120120+o.charCodeAt(0)-65):/\d/.test(o)?String.fromCodePoint(120792+ +o):o),Ke={mathbf:{up:!0,b:!0},bf:{up:!0,b:!0},boldsymbol:{b:!0},bm:{b:!0},mathrm:{up:!0},rm:{up:!0},operatorname:{up:!0},mathit:{},text:{up:!0,text:!0},textrm:{up:!0,text:!0},mathsf:{up:!0,sans:!0},textbf:{up:!0,b:!0,text:!0},mathcal:{up:!0,map:Xe},mathscr:{up:!0,map:Xe},mathbb:{up:!0,map:hr}},dr=new Set("left right big Big bigl bigr Bigl Bigr displaystyle".split(" "));function mr(o){let n=0,i=(h,x,g=!1)=>{if(x.text)return{t:h,it:!1,b:!!x.b,serif:!x.sans};if(h==="-")h="−";return{t:h,it:g&&!x.up,b:!!x.b,serif:!x.sans,cls:Ut[h]}};function a(h,x){let g=[];while(n<o.length){let v=o[n];if(v===x){n++;break}if(v==="^"||v==="_"){n++;let R=v==="^"?"sup":"sub",I=g[g.length-1];if(!I||"t"in I||I[R])g.push(I={});I[R]=u(h);continue}if(v===" "&&!h.text){n++;continue}g.push(...u(h))}return g}function u(h){while(o[n]===" "&&!h.text)n++;let x=o[n++];if(x===void 0)return[];if(x==="{")return a(h,"}");if(x!=="\\")return[i(h.map?h.map(x):x,h,/[A-Za-z]/.test(x)&&!h.map)];let g=/^[A-Za-z]+/.exec(o.slice(n))?.[0]||o[n]||"";if(n+=g.length,We[g])return[i(We[g],h,/^[a-z]/.test(g))];if(Je[g])return[i(Je[g],h)];if(Ye[g])return[i(Ye[g],h)];if(fr.has(g))return[i(g,{...h,up:!0})];if(Ke[g])return u({...h,...Ke[g]});if(Ze[g]){let v=u(h),R=v.find((I)=>("t"in I));if(R)R.t+=Ze[g];return v}if(g==="frac"){let v=u(h),R=u(h);return[...v,i("/",h),...R]}if(g==="sqrt")return[i("√",h),...u(h)];if(dr.has(g))return[];return[i(g,{...h,up:!0})]}return a({})}var Ce=new Map,ae,Qe=new Map;function gr(o,n,i,a,u='Inter,"Helvetica Neue",Helvetica,Arial,sans-serif'){return`${n?"italic ":""}${i===!0?700:i||400} ${o}px ${a?'"Latin Modern Math","STIX Two Math","Cambria Math","Times New Roman",serif':u}`}function br(o){if(/[iljI.,:;'|! ]/.test(o))return 0.28;if(/[mwMW]/.test(o))return 0.86;if(/[A-Z]/.test(o))return 0.68;if(/[̀-ͯ⃗]/.test(o))return 0;if(/[ -鿿-]/.test(o))return 1;if(o===" ")return 0.28;return 0.56}function xr(o,n,i){let a=n+"\x00"+o,u=Qe.get(a);if(u!==void 0)return u;if(ae===void 0)ae=typeof document<"u"?document.createElement("canvas").getContext("2d"):null;if(ae)ae.font=n,u=ae.measureText(o).width;else{u=0;for(let h of o)u+=br(h)*i}return Qe.set(a,u),u}function bt(o,n,i=400,a='Inter,"Helvetica Neue",Helvetica,Arial,sans-serif'){let u=o+"\x00"+n+"\x00"+i+"\x00"+a+"\x00"+(ie===void 0?!!eo():!!ie),h=Ce.get(u);if(h)return h;let x=[],g=[];for(let I of o.split(/\n|\\\\|<br\s*\/?>/)){let q=[],O=0,U=(X,V,_,ft=!1)=>{let W="",lt=!1,wt="",ot=!1;for(let H of X)if("t"in H){let it=H.cls??"ord",ct=!W||W==="bin"||W==="rel"||W==="open"||W==="punct";if(it==="bin"&&ct)it="ord";else if(it==="mid"&&(ct||ot))it=ot?"close":"open",ot=!ot;let ht=0;if(ft&&W&&it!=="punct"&&it!=="close"&&W!=="open"){if(it==="rel"!==(W==="rel"))ht=0.2777777777777778;else if(it==="bin"||W==="bin")ht=0.2222222222222222;else if(W==="punct"||it==="mid"||W==="mid")ht=0.16666666666666666}if(lt&&!H.it&&it!=="punct")ht+=0.08;O+=ht*_;let kt=xr(H.t,gr(_,H.it,H.b||(H.serif?!1:i),H.serif,a),_),pt=q[q.length-1];if(!ht&&pt&&pt.dy===V&&pt.size===_&&pt.it===H.it&&pt.b===H.b&&pt.serif===H.serif)pt.t+=H.t;else q.push({t:H.t,x:O,dy:V,size:_,it:H.it,b:H.b,serif:H.serif});O+=kt,W=it,lt=H.it,wt=H.t.slice(-1)}else{let it=O,ct=Math.max(_*0.72,Math.min(_,8.5)),ht=it,kt=it;if(H.sup)O=it+(lt?(wt==="f"?0.2:0.08)*_:0),U(H.sup,V-_*0.38,ct),ht=O,O=it;if(H.sub)U(H.sub,V+_*0.24,ct),kt=O;O=Math.max(ht,kt)+0.06*_,lt=!1}};I.split("$").forEach((X,V)=>{let _=V%2?lr(X,n*1.1):null;if(_)q.push({t:"",x:O,dy:0,size:n,it:!1,b:!1,serif:!0,math:_}),O+=_.w;else if(V%2)U(mr(X),0,n*1.1,!0);else if(X)U([{t:X,it:!1,b:!1,serif:!1}],0,n)}),x.push(q),g.push(O)}let v=n*1.3,R={lines:x,widths:g,w:Math.max(0,...g),h:x.length*v,lh:v,size:n};return Ce.set(u,R),R}var Pe=13,$e=new Set(["tensor","stack","matrix","voxels","image","magnify","neurons","gaussian","space","sphere","pointcloud","waveform"]),je=(o)=>JSON.parse(o.attrs.values??"[]"),we=(o,n)=>+o.attrs.fontSize||(o.shape==="thin"||o.shape==="vector"||$e.has(o.shape)?n-1:n),ke=(o)=>{let n=(+o.tilt||0)*Math.PI/180,i=(+o.rotate||0)*Math.PI/180,[a,u]=[Math.cos(n),-Math.sin(n)*0.35],[h,x]=[Math.cos(i),Math.sin(i)];return[h*a-x*u,x*a+h*u,-x,h]},dt=(o,n)=>o?o.split(",").map(Number):n,Ie={"left-of":[-1,0],"right-of":[1,0],above:[0,-1],below:[0,1]},ce=14,ve=(o,n)=>o[0]<n[2]&&o[2]>n[0]&&o[1]<n[3]&&o[3]>n[1];function yr(o,n,i,a){let u=we(o,i),h=o.attrs.fontFamily||a,x=bt(o.label,u,500,h),g,v;switch(o.shape){case"thin":g=Math.max(x.w+26,64),v=x.h+9;break;case"op":g=v=22;break;case"circle":g=v=Math.max(42,x.w+18,x.h+16);break;case"tensor":{let[R,I,q]=dt(o.attrs.size,[36,64,20]);g=R+q*0.6,v=I+q*0.6;break}case"stack":{let[R,I]=dt(o.attrs.size,[44,60]),q=+o.attrs.n||4,O=+o.attrs.offset||7;g=R+(q-1)*O,v=I+(q-1)*O;break}case"enc":case"dec":if(n)g=x.w+40,v=Math.max(84,x.h+44);else g=Math.max(x.w+64,120),v=Math.max(52,x.h+26);break;case"neurons":{let R=(+o.attrs.n||4)*20+4;[g,v]=n?[22,R]:[R,22];break}case"vector":{let R=je(o),I=Math.max(36,...R.map((O)=>bt(O.trim(),u,Ct,h).w+14)),q=Math.max(20,u+8);[g,v]=n?[I,R.length*q+4]:[R.length*I+4,q+2];break}case"image":{let[R,I]=dt(o.attrs.size,[64,64]),q=+o.attrs.n||1,O=R+(q-1)*6,U=I+(q-1)*6,[X,V,_,ft]=ke(o.attrs);g=Math.abs(X)*O+Math.abs(_)*U,v=Math.abs(V)*O+Math.abs(ft)*U;break}case"magnify":[g,v]=dt(o.attrs.size,[64,64]);break;case"matrix":{let R=+o.attrs.cellSize||16;g=+o.attrs.cols*R,v=+o.attrs.rows*R;break}case"voxels":{let R=+o.attrs.cellSize||16,I=+o.attrs.depth*R*0.5;g=+o.attrs.cols*R+I,v=+o.attrs.rows*R+I;break}case"gaussian":[g,v]=dt(o.attrs.size,[120,84]);break;case"space":[g,v]=dt(o.attrs.size,[110,100]);break;case"sphere":[g,v]=dt(o.attrs.size,[84,84]);break;case"pointcloud":[g,v]=dt(o.attrs.size,[100,84]);break;case"waveform":[g,v]=dt(o.attrs.size,[120,44]);break;case"text":g=x.w+8,v=x.h+4;break;case"empty":g=v=0;break;default:g=Math.max(x.w+30,76),v=x.h+18}o.w=+o.attrs.w||g,o.h=+o.attrs.h||v}function Zt(o){let n=o.dir==="LR"||o.dir==="RL",[i,a]=o.gap??(n?[56,36]:[30,44]),u=[...o.nodes.values()],h=o.fontSize??Pe,x=o.fontFamily??ye;for(let e of u)yr(e,n,h,x);let g=(e)=>n?e.w:e.h,v=(e)=>n?e.h:e.w,R=(e,r)=>o.dir==="TB"?[r,e]:o.dir==="BT"?[r,-e]:o.dir==="LR"?[e,r]:[-e,r],I=(e,r)=>o.dir==="TB"?[r,e]:o.dir==="BT"?[-r,e]:o.dir==="LR"?[e,r]:[-e,r],q=(e)=>("at"in e.attrs)||Object.keys(Ie).some((r)=>(r in e.attrs)),O=u.filter((e)=>!q(e)),U=new Set(O.map((e)=>e.id)),X=new Map(O.map((e)=>[e.id,e.id])),V=(e)=>{while(X.get(e)!==e)e=X.get(e);return e};for(let e of o.ranks)for(let r of e.slice(1))if(U.has(r)&&U.has(e[0]))X.set(V(r),V(e[0]));let _=new Map;for(let e of o.edges){if(!U.has(e.from)||!U.has(e.to))continue;let r=V(e.from),s=V(e.to);if(r===s)continue;if(!_.has(r))_.set(r,[]);_.get(r).push({to:s,e})}let ft=new Set,W=new Map,lt=[],wt=(e)=>{W.set(e,1);for(let{to:r,e:s}of _.get(e)??[]){let c=W.get(r);if(c===1)ft.add(s);else if(!c)wt(r)}W.set(e,2),lt.push(e)};for(let e of O)if(!W.has(V(e.id)))wt(V(e.id));let ot=new Map;for(let e of lt.reverse()){let r=ot.get(e)??0;ot.set(e,r);for(let{to:s,e:c}of _.get(e)??[])if(!ft.has(c))ot.set(s,Math.max(ot.get(s)??0,r+1))}let H=(e)=>ot.get(V(e.id)),it=new Map;for(let e of o.edges)if(U.has(e.from)&&U.has(e.to)&&!ft.has(e)&&!e.flags.has("skip")&&!e.flags.has("join")){if(!it.has(e.to))it.set(e.to,[]);it.get(e.to).push(o.nodes.get(e.from))}let ct=new Map,ht=new Set,kt=new Set,pt=0;for(let e of[...O].sort((r,s)=>H(r)-H(s)||r.order-s.order)){let r=H(e),s=(y)=>!kt.has(r+":"+y),c=e.attrs.lane!==void 0?+e.attrs.lane:e.attrs.align?ct.get(e.attrs.align):void 0;if(c===void 0){let y=(it.get(e.id)??[]).filter((f)=>H(f)<r),p=y.map((f)=>ct.get(f.id));if(y.length>1&&y.every((f)=>H(f)===H(y[0]))&&new Set(p).size===p.length){let f=p.reduce((S,E)=>S+E)/p.length;if(s(f))c=f}else for(let f of y){let S=ct.get(f.id);if(!ht.has(f.id)&&s(S)){c=S,ht.add(f.id);break}}if(c===void 0&&!y.length)for(let f of o.edges){if(f.from!==e.id||!ft.has(f)||!ct.has(f.to)||ht.has(f.to))continue;let S=ct.get(f.to);if(s(S)){c=S,ht.add(f.to);break}}}if(c===void 0||!s(c)){while(!s(pt))pt++;c=pt}pt=Math.max(pt,Math.floor(c)+1),kt.add(r+":"+c),ct.set(e.id,c)}let yt=(e)=>H(o.nodes.get(e)),Gt=o.edges.filter((e)=>U.has(e.from)&&U.has(e.to)&&!ft.has(e)&&!e.flags.has("skip")&&yt(e.from)<yt(e.to)),Pt=(e,r)=>ct.get(r<yt(e.to)?e.from:e.to),Xt=()=>{let e=0;for(let r=0;r<Gt.length;r++)for(let s=r+1;s<Gt.length;s++){let c=Gt[r],y=Gt[s],p=Math.max(yt(c.from),yt(y.from)),f=Math.min(yt(c.to),yt(y.to));if(p<f&&(Pt(c,p)-Pt(y,p))*(Pt(c,f)-Pt(y,f))<0)e++}return e},Dt=()=>{let e=0;for(let r of o.groups){let s=O.filter((p)=>p.groups.includes(r.id));if(!s.length)continue;let c=s.map((p)=>ct.get(p.id)),y=s.map(H);for(let p of O){let f=ct.get(p.id),S=H(p);if(!p.groups.includes(r.id)&&f>=Math.min(...c)&&f<=Math.max(...c)&&S>=Math.min(...y)&&S<=Math.max(...y))e++}}return e},Lt=new Set;for(let e of O){let r=ct.get(e.id);if(e.attrs.lane!==void 0)Lt.add(r);if(!Number.isInteger(r))Lt.add(Math.floor(r)).add(Math.ceil(r))}let he=(e)=>{for(let[r,s]of ct)if(s===e||s===e+1)ct.set(r,2*e+1-s)},zt=Xt(),Kt=zt?Dt():0;for(let e=0,r=!0;zt&&r&&e<pt;e++){r=!1;for(let s=0;s+1<pt&&zt;s++){if(Lt.has(s)||Lt.has(s+1))continue;he(s);let c=Xt(),y=c<zt?Dt():1/0;if(y<=Kt)zt=c,Kt=y,r=!0;else he(s)}}let jt=Math.max(0,...[...ct.values()].map(Math.ceil))+1,_t=Math.max(0,...O.map(H))+1,Ft=Array(jt).fill(0),Bt=Array(_t).fill(0),Ht=Array.from({length:jt},()=>new Set),Qt=Array.from({length:_t},()=>new Set);for(let e of O){let r=ct.get(e.id),s=H(e),c=e.groups.join();if(Number.isInteger(r))Ft[r]=Math.max(Ft[r],v(e)),Ht[r].add(c);Bt[s]=Math.max(Bt[s],g(e)),e.groups.forEach((y)=>Qt[s].add(y))}let te=(e,r)=>e.size!==r.size||[...e].some((s)=>!r.has(s)),ee=Array(_t).fill(i);for(let e of o.edges){let r=o.nodes.get(e.from),s=o.nodes.get(e.to);if(!e.label||!U.has(r.id)||!U.has(s.id)||H(s)<=H(r))continue;let c=bt(e.label,h-1,Ct,x);ee[H(s)]=Math.max(ee[H(s)],n?c.w+24:c.h+16)}let $t=[Ft[0]/2],oe=[Bt[0]/2];for(let e=1;e<jt;e++)$t[e]=$t[e-1]+Ft[e-1]/2+a+(te(Ht[e-1],Ht[e])?ce*2:0)+Ft[e]/2;for(let e=1;e<_t;e++)oe[e]=oe[e-1]+Bt[e-1]/2+ee[e]+(te(Qt[e-1],Qt[e])?ce*1.5:0)+Bt[e]/2;let de=(e)=>{let r=Math.floor(e),s=Math.ceil(e);return $t[r]+($t[s]-$t[r])*(e-r)},re=(e)=>{let[r,s]=dt(e.attrs.nudge,[0,0]);e.x+=r,e.y+=s};for(let e of O)[e.x,e.y]=R(oe[H(e)],de(ct.get(e.id))),re(e);let qt=u.filter(q),se=new Set(U);for(let e=0;qt.length&&e<=u.length;e++)qt=qt.filter((r)=>{if(r.attrs.at)return[r.x,r.y]=dt(r.attrs.at,[0,0]),re(r),se.add(r.id),!1;let s=Object.keys(Ie).find((S)=>(S in r.attrs)),c=o.nodes.get(r.attrs[s]);if(!c)throw Error(`${r.id}: ${s} refers to unknown node "${r.attrs[s]}"`);if(!se.has(c.id))return!0;let[y,p]=Ie[s],f=+r.attrs.dist||22;return r.x=c.x+y*(c.w/2+r.w/2+f),r.y=c.y+p*(c.h/2+r.h/2+f),re(r),se.add(r.id),!1});if(qt.length)throw Error(`circular placement: ${qt.map((e)=>e.id).join(", ")}`);let vt=new Map;for(let e of u){let[r,s]=I(e.x,e.y);vt.set(e.id,{u:r,v:s,hu:g(e)/2,hv:v(e)/2})}let ne=(e,r,s)=>{for(let c=0;c+1<e.length;c++){let[y,p]=[e[c],e[c+1]],f=Math.min(y[0],p[0]),S=Math.max(y[0],p[0]),E=Math.min(y[1],p[1]),m=Math.max(y[1],p[1]);for(let[w,z]of vt)if(w!==r&&w!==s&&f<z.u+z.hu+3&&S>z.u-z.hu-3&&E<z.v+z.hv+3&&m>z.v-z.hv-3)return!0}return!1},me=[],ge=(e,r,s,c,y)=>{let p=e>0?y:c;for(let f=!0;f;){f=!1;for(let S of vt.values())if(S.u-S.hu<s&&S.u+S.hu>r&&S.v+S.hv>c-1&&S.v-S.hv<y+1){let E=e>0?S.v+S.hv:S.v-S.hv;if(e>0?E>p:E<p)p=E,f=!0,e>0?y=p:c=p}}p+=e*16;for(let f=!0;f;){f=!1;for(let S of me)if(S.side===e&&S.a<s&&S.b>r&&Math.abs(S.v-p)<9)p+=e*9,f=!0}return me.push({side:e,a:r,b:s,v:p}),p},be=[],St=[];for(let e of o.edges){let r=vt.get(e.from),s=vt.get(e.to),c=o.nodes.get(e.from),y=o.nodes.get(e.to);if(e.op==="=>"&&c.shape==="neurons"&&y.shape==="neurons"){e.fc=!0;continue}if(e.from!==e.to&&!e.flags.has("skip")&&s.u-s.hu>=r.u+r.hu+2){let p=Math.max(r.v-r.hv,s.v-s.hv)+6,f=Math.min(r.v+r.hv,s.v+s.hv)-6,S=p<=f?Math.min(f,Math.max(p,r.hv<=s.hv?r.v:s.v)):NaN;be.push({e,S:r,T:s,ps:isNaN(S)?r.v:S,pt:isNaN(S)?s.v:S})}else St.push(e)}let xe=({e,S:r,T:s,ps:c,pt:y})=>{let p=r.u+r.hu,f=s.u-s.hu,S=(p+f)/2,E=(w)=>[[p,c],[w,c],[w,y],[f,y]],m=Math.abs(c-y)<0.5?[[[p,y],[f,y]]]:[E(Math.max(S,f-i/2)),E(Math.min(S,p+i/2))];if(e.flags.has("join")&&Math.abs(c-s.v)>s.hv){let w=c>s.v?1:-1;m.unshift([[p,c],[s.u,c],[s.u,s.v+w*s.hv]])}return m.find((w)=>!ne(w,e.from,e.to))},Ot=be.filter((e)=>xe(e)||(St.push(e.e),!1)),Nt=new Map;for(let e of Ot){if(!Nt.has(e.e.to))Nt.set(e.e.to,[]);Nt.get(e.e.to).push(e)}let Wt=(e,r,s)=>{let c=e.map((f)=>f.pt),y=r?e.indexOf(r):0;for(let f=y+1;f<e.length;f++)c[f]=Math.max(c[f],c[f-1]+12);for(let f=y-1;f>=0;f--)c[f]=Math.min(c[f],c[f+1]-12);let p=Math.max(0,c[c.length-1]-(s.v+s.hv-5))-Math.max(0,s.v-s.hv+5-c[0]);return{pt:c.map((f)=>f-p),moved:p!==0}};for(let e of Nt.values()){if(e.length<2)continue;let r=e[0].T;e.sort((p,f)=>p.pt-f.pt||p.S.v-f.S.v);let s=e.filter((p)=>Math.abs(p.ps-p.pt)<0.5).sort((p,f)=>Math.abs(p.pt-r.v)-Math.abs(f.pt-r.v))[0],c=e,y=Wt(c,s,r);if(s&&y.moved)c=e.filter((p)=>{if(p===s||Math.abs(p.S.v-r.v)<=r.hv)return!0;let f=p.S.v>r.v?1:-1,S=[[p.S.u+p.S.hu,p.S.v],[r.u,p.S.v],[r.u,r.v+f*r.hv]];if(ne(S,p.e.from,p.e.to))return!0;return p.alt=S,!1}),y=Wt(c,s,r);c.forEach((p,f)=>{p.pt=y.pt[f]});for(let p of c)if(Math.abs(p.pt-p.S.v)<p.S.hv-4)p.ps=p.pt}let At=(e,r,s=0)=>{if(e.pts=r.map(([c,y])=>R(c,y)),e.flags.has("curve")&&r.length===4)e.curve=s};for(let e of Ot){let r=e.alt??xe(e);if(r)At(e.e,r,1);else St.push(e.e)}let t=(e)=>o.edges.some((r)=>r!==e&&(r.from===e.from&&r.to===e.to||r.from===e.to&&r.to===e.from));for(let e=St.length-1;e>=0;e--){let r=St[e],s=vt.get(r.from),c=vt.get(r.to);if(r.from===r.to||r.flags.has("skip")||c.u+c.hu>s.u-s.hu-2||t(r))continue;let y=s.u-s.hu,p=c.u+c.hu,f=c.v>s.v?1:-1,S=(y+p)/2,E=Math.max(s.v-s.hv,c.v-c.hv)+6,m=Math.min(s.v+s.hv,c.v+c.hv)-6,w=[];if(E<=m){let P=Math.min(m,Math.max(E,s.hv<=c.hv?s.v:c.v));w.push([[y,P],[p,P]])}else w.push([[s.u,s.v+f*s.hv],[s.u,c.v],[p,c.v]],[[y,s.v],[S,s.v],[S,c.v],[p,c.v]]);let z=w.find((P)=>!ne(P,r.from,r.to));if(z)At(r,z,1),St.splice(e,1)}for(let e of St){let r=vt.get(e.from),s=vt.get(e.to),c=/^(left|top|-)/.test(e.attrs.side??"")?-1:1,y=Math.min(r.v-r.hv,s.v-s.hv),p=Math.max(r.v+r.hv,s.v+s.hv);if(e.from===e.to){let f=r.v+c*(r.hv+18);At(e,[[r.u+r.hu*0.4,r.v+c*r.hv],[r.u+r.hu*0.4,f],[r.u-r.hu*0.4,f],[r.u-r.hu*0.4,r.v+c*r.hv]],1.33)}else if(!e.flags.has("skip")&&r.u-r.hu<s.u+s.hu&&s.u-s.hu<r.u+r.hu){let f=(Math.max(r.u-r.hu,s.u-s.hu)+Math.min(r.u+r.hu,s.u+s.hu))/2,S=s.v>r.v?1:-1;At(e,[[f,r.v+S*r.hv],[f,s.v-S*s.hv]])}else if(s.u>r.u){let f=r.u+r.hu,S=Math.min(i/2,(s.u-s.hu-f)/2),E=o.nodes.get(e.from);if(o.nodes.get(e.to).groups.some((z)=>!E.groups.includes(z))){let z=Math.min(...[...vt.values()].filter((P)=>P.u-P.hu>f+1&&P.u<s.u&&P.v+P.hv>y&&P.v-P.hv<p).map((P)=>P.u-P.hu));if(z-ce+6-f>6)S=z-ce+6-f}let w=ge(c,f+S,s.u+s.hu,y,p);At(e,[[f,r.v],[f+S,r.v],[f+S,w],[s.u,w],[s.u,s.v+c*s.hv]])}else{let f=ge(c,Math.min(r.u-r.hu,s.u-s.hu),Math.max(r.u+r.hu,s.u+s.hu),y,p);At(e,[[r.u,r.v+c*r.hv],[r.u,f],[s.u,f],[s.u,s.v+c*s.hv]],1.33)}}for(let e of[...o.groups].reverse()){let r=1/0,s=1/0,c=-1/0,y=-1/0,p=(E,m,w=ce)=>{r=Math.min(r,E-w),s=Math.min(s,m-w),c=Math.max(c,E+w),y=Math.max(y,m+w)},f=new Set(u.filter((E)=>E.groups.includes(e.id)).map((E)=>E.id));for(let E of f){let m=o.nodes.get(E);p(m.x-m.w/2,m.y-m.h/2),p(m.x+m.w/2,m.y+m.h/2)}for(let E of o.edges)if(f.has(E.from)&&f.has(E.to))for(let[m,w]of E.pts)p(m,w);else if(f.has(E.to)&&E.pts.length===5)for(let[m,w]of E.pts.slice(2))p(m,w,6);for(let E of o.groups)if(E.parent===e.id&&E.w)p(E.x,E.y),p(E.x+E.w,E.y+E.h);if(r===1/0)continue;let S=e.label?18:0;Object.assign(e,{x:r,y:s-S,w:c-r,h:y-s+S})}let l=[];for(let e of u)if(l.push([e.x-e.w/2,e.y-e.h/2,e.x+e.w/2,e.y+e.h/2+(e.attrs.caption?20:0)]),$e.has(e.shape)&&e.label){let r=bt(e.label,we(e,h),500,e.attrs.fontFamily||x),s=e.y-e.h/2-11-+(e.attrs.ghost??e.attrs.heads??0)*5;l.push([e.x-r.w/2,s-r.h/2,e.x+r.w/2,s+r.h/2])}for(let e of o.groups)if(e.w&&e.label){let r=bt(e.label,h-2,600,x);l.push([e.x+12,e.y+13-r.h/2,e.x+12+r.w,e.y+13+r.h/2])}let d=o.edges.flatMap((e)=>e.pts.slice(1).map((r,s)=>{let c=e.pts[s];return[e,s,[Math.min(c[0],r[0])-2,Math.min(c[1],r[1])-2,Math.max(c[0],r[0])+2,Math.max(c[1],r[1])+2]]}));for(let e of o.edges){if(!e.label||e.pts.length<2)continue;let r=bt(e.label,h-1,Ct,x),s=e.pts[0],c=e.pts[e.pts.length-1],y=(C)=>Math.hypot(e.pts[C+1][0]-e.pts[C][0],e.pts[C+1][1]-e.pts[C][1]),p=0;for(let C=1;C+1<e.pts.length;C++)if(y(C)>y(p))p=C;if(e.flags.has("join")&&e.pts.length===3)p=1;let f=(C,Y,st)=>{let[b,k]=[e.pts[C],e.pts[C+1]],G=b[0]+(k[0]-b[0])*Y,N=b[1]+(k[1]-b[1])*Y;if(Math.abs(b[1]-k[1])<Math.abs(b[0]-k[0])){let L=(N>Math.max(s[1],c[1])+1?1:-1)*st;return[G,N+L*(r.h/2+5),C]}let T=(G<Math.min(s[0],c[0])-1?-1:1)*st;return[G+T*(r.w/2+7),N,C]},S=[p,...e.pts.slice(1).map((C,Y)=>Y).filter((C)=>C!==p&&y(C)>1).sort((C,Y)=>y(Y)-y(C))],E=[0.5,0.25,0.75].flatMap((C)=>S.flatMap((Y)=>[f(Y,C,1),f(Y,C,-1)])),m=([C,Y])=>[C-r.w/2,Y-r.h/2,C+r.w/2,Y+r.h/2],w=([C,Y,st])=>!l.some((b)=>ve(m([C,Y]),b))&&!d.some(([b,k,G])=>!(b===e&&k===st)&&ve(m([C,Y]),G)),[z,P]=E.find(w)??E[0];e.lx=z,e.ly=P,l.push(m([z,P]))}return o}var Tt,fe,Yt={},oo=new Map;function ro(o,n,i){let a=o.createShader(n);if(o.shaderSource(a,i),o.compileShader(a),!o.getShaderParameter(a,o.COMPILE_STATUS))throw Error(o.getShaderInfoLog(a)??"shader");return a}function le(o,n){let i=o.createProgram();if(o.attachShader(i,ro(o,o.VERTEX_SHADER,`
|
|
2
2
|
attribute vec2 aPos;
|
|
3
3
|
varying vec2 vUv;
|
|
4
4
|
void main() {
|
|
5
5
|
vUv = aPos * 0.5 + 0.5;
|
|
6
6
|
gl_Position = vec4(aPos, 0.0, 1.0);
|
|
7
|
-
}`)),
|
|
7
|
+
}`)),o.attachShader(i,ro(o,o.FRAGMENT_SHADER,n)),o.bindAttribLocation(i,0,"aPos"),o.linkProgram(i),!o.getProgramParameter(i,o.LINK_STATUS))throw Error(o.getProgramInfoLog(i)??"link");return i}function vr(){if(Tt!==void 0)return Tt;if(Tt=null,typeof document>"u")return Tt;try{fe=document.createElement("canvas"),fe.width=fe.height=1024;let o=fe.getContext("webgl",{preserveDrawingBuffer:!0,premultipliedAlpha:!1});if(!o)return Tt;o.bindBuffer(o.ARRAY_BUFFER,o.createBuffer()),o.bufferData(o.ARRAY_BUFFER,new Float32Array([-1,-1,1,-1,-1,1,1,1]),o.STATIC_DRAW),o.enableVertexAttribArray(0),o.vertexAttribPointer(0,2,o.FLOAT,!1,0,0),Yt.ice=le(o,`
|
|
8
8
|
precision highp float;
|
|
9
9
|
varying vec2 vUv;
|
|
10
10
|
uniform vec2 uRes;
|
|
@@ -147,7 +147,7 @@ void main() {
|
|
|
147
147
|
|
|
148
148
|
alpha = alpha * frozen + front * 0.8;
|
|
149
149
|
gl_FragColor = vec4(min(col, 1.0), clamp(alpha, 0.0, 0.97));
|
|
150
|
-
}`),
|
|
150
|
+
}`),Yt.heat=le(o,`
|
|
151
151
|
precision highp float;
|
|
152
152
|
varying vec2 vUv;
|
|
153
153
|
uniform vec2 uRes;
|
|
@@ -248,7 +248,7 @@ void main() {
|
|
|
248
248
|
col = mix(col, vec3(0.018, 0.014, 0.012) + rough * 0.035, frame);
|
|
249
249
|
col += exp(-pow((d - 1.4) / 0.9, 2.0)) * 0.16;
|
|
250
250
|
gl_FragColor = vec4(min(col, 1.0), 1.0);
|
|
251
|
-
}`),
|
|
251
|
+
}`),Yt.glass=le(o,`
|
|
252
252
|
precision highp float;
|
|
253
253
|
varying vec2 vUv;
|
|
254
254
|
uniform vec2 uRes;
|
|
@@ -325,7 +325,7 @@ void main() {
|
|
|
325
325
|
float a = 0.2 + frost * 0.18 + rim * 0.35 * vUv.y + edge * 0.6 + s1 * 0.3 + s2 * 0.45;
|
|
326
326
|
vec3 col = vec3(1.0) - (1.0 - vUv.y) * rim * 0.3;
|
|
327
327
|
gl_FragColor = vec4(col, clamp(a, 0.0, 0.9));
|
|
328
|
-
}`),
|
|
328
|
+
}`),Yt.metal=le(o,`
|
|
329
329
|
precision highp float;
|
|
330
330
|
varying vec2 vUv;
|
|
331
331
|
uniform vec2 uRes;
|
|
@@ -403,7 +403,7 @@ void main() {
|
|
|
403
403
|
col = mix(col, mix(vec3(0.32, 0.34, 0.37), vec3(0.96), vUv.y), bevel * 0.55);
|
|
404
404
|
col += exp(-pow((d - 1.2) / 0.8, 2.0)) * 0.25;
|
|
405
405
|
gl_FragColor = vec4(clamp(col, 0.0, 1.0), 1.0);
|
|
406
|
-
}`),
|
|
406
|
+
}`),Yt.noise=le(o,`
|
|
407
407
|
precision highp float;
|
|
408
408
|
varying vec2 vUv;
|
|
409
409
|
uniform vec2 uRes;
|
|
@@ -478,4 +478,4 @@ void main() {
|
|
|
478
478
|
vec3 tint = vec3(hash2(c + 17.0).x, hash2(c + 41.0).x, hash2(c + 73.0).x) - 0.5;
|
|
479
479
|
vec3 col = clamp(0.5 + g * 0.18 + tint * 0.25, 0.0, 1.0);
|
|
480
480
|
gl_FragColor = vec4(col, 0.55 + exp(-pow((d - 1.2) / 0.9, 2.0)) * 0.3);
|
|
481
|
-
}`),uJ=V}catch{uJ=null}return uJ}function sV(V,X,K,Y=0){X=Math.round(X),K=Math.round(K);let I=`${V}:${X}x${K}:${Y}`,O=tV.get(I);if(O)return O;let D=O4();if(!D||X<1||K<1)return null;let F=Math.min(Math.max(1,globalThis.devicePixelRatio||1),2,1024/Math.max(X,K)),E=Math.round(X*F),S=Math.round(K*F),j=rJ[V];D.viewport(0,0,E,S),D.useProgram(j);let f=(h)=>D.getUniformLocation(j,h);D.uniform2f(f("uRes"),X,K),D.uniform1f(f("uTime"),0),D.uniform1f(f("uIntensity"),0.5),D.uniform1f(f("uSeed"),Y),D.uniform1f(f("uCover"),1),D.drawArrays(D.TRIANGLE_STRIP,0,4);let v=document.createElement("canvas");v.width=E,v.height=S,v.getContext("2d").drawImage(UV,0,1024-S,E,S,0,0,E,S);let d=v.toDataURL("image/webp",0.9);return tV.set(I,d),d}var XV={light:{none:["#e2e5e9","#7f8794"],bg:"none",text:"#1d2430",sub:"#5b6474",edge:"#39414f",halo:"#fff",group:["#f5f6f9","#d5dae2"],shadow:"rgba(30,40,60,.10)",dark:!1,ice:["#f7fdff","#d2effb","#aedcf2","#6fb9de"],fire:["#fff6e8","#ffcf9f","#ee7d36"],c:{white:["#ffffff","#8b95a5"],gray:["#eceff3","#9aa3b2"],pink:["#fde1e4","#e58c98"],orange:["#fde4c8","#e69a50"],yellow:["#f4f5cc","#c0c064"],green:["#d8f1de","#68b880"],blue:["#d6eafc","#5f9fd8"],purple:["#e4defa","#9584db"],red:["#ffd7d2","#dd6559"],teal:["#d1f0ec","#48a99f"]}},dark:{none:["#2a2f37","#8a93a2"],bg:"#0e1117",text:"#e8edf5",sub:"#9aa4b5",edge:"#c3cad6",halo:"#0e1117",group:["#161a22","#2b323f"],shadow:"rgba(0,0,0,.45)",dark:!0,ice:["#29506a","#163a50","#0f2c3e","#7fd4ff"],fire:["#4a2a12","#33180a","#ff9447"],c:{white:["#1b2029","#7d8799"],gray:["#1f242d","#8792a4"],pink:["#3a1e29","#ff8ea2"],orange:["#3a2916","#ffae5c"],yellow:["#2f3018","#e3da66"],green:["#142e22","#5ad08a"],blue:["#13273c","#5ab1ff"],purple:["#241e3f","#a892ff"],red:["#3c1b19","#ff786a"],teal:["#11312f","#4cd3c3"]}}},HJ=(V)=>+V.toFixed(1),a=(V,...X)=>V.reduce((K,Y,I)=>K+(typeof X[I-1]==="number"?HJ(X[I-1]):X[I-1])+Y),gJ=(V)=>V.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,"""),DV=(V)=>/^#([\da-f]{3}|[\da-f]{6})$/i.test(V),eV=(V)=>{let X=V.replace("#",""),K=X.length===3?[...X].map((Y)=>Y+Y).join(""):X;return[0,2,4].map((Y)=>parseInt(K.slice(Y,Y+2),16))},DJ=(V,X,K)=>{if(!DV(V)||!DV(X))return V;return K=Math.min(1,Math.max(0,K)),"#"+eV(V).map((Y,I)=>Math.round(Y+(eV(X)[I]-Y)*K).toString(16).padStart(2,"0")).join("")},TJ=(V,X,K,Y,I)=>{return I=Math.min(I,K/2,Y/2),a`M${V+I},${X}h${K-2*I}a${I},${I} 0 0 1 ${I},${I}v${Y-2*I}a${I},${I} 0 0 1 ${-I},${I}h${2*I-K}a${I},${I} 0 0 1 ${-I},${-I}v${2*I-Y}a${I},${I} 0 0 1 ${I},${-I}z`},ZV=(V,X,K)=>a`M${V-K},${X}a${K},${K} 0 1 0 ${2*K},0a${K},${K} 0 1 0 ${-2*K},0z`,lJ=(...V)=>"M"+V.map(HJ).join(",").replace(/([^,]*,[^,]*),/g,"$1L")+"z",U0=-0.65,X0=0.45,nV=Math.cos(U0),J0=Math.sin(U0),V0=Math.cos(X0),$0=Math.sin(X0),CJ=(V,X,K)=>{let Y=V*nV-X*J0,I=V*J0+X*nV;return[Y,-K*V0-I*$0,I*V0-K*$0]},zV=(V,X,K,Y,I,O)=>{let D=V.map((k)=>k[0]),F=V.map((k)=>k[1]),E=Math.min(...D),S=Math.max(...D),j=Math.min(...F),f=Math.max(...F),v=Math.min((Y-2*O)/(S-E||1),(I-2*O)/(f-j||1)),d=X+Y/2-(E+S)/2*v,h=K+I/2-(j+f)/2*v;return(k)=>[d+k[0]*v,h+k[1]*v]},Z4=(V)=>[(0.5+V*0.7548776662)%1,(0.5+V*0.569840291)%1],F4=(V,X)=>Array.from({length:X},(K,Y)=>{let[I,O]=Z4(Y),D=I*2*Math.PI;if(V==="sphere"){let h=2*O-1,k=Math.sqrt(1-h*h);return[k*Math.cos(D),k*Math.sin(D),h]}if(V==="cube"){let h=Y%6,k=2*I-1,MJ=2*O-1,UJ=h<3?1:-1;return h%3===0?[UJ,k,MJ]:h%3===1?[k,UJ,MJ]:[k,MJ,UJ]}let F=O*2*Math.PI,E=0.68,S=0.3,j=0.8,f=(E+S*Math.cos(F))*Math.cos(D),v=(E+S*Math.cos(F))*Math.sin(D),d=S*Math.sin(F);return[f,v*Math.cos(j)-d*Math.sin(j),v*Math.sin(j)+d*Math.cos(j)]}),D4=(V)=>Array.from({length:V},(X,K)=>{let Y=K/V,O=0.06+0.9*[[0.14,0.9],[0.3,0.6],[0.47,1],[0.66,0.75],[0.84,0.5]].reduce((D,[F,E])=>D+E*Math.exp(-(((Y-F)/0.07)**2)),0)*(0.55+0.45*Math.abs(Math.sin(K*1.7)*Math.cos(K*0.37)));return[-O*(0.85+0.15*Math.sin(K*2.3)),O]}),FV=["blue","orange","green","purple","red","teal","pink","yellow"],wV=(V)=>FV[((V|0)%FV.length+FV.length)%FV.length],M4=0;function MV(V,X,K=!0){let Y=XV[X??V.theme]??XV.light,I=`mm${M4++}-`,O=V.dir==="LR"||V.dir==="RL",D=V.fontSize??jV,F=new Map,E=new Map,S=(J)=>{if(!J)return"none";if(/^#?\w+$/.test(J))return J.replace("#","");if(!E.has(J))E.set(J,`css${E.size}`);return E.get(J)},j=1/0,f=1/0,v=-1/0,d=-1/0,h=(J,U,Q=0,Z=0)=>{j=Math.min(j,J),f=Math.min(f,U),v=Math.max(v,J+Q),d=Math.max(d,U+Z)},k=(J)=>{if(!J)return Y.none;if(Y.c[J])return Y.c[J];if(DV(J))return[Y.dark?DJ(J,"#0e1117",0.75):DJ(J,"#ffffff",0.78),J];return[gJ(J),Y.edge]},MJ=(J)=>{let U="c-"+S(J);if(!F.has(U)){let[Q,Z]=k(J),N=Y.dark?DJ(Q,Z,0.16):DJ(Q,"#ffffff",0.6);F.set(U,a`<linearGradient id="${I}${U}" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stop-color="${N}"/><stop offset="1" stop-color="${Q}"/></linearGradient>`)}return`url(#${I}${U})`},UJ=()=>{if(F.has("ice"))return;let[J,U,Q]=Y.ice;F.set("ice",`<linearGradient id="${I}ice" x1="0" y1="0" x2="1" y2="1"><stop offset="0" stop-color="${J}"/><stop offset=".55" stop-color="${U}"/><stop offset="1" stop-color="${Q}"/></linearGradient><pattern id="${I}frost" width="56" height="56" patternUnits="userSpaceOnUse"><path d="M0 14 16 20 25 6M16 20 22 37 41 33 56 45M41 33 47 12 56 9M22 37 9 56M47 12 33 0" fill="none" stroke="#fff" stroke-opacity="${Y.dark?0.18:0.75}" stroke-width="1"/><path d="M5 30 12 33M30 50 36 46M50 25 54 30" stroke="${Y.ice[3]}" stroke-opacity=".35" stroke-width=".8"/></pattern>`)},s=()=>{if(F.has("fire"))return;let[J,U,Q]=Y.fire;F.set("fire",`<linearGradient id="${I}fire" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stop-color="${J}"/><stop offset="1" stop-color="${U}"/></linearGradient><linearGradient id="${I}flame" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stop-color="#ffd24a"/><stop offset=".6" stop-color="${Q}"/><stop offset="1" stop-color="#e0452b"/></linearGradient>`)},OJ=(J)=>{let U=J.attrs.texture??(J.flags.has("frozen")?"ice":J.flags.has("train")?"magma":"");if(U==="ice")return UJ(),{fill:`url(#${I}ice)`,stroke:Y.ice[3],fx:U};if(U==="magma")return s(),{fill:`url(#${I}fire)`,stroke:Y.fire[2],fx:U};if(U==="metal")return{fill:MJ(J.color),stroke:Y.dark?"#9aa3ae":"#6d747d",fx:U};return{fill:MJ(J.color),stroke:k(J.color)[1],fx:U}},VJ=(J,U,Q,Z,N,H,L="middle")=>{let o=qJ(J,Z,N),x="",l="";return o.lines.forEach((FJ,e)=>{let C=L==="middle"?U-o.widths[e]/2:L==="start"?U:U-o.widths[e],XJ=Q-o.h/2+(e+0.5)*o.lh+Z*0.36;for(let p of FJ){if(p.math){let $J=p.math;l+=a`<svg x="${C+p.x}" y="${XJ-$J.asc}" width="${$J.w}" height="${$J.asc+$J.desc}" viewBox="${$J.viewBox}" overflow="visible" color="${H}">${$J.inner}</svg>`;continue}if(x+=a`<tspan x="${C+p.x}" y="${XJ+p.dy}"`,p.size!==Z)x+=a` font-size="${p.size}"`;if(p.it)x+=' font-style="italic"';if(p.serif)x+=` font-family='${iV}' font-weight="${p.b?700:400}"`;else if(p.b)x+=' font-weight="700"';x+=`>${gJ(p.t)}</tspan>`}}),h(U-(L==="middle"?o.w/2:L==="end"?o.w:0),Q-o.h/2,o.w,o.h),(x?a`<text font-size="${Z}" font-weight="${N}" fill="${H}" style="white-space:pre">${x}</text>`:"")+l},t=(J,U)=>J.flags.has("frozen")?"ice":J.flags.has("train")?"magma":U==="ice"||U==="magma"?U:"",IJ=(J,U,Q)=>{if(J==="ice")return a`<g transform="translate(${U},${Q})"><circle r="7.5" fill="${Y.dark?"#0f2c3e":"#fff"}" stroke="${Y.ice[3]}"/><path d="M0-4.6V4.6M-4 -2.3 4 2.3M-4 2.3 4-2.3M-1.4-3.6 0-2.4 1.4-3.6M-1.4 3.6 0 2.4 1.4 3.6" fill="none" stroke="${Y.ice[3]}" stroke-width="1.1" stroke-linecap="round"/></g>`;if(J!=="magma")return"";return s(),a`<g transform="translate(${U},${Q})"><circle r="7.5" fill="${Y.dark?"#33180a":"#fff"}" stroke="${Y.fire[2]}"/><path d="M0.4-5C3 -2.4 3.9-.2 3.1 2.2 2.5 4.1-2.3 4.3-3 2.1-3.6.2-2.2-1.2-1.6-2.6-.9-1.4-.3-.9.2-.6.9-2 .9-3.5.4-5z" fill="url(#${I}flame)"/></g>`},ZJ=0,NJ={ice:"ice",magma:"heat",glass:"glass",metal:"metal",noise:"noise"},EJ=(J,U,Q,Z,N,H,L)=>{if(WJ.has(J))return`<path d="${U}" fill="${SJ(J,L)}"/>`;let o=K&&NJ[J]?sV(NJ[J],N,H):null;if(!o){if(J==="noise")return`<path d="${U}" fill="${SJ("grain",L)}"/>`;if(J==="glass")return`<path d="${U}" fill="#fff" fill-opacity="${Y.dark?0.12:0.4}"/>`;if(J==="metal")return`<path d="${U}" fill="${vJ()}"/>`;return""}let x=`m-${J}-${Math.round(N)}x${Math.round(H)}`;if(!F.has(x))F.set(x,a`<image id="${I}${x}" width="${Math.round(N)}" height="${Math.round(H)}" preserveAspectRatio="none" href="${o}"/>`);let l=`${I}k${ZJ++}`;return`<clipPath id="${l}"><path d="${U}"/></clipPath><g clip-path="url(#${l})">`+a`<use href="#${I}${x}" x="${Q}" y="${Z}"/></g>`},WJ=new Set(["quantized","sparse","hatch"]),SJ=(J,U)=>{let Q=`p-${J}-${S(U)}`;if(!F.has(Q)){let[Z,N]=k(U),H=24,L="",o="";if(J==="quantized")H=32,[0,2,1,3,2,0,3,1,1,3,0,2,3,1,2,0].forEach((x,l)=>{L+=`<rect x="${l%4*8}" y="${(l>>2)*8}" width="8" height="8" fill="${DJ(Z,N,x/3*0.7)}"/>`});else if(J==="sparse")[1,0,1,1,0,1,0,1,1,1,0,0,1,0,1,1].forEach((x,l)=>{if(x)L+=`<circle cx="${l%4*6+3}" cy="${(l>>2)*6+3}" r="1.5" fill="${N}" fill-opacity=".75"/>`});else if(J==="hatch")H=7,o=' patternTransform="rotate(45)"',L=`<path d="M0 0V7" stroke="${N}" stroke-opacity=".55" stroke-width="1.4"/>`;else{H=24;for(let x=0;x<64;x++)L+=`<rect x="${x%8*3}" y="${(x>>3)*3}" width="3" height="3" fill="${["#000","#fff"][x*37%7>3?1:0]}" fill-opacity="${0.08+x*53%11/40}"/>`}F.set(Q,`<pattern id="${I}${Q}" width="${H}" height="${H}" patternUnits="userSpaceOnUse"${o}>${L}</pattern>`)}return`url(#${I}${Q})`},cJ=(J,U,Q)=>{if(WJ.has(J))return`<path d="${U}" fill="${SJ(J,Q)}"/>`;if(J==="noise")return`<path d="${U}" fill="${SJ("grain",Q)}"/>`;if(J==="ice")return UJ(),`<path d="${U}" fill="url(#${I}ice)"/><path d="${U}" fill="url(#${I}frost)"/>`;if(J==="magma")return s(),`<path d="${U}" fill="url(#${I}fire)"/>`;if(J==="metal")return`<path d="${U}" fill="${vJ()}"/>`;if(J==="glass")return`<path d="${U}" fill="#fff" fill-opacity="${Y.dark?0.15:0.45}"/>`;return""},vJ=()=>{if(!F.has("metal"))F.set("metal",`<linearGradient id="${I}metal" x1="0" y1="0" x2="1" y2="1"><stop offset="0" stop-color="#e4e7eb"/><stop offset=".45" stop-color="#a7adb5"/><stop offset=".55" stop-color="#cfd3d8"/><stop offset="1" stop-color="#8d949d"/></linearGradient>`);return`url(#${I}metal)`},zJ=(J,U,Q,Z)=>{let N=J+Z,H=J+Q-Z;if(H-N<8)return"";let L=a`M${N},${U+1}H${H}V${U+3.5}`,o=Math.max(2,Math.round((H-N)/9)),x=(H-N)/o;for(let l=0;l<o;l++)L+=a`q${-x/2},${[4,7,3.5,6,5][l%5]} ${-x},0`;return`<path d="${L}z" fill="#fff" fill-opacity="${Y.dark?0.55:0.92}"/>`},kJ=(J)=>{let[U,Q]=GJ(J.attrs.size,[64,64]),Z=+J.attrs.n||1;return{W:U,H:Q,k:Z,x0:J.x-(U+(Z-1)*6)/2,y0:J.y-(Q+(Z-1)*6)/2,fit:J.attrs.fit==="contain"?"xMidYMid meet":"xMidYMid slice",srcs:J.attrs.srcs?JSON.parse(J.attrs.srcs):[J.attrs.src??""]}},mJ=(J)=>{let U=V.edges.find((Q)=>Q.to===J.id&&V.nodes.get(Q.from)?.shape==="image");return U&&V.nodes.get(U.from)},BV=(J)=>{let U=V.nodes.get(J.to);return U.shape==="magnify"&&mJ(U)?.id===J.from},pJ=(J,U)=>{if(J.attrs.region)return GJ(J.attrs.region,[]);let Q=+U.attrs.grid||4,[Z,N]=GJ(J.attrs.patch,[-1,-1]);return Z>=0?[Z/Q,N/Q,1/Q,1/Q]:[0.375,0.375,0.25,0.25]},yJ=(J)=>{let U=J.x-J.w/2,Q=J.y-J.h/2,{fill:Z,stroke:N,fx:H}=OJ(J),L=J.flags.has("dashed")?' stroke-dasharray="4 3"':"",o=J.flags.has("bold")?2:1.2,x=`fill="${Z}" stroke="${N}" stroke-width="${o}"${L}`,l=+(J.attrs.ghost??J.attrs.heads??0),FJ=!1,e=(M,A=[U,Q,J.w,J.h],b=0)=>{let w="";for(let q=Math.min(l,6)-1;q>0;q--)w+=a`<path d="${M}" transform="translate(${q*5},${-q*5})" ${x} opacity="${0.35+0.5/(q+1)}"/>`;if(!Y.dark||H)w+=a`<path d="${M}" transform="translate(0,1.6)" fill="${Y.shadow}"/>`;w+=`<path d="${M}" ${x}/>`;let z=EJ(H,M,A[0],A[1],A[2],A[3],J.color);if(z)FJ=!0,w+=z+`<path d="${M}" fill="none" stroke="${N}" stroke-width="${o}" stroke-opacity=".7"${L}/>`;else if(H==="ice")w+=`<path d="${M}" fill="url(#${I}frost)"/>`+(b?zJ(A[0],A[1],A[2],b):"");return h(U,Q-l*5,J.w+l*5,J.h+l*5),w},C="",XJ=vV(J,D),[p,$J]=H==="metal"?["#1d2430","#e8ebee"]:[Y.text,Y.halo],BJ=()=>FJ&&H==="magma"?`<g paint-order="stroke" stroke="#1a0d07" stroke-width="3" stroke-linejoin="round">${VJ(J.label,J.x,J.y,XJ,600,"#fff4e8")}</g>`:FJ&&H!=="ice"&&H!=="glass"?`<g paint-order="stroke" stroke="${$J}" stroke-width="3" stroke-linejoin="round" stroke-opacity=".85">${VJ(J.label,J.x,J.y,XJ,600,p)}</g>`:VJ(J.label,J.x,J.y,XJ,500,Y.text);switch(J.shape){case"text":C=BJ();break;case"op":{C=e(ZV(J.x,J.y,11),[J.x-11,J.y-11,22,22]);let M=J.label==="+"?a`M${J.x-6.5},${J.y}h13M${J.x},${J.y-6.5}v13`:/^[*x×]$/.test(J.label)?a`M${J.x-4.6},${J.y-4.6}l9.2,9.2M${J.x+4.6},${J.y-4.6}l-9.2,9.2`:J.label==="~"?a`M${J.x-7},${J.y}q3.5,-8 7,0t7,0`:"";C+=M?`<path d="${M}" fill="none" stroke="${N}" stroke-width="1.5" stroke-linecap="round"/>`:VJ(J.label,J.x,J.y,XJ,500,Y.text);break}case"circle":C=e(ZV(J.x,J.y,J.w/2),[U,Q,J.w,J.h])+BJ();break;case"tensor":{let[M,A,b]=GJ(J.attrs.size,[36,64,20]),w=b*0.6,z=U,q=Q+w,[r,y]=k(J.color),R=H==="ice"?Y.ice[0]:Y.dark?DJ(r,y,0.3):DJ(r,"#fff",0.5),P=H==="ice"?Y.ice[2]:Y.dark?DJ(r,"#000",0.25):DJ(r,y,0.35);C=e(TJ(z,q,M,A,1.5),[z,q,M,A])+`<path d="${lJ(z,q,z+w,q-w,z+M+w,q-w,z+M,q)}" fill="${R}" stroke="${N}" stroke-width="1" stroke-linejoin="round"/><path d="${lJ(z+M,q,z+M+w,q-w,z+M+w,q+A-w,z+M,q+A)}" fill="${P}" stroke="${N}" stroke-width="1" stroke-linejoin="round"/>`;break}case"stack":{let[M,A]=GJ(J.attrs.size,[44,60]),b=+J.attrs.n||4,w=+J.attrs.offset||7;for(let z=0;z<b;z++){let q=TJ(U+z*w,Q+z*w,M,A,2);C+=z===b-1?e(q,[U+z*w,Q+z*w,M,A]):`<path d="${q}" ${x}/>`}h(U,Q,J.w,J.h);break}case"enc":case"dec":{let M=J.shape==="enc",b=O?M!==WV()?lJ(U,Q,U+J.w,Q+J.h*0.22,U+J.w,Q+J.h*0.78,U,Q+J.h):lJ(U,Q+J.h*0.22,U+J.w,Q,U+J.w,Q+J.h,U,Q+J.h*0.78):M!==sJ()?lJ(U,Q,U+J.w,Q,U+J.w*0.78,Q+J.h,U+J.w*0.22,Q+J.h):lJ(U+J.w*0.22,Q,U+J.w*0.78,Q,U+J.w,Q+J.h,U,Q+J.h);C=e(b)+BJ();break}case"neurons":{let M=+J.attrs.n||4;for(let A=0;A<M;A++){let b=-((M-1)/2)*20+A*20;{let w=O?J.x:J.x+b,z=O?J.y+b:J.y;C+=e(ZV(w,z,7),[w-7,z-7,14,14])}}break}case"matrix":case"voxels":{let M=+J.attrs.cellSize||16,A=[];if(J.shape==="matrix")JSON.parse(J.attrs.grid).forEach((r,y)=>r.forEach((R,P)=>A.push([[[U+P*M,Q+y*M],[U+(P+1)*M,Q+y*M],[U+(P+1)*M,Q+(y+1)*M],[U+P*M,Q+(y+1)*M]],R,0])));else{let r=JSON.parse(J.attrs.faces),y=+J.attrs.cols,R=M*0.5,P=U,c=Q+ +J.attrs.depth*R,JJ=(m,g)=>[P+m*M+g*R,c-g*R],T=(m,g)=>[P+y*M+g*R,c+m*M-g*R];r.top.forEach((m,g)=>m.forEach((QJ,KJ)=>A.push([[JJ(KJ,g),JJ(KJ+1,g),JJ(KJ+1,g+1),JJ(KJ,g+1)],QJ,0.3]))),r.side.forEach((m,g)=>m.forEach((QJ,KJ)=>A.push([[T(g,KJ),T(g,KJ+1),T(g+1,KJ+1),T(g+1,KJ)],QJ,-0.25]))),r.front.forEach((m,g)=>m.forEach((QJ,KJ)=>A.push([[[P+KJ*M,c+g*M],[P+(KJ+1)*M,c+g*M],[P+(KJ+1)*M,c+(g+1)*M],[P+KJ*M,c+(g+1)*M]],QJ,0])))}let[b,w]=k(J.color),z=Y.dark?"#0e1117":"#ffffff";for(let[r,y,R]of A){let P=y.c?Y.c[y.c]?DJ(Y.c[y.c][0],Y.c[y.c][1],0.55):gJ(y.c):DJ(Y.dark?w:DJ(b,"#ffffff",0.4),Y.dark?b:w,Y.dark?1-(y.v??0.5):y.v??0.5);P=R>0?DJ(P,"#ffffff",R):R<0?DJ(P,"#0b1a2e",-R):P;let c="M"+r.map((m)=>m.map(HJ).join(",")).join("L")+"z";if(C+=`<path d="${c}" fill="${P}" stroke="${z}" stroke-width="1.5" stroke-linejoin="round"/>`,y.t)C+=cJ(y.t,c,y.c??J.color);let[JJ,T]=r.reduce((m,g)=>[m[0]+g[0]/4,m[1]+g[1]/4],[0,0]);if(y.l)C+=VJ(y.l,JJ,T,Math.min(D-2,M*0.6),500,Y.text);if(y.x)C+=fJ(JJ-M*0.28,T-M*0.28,JJ+M*0.28,T+M*0.28,1.4)}let q=JSON.parse(J.attrs.axes??"{}");if(J.attrs.axes){let r=+J.attrs.rows,y=+J.attrs.cols,R=J.shape==="voxels"?+J.attrs.depth*M*0.5:0,P=U,c=Q+R,JJ=y*M,T=r*M,m=D-1,g=V.edges.some((AJ)=>AJ.from===J.id||AJ.to===J.id),QJ=c+T/2,KJ=P+JJ/2;if(g&&O&&Math.abs(QJ-J.y)<m*0.85)QJ=J.y+m*0.85;let i=q.cols?qJ(q.cols,m,500).w:0;if(g&&!O&&Math.abs(KJ-J.x)<i/2+6)KJ=J.x+i/2+6;let YJ="";if(q.cols)YJ+=VJ(q.cols,KJ,c+T+m*0.9,m,500,Y.sub);if(q.rows)YJ+=VJ(q.rows,P-5,QJ,m,500,Y.sub,"end");if(q.depth&&R)YJ+=VJ(q.depth,P+JJ+R/2+5,c+T-R/2+m*0.5,m,500,Y.sub,"start");C+=`<g paint-order="stroke" stroke="${Y.halo}" stroke-width="3" stroke-linejoin="round">${YJ}</g>`}h(U,Q,J.w,J.h);break}case"vector":{let M=SV(J);C=e(TJ(U,Q,J.w,J.h,3));let A=(O?J.h-4:J.w-4)/M.length;M.forEach((b,w)=>{let z=-((M.length-1)/2)*A+w*A;if(w)C+=O?a`<path d="M${U+3},${J.y+z-A/2}h${J.w-6}" stroke="${N}" stroke-opacity=".5"/>`:a`<path d="M${J.x+z-A/2},${Q+3}v${J.h-6}" stroke="${N}" stroke-opacity=".5"/>`;C+=VJ(b.trim(),O?J.x:J.x+z,O?J.y+z:J.y,XJ,500,Y.text)});break}case"image":{let{W:M,H:A,k:b,x0:w,y0:z,fit:q,srcs:r}=kJ(J),y=+J.attrs.grid||0;for(let T=0;T<b;T++){let m=w+T*6,g=z+T*6,QJ=TJ(m,g,M,A,3),KJ=r.length>1?r[b-1-T]:T===b-1?r[0]:void 0;if(!KJ){C+=`<path d="${QJ}" fill="${Y.dark?"#1b2029":"#fff"}" stroke="${N}"/>`;continue}let i=`${I}k${ZJ++}`;if(C+=`<clipPath id="${i}"><path d="${QJ}"/></clipPath>`+a`<image href="${gJ(KJ)}" x="${m}" y="${g}" width="${M}" height="${A}" preserveAspectRatio="${q}" clip-path="url(#${i})"/>`,y>1&&T===b-1){let YJ="";for(let AJ=1;AJ<y;AJ++)YJ+=a`M${m+M*AJ/y},${g}v${A}M${m},${g+A*AJ/y}h${M}`;C+=`<path d="${YJ}" stroke="#fff" stroke-opacity=".9"/>`}C+=`<path d="${QJ}" fill="none" stroke="${N}"/>`}let[R,P,c,JJ]=OV(J.attrs);if(R!==1||P!==0||c!==0||JJ!==1)C=a`<g transform="matrix(${[R,P,c,JJ].map((T)=>T.toFixed(4)).join(" ")} ${J.x-R*J.x-c*J.y} ${J.y-P*J.x-JJ*J.y})">`+C+"</g>";h(U,Q,J.w,J.h);break}case"magnify":{let M=mJ(J),A=Math.min(J.w,J.h)/2,b=J.attrs.lens!=="square",w=b?ZV(J.x,J.y,A):TJ(U,Q,J.w,J.h,4),z=`${I}k${ZJ++}`;if(C+=`<clipPath id="${z}"><path d="${w}"/></clipPath>`,J.attrs.src)C+=a`<image href="${gJ(J.attrs.src)}" x="${U}" y="${Q}" width="${J.w}" height="${J.h}" preserveAspectRatio="${J.attrs.fit==="contain"?"xMidYMid meet":"xMidYMid slice"}" clip-path="url(#${z})"/>`;else if(M){let q=kJ(M),[r,y,R,P]=pJ(J,M);C+=a`<g clip-path="url(#${z})"><svg x="${U}" y="${Q}" width="${J.w}" height="${J.h}" viewBox="${r*q.W} ${y*q.H} ${R*q.W} ${P*q.H}" preserveAspectRatio="xMidYMid slice">`+a`<image href="${gJ(q.srcs[0])}" width="${q.W}" height="${q.H}" preserveAspectRatio="${q.fit}"/></svg></g>`}else C+=`<path d="${w}" fill="${Z}"/>`;if(C+=`<path d="${w}" fill="none" stroke="${N}" stroke-width="2.5"/>`,b){let q=A*Math.SQRT1_2;C+=a`<path d="M${J.x+q+1},${J.y+q+1}l${A*0.5},${A*0.5}" stroke="${N}" stroke-width="5" stroke-linecap="round"/>`,h(J.x+q,J.y+q,A*0.6,A*0.6)}h(U,Q,J.w,J.h);break}case"gaussian":{let M=JSON.parse(J.attrs.peaks??"[[0,0,1,0.45]]"),A=Math.min(40,+J.attrs.mesh||16),[b,w]=k(J.color),z=(T,m)=>M.reduce((g,[QJ,KJ,i=1,YJ=0.45])=>g+i*Math.exp(-((T-QJ)**2+(m-KJ)**2)/(2*YJ*YJ)),0),q=Array.from({length:A+1},(T,m)=>Array.from({length:A+1},(g,QJ)=>z(-1+2*m/A,-1+2*QJ/A))),r=Math.max(...q.flat())||1,y=q.map((T,m)=>T.map((g,QJ)=>CJ(-1+2*m/A,-1+2*QJ/A,g/r))),R=[[-1,-1],[1,-1],[1,1],[-1,1]].map(([T,m])=>CJ(T,m,0)),P=zV([...y.flat(),...R],U,Q,J.w,J.h,2),c=(T)=>P(T).map(HJ).join(",");C+=`<path d="M${R.map(c).join("L")}z" fill="${b}" fill-opacity=".5" stroke="${w}" stroke-opacity=".4"/>`;let JJ=[];for(let T=0;T<A;T++)for(let m=0;m<A;m++){let g=[y[T][m],y[T+1][m],y[T+1][m+1],y[T][m+1]];JJ.push([g.reduce((QJ,KJ)=>QJ+KJ[2],0),`M${g.map(c).join("L")}z`,(q[T][m]+q[T+1][m+1])/2/r])}JJ.sort((T,m)=>m[0]-T[0]);for(let[,T,m]of JJ){let g=DJ(b,w,0.12+0.88*m);C+=`<path d="${T}" fill="${g}" stroke="${DJ(g,Y.dark?"#ffffff":w,0.35)}" stroke-width=".5"/>`}h(U,Q,J.w,J.h);break}case"space":{let[M,A]=k(J.color),b=JSON.parse(J.attrs.points??"[]"),w=Object.values({x:"x",y:"y",z:"z",...JSON.parse(J.attrs.axes??"{}")}),z=CJ(1,0,0)[2]>CJ(-1,0,0)[2]?1:-1,q=CJ(0,1,0)[2]>CJ(0,-1,0)[2]?1:-1,r=[-1,1].flatMap((i)=>[-1,1].flatMap((YJ)=>[-1,1].map((AJ)=>CJ(i,YJ,AJ)))),y=zV(r,U,Q,J.w,J.h,10),R=(i,YJ,AJ)=>y(CJ(i,YJ,AJ)).map(HJ).join(","),P=(i)=>`<path d="M${i.map((YJ)=>R(YJ[0],YJ[1],YJ[2])).join("L")}z" fill="${M}" fill-opacity=".55" stroke="${A}" stroke-opacity=".45" stroke-linejoin="round"/>`;C+=P([[-1,-1,-1],[1,-1,-1],[1,1,-1],[-1,1,-1]])+P([[z,-1,-1],[z,1,-1],[z,1,1],[z,-1,1]])+P([[-1,q,-1],[1,q,-1],[1,q,1],[-1,q,1]]);let c="";for(let i of[-0.5,0,0.5])c+=`M${R(i,-1,-1)}L${R(i,1,-1)}M${R(-1,i,-1)}L${R(1,i,-1)}M${R(z,-1,i)}L${R(z,1,i)}M${R(-1,q,i)}L${R(1,q,i)}`;C+=`<path d="${c}" stroke="${A}" stroke-opacity=".18"/>`;let JJ=b.map((i)=>[...i,CJ(i[0],i[1],i[2])[2]]).sort((i,YJ)=>YJ[YJ.length-1]-i[i.length-1]);for(let i of JJ){let[YJ,AJ]=y(CJ(i[0],i[1],i[2])),HV=Y.c[wV(i.length>4?i[3]:0)][1];C+=a`<circle cx="${YJ}" cy="${AJ}" r="3.2" fill="${HV}" stroke="${Y.dark?"#0e1117":"#fff"}" stroke-width=".9"/>`}let T=(i,YJ,AJ,HV)=>{let[d0,o0]=y(CJ(YJ,AJ,HV));return VJ(i,d0,o0,D-2,400,Y.sub)},[m,g]=[[z,-q],[-z,q]].sort((i,YJ)=>y(CJ(i[0],i[1],0))[0]-y(CJ(YJ[0],YJ[1],0))[0])[0],[QJ,KJ]=y(CJ(m,g,0));C+=T(w[0],0,-q*1.22,-1)+T(w[1],-z*1.22,0,-1)+VJ(w[2],QJ-8,KJ,D-2,400,Y.sub,"end"),h(U,Q,J.w,J.h);break}case"pointcloud":{let[M,A]=k(J.color),w=(J.attrs.points?JSON.parse(J.attrs.points):F4(J.attrs.shape??"torus",+J.attrs.n||240)).map((R)=>[...CJ(R[0],R[1],R[2]),R[3]]),z=zV(w,U,Q,J.w,J.h,4),q=w.map((R)=>R[2]),r=Math.min(...q),y=Math.max(...q);w.sort((R,P)=>P[2]-R[2]);for(let R of w){let P=(y-R[2])/(y-r||1),[c,JJ]=z(R),T=R[3]!==void 0?Y.c[wV(R[3])][1]:DJ(M,A,0.3+0.7*P);C+=a`<circle cx="${c}" cy="${JJ}" r="${1+1.4*P}" fill="${T}" fill-opacity="${0.5+0.5*P}"/>`}h(U,Q,J.w,J.h);break}case"waveform":{C=e(TJ(U,Q,J.w,J.h,6));let M=J.attrs.wave?JSON.parse(J.attrs.wave):D4(72),A=6,b=J.y,w=J.h/2-A,z=(J.w-2*A)/M.length,q="";M.forEach(([r,y],R)=>{let P=U+A+z*(R+0.5);q+=a`M${P},${b-Math.max(y,0.02)*w}V${b-Math.min(r,-0.02)*w}`}),C+=`<path d="${q}" stroke="${N}" stroke-width="${HJ(Math.max(0.8,z*0.6))}" stroke-linecap="round"/>`;break}case"sphere":{let[M,A]=k(J.color),b=Math.min(J.w,J.h)/2-2,w="sph-"+S(J.color);if(!F.has(w))F.set(w,`<radialGradient id="${I}${w}" cx=".35" cy=".3" r=".8"><stop offset="0" stop-color="${Y.dark?DJ(M,A,0.35):"#ffffff"}"/><stop offset=".55" stop-color="${M}"/><stop offset="1" stop-color="${DJ(M,A,0.35)}"/></radialGradient>`);C+=a`<circle cx="${J.x}" cy="${J.y}" r="${b}" fill="url(#${I}${w})" stroke="${A}"/>`;let z="",q="",r=(R)=>{let P;for(let c=0;c<=64;c++){let[JJ,T,m]=CJ(...R(c/64*Math.PI*2)),g=m<=0,QJ=(g===P?"L":"M")+HJ(J.x+JJ*b)+","+HJ(J.y+T*b);if(g)z+=QJ;else q+=QJ;P=g}};for(let R of[-0.5,0,0.5]){let P=Math.sqrt(1-R*R);r((c)=>[P*Math.cos(c),P*Math.sin(c),R])}for(let R of[0,Math.PI/3,2*Math.PI/3])r((P)=>[Math.cos(P)*Math.cos(R),Math.cos(P)*Math.sin(R),Math.sin(P)]);C+=`<path d="${q}" fill="none" stroke="${A}" stroke-opacity=".22" stroke-dasharray="2 2"/><path d="${z}" fill="none" stroke="${A}" stroke-opacity=".55"/>`;let y=JSON.parse(J.attrs.points??"[]");for(let R of y){let P=Math.hypot(R[0],R[1],R[2])||1,[c,JJ,T]=CJ(R[0]/P,R[1]/P,R[2]/P),m=Y.c[wV(R[3]??0)][1];C+=a`<circle cx="${J.x+c*b}" cy="${J.y+JJ*b}" r="3" fill="${m}" stroke="${Y.dark?"#0e1117":"#fff"}" stroke-width=".8" opacity="${T<=0?1:0.3}"/>`}h(U,Q,J.w,J.h);break}default:{let M=J.shape==="thin"?5:7;C=e(TJ(U,Q,J.w,J.h,M),void 0,M)+BJ()}}if(LV.has(J.shape)&&J.label)C+=VJ(J.label,J.x,Q-l*5-11,XJ,500,Y.sub);if(J.attrs.caption)C+=VJ(J.attrs.caption,J.x,J.y+J.h/2+12,D-1,400,Y.sub);let _J=t(J,H);if(_J){let M=J.shape==="op"||J.shape==="circle";C+=M?IJ(_J,J.x+J.w*0.36,J.y-J.h*0.36):IJ(_J,U+J.w-2,Q+2)}if(J.flags.has("crossed")){let M=J.shape==="op"||J.shape==="circle"?J.w*0.15:3;C=`<g opacity=".5">${C}</g>`+fJ(U+M,Q+M,U+J.w-M,Q+J.h-M,2.2)}return`<g>${C}</g>`},fJ=(J,U,Q,Z,N)=>{let H=a`M${J},${U}L${Q},${Z}M${Q},${U}L${J},${Z}`,L=Y.c.red[1];return`<path d="${H}" stroke="${Y.halo}" stroke-width="${N+2.5}" stroke-linecap="round"/><path d="${H}" stroke="${L}" stroke-width="${N}" stroke-linecap="round"/>`},WV=()=>V.dir==="RL",sJ=()=>V.dir==="BT",hJ=(J)=>{if(BV(J))return"";let U=J.attrs.color,Q=!U?Y.edge:Y.c[U]||DV(U)?k(U)[1]:gJ(U);if(J.fc)return RJ(J,Q);let Z=J.pts.map((C)=>[...C]);if(Z.length<2)return"";let N=J.op==="=>"||J.flags.has("thick"),H=J.op.startsWith("..")||J.flags.has("dashed"),L=J.op==="--"||J.op===".."?0:J.op==="<->"?2:1,o=N?9:7.5,x=N?4.2:3.4,l="",FJ=(C,XJ)=>{let p=C[0]-XJ[0],$J=C[1]-XJ[1],BJ=Math.hypot(p,$J)||1,_J=p/BJ,M=$J/BJ,A=C[0]-_J*o,b=C[1]-M*o;return l+=`<path d="${lJ(C[0],C[1],A-M*x,b+_J*x,C[0]-_J*o*0.72,C[1]-M*o*0.72,A+M*x,b-_J*x)}" fill="${Q}"/>`,[C[0]-_J*o*0.7,C[1]-M*o*0.7]};for(let C of Z)h(C[0]-2,C[1]-2,4,4);let e;if(J.curve){let[C,XJ,p,$J]=Z,BJ=J.curve,_J=[C[0]+(XJ[0]-C[0])*BJ,C[1]+(XJ[1]-C[1])*BJ],M=[$J[0]+(p[0]-$J[0])*BJ,$J[1]+(p[1]-$J[1])*BJ],A=L?FJ($J,M):$J,b=L===2?FJ(C,_J):C;e=a`M${b[0]},${b[1]}C${_J[0]},${_J[1]} ${M[0]},${M[1]} ${A[0]},${A[1]}`}else{if(L)Z[Z.length-1]=FJ(Z[Z.length-1],Z[Z.length-2]);if(L===2)Z[0]=FJ(Z[0],Z[1]);e=a`M${Z[0][0]},${Z[0][1]}`;for(let C=1;C<Z.length-1;C++){let[XJ,p,$J]=[Z[C-1],Z[C],Z[C+1]],BJ=Math.hypot(p[0]-XJ[0],p[1]-XJ[1]),_J=Math.hypot($J[0]-p[0],$J[1]-p[1]),M=Math.min(8,BJ/2,_J/2);e+=a`L${p[0]+(XJ[0]-p[0])*M/(BJ||1)},${p[1]+(XJ[1]-p[1])*M/(BJ||1)}Q${p[0]},${p[1]} ${p[0]+($J[0]-p[0])*M/(_J||1)},${p[1]+($J[1]-p[1])*M/(_J||1)}`}e+=a`L${Z[Z.length-1][0]},${Z[Z.length-1][1]}`}if(J.flags.has("crossed")){let C=0,XJ=-1;for(let BJ=0;BJ+1<J.pts.length;BJ++){let _J=Math.hypot(J.pts[BJ+1][0]-J.pts[BJ][0],J.pts[BJ+1][1]-J.pts[BJ][1]);if(_J>XJ)XJ=_J,C=BJ}let p=(J.pts[C][0]+J.pts[C+1][0])/2,$J=(J.pts[C][1]+J.pts[C+1][1])/2;l+=fJ(p-5,$J-5,p+5,$J+5,2)}return a`<path d="${e}" fill="none" stroke="${Q}" stroke-width="${N?2.2:1.4}" stroke-linecap="round" stroke-linejoin="round"${H?' stroke-dasharray="5 4"':""}/>`+l},dJ=(J)=>{let U=+J.attrs.n||4;return Array.from({length:U},(Q,Z)=>{let N=-((U-1)/2)*20+Z*20;return O?[J.x,J.y+N]:[J.x+N,J.y]})},RJ=(J,U)=>{let Q="";for(let[Z,N]of dJ(oJ(J.from)))for(let[H,L]of dJ(oJ(J.to))){let o=Math.hypot(H-Z,L-N),x=(H-Z)/o,l=(L-N)/o;Q+=a`M${Z+x*7},${N+l*7}L${H-x*7},${L-l*7}`}return`<path d="${Q}" stroke="${U}" stroke-opacity=".45" stroke-width=".8"/>`},oJ=(J)=>V.nodes.get(J),KV=(J)=>{if(!J.w)return"";let{fill:U,stroke:Q,fx:Z}=J.attrs.texture||J.flags.has("frozen")||J.flags.has("train")?OJ(J):{fill:J.attrs.color?k(J.attrs.color)[0]:Y.group[0],stroke:J.attrs.color?k(J.attrs.color)[1]:Y.group[1],fx:""},N=J.flags.has("dashed")?' stroke-dasharray="5 4"':"",H=TJ(J.x,J.y,J.w,J.h,12),L=`<path d="${H}" fill="${U}" stroke="${Q}" stroke-width="1.2"${N}/>`,o=EJ(Z,H,J.x,J.y,J.w,J.h,J.attrs.color);if(o)L+=o+`<path d="${H}" fill="none" stroke="${Q}" stroke-width="1.2" stroke-opacity=".7"/>`;else if(Z==="ice")L+=`<path d="${H}" fill="url(#${I}frost)"/>`+zJ(J.x,J.y,J.w,12);if(h(J.x,J.y,J.w,J.h),J.label)L+=VJ(J.label,J.x+12,J.y+13,D-2,600,Y.sub,"start");if(J.attrs.repeat){let[x,l,FJ]=iJ(J);L+=VJ(J.attrs.repeat,x,l,D+2,500,Y.text,FJ)}return L+=IJ(t(J,Z),J.x+J.w-2,J.y+2),L},bJ=[...V.nodes.values()],QV=bJ.reduce((J,U)=>J+U.x,0)/(bJ.length||1),jJ=(J,U)=>J[0]<U[2]&&J[2]>U[0]&&J[1]<U[3]&&J[3]>U[1],eJ=[],IV=(J)=>{let U=new Set;for(let Z=J;Z;Z=V.groups.find((N)=>N.id===Z.parent))U.add(Z.id);let Q=bJ.map((Z)=>[Z.x-Z.w/2,Z.y-Z.h/2,Z.x+Z.w/2,Z.y+Z.h/2+(Z.attrs.caption?20:0)]);for(let Z of V.edges){for(let N=0;N+1<Z.pts.length;N++){let[H,L]=[Z.pts[N],Z.pts[N+1]];Q.push([Math.min(H[0],L[0])-2,Math.min(H[1],L[1])-2,Math.max(H[0],L[0])+2,Math.max(H[1],L[1])+2])}if(Z.label&&Z.lx!==void 0){let N=qJ(Z.label,D-1);Q.push([Z.lx-N.w/2,Z.ly-N.h/2,Z.lx+N.w/2,Z.ly+N.h/2])}}for(let Z of V.groups)if(Z.w&&!U.has(Z.id)&&Z.parent!==J.id)Q.push([Z.x,Z.y,Z.x+Z.w,Z.y+Z.h]);return[...Q,...eJ]},iJ=(J)=>{let U=qJ(J.attrs.repeat,D+2,500),Z={left:(l=0.5)=>[J.x-10,J.y+J.h*l,"end"],right:(l=0.5)=>[J.x+J.w+10,J.y+J.h*l,"start"],top:()=>[J.x+J.w/2,J.y-10-U.h/2,"middle"],bottom:()=>[J.x+J.w/2,J.y+J.h+10+U.h/2,"middle"]},N=([l,FJ,e])=>[e==="end"?l-U.w:e==="middle"?l-U.w/2:l,FJ-U.h/2,e==="start"?l+U.w:e==="middle"?l+U.w/2:l,FJ+U.h/2],H=J.x+J.w/2>QV?"right":"left",L=H==="right"?"left":"right",o=J.attrs["repeat-side"],x=o&&o!=="auto"?Z[o]():void 0;if(!x){let l=[Z[H](),Z.bottom(),Z[L](),Z.top(),...[0.25,0.75].flatMap((e)=>[Z[H](e),Z[L](e)])],FJ=IV(J);x=l.find((e)=>!FJ.some((C)=>jJ(N(e),C)))??l[0]}return eJ.push(N(x)),x},_V=bJ.filter((J)=>J.shape==="magnify").map((J)=>{let U=mJ(J);if(!U)return"";let Q=kJ(U),[Z,N,H,L]=pJ(J,U),[o,x,l,FJ]=OV(U.attrs),e=Q.x0+(Q.k-1)*6,C=Q.y0+(Q.k-1)*6,XJ=(P,c)=>{let JJ=e+P*Q.W-U.x,T=C+c*Q.H-U.y;return[U.x+o*JJ+l*T,U.y+x*JJ+FJ*T]},p=[XJ(Z,N),XJ(Z+H,N),XJ(Z+H,N+L),XJ(Z,N+L)],[$J,BJ]=p.reduce((P,c)=>[P[0]+c[0]/4,P[1]+c[1]/4],[0,0]),_J=Math.hypot(J.x-$J,J.y-BJ)||1,M=-(J.y-BJ)/_J,A=(J.x-$J)/_J,b=(P)=>P[0]*M+P[1]*A,w=[...p].sort((P,c)=>b(P)-b(c)),z=Math.min(J.w,J.h)/2,q=J.attrs.lens==="square"?[[J.x-J.w/2,J.y-J.h/2],[J.x+J.w/2,J.y-J.h/2],[J.x+J.w/2,J.y+J.h/2],[J.x-J.w/2,J.y+J.h/2]].sort((P,c)=>b(P)-b(c)):[[J.x-M*z,J.y-A*z],[J.x+M*z,J.y+A*z]],[r,y]=[q[0],q[q.length-1]],R=k(J.color)[1];return`<path d="M${p.map((P)=>P.map(HJ).join(",")).join("L")}z" fill="none" stroke="${R}" stroke-width="2" stroke-linejoin="round"/><path d="M${w[0].map(HJ)}L${r.map(HJ)}M${w[3].map(HJ)}L${y.map(HJ)}" stroke="${R}" stroke-opacity=".75" stroke-dasharray="3 3"/>`}).join(""),wJ=V.groups.map(KV).join(""),AV=V.edges.map(hJ).join(""),$=[...V.nodes.values()].map(yJ).join(""),B=V.edges.filter((J)=>J.label&&J.lx!==void 0).map((J)=>VJ(J.label,J.lx,J.ly,D-1,400,Y.sub)).join("");if(j===1/0)j=f=v=d=0;let _=14,G=v-j+_*2,u=d-f+_*2,W=Y.bg!=="none"?a`<rect x="${j-_}" y="${f-_}" width="${G}" height="${u}" rx="10" fill="${Y.bg}"/>`:"";return a`<svg xmlns="http://www.w3.org/2000/svg" viewBox="${j-_} ${f-_} ${G} ${u}" width="${G}" height="${u}" font-family='${oV}' style="max-width:100%;height:auto" role="img">`+`<defs>${[...F.values()].join("")}</defs>${W}<g>${wJ}</g><g>${AV}</g><g>${$}</g><g>${_V}</g><g paint-order="stroke" stroke="${Y.halo}" stroke-width="4" stroke-linejoin="round">${B}</g></svg>`}class NV{nodes=new Map;edges=[];groups=new Map;scope=[];ranks=[];ids=new Set;claim(V,X,K){if(V===void 0)while(this.ids.has(V=`${X}${K}`))K++;else if(this.ids.has(V))throw Error(`duplicate id "${V}"`);return this.ids.add(V),V}node(V){let X=this.nodes.get(V);if(!X){let K=this.claim(V.options.id,"n",this.nodes.size),Y=this.scope.map((I)=>this.groups.get(I)?.id).filter((I)=>!!I);X={id:K,mods:[...this.scope],groups:Y},this.nodes.set(V,X)}return X.id}apply(V,X){let K=this.node(V);for(let Y of X)this.edges.push({from:Y.node,to:V,style:Y.style});return new PJ(this,K)}enter(V){let X=V.options;if((X.box??!!(X.label||X.repeat))&&!this.groups.has(V)){let K=[...this.scope].reverse().map((Y)=>this.groups.get(Y)).find(Boolean);this.groups.set(V,{id:this.claim(X.id,"g",this.groups.size),label:X.label??"",parent:K?.id,flags:new Set(X.dashed?["dashed"]:[]),attrs:{...X.repeat&&{repeat:X.repeat},...X.repeatSide&&{"repeat-side":X.repeatSide},...X.color&&{color:X.color},...X.texture&&{texture:X.texture}},x:0,y:0,w:0,h:0})}this.scope.push(V)}id(V){if(V instanceof PJ)return V.node;let X=this.nodes.get(V);if(!X)throw Error(`"${V.label}" is not part of this figure`);return X.id}}class PJ{graph;node;style;constructor(V,X,K={}){this.graph=V;this.node=X;this.style=K}edge(V){return new PJ(this.graph,this.node,{...this.style,...typeof V==="string"?{label:V}:V})}}var Y0=(V,X)=>{let K=V[0]?.graph;if(!K)throw Error(`${X} was called without inputs; start from fig.input()`);if(V.some((Y)=>!(Y instanceof PJ)||Y.graph!==K))throw Error(`${X}: inputs must be Tensors of the same figure`);return K};function LJ(V,X,K,Y=V,I={}){let O=(...D)=>Y0(D,`"${O.label}"`).apply(O,D);return Object.assign(O,{shape:V,kind:Y,label:X,options:K,defaults:I,freeze(){return O.options.frozen=!0,O.options.trainable=!1,O},trainable(){return O.options.trainable=!0,O.options.frozen=!1,O}})}function n(V,X="",K={}){let{kind:Y=V,...I}=typeof K==="string"?{color:K}:K;return(O,D)=>{let[F,E]=typeof O==="string"?[O,D??{}]:[X,O??{}];return LJ(V,F,{...E},Y,I)}}var B0=n("block"),K0=n("thin"),kV=n("text"),Q0=n("circle"),GV=n("op","+"),I0=n("enc","Encoder","blue"),_0=n("dec","Decoder","green"),O0=n("tensor","","blue"),Z0=n("stack","","blue"),F0=n("neurons");function D0(V,X,K){let[Y,I]=typeof X==="string"?[X,K??{}]:["",X??{}];return LJ("vector",Y,{...I,values:V})}var M0=n("block","Multi-Head Attention",{color:"orange",kind:"mha"}),N0=n("block","Attention",{color:"orange",kind:"attention"}),G0=n("block","Feed Forward",{color:"blue",kind:"ffn"}),C0=n("block","MLP",{color:"blue",kind:"mlp"}),W0=n("thin","Add & Norm",{color:"yellow",kind:"norm"}),A0=n("block","Embedding",{color:"pink",kind:"embed"}),H0=n("thin","Linear",{color:"purple",kind:"linear"}),R0=n("thin","Softmax",{color:"green",kind:"softmax"}),E0=n("thin","MatMul",{color:"purple",kind:"matmul"}),q0=n("thin","Concat",{color:"yellow",kind:"concat"}),P0=n("block","Conv",{color:"teal",kind:"conv"}),j0=n("thin","Pool",{color:"gray",kind:"pool"}),L0=n("thin","ReLU",{color:"green",kind:"act"}),S0=n("thin","Dropout",{color:"gray",kind:"dropout"}),v0=n("block","Loss",{color:"red",kind:"loss"}),z0=n("gaussian","","purple"),w0=n("space","","gray"),u0=n("sphere","","blue"),T0=n("pointcloud","","blue");function k0(V,X){let[K,{samples:Y,...I}]=typeof V==="string"?[V,X??{}]:["",V??{}],O;if(Y?.length){let D=Math.min(256,Y.length),F=(E)=>Math.round(E*1000)/1000;O=Array.from({length:D},(E,S)=>{let j=1/0,f=-1/0;for(let v=Math.floor(S*Y.length/D);v<Math.floor((S+1)*Y.length/D);v++)j=Math.min(j,Y[v]),f=Math.max(f,Y[v]);return[F(j),F(f)]})}return LJ("waveform",K,{...I,...O&&{wave:O}},"waveform",{color:"teal"})}var uV=(V)=>typeof V==="string"?V:("toDataURL"in V)?V.toDataURL("image/png"):V.currentSrc||V.src||"";function m0(V,X,K){let[Y,I]=typeof X==="string"?[X,K??{}]:["",X??{}],O=Array.isArray(V)?V.map(uV):[uV(V)];return LJ("image",Y,{n:O.length>1?O.length:void 0,...I,src:O[0],...O.length>1&&{srcs:O}})}function y0(V,X){let[K,{src:Y,...I}]=typeof V==="string"?[V,X??{}]:["",V??{}];return LJ("magnify",K,{...I,...Y&&{src:uV(Y)}},"magnify",{color:"orange"})}var f0=(V,X)=>V===void 0?{v:Math.abs(Math.sin(X*12.9898+4.1)*43758.5453)%1}:typeof V==="number"?{v:V}:typeof V==="string"?{c:V}:{v:V.value,c:V.color,t:V.texture,l:V.label,x:V.crossed};function h0(V,X){let[K,{cells:Y,...I}]=typeof V==="string"?[V,X??{}]:["",V??{}],O=I.rows??(Array.isArray(Y)?Y.length:5),D=I.cols??(Array.isArray(Y)?Math.max(0,...Y.map((S)=>S.length)):5),F=(S,j)=>typeof Y==="function"?Y(S,j):Y?.[S]?.[j],E=Array.from({length:O},(S,j)=>Array.from({length:D},(f,v)=>f0(F(j,v),j*31+v)));return LJ("matrix",K,{...I,rows:O,cols:D,grid:E},"matrix",{color:"blue"})}function b0(V,X){let[K,{cells:Y,...I}]=typeof V==="string"?[V,X??{}]:["",V??{}],O=Array.isArray(Y)?Y:void 0,D=I.rows??O?.length??5,F=I.cols??(O?Math.max(0,...O.map((v)=>v.length)):5),E=I.depth??(O?Math.max(0,...O.flatMap((v)=>v.map((d)=>d.length))):5),S=(v,d,h)=>f0(typeof Y==="function"?Y(v,d,h):Y?.[v]?.[d]?.[h],v*97+d*31+h*7),j=(v,d,h)=>Array.from({length:v},(k,MJ)=>Array.from({length:d},(UJ,s)=>h(MJ,s))),f={front:j(D,F,(v,d)=>S(v,d,0)),top:j(E,F,(v,d)=>S(0,d,v)),side:j(D,E,(v,d)=>S(v,F-1,d))};return LJ("voxels",K,{...I,rows:D,cols:F,depth:E,faces:f},"voxels",{color:"blue"})}var x0=(...V)=>GV("+")(...V),g0=(...V)=>GV("*")(...V);class mV{constructor(V={}){let X=(...K)=>{let Y=Y0(K,X.constructor.name||"module");Y.enter(X);try{return X.forward(...K)}finally{Y.scope.pop()}};return delete X.name,delete X.length,Object.setPrototypeOf(X,new.target.prototype),X.options={...V},X}freeze(){return this.options.frozen=!0,this.options.trainable=!1,this}trainable(){return this.options.trainable=!0,this.options.frozen=!1,this}}var YV={};function l0(V){if(V===null)qV(void 0);else if("tex"in V)qV(V.tex);let{tex:X,...K}=V??{};return YV=V===null?{}:TV(YV,K),structuredClone(YV)}var TV=(V,X)=>{let K={...V.kinds};for(let[Y,I]of Object.entries(X.kinds??{}))K[Y]={...K[Y],...I};return{...V,...X,style:{...V.style,...X.style},kinds:K}},N4=(...V)=>{let X={};for(let K of V)for(let[Y,I]of Object.entries(K??{}))if(I!==void 0)X[Y]=I;return X},G4=new Set(["id","color","frozen","trainable","dashed","bold","crossed"]),C4={leftOf:"left-of",rightOf:"right-of"};class CV{options;g=new NV;constructor(V={}){this.options={...V}}input(V="",X={}){if(typeof V!=="string")Object.assign(V.options,X);let K=typeof V==="string"?kV(V,X):V;return new PJ(this.g,this.g.node(K))}edge(V,X,K={}){if(V.graph!==this.g)throw Error("edge: tensor belongs to another figure");if(!(X instanceof PJ))this.g.node(X);return this.g.edges.push({from:V.node,to:X,style:{...V.style,...typeof K==="string"?{label:K}:K}}),this}rank(...V){return this.g.ranks.push(V),this}diagram(){let V=this.g,X=new Map,K=TV(YV,this.options),Y=0;for(let[O,D]of V.nodes){let F=[K,...D.mods.map((s)=>s.options)],E=O.options,S=N4(O.defaults,...F.flatMap((s)=>[s?.style,s?.kinds?.[O.kind]]),E),j=O.label,f=(s)=>{if(!j.includes(s))return!1;return j=j.replace(s,"").trim(),!0},v=f("\uD83E\uDDCA"),d=f("\uD83D\uDD25"),h=[...D.mods].reverse().map((s)=>s.options).find((s)=>s.frozen||s.trainable),k=E.frozen||v?"frozen":E.trainable||d?"train":h?.frozen?"frozen":h?.trainable?"train":"";if(k&&!E.texture)delete S.texture;let MJ=new Set([k,S.dashed&&"dashed",S.bold&&"bold",S.crossed&&"crossed"].filter((s)=>!!s)),UJ={};for(let[s,OJ]of Object.entries(S)){if(G4.has(s)||OJ===void 0)continue;UJ[C4[s]??s]=OJ instanceof PJ||typeof OJ==="function"?V.id(OJ):Array.isArray(OJ)&&OJ.every((VJ)=>typeof VJ==="number")?OJ.join(","):typeof OJ==="object"?JSON.stringify(OJ):String(OJ)}X.set(D.id,{id:D.id,shape:O.shape,label:j,color:S.color,flags:MJ,attrs:UJ,groups:D.groups,order:Y++,x:0,y:0,w:0,h:0})}let I=V.edges.map(({from:O,to:D,style:F})=>({from:O,to:V.id(D),label:F.label,pts:[],op:F.dense?"=>":F.arrow==="none"?F.dashed?"..":"--":F.arrow==="both"?"<->":F.dashed?"..>":"->",flags:new Set([...["skip","curve","thick","dashed","crossed"].filter((E)=>F[E]),...F.join?["join"]:[]]),attrs:{...F.side&&{side:F.side},...F.color&&{color:F.color}}}));return{dir:K.flow??"TB",theme:K.theme??"light",gap:K.gap,fontSize:K.style?.fontSize,nodes:X,edges:I,groups:[...V.groups.values()].map((O)=>({...O,flags:new Set(O.flags)})),ranks:V.ranks.map((O)=>O.map((D)=>V.id(D)))}}svg(V={}){let X=this.diagram();return MV(tJ(X),V.theme??X.theme,TV(YV,this.options).materials??!0)}async toBlob(V={}){let X=V.format??"svg",K=this.svg({theme:V.theme});if(X==="svg")return new Blob([K],{type:"image/svg+xml"});if(!["png","jpeg","webp"].includes(X))throw Error(`export: unknown format "${X}" (svg, png, jpeg, webp)`);return A4(K,{...V,format:X},`image/${X}`)}async download(V="figure.svg",X={}){let K=/\.(\w+)$/.exec(V)?.[1].toLowerCase(),Y=X.format??(K==="jpg"?"jpeg":K)??"svg",I=URL.createObjectURL(await this.toBlob({...X,format:Y}));Object.assign(document.createElement("a"),{href:I,download:V}).click(),setTimeout(()=>URL.revokeObjectURL(I),1000)}mount(V,X={}){let K=typeof V==="string"?document.querySelector(V):V;if(!K)throw Error(`mount: no element matches "${V}"`);return K.innerHTML=this.svg(X),this}}async function W4(V){let X=[...new Set([...V.matchAll(/<image href="(?!data:)([^"]+)"/g)].map((K)=>K[1].replace(/&/g,"&")))];for(let K of X)try{let Y=await(await fetch(K)).blob(),I=await new Promise((O,D)=>{let F=new FileReader;F.onload=()=>O(F.result),F.onerror=D,F.readAsDataURL(Y)});V=V.split(`href="${K.replace(/&/g,"&")}"`).join(`href="${I}"`)}catch{}return V}async function A4(V,X,K){if(typeof document>"u")throw Error(`export: ${X.format} needs a browser (canvas); in Node use svg()`);let[,Y,I]=/width="([\d.]+)" height="([\d.]+)"/.exec(V)??[],O=X.scale??2,D=new Image;D.src="data:image/svg+xml;charset=utf-8,"+encodeURIComponent(await W4(V)),await D.decode();let F=document.createElement("canvas");F.width=Math.ceil(+Y*O),F.height=Math.ceil(+I*O);let E=F.getContext("2d"),S=X.background??(K==="image/jpeg"?"#ffffff":"");if(S)E.fillStyle=S,E.fillRect(0,0,F.width,F.height);return E.drawImage(D,0,0,F.width,F.height),new Promise((j,f)=>F.toBlob((v)=>v?j(v):f(Error("export: canvas.toBlob failed")),K,X.quality??0.92))}function c0(V,X){let[K,Y]=typeof V==="function"?[{},V]:[V,X],I=new CV(K);return Y(I),I}function p0(V,X={}){return MV(tJ(V),X.theme,X.materials??!0)}var H4={...yV,draw:p0,layout:tJ,THEMES:XV};})();
|
|
481
|
+
}`),Tt=o}catch{Tt=null}return Tt}function so(o,n,i,a=0){n=Math.round(n),i=Math.round(i);let u=`${o}:${n}x${i}:${a}`,h=oo.get(u);if(h)return h;let x=vr();if(!x||n<1||i<1)return null;let g=Math.min(Math.max(1,globalThis.devicePixelRatio||1),2,1024/Math.max(n,i)),v=Math.round(n*g),R=Math.round(i*g),I=Yt[o];x.viewport(0,0,v,R),x.useProgram(I);let q=(X)=>x.getUniformLocation(I,X);x.uniform2f(q("uRes"),n,i),x.uniform1f(q("uTime"),0),x.uniform1f(q("uIntensity"),0.5),x.uniform1f(q("uSeed"),a),x.uniform1f(q("uCover"),1),x.drawArrays(x.TRIANGLE_STRIP,0,4);let O=document.createElement("canvas");O.width=v,O.height=R,O.getContext("2d").drawImage(fe,0,1024-R,v,R,0,0,v,R);let U=O.toDataURL("image/webp",0.9);return oo.set(u,U),U}var ue={light:{none:["#e2e5e9","#7f8794"],bg:"none",text:"#1d2430",sub:"#5b6474",edge:"#39414f",halo:"#fff",group:["#f5f6f9","#d5dae2"],shadow:"rgba(30,40,60,.10)",dark:!1,ice:["#f7fdff","#d2effb","#aedcf2","#6fb9de"],fire:["#fff6e8","#ffcf9f","#ee7d36"],c:{white:["#ffffff","#8b95a5"],gray:["#eceff3","#9aa3b2"],pink:["#fde1e4","#e58c98"],orange:["#fde4c8","#e69a50"],yellow:["#f4f5cc","#c0c064"],green:["#d8f1de","#68b880"],blue:["#d6eafc","#5f9fd8"],purple:["#e4defa","#9584db"],red:["#ffd7d2","#dd6559"],teal:["#d1f0ec","#48a99f"]}},dark:{none:["#2a2f37","#8a93a2"],bg:"#0e1117",text:"#e8edf5",sub:"#9aa4b5",edge:"#c3cad6",halo:"#0e1117",group:["#161a22","#2b323f"],shadow:"rgba(0,0,0,.45)",dark:!0,ice:["#29506a","#163a50","#0f2c3e","#7fd4ff"],fire:["#4a2a12","#33180a","#ff9447"],c:{white:["#1b2029","#7d8799"],gray:["#1f242d","#8792a4"],pink:["#3a1e29","#ff8ea2"],orange:["#3a2916","#ffae5c"],yellow:["#2f3018","#e3da66"],green:["#142e22","#5ad08a"],blue:["#13273c","#5ab1ff"],purple:["#241e3f","#a892ff"],red:["#3c1b19","#ff786a"],teal:["#11312f","#4cd3c3"]}}},xt=(o)=>+o.toFixed(1),Z=(o,...n)=>o.reduce((i,a,u)=>i+(typeof n[u-1]==="number"?xt(n[u-1]):n[u-1])+a),Et=(o)=>o.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,"""),Oe=(o)=>/^#([\da-f]{3}|[\da-f]{6})$/i.test(o),no=(o)=>{let n=o.replace("#",""),i=n.length===3?[...n].map((a)=>a+a).join(""):n;return[0,2,4].map((a)=>parseInt(i.slice(a,a+2),16))},ut=(o,n,i)=>{if(!Oe(o)||!Oe(n))return o;return i=Math.min(1,Math.max(0,i)),"#"+no(o).map((a,u)=>Math.round(a+(no(n)[u]-a)*i).toString(16).padStart(2,"0")).join("")},It=(o,n,i,a,u)=>(u=Math.min(u,i/2,a/2),Z`M${o+u},${n}h${i-2*u}a${u},${u} 0 0 1 ${u},${u}v${a-2*u}a${u},${u} 0 0 1 ${-u},${u}h${2*u-i}a${u},${u} 0 0 1 ${-u},${-u}v${2*u-a}a${u},${u} 0 0 1 ${u},${-u}z`),Me=(o,n,i)=>Z`M${o-i},${n}a${i},${i} 0 1 0 ${2*i},0a${i},${i} 0 1 0 ${-2*i},0z`,Vt=(...o)=>"M"+o.map(xt).join(",").replace(/([^,]*,[^,]*),/g,"$1L")+"z",fo=-0.65,uo=0.45,ao=Math.cos(fo),io=Math.sin(fo),co=Math.cos(uo),lo=Math.sin(uo),mt=(o,n,i)=>{let a=o*ao-n*io,u=o*io+n*ao;return[a,-i*co-u*lo,u*co-i*lo]},Fe=(o,n,i,a,u,h)=>{let x=o.map((V)=>V[0]),g=o.map((V)=>V[1]),v=Math.min(...x),R=Math.max(...x),I=Math.min(...g),q=Math.max(...g),O=Math.min((a-2*h)/(R-v||1),(u-2*h)/(q-I||1)),U=n+a/2-(v+R)/2*O,X=i+u/2-(I+q)/2*O;return(V)=>[U+V[0]*O,X+V[1]*O]},$r=(o)=>[(0.5+o*0.7548776662)%1,(0.5+o*0.569840291)%1],wr=(o,n)=>Array.from({length:n},(i,a)=>{let[u,h]=$r(a),x=u*2*Math.PI;if(o==="sphere"){let X=2*h-1,V=Math.sqrt(1-X*X);return[V*Math.cos(x),V*Math.sin(x),X]}if(o==="cube"){let X=a%6,V=2*u-1,_=2*h-1,ft=X<3?1:-1;return X%3===0?[ft,V,_]:X%3===1?[V,ft,_]:[V,_,ft]}let g=h*2*Math.PI,v=0.68,R=0.3,I=0.8,q=(v+R*Math.cos(g))*Math.cos(x),O=(v+R*Math.cos(g))*Math.sin(x),U=R*Math.sin(g);return[q,O*Math.cos(I)-U*Math.sin(I),O*Math.sin(I)+U*Math.cos(I)]}),kr=(o)=>Array.from({length:o},(n,i)=>{let a=i/o,h=0.06+0.9*[[0.14,0.9],[0.3,0.6],[0.47,1],[0.66,0.75],[0.84,0.5]].reduce((x,[g,v])=>x+v*Math.exp(-(((a-g)/0.07)**2)),0)*(0.55+0.45*Math.abs(Math.sin(i*1.7)*Math.cos(i*0.37)));return[-h*(0.85+0.15*Math.sin(i*2.3)),h]}),Se=["blue","orange","green","purple","red","teal","pink","yellow"],Be=(o)=>Se[((o|0)%Se.length+Se.length)%Se.length],Mr=0;function Ee(o,n,i=!0){let a=ue[n??o.theme]??ue.light,u=`mm${Mr++}-`,h=o.dir==="LR"||o.dir==="RL",x=o.fontSize??Pe,g=o.fontFamily??ye,v=new Map,R=new Map,I=(t)=>{if(!t)return"none";if(/^#?\w+$/.test(t))return t.replace("#","");if(!R.has(t))R.set(t,`css${R.size}`);return R.get(t)},q=1/0,O=1/0,U=-1/0,X=-1/0,V=(t,l,d=0,e=0)=>{q=Math.min(q,t),O=Math.min(O,l),U=Math.max(U,t+d),X=Math.max(X,l+e)},_=(t)=>{if(!t)return a.none;if(a.c[t])return a.c[t];if(Oe(t))return[a.dark?ut(t,"#0e1117",0.75):ut(t,"#ffffff",0.78),t];return[Et(t),a.edge]},ft=(t)=>{let l="c-"+I(t);if(!v.has(l)){let[d,e]=_(t),r=a.dark?ut(d,e,0.16):ut(d,"#ffffff",0.6);v.set(l,Z`<linearGradient id="${u}${l}" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stop-color="${r}"/><stop offset="1" stop-color="${d}"/></linearGradient>`)}return`url(#${u}${l})`},W=()=>{if(v.has("ice"))return;let[t,l,d]=a.ice;v.set("ice",`<linearGradient id="${u}ice" x1="0" y1="0" x2="1" y2="1"><stop offset="0" stop-color="${t}"/><stop offset=".55" stop-color="${l}"/><stop offset="1" stop-color="${d}"/></linearGradient><pattern id="${u}frost" width="56" height="56" patternUnits="userSpaceOnUse"><path d="M0 14 16 20 25 6M16 20 22 37 41 33 56 45M41 33 47 12 56 9M22 37 9 56M47 12 33 0" fill="none" stroke="#fff" stroke-opacity="${a.dark?0.18:0.75}" stroke-width="1"/><path d="M5 30 12 33M30 50 36 46M50 25 54 30" stroke="${a.ice[3]}" stroke-opacity=".35" stroke-width=".8"/></pattern>`)},lt=()=>{if(v.has("fire"))return;let[t,l,d]=a.fire;v.set("fire",`<linearGradient id="${u}fire" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stop-color="${t}"/><stop offset="1" stop-color="${l}"/></linearGradient><linearGradient id="${u}flame" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stop-color="#ffd24a"/><stop offset=".6" stop-color="${d}"/><stop offset="1" stop-color="#e0452b"/></linearGradient>`)},wt=(t)=>{let l=t.attrs.texture??(t.flags.has("frozen")?"ice":t.flags.has("train")?"magma":"");if(l==="ice")return W(),{fill:`url(#${u}ice)`,stroke:a.ice[3],fx:l};if(l==="magma")return lt(),{fill:`url(#${u}fire)`,stroke:a.fire[2],fx:l};if(l==="metal")return{fill:ft(t.color),stroke:a.dark?"#9aa3ae":"#6d747d",fx:l};return{fill:ft(t.color),stroke:_(t.color)[1],fx:l}},ot=(t,l,d,e,r,s,c="middle",y=g)=>{let p=bt(t,e,r,y),f="",S="";return p.lines.forEach((E,m)=>{let w=c==="middle"?l-p.widths[m]/2:c==="start"?l:l-p.widths[m],z=d-p.h/2+(m+0.5)*p.lh+e*0.36;for(let P of E){if(P.math){let C=P.math;S+=Z`<svg x="${w+P.x}" y="${z-C.asc}" width="${C.w}" height="${C.asc+C.desc}" viewBox="${C.viewBox}" overflow="visible" color="${s}">${C.inner}</svg>`;continue}if(f+=Z`<tspan x="${w+P.x}" y="${z+P.dy}"`,P.size!==e)f+=Z` font-size="${P.size}"`;if(P.it)f+=' font-style="italic"';if(P.serif)f+=` font-family='${to}' font-weight="${P.b?700:400}"`;else if(P.b)f+=' font-weight="700"';f+=`>${Et(P.t)}</tspan>`}}),V(l-(c==="middle"?p.w/2:c==="end"?p.w:0),d-p.h/2,p.w,p.h),(f?Z`<text${y!==g?` font-family="${Et(y)}"`:""} font-size="${e}" font-weight="${r}" fill="${s}" style="white-space:pre">${f}</text>`:"")+S},H=(t,l)=>t.flags.has("frozen")?"ice":t.flags.has("train")?"magma":l==="ice"||l==="magma"?l:"",it=(t,l,d)=>{if(t==="ice")return Z`<g transform="translate(${l},${d})"><circle r="7.5" fill="${a.dark?"#0f2c3e":"#fff"}" stroke="${a.ice[3]}"/><path d="M0-4.6V4.6M-4 -2.3 4 2.3M-4 2.3 4-2.3M-1.4-3.6 0-2.4 1.4-3.6M-1.4 3.6 0 2.4 1.4 3.6" fill="none" stroke="${a.ice[3]}" stroke-width="1.1" stroke-linecap="round"/></g>`;if(t!=="magma")return"";return lt(),Z`<g transform="translate(${l},${d})"><circle r="7.5" fill="${a.dark?"#33180a":"#fff"}" stroke="${a.fire[2]}"/><path d="M0.4-5C3 -2.4 3.9-.2 3.1 2.2 2.5 4.1-2.3 4.3-3 2.1-3.6.2-2.2-1.2-1.6-2.6-.9-1.4-.3-.9.2-.6.9-2 .9-3.5.4-5z" fill="url(#${u}flame)"/></g>`},ct=0,ht={ice:"ice",magma:"heat",glass:"glass",metal:"metal",noise:"noise"},kt=(t,l,d,e,r,s,c)=>{if(pt.has(t))return`<path d="${l}" fill="${yt(t,c)}"/>`;let y=i&&ht[t]?so(ht[t],r,s):null;if(!y){if(t==="noise")return`<path d="${l}" fill="${yt("grain",c)}"/>`;if(t==="glass")return`<path d="${l}" fill="#fff" fill-opacity="${a.dark?0.12:0.4}"/>`;if(t==="metal")return`<path d="${l}" fill="${Pt()}"/>`;return""}let p=`m-${t}-${Math.round(r)}x${Math.round(s)}`;if(!v.has(p))v.set(p,Z`<image id="${u}${p}" width="${Math.round(r)}" height="${Math.round(s)}" preserveAspectRatio="none" href="${y}"/>`);let f=`${u}k${ct++}`;return`<clipPath id="${f}"><path d="${l}"/></clipPath><g clip-path="url(#${f})">`+Z`<use href="#${u}${p}" x="${d}" y="${e}"/></g>`},pt=new Set(["quantized","sparse","hatch"]),yt=(t,l)=>{let d=`p-${t}-${I(l)}`;if(!v.has(d)){let[e,r]=_(l),s=24,c="",y="";if(t==="quantized")s=32,[0,2,1,3,2,0,3,1,1,3,0,2,3,1,2,0].forEach((p,f)=>{c+=`<rect x="${f%4*8}" y="${(f>>2)*8}" width="8" height="8" fill="${ut(e,r,p/3*0.7)}"/>`});else if(t==="sparse")[1,0,1,1,0,1,0,1,1,1,0,0,1,0,1,1].forEach((p,f)=>{if(p)c+=`<circle cx="${f%4*6+3}" cy="${(f>>2)*6+3}" r="1.5" fill="${r}" fill-opacity=".75"/>`});else if(t==="hatch")s=7,y=' patternTransform="rotate(45)"',c=`<path d="M0 0V7" stroke="${r}" stroke-opacity=".55" stroke-width="1.4"/>`;else{s=24;for(let p=0;p<64;p++)c+=`<rect x="${p%8*3}" y="${(p>>3)*3}" width="3" height="3" fill="${["#000","#fff"][p*37%7>3?1:0]}" fill-opacity="${0.08+p*53%11/40}"/>`}v.set(d,`<pattern id="${u}${d}" width="${s}" height="${s}" patternUnits="userSpaceOnUse"${y}>${c}</pattern>`)}return`url(#${u}${d})`},Gt=(t,l,d)=>{if(pt.has(t))return`<path d="${l}" fill="${yt(t,d)}"/>`;if(t==="noise")return`<path d="${l}" fill="${yt("grain",d)}"/>`;if(t==="ice")return W(),`<path d="${l}" fill="url(#${u}ice)"/><path d="${l}" fill="url(#${u}frost)"/>`;if(t==="magma")return lt(),`<path d="${l}" fill="url(#${u}fire)"/>`;if(t==="metal")return`<path d="${l}" fill="${Pt()}"/>`;if(t==="glass")return`<path d="${l}" fill="#fff" fill-opacity="${a.dark?0.15:0.45}"/>`;return""},Pt=()=>{if(!v.has("metal"))v.set("metal",`<linearGradient id="${u}metal" x1="0" y1="0" x2="1" y2="1"><stop offset="0" stop-color="#e4e7eb"/><stop offset=".45" stop-color="#a7adb5"/><stop offset=".55" stop-color="#cfd3d8"/><stop offset="1" stop-color="#8d949d"/></linearGradient>`);return`url(#${u}metal)`},Xt=(t,l,d,e)=>{let r=t+e,s=t+d-e;if(s-r<8)return"";let c=Z`M${r},${l+1}H${s}V${l+3.5}`,y=Math.max(2,Math.round((s-r)/9)),p=(s-r)/y;for(let f=0;f<y;f++)c+=Z`q${-p/2},${[4,7,3.5,6,5][f%5]} ${-p},0`;return`<path d="${c}z" fill="#fff" fill-opacity="${a.dark?0.55:0.92}"/>`},Dt=(t)=>{let[l,d]=dt(t.attrs.size,[64,64]),e=+t.attrs.n||1;return{W:l,H:d,k:e,x0:t.x-(l+(e-1)*6)/2,y0:t.y-(d+(e-1)*6)/2,fit:t.attrs.fit==="contain"?"xMidYMid meet":"xMidYMid slice",srcs:t.attrs.srcs?JSON.parse(t.attrs.srcs):[t.attrs.src??""]}},Lt=(t)=>{let l=o.edges.find((d)=>d.to===t.id&&o.nodes.get(d.from)?.shape==="image");return l&&o.nodes.get(l.from)},he=(t)=>{let l=o.nodes.get(t.to);return l.shape==="magnify"&&Lt(l)?.id===t.from},zt=(t,l)=>{if(t.attrs.region)return dt(t.attrs.region,[]);let d=+l.attrs.grid||4,[e,r]=dt(t.attrs.patch,[-1,-1]);return e>=0?[e/d,r/d,1/d,1/d]:[0.375,0.375,0.25,0.25]},Kt=(t)=>{let l=t.x-t.w/2,d=t.y-t.h/2,{fill:e,stroke:r,fx:s}=wt(t),c=t.flags.has("dashed")?' stroke-dasharray="4 3"':"",y=t.flags.has("bold")?2:1.2,p=`fill="${e}" stroke="${r}" stroke-width="${y}"${c}`,f=+(t.attrs.ghost??t.attrs.heads??0),S=!1,E=(b,k=[l,d,t.w,t.h],G=0)=>{let N="";for(let L=Math.min(f,6)-1;L>0;L--)N+=Z`<path d="${b}" transform="translate(${L*5},${-L*5})" ${p} opacity="${0.35+0.5/(L+1)}"/>`;if(!a.dark||s)N+=Z`<path d="${b}" transform="translate(0,1.6)" fill="${a.shadow}"/>`;N+=`<path d="${b}" ${p}/>`;let T=kt(s,b,k[0],k[1],k[2],k[3],t.color);if(T)S=!0,N+=T+`<path d="${b}" fill="none" stroke="${r}" stroke-width="${y}" stroke-opacity=".7"${c}/>`;else if(s==="ice")N+=`<path d="${b}" fill="url(#${u}frost)"/>`+(G?Xt(k[0],k[1],k[2],G):"");return V(l,d-f*5,t.w+f*5,t.h+f*5),N},m="",w=we(t,x),z=t.attrs.fontFamily||g,[P,C]=s==="metal"?["#1d2430","#e8ebee"]:[a.text,a.halo],Y=()=>S&&s==="magma"?`<g paint-order="stroke" stroke="#1a0d07" stroke-width="3" stroke-linejoin="round">${ot(t.label,t.x,t.y,w,600,"#fff4e8","middle",z)}</g>`:S&&s!=="ice"&&s!=="glass"?`<g paint-order="stroke" stroke="${C}" stroke-width="3" stroke-linejoin="round" stroke-opacity=".85">${ot(t.label,t.x,t.y,w,600,P,"middle",z)}</g>`:ot(t.label,t.x,t.y,w,500,a.text,"middle",z);switch(t.shape){case"text":m=Y();break;case"empty":break;case"op":{m=E(Me(t.x,t.y,11),[t.x-11,t.y-11,22,22]);let b=t.label==="+"?Z`M${t.x-6.5},${t.y}h13M${t.x},${t.y-6.5}v13`:/^[*x×]$/.test(t.label)?Z`M${t.x-4.6},${t.y-4.6}l9.2,9.2M${t.x+4.6},${t.y-4.6}l-9.2,9.2`:t.label==="~"?Z`M${t.x-7},${t.y}q3.5,-8 7,0t7,0`:"";m+=b?`<path d="${b}" fill="none" stroke="${r}" stroke-width="1.5" stroke-linecap="round"/>`:ot(t.label,t.x,t.y,w,500,a.text,"middle",z);break}case"circle":m=E(Me(t.x,t.y,t.w/2),[l,d,t.w,t.h])+Y();break;case"tensor":{let[b,k,G]=dt(t.attrs.size,[36,64,20]),N=G*0.6,T=l,L=d+N,[Q,j]=_(t.color),M=s==="ice"?a.ice[0]:a.dark?ut(Q,j,0.3):ut(Q,"#fff",0.5),A=s==="ice"?a.ice[2]:a.dark?ut(Q,"#000",0.25):ut(Q,j,0.35);m=E(It(T,L,b,k,1.5),[T,L,b,k])+`<path d="${Vt(T,L,T+N,L-N,T+b+N,L-N,T+b,L)}" fill="${M}" stroke="${r}" stroke-width="1" stroke-linejoin="round"/><path d="${Vt(T+b,L,T+b+N,L-N,T+b+N,L+k-N,T+b,L+k)}" fill="${A}" stroke="${r}" stroke-width="1" stroke-linejoin="round"/>`;break}case"stack":{let[b,k]=dt(t.attrs.size,[44,60]),G=+t.attrs.n||4,N=+t.attrs.offset||7;for(let T=0;T<G;T++){let L=It(l+T*N,d+T*N,b,k,2);m+=T===G-1?E(L,[l+T*N,d+T*N,b,k]):`<path d="${L}" ${p}/>`}V(l,d,t.w,t.h);break}case"enc":case"dec":{let b=t.shape==="enc",G=h?b!==_t()?Vt(l,d,l+t.w,d+t.h*0.22,l+t.w,d+t.h*0.78,l,d+t.h):Vt(l,d+t.h*0.22,l+t.w,d,l+t.w,d+t.h,l,d+t.h*0.78):b!==Ft()?Vt(l,d,l+t.w,d,l+t.w*0.78,d+t.h,l+t.w*0.22,d+t.h):Vt(l+t.w*0.22,d,l+t.w*0.78,d,l+t.w,d+t.h,l,d+t.h);m=E(G)+Y();break}case"neurons":{let b=+t.attrs.n||4;for(let k=0;k<b;k++){let G=-((b-1)/2)*20+k*20;{let N=h?t.x:t.x+G,T=h?t.y+G:t.y;m+=E(Me(N,T,7),[N-7,T-7,14,14])}}break}case"matrix":case"voxels":{let b=+t.attrs.cellSize||16,k=[];if(t.shape==="matrix")JSON.parse(t.attrs.grid).forEach((Q,j)=>Q.forEach((M,A)=>k.push([[[l+A*b,d+j*b],[l+(A+1)*b,d+j*b],[l+(A+1)*b,d+(j+1)*b],[l+A*b,d+(j+1)*b]],M,0])));else{let Q=JSON.parse(t.attrs.faces),j=+t.attrs.cols,M=b*0.5,A=l,K=d+ +t.attrs.depth*M,et=(F,D)=>[A+F*b+D*M,K-D*M],B=(F,D)=>[A+j*b+D*M,K+F*b-D*M];Q.top.forEach((F,D)=>F.forEach((at,nt)=>k.push([[et(nt,D),et(nt+1,D),et(nt+1,D+1),et(nt,D+1)],at,0.3]))),Q.side.forEach((F,D)=>F.forEach((at,nt)=>k.push([[B(D,nt),B(D,nt+1),B(D+1,nt+1),B(D+1,nt)],at,-0.25]))),Q.front.forEach((F,D)=>F.forEach((at,nt)=>k.push([[[A+nt*b,K+D*b],[A+(nt+1)*b,K+D*b],[A+(nt+1)*b,K+(D+1)*b],[A+nt*b,K+(D+1)*b]],at,0])))}let[G,N]=_(t.color),T=a.dark?"#0e1117":"#ffffff";for(let[Q,j,M]of k){let A=j.c?a.c[j.c]?ut(a.c[j.c][0],a.c[j.c][1],0.55):Et(j.c):ut(a.dark?N:ut(G,"#ffffff",0.4),a.dark?G:N,a.dark?1-(j.v??0.5):j.v??0.5);A=M>0?ut(A,"#ffffff",M):M<0?ut(A,"#0b1a2e",-M):A;let K="M"+Q.map((F)=>F.map(xt).join(",")).join("L")+"z";if(m+=`<path d="${K}" fill="${A}" stroke="${T}" stroke-width="1.5" stroke-linejoin="round"/>`,j.t)m+=Gt(j.t,K,j.c??t.color);let[et,B]=Q.reduce((F,D)=>[F[0]+D[0]/4,F[1]+D[1]/4],[0,0]);if(j.l)m+=ot(j.l,et,B,Math.min(x-2,b*0.6),500,a.text);if(j.x)m+=jt(et-b*0.28,B-b*0.28,et+b*0.28,B+b*0.28,1.4)}let L=JSON.parse(t.attrs.axes??"{}");if(t.attrs.axes){let Q=+t.attrs.rows,j=+t.attrs.cols,M=t.shape==="voxels"?+t.attrs.depth*b*0.5:0,A=l,K=d+M,et=j*b,B=Q*b,F=x-1,D=o.edges.some((gt)=>gt.from===t.id||gt.to===t.id),at=K+B/2,nt=A+et/2;if(D&&h&&Math.abs(at-t.y)<F*0.85)at=t.y+F*0.85;let J=L.cols?bt(L.cols,F,500,g).w:0;if(D&&!h&&Math.abs(nt-t.x)<J/2+6)nt=t.x+J/2+6;let rt="";if(L.cols)rt+=ot(L.cols,nt,K+B+F*0.9,F,500,a.sub);if(L.rows)rt+=ot(L.rows,A-5,at,F,500,a.sub,"end");if(L.depth&&M)rt+=ot(L.depth,A+et+M/2+5,K+B-M/2+F*0.5,F,500,a.sub,"start");m+=`<g paint-order="stroke" stroke="${a.halo}" stroke-width="3" stroke-linejoin="round">${rt}</g>`}V(l,d,t.w,t.h);break}case"vector":{let b=je(t);m=E(It(l,d,t.w,t.h,3));let k=(h?t.h-4:t.w-4)/b.length;b.forEach((G,N)=>{let T=-((b.length-1)/2)*k+N*k;if(N)m+=h?Z`<path d="M${l+3},${t.y+T-k/2}h${t.w-6}" stroke="${r}" stroke-opacity=".5"/>`:Z`<path d="M${t.x+T-k/2},${d+3}v${t.h-6}" stroke="${r}" stroke-opacity=".5"/>`;m+=ot(G.trim(),h?t.x:t.x+T,h?t.y+T:t.y,w,500,a.text,"middle",z)});break}case"image":{let{W:b,H:k,k:G,x0:N,y0:T,fit:L,srcs:Q}=Dt(t),j=+t.attrs.grid||0;for(let B=0;B<G;B++){let F=N+B*6,D=T+B*6,at=It(F,D,b,k,3),nt=Q.length>1?Q[G-1-B]:B===G-1?Q[0]:void 0;if(!nt){m+=`<path d="${at}" fill="${a.dark?"#1b2029":"#fff"}" stroke="${r}"/>`;continue}let J=`${u}k${ct++}`;if(m+=`<clipPath id="${J}"><path d="${at}"/></clipPath>`+Z`<image href="${Et(nt)}" x="${F}" y="${D}" width="${b}" height="${k}" preserveAspectRatio="${L}" clip-path="url(#${J})"/>`,j>1&&B===G-1){let rt="";for(let gt=1;gt<j;gt++)rt+=Z`M${F+b*gt/j},${D}v${k}M${F},${D+k*gt/j}h${b}`;m+=`<path d="${rt}" stroke="#fff" stroke-opacity=".9"/>`}m+=`<path d="${at}" fill="none" stroke="${r}"/>`}let[M,A,K,et]=ke(t.attrs);if(M!==1||A!==0||K!==0||et!==1)m=Z`<g transform="matrix(${[M,A,K,et].map((B)=>B.toFixed(4)).join(" ")} ${t.x-M*t.x-K*t.y} ${t.y-A*t.x-et*t.y})">`+m+"</g>";V(l,d,t.w,t.h);break}case"magnify":{let b=Lt(t),k=Math.min(t.w,t.h)/2,G=t.attrs.lens!=="square",N=G?Me(t.x,t.y,k):It(l,d,t.w,t.h,4),T=`${u}k${ct++}`;if(m+=`<clipPath id="${T}"><path d="${N}"/></clipPath>`,t.attrs.src)m+=Z`<image href="${Et(t.attrs.src)}" x="${l}" y="${d}" width="${t.w}" height="${t.h}" preserveAspectRatio="${t.attrs.fit==="contain"?"xMidYMid meet":"xMidYMid slice"}" clip-path="url(#${T})"/>`;else if(b){let L=Dt(b),[Q,j,M,A]=zt(t,b);m+=Z`<g clip-path="url(#${T})"><svg x="${l}" y="${d}" width="${t.w}" height="${t.h}" viewBox="${Q*L.W} ${j*L.H} ${M*L.W} ${A*L.H}" preserveAspectRatio="xMidYMid slice">`+Z`<image href="${Et(L.srcs[0])}" width="${L.W}" height="${L.H}" preserveAspectRatio="${L.fit}"/></svg></g>`}else m+=`<path d="${N}" fill="${e}"/>`;if(m+=`<path d="${N}" fill="none" stroke="${r}" stroke-width="2.5"/>`,G){let L=k*Math.SQRT1_2;m+=Z`<path d="M${t.x+L+1},${t.y+L+1}l${k*0.5},${k*0.5}" stroke="${r}" stroke-width="5" stroke-linecap="round"/>`,V(t.x+L,t.y+L,k*0.6,k*0.6)}V(l,d,t.w,t.h);break}case"gaussian":{let b=JSON.parse(t.attrs.peaks??"[[0,0,1,0.45]]"),k=Math.min(40,+t.attrs.mesh||16),[G,N]=_(t.color),T=(B,F)=>b.reduce((D,[at,nt,J=1,rt=0.45])=>D+J*Math.exp(-((B-at)**2+(F-nt)**2)/(2*rt*rt)),0),L=Array.from({length:k+1},(B,F)=>Array.from({length:k+1},(D,at)=>T(-1+2*F/k,-1+2*at/k))),Q=Math.max(...L.flat())||1,j=L.map((B,F)=>B.map((D,at)=>mt(-1+2*F/k,-1+2*at/k,D/Q))),M=[[-1,-1],[1,-1],[1,1],[-1,1]].map(([B,F])=>mt(B,F,0)),A=Fe([...j.flat(),...M],l,d,t.w,t.h,2),K=(B)=>A(B).map(xt).join(",");m+=`<path d="M${M.map(K).join("L")}z" fill="${G}" fill-opacity=".5" stroke="${N}" stroke-opacity=".4"/>`;let et=[];for(let B=0;B<k;B++)for(let F=0;F<k;F++){let D=[j[B][F],j[B+1][F],j[B+1][F+1],j[B][F+1]];et.push([D.reduce((at,nt)=>at+nt[2],0),`M${D.map(K).join("L")}z`,(L[B][F]+L[B+1][F+1])/2/Q])}et.sort((B,F)=>F[0]-B[0]);for(let[,B,F]of et){let D=ut(G,N,0.12+0.88*F);m+=`<path d="${B}" fill="${D}" stroke="${ut(D,a.dark?"#ffffff":N,0.35)}" stroke-width=".5"/>`}V(l,d,t.w,t.h);break}case"space":{let[b,k]=_(t.color),G=JSON.parse(t.attrs.points??"[]"),N=Object.values({x:"x",y:"y",z:"z",...JSON.parse(t.attrs.axes??"{}")}),T=mt(1,0,0)[2]>mt(-1,0,0)[2]?1:-1,L=mt(0,1,0)[2]>mt(0,-1,0)[2]?1:-1,Q=[-1,1].flatMap((J)=>[-1,1].flatMap((rt)=>[-1,1].map((gt)=>mt(J,rt,gt)))),j=Fe(Q,l,d,t.w,t.h,10),M=(J,rt,gt)=>j(mt(J,rt,gt)).map(xt).join(","),A=(J)=>`<path d="M${J.map((rt)=>M(rt[0],rt[1],rt[2])).join("L")}z" fill="${b}" fill-opacity=".55" stroke="${k}" stroke-opacity=".45" stroke-linejoin="round"/>`;m+=A([[-1,-1,-1],[1,-1,-1],[1,1,-1],[-1,1,-1]])+A([[T,-1,-1],[T,1,-1],[T,1,1],[T,-1,1]])+A([[-1,L,-1],[1,L,-1],[1,L,1],[-1,L,1]]);let K="";for(let J of[-0.5,0,0.5])K+=`M${M(J,-1,-1)}L${M(J,1,-1)}M${M(-1,J,-1)}L${M(1,J,-1)}M${M(T,-1,J)}L${M(T,1,J)}M${M(-1,L,J)}L${M(1,L,J)}`;m+=`<path d="${K}" stroke="${k}" stroke-opacity=".18"/>`;let et=G.map((J)=>[...J,mt(J[0],J[1],J[2])[2]]).sort((J,rt)=>rt[rt.length-1]-J[J.length-1]);for(let J of et){let[rt,gt]=j(mt(J[0],J[1],J[2])),Ne=a.c[Be(J.length>4?J[3]:0)][1];m+=Z`<circle cx="${rt}" cy="${gt}" r="3.2" fill="${Ne}" stroke="${a.dark?"#0e1117":"#fff"}" stroke-width=".9"/>`}let B=(J,rt,gt,Ne)=>{let[tr,er]=j(mt(rt,gt,Ne));return ot(J,tr,er,x-2,400,a.sub)},[F,D]=[[T,-L],[-T,L]].sort((J,rt)=>j(mt(J[0],J[1],0))[0]-j(mt(rt[0],rt[1],0))[0])[0],[at,nt]=j(mt(F,D,0));m+=B(N[0],0,-L*1.22,-1)+B(N[1],-T*1.22,0,-1)+ot(N[2],at-8,nt,x-2,400,a.sub,"end"),V(l,d,t.w,t.h);break}case"pointcloud":{let[b,k]=_(t.color),N=(t.attrs.points?JSON.parse(t.attrs.points):wr(t.attrs.shape??"torus",+t.attrs.n||240)).map((M)=>[...mt(M[0],M[1],M[2]),M[3]]),T=Fe(N,l,d,t.w,t.h,4),L=N.map((M)=>M[2]),Q=Math.min(...L),j=Math.max(...L);N.sort((M,A)=>A[2]-M[2]);for(let M of N){let A=(j-M[2])/(j-Q||1),[K,et]=T(M),B=M[3]!==void 0?a.c[Be(M[3])][1]:ut(b,k,0.3+0.7*A);m+=Z`<circle cx="${K}" cy="${et}" r="${1+1.4*A}" fill="${B}" fill-opacity="${0.5+0.5*A}"/>`}V(l,d,t.w,t.h);break}case"waveform":{m=E(It(l,d,t.w,t.h,6));let b=t.attrs.wave?JSON.parse(t.attrs.wave):kr(72),k=6,G=t.y,N=t.h/2-k,T=(t.w-2*k)/b.length,L="";b.forEach(([Q,j],M)=>{let A=l+k+T*(M+0.5);L+=Z`M${A},${G-Math.max(j,0.02)*N}V${G-Math.min(Q,-0.02)*N}`}),m+=`<path d="${L}" stroke="${r}" stroke-width="${xt(Math.max(0.8,T*0.6))}" stroke-linecap="round"/>`;break}case"sphere":{let[b,k]=_(t.color),G=Math.min(t.w,t.h)/2-2,N="sph-"+I(t.color);if(!v.has(N))v.set(N,`<radialGradient id="${u}${N}" cx=".35" cy=".3" r=".8"><stop offset="0" stop-color="${a.dark?ut(b,k,0.35):"#ffffff"}"/><stop offset=".55" stop-color="${b}"/><stop offset="1" stop-color="${ut(b,k,0.35)}"/></radialGradient>`);m+=Z`<circle cx="${t.x}" cy="${t.y}" r="${G}" fill="url(#${u}${N})" stroke="${k}"/>`;let T="",L="",Q=(M)=>{let A;for(let K=0;K<=64;K++){let[et,B,F]=mt(...M(K/64*Math.PI*2)),D=F<=0,at=(D===A?"L":"M")+xt(t.x+et*G)+","+xt(t.y+B*G);if(D)T+=at;else L+=at;A=D}};for(let M of[-0.5,0,0.5]){let A=Math.sqrt(1-M*M);Q((K)=>[A*Math.cos(K),A*Math.sin(K),M])}for(let M of[0,Math.PI/3,2*Math.PI/3])Q((A)=>[Math.cos(A)*Math.cos(M),Math.cos(A)*Math.sin(M),Math.sin(A)]);m+=`<path d="${L}" fill="none" stroke="${k}" stroke-opacity=".22" stroke-dasharray="2 2"/><path d="${T}" fill="none" stroke="${k}" stroke-opacity=".55"/>`;let j=JSON.parse(t.attrs.points??"[]");for(let M of j){let A=Math.hypot(M[0],M[1],M[2])||1,[K,et,B]=mt(M[0]/A,M[1]/A,M[2]/A),F=a.c[Be(M[3]??0)][1];m+=Z`<circle cx="${t.x+K*G}" cy="${t.y+et*G}" r="3" fill="${F}" stroke="${a.dark?"#0e1117":"#fff"}" stroke-width=".8" opacity="${B<=0?1:0.3}"/>`}V(l,d,t.w,t.h);break}default:{let b=t.shape==="thin"?5:7;m=E(It(l,d,t.w,t.h,b),void 0,b)+Y()}}if($e.has(t.shape)&&t.label)m+=ot(t.label,t.x,d-f*5-11,w,500,a.sub,"middle",z);if(t.attrs.caption)m+=ot(t.attrs.caption,t.x,t.y+t.h/2+12,x-1,400,a.sub);let st=H(t,s);if(st){let b=t.shape==="op"||t.shape==="circle";m+=b?it(st,t.x+t.w*0.36,t.y-t.h*0.36):it(st,l+t.w-2,d+2)}if(t.flags.has("crossed")){let b=t.shape==="op"||t.shape==="circle"?t.w*0.15:3;m=`<g opacity=".5">${m}</g>`+jt(l+b,d+b,l+t.w-b,d+t.h-b,2.2)}return`<g>${m}</g>`},jt=(t,l,d,e,r)=>{let s=Z`M${t},${l}L${d},${e}M${d},${l}L${t},${e}`,c=a.c.red[1];return`<path d="${s}" stroke="${a.halo}" stroke-width="${r+2.5}" stroke-linecap="round"/><path d="${s}" stroke="${c}" stroke-width="${r}" stroke-linecap="round"/>`},_t=()=>o.dir==="RL",Ft=()=>o.dir==="BT",Bt=(t)=>{if(he(t))return"";let l=t.attrs.color,d=!l?a.edge:a.c[l]||Oe(l)?_(l)[1]:Et(l);if(t.fc)return Qt(t,d);let e=t.pts.map((m)=>[...m]);if(e.length<2)return"";let r=t.op==="=>"||t.flags.has("thick"),s=t.op.startsWith("..")||t.flags.has("dashed"),c=t.op==="--"||t.op===".."?0:t.op==="<->"?2:1,y=r?9:7.5,p=r?4.2:3.4,f="",S=(m,w)=>{let z=m[0]-w[0],P=m[1]-w[1],C=Math.hypot(z,P)||1,Y=z/C,st=P/C,b=m[0]-Y*y,k=m[1]-st*y;return f+=`<path d="${Vt(m[0],m[1],b-st*p,k+Y*p,m[0]-Y*y*0.72,m[1]-st*y*0.72,b+st*p,k-Y*p)}" fill="${d}"/>`,[m[0]-Y*y*0.7,m[1]-st*y*0.7]};for(let m of e)V(m[0]-2,m[1]-2,4,4);let E;if(t.curve){let[m,w,z,P]=e,C=t.curve,Y=[m[0]+(w[0]-m[0])*C,m[1]+(w[1]-m[1])*C],st=[P[0]+(z[0]-P[0])*C,P[1]+(z[1]-P[1])*C],b=c?S(P,st):P,k=c===2?S(m,Y):m;E=Z`M${k[0]},${k[1]}C${Y[0]},${Y[1]} ${st[0]},${st[1]} ${b[0]},${b[1]}`}else{if(c)e[e.length-1]=S(e[e.length-1],e[e.length-2]);if(c===2)e[0]=S(e[0],e[1]);E=Z`M${e[0][0]},${e[0][1]}`;for(let m=1;m<e.length-1;m++){let[w,z,P]=[e[m-1],e[m],e[m+1]],C=Math.hypot(z[0]-w[0],z[1]-w[1]),Y=Math.hypot(P[0]-z[0],P[1]-z[1]),st=Math.min(8,C/2,Y/2);E+=Z`L${z[0]+(w[0]-z[0])*st/(C||1)},${z[1]+(w[1]-z[1])*st/(C||1)}Q${z[0]},${z[1]} ${z[0]+(P[0]-z[0])*st/(Y||1)},${z[1]+(P[1]-z[1])*st/(Y||1)}`}E+=Z`L${e[e.length-1][0]},${e[e.length-1][1]}`}if(t.flags.has("crossed")){let m=0,w=-1;for(let C=0;C+1<t.pts.length;C++){let Y=Math.hypot(t.pts[C+1][0]-t.pts[C][0],t.pts[C+1][1]-t.pts[C][1]);if(Y>w)w=Y,m=C}let z=(t.pts[m][0]+t.pts[m+1][0])/2,P=(t.pts[m][1]+t.pts[m+1][1])/2;f+=jt(z-5,P-5,z+5,P+5,2)}return Z`<path d="${E}" fill="none" stroke="${d}" stroke-width="${r?2.2:1.4}" stroke-linecap="round" stroke-linejoin="round"${s?' stroke-dasharray="5 4"':""}/>`+f},Ht=(t)=>{let l=+t.attrs.n||4;return Array.from({length:l},(d,e)=>{let r=-((l-1)/2)*20+e*20;return h?[t.x,t.y+r]:[t.x+r,t.y]})},Qt=(t,l)=>{let d="";for(let[e,r]of Ht(te(t.from)))for(let[s,c]of Ht(te(t.to))){let y=Math.hypot(s-e,c-r),p=(s-e)/y,f=(c-r)/y;d+=Z`M${e+p*7},${r+f*7}L${s-p*7},${c-f*7}`}return`<path d="${d}" stroke="${l}" stroke-opacity=".45" stroke-width=".8"/>`},te=(t)=>o.nodes.get(t),ee=(t)=>{if(!t.w)return"";let{fill:l,stroke:d,fx:e}=t.attrs.texture||t.flags.has("frozen")||t.flags.has("train")?wt(t):{fill:t.attrs.color?_(t.attrs.color)[0]:a.group[0],stroke:t.attrs.color?_(t.attrs.color)[1]:a.group[1],fx:""},r=t.flags.has("dashed")?' stroke-dasharray="5 4"':"",s=It(t.x,t.y,t.w,t.h,12),c=`<path d="${s}" fill="${l}" stroke="${d}" stroke-width="1.2"${r}/>`,y=kt(e,s,t.x,t.y,t.w,t.h,t.attrs.color);if(y)c+=y+`<path d="${s}" fill="none" stroke="${d}" stroke-width="1.2" stroke-opacity=".7"/>`;else if(e==="ice")c+=`<path d="${s}" fill="url(#${u}frost)"/>`+Xt(t.x,t.y,t.w,12);if(V(t.x,t.y,t.w,t.h),t.label)c+=ot(t.label,t.x+12,t.y+13,x-2,600,a.sub,"start");if(t.attrs.repeat){let[p,f,S]=qt(t);c+=ot(t.attrs.repeat,p,f,x+2,500,a.text,S)}return c+=it(H(t,e),t.x+t.w-2,t.y+2),c},$t=[...o.nodes.values()],oe=$t.reduce((t,l)=>t+l.x,0)/($t.length||1),de=[],re=(t)=>{let l=new Set;for(let e=t;e;e=o.groups.find((r)=>r.id===e.parent))l.add(e.id);let d=$t.map((e)=>[e.x-e.w/2,e.y-e.h/2,e.x+e.w/2,e.y+e.h/2+(e.attrs.caption?20:0)]);for(let e of o.edges){for(let r=0;r+1<e.pts.length;r++){let[s,c]=[e.pts[r],e.pts[r+1]];d.push([Math.min(s[0],c[0])-2,Math.min(s[1],c[1])-2,Math.max(s[0],c[0])+2,Math.max(s[1],c[1])+2])}if(e.label&&e.lx!==void 0){let r=bt(e.label,x-1,Ct,g);d.push([e.lx-r.w/2,e.ly-r.h/2,e.lx+r.w/2,e.ly+r.h/2])}}for(let e of o.groups)if(e.w&&!l.has(e.id)&&e.parent!==t.id)d.push([e.x,e.y,e.x+e.w,e.y+e.h]);return[...d,...de]},qt=(t)=>{let l=bt(t.attrs.repeat,x+2,500,g),e={left:(f=0.5)=>[t.x-10,t.y+t.h*f,"end"],right:(f=0.5)=>[t.x+t.w+10,t.y+t.h*f,"start"],top:()=>[t.x+t.w/2,t.y-10-l.h/2,"middle"],bottom:()=>[t.x+t.w/2,t.y+t.h+10+l.h/2,"middle"]},r=([f,S,E])=>[E==="end"?f-l.w:E==="middle"?f-l.w/2:f,S-l.h/2,E==="start"?f+l.w:E==="middle"?f+l.w/2:f,S+l.h/2],s=t.x+t.w/2>oe?"right":"left",c=s==="right"?"left":"right",y=t.attrs["repeat-side"],p=y&&y!=="auto"?e[y]():void 0;if(!p){let f=[e[s](),e.bottom(),e[c](),e.top(),...[0.25,0.75].flatMap((E)=>[e[s](E),e[c](E)])],S=re(t);p=f.find((E)=>!S.some((m)=>ve(r(E),m)))??f[0]}return de.push(r(p)),p},se=$t.filter((t)=>t.shape==="magnify").map((t)=>{let l=Lt(t);if(!l)return"";let d=Dt(l),[e,r,s,c]=zt(t,l),[y,p,f,S]=ke(l.attrs),E=d.x0+(d.k-1)*6,m=d.y0+(d.k-1)*6,w=(M,A)=>{let K=E+M*d.W-l.x,et=m+A*d.H-l.y;return[l.x+y*K+f*et,l.y+p*K+S*et]},z=[w(e,r),w(e+s,r),w(e+s,r+c),w(e,r+c)],[P,C]=z.reduce((M,A)=>[M[0]+A[0]/4,M[1]+A[1]/4],[0,0]),Y=Math.hypot(t.x-P,t.y-C)||1,st=-(t.y-C)/Y,b=(t.x-P)/Y,k=(M)=>M[0]*st+M[1]*b,G=[...z].sort((M,A)=>k(M)-k(A)),N=Math.min(t.w,t.h)/2,T=t.attrs.lens==="square"?[[t.x-t.w/2,t.y-t.h/2],[t.x+t.w/2,t.y-t.h/2],[t.x+t.w/2,t.y+t.h/2],[t.x-t.w/2,t.y+t.h/2]].sort((M,A)=>k(M)-k(A)):[[t.x-st*N,t.y-b*N],[t.x+st*N,t.y+b*N]],[L,Q]=[T[0],T[T.length-1]],j=_(t.color)[1];return`<path d="M${z.map((M)=>M.map(xt).join(",")).join("L")}z" fill="none" stroke="${j}" stroke-width="2" stroke-linejoin="round"/><path d="M${G[0].map(xt)}L${L.map(xt)}M${G[3].map(xt)}L${Q.map(xt)}" stroke="${j}" stroke-opacity=".75" stroke-dasharray="3 3"/>`}).join(""),vt=["frozen","trainable","glass","metal","noise","quantized","sparse","hatch","crossed"],ne=({position:t,labels:l={}})=>{let d=new Map,e=(w,z)=>{if(!d.has(w))d.set(w,z)};for(let w of[...$t,...o.groups]){let z=w.attrs.texture??(w.flags.has("frozen")?"ice":w.flags.has("train")?"magma":""),P=H(w,z);if(P)e(P==="ice"?"frozen":"trainable");if(z&&z!=="ice"&&z!=="magma")e(z,w.color??w.attrs.color);if(w.flags.has("crossed"))e("crossed")}if(o.edges.some((w)=>w.flags.has("crossed")))e("crossed");let r=vt.filter((w)=>d.has(w)).map((w)=>[w,l[w]??(w==="crossed"?"Removed":w[0].toUpperCase()+w.slice(1))]);if(!r.length)return"";let s=30,c=18,y=x-1,p=t==="right",f=r.map(([,w])=>bt(w,y,Ct,g).w),S=p?U+28:(q+U)/2-f.reduce((w,z)=>w+s+12+z+20,-20)/2,E=p?O+c/2+6:X+22+c/2,m="";return r.forEach(([w,z],P)=>{let C=w==="frozen"?"frozen":w==="trainable"?"train":w==="crossed"?"crossed":"";if(m+=Kt({id:"",shape:"block",label:"",color:d.get(w),flags:new Set(C?[C]:[]),attrs:C?{}:{texture:w},groups:[],order:0,x:S+s/2,y:E,w:s,h:c})+ot(z,S+s+12,E,y,Ct,a.text,"start"),p)E+=c+14;else S+=s+12+f[P]+20}),m},me=o.groups.map(ee).join(""),ge=o.edges.map(Bt).join(""),be=[...o.nodes.values()].map(Kt).join(""),St=o.edges.filter((t)=>t.label&&t.lx!==void 0).map((t)=>ot(t.label,t.lx,t.ly,x-1,400,a.sub)).join(""),xe=o.legend?ne(o.legend):"";if(q===1/0)q=O=U=X=0;let Ot=14,Nt=U-q+Ot*2,Wt=X-O+Ot*2,At=a.bg!=="none"?Z`<rect x="${q-Ot}" y="${O-Ot}" width="${Nt}" height="${Wt}" rx="10" fill="${a.bg}"/>`:"";return Z`<svg xmlns="http://www.w3.org/2000/svg" viewBox="${q-Ot} ${O-Ot} ${Nt} ${Wt}" width="${Nt}" height="${Wt}" font-family="${Et(g)}" style="max-width:100%;height:auto" role="img">`+`<defs>${[...v.values()].join("")}</defs>${At}<g>${me}</g><g>${ge}</g><g>${be}</g><g>${se}</g><g paint-order="stroke" stroke="${a.halo}" stroke-width="4" stroke-linejoin="round">${St}</g>${xe}</svg>`}class Re{nodes=new Map;edges=[];groups=new Map;scope=[];ranks=[];ids=new Set;claim(o,n,i){if(o===void 0)while(this.ids.has(o=`${n}${i}`))i++;else if(this.ids.has(o))throw Error(`duplicate id "${o}"`);return this.ids.add(o),o}node(o){let n=this.nodes.get(o);if(!n){let i=this.claim(o.options.id,"n",this.nodes.size),a=this.scope.map((u)=>this.groups.get(u)?.id).filter((u)=>!!u);n={id:i,mods:[...this.scope],groups:a},this.nodes.set(o,n)}return n.id}apply(o,n){let i=this.node(o);for(let a of n)this.edges.push({from:a.node,to:o,style:a.style});return new Mt(this,i)}enter(o){let n=o.options;if((n.box??!!(n.label||n.repeat))&&!this.groups.has(o)){let i=[...this.scope].reverse().map((a)=>this.groups.get(a)).find(Boolean);this.groups.set(o,{id:this.claim(n.id,"g",this.groups.size),label:n.label??"",parent:i?.id,flags:new Set(n.dashed?["dashed"]:[]),attrs:{...n.repeat&&{repeat:n.repeat},...n.repeatSide&&{"repeat-side":n.repeatSide},...n.color&&{color:n.color},...n.texture&&{texture:n.texture}},x:0,y:0,w:0,h:0})}this.scope.push(o)}id(o){if(o instanceof Mt)return o.node;let n=this.nodes.get(o);if(!n)throw Error(`"${o.label}" is not part of this figure`);return n.id}}class Mt{graph;node;style;constructor(o,n,i={}){this.graph=o;this.node=n;this.style=i}edge(o){return new Mt(this.graph,this.node,{...this.style,...typeof o==="string"?{label:o}:o})}}var po=(o,n)=>{let i=o[0]?.graph;if(!i)throw Error(`${n} was called without inputs; start from fig.input()`);if(o.some((a)=>!(a instanceof Mt)||a.graph!==i))throw Error(`${n}: inputs must be Tensors of the same figure`);return i};function Rt(o,n,i,a=o,u={}){let h=(...x)=>po(x,`"${h.label}"`).apply(h,x);return Object.assign(h,{shape:o,kind:a,label:n,options:i,defaults:u,freeze(){return h.options.frozen=!0,h.options.trainable=!1,h},trainable(){return h.options.trainable=!0,h.options.frozen=!1,h}})}function tt(o,n="",i={}){let{kind:a=o,...u}=typeof i==="string"?{color:i}:i;return(h,x)=>{let[g,v]=typeof h==="string"?[h,x??{}]:[n,h??{}];return Rt(o,g,{...v},a,u)}}var ho=tt("block"),mo=tt("thin"),Ve=tt("text"),go=tt("circle"),Le=tt("op","+"),bo=tt("enc","Encoder","blue"),xo=tt("dec","Decoder","green"),yo=tt("tensor","","blue"),vo=tt("stack","","blue"),$o=tt("neurons"),wo=tt("empty");function ko(o,n,i){let[a,u]=typeof n==="string"?[n,i??{}]:["",n??{}];return Rt("vector",a,{...u,values:o})}var Mo=tt("block","Multi-Head Attention",{color:"orange",kind:"mha"}),So=tt("block","Attention",{color:"orange",kind:"attention"}),Oo=tt("block","Feed Forward",{color:"blue",kind:"ffn"}),Eo=tt("block","MLP",{color:"blue",kind:"mlp"}),Ro=tt("thin","Add & Norm",{color:"yellow",kind:"norm"}),Lo=tt("block","Embedding",{color:"pink",kind:"embed"}),zo=tt("thin","Linear",{color:"purple",kind:"linear"}),No=tt("thin","Softmax",{color:"green",kind:"softmax"}),Ao=tt("thin","MatMul",{color:"purple",kind:"matmul"}),Co=tt("thin","Concat",{color:"yellow",kind:"concat"}),To=tt("block","Conv",{color:"teal",kind:"conv"}),Io=tt("thin","Pool",{color:"gray",kind:"pool"}),Po=tt("thin","ReLU",{color:"green",kind:"act"}),jo=tt("thin","Dropout",{color:"gray",kind:"dropout"}),Fo=tt("block","Loss",{color:"red",kind:"loss"}),Bo=tt("gaussian","","purple"),qo=tt("space","","gray"),Uo=tt("sphere","","blue"),Vo=tt("pointcloud","","blue");function Go(o,n){let[i,{samples:a,...u}]=typeof o==="string"?[o,n??{}]:["",o??{}],h;if(a?.length){let x=Math.min(256,a.length),g=(v)=>Math.round(v*1000)/1000;h=Array.from({length:x},(v,R)=>{let I=1/0,q=-1/0;for(let O=Math.floor(R*a.length/x);O<Math.floor((R+1)*a.length/x);O++)I=Math.min(I,a[O]),q=Math.max(q,a[O]);return[g(I),g(q)]})}return Rt("waveform",i,{...u,...h&&{wave:h}},"waveform",{color:"teal"})}var qe=(o)=>typeof o==="string"?o:("toDataURL"in o)?o.toDataURL("image/png"):o.currentSrc||o.src||"";function Do(o,n,i){let[a,u]=typeof n==="string"?[n,i??{}]:["",n??{}],h=Array.isArray(o)?o.map(qe):[qe(o)];return Rt("image",a,{n:h.length>1?h.length:void 0,...u,src:h[0],...h.length>1&&{srcs:h}})}function _o(o,n){let[i,{src:a,...u}]=typeof o==="string"?[o,n??{}]:["",o??{}];return Rt("magnify",i,{...u,...a&&{src:qe(a)}},"magnify",{color:"orange"})}var Ho=(o,n)=>o===void 0?{v:Math.abs(Math.sin(n*12.9898+4.1)*43758.5453)%1}:typeof o==="number"?{v:o}:typeof o==="string"?{c:o}:{v:o.value,c:o.color,t:o.texture,l:o.label,x:o.crossed};function Wo(o,n){let[i,{cells:a,...u}]=typeof o==="string"?[o,n??{}]:["",o??{}],h=u.rows??(Array.isArray(a)?a.length:5),x=u.cols??(Array.isArray(a)?Math.max(0,...a.map((R)=>R.length)):5),g=(R,I)=>typeof a==="function"?a(R,I):a?.[R]?.[I],v=Array.from({length:h},(R,I)=>Array.from({length:x},(q,O)=>Ho(g(I,O),I*31+O)));return Rt("matrix",i,{...u,rows:h,cols:x,grid:v},"matrix",{color:"blue"})}function Jo(o,n){let[i,{cells:a,...u}]=typeof o==="string"?[o,n??{}]:["",o??{}],h=Array.isArray(a)?a:void 0,x=u.rows??h?.length??5,g=u.cols??(h?Math.max(0,...h.map((O)=>O.length)):5),v=u.depth??(h?Math.max(0,...h.flatMap((O)=>O.map((U)=>U.length))):5),R=(O,U,X)=>Ho(typeof a==="function"?a(O,U,X):a?.[O]?.[U]?.[X],O*97+U*31+X*7),I=(O,U,X)=>Array.from({length:O},(V,_)=>Array.from({length:U},(ft,W)=>X(_,W))),q={front:I(x,g,(O,U)=>R(O,U,0)),top:I(v,g,(O,U)=>R(0,U,O)),side:I(x,v,(O,U)=>R(O,g-1,U))};return Rt("voxels",i,{...u,rows:x,cols:g,depth:v,faces:q},"voxels",{color:"blue"})}var Zo=(...o)=>Le("+")(...o),Yo=(...o)=>Le("*")(...o);class Ge{constructor(o={}){let n=(...i)=>{let a=po(i,n.constructor.name||"module");a.enter(n);try{return n.forward(...i)}finally{a.scope.pop()}};return delete n.name,delete n.length,Object.setPrototypeOf(n,new.target.prototype),n.options={...o},n}freeze(){return this.options.frozen=!0,this.options.trainable=!1,this}trainable(){return this.options.trainable=!0,this.options.frozen=!1,this}}var pe={};function Xo(o){if(o===null)Te(void 0);else if("tex"in o)Te(o.tex);let{tex:n,...i}=o??{};return pe=o===null?{}:Ue(pe,i),structuredClone(pe)}var Ue=(o,n)=>{let i={...o.kinds};for(let[a,u]of Object.entries(n.kinds??{}))i[a]={...i[a],...u};return{...o,...n,style:{...o.style,...n.style},kinds:i}},Sr=(...o)=>{let n={};for(let i of o)for(let[a,u]of Object.entries(i??{}))if(u!==void 0)n[a]=u;return n},Or=new Set(["id","color","frozen","trainable","dashed","bold","crossed"]),Er={leftOf:"left-of",rightOf:"right-of"};class ze{options;g=new Re;constructor(o={}){this.options={...o}}input(o="",n={}){if(typeof o!=="string")Object.assign(o.options,n);let i=typeof o==="string"?Ve(o,n):o;return new Mt(this.g,this.g.node(i))}edge(o,n,i={}){if(o.graph!==this.g)throw Error("edge: tensor belongs to another figure");if(!(n instanceof Mt))this.g.node(n);return this.g.edges.push({from:o.node,to:n,style:{...o.style,...typeof i==="string"?{label:i}:i}}),this}rank(...o){return this.g.ranks.push(o),this}diagram(){let o=this.g,n=new Map,i=Ue(pe,this.options),a=0;for(let[h,x]of o.nodes){let g=[i,...x.mods.map((W)=>W.options)],v=h.options,R=Sr(h.defaults,...g.flatMap((W)=>[W?.style,W?.kinds?.[h.kind]]),v),I=h.label,q=(W)=>{if(!I.includes(W))return!1;return I=I.replace(W,"").trim(),!0},O=q("\uD83E\uDDCA"),U=q("\uD83D\uDD25"),X=[...x.mods].reverse().map((W)=>W.options).find((W)=>W.frozen||W.trainable),V=v.frozen||O?"frozen":v.trainable||U?"train":X?.frozen?"frozen":X?.trainable?"train":"";if(V&&!v.texture)delete R.texture;let _=new Set([V,R.dashed&&"dashed",R.bold&&"bold",R.crossed&&"crossed"].filter((W)=>!!W)),ft={};for(let[W,lt]of Object.entries(R)){if(Or.has(W)||lt===void 0)continue;ft[Er[W]??W]=lt instanceof Mt||typeof lt==="function"?o.id(lt):Array.isArray(lt)&<.every((wt)=>typeof wt==="number")?lt.join(","):typeof lt==="object"?JSON.stringify(lt):String(lt)}n.set(x.id,{id:x.id,shape:h.shape,label:I,color:R.color,flags:_,attrs:ft,groups:x.groups,order:a++,x:0,y:0,w:0,h:0})}let u=o.edges.map(({from:h,to:x,style:g})=>({from:h,to:o.id(x),label:g.label,pts:[],op:g.dense?"=>":g.arrow==="none"?g.dashed?"..":"--":g.arrow==="both"?"<->":g.dashed?"..>":"->",flags:new Set([...["skip","curve","thick","dashed","crossed"].filter((v)=>g[v]),...g.join?["join"]:[]]),attrs:{...g.side&&{side:g.side},...g.color&&{color:g.color}}}));return{dir:i.flow??"TB",theme:i.theme??"light",gap:i.gap,fontSize:i.style?.fontSize,fontFamily:i.style?.fontFamily,legend:i.legend===!0?{}:i.legend||void 0,nodes:n,edges:u,groups:[...o.groups.values()].map((h)=>({...h,flags:new Set(h.flags)})),ranks:o.ranks.map((h)=>h.map((x)=>o.id(x)))}}svg(o={}){let n=this.diagram();return Ee(Zt(n),o.theme??n.theme,Ue(pe,this.options).materials??!0)}async toBlob(o={}){let n=o.format??"svg",i=this.svg({theme:o.theme});if(n==="svg")return new Blob([i],{type:"image/svg+xml"});if(!["png","jpeg","webp"].includes(n))throw Error(`export: unknown format "${n}" (svg, png, jpeg, webp)`);return Lr(i,{...o,format:n},`image/${n}`)}async download(o="figure.svg",n={}){let i=/\.(\w+)$/.exec(o)?.[1].toLowerCase(),a=n.format??(i==="jpg"?"jpeg":i)??"svg",u=URL.createObjectURL(await this.toBlob({...n,format:a}));Object.assign(document.createElement("a"),{href:u,download:o}).click(),setTimeout(()=>URL.revokeObjectURL(u),1000)}mount(o,n={}){let i=typeof o==="string"?document.querySelector(o):o;if(!i)throw Error(`mount: no element matches "${o}"`);return i.innerHTML=this.svg(n),this}}async function Rr(o){let n=[...new Set([...o.matchAll(/<image href="(?!data:)([^"]+)"/g)].map((i)=>i[1].replace(/&/g,"&")))];for(let i of n)try{let a=await(await fetch(i)).blob(),u=await new Promise((h,x)=>{let g=new FileReader;g.onload=()=>h(g.result),g.onerror=x,g.readAsDataURL(a)});o=o.split(`href="${i.replace(/&/g,"&")}"`).join(`href="${u}"`)}catch{}return o}async function Lr(o,n,i){if(typeof document>"u")throw Error(`export: ${n.format} needs a browser (canvas); in Node use svg()`);let[,a,u]=/width="([\d.]+)" height="([\d.]+)"/.exec(o)??[],h=n.scale??2,x=new Image;x.src="data:image/svg+xml;charset=utf-8,"+encodeURIComponent(await Rr(o)),await x.decode();let g=document.createElement("canvas");g.width=Math.ceil(+a*h),g.height=Math.ceil(+u*h);let v=g.getContext("2d"),R=n.background??(i==="image/jpeg"?"#ffffff":"");if(R)v.fillStyle=R,v.fillRect(0,0,g.width,g.height);return v.drawImage(x,0,0,g.width,g.height),new Promise((I,q)=>g.toBlob((O)=>O?I(O):q(Error("export: canvas.toBlob failed")),i,n.quality??0.92))}function Ko(o,n){let[i,a]=typeof o==="function"?[{},o]:[o,n],u=new ze(i);return a(u),u}function Qo(o,n={}){return Ee(Zt(o),n.theme,n.materials??!0)}var zr={...De,draw:Qo,layout:Zt,THEMES:ue};})();
|
package/dist/forwardfig.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
var B0=Object.defineProperty;var K0=(V)=>V;function Q0(V,X){this[V]=K0.bind(null,X)}var I0=(V,X)=>{for(var K in X)B0(V,K,{get:X[K],enumerable:!0,configurable:!0,set:Q0.bind(X,K)})};var TV={};I0(TV,{waveform:()=>J4,voxels:()=>X4,vector:()=>k0,thin:()=>L0,text:()=>V0,tensor:()=>w0,stack:()=>u0,sphere:()=>e0,space:()=>s0,softmax:()=>l0,preset:()=>JJ,pool:()=>o0,pointcloud:()=>n0,op:()=>wV,norm:()=>b0,neurons:()=>T0,mul:()=>B4,mlp:()=>h0,mha:()=>m0,matrix:()=>U4,matmul:()=>c0,magnify:()=>$4,loss:()=>t0,linear:()=>g0,layer:()=>TJ,image:()=>V4,gaussian:()=>r0,figure:()=>F4,ffn:()=>f0,enc:()=>v0,embed:()=>x0,dropout:()=>a0,dec:()=>z0,conv:()=>d0,configure:()=>K4,concat:()=>p0,circle:()=>S0,block:()=>j0,attention:()=>y0,add:()=>Y4,act:()=>i0,Tensor:()=>jJ,Module:()=>U0,Graph:()=>zV,Figure:()=>uV});var gV='Inter,"Helvetica Neue",Helvetica,Arial,sans-serif',lV='"Latin Modern Math","STIX Two Math","Cambria Math","Times New Roman",serif',nJ;function AV(V){nJ=V,WV.clear()}var cV=()=>{let V=globalThis.MathJax;return V?.tex2svg?(X)=>V.tex2svg(X,{display:!1}).querySelector("svg")?.outerHTML??"":null},_0=(V,X)=>{let K=nJ===void 0?cV():nJ;if(!K)return null;let Y;try{Y=K(V)}catch{return null}let I=/viewBox="([^"]+)"/.exec(Y)?.[1];if(!I)return null;let[,O,D,F]=I.trim().split(/[\s,]+/).map(Number),E=X/1000;return{inner:Y.replace(/^<svg[^>]*>/,"").replace(/<\/svg>\s*$/,""),viewBox:I,w:D*E,asc:-O*E,desc:(F+O)*E}},aJ=(V)=>{let X={},K=V.split(" ");for(let Y=0;Y<K.length;Y+=2)X[K[Y]]=K[Y+1];return X},kV=aJ("alpha α beta β gamma γ delta δ epsilon ϵ varepsilon ε zeta ζ eta η theta θ vartheta ϑ iota ι kappa κ lambda λ mu μ nu ν xi ξ pi π rho ρ sigma σ tau τ upsilon υ phi ϕ varphi φ chi χ psi ψ omega ω Gamma Γ Delta Δ Theta Θ Lambda Λ Xi Ξ Pi Π Sigma Σ Phi Φ Psi Ψ Omega Ω"),mV=aJ("cdot · times × oplus ⊕ otimes ⊗ odot ⊙ to → rightarrow → leftarrow ← gets ← Rightarrow ⇒ leftrightarrow ↔ mapsto ↦ sum ∑ prod ∏ int ∫ infty ∞ partial ∂ nabla ∇ in ∈ notin ∉ sim ∼ approx ≈ propto ∝ leq ≤ le ≤ geq ≥ ge ≥ neq ≠ ne ≠ pm ± mp ∓ ldots … dots … cdots ⋯ vdots ⋮ mid ∣ | ‖ ast ∗ star ⋆ circ ∘ bullet • forall ∀ exists ∃ emptyset ∅ cup ∪ cap ∩ subset ⊂ subseteq ⊆ top ⊤ perp ⊥ langle ⟨ rangle ⟩ ell ℓ hbar ℏ dagger † prime ′ { { } } % % _ _ # # & & $ $"),xJ={};for(let V of"+−×·±∓⊕⊗⊙∘∗⋆∪∩")xJ[V]="bin";for(let V of"=<>≤≥≠≈∼∝∈∉⊂⊆→←⇒↔↦∣")xJ[V]="rel";for(let V of",;")xJ[V]="punct";xJ["|"]="mid";for(let V of"([⟨")xJ[V]="open";for(let V of")]⟩")xJ[V]="close";var O0=new Set("log exp ln sin cos tanh max min arg argmax argmin softmax lim det tr sup inf Pr KL diag sign".split(" ")),yV=aJ("hat ̂ widehat ̂ tilde ̃ widetilde ̃ bar ̄ overline ̅ dot ̇ ddot ̈ vec ⃗"),fV=aJ(", : ; quad qquad ! "),Z0=aJ("B ℬ E ℰ F ℱ H ℋ I ℐ L ℒ M ℳ R ℛ"),F0=aJ("C ℂ H ℍ N ℕ P ℙ Q ℚ R ℝ Z ℤ"),hV=(V)=>Z0[V]||(/[A-Z]/.test(V)?String.fromCodePoint(119964+V.charCodeAt(0)-65):V),D0=(V)=>F0[V]||(/[A-Z]/.test(V)?String.fromCodePoint(120120+V.charCodeAt(0)-65):/\d/.test(V)?String.fromCodePoint(120792+ +V):V),bV={mathbf:{up:!0,b:!0},bf:{up:!0,b:!0},boldsymbol:{b:!0},bm:{b:!0},mathrm:{up:!0},rm:{up:!0},operatorname:{up:!0},mathit:{},text:{up:!0,text:!0},textrm:{up:!0,text:!0},mathsf:{up:!0,sans:!0},textbf:{up:!0,b:!0,text:!0},mathcal:{up:!0,map:hV},mathscr:{up:!0,map:hV},mathbb:{up:!0,map:D0}},M0=new Set("left right big Big bigl bigr Bigl Bigr displaystyle".split(" "));function N0(V){let X=0,K=(O,D,F=!1)=>{if(D.text)return{t:O,it:!1,b:!!D.b,serif:!D.sans};if(O==="-")O="−";return{t:O,it:F&&!D.up,b:!!D.b,serif:!D.sans,cls:xJ[O]}};function Y(O,D){let F=[];while(X<V.length){let E=V[X];if(E===D){X++;break}if(E==="^"||E==="_"){X++;let S=E==="^"?"sup":"sub",j=F[F.length-1];if(!j||"t"in j||j[S])F.push(j={});j[S]=I(O);continue}if(E===" "&&!O.text){X++;continue}F.push(...I(O))}return F}function I(O){while(V[X]===" "&&!O.text)X++;let D=V[X++];if(D===void 0)return[];if(D==="{")return Y(O,"}");if(D!=="\\")return[K(O.map?O.map(D):D,O,/[A-Za-z]/.test(D)&&!O.map)];let F=/^[A-Za-z]+/.exec(V.slice(X))?.[0]||V[X]||"";if(X+=F.length,kV[F])return[K(kV[F],O,/^[a-z]/.test(F))];if(mV[F])return[K(mV[F],O)];if(fV[F])return[K(fV[F],O)];if(O0.has(F))return[K(F,{...O,up:!0})];if(bV[F])return I({...O,...bV[F]});if(yV[F]){let E=I(O),S=E.find((j)=>("t"in j));if(S)S.t+=yV[F];return E}if(F==="frac"){let E=I(O),S=I(O);return[...E,K("/",O),...S]}if(F==="sqrt")return[K("√",O),...I(O)];if(M0.has(F))return[];return[K(F,{...O,up:!0})]}return Y({})}var WV=new Map,eJ,xV=new Map;function G0(V,X,K,Y){return`${X?"italic ":""}${K===!0?700:K||400} ${V}px ${Y?'"Latin Modern Math","STIX Two Math","Cambria Math","Times New Roman",serif':'Inter,"Helvetica Neue",Helvetica,Arial,sans-serif'}`}function C0(V){if(/[iljI.,:;'|! ]/.test(V))return 0.28;if(/[mwMW]/.test(V))return 0.86;if(/[A-Z]/.test(V))return 0.68;if(/[̀-ͯ⃗]/.test(V))return 0;if(/[ -鿿-]/.test(V))return 1;if(V===" ")return 0.28;return 0.56}function W0(V,X,K){let Y=X+"\x00"+V,I=xV.get(Y);if(I!==void 0)return I;if(eJ===void 0)eJ=typeof document<"u"?document.createElement("canvas").getContext("2d"):null;if(eJ)eJ.font=X,I=eJ.measureText(V).width;else{I=0;for(let O of V)I+=C0(O)*K}return xV.set(Y,I),I}function qJ(V,X,K=400){let Y=V+"\x00"+X+"\x00"+K+"\x00"+(nJ===void 0?!!cV():!!nJ),I=WV.get(Y);if(I)return I;let O=[],D=[];for(let S of V.split(/\n|\\\\|<br\s*\/?>/)){let j=[],f=0,v=(d,h,k,MJ=!1)=>{let UJ="",s=!1,OJ="",VJ=!1;for(let t of d)if("t"in t){let IJ=t.cls??"ord",ZJ=!UJ||UJ==="bin"||UJ==="rel"||UJ==="open"||UJ==="punct";if(IJ==="bin"&&ZJ)IJ="ord";else if(IJ==="mid"&&(ZJ||VJ))IJ=VJ?"close":"open",VJ=!VJ;let NJ=0;if(MJ&&UJ&&IJ!=="punct"&&IJ!=="close"&&UJ!=="open"){if(IJ==="rel"!==(UJ==="rel"))NJ=0.2777777777777778;else if(IJ==="bin"||UJ==="bin")NJ=0.2222222222222222;else if(UJ==="punct"||IJ==="mid"||UJ==="mid")NJ=0.16666666666666666}if(s&&!t.it&&IJ!=="punct")NJ+=0.08;f+=NJ*k;let EJ=W0(t.t,G0(k,t.it,t.b||(t.serif?!1:K),t.serif),k),WJ=j[j.length-1];if(!NJ&&WJ&&WJ.dy===h&&WJ.size===k&&WJ.it===t.it&&WJ.b===t.b&&WJ.serif===t.serif)WJ.t+=t.t;else j.push({t:t.t,x:f,dy:h,size:k,it:t.it,b:t.b,serif:t.serif});f+=EJ,UJ=IJ,s=t.it,OJ=t.t.slice(-1)}else{let IJ=f,ZJ=Math.max(k*0.72,Math.min(k,8.5)),NJ=IJ,EJ=IJ;if(t.sup)f=IJ+(s?(OJ==="f"?0.2:0.08)*k:0),v(t.sup,h-k*0.38,ZJ),NJ=f,f=IJ;if(t.sub)v(t.sub,h+k*0.24,ZJ),EJ=f;f=Math.max(NJ,EJ)+0.06*k,s=!1}};S.split("$").forEach((d,h)=>{let k=h%2?_0(d,X*1.1):null;if(k)j.push({t:"",x:f,dy:0,size:X,it:!1,b:!1,serif:!0,math:k}),f+=k.w;else if(h%2)v(N0(d),0,X*1.1,!0);else if(d)v([{t:d,it:!1,b:!1,serif:!1}],0,X)}),O.push(j),D.push(f)}let F=X*1.3,E={lines:O,widths:D,w:Math.max(0,...D),h:O.length*F,lh:F,size:X};return WV.set(Y,E),E}var RV=13,EV=new Set(["tensor","stack","matrix","voxels","image","magnify","neurons","gaussian","space","sphere","pointcloud","waveform"]),qV=(V)=>JSON.parse(V.attrs.values??"[]"),PV=(V,X)=>+V.attrs.fontSize||(V.shape==="thin"||V.shape==="vector"||EV.has(V.shape)?X-1:X),_V=(V)=>{let X=(+V.tilt||0)*Math.PI/180,K=(+V.rotate||0)*Math.PI/180,[Y,I]=[Math.cos(X),-Math.sin(X)*0.35],[O,D]=[Math.cos(K),Math.sin(K)];return[O*Y-D*I,D*Y+O*I,-D,O]},GJ=(V,X)=>V?V.split(",").map(Number):X,HV={"left-of":[-1,0],"right-of":[1,0],above:[0,-1],below:[0,1]},JV=14;function A0(V,X,K){let Y=PV(V,K),I=qJ(V.label,Y,500),O,D;switch(V.shape){case"thin":O=Math.max(I.w+26,64),D=I.h+9;break;case"op":O=D=22;break;case"circle":O=D=Math.max(42,I.w+18,I.h+16);break;case"tensor":{let[F,E,S]=GJ(V.attrs.size,[36,64,20]);O=F+S*0.6,D=E+S*0.6;break}case"stack":{let[F,E]=GJ(V.attrs.size,[44,60]),S=+V.attrs.n||4,j=+V.attrs.offset||7;O=F+(S-1)*j,D=E+(S-1)*j;break}case"enc":case"dec":if(X)O=I.w+40,D=Math.max(84,I.h+44);else O=Math.max(I.w+64,120),D=Math.max(52,I.h+26);break;case"neurons":{let F=(+V.attrs.n||4)*20+4;[O,D]=X?[22,F]:[F,22];break}case"vector":{let F=qV(V),E=Math.max(36,...F.map((j)=>qJ(j.trim(),Y).w+14)),S=Math.max(20,Y+8);[O,D]=X?[E,F.length*S+4]:[F.length*E+4,S+2];break}case"image":{let[F,E]=GJ(V.attrs.size,[64,64]),S=+V.attrs.n||1,j=F+(S-1)*6,f=E+(S-1)*6,[v,d,h,k]=_V(V.attrs);O=Math.abs(v)*j+Math.abs(h)*f,D=Math.abs(d)*j+Math.abs(k)*f;break}case"magnify":[O,D]=GJ(V.attrs.size,[64,64]);break;case"matrix":{let F=+V.attrs.cellSize||16;O=+V.attrs.cols*F,D=+V.attrs.rows*F;break}case"voxels":{let F=+V.attrs.cellSize||16,E=+V.attrs.depth*F*0.5;O=+V.attrs.cols*F+E,D=+V.attrs.rows*F+E;break}case"gaussian":[O,D]=GJ(V.attrs.size,[120,84]);break;case"space":[O,D]=GJ(V.attrs.size,[110,100]);break;case"sphere":[O,D]=GJ(V.attrs.size,[84,84]);break;case"pointcloud":[O,D]=GJ(V.attrs.size,[100,84]);break;case"waveform":[O,D]=GJ(V.attrs.size,[120,44]);break;case"text":O=I.w+8,D=I.h+4;break;default:O=Math.max(I.w+30,76),D=I.h+18}V.w=+V.attrs.w||O,V.h=+V.attrs.h||D}function VV(V){let X=V.dir==="LR"||V.dir==="RL",[K,Y]=V.gap??(X?[56,36]:[30,44]),I=[...V.nodes.values()],O=V.fontSize??RV;for(let $ of I)A0($,X,O);let D=($)=>X?$.w:$.h,F=($)=>X?$.h:$.w,E=($,B)=>V.dir==="TB"?[B,$]:V.dir==="BT"?[B,-$]:V.dir==="LR"?[$,B]:[-$,B],S=($,B)=>V.dir==="TB"?[B,$]:V.dir==="BT"?[-B,$]:V.dir==="LR"?[$,B]:[-$,B],j=($)=>("at"in $.attrs)||Object.keys(HV).some((B)=>(B in $.attrs)),f=I.filter(($)=>!j($)),v=new Set(f.map(($)=>$.id)),d=new Map(f.map(($)=>[$.id,$.id])),h=($)=>{while(d.get($)!==$)$=d.get($);return $};for(let $ of V.ranks)for(let B of $.slice(1))if(v.has(B)&&v.has($[0]))d.set(h(B),h($[0]));let k=new Map;for(let $ of V.edges){if(!v.has($.from)||!v.has($.to))continue;let B=h($.from),_=h($.to);if(B===_)continue;if(!k.has(B))k.set(B,[]);k.get(B).push({to:_,e:$})}let MJ=new Set,UJ=new Map,s=[],OJ=($)=>{UJ.set($,1);for(let{to:B,e:_}of k.get($)??[]){let G=UJ.get(B);if(G===1)MJ.add(_);else if(!G)OJ(B)}UJ.set($,2),s.push($)};for(let $ of f)if(!UJ.has(h($.id)))OJ(h($.id));let VJ=new Map;for(let $ of s.reverse()){let B=VJ.get($)??0;VJ.set($,B);for(let{to:_,e:G}of k.get($)??[])if(!MJ.has(G))VJ.set(_,Math.max(VJ.get(_)??0,B+1))}let t=($)=>VJ.get(h($.id)),IJ=new Map;for(let $ of V.edges)if(v.has($.from)&&v.has($.to)&&!MJ.has($)&&!$.flags.has("skip")&&!$.flags.has("join")){if(!IJ.has($.to))IJ.set($.to,[]);IJ.get($.to).push(V.nodes.get($.from))}let ZJ=new Map,NJ=new Set,EJ=new Set,WJ=0;for(let $ of[...f].sort((B,_)=>t(B)-t(_)||B.order-_.order)){let B=t($),_=(u)=>!EJ.has(B+":"+u),G=$.attrs.lane!==void 0?+$.attrs.lane:$.attrs.align?ZJ.get($.attrs.align):void 0;if(G===void 0){let u=(IJ.get($.id)??[]).filter((J)=>t(J)<B),W=u.map((J)=>ZJ.get(J.id));if(u.length>1&&u.every((J)=>t(J)===t(u[0]))&&new Set(W).size===W.length){let J=W.reduce((U,Q)=>U+Q)/W.length;if(_(J))G=J}else for(let J of u){let U=ZJ.get(J.id);if(!NJ.has(J.id)&&_(U)){G=U,NJ.add(J.id);break}}if(G===void 0&&!u.length)for(let J of V.edges){if(J.from!==$.id||!MJ.has(J)||!ZJ.has(J.to)||NJ.has(J.to))continue;let U=ZJ.get(J.to);if(_(U)){G=U,NJ.add(J.to);break}}}if(G===void 0||!_(G)){while(!_(WJ))WJ++;G=WJ}WJ=Math.max(WJ,Math.floor(G)+1),EJ.add(B+":"+G),ZJ.set($.id,G)}let LJ=Math.max(0,...[...ZJ.values()].map(Math.ceil))+1,cJ=Math.max(0,...f.map(t))+1,SJ=Array(LJ).fill(0),vJ=Array(cJ).fill(0),kJ=Array.from({length:LJ},()=>new Set),mJ=Array.from({length:cJ},()=>new Set);for(let $ of f){let B=ZJ.get($.id),_=t($),G=$.groups.join();if(Number.isInteger(B))SJ[B]=Math.max(SJ[B],F($)),kJ[B].add(G);vJ[_]=Math.max(vJ[_],D($)),$.groups.forEach((u)=>mJ[_].add(u))}let YV=($,B)=>$.size!==B.size||[...$].some((_)=>!B.has(_)),pJ=Array(cJ).fill(K);for(let $ of V.edges){let B=V.nodes.get($.from),_=V.nodes.get($.to);if(!$.label||!v.has(B.id)||!v.has(_.id)||t(_)<=t(B))continue;let G=qJ($.label,O-1);pJ[t(_)]=Math.max(pJ[t(_)],X?G.w+24:G.h+16)}let yJ=[SJ[0]/2],fJ=[vJ[0]/2];for(let $=1;$<LJ;$++)yJ[$]=yJ[$-1]+SJ[$-1]/2+Y+(YV(kJ[$-1],kJ[$])?JV*2:0)+SJ[$]/2;for(let $=1;$<cJ;$++)fJ[$]=fJ[$-1]+vJ[$-1]/2+pJ[$]+(YV(mJ[$-1],mJ[$])?JV*1.5:0)+vJ[$]/2;let NV=($)=>{let B=Math.floor($),_=Math.ceil($);return yJ[B]+(yJ[_]-yJ[B])*($-B)},rJ=($)=>{let[B,_]=GJ($.attrs.nudge,[0,0]);$.x+=B,$.y+=_};for(let $ of f)[$.x,$.y]=E(fJ[t($)],NV(ZJ.get($.id))),rJ($);let hJ=I.filter(j),dJ=new Set(v);for(let $=0;hJ.length&&$<=I.length;$++)hJ=hJ.filter((B)=>{if(B.attrs.at)return[B.x,B.y]=GJ(B.attrs.at,[0,0]),rJ(B),dJ.add(B.id),!1;let _=Object.keys(HV).find((U)=>(U in B.attrs)),G=V.nodes.get(B.attrs[_]);if(!G)throw Error(`${B.id}: ${_} refers to unknown node "${B.attrs[_]}"`);if(!dJ.has(G.id))return!0;let[u,W]=HV[_],J=+B.attrs.dist||22;return B.x=G.x+u*(G.w/2+B.w/2+J),B.y=G.y+W*(G.h/2+B.h/2+J),rJ(B),dJ.add(B.id),!1});if(hJ.length)throw Error(`circular placement: ${hJ.map(($)=>$.id).join(", ")}`);let RJ=new Map;for(let $ of I){let[B,_]=S($.x,$.y);RJ.set($.id,{u:B,v:_,hu:D($)/2,hv:F($)/2})}let oJ=($,B,_)=>{for(let G=0;G+1<$.length;G++){let[u,W]=[$[G],$[G+1]],J=Math.min(u[0],W[0]),U=Math.max(u[0],W[0]),Q=Math.min(u[1],W[1]),Z=Math.max(u[1],W[1]);for(let[N,H]of RJ)if(N!==B&&N!==_&&J<H.u+H.hu+3&&U>H.u-H.hu-3&&Q<H.v+H.hv+3&&Z>H.v-H.hv-3)return!0}return!1},BV=[],bJ=($,B,_,G,u)=>{let W=$>0?u:G;for(let J=!0;J;){J=!1;for(let U of RJ.values())if(U.u-U.hu<_&&U.u+U.hu>B&&U.v+U.hv>G-1&&U.v-U.hv<u+1){let Q=$>0?U.v+U.hv:U.v-U.hv;if($>0?Q>W:Q<W)W=Q,J=!0,$>0?u=W:G=W}}W+=$*16;for(let J=!0;J;){J=!1;for(let U of BV)if(U.side===$&&U.a<_&&U.b>B&&Math.abs(U.v-W)<9)W+=$*9,J=!0}return BV.push({side:$,a:B,b:_,v:W}),W},KV=[],PJ=[];for(let $ of V.edges){let B=RJ.get($.from),_=RJ.get($.to),G=V.nodes.get($.from),u=V.nodes.get($.to);if($.op==="=>"&&G.shape==="neurons"&&u.shape==="neurons"){$.fc=!0;continue}if($.from!==$.to&&!$.flags.has("skip")&&_.u-_.hu>=B.u+B.hu+2){let W=Math.max(B.v-B.hv,_.v-_.hv)+6,J=Math.min(B.v+B.hv,_.v+_.hv)-6,U=W<=J?Math.min(J,Math.max(W,B.hv<=_.hv?B.v:_.v)):NaN;KV.push({e:$,S:B,T:_,ps:isNaN(U)?B.v:U,pt:isNaN(U)?_.v:U})}else PJ.push($)}let sJ=({e:$,S:B,T:_,ps:G,pt:u})=>{let W=B.u+B.hu,J=_.u-_.hu,U=(W+J)/2,Q=(N)=>[[W,G],[N,G],[N,u],[J,u]],Z=Math.abs(G-u)<0.5?[[[W,u],[J,u]]]:[Q(Math.max(U,J-K/2)),Q(Math.min(U,W+K/2))];if($.flags.has("join")&&Math.abs(G-_.v)>_.hv){let N=G>_.v?1:-1;Z.unshift([[W,G],[_.u,G],[_.u,_.v+N*_.hv]])}return Z.find((N)=>!oJ(N,$.from,$.to))},QV=KV.filter(($)=>sJ($)||(PJ.push($.e),!1)),iJ=new Map;for(let $ of QV){if(!iJ.has($.e.to))iJ.set($.e.to,[]);iJ.get($.e.to).push($)}let IV=($,B,_)=>{let G=$.map((J)=>J.pt),u=B?$.indexOf(B):0;for(let J=u+1;J<$.length;J++)G[J]=Math.max(G[J],G[J-1]+12);for(let J=u-1;J>=0;J--)G[J]=Math.min(G[J],G[J+1]-12);let W=Math.max(0,G[G.length-1]-(_.v+_.hv-5))-Math.max(0,_.v-_.hv+5-G[0]);return{pt:G.map((J)=>J-W),moved:W!==0}};for(let $ of iJ.values()){if($.length<2)continue;let B=$[0].T;$.sort((W,J)=>W.pt-J.pt||W.S.v-J.S.v);let _=$.filter((W)=>Math.abs(W.ps-W.pt)<0.5).sort((W,J)=>Math.abs(W.pt-B.v)-Math.abs(J.pt-B.v))[0],G=$,u=IV(G,_,B);if(_&&u.moved)G=$.filter((W)=>{if(W===_||Math.abs(W.S.v-B.v)<=B.hv)return!0;let J=W.S.v>B.v?1:-1,U=[[W.S.u+W.S.hu,W.S.v],[B.u,W.S.v],[B.u,B.v+J*B.hv]];if(oJ(U,W.e.from,W.e.to))return!0;return W.alt=U,!1}),u=IV(G,_,B);G.forEach((W,J)=>{W.pt=u.pt[J]});for(let W of G)if(Math.abs(W.pt-W.S.v)<W.S.hv-4)W.ps=W.pt}let zJ=($,B,_=0)=>{if($.pts=B.map(([G,u])=>E(G,u)),$.flags.has("curve")&&B.length===4)$.curve=_};for(let $ of QV){let B=$.alt??sJ($);if(B)zJ($.e,B,1);else PJ.push($.e)}let GV=($)=>V.edges.some((B)=>B!==$&&(B.from===$.from&&B.to===$.to||B.from===$.to&&B.to===$.from));for(let $=PJ.length-1;$>=0;$--){let B=PJ[$],_=RJ.get(B.from),G=RJ.get(B.to);if(B.from===B.to||B.flags.has("skip")||G.u+G.hu>_.u-_.hu-2||GV(B))continue;let u=_.u-_.hu,W=G.u+G.hu,J=G.v>_.v?1:-1,U=(u+W)/2,Q=Math.max(_.v-_.hv,G.v-G.hv)+6,Z=Math.min(_.v+_.hv,G.v+G.hv)-6,N=[];if(Q<=Z){let L=Math.min(Z,Math.max(Q,_.hv<=G.hv?_.v:G.v));N.push([[u,L],[W,L]])}else N.push([[_.u,_.v+J*_.hv],[_.u,G.v],[W,G.v]],[[u,_.v],[U,_.v],[U,G.v],[W,G.v]]);let H=N.find((L)=>!oJ(L,B.from,B.to));if(H)zJ(B,H,1),PJ.splice($,1)}for(let $ of PJ){let B=RJ.get($.from),_=RJ.get($.to),G=/^(left|top|-)/.test($.attrs.side??"")?-1:1,u=Math.min(B.v-B.hv,_.v-_.hv),W=Math.max(B.v+B.hv,_.v+_.hv);if($.from===$.to){let J=B.v+G*(B.hv+18);zJ($,[[B.u+B.hu*0.4,B.v+G*B.hv],[B.u+B.hu*0.4,J],[B.u-B.hu*0.4,J],[B.u-B.hu*0.4,B.v+G*B.hv]],1.33)}else if(!$.flags.has("skip")&&B.u-B.hu<_.u+_.hu&&_.u-_.hu<B.u+B.hu){let J=(Math.max(B.u-B.hu,_.u-_.hu)+Math.min(B.u+B.hu,_.u+_.hu))/2,U=_.v>B.v?1:-1;zJ($,[[J,B.v+U*B.hv],[J,_.v-U*_.hv]])}else if(_.u>B.u){let J=B.u+B.hu,U=Math.min(K/2,(_.u-_.hu-J)/2),Q=V.nodes.get($.from);if(V.nodes.get($.to).groups.some((H)=>!Q.groups.includes(H))){let H=Math.min(...[...RJ.values()].filter((L)=>L.u-L.hu>J+1&&L.u<_.u&&L.v+L.hv>u&&L.v-L.hv<W).map((L)=>L.u-L.hu));if(H-JV+6-J>6)U=H-JV+6-J}let N=bJ(G,J+U,_.u+_.hu,u,W);zJ($,[[J,B.v],[J+U,B.v],[J+U,N],[_.u,N],[_.u,_.v+G*_.hv]])}else{let J=bJ(G,Math.min(B.u-B.hu,_.u-_.hu),Math.max(B.u+B.hu,_.u+_.hu),u,W);zJ($,[[B.u,B.v+G*B.hv],[B.u,J],[_.u,J],[_.u,_.v+G*_.hv]],1.33)}}for(let $ of V.edges){if(!$.label||$.pts.length<2)continue;let B=qJ($.label,O-1),_=0,G=0;for(let N=0;N+1<$.pts.length;N++){let H=Math.hypot($.pts[N+1][0]-$.pts[N][0],$.pts[N+1][1]-$.pts[N][1]);if(H>_)_=H,G=N}if($.flags.has("join")&&$.pts.length===3)G=1;let[u,W]=[$.pts[G],$.pts[G+1]],J=$.pts[0],U=$.pts[$.pts.length-1],Q=(u[0]+W[0])/2,Z=(u[1]+W[1])/2;if(Math.abs(u[1]-W[1])<Math.abs(u[0]-W[0])){let N=Z>Math.max(J[1],U[1])+1;$.lx=Q,$.ly=Z+(N?1:-1)*(B.h/2+5)}else{let N=Q<Math.min(J[0],U[0])-1;$.lx=Q+(N?-1:1)*(B.w/2+7),$.ly=Z}}for(let $ of[...V.groups].reverse()){let B=1/0,_=1/0,G=-1/0,u=-1/0,W=(Q,Z,N=JV)=>{B=Math.min(B,Q-N),_=Math.min(_,Z-N),G=Math.max(G,Q+N),u=Math.max(u,Z+N)},J=new Set(I.filter((Q)=>Q.groups.includes($.id)).map((Q)=>Q.id));for(let Q of J){let Z=V.nodes.get(Q);W(Z.x-Z.w/2,Z.y-Z.h/2),W(Z.x+Z.w/2,Z.y+Z.h/2)}for(let Q of V.edges)if(J.has(Q.from)&&J.has(Q.to))for(let[Z,N]of Q.pts)W(Z,N);else if(J.has(Q.to)&&Q.pts.length===5)for(let[Z,N]of Q.pts.slice(2))W(Z,N,6);for(let Q of V.groups)if(Q.parent===$.id&&Q.w)W(Q.x,Q.y),W(Q.x+Q.w,Q.y+Q.h);if(B===1/0)continue;let U=$.label?18:0;Object.assign($,{x:B,y:_-U,w:G-B,h:u-_+U})}return V}var wJ,UV,tJ={},pV=new Map;function dV(V,X,K){let Y=V.createShader(X);if(V.shaderSource(Y,K),V.compileShader(Y),!V.getShaderParameter(Y,V.COMPILE_STATUS))throw Error(V.getShaderInfoLog(Y)??"shader");return Y}function $V(V,X){let K=V.createProgram();if(V.attachShader(K,dV(V,V.VERTEX_SHADER,`
|
|
1
|
+
var ho=Object.defineProperty;var mo=(o)=>o;function go(o,n){this[o]=mo.bind(null,n)}var bo=(o,n)=>{for(var c in n)ho(o,c,{get:n[c],enumerable:!0,configurable:!0,set:go.bind(n,c)})};var Ue={};bo(Ue,{Figure:()=>qe,Graph:()=>Fe,Module:()=>fo,Tensor:()=>Et,act:()=>or,add:()=>dr,attention:()=>_o,block:()=>To,circle:()=>Po,concat:()=>Qo,configure:()=>gr,conv:()=>tr,dec:()=>Fo,dropout:()=>rr,embed:()=>Zo,empty:()=>Vo,enc:()=>jo,ffn:()=>Ho,figure:()=>wr,gaussian:()=>nr,image:()=>fr,layer:()=>It,linear:()=>Yo,loss:()=>sr,magnify:()=>ur,matmul:()=>Ko,matrix:()=>pr,mha:()=>Do,mlp:()=>Wo,mul:()=>mr,neurons:()=>Uo,norm:()=>Jo,op:()=>Be,pointcloud:()=>cr,pool:()=>er,preset:()=>tt,softmax:()=>Xo,space:()=>ar,sphere:()=>ir,stack:()=>qo,tensor:()=>Bo,text:()=>co,thin:()=>Io,vector:()=>Go,voxels:()=>hr,waveform:()=>lr});var xe='Inter,"Helvetica Neue",Helvetica,Arial,sans-serif',At=400,Ze='"Latin Modern Math","STIX Two Math","Cambria Math","Times New Roman",serif',ae;function ze(o){ae=o,Le.clear()}var Ye=()=>{let o=globalThis.MathJax;return o?.tex2svg?(n)=>o.tex2svg(n,{display:!1}).querySelector("svg")?.outerHTML??"":null},xo=(o,n)=>{let c=ae===void 0?Ye():ae;if(!c)return null;let a;try{a=c(o)}catch{return null}let u=/viewBox="([^"]+)"/.exec(a)?.[1];if(!u)return null;let[,h,x,g]=u.trim().split(/[\s,]+/).map(Number),v=n/1000;return{inner:a.replace(/^<svg[^>]*>/,"").replace(/<\/svg>\s*$/,""),viewBox:u,w:x*v,asc:-h*v,desc:(g+h)*v}},Jt=(o)=>{let n={},c=o.split(" ");for(let a=0;a<c.length;a+=2)n[c[a]]=c[a+1];return n},Ve=Jt("alpha α beta β gamma γ delta δ epsilon ϵ varepsilon ε zeta ζ eta η theta θ vartheta ϑ iota ι kappa κ lambda λ mu μ nu ν xi ξ pi π rho ρ sigma σ tau τ upsilon υ phi ϕ varphi φ chi χ psi ψ omega ω Gamma Γ Delta Δ Theta Θ Lambda Λ Xi Ξ Pi Π Sigma Σ Phi Φ Psi Ψ Omega Ω"),Ge=Jt("cdot · times × oplus ⊕ otimes ⊗ odot ⊙ to → rightarrow → leftarrow ← gets ← Rightarrow ⇒ leftrightarrow ↔ mapsto ↦ sum ∑ prod ∏ int ∫ infty ∞ partial ∂ nabla ∇ in ∈ notin ∉ sim ∼ approx ≈ propto ∝ leq ≤ le ≤ geq ≥ ge ≥ neq ≠ ne ≠ pm ± mp ∓ ldots … dots … cdots ⋯ vdots ⋮ mid ∣ | ‖ ast ∗ star ⋆ circ ∘ bullet • forall ∀ exists ∃ emptyset ∅ cup ∪ cap ∩ subset ⊂ subseteq ⊆ top ⊤ perp ⊥ langle ⟨ rangle ⟩ ell ℓ hbar ℏ dagger † prime ′ { { } } % % _ _ # # & & $ $"),Ut={};for(let o of"+−×·±∓⊕⊗⊙∘∗⋆∪∩")Ut[o]="bin";for(let o of"=<>≤≥≠≈∼∝∈∉⊂⊆→←⇒↔↦∣")Ut[o]="rel";for(let o of",;")Ut[o]="punct";Ut["|"]="mid";for(let o of"([⟨")Ut[o]="open";for(let o of")]⟩")Ut[o]="close";var yo=new Set("log exp ln sin cos tanh max min arg argmax argmin softmax lim det tr sup inf Pr KL diag sign".split(" ")),De=Jt("hat ̂ widehat ̂ tilde ̃ widetilde ̃ bar ̄ overline ̅ dot ̇ ddot ̈ vec ⃗"),_e=Jt(", : ; quad qquad ! "),vo=Jt("B ℬ E ℰ F ℱ H ℋ I ℐ L ℒ M ℳ R ℛ"),$o=Jt("C ℂ H ℍ N ℕ P ℙ Q ℚ R ℝ Z ℤ"),He=(o)=>vo[o]||(/[A-Z]/.test(o)?String.fromCodePoint(119964+o.charCodeAt(0)-65):o),wo=(o)=>$o[o]||(/[A-Z]/.test(o)?String.fromCodePoint(120120+o.charCodeAt(0)-65):/\d/.test(o)?String.fromCodePoint(120792+ +o):o),We={mathbf:{up:!0,b:!0},bf:{up:!0,b:!0},boldsymbol:{b:!0},bm:{b:!0},mathrm:{up:!0},rm:{up:!0},operatorname:{up:!0},mathit:{},text:{up:!0,text:!0},textrm:{up:!0,text:!0},mathsf:{up:!0,sans:!0},textbf:{up:!0,b:!0,text:!0},mathcal:{up:!0,map:He},mathscr:{up:!0,map:He},mathbb:{up:!0,map:wo}},ko=new Set("left right big Big bigl bigr Bigl Bigr displaystyle".split(" "));function Mo(o){let n=0,c=(h,x,g=!1)=>{if(x.text)return{t:h,it:!1,b:!!x.b,serif:!x.sans};if(h==="-")h="−";return{t:h,it:g&&!x.up,b:!!x.b,serif:!x.sans,cls:Ut[h]}};function a(h,x){let g=[];while(n<o.length){let v=o[n];if(v===x){n++;break}if(v==="^"||v==="_"){n++;let R=v==="^"?"sup":"sub",I=g[g.length-1];if(!I||"t"in I||I[R])g.push(I={});I[R]=u(h);continue}if(v===" "&&!h.text){n++;continue}g.push(...u(h))}return g}function u(h){while(o[n]===" "&&!h.text)n++;let x=o[n++];if(x===void 0)return[];if(x==="{")return a(h,"}");if(x!=="\\")return[c(h.map?h.map(x):x,h,/[A-Za-z]/.test(x)&&!h.map)];let g=/^[A-Za-z]+/.exec(o.slice(n))?.[0]||o[n]||"";if(n+=g.length,Ve[g])return[c(Ve[g],h,/^[a-z]/.test(g))];if(Ge[g])return[c(Ge[g],h)];if(_e[g])return[c(_e[g],h)];if(yo.has(g))return[c(g,{...h,up:!0})];if(We[g])return u({...h,...We[g]});if(De[g]){let v=u(h),R=v.find((I)=>("t"in I));if(R)R.t+=De[g];return v}if(g==="frac"){let v=u(h),R=u(h);return[...v,c("/",h),...R]}if(g==="sqrt")return[c("√",h),...u(h)];if(ko.has(g))return[];return[c(g,{...h,up:!0})]}return a({})}var Le=new Map,ne,Je=new Map;function So(o,n,c,a,u='Inter,"Helvetica Neue",Helvetica,Arial,sans-serif'){return`${n?"italic ":""}${c===!0?700:c||400} ${o}px ${a?'"Latin Modern Math","STIX Two Math","Cambria Math","Times New Roman",serif':u}`}function Oo(o){if(/[iljI.,:;'|! ]/.test(o))return 0.28;if(/[mwMW]/.test(o))return 0.86;if(/[A-Z]/.test(o))return 0.68;if(/[̀-ͯ⃗]/.test(o))return 0;if(/[ -鿿-]/.test(o))return 1;if(o===" ")return 0.28;return 0.56}function Eo(o,n,c){let a=n+"\x00"+o,u=Je.get(a);if(u!==void 0)return u;if(ne===void 0)ne=typeof document<"u"?document.createElement("canvas").getContext("2d"):null;if(ne)ne.font=n,u=ne.measureText(o).width;else{u=0;for(let h of o)u+=Oo(h)*c}return Je.set(a,u),u}function bt(o,n,c=400,a='Inter,"Helvetica Neue",Helvetica,Arial,sans-serif'){let u=o+"\x00"+n+"\x00"+c+"\x00"+a+"\x00"+(ae===void 0?!!Ye():!!ae),h=Le.get(u);if(h)return h;let x=[],g=[];for(let I of o.split(/\n|\\\\|<br\s*\/?>/)){let q=[],O=0,U=(X,V,_,ft=!1)=>{let W="",lt=!1,wt="",ot=!1;for(let H of X)if("t"in H){let it=H.cls??"ord",ct=!W||W==="bin"||W==="rel"||W==="open"||W==="punct";if(it==="bin"&&ct)it="ord";else if(it==="mid"&&(ct||ot))it=ot?"close":"open",ot=!ot;let ht=0;if(ft&&W&&it!=="punct"&&it!=="close"&&W!=="open"){if(it==="rel"!==(W==="rel"))ht=0.2777777777777778;else if(it==="bin"||W==="bin")ht=0.2222222222222222;else if(W==="punct"||it==="mid"||W==="mid")ht=0.16666666666666666}if(lt&&!H.it&&it!=="punct")ht+=0.08;O+=ht*_;let kt=Eo(H.t,So(_,H.it,H.b||(H.serif?!1:c),H.serif,a),_),pt=q[q.length-1];if(!ht&&pt&&pt.dy===V&&pt.size===_&&pt.it===H.it&&pt.b===H.b&&pt.serif===H.serif)pt.t+=H.t;else q.push({t:H.t,x:O,dy:V,size:_,it:H.it,b:H.b,serif:H.serif});O+=kt,W=it,lt=H.it,wt=H.t.slice(-1)}else{let it=O,ct=Math.max(_*0.72,Math.min(_,8.5)),ht=it,kt=it;if(H.sup)O=it+(lt?(wt==="f"?0.2:0.08)*_:0),U(H.sup,V-_*0.38,ct),ht=O,O=it;if(H.sub)U(H.sub,V+_*0.24,ct),kt=O;O=Math.max(ht,kt)+0.06*_,lt=!1}};I.split("$").forEach((X,V)=>{let _=V%2?xo(X,n*1.1):null;if(_)q.push({t:"",x:O,dy:0,size:n,it:!1,b:!1,serif:!0,math:_}),O+=_.w;else if(V%2)U(Mo(X),0,n*1.1,!0);else if(X)U([{t:X,it:!1,b:!1,serif:!1}],0,n)}),x.push(q),g.push(O)}let v=n*1.3,R={lines:x,widths:g,w:Math.max(0,...g),h:x.length*v,lh:v,size:n};return Le.set(u,R),R}var Ae=13,ve=new Set(["tensor","stack","matrix","voxels","image","magnify","neurons","gaussian","space","sphere","pointcloud","waveform"]),Ce=(o)=>JSON.parse(o.attrs.values??"[]"),$e=(o,n)=>+o.attrs.fontSize||(o.shape==="thin"||o.shape==="vector"||ve.has(o.shape)?n-1:n),we=(o)=>{let n=(+o.tilt||0)*Math.PI/180,c=(+o.rotate||0)*Math.PI/180,[a,u]=[Math.cos(n),-Math.sin(n)*0.35],[h,x]=[Math.cos(c),Math.sin(c)];return[h*a-x*u,x*a+h*u,-x,h]},dt=(o,n)=>o?o.split(",").map(Number):n,Ne={"left-of":[-1,0],"right-of":[1,0],above:[0,-1],below:[0,1]},ie=14,ye=(o,n)=>o[0]<n[2]&&o[2]>n[0]&&o[1]<n[3]&&o[3]>n[1];function Ro(o,n,c,a){let u=$e(o,c),h=o.attrs.fontFamily||a,x=bt(o.label,u,500,h),g,v;switch(o.shape){case"thin":g=Math.max(x.w+26,64),v=x.h+9;break;case"op":g=v=22;break;case"circle":g=v=Math.max(42,x.w+18,x.h+16);break;case"tensor":{let[R,I,q]=dt(o.attrs.size,[36,64,20]);g=R+q*0.6,v=I+q*0.6;break}case"stack":{let[R,I]=dt(o.attrs.size,[44,60]),q=+o.attrs.n||4,O=+o.attrs.offset||7;g=R+(q-1)*O,v=I+(q-1)*O;break}case"enc":case"dec":if(n)g=x.w+40,v=Math.max(84,x.h+44);else g=Math.max(x.w+64,120),v=Math.max(52,x.h+26);break;case"neurons":{let R=(+o.attrs.n||4)*20+4;[g,v]=n?[22,R]:[R,22];break}case"vector":{let R=Ce(o),I=Math.max(36,...R.map((O)=>bt(O.trim(),u,At,h).w+14)),q=Math.max(20,u+8);[g,v]=n?[I,R.length*q+4]:[R.length*I+4,q+2];break}case"image":{let[R,I]=dt(o.attrs.size,[64,64]),q=+o.attrs.n||1,O=R+(q-1)*6,U=I+(q-1)*6,[X,V,_,ft]=we(o.attrs);g=Math.abs(X)*O+Math.abs(_)*U,v=Math.abs(V)*O+Math.abs(ft)*U;break}case"magnify":[g,v]=dt(o.attrs.size,[64,64]);break;case"matrix":{let R=+o.attrs.cellSize||16;g=+o.attrs.cols*R,v=+o.attrs.rows*R;break}case"voxels":{let R=+o.attrs.cellSize||16,I=+o.attrs.depth*R*0.5;g=+o.attrs.cols*R+I,v=+o.attrs.rows*R+I;break}case"gaussian":[g,v]=dt(o.attrs.size,[120,84]);break;case"space":[g,v]=dt(o.attrs.size,[110,100]);break;case"sphere":[g,v]=dt(o.attrs.size,[84,84]);break;case"pointcloud":[g,v]=dt(o.attrs.size,[100,84]);break;case"waveform":[g,v]=dt(o.attrs.size,[120,44]);break;case"text":g=x.w+8,v=x.h+4;break;case"empty":g=v=0;break;default:g=Math.max(x.w+30,76),v=x.h+18}o.w=+o.attrs.w||g,o.h=+o.attrs.h||v}function ce(o){let n=o.dir==="LR"||o.dir==="RL",[c,a]=o.gap??(n?[56,36]:[30,44]),u=[...o.nodes.values()],h=o.fontSize??Ae,x=o.fontFamily??xe;for(let e of u)Ro(e,n,h,x);let g=(e)=>n?e.w:e.h,v=(e)=>n?e.h:e.w,R=(e,r)=>o.dir==="TB"?[r,e]:o.dir==="BT"?[r,-e]:o.dir==="LR"?[e,r]:[-e,r],I=(e,r)=>o.dir==="TB"?[r,e]:o.dir==="BT"?[-r,e]:o.dir==="LR"?[e,r]:[-e,r],q=(e)=>("at"in e.attrs)||Object.keys(Ne).some((r)=>(r in e.attrs)),O=u.filter((e)=>!q(e)),U=new Set(O.map((e)=>e.id)),X=new Map(O.map((e)=>[e.id,e.id])),V=(e)=>{while(X.get(e)!==e)e=X.get(e);return e};for(let e of o.ranks)for(let r of e.slice(1))if(U.has(r)&&U.has(e[0]))X.set(V(r),V(e[0]));let _=new Map;for(let e of o.edges){if(!U.has(e.from)||!U.has(e.to))continue;let r=V(e.from),s=V(e.to);if(r===s)continue;if(!_.has(r))_.set(r,[]);_.get(r).push({to:s,e})}let ft=new Set,W=new Map,lt=[],wt=(e)=>{W.set(e,1);for(let{to:r,e:s}of _.get(e)??[]){let i=W.get(r);if(i===1)ft.add(s);else if(!i)wt(r)}W.set(e,2),lt.push(e)};for(let e of O)if(!W.has(V(e.id)))wt(V(e.id));let ot=new Map;for(let e of lt.reverse()){let r=ot.get(e)??0;ot.set(e,r);for(let{to:s,e:i}of _.get(e)??[])if(!ft.has(i))ot.set(s,Math.max(ot.get(s)??0,r+1))}let H=(e)=>ot.get(V(e.id)),it=new Map;for(let e of o.edges)if(U.has(e.from)&&U.has(e.to)&&!ft.has(e)&&!e.flags.has("skip")&&!e.flags.has("join")){if(!it.has(e.to))it.set(e.to,[]);it.get(e.to).push(o.nodes.get(e.from))}let ct=new Map,ht=new Set,kt=new Set,pt=0;for(let e of[...O].sort((r,s)=>H(r)-H(s)||r.order-s.order)){let r=H(e),s=(y)=>!kt.has(r+":"+y),i=e.attrs.lane!==void 0?+e.attrs.lane:e.attrs.align?ct.get(e.attrs.align):void 0;if(i===void 0){let y=(it.get(e.id)??[]).filter((f)=>H(f)<r),p=y.map((f)=>ct.get(f.id));if(y.length>1&&y.every((f)=>H(f)===H(y[0]))&&new Set(p).size===p.length){let f=p.reduce((S,E)=>S+E)/p.length;if(s(f))i=f}else for(let f of y){let S=ct.get(f.id);if(!ht.has(f.id)&&s(S)){i=S,ht.add(f.id);break}}if(i===void 0&&!y.length)for(let f of o.edges){if(f.from!==e.id||!ft.has(f)||!ct.has(f.to)||ht.has(f.to))continue;let S=ct.get(f.to);if(s(S)){i=S,ht.add(f.to);break}}}if(i===void 0||!s(i)){while(!s(pt))pt++;i=pt}pt=Math.max(pt,Math.floor(i)+1),kt.add(r+":"+i),ct.set(e.id,i)}let yt=(e)=>H(o.nodes.get(e)),Gt=o.edges.filter((e)=>U.has(e.from)&&U.has(e.to)&&!ft.has(e)&&!e.flags.has("skip")&&yt(e.from)<yt(e.to)),Pt=(e,r)=>ct.get(r<yt(e.to)?e.from:e.to),Yt=()=>{let e=0;for(let r=0;r<Gt.length;r++)for(let s=r+1;s<Gt.length;s++){let i=Gt[r],y=Gt[s],p=Math.max(yt(i.from),yt(y.from)),f=Math.min(yt(i.to),yt(y.to));if(p<f&&(Pt(i,p)-Pt(y,p))*(Pt(i,f)-Pt(y,f))<0)e++}return e},Dt=()=>{let e=0;for(let r of o.groups){let s=O.filter((p)=>p.groups.includes(r.id));if(!s.length)continue;let i=s.map((p)=>ct.get(p.id)),y=s.map(H);for(let p of O){let f=ct.get(p.id),S=H(p);if(!p.groups.includes(r.id)&&f>=Math.min(...i)&&f<=Math.max(...i)&&S>=Math.min(...y)&&S<=Math.max(...y))e++}}return e},Rt=new Set;for(let e of O){let r=ct.get(e.id);if(e.attrs.lane!==void 0)Rt.add(r);if(!Number.isInteger(r))Rt.add(Math.floor(r)).add(Math.ceil(r))}let pe=(e)=>{for(let[r,s]of ct)if(s===e||s===e+1)ct.set(r,2*e+1-s)},Lt=Yt(),Xt=Lt?Dt():0;for(let e=0,r=!0;Lt&&r&&e<pt;e++){r=!1;for(let s=0;s+1<pt&≪s++){if(Rt.has(s)||Rt.has(s+1))continue;pe(s);let i=Yt(),y=i<Lt?Dt():1/0;if(y<=Xt)Lt=i,Xt=y,r=!0;else pe(s)}}let jt=Math.max(0,...[...ct.values()].map(Math.ceil))+1,_t=Math.max(0,...O.map(H))+1,Ft=Array(jt).fill(0),Bt=Array(_t).fill(0),Ht=Array.from({length:jt},()=>new Set),Kt=Array.from({length:_t},()=>new Set);for(let e of O){let r=ct.get(e.id),s=H(e),i=e.groups.join();if(Number.isInteger(r))Ft[r]=Math.max(Ft[r],v(e)),Ht[r].add(i);Bt[s]=Math.max(Bt[s],g(e)),e.groups.forEach((y)=>Kt[s].add(y))}let Qt=(e,r)=>e.size!==r.size||[...e].some((s)=>!r.has(s)),te=Array(_t).fill(c);for(let e of o.edges){let r=o.nodes.get(e.from),s=o.nodes.get(e.to);if(!e.label||!U.has(r.id)||!U.has(s.id)||H(s)<=H(r))continue;let i=bt(e.label,h-1,At,x);te[H(s)]=Math.max(te[H(s)],n?i.w+24:i.h+16)}let $t=[Ft[0]/2],ee=[Bt[0]/2];for(let e=1;e<jt;e++)$t[e]=$t[e-1]+Ft[e-1]/2+a+(Qt(Ht[e-1],Ht[e])?ie*2:0)+Ft[e]/2;for(let e=1;e<_t;e++)ee[e]=ee[e-1]+Bt[e-1]/2+te[e]+(Qt(Kt[e-1],Kt[e])?ie*1.5:0)+Bt[e]/2;let he=(e)=>{let r=Math.floor(e),s=Math.ceil(e);return $t[r]+($t[s]-$t[r])*(e-r)},oe=(e)=>{let[r,s]=dt(e.attrs.nudge,[0,0]);e.x+=r,e.y+=s};for(let e of O)[e.x,e.y]=R(ee[H(e)],he(ct.get(e.id))),oe(e);let qt=u.filter(q),re=new Set(U);for(let e=0;qt.length&&e<=u.length;e++)qt=qt.filter((r)=>{if(r.attrs.at)return[r.x,r.y]=dt(r.attrs.at,[0,0]),oe(r),re.add(r.id),!1;let s=Object.keys(Ne).find((S)=>(S in r.attrs)),i=o.nodes.get(r.attrs[s]);if(!i)throw Error(`${r.id}: ${s} refers to unknown node "${r.attrs[s]}"`);if(!re.has(i.id))return!0;let[y,p]=Ne[s],f=+r.attrs.dist||22;return r.x=i.x+y*(i.w/2+r.w/2+f),r.y=i.y+p*(i.h/2+r.h/2+f),oe(r),re.add(r.id),!1});if(qt.length)throw Error(`circular placement: ${qt.map((e)=>e.id).join(", ")}`);let vt=new Map;for(let e of u){let[r,s]=I(e.x,e.y);vt.set(e.id,{u:r,v:s,hu:g(e)/2,hv:v(e)/2})}let se=(e,r,s)=>{for(let i=0;i+1<e.length;i++){let[y,p]=[e[i],e[i+1]],f=Math.min(y[0],p[0]),S=Math.max(y[0],p[0]),E=Math.min(y[1],p[1]),m=Math.max(y[1],p[1]);for(let[w,z]of vt)if(w!==r&&w!==s&&f<z.u+z.hu+3&&S>z.u-z.hu-3&&E<z.v+z.hv+3&&m>z.v-z.hv-3)return!0}return!1},de=[],me=(e,r,s,i,y)=>{let p=e>0?y:i;for(let f=!0;f;){f=!1;for(let S of vt.values())if(S.u-S.hu<s&&S.u+S.hu>r&&S.v+S.hv>i-1&&S.v-S.hv<y+1){let E=e>0?S.v+S.hv:S.v-S.hv;if(e>0?E>p:E<p)p=E,f=!0,e>0?y=p:i=p}}p+=e*16;for(let f=!0;f;){f=!1;for(let S of de)if(S.side===e&&S.a<s&&S.b>r&&Math.abs(S.v-p)<9)p+=e*9,f=!0}return de.push({side:e,a:r,b:s,v:p}),p},ge=[],Mt=[];for(let e of o.edges){let r=vt.get(e.from),s=vt.get(e.to),i=o.nodes.get(e.from),y=o.nodes.get(e.to);if(e.op==="=>"&&i.shape==="neurons"&&y.shape==="neurons"){e.fc=!0;continue}if(e.from!==e.to&&!e.flags.has("skip")&&s.u-s.hu>=r.u+r.hu+2){let p=Math.max(r.v-r.hv,s.v-s.hv)+6,f=Math.min(r.v+r.hv,s.v+s.hv)-6,S=p<=f?Math.min(f,Math.max(p,r.hv<=s.hv?r.v:s.v)):NaN;ge.push({e,S:r,T:s,ps:isNaN(S)?r.v:S,pt:isNaN(S)?s.v:S})}else Mt.push(e)}let be=({e,S:r,T:s,ps:i,pt:y})=>{let p=r.u+r.hu,f=s.u-s.hu,S=(p+f)/2,E=(w)=>[[p,i],[w,i],[w,y],[f,y]],m=Math.abs(i-y)<0.5?[[[p,y],[f,y]]]:[E(Math.max(S,f-c/2)),E(Math.min(S,p+c/2))];if(e.flags.has("join")&&Math.abs(i-s.v)>s.hv){let w=i>s.v?1:-1;m.unshift([[p,i],[s.u,i],[s.u,s.v+w*s.hv]])}return m.find((w)=>!se(w,e.from,e.to))},St=ge.filter((e)=>be(e)||(Mt.push(e.e),!1)),zt=new Map;for(let e of St){if(!zt.has(e.e.to))zt.set(e.e.to,[]);zt.get(e.e.to).push(e)}let Wt=(e,r,s)=>{let i=e.map((f)=>f.pt),y=r?e.indexOf(r):0;for(let f=y+1;f<e.length;f++)i[f]=Math.max(i[f],i[f-1]+12);for(let f=y-1;f>=0;f--)i[f]=Math.min(i[f],i[f+1]-12);let p=Math.max(0,i[i.length-1]-(s.v+s.hv-5))-Math.max(0,s.v-s.hv+5-i[0]);return{pt:i.map((f)=>f-p),moved:p!==0}};for(let e of zt.values()){if(e.length<2)continue;let r=e[0].T;e.sort((p,f)=>p.pt-f.pt||p.S.v-f.S.v);let s=e.filter((p)=>Math.abs(p.ps-p.pt)<0.5).sort((p,f)=>Math.abs(p.pt-r.v)-Math.abs(f.pt-r.v))[0],i=e,y=Wt(i,s,r);if(s&&y.moved)i=e.filter((p)=>{if(p===s||Math.abs(p.S.v-r.v)<=r.hv)return!0;let f=p.S.v>r.v?1:-1,S=[[p.S.u+p.S.hu,p.S.v],[r.u,p.S.v],[r.u,r.v+f*r.hv]];if(se(S,p.e.from,p.e.to))return!0;return p.alt=S,!1}),y=Wt(i,s,r);i.forEach((p,f)=>{p.pt=y.pt[f]});for(let p of i)if(Math.abs(p.pt-p.S.v)<p.S.hv-4)p.ps=p.pt}let Nt=(e,r,s=0)=>{if(e.pts=r.map(([i,y])=>R(i,y)),e.flags.has("curve")&&r.length===4)e.curve=s};for(let e of St){let r=e.alt??be(e);if(r)Nt(e.e,r,1);else Mt.push(e.e)}let t=(e)=>o.edges.some((r)=>r!==e&&(r.from===e.from&&r.to===e.to||r.from===e.to&&r.to===e.from));for(let e=Mt.length-1;e>=0;e--){let r=Mt[e],s=vt.get(r.from),i=vt.get(r.to);if(r.from===r.to||r.flags.has("skip")||i.u+i.hu>s.u-s.hu-2||t(r))continue;let y=s.u-s.hu,p=i.u+i.hu,f=i.v>s.v?1:-1,S=(y+p)/2,E=Math.max(s.v-s.hv,i.v-i.hv)+6,m=Math.min(s.v+s.hv,i.v+i.hv)-6,w=[];if(E<=m){let P=Math.min(m,Math.max(E,s.hv<=i.hv?s.v:i.v));w.push([[y,P],[p,P]])}else w.push([[s.u,s.v+f*s.hv],[s.u,i.v],[p,i.v]],[[y,s.v],[S,s.v],[S,i.v],[p,i.v]]);let z=w.find((P)=>!se(P,r.from,r.to));if(z)Nt(r,z,1),Mt.splice(e,1)}for(let e of Mt){let r=vt.get(e.from),s=vt.get(e.to),i=/^(left|top|-)/.test(e.attrs.side??"")?-1:1,y=Math.min(r.v-r.hv,s.v-s.hv),p=Math.max(r.v+r.hv,s.v+s.hv);if(e.from===e.to){let f=r.v+i*(r.hv+18);Nt(e,[[r.u+r.hu*0.4,r.v+i*r.hv],[r.u+r.hu*0.4,f],[r.u-r.hu*0.4,f],[r.u-r.hu*0.4,r.v+i*r.hv]],1.33)}else if(!e.flags.has("skip")&&r.u-r.hu<s.u+s.hu&&s.u-s.hu<r.u+r.hu){let f=(Math.max(r.u-r.hu,s.u-s.hu)+Math.min(r.u+r.hu,s.u+s.hu))/2,S=s.v>r.v?1:-1;Nt(e,[[f,r.v+S*r.hv],[f,s.v-S*s.hv]])}else if(s.u>r.u){let f=r.u+r.hu,S=Math.min(c/2,(s.u-s.hu-f)/2),E=o.nodes.get(e.from);if(o.nodes.get(e.to).groups.some((z)=>!E.groups.includes(z))){let z=Math.min(...[...vt.values()].filter((P)=>P.u-P.hu>f+1&&P.u<s.u&&P.v+P.hv>y&&P.v-P.hv<p).map((P)=>P.u-P.hu));if(z-ie+6-f>6)S=z-ie+6-f}let w=me(i,f+S,s.u+s.hu,y,p);Nt(e,[[f,r.v],[f+S,r.v],[f+S,w],[s.u,w],[s.u,s.v+i*s.hv]])}else{let f=me(i,Math.min(r.u-r.hu,s.u-s.hu),Math.max(r.u+r.hu,s.u+s.hu),y,p);Nt(e,[[r.u,r.v+i*r.hv],[r.u,f],[s.u,f],[s.u,s.v+i*s.hv]],1.33)}}for(let e of[...o.groups].reverse()){let r=1/0,s=1/0,i=-1/0,y=-1/0,p=(E,m,w=ie)=>{r=Math.min(r,E-w),s=Math.min(s,m-w),i=Math.max(i,E+w),y=Math.max(y,m+w)},f=new Set(u.filter((E)=>E.groups.includes(e.id)).map((E)=>E.id));for(let E of f){let m=o.nodes.get(E);p(m.x-m.w/2,m.y-m.h/2),p(m.x+m.w/2,m.y+m.h/2)}for(let E of o.edges)if(f.has(E.from)&&f.has(E.to))for(let[m,w]of E.pts)p(m,w);else if(f.has(E.to)&&E.pts.length===5)for(let[m,w]of E.pts.slice(2))p(m,w,6);for(let E of o.groups)if(E.parent===e.id&&E.w)p(E.x,E.y),p(E.x+E.w,E.y+E.h);if(r===1/0)continue;let S=e.label?18:0;Object.assign(e,{x:r,y:s-S,w:i-r,h:y-s+S})}let l=[];for(let e of u)if(l.push([e.x-e.w/2,e.y-e.h/2,e.x+e.w/2,e.y+e.h/2+(e.attrs.caption?20:0)]),ve.has(e.shape)&&e.label){let r=bt(e.label,$e(e,h),500,e.attrs.fontFamily||x),s=e.y-e.h/2-11-+(e.attrs.ghost??e.attrs.heads??0)*5;l.push([e.x-r.w/2,s-r.h/2,e.x+r.w/2,s+r.h/2])}for(let e of o.groups)if(e.w&&e.label){let r=bt(e.label,h-2,600,x);l.push([e.x+12,e.y+13-r.h/2,e.x+12+r.w,e.y+13+r.h/2])}let d=o.edges.flatMap((e)=>e.pts.slice(1).map((r,s)=>{let i=e.pts[s];return[e,s,[Math.min(i[0],r[0])-2,Math.min(i[1],r[1])-2,Math.max(i[0],r[0])+2,Math.max(i[1],r[1])+2]]}));for(let e of o.edges){if(!e.label||e.pts.length<2)continue;let r=bt(e.label,h-1,At,x),s=e.pts[0],i=e.pts[e.pts.length-1],y=(C)=>Math.hypot(e.pts[C+1][0]-e.pts[C][0],e.pts[C+1][1]-e.pts[C][1]),p=0;for(let C=1;C+1<e.pts.length;C++)if(y(C)>y(p))p=C;if(e.flags.has("join")&&e.pts.length===3)p=1;let f=(C,Y,st)=>{let[b,k]=[e.pts[C],e.pts[C+1]],G=b[0]+(k[0]-b[0])*Y,N=b[1]+(k[1]-b[1])*Y;if(Math.abs(b[1]-k[1])<Math.abs(b[0]-k[0])){let L=(N>Math.max(s[1],i[1])+1?1:-1)*st;return[G,N+L*(r.h/2+5),C]}let T=(G<Math.min(s[0],i[0])-1?-1:1)*st;return[G+T*(r.w/2+7),N,C]},S=[p,...e.pts.slice(1).map((C,Y)=>Y).filter((C)=>C!==p&&y(C)>1).sort((C,Y)=>y(Y)-y(C))],E=[0.5,0.25,0.75].flatMap((C)=>S.flatMap((Y)=>[f(Y,C,1),f(Y,C,-1)])),m=([C,Y])=>[C-r.w/2,Y-r.h/2,C+r.w/2,Y+r.h/2],w=([C,Y,st])=>!l.some((b)=>ye(m([C,Y]),b))&&!d.some(([b,k,G])=>!(b===e&&k===st)&&ye(m([C,Y]),G)),[z,P]=E.find(w)??E[0];e.lx=z,e.ly=P,l.push(m([z,P]))}return o}var Ct,fe,Zt={},Xe=new Map;function Ke(o,n,c){let a=o.createShader(n);if(o.shaderSource(a,c),o.compileShader(a),!o.getShaderParameter(a,o.COMPILE_STATUS))throw Error(o.getShaderInfoLog(a)??"shader");return a}function le(o,n){let c=o.createProgram();if(o.attachShader(c,Ke(o,o.VERTEX_SHADER,`
|
|
2
2
|
attribute vec2 aPos;
|
|
3
3
|
varying vec2 vUv;
|
|
4
4
|
void main() {
|
|
5
5
|
vUv = aPos * 0.5 + 0.5;
|
|
6
6
|
gl_Position = vec4(aPos, 0.0, 1.0);
|
|
7
|
-
}`)),
|
|
7
|
+
}`)),o.attachShader(c,Ke(o,o.FRAGMENT_SHADER,n)),o.bindAttribLocation(c,0,"aPos"),o.linkProgram(c),!o.getProgramParameter(c,o.LINK_STATUS))throw Error(o.getProgramInfoLog(c)??"link");return c}function Lo(){if(Ct!==void 0)return Ct;if(Ct=null,typeof document>"u")return Ct;try{fe=document.createElement("canvas"),fe.width=fe.height=1024;let o=fe.getContext("webgl",{preserveDrawingBuffer:!0,premultipliedAlpha:!1});if(!o)return Ct;o.bindBuffer(o.ARRAY_BUFFER,o.createBuffer()),o.bufferData(o.ARRAY_BUFFER,new Float32Array([-1,-1,1,-1,-1,1,1,1]),o.STATIC_DRAW),o.enableVertexAttribArray(0),o.vertexAttribPointer(0,2,o.FLOAT,!1,0,0),Zt.ice=le(o,`
|
|
8
8
|
precision highp float;
|
|
9
9
|
varying vec2 vUv;
|
|
10
10
|
uniform vec2 uRes;
|
|
@@ -147,7 +147,7 @@ void main() {
|
|
|
147
147
|
|
|
148
148
|
alpha = alpha * frozen + front * 0.8;
|
|
149
149
|
gl_FragColor = vec4(min(col, 1.0), clamp(alpha, 0.0, 0.97));
|
|
150
|
-
}`),
|
|
150
|
+
}`),Zt.heat=le(o,`
|
|
151
151
|
precision highp float;
|
|
152
152
|
varying vec2 vUv;
|
|
153
153
|
uniform vec2 uRes;
|
|
@@ -248,7 +248,7 @@ void main() {
|
|
|
248
248
|
col = mix(col, vec3(0.018, 0.014, 0.012) + rough * 0.035, frame);
|
|
249
249
|
col += exp(-pow((d - 1.4) / 0.9, 2.0)) * 0.16;
|
|
250
250
|
gl_FragColor = vec4(min(col, 1.0), 1.0);
|
|
251
|
-
}`),
|
|
251
|
+
}`),Zt.glass=le(o,`
|
|
252
252
|
precision highp float;
|
|
253
253
|
varying vec2 vUv;
|
|
254
254
|
uniform vec2 uRes;
|
|
@@ -325,7 +325,7 @@ void main() {
|
|
|
325
325
|
float a = 0.2 + frost * 0.18 + rim * 0.35 * vUv.y + edge * 0.6 + s1 * 0.3 + s2 * 0.45;
|
|
326
326
|
vec3 col = vec3(1.0) - (1.0 - vUv.y) * rim * 0.3;
|
|
327
327
|
gl_FragColor = vec4(col, clamp(a, 0.0, 0.9));
|
|
328
|
-
}`),
|
|
328
|
+
}`),Zt.metal=le(o,`
|
|
329
329
|
precision highp float;
|
|
330
330
|
varying vec2 vUv;
|
|
331
331
|
uniform vec2 uRes;
|
|
@@ -403,7 +403,7 @@ void main() {
|
|
|
403
403
|
col = mix(col, mix(vec3(0.32, 0.34, 0.37), vec3(0.96), vUv.y), bevel * 0.55);
|
|
404
404
|
col += exp(-pow((d - 1.2) / 0.8, 2.0)) * 0.25;
|
|
405
405
|
gl_FragColor = vec4(clamp(col, 0.0, 1.0), 1.0);
|
|
406
|
-
}`),
|
|
406
|
+
}`),Zt.noise=le(o,`
|
|
407
407
|
precision highp float;
|
|
408
408
|
varying vec2 vUv;
|
|
409
409
|
uniform vec2 uRes;
|
|
@@ -478,4 +478,4 @@ void main() {
|
|
|
478
478
|
vec3 tint = vec3(hash2(c + 17.0).x, hash2(c + 41.0).x, hash2(c + 73.0).x) - 0.5;
|
|
479
479
|
vec3 col = clamp(0.5 + g * 0.18 + tint * 0.25, 0.0, 1.0);
|
|
480
480
|
gl_FragColor = vec4(col, 0.55 + exp(-pow((d - 1.2) / 0.9, 2.0)) * 0.3);
|
|
481
|
-
}`),wJ=V}catch{wJ=null}return wJ}function oV(V,X,K,Y=0){X=Math.round(X),K=Math.round(K);let I=`${V}:${X}x${K}:${Y}`,O=pV.get(I);if(O)return O;let D=H0();if(!D||X<1||K<1)return null;let F=Math.min(Math.max(1,globalThis.devicePixelRatio||1),2,1024/Math.max(X,K)),E=Math.round(X*F),S=Math.round(K*F),j=tJ[V];D.viewport(0,0,E,S),D.useProgram(j);let f=(h)=>D.getUniformLocation(j,h);D.uniform2f(f("uRes"),X,K),D.uniform1f(f("uTime"),0),D.uniform1f(f("uIntensity"),0.5),D.uniform1f(f("uSeed"),Y),D.uniform1f(f("uCover"),1),D.drawArrays(D.TRIANGLE_STRIP,0,4);let v=document.createElement("canvas");v.width=E,v.height=S,v.getContext("2d").drawImage(UV,0,1024-S,E,S,0,0,E,S);let d=v.toDataURL("image/webp",0.9);return pV.set(I,d),d}var FV={light:{none:["#e2e5e9","#7f8794"],bg:"none",text:"#1d2430",sub:"#5b6474",edge:"#39414f",halo:"#fff",group:["#f5f6f9","#d5dae2"],shadow:"rgba(30,40,60,.10)",dark:!1,ice:["#f7fdff","#d2effb","#aedcf2","#6fb9de"],fire:["#fff6e8","#ffcf9f","#ee7d36"],c:{white:["#ffffff","#8b95a5"],gray:["#eceff3","#9aa3b2"],pink:["#fde1e4","#e58c98"],orange:["#fde4c8","#e69a50"],yellow:["#f4f5cc","#c0c064"],green:["#d8f1de","#68b880"],blue:["#d6eafc","#5f9fd8"],purple:["#e4defa","#9584db"],red:["#ffd7d2","#dd6559"],teal:["#d1f0ec","#48a99f"]}},dark:{none:["#2a2f37","#8a93a2"],bg:"#0e1117",text:"#e8edf5",sub:"#9aa4b5",edge:"#c3cad6",halo:"#0e1117",group:["#161a22","#2b323f"],shadow:"rgba(0,0,0,.45)",dark:!0,ice:["#29506a","#163a50","#0f2c3e","#7fd4ff"],fire:["#4a2a12","#33180a","#ff9447"],c:{white:["#1b2029","#7d8799"],gray:["#1f242d","#8792a4"],pink:["#3a1e29","#ff8ea2"],orange:["#3a2916","#ffae5c"],yellow:["#2f3018","#e3da66"],green:["#142e22","#5ad08a"],blue:["#13273c","#5ab1ff"],purple:["#241e3f","#a892ff"],red:["#3c1b19","#ff786a"],teal:["#11312f","#4cd3c3"]}}},HJ=(V)=>+V.toFixed(1),a=(V,...X)=>V.reduce((K,Y,I)=>K+(typeof X[I-1]==="number"?HJ(X[I-1]):X[I-1])+Y),gJ=(V)=>V.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,"""),DV=(V)=>/^#([\da-f]{3}|[\da-f]{6})$/i.test(V),iV=(V)=>{let X=V.replace("#",""),K=X.length===3?[...X].map((Y)=>Y+Y).join(""):X;return[0,2,4].map((Y)=>parseInt(K.slice(Y,Y+2),16))},DJ=(V,X,K)=>{if(!DV(V)||!DV(X))return V;return K=Math.min(1,Math.max(0,K)),"#"+iV(V).map((Y,I)=>Math.round(Y+(iV(X)[I]-Y)*K).toString(16).padStart(2,"0")).join("")},uJ=(V,X,K,Y,I)=>{return I=Math.min(I,K/2,Y/2),a`M${V+I},${X}h${K-2*I}a${I},${I} 0 0 1 ${I},${I}v${Y-2*I}a${I},${I} 0 0 1 ${-I},${I}h${2*I-K}a${I},${I} 0 0 1 ${-I},${-I}v${2*I-Y}a${I},${I} 0 0 1 ${I},${-I}z`},OV=(V,X,K)=>a`M${V-K},${X}a${K},${K} 0 1 0 ${2*K},0a${K},${K} 0 1 0 ${-2*K},0z`,lJ=(...V)=>"M"+V.map(HJ).join(",").replace(/([^,]*,[^,]*),/g,"$1L")+"z",eV=-0.65,nV=0.45,aV=Math.cos(eV),tV=Math.sin(eV),rV=Math.cos(nV),sV=Math.sin(nV),CJ=(V,X,K)=>{let Y=V*aV-X*tV,I=V*tV+X*aV;return[Y,-K*rV-I*sV,I*rV-K*sV]},jV=(V,X,K,Y,I,O)=>{let D=V.map((k)=>k[0]),F=V.map((k)=>k[1]),E=Math.min(...D),S=Math.max(...D),j=Math.min(...F),f=Math.max(...F),v=Math.min((Y-2*O)/(S-E||1),(I-2*O)/(f-j||1)),d=X+Y/2-(E+S)/2*v,h=K+I/2-(j+f)/2*v;return(k)=>[d+k[0]*v,h+k[1]*v]},R0=(V)=>[(0.5+V*0.7548776662)%1,(0.5+V*0.569840291)%1],E0=(V,X)=>Array.from({length:X},(K,Y)=>{let[I,O]=R0(Y),D=I*2*Math.PI;if(V==="sphere"){let h=2*O-1,k=Math.sqrt(1-h*h);return[k*Math.cos(D),k*Math.sin(D),h]}if(V==="cube"){let h=Y%6,k=2*I-1,MJ=2*O-1,UJ=h<3?1:-1;return h%3===0?[UJ,k,MJ]:h%3===1?[k,UJ,MJ]:[k,MJ,UJ]}let F=O*2*Math.PI,E=0.68,S=0.3,j=0.8,f=(E+S*Math.cos(F))*Math.cos(D),v=(E+S*Math.cos(F))*Math.sin(D),d=S*Math.sin(F);return[f,v*Math.cos(j)-d*Math.sin(j),v*Math.sin(j)+d*Math.cos(j)]}),q0=(V)=>Array.from({length:V},(X,K)=>{let Y=K/V,O=0.06+0.9*[[0.14,0.9],[0.3,0.6],[0.47,1],[0.66,0.75],[0.84,0.5]].reduce((D,[F,E])=>D+E*Math.exp(-(((Y-F)/0.07)**2)),0)*(0.55+0.45*Math.abs(Math.sin(K*1.7)*Math.cos(K*0.37)));return[-O*(0.85+0.15*Math.sin(K*2.3)),O]}),ZV=["blue","orange","green","purple","red","teal","pink","yellow"],LV=(V)=>ZV[((V|0)%ZV.length+ZV.length)%ZV.length],P0=0;function MV(V,X,K=!0){let Y=FV[X??V.theme]??FV.light,I=`mm${P0++}-`,O=V.dir==="LR"||V.dir==="RL",D=V.fontSize??RV,F=new Map,E=new Map,S=(J)=>{if(!J)return"none";if(/^#?\w+$/.test(J))return J.replace("#","");if(!E.has(J))E.set(J,`css${E.size}`);return E.get(J)},j=1/0,f=1/0,v=-1/0,d=-1/0,h=(J,U,Q=0,Z=0)=>{j=Math.min(j,J),f=Math.min(f,U),v=Math.max(v,J+Q),d=Math.max(d,U+Z)},k=(J)=>{if(!J)return Y.none;if(Y.c[J])return Y.c[J];if(DV(J))return[Y.dark?DJ(J,"#0e1117",0.75):DJ(J,"#ffffff",0.78),J];return[gJ(J),Y.edge]},MJ=(J)=>{let U="c-"+S(J);if(!F.has(U)){let[Q,Z]=k(J),N=Y.dark?DJ(Q,Z,0.16):DJ(Q,"#ffffff",0.6);F.set(U,a`<linearGradient id="${I}${U}" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stop-color="${N}"/><stop offset="1" stop-color="${Q}"/></linearGradient>`)}return`url(#${I}${U})`},UJ=()=>{if(F.has("ice"))return;let[J,U,Q]=Y.ice;F.set("ice",`<linearGradient id="${I}ice" x1="0" y1="0" x2="1" y2="1"><stop offset="0" stop-color="${J}"/><stop offset=".55" stop-color="${U}"/><stop offset="1" stop-color="${Q}"/></linearGradient><pattern id="${I}frost" width="56" height="56" patternUnits="userSpaceOnUse"><path d="M0 14 16 20 25 6M16 20 22 37 41 33 56 45M41 33 47 12 56 9M22 37 9 56M47 12 33 0" fill="none" stroke="#fff" stroke-opacity="${Y.dark?0.18:0.75}" stroke-width="1"/><path d="M5 30 12 33M30 50 36 46M50 25 54 30" stroke="${Y.ice[3]}" stroke-opacity=".35" stroke-width=".8"/></pattern>`)},s=()=>{if(F.has("fire"))return;let[J,U,Q]=Y.fire;F.set("fire",`<linearGradient id="${I}fire" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stop-color="${J}"/><stop offset="1" stop-color="${U}"/></linearGradient><linearGradient id="${I}flame" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stop-color="#ffd24a"/><stop offset=".6" stop-color="${Q}"/><stop offset="1" stop-color="#e0452b"/></linearGradient>`)},OJ=(J)=>{let U=J.attrs.texture??(J.flags.has("frozen")?"ice":J.flags.has("train")?"magma":"");if(U==="ice")return UJ(),{fill:`url(#${I}ice)`,stroke:Y.ice[3],fx:U};if(U==="magma")return s(),{fill:`url(#${I}fire)`,stroke:Y.fire[2],fx:U};if(U==="metal")return{fill:MJ(J.color),stroke:Y.dark?"#9aa3ae":"#6d747d",fx:U};return{fill:MJ(J.color),stroke:k(J.color)[1],fx:U}},VJ=(J,U,Q,Z,N,H,L="middle")=>{let o=qJ(J,Z,N),x="",l="";return o.lines.forEach((FJ,e)=>{let C=L==="middle"?U-o.widths[e]/2:L==="start"?U:U-o.widths[e],XJ=Q-o.h/2+(e+0.5)*o.lh+Z*0.36;for(let p of FJ){if(p.math){let $J=p.math;l+=a`<svg x="${C+p.x}" y="${XJ-$J.asc}" width="${$J.w}" height="${$J.asc+$J.desc}" viewBox="${$J.viewBox}" overflow="visible" color="${H}">${$J.inner}</svg>`;continue}if(x+=a`<tspan x="${C+p.x}" y="${XJ+p.dy}"`,p.size!==Z)x+=a` font-size="${p.size}"`;if(p.it)x+=' font-style="italic"';if(p.serif)x+=` font-family='${lV}' font-weight="${p.b?700:400}"`;else if(p.b)x+=' font-weight="700"';x+=`>${gJ(p.t)}</tspan>`}}),h(U-(L==="middle"?o.w/2:L==="end"?o.w:0),Q-o.h/2,o.w,o.h),(x?a`<text font-size="${Z}" font-weight="${N}" fill="${H}" style="white-space:pre">${x}</text>`:"")+l},t=(J,U)=>J.flags.has("frozen")?"ice":J.flags.has("train")?"magma":U==="ice"||U==="magma"?U:"",IJ=(J,U,Q)=>{if(J==="ice")return a`<g transform="translate(${U},${Q})"><circle r="7.5" fill="${Y.dark?"#0f2c3e":"#fff"}" stroke="${Y.ice[3]}"/><path d="M0-4.6V4.6M-4 -2.3 4 2.3M-4 2.3 4-2.3M-1.4-3.6 0-2.4 1.4-3.6M-1.4 3.6 0 2.4 1.4 3.6" fill="none" stroke="${Y.ice[3]}" stroke-width="1.1" stroke-linecap="round"/></g>`;if(J!=="magma")return"";return s(),a`<g transform="translate(${U},${Q})"><circle r="7.5" fill="${Y.dark?"#33180a":"#fff"}" stroke="${Y.fire[2]}"/><path d="M0.4-5C3 -2.4 3.9-.2 3.1 2.2 2.5 4.1-2.3 4.3-3 2.1-3.6.2-2.2-1.2-1.6-2.6-.9-1.4-.3-.9.2-.6.9-2 .9-3.5.4-5z" fill="url(#${I}flame)"/></g>`},ZJ=0,NJ={ice:"ice",magma:"heat",glass:"glass",metal:"metal",noise:"noise"},EJ=(J,U,Q,Z,N,H,L)=>{if(WJ.has(J))return`<path d="${U}" fill="${LJ(J,L)}"/>`;let o=K&&NJ[J]?oV(NJ[J],N,H):null;if(!o){if(J==="noise")return`<path d="${U}" fill="${LJ("grain",L)}"/>`;if(J==="glass")return`<path d="${U}" fill="#fff" fill-opacity="${Y.dark?0.12:0.4}"/>`;if(J==="metal")return`<path d="${U}" fill="${SJ()}"/>`;return""}let x=`m-${J}-${Math.round(N)}x${Math.round(H)}`;if(!F.has(x))F.set(x,a`<image id="${I}${x}" width="${Math.round(N)}" height="${Math.round(H)}" preserveAspectRatio="none" href="${o}"/>`);let l=`${I}k${ZJ++}`;return`<clipPath id="${l}"><path d="${U}"/></clipPath><g clip-path="url(#${l})">`+a`<use href="#${I}${x}" x="${Q}" y="${Z}"/></g>`},WJ=new Set(["quantized","sparse","hatch"]),LJ=(J,U)=>{let Q=`p-${J}-${S(U)}`;if(!F.has(Q)){let[Z,N]=k(U),H=24,L="",o="";if(J==="quantized")H=32,[0,2,1,3,2,0,3,1,1,3,0,2,3,1,2,0].forEach((x,l)=>{L+=`<rect x="${l%4*8}" y="${(l>>2)*8}" width="8" height="8" fill="${DJ(Z,N,x/3*0.7)}"/>`});else if(J==="sparse")[1,0,1,1,0,1,0,1,1,1,0,0,1,0,1,1].forEach((x,l)=>{if(x)L+=`<circle cx="${l%4*6+3}" cy="${(l>>2)*6+3}" r="1.5" fill="${N}" fill-opacity=".75"/>`});else if(J==="hatch")H=7,o=' patternTransform="rotate(45)"',L=`<path d="M0 0V7" stroke="${N}" stroke-opacity=".55" stroke-width="1.4"/>`;else{H=24;for(let x=0;x<64;x++)L+=`<rect x="${x%8*3}" y="${(x>>3)*3}" width="3" height="3" fill="${["#000","#fff"][x*37%7>3?1:0]}" fill-opacity="${0.08+x*53%11/40}"/>`}F.set(Q,`<pattern id="${I}${Q}" width="${H}" height="${H}" patternUnits="userSpaceOnUse"${o}>${L}</pattern>`)}return`url(#${I}${Q})`},cJ=(J,U,Q)=>{if(WJ.has(J))return`<path d="${U}" fill="${LJ(J,Q)}"/>`;if(J==="noise")return`<path d="${U}" fill="${LJ("grain",Q)}"/>`;if(J==="ice")return UJ(),`<path d="${U}" fill="url(#${I}ice)"/><path d="${U}" fill="url(#${I}frost)"/>`;if(J==="magma")return s(),`<path d="${U}" fill="url(#${I}fire)"/>`;if(J==="metal")return`<path d="${U}" fill="${SJ()}"/>`;if(J==="glass")return`<path d="${U}" fill="#fff" fill-opacity="${Y.dark?0.15:0.45}"/>`;return""},SJ=()=>{if(!F.has("metal"))F.set("metal",`<linearGradient id="${I}metal" x1="0" y1="0" x2="1" y2="1"><stop offset="0" stop-color="#e4e7eb"/><stop offset=".45" stop-color="#a7adb5"/><stop offset=".55" stop-color="#cfd3d8"/><stop offset="1" stop-color="#8d949d"/></linearGradient>`);return`url(#${I}metal)`},vJ=(J,U,Q,Z)=>{let N=J+Z,H=J+Q-Z;if(H-N<8)return"";let L=a`M${N},${U+1}H${H}V${U+3.5}`,o=Math.max(2,Math.round((H-N)/9)),x=(H-N)/o;for(let l=0;l<o;l++)L+=a`q${-x/2},${[4,7,3.5,6,5][l%5]} ${-x},0`;return`<path d="${L}z" fill="#fff" fill-opacity="${Y.dark?0.55:0.92}"/>`},kJ=(J)=>{let[U,Q]=GJ(J.attrs.size,[64,64]),Z=+J.attrs.n||1;return{W:U,H:Q,k:Z,x0:J.x-(U+(Z-1)*6)/2,y0:J.y-(Q+(Z-1)*6)/2,fit:J.attrs.fit==="contain"?"xMidYMid meet":"xMidYMid slice",srcs:J.attrs.srcs?JSON.parse(J.attrs.srcs):[J.attrs.src??""]}},mJ=(J)=>{let U=V.edges.find((Q)=>Q.to===J.id&&V.nodes.get(Q.from)?.shape==="image");return U&&V.nodes.get(U.from)},YV=(J)=>{let U=V.nodes.get(J.to);return U.shape==="magnify"&&mJ(U)?.id===J.from},pJ=(J,U)=>{if(J.attrs.region)return GJ(J.attrs.region,[]);let Q=+U.attrs.grid||4,[Z,N]=GJ(J.attrs.patch,[-1,-1]);return Z>=0?[Z/Q,N/Q,1/Q,1/Q]:[0.375,0.375,0.25,0.25]},yJ=(J)=>{let U=J.x-J.w/2,Q=J.y-J.h/2,{fill:Z,stroke:N,fx:H}=OJ(J),L=J.flags.has("dashed")?' stroke-dasharray="4 3"':"",o=J.flags.has("bold")?2:1.2,x=`fill="${Z}" stroke="${N}" stroke-width="${o}"${L}`,l=+(J.attrs.ghost??J.attrs.heads??0),FJ=!1,e=(M,A=[U,Q,J.w,J.h],b=0)=>{let w="";for(let q=Math.min(l,6)-1;q>0;q--)w+=a`<path d="${M}" transform="translate(${q*5},${-q*5})" ${x} opacity="${0.35+0.5/(q+1)}"/>`;if(!Y.dark||H)w+=a`<path d="${M}" transform="translate(0,1.6)" fill="${Y.shadow}"/>`;w+=`<path d="${M}" ${x}/>`;let z=EJ(H,M,A[0],A[1],A[2],A[3],J.color);if(z)FJ=!0,w+=z+`<path d="${M}" fill="none" stroke="${N}" stroke-width="${o}" stroke-opacity=".7"${L}/>`;else if(H==="ice")w+=`<path d="${M}" fill="url(#${I}frost)"/>`+(b?vJ(A[0],A[1],A[2],b):"");return h(U,Q-l*5,J.w+l*5,J.h+l*5),w},C="",XJ=PV(J,D),[p,$J]=H==="metal"?["#1d2430","#e8ebee"]:[Y.text,Y.halo],BJ=()=>FJ&&H==="magma"?`<g paint-order="stroke" stroke="#1a0d07" stroke-width="3" stroke-linejoin="round">${VJ(J.label,J.x,J.y,XJ,600,"#fff4e8")}</g>`:FJ&&H!=="ice"&&H!=="glass"?`<g paint-order="stroke" stroke="${$J}" stroke-width="3" stroke-linejoin="round" stroke-opacity=".85">${VJ(J.label,J.x,J.y,XJ,600,p)}</g>`:VJ(J.label,J.x,J.y,XJ,500,Y.text);switch(J.shape){case"text":C=BJ();break;case"op":{C=e(OV(J.x,J.y,11),[J.x-11,J.y-11,22,22]);let M=J.label==="+"?a`M${J.x-6.5},${J.y}h13M${J.x},${J.y-6.5}v13`:/^[*x×]$/.test(J.label)?a`M${J.x-4.6},${J.y-4.6}l9.2,9.2M${J.x+4.6},${J.y-4.6}l-9.2,9.2`:J.label==="~"?a`M${J.x-7},${J.y}q3.5,-8 7,0t7,0`:"";C+=M?`<path d="${M}" fill="none" stroke="${N}" stroke-width="1.5" stroke-linecap="round"/>`:VJ(J.label,J.x,J.y,XJ,500,Y.text);break}case"circle":C=e(OV(J.x,J.y,J.w/2),[U,Q,J.w,J.h])+BJ();break;case"tensor":{let[M,A,b]=GJ(J.attrs.size,[36,64,20]),w=b*0.6,z=U,q=Q+w,[r,y]=k(J.color),R=H==="ice"?Y.ice[0]:Y.dark?DJ(r,y,0.3):DJ(r,"#fff",0.5),P=H==="ice"?Y.ice[2]:Y.dark?DJ(r,"#000",0.25):DJ(r,y,0.35);C=e(uJ(z,q,M,A,1.5),[z,q,M,A])+`<path d="${lJ(z,q,z+w,q-w,z+M+w,q-w,z+M,q)}" fill="${R}" stroke="${N}" stroke-width="1" stroke-linejoin="round"/><path d="${lJ(z+M,q,z+M+w,q-w,z+M+w,q+A-w,z+M,q+A)}" fill="${P}" stroke="${N}" stroke-width="1" stroke-linejoin="round"/>`;break}case"stack":{let[M,A]=GJ(J.attrs.size,[44,60]),b=+J.attrs.n||4,w=+J.attrs.offset||7;for(let z=0;z<b;z++){let q=uJ(U+z*w,Q+z*w,M,A,2);C+=z===b-1?e(q,[U+z*w,Q+z*w,M,A]):`<path d="${q}" ${x}/>`}h(U,Q,J.w,J.h);break}case"enc":case"dec":{let M=J.shape==="enc",b=O?M!==NV()?lJ(U,Q,U+J.w,Q+J.h*0.22,U+J.w,Q+J.h*0.78,U,Q+J.h):lJ(U,Q+J.h*0.22,U+J.w,Q,U+J.w,Q+J.h,U,Q+J.h*0.78):M!==rJ()?lJ(U,Q,U+J.w,Q,U+J.w*0.78,Q+J.h,U+J.w*0.22,Q+J.h):lJ(U+J.w*0.22,Q,U+J.w*0.78,Q,U+J.w,Q+J.h,U,Q+J.h);C=e(b)+BJ();break}case"neurons":{let M=+J.attrs.n||4;for(let A=0;A<M;A++){let b=-((M-1)/2)*20+A*20;{let w=O?J.x:J.x+b,z=O?J.y+b:J.y;C+=e(OV(w,z,7),[w-7,z-7,14,14])}}break}case"matrix":case"voxels":{let M=+J.attrs.cellSize||16,A=[];if(J.shape==="matrix")JSON.parse(J.attrs.grid).forEach((r,y)=>r.forEach((R,P)=>A.push([[[U+P*M,Q+y*M],[U+(P+1)*M,Q+y*M],[U+(P+1)*M,Q+(y+1)*M],[U+P*M,Q+(y+1)*M]],R,0])));else{let r=JSON.parse(J.attrs.faces),y=+J.attrs.cols,R=M*0.5,P=U,c=Q+ +J.attrs.depth*R,n=(m,g)=>[P+m*M+g*R,c-g*R],T=(m,g)=>[P+y*M+g*R,c+m*M-g*R];r.top.forEach((m,g)=>m.forEach((QJ,KJ)=>A.push([[n(KJ,g),n(KJ+1,g),n(KJ+1,g+1),n(KJ,g+1)],QJ,0.3]))),r.side.forEach((m,g)=>m.forEach((QJ,KJ)=>A.push([[T(g,KJ),T(g,KJ+1),T(g+1,KJ+1),T(g+1,KJ)],QJ,-0.25]))),r.front.forEach((m,g)=>m.forEach((QJ,KJ)=>A.push([[[P+KJ*M,c+g*M],[P+(KJ+1)*M,c+g*M],[P+(KJ+1)*M,c+(g+1)*M],[P+KJ*M,c+(g+1)*M]],QJ,0])))}let[b,w]=k(J.color),z=Y.dark?"#0e1117":"#ffffff";for(let[r,y,R]of A){let P=y.c?Y.c[y.c]?DJ(Y.c[y.c][0],Y.c[y.c][1],0.55):gJ(y.c):DJ(Y.dark?w:DJ(b,"#ffffff",0.4),Y.dark?b:w,Y.dark?1-(y.v??0.5):y.v??0.5);P=R>0?DJ(P,"#ffffff",R):R<0?DJ(P,"#0b1a2e",-R):P;let c="M"+r.map((m)=>m.map(HJ).join(",")).join("L")+"z";if(C+=`<path d="${c}" fill="${P}" stroke="${z}" stroke-width="1.5" stroke-linejoin="round"/>`,y.t)C+=cJ(y.t,c,y.c??J.color);let[n,T]=r.reduce((m,g)=>[m[0]+g[0]/4,m[1]+g[1]/4],[0,0]);if(y.l)C+=VJ(y.l,n,T,Math.min(D-2,M*0.6),500,Y.text);if(y.x)C+=fJ(n-M*0.28,T-M*0.28,n+M*0.28,T+M*0.28,1.4)}let q=JSON.parse(J.attrs.axes??"{}");if(J.attrs.axes){let r=+J.attrs.rows,y=+J.attrs.cols,R=J.shape==="voxels"?+J.attrs.depth*M*0.5:0,P=U,c=Q+R,n=y*M,T=r*M,m=D-1,g=V.edges.some((AJ)=>AJ.from===J.id||AJ.to===J.id),QJ=c+T/2,KJ=P+n/2;if(g&&O&&Math.abs(QJ-J.y)<m*0.85)QJ=J.y+m*0.85;let i=q.cols?qJ(q.cols,m,500).w:0;if(g&&!O&&Math.abs(KJ-J.x)<i/2+6)KJ=J.x+i/2+6;let YJ="";if(q.cols)YJ+=VJ(q.cols,KJ,c+T+m*0.9,m,500,Y.sub);if(q.rows)YJ+=VJ(q.rows,P-5,QJ,m,500,Y.sub,"end");if(q.depth&&R)YJ+=VJ(q.depth,P+n+R/2+5,c+T-R/2+m*0.5,m,500,Y.sub,"start");C+=`<g paint-order="stroke" stroke="${Y.halo}" stroke-width="3" stroke-linejoin="round">${YJ}</g>`}h(U,Q,J.w,J.h);break}case"vector":{let M=qV(J);C=e(uJ(U,Q,J.w,J.h,3));let A=(O?J.h-4:J.w-4)/M.length;M.forEach((b,w)=>{let z=-((M.length-1)/2)*A+w*A;if(w)C+=O?a`<path d="M${U+3},${J.y+z-A/2}h${J.w-6}" stroke="${N}" stroke-opacity=".5"/>`:a`<path d="M${J.x+z-A/2},${Q+3}v${J.h-6}" stroke="${N}" stroke-opacity=".5"/>`;C+=VJ(b.trim(),O?J.x:J.x+z,O?J.y+z:J.y,XJ,500,Y.text)});break}case"image":{let{W:M,H:A,k:b,x0:w,y0:z,fit:q,srcs:r}=kJ(J),y=+J.attrs.grid||0;for(let T=0;T<b;T++){let m=w+T*6,g=z+T*6,QJ=uJ(m,g,M,A,3),KJ=r.length>1?r[b-1-T]:T===b-1?r[0]:void 0;if(!KJ){C+=`<path d="${QJ}" fill="${Y.dark?"#1b2029":"#fff"}" stroke="${N}"/>`;continue}let i=`${I}k${ZJ++}`;if(C+=`<clipPath id="${i}"><path d="${QJ}"/></clipPath>`+a`<image href="${gJ(KJ)}" x="${m}" y="${g}" width="${M}" height="${A}" preserveAspectRatio="${q}" clip-path="url(#${i})"/>`,y>1&&T===b-1){let YJ="";for(let AJ=1;AJ<y;AJ++)YJ+=a`M${m+M*AJ/y},${g}v${A}M${m},${g+A*AJ/y}h${M}`;C+=`<path d="${YJ}" stroke="#fff" stroke-opacity=".9"/>`}C+=`<path d="${QJ}" fill="none" stroke="${N}"/>`}let[R,P,c,n]=_V(J.attrs);if(R!==1||P!==0||c!==0||n!==1)C=a`<g transform="matrix(${[R,P,c,n].map((T)=>T.toFixed(4)).join(" ")} ${J.x-R*J.x-c*J.y} ${J.y-P*J.x-n*J.y})">`+C+"</g>";h(U,Q,J.w,J.h);break}case"magnify":{let M=mJ(J),A=Math.min(J.w,J.h)/2,b=J.attrs.lens!=="square",w=b?OV(J.x,J.y,A):uJ(U,Q,J.w,J.h,4),z=`${I}k${ZJ++}`;if(C+=`<clipPath id="${z}"><path d="${w}"/></clipPath>`,J.attrs.src)C+=a`<image href="${gJ(J.attrs.src)}" x="${U}" y="${Q}" width="${J.w}" height="${J.h}" preserveAspectRatio="${J.attrs.fit==="contain"?"xMidYMid meet":"xMidYMid slice"}" clip-path="url(#${z})"/>`;else if(M){let q=kJ(M),[r,y,R,P]=pJ(J,M);C+=a`<g clip-path="url(#${z})"><svg x="${U}" y="${Q}" width="${J.w}" height="${J.h}" viewBox="${r*q.W} ${y*q.H} ${R*q.W} ${P*q.H}" preserveAspectRatio="xMidYMid slice">`+a`<image href="${gJ(q.srcs[0])}" width="${q.W}" height="${q.H}" preserveAspectRatio="${q.fit}"/></svg></g>`}else C+=`<path d="${w}" fill="${Z}"/>`;if(C+=`<path d="${w}" fill="none" stroke="${N}" stroke-width="2.5"/>`,b){let q=A*Math.SQRT1_2;C+=a`<path d="M${J.x+q+1},${J.y+q+1}l${A*0.5},${A*0.5}" stroke="${N}" stroke-width="5" stroke-linecap="round"/>`,h(J.x+q,J.y+q,A*0.6,A*0.6)}h(U,Q,J.w,J.h);break}case"gaussian":{let M=JSON.parse(J.attrs.peaks??"[[0,0,1,0.45]]"),A=Math.min(40,+J.attrs.mesh||16),[b,w]=k(J.color),z=(T,m)=>M.reduce((g,[QJ,KJ,i=1,YJ=0.45])=>g+i*Math.exp(-((T-QJ)**2+(m-KJ)**2)/(2*YJ*YJ)),0),q=Array.from({length:A+1},(T,m)=>Array.from({length:A+1},(g,QJ)=>z(-1+2*m/A,-1+2*QJ/A))),r=Math.max(...q.flat())||1,y=q.map((T,m)=>T.map((g,QJ)=>CJ(-1+2*m/A,-1+2*QJ/A,g/r))),R=[[-1,-1],[1,-1],[1,1],[-1,1]].map(([T,m])=>CJ(T,m,0)),P=jV([...y.flat(),...R],U,Q,J.w,J.h,2),c=(T)=>P(T).map(HJ).join(",");C+=`<path d="M${R.map(c).join("L")}z" fill="${b}" fill-opacity=".5" stroke="${w}" stroke-opacity=".4"/>`;let n=[];for(let T=0;T<A;T++)for(let m=0;m<A;m++){let g=[y[T][m],y[T+1][m],y[T+1][m+1],y[T][m+1]];n.push([g.reduce((QJ,KJ)=>QJ+KJ[2],0),`M${g.map(c).join("L")}z`,(q[T][m]+q[T+1][m+1])/2/r])}n.sort((T,m)=>m[0]-T[0]);for(let[,T,m]of n){let g=DJ(b,w,0.12+0.88*m);C+=`<path d="${T}" fill="${g}" stroke="${DJ(g,Y.dark?"#ffffff":w,0.35)}" stroke-width=".5"/>`}h(U,Q,J.w,J.h);break}case"space":{let[M,A]=k(J.color),b=JSON.parse(J.attrs.points??"[]"),w=Object.values({x:"x",y:"y",z:"z",...JSON.parse(J.attrs.axes??"{}")}),z=CJ(1,0,0)[2]>CJ(-1,0,0)[2]?1:-1,q=CJ(0,1,0)[2]>CJ(0,-1,0)[2]?1:-1,r=[-1,1].flatMap((i)=>[-1,1].flatMap((YJ)=>[-1,1].map((AJ)=>CJ(i,YJ,AJ)))),y=jV(r,U,Q,J.w,J.h,10),R=(i,YJ,AJ)=>y(CJ(i,YJ,AJ)).map(HJ).join(","),P=(i)=>`<path d="M${i.map((YJ)=>R(YJ[0],YJ[1],YJ[2])).join("L")}z" fill="${M}" fill-opacity=".55" stroke="${A}" stroke-opacity=".45" stroke-linejoin="round"/>`;C+=P([[-1,-1,-1],[1,-1,-1],[1,1,-1],[-1,1,-1]])+P([[z,-1,-1],[z,1,-1],[z,1,1],[z,-1,1]])+P([[-1,q,-1],[1,q,-1],[1,q,1],[-1,q,1]]);let c="";for(let i of[-0.5,0,0.5])c+=`M${R(i,-1,-1)}L${R(i,1,-1)}M${R(-1,i,-1)}L${R(1,i,-1)}M${R(z,-1,i)}L${R(z,1,i)}M${R(-1,q,i)}L${R(1,q,i)}`;C+=`<path d="${c}" stroke="${A}" stroke-opacity=".18"/>`;let n=b.map((i)=>[...i,CJ(i[0],i[1],i[2])[2]]).sort((i,YJ)=>YJ[YJ.length-1]-i[i.length-1]);for(let i of n){let[YJ,AJ]=y(CJ(i[0],i[1],i[2])),CV=Y.c[LV(i.length>4?i[3]:0)][1];C+=a`<circle cx="${YJ}" cy="${AJ}" r="3.2" fill="${CV}" stroke="${Y.dark?"#0e1117":"#fff"}" stroke-width=".9"/>`}let T=(i,YJ,AJ,CV)=>{let[X0,Y0]=y(CJ(YJ,AJ,CV));return VJ(i,X0,Y0,D-2,400,Y.sub)},[m,g]=[[z,-q],[-z,q]].sort((i,YJ)=>y(CJ(i[0],i[1],0))[0]-y(CJ(YJ[0],YJ[1],0))[0])[0],[QJ,KJ]=y(CJ(m,g,0));C+=T(w[0],0,-q*1.22,-1)+T(w[1],-z*1.22,0,-1)+VJ(w[2],QJ-8,KJ,D-2,400,Y.sub,"end"),h(U,Q,J.w,J.h);break}case"pointcloud":{let[M,A]=k(J.color),w=(J.attrs.points?JSON.parse(J.attrs.points):E0(J.attrs.shape??"torus",+J.attrs.n||240)).map((R)=>[...CJ(R[0],R[1],R[2]),R[3]]),z=jV(w,U,Q,J.w,J.h,4),q=w.map((R)=>R[2]),r=Math.min(...q),y=Math.max(...q);w.sort((R,P)=>P[2]-R[2]);for(let R of w){let P=(y-R[2])/(y-r||1),[c,n]=z(R),T=R[3]!==void 0?Y.c[LV(R[3])][1]:DJ(M,A,0.3+0.7*P);C+=a`<circle cx="${c}" cy="${n}" r="${1+1.4*P}" fill="${T}" fill-opacity="${0.5+0.5*P}"/>`}h(U,Q,J.w,J.h);break}case"waveform":{C=e(uJ(U,Q,J.w,J.h,6));let M=J.attrs.wave?JSON.parse(J.attrs.wave):q0(72),A=6,b=J.y,w=J.h/2-A,z=(J.w-2*A)/M.length,q="";M.forEach(([r,y],R)=>{let P=U+A+z*(R+0.5);q+=a`M${P},${b-Math.max(y,0.02)*w}V${b-Math.min(r,-0.02)*w}`}),C+=`<path d="${q}" stroke="${N}" stroke-width="${HJ(Math.max(0.8,z*0.6))}" stroke-linecap="round"/>`;break}case"sphere":{let[M,A]=k(J.color),b=Math.min(J.w,J.h)/2-2,w="sph-"+S(J.color);if(!F.has(w))F.set(w,`<radialGradient id="${I}${w}" cx=".35" cy=".3" r=".8"><stop offset="0" stop-color="${Y.dark?DJ(M,A,0.35):"#ffffff"}"/><stop offset=".55" stop-color="${M}"/><stop offset="1" stop-color="${DJ(M,A,0.35)}"/></radialGradient>`);C+=a`<circle cx="${J.x}" cy="${J.y}" r="${b}" fill="url(#${I}${w})" stroke="${A}"/>`;let z="",q="",r=(R)=>{let P;for(let c=0;c<=64;c++){let[n,T,m]=CJ(...R(c/64*Math.PI*2)),g=m<=0,QJ=(g===P?"L":"M")+HJ(J.x+n*b)+","+HJ(J.y+T*b);if(g)z+=QJ;else q+=QJ;P=g}};for(let R of[-0.5,0,0.5]){let P=Math.sqrt(1-R*R);r((c)=>[P*Math.cos(c),P*Math.sin(c),R])}for(let R of[0,Math.PI/3,2*Math.PI/3])r((P)=>[Math.cos(P)*Math.cos(R),Math.cos(P)*Math.sin(R),Math.sin(P)]);C+=`<path d="${q}" fill="none" stroke="${A}" stroke-opacity=".22" stroke-dasharray="2 2"/><path d="${z}" fill="none" stroke="${A}" stroke-opacity=".55"/>`;let y=JSON.parse(J.attrs.points??"[]");for(let R of y){let P=Math.hypot(R[0],R[1],R[2])||1,[c,n,T]=CJ(R[0]/P,R[1]/P,R[2]/P),m=Y.c[LV(R[3]??0)][1];C+=a`<circle cx="${J.x+c*b}" cy="${J.y+n*b}" r="3" fill="${m}" stroke="${Y.dark?"#0e1117":"#fff"}" stroke-width=".8" opacity="${T<=0?1:0.3}"/>`}h(U,Q,J.w,J.h);break}default:{let M=J.shape==="thin"?5:7;C=e(uJ(U,Q,J.w,J.h,M),void 0,M)+BJ()}}if(EV.has(J.shape)&&J.label)C+=VJ(J.label,J.x,Q-l*5-11,XJ,500,Y.sub);if(J.attrs.caption)C+=VJ(J.attrs.caption,J.x,J.y+J.h/2+12,D-1,400,Y.sub);let _J=t(J,H);if(_J){let M=J.shape==="op"||J.shape==="circle";C+=M?IJ(_J,J.x+J.w*0.36,J.y-J.h*0.36):IJ(_J,U+J.w-2,Q+2)}if(J.flags.has("crossed")){let M=J.shape==="op"||J.shape==="circle"?J.w*0.15:3;C=`<g opacity=".5">${C}</g>`+fJ(U+M,Q+M,U+J.w-M,Q+J.h-M,2.2)}return`<g>${C}</g>`},fJ=(J,U,Q,Z,N)=>{let H=a`M${J},${U}L${Q},${Z}M${Q},${U}L${J},${Z}`,L=Y.c.red[1];return`<path d="${H}" stroke="${Y.halo}" stroke-width="${N+2.5}" stroke-linecap="round"/><path d="${H}" stroke="${L}" stroke-width="${N}" stroke-linecap="round"/>`},NV=()=>V.dir==="RL",rJ=()=>V.dir==="BT",hJ=(J)=>{if(YV(J))return"";let U=J.attrs.color,Q=!U?Y.edge:Y.c[U]||DV(U)?k(U)[1]:gJ(U);if(J.fc)return RJ(J,Q);let Z=J.pts.map((C)=>[...C]);if(Z.length<2)return"";let N=J.op==="=>"||J.flags.has("thick"),H=J.op.startsWith("..")||J.flags.has("dashed"),L=J.op==="--"||J.op===".."?0:J.op==="<->"?2:1,o=N?9:7.5,x=N?4.2:3.4,l="",FJ=(C,XJ)=>{let p=C[0]-XJ[0],$J=C[1]-XJ[1],BJ=Math.hypot(p,$J)||1,_J=p/BJ,M=$J/BJ,A=C[0]-_J*o,b=C[1]-M*o;return l+=`<path d="${lJ(C[0],C[1],A-M*x,b+_J*x,C[0]-_J*o*0.72,C[1]-M*o*0.72,A+M*x,b-_J*x)}" fill="${Q}"/>`,[C[0]-_J*o*0.7,C[1]-M*o*0.7]};for(let C of Z)h(C[0]-2,C[1]-2,4,4);let e;if(J.curve){let[C,XJ,p,$J]=Z,BJ=J.curve,_J=[C[0]+(XJ[0]-C[0])*BJ,C[1]+(XJ[1]-C[1])*BJ],M=[$J[0]+(p[0]-$J[0])*BJ,$J[1]+(p[1]-$J[1])*BJ],A=L?FJ($J,M):$J,b=L===2?FJ(C,_J):C;e=a`M${b[0]},${b[1]}C${_J[0]},${_J[1]} ${M[0]},${M[1]} ${A[0]},${A[1]}`}else{if(L)Z[Z.length-1]=FJ(Z[Z.length-1],Z[Z.length-2]);if(L===2)Z[0]=FJ(Z[0],Z[1]);e=a`M${Z[0][0]},${Z[0][1]}`;for(let C=1;C<Z.length-1;C++){let[XJ,p,$J]=[Z[C-1],Z[C],Z[C+1]],BJ=Math.hypot(p[0]-XJ[0],p[1]-XJ[1]),_J=Math.hypot($J[0]-p[0],$J[1]-p[1]),M=Math.min(8,BJ/2,_J/2);e+=a`L${p[0]+(XJ[0]-p[0])*M/(BJ||1)},${p[1]+(XJ[1]-p[1])*M/(BJ||1)}Q${p[0]},${p[1]} ${p[0]+($J[0]-p[0])*M/(_J||1)},${p[1]+($J[1]-p[1])*M/(_J||1)}`}e+=a`L${Z[Z.length-1][0]},${Z[Z.length-1][1]}`}if(J.flags.has("crossed")){let C=0,XJ=-1;for(let BJ=0;BJ+1<J.pts.length;BJ++){let _J=Math.hypot(J.pts[BJ+1][0]-J.pts[BJ][0],J.pts[BJ+1][1]-J.pts[BJ][1]);if(_J>XJ)XJ=_J,C=BJ}let p=(J.pts[C][0]+J.pts[C+1][0])/2,$J=(J.pts[C][1]+J.pts[C+1][1])/2;l+=fJ(p-5,$J-5,p+5,$J+5,2)}return a`<path d="${e}" fill="none" stroke="${Q}" stroke-width="${N?2.2:1.4}" stroke-linecap="round" stroke-linejoin="round"${H?' stroke-dasharray="5 4"':""}/>`+l},dJ=(J)=>{let U=+J.attrs.n||4;return Array.from({length:U},(Q,Z)=>{let N=-((U-1)/2)*20+Z*20;return O?[J.x,J.y+N]:[J.x+N,J.y]})},RJ=(J,U)=>{let Q="";for(let[Z,N]of dJ(oJ(J.from)))for(let[H,L]of dJ(oJ(J.to))){let o=Math.hypot(H-Z,L-N),x=(H-Z)/o,l=(L-N)/o;Q+=a`M${Z+x*7},${N+l*7}L${H-x*7},${L-l*7}`}return`<path d="${Q}" stroke="${U}" stroke-opacity=".45" stroke-width=".8"/>`},oJ=(J)=>V.nodes.get(J),BV=(J)=>{if(!J.w)return"";let{fill:U,stroke:Q,fx:Z}=J.attrs.texture||J.flags.has("frozen")||J.flags.has("train")?OJ(J):{fill:J.attrs.color?k(J.attrs.color)[0]:Y.group[0],stroke:J.attrs.color?k(J.attrs.color)[1]:Y.group[1],fx:""},N=J.flags.has("dashed")?' stroke-dasharray="5 4"':"",H=uJ(J.x,J.y,J.w,J.h,12),L=`<path d="${H}" fill="${U}" stroke="${Q}" stroke-width="1.2"${N}/>`,o=EJ(Z,H,J.x,J.y,J.w,J.h,J.attrs.color);if(o)L+=o+`<path d="${H}" fill="none" stroke="${Q}" stroke-width="1.2" stroke-opacity=".7"/>`;else if(Z==="ice")L+=`<path d="${H}" fill="url(#${I}frost)"/>`+vJ(J.x,J.y,J.w,12);if(h(J.x,J.y,J.w,J.h),J.label)L+=VJ(J.label,J.x+12,J.y+13,D-2,600,Y.sub,"start");if(J.attrs.repeat){let[x,l,FJ]=iJ(J);L+=VJ(J.attrs.repeat,x,l,D+2,500,Y.text,FJ)}return L+=IJ(t(J,Z),J.x+J.w-2,J.y+2),L},bJ=[...V.nodes.values()],KV=bJ.reduce((J,U)=>J+U.x,0)/(bJ.length||1),PJ=(J,U)=>J[0]<U[2]&&J[2]>U[0]&&J[1]<U[3]&&J[3]>U[1],sJ=[],QV=(J)=>{let U=new Set;for(let Z=J;Z;Z=V.groups.find((N)=>N.id===Z.parent))U.add(Z.id);let Q=bJ.map((Z)=>[Z.x-Z.w/2,Z.y-Z.h/2,Z.x+Z.w/2,Z.y+Z.h/2+(Z.attrs.caption?20:0)]);for(let Z of V.edges){for(let N=0;N+1<Z.pts.length;N++){let[H,L]=[Z.pts[N],Z.pts[N+1]];Q.push([Math.min(H[0],L[0])-2,Math.min(H[1],L[1])-2,Math.max(H[0],L[0])+2,Math.max(H[1],L[1])+2])}if(Z.label&&Z.lx!==void 0){let N=qJ(Z.label,D-1);Q.push([Z.lx-N.w/2,Z.ly-N.h/2,Z.lx+N.w/2,Z.ly+N.h/2])}}for(let Z of V.groups)if(Z.w&&!U.has(Z.id)&&Z.parent!==J.id)Q.push([Z.x,Z.y,Z.x+Z.w,Z.y+Z.h]);return[...Q,...sJ]},iJ=(J)=>{let U=qJ(J.attrs.repeat,D+2,500),Z={left:(l=0.5)=>[J.x-10,J.y+J.h*l,"end"],right:(l=0.5)=>[J.x+J.w+10,J.y+J.h*l,"start"],top:()=>[J.x+J.w/2,J.y-10-U.h/2,"middle"],bottom:()=>[J.x+J.w/2,J.y+J.h+10+U.h/2,"middle"]},N=([l,FJ,e])=>[e==="end"?l-U.w:e==="middle"?l-U.w/2:l,FJ-U.h/2,e==="start"?l+U.w:e==="middle"?l+U.w/2:l,FJ+U.h/2],H=J.x+J.w/2>KV?"right":"left",L=H==="right"?"left":"right",o=J.attrs["repeat-side"],x=o&&o!=="auto"?Z[o]():void 0;if(!x){let l=[Z[H](),Z.bottom(),Z[L](),Z.top(),...[0.25,0.75].flatMap((e)=>[Z[H](e),Z[L](e)])],FJ=QV(J);x=l.find((e)=>!FJ.some((C)=>PJ(N(e),C)))??l[0]}return sJ.push(N(x)),x},IV=bJ.filter((J)=>J.shape==="magnify").map((J)=>{let U=mJ(J);if(!U)return"";let Q=kJ(U),[Z,N,H,L]=pJ(J,U),[o,x,l,FJ]=_V(U.attrs),e=Q.x0+(Q.k-1)*6,C=Q.y0+(Q.k-1)*6,XJ=(P,c)=>{let n=e+P*Q.W-U.x,T=C+c*Q.H-U.y;return[U.x+o*n+l*T,U.y+x*n+FJ*T]},p=[XJ(Z,N),XJ(Z+H,N),XJ(Z+H,N+L),XJ(Z,N+L)],[$J,BJ]=p.reduce((P,c)=>[P[0]+c[0]/4,P[1]+c[1]/4],[0,0]),_J=Math.hypot(J.x-$J,J.y-BJ)||1,M=-(J.y-BJ)/_J,A=(J.x-$J)/_J,b=(P)=>P[0]*M+P[1]*A,w=[...p].sort((P,c)=>b(P)-b(c)),z=Math.min(J.w,J.h)/2,q=J.attrs.lens==="square"?[[J.x-J.w/2,J.y-J.h/2],[J.x+J.w/2,J.y-J.h/2],[J.x+J.w/2,J.y+J.h/2],[J.x-J.w/2,J.y+J.h/2]].sort((P,c)=>b(P)-b(c)):[[J.x-M*z,J.y-A*z],[J.x+M*z,J.y+A*z]],[r,y]=[q[0],q[q.length-1]],R=k(J.color)[1];return`<path d="M${p.map((P)=>P.map(HJ).join(",")).join("L")}z" fill="none" stroke="${R}" stroke-width="2" stroke-linejoin="round"/><path d="M${w[0].map(HJ)}L${r.map(HJ)}M${w[3].map(HJ)}L${y.map(HJ)}" stroke="${R}" stroke-opacity=".75" stroke-dasharray="3 3"/>`}).join(""),zJ=V.groups.map(BV).join(""),GV=V.edges.map(hJ).join(""),$=[...V.nodes.values()].map(yJ).join(""),B=V.edges.filter((J)=>J.label&&J.lx!==void 0).map((J)=>VJ(J.label,J.lx,J.ly,D-1,400,Y.sub)).join("");if(j===1/0)j=f=v=d=0;let _=14,G=v-j+_*2,u=d-f+_*2,W=Y.bg!=="none"?a`<rect x="${j-_}" y="${f-_}" width="${G}" height="${u}" rx="10" fill="${Y.bg}"/>`:"";return a`<svg xmlns="http://www.w3.org/2000/svg" viewBox="${j-_} ${f-_} ${G} ${u}" width="${G}" height="${u}" font-family='${gV}' style="max-width:100%;height:auto" role="img">`+`<defs>${[...F.values()].join("")}</defs>${W}<g>${zJ}</g><g>${GV}</g><g>${$}</g><g>${IV}</g><g paint-order="stroke" stroke="${Y.halo}" stroke-width="4" stroke-linejoin="round">${B}</g></svg>`}class zV{nodes=new Map;edges=[];groups=new Map;scope=[];ranks=[];ids=new Set;claim(V,X,K){if(V===void 0)while(this.ids.has(V=`${X}${K}`))K++;else if(this.ids.has(V))throw Error(`duplicate id "${V}"`);return this.ids.add(V),V}node(V){let X=this.nodes.get(V);if(!X){let K=this.claim(V.options.id,"n",this.nodes.size),Y=this.scope.map((I)=>this.groups.get(I)?.id).filter((I)=>!!I);X={id:K,mods:[...this.scope],groups:Y},this.nodes.set(V,X)}return X.id}apply(V,X){let K=this.node(V);for(let Y of X)this.edges.push({from:Y.node,to:V,style:Y.style});return new jJ(this,K)}enter(V){let X=V.options;if((X.box??!!(X.label||X.repeat))&&!this.groups.has(V)){let K=[...this.scope].reverse().map((Y)=>this.groups.get(Y)).find(Boolean);this.groups.set(V,{id:this.claim(X.id,"g",this.groups.size),label:X.label??"",parent:K?.id,flags:new Set(X.dashed?["dashed"]:[]),attrs:{...X.repeat&&{repeat:X.repeat},...X.repeatSide&&{"repeat-side":X.repeatSide},...X.color&&{color:X.color},...X.texture&&{texture:X.texture}},x:0,y:0,w:0,h:0})}this.scope.push(V)}id(V){if(V instanceof jJ)return V.node;let X=this.nodes.get(V);if(!X)throw Error(`"${V.label}" is not part of this figure`);return X.id}}class jJ{graph;node;style;constructor(V,X,K={}){this.graph=V;this.node=X;this.style=K}edge(V){return new jJ(this.graph,this.node,{...this.style,...typeof V==="string"?{label:V}:V})}}var J0=(V,X)=>{let K=V[0]?.graph;if(!K)throw Error(`${X} was called without inputs; start from fig.input()`);if(V.some((Y)=>!(Y instanceof jJ)||Y.graph!==K))throw Error(`${X}: inputs must be Tensors of the same figure`);return K};function TJ(V,X,K,Y=V,I={}){let O=(...D)=>J0(D,`"${O.label}"`).apply(O,D);return Object.assign(O,{shape:V,kind:Y,label:X,options:K,defaults:I,freeze(){return O.options.frozen=!0,O.options.trainable=!1,O},trainable(){return O.options.trainable=!0,O.options.frozen=!1,O}})}function JJ(V,X="",K={}){let{kind:Y=V,...I}=typeof K==="string"?{color:K}:K;return(O,D)=>{let[F,E]=typeof O==="string"?[O,D??{}]:[X,O??{}];return TJ(V,F,{...E},Y,I)}}var j0=JJ("block"),L0=JJ("thin"),V0=JJ("text"),S0=JJ("circle"),wV=JJ("op","+"),v0=JJ("enc","Encoder","blue"),z0=JJ("dec","Decoder","green"),w0=JJ("tensor","","blue"),u0=JJ("stack","","blue"),T0=JJ("neurons");function k0(V,X,K){let[Y,I]=typeof X==="string"?[X,K??{}]:["",X??{}];return TJ("vector",Y,{...I,values:V})}var m0=JJ("block","Multi-Head Attention",{color:"orange",kind:"mha"}),y0=JJ("block","Attention",{color:"orange",kind:"attention"}),f0=JJ("block","Feed Forward",{color:"blue",kind:"ffn"}),h0=JJ("block","MLP",{color:"blue",kind:"mlp"}),b0=JJ("thin","Add & Norm",{color:"yellow",kind:"norm"}),x0=JJ("block","Embedding",{color:"pink",kind:"embed"}),g0=JJ("thin","Linear",{color:"purple",kind:"linear"}),l0=JJ("thin","Softmax",{color:"green",kind:"softmax"}),c0=JJ("thin","MatMul",{color:"purple",kind:"matmul"}),p0=JJ("thin","Concat",{color:"yellow",kind:"concat"}),d0=JJ("block","Conv",{color:"teal",kind:"conv"}),o0=JJ("thin","Pool",{color:"gray",kind:"pool"}),i0=JJ("thin","ReLU",{color:"green",kind:"act"}),a0=JJ("thin","Dropout",{color:"gray",kind:"dropout"}),t0=JJ("block","Loss",{color:"red",kind:"loss"}),r0=JJ("gaussian","","purple"),s0=JJ("space","","gray"),e0=JJ("sphere","","blue"),n0=JJ("pointcloud","","blue");function J4(V,X){let[K,{samples:Y,...I}]=typeof V==="string"?[V,X??{}]:["",V??{}],O;if(Y?.length){let D=Math.min(256,Y.length),F=(E)=>Math.round(E*1000)/1000;O=Array.from({length:D},(E,S)=>{let j=1/0,f=-1/0;for(let v=Math.floor(S*Y.length/D);v<Math.floor((S+1)*Y.length/D);v++)j=Math.min(j,Y[v]),f=Math.max(f,Y[v]);return[F(j),F(f)]})}return TJ("waveform",K,{...I,...O&&{wave:O}},"waveform",{color:"teal"})}var SV=(V)=>typeof V==="string"?V:("toDataURL"in V)?V.toDataURL("image/png"):V.currentSrc||V.src||"";function V4(V,X,K){let[Y,I]=typeof X==="string"?[X,K??{}]:["",X??{}],O=Array.isArray(V)?V.map(SV):[SV(V)];return TJ("image",Y,{n:O.length>1?O.length:void 0,...I,src:O[0],...O.length>1&&{srcs:O}})}function $4(V,X){let[K,{src:Y,...I}]=typeof V==="string"?[V,X??{}]:["",V??{}];return TJ("magnify",K,{...I,...Y&&{src:SV(Y)}},"magnify",{color:"orange"})}var $0=(V,X)=>V===void 0?{v:Math.abs(Math.sin(X*12.9898+4.1)*43758.5453)%1}:typeof V==="number"?{v:V}:typeof V==="string"?{c:V}:{v:V.value,c:V.color,t:V.texture,l:V.label,x:V.crossed};function U4(V,X){let[K,{cells:Y,...I}]=typeof V==="string"?[V,X??{}]:["",V??{}],O=I.rows??(Array.isArray(Y)?Y.length:5),D=I.cols??(Array.isArray(Y)?Math.max(0,...Y.map((S)=>S.length)):5),F=(S,j)=>typeof Y==="function"?Y(S,j):Y?.[S]?.[j],E=Array.from({length:O},(S,j)=>Array.from({length:D},(f,v)=>$0(F(j,v),j*31+v)));return TJ("matrix",K,{...I,rows:O,cols:D,grid:E},"matrix",{color:"blue"})}function X4(V,X){let[K,{cells:Y,...I}]=typeof V==="string"?[V,X??{}]:["",V??{}],O=Array.isArray(Y)?Y:void 0,D=I.rows??O?.length??5,F=I.cols??(O?Math.max(0,...O.map((v)=>v.length)):5),E=I.depth??(O?Math.max(0,...O.flatMap((v)=>v.map((d)=>d.length))):5),S=(v,d,h)=>$0(typeof Y==="function"?Y(v,d,h):Y?.[v]?.[d]?.[h],v*97+d*31+h*7),j=(v,d,h)=>Array.from({length:v},(k,MJ)=>Array.from({length:d},(UJ,s)=>h(MJ,s))),f={front:j(D,F,(v,d)=>S(v,d,0)),top:j(E,F,(v,d)=>S(0,d,v)),side:j(D,E,(v,d)=>S(v,F-1,d))};return TJ("voxels",K,{...I,rows:D,cols:F,depth:E,faces:f},"voxels",{color:"blue"})}var Y4=(...V)=>wV("+")(...V),B4=(...V)=>wV("*")(...V);class U0{constructor(V={}){let X=(...K)=>{let Y=J0(K,X.constructor.name||"module");Y.enter(X);try{return X.forward(...K)}finally{Y.scope.pop()}};return delete X.name,delete X.length,Object.setPrototypeOf(X,new.target.prototype),X.options={...V},X}freeze(){return this.options.frozen=!0,this.options.trainable=!1,this}trainable(){return this.options.trainable=!0,this.options.frozen=!1,this}}var XV={};function K4(V){if(V===null)AV(void 0);else if("tex"in V)AV(V.tex);let{tex:X,...K}=V??{};return XV=V===null?{}:vV(XV,K),structuredClone(XV)}var vV=(V,X)=>{let K={...V.kinds};for(let[Y,I]of Object.entries(X.kinds??{}))K[Y]={...K[Y],...I};return{...V,...X,style:{...V.style,...X.style},kinds:K}},Q4=(...V)=>{let X={};for(let K of V)for(let[Y,I]of Object.entries(K??{}))if(I!==void 0)X[Y]=I;return X},I4=new Set(["id","color","frozen","trainable","dashed","bold","crossed"]),_4={leftOf:"left-of",rightOf:"right-of"};class uV{options;g=new zV;constructor(V={}){this.options={...V}}input(V="",X={}){if(typeof V!=="string")Object.assign(V.options,X);let K=typeof V==="string"?V0(V,X):V;return new jJ(this.g,this.g.node(K))}edge(V,X,K={}){if(V.graph!==this.g)throw Error("edge: tensor belongs to another figure");if(!(X instanceof jJ))this.g.node(X);return this.g.edges.push({from:V.node,to:X,style:{...V.style,...typeof K==="string"?{label:K}:K}}),this}rank(...V){return this.g.ranks.push(V),this}diagram(){let V=this.g,X=new Map,K=vV(XV,this.options),Y=0;for(let[O,D]of V.nodes){let F=[K,...D.mods.map((s)=>s.options)],E=O.options,S=Q4(O.defaults,...F.flatMap((s)=>[s?.style,s?.kinds?.[O.kind]]),E),j=O.label,f=(s)=>{if(!j.includes(s))return!1;return j=j.replace(s,"").trim(),!0},v=f("\uD83E\uDDCA"),d=f("\uD83D\uDD25"),h=[...D.mods].reverse().map((s)=>s.options).find((s)=>s.frozen||s.trainable),k=E.frozen||v?"frozen":E.trainable||d?"train":h?.frozen?"frozen":h?.trainable?"train":"";if(k&&!E.texture)delete S.texture;let MJ=new Set([k,S.dashed&&"dashed",S.bold&&"bold",S.crossed&&"crossed"].filter((s)=>!!s)),UJ={};for(let[s,OJ]of Object.entries(S)){if(I4.has(s)||OJ===void 0)continue;UJ[_4[s]??s]=OJ instanceof jJ||typeof OJ==="function"?V.id(OJ):Array.isArray(OJ)&&OJ.every((VJ)=>typeof VJ==="number")?OJ.join(","):typeof OJ==="object"?JSON.stringify(OJ):String(OJ)}X.set(D.id,{id:D.id,shape:O.shape,label:j,color:S.color,flags:MJ,attrs:UJ,groups:D.groups,order:Y++,x:0,y:0,w:0,h:0})}let I=V.edges.map(({from:O,to:D,style:F})=>({from:O,to:V.id(D),label:F.label,pts:[],op:F.dense?"=>":F.arrow==="none"?F.dashed?"..":"--":F.arrow==="both"?"<->":F.dashed?"..>":"->",flags:new Set([...["skip","curve","thick","dashed","crossed"].filter((E)=>F[E]),...F.join?["join"]:[]]),attrs:{...F.side&&{side:F.side},...F.color&&{color:F.color}}}));return{dir:K.flow??"TB",theme:K.theme??"light",gap:K.gap,fontSize:K.style?.fontSize,nodes:X,edges:I,groups:[...V.groups.values()].map((O)=>({...O,flags:new Set(O.flags)})),ranks:V.ranks.map((O)=>O.map((D)=>V.id(D)))}}svg(V={}){let X=this.diagram();return MV(VV(X),V.theme??X.theme,vV(XV,this.options).materials??!0)}async toBlob(V={}){let X=V.format??"svg",K=this.svg({theme:V.theme});if(X==="svg")return new Blob([K],{type:"image/svg+xml"});if(!["png","jpeg","webp"].includes(X))throw Error(`export: unknown format "${X}" (svg, png, jpeg, webp)`);return Z4(K,{...V,format:X},`image/${X}`)}async download(V="figure.svg",X={}){let K=/\.(\w+)$/.exec(V)?.[1].toLowerCase(),Y=X.format??(K==="jpg"?"jpeg":K)??"svg",I=URL.createObjectURL(await this.toBlob({...X,format:Y}));Object.assign(document.createElement("a"),{href:I,download:V}).click(),setTimeout(()=>URL.revokeObjectURL(I),1000)}mount(V,X={}){let K=typeof V==="string"?document.querySelector(V):V;if(!K)throw Error(`mount: no element matches "${V}"`);return K.innerHTML=this.svg(X),this}}async function O4(V){let X=[...new Set([...V.matchAll(/<image href="(?!data:)([^"]+)"/g)].map((K)=>K[1].replace(/&/g,"&")))];for(let K of X)try{let Y=await(await fetch(K)).blob(),I=await new Promise((O,D)=>{let F=new FileReader;F.onload=()=>O(F.result),F.onerror=D,F.readAsDataURL(Y)});V=V.split(`href="${K.replace(/&/g,"&")}"`).join(`href="${I}"`)}catch{}return V}async function Z4(V,X,K){if(typeof document>"u")throw Error(`export: ${X.format} needs a browser (canvas); in Node use svg()`);let[,Y,I]=/width="([\d.]+)" height="([\d.]+)"/.exec(V)??[],O=X.scale??2,D=new Image;D.src="data:image/svg+xml;charset=utf-8,"+encodeURIComponent(await O4(V)),await D.decode();let F=document.createElement("canvas");F.width=Math.ceil(+Y*O),F.height=Math.ceil(+I*O);let E=F.getContext("2d"),S=X.background??(K==="image/jpeg"?"#ffffff":"");if(S)E.fillStyle=S,E.fillRect(0,0,F.width,F.height);return E.drawImage(D,0,0,F.width,F.height),new Promise((j,f)=>F.toBlob((v)=>v?j(v):f(Error("export: canvas.toBlob failed")),K,X.quality??0.92))}function F4(V,X){let[K,Y]=typeof V==="function"?[{},V]:[V,X],I=new uV(K);return Y(I),I}function D4(V,X={}){return MV(VV(V),X.theme,X.materials??!0)}var v4={...TV,draw:D4,layout:VV,THEMES:FV};export{J4 as waveform,X4 as voxels,k0 as vector,L0 as thin,V0 as text,w0 as tensor,u0 as stack,e0 as sphere,s0 as space,l0 as softmax,JJ as preset,o0 as pool,n0 as pointcloud,wV as op,b0 as norm,T0 as neurons,B4 as mul,h0 as mlp,m0 as mha,U4 as matrix,c0 as matmul,$4 as magnify,t0 as loss,g0 as linear,VV as layout,TJ as layer,V4 as image,r0 as gaussian,F4 as figure,f0 as ffn,v0 as enc,x0 as embed,a0 as dropout,D4 as draw,v4 as default,z0 as dec,d0 as conv,K4 as configure,p0 as concat,S0 as circle,j0 as block,y0 as attention,Y4 as add,i0 as act,jJ as Tensor,FV as THEMES,U0 as Module,zV as Graph,uV as Figure};
|
|
481
|
+
}`),Ct=o}catch{Ct=null}return Ct}function Qe(o,n,c,a=0){n=Math.round(n),c=Math.round(c);let u=`${o}:${n}x${c}:${a}`,h=Xe.get(u);if(h)return h;let x=Lo();if(!x||n<1||c<1)return null;let g=Math.min(Math.max(1,globalThis.devicePixelRatio||1),2,1024/Math.max(n,c)),v=Math.round(n*g),R=Math.round(c*g),I=Zt[o];x.viewport(0,0,v,R),x.useProgram(I);let q=(X)=>x.getUniformLocation(I,X);x.uniform2f(q("uRes"),n,c),x.uniform1f(q("uTime"),0),x.uniform1f(q("uIntensity"),0.5),x.uniform1f(q("uSeed"),a),x.uniform1f(q("uCover"),1),x.drawArrays(x.TRIANGLE_STRIP,0,4);let O=document.createElement("canvas");O.width=v,O.height=R,O.getContext("2d").drawImage(fe,0,1024-R,v,R,0,0,v,R);let U=O.toDataURL("image/webp",0.9);return Xe.set(u,U),U}var Se={light:{none:["#e2e5e9","#7f8794"],bg:"none",text:"#1d2430",sub:"#5b6474",edge:"#39414f",halo:"#fff",group:["#f5f6f9","#d5dae2"],shadow:"rgba(30,40,60,.10)",dark:!1,ice:["#f7fdff","#d2effb","#aedcf2","#6fb9de"],fire:["#fff6e8","#ffcf9f","#ee7d36"],c:{white:["#ffffff","#8b95a5"],gray:["#eceff3","#9aa3b2"],pink:["#fde1e4","#e58c98"],orange:["#fde4c8","#e69a50"],yellow:["#f4f5cc","#c0c064"],green:["#d8f1de","#68b880"],blue:["#d6eafc","#5f9fd8"],purple:["#e4defa","#9584db"],red:["#ffd7d2","#dd6559"],teal:["#d1f0ec","#48a99f"]}},dark:{none:["#2a2f37","#8a93a2"],bg:"#0e1117",text:"#e8edf5",sub:"#9aa4b5",edge:"#c3cad6",halo:"#0e1117",group:["#161a22","#2b323f"],shadow:"rgba(0,0,0,.45)",dark:!0,ice:["#29506a","#163a50","#0f2c3e","#7fd4ff"],fire:["#4a2a12","#33180a","#ff9447"],c:{white:["#1b2029","#7d8799"],gray:["#1f242d","#8792a4"],pink:["#3a1e29","#ff8ea2"],orange:["#3a2916","#ffae5c"],yellow:["#2f3018","#e3da66"],green:["#142e22","#5ad08a"],blue:["#13273c","#5ab1ff"],purple:["#241e3f","#a892ff"],red:["#3c1b19","#ff786a"],teal:["#11312f","#4cd3c3"]}}},xt=(o)=>+o.toFixed(1),Z=(o,...n)=>o.reduce((c,a,u)=>c+(typeof n[u-1]==="number"?xt(n[u-1]):n[u-1])+a),Ot=(o)=>o.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,"""),Oe=(o)=>/^#([\da-f]{3}|[\da-f]{6})$/i.test(o),to=(o)=>{let n=o.replace("#",""),c=n.length===3?[...n].map((a)=>a+a).join(""):n;return[0,2,4].map((a)=>parseInt(c.slice(a,a+2),16))},ut=(o,n,c)=>{if(!Oe(o)||!Oe(n))return o;return c=Math.min(1,Math.max(0,c)),"#"+to(o).map((a,u)=>Math.round(a+(to(n)[u]-a)*c).toString(16).padStart(2,"0")).join("")},Tt=(o,n,c,a,u)=>(u=Math.min(u,c/2,a/2),Z`M${o+u},${n}h${c-2*u}a${u},${u} 0 0 1 ${u},${u}v${a-2*u}a${u},${u} 0 0 1 ${-u},${u}h${2*u-c}a${u},${u} 0 0 1 ${-u},${-u}v${2*u-a}a${u},${u} 0 0 1 ${u},${-u}z`),ke=(o,n,c)=>Z`M${o-c},${n}a${c},${c} 0 1 0 ${2*c},0a${c},${c} 0 1 0 ${-2*c},0z`,Vt=(...o)=>"M"+o.map(xt).join(",").replace(/([^,]*,[^,]*),/g,"$1L")+"z",no=-0.65,ao=0.45,eo=Math.cos(no),oo=Math.sin(no),ro=Math.cos(ao),so=Math.sin(ao),mt=(o,n,c)=>{let a=o*eo-n*oo,u=o*oo+n*eo;return[a,-c*ro-u*so,u*ro-c*so]},Te=(o,n,c,a,u,h)=>{let x=o.map((V)=>V[0]),g=o.map((V)=>V[1]),v=Math.min(...x),R=Math.max(...x),I=Math.min(...g),q=Math.max(...g),O=Math.min((a-2*h)/(R-v||1),(u-2*h)/(q-I||1)),U=n+a/2-(v+R)/2*O,X=c+u/2-(I+q)/2*O;return(V)=>[U+V[0]*O,X+V[1]*O]},zo=(o)=>[(0.5+o*0.7548776662)%1,(0.5+o*0.569840291)%1],No=(o,n)=>Array.from({length:n},(c,a)=>{let[u,h]=zo(a),x=u*2*Math.PI;if(o==="sphere"){let X=2*h-1,V=Math.sqrt(1-X*X);return[V*Math.cos(x),V*Math.sin(x),X]}if(o==="cube"){let X=a%6,V=2*u-1,_=2*h-1,ft=X<3?1:-1;return X%3===0?[ft,V,_]:X%3===1?[V,ft,_]:[V,_,ft]}let g=h*2*Math.PI,v=0.68,R=0.3,I=0.8,q=(v+R*Math.cos(g))*Math.cos(x),O=(v+R*Math.cos(g))*Math.sin(x),U=R*Math.sin(g);return[q,O*Math.cos(I)-U*Math.sin(I),O*Math.sin(I)+U*Math.cos(I)]}),Ao=(o)=>Array.from({length:o},(n,c)=>{let a=c/o,h=0.06+0.9*[[0.14,0.9],[0.3,0.6],[0.47,1],[0.66,0.75],[0.84,0.5]].reduce((x,[g,v])=>x+v*Math.exp(-(((a-g)/0.07)**2)),0)*(0.55+0.45*Math.abs(Math.sin(c*1.7)*Math.cos(c*0.37)));return[-h*(0.85+0.15*Math.sin(c*2.3)),h]}),Me=["blue","orange","green","purple","red","teal","pink","yellow"],Ie=(o)=>Me[((o|0)%Me.length+Me.length)%Me.length],Co=0;function Ee(o,n,c=!0){let a=Se[n??o.theme]??Se.light,u=`mm${Co++}-`,h=o.dir==="LR"||o.dir==="RL",x=o.fontSize??Ae,g=o.fontFamily??xe,v=new Map,R=new Map,I=(t)=>{if(!t)return"none";if(/^#?\w+$/.test(t))return t.replace("#","");if(!R.has(t))R.set(t,`css${R.size}`);return R.get(t)},q=1/0,O=1/0,U=-1/0,X=-1/0,V=(t,l,d=0,e=0)=>{q=Math.min(q,t),O=Math.min(O,l),U=Math.max(U,t+d),X=Math.max(X,l+e)},_=(t)=>{if(!t)return a.none;if(a.c[t])return a.c[t];if(Oe(t))return[a.dark?ut(t,"#0e1117",0.75):ut(t,"#ffffff",0.78),t];return[Ot(t),a.edge]},ft=(t)=>{let l="c-"+I(t);if(!v.has(l)){let[d,e]=_(t),r=a.dark?ut(d,e,0.16):ut(d,"#ffffff",0.6);v.set(l,Z`<linearGradient id="${u}${l}" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stop-color="${r}"/><stop offset="1" stop-color="${d}"/></linearGradient>`)}return`url(#${u}${l})`},W=()=>{if(v.has("ice"))return;let[t,l,d]=a.ice;v.set("ice",`<linearGradient id="${u}ice" x1="0" y1="0" x2="1" y2="1"><stop offset="0" stop-color="${t}"/><stop offset=".55" stop-color="${l}"/><stop offset="1" stop-color="${d}"/></linearGradient><pattern id="${u}frost" width="56" height="56" patternUnits="userSpaceOnUse"><path d="M0 14 16 20 25 6M16 20 22 37 41 33 56 45M41 33 47 12 56 9M22 37 9 56M47 12 33 0" fill="none" stroke="#fff" stroke-opacity="${a.dark?0.18:0.75}" stroke-width="1"/><path d="M5 30 12 33M30 50 36 46M50 25 54 30" stroke="${a.ice[3]}" stroke-opacity=".35" stroke-width=".8"/></pattern>`)},lt=()=>{if(v.has("fire"))return;let[t,l,d]=a.fire;v.set("fire",`<linearGradient id="${u}fire" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stop-color="${t}"/><stop offset="1" stop-color="${l}"/></linearGradient><linearGradient id="${u}flame" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stop-color="#ffd24a"/><stop offset=".6" stop-color="${d}"/><stop offset="1" stop-color="#e0452b"/></linearGradient>`)},wt=(t)=>{let l=t.attrs.texture??(t.flags.has("frozen")?"ice":t.flags.has("train")?"magma":"");if(l==="ice")return W(),{fill:`url(#${u}ice)`,stroke:a.ice[3],fx:l};if(l==="magma")return lt(),{fill:`url(#${u}fire)`,stroke:a.fire[2],fx:l};if(l==="metal")return{fill:ft(t.color),stroke:a.dark?"#9aa3ae":"#6d747d",fx:l};return{fill:ft(t.color),stroke:_(t.color)[1],fx:l}},ot=(t,l,d,e,r,s,i="middle",y=g)=>{let p=bt(t,e,r,y),f="",S="";return p.lines.forEach((E,m)=>{let w=i==="middle"?l-p.widths[m]/2:i==="start"?l:l-p.widths[m],z=d-p.h/2+(m+0.5)*p.lh+e*0.36;for(let P of E){if(P.math){let C=P.math;S+=Z`<svg x="${w+P.x}" y="${z-C.asc}" width="${C.w}" height="${C.asc+C.desc}" viewBox="${C.viewBox}" overflow="visible" color="${s}">${C.inner}</svg>`;continue}if(f+=Z`<tspan x="${w+P.x}" y="${z+P.dy}"`,P.size!==e)f+=Z` font-size="${P.size}"`;if(P.it)f+=' font-style="italic"';if(P.serif)f+=` font-family='${Ze}' font-weight="${P.b?700:400}"`;else if(P.b)f+=' font-weight="700"';f+=`>${Ot(P.t)}</tspan>`}}),V(l-(i==="middle"?p.w/2:i==="end"?p.w:0),d-p.h/2,p.w,p.h),(f?Z`<text${y!==g?` font-family="${Ot(y)}"`:""} font-size="${e}" font-weight="${r}" fill="${s}" style="white-space:pre">${f}</text>`:"")+S},H=(t,l)=>t.flags.has("frozen")?"ice":t.flags.has("train")?"magma":l==="ice"||l==="magma"?l:"",it=(t,l,d)=>{if(t==="ice")return Z`<g transform="translate(${l},${d})"><circle r="7.5" fill="${a.dark?"#0f2c3e":"#fff"}" stroke="${a.ice[3]}"/><path d="M0-4.6V4.6M-4 -2.3 4 2.3M-4 2.3 4-2.3M-1.4-3.6 0-2.4 1.4-3.6M-1.4 3.6 0 2.4 1.4 3.6" fill="none" stroke="${a.ice[3]}" stroke-width="1.1" stroke-linecap="round"/></g>`;if(t!=="magma")return"";return lt(),Z`<g transform="translate(${l},${d})"><circle r="7.5" fill="${a.dark?"#33180a":"#fff"}" stroke="${a.fire[2]}"/><path d="M0.4-5C3 -2.4 3.9-.2 3.1 2.2 2.5 4.1-2.3 4.3-3 2.1-3.6.2-2.2-1.2-1.6-2.6-.9-1.4-.3-.9.2-.6.9-2 .9-3.5.4-5z" fill="url(#${u}flame)"/></g>`},ct=0,ht={ice:"ice",magma:"heat",glass:"glass",metal:"metal",noise:"noise"},kt=(t,l,d,e,r,s,i)=>{if(pt.has(t))return`<path d="${l}" fill="${yt(t,i)}"/>`;let y=c&&ht[t]?Qe(ht[t],r,s):null;if(!y){if(t==="noise")return`<path d="${l}" fill="${yt("grain",i)}"/>`;if(t==="glass")return`<path d="${l}" fill="#fff" fill-opacity="${a.dark?0.12:0.4}"/>`;if(t==="metal")return`<path d="${l}" fill="${Pt()}"/>`;return""}let p=`m-${t}-${Math.round(r)}x${Math.round(s)}`;if(!v.has(p))v.set(p,Z`<image id="${u}${p}" width="${Math.round(r)}" height="${Math.round(s)}" preserveAspectRatio="none" href="${y}"/>`);let f=`${u}k${ct++}`;return`<clipPath id="${f}"><path d="${l}"/></clipPath><g clip-path="url(#${f})">`+Z`<use href="#${u}${p}" x="${d}" y="${e}"/></g>`},pt=new Set(["quantized","sparse","hatch"]),yt=(t,l)=>{let d=`p-${t}-${I(l)}`;if(!v.has(d)){let[e,r]=_(l),s=24,i="",y="";if(t==="quantized")s=32,[0,2,1,3,2,0,3,1,1,3,0,2,3,1,2,0].forEach((p,f)=>{i+=`<rect x="${f%4*8}" y="${(f>>2)*8}" width="8" height="8" fill="${ut(e,r,p/3*0.7)}"/>`});else if(t==="sparse")[1,0,1,1,0,1,0,1,1,1,0,0,1,0,1,1].forEach((p,f)=>{if(p)i+=`<circle cx="${f%4*6+3}" cy="${(f>>2)*6+3}" r="1.5" fill="${r}" fill-opacity=".75"/>`});else if(t==="hatch")s=7,y=' patternTransform="rotate(45)"',i=`<path d="M0 0V7" stroke="${r}" stroke-opacity=".55" stroke-width="1.4"/>`;else{s=24;for(let p=0;p<64;p++)i+=`<rect x="${p%8*3}" y="${(p>>3)*3}" width="3" height="3" fill="${["#000","#fff"][p*37%7>3?1:0]}" fill-opacity="${0.08+p*53%11/40}"/>`}v.set(d,`<pattern id="${u}${d}" width="${s}" height="${s}" patternUnits="userSpaceOnUse"${y}>${i}</pattern>`)}return`url(#${u}${d})`},Gt=(t,l,d)=>{if(pt.has(t))return`<path d="${l}" fill="${yt(t,d)}"/>`;if(t==="noise")return`<path d="${l}" fill="${yt("grain",d)}"/>`;if(t==="ice")return W(),`<path d="${l}" fill="url(#${u}ice)"/><path d="${l}" fill="url(#${u}frost)"/>`;if(t==="magma")return lt(),`<path d="${l}" fill="url(#${u}fire)"/>`;if(t==="metal")return`<path d="${l}" fill="${Pt()}"/>`;if(t==="glass")return`<path d="${l}" fill="#fff" fill-opacity="${a.dark?0.15:0.45}"/>`;return""},Pt=()=>{if(!v.has("metal"))v.set("metal",`<linearGradient id="${u}metal" x1="0" y1="0" x2="1" y2="1"><stop offset="0" stop-color="#e4e7eb"/><stop offset=".45" stop-color="#a7adb5"/><stop offset=".55" stop-color="#cfd3d8"/><stop offset="1" stop-color="#8d949d"/></linearGradient>`);return`url(#${u}metal)`},Yt=(t,l,d,e)=>{let r=t+e,s=t+d-e;if(s-r<8)return"";let i=Z`M${r},${l+1}H${s}V${l+3.5}`,y=Math.max(2,Math.round((s-r)/9)),p=(s-r)/y;for(let f=0;f<y;f++)i+=Z`q${-p/2},${[4,7,3.5,6,5][f%5]} ${-p},0`;return`<path d="${i}z" fill="#fff" fill-opacity="${a.dark?0.55:0.92}"/>`},Dt=(t)=>{let[l,d]=dt(t.attrs.size,[64,64]),e=+t.attrs.n||1;return{W:l,H:d,k:e,x0:t.x-(l+(e-1)*6)/2,y0:t.y-(d+(e-1)*6)/2,fit:t.attrs.fit==="contain"?"xMidYMid meet":"xMidYMid slice",srcs:t.attrs.srcs?JSON.parse(t.attrs.srcs):[t.attrs.src??""]}},Rt=(t)=>{let l=o.edges.find((d)=>d.to===t.id&&o.nodes.get(d.from)?.shape==="image");return l&&o.nodes.get(l.from)},pe=(t)=>{let l=o.nodes.get(t.to);return l.shape==="magnify"&&Rt(l)?.id===t.from},Lt=(t,l)=>{if(t.attrs.region)return dt(t.attrs.region,[]);let d=+l.attrs.grid||4,[e,r]=dt(t.attrs.patch,[-1,-1]);return e>=0?[e/d,r/d,1/d,1/d]:[0.375,0.375,0.25,0.25]},Xt=(t)=>{let l=t.x-t.w/2,d=t.y-t.h/2,{fill:e,stroke:r,fx:s}=wt(t),i=t.flags.has("dashed")?' stroke-dasharray="4 3"':"",y=t.flags.has("bold")?2:1.2,p=`fill="${e}" stroke="${r}" stroke-width="${y}"${i}`,f=+(t.attrs.ghost??t.attrs.heads??0),S=!1,E=(b,k=[l,d,t.w,t.h],G=0)=>{let N="";for(let L=Math.min(f,6)-1;L>0;L--)N+=Z`<path d="${b}" transform="translate(${L*5},${-L*5})" ${p} opacity="${0.35+0.5/(L+1)}"/>`;if(!a.dark||s)N+=Z`<path d="${b}" transform="translate(0,1.6)" fill="${a.shadow}"/>`;N+=`<path d="${b}" ${p}/>`;let T=kt(s,b,k[0],k[1],k[2],k[3],t.color);if(T)S=!0,N+=T+`<path d="${b}" fill="none" stroke="${r}" stroke-width="${y}" stroke-opacity=".7"${i}/>`;else if(s==="ice")N+=`<path d="${b}" fill="url(#${u}frost)"/>`+(G?Yt(k[0],k[1],k[2],G):"");return V(l,d-f*5,t.w+f*5,t.h+f*5),N},m="",w=$e(t,x),z=t.attrs.fontFamily||g,[P,C]=s==="metal"?["#1d2430","#e8ebee"]:[a.text,a.halo],Y=()=>S&&s==="magma"?`<g paint-order="stroke" stroke="#1a0d07" stroke-width="3" stroke-linejoin="round">${ot(t.label,t.x,t.y,w,600,"#fff4e8","middle",z)}</g>`:S&&s!=="ice"&&s!=="glass"?`<g paint-order="stroke" stroke="${C}" stroke-width="3" stroke-linejoin="round" stroke-opacity=".85">${ot(t.label,t.x,t.y,w,600,P,"middle",z)}</g>`:ot(t.label,t.x,t.y,w,500,a.text,"middle",z);switch(t.shape){case"text":m=Y();break;case"empty":break;case"op":{m=E(ke(t.x,t.y,11),[t.x-11,t.y-11,22,22]);let b=t.label==="+"?Z`M${t.x-6.5},${t.y}h13M${t.x},${t.y-6.5}v13`:/^[*x×]$/.test(t.label)?Z`M${t.x-4.6},${t.y-4.6}l9.2,9.2M${t.x+4.6},${t.y-4.6}l-9.2,9.2`:t.label==="~"?Z`M${t.x-7},${t.y}q3.5,-8 7,0t7,0`:"";m+=b?`<path d="${b}" fill="none" stroke="${r}" stroke-width="1.5" stroke-linecap="round"/>`:ot(t.label,t.x,t.y,w,500,a.text,"middle",z);break}case"circle":m=E(ke(t.x,t.y,t.w/2),[l,d,t.w,t.h])+Y();break;case"tensor":{let[b,k,G]=dt(t.attrs.size,[36,64,20]),N=G*0.6,T=l,L=d+N,[Q,j]=_(t.color),M=s==="ice"?a.ice[0]:a.dark?ut(Q,j,0.3):ut(Q,"#fff",0.5),A=s==="ice"?a.ice[2]:a.dark?ut(Q,"#000",0.25):ut(Q,j,0.35);m=E(Tt(T,L,b,k,1.5),[T,L,b,k])+`<path d="${Vt(T,L,T+N,L-N,T+b+N,L-N,T+b,L)}" fill="${M}" stroke="${r}" stroke-width="1" stroke-linejoin="round"/><path d="${Vt(T+b,L,T+b+N,L-N,T+b+N,L+k-N,T+b,L+k)}" fill="${A}" stroke="${r}" stroke-width="1" stroke-linejoin="round"/>`;break}case"stack":{let[b,k]=dt(t.attrs.size,[44,60]),G=+t.attrs.n||4,N=+t.attrs.offset||7;for(let T=0;T<G;T++){let L=Tt(l+T*N,d+T*N,b,k,2);m+=T===G-1?E(L,[l+T*N,d+T*N,b,k]):`<path d="${L}" ${p}/>`}V(l,d,t.w,t.h);break}case"enc":case"dec":{let b=t.shape==="enc",G=h?b!==_t()?Vt(l,d,l+t.w,d+t.h*0.22,l+t.w,d+t.h*0.78,l,d+t.h):Vt(l,d+t.h*0.22,l+t.w,d,l+t.w,d+t.h,l,d+t.h*0.78):b!==Ft()?Vt(l,d,l+t.w,d,l+t.w*0.78,d+t.h,l+t.w*0.22,d+t.h):Vt(l+t.w*0.22,d,l+t.w*0.78,d,l+t.w,d+t.h,l,d+t.h);m=E(G)+Y();break}case"neurons":{let b=+t.attrs.n||4;for(let k=0;k<b;k++){let G=-((b-1)/2)*20+k*20;{let N=h?t.x:t.x+G,T=h?t.y+G:t.y;m+=E(ke(N,T,7),[N-7,T-7,14,14])}}break}case"matrix":case"voxels":{let b=+t.attrs.cellSize||16,k=[];if(t.shape==="matrix")JSON.parse(t.attrs.grid).forEach((Q,j)=>Q.forEach((M,A)=>k.push([[[l+A*b,d+j*b],[l+(A+1)*b,d+j*b],[l+(A+1)*b,d+(j+1)*b],[l+A*b,d+(j+1)*b]],M,0])));else{let Q=JSON.parse(t.attrs.faces),j=+t.attrs.cols,M=b*0.5,A=l,K=d+ +t.attrs.depth*M,et=(F,D)=>[A+F*b+D*M,K-D*M],B=(F,D)=>[A+j*b+D*M,K+F*b-D*M];Q.top.forEach((F,D)=>F.forEach((at,nt)=>k.push([[et(nt,D),et(nt+1,D),et(nt+1,D+1),et(nt,D+1)],at,0.3]))),Q.side.forEach((F,D)=>F.forEach((at,nt)=>k.push([[B(D,nt),B(D,nt+1),B(D+1,nt+1),B(D+1,nt)],at,-0.25]))),Q.front.forEach((F,D)=>F.forEach((at,nt)=>k.push([[[A+nt*b,K+D*b],[A+(nt+1)*b,K+D*b],[A+(nt+1)*b,K+(D+1)*b],[A+nt*b,K+(D+1)*b]],at,0])))}let[G,N]=_(t.color),T=a.dark?"#0e1117":"#ffffff";for(let[Q,j,M]of k){let A=j.c?a.c[j.c]?ut(a.c[j.c][0],a.c[j.c][1],0.55):Ot(j.c):ut(a.dark?N:ut(G,"#ffffff",0.4),a.dark?G:N,a.dark?1-(j.v??0.5):j.v??0.5);A=M>0?ut(A,"#ffffff",M):M<0?ut(A,"#0b1a2e",-M):A;let K="M"+Q.map((F)=>F.map(xt).join(",")).join("L")+"z";if(m+=`<path d="${K}" fill="${A}" stroke="${T}" stroke-width="1.5" stroke-linejoin="round"/>`,j.t)m+=Gt(j.t,K,j.c??t.color);let[et,B]=Q.reduce((F,D)=>[F[0]+D[0]/4,F[1]+D[1]/4],[0,0]);if(j.l)m+=ot(j.l,et,B,Math.min(x-2,b*0.6),500,a.text);if(j.x)m+=jt(et-b*0.28,B-b*0.28,et+b*0.28,B+b*0.28,1.4)}let L=JSON.parse(t.attrs.axes??"{}");if(t.attrs.axes){let Q=+t.attrs.rows,j=+t.attrs.cols,M=t.shape==="voxels"?+t.attrs.depth*b*0.5:0,A=l,K=d+M,et=j*b,B=Q*b,F=x-1,D=o.edges.some((gt)=>gt.from===t.id||gt.to===t.id),at=K+B/2,nt=A+et/2;if(D&&h&&Math.abs(at-t.y)<F*0.85)at=t.y+F*0.85;let J=L.cols?bt(L.cols,F,500,g).w:0;if(D&&!h&&Math.abs(nt-t.x)<J/2+6)nt=t.x+J/2+6;let rt="";if(L.cols)rt+=ot(L.cols,nt,K+B+F*0.9,F,500,a.sub);if(L.rows)rt+=ot(L.rows,A-5,at,F,500,a.sub,"end");if(L.depth&&M)rt+=ot(L.depth,A+et+M/2+5,K+B-M/2+F*0.5,F,500,a.sub,"start");m+=`<g paint-order="stroke" stroke="${a.halo}" stroke-width="3" stroke-linejoin="round">${rt}</g>`}V(l,d,t.w,t.h);break}case"vector":{let b=Ce(t);m=E(Tt(l,d,t.w,t.h,3));let k=(h?t.h-4:t.w-4)/b.length;b.forEach((G,N)=>{let T=-((b.length-1)/2)*k+N*k;if(N)m+=h?Z`<path d="M${l+3},${t.y+T-k/2}h${t.w-6}" stroke="${r}" stroke-opacity=".5"/>`:Z`<path d="M${t.x+T-k/2},${d+3}v${t.h-6}" stroke="${r}" stroke-opacity=".5"/>`;m+=ot(G.trim(),h?t.x:t.x+T,h?t.y+T:t.y,w,500,a.text,"middle",z)});break}case"image":{let{W:b,H:k,k:G,x0:N,y0:T,fit:L,srcs:Q}=Dt(t),j=+t.attrs.grid||0;for(let B=0;B<G;B++){let F=N+B*6,D=T+B*6,at=Tt(F,D,b,k,3),nt=Q.length>1?Q[G-1-B]:B===G-1?Q[0]:void 0;if(!nt){m+=`<path d="${at}" fill="${a.dark?"#1b2029":"#fff"}" stroke="${r}"/>`;continue}let J=`${u}k${ct++}`;if(m+=`<clipPath id="${J}"><path d="${at}"/></clipPath>`+Z`<image href="${Ot(nt)}" x="${F}" y="${D}" width="${b}" height="${k}" preserveAspectRatio="${L}" clip-path="url(#${J})"/>`,j>1&&B===G-1){let rt="";for(let gt=1;gt<j;gt++)rt+=Z`M${F+b*gt/j},${D}v${k}M${F},${D+k*gt/j}h${b}`;m+=`<path d="${rt}" stroke="#fff" stroke-opacity=".9"/>`}m+=`<path d="${at}" fill="none" stroke="${r}"/>`}let[M,A,K,et]=we(t.attrs);if(M!==1||A!==0||K!==0||et!==1)m=Z`<g transform="matrix(${[M,A,K,et].map((B)=>B.toFixed(4)).join(" ")} ${t.x-M*t.x-K*t.y} ${t.y-A*t.x-et*t.y})">`+m+"</g>";V(l,d,t.w,t.h);break}case"magnify":{let b=Rt(t),k=Math.min(t.w,t.h)/2,G=t.attrs.lens!=="square",N=G?ke(t.x,t.y,k):Tt(l,d,t.w,t.h,4),T=`${u}k${ct++}`;if(m+=`<clipPath id="${T}"><path d="${N}"/></clipPath>`,t.attrs.src)m+=Z`<image href="${Ot(t.attrs.src)}" x="${l}" y="${d}" width="${t.w}" height="${t.h}" preserveAspectRatio="${t.attrs.fit==="contain"?"xMidYMid meet":"xMidYMid slice"}" clip-path="url(#${T})"/>`;else if(b){let L=Dt(b),[Q,j,M,A]=Lt(t,b);m+=Z`<g clip-path="url(#${T})"><svg x="${l}" y="${d}" width="${t.w}" height="${t.h}" viewBox="${Q*L.W} ${j*L.H} ${M*L.W} ${A*L.H}" preserveAspectRatio="xMidYMid slice">`+Z`<image href="${Ot(L.srcs[0])}" width="${L.W}" height="${L.H}" preserveAspectRatio="${L.fit}"/></svg></g>`}else m+=`<path d="${N}" fill="${e}"/>`;if(m+=`<path d="${N}" fill="none" stroke="${r}" stroke-width="2.5"/>`,G){let L=k*Math.SQRT1_2;m+=Z`<path d="M${t.x+L+1},${t.y+L+1}l${k*0.5},${k*0.5}" stroke="${r}" stroke-width="5" stroke-linecap="round"/>`,V(t.x+L,t.y+L,k*0.6,k*0.6)}V(l,d,t.w,t.h);break}case"gaussian":{let b=JSON.parse(t.attrs.peaks??"[[0,0,1,0.45]]"),k=Math.min(40,+t.attrs.mesh||16),[G,N]=_(t.color),T=(B,F)=>b.reduce((D,[at,nt,J=1,rt=0.45])=>D+J*Math.exp(-((B-at)**2+(F-nt)**2)/(2*rt*rt)),0),L=Array.from({length:k+1},(B,F)=>Array.from({length:k+1},(D,at)=>T(-1+2*F/k,-1+2*at/k))),Q=Math.max(...L.flat())||1,j=L.map((B,F)=>B.map((D,at)=>mt(-1+2*F/k,-1+2*at/k,D/Q))),M=[[-1,-1],[1,-1],[1,1],[-1,1]].map(([B,F])=>mt(B,F,0)),A=Te([...j.flat(),...M],l,d,t.w,t.h,2),K=(B)=>A(B).map(xt).join(",");m+=`<path d="M${M.map(K).join("L")}z" fill="${G}" fill-opacity=".5" stroke="${N}" stroke-opacity=".4"/>`;let et=[];for(let B=0;B<k;B++)for(let F=0;F<k;F++){let D=[j[B][F],j[B+1][F],j[B+1][F+1],j[B][F+1]];et.push([D.reduce((at,nt)=>at+nt[2],0),`M${D.map(K).join("L")}z`,(L[B][F]+L[B+1][F+1])/2/Q])}et.sort((B,F)=>F[0]-B[0]);for(let[,B,F]of et){let D=ut(G,N,0.12+0.88*F);m+=`<path d="${B}" fill="${D}" stroke="${ut(D,a.dark?"#ffffff":N,0.35)}" stroke-width=".5"/>`}V(l,d,t.w,t.h);break}case"space":{let[b,k]=_(t.color),G=JSON.parse(t.attrs.points??"[]"),N=Object.values({x:"x",y:"y",z:"z",...JSON.parse(t.attrs.axes??"{}")}),T=mt(1,0,0)[2]>mt(-1,0,0)[2]?1:-1,L=mt(0,1,0)[2]>mt(0,-1,0)[2]?1:-1,Q=[-1,1].flatMap((J)=>[-1,1].flatMap((rt)=>[-1,1].map((gt)=>mt(J,rt,gt)))),j=Te(Q,l,d,t.w,t.h,10),M=(J,rt,gt)=>j(mt(J,rt,gt)).map(xt).join(","),A=(J)=>`<path d="M${J.map((rt)=>M(rt[0],rt[1],rt[2])).join("L")}z" fill="${b}" fill-opacity=".55" stroke="${k}" stroke-opacity=".45" stroke-linejoin="round"/>`;m+=A([[-1,-1,-1],[1,-1,-1],[1,1,-1],[-1,1,-1]])+A([[T,-1,-1],[T,1,-1],[T,1,1],[T,-1,1]])+A([[-1,L,-1],[1,L,-1],[1,L,1],[-1,L,1]]);let K="";for(let J of[-0.5,0,0.5])K+=`M${M(J,-1,-1)}L${M(J,1,-1)}M${M(-1,J,-1)}L${M(1,J,-1)}M${M(T,-1,J)}L${M(T,1,J)}M${M(-1,L,J)}L${M(1,L,J)}`;m+=`<path d="${K}" stroke="${k}" stroke-opacity=".18"/>`;let et=G.map((J)=>[...J,mt(J[0],J[1],J[2])[2]]).sort((J,rt)=>rt[rt.length-1]-J[J.length-1]);for(let J of et){let[rt,gt]=j(mt(J[0],J[1],J[2])),Re=a.c[Ie(J.length>4?J[3]:0)][1];m+=Z`<circle cx="${rt}" cy="${gt}" r="3.2" fill="${Re}" stroke="${a.dark?"#0e1117":"#fff"}" stroke-width=".9"/>`}let B=(J,rt,gt,Re)=>{let[uo,po]=j(mt(rt,gt,Re));return ot(J,uo,po,x-2,400,a.sub)},[F,D]=[[T,-L],[-T,L]].sort((J,rt)=>j(mt(J[0],J[1],0))[0]-j(mt(rt[0],rt[1],0))[0])[0],[at,nt]=j(mt(F,D,0));m+=B(N[0],0,-L*1.22,-1)+B(N[1],-T*1.22,0,-1)+ot(N[2],at-8,nt,x-2,400,a.sub,"end"),V(l,d,t.w,t.h);break}case"pointcloud":{let[b,k]=_(t.color),N=(t.attrs.points?JSON.parse(t.attrs.points):No(t.attrs.shape??"torus",+t.attrs.n||240)).map((M)=>[...mt(M[0],M[1],M[2]),M[3]]),T=Te(N,l,d,t.w,t.h,4),L=N.map((M)=>M[2]),Q=Math.min(...L),j=Math.max(...L);N.sort((M,A)=>A[2]-M[2]);for(let M of N){let A=(j-M[2])/(j-Q||1),[K,et]=T(M),B=M[3]!==void 0?a.c[Ie(M[3])][1]:ut(b,k,0.3+0.7*A);m+=Z`<circle cx="${K}" cy="${et}" r="${1+1.4*A}" fill="${B}" fill-opacity="${0.5+0.5*A}"/>`}V(l,d,t.w,t.h);break}case"waveform":{m=E(Tt(l,d,t.w,t.h,6));let b=t.attrs.wave?JSON.parse(t.attrs.wave):Ao(72),k=6,G=t.y,N=t.h/2-k,T=(t.w-2*k)/b.length,L="";b.forEach(([Q,j],M)=>{let A=l+k+T*(M+0.5);L+=Z`M${A},${G-Math.max(j,0.02)*N}V${G-Math.min(Q,-0.02)*N}`}),m+=`<path d="${L}" stroke="${r}" stroke-width="${xt(Math.max(0.8,T*0.6))}" stroke-linecap="round"/>`;break}case"sphere":{let[b,k]=_(t.color),G=Math.min(t.w,t.h)/2-2,N="sph-"+I(t.color);if(!v.has(N))v.set(N,`<radialGradient id="${u}${N}" cx=".35" cy=".3" r=".8"><stop offset="0" stop-color="${a.dark?ut(b,k,0.35):"#ffffff"}"/><stop offset=".55" stop-color="${b}"/><stop offset="1" stop-color="${ut(b,k,0.35)}"/></radialGradient>`);m+=Z`<circle cx="${t.x}" cy="${t.y}" r="${G}" fill="url(#${u}${N})" stroke="${k}"/>`;let T="",L="",Q=(M)=>{let A;for(let K=0;K<=64;K++){let[et,B,F]=mt(...M(K/64*Math.PI*2)),D=F<=0,at=(D===A?"L":"M")+xt(t.x+et*G)+","+xt(t.y+B*G);if(D)T+=at;else L+=at;A=D}};for(let M of[-0.5,0,0.5]){let A=Math.sqrt(1-M*M);Q((K)=>[A*Math.cos(K),A*Math.sin(K),M])}for(let M of[0,Math.PI/3,2*Math.PI/3])Q((A)=>[Math.cos(A)*Math.cos(M),Math.cos(A)*Math.sin(M),Math.sin(A)]);m+=`<path d="${L}" fill="none" stroke="${k}" stroke-opacity=".22" stroke-dasharray="2 2"/><path d="${T}" fill="none" stroke="${k}" stroke-opacity=".55"/>`;let j=JSON.parse(t.attrs.points??"[]");for(let M of j){let A=Math.hypot(M[0],M[1],M[2])||1,[K,et,B]=mt(M[0]/A,M[1]/A,M[2]/A),F=a.c[Ie(M[3]??0)][1];m+=Z`<circle cx="${t.x+K*G}" cy="${t.y+et*G}" r="3" fill="${F}" stroke="${a.dark?"#0e1117":"#fff"}" stroke-width=".8" opacity="${B<=0?1:0.3}"/>`}V(l,d,t.w,t.h);break}default:{let b=t.shape==="thin"?5:7;m=E(Tt(l,d,t.w,t.h,b),void 0,b)+Y()}}if(ve.has(t.shape)&&t.label)m+=ot(t.label,t.x,d-f*5-11,w,500,a.sub,"middle",z);if(t.attrs.caption)m+=ot(t.attrs.caption,t.x,t.y+t.h/2+12,x-1,400,a.sub);let st=H(t,s);if(st){let b=t.shape==="op"||t.shape==="circle";m+=b?it(st,t.x+t.w*0.36,t.y-t.h*0.36):it(st,l+t.w-2,d+2)}if(t.flags.has("crossed")){let b=t.shape==="op"||t.shape==="circle"?t.w*0.15:3;m=`<g opacity=".5">${m}</g>`+jt(l+b,d+b,l+t.w-b,d+t.h-b,2.2)}return`<g>${m}</g>`},jt=(t,l,d,e,r)=>{let s=Z`M${t},${l}L${d},${e}M${d},${l}L${t},${e}`,i=a.c.red[1];return`<path d="${s}" stroke="${a.halo}" stroke-width="${r+2.5}" stroke-linecap="round"/><path d="${s}" stroke="${i}" stroke-width="${r}" stroke-linecap="round"/>`},_t=()=>o.dir==="RL",Ft=()=>o.dir==="BT",Bt=(t)=>{if(pe(t))return"";let l=t.attrs.color,d=!l?a.edge:a.c[l]||Oe(l)?_(l)[1]:Ot(l);if(t.fc)return Kt(t,d);let e=t.pts.map((m)=>[...m]);if(e.length<2)return"";let r=t.op==="=>"||t.flags.has("thick"),s=t.op.startsWith("..")||t.flags.has("dashed"),i=t.op==="--"||t.op===".."?0:t.op==="<->"?2:1,y=r?9:7.5,p=r?4.2:3.4,f="",S=(m,w)=>{let z=m[0]-w[0],P=m[1]-w[1],C=Math.hypot(z,P)||1,Y=z/C,st=P/C,b=m[0]-Y*y,k=m[1]-st*y;return f+=`<path d="${Vt(m[0],m[1],b-st*p,k+Y*p,m[0]-Y*y*0.72,m[1]-st*y*0.72,b+st*p,k-Y*p)}" fill="${d}"/>`,[m[0]-Y*y*0.7,m[1]-st*y*0.7]};for(let m of e)V(m[0]-2,m[1]-2,4,4);let E;if(t.curve){let[m,w,z,P]=e,C=t.curve,Y=[m[0]+(w[0]-m[0])*C,m[1]+(w[1]-m[1])*C],st=[P[0]+(z[0]-P[0])*C,P[1]+(z[1]-P[1])*C],b=i?S(P,st):P,k=i===2?S(m,Y):m;E=Z`M${k[0]},${k[1]}C${Y[0]},${Y[1]} ${st[0]},${st[1]} ${b[0]},${b[1]}`}else{if(i)e[e.length-1]=S(e[e.length-1],e[e.length-2]);if(i===2)e[0]=S(e[0],e[1]);E=Z`M${e[0][0]},${e[0][1]}`;for(let m=1;m<e.length-1;m++){let[w,z,P]=[e[m-1],e[m],e[m+1]],C=Math.hypot(z[0]-w[0],z[1]-w[1]),Y=Math.hypot(P[0]-z[0],P[1]-z[1]),st=Math.min(8,C/2,Y/2);E+=Z`L${z[0]+(w[0]-z[0])*st/(C||1)},${z[1]+(w[1]-z[1])*st/(C||1)}Q${z[0]},${z[1]} ${z[0]+(P[0]-z[0])*st/(Y||1)},${z[1]+(P[1]-z[1])*st/(Y||1)}`}E+=Z`L${e[e.length-1][0]},${e[e.length-1][1]}`}if(t.flags.has("crossed")){let m=0,w=-1;for(let C=0;C+1<t.pts.length;C++){let Y=Math.hypot(t.pts[C+1][0]-t.pts[C][0],t.pts[C+1][1]-t.pts[C][1]);if(Y>w)w=Y,m=C}let z=(t.pts[m][0]+t.pts[m+1][0])/2,P=(t.pts[m][1]+t.pts[m+1][1])/2;f+=jt(z-5,P-5,z+5,P+5,2)}return Z`<path d="${E}" fill="none" stroke="${d}" stroke-width="${r?2.2:1.4}" stroke-linecap="round" stroke-linejoin="round"${s?' stroke-dasharray="5 4"':""}/>`+f},Ht=(t)=>{let l=+t.attrs.n||4;return Array.from({length:l},(d,e)=>{let r=-((l-1)/2)*20+e*20;return h?[t.x,t.y+r]:[t.x+r,t.y]})},Kt=(t,l)=>{let d="";for(let[e,r]of Ht(Qt(t.from)))for(let[s,i]of Ht(Qt(t.to))){let y=Math.hypot(s-e,i-r),p=(s-e)/y,f=(i-r)/y;d+=Z`M${e+p*7},${r+f*7}L${s-p*7},${i-f*7}`}return`<path d="${d}" stroke="${l}" stroke-opacity=".45" stroke-width=".8"/>`},Qt=(t)=>o.nodes.get(t),te=(t)=>{if(!t.w)return"";let{fill:l,stroke:d,fx:e}=t.attrs.texture||t.flags.has("frozen")||t.flags.has("train")?wt(t):{fill:t.attrs.color?_(t.attrs.color)[0]:a.group[0],stroke:t.attrs.color?_(t.attrs.color)[1]:a.group[1],fx:""},r=t.flags.has("dashed")?' stroke-dasharray="5 4"':"",s=Tt(t.x,t.y,t.w,t.h,12),i=`<path d="${s}" fill="${l}" stroke="${d}" stroke-width="1.2"${r}/>`,y=kt(e,s,t.x,t.y,t.w,t.h,t.attrs.color);if(y)i+=y+`<path d="${s}" fill="none" stroke="${d}" stroke-width="1.2" stroke-opacity=".7"/>`;else if(e==="ice")i+=`<path d="${s}" fill="url(#${u}frost)"/>`+Yt(t.x,t.y,t.w,12);if(V(t.x,t.y,t.w,t.h),t.label)i+=ot(t.label,t.x+12,t.y+13,x-2,600,a.sub,"start");if(t.attrs.repeat){let[p,f,S]=qt(t);i+=ot(t.attrs.repeat,p,f,x+2,500,a.text,S)}return i+=it(H(t,e),t.x+t.w-2,t.y+2),i},$t=[...o.nodes.values()],ee=$t.reduce((t,l)=>t+l.x,0)/($t.length||1),he=[],oe=(t)=>{let l=new Set;for(let e=t;e;e=o.groups.find((r)=>r.id===e.parent))l.add(e.id);let d=$t.map((e)=>[e.x-e.w/2,e.y-e.h/2,e.x+e.w/2,e.y+e.h/2+(e.attrs.caption?20:0)]);for(let e of o.edges){for(let r=0;r+1<e.pts.length;r++){let[s,i]=[e.pts[r],e.pts[r+1]];d.push([Math.min(s[0],i[0])-2,Math.min(s[1],i[1])-2,Math.max(s[0],i[0])+2,Math.max(s[1],i[1])+2])}if(e.label&&e.lx!==void 0){let r=bt(e.label,x-1,At,g);d.push([e.lx-r.w/2,e.ly-r.h/2,e.lx+r.w/2,e.ly+r.h/2])}}for(let e of o.groups)if(e.w&&!l.has(e.id)&&e.parent!==t.id)d.push([e.x,e.y,e.x+e.w,e.y+e.h]);return[...d,...he]},qt=(t)=>{let l=bt(t.attrs.repeat,x+2,500,g),e={left:(f=0.5)=>[t.x-10,t.y+t.h*f,"end"],right:(f=0.5)=>[t.x+t.w+10,t.y+t.h*f,"start"],top:()=>[t.x+t.w/2,t.y-10-l.h/2,"middle"],bottom:()=>[t.x+t.w/2,t.y+t.h+10+l.h/2,"middle"]},r=([f,S,E])=>[E==="end"?f-l.w:E==="middle"?f-l.w/2:f,S-l.h/2,E==="start"?f+l.w:E==="middle"?f+l.w/2:f,S+l.h/2],s=t.x+t.w/2>ee?"right":"left",i=s==="right"?"left":"right",y=t.attrs["repeat-side"],p=y&&y!=="auto"?e[y]():void 0;if(!p){let f=[e[s](),e.bottom(),e[i](),e.top(),...[0.25,0.75].flatMap((E)=>[e[s](E),e[i](E)])],S=oe(t);p=f.find((E)=>!S.some((m)=>ye(r(E),m)))??f[0]}return he.push(r(p)),p},re=$t.filter((t)=>t.shape==="magnify").map((t)=>{let l=Rt(t);if(!l)return"";let d=Dt(l),[e,r,s,i]=Lt(t,l),[y,p,f,S]=we(l.attrs),E=d.x0+(d.k-1)*6,m=d.y0+(d.k-1)*6,w=(M,A)=>{let K=E+M*d.W-l.x,et=m+A*d.H-l.y;return[l.x+y*K+f*et,l.y+p*K+S*et]},z=[w(e,r),w(e+s,r),w(e+s,r+i),w(e,r+i)],[P,C]=z.reduce((M,A)=>[M[0]+A[0]/4,M[1]+A[1]/4],[0,0]),Y=Math.hypot(t.x-P,t.y-C)||1,st=-(t.y-C)/Y,b=(t.x-P)/Y,k=(M)=>M[0]*st+M[1]*b,G=[...z].sort((M,A)=>k(M)-k(A)),N=Math.min(t.w,t.h)/2,T=t.attrs.lens==="square"?[[t.x-t.w/2,t.y-t.h/2],[t.x+t.w/2,t.y-t.h/2],[t.x+t.w/2,t.y+t.h/2],[t.x-t.w/2,t.y+t.h/2]].sort((M,A)=>k(M)-k(A)):[[t.x-st*N,t.y-b*N],[t.x+st*N,t.y+b*N]],[L,Q]=[T[0],T[T.length-1]],j=_(t.color)[1];return`<path d="M${z.map((M)=>M.map(xt).join(",")).join("L")}z" fill="none" stroke="${j}" stroke-width="2" stroke-linejoin="round"/><path d="M${G[0].map(xt)}L${L.map(xt)}M${G[3].map(xt)}L${Q.map(xt)}" stroke="${j}" stroke-opacity=".75" stroke-dasharray="3 3"/>`}).join(""),vt=["frozen","trainable","glass","metal","noise","quantized","sparse","hatch","crossed"],se=({position:t,labels:l={}})=>{let d=new Map,e=(w,z)=>{if(!d.has(w))d.set(w,z)};for(let w of[...$t,...o.groups]){let z=w.attrs.texture??(w.flags.has("frozen")?"ice":w.flags.has("train")?"magma":""),P=H(w,z);if(P)e(P==="ice"?"frozen":"trainable");if(z&&z!=="ice"&&z!=="magma")e(z,w.color??w.attrs.color);if(w.flags.has("crossed"))e("crossed")}if(o.edges.some((w)=>w.flags.has("crossed")))e("crossed");let r=vt.filter((w)=>d.has(w)).map((w)=>[w,l[w]??(w==="crossed"?"Removed":w[0].toUpperCase()+w.slice(1))]);if(!r.length)return"";let s=30,i=18,y=x-1,p=t==="right",f=r.map(([,w])=>bt(w,y,At,g).w),S=p?U+28:(q+U)/2-f.reduce((w,z)=>w+s+12+z+20,-20)/2,E=p?O+i/2+6:X+22+i/2,m="";return r.forEach(([w,z],P)=>{let C=w==="frozen"?"frozen":w==="trainable"?"train":w==="crossed"?"crossed":"";if(m+=Xt({id:"",shape:"block",label:"",color:d.get(w),flags:new Set(C?[C]:[]),attrs:C?{}:{texture:w},groups:[],order:0,x:S+s/2,y:E,w:s,h:i})+ot(z,S+s+12,E,y,At,a.text,"start"),p)E+=i+14;else S+=s+12+f[P]+20}),m},de=o.groups.map(te).join(""),me=o.edges.map(Bt).join(""),ge=[...o.nodes.values()].map(Xt).join(""),Mt=o.edges.filter((t)=>t.label&&t.lx!==void 0).map((t)=>ot(t.label,t.lx,t.ly,x-1,400,a.sub)).join(""),be=o.legend?se(o.legend):"";if(q===1/0)q=O=U=X=0;let St=14,zt=U-q+St*2,Wt=X-O+St*2,Nt=a.bg!=="none"?Z`<rect x="${q-St}" y="${O-St}" width="${zt}" height="${Wt}" rx="10" fill="${a.bg}"/>`:"";return Z`<svg xmlns="http://www.w3.org/2000/svg" viewBox="${q-St} ${O-St} ${zt} ${Wt}" width="${zt}" height="${Wt}" font-family="${Ot(g)}" style="max-width:100%;height:auto" role="img">`+`<defs>${[...v.values()].join("")}</defs>${Nt}<g>${de}</g><g>${me}</g><g>${ge}</g><g>${re}</g><g paint-order="stroke" stroke="${a.halo}" stroke-width="4" stroke-linejoin="round">${Mt}</g>${be}</svg>`}class Fe{nodes=new Map;edges=[];groups=new Map;scope=[];ranks=[];ids=new Set;claim(o,n,c){if(o===void 0)while(this.ids.has(o=`${n}${c}`))c++;else if(this.ids.has(o))throw Error(`duplicate id "${o}"`);return this.ids.add(o),o}node(o){let n=this.nodes.get(o);if(!n){let c=this.claim(o.options.id,"n",this.nodes.size),a=this.scope.map((u)=>this.groups.get(u)?.id).filter((u)=>!!u);n={id:c,mods:[...this.scope],groups:a},this.nodes.set(o,n)}return n.id}apply(o,n){let c=this.node(o);for(let a of n)this.edges.push({from:a.node,to:o,style:a.style});return new Et(this,c)}enter(o){let n=o.options;if((n.box??!!(n.label||n.repeat))&&!this.groups.has(o)){let c=[...this.scope].reverse().map((a)=>this.groups.get(a)).find(Boolean);this.groups.set(o,{id:this.claim(n.id,"g",this.groups.size),label:n.label??"",parent:c?.id,flags:new Set(n.dashed?["dashed"]:[]),attrs:{...n.repeat&&{repeat:n.repeat},...n.repeatSide&&{"repeat-side":n.repeatSide},...n.color&&{color:n.color},...n.texture&&{texture:n.texture}},x:0,y:0,w:0,h:0})}this.scope.push(o)}id(o){if(o instanceof Et)return o.node;let n=this.nodes.get(o);if(!n)throw Error(`"${o.label}" is not part of this figure`);return n.id}}class Et{graph;node;style;constructor(o,n,c={}){this.graph=o;this.node=n;this.style=c}edge(o){return new Et(this.graph,this.node,{...this.style,...typeof o==="string"?{label:o}:o})}}var io=(o,n)=>{let c=o[0]?.graph;if(!c)throw Error(`${n} was called without inputs; start from fig.input()`);if(o.some((a)=>!(a instanceof Et)||a.graph!==c))throw Error(`${n}: inputs must be Tensors of the same figure`);return c};function It(o,n,c,a=o,u={}){let h=(...x)=>io(x,`"${h.label}"`).apply(h,x);return Object.assign(h,{shape:o,kind:a,label:n,options:c,defaults:u,freeze(){return h.options.frozen=!0,h.options.trainable=!1,h},trainable(){return h.options.trainable=!0,h.options.frozen=!1,h}})}function tt(o,n="",c={}){let{kind:a=o,...u}=typeof c==="string"?{color:c}:c;return(h,x)=>{let[g,v]=typeof h==="string"?[h,x??{}]:[n,h??{}];return It(o,g,{...v},a,u)}}var To=tt("block"),Io=tt("thin"),co=tt("text"),Po=tt("circle"),Be=tt("op","+"),jo=tt("enc","Encoder","blue"),Fo=tt("dec","Decoder","green"),Bo=tt("tensor","","blue"),qo=tt("stack","","blue"),Uo=tt("neurons"),Vo=tt("empty");function Go(o,n,c){let[a,u]=typeof n==="string"?[n,c??{}]:["",n??{}];return It("vector",a,{...u,values:o})}var Do=tt("block","Multi-Head Attention",{color:"orange",kind:"mha"}),_o=tt("block","Attention",{color:"orange",kind:"attention"}),Ho=tt("block","Feed Forward",{color:"blue",kind:"ffn"}),Wo=tt("block","MLP",{color:"blue",kind:"mlp"}),Jo=tt("thin","Add & Norm",{color:"yellow",kind:"norm"}),Zo=tt("block","Embedding",{color:"pink",kind:"embed"}),Yo=tt("thin","Linear",{color:"purple",kind:"linear"}),Xo=tt("thin","Softmax",{color:"green",kind:"softmax"}),Ko=tt("thin","MatMul",{color:"purple",kind:"matmul"}),Qo=tt("thin","Concat",{color:"yellow",kind:"concat"}),tr=tt("block","Conv",{color:"teal",kind:"conv"}),er=tt("thin","Pool",{color:"gray",kind:"pool"}),or=tt("thin","ReLU",{color:"green",kind:"act"}),rr=tt("thin","Dropout",{color:"gray",kind:"dropout"}),sr=tt("block","Loss",{color:"red",kind:"loss"}),nr=tt("gaussian","","purple"),ar=tt("space","","gray"),ir=tt("sphere","","blue"),cr=tt("pointcloud","","blue");function lr(o,n){let[c,{samples:a,...u}]=typeof o==="string"?[o,n??{}]:["",o??{}],h;if(a?.length){let x=Math.min(256,a.length),g=(v)=>Math.round(v*1000)/1000;h=Array.from({length:x},(v,R)=>{let I=1/0,q=-1/0;for(let O=Math.floor(R*a.length/x);O<Math.floor((R+1)*a.length/x);O++)I=Math.min(I,a[O]),q=Math.max(q,a[O]);return[g(I),g(q)]})}return It("waveform",c,{...u,...h&&{wave:h}},"waveform",{color:"teal"})}var Pe=(o)=>typeof o==="string"?o:("toDataURL"in o)?o.toDataURL("image/png"):o.currentSrc||o.src||"";function fr(o,n,c){let[a,u]=typeof n==="string"?[n,c??{}]:["",n??{}],h=Array.isArray(o)?o.map(Pe):[Pe(o)];return It("image",a,{n:h.length>1?h.length:void 0,...u,src:h[0],...h.length>1&&{srcs:h}})}function ur(o,n){let[c,{src:a,...u}]=typeof o==="string"?[o,n??{}]:["",o??{}];return It("magnify",c,{...u,...a&&{src:Pe(a)}},"magnify",{color:"orange"})}var lo=(o,n)=>o===void 0?{v:Math.abs(Math.sin(n*12.9898+4.1)*43758.5453)%1}:typeof o==="number"?{v:o}:typeof o==="string"?{c:o}:{v:o.value,c:o.color,t:o.texture,l:o.label,x:o.crossed};function pr(o,n){let[c,{cells:a,...u}]=typeof o==="string"?[o,n??{}]:["",o??{}],h=u.rows??(Array.isArray(a)?a.length:5),x=u.cols??(Array.isArray(a)?Math.max(0,...a.map((R)=>R.length)):5),g=(R,I)=>typeof a==="function"?a(R,I):a?.[R]?.[I],v=Array.from({length:h},(R,I)=>Array.from({length:x},(q,O)=>lo(g(I,O),I*31+O)));return It("matrix",c,{...u,rows:h,cols:x,grid:v},"matrix",{color:"blue"})}function hr(o,n){let[c,{cells:a,...u}]=typeof o==="string"?[o,n??{}]:["",o??{}],h=Array.isArray(a)?a:void 0,x=u.rows??h?.length??5,g=u.cols??(h?Math.max(0,...h.map((O)=>O.length)):5),v=u.depth??(h?Math.max(0,...h.flatMap((O)=>O.map((U)=>U.length))):5),R=(O,U,X)=>lo(typeof a==="function"?a(O,U,X):a?.[O]?.[U]?.[X],O*97+U*31+X*7),I=(O,U,X)=>Array.from({length:O},(V,_)=>Array.from({length:U},(ft,W)=>X(_,W))),q={front:I(x,g,(O,U)=>R(O,U,0)),top:I(v,g,(O,U)=>R(0,U,O)),side:I(x,v,(O,U)=>R(O,g-1,U))};return It("voxels",c,{...u,rows:x,cols:g,depth:v,faces:q},"voxels",{color:"blue"})}var dr=(...o)=>Be("+")(...o),mr=(...o)=>Be("*")(...o);class fo{constructor(o={}){let n=(...c)=>{let a=io(c,n.constructor.name||"module");a.enter(n);try{return n.forward(...c)}finally{a.scope.pop()}};return delete n.name,delete n.length,Object.setPrototypeOf(n,new.target.prototype),n.options={...o},n}freeze(){return this.options.frozen=!0,this.options.trainable=!1,this}trainable(){return this.options.trainable=!0,this.options.frozen=!1,this}}var ue={};function gr(o){if(o===null)ze(void 0);else if("tex"in o)ze(o.tex);let{tex:n,...c}=o??{};return ue=o===null?{}:je(ue,c),structuredClone(ue)}var je=(o,n)=>{let c={...o.kinds};for(let[a,u]of Object.entries(n.kinds??{}))c[a]={...c[a],...u};return{...o,...n,style:{...o.style,...n.style},kinds:c}},br=(...o)=>{let n={};for(let c of o)for(let[a,u]of Object.entries(c??{}))if(u!==void 0)n[a]=u;return n},xr=new Set(["id","color","frozen","trainable","dashed","bold","crossed"]),yr={leftOf:"left-of",rightOf:"right-of"};class qe{options;g=new Fe;constructor(o={}){this.options={...o}}input(o="",n={}){if(typeof o!=="string")Object.assign(o.options,n);let c=typeof o==="string"?co(o,n):o;return new Et(this.g,this.g.node(c))}edge(o,n,c={}){if(o.graph!==this.g)throw Error("edge: tensor belongs to another figure");if(!(n instanceof Et))this.g.node(n);return this.g.edges.push({from:o.node,to:n,style:{...o.style,...typeof c==="string"?{label:c}:c}}),this}rank(...o){return this.g.ranks.push(o),this}diagram(){let o=this.g,n=new Map,c=je(ue,this.options),a=0;for(let[h,x]of o.nodes){let g=[c,...x.mods.map((W)=>W.options)],v=h.options,R=br(h.defaults,...g.flatMap((W)=>[W?.style,W?.kinds?.[h.kind]]),v),I=h.label,q=(W)=>{if(!I.includes(W))return!1;return I=I.replace(W,"").trim(),!0},O=q("\uD83E\uDDCA"),U=q("\uD83D\uDD25"),X=[...x.mods].reverse().map((W)=>W.options).find((W)=>W.frozen||W.trainable),V=v.frozen||O?"frozen":v.trainable||U?"train":X?.frozen?"frozen":X?.trainable?"train":"";if(V&&!v.texture)delete R.texture;let _=new Set([V,R.dashed&&"dashed",R.bold&&"bold",R.crossed&&"crossed"].filter((W)=>!!W)),ft={};for(let[W,lt]of Object.entries(R)){if(xr.has(W)||lt===void 0)continue;ft[yr[W]??W]=lt instanceof Et||typeof lt==="function"?o.id(lt):Array.isArray(lt)&<.every((wt)=>typeof wt==="number")?lt.join(","):typeof lt==="object"?JSON.stringify(lt):String(lt)}n.set(x.id,{id:x.id,shape:h.shape,label:I,color:R.color,flags:_,attrs:ft,groups:x.groups,order:a++,x:0,y:0,w:0,h:0})}let u=o.edges.map(({from:h,to:x,style:g})=>({from:h,to:o.id(x),label:g.label,pts:[],op:g.dense?"=>":g.arrow==="none"?g.dashed?"..":"--":g.arrow==="both"?"<->":g.dashed?"..>":"->",flags:new Set([...["skip","curve","thick","dashed","crossed"].filter((v)=>g[v]),...g.join?["join"]:[]]),attrs:{...g.side&&{side:g.side},...g.color&&{color:g.color}}}));return{dir:c.flow??"TB",theme:c.theme??"light",gap:c.gap,fontSize:c.style?.fontSize,fontFamily:c.style?.fontFamily,legend:c.legend===!0?{}:c.legend||void 0,nodes:n,edges:u,groups:[...o.groups.values()].map((h)=>({...h,flags:new Set(h.flags)})),ranks:o.ranks.map((h)=>h.map((x)=>o.id(x)))}}svg(o={}){let n=this.diagram();return Ee(ce(n),o.theme??n.theme,je(ue,this.options).materials??!0)}async toBlob(o={}){let n=o.format??"svg",c=this.svg({theme:o.theme});if(n==="svg")return new Blob([c],{type:"image/svg+xml"});if(!["png","jpeg","webp"].includes(n))throw Error(`export: unknown format "${n}" (svg, png, jpeg, webp)`);return $r(c,{...o,format:n},`image/${n}`)}async download(o="figure.svg",n={}){let c=/\.(\w+)$/.exec(o)?.[1].toLowerCase(),a=n.format??(c==="jpg"?"jpeg":c)??"svg",u=URL.createObjectURL(await this.toBlob({...n,format:a}));Object.assign(document.createElement("a"),{href:u,download:o}).click(),setTimeout(()=>URL.revokeObjectURL(u),1000)}mount(o,n={}){let c=typeof o==="string"?document.querySelector(o):o;if(!c)throw Error(`mount: no element matches "${o}"`);return c.innerHTML=this.svg(n),this}}async function vr(o){let n=[...new Set([...o.matchAll(/<image href="(?!data:)([^"]+)"/g)].map((c)=>c[1].replace(/&/g,"&")))];for(let c of n)try{let a=await(await fetch(c)).blob(),u=await new Promise((h,x)=>{let g=new FileReader;g.onload=()=>h(g.result),g.onerror=x,g.readAsDataURL(a)});o=o.split(`href="${c.replace(/&/g,"&")}"`).join(`href="${u}"`)}catch{}return o}async function $r(o,n,c){if(typeof document>"u")throw Error(`export: ${n.format} needs a browser (canvas); in Node use svg()`);let[,a,u]=/width="([\d.]+)" height="([\d.]+)"/.exec(o)??[],h=n.scale??2,x=new Image;x.src="data:image/svg+xml;charset=utf-8,"+encodeURIComponent(await vr(o)),await x.decode();let g=document.createElement("canvas");g.width=Math.ceil(+a*h),g.height=Math.ceil(+u*h);let v=g.getContext("2d"),R=n.background??(c==="image/jpeg"?"#ffffff":"");if(R)v.fillStyle=R,v.fillRect(0,0,g.width,g.height);return v.drawImage(x,0,0,g.width,g.height),new Promise((I,q)=>g.toBlob((O)=>O?I(O):q(Error("export: canvas.toBlob failed")),c,n.quality??0.92))}function wr(o,n){let[c,a]=typeof o==="function"?[{},o]:[o,n],u=new qe(c);return a(u),u}function kr(o,n={}){return Ee(ce(o),n.theme,n.materials??!0)}var Fr={...Ue,draw:kr,layout:ce,THEMES:Se};export{qe as Figure,Fe as Graph,fo as Module,Se as THEMES,Et as Tensor,or as act,dr as add,_o as attention,To as block,Po as circle,Qo as concat,gr as configure,tr as conv,Fo as dec,Fr as default,kr as draw,rr as dropout,Zo as embed,Vo as empty,jo as enc,Ho as ffn,wr as figure,nr as gaussian,fr as image,It as layer,ce as layout,Yo as linear,sr as loss,ur as magnify,Ko as matmul,pr as matrix,Do as mha,Wo as mlp,mr as mul,Uo as neurons,Jo as norm,Be as op,cr as pointcloud,er as pool,tt as preset,Xo as softmax,ar as space,ir as sphere,qo as stack,Bo as tensor,co as text,Io as thin,Go as vector,hr as voxels,lr as waveform};
|
package/dist/index.d.ts
CHANGED
|
@@ -39,6 +39,7 @@ declare const _default: {
|
|
|
39
39
|
tensor: api.Factory<api.TensorOptions>;
|
|
40
40
|
stack: api.Factory<api.StackOptions>;
|
|
41
41
|
neurons: api.Factory<api.NeuronOptions>;
|
|
42
|
+
empty: api.Factory<api.NodeOptions>;
|
|
42
43
|
mha: api.Factory<api.NodeOptions>;
|
|
43
44
|
attention: api.Factory<api.NodeOptions>;
|
|
44
45
|
ffn: api.Factory<api.NodeOptions>;
|
package/dist/layout.d.ts
CHANGED
|
@@ -12,4 +12,6 @@ export declare const labelSize: (n: Node, base: number) => number;
|
|
|
12
12
|
*/
|
|
13
13
|
export declare const imageMatrix: (attrs: Record<string, string>) => [number, number, number, number];
|
|
14
14
|
export declare const nums: (s: string | undefined, def: number[]) => number[];
|
|
15
|
+
export type Box = [number, number, number, number];
|
|
16
|
+
export declare const hits: (a: Box, b: Box) => boolean;
|
|
15
17
|
export declare function layout(d: Diagram): Diagram;
|
package/dist/tex.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
export declare const SANS = "Inter,\"Helvetica Neue\",Helvetica,Arial,sans-serif";
|
|
2
|
+
/** normal font weight, the default for labels */
|
|
3
|
+
export declare const REGULAR = 400;
|
|
2
4
|
export declare const SERIF = "\"Latin Modern Math\",\"STIX Two Math\",\"Cambria Math\",\"Times New Roman\",serif";
|
|
3
5
|
export interface Run {
|
|
4
6
|
t: string;
|
|
@@ -29,7 +31,7 @@ export interface TextBox {
|
|
|
29
31
|
lh: number;
|
|
30
32
|
size: number;
|
|
31
33
|
}
|
|
32
|
-
export declare function font(size: number, it: boolean, b: boolean | number, serif: boolean): string;
|
|
34
|
+
export declare function font(size: number, it: boolean, b: boolean | number, serif: boolean, family?: string): string;
|
|
33
35
|
export declare function measure(t: string, f: string, size: number): number;
|
|
34
36
|
/** Lay out a label: newline, `\\` or `<br>` break lines, `$...$` switches to math. */
|
|
35
|
-
export declare function layoutLabel(label: string, size: number, weight?: number): TextBox;
|
|
37
|
+
export declare function layoutLabel(label: string, size: number, weight?: number, family?: string): TextBox;
|
package/dist/types.d.ts
CHANGED
|
@@ -43,6 +43,13 @@ export interface Diagram {
|
|
|
43
43
|
gap?: [number, number];
|
|
44
44
|
/** base text size (px); blocks may set attrs.fontSize */
|
|
45
45
|
fontSize?: number;
|
|
46
|
+
/** base CSS font family; blocks may set attrs.fontFamily */
|
|
47
|
+
fontFamily?: string;
|
|
48
|
+
/** a key to the looks the figure uses (frozen, trainable, textures, crossed); labels override the names */
|
|
49
|
+
legend?: {
|
|
50
|
+
position?: 'bottom' | 'right';
|
|
51
|
+
labels?: Partial<Record<string, string>>;
|
|
52
|
+
};
|
|
46
53
|
nodes: Map<string, Node>;
|
|
47
54
|
edges: Edge[];
|
|
48
55
|
groups: Group[];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "forwardfig",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Draw ML architecture figures in TypeScript, written the way you write the model. Zero dependencies, SVG out.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/forwardfig.js",
|
|
@@ -12,11 +12,14 @@
|
|
|
12
12
|
"import": "./dist/forwardfig.js"
|
|
13
13
|
}
|
|
14
14
|
},
|
|
15
|
+
"bin": {
|
|
16
|
+
"forwardfig": "dist/cli.js"
|
|
17
|
+
},
|
|
15
18
|
"files": [
|
|
16
19
|
"dist"
|
|
17
20
|
],
|
|
18
21
|
"scripts": {
|
|
19
|
-
"build": "bun build src/index.ts --minify --outfile dist/forwardfig.js && bun build src/index.ts --minify --format=iife --outfile dist/forwardfig.iife.js && tsc --emitDeclarationOnly --declaration --outDir dist",
|
|
22
|
+
"build": "bun build src/index.ts --minify --outfile dist/forwardfig.js && bun build src/cli.ts --minify --target=node --external sharp --outfile dist/cli.js && bun build src/index.ts --minify --format=iife --outfile dist/forwardfig.iife.js && tsc --emitDeclarationOnly --declaration --outDir dist",
|
|
20
23
|
"test": "bun test",
|
|
21
24
|
"check": "tsc --noEmit",
|
|
22
25
|
"docs:build": "bun doc/site/build.mjs",
|
|
@@ -24,9 +27,19 @@
|
|
|
24
27
|
"prepublishOnly": "bun run check && bun test && bun run build"
|
|
25
28
|
},
|
|
26
29
|
"devDependencies": {
|
|
30
|
+
"@types/node": "^26.6.5",
|
|
27
31
|
"marked": "^18.1.1",
|
|
32
|
+
"sharp": "^0.35.5",
|
|
28
33
|
"typescript": "^5.6.0"
|
|
29
34
|
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"sharp": ">=0.33"
|
|
37
|
+
},
|
|
38
|
+
"peerDependenciesMeta": {
|
|
39
|
+
"sharp": {
|
|
40
|
+
"optional": true
|
|
41
|
+
}
|
|
42
|
+
},
|
|
30
43
|
"license": "MIT",
|
|
31
44
|
"repository": {
|
|
32
45
|
"type": "git",
|