license import { KernelConfig , KernelFunc , MirrorPad , MirrorPadAttrs , MirrorPadInputs , NumericDataType , TensorInfo , TypedArray , util } from '@tensorflow/tfjs-core' ; import { MathBackendCPU } from '../backend_cpu' ; import { assertNotComplex } from '../cpu_util' ; export function mirrorPad ( args ) { const { inputs , backend , attrs } = args ; const { x } = inputs ; const { paddings , mode } = attrs ; assertNotComplex ( x , 'mirrorPad' ) ; const outShape = paddings . map ( ( p , i ) => p [ 0 ] + x . shape [ i ] + p [ 1 ] ) ; const start = paddings . map ( p => p [ 0 ] ) ; const end = paddings . map ( ( p , i ) => p [ 0 ] + x . shape [ i ] ) ; const offset = mode === 'reflect' ? 0 : 1 ; const xVals = backend . data . get ( x . dataId ) . values as TypedArray ; const xRank = x . shape . length ; const xStrides = util . computeStrides ( x . shape ) ; const resultSize = util . sizeFromShape ( outShape ) ; const resultRank = outShape . length ; const resultStrides = util . computeStrides ( outShape ) ; const resVals = util . getTypedArrayFromDType ( x . dtype as NumericDataType , resultSize ) ; for ( let i = 0 ; i < resultSize ; i ++ ) { let coords = util . indexToLoc ( i , resultRank , resultStrides ) ; for ( let i = 0 ; i < resultRank ; i ++ ) { if ( coords [ i ] < start [ i ] ) { coords [ i ] = start [ i ] * 2 - coords [ i ] - offset ; } else if ( coords [ i ] >= end [ i ] ) { coords [ i ] = ( end [ i ] - 1 ) * 2 - coords [ i ] + offset ; } } coords = coords . map ( ( c , i ) => c - start [ i ] ) ; const inIndex = util . locToIndex ( coords , xRank , xStrides ) ; resVals [ i ] = xVals [ inIndex ] ; } const outId = backend . write ( resVals , outShape , x . dtype ) ; return { dataId : outId , shape : outShape , dtype : x . dtype } ; } export const mirrorPadConfig = { kernelName : MirrorPad , backendName : 'cpu' , kernelFunc : mirrorPad as { } as KernelFunc } ;