File size: 5,430 Bytes
80fd73c
 
78e34bc
eccf3fa
 
 
 
 
 
 
 
 
 
 
 
 
7be4bb6
 
 
 
 
 
 
 
 
78e34bc
eccf3fa
7be4bb6
 
 
 
 
 
 
 
 
 
 
 
 
78e34bc
7be4bb6
 
 
 
 
 
78e34bc
7be4bb6
 
 
 
 
 
 
eccf3fa
7be4bb6
 
 
 
 
 
 
 
 
 
 
 
 
eccf3fa
7be4bb6
 
 
 
 
 
 
 
27da4cd
eccf3fa
27da4cd
eccf3fa
 
 
 
 
 
 
 
27da4cd
 
 
 
 
eccf3fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80fd73c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
"use strict";

function updateGetModelProgress(name, loaded, total) {
  const progressElement = document.getElementById('model-progress');
  if (total === 0) {
    progressElement.innerHTML = `Model ${name} is already in local cache`;
    return;
  }
  const percent = ((loaded / total) * 100).toFixed(2);
  let progress;
  if (loaded >= total) {
    progress = "Downloaded";
  } else {
    progress = "Downloading";
  }
  progressElement.innerHTML = `${progress} model ${name}: Total ${total} bytes, loaded ${loaded} bytes, ${percent}%`;
}

// Get model via Origin Private File System
async function getModelOPFS(name, url, updateModel) {
  const root = await navigator.storage.getDirectory();
  let fileHandle;

  async function updateFile() {
    const response = await fetch(url);
    const buffer = await readResponse(name, response, updateGetModelProgress);
    fileHandle = await root.getFileHandle(name, { create: true });
    const writable = await fileHandle.createWritable();
    await writable.write(buffer);
    await writable.close();
    return buffer;
  }

  if (updateModel) {
    return await updateFile();
  }

  try {
    fileHandle = await root.getFileHandle(name);
    const blob = await fileHandle.getFile();
    updateGetModelProgress(name, 0, 0);
    return await blob.arrayBuffer();
  } catch (e) {
    return await updateFile();
  }
}

async function readResponse(name, response, progressCallback) {
  const contentLength = response.headers.get('Content-Length');
  let total = parseInt(contentLength ?? '0');
  let buffer = new Uint8Array(total);
  let loaded = 0;

  const reader = response.body.getReader();
  async function read() {
    const { done, value } = await reader.read();
    if (done) return;

    let newLoaded = loaded + value.length;
    if (newLoaded > total) {
      total = newLoaded;
      let newBuffer = new Uint8Array(total);
      newBuffer.set(buffer);
      buffer = newBuffer;
    }
    buffer.set(value, loaded);
    loaded = newLoaded;

    if (progressCallback) {
      progressCallback(name, loaded, total);
    }

    return read();
  }

  await read();
  return buffer;
}

function isInternal() {
  if (window.location.href.includes('intel')) {
    return true;
  } else {
    return false;
  }
}

function getModelsPath() {
  if (isInternal()) {
    return "https://wp-27.sh.intel.com/workspace/project/models/";
  } else {
    return "https://huggingface.co/webai-community/models/resolve/main/";
  }
}

function getParam(name, type, _default) {
  name = name.replace(/[\[\]]/g, "\\$&");
  let regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)", "i");
  let results = regex.exec(window.location.href);
  if (!results || !results[2]) return _default;

  const result = decodeURIComponent(results[2].replace(/\+/g, " "));
  if (type === "Boolean") {
    if (result === "true") {
      return true;
    } else if (result === "false") {
      return false;
    }
  } else if (type === "Number") {
    return parseInt(result);
  } else {
    return result;
  }
}

async function loadOrt() {
  const ortUrl = getParam("ortUrl", "String", "default");
  if (ortUrl !== "default") {
    await loadScript(ortUrl);
  } else if (isInternal()) {
    await loadScript("https://wp-27.sh.intel.com/workspace/project/onnxruntime/js/web/dist/ort.webgpu.min.js");
  } else {
    await loadScript("https://cdn.jsdelivr.net/npm/onnxruntime-web@dev/dist/ort.webgpu.min.js");
  }
}

async function loadScript(url) {
  return new Promise((resolve, reject) => {
    const script = document.createElement('script');
    script.onload = resolve;
    script.onerror = reject;
    script.src = url;
    if (url.startsWith('http')) {
      script.crossOrigin = 'anonymous';
    }
    document.body.append(script);
  })
}

/*
const webgpuCanvas = new OffscreenCanvas(canvasWidth, canvasHeight);
const webglCanvas = new OffscreenCanvas(canvasWidth, canvasHeight);
const gl = webglCanvas.getContext('webgl2');
const webglTexture = gl.createTexture();
*/
function readGPUBufferSync(buffer, byteOffset, webgpuCanvas, gl, glTexture, device) {
  const bufferSize = buffer.size;
  const dataBytes = bufferSize - byteOffset;

  const canvasWidth = 8192;
  const canvasHeight = dataBytes / 4 / canvasWidth + 1;

  // Copy WebGPU buffer to WebGPU texture
  const webgpuContext = webgpuCanvas.getContext('webgpu');
  webgpuContext.configure({
    device: device,
    format: 'rgb8unorm',
    usage: GPUTextureUsage.COPY_DST,
    alphaMode: 'premultiplied',
  });
  const webgpuTexture = webgpuContext.getCurrentTexture();
  const bytesPerRow = canvasWidth * 4;
  const commandEncoder = device.createCommandEncoder();
  commandEncoder.copyBufferToTexture(
    {
      buffer,
      bytesPerRow,
      byteOffset,
    },
    {
      webgpuTexture,
    },
    {
      canvasWidth,
      canvasHeight,
    });
  device.queue.submit([commandEncoder.finish()]);
  commandEncoder = null;

  // Read WebGPU texture via WebGL
  gl.bindTexture(gl.TEXTURE_2D, webglTexture);
  gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
  gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA8, gl.RGBA, gl.UNSIGNED_BYTE, webgpuCanvas);
  gl.bindFramebuffer(gl.FRAMEBUFFER, gl.createFramebuffer());
  gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, webglTexture, 0);
  const pixels = new Uint8Array(dataBytes);
  gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, pixels);

  return pixels;
}