license import { DataType , KernelFunc , TypedArray , UnaryInputs , util } from '@tensorflow/tfjs-core' ; import { MathBackendCPU } from '../backend_cpu' ; import { assertNotComplex } from '../cpu_util' ; import { SimpleUnaryImpl , SimpleUnaryOperation } from './unary_types' ; param name param op param dtype export function unaryKernelFunc ( name , op , dtype ? ) { return ( { inputs , attrs , backend } ) => { const { x } = inputs as UnaryInputs ; assertNotComplex ( x , name ) ; if ( x . dtype === 'string' || dtype === 'string' ) { throw new Error ( 'unaryKernelFunc does not support string input/output' ) ; } const cpuBackend = backend as MathBackendCPU ; const values = cpuBackend . data . get ( x . dataId ) . values as TypedArray ; const xSize = util . sizeFromShape ( x . shape ) ; const $dtype = dtype || x . dtype ; const newValues = util . getArrayFromDType ( $dtype , xSize ) ; for ( let i = 0 ; i < xSize ; ++ i ) { newValues [ i ] = op ( values [ i ] , attrs ) ; } return cpuBackend . makeTensorInfo ( x . shape , $dtype , newValues ) ; } ; } param name param unaryImpl param dtype export function unaryKernelFuncFromImpl ( name , unaryImpl , dtype ? ) { return ( { inputs , attrs , backend } ) => { const { x } = inputs as UnaryInputs ; assertNotComplex ( x , name ) ; if ( x . dtype === 'string' || dtype === 'string' ) { throw new Error ( 'unaryKernelFunc does not support string input/output' ) ; } const cpuBackend = backend as MathBackendCPU ; const values = cpuBackend . data . get ( x . dataId ) . values as TypedArray ; const $dtype = dtype || x . dtype ; const newValues = unaryImpl ( values , $dtype , attrs ) ; return cpuBackend . makeTensorInfo ( x . shape , $dtype , newValues ) ; } ; }