@elarsaks/umap-wasm 0.4.6 → 0.4.8

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/dist/umap.d.ts CHANGED
@@ -99,7 +99,6 @@ export declare class UMAP {
99
99
  private optimizeLayout;
100
100
  private optimizeLayoutBatchWasm;
101
101
  private materializeEmbeddingFromWasm;
102
- private advanceRngState;
103
102
  private getNEpochs;
104
103
  }
105
104
  export declare function euclidean(x: Vector, y: Vector): number;
package/dist/umap.js CHANGED
@@ -484,6 +484,8 @@ export class UMAP {
484
484
  if (this.useWasmOptimizer && wasmBridge.isWasmAvailable()) {
485
485
  const { head, tail, nEpochs, nVertices, a, b } = this.optimizationState;
486
486
  this.wasmOptimizerState = wasmBridge.createOptimizerState(head, tail, headEmbedding, tailEmbedding, epochsPerSample, epochsPerNegativeSample, moveOther, learningRate, repulsionStrength, a, b, dim, nEpochs, nVertices);
487
+ this.rngState = BigInt(Math.floor(this.random() * 0xFFFFFFFF));
488
+ this.wasmOptimizerState.set_rng_seed(this.rngState);
487
489
  }
488
490
  }
489
491
  initializeOptimization() {
@@ -507,8 +509,7 @@ export class UMAP {
507
509
  }
508
510
  optimizeLayoutStep(n) {
509
511
  if (this.useWasmOptimizer && this.wasmOptimizerState) {
510
- wasmBridge.optimizeLayoutStepInPlaceWasm(this.wasmOptimizerState, this.rngState);
511
- this.advanceRngState(1);
512
+ wasmBridge.optimizeLayoutStepInPlaceWasm(this.wasmOptimizerState);
512
513
  this.optimizationState.currentEpoch += 1;
513
514
  return this.materializeEmbeddingFromWasm();
514
515
  }
@@ -657,8 +658,7 @@ export class UMAP {
657
658
  if (actualSteps <= 0) {
658
659
  return 0;
659
660
  }
660
- wasmBridge.optimizeLayoutBatchInPlaceWasm(this.wasmOptimizerState, this.rngState, actualSteps);
661
- this.advanceRngState(actualSteps);
661
+ wasmBridge.optimizeLayoutBatchInPlaceWasm(this.wasmOptimizerState, actualSteps);
662
662
  this.optimizationState.currentEpoch += actualSteps;
663
663
  return actualSteps;
664
664
  }
@@ -680,13 +680,6 @@ export class UMAP {
680
680
  this.embedding = embedding;
681
681
  return embedding;
682
682
  }
683
- advanceRngState(steps) {
684
- const A = BigInt('6364136223846793005');
685
- const C = BigInt('1442695040888963407');
686
- for (let i = 0; i < steps; i++) {
687
- this.rngState = (this.rngState * A + C) & BigInt('0xFFFFFFFFFFFFFFFF');
688
- }
689
- }
690
683
  getNEpochs() {
691
684
  const graph = this.graph;
692
685
  if (this.nEpochs > 0) {
@@ -64,16 +64,18 @@ export declare function wasmSparseMatrixGetAllTyped(matrix: WasmSparseMatrix): {
64
64
  export declare function nnDescentWasm(data: number[][], leafArray: ArrayLike<number>[], nNeighbors: number, nIters?: number, maxCandidates?: number, delta?: number, rho?: number, rpTreeInit?: boolean, distanceMetric?: string, seed?: number): number[][][];
65
65
  export declare function nnDescentWasmFlat(flatData: Float64Array, nSamples: number, dim: number, flatLeafArray: Int32Array, nLeaves: number, leafSize: number, nNeighbors: number, nIters?: number, maxCandidates?: number, delta?: number, rho?: number, rpTreeInit?: boolean, distanceMetric?: string, seed?: number): number[];
66
66
  export interface WasmOptimizerState {
67
- head_embedding: Float64Array;
67
+ head_embedding: Float32Array;
68
68
  current_epoch: number;
69
69
  n_epochs: number;
70
70
  head_embedding_ptr(): number;
71
71
  head_embedding_len(): number;
72
+ set_rng_seed(seed: bigint): void;
73
+ rng_seed(): bigint;
72
74
  free(): void;
73
75
  }
74
76
  export declare function createOptimizerState(head: number[], tail: number[], headEmbedding: number[][], tailEmbedding: number[][], epochsPerSample: number[], epochsPerNegativeSample: number[], moveOther: boolean, initialAlpha: number, gamma: number, a: number, b: number, dim: number, nEpochs: number, nVertices: number): WasmOptimizerState;
75
- export declare function optimizeLayoutStepWasm(state: WasmOptimizerState, rngSeed: bigint): Float64Array;
76
- export declare function optimizeLayoutStepInPlaceWasm(state: WasmOptimizerState, rngSeed: bigint): void;
77
- export declare function optimizeLayoutBatchWasm(state: WasmOptimizerState, rngSeed: bigint, nSteps: number): Float64Array;
78
- export declare function optimizeLayoutBatchInPlaceWasm(state: WasmOptimizerState, rngSeed: bigint, nSteps: number): void;
79
- export declare function getOptimizerEmbeddingView(state: WasmOptimizerState): Float64Array;
77
+ export declare function optimizeLayoutStepWasm(state: WasmOptimizerState): Float32Array;
78
+ export declare function optimizeLayoutStepInPlaceWasm(state: WasmOptimizerState): void;
79
+ export declare function optimizeLayoutBatchWasm(state: WasmOptimizerState, nSteps: number): Float32Array;
80
+ export declare function optimizeLayoutBatchInPlaceWasm(state: WasmOptimizerState, nSteps: number): void;
81
+ export declare function getOptimizerEmbeddingView(state: WasmOptimizerState): Float32Array;
@@ -273,8 +273,8 @@ export function nnDescentWasmFlat(flatData, nSamples, dim, flatLeafArray, nLeave
273
273
  export function createOptimizerState(head, tail, headEmbedding, tailEmbedding, epochsPerSample, epochsPerNegativeSample, moveOther, initialAlpha, gamma, a, b, dim, nEpochs, nVertices) {
274
274
  if (!wasmModule)
275
275
  throw new Error('WASM module not initialized');
276
- const flatHeadEmbedding = new Float64Array(headEmbedding.length * dim);
277
- const flatTailEmbedding = new Float64Array(tailEmbedding.length * dim);
276
+ const flatHeadEmbedding = new Float32Array(headEmbedding.length * dim);
277
+ const flatTailEmbedding = new Float32Array(tailEmbedding.length * dim);
278
278
  for (let i = 0; i < headEmbedding.length; i++) {
279
279
  for (let j = 0; j < dim; j++) {
280
280
  flatHeadEmbedding[i * dim + j] = headEmbedding[i][j];
@@ -287,29 +287,29 @@ export function createOptimizerState(head, tail, headEmbedding, tailEmbedding, e
287
287
  }
288
288
  const headArray = new Uint32Array(head);
289
289
  const tailArray = new Uint32Array(tail);
290
- const epochsPerSampleArray = new Float64Array(epochsPerSample);
291
- const epochsPerNegativeSampleArray = new Float64Array(epochsPerNegativeSample);
290
+ const epochsPerSampleArray = new Float32Array(epochsPerSample);
291
+ const epochsPerNegativeSampleArray = new Float32Array(epochsPerNegativeSample);
292
292
  return new wasmModule.OptimizerState(headArray, tailArray, flatHeadEmbedding, flatTailEmbedding, epochsPerSampleArray, epochsPerNegativeSampleArray, moveOther, initialAlpha, gamma, a, b, dim, nEpochs, nVertices);
293
293
  }
294
- export function optimizeLayoutStepWasm(state, rngSeed) {
294
+ export function optimizeLayoutStepWasm(state) {
295
295
  if (!wasmModule)
296
296
  throw new Error('WASM module not initialized');
297
- return wasmModule.optimize_layout_step(state, rngSeed);
297
+ return wasmModule.optimize_layout_step(state);
298
298
  }
299
- export function optimizeLayoutStepInPlaceWasm(state, rngSeed) {
299
+ export function optimizeLayoutStepInPlaceWasm(state) {
300
300
  if (!wasmModule)
301
301
  throw new Error('WASM module not initialized');
302
- wasmModule.optimize_layout_step_in_place(state, rngSeed);
302
+ wasmModule.optimize_layout_step_in_place(state);
303
303
  }
304
- export function optimizeLayoutBatchWasm(state, rngSeed, nSteps) {
304
+ export function optimizeLayoutBatchWasm(state, nSteps) {
305
305
  if (!wasmModule)
306
306
  throw new Error('WASM module not initialized');
307
- return wasmModule.optimize_layout_batch(state, rngSeed, nSteps);
307
+ return wasmModule.optimize_layout_batch(state, nSteps);
308
308
  }
309
- export function optimizeLayoutBatchInPlaceWasm(state, rngSeed, nSteps) {
309
+ export function optimizeLayoutBatchInPlaceWasm(state, nSteps) {
310
310
  if (!wasmModule)
311
311
  throw new Error('WASM module not initialized');
312
- wasmModule.optimize_layout_batch_in_place(state, rngSeed, nSteps);
312
+ wasmModule.optimize_layout_batch_in_place(state, nSteps);
313
313
  }
314
314
  export function getOptimizerEmbeddingView(state) {
315
315
  if (!wasmModule)
@@ -322,5 +322,5 @@ export function getOptimizerEmbeddingView(state) {
322
322
  if (!memory?.buffer) {
323
323
  throw new Error('WASM memory is not available');
324
324
  }
325
- return new Float64Array(memory.buffer, ptr, len);
325
+ return new Float32Array(memory.buffer, ptr, len);
326
326
  }
package/lib/umap-js.js CHANGED
@@ -6747,8 +6747,8 @@ function nnDescentWasmFlat(flatData, nSamples, dim, flatLeafArray, nLeaves, leaf
6747
6747
  function createOptimizerState(head, tail, headEmbedding, tailEmbedding, epochsPerSample, epochsPerNegativeSample, moveOther, initialAlpha, gamma, a, b, dim, nEpochs, nVertices) {
6748
6748
  if (!wasmModule)
6749
6749
  throw new Error('WASM module not initialized');
6750
- const flatHeadEmbedding = new Float64Array(headEmbedding.length * dim);
6751
- const flatTailEmbedding = new Float64Array(tailEmbedding.length * dim);
6750
+ const flatHeadEmbedding = new Float32Array(headEmbedding.length * dim);
6751
+ const flatTailEmbedding = new Float32Array(tailEmbedding.length * dim);
6752
6752
  for (let i = 0; i < headEmbedding.length; i++) {
6753
6753
  for (let j = 0; j < dim; j++) {
6754
6754
  flatHeadEmbedding[i * dim + j] = headEmbedding[i][j];
@@ -6761,29 +6761,29 @@ function createOptimizerState(head, tail, headEmbedding, tailEmbedding, epochsPe
6761
6761
  }
6762
6762
  const headArray = new Uint32Array(head);
6763
6763
  const tailArray = new Uint32Array(tail);
6764
- const epochsPerSampleArray = new Float64Array(epochsPerSample);
6765
- const epochsPerNegativeSampleArray = new Float64Array(epochsPerNegativeSample);
6764
+ const epochsPerSampleArray = new Float32Array(epochsPerSample);
6765
+ const epochsPerNegativeSampleArray = new Float32Array(epochsPerNegativeSample);
6766
6766
  return new wasmModule.OptimizerState(headArray, tailArray, flatHeadEmbedding, flatTailEmbedding, epochsPerSampleArray, epochsPerNegativeSampleArray, moveOther, initialAlpha, gamma, a, b, dim, nEpochs, nVertices);
6767
6767
  }
6768
- function optimizeLayoutStepWasm(state, rngSeed) {
6768
+ function optimizeLayoutStepWasm(state) {
6769
6769
  if (!wasmModule)
6770
6770
  throw new Error('WASM module not initialized');
6771
- return wasmModule.optimize_layout_step(state, rngSeed);
6771
+ return wasmModule.optimize_layout_step(state);
6772
6772
  }
6773
- function optimizeLayoutStepInPlaceWasm(state, rngSeed) {
6773
+ function optimizeLayoutStepInPlaceWasm(state) {
6774
6774
  if (!wasmModule)
6775
6775
  throw new Error('WASM module not initialized');
6776
- wasmModule.optimize_layout_step_in_place(state, rngSeed);
6776
+ wasmModule.optimize_layout_step_in_place(state);
6777
6777
  }
6778
- function optimizeLayoutBatchWasm(state, rngSeed, nSteps) {
6778
+ function optimizeLayoutBatchWasm(state, nSteps) {
6779
6779
  if (!wasmModule)
6780
6780
  throw new Error('WASM module not initialized');
6781
- return wasmModule.optimize_layout_batch(state, rngSeed, nSteps);
6781
+ return wasmModule.optimize_layout_batch(state, nSteps);
6782
6782
  }
6783
- function optimizeLayoutBatchInPlaceWasm(state, rngSeed, nSteps) {
6783
+ function optimizeLayoutBatchInPlaceWasm(state, nSteps) {
6784
6784
  if (!wasmModule)
6785
6785
  throw new Error('WASM module not initialized');
6786
- wasmModule.optimize_layout_batch_in_place(state, rngSeed, nSteps);
6786
+ wasmModule.optimize_layout_batch_in_place(state, nSteps);
6787
6787
  }
6788
6788
  function getOptimizerEmbeddingView(state) {
6789
6789
  if (!wasmModule)
@@ -6796,7 +6796,7 @@ function getOptimizerEmbeddingView(state) {
6796
6796
  if (!memory?.buffer) {
6797
6797
  throw new Error('WASM memory is not available');
6798
6798
  }
6799
- return new Float64Array(memory.buffer, ptr, len);
6799
+ return new Float32Array(memory.buffer, ptr, len);
6800
6800
  }
6801
6801
 
6802
6802
  ;// ./src/tree.ts
@@ -8118,6 +8118,8 @@ class UMAP {
8118
8118
  if (this.useWasmOptimizer && isWasmAvailable()) {
8119
8119
  const { head, tail, nEpochs, nVertices, a, b } = this.optimizationState;
8120
8120
  this.wasmOptimizerState = createOptimizerState(head, tail, headEmbedding, tailEmbedding, epochsPerSample, epochsPerNegativeSample, moveOther, learningRate, repulsionStrength, a, b, dim, nEpochs, nVertices);
8121
+ this.rngState = BigInt(Math.floor(this.random() * 0xFFFFFFFF));
8122
+ this.wasmOptimizerState.set_rng_seed(this.rngState);
8121
8123
  }
8122
8124
  }
8123
8125
  initializeOptimization() {
@@ -8141,8 +8143,7 @@ class UMAP {
8141
8143
  }
8142
8144
  optimizeLayoutStep(n) {
8143
8145
  if (this.useWasmOptimizer && this.wasmOptimizerState) {
8144
- optimizeLayoutStepInPlaceWasm(this.wasmOptimizerState, this.rngState);
8145
- this.advanceRngState(1);
8146
+ optimizeLayoutStepInPlaceWasm(this.wasmOptimizerState);
8146
8147
  this.optimizationState.currentEpoch += 1;
8147
8148
  return this.materializeEmbeddingFromWasm();
8148
8149
  }
@@ -8291,8 +8292,7 @@ class UMAP {
8291
8292
  if (actualSteps <= 0) {
8292
8293
  return 0;
8293
8294
  }
8294
- optimizeLayoutBatchInPlaceWasm(this.wasmOptimizerState, this.rngState, actualSteps);
8295
- this.advanceRngState(actualSteps);
8295
+ optimizeLayoutBatchInPlaceWasm(this.wasmOptimizerState, actualSteps);
8296
8296
  this.optimizationState.currentEpoch += actualSteps;
8297
8297
  return actualSteps;
8298
8298
  }
@@ -8314,13 +8314,6 @@ class UMAP {
8314
8314
  this.embedding = embedding;
8315
8315
  return embedding;
8316
8316
  }
8317
- advanceRngState(steps) {
8318
- const A = BigInt('6364136223846793005');
8319
- const C = BigInt('1442695040888963407');
8320
- for (let i = 0; i < steps; i++) {
8321
- this.rngState = (this.rngState * A + C) & BigInt('0xFFFFFFFFFFFFFFFF');
8322
- }
8323
- }
8324
8317
  getNEpochs() {
8325
8318
  const graph = this.graph;
8326
8319
  if (this.nEpochs > 0) {