UNPKG

2.4 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright 2020 Google LLC. All Rights Reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * =============================================================================
17 */
18Object.defineProperty(exports, "__esModule", { value: true });
19var tfjs_1 = require("@tensorflow/tfjs");
20var nodejs_kernel_backend_1 = require("../nodejs_kernel_backend");
21exports.gatherV2Config = {
22 kernelName: tfjs_1.GatherV2,
23 backendName: 'tensorflow',
24 kernelFunc: function (args) {
25 var _a = args.inputs, x = _a.x, indices = _a.indices;
26 var backend = args.backend;
27 var _b = args.attrs, axis = _b.axis, batchDims = _b.batchDims;
28 // Throw error when any index is out of bound.
29 var indicesVals = backend.readSync(indices.dataId);
30 var axisDim = x.shape[axis];
31 var _loop_1 = function (i) {
32 var index = indicesVals[i];
33 tfjs_1.util.assert(index <= axisDim - 1 && index >= 0, function () { return "GatherV2: the index value " + index + " is not in [0, " + (axisDim - 1) + "]"; });
34 };
35 for (var i = 0; i < indicesVals.length; ++i) {
36 _loop_1(i);
37 }
38 // validate the inputs
39 tfjs_1.backend_util.segment_util.collectGatherOpShapeInfo(x, indices, axis, batchDims);
40 var axisTensor = tfjs_1.scalar(axis, 'int32');
41 var opAttrs = [
42 { name: 'batch_dims', type: backend.binding.TF_ATTR_INT, value: batchDims },
43 nodejs_kernel_backend_1.createTensorsTypeOpAttr('Tparams', x.dtype),
44 nodejs_kernel_backend_1.createTensorsTypeOpAttr('Tindices', indices.dtype),
45 nodejs_kernel_backend_1.createTensorsTypeOpAttr('Taxis', 'int32')
46 ];
47 var res = backend.executeSingleOutput(tfjs_1.GatherV2, opAttrs, [x, indices, axisTensor]);
48 axisTensor.dispose();
49 return res;
50 }
51};