Spaces:
Runtime error
Runtime error
Khunndev Sisyphus commited on
Commit ·
0fdcb52
1
Parent(s): 76b467a
Run tile inference concurrently with memory-aware concurrency to cut counting wall-clock time.
Browse filesUltraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)
Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
- frontend/src/lib/inference.ts +56 -18
frontend/src/lib/inference.ts
CHANGED
|
@@ -271,6 +271,37 @@ export async function runDetection(
|
|
| 271 |
}))
|
| 272 |
}
|
| 273 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
async function runTiledDetection(
|
| 275 |
image: HTMLImageElement,
|
| 276 |
session: ort.InferenceSession,
|
|
@@ -297,37 +328,44 @@ async function runTiledDetection(
|
|
| 297 |
? enhanceImageElement(image, options?.enhanceOptions)
|
| 298 |
: image
|
| 299 |
|
| 300 |
-
|
| 301 |
-
|
|
|
|
| 302 |
for (let gy = 0; gy < grid; gy++) {
|
| 303 |
for (let gx = 0; gx < grid; gx++) {
|
| 304 |
-
tileIndex++
|
| 305 |
-
onProgress?.(`Tile ${tileIndex}/${totalTiles}...`)
|
| 306 |
const x1 = Math.max(0, gx * tileW - (tileW * overlap * (gx > 0 ? 1 : 0)))
|
| 307 |
const y1 = Math.max(0, gy * tileH - (tileH * overlap * (gy > 0 ? 1 : 0)))
|
| 308 |
const x2 = Math.min(w, (gx + 1) * tileW + (tileW * overlap * (gx < grid - 1 ? 1 : 0)))
|
| 309 |
const y2 = Math.min(h, (gy + 1) * tileH + (tileH * overlap * (gy < grid - 1 ? 1 : 0)))
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 313 |
const tileCanvas = document.createElement('canvas')
|
| 314 |
-
tileCanvas.width = cropW
|
| 315 |
-
tileCanvas.height = cropH
|
| 316 |
const ctx = tileCanvas.getContext('2d')!
|
| 317 |
-
ctx.drawImage(enhanced, x1, y1, cropW, cropH, 0, 0, cropW, cropH)
|
| 318 |
-
|
| 319 |
const dets = await inferCanvas(
|
| 320 |
tileCanvas,
|
| 321 |
session,
|
| 322 |
inferenceSize,
|
| 323 |
-
x1,
|
| 324 |
-
y1,
|
| 325 |
-
cropW,
|
| 326 |
-
cropH,
|
| 327 |
)
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 331 |
|
| 332 |
const merged = reassignDetectionIds(
|
| 333 |
postProcessPipeline(all, {
|
|
|
|
| 271 |
}))
|
| 272 |
}
|
| 273 |
|
| 274 |
+
// How many tiles may be inferred in flight at once. Tile inference is the
|
| 275 |
+
// dominant cost on dense bundles, and the old loop ran every tile strictly
|
| 276 |
+
// sequentially. Overlapping tiles lets CPU tensor-prep of the next tile run
|
| 277 |
+
// while the previous tile's session.run is in flight (and on WebGPU the GPU
|
| 278 |
+
// pipelines the runs). Each in-flight 1024 tensor + output is ~25MB+, so we
|
| 279 |
+
// stay sequential on low-RAM phones to avoid OOM.
|
| 280 |
+
function tileConcurrency(): number {
|
| 281 |
+
if (isLowMemoryDevice()) return 1
|
| 282 |
+
if (getActiveBackend() === 'webgpu') return 4
|
| 283 |
+
return 2
|
| 284 |
+
}
|
| 285 |
+
|
| 286 |
+
async function runWithConcurrency<T, R>(
|
| 287 |
+
items: readonly T[],
|
| 288 |
+
worker: (item: T) => Promise<R>,
|
| 289 |
+
concurrency: number,
|
| 290 |
+
): Promise<R[]> {
|
| 291 |
+
const results: R[] = new Array(items.length)
|
| 292 |
+
let cursor = 0
|
| 293 |
+
const runLane = async (): Promise<void> => {
|
| 294 |
+
while (cursor < items.length) {
|
| 295 |
+
const index = cursor
|
| 296 |
+
cursor += 1
|
| 297 |
+
results[index] = await worker(items[index])
|
| 298 |
+
}
|
| 299 |
+
}
|
| 300 |
+
const lanes = Math.max(1, Math.min(concurrency, items.length))
|
| 301 |
+
await Promise.all(Array.from({ length: lanes }, runLane))
|
| 302 |
+
return results
|
| 303 |
+
}
|
| 304 |
+
|
| 305 |
async function runTiledDetection(
|
| 306 |
image: HTMLImageElement,
|
| 307 |
session: ort.InferenceSession,
|
|
|
|
| 328 |
? enhanceImageElement(image, options?.enhanceOptions)
|
| 329 |
: image
|
| 330 |
|
| 331 |
+
// Build tile rects (unchanged geometry), then infer with a memory-aware
|
| 332 |
+
// concurrency limit instead of a strictly sequential loop.
|
| 333 |
+
const rects: Array<{ x1: number; y1: number; cropW: number; cropH: number }> = []
|
| 334 |
for (let gy = 0; gy < grid; gy++) {
|
| 335 |
for (let gx = 0; gx < grid; gx++) {
|
|
|
|
|
|
|
| 336 |
const x1 = Math.max(0, gx * tileW - (tileW * overlap * (gx > 0 ? 1 : 0)))
|
| 337 |
const y1 = Math.max(0, gy * tileH - (tileH * overlap * (gy > 0 ? 1 : 0)))
|
| 338 |
const x2 = Math.min(w, (gx + 1) * tileW + (tileW * overlap * (gx < grid - 1 ? 1 : 0)))
|
| 339 |
const y2 = Math.min(h, (gy + 1) * tileH + (tileH * overlap * (gy < grid - 1 ? 1 : 0)))
|
| 340 |
+
rects.push({ x1, y1, cropW: x2 - x1, cropH: y2 - y1 })
|
| 341 |
+
}
|
| 342 |
+
}
|
| 343 |
+
const totalTiles = rects.length
|
| 344 |
+
let done = 0
|
| 345 |
+
const tileBatches = await runWithConcurrency(
|
| 346 |
+
rects,
|
| 347 |
+
async (r) => {
|
| 348 |
const tileCanvas = document.createElement('canvas')
|
| 349 |
+
tileCanvas.width = r.cropW
|
| 350 |
+
tileCanvas.height = r.cropH
|
| 351 |
const ctx = tileCanvas.getContext('2d')!
|
| 352 |
+
ctx.drawImage(enhanced, r.x1, r.y1, r.cropW, r.cropH, 0, 0, r.cropW, r.cropH)
|
|
|
|
| 353 |
const dets = await inferCanvas(
|
| 354 |
tileCanvas,
|
| 355 |
session,
|
| 356 |
inferenceSize,
|
| 357 |
+
r.x1,
|
| 358 |
+
r.y1,
|
| 359 |
+
r.cropW,
|
| 360 |
+
r.cropH,
|
| 361 |
)
|
| 362 |
+
done += 1
|
| 363 |
+
onProgress?.(`Tile ${done}/${totalTiles}...`)
|
| 364 |
+
return dets
|
| 365 |
+
},
|
| 366 |
+
tileConcurrency(),
|
| 367 |
+
)
|
| 368 |
+
for (const dets of tileBatches) all.push(...dets)
|
| 369 |
|
| 370 |
const merged = reassignDetectionIds(
|
| 371 |
postProcessPipeline(all, {
|