@coherentglobal/wasm-runner 0.1.4 → 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.
Files changed (41) hide show
  1. package/README.md +137 -222
  2. package/dist/browser/template/main.template.js +1 -1
  3. package/dist/browser/template/main.template.js.map +1 -1
  4. package/dist/browser/template/worker.template.js +7 -4
  5. package/dist/browser/template/worker.template.js.map +1 -1
  6. package/dist/browser/template.js +1 -1
  7. package/dist/browser/template.js.map +1 -1
  8. package/dist/browser.d.ts +2 -2
  9. package/dist/browser.js +6 -6
  10. package/dist/browser.js.map +1 -1
  11. package/dist/error.d.ts +5 -0
  12. package/dist/error.js +13 -3
  13. package/dist/error.js.map +1 -1
  14. package/dist/node/logger.js +1 -1
  15. package/dist/node/logger.js.map +1 -1
  16. package/dist/node/logger.ts +1 -1
  17. package/dist/node/template/main.template.ejs +77 -64
  18. package/dist/node/threads/workerPool.d.ts +3 -5
  19. package/dist/node/threads/workerPool.js +7 -11
  20. package/dist/node/threads/workerPool.js.map +1 -1
  21. package/dist/node/threads/workerPool.ts +12 -10
  22. package/dist/node/threads/workerThread.d.ts +1 -0
  23. package/dist/node/threads/workerThread.js +17 -9
  24. package/dist/node/threads/workerThread.js.map +1 -1
  25. package/dist/node/threads/workerThread.ts +21 -14
  26. package/dist/node.d.ts +20 -5
  27. package/dist/node.js +243 -36
  28. package/dist/node.js.map +1 -1
  29. package/dist/responseTimeMetric.d.ts +16 -0
  30. package/dist/responseTimeMetric.js +56 -0
  31. package/dist/responseTimeMetric.js.map +1 -0
  32. package/dist/serializer/columnarSerializer.d.ts +3 -2
  33. package/dist/serializer/columnarSerializer.js +10 -1
  34. package/dist/serializer/columnarSerializer.js.map +1 -1
  35. package/dist/types.d.ts +34 -2
  36. package/dist/types.js +8 -0
  37. package/dist/types.js.map +1 -1
  38. package/dist/utils.d.ts +1 -0
  39. package/dist/utils.js +19 -15
  40. package/dist/utils.js.map +1 -1
  41. package/package.json +7 -4
@@ -16,6 +16,7 @@ declare class WorkerThread extends ThreadWorker {
16
16
  * Function to receive the message data.
17
17
  */
18
18
  onMessage(callback: (value?: any) => void): void;
19
+ private cleanupListener;
19
20
  /**
20
21
  * Sends a request to the main thread and waits for a response.
21
22
  *
@@ -10,12 +10,21 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  const worker_threads_1 = require("worker_threads");
13
- const cuid2_1 = require("@paralleldrive/cuid2");
14
13
  const poolifier_1 = require("poolifier");
15
14
  const constants_1 = require("../../constants");
15
+ const activeListeners = new Map();
16
16
  class WorkerThread extends poolifier_1.ThreadWorker {
17
17
  constructor(taskFunctions, opts) {
18
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
+ };
19
28
  }
20
29
  /**
21
30
  * Sends message back to the main thread.
@@ -56,25 +65,24 @@ class WorkerThread extends poolifier_1.ThreadWorker {
56
65
  requestMessage(payload, context) {
57
66
  return __awaiter(this, void 0, void 0, function* () {
58
67
  return new Promise((resolve, reject) => {
59
- const eventId = (0, cuid2_1.createId)();
60
68
  // Send the request to parent
61
69
  this.postMessage({
62
70
  payload,
63
71
  context,
64
72
  type: constants_1.THREAD_EVENT_TYPES.REQUEST_EXECUTE,
65
- eventId,
73
+ threadId: worker_threads_1.threadId,
66
74
  });
67
75
  // Receive the response from parent once ready
68
- this.onMessage((message) => {
69
- if (message.eventId === eventId &&
70
- (message === null || message === void 0 ? void 0 : message.type) === constants_1.THREAD_EVENT_TYPES.RESPONSE) {
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) {
71
79
  resolve(message === null || message === void 0 ? void 0 : message.payload);
72
80
  }
73
- else if (message.eventId === eventId &&
74
- (message === null || message === void 0 ? void 0 : message.type) === constants_1.THREAD_EVENT_TYPES.ERROR) {
81
+ else if ((message === null || message === void 0 ? void 0 : message.type) === constants_1.THREAD_EVENT_TYPES.ERROR) {
75
82
  reject(message === null || message === void 0 ? void 0 : message.payload);
76
83
  }
77
- });
84
+ };
85
+ this.onMessage(messageListener);
78
86
  });
79
87
  });
80
88
  }
@@ -1 +1 @@
1
- {"version":3,"file":"workerThread.js","sourceRoot":"","sources":["../../../src/node/threads/workerThread.ts"],"names":[],"mappings":";;;;;;;;;;;AAAA,mDAA0D;AAC1D,gDAAwD;AACxD,yCAAyC;AACzC,+CAAqD;AAKrD,MAAM,YAAa,SAAQ,wBAAY;IACrC,YACE,aAEoC,EACpC,IAAoB;QAEpB,KAAK,CAAC,aAAa,EAAE,IAAI,CAAC,CAAC;IAC7B,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,CAAC;IAED;;;;;OAKG;IACH,SAAS,CAAC,QAA+B;QACvC,IAAI,6BAAY,EAAE,CAAC;YACjB,MAAM,KAAK,CAAC,8CAA8C,CAAC,CAAC;QAC9D,CAAC;aAAM,CAAC;YACN,2BAAU,CAAC,EAAE,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;QACrC,CAAC;IACH,CAAC;IAED;;;;;;;;OAQG;IACG,cAAc,CAAC,OAAY,EAAE,OAAY;;YAC7C,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;gBACrC,MAAM,OAAO,GAAG,IAAA,gBAAI,GAAE,CAAC;gBAEvB,6BAA6B;gBAC7B,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P,OAAO,uBAAP,OAAO,CAAE,IAAI,MAAK,8BAAkB,CAAC,KAAK,EAAE,CAAC;wBACtD,MAAM,CAAC,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,OAAO,CAAC,CAAC;oBAC3B,CAAC;gBAEH,CAAC,CAAC;gBACF,IAAI,CAAC,SAAS,CAAC,eAAe,CAAC,CAAC;YAClC,CAAC,CAAC,CAAC;QACL,CAAC;KAAA;CACF;AAED,kBAAe,YAAY,CAAC"}
@@ -1,4 +1,4 @@
1
- import { isMainThread, parentPort } from "worker_threads";
1
+ import { isMainThread, parentPort, threadId } from "worker_threads";
2
2
  import { createId as cuid } from "@paralleldrive/cuid2";
3
3
  import { ThreadWorker } from "poolifier";
4
4
  import { THREAD_EVENT_TYPES } from "../../constants";
@@ -6,6 +6,8 @@ import { THREAD_EVENT_TYPES } from "../../constants";
6
6
  import type { TaskFunctions, TaskFunction, WorkerOptions } from "poolifier";
7
7
  import type { EVENT_TYPE, EventRequestData } from "../../types";
8
8
 
9
+ const activeListeners = new Map();
10
+
9
11
  class WorkerThread extends ThreadWorker {
10
12
  constructor(
11
13
  taskFunctions:
@@ -43,6 +45,16 @@ class WorkerThread extends ThreadWorker {
43
45
  }
44
46
  }
45
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
+
46
58
  /**
47
59
  * Sends a request to the main thread and waits for a response.
48
60
  *
@@ -54,30 +66,25 @@ class WorkerThread extends ThreadWorker {
54
66
  */
55
67
  async requestMessage(payload: any, context: any) {
56
68
  return new Promise((resolve, reject) => {
57
- const eventId = cuid();
58
-
59
69
  // Send the request to parent
60
70
  this.postMessage({
61
71
  payload,
62
72
  context,
63
73
  type: THREAD_EVENT_TYPES.REQUEST_EXECUTE as EVENT_TYPE,
64
- eventId,
74
+ threadId,
65
75
  });
66
76
 
67
77
  // Receive the response from parent once ready
68
- this.onMessage((message: EventRequestData) => {
69
- if (
70
- message.eventId === eventId &&
71
- message?.type === THREAD_EVENT_TYPES.RESPONSE
72
- ) {
78
+ const messageListener = (message: EventRequestData) => {
79
+ parentPort.off("message", messageListener);
80
+ if (message?.type === THREAD_EVENT_TYPES.RESPONSE) {
73
81
  resolve(message?.payload);
74
- } else if (
75
- message.eventId === eventId &&
76
- message?.type === THREAD_EVENT_TYPES.ERROR
77
- ) {
82
+ } else if (message?.type === THREAD_EVENT_TYPES.ERROR) {
78
83
  reject(message?.payload);
79
84
  }
80
- });
85
+
86
+ };
87
+ this.onMessage(messageListener);
81
88
  });
82
89
  }
83
90
  }
package/dist/node.d.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import tmp from "tmp";
2
- import type { FixedThreadPool } from "./node/threads/workerPool";
3
- import type { NodeModel, RunnerConfig, ModelConfig, ResponseObject } from "./types";
2
+ import { NodeModel, RunnerConfig, ModelConfig, ResponseObject, Compatibility, WasmOptions, ModelInstance } from "./types";
4
3
  import { CancellationToken } from "./CancellationToken";
5
4
  export { ColumnarSerializer } from "./serializer/columnarSerializer";
6
5
  export { CancellationToken } from "./CancellationToken";
@@ -18,12 +17,14 @@ export declare class WasmRunner {
18
17
  models: any[];
19
18
  externalResolverModule: any;
20
19
  initializingModels: string[];
20
+ options: WasmOptions;
21
+ safeCompiler: string;
21
22
  /**
22
23
  * Create new Wasm Runner instance
23
24
  *
24
25
  * @param {RunnerConfig} wasmRunnerConfig
25
26
  */
26
- constructor(wasmRunnerConfig?: RunnerConfig, externalResolverModule?: string, license?: string);
27
+ constructor(wasmRunnerConfig?: RunnerConfig, externalResolverModule?: string, options?: any, license?: string);
27
28
  /**
28
29
  * Initialize wasm runner fetching model from remote server
29
30
  *
@@ -31,9 +32,9 @@ export declare class WasmRunner {
31
32
  * @function initialize
32
33
  */
33
34
  initialize(license?: string): Promise<void>;
34
- _createModelInstance(id: any, url: any, workerFolder: any, size: any): Promise<FixedThreadPool<any, any>>;
35
+ _createModelInstance(id: any, url: any, workerFolder: any, size?: number): Promise<ModelInstance>;
35
36
  _initializeModelInstance(m: NodeModel): Promise<void>;
36
- _createModelInstanceFromURL(id: string, url: string, workerFolder: string, modelFolder: string, size: number): Promise<FixedThreadPool<any, any>>;
37
+ _createModelInstanceFromURL(id: string, url: string, workerFolder: string, modelFolder: string, size: number): Promise<ModelInstance>;
37
38
  /**
38
39
  * Append and initialize model in preparation for the `execute()`.
39
40
  *
@@ -54,12 +55,23 @@ export declare class WasmRunner {
54
55
  remove(id: string): Promise<void>;
55
56
  _parseError(rawResponse: any): Object;
56
57
  _getModel(id: string): NodeModel;
58
+ getModelSize(id: string): number;
59
+ getModelCompilerVersion(id: any): string;
57
60
  /**
58
61
  * Check if model existed
59
62
  * @param {string} id Model id
60
63
  * @returns {boolean}
61
64
  */
62
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;
63
75
  /**
64
76
  * Perform calculation. The runner will use request_meta.version_uuid of input argument to locate model
65
77
  * @async
@@ -67,4 +79,7 @@ export declare class WasmRunner {
67
79
  * @returns {Promise<ResponseObject>} - Response model data
68
80
  */
69
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;
70
85
  }