(()=>{function B(){}function xt(t){return t()}function ut(){return Object.create(null)}function L(t){t.forEach(xt)}function ft(t){return typeof t=="function"}function Ot(t,e){return t!=t?e==e:t!==e||t&&typeof t=="object"||typeof t=="function"}function At(t){return Object.keys(t).length===0}function U(t){return t??""}var bt=typeof window<"u"?window:typeof globalThis<"u"?globalThis:global;var pt=class t{_listeners="WeakMap"in bt?new WeakMap:void 0;_observer=void 0;options;constructor(e){this.options=e}observe(e,n){return this._listeners.set(e,n),this._getObserver().observe(e,this.options),()=>{this._listeners.delete(e),this._observer.unobserve(e)}}_getObserver(){return this._observer??(this._observer=new ResizeObserver(e=>{for(let n of e)t.entries.set(n.target,n),this._listeners.get(n.target)?.(n)}))}};pt.entries="WeakMap"in bt?new WeakMap:void 0;var Ct=!1;function Tt(){Ct=!0}function Dt(){Ct=!1}function c(t,e){t.appendChild(e)}function R(t,e,n){t.insertBefore(e,n||null)}function T(t){t.parentNode&&t.parentNode.removeChild(t)}function nt(t,e){for(let n=0;nt.removeEventListener(e,n,r)}function x(t,e,n){n==null?t.removeAttribute(e):t.getAttribute(e)!==n&&t.setAttribute(e,n)}function jt(t){return Array.from(t.childNodes)}function X(t,e){e=""+e,t.data!==e&&(t.data=e)}function Lt(t){let e={};return t.childNodes.forEach(n=>{e[n.slot||"default"]=!0}),e}var rt;function J(t){rt=t}var Y=[];var dt=[],it=[],It=[],ke=Promise.resolve(),$t=!1;function Bt(){$t||($t=!0,ke.then(_t))}function ct(t){it.push(t)}var yt=new Set,ot=0;function _t(){if(ot!==0)return;let t=rt;do{try{for(;ott.indexOf(r)===-1?e.push(r):n.push(r)),n.forEach(r=>r()),it=e}var Ee=new Set;function ht(t,e){t&&t.i&&(Ee.delete(t),t.i(e))}function D(t){return t?.length!==void 0?t:Array.from(t)}var Se=["allowfullscreen","allowpaymentrequest","async","autofocus","autoplay","checked","controls","default","defer","disabled","formnovalidate","hidden","inert","ismap","loop","multiple","muted","nomodule","novalidate","open","playsinline","readonly","required","reversed","selected"],Ne=new Set([...Se]);function Ae(t,e,n){let{fragment:r,after_update:o}=t.$$;r&&r.m(e,n),ct(()=>{let i=t.$$.on_mount.map(xt).filter(ft);t.$$.on_destroy?t.$$.on_destroy.push(...i):L(i),t.$$.on_mount=[]}),o.forEach(ct)}function Ce(t,e){let n=t.$$;n.fragment!==null&&(Pt(n.after_update),L(n.on_destroy),n.fragment&&n.fragment.d(e),n.on_destroy=n.fragment=null,n.ctx=[])}function Te(t,e){t.$$.dirty[0]===-1&&(Y.push(t),Bt(),t.$$.dirty.fill(0)),t.$$.dirty[e/31|0]|=1<{let C=w.length?w[0]:v;return u.ctx&&o(u.ctx[d],u.ctx[d]=C)&&(!u.skip_bound&&u.bound[d]&&u.bound[d](C),y&&Te(t,d)),v}):[],u.update(),y=!0,L(u.before_update),u.fragment=r?r(u.ctx):!1,e.target){if(e.hydrate){Tt();let d=jt(e.target);u.fragment&&u.fragment.l(d),d.forEach(T)}else u.fragment&&u.fragment.c();e.intro&&ht(t.$$.fragment),Ae(t,e.target,e.anchor),Dt(),_t()}J(a)}var De;typeof HTMLElement=="function"&&(De=class extends HTMLElement{$$ctor;$$s;$$c;$$cn=!1;$$d={};$$r=!1;$$p_d={};$$l={};$$l_u=new Map;constructor(t,e,n){super(),this.$$ctor=t,this.$$s=e,n&&this.attachShadow({mode:"open"})}addEventListener(t,e,n){if(this.$$l[t]=this.$$l[t]||[],this.$$l[t].push(e),this.$$c){let r=this.$$c.$on(t,e);this.$$l_u.set(e,r)}super.addEventListener(t,e,n)}removeEventListener(t,e,n){if(super.removeEventListener(t,e,n),this.$$c){let r=this.$$l_u.get(e);r&&(r(),this.$$l_u.delete(e))}}async connectedCallback(){if(this.$$cn=!0,!this.$$c){let t=function(o){return()=>{let i;return{c:function(){i=h("slot"),o!=="default"&&x(i,"name",o)},m:function(a,u){R(a,i,u)},d:function(a){a&&T(i)}}}};if(await Promise.resolve(),!this.$$cn||this.$$c)return;let e={},n=Lt(this);for(let o of this.$$s)o in n&&(e[o]=[t(o)]);for(let o of this.attributes){let i=this.$$g_p(o.name);i in this.$$d||(this.$$d[i]=wt(i,o.value,this.$$p_d,"toProp"))}for(let o in this.$$p_d)!(o in this.$$d)&&this[o]!==void 0&&(this.$$d[o]=this[o],delete this[o]);this.$$c=new this.$$ctor({target:this.shadowRoot||this,props:{...this.$$d,$$slots:e,$$scope:{ctx:[]}}});let r=()=>{this.$$r=!0;for(let o in this.$$p_d)if(this.$$d[o]=this.$$c.$$.ctx[this.$$c.$$.props[o]],this.$$p_d[o].reflect){let i=wt(o,this.$$d[o],this.$$p_d,"toAttribute");i==null?this.removeAttribute(this.$$p_d[o].attribute||o):this.setAttribute(this.$$p_d[o].attribute||o,i)}this.$$r=!1};this.$$c.$$.after_update.push(r),r();for(let o in this.$$l)for(let i of this.$$l[o]){let s=this.$$c.$on(o,i);this.$$l_u.set(i,s)}this.$$l={}}}attributeChangedCallback(t,e,n){this.$$r||(t=this.$$g_p(t),this.$$d[t]=wt(t,n,this.$$p_d,"toProp"),this.$$c?.$set({[t]:this.$$d[t]}))}disconnectedCallback(){this.$$cn=!1,Promise.resolve().then(()=>{!this.$$cn&&this.$$c&&(this.$$c.$destroy(),this.$$c=void 0)})}$$g_p(t){return Object.keys(this.$$p_d).find(e=>this.$$p_d[e].attribute===t||!this.$$p_d[e].attribute&&e.toLowerCase()===t)||t}});function wt(t,e,n,r){let o=n[t]?.type;if(e=o==="Boolean"&&typeof e!="boolean"?e!=null:e,!r||!n[t])return e;if(r==="toAttribute")switch(o){case"Object":case"Array":return e==null?null:JSON.stringify(e);case"Boolean":return e?"":null;case"Number":return e??null;default:return e}else switch(o){case"Object":case"Array":return e&&JSON.parse(e);case"Boolean":return e;case"Number":return e!=null?+e:e;default:return e}}var at=class{$$=void 0;$$set=void 0;$destroy(){Ce(this,1),this.$destroy=B}$on(e,n){if(!ft(n))return B;let r=this.$$.callbacks[e]||(this.$$.callbacks[e]=[]);return r.push(n),()=>{let o=r.indexOf(n);o!==-1&&r.splice(o,1)}}$set(e){this.$$set&&!At(e)&&(this.$$.skip_bound=!0,this.$$set(e),this.$$.skip_bound=!1)}};var qt="4";typeof window<"u"&&(window.__svelte||(window.__svelte={v:new Set})).v.add(qt);var je=(t,e)=>new Array(t).fill(e),Le=new FinalizationRegistry(t=>{wasm_bindings.dealloc_matrix(t)}),V=(t,e,n)=>{let r={m:t,n:e,data:wasm_bindings.alloc_matrix(t,e,n)};return Le.register(r,r.data),r},P=t=>new Float32Array(wasm_bindings.memory.buffer,wasm_bindings.matrix_buffer(t.data),t.n*t.m),Et=t=>{let e=V(t.length,1,NaN);return P(e).set(t),e},Mt=t=>e=>{let[n,r]=H(e),o=V(n,r,NaN),i=P(o),s=P(e);for(let p=0;p[t,e],vt=(t,e)=>{let[n,r]=H(t),[o,i]=H(e);if(r!==o)throw"actually, mathematics forbids this.";let s=V(n,i,NaN);return wasm_bindings.gemm(t.data,e.data,s.data),s},zt=(t,e)=>{let n=vt(t,e);return n.backward=r=>{e.backward(vt(Gt(t),r)),t.backward(vt(r,Gt(e)))},n},mt=t=>(e,n)=>{let[r,o]=H(e),[i,s]=H(n);if(i!==r||s!==o)throw"no.";let p=V(r,o,0),a=P(p),u=P(e),y=P(n);for(let d=0;d(t.backward=n=>{let r=P(n),o=P(t);for(let i=0;i(t.backward=()=>{},t),Gt=t=>{let[e,n]=H(t),r=V(n,e,NaN);return wasm_bindings.transpose(t.data,r.data),r},Ie=(t,e,n)=>{let[r,o]=H(e),[i,s]=H(n);if(i!==r||s!==o)throw"no.";let p=V(r,o,NaN);return t(e.data,n.data,p.data),p},Be=(t,e)=>Ie(wasm_bindings.hadamard,t,e),Ht=(t,e)=>n=>{let r=Mt(t)(n);return r.backward=o=>n.backward(Be(o,Mt(e)(n))),r},Pe=(t,e)=>{let n=mt((r,o)=>r+o)(t,e);return n.backward=r=>{t.backward(r),e.backward(r)},n},Jt=t=>Math.min(Math.max(Math.log(t),-100),100),We=Ht(Math.tanh,t=>1-Math.tanh(t)**2),Ft=t=>1/(Math.exp(-t)+1),qe=Ht(Ft,t=>Ft(t)*(1-Ft(t))),ze=(t,e)=>{let n=mt((r,o)=>-o*Jt(r)+-(1-o)*Jt(1-r))(t,e);return n.backward=r=>{t.backward(mt((o,i)=>o*i)(r,mt((o,i)=>-i/o+-(i-1)/(1-o))(t,e)))},n},O=32,Ge=.003,Vt=t=>Mt(()=>Math.random()*2*t-t),Ut=(t={})=>{let e=t.learningRate??Ge,n=Math.sqrt(6/(O+O)),r=Math.sqrt(6/(O+1)),o=je(3,null).map(s=>({weight:kt(Vt(n)(V(O,O,NaN)),e),bias:kt(V(O,1,0),e)})),i=kt(Vt(r)(V(1,O,NaN)),e);return{layers:o,outputProjection:i,learningRate:e}},Xt=(t,e)=>{let n=Re(Et(t));for(let r of e.layers)n=Pe(zt(r.weight,n),r.bias),n=We(n);return n=zt(e.outputProjection,n),n=qe(n),n};var Yt=(t,e)=>{let n=Xt(e,t);return P(n)[0]},Zt=(t,e,n)=>{let r=Xt(e,t),o=P(r)[0];return isNaN(o)?!1:(ze(r,Et([n])).backward(Et([1])),!0)},Kt=(t,e,n={})=>{let r=n.replaySteps??4,o=n.replayWindow??64,i=n.random??Math.random,s=!1;for(t.training_step??=0;t.training_step{if(!Number.isInteger(t)||t<1)throw new RangeError("order must be a positive integer");return{order:t,priorOnes:e.priorOnes??1,priorTotal:e.priorTotal??2,counts:new Map}},te=(t,e)=>e.slice(-t.order).join(""),ee=(t,e)=>{if(e.length{if(e.length0&&pe(t);return{c(){e=h("div"),n=h("div"),r=h("button"),r.textContent="\u2190 Left",o=E(),i=h("button"),i.textContent="\u2192 Right",s=E(),p=h("br"),a=E(),u=h("table");for(let f=0;f0?A?A.p(f,G):(A=pe(f),A.c(),A.m(e,z)):A&&(A.d(1),A=null)},i:B,o:B,d(f){f&&T(e),nt(M,f),nt(S,f),nt(N,f),A&&A.d(),t[12](null),K=!1,L(Nt)}}}var gt=100,Ve=20;function Ze(t,e,n){let r=[1,2,3,4],o=["d","ArrowLeft","1"],i=["f","ArrowRight","0"],s=o.map(($,_)=>`${$}/${i[_]}`).join(", "),p=[],a=r.map(()=>[]),u=[],y,d=32,v=r.map(Qt),w=[],C=$=>{if(w.length===0)return .5;let _=[];for(let b of w)_.push(Math.min(1-1e-7,Math.max(1e-7,Yt(b,$.slice(-O)))));let m=_.map(b=>Math.log(b/(1-b))).reduce((b,g)=>b+g,0)/_.length;return 1/(1+Math.exp(-m))},k=$=>{for(v.forEach((b,g)=>{a[g].push(+(ee(b,p)>=.5)),ne(b,p,$)}),n(1,a),u.push(+(C(p)>=.5)),n(2,u),p.push($),n(0,p);!(p.lengthKt(g,p));if(b.length===w.length)break;console.warn(`Replaced ${w.length-b.length} NN model(s) after a non-finite update`),w=b}if(window.models=w,!y)return;let _=y.querySelector("table td.val"),m=y.querySelector("table td.label");_&&n(4,d=Math.floor((y.getBoundingClientRect().width-m.getBoundingClientRect().width)/_.getBoundingClientRect().width))},W=$=>{let _=p.slice(-gt),m=$.slice(-gt);return _.filter((b,g)=>b===m[g]).length/_.length},I=$=>{o.includes($.key)?k(1):i.includes($.key)&&k(0)},q=()=>k(1),z=()=>k(0);function j($){dt[$?"unshift":"push"](()=>{y=$,n(3,y)})}return[p,a,u,y,d,r,s,k,W,I,q,z,j]}var St=class extends at{constructor(e){super(),Wt(this,e,Ze,Je,Ot,{})}},_e=St;var st;function He(t,e){let n=new Uint8Array(st.exports.memory.buffer,t,e);console.log("WASM",new TextDecoder().decode(n))}var he={env:{memory:new WebAssembly.Memory({initial:5,maximum:500}),console_log_ex:He}},Ue=async()=>{if(typeof window<"u")return WebAssembly.instantiateStreaming(fetch("gemm.wasm"),he);let t=new Function("specifier","return import(specifier)"),{readFile:e}=await t("node:fs/promises");return WebAssembly.instantiate(await e("static/gemm.wasm"),he)},me=Ue().then(t=>(st=t.instance,st.exports.init_debug(),globalThis.wasm_instance=st,globalThis.wasm_bindings=st.exports,st.exports));me.then(()=>{new _e({target:document.querySelector("#app")})});})();