-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
52 lines (45 loc) · 1.38 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// The core APIs are in native bindings.
const core = require(__dirname + '/build/Release/mlx.node');
// Helper for creating complex number.
core.Complex = (re, im) => {
return {re, im: im ?? 0};
};
// The stream helper is in TS.
core.stream = require('./dist/stream').stream;
// Export core (mx) module.
exports.core = core;
// Export utils module.
exports.utils = require('./dist/utils');
// Lazy-load nn/optimizers modules to avoid circular references.
const cache = {};
defineLazyModule('nn');
defineLazyModule('optimizers');
// Export nn/optimizers modules.
// Note that while it is tempting to get rid of the |cache| object and define
// the lazy property on |exports| directly, doing so would make the exports
// undetectable from cjs-module-lexer, and "import {core} from 'mlx'" will not
// work in Node.js.
Object.defineProperty(exports, 'nn', {
enumerable: true,
get() { return cache.nn; }
});
Object.defineProperty(exports, 'optimizers', {
enumerable: true,
get() { return cache.optimizers; }
});
// Helper to define a lazy loaded property on |cache|.
function defineLazyModule(name) {
Object.defineProperty(cache, name, {
configurable: true,
enumerable: true,
get() {
const mod = require(`./dist/${name}`);
Object.defineProperty(cache, name, {
value: mod,
writable: false,
configurable: false,
});
return mod;
}
});
}