UNPKG

5.47 kBJavaScriptView 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 */
17import * as tfc from '@tensorflow/tfjs-core';
18import { NodeValueImpl } from './custom_op/node_value_impl';
19import { getRegisteredOp } from './custom_op/register';
20import * as arithmetic from './executors/arithmetic_executor';
21import * as basicMath from './executors/basic_math_executor';
22import * as control from './executors/control_executor';
23import * as convolution from './executors/convolution_executor';
24import * as creation from './executors/creation_executor';
25import * as dynamic from './executors/dynamic_executor';
26import * as evaluation from './executors/evaluation_executor';
27import * as graph from './executors/graph_executor';
28import * as hashTable from './executors/hash_table_executor';
29import * as image from './executors/image_executor';
30import * as logical from './executors/logical_executor';
31import * as matrices from './executors/matrices_executor';
32import * as normalization from './executors/normalization_executor';
33import * as reduction from './executors/reduction_executor';
34import * as sliceJoin from './executors/slice_join_executor';
35import * as sparse from './executors/sparse_executor';
36import * as spectral from './executors/spectral_executor';
37import * as string from './executors/string_executor';
38import * as transformation from './executors/transformation_executor';
39/**
40 * Executes the op defined by the node object.
41 * @param node
42 * @param tensorMap contains tensors for executed nodes and weights
43 * @param context contains tensors and information for running the current node.
44 * @param resourceManager Optional. Contains global resources of the model.
45 */
46export function executeOp(node, tensorMap, context, resourceManager) {
47 const value = ((node, tensorMap, context) => {
48 switch (node.category) {
49 case 'arithmetic':
50 return tfc.tidy(() => arithmetic.executeOp(node, tensorMap, context));
51 case 'basic_math':
52 return tfc.tidy(() => basicMath.executeOp(node, tensorMap, context));
53 case 'control':
54 return control.executeOp(node, tensorMap, context);
55 case 'convolution':
56 return tfc.tidy(() => convolution.executeOp(node, tensorMap, context));
57 case 'creation':
58 return tfc.tidy(() => creation.executeOp(node, tensorMap, context));
59 case 'dynamic':
60 return dynamic.executeOp(node, tensorMap, context);
61 case 'evaluation':
62 return tfc.tidy(() => evaluation.executeOp(node, tensorMap, context));
63 case 'image':
64 return tfc.tidy(() => image.executeOp(node, tensorMap, context));
65 case 'graph':
66 return tfc.tidy(() => graph.executeOp(node, tensorMap, context));
67 case 'logical':
68 return tfc.tidy(() => logical.executeOp(node, tensorMap, context));
69 case 'matrices':
70 return tfc.tidy(() => matrices.executeOp(node, tensorMap, context));
71 case 'normalization':
72 return tfc.tidy(() => normalization.executeOp(node, tensorMap, context));
73 case 'reduction':
74 return tfc.tidy(() => reduction.executeOp(node, tensorMap, context));
75 case 'slice_join':
76 return tfc.tidy(() => sliceJoin.executeOp(node, tensorMap, context));
77 case 'sparse':
78 return tfc.tidy(() => sparse.executeOp(node, tensorMap, context));
79 case 'spectral':
80 return tfc.tidy(() => spectral.executeOp(node, tensorMap, context));
81 case 'string':
82 return tfc.tidy(() => string.executeOp(node, tensorMap, context));
83 case 'transformation':
84 return tfc.tidy(() => transformation.executeOp(node, tensorMap, context));
85 case 'hash_table':
86 return hashTable.executeOp(node, tensorMap, context, resourceManager);
87 case 'custom':
88 const opMapper = getRegisteredOp(node.op);
89 if (opMapper && opMapper.customExecutor) {
90 return opMapper.customExecutor(new NodeValueImpl(node, tensorMap, context));
91 }
92 else {
93 throw TypeError(`Custom op ${node.op} is not registered.`);
94 }
95 default:
96 throw TypeError(`Unknown op '${node.op}'. File an issue at ` +
97 `https://github.com/tensorflow/tfjs/issues so we can add it` +
98 `, or register a custom execution with tf.registerOp()`);
99 }
100 })(node, tensorMap, context);
101 if (tfc.util.isPromise(value)) {
102 return value.then((data) => [].concat(data));
103 }
104 return [].concat(value);
105}
106//# sourceMappingURL=operation_executor.js.map
\No newline at end of file