Skip to content

Commit

Permalink
support cache util delete functions, including delete model, WASM, co…
Browse files Browse the repository at this point in the history
…nfig and all
  • Loading branch information
DiegoCao committed Feb 27, 2024
1 parent 3b0cb09 commit bb891d7
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 19 deletions.
72 changes: 72 additions & 0 deletions src/cache_util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import * as tvmjs from "tvmjs";
import {
AppConfig,
prebuiltAppConfig,
} from "./config";

function findModelRecord (localId: string, appConfig?: AppConfig){
const matchedItem = appConfig?.model_list.find(
item => item.local_id == localId
);
if (matchedItem !== undefined) {
return matchedItem;
}
throw Error("Cannot find model_url for " + localId);
}

export async function hasModelInCache(localId: string, appConfig?: AppConfig): Promise<boolean> {
if (appConfig === undefined) {
appConfig = prebuiltAppConfig;
}
const modelRecord = await findModelRecord(localId, appConfig);
const modelUrl = modelRecord.model_url;
return tvmjs.hasNDArrayInCache(modelUrl, "webllm/model");
}

export async function deleteModelAllInfoInCache(localId: string, appConfig?: AppConfig) {
// function to delete model all information in cache
if (appConfig === undefined) {
appConfig = prebuiltAppConfig;
}
// delete model and tokenizer in Cache
await deleteModelInCache(localId, appConfig);
// delete wasm in cache
await deleteModelWasmInCache(localId, appConfig);
// delete chat config
await deleteChatConfigInCache(localId, appConfig);
}


export async function deleteModelInCache(localId: string, appConfig?: AppConfig){
// delete the model NDArray In Cache
if (appConfig === undefined) {
appConfig = prebuiltAppConfig;
}
const modelRecord = await findModelRecord(localId, appConfig);
tvmjs.deleteNDArrayCache(modelRecord.model_url, "webllm/model");
const modelCache = new tvmjs.ArtifactCache("webllm/model");
await modelCache.deleteInCache(new URL("tokenizer.model", modelRecord.model_url).href);
await modelCache.deleteInCache(new URL("tokenizer.json", modelRecord.model_url).href);
}

export async function deleteChatConfigInCache(localId: string, appConfig?: AppConfig){
// delete the chat configuration in Cache
if (appConfig === undefined) {
appConfig = prebuiltAppConfig;
}
const modelRecord = await findModelRecord(localId, appConfig);
const configCache = new tvmjs.ArtifactCache("webllm/config");
const configUrl = new URL("mlc-chat-config.json", modelRecord.model_url).href;
await configCache.deleteInCache(configUrl);
}


export async function deleteModelWasmInCache(localId: string, appConfig?: AppConfig){
// delete the wasm in Cache
if (appConfig === undefined) {
appConfig = prebuiltAppConfig;
}
const modelRecord = await findModelRecord(localId, appConfig);
const wasmCache = new tvmjs.ArtifactCache("webllm/wasm");
await wasmCache.deleteInCache(modelRecord.model_lib_url);
}
21 changes: 3 additions & 18 deletions src/chat_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
AppConfig,
prebuiltAppConfig,
GenerationConfig,
postInitAndCheckGenerationConfigValues
postInitAndCheckGenerationConfigValues,
ModelRecord
} from "./config";
import { LLMChatPipeline } from "./llm_chat"
import {
Expand Down Expand Up @@ -460,7 +461,7 @@ export class ChatModule implements ChatInterface {
message.content,
]);
} else {
throw new Error("Unsuppoerted role: " + message.role);
throw new Error("Unsupported role: " + message.role);
}
}
this.getPipeline().overrideConversationMessages(messages);
Expand Down Expand Up @@ -659,19 +660,3 @@ export class ChatRestModule implements ChatInterface {
});
}
}

export async function hasModelInCache(localId: string, appConfig?: AppConfig): Promise<boolean> {
if (appConfig === undefined) {
appConfig = prebuiltAppConfig;
}
const findModelRecord = () => {
const matchedItem = appConfig?.model_list.find(
item => item.local_id == localId
);
if (matchedItem !== undefined) return matchedItem;
throw Error("Cannot find model_url for " + localId);
}
const modelRecord = findModelRecord();
const modelUrl = modelRecord.model_url;
return tvmjs.hasNDArrayInCache(modelUrl, "webllm/model");
}
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ export {

export {
ChatModule,
ChatRestModule, hasModelInCache
ChatRestModule,
} from "./chat_module";

export {
hasModelInCache, deleteChatConfigInCache, deleteModelAllInfoInCache, deleteModelWasmInCache, deleteModelInCache,
} from "./cache_util";

export {
ChatWorkerHandler,
ChatWorkerClient,
Expand Down

0 comments on commit bb891d7

Please sign in to comment.