@coherentglobal/wasm-runner 0.0.103 → 0.1.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +137 -222
- package/dist/CancellationToken.js.map +1 -1
- package/dist/browser/logger.js.map +1 -1
- package/dist/browser/template/main.template.js +1 -1
- package/dist/browser/template/main.template.js.map +1 -1
- package/dist/browser/template/runtime.template.js.map +1 -1
- package/dist/browser/template/worker.template.js +7 -4
- package/dist/browser/template/worker.template.js.map +1 -1
- package/dist/browser/template.js +1 -1
- package/dist/browser/template.js.map +1 -1
- package/dist/browser.d.ts +2 -2
- package/dist/browser.js +6 -6
- package/dist/browser.js.map +1 -1
- package/dist/constants.d.ts +5 -0
- package/dist/constants.js +14 -0
- package/dist/constants.js.map +1 -0
- package/dist/error.d.ts +5 -0
- package/dist/error.js +13 -3
- package/dist/error.js.map +1 -1
- package/dist/mockConstants.d.ts +5 -0
- package/dist/mockConstants.js +14 -0
- package/dist/mockConstants.js.map +1 -0
- package/dist/node/logger.d.ts +1 -4
- package/dist/node/logger.js +1 -1
- package/dist/node/logger.js.map +1 -1
- package/dist/node/logger.ts +1 -1
- package/dist/node/template/main.template.ejs +126 -86
- package/dist/node/threads/mockWorkerThread.d.ts +28 -0
- package/dist/node/threads/mockWorkerThread.js +89 -0
- package/dist/node/threads/mockWorkerThread.js.map +1 -0
- package/dist/node/threads/workerPool.d.ts +17 -0
- package/dist/node/threads/workerPool.js +27 -0
- package/dist/node/threads/workerPool.js.map +1 -0
- package/dist/node/threads/workerPool.ts +38 -0
- package/dist/node/threads/workerThread.d.ts +31 -0
- package/dist/node/threads/workerThread.js +91 -0
- package/dist/node/threads/workerThread.js.map +1 -0
- package/dist/node/threads/workerThread.ts +92 -0
- package/dist/node.d.ts +36 -26
- package/dist/node.js +334 -62
- package/dist/node.js.map +1 -1
- package/dist/responseTimeMetric.d.ts +16 -0
- package/dist/responseTimeMetric.js +56 -0
- package/dist/responseTimeMetric.js.map +1 -0
- package/dist/serializer/columnarSerializer.d.ts +3 -2
- package/dist/serializer/columnarSerializer.js +10 -1
- package/dist/serializer/columnarSerializer.js.map +1 -1
- package/dist/types.d.ts +41 -2
- package/dist/types.js +8 -0
- package/dist/types.js.map +1 -1
- package/dist/utils.d.ts +27 -0
- package/dist/utils.js +49 -13
- package/dist/utils.js.map +1 -1
- package/package.json +8 -5
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
const worker_threads_1 = require("worker_threads");
|
|
13
|
+
const poolifier_1 = require("poolifier");
|
|
14
|
+
const constants_1 = require("../../constants");
|
|
15
|
+
const activeListeners = new Map();
|
|
16
|
+
class WorkerThread extends poolifier_1.ThreadWorker {
|
|
17
|
+
constructor(taskFunctions, opts) {
|
|
18
|
+
super(taskFunctions, opts);
|
|
19
|
+
this.cleanupListener = (eventId) => {
|
|
20
|
+
setTimeout(() => {
|
|
21
|
+
const listener = activeListeners.get(eventId);
|
|
22
|
+
if (listener) {
|
|
23
|
+
worker_threads_1.parentPort.off("message", listener);
|
|
24
|
+
activeListeners.delete(eventId);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Sends message back to the main thread.
|
|
31
|
+
*
|
|
32
|
+
* @param {EventRequestData} message
|
|
33
|
+
*/
|
|
34
|
+
postMessage(message) {
|
|
35
|
+
if (worker_threads_1.isMainThread) {
|
|
36
|
+
throw Error("Failed to post message using main thread.");
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
worker_threads_1.parentPort.postMessage(message);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Receives message from the main thread or other threads.
|
|
44
|
+
*
|
|
45
|
+
* @param {Function} callback
|
|
46
|
+
* Function to receive the message data.
|
|
47
|
+
*/
|
|
48
|
+
onMessage(callback) {
|
|
49
|
+
if (worker_threads_1.isMainThread) {
|
|
50
|
+
throw Error("Failed to receive message using main thread.");
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
worker_threads_1.parentPort.on("message", callback);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Sends a request to the main thread and waits for a response.
|
|
58
|
+
*
|
|
59
|
+
* @param {Object} payload
|
|
60
|
+
* Object or details that needs to be passed to main thread as `payload`.
|
|
61
|
+
*
|
|
62
|
+
* @returns {Promise<Object>}
|
|
63
|
+
* Response from main thread.
|
|
64
|
+
*/
|
|
65
|
+
requestMessage(payload, context) {
|
|
66
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
67
|
+
return new Promise((resolve, reject) => {
|
|
68
|
+
// Send the request to parent
|
|
69
|
+
this.postMessage({
|
|
70
|
+
payload,
|
|
71
|
+
context,
|
|
72
|
+
type: constants_1.THREAD_EVENT_TYPES.REQUEST_EXECUTE,
|
|
73
|
+
threadId: worker_threads_1.threadId,
|
|
74
|
+
});
|
|
75
|
+
// Receive the response from parent once ready
|
|
76
|
+
const messageListener = (message) => {
|
|
77
|
+
worker_threads_1.parentPort.off("message", messageListener);
|
|
78
|
+
if ((message === null || message === void 0 ? void 0 : message.type) === constants_1.THREAD_EVENT_TYPES.RESPONSE) {
|
|
79
|
+
resolve(message === null || message === void 0 ? void 0 : message.payload);
|
|
80
|
+
}
|
|
81
|
+
else if ((message === null || message === void 0 ? void 0 : message.type) === constants_1.THREAD_EVENT_TYPES.ERROR) {
|
|
82
|
+
reject(message === null || message === void 0 ? void 0 : message.payload);
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
this.onMessage(messageListener);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
exports.default = WorkerThread;
|
|
91
|
+
//# sourceMappingURL=workerThread.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"workerThread.js","sourceRoot":"","sources":["../../../src/node/threads/workerThread.ts"],"names":[],"mappings":";;;;;;;;;;;AAAA,mDAAoE;AAEpE,yCAAyC;AACzC,+CAAqD;AAKrD,MAAM,eAAe,GAAG,IAAI,GAAG,EAAE,CAAC;AAElC,MAAM,YAAa,SAAQ,wBAAY;IACrC,YACE,aAEoC,EACpC,IAAoB;QAEpB,KAAK,CAAC,aAAa,EAAE,IAAI,CAAC,CAAC;QA8BrB,oBAAe,GAAG,CAAC,OAAO,EAAE,EAAE;YACpC,UAAU,CAAC,GAAG,EAAE;gBACd,MAAM,QAAQ,GAAG,eAAe,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;gBAC9C,IAAI,QAAQ,EAAE,CAAC;oBACb,2BAAU,CAAC,GAAG,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;oBACpC,eAAe,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;gBAClC,CAAC;YACH,CAAC,CAAC,CAAC;QACL,CAAC,CAAC;IArCF,CAAC;IAED;;;;OAIG;IACH,WAAW,CAAC,OAAyB;QACnC,IAAI,6BAAY,EAAE,CAAC;YACjB,MAAM,KAAK,CAAC,2CAA2C,CAAC,CAAC;QAC3D,CAAC;aAAM,CAAC;YACN,2BAAU,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;QAClC,CAAC;IACH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threads";
|
|
2
|
+
import { createId as cuid } from "@paralleldrive/cuid2";
|
|
3
|
+
import { ThreadWorker } from "poolifier";
|
|
4
|
+
import { THREAD_EVENT_TYPES } from "../../constants";
|
|
5
|
+
|
|
6
|
+
import type { TaskFunctions, TaskFunction, WorkerOptions } from "poolifier";
|
|
7
|
+
import type { EVENT_TYPE, EventRequestData } from "../../types";
|
|
8
|
+
|
|
9
|
+
const activeListeners = new Map();
|
|
10
|
+
|
|
11
|
+
class WorkerThread extends ThreadWorker {
|
|
12
|
+
constructor(
|
|
13
|
+
taskFunctions:
|
|
14
|
+
| TaskFunction<unknown, Response>
|
|
15
|
+
| TaskFunctions<unknown, Response>,
|
|
16
|
+
opts?: WorkerOptions
|
|
17
|
+
) {
|
|
18
|
+
super(taskFunctions, opts);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Sends message back to the main thread.
|
|
23
|
+
*
|
|
24
|
+
* @param {EventRequestData} message
|
|
25
|
+
*/
|
|
26
|
+
postMessage(message: EventRequestData) {
|
|
27
|
+
if (isMainThread) {
|
|
28
|
+
throw Error("Failed to post message using main thread.");
|
|
29
|
+
} else {
|
|
30
|
+
parentPort.postMessage(message);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Receives message from the main thread or other threads.
|
|
36
|
+
*
|
|
37
|
+
* @param {Function} callback
|
|
38
|
+
* Function to receive the message data.
|
|
39
|
+
*/
|
|
40
|
+
onMessage(callback: (value?: any) => void) {
|
|
41
|
+
if (isMainThread) {
|
|
42
|
+
throw Error("Failed to receive message using main thread.");
|
|
43
|
+
} else {
|
|
44
|
+
parentPort.on("message", callback);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
private cleanupListener = (eventId) => {
|
|
49
|
+
setTimeout(() => {
|
|
50
|
+
const listener = activeListeners.get(eventId);
|
|
51
|
+
if (listener) {
|
|
52
|
+
parentPort.off("message", listener);
|
|
53
|
+
activeListeners.delete(eventId);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Sends a request to the main thread and waits for a response.
|
|
60
|
+
*
|
|
61
|
+
* @param {Object} payload
|
|
62
|
+
* Object or details that needs to be passed to main thread as `payload`.
|
|
63
|
+
*
|
|
64
|
+
* @returns {Promise<Object>}
|
|
65
|
+
* Response from main thread.
|
|
66
|
+
*/
|
|
67
|
+
async requestMessage(payload: any, context: any) {
|
|
68
|
+
return new Promise((resolve, reject) => {
|
|
69
|
+
// Send the request to parent
|
|
70
|
+
this.postMessage({
|
|
71
|
+
payload,
|
|
72
|
+
context,
|
|
73
|
+
type: THREAD_EVENT_TYPES.REQUEST_EXECUTE as EVENT_TYPE,
|
|
74
|
+
threadId,
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
// Receive the response from parent once ready
|
|
78
|
+
const messageListener = (message: EventRequestData) => {
|
|
79
|
+
parentPort.off("message", messageListener);
|
|
80
|
+
if (message?.type === THREAD_EVENT_TYPES.RESPONSE) {
|
|
81
|
+
resolve(message?.payload);
|
|
82
|
+
} else if (message?.type === THREAD_EVENT_TYPES.ERROR) {
|
|
83
|
+
reject(message?.payload);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
};
|
|
87
|
+
this.onMessage(messageListener);
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export default WorkerThread;
|
package/dist/node.d.ts
CHANGED
|
@@ -1,22 +1,9 @@
|
|
|
1
1
|
import tmp from "tmp";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { CancellationToken } from './CancellationToken';
|
|
2
|
+
import { NodeModel, RunnerConfig, ModelConfig, ResponseObject, Compatibility, WasmOptions, ModelInstance } from "./types";
|
|
3
|
+
import { CancellationToken } from "./CancellationToken";
|
|
5
4
|
export { ColumnarSerializer } from "./serializer/columnarSerializer";
|
|
6
|
-
export { CancellationToken } from
|
|
5
|
+
export { CancellationToken } from "./CancellationToken";
|
|
7
6
|
export { ModelExecuteCancelled } from "./error";
|
|
8
|
-
/**
|
|
9
|
-
* WASM runner config
|
|
10
|
-
* @typedef {Object} RunnerConfig
|
|
11
|
-
* @property {string} id - Model id or version_uuid
|
|
12
|
-
* @property {string} url - Model url
|
|
13
|
-
*/
|
|
14
|
-
/**
|
|
15
|
-
* Input object
|
|
16
|
-
* @typedef {Object} InputObject
|
|
17
|
-
* @property {Object} request_data - Request input date
|
|
18
|
-
* @property {Object} request_meta - Request input meta
|
|
19
|
-
*/
|
|
20
7
|
/**
|
|
21
8
|
* WasmRunner class, responsible for managing model and it's lifecycle
|
|
22
9
|
*
|
|
@@ -28,13 +15,16 @@ export declare class WasmRunner {
|
|
|
28
15
|
_tempModelFolder: tmp.DirResult;
|
|
29
16
|
license: string;
|
|
30
17
|
models: any[];
|
|
31
|
-
externalResolverModule:
|
|
18
|
+
externalResolverModule: any;
|
|
19
|
+
initializingModels: string[];
|
|
20
|
+
options: WasmOptions;
|
|
21
|
+
safeCompiler: string;
|
|
32
22
|
/**
|
|
33
23
|
* Create new Wasm Runner instance
|
|
34
24
|
*
|
|
35
25
|
* @param {RunnerConfig} wasmRunnerConfig
|
|
36
26
|
*/
|
|
37
|
-
constructor(wasmRunnerConfig?: RunnerConfig, externalResolverModule?: string, license?: string);
|
|
27
|
+
constructor(wasmRunnerConfig?: RunnerConfig, externalResolverModule?: string, options?: any, license?: string);
|
|
38
28
|
/**
|
|
39
29
|
* Initialize wasm runner fetching model from remote server
|
|
40
30
|
*
|
|
@@ -42,29 +32,46 @@ export declare class WasmRunner {
|
|
|
42
32
|
* @function initialize
|
|
43
33
|
*/
|
|
44
34
|
initialize(license?: string): Promise<void>;
|
|
45
|
-
_createModelInstance(id: any, url: any, workerFolder: any, size
|
|
35
|
+
_createModelInstance(id: any, url: any, workerFolder: any, size?: number): Promise<ModelInstance>;
|
|
46
36
|
_initializeModelInstance(m: NodeModel): Promise<void>;
|
|
47
|
-
_createModelInstanceFromURL(id: string, url: string, workerFolder: string, modelFolder: string, size: number): Promise<
|
|
37
|
+
_createModelInstanceFromURL(id: string, url: string, workerFolder: string, modelFolder: string, size: number): Promise<ModelInstance>;
|
|
48
38
|
/**
|
|
49
|
-
* Append and
|
|
39
|
+
* Append and initialize model in preparation for the `execute()`.
|
|
40
|
+
*
|
|
50
41
|
* @param {ModelConfig} modelConfig
|
|
51
|
-
*
|
|
42
|
+
* Model details like version_id as `id` and zip path as `url`.
|
|
43
|
+
*
|
|
44
|
+
* @returns {Promise<void>}
|
|
52
45
|
*/
|
|
53
46
|
append(modelConfig: ModelConfig): Promise<void>;
|
|
54
47
|
/**
|
|
55
|
-
* Remove model from
|
|
56
|
-
*
|
|
57
|
-
* @
|
|
48
|
+
* Remove model from the list of initialized model useful for GC.
|
|
49
|
+
*
|
|
50
|
+
* @param {String} id
|
|
51
|
+
* ID of the model that needs to be removed.
|
|
52
|
+
*
|
|
53
|
+
* @returns {Promise<void>}
|
|
58
54
|
*/
|
|
59
55
|
remove(id: string): Promise<void>;
|
|
60
56
|
_parseError(rawResponse: any): Object;
|
|
61
57
|
_getModel(id: string): NodeModel;
|
|
58
|
+
getModelSize(id: string): number;
|
|
59
|
+
getModelCompilerVersion(id: any): string;
|
|
62
60
|
/**
|
|
63
61
|
* Check if model existed
|
|
64
62
|
* @param {string} id Model id
|
|
65
|
-
* @returns boolean
|
|
63
|
+
* @returns {boolean}
|
|
66
64
|
*/
|
|
67
65
|
isExist(id: string): boolean;
|
|
66
|
+
isModelCompatible(model: any, input: any): boolean;
|
|
67
|
+
getModelsStats(): {
|
|
68
|
+
stats: any;
|
|
69
|
+
busy: any;
|
|
70
|
+
size: any;
|
|
71
|
+
id: any;
|
|
72
|
+
}[];
|
|
73
|
+
private getPartiallyCompatibleMessage;
|
|
74
|
+
private getIncompatibleMessage;
|
|
68
75
|
/**
|
|
69
76
|
* Perform calculation. The runner will use request_meta.version_uuid of input argument to locate model
|
|
70
77
|
* @async
|
|
@@ -72,4 +79,7 @@ export declare class WasmRunner {
|
|
|
72
79
|
* @returns {Promise<ResponseObject>} - Response model data
|
|
73
80
|
*/
|
|
74
81
|
execute(input: any, id?: string, raw?: boolean, cancelToken?: CancellationToken): Promise<ResponseObject>;
|
|
82
|
+
getNeuronCompilerVersion(logFile: any): Promise<any>;
|
|
83
|
+
checkCompilerVersionCompatibility(modelCompilerVersion: string): Promise<Compatibility>;
|
|
84
|
+
getSafeCompilerVersion(runnerVersion: string): any;
|
|
75
85
|
}
|