license import { clone , Tensor , util } from '@tensorflow/tfjs-core' ; import { NamedTensorsMap } from '../../data/types' ; import { ExecutionContext } from '../../executor/execution_context' ; import { ResourceManager } from '../../executor/resource_manager' ; import { Node , ValueType } from '../types' ; export function getParamValue ( paramName , node , tensorMap , context , resourceManager ? ) { const inputParam = node . inputParams [ paramName ] ; if ( inputParam && inputParam . inputIndexStart !== undefined ) { const start = inputParam . inputIndexStart ; const end = inputParam . inputIndexEnd === 0 ? undefined : ( inputParam . inputIndexEnd === undefined ? start + 1 : inputParam . inputIndexEnd ) ; if ( inputParam . type === 'tensor' ) { return getTensor ( node . inputNames [ inputParam . inputIndexStart ] , tensorMap , context , resourceManager ) ; } if ( inputParam . type === 'tensors' ) { const inputs = node . inputNames . slice ( start , end ) ; return inputs . map ( name => getTensor ( name , tensorMap , context , resourceManager ) ) ; } const tensor = getTensor ( node . inputNames . slice ( start ) [ 0 ] , tensorMap , context , resourceManager ) ; const data = tensor . dataSync ( ) ; return inputParam . type === 'number' ? data [ 0 ] : util . toNestedArray ( tensor . shape , data ) ; } const attrParam = node . attrParams [ paramName ] ; return attrParam && attrParam . value ; } param name param tensorsMap param context param resourceManager export function getTensor ( name , tensorsMap , context , resourceManager ? ) { const [ nodeName , index ] = parseNodeName ( name ) ; if ( resourceManager != null ) { const tensor = resourceManager . getHashTableHandleByName ( nodeName ) ; if ( tensor != null ) { return tensor ; } } const contextId = context . currentContextIds . find ( contextId => { return ! ! tensorsMap [ getNodeNameWithContextId ( nodeName , contextId ) ] ; } ) ; return contextId !== undefined ? tensorsMap [ getNodeNameWithContextId ( nodeName , contextId ) ] [ index ] : undefined ; } param name param tensorsMap export function getTensorsForCurrentContenxt ( name , tensorsMap , context ) { return tensorsMap [ getNodeNameWithContextId ( name , context . currentContextId ) ] ; } param inputName export function getNodeNameAndIndex ( inputName , context ? ) { const [ nodeName , index , outputName ] = parseNodeName ( inputName ) ; return [ getNodeNameWithContextId ( nodeName , context && context . currentContextId ) , index , outputName ] ; } function getNodeNameWithContextId ( name , contextId ? ) { return ! ! contextId ? `${ name }-${ contextId }` : name ; } export function parseNodeName ( name ) { const parts = name . split ( ':' ) ; if ( parts . length === 1 ) { return [ name , 0 , undefined ] ; } const nodeName = parts [ 0 ] ; const outputName = parts . length === 3 ? parts [ 1 ] : undefined ; const index = Number ( parts [ parts . length - 1 ] ) ; return [ nodeName , index , outputName ] ; } export function split ( arr , size ) { const res = [ ] ; for ( let i = 0 ; i < arr . length ; i += size ) { res . push ( arr . slice ( i , i + size ) ) ; } return res ; } export function getPadding ( node , tensorMap , context ) { let pad = getParamValue ( 'pad' , node , tensorMap , context ) ; if ( pad === 'explicit' ) { pad = getParamValue ( 'explicitPaddings' , node , tensorMap , context ) ; const explicitPadding = [ [ 0 , 0 ] , [ 0 , 0 ] , [ 0 , 0 ] , [ 0 , 0 ] ] ; for ( let i = 0 ; i < 4 ; i ++ ) { explicitPadding [ i ] [ 0 ] = ( pad as number [ ] ) [ i * 2 ] ; explicitPadding [ i ] [ 1 ] = ( pad as number [ ] ) [ i * 2 + 1 ] ; } return explicitPadding ; } return pad ; } param tensor export function cloneTensor ( tensor ) { return tensor . kept ? tensor : clone ( tensor ) ; }