license import { Any , AnyAttrs , AnyInputs , backend_util , KernelConfig , KernelFunc , TensorInfo , util } from '@tensorflow/tfjs-core' ; import { MathBackendWebGL } from '../backend_webgl' ; import { reduce } from '../kernel_utils/reduce' ; import { reshape } from './Reshape' ; import { transpose } from './Transpose' ; export function any ( args ) { const { inputs , backend , attrs } = args ; const { x } = inputs ; const { axis , keepDims } = attrs ; const xRank = x . shape . length ; const origAxes = util . parseAxisParam ( axis , x . shape ) ; let axes = origAxes ; const permutedAxes = backend_util . getAxesPermutation ( axes , xRank ) ; let permutedX = x ; if ( permutedAxes != null ) { permutedX = transpose ( { inputs : { x } , backend , attrs : { perm : permutedAxes } } ) ; axes = backend_util . getInnerMostAxes ( axes . length , xRank ) ; } backend_util . assertAxesAreInnerMostDims ( 'any' , axes , xRank ) ; const [ outShape , reduceShape ] = backend_util . computeOutAndReduceShapes ( permutedX . shape , axes ) ; const inSize = util . sizeFromShape ( reduceShape ) ; const a2D = reshape ( { inputs : { x : permutedX } , backend , attrs : { shape : [ - 1 , inSize ] } } ) ; const reduced = reduce ( a2D , a2D . dtype , 'any' , backend ) ; let res ; if ( keepDims ) { const newShape = backend_util . expandShapeToKeepDim ( outShape , origAxes ) ; res = reshape ( { inputs : { x : reduced } , backend , attrs : { shape : newShape } } ) ; } else { res = reshape ( { inputs : { x : reduced } , backend , attrs : { shape : outShape } } ) ; } backend . disposeIntermediateTensorInfo ( a2D ) ; backend . disposeIntermediateTensorInfo ( reduced ) ; if ( permutedAxes != null ) { backend . disposeIntermediateTensorInfo ( permutedX ) ; } return res ; } export const anyConfig = { kernelName : Any , backendName : 'webgl' , kernelFunc : any as { } as KernelFunc } ;