Skip to content

Commit

Permalink
Move ArtifactCache to Interface to support future different cache typ…
Browse files Browse the repository at this point in the history
…es, fix README path typo, Support delete and batch delete
  • Loading branch information
DiegoCao committed Feb 12, 2024
1 parent f21c5a4 commit 4b55a1b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
2 changes: 1 addition & 1 deletion web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,4 @@ Right now we use the SPIRV to generate shaders that can be accepted by Chrome an
- Firefox should be close pending the support of Fence.
- Download vulkan SDK (1.1 or higher) that supports SPIRV 1.3
- Start the WebSocket RPC
- run `python tests/node/webgpu_rpc_test.py`
- run `python tests/python/webgpu_rpc_test.py`
19 changes: 19 additions & 0 deletions web/src/artifact_cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
Common Interface for the artifact cache
*/
export interface ArtifactCacheTemplate {
/**
* fetch key url from cache
*/
fetchWithCache(url: string);

/**
* check if cache has all keys in Cache
*/
hasAllKeys(keys: string[]);

/**
* Delete url in cache if url exists
*/
deleteInCache(url: string);
}
51 changes: 49 additions & 2 deletions web/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { Memory, CachedCallStack } from "./memory";
import { assert, StringToUint8Array } from "./support";
import { Environment } from "./environment";
import { FunctionInfo, WebGPUContext } from "./webgpu";
import { ArtifactCacheTemplate } from "./artifact_cache";

import * as compact from "./compact";
import * as ctypes from "./ctypes";
Expand Down Expand Up @@ -985,7 +986,7 @@ export type InitProgressCallback = (report: InitProgressReport) => void;
/**
* Cache to store model related data.
*/
export class ArtifactCache {
export class ArtifactCache implements ArtifactCacheTemplate {
private scope: string;
private cache?: Cache;

Expand Down Expand Up @@ -1018,6 +1019,17 @@ export class ArtifactCache {
.then(cacheKeys => keys.every(key => cacheKeys.indexOf(key) !== -1))
.catch(err => false);
}

async deleteInCache(url: string) {
if (this.cache === undefined) {
this.cache = await caches.open(this.scope);
}
const result = await this.cache.delete(url);
if (result === undefined){
throw Error("Cannot delete " + url);
}
return result;
}
}

/**
Expand Down Expand Up @@ -1477,6 +1489,41 @@ export class Instance implements Disposable {
this.cacheMetadata = { ...this.cacheMetadata, ...(list["metadata"] as Record<string, any>) };
}

/**
* Delete NDArray from cache url.
*
* @param ndarrayCacheUrl
* @param device
* @param cacheScope
*/
async deleteNDArrayCache(
ndarrayCacheUrl: string,
device: DLDevice,
cacheScope = "tvmjs"
): Promise<any>{
const artifactCache = new ArtifactCache(cacheScope);
const jsonUrl = new URL("ndarray-cache.json", ndarrayCacheUrl).href;
const result = await artifactCache.fetchWithCache(jsonUrl);
let list;
if (result instanceof Response){
list = await result.json();
}
const arrayentry = list["records"] as Array<NDArrayShardEntry>;
for (let i = 0; i < arrayentry.length; ++i){
const dataUrl = new URL(arrayentry[i].dataPath, ndarrayCacheUrl).href;
try {
const result = await(await artifactCache.deleteInCache(dataUrl));
if (result === false){
return false;
}
} catch (err){
this.env.logger("Error: Cannot fetch " + dataUrl + " err= " + err);
throw err;
}
}
return true;
}

/**
* Fetch list of NDArray into the NDArrayCache.
*
Expand All @@ -1489,7 +1536,7 @@ export class Instance implements Disposable {
ndarrayCacheUrl: string,
list: Array<NDArrayShardEntry>,
device: DLDevice,
artifactCache: ArtifactCache
artifactCache: ArtifactCacheTemplate
) {
const perf = compact.getPerformance();
const tstart = perf.now();
Expand Down

0 comments on commit 4b55a1b

Please sign in to comment.