Dispose of embeddings pipeline occasionally to clear memory (#1047)
Browse files
src/lib/server/embeddingEndpoints/transformersjs/embeddingEndpoints.ts
CHANGED
@@ -18,9 +18,15 @@ class TransformersJSModelsSingleton {
|
|
18 |
|
19 |
if (modelPipelineInstance) {
|
20 |
const [, modelPipeline] = modelPipelineInstance;
|
21 |
-
return modelPipeline;
|
22 |
-
}
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
const newModelPipeline = pipeline("feature-extraction", modelName);
|
25 |
this.instances.push([modelName, newModelPipeline]);
|
26 |
|
|
|
18 |
|
19 |
if (modelPipelineInstance) {
|
20 |
const [, modelPipeline] = modelPipelineInstance;
|
|
|
|
|
21 |
|
22 |
+
// 80% chance to return the pipeline
|
23 |
+
if (Math.random() > 0.2) {
|
24 |
+
return modelPipeline;
|
25 |
+
}
|
26 |
+
// else we dispose of the pipeline to clear memory
|
27 |
+
await (await modelPipeline).dispose();
|
28 |
+
this.instances = this.instances.filter(([name]) => name !== modelName);
|
29 |
+
}
|
30 |
const newModelPipeline = pipeline("feature-extraction", modelName);
|
31 |
this.instances.push([modelName, newModelPipeline]);
|
32 |
|