1
// Copyright (c) Microsoft Corporation. All rights reserved.
2
// Licensed under the MIT License.
4
import { Graph } from '../../../graph';
5
import { OperatorImplementation, OperatorInitialization } from '../../../operators';
6
import { Tensor } from '../../../tensor';
7
import { ShapeUtil } from '../../../util';
8
import { WebGLInferenceHandler } from '../inference-handler';
10
export const flatten: OperatorImplementation<number> = (
11
inferenceHandler: WebGLInferenceHandler,
15
validateInputs(inputs, axis);
17
const outputDims = ShapeUtil.flattenShape(inputs[0].dims, axis);
18
return [inferenceHandler.reshapeUnpacked(inputs[0], outputDims)];
21
export const parseFlattenAttributes: OperatorInitialization<number> = (node: Graph.Node): number =>
22
node.attributes.getInt('axis', 1); // default axis is 1
24
const validateInputs = (inputs: Tensor[], axis: number): void => {
25
if (!inputs || inputs.length !== 1) {
26
throw new Error('Flatten requires 1 input.');
29
const r = inputs[0].dims.length;
31
throw new Error('scalar tensor is not supported.');
34
if (axis < -r || axis > r) {
35
throw new Error('Invalid axis');
38
// TODO: Support string type
39
if (inputs[0].type === 'string') {
40
throw new Error('string tensor is not supported.');