license import { buffer , DataType , Rank , TensorBuffer , TypedArray } from '@tensorflow/tfjs-core' ; export function gatherNdImpl < R extends Rank > ( indicesData , paramsBuf , dtype , numSlices , sliceRank , sliceSize , strides , paramsShape , paramsSize ) { const outBuf = buffer ( [ numSlices , sliceSize ] , dtype ) ; for ( let i = 0 ; i < numSlices ; i ++ ) { const index = [ ] ; let flattenIndex = 0 ; for ( let j = 0 ; j < sliceRank ; j ++ ) { const dim = indicesData [ i * sliceRank + j ] ; flattenIndex += dim * strides [ j ] ; index . push ( dim ) ; } if ( flattenIndex < 0 || flattenIndex >= paramsSize / sliceSize ) { throw new Error ( `Invalid indices: ${ index } does not index into ${ paramsShape }` ) ; } for ( let k = 0 ; k < sliceSize ; k ++ ) { outBuf . values [ i * sliceSize + k ] = paramsBuf . get ( ... paramsBuf . indexToLoc ( flattenIndex * sliceSize + k ) ) ; } } return outBuf as TensorBuffer < R > ; }