UNPKG

3.09 kBPlain TextView Raw
1/**
2 * @license
3 * Copyright 2018 Google LLC. All Rights Reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 * =============================================================================
16 */
17
18// Register all kernels.
19import './register_all_kernels';
20
21import * as tf from '@tensorflow/tfjs';
22import * as path from 'path';
23
24import {ProgbarLogger} from './callbacks';
25import {nodeFileSystemRouter} from './io/file_system';
26import * as nodeIo from './io/index';
27import {NodeJSKernelBackend} from './nodejs_kernel_backend';
28import {TFJSBinding} from './tfjs_binding';
29import * as nodeVersion from './version';
30
31// tslint:disable-next-line:no-require-imports
32const binary = require('@mapbox/node-pre-gyp');
33const bindingPath =
34 binary.find(path.resolve(path.join(__dirname, '/../package.json')));
35
36// Check if the node native addon module exists.
37// tslint:disable-next-line:no-require-imports
38const fs = require('fs');
39if (!fs.existsSync(bindingPath)) {
40 throw new Error(
41 `The Node.js native addon module (tfjs_binding.node) can not ` +
42 `be found at path: ` + String(bindingPath) + `. \nPlease run command ` +
43 `'npm rebuild @tensorflow/tfjs-node` +
44 (String(bindingPath).indexOf('tfjs-node-gpu') > 0 ? `-gpu` : ``) +
45 ` --build-addon-from-source' to ` +
46 `rebuild the native addon module. \nIf you have problem with building ` +
47 `the addon module, please check ` +
48 `https://github.com/tensorflow/tfjs/blob/master/tfjs-node/` +
49 `WINDOWS_TROUBLESHOOTING.md or file an issue.`);
50}
51// tslint:disable-next-line:no-require-imports
52const bindings = require(bindingPath);
53
54// Merge version and io namespaces.
55export const version = {
56 ...tf.version,
57 'tfjs-node': nodeVersion.version
58};
59export const io = {
60 ...tf.io,
61 ...nodeIo
62};
63
64// Export all union package symbols
65export * from '@tensorflow/tfjs';
66export * from './node';
67
68// tslint:disable-next-line:no-require-imports
69const pjson = require('../package.json');
70
71// Side effects for default initialization of Node backend.
72tf.registerBackend('tensorflow', () => {
73 return new NodeJSKernelBackend(bindings as TFJSBinding, pjson.name);
74}, 3 /* priority */);
75
76const success = tf.setBackend('tensorflow');
77if (!success) {
78 throw new Error(`Could not initialize TensorFlow backend.`);
79}
80
81// Register the model saving and loading handlers for the 'file://' URL scheme.
82tf.io.registerLoadRouter(nodeFileSystemRouter);
83tf.io.registerSaveRouter(nodeFileSystemRouter);
84
85// Register the ProgbarLogger for Model.fit() at verbosity level 1.
86tf.registerCallbackConstructor(1, ProgbarLogger);