license import { KernelConfig , KernelFunc , NumericDataType , PadV2 , PadV2Attrs , PadV2Inputs , TensorInfo , TypedArray , util } from '@tensorflow/tfjs-core' ; import { MathBackendCPU } from '../backend_cpu' ; import { assertNotComplex } from '../cpu_util' ; export function padV2 ( args ) { const { inputs , backend , attrs } = args ; const { x } = inputs ; const { paddings , constantValue } = attrs ; assertNotComplex ( x , 'pad' ) ; const outShape = paddings . map ( ( p , i ) => p [ 0 ] + x . shape [ i ] + p [ 1 ] ) ; const start = paddings . map ( p => p [ 0 ] ) ; const xVals = backend . data . get ( x . dataId ) . values as TypedArray ; const xSize = util . sizeFromShape ( x . shape ) ; 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 ) ; if ( constantValue !== 0 ) { resVals . fill ( constantValue ) ; } for ( let i = 0 ; i < xSize ; i ++ ) { const coords = util . indexToLoc ( i , xRank , xStrides ) ; const outCoords = coords . map ( ( c , i ) => c + start [ i ] ) ; const outIndex = util . locToIndex ( outCoords , resultRank , resultStrides ) ; resVals [ outIndex ] = xVals [ i ] ; } const outId = backend . write ( resVals , outShape , x . dtype ) ; return { dataId : outId , shape : outShape , dtype : x . dtype } ; } export const padV2Config = { kernelName : PadV2 , backendName : 'cpu' , kernelFunc : padV2 as { } as KernelFunc } ;