Spaces:
Runtime error
Runtime error
Khunndev Sisyphus commited on
Commit ·
76b467a
1
Parent(s): b505c60
Write the inference tensor in CHW layout in a single pass to halve per-tile allocation.
Browse filesUltraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)
Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
frontend/src/lib/preprocess.ts
CHANGED
|
@@ -37,20 +37,16 @@ export function canvasToTensor(canvas: HTMLCanvasElement, size = INFERENCE_SIZE)
|
|
| 37 |
throw new Error('Canvas 2D not available')
|
| 38 |
}
|
| 39 |
const { data } = ctx.getImageData(0, 0, size, size)
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
tensor[p++] = data[i] / 255
|
| 44 |
-
tensor[p++] = data[i + 1] / 255
|
| 45 |
-
tensor[p++] = data[i + 2] / 255
|
| 46 |
-
}
|
| 47 |
-
|
| 48 |
-
const chw = new Float32Array(3 * size * size)
|
| 49 |
const plane = size * size
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
chw[
|
|
|
|
|
|
|
| 54 |
}
|
| 55 |
return chw
|
| 56 |
}
|
|
|
|
| 37 |
throw new Error('Canvas 2D not available')
|
| 38 |
}
|
| 39 |
const { data } = ctx.getImageData(0, 0, size, size)
|
| 40 |
+
// Write straight into CHW layout (R-plane | G-plane | B-plane) in a single
|
| 41 |
+
// pass. Previously this allocated a second 12.5MB Float32Array (at 1024) and
|
| 42 |
+
// transposed into it — pointless across 16-25 tiles per count.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
const plane = size * size
|
| 44 |
+
const chw = new Float32Array(3 * plane)
|
| 45 |
+
for (let p = 0; p < plane; p++) {
|
| 46 |
+
const i = p * 4
|
| 47 |
+
chw[p] = data[i] / 255
|
| 48 |
+
chw[plane + p] = data[i + 1] / 255
|
| 49 |
+
chw[2 * plane + p] = data[i + 2] / 255
|
| 50 |
}
|
| 51 |
return chw
|
| 52 |
}
|