Skip to content

Commit

Permalink
add delete all in cache
Browse files Browse the repository at this point in the history
  • Loading branch information
DiegoCao committed Feb 26, 2024
1 parent 3b0cb09 commit dcd26dc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
47 changes: 38 additions & 9 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 @@ -660,18 +661,46 @@ export class ChatRestModule implements ChatInterface {
}
}

export async 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 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 modelRecord = await findModelRecord(localId, appConfig);
const modelUrl = modelRecord.model_url;
return tvmjs.hasNDArrayInCache(modelUrl, "webllm/model");
}

export async function deleteAllModelDataInCache(localId: string, appConfig?: AppConfig) {
if (appConfig === undefined) {
appConfig = prebuiltAppConfig;
}
// delete model
const modelRecord = await findModelRecord(localId, appConfig);
const modelUrl = modelRecord.model_url;
tvmjs.deleteNDArrayCache(modelUrl, "webllm/model");
const modelCache = new tvmjs.ArtifactCache("webllm/model");
// delete tokenizer
const url = new URL("tokenizer.model", modelUrl).href;
await modelCache.deleteInCache(url);
const url2 = new URL("tokenizer.json", modelUrl).href;
await modelCache.deleteInCache(url2);
// delete wasm
const wasmUrl = modelRecord.model_lib_url;
const wasmCache = new tvmjs.ArtifactCache("webllm/wasm");
await wasmCache.deleteInCache(wasmUrl);
// delete config
const configCache = new tvmjs.ArtifactCache("webllm/config");
const configUrl = new URL("mlc-chat-config.json", modelUrl).href;
await configCache.deleteInCache(configUrl);
}

2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export {

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

export {
Expand Down

0 comments on commit dcd26dc

Please sign in to comment.