This is an automated email from the ASF dual-hosted git repository.
tqchen pushed a commit to branch unity
in repository https://gitbox.apache.org/repos/asf/tvm.git
The following commit(s) were added to refs/heads/unity by this push:
new f15f612aa7 [Unity] Improve caching logic in webruntime (#14940)
f15f612aa7 is described below
commit f15f612aa7f124c72411b7e4f15b03914fb0029f
Author: Tianqi Chen <[email protected]>
AuthorDate: Wed May 24 21:52:31 2023 -0400
[Unity] Improve caching logic in webruntime (#14940)
This PR consolidate and exposes the artifact
cache in the web runtime.
---
web/src/index.ts | 2 +-
web/src/runtime.ts | 60 ++++++++++++++++++++++++++----------------------------
2 files changed, 30 insertions(+), 32 deletions(-)
diff --git a/web/src/index.ts b/web/src/index.ts
index fc3f5ada32..7d26fa7001 100644
--- a/web/src/index.ts
+++ b/web/src/index.ts
@@ -22,7 +22,7 @@ export {
PackedFunc, Module, NDArray,
TVMArray, TVMObject, VirtualMachine,
InitProgressCallback, InitProgressReport,
- Instance, instantiate
+ ArtifactCache, Instance, instantiate
} from "./runtime";
export { Disposable, LibraryProvider } from "./types";
export { RPCServer } from "./rpc_server";
diff --git a/web/src/runtime.ts b/web/src/runtime.ts
index 577a50bfbc..e7330ff2aa 100644
--- a/web/src/runtime.ts
+++ b/web/src/runtime.ts
@@ -793,7 +793,7 @@ export class Module implements Disposable {
* @param requireNotNull require handle is not null.
* @returns The handle.
*/
- getHandle(requireNotNull : boolean = true): Pointer {
+ getHandle(requireNotNull = true): Pointer {
if (requireNotNull && this.handle == 0) {
throw Error("Module has already been disposed");
}
@@ -971,6 +971,29 @@ export interface InitProgressReport {
export type InitProgressCallback = (report: InitProgressReport) => void;
+/**
+ * Cache to store model related data.
+ */
+export class ArtifactCache {
+ private cache?: Cache;
+
+ async fetchWithCache(url: string) {
+ const request = new Request(url);
+ if (this.cache === undefined) {
+ this.cache = await caches.open("tvmjs");
+ }
+ let result = await this.cache.match(request);
+ if (result === undefined) {
+ await this.cache.add(request);
+ result = await this.cache.match(request);
+ }
+ if (result == undefined) {
+ throw Error("Cannot fetch " + url);
+ }
+ return result;
+ }
+}
+
/**
* TVM runtime instance.
*
@@ -997,6 +1020,7 @@ export class Instance implements Disposable {
private objFactory: Map<number, FObjectConstructor>;
private ctx: RuntimeContext;
private initProgressCallback: Array<InitProgressCallback> = [];
+ private artifactCache = new ArtifactCache();
/**
* Internal function(registered by the runtime)
@@ -1396,21 +1420,8 @@ export class Instance implements Disposable {
*/
async fetchNDArrayCache(ndarrayCacheUrl: string, device: DLDevice) :
Promise<any> {
const jsonUrl = new URL("ndarray-cache.json", ndarrayCacheUrl).href;
- const request = new Request(jsonUrl);
- const cache = await caches.open("tvmjs");
- let result = await cache.match(request);
- if (result === undefined) {
- await cache.add(request);
- result = await cache.match(request);
- }
- if (result === undefined) {
- this.env.logger("Error: Cannot cache " + jsonUrl + ", reloading will be
slow");
- try {
- result = await fetch(request);
- } catch(err) {
- this.env.logger("Cannot fetch " + jsonUrl);
- }
- }
+ const result = await this.artifactCache.fetchWithCache(jsonUrl);
+
let list;
if (result instanceof Response) {
list = await result.json();
@@ -1463,26 +1474,14 @@ export class Instance implements Disposable {
text: "Start to fetch params",
});
}
- const cache = await caches.open("tvmjs");
for (let i = 0; i < list.length; ++i) {
reportCallback(i);
fetchedBytes += list[i].nbytes;
const dataUrl = new URL(list[i].dataPath, ndarrayCacheUrl).href;
- const request = new Request(dataUrl);
let buffer;
try {
- // use native cache
- let result = await cache.match(request);
- if (result === undefined) {
- await cache.add(request);
- result = await cache.match(request);
- }
- if (result == undefined) {
- this.env.logger("Error: Cannot cache " + dataUrl + ", reloading will
be slow");
- result = await fetch(request);
- }
- buffer = await result.arrayBuffer();
+ buffer = await (await
this.artifactCache.fetchWithCache(dataUrl)).arrayBuffer();
} catch (err) {
this.env.logger("Error: Cannot fetch " + dataUrl + " err= " + err);
throw err;
@@ -1842,8 +1841,7 @@ export class Instance implements Disposable {
this.beginScope();
const fmap_str = mod.getFunction("webgpu.get_fmap", true)() as string;
- let fmap: Record<string, FunctionInfo> = JSON.parse(fmap_str);
- const totalFuncs = fmap.length;
+ const fmap: Record<string, FunctionInfo> = JSON.parse(fmap_str);
const fGetShader = this.detachFromCurrentScope(
mod.getFunction("webgpu.get_shader")
);