@hackbuild/ai-views 0.1.10 → 0.1.12

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.
@@ -7725,6 +7725,73 @@ async function euler(latent, plan, eps, signal) {
7725
7725
  return x;
7726
7726
  }
7727
7727
  var init_schedule = __esmMin((() => {}));
7728
+ // packages/imagegen/src/half.ts
7729
+ /** Half float conversion at the graph edge, for exports with fp16 inputs and outputs. */
7730
+ /** Float32 to IEEE 754 half bits, round to nearest even. */
7731
+ function toHalf(f) {
7732
+ const out = new Uint16Array(f.length);
7733
+ const b = /* @__PURE__ */ new Float32Array(1);
7734
+ const u = new Uint32Array(b.buffer);
7735
+ for (let i = 0; i < f.length; i++) {
7736
+ b[0] = f[i];
7737
+ const x = u[0];
7738
+ const sign = x >>> 16 & 32768;
7739
+ const exp = x >>> 23 & 255;
7740
+ let mant = x & 8388607;
7741
+ if (exp === 255) {
7742
+ out[i] = sign | 31744 | (mant ? 512 : 0);
7743
+ continue;
7744
+ }
7745
+ const e = exp - 127 + 15;
7746
+ if (e >= 31) {
7747
+ out[i] = sign | 31744;
7748
+ continue;
7749
+ }
7750
+ if (e <= 0) {
7751
+ if (e < -10) {
7752
+ out[i] = sign;
7753
+ continue;
7754
+ }
7755
+ mant |= 8388608;
7756
+ const shift = 14 - e;
7757
+ let h = mant >>> shift;
7758
+ const rem = mant & (1 << shift) - 1;
7759
+ const half = 1 << shift - 1;
7760
+ if (rem > half || rem === half && h & 1) h++;
7761
+ out[i] = sign | h;
7762
+ continue;
7763
+ }
7764
+ let h = e << 10 | mant >>> 13;
7765
+ const rem = mant & 8191;
7766
+ if (rem > 4096 || rem === 4096 && h & 1) h++;
7767
+ out[i] = sign | h;
7768
+ }
7769
+ return out;
7770
+ }
7771
+ /** IEEE 754 half bits to float32. */
7772
+ function fromHalf(h) {
7773
+ const out = new Float32Array(h.length);
7774
+ for (let i = 0; i < h.length; i++) {
7775
+ const v = h[i];
7776
+ const s = v & 32768 ? -1 : 1;
7777
+ const e = v >>> 10 & 31;
7778
+ const m = v & 1023;
7779
+ out[i] = e === 0 ? s * m * 2 ** -24 : e === 31 ? m ? NaN : s * Infinity : s * (1 + m / 1024) * 2 ** (e - 15);
7780
+ }
7781
+ return out;
7782
+ }
7783
+ /**
7784
+ * An output's floats. onnxruntime-web gives a float16 output as a
7785
+ * Float16Array of real values where the browser has one, and as Uint16Array
7786
+ * half bits where it does not; only the bits are decoded.
7787
+ */
7788
+ function floatsOf(t) {
7789
+ const F16 = globalThis.Float16Array;
7790
+ if (F16 && t.data instanceof F16) return Float32Array.from(t.data);
7791
+ if (t.data instanceof Uint16Array) return fromHalf(t.data);
7792
+ return Float32Array.from(t.data);
7793
+ }
7794
+ var init_half = __esmMin((() => {}));
7728
7795
  // packages/imagegen/src/pipeline.ts
7729
7796
  /** NCHW floats in [-1, 1] to RGBA bytes. */
7730
7797
  function toRgba(nchw, w, h) {
@@ -7763,10 +7830,10 @@ async function generate(ort, s, tokenize, o) {
7763
7830
  };
7764
7831
  let t = now();
7765
7832
  const encode = async (text) => {
7766
- const ids = padIds(await tokenize(text.length ? text : " "));
7833
+ const ids = padIds(await tokenize(text.length ? text : " "), 77, o.pad);
7767
7834
  const te = await run(() => s.textEncoder.run({ input_ids: new ort.Tensor("int32", ids, [1, 77]) }));
7768
7835
  const hidden = te.last_hidden_state ?? Object.values(te)[0];
7769
- return new ort.Tensor("float32", Float32Array.from(hidden.data), [...hidden.dims ?? [
7836
+ return new ort.Tensor("float32", floatsOf(hidden), [...hidden.dims ?? [
7770
7837
  1,
7771
7838
  77,
7772
7839
  hidden.data.length / 77
@@ -7800,7 +7867,7 @@ async function generate(ort, s, tokenize, o) {
7800
7867
  timestep: new ort.Tensor("int64", BigInt64Array.from([BigInt(timestep)]), [1]),
7801
7868
  encoder_hidden_states: h
7802
7869
  }));
7803
- return (u.out_sample ?? Object.values(u)[0]).data;
7870
+ return floatsOf(u.out_sample ?? Object.values(u)[0]);
7804
7871
  };
7805
7872
  const c = await unet(hiddenTensor);
7806
7873
  let e = c;
@@ -7839,13 +7906,14 @@ async function generate(ort, s, tokenize, o) {
7839
7906
  return {
7840
7907
  width: size,
7841
7908
  height: size,
7842
- rgba: toRgba((v.sample ?? Object.values(v)[0]).data, size, size),
7909
+ rgba: toRgba(floatsOf(v.sample ?? Object.values(v)[0]), size, size),
7843
7910
  seed,
7844
7911
  ms
7845
7912
  };
7846
7913
  }
7847
7914
  var VAE_SCALE;
7848
7915
  var init_pipeline = __esmMin((() => {
7916
+ init_half();
7849
7917
  init_schedule();
7850
7918
  VAE_SCALE = .18215;
7851
7919
  }));
@@ -7861,7 +7929,7 @@ function loadLabel(rec) {
7861
7929
  if (rec.sizeMB === 0) return `load ${name}, no download`;
7862
7930
  return `load ${name}, about ${rec.sizeMB >= 1e3 ? `${(rec.sizeMB / 1e3).toFixed(1)} gb` : `${rec.sizeMB} mb`} (${rec.sizeSource})`;
7863
7931
  }
7864
- var graphs, files, free512, IMAGEGEN_RECORDS, IMAGEGEN_PICKER_RECORDS;
7932
+ var graphs, files, SD15, free512, IMAGEGEN_RECORDS, IMAGEGEN_PICKER_RECORDS;
7865
7933
  var init_records$3 = __esmMin((() => {
7866
7934
  graphs = {
7867
7935
  textEncoder: "text_encoder",
@@ -7873,6 +7941,11 @@ var init_records$3 = __esmMin((() => {
7873
7941
  { path: "unet/model.onnx" },
7874
7942
  { path: "vae_decoder/model.onnx" }
7875
7943
  ];
7944
+ SD15 = [
7945
+ "Stable-Diffusion-v1.5-text-encoder-float16",
7946
+ "sd-unet-v1.5-model-b2c4h64w64s77-float16-compute-and-inputs-layernorm",
7947
+ "Stable-Diffusion-v1.5-vae-decoder-float16-fp32-instancenorm"
7948
+ ];
7876
7949
  free512 = { freeDimensionOverrides: {
7877
7950
  batch_size: 1,
7878
7951
  num_channels: 4,
@@ -7909,7 +7982,7 @@ var init_records$3 = __esmMin((() => {
7909
7982
  notes: [
7910
7983
  "webgpu only, no wasm floor",
7911
7984
  "tokenizer: Xenova/clip-vit-base-patch16 through transformers.js (CLIP weights MIT)",
7912
- "graph file paths inferred, not checked against the repo"
7985
+ "graph files, names and dtypes read from the export (measured): fp32 edges, int32 input_ids, int64 timestep; pads with token 0"
7913
7986
  ],
7914
7987
  form: {
7915
7988
  steps: {
@@ -7923,6 +7996,7 @@ var init_records$3 = __esmMin((() => {
7923
7996
  spacing: "trailing",
7924
7997
  note: "distilled from sd 2.1. 1 to 4 steps, no negative prompt. the fastest path.",
7925
7998
  tokenizer: "Xenova/clip-vit-base-patch16",
7999
+ pad: 0,
7926
8000
  graphs
7927
8001
  }
7928
8002
  }, {
@@ -7933,9 +8007,10 @@ var init_records$3 = __esmMin((() => {
7933
8007
  group: "good",
7934
8008
  provides: ["imagegen"],
7935
8009
  provider: "ort",
8010
+ unrunnable: "its unet takes a fixed batch of 2 at 64 by 64 latents with fp16 inputs (measured from the graph); the pipeline sends batch 1 in fp32.",
7936
8011
  source: {
7937
8012
  repo: "microsoft/stable-diffusion-v1.5-webnn",
7938
- files
8013
+ files: SD15.map((g) => ({ path: `${g}.onnx` }))
7939
8014
  },
7940
8015
  sizeMB: 1969,
7941
8016
  sizeSource: "vendor",
@@ -7949,7 +8024,7 @@ var init_records$3 = __esmMin((() => {
7949
8024
  place: "gpu",
7950
8025
  runtime: "onnxruntime-web@1.27.0",
7951
8026
  notes: [
7952
- "about 2 GB (vendor); the repo, its file paths and its input dtypes are inferred and not checked",
8027
+ "about 2 GB (vendor). the export's three graphs were read (measured): int32 input_ids, fp16 hidden states, a unet fixed at batch 2 with fp16 sample and int64 timestep, an fp16 vae",
7953
8028
  "runs the unet twice a step for guidance, so a step costs about twice an sd-turbo step (inferred)",
7954
8029
  "tokenizer: Xenova/clip-vit-base-patch16 through transformers.js (CLIP weights MIT)"
7955
8030
  ],
@@ -7970,7 +8045,11 @@ var init_records$3 = __esmMin((() => {
7970
8045
  spacing: "leading",
7971
8046
  note: "classic sd, openrail-m licence. 20 to 30 steps with guidance, takes a negative prompt. slower per image.",
7972
8047
  tokenizer: "Xenova/clip-vit-base-patch16",
7973
- graphs
8048
+ graphs: {
8049
+ textEncoder: SD15[0],
8050
+ unet: SD15[1],
8051
+ vaeDecoder: SD15[2]
8052
+ }
7974
8053
  }
7975
8054
  }];
7976
8055
  IMAGEGEN_PICKER_RECORDS = [{
@@ -7978,7 +8057,7 @@ var init_records$3 = __esmMin((() => {
7978
8057
  label: "sdxl-turbo 512",
7979
8058
  speed: "steady",
7980
8059
  verdict: "not run yet",
7981
- unrunnable: "not run yet. the sdxl pipeline is written; the export's graph names and fp16 io are inferred and unchecked.",
8060
+ unrunnable: "not run yet. the graph names and dtypes are read from the export's files (measured); the pipeline has not run on them.",
7982
8061
  group: "good",
7983
8062
  provides: ["imagegen"],
7984
8063
  provider: "ort",
@@ -8004,7 +8083,7 @@ var init_records$3 = __esmMin((() => {
8004
8083
  runtime: "onnxruntime-web@1.27.0",
8005
8084
  notes: [
8006
8085
  "two text encoders, pooled embeddings and time ids, on generateXl",
8007
- "fp16 inputs and outputs per the export's README (--force_fp16_inputs); timestep dtype and hidden state names inferred",
8086
+ "fp16 edges and a float16 timestep; input_ids int32 for the first encoder and int64 for the second (measured from the graphs)",
8008
8087
  "the highest quality of the three image models"
8009
8088
  ],
8010
8089
  form: {
@@ -8028,70 +8107,13 @@ var init_records$3 = __esmMin((() => {
8028
8107
  pipeline: "sdxl",
8029
8108
  xl: {
8030
8109
  io: "float16",
8031
- timestep: "float16"
8110
+ timestep: "float16",
8111
+ ids2: "int64"
8032
8112
  }
8033
8113
  }
8034
8114
  }];
8035
8115
  }));
8036
8116
  // packages/imagegen/src/xl.ts
8037
- /** Float32 to IEEE 754 half bits, round to nearest even. */
8038
- function toHalf(f) {
8039
- const out = new Uint16Array(f.length);
8040
- const b = /* @__PURE__ */ new Float32Array(1);
8041
- const u = new Uint32Array(b.buffer);
8042
- for (let i = 0; i < f.length; i++) {
8043
- b[0] = f[i];
8044
- const x = u[0];
8045
- const sign = x >>> 16 & 32768;
8046
- const exp = x >>> 23 & 255;
8047
- let mant = x & 8388607;
8048
- if (exp === 255) {
8049
- out[i] = sign | 31744 | (mant ? 512 : 0);
8050
- continue;
8051
- }
8052
- const e = exp - 127 + 15;
8053
- if (e >= 31) {
8054
- out[i] = sign | 31744;
8055
- continue;
8056
- }
8057
- if (e <= 0) {
8058
- if (e < -10) {
8059
- out[i] = sign;
8060
- continue;
8061
- }
8062
- mant |= 8388608;
8063
- const shift = 14 - e;
8064
- let h = mant >>> shift;
8065
- const rem = mant & (1 << shift) - 1;
8066
- const half = 1 << shift - 1;
8067
- if (rem > half || rem === half && h & 1) h++;
8068
- out[i] = sign | h;
8069
- continue;
8070
- }
8071
- let h = e << 10 | mant >>> 13;
8072
- const rem = mant & 8191;
8073
- if (rem > 4096 || rem === 4096 && h & 1) h++;
8074
- out[i] = sign | h;
8075
- }
8076
- return out;
8077
- }
8078
- /** IEEE 754 half bits to float32. */
8079
- function fromHalf(h) {
8080
- const out = new Float32Array(h.length);
8081
- for (let i = 0; i < h.length; i++) {
8082
- const v = h[i];
8083
- const s = v & 32768 ? -1 : 1;
8084
- const e = v >>> 10 & 31;
8085
- const m = v & 1023;
8086
- out[i] = e === 0 ? s * m * 2 ** -24 : e === 31 ? m ? NaN : s * Infinity : s * (1 + m / 1024) * 2 ** (e - 15);
8087
- }
8088
- return out;
8089
- }
8090
- /** An output's floats; fp16 outputs (type "float16" or Uint16Array data) are decoded. */
8091
- function floatsOf(t) {
8092
- if (t.type === "float16" || t.data instanceof Uint16Array) return fromHalf(t.data);
8093
- return Float32Array.from(t.data);
8094
- }
8095
8117
  /** CLIP ids for the second encoder: up to the first end token, then `pad` to 77. */
8096
8118
  function padIds2(ids, pad = 0) {
8097
8119
  const list = Array.from(ids);
@@ -8132,8 +8154,9 @@ async function generateXl(ort, s, tokenize, o) {
8132
8154
  let t = now();
8133
8155
  const encode = async (text) => {
8134
8156
  const ids = await tokenize(text.length ? text : " ");
8135
- const a = await run(() => s.textEncoder.run({ input_ids: new ort.Tensor("int32", padIds(ids), [1, 77]) }));
8136
- const b = await run(() => s.textEncoder2.run({ input_ids: new ort.Tensor("int32", padIds2(ids, io.pad2), [1, 77]) }));
8157
+ const idsOf = (v, dtype) => dtype === "int64" ? new ort.Tensor("int64", BigInt64Array.from(v, (x) => BigInt(x)), [1, 77]) : new ort.Tensor("int32", v, [1, 77]);
8158
+ const a = await run(() => s.textEncoder.run({ input_ids: idsOf(padIds(ids), io.ids1) }));
8159
+ const b = await run(() => s.textEncoder2.run({ input_ids: idsOf(padIds2(ids, io.pad2), io.ids2) }));
8137
8160
  const h1 = floatsOf(pick(a, io.hidden1, ["last_hidden_state"], "text encoder"));
8138
8161
  const h2 = floatsOf(pick(b, io.hidden2, ["last_hidden_state"], "text encoder 2"));
8139
8162
  const pooled = floatsOf(pick(b, io.pooled, ["pooler_output"], "text encoder 2"));
@@ -8239,9 +8262,12 @@ var XL_IO, EOT;
8239
8262
  var init_xl = __esmMin((() => {
8240
8263
  init_pipeline();
8241
8264
  init_schedule();
8265
+ init_half();
8242
8266
  XL_IO = {
8243
8267
  io: "float32",
8244
8268
  timestep: "int64",
8269
+ ids1: "int32",
8270
+ ids2: "int32",
8245
8271
  hidden1: "hidden_states.11",
8246
8272
  hidden2: "hidden_states.31",
8247
8273
  pooled: "text_embeds",
@@ -8284,6 +8310,7 @@ function applyForm(form, o) {
8284
8310
  if (form.guidance) out.guidance = Math.min(form.guidance.max, Math.max(form.guidance.min, o.guidance ?? form.guidance.default));
8285
8311
  else delete out.guidance;
8286
8312
  if (!form.negative) delete out.negative;
8313
+ if (form.pad !== void 0) out.pad = form.pad;
8287
8314
  return out;
8288
8315
  }
8289
8316
  /**
@@ -16573,7 +16600,7 @@ function esc(s) {
16573
16600
  }
16574
16601
  /** Inline SVG icons, stroked in currentColor, 16 px. */
16575
16602
  const svg = (d, size = 16) => `<svg class="hb-icon is-stroke" width="${size}" height="${size}" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="square" aria-hidden="true">${d}</svg>`;
16576
- svg("<path d=\"M9 14 4 9l5-5\"/><path d=\"M4 9h10a6 6 0 0 1 0 12h-3\"/>"), svg("<path d=\"m15 14 5-5-5-5\"/><path d=\"M20 9H10a6 6 0 0 0 0 12h3\"/>"), svg("<circle cx=\"11\" cy=\"11\" r=\"7\"/><path d=\"M21 21l-4.3-4.3M11 8v6M8 11h6\"/>"), svg("<circle cx=\"11\" cy=\"11\" r=\"7\"/><path d=\"M21 21l-4.3-4.3M8 11h6\"/>"), svg("<path d=\"M4 9V4h5M20 9V4h-5M4 15v5h5M20 15v5h-5\"/>"), svg("<path d=\"M6 6l12 12M18 6 6 18\"/>", 13), svg("<rect x=\"9\" y=\"9\" width=\"11\" height=\"11\"/><path d=\"M5 15V4h11\"/>", 14), svg("<path d=\"M21 12a9 9 0 1 1-3-6.7L21 8\"/><path d=\"M21 3v5h-5\"/>", 14), svg("<circle cx=\"12\" cy=\"12\" r=\"3\"/><path d=\"M12 2v5M12 17v5M2 12h5M17 12h5\"/>", 18), svg("<path d=\"m3 21 12-12M15 3v3M18 6h3M19 2l-1 1M14 9l1 1\"/>", 18), svg("<path d=\"M18 3 9 12l3 3 9-9z\"/><path d=\"M9 12c-3 0-5 2-5 5 0 2-1 3-2 4 4 0 8-1 9-4\"/>", 18), svg("<ellipse cx=\"12\" cy=\"9\" rx=\"8\" ry=\"5\"/><path d=\"M7 13c-1 2 0 4 2 4s2 3 0 5\"/>", 18), svg("<rect x=\"3\" y=\"3\" width=\"7\" height=\"7\"/><rect x=\"14\" y=\"3\" width=\"7\" height=\"7\"/><rect x=\"3\" y=\"14\" width=\"7\" height=\"7\"/><rect x=\"14\" y=\"14\" width=\"7\" height=\"7\"/>", 18), svg("<path d=\"M3 7h4l2-3h6l2 3h4v13H3z\"/><circle cx=\"12\" cy=\"13\" r=\"4\"/>"), svg("<path d=\"M4 6h16M4 12h16M4 18h16\"/>"), svg("<path d=\"M12 5v14M5 12h14\"/>", 14), svg("<path d=\"M4 20h4L19 9l-4-4L4 16z\"/>", 13), svg("<path d=\"M4 7h16M9 7V4h6v3M6 7l1 13h10l1-13\"/>", 13);
16603
+ svg("<path d=\"M9 14 4 9l5-5\"/><path d=\"M4 9h10a6 6 0 0 1 0 12h-3\"/>"), svg("<path d=\"m15 14 5-5-5-5\"/><path d=\"M20 9H10a6 6 0 0 0 0 12h3\"/>"), svg("<circle cx=\"11\" cy=\"11\" r=\"7\"/><path d=\"M21 21l-4.3-4.3M11 8v6M8 11h6\"/>"), svg("<circle cx=\"11\" cy=\"11\" r=\"7\"/><path d=\"M21 21l-4.3-4.3M8 11h6\"/>"), svg("<path d=\"M4 9V4h5M20 9V4h-5M4 15v5h5M20 15v5h-5\"/>"), svg("<path d=\"M6 6l12 12M18 6 6 18\"/>", 14), svg("<rect x=\"9\" y=\"9\" width=\"11\" height=\"11\"/><path d=\"M5 15V4h11\"/>", 14), svg("<path d=\"M21 12a9 9 0 1 1-3-6.7L21 8\"/><path d=\"M21 3v5h-5\"/>", 14), svg("<circle cx=\"12\" cy=\"12\" r=\"3\"/><path d=\"M12 2v5M12 17v5M2 12h5M17 12h5\"/>", 18), svg("<path d=\"m3 21 12-12M15 3v3M18 6h3M19 2l-1 1M14 9l1 1\"/>", 18), svg("<path d=\"M18 3 9 12l3 3 9-9z\"/><path d=\"M9 12c-3 0-5 2-5 5 0 2-1 3-2 4 4 0 8-1 9-4\"/>", 18), svg("<ellipse cx=\"12\" cy=\"9\" rx=\"8\" ry=\"5\"/><path d=\"M7 13c-1 2 0 4 2 4s2 3 0 5\"/>", 18), svg("<rect x=\"3\" y=\"3\" width=\"7\" height=\"7\"/><rect x=\"14\" y=\"3\" width=\"7\" height=\"7\"/><rect x=\"3\" y=\"14\" width=\"7\" height=\"7\"/><rect x=\"14\" y=\"14\" width=\"7\" height=\"7\"/>", 18), svg("<path d=\"M3 7h4l2-3h6l2 3h4v13H3z\"/><circle cx=\"12\" cy=\"13\" r=\"4\"/>"), svg("<path d=\"M4 6h16M4 12h16M4 18h16\"/>"), svg("<path d=\"M12 5v14M5 12h14\"/>", 14), svg("<path d=\"M4 20h4L19 9l-4-4L4 16z\"/>", 14), svg("<path d=\"M4 7h16M9 7V4h6v3M6 7l1 13h10l1-13\"/>", 14);
16577
16604
  // packages/ai-views/src/memory.ts
16578
16605
  const n3 = (x) => Number.isFinite(x) ? x.toFixed(3) : "none";
16579
16606
  const rank = (r) => r >= 0 ? `#${r + 1}` : "none";