Skip to content

Commit 4c99370

Browse files
committed
Parallel Download, Move ArtifactCache to Interface to support future different cache types, fix README path typo, Support delete and batch delete
1 parent f21c5a4 commit 4c99370

File tree

6 files changed

+68
-11
lines changed

6 files changed

+68
-11
lines changed

web/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,4 @@ Right now we use the SPIRV to generate shaders that can be accepted by Chrome an
9494
- Firefox should be close pending the support of Fence.
9595
- Download vulkan SDK (1.1 or higher) that supports SPIRV 1.3
9696
- Start the WebSocket RPC
97-
- run `python tests/node/webgpu_rpc_test.py`
97+
- run `python tests/python/webgpu_rpc_test.py`

web/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web/src/artifact_cache.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
Common Interface for the artifact cache
3+
*/
4+
export interface ArtifactCacheTemplate {
5+
/**
6+
* fetch key url from cache
7+
*/
8+
fetchWithCache(url: string);
9+
10+
/**
11+
* check if cache has all keys in Cache
12+
*/
13+
hasAllKeys(keys: string[]);
14+
15+
/**
16+
* Delete url in cache if url exists
17+
*/
18+
deleteInCache(url: string);
19+
}

web/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export {
2222
PackedFunc, Module, NDArray,
2323
TVMArray, TVMObject, VirtualMachine,
2424
InitProgressCallback, InitProgressReport,
25-
ArtifactCache, Instance, instantiate, hasNDArrayInCache
25+
ArtifactCache, Instance, instantiate, hasNDArrayInCache, deleteNDArrayCache
2626
} from "./runtime";
2727
export { Disposable, LibraryProvider } from "./types";
2828
export { RPCServer } from "./rpc_server";

web/src/runtime.ts

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { Memory, CachedCallStack } from "./memory";
2626
import { assert, StringToUint8Array } from "./support";
2727
import { Environment } from "./environment";
2828
import { FunctionInfo, WebGPUContext } from "./webgpu";
29+
import { ArtifactCacheTemplate } from "./artifact_cache";
2930

3031
import * as compact from "./compact";
3132
import * as ctypes from "./ctypes";
@@ -985,7 +986,7 @@ export type InitProgressCallback = (report: InitProgressReport) => void;
985986
/**
986987
* Cache to store model related data.
987988
*/
988-
export class ArtifactCache {
989+
export class ArtifactCache implements ArtifactCacheTemplate {
989990
private scope: string;
990991
private cache?: Cache;
991992

@@ -1018,6 +1019,14 @@ export class ArtifactCache {
10181019
.then(cacheKeys => keys.every(key => cacheKeys.indexOf(key) !== -1))
10191020
.catch(err => false);
10201021
}
1022+
1023+
async deleteInCache(url: string) {
1024+
if (this.cache === undefined) {
1025+
this.cache = await caches.open(this.scope);
1026+
}
1027+
const result = await this.cache.delete(url);
1028+
return result;
1029+
}
10211030
}
10221031

10231032
/**
@@ -1451,7 +1460,7 @@ export class Instance implements Disposable {
14511460
}
14521461

14531462
/**
1454-
* Fetch NDArray cache from url.
1463+
* Given cacheUrl, search up items to fetch based on cacheUrl/ndarray-cache.json
14551464
*
14561465
* @param ndarrayCacheUrl The cache url.
14571466
* @param device The device to be fetched to.
@@ -1477,6 +1486,7 @@ export class Instance implements Disposable {
14771486
this.cacheMetadata = { ...this.cacheMetadata, ...(list["metadata"] as Record<string, any>) };
14781487
}
14791488

1489+
14801490
/**
14811491
* Fetch list of NDArray into the NDArrayCache.
14821492
*
@@ -1489,7 +1499,7 @@ export class Instance implements Disposable {
14891499
ndarrayCacheUrl: string,
14901500
list: Array<NDArrayShardEntry>,
14911501
device: DLDevice,
1492-
artifactCache: ArtifactCache
1502+
artifactCache: ArtifactCacheTemplate
14931503
) {
14941504
const perf = compact.getPerformance();
14951505
const tstart = perf.now();
@@ -1536,18 +1546,19 @@ export class Instance implements Disposable {
15361546
});
15371547
}
15381548

1539-
for (let i = 0; i < list.length; ++i) {
1549+
const processShard = async (i: number) => {
15401550
reportCallback(i);
1541-
fetchedBytes += list[i].nbytes;
1542-
const dataUrl = new URL(list[i].dataPath, ndarrayCacheUrl).href;
1551+
const shard = list[i];
1552+
fetchedBytes += shard.nbytes;
1553+
const dataUrl = new URL(shard.dataPath, ndarrayCacheUrl).href;
15431554
let buffer;
15441555
try {
15451556
buffer = await (await artifactCache.fetchWithCache(dataUrl)).arrayBuffer();
15461557
} catch (err) {
15471558
this.env.logger("Error: Cannot fetch " + dataUrl + " err= " + err);
15481559
throw err;
15491560
}
1550-
const shardRecords = list[i].records;
1561+
const shardRecords = shard.records;
15511562
for (let j = 0; j < shardRecords.length; ++j) {
15521563
const rec = shardRecords[j];
15531564
const cpu_arr = this.withNewScope(() => {
@@ -1578,6 +1589,7 @@ export class Instance implements Disposable {
15781589
}
15791590
timeElapsed = Math.ceil((perf.now() - tstart) / 1000);
15801591
}
1592+
await Promise.all(list.map((_, index) => processShard(index)));
15811593
reportCallback(list.length);
15821594
}
15831595

@@ -2432,3 +2444,28 @@ export async function hasNDArrayInCache(
24322444
list = list["records"] as Array<NDArrayShardEntry>;
24332445
return await artifactCache.hasAllKeys(list.map(key => new URL(key.dataPath, ndarrayCacheUrl).href));
24342446
}
2447+
2448+
/**
2449+
* Given cacheUrl, search up items to delete based on cacheUrl/ndarray-cache.json
2450+
*
2451+
* @param ndarrayCacheUrl
2452+
* @param cacheScope
2453+
*/
2454+
export async function deleteNDArrayCache(
2455+
ndarrayCacheUrl: string,
2456+
cacheScope = "tvmjs"
2457+
) {
2458+
const artifactCache = new ArtifactCache(cacheScope);
2459+
const jsonUrl = new URL("ndarray-cache.json", ndarrayCacheUrl).href;
2460+
const result = await artifactCache.fetchWithCache(jsonUrl);
2461+
let list;
2462+
if (result instanceof Response){
2463+
list = await result.json();
2464+
}
2465+
const arrayentry = list["records"] as Array<NDArrayShardEntry>;
2466+
const processShard = async (i: number) => {
2467+
const dataUrl = new URL(arrayentry[i].dataPath, ndarrayCacheUrl).href;
2468+
await(await artifactCache.deleteInCache(dataUrl));
2469+
}
2470+
await Promise.all(arrayentry.map((_, index) => processShard(index)));
2471+
}

web/tvm_home

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/ssd1/hangruic/tvm/

0 commit comments

Comments
 (0)