license import { Tensor , Tensor4D } from '@tensorflow/tfjs-core' ; import * as tfOps from '@tensorflow/tfjs-core/dist/ops/ops_for_converter' ; import { NamedTensorsMap } from '../../data/types' ; import { ExecutionContext } from '../../executor/execution_context' ; import { InternalOpExecutor , Node } from '../types' ; import { getParamValue } from './utils' ; export const executeOp = ( node , tensorMap , context ) : Tensor [ ] => { switch ( node . op ) { case 'Cast' : { return [ tfOps . cast ( getParamValue ( 'x' , node , tensorMap , context ) as Tensor , getParamValue ( 'dtype' , node , tensorMap , context ) as 'int32' | 'float32' | 'bool' ) ] ; } case 'ExpandDims' : { const axis = getParamValue ( 'axis' , node , tensorMap , context ) as number ; return [ tfOps . expandDims ( getParamValue ( 'x' , node , tensorMap , context ) as Tensor , axis ) ] ; } case 'Squeeze' : { const axis = getParamValue ( 'axis' , node , tensorMap , context ) as number [ ] ; return [ tfOps . squeeze ( getParamValue ( 'x' , node , tensorMap , context ) as Tensor , axis ) ] ; } case 'Reshape' : { return [ tfOps . reshape ( getParamValue ( 'x' , node , tensorMap , context ) as Tensor , getParamValue ( 'shape' , node , tensorMap , context ) as number [ ] ) ] ; } case 'MirrorPad' : { return [ tfOps . mirrorPad ( getParamValue ( 'x' , node , tensorMap , context ) as Tensor , getParamValue ( 'padding' , node , tensorMap , context ) as Array < [ number , number ] > , getParamValue ( 'mode' , node , tensorMap , context ) as 'reflect' | 'symmetric' ) ] ; } case 'PadV2' : case 'Pad' : { return [ tfOps . pad ( getParamValue ( 'x' , node , tensorMap , context ) as Tensor , getParamValue ( 'padding' , node , tensorMap , context ) as Array < [ number , number ] > , getParamValue ( 'constantValue' , node , tensorMap , context ) as number ) ] ; } case 'SpaceToBatchND' : { const blockShape = getParamValue ( 'blockShape' , node , tensorMap , context ) as number [ ] ; const paddings = getParamValue ( 'paddings' , node , tensorMap , context ) as number [ ] [ ] ; return [ tfOps . spaceToBatchND ( getParamValue ( 'x' , node , tensorMap , context ) as Tensor , blockShape , paddings ) ] ; } case 'BatchToSpaceND' : { const blockShape = getParamValue ( 'blockShape' , node , tensorMap , context ) as number [ ] ; const crops = getParamValue ( 'crops' , node , tensorMap , context ) as number [ ] [ ] ; return [ tfOps . batchToSpaceND ( getParamValue ( 'x' , node , tensorMap , context ) as Tensor , blockShape , crops ) ] ; } case 'DepthToSpace' : { const blockSize = getParamValue ( 'blockSize' , node , tensorMap , context ) as number ; const dataFormat = ( getParamValue ( 'dataFormat' , node , tensorMap , context ) as string ) . toUpperCase ( ) as 'NHWC' | 'NCHW' ; return [ tfOps . depthToSpace ( getParamValue ( 'x' , node , tensorMap , context ) as Tensor4D , blockSize , dataFormat ) ] ; } case 'BroadcastTo' : { return [ tfOps . broadcastTo ( getParamValue ( 'x' , node , tensorMap , context ) as Tensor , getParamValue ( 'shape' , node , tensorMap , context ) as number [ ] ) ] ; } case 'BroadcastArgs' : { return [ tfOps . broadcastArgs ( getParamValue ( 's0' , node , tensorMap , context ) as Tensor , getParamValue ( 's1' , node , tensorMap , context ) as Tensor ) ] ; } default : throw TypeError ( `Node type ${ node . op } is not implemented` ) ; } } ; export const CATEGORY = 'transformation' ;