This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
operator: add Flatten ops for webgl and cpu backends (#93)
* add Flatten ops for webgl and cpu backends * use flattenShape function for both cpu and webgl backends * strictly following onnx specs and checking for scalar tensor
- Loading branch information
Showing
7 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
import {Flatten} from '../../../ops/flatten'; | ||
import {Tensor} from '../../../tensor'; | ||
import {ShapeUtil} from '../../../util'; | ||
import {CpuInferenceHandler} from '../inference-handler'; | ||
|
||
export class CpuFlatten extends Flatten { | ||
run(inferenceHandler: CpuInferenceHandler, inputs: Tensor[]): Tensor[] { | ||
const output = flatten(inputs[0], this.axis); | ||
return [output]; | ||
} | ||
} | ||
|
||
export function flatten(x: Tensor, axis: number): Tensor { | ||
const outputDims = ShapeUtil.flattenShape(x.dims, axis); | ||
const output = new Tensor(outputDims, x.type); | ||
|
||
const X = x.numberData; | ||
const Y = output.numberData; | ||
|
||
Y.set(X); | ||
|
||
return output; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
import {Flatten} from '../../../ops/flatten'; | ||
import {Tensor} from '../../../tensor'; | ||
import {ShapeUtil} from '../../../util'; | ||
import {WebGLInferenceHandler} from '../inference-handler'; | ||
|
||
import {reshape} from './reshape'; | ||
|
||
export class WebGLFlatten extends Flatten { | ||
run(inferenceHandler: WebGLInferenceHandler, inputs: Tensor[]): Tensor[] { | ||
const outputDims = ShapeUtil.flattenShape(inputs[0].dims, this.axis); | ||
|
||
return [reshape(inferenceHandler, inputs[0], outputDims)]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
import {Attribute} from '../attribute'; | ||
import {InferenceHandler} from '../backend'; | ||
import {Operator} from '../operators'; | ||
import {Tensor} from '../tensor'; | ||
|
||
export abstract class Flatten implements Operator { | ||
abstract run(inferenceHandler: InferenceHandler, inputs: Tensor[]): Tensor[]|Promise<Tensor[]>; | ||
|
||
initialize(attributes: Attribute): void { | ||
this.axis = attributes.getInt('axis', 1); // default axis is 1 | ||
} | ||
|
||
checkInputs(inputs: Tensor[]): boolean { | ||
if (!inputs || inputs.length !== 1) { | ||
return false; | ||
} | ||
|
||
if (inputs[0].dims.length === 0) { | ||
return false; // scalar tensor is not supported | ||
} | ||
|
||
if (this.axis < 0 || this.axis > inputs[0].dims.length) { | ||
return false; | ||
} | ||
|
||
return this.checkInputTypes(inputs); | ||
} | ||
|
||
protected checkInputTypes(inputs: Tensor[]): boolean { | ||
// TODO: Support string type | ||
if (inputs[0].type === 'string') { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
protected axis: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters