@@ -26,6 +26,7 @@ import { Memory, CachedCallStack } from "./memory";
2626import { assert , StringToUint8Array } from "./support" ;
2727import { Environment } from "./environment" ;
2828import { FunctionInfo , WebGPUContext } from "./webgpu" ;
29+ import { ArtifactCacheTemplate } from "./artifact_cache" ;
2930
3031import * as compact from "./compact" ;
3132import * 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+ }
0 commit comments