Skip to content

Commit a1eed81

Browse files
authored
Render explicit sequential model designs in the 3D viewer (#27)
1 parent 4c48ef4 commit a1eed81

6 files changed

Lines changed: 52 additions & 21 deletions

File tree

‎content/_index.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@
1616
<p id="objective" class="model-objective"></p><div class="workspace"><div class="visual-column"><div id="stage" aria-label="3D model diagram"><div id="canvas-host"></div><div class="scene-note"><span class="dot"></span><span id="scene-note">Your design will appear here</span></div><div class="scene-help" id="scene-help">Drag to orbit · Scroll to zoom · Select a layer</div><div class="view-mark" aria-hidden="true">X <span>⌁</span> Y</div><div id="fallback"><div class="empty-symbol">⌘</div><h3>Your design starts here.</h3><p>Describe your task in the chat. Your model will take shape here as the designer learns what you need.</p></div></div><div id="layer-list" hidden aria-label="Model layers"></div><div class="layer-strip" id="layer-strip" aria-label="Select a layer"></div><div class="flow-caption" id="flow-caption" role="status">An architecture preview. No trained weights or predictions.</div></div>
1717
<aside class="inspector" aria-label="Selected layer details"><div class="inspector-kicker"><span id="layer-index">WAITING FOR YOUR IDEA</span><span id="layer-kind"></span></div><div class="layer-glyph" aria-hidden="true">⠿</div><h3 id="layer-title">Your model starts here</h3><p id="layer-description"></p><dl><div><dt>Output shape</dt><dd id="layer-shape"></dd></div><div><dt>Parameters</dt><dd id="layer-params"></dd></div></dl><div class="inspector-subtitle">IN THIS LAYER</div><ul id="layer-items"></ul><p class="inspector-tip">Select any layer to take a closer look.</p></aside></div>
1818
<div class="card-footer"><button id="download" class="download-project" disabled>Download project ↓</button><div class="metric"><strong id="metric-features">—</strong><span>input features</span></div><div class="metric"><strong id="metric-params">—</strong><span id="metric-params-label">parameters</span></div><div class="metric"><strong id="metric-runtime">—</strong><span id="metric-runtime-label">local training</span></div><div class="ownership"><span>↗</span><p>Your data. Your compute.<br><strong>Your model.</strong></p></div></div></section>
19-
<details class="design-context"><summary>Design details & evidence</summary><dl><dt>Architecture</dt><dd id="architecture">Not specified yet.</dd><dt>Compute</dt><dd id="compute">Training runs on your hardware.</dd><dt>Research</dt><dd id="research">No design yet.</dd><dt>Local handoff</dt><dd><code id="instruction">Read https://zer.foo/start/ and help me create, train and run a Zerfoo model on my hardware using Kazi.</code><button id="copy" type="button">Copy instruction</button><span id="copied" role="status"></span></dd></dl></details><p class="workspace-foot">Design here. Train, evaluate and run on your hardware.</p></section></section></main><div class="toast" role="status" aria-live="polite"></div><script type="module" src="/create/app.mjs"></script></body></html>
19+
<details class="design-context"><summary>Design details & evidence</summary><dl><dt>Architecture</dt><dd id="architecture">Not specified yet.</dd><dt>Compute</dt><dd id="compute">Training runs on your hardware.</dd><dt>Research</dt><dd id="research">No design yet.</dd><dt>Local handoff</dt><dd><code id="instruction">Read https://zer.foo/start/ and help me create, train and run a Zerfoo model on my hardware using Kazi.</code><button id="copy" type="button">Copy instruction</button><span id="copied" role="status"></span></dd></dl></details><p class="workspace-foot">Design here. Train, evaluate and run on your hardware.</p></section></section></main><div class="toast" role="status" aria-live="polite"></div><script type="module" src="/create/app.mjs?v=sequential-1"></script></body></html>

‎static/create/app.mjs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {trackFunnel} from './analytics.mjs';
2-
import {adaptProject} from './model.mjs';
3-
import {createModelViewer} from './viewer.mjs';
2+
import {adaptProject} from './model.mjs?v=sequential-1';
3+
import {createModelViewer} from './viewer.mjs?v=sequential-1';
44
const $=id=>document.getElementById(id);
55
let messages=[],project=null,download=null,busy=false,chatStarted=false;
66
const viewerRoot=document.querySelector?.('[data-model-viewer]');

‎static/create/model.mjs‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,31 @@ export function adaptProject(value, ) {
88
if(p.components!=null&&(!Array.isArray(p.components)||p.components.length>16||p.components.some(c=>typeof c!=='string'||c.length>300))) throw Error('Invalid component list.');
99
const supported=p.task==='numeric_classification'&&p.recipe==='dense-relu-16-v1';
1010
if(supported&&(!p.features.length||new Set(p.features).size!==p.features.length||p.features.includes(p.target))) throw Error('The numeric recipe needs distinct features that exclude the target.');
11-
const layers=supported?[
11+
let layers=supported?[
1212
{id:'input',name:'Your inputs',kind:'Input',count:p.features.length,color:'#78d0cd',subtitle:`${p.features.length} numeric features`,description:'One number for each measurement. These are the columns your model will read, in the order shown.',items:p.features,shape:`[batch, ${p.features.length}]`,params:0},
1313
{id:'dense',name:'Find patterns',kind:'Dense',count:16,color:'#e4d58f',subtitle:'16 connected units',description:'Each unit combines all your inputs. Training adjusts these connections to find patterns that help distinguish the classes.',items:['Every input connects to every unit','Weights and biases learned locally'],shape:'[batch, 16]',params:p.features.length*16+16},
1414
{id:'relu',name:'Add flexibility',kind:'ReLU',count:16,color:'#d4d77b',subtitle:'ReLU activation',description:'Keeps positive values and replaces negative values with zero. This lets the model learn more than a straight-line relationship.',items:['16 values in → 16 values out','No trainable parameters'],shape:'[batch, 16]',params:0},
1515
{id:'output',name:'Class scores',kind:'Dense',count:exampleLabels?.length??null,color:'#a4d5a8',subtitle:exampleLabels?`${exampleLabels.length} example classes`:'Class count determined locally',description:exampleLabels?'One score for each example class. These labels illustrate the design; no trained model or prediction is loaded.':'One score for each distinct target label. The number of classes is discovered when your local dataset is inspected.',items:exampleLabels??['Target: '+(p.target||'not specified'),'Class labels are not in the design response'],shape:`[batch, ${exampleLabels?.length??'classes'}]`,params:exampleLabels?17*exampleLabels.length:null}
1616
]:(p.components||[]).map((id,i)=>({id:'component-'+i,name:id.split('/').pop(),kind:'Component',count:1,color:'#78d0cd',subtitle:'Proposed component',description:'Selected by the designer. Its dimensions, connections and implementation still need local validation.',items:[id],shape:'Not yet specified',params:null}));
17-
return {project:p,layers,supported,example:!!exampleLabels,objective:p.objective||'Your model design',parameters:supported&&exampleLabels?(p.features.length+1)*16+17*exampleLabels.length:null};
17+
if(p.task==='component_design'&&p.topology!=null)layers=topologyLayers(p.topology);
18+
return {project:p,layers,supported:supported||!!(p.task==='component_design'&&p.topology),starter:supported,example:!!exampleLabels,objective:p.objective||'Your model design',parameters:supported&&exampleLabels?(p.features.length+1)*16+17*exampleLabels.length:null};
19+
}
20+
21+
function topologyLayers(topology){
22+
if(topology.type!=='sequential'||!Array.isArray(topology.layers)||topology.layers.length<3||topology.layers.length>12)throw Error('Invalid sequential topology.');
23+
let previous=null;
24+
const colors=['#78d0cd','#e4d58f','#d4d77b','#a4d5a8'];
25+
const layers=topology.layers.map((layer,i)=>{
26+
const kinds=['Input','Dense','ReLU','Sigmoid','Tanh','Softmax','Dropout'];
27+
if(!layer||!kinds.includes(layer.kind)||(i===0)!==(layer.kind==='Input'))throw Error('Invalid topology layer.');
28+
const count=layer.units??null;
29+
if(count!==null&&(!Number.isSafeInteger(count)||count<1||count>4096))throw Error('Invalid topology units.');
30+
const activation=!['Input','Dense'].includes(layer.kind);
31+
if(activation&&count!==previous)throw Error('Activation must preserve layer width.');
32+
const params=layer.kind==='Dense'?(previous!==null&&count!==null?(previous+1)*count:null):0;
33+
previous=count;
34+
return {id:'layer-'+i,name:layer.kind==='Input'?'Input':layer.kind==='Dense'?(i===topology.layers.length-1?'Output layer':'Hidden layer') :layer.kind,kind:layer.kind,count,color:colors[i%colors.length],subtitle:count===null?'Width not yet specified':`${count} ${activation?'values':'units'}`,description:layer.kind==='Dense'?'A proposed fully connected layer. Its weights and biases will be learned during local training.':layer.kind==='Input'?'The model input. Its width will be set for your task.':'A proposed '+layer.kind+' operation, preserving the preceding layer width.',items:['Proposed sequential connection',count===null?'Dimension remains unspecified':`${count} values${count>32?' (32 drawn)':''}`],shape:`[batch, ${count??'?'}]`,params};
35+
});
36+
if(!layers.some(x=>x.kind==='Dense'))throw Error('Dense topology required.');
37+
return layers;
1838
}

‎static/create/scene.mjs‎

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,37 @@ export function createScene(host,onSelect) {
1111
const rim=new THREE.PointLight(0x78d0cd,80);rim.position.set(-5,1,4);scene.add(rim);
1212
let group=new THREE.Group();scene.add(group);let meshes=[],lines=[],particles=[],selected=1,dirty=true,flow=false,flowTime=0,lastTime=0,light=false,positions=[],model;
1313
const ray=new THREE.Raycaster(),pointer=new THREE.Vector2();
14-
const xPositions=[-5.7,-1.7,1.6,5.4];
14+
let xPositions=[];
1515
function reset(){camera.position.set(3.8,3.2,15.8);controls.target.set(0,0,0);controls.update();dirty=true;}
1616
function label(text,x,y,color){const canvas=document.createElement('canvas');canvas.width=640;canvas.height=80;const c=canvas.getContext('2d');c.font='500 28px Inter, Arial';c.textAlign='center';c.fillStyle=color;c.fillText(text,320,47);const texture=new THREE.CanvasTexture(canvas);const sprite=new THREE.Sprite(new THREE.SpriteMaterial({map:texture,transparent:true,depthTest:false}));sprite.position.set(x,y,0);sprite.scale.set(4,.5,1);group.add(sprite);}
1717
function clear(){group.traverse(o=>{o.geometry?.dispose();for(const m of [].concat(o.material||[])){m.map?.dispose();m.dispose();}});scene.remove(group);group=new THREE.Group();scene.add(group);meshes=[];lines=[];particles=[];positions=[];}
1818
function build(next){model=next;clear();if(!next.supported){dirty=true;return;}
19+
xPositions=next.layers.map((_,i)=>-5.7+i*11.4/(next.layers.length-1));
1920
const grid=new THREE.GridHelper(24,32,light?0xccccbf:0x363730,light?0xddddcf:0x24251f);grid.position.y=-3.3;grid.material.transparent=true;grid.material.opacity=.36;group.add(grid);
2021
next.layers.forEach((layer,index)=>{
2122
const count=Math.min(layer.count??1,32),arr=[];let color=new THREE.Color(layer.color);if(light)color.multiplyScalar(.66);
2223
const meshGroup=new THREE.Group();meshGroup.userData.layer=index;
2324
for(let n=0;n<count;n++){
24-
let y,z;if(index===1||index===2){y=(1.5-Math.floor(n/4))*1.05;z=(n%4-1.5)*1.05;}else{const cols=count>8?Math.ceil(count/8):1;const rows=Math.ceil(count/cols);y=((rows-1)/2-Math.floor(n/cols))*.88;z=(n%cols-(cols-1)/2)*.9;}
25+
let y,z;if(layer.kind!=='Input'&&count>4){const cols=Math.ceil(Math.sqrt(count)),rows=Math.ceil(count/cols);y=((rows-1)/2-Math.floor(n/cols))*.65;z=(n%cols-(cols-1)/2)*.65;}else{const cols=count>8?Math.ceil(count/8):1;const rows=Math.ceil(count/cols);y=((rows-1)/2-Math.floor(n/cols))*.88;z=(n%cols-(cols-1)/2)*.9;}
2526
const pos=new THREE.Vector3(xPositions[index],y,z);arr.push(pos);
26-
const sphere=new THREE.Mesh(new THREE.SphereGeometry(index===2?.085:.145,20,16),new THREE.MeshStandardMaterial({color,emissive:color,emissiveIntensity:light?.1:.4,metalness:.35,roughness:.28,transparent:true,opacity:index===2?.72:1}));sphere.position.copy(pos);sphere.userData.layer=index;meshGroup.add(sphere);meshes.push(sphere);
27-
if(index!==2){const halo=new THREE.Mesh(new THREE.SphereGeometry(.22,16,12),new THREE.MeshBasicMaterial({color,transparent:true,opacity:light?.07:.055,depthWrite:false}));halo.position.copy(pos);meshGroup.add(halo);}
27+
const sphere=new THREE.Mesh(new THREE.SphereGeometry(layer.kind==='ReLU'?.085:.145,20,16),new THREE.MeshStandardMaterial({color,emissive:color,emissiveIntensity:light?.1:.4,metalness:.35,roughness:.28,transparent:true,opacity:layer.kind==='ReLU'?.72:1}));sphere.position.copy(pos);sphere.userData.layer=index;meshGroup.add(sphere);meshes.push(sphere);
28+
if(layer.kind!=='ReLU'){const halo=new THREE.Mesh(new THREE.SphereGeometry(.22,16,12),new THREE.MeshBasicMaterial({color,transparent:true,opacity:light?.07:.055,depthWrite:false}));halo.position.copy(pos);meshGroup.add(halo);}
2829
}
2930
const plateGeometry=new THREE.BoxGeometry(.025,4.4,index===0||index===3?2.1:4.3);
3031
const plate=new THREE.Mesh(plateGeometry,new THREE.MeshBasicMaterial({color,transparent:true,opacity:.035,depthWrite:false}));plate.position.set(xPositions[index],0,0);plate.userData.layer=index;meshGroup.add(plate);meshes.push(plate);
3132
const edge=new THREE.LineSegments(new THREE.EdgesGeometry(plateGeometry),new THREE.LineBasicMaterial({color,transparent:true,opacity:.22}));edge.position.copy(plate.position);meshGroup.add(edge);group.add(meshGroup);
32-
label(['INPUT','DENSE · 16','ReLU',layer.count===null?'OUTPUT · ?':'OUTPUT'][index],xPositions[index],-2.8,light?'#60625e':'#a5a5a5');
33+
label(layer.kind.toUpperCase()+' · '+(layer.count??'?'),xPositions[index],-2.8,light?'#60625e':'#a5a5a5');
3334
positions.push(arr);
3435
});
35-
for(let l=0;l<3;l++)for(let a=0;a<positions[l].length;a++)for(let b=0;b<positions[l+1].length;b++){
36-
if(l===1&&a!==b)continue;
37-
const start=positions[l][a],end=positions[l+1][b],color=model.layers[l].color;
38-
const line=new THREE.Line(new THREE.BufferGeometry().setFromPoints([start,end]),new THREE.LineBasicMaterial({color:light?0x899483:color,transparent:true,opacity:l===1?.16:.095,depthWrite:false}));line.userData={from:l,to:l+1};group.add(line);lines.push(line);
39-
if((a+b)%3===0){const particle=new THREE.Mesh(new THREE.SphereGeometry(.045,8,6),new THREE.MeshBasicMaterial({color}));particle.visible=false;particle.userData={start,end,phase:(a+b*.37)%1,stage:l};group.add(particle);particles.push(particle);}
36+
for(let l=0;l<positions.length-1;l++){
37+
const vertices=[],color=model.layers[l].color;
38+
for(let a=0;a<positions[l].length;a++)for(let b=0;b<positions[l+1].length;b++){
39+
if(!['Dense','Input'].includes(model.layers[l+1].kind)&&a!==b)continue;
40+
const start=positions[l][a],end=positions[l+1][b];vertices.push(start.x,start.y,start.z,end.x,end.y,end.z);
41+
if((a+b)%7===0&&particles.filter(p=>p.userData.stage===l).length<12){const particle=new THREE.Mesh(new THREE.SphereGeometry(.045,8,6),new THREE.MeshBasicMaterial({color}));particle.visible=false;particle.userData={start,end,phase:(a+b*.37)%1,stage:l};group.add(particle);particles.push(particle);}
42+
}
43+
const geometry=new THREE.BufferGeometry();geometry.setAttribute('position',new THREE.Float32BufferAttribute(vertices,3));
44+
const line=new THREE.LineSegments(geometry,new THREE.LineBasicMaterial({color:light?0x899483:color,transparent:true,opacity:.095,depthWrite:false}));line.userData={from:l,to:l+1};group.add(line);lines.push(line);
4045
}
4146
select(selected);dirty=true;
4247
}
@@ -47,7 +52,7 @@ export function createScene(host,onSelect) {
4752
renderer.domElement.addEventListener('pointerup',e=>{if(!down||Math.hypot(e.clientX-down[0],e.clientY-down[1])>5)return;const r=renderer.domElement.getBoundingClientRect();pointer.set((e.clientX-r.left)/r.width*2-1,-(e.clientY-r.top)/r.height*2+1);ray.setFromCamera(pointer,camera);const hit=ray.intersectObjects(meshes)[0];if(hit)onSelect(hit.object.userData.layer);});
4853
controls.addEventListener('change',()=>dirty=true);
4954
const observer=new ResizeObserver(resize);observer.observe(host);reset();resize();
50-
renderer.setAnimationLoop(t=>{const dt=Math.min((t-lastTime)/1000,.05);lastTime=t;if(document.hidden)return;controls.update();if(flow){flowTime+=dt;for(const p of particles){const cycle=(flowTime*.45+p.userData.phase)%3;p.visible=Math.floor(cycle)===p.userData.stage;p.position.lerpVectors(p.userData.start,p.userData.end,cycle%1);}dirty=true;}if(dirty){renderer.render(scene,camera);dirty=false;}});
55+
renderer.setAnimationLoop(t=>{const dt=Math.min((t-lastTime)/1000,.05);lastTime=t;if(document.hidden)return;controls.update();if(flow){flowTime+=dt;for(const p of particles){const cycle=(flowTime*.45+p.userData.phase)%Math.max(1,positions.length-1);p.visible=Math.floor(cycle)===p.userData.stage;p.position.lerpVectors(p.userData.start,p.userData.end,cycle%1);}dirty=true;}if(dirty){renderer.render(scene,camera);dirty=false;}});
5156
renderer.domElement.addEventListener('webglcontextlost',e=>{e.preventDefault();host.dispatchEvent(new Event('preview-unavailable'));});
5257
return {build,select,reset,setFlow(value){flow=value;for(const p of particles)p.visible=false;dirty=true;},setTheme(value){light=value;ambient.intensity=light?.8:2;key.intensity=light?1:3;if(model)build(model);},dispose(){observer.disconnect();controls.dispose();clear();renderer.setAnimationLoop(null);renderer.dispose();renderer.domElement.remove();}};
5358
}

0 commit comments

Comments
 (0)