in src/plugin/utils/local_storage.ts [79:121]
async save(modelArtifacts: io.ModelArtifacts): Promise<io.SaveResult> {
if (modelArtifacts.modelTopology instanceof ArrayBuffer) {
throw new Error(
'AsyncStorageHandler.save() does not support saving model topology ' +
'in binary format.');
} else {
// We save three items separately for each model,
// a ModelArtifactsInfo, a ModelArtifacts without weights
// and the model weights.
const modelArtifactsInfo: io.ModelArtifactsInfo =
getModelArtifactsInfoForJSON(modelArtifacts);
const { weightData, ...modelArtifactsWithoutWeights } = modelArtifacts;
const weights = splitString(fromByteArray(new Uint8Array(weightData)),
800 * 1024);
try {
wx.setStorageSync(this.keys.info, JSON.stringify(modelArtifactsInfo));
wx.setStorageSync(
this.keys.modelArtifactsWithoutWeights,
JSON.stringify(modelArtifactsWithoutWeights));
// split the weight string into 10M chunk, size local storage has a
// size limit.
wx.setStorageSync(this.keys.weightDataChunkSize, weights.length);
weights.forEach((value, index) => {
wx.setStorageSync(
`${this.keys.weightData}:${index}`, value);
});
return { modelArtifactsInfo };
} catch (err) {
// If saving failed, clean up all items saved so far.
wx.removeStorageSync(this.keys.info);
weights.forEach((value, index) => {
wx.removeStorageSync(`${this.keys.weightData}:${index}`);
});
wx.removeStorageSync(this.keys.weightDataChunkSize);
wx.removeStorageSync(this.keys.modelArtifactsWithoutWeights);
throw new Error(
`Failed to save model '${this.modelPath}' to local storage.
Error info ${err}`);
}
}
}