gx
chenyc
2025-06-12 7b72ac13a83764a662159d4a49b7fffb90476ecb
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env node
"use strict";
/**
 * @license
 * Copyright 2020 Google Inc. All Rights Reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * =============================================================================
 */
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * Entry point for cli tool to build custom tfjs modules
 */
var fs = require("fs");
var path = require("path");
var yargs = require("yargs");
var tfjs_core_1 = require("@tensorflow/tfjs-core");
var util_1 = require("./util");
var types_1 = require("./types");
var esm_module_provider_1 = require("./esm_module_provider");
// Will be configured when loading the config file.
var moduleProvider;
var BASE_PATH = process.env.BASE_PATH || process.cwd();
var DEFAULT_CUSTOM_BUNDLE_ARGS = {
    entries: [],
    models: [],
    kernels: [],
    forwardModeOnly: true,
    backends: ['cpu', 'webgl'],
    moduleOptions: {},
};
var argParser = yargs.options({
    config: {
        description: 'Path to custom module config file.',
        type: 'string',
        demandOption: true
    }
});
var args = argParser.argv;
function validateArgs() {
    var configFilePath = args.config;
    if (configFilePath == null) {
        (0, util_1.bail)("Error: no config file passed");
    }
    configFilePath = path.resolve(BASE_PATH, configFilePath);
    if (!fs.existsSync(configFilePath)) {
        (0, util_1.bail)("Error: config file does not exist at ".concat(configFilePath));
    }
    var config;
    try {
        config = JSON.parse(fs.readFileSync(configFilePath, 'utf-8'));
    }
    catch (error) {
        (0, util_1.bail)("Error could not read/parse JSON config file. \n ".concat(error.message));
    }
    if (config.outputPath == null) {
        (0, util_1.bail)('Error: config must specify "outputPath" property');
    }
    console.log("Using custom module configuration from ".concat(configFilePath, "."));
    var finalConfig = Object.assign({}, DEFAULT_CUSTOM_BUNDLE_ARGS, config);
    if (finalConfig.entries.length !== 0) {
        (0, util_1.bail)('Error: config.entries not yet supported');
    }
    // if (finalConfig.models.length !== 0) {
    // TODO validate that all these paths exist.
    // bail('Error: config.models not yet supported');
    // }
    for (var _i = 0, _a = finalConfig.backends; _i < _a.length; _i++) {
        var requestedBackend = _a[_i];
        if (requestedBackend !== types_1.SupportedBackends.cpu &&
            requestedBackend !== types_1.SupportedBackends.webgl &&
            requestedBackend !== types_1.SupportedBackends.wasm) {
            (0, util_1.bail)("Error: Unsupported backend specified '".concat(requestedBackend, "'"));
        }
    }
    // Normalize the paths to absolute paths.
    function normalizePath(p) {
        return path.resolve(BASE_PATH, p);
    }
    finalConfig.models = finalConfig.models.map(normalizePath);
    finalConfig.entries = finalConfig.entries.map(normalizePath);
    finalConfig.normalizedOutputPath = normalizePath(finalConfig.outputPath);
    moduleProvider = (0, esm_module_provider_1.getModuleProvider)(finalConfig.moduleOptions);
    console.log('Final Configuration', finalConfig);
    return finalConfig;
}
function getKernelNamesForConfig(config) {
    // Later on this will do a union of kernels from entries, models and
    // kernels, (and kernels used by the converter itself) Currently we only
    // support directly listing kernels. remember that this also needs to handle
    // kernels used by gradients if forwardModeOnly is false.
    // Ops in core that are implemented as custom ops may appear in tf.profile
    // they will have __op as a suffix. These do not have corresponding backend
    // kernels so we need to filter them out.
    function isNotCustomOp(kernelName) {
        // opSuffix value is defined in tfjs-core/src/operation.ts
        // duplicating it here to avoid an export.
        return !kernelName.endsWith(tfjs_core_1.OP_SCOPE_SUFFIX);
    }
    return config.kernels.filter(isNotCustomOp);
}
var customConfig = validateArgs();
var kernelsToInclude = getKernelNamesForConfig(customConfig);
customConfig.kernels = kernelsToInclude;
if (moduleProvider != null) {
    moduleProvider.produceCustomTFJSModule(customConfig);
}
else {
    throw new Error('No module provider has been initialized.');
}
//# sourceMappingURL=cli.js.map