wasm-bhtsne 1.1.0 → 1.2.2

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/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "wasm-bhtsne",
3
+ "type": "module",
3
4
  "collaborators": [
4
5
  "lv291 <baiunco291@proton.me>"
5
6
  ],
6
7
  "description": "Barnes-Hut implementations of t-SNE in wasm",
7
- "version": "1.1.0",
8
+ "version": "1.2.2",
8
9
  "license": "MIT",
9
10
  "repository": {
10
11
  "type": "git",
@@ -13,9 +14,10 @@
13
14
  "files": [
14
15
  "wasm_bhtsne_bg.wasm",
15
16
  "wasm_bhtsne.js",
16
- "wasm_bhtsne.d.ts"
17
+ "wasm_bhtsne.d.ts",
18
+ "snippets"
17
19
  ],
18
- "module": "wasm_bhtsne.js",
20
+ "main": "wasm_bhtsne.js",
19
21
  "types": "wasm_bhtsne.d.ts",
20
22
  "sideEffects": [
21
23
  "./snippets/*"
@@ -0,0 +1,107 @@
1
+ /*
2
+ * Copyright 2022 Google Inc. All Rights Reserved.
3
+ * Licensed under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License.
5
+ * You may obtain a copy of the License at
6
+ * http://www.apache.org/licenses/LICENSE-2.0
7
+ * Unless required by applicable law or agreed to in writing, software
8
+ * distributed under the License is distributed on an "AS IS" BASIS,
9
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ * See the License for the specific language governing permissions and
11
+ * limitations under the License.
12
+ */
13
+
14
+ // Note: we use `wasm_bindgen_worker_`-prefixed message types to make sure
15
+ // we can handle bundling into other files, which might happen to have their
16
+ // own `postMessage`/`onmessage` communication channels.
17
+ //
18
+ // If we didn't take that into the account, we could send much simpler signals
19
+ // like just `0` or whatever, but the code would be less resilient.
20
+
21
+ function waitForMsgType(target, type) {
22
+ return new Promise(resolve => {
23
+ target.addEventListener('message', function onMsg({ data }) {
24
+ if (data?.type !== type) return;
25
+ target.removeEventListener('message', onMsg);
26
+ resolve(data);
27
+ });
28
+ });
29
+ }
30
+
31
+ waitForMsgType(self, 'wasm_bindgen_worker_init').then(async ({ init, receiver }) => {
32
+ // # Note 1
33
+ // Our JS should have been generated in
34
+ // `[out-dir]/snippets/wasm-bindgen-rayon-[hash]/workerHelpers.js`,
35
+ // resolve the main module via `../../..`.
36
+ //
37
+ // This might need updating if the generated structure changes on wasm-bindgen
38
+ // side ever in the future, but works well with bundlers today. The whole
39
+ // point of this crate, after all, is to abstract away unstable features
40
+ // and temporary bugs so that you don't need to deal with them in your code.
41
+ //
42
+ // # Note 2
43
+ // This could be a regular import, but then some bundlers complain about
44
+ // circular deps.
45
+ //
46
+ // Dynamic import could be cheap if this file was inlined into the parent,
47
+ // which would require us just using `../../..` in `new Worker` below,
48
+ // but that doesn't work because wasm-pack unconditionally adds
49
+ // "sideEffects":false (see below).
50
+ //
51
+ // OTOH, even though it can't be inlined, it should be still reasonably
52
+ // cheap since the requested file is already in cache (it was loaded by
53
+ // the main thread).
54
+ const pkg = await import('../../..');
55
+ await pkg.default(init);
56
+ postMessage({ type: 'wasm_bindgen_worker_ready' });
57
+ pkg.wbg_rayon_start_worker(receiver);
58
+ });
59
+
60
+ // Note: this is never used, but necessary to prevent a bug in Firefox
61
+ // (https://bugzilla.mozilla.org/show_bug.cgi?id=1702191) where it collects
62
+ // Web Workers that have a shared WebAssembly memory with the main thread,
63
+ // but are not explicitly rooted via a `Worker` instance.
64
+ //
65
+ // By storing them in a variable, we can keep `Worker` objects around and
66
+ // prevent them from getting GC-d.
67
+ let _workers;
68
+
69
+ export async function startWorkers(module, memory, builder) {
70
+ if (builder.numThreads() === 0) {
71
+ throw new Error(`num_threads must be > 0.`);
72
+ }
73
+
74
+ const workerInit = {
75
+ type: 'wasm_bindgen_worker_init',
76
+ init: { module_or_path: module, memory },
77
+ receiver: builder.receiver()
78
+ };
79
+
80
+ _workers = await Promise.all(
81
+ Array.from({ length: builder.numThreads() }, async () => {
82
+ // Self-spawn into a new Worker.
83
+ //
84
+ // TODO: while `new URL('...', import.meta.url) becomes a semi-standard
85
+ // way to get asset URLs relative to the module across various bundlers
86
+ // and browser, ideally we should switch to `import.meta.resolve`
87
+ // once it becomes a standard.
88
+ //
89
+ // Note: we could use `../../..` as the URL here to inline workerHelpers.js
90
+ // into the parent entry instead of creating another split point -
91
+ // this would be preferable from optimization perspective -
92
+ // however, Webpack then eliminates all message handler code
93
+ // because wasm-pack produces "sideEffects":false in package.json
94
+ // unconditionally.
95
+ //
96
+ // The only way to work around that is to have side effect code
97
+ // in an entry point such as Worker file itself.
98
+ const worker = new Worker(new URL('./workerHelpers.js', import.meta.url), {
99
+ type: 'module'
100
+ });
101
+ worker.postMessage(workerInit);
102
+ await waitForMsgType(worker, 'wasm_bindgen_worker_ready');
103
+ return worker;
104
+ })
105
+ );
106
+ builder.build();
107
+ }
package/wasm_bhtsne.d.ts CHANGED
@@ -1,116 +1,93 @@
1
1
  /* tslint:disable */
2
2
  /* eslint-disable */
3
- /**
4
- * @param {number} num_threads
5
- * @returns {Promise<any>}
6
- */
7
- export function initThreadPool(num_threads: number): Promise<any>;
8
- /**
9
- * @param {number} receiver
10
- */
11
- export function wbg_rayon_start_worker(receiver: number): void;
12
- /**
13
- * t-distributed stochastic neighbor embedding. Provides a parallel implementation of both the
14
- * exact version of the algorithm and the tree accelerated one leveraging space partitioning trees.
15
- */
3
+
16
4
  export class bhtSNEf32 {
17
5
  free(): void;
18
- /**
19
- * @param {any} data
20
- * @param {any} opt
21
- */
6
+ [Symbol.dispose](): void;
22
7
  constructor(data: any, opt: any);
23
- /**
24
- * Performs a parallel Barnes-Hut approximation of the t-SNE algorithm.
25
- *
26
- * # Arguments
27
- *
28
- * `epochs` - the maximum number of fitting iterations. Must be positive
29
- * @param {number} epochs
30
- * @returns {any}
31
- */
8
+ /**
9
+ * Performs a parallel Barnes-Hut approximation of the t-SNE algorithm.
10
+ *
11
+ * # Arguments
12
+ *
13
+ * `epochs` - the maximum number of fitting iterations. Must be positive
14
+ */
32
15
  step(epochs: number): any;
33
16
  }
34
- /**
35
- */
17
+
36
18
  export class bhtSNEf64 {
37
19
  free(): void;
38
- /**
39
- * @param {any} data
40
- * @param {any} opt
41
- */
20
+ [Symbol.dispose](): void;
42
21
  constructor(data: any, opt: any);
43
- /**
44
- * Performs a parallel Barnes-Hut approximation of the t-SNE algorithm.
45
- *
46
- * # Arguments
47
- *
48
- * `epochs` - Sets epochs, the maximum number of fitting iterations.
49
- * @param {number} epochs
50
- * @returns {any}
51
- */
22
+ /**
23
+ * Performs a parallel Barnes-Hut approximation of the t-SNE algorithm.
24
+ *
25
+ * # Arguments
26
+ *
27
+ * `epochs` - Sets epochs, the maximum number of fitting iterations.
28
+ */
52
29
  step(epochs: number): any;
53
30
  }
54
- /**
55
- */
31
+
32
+ export function initThreadPool(num_threads: number): Promise<any>;
33
+
56
34
  export class wbg_rayon_PoolBuilder {
35
+ private constructor();
57
36
  free(): void;
58
- /**
59
- * @returns {number}
60
- */
37
+ [Symbol.dispose](): void;
61
38
  numThreads(): number;
62
- /**
63
- * @returns {number}
64
- */
65
- receiver(): number;
66
- /**
67
- */
68
39
  build(): void;
40
+ receiver(): number;
69
41
  }
70
42
 
43
+ export function wbg_rayon_start_worker(receiver: number): void;
44
+
71
45
  export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
72
46
 
73
47
  export interface InitOutput {
74
- readonly __wbg_bhtsnef32_free: (a: number) => void;
75
- readonly bhtsnef32_new: (a: number, b: number) => number;
76
- readonly bhtsnef32_step: (a: number, b: number, c: number) => void;
77
- readonly __wbg_bhtsnef64_free: (a: number) => void;
78
- readonly bhtsnef64_new: (a: number, b: number) => number;
79
- readonly bhtsnef64_step: (a: number, b: number, c: number) => void;
80
- readonly __wbg_wbg_rayon_poolbuilder_free: (a: number) => void;
48
+ readonly __wbg_bhtsnef32_free: (a: number, b: number) => void;
49
+ readonly __wbg_bhtsnef64_free: (a: number, b: number) => void;
50
+ readonly bhtsnef32_new: (a: any, b: any) => number;
51
+ readonly bhtsnef32_step: (a: number, b: number) => [number, number, number];
52
+ readonly bhtsnef64_new: (a: any, b: any) => number;
53
+ readonly bhtsnef64_step: (a: number, b: number) => [number, number, number];
54
+ readonly __wbg_wbg_rayon_poolbuilder_free: (a: number, b: number) => void;
55
+ readonly initThreadPool: (a: number) => any;
56
+ readonly wbg_rayon_poolbuilder_build: (a: number) => void;
81
57
  readonly wbg_rayon_poolbuilder_numThreads: (a: number) => number;
82
58
  readonly wbg_rayon_poolbuilder_receiver: (a: number) => number;
83
- readonly wbg_rayon_poolbuilder_build: (a: number) => void;
84
- readonly initThreadPool: (a: number) => number;
85
59
  readonly wbg_rayon_start_worker: (a: number) => void;
86
60
  readonly memory: WebAssembly.Memory;
87
61
  readonly __wbindgen_malloc: (a: number, b: number) => number;
88
62
  readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
89
- readonly __wbindgen_add_to_stack_pointer: (a: number) => number;
90
63
  readonly __wbindgen_exn_store: (a: number) => void;
91
- readonly __wbindgen_thread_destroy: (a?: number, b?: number) => void;
92
- readonly __wbindgen_start: () => void;
64
+ readonly __externref_table_alloc: () => number;
65
+ readonly __wbindgen_externrefs: WebAssembly.Table;
66
+ readonly __externref_table_dealloc: (a: number) => void;
67
+ readonly __wbindgen_thread_destroy: (a?: number, b?: number, c?: number) => void;
68
+ readonly __wbindgen_start: (a: number) => void;
93
69
  }
94
70
 
95
71
  export type SyncInitInput = BufferSource | WebAssembly.Module;
72
+
96
73
  /**
97
74
  * Instantiates the given `module`, which can either be bytes or
98
75
  * a precompiled `WebAssembly.Module`.
99
76
  *
100
- * @param {SyncInitInput} module
101
- * @param {WebAssembly.Memory} maybe_memory
77
+ * @param {{ module: SyncInitInput, memory?: WebAssembly.Memory, thread_stack_size?: number }} module - Passing `SyncInitInput` directly is deprecated.
78
+ * @param {WebAssembly.Memory} memory - Deprecated.
102
79
  *
103
80
  * @returns {InitOutput}
104
81
  */
105
- export function initSync(module: SyncInitInput, maybe_memory?: WebAssembly.Memory): InitOutput;
82
+ export function initSync(module: { module: SyncInitInput, memory?: WebAssembly.Memory, thread_stack_size?: number } | SyncInitInput, memory?: WebAssembly.Memory): InitOutput;
106
83
 
107
84
  /**
108
85
  * If `module_or_path` is {RequestInfo} or {URL}, makes a request and
109
86
  * for everything else, calls `WebAssembly.instantiate` directly.
110
87
  *
111
- * @param {InitInput | Promise<InitInput>} module_or_path
112
- * @param {WebAssembly.Memory} maybe_memory
88
+ * @param {{ module_or_path: InitInput | Promise<InitInput>, memory?: WebAssembly.Memory, thread_stack_size?: number }} module_or_path - Passing `InitInput` directly is deprecated.
89
+ * @param {WebAssembly.Memory} memory - Deprecated.
113
90
  *
114
91
  * @returns {Promise<InitOutput>}
115
92
  */
116
- export default function __wbg_init (module_or_path?: InitInput | Promise<InitInput>, maybe_memory?: WebAssembly.Memory): Promise<InitOutput>;
93
+ export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput>, memory?: WebAssembly.Memory, thread_stack_size?: number } | InitInput | Promise<InitInput>, memory?: WebAssembly.Memory): Promise<InitOutput>;