Input: Update the opName for the variable with the given vertex id [CODESPLIT] public void updateVariableName ( String varName , String withName ) { SDVariable oldVarNameRef = getVariable ( varName ) ; variableMap . remove ( oldVarNameRef . getVarName ( ) ) ; val oldVarName = varName ; oldVarNameRef . setVarName ( withName ) ; variableMap . put ( withName , oldVarNameRef ) ; for ( val reverseValues : outgoingArgsReverse . entrySet ( ) ) { for ( int i = 0 ; i < reverseValues . getValue ( ) . length ; i ++ ) { if ( reverseValues . getValue ( ) [ i ] . equals ( oldVarName ) ) { reverseValues . getValue ( ) [ i ] = withName ; } } } for ( val reverseValues : incomingArgsReverse . entrySet ( ) ) { for ( int i = 0 ; i < reverseValues . getValue ( ) . length ; i ++ ) { if ( reverseValues . getValue ( ) [ i ] . equals ( oldVarName ) ) { reverseValues . getValue ( ) [ i ] = withName ; } } } if ( variableNameToArr . containsKey ( oldVarName ) ) { val arr = variableNameToArr . remove ( oldVarName ) ; variableNameToArr . put ( withName , arr ) ; } if ( variableNameToShape . containsKey ( oldVarName ) ) { val shape = variableNameToShape . remove ( oldVarName ) ; variableNameToShape . put ( withName , shape ) ; } if ( gradients . containsKey ( oldVarName ) ) { val grad = gradients . remove ( oldVarName ) ; gradients . put ( withName , grad ) ; } if ( forwardVarForGrad . containsKey ( oldVarName ) ) { val forwardGrad = forwardVarForGrad . remove ( oldVarName ) ; forwardVarForGrad . put ( withName , forwardGrad ) ; } if ( placeHolderMap . containsKey ( oldVarName ) ) { val placeholders = placeHolderMap . remove ( oldVarName ) ; placeHolderMap . put ( withName , placeholders ) ; } if ( functionsArgsFor . containsKey ( oldVarName ) ) { val funcs = functionsArgsFor . remove ( oldVarName ) ; for ( val func : funcs ) { if ( func instanceof BaseOp ) { BaseOp baseOp = ( BaseOp ) func ; if ( baseOp . getXVertexId ( ) != null && baseOp . getXVertexId ( ) . equals ( oldVarName ) ) { baseOp . setXVertexId ( withName ) ; } if ( baseOp . getYVertexId ( ) != null && baseOp . getYVertexId ( ) . equals ( oldVarName ) ) { baseOp . setYVertexId ( withName ) ; } if ( baseOp . getZVertexId ( ) != null && baseOp . getZVertexId ( ) . equals ( oldVarName ) ) { baseOp . setZVertexId ( withName ) ; } } } functionsArgsFor . put ( withName , funcs ) ; } if ( functionOutputFor . containsKey ( oldVarName ) ) { val funcs = functionOutputFor . remove ( oldVarName ) ; for ( val func : funcs ) { if ( func instanceof BaseOp ) { BaseOp baseOp = ( BaseOp ) func ; if ( baseOp . getXVertexId ( ) != null && baseOp . getXVertexId ( ) . equals ( oldVarName ) ) { baseOp . setXVertexId ( withName ) ; } if ( baseOp . getYVertexId ( ) != null && baseOp . getYVertexId ( ) . equals ( oldVarName ) ) { baseOp . setYVertexId ( withName ) ; } if ( baseOp . getZVertexId ( ) != null && baseOp . getZVertexId ( ) . equals ( oldVarName ) ) { baseOp . setZVertexId ( withName ) ; } } } functionOutputFor . put ( withName , funcs ) ; } variableMap . remove ( oldVarName ) ; }


Input: Get the function by the { @link DifferentialFunction#getOwnName () } [CODESPLIT] public DifferentialFunction getFunctionById ( String id ) { if ( ! functionInstancesById . containsKey ( id ) ) { throw new ND4JIllegalStateException ( "No function with id " + id + " found!" ) ; } return functionInstancesById . get ( id ) ; }


Input: Put the function for id [CODESPLIT] public void putFunctionForId ( String id , DifferentialFunction function ) { if ( functionInstancesById . containsKey ( id ) ) { throw new ND4JIllegalStateException ( "Function by id already exists!" ) ; } else if ( function instanceof SDVariable ) { throw new ND4JIllegalStateException ( "Function must not be a variable!" ) ; } functionInstancesById . put ( id , function ) ; }


Input: Returns the inputs for the given function [CODESPLIT] public String [ ] getInputsForFunction ( DifferentialFunction function ) { if ( ! incomingArgsReverse . containsKey ( function . getOwnName ( ) ) ) throw new ND4JIllegalStateException ( "Illegal function instance id found " + function . getOwnName ( ) ) ; return incomingArgsReverse . get ( function . getOwnName ( ) ) ; }


Input: Update the ndarray for the given vertex id . [CODESPLIT] public void updateArrayForVarName ( String varName , INDArray arr ) { if ( ! variableNameToArr . containsKey ( varName ) ) { throw new ND4JIllegalStateException ( "Array for " + varName + " does not exist. Please use putArrayForVertexId instead." ) ; } variableNameToArr . put ( varName , arr ) ; reverseArrayLookup . put ( arr , getVariable ( varName ) ) ; }


Input: Adds an ndarray for a given vertex id . Use { @link #updateArrayForVarName ( String INDArray ) } if the array already exists . [CODESPLIT] public void putArrayForVarName ( String varName , INDArray arr ) { if ( varName == null ) throw new ND4JIllegalStateException ( "No null names allowed!" ) ; if ( variableNameToArr . containsKey ( varName ) ) { throw new ND4JIllegalStateException ( "Array for " + varName + " already exists!" ) ; } variableNameToArr . put ( varName , arr ) ; }


Input: Get the shape for the given vertex id . Note that if an array is defined it will use that shape instead . <p > A shape * and * an array should not be defined at the same time . This wastes memory . The internal map used for tracking shapes for particular vertex ids should also delete redundant shapes stored to avoid redundant sources of information . [CODESPLIT] public long [ ] getShapeForVarName ( String varName ) { if ( variableNameToArr . containsKey ( varName ) ) { return variableNameToArr . get ( varName ) . shape ( ) ; } return variableNameToShape . get ( varName ) ; }


Input: Update a vertex id with the given shape . Note that you should use { @link #putShapeForVarName ( String int [] ) } if you want to add a new shape . Update is meant to be an in place replacement of the shape for the vertex id * only * . [CODESPLIT] public void updateShapeForVarName ( String varName , long [ ] shape ) { if ( shape == null ) { throw new ND4JIllegalStateException ( "Null shapes not allowed!" ) ; } if ( variableNameToArr . containsKey ( varName ) && ! Arrays . equals ( variableNameToArr . get ( varName ) . shape ( ) , shape ) ) { throw new ND4JIllegalStateException ( "Already found an existing array!" ) ; } for ( int i = 0 ; i < shape . length ; i ++ ) { if ( shape [ i ] < 1 ) { addAsPlaceHolder ( varName ) ; placeHolderOriginalShapes . put ( varName , shape ) ; return ; } } variableNameToShape . put ( varName , shape ) ; }


Input: Associate a vertex id with the given shape . [CODESPLIT] public void putShapeForVarName ( String varName , long [ ] shape ) { if ( shape == null ) { throw new ND4JIllegalStateException ( "Shape must not be null!" ) ; } if ( variableNameToShape . containsKey ( varName ) ) { throw new ND4JIllegalStateException ( "Shape for " + varName + " already exists!" ) ; } for ( int i = 0 ; i < shape . length ; i ++ ) { if ( shape [ i ] < 1 ) { addAsPlaceHolder ( varName ) ; placeHolderOriginalShapes . put ( varName , shape ) ; return ; } } variableNameToShape . put ( varName , shape ) ; }


Input: Associate the array with the given variable . [CODESPLIT] public void associateArrayWithVariable ( INDArray arr , SDVariable variable ) { if ( variable == null ) { throw new ND4JIllegalArgumentException ( "Variable must not be null!" ) ; } if ( arr == null ) { throw new ND4JIllegalArgumentException ( "Array must not be null" ) ; } reverseArrayLookup . put ( arr , variable ) ; variableNameToArr . put ( variable . getVarName ( ) , arr ) ; if ( ! shapeAlreadyExistsForVarName ( variable . getVarName ( ) ) ) putShapeForVarName ( variable . getVarName ( ) , arr . shape ( ) ) ; else { updateShapeForVarName ( variable . getVarName ( ) , arr . shape ( ) ) ; } }


Input: Get the property for a given function [CODESPLIT] public < T > T getPropertyForFunction ( DifferentialFunction functionInstance , String propertyName ) { if ( ! propertiesForFunction . containsKey ( functionInstance . getOwnName ( ) ) ) { return null ; } else { val map = propertiesForFunction . get ( functionInstance . getOwnName ( ) ) ; return ( T ) map . get ( propertyName ) ; } }


Input: Add a property for the given function [CODESPLIT] public void addPropertyForFunction ( DifferentialFunction functionFor , String propertyName , INDArray property ) { addPropertyForFunction ( functionFor , propertyName , ( Object ) property ) ; }


Input: Adds outgoing arguments to the graph . Also checks for input arguments and updates the graph adding an appropriate edge when the full graph is declared . [CODESPLIT] public void addOutgoingFor ( String [ ] varNames , DifferentialFunction function ) { if ( function . getOwnName ( ) == null ) throw new ND4JIllegalStateException ( "Instance id can not be null. Function not initialized properly" ) ; if ( outgoingArgsReverse . containsKey ( function . getOwnName ( ) ) ) { throw new ND4JIllegalStateException ( "Outgoing arguments already declared for " + function ) ; } if ( varNames == null ) throw new ND4JIllegalStateException ( "Var names can not be null!" ) ; for ( int i = 0 ; i < varNames . length ; i ++ ) { if ( varNames [ i ] == null ) throw new ND4JIllegalStateException ( "Variable name elements can not be null!" ) ; } outgoingArgsReverse . put ( function . getOwnName ( ) , varNames ) ; outgoingArgs . put ( varNames , function ) ; for ( val resultName : varNames ) { List < DifferentialFunction > funcs = functionOutputFor . get ( resultName ) ; if ( funcs == null ) { funcs = new ArrayList <> ( ) ; functionOutputFor . put ( resultName , funcs ) ; } funcs . add ( function ) ; } }


Input: Adds incoming args to the graph [CODESPLIT] public void addArgsFor ( String [ ] variables , DifferentialFunction function ) { if ( function . getOwnName ( ) == null ) throw new ND4JIllegalStateException ( "Instance id can not be null. Function not initialized properly" ) ; //double check if function contains placeholder args for ( val varName : variables ) { if ( isPlaceHolder ( varName ) ) { placeHolderFunctions . add ( function . getOwnName ( ) ) ; } } incomingArgs . put ( variables , function ) ; incomingArgsReverse . put ( function . getOwnName ( ) , variables ) ; for ( val variableName : variables ) { List < DifferentialFunction > funcs = functionsArgsFor . get ( variableName ) ; if ( funcs == null ) { funcs = new ArrayList <> ( ) ; functionsArgsFor . put ( variableName , funcs ) ; } funcs . add ( function ) ; } }


Input: Returns true if this function already has defined arguments [CODESPLIT] public boolean hasArgs ( DifferentialFunction function ) { val vertexIdArgs = incomingArgsReverse . get ( function . getOwnName ( ) ) ; if ( vertexIdArgs != null ) { val args = incomingArgs . get ( vertexIdArgs ) ; if ( args != null ) return true ; } return false ; }


Input: Evaluate the given inputs based on the current graph [CODESPLIT] public INDArray [ ] eval ( Map < String , INDArray > inputs ) { SameDiff execPipeline = dup ( ) ; List < DifferentialFunction > opExecAction = execPipeline . exec ( ) . getRight ( ) ; if ( opExecAction . isEmpty ( ) ) throw new IllegalStateException ( "No ops found to execute." ) ; INDArray [ ] ret = new INDArray [ opExecAction . size ( ) ] ; for ( int i = 0 ; i < ret . length ; i ++ ) { val varName = opExecAction . get ( i ) . outputVariables ( ) [ 0 ] . getVarName ( ) ; ret [ i ] = execPipeline . getArrForVarName ( varName ) ; } return ret ; }


Input: Variable initialization with 1 . 0 [CODESPLIT] public SDVariable one ( String name , int [ ] shape ) { return var ( name , ArrayUtil . toLongArray ( shape ) , new ConstantInitScheme ( ' ' , 1.0 ) ) ; }


Input: Return a variable of all 1s with the same shape as the input [CODESPLIT] public SDVariable onesLike ( String name , SDVariable input ) { return f ( ) . onesLike ( name , input ) ; }


Input: Return a variable of all 0s with the same shape as the input [CODESPLIT] public SDVariable zerosLike ( String name , SDVariable input ) { return f ( ) . zerosLike ( name , input ) ; }


Input: Variable initialization with a specified { @link WeightInitScheme } [CODESPLIT] public SDVariable var ( String name , long [ ] shape , WeightInitScheme weightInitScheme ) { if ( variableMap . containsKey ( name ) && variableMap . get ( name ) . getArr ( ) != null ) return variableMap . get ( name ) ; if ( name == null || name . length ( ) < 1 ) throw new IllegalArgumentException ( "Name for variable must be defined" ) ; if ( workspace == null ) initWorkspace ( ) ; SDVariable ret = SDVariable . builder ( ) . sameDiff ( this ) . shape ( shape ) . weightInitScheme ( weightInitScheme ) . varName ( name ) . build ( ) ; addVariable ( ret ) ; variableMap . put ( name , ret ) ; return ret ; }


Input:
Initialize a { @link SDVariable } reference tying this variable to this samediff instance . <p > { @link NDArraySupplierInitScheme } is used to ensure that if the array is allocated anywhere and { @link SameDiff } instance to exist as a copy of the variable . [CODESPLIT] public SDVariable var ( final SDVariable arr ) { if ( variableMap . containsKey ( arr . getVarName ( ) ) && variableMap . get ( arr . getVarName ( ) ) . getArr ( ) != null ) return variableMap . get ( arr . getVarName ( ) ) ; if ( arr . getVarName ( ) == null || arr . getVarName ( ) . length ( ) < 1 ) throw new IllegalArgumentException ( "Name for variable must be defined" ) ; if ( arr == null ) throw new IllegalArgumentException ( "Array for " + arr . getVarName ( ) + " must not be null" ) ; if ( workspace == null ) initWorkspace ( ) ; final SDVariable ret = SDVariable . builder ( ) . sameDiff ( this ) . shape ( arr . getShape ( ) ) . varName ( arr . getVarName ( ) ) . weightInitScheme ( new NDArraySupplierInitScheme ( new NDArraySupplierInitScheme . NDArraySupplier ( ) { @ Override public INDArray getArr ( ) { /**
                         * Pre allocate the array if it doesn't already exist.
                         * The reason we do this is to avoid race conditions with
                         * {@link #allocate()}
                         */ if ( arr . getArr ( ) == null ) { INDArray retArr = arr . getWeightInitScheme ( ) . create ( arr . getShape ( ) ) ; associateArrayWithVariable ( retArr , arr ) ; } return arr . getArr ( ) ; } } ) ) . build ( ) ; variableMap . put ( arr . getVarName ( ) , ret ) ; return ret ; }


Input:
Remove an argument for a function . Note that if this function does not contain the argument it will just be a no op . [CODESPLIT] public void removeArgFromFunction ( String varName , DifferentialFunction function ) { val args = function . args ( ) ; for ( int i = 0 ; i < args . length ; i ++ ) { if ( args [ i ] . getVarName ( ) . equals ( varName ) ) { /**
                 * Since we are removing the variable reference
                 * from the arguments we need to  update both
                 * the reverse and forward arguments.
                 */ val reverseArgs = incomingArgsReverse . get ( function . getOwnName ( ) ) ; incomingArgs . remove ( reverseArgs ) ; incomingArgsReverse . remove ( function . getOwnName ( ) ) ; val newArgs = new ArrayList < String > ( args . length - 1 ) ; for ( int arg = 0 ; arg < args . length ; arg ++ ) { if ( ! reverseArgs [ arg ] . equals ( varName ) ) { newArgs . add ( reverseArgs [ arg ] ) ; } } val newArgsArr = newArgs . toArray ( new String [ newArgs . size ( ) ] ) ; incomingArgs . put ( newArgsArr , function ) ; incomingArgsReverse . put ( function . getOwnName ( ) , newArgsArr ) ; //no further need to scan break ; } } }


Input: Assign a vertex id to a gradient [CODESPLIT] public void setGradientForVariableName ( String variableName , SDVariable variable ) { if ( variable == null ) { throw new ND4JIllegalStateException ( "Unable to set null gradient for variable name " + variableName ) ; } gradients . put ( variableName , variable ) ; }


Input: Average pooling 2d operation . [CODESPLIT] public SDVariable avgPooling2d ( SDVariable [ ] inputs , Pooling2DConfig pooling2DConfig ) { return avgPooling2d ( null , inputs , pooling2DConfig ) ; }


Input: Average pooling 2d operation . [CODESPLIT] public SDVariable avgPooling2d ( String name , SDVariable [ ] inputs , Pooling2DConfig pooling2DConfig ) { SDVariable ret = f ( ) . avgPooling2d ( inputs , pooling2DConfig ) ; return updateVariableNameAndReference ( ret , name ) ; }


Input: Max pooling 2d operation . [CODESPLIT] public SDVariable maxPooling2d ( SDVariable [ ] inputs , Pooling2DConfig pooling2DConfig ) { return maxPooling2d ( null , inputs , pooling2DConfig ) ; }


Input: Average pooling 3d operation . [CODESPLIT] public SDVariable avgPooling3d ( SDVariable [ ] inputs , Pooling3DConfig pooling3DConfig ) { return avgPooling3d ( null , inputs , pooling3DConfig ) ; }


Input: Max pooling 3d operation . [CODESPLIT] public SDVariable maxPooling3d ( SDVariable [ ] inputs , Pooling3DConfig pooling3DConfig ) { return maxPooling3d ( null , inputs , pooling3DConfig ) ; }


Input: Max pooling 3d operation . [CODESPLIT] public SDVariable maxPooling3d ( String name , SDVariable [ ] inputs , Pooling3DConfig pooling3DConfig ) { SDVariable ret = f ( ) . maxPooling3d ( inputs , pooling3DConfig ) ; return updateVariableNameAndReference ( ret , name ) ; }


Input: Conv1d operation . [CODESPLIT] public SDVariable conv1d ( SDVariable [ ] inputs , Conv1DConfig conv1DConfig ) { return conv1d ( null , inputs , conv1DConfig ) ; }


Input: Conv1d operation . [CODESPLIT] public SDVariable conv1d ( String name , SDVariable [ ] inputs , Conv1DConfig conv1DConfig ) { SDVariable ret = f ( ) . conv1d ( inputs , conv1DConfig ) ; return updateVariableNameAndReference ( ret , name ) ; }


Input: Local response normalization operation . [CODESPLIT] public SDVariable localResponseNormalization ( SDVariable inputs , LocalResponseNormalizationConfig lrnConfig ) { return localResponseNormalization ( null , inputs , lrnConfig ) ; }


Input: Local response normalization operation . [CODESPLIT] public SDVariable localResponseNormalization ( String name , SDVariable inputs , LocalResponseNormalizationConfig lrnConfig ) { SDVariable ret = f ( ) . localResponseNormalization ( inputs , lrnConfig ) ; return updateVariableNameAndReference ( ret , name ) ; }


Input: Conv2d operation . [CODESPLIT] public SDVariable conv2d ( SDVariable [ ] inputs , Conv2DConfig conv2DConfig ) { return conv2d ( null , inputs , conv2DConfig ) ; }


Input: Depth - wise Conv2d operation . [CODESPLIT] public SDVariable depthWiseConv2d ( SDVariable [ ] inputs , Conv2DConfig depthConv2DConfig ) { return depthWiseConv2d ( null , inputs , depthConv2DConfig ) ; }


Input: Depth - wise Conv2d operation . [CODESPLIT] public SDVariable depthWiseConv2d ( String name , SDVariable [ ] inputs , Conv2DConfig depthConv2DConfig ) { SDVariable ret = f ( ) . depthWiseConv2d ( inputs , depthConv2DConfig ) ; return updateVariableNameAndReference ( ret , name ) ; }


Input: Separable Conv2d operation . [CODESPLIT] public SDVariable sconv2d ( SDVariable [ ] inputs , Conv2DConfig conv2DConfig ) { return sconv2d ( null , inputs , conv2DConfig ) ; }


Input: Separable Conv2d operation . [CODESPLIT] public SDVariable sconv2d ( String name , SDVariable [ ] inputs , Conv2DConfig conv2DConfig ) { SDVariable ret = f ( ) . sconv2d ( inputs , conv2DConfig ) ; return updateVariableNameAndReference ( ret , name ) ; }


Input: Deconv2d operation . [CODESPLIT] public SDVariable deconv2d ( SDVariable [ ] inputs , DeConv2DConfig deconv2DConfig ) { return deconv2d ( null , inputs , deconv2DConfig ) ; }


Input: Deconv2d operation . [CODESPLIT] public SDVariable deconv2d ( String name , SDVariable [ ] inputs , DeConv2DConfig deconv2DConfig ) { SDVariable ret = f ( ) . deconv2d ( inputs , deconv2DConfig ) ; return updateVariableNameAndReference ( ret , name ) ; }


Input: Conv3d operation . [CODESPLIT] public SDVariable conv3d ( SDVariable [ ] inputs , Conv3DConfig conv3DConfig ) { return conv3d ( null , inputs , conv3DConfig ) ; }


Input: Conv3d operation . [CODESPLIT] public SDVariable conv3d ( String name , SDVariable [ ] inputs , Conv3DConfig conv3DConfig ) { SDVariable ret = f ( ) . conv3d ( inputs , conv3DConfig ) ; return updateVariableNameAndReference ( ret , name ) ; }


Input: Batch norm operation . [CODESPLIT] public SDVariable batchNorm ( SDVariable input , SDVariable mean , SDVariable variance , SDVariable gamma , SDVariable beta , boolean applyGamma , boolean applyBeta , double epsilon ) { return batchNorm ( null , input , mean , variance , gamma , beta , applyGamma , applyBeta , epsilon ) ; }


Input: Batch norm operation . [CODESPLIT] public SDVariable batchNorm ( String name , SDVariable input , SDVariable mean , SDVariable variance , SDVariable gamma , SDVariable beta , boolean applyGamma , boolean applyBeta , double epsilon ) { SDVariable res = f ( ) . batchNorm ( input , mean , variance , gamma , beta , applyGamma , applyBeta , epsilon ) ; return updateVariableNameAndReference ( res , name ) ; }


Input: LSTM unit [CODESPLIT] public SDVariable lstm ( String baseName , LSTMCellConfiguration configuration ) { return new LSTMCell ( this , configuration ) . outputVariables ( baseName ) [ 0 ] ; }


Input: The gru cell [CODESPLIT] public SDVariable gru ( String baseName , GRUCellConfiguration configuration ) { return new GRUCell ( this , configuration ) . outputVariables ( baseName ) [ 0 ] ; }


Input: Generate the variables based on the given input op and return the output variable names . [CODESPLIT] public SDVariable [ ] generateOutputVariableForOp ( DifferentialFunction function , String baseName ) { //xyz ops only have 1 output //if there is already a base name defined, use that if ( baseName == null || baseName . isEmpty ( ) && getBaseNameForFunction ( function ) != null ) baseName = getBaseNameForFunction ( function ) ; if ( baseName == null ) baseName = function . opName ( ) ; val outputShape = function . calculateOutputShape ( ) ; if ( outputShape == null || outputShape . isEmpty ( ) ) { if ( function instanceof CustomOp ) { CustomOp customOp = ( CustomOp ) function ; val descriptor = customOp . getDescriptor ( ) ; //can't guess number of outputs, variable if ( descriptor == null || descriptor . getNumOutputs ( ) <= 0 ) { throw new ND4JIllegalStateException ( "No output variables found!" ) ; } else { char ordering = ' ' ; if ( function . args ( ) [ 0 ] . getArr ( ) != null ) { ordering = function . args ( ) [ 0 ] . getArr ( ) . ordering ( ) ; } SDVariable [ ] ret = new SDVariable [ descriptor . getNumOutputs ( ) ] ; //dynamic shapes for ( int i = 0 ; i < ret . length ; i ++ ) { SDVariable checkGet = getVariable ( baseName ) ; if ( checkGet == null ) { checkGet = var ( generateNewVarName ( baseName , i ) , null , new ZeroInitScheme ( ordering ) ) ; } else if ( i > 0 && ! importedVarName . contains ( baseName ) ) { //need to find a new name String newName = generateNewVarName ( baseName , i ) ; checkGet = getVariable ( newName ) ; } if ( checkGet == null ) { String newName = generateNewVarName ( baseName , i ) ; checkGet = var ( newName , null , new ZeroInitScheme ( ordering ) ) ; } ret [ i ] = checkGet ; } return ret ; } } //this is for unresolved shapes, we know xyz is always 1 output else if ( function instanceof BaseOp && outputShape . isEmpty ( ) ) { SDVariable [ ] ret = new SDVariable [ 1 ] ; SDVariable checkGet = getVariable ( baseName ) ; char ordering = ' ' ; if ( function . args ( ) [ 0 ] . getArr ( ) != null ) { ordering = function . args ( ) [ 0 ] . getArr ( ) . ordering ( ) ; } if ( checkGet == null ) { checkGet = var ( baseName , null , new ZeroInitScheme ( ordering ) ) ; } else if ( ! importedVarName . contains ( baseName ) ) { //need to find a new name String newName = generateNewVarName ( baseName , 0 ) ; checkGet = var ( newName , null , new ZeroInitScheme ( ordering ) ) ; } if ( checkGet == null ) { checkGet = var ( baseName , null , new ZeroInitScheme ( ordering ) ) ; } ret [ 0 ] = checkGet ; return ret ; } } char ordering = ' ' ; if ( function . args ( ) [ 0 ] . getArr ( ) != null ) { ordering = function . args ( ) [ 0 ] . getArr ( ) . ordering ( ) ; } SDVariable [ ] ret = new SDVariable [ outputShape . size ( ) ] ; // ownName/baseName will be used to get variables names val ownName = function . getOwnName ( ) ; val rootName = baseName ; for ( int i = 0 ; i < ret . length ; i ++ ) { val shape = outputShape . get ( i ) ; // it should be: rootName:index. i.e.: split:1, split:2, split:3, split:4 etc baseName = rootName + ( i > 0 ? ":" + i : "" ) ; SDVariable checkGet = getVariable ( baseName ) ; if ( checkGet == null ) { // obviously - there's no such var, just add it checkGet = var ( baseName , shape , new ZeroInitScheme ( ordering ) ) ; } else if ( shape != null && ! shapeAlreadyExistsForVarName ( checkGet . getVarName ( ) ) ) { // var exists, let's update its shape putShapeForVarName ( checkGet . getVarName ( ) , shape ) ; } else if ( shape != null && shapeAlreadyExistsForVarName ( checkGet . getVarName ( ) ) ) { // no-op. // TODO: maybe we should check shapes equality here? // it's either var that already exist, or something bad happening } else if ( ! importedVarName . contains ( baseName ) ) { // FIXME: dead end.  it's impossible to get here with null as shape //need to find a new name int count = 1 ; String name = baseName + "_" + count + ( i > 0 ? ":" + i : "" ) ; while ( getVariable ( name ) != null ) { count ++ ; name = baseName + "_" + count + ( i > 0 ? ":" + i : "" ) ; } if ( getVariable ( name ) != null ) { throw new ND4JIllegalStateException ( "Converged on already generated variable!" ) ; } checkGet = var ( name , shape , new ZeroInitScheme ( ordering ) ) ; } if ( checkGet == null ) { checkGet = var ( baseName + ( i > 0 ? ":" + i : "" ) , shape , new ZeroInitScheme ( ordering ) ) ; } ret [ i ] = checkGet ; } return ret ; }


Input: u [CODESPLIT] public INDArray execAndEndResult ( List < DifferentialFunction > ops ) { List < DifferentialFunction > exec = exec ( ops ) ; Op op = ( Op ) exec . get ( exec . size ( ) - 1 ) ; return op . z ( ) ; }


Input: Executes the list of operations . This exec method is for only invoking operations rather than creating them [CODESPLIT] public List < DifferentialFunction > exec ( List < DifferentialFunction > ops ) { for ( int i = 0 ; i < ops . size ( ) ; i ++ ) { Op op = ( Op ) ops . get ( i ) ; Nd4j . getExecutioner ( ) . exec ( op ) ; } return ops ; }


Input: Creates a while statement [CODESPLIT] public While whileStatement ( SameDiffConditional sameDiffConditional , SameDiffFunctionDefinition conditionBody , SameDiff . SameDiffFunctionDefinition loopBody , SDVariable [ ] inputVars ) { return While . builder ( ) . inputVars ( inputVars ) . condition ( conditionBody ) . predicate ( sameDiffConditional ) . trueBody ( loopBody ) . parent ( this ) . blockName ( "while-" + UUID . randomUUID ( ) . toString ( ) ) . build ( ) ; }


Input: Exec a given function [CODESPLIT] public Pair < Map < SDVariable , DifferentialFunction > , List < DifferentialFunction > > exec ( String functionName ) { if ( debugMode ) { return sameDiffFunctionInstances . get ( functionName ) . enableDebugMode ( ) . exec ( ) ; } else return sameDiffFunctionInstances . get ( functionName ) . exec ( ) ; }


Input: Exec the given function given the ops [CODESPLIT] public List < DifferentialFunction > exec ( String functionName , List < DifferentialFunction > cachedOps ) { return sameDiffFunctionInstances . get ( functionName ) . exec ( cachedOps ) ; }


Input: Builds a backwards graph and executes the operations on that graph . [CODESPLIT] public Pair < Map < SDVariable , DifferentialFunction > , List < DifferentialFunction > > execBackwards ( ) { final SameDiff outer = this ; if ( getFunction ( "grad" ) == null ) defineFunction ( "grad" , new SameDiffFunctionDefinition ( ) { @ Override public SDVariable [ ] define ( SameDiff sameDiff , Map < String , INDArray > inputs , SDVariable [ ] variableInputs ) { //propagate graph to this samediff instance //which will also contain the backward if ( SameDiff . this . debugMode ) { sameDiff . enableDebugMode ( ) ; } outer . invokeGraphOn ( sameDiff ) ; List < DifferentialFunction > allFunctions = new ArrayList <> ( sameDiff . functionInstancesById . values ( ) ) ; if ( allFunctions . isEmpty ( ) ) { throw new ND4JIllegalStateException ( "No ops found!" ) ; } for ( val func : allFunctions ) { if ( func instanceof SDVariable ) { continue ; } val args = func . args ( ) ; for ( val arg : args ) arg . setSameDiff ( sameDiff ) ; val outputs = func . outputVariables ( ) ; for ( val output : outputs ) output . setSameDiff ( sameDiff ) ; func . setSameDiff ( sameDiff ) ; } val initialOuts = allFunctions . get ( allFunctions . size ( ) - 1 ) . outputVariables ( ) ; val firstBackward = initialOuts [ 0 ] ; //start with scalar backprop SDVariable initialGrad = sameDiff . var ( "one-var" , Nd4j . scalar ( 1.0 ) ) ; sameDiff . forwardVarForGrad . put ( firstBackward . getVarName ( ) , initialGrad ) ; sameDiff . gradients . put ( firstBackward . getVarName ( ) , initialGrad ) ; SDVariable gradientBackwardsMarker = sameDiff . gradientBackwardsMarker ( firstBackward ) ; //reinitialize list with all declared variables allFunctions = new ArrayList < DifferentialFunction > ( sameDiff . functionInstancesById . values ( ) ) ; Collections . reverse ( allFunctions ) ; for ( DifferentialFunction action : allFunctions ) { if ( action instanceof GradientBackwardsMarker ) { log . warn ( "Action op state is null for " + action . opName ( ) ) ; continue ; } DifferentialFunction currFunction = action ; Preconditions . checkState ( currFunction . getSameDiff ( ) == sameDiff , "Wrong samediff instance found!" ) ; //Preconditions.checkNotNull("Gradient for " + currFunction.opName() + " was null ! " + sameDiff.getVariableForVertexId(currFunction.getVertexId()).getGradient()); val args = currFunction . outputVariables ( ) ; for ( val arg : args ) { if ( arg . getSameDiff ( ) != sameDiff ) { arg . setSameDiff ( sameDiff ) ; } } List < SDVariable > grads = new ArrayList <> ( ) ; for ( val varToGrad : args ) { val grad = varToGrad . gradient ( ) ; if ( grad == null ) throw new ND4JIllegalStateException ( "No gradient found for " + varToGrad . getVarName ( ) ) ; grads . add ( grad ) ; } List < SDVariable > currFnGrads = currFunction . diff ( grads ) ; } if ( sameDiff . isDebugMode ( ) ) { //ensure all gradients are present for all variables for ( SDVariable sdVariable : variables ( ) ) { sdVariable . gradient ( ) ; } } return new SDVariable [ ] { sameDiff . var ( "grad" , new int [ ] { 1 , 1 } ) } ; } } ) ; Pair < Map < SDVariable , DifferentialFunction > , List < DifferentialFunction > > forward = exec ( "grad" ) ; SameDiff grad = getFunction ( "grad" ) ; if ( grad . isDebugMode ( ) ) { //ensure all gradients are present for all variables for ( SDVariable sdVariable : grad . variables ( ) ) { sdVariable . gradient ( ) ; } } return forward ; }


Input: Exec a backwards operation and return the end result [CODESPLIT] public INDArray execBackwardAndEndResult ( ) { List < DifferentialFunction > backwards = execBackwards ( ) . getRight ( ) ; DifferentialFunction df = backwards . get ( backwards . size ( ) - 1 ) ; if ( df instanceof Op ) { return ( ( Op ) df ) . z ( ) ; } else if ( df instanceof DynamicCustomOp ) { return ( ( DynamicCustomOp ) df ) . getOutputArgument ( 0 ) ; } else { return null ; } }


Input: Add this vertex id as a place holder [CODESPLIT] public void addAsPlaceHolder ( String varName ) { placeHolderVarNames . add ( varName ) ; if ( getVariable ( varName ) != null && getVariable ( varName ) . getShape ( ) != null ) { placeHolderOriginalShapes . put ( varName , getVariable ( varName ) . getShape ( ) ) ; } }


Input: Resolve all ndarrays by updating the variables for each array specified in the given map . An { @link IllegalStateException } will be thrown if not all arrays are specified for resolution . [CODESPLIT] public void resolveVariablesWith ( Map < String , INDArray > arrays ) { for ( val arrayEntry : arrays . entrySet ( ) ) { val varForName = getVariable ( arrayEntry . getKey ( ) ) ; if ( varForName == null ) { throw new ND4JIllegalStateException ( "No variable name found for " + arrayEntry . getKey ( ) ) ; } if ( placeHolderOriginalShapes . containsKey ( arrayEntry . getKey ( ) ) ) { val originalShape = placeHolderOriginalShapes . get ( arrayEntry . getKey ( ) ) ; if ( originalShape . length == arrayEntry . getValue ( ) . rank ( ) ) { for ( int i = 0 ; i < originalShape . length ; i ++ ) { if ( originalShape [ i ] != arrayEntry . getValue ( ) . shape ( ) [ i ] && originalShape [ i ] >= 1 ) { throw new ND4JIllegalStateException ( "Incompatible shape passed for variable. " + Arrays . toString ( arrayEntry . getValue ( ) . shape ( ) ) ) ; } } } } } for ( val entry : arrays . entrySet ( ) ) { if ( ! placeHolderVarNames . contains ( entry . getKey ( ) ) ) { throw new ND4JIllegalStateException ( "Illegal variable " + entry . getKey ( ) + " passed in. Variable found not to be a place holder variable" ) ; } val specifiedShape = getOriginalShapeForPlaceHolder ( entry . getKey ( ) ) ; //whole shape was specified: validate whether the input array shape is equal if ( ! Shape . isPlaceholderShape ( specifiedShape ) ) { if ( ! Shape . shapeEquals ( specifiedShape , entry . getValue ( ) . shape ( ) ) ) { throw new ND4JIllegalStateException ( "Place holder shape specified was " + Arrays . toString ( specifiedShape ) + " but array shape was " + Arrays . toString ( entry . getValue ( ) . shape ( ) ) ) ; } } updateShapeForVarName ( entry . getKey ( ) , entry . getValue ( ) . shape ( ) ) ; associateArrayWithVariable ( entry . getValue ( ) , getVariable ( entry . getKey ( ) ) ) ; updateArrayForVarName ( entry . getKey ( ) , entry . getValue ( ) ) ; } for ( val funcName : propertiesToResolve . keySet ( ) ) { val func = functionInstancesById . get ( funcName ) ; if ( ! functionInstancesById . containsKey ( funcName ) ) { throw new ND4JIllegalStateException ( "Unable to resolve function name " + funcName ) ; } if ( func instanceof CustomOp ) { CustomOp customOp = ( CustomOp ) func ; customOp . populateInputsAndOutputsFromSameDiff ( ) ; } } //declare resolved resolvedVariables = true ; }


Input: Returns true if all place holder variables are resolved . A place holder variable is resolved when { @link #getVariable ( String ) } getArr () does not return null and the shape is properly resolved . [CODESPLIT] public boolean allPlaceHolderVariablesResolved ( ) { for ( val vertexId : placeHolderVarNames ) { val var = getVariable ( vertexId ) ; if ( var . getArr ( ) == null ) { return false ; } } return true ; }


Input: Add one or or more place holder variables for the given vertex id . <p > Note that if a vertex id in placeHolderVariables isn t present in this samediff instance anyways an { @link ND4JIllegalStateException } is thrown [CODESPLIT] public void putPlaceHolderForVariable ( String varName , String ... placeHolderVariables ) { for ( val placeHolderVariable : placeHolderVariables ) { if ( ! variableMap . containsKey ( placeHolderVariable ) ) { throw new ND4JIllegalStateException ( "No variable found for " + placeHolderVariable ) ; } } List < String [ ] > placeHolders = placeHolderMap . get ( varName ) ; if ( placeHolders == null ) { placeHolders = new ArrayList <> ( ) ; placeHolderMap . put ( varName , placeHolders ) ; } placeHolders . add ( placeHolderVariables ) ; }


Input: Creates and executes a list of operations based on the given variables passed in . { @link #resolveVariablesWith ( Map ) } is called [CODESPLIT] public Pair < Map < SDVariable , DifferentialFunction > , List < DifferentialFunction > > execWithPlaceHolder ( Map < String , INDArray > inputs ) { resolveVariablesWith ( inputs ) ; return exec ( ) ; }


Input: Get the { @link SDVariable } associated with each function based on the { @link DifferentialFunction#outputVariables () } () } [CODESPLIT] public List < SDVariable > getVariablesAssociatedWithFunctions ( List < DifferentialFunction > functions ) { List < SDVariable > ret = new ArrayList <> ( functions . size ( ) ) ; for ( DifferentialFunction function : functions ) { ret . addAll ( Arrays . asList ( function . outputVariables ( ) ) ) ; } return ret ; }


Input: Updates the variable name property on the passed in variable the reference in samediff and returns the variable . <p > Note that if null for the new variable is passed in it will just return the original input variable . [CODESPLIT] public SDVariable updateVariableNameAndReference ( SDVariable varToUpdate , String newVarName ) { if ( varToUpdate == null ) { throw new NullPointerException ( "Null input: No variable found for updating!" ) ; } if ( newVarName == null && variableMap . containsKey ( varToUpdate . getVarName ( ) ) ) { //Edge case: suppose we do m1=sd.mean(in), m2=sd.mean(m1) -> both initially have the name // "mean" and consequently a new variable name needs to be generated newVarName = generateNewVarName ( varToUpdate . getVarName ( ) , 0 ) ; } if ( newVarName == null || varToUpdate . getVarName ( ) . equals ( newVarName ) ) { return varToUpdate ; } val oldVarName = varToUpdate . getVarName ( ) ; varToUpdate . setVarName ( newVarName ) ; updateVariableName ( oldVarName , newVarName ) ; return varToUpdate ; }


Input:
Creates and executes a list of operations [CODESPLIT] public Pair < Map < SDVariable , DifferentialFunction > , List < DifferentialFunction > > exec ( ) { if ( ! resolvedVariables ) resolveVariablesWith ( new LinkedHashMap < String , INDArray > ( ) ) ; List < DifferentialFunction > ops = new ArrayList <> ( ) ; // we don't care if this thread had any other FlowPath objects attached. we'll just create new one localFlowPath . set ( new FlowPath ( ) ) ; val flowPath = localFlowPath . get ( ) ; Map < SDVariable , DifferentialFunction > opMap = new HashMap <> ( ) ; val funcs = new ArrayList < DifferentialFunction > ( functionInstancesById . values ( ) ) ; boolean onBackward = false ; // dequeue for Frames (nested, probably) val frames = new ArrayDeque < String > ( ) ; // simple flag, set true if within frame boolean inFrame = false ; // yet another flag, to remove LastFrame once we really left last frame boolean frameLeft = false ; int i = 0 ; int exec_counter = 0 ; for ( ; i < funcs . size ( ) ; i ++ ) { ++ exec_counter ; val opName = funcs . get ( i ) . opName ( ) ; if ( ! onBackward && opName . equals ( new GradientBackwardsMarker ( ) . opName ( ) ) ) { onBackward = true ; } if ( opName . equals ( new GradientBackwardsMarker ( ) . opName ( ) ) ) continue ; DifferentialFunction differentialFunction = funcs . get ( i ) ; val ownName = differentialFunction . getOwnName ( ) ; // just registering function for this pass flowPath . ensureNodeStateExists ( differentialFunction . getOwnName ( ) ) ; if ( differentialFunction instanceof SDVariable ) { continue ; } val args = getInputsForFunction ( differentialFunction ) ; log . debug ( "Step: {}; Executing op {} for node [{}]" , exec_counter , opName , ownName ) ; // check if inputs are active nodes. skip step otherwise // please note: Exit node can't be skipped, because it's either rewind point or exit loop point boolean shouldSkip = false ; if ( differentialFunction instanceof Merge ) { val arg0 = args [ 0 ] ; val arg1 = args [ 1 ] ; if ( ! flowPath . isActive ( arg0 ) && ! flowPath . isActive ( arg1 ) ) shouldSkip = true ; } else { if ( ! ( differentialFunction instanceof Exit ) ) { // if we've left Exit nodes, we can finally delete last frame name if ( frameLeft ) { frameLeft = false ; val frame_name = frames . removeLast ( ) ; flowPath . activateFrame ( frame_name , false ) ; flowPath . forgetFrame ( frame_name ) ; } // we must check, if there's inactive nodes used as inputs for this node for ( val input : args ) { if ( ! flowPath . isActive ( input ) ) { // propagate inactivity flowPath . markActive ( differentialFunction . getOwnName ( ) , false ) ; shouldSkip = true ; break ; } } } } if ( shouldSkip ) continue ; differentialFunction . resolvePropertiesFromSameDiffBeforeExecution ( ) ; flowPath . markActive ( differentialFunction . getOwnName ( ) , true ) ; /**
             * This set of operations (Enter/Exit/NextIteration/Exit/Switch) are special snowflakes: they modify graph execution order, and basically used here to replicate TF logic.
             * Since SameDiff itself has own logic for loops and conditionals using Scopes
             */ if ( differentialFunction instanceof LoopCond ) { // this node just passes single input forward, for future evaluation val inputs = getInputVariablesForFunction ( differentialFunction ) ; val array = inputs [ 0 ] . getArr ( ) ; variableNameToArr . put ( differentialFunction . getOwnName ( ) , array . dup ( array . ordering ( ) ) ) ; flowPath . markExecuted ( differentialFunction . getOwnName ( ) , true ) ; if ( ( int ) array . getDouble ( 0 ) == 1 ) { val frameName = frames . getLast ( ) ; // incrementing number of cycles for THIS frame, only if LoopCond is true flowPath . incrementNumberOfCycles ( frameName ) ; } } else if ( differentialFunction instanceof Enter ) { //  if (flowPath.wasExecuted(differentialFunction.getOwnName())) //      continue; val inputs = getInputVariablesForFunction ( differentialFunction ) ; val array = inputs [ 0 ] . getArr ( ) ; variableNameToArr . put ( differentialFunction . getOwnName ( ) , array . dup ( array . ordering ( ) ) ) ; flowPath . markExecuted ( differentialFunction . getOwnName ( ) , true ) ; // frame_name MUST be non-null here val frame_name = ( ( Enter ) differentialFunction ) . getFrameName ( ) ; if ( ! flowPath . isRegisteredFrame ( frame_name ) ) { flowPath . registerFrame ( frame_name ) ; frames . addLast ( frame_name ) ; inFrame = true ; } } else if ( differentialFunction instanceof Exit ) { // this is just exit point of graph: it maps own input to own output or rewinds graph to specific position planned at first NextIteration node val frame_name = frames . getLast ( ) ; // saving frame_name for backward pass ( ( Exit ) differentialFunction ) . setFrameName ( frame_name ) ; if ( ! flowPath . isFrameActive ( frame_name ) ) { flowPath . markActive ( differentialFunction . getOwnName ( ) , false ) ; // if frame is inactive, lets remove it from queue as well frameLeft = true ; continue ; } // Exit node is called in any way, doesn't matters if body was executed or not // so, we're checking if rewind was planned (so, NextIteration was executed before Exit) // and if it's TRUE - we're setting applying rewind by setting loop idx and calling continue if ( flowPath . isRewindPlanned ( frame_name ) ) { // just reset loop flowPath . planRewind ( frame_name , false ) ; val currentPosition = i ; i = flowPath . getRewindPosition ( frame_name ) ; val startPosition = i + 1 ; flowPath . setRewindPosition ( frame_name , - 1 ) ; continue ; } val inputs = getInputVariablesForFunction ( differentialFunction ) ; val array = inputs [ 0 ] . getArr ( ) ; variableNameToArr . put ( differentialFunction . getOwnName ( ) , array . dup ( array . ordering ( ) ) ) ; flowPath . markExecuted ( differentialFunction . getOwnName ( ) , true ) ; // now it's safe to remove LastFrame frameLeft = true ; } else if ( differentialFunction instanceof NextIteration ) { // this operations merges own input, and schedules rewind to specific Merge node val inputs = getInputVariablesForFunction ( differentialFunction ) ; val frame_name = frames . getLast ( ) ; val array = inputs [ 0 ] . getArr ( ) ; variableNameToArr . put ( differentialFunction . getOwnName ( ) , array . dup ( array . ordering ( ) ) ) ; flowPath . markExecuted ( differentialFunction . getOwnName ( ) , true ) ; // if NextIteration wasn't skipped with inactive branch, we'll plan rewind for this frame. obviously, only once if ( ! flowPath . isRewindPlanned ( frame_name ) ) { flowPath . planRewind ( frame_name , true ) ; continue ; } } else if ( differentialFunction instanceof Merge ) { // merge operation takes two inputs, and saves one of them as own output. // if SDVariable exists for second input - we use it. First input used otherwise val inputs = getInputVariablesForFunction ( differentialFunction ) ; val frame_name = frames . size ( ) > 0 ? frames . getLast ( ) : null ; if ( frame_name != null ) flowPath . activateFrame ( frame_name , true ) ; // frame_name can be null if this merge node is used for something that's not loop. i.e. switch/merge pair if ( frame_name != null ) flowPath . setRewindPositionOnce ( frame_name , i - 1 ) ; // NextIteration can have NO frame_name defined. so let's propagate it if ( inputs . length == 2 ) { val secondArg = functionInstancesById . get ( inputs [ 1 ] . getVarName ( ) ) ; if ( secondArg != null && secondArg instanceof NextIteration ) { ( ( NextIteration ) secondArg ) . setFrameName ( frame_name ) ; } } // we must check second input first here if ( flowPath . wasExecuted ( inputs [ 1 ] . getVarName ( ) ) ) { // propagate second input val array = inputs [ 1 ] . getArr ( ) ; variableNameToArr . put ( differentialFunction . getOwnName ( ) , array . dup ( array . ordering ( ) ) ) ; // nullify executed mark flowPath . markExecuted ( inputs [ 1 ] . getVarName ( ) , false ) ; } else { // propagate first input val array = inputs [ 0 ] . getArr ( ) ; variableNameToArr . put ( differentialFunction . getOwnName ( ) , array . dup ( array . ordering ( ) ) ) ; } flowPath . markExecuted ( differentialFunction . getOwnName ( ) , true ) ; } else if ( differentialFunction instanceof Switch ) { // switch takes 2 inputs: actual input and boolean scalar. If scalar is false, input is saved as output:0, if scalar is true, input is saved as output:1 ( ( CustomOp ) differentialFunction ) . populateInputsAndOutputsFromSameDiff ( ) ; val inputs = getInputVariablesForFunction ( differentialFunction ) ; val input = inputs [ 0 ] . getArr ( ) ; val bool = inputs [ 1 ] . getArr ( ) ; // basically we're setting one of the graph branches inactive. branch 0 for false, branch 1 for true if ( ( int ) bool . getDouble ( 0 ) == 0 ) { // false step, we'll propagate output:0 here flowPath . setActiveBranch ( differentialFunction . getOwnName ( ) , 0 ) ; flowPath . markActive ( differentialFunction . getOwnName ( ) , true ) ; flowPath . markActive ( differentialFunction . getOwnName ( ) + ":1" , false ) ; variableNameToArr . put ( differentialFunction . getOwnName ( ) , input . dup ( input . ordering ( ) ) ) ; } else { // true step, we'll propagate output:1 here flowPath . setActiveBranch ( differentialFunction . getOwnName ( ) , 1 ) ; variableNameToArr . put ( differentialFunction . getOwnName ( ) + ":1" , input . dup ( input . ordering ( ) ) ) ; flowPath . markActive ( differentialFunction . getOwnName ( ) , false ) ; flowPath . markActive ( differentialFunction . getOwnName ( ) + ":1" , true ) ; } flowPath . markExecuted ( differentialFunction . getOwnName ( ) , true ) ; } else if ( differentialFunction instanceof If ) { If ifOp = ( If ) differentialFunction ; if ( ! onBackward ) { ifOp . getPredicateExecution ( ) . exec ( ) ; //depending on the block add the proper graph body to this for persistence //and possible later processing. if ( ifOp . getTargetBoolean ( ) . getArr ( ) . sumNumber ( ) . doubleValue ( ) > 0 ) { ifOp . getLoopBodyExecution ( ) . exec ( ) ; ifOp . exectedTrueOrFalse ( true ) ; } else { ifOp . getFalseBodyExecution ( ) . exec ( ) ; ifOp . exectedTrueOrFalse ( false ) ; } } else { if ( ifOp . getTrueBodyExecuted ( ) != null ) { Pair < Map < SDVariable , DifferentialFunction > , List < DifferentialFunction > > execBackwards = null ; List < SDVariable > variablesForFunctions = null ; if ( ifOp . getTrueBodyExecuted ( ) ) { execBackwards = ifOp . getLoopBodyExecution ( ) . execBackwards ( ) ; variablesForFunctions = ifOp . getLoopBodyExecution ( ) . getVariablesAssociatedWithFunctions ( execBackwards . getRight ( ) ) ; } else { execBackwards = ifOp . getFalseBodyExecution ( ) . execBackwards ( ) ; variablesForFunctions = ifOp . getFalseBodyExecution ( ) . getVariablesAssociatedWithFunctions ( execBackwards . getRight ( ) ) ; } /**
                         * Maps the variables from the child namespace body to
                         * the parent. This allows access to the underlying ndarray
                         * and returning a valid variable reference for autodiff.
                         */ for ( SDVariable variable : variablesForFunctions ) { SDVariable proxyVar = var ( variable ) ; } } else throw new ND4JIllegalStateException ( "No body was run." ) ; } flowPath . markExecuted ( differentialFunction . getOwnName ( ) , true ) ; ops . add ( differentialFunction ) ; } else if ( differentialFunction instanceof While ) { While whileOp = ( While ) differentialFunction ; if ( ! onBackward ) { SameDiff execBody = whileOp . getLoopBodyExecution ( ) ; //depending on the block add the proper graph body to this for persistence //and possible later processing. //note that we need to update the graph predicate by running the execution whileOp . getPredicateExecution ( ) . exec ( ) ; while ( whileOp . getTargetBoolean ( ) . getArr ( ) . sumNumber ( ) . doubleValue ( ) > 0 ) { //run the body execBody . exec ( ) ; //update the predicate whileOp . getPredicateExecution ( ) . exec ( ) ; whileOp . incrementLoopCounter ( ) ; } List < SDVariable > outputs = new ArrayList <> ( ) ; val outputFuncArgs = new ArrayList <> ( execBody . functionInstancesById . values ( ) ) . get ( execBody . functionInstancesById . values ( ) . size ( ) - 1 ) . outputVariables ( ) ; outputs . addAll ( Arrays . asList ( outputFuncArgs ) ) ; whileOp . setOutputVars ( outputs . toArray ( new SDVariable [ outputs . size ( ) ] ) ) ; ops . add ( differentialFunction ) ; } else { /**
                     * Note: Need to accumulate gradients.
                     * Multiply each value by the number of times looped.
                     * This approximates accumulating the gradient
                     * across a number of loop cycles.
                     * We only compute the gradient for the internal loop once
                     * and from that we multiply the gradient by 5.
                     *
                     */ Pair < Map < SDVariable , DifferentialFunction > , List < DifferentialFunction > > mapListPair = whileOp . getLoopBodyExecution ( ) . execBackwards ( ) ; for ( SDVariable variable : mapListPair . getFirst ( ) . keySet ( ) ) { variable . getArr ( ) . muli ( whileOp . getNumLooped ( ) ) ; } } flowPath . markExecuted ( differentialFunction . getOwnName ( ) , true ) ; } else if ( differentialFunction instanceof CustomOp ) { DynamicCustomOp customOp = ( DynamicCustomOp ) differentialFunction ; customOp . populateInputsAndOutputsFromSameDiff ( ) ; customOp . assertValidForExecution ( ) ; customOp . updateInputsFromSameDiff ( ) ; Nd4j . getExecutioner ( ) . exec ( customOp ) ; /*
                if (customOp instanceof LessThanOrEqual) {
                    log.info("Step: {}; InnerCondition: {} <= {} = {}", exec_counter, customOp.getInputArgument(0), customOp.getInputArgument(1), customOp.getOutputArgument(0));
                } else if (customOp instanceof LessThan) {
                    log.info("Step: {}; OuterCondition: {} <= {} = {}", exec_counter, customOp.getInputArgument(0), customOp.getInputArgument(1), customOp.getOutputArgument(0));
                }
                */ flowPath . markExecuted ( differentialFunction . getOwnName ( ) , true ) ; ops . add ( customOp ) ; } else if ( differentialFunction instanceof Op ) { val inputs = getInputVariablesForFunction ( differentialFunction ) ; Op op = ( Op ) differentialFunction ; // ops in differential function might have stale NDArrays used. we should renew them op . setX ( inputs [ 0 ] . getArr ( ) ) ; if ( inputs . length == 2 ) op . setY ( inputs [ 1 ] . getArr ( ) ) ; if ( differentialFunction . getDimensions ( ) == null ) Nd4j . getExecutioner ( ) . exec ( op ) ; else if ( op . isExecSpecial ( ) ) { op . exec ( ) ; } else { int [ ] axes = differentialFunction . getDimensions ( ) ; if ( differentialFunction instanceof Accumulation ) { Accumulation accumulation = ( Accumulation ) differentialFunction ; Nd4j . getExecutioner ( ) . exec ( accumulation , axes ) ; if ( differentialFunction . outputVariables ( ) [ 0 ] . getArr ( ) == null ) { val var = differentialFunction . outputVariables ( ) [ 0 ] ; updateArrayForVarName ( var . getVarName ( ) , accumulation . z ( ) ) ; updateShapeForVarName ( var . getVarName ( ) , accumulation . z ( ) . shape ( ) ) ; } } else if ( differentialFunction instanceof BroadcastOp ) { BroadcastOp broadcastOp = ( BroadcastOp ) differentialFunction ; Nd4j . getExecutioner ( ) . exec ( broadcastOp , axes ) ; } else if ( differentialFunction instanceof GradientOp ) { Nd4j . getExecutioner ( ) . exec ( op ) ; } else if ( differentialFunction instanceof IndexAccumulation ) { IndexAccumulation indexAccumulation = ( IndexAccumulation ) differentialFunction ; Nd4j . getExecutioner ( ) . exec ( indexAccumulation , axes ) ; } else if ( differentialFunction instanceof TransformOp ) { TransformOp t = ( TransformOp ) differentialFunction ; Nd4j . getExecutioner ( ) . exec ( t , axes ) ; } } flowPath . markExecuted ( differentialFunction . getOwnName ( ) , true ) ; ops . add ( differentialFunction ) ; } //debug // printFunction(differentialFunction); } return new Pair <> ( opMap , ops ) ; }


Input: Print the given function for debugging ( will not print functions ) [CODESPLIT] public void printFunction ( DifferentialFunction differentialFunction ) { if ( ! logExecution ) return ; if ( differentialFunction instanceof SDVariable ) return ; StringBuilder argShapes = new StringBuilder ( ) ; for ( val arg : differentialFunction . args ( ) ) { argShapes . append ( " Variable " + arg . getVarName ( ) + " Shape for " + Arrays . toString ( arg . getShape ( ) ) ) ; } for ( val func : differentialFunction . outputVariables ( ) ) { argShapes . append ( "  Output variable " + func . getVarName ( ) + " is " + Arrays . toString ( func . getShape ( ) ) ) ; } StringBuilder realShapes = new StringBuilder ( ) ; for ( val arg : differentialFunction . args ( ) ) { realShapes . append ( " Input shape for " + arg . getVarName ( ) + " is  " + Arrays . toString ( getShapeForVarName ( arg . getVarName ( ) ) ) ) ; } for ( val arg : differentialFunction . outputVariables ( ) ) { realShapes . append ( " Output shape for " + arg . getVarName ( ) + " is  " + Arrays . toString ( getShapeForVarName ( arg . getVarName ( ) ) ) ) ; } //        log.info(realShapes.toString()); }


Input:
Permute indices for the samediff / dl4j format . Due to the dl4j format being NCHW this is a simple routine for returning permute indices . This is typically used for model import . [CODESPLIT] public static int [ ] permuteDataFormatForSameDiff ( String dataFormat , boolean weights ) { val dl4jFormat = "NCHW" ; dataFormat = dataFormat . toUpperCase ( ) ; //TF: filter_height, filter_width, in_channels, out_channels /**
         * N: filter_height
         * H: filter_width
         * W: in_channels
         * C: out_channels
         */ /**
         *
         *
         */ //DL4J: filter_height,out_channels,filter_width,in_channels // Weights should be: out channels, in channels, height,width int [ ] ret = new int [ 4 ] ; if ( weights ) { ret [ 0 ] = dataFormat . indexOf ( ' ' ) ; ret [ 1 ] = dataFormat . indexOf ( ' ' ) ; ret [ 2 ] = dataFormat . indexOf ( ' ' ) ; ret [ 3 ] = dataFormat . indexOf ( ' ' ) ; return ret ; } //NHWC //DL4J: NCHW for ( int i = 0 ; i < dataFormat . length ( ) ; i ++ ) { if ( dl4jFormat . indexOf ( dataFormat . charAt ( i ) ) < 0 ) { throw new ND4JIllegalStateException ( "Illegal convolution data format string passed in " + dataFormat + " must be some variant of NCHW" ) ; } } for ( int i = 0 ; i < dl4jFormat . length ( ) ; i ++ ) { ret [ i ] = dl4jFormat . indexOf ( dataFormat . charAt ( i ) ) ; } return ret ; }


Input: Update the { @link INDArray } ndarray for the given variable name [CODESPLIT] public void updateVariable ( String variableName , INDArray arr ) { if ( ! variableNameToArr . containsKey ( variableName ) ) putArrayForVarName ( variableName , arr ) ; else updateArrayForVarName ( variableName , arr ) ; }


Input: This method exports given SameDiff instance into FlatBuffers [CODESPLIT] public ByteBuffer asFlatBuffers ( @ NonNull ExecutorConfiguration configuration ) { Nd4j . getExecutioner ( ) . commit ( ) ; FlatBufferBuilder bufferBuilder = new FlatBufferBuilder ( 1024 ) ; val idCounter = new AtomicInteger ( 0 ) ; val flatVariables = new ArrayList < Integer > ( ) ; val flatOffsets = new ArrayList < Integer > ( ) ; val flatNodes = new ArrayList < Integer > ( ) ; // first of all we build VariableSpace dump List < SDVariable > variableList = new ArrayList <> ( variables ( ) ) ; val reverseMap = new LinkedHashMap < String , Integer > ( ) ; val forwardMap = new LinkedHashMap < String , Integer > ( ) ; val framesMap = new LinkedHashMap < String , Integer > ( ) ; int idx = 0 ; for ( val variable : variables ( ) ) { log . debug ( "Exporting variable: [{}]" , variable . getVarName ( ) ) ; if ( variable . getArr ( ) == null || variable . getShape ( ) == null ) { //putArrayForVarName(variable.getVarName(), Nd4j.scalar(1.0)); //addAsPlaceHolder(variable.getVarName()); continue ; } val pair = parseVariable ( variable . getVarName ( ) ) ; reverseMap . put ( pair . getFirst ( ) , idCounter . incrementAndGet ( ) ) ; log . debug ( "Adding [{}] as [{}]" , pair . getFirst ( ) , idCounter . get ( ) ) ; val arr = variable . getArr ( ) ; int name = bufferBuilder . createString ( variable . getVarName ( ) ) ; int array = arr . toFlatArray ( bufferBuilder ) ; int id = IntPair . createIntPair ( bufferBuilder , idCounter . get ( ) , 0 ) ; int flatVariable = FlatVariable . createFlatVariable ( bufferBuilder , id , name , 0 , array , - 1 ) ; flatVariables . add ( flatVariable ) ; } //add functions for ( val func : functionInstancesById . values ( ) ) { flatNodes . add ( asFlatNode ( func , bufferBuilder , variableList , reverseMap , forwardMap , framesMap , idCounter ) ) ; } // we're dumping scopes now for ( val scope : sameDiffFunctionInstances . entrySet ( ) ) { flatNodes . add ( asFlatNode ( scope . getKey ( ) , scope . getValue ( ) , bufferBuilder ) ) ; val currVarList = new ArrayList < SDVariable > ( scope . getValue ( ) . variables ( ) ) ; // converting all ops from node for ( val node : scope . getValue ( ) . variables ( ) ) { INDArray arr = node . getArr ( ) ; if ( arr == null ) { //val otherArr = Nd4j.scalar(1.0); //scope.getValue().putArrayForVarName(node.getVarName(), otherArr); //log.warn("Adding placeholder for export for var name {}", node.getVarName()); //arr = otherArr; continue ; } int name = bufferBuilder . createString ( node . getVarName ( ) ) ; int array = arr . toFlatArray ( bufferBuilder ) ; int id = IntPair . createIntPair ( bufferBuilder , ++ idx , 0 ) ; val pair = parseVariable ( node . getVarName ( ) ) ; reverseMap . put ( pair . getFirst ( ) , idx ) ; log . debug ( "Adding [{}] as [{}]" , pair . getFirst ( ) , idx ) ; int flatVariable = FlatVariable . createFlatVariable ( bufferBuilder , id , name , 0 , array , - 1 ) ; flatVariables . add ( flatVariable ) ; } //add functions for ( val func : scope . getValue ( ) . functionInstancesById . values ( ) ) { flatNodes . add ( asFlatNode ( func , bufferBuilder , currVarList , reverseMap , forwardMap , framesMap , idCounter ) ) ; } } int outputsOffset = FlatGraph . createVariablesVector ( bufferBuilder , Ints . toArray ( flatOffsets ) ) ; int variablesOffset = FlatGraph . createVariablesVector ( bufferBuilder , Ints . toArray ( flatVariables ) ) ; int nodesOffset = FlatGraph . createNodesVector ( bufferBuilder , Ints . toArray ( flatNodes ) ) ; int fg = FlatGraph . createFlatGraph ( bufferBuilder , 119 , variablesOffset , nodesOffset , outputsOffset , configuration . getFlatConfiguration ( bufferBuilder ) ) ; bufferBuilder . finish ( fg ) ; return bufferBuilder . dataBuffer ( ) ; }


Input: This method returns flattened graph . [CODESPLIT] public String asFlatPrint ( ) { val sb = new StringBuilder ( ) ; val fb = asFlatBuffers ( ) ; val graph = FlatGraph . getRootAsFlatGraph ( fb ) ; sb . append ( "\nExternal variables:\n\n" ) ; for ( int e = 0 ; e < graph . variablesLength ( ) ; e ++ ) { val var = graph . variables ( e ) ; val ndarray = Nd4j . createFromFlatArray ( var . ndarray ( ) ) ; sb . append ( var . id ( ) . first ( ) ) . append ( ":<" ) . append ( var . name ( ) ) . append ( "> " ) . append ( Arrays . toString ( ndarray . shapeInfoDataBuffer ( ) . asInt ( ) ) ) . append ( "; Values: " ) . append ( Arrays . toString ( ndarray . data ( ) . asFloat ( ) ) ) . append ( ";\n" ) ; } val map = Nd4j . getExecutioner ( ) . getCustomOperations ( ) ; sb . append ( "\nOps sequence:\n\n" ) ; for ( int e = 0 ; e < graph . nodesLength ( ) ; e ++ ) { val node = graph . nodes ( e ) ; log . info ( "{}:<{}>" , node . id ( ) , node . name ( ) ) ; sb . append ( node . id ( ) ) . append ( ":<" ) . append ( node . name ( ) ) . append ( "> " ) . append ( SameDiff . getTypeFromByte ( node . opType ( ) ) ) ; if ( SameDiff . getTypeFromByte ( node . opType ( ) ) != Op . Type . CUSTOM ) sb . append ( ": " ) . append ( node . opNum ( ) ) ; else { val keys = map . keySet ( ) ; String opName = null ; for ( val k : keys ) { val d = map . get ( k ) ; if ( d . getHash ( ) == node . opNum ( ) ) opName = k ; } if ( opName == null ) opName = "unknown" ; sb . append ( ": " ) . append ( opName ) ; } sb . append ( "; Inputs: {" ) ; for ( int i = 0 ; i < node . inputPairedLength ( ) ; i ++ ) { val pair = node . inputPaired ( i ) ; sb . append ( "[" ) . append ( pair . first ( ) ) . append ( ":" ) . append ( pair . second ( ) ) . append ( "]" ) ; if ( i < node . inputPairedLength ( ) - 1 ) sb . append ( ", " ) ; } sb . append ( "};" ) ; sb . append ( " OpNum: {" ) . append ( node . opNum ( ) ) . append ( "};" ) ; sb . append ( "\n" ) ; } return sb . toString ( ) ; }


Input: This method converts enums for DataType [CODESPLIT] public static DataBuffer . Type getDataTypeFromByte ( byte val ) { if ( val == DataType . FLOAT ) return DataBuffer . Type . FLOAT ; else if ( val == DataType . DOUBLE ) return DataBuffer . Type . DOUBLE ; else if ( val == DataType . HALF ) return DataBuffer . Type . HALF ; throw new UnsupportedOperationException ( "Unsupported DataType: [" + val + "]" ) ; }


Input: This method converts enums for DataType [CODESPLIT] public static byte getDataTypeAsByte ( DataBuffer . Type type ) { switch ( type ) { case FLOAT : return DataType . FLOAT ; case DOUBLE : return DataType . DOUBLE ; case HALF : return DataType . HALF ; case INT : return DataType . INT32 ; case LONG : return DataType . INT64 ; default : throw new ND4JIllegalStateException ( "Unknown or unsupported DataType used: [" + type + "]" ) ; } }


Input: This method return operation ID for given op name / type pair . [CODESPLIT] public static long getOpNum ( String name , Op . Type type ) { if ( type == Op . Type . LOOP ) { return 0 ; } else if ( type == Op . Type . RETURN ) { return 40 ; } else if ( type == Op . Type . IF ) { return 30 ; } else if ( type == Op . Type . CONDITIONAL ) { return 10 ; } else if ( type == Op . Type . MERGE ) { return 60L ; } else if ( type == Op . Type . LOOP_COND ) { return 70L ; } else if ( type == Op . Type . NEXT_ITERATION ) { return 80L ; } else if ( type == Op . Type . EXIT ) { return 90L ; } else if ( type == Op . Type . ENTER ) { return 100L ; } else if ( type == Op . Type . CUSTOM ) { val name2 = Nd4j . getExecutioner ( ) . getCustomOperations ( ) . get ( name . toLowerCase ( ) ) ; if ( name2 == null ) return 0 ; return Nd4j . getExecutioner ( ) . getCustomOperations ( ) . get ( name . toLowerCase ( ) ) . getHash ( ) ; } else return ( long ) Nd4j . getOpFactory ( ) . getOpNumByName ( name ) ; }


Input: This method converts enums for DataType [CODESPLIT] public static Op . Type getTypeFromByte ( byte type ) { switch ( type ) { case OpType . SCALAR : return Op . Type . SCALAR ; case OpType . BROADCAST : return Op . Type . BROADCAST ; case OpType . TRANSFORM : return Op . Type . TRANSFORM ; case OpType . ACCUMULATION : return Op . Type . REDUCE ; case OpType . ACCUMULATION3 : return Op . Type . REDUCE3 ; case OpType . INDEX_ACCUMULATION : return Op . Type . INDEXREDUCE ; case OpType . RANDOM : return Op . Type . RANDOM ; case OpType . LOGIC : return Op . Type . META ; case OpType . CUSTOM : return Op . Type . CUSTOM ; case OpType . SHAPE : return Op . Type . SHAPE ; case OpType . PAIRWISE : return Op . Type . PAIRWISE ; case OpType . SUMMARYSTATS : return Op . Type . SUMMARYSTATS ; default : throw new UnsupportedOperationException ( "Unknown op type passed in: " + type ) ; } }


Input: This method converts enums for DataType [CODESPLIT] public static byte getFlatOpType ( Op . Type type ) { switch ( type ) { case SCALAR : return OpType . SCALAR ; case BROADCAST : return OpType . BROADCAST ; case TRANSFORM : case SPECIAL : return OpType . TRANSFORM ; case REDUCE : return OpType . ACCUMULATION ; case REDUCE3 : return OpType . ACCUMULATION3 ; case INDEXREDUCE : return OpType . INDEX_ACCUMULATION ; case RANDOM : return OpType . RANDOM ; case MERGE : case CONDITIONAL : case LOOP : case RETURN : case ENTER : case EXIT : case NEXT_ITERATION : case LOOP_COND : case IF : return OpType . LOGIC ; case CUSTOM : return OpType . CUSTOM ; case SHAPE : return OpType . SHAPE ; case PAIRWISE : return OpType . PAIRWISE ; case SUMMARYSTATS : return OpType . SUMMARYSTATS ; default : throw new UnsupportedOperationException ( "Unknown op type passed in: " + type ) ; } }


Input: This method returns Pointer to allocated memory chunk [CODESPLIT] @ Override public Pointer allocate ( long bytes , MemoryKind kind , boolean initialize ) { AtomicAllocator allocator = AtomicAllocator . getInstance ( ) ; //log.info("Allocating {} bytes in {} memory...", bytes, kind); if ( kind == MemoryKind . HOST ) { Pointer ptr = NativeOpsHolder . getInstance ( ) . getDeviceNativeOps ( ) . mallocHost ( bytes , 0 ) ; if ( ptr == null ) throw new RuntimeException ( "Failed to allocate " + bytes + " bytes from HOST memory" ) ; if ( initialize ) Pointer . memset ( ptr , 0 , bytes ) ; return ptr ; //allocator.getMemoryHandler().alloc(AllocationStatus.HOST, null, null, initialize).getHostPointer(); } else if ( kind == MemoryKind . DEVICE ) { Pointer ptr = NativeOpsHolder . getInstance ( ) . getDeviceNativeOps ( ) . mallocDevice ( bytes , null , 0 ) ; //log.info("Allocating {} bytes for device_{}", bytes, Nd4j.getAffinityManager().getDeviceForCurrentThread()); if ( ptr == null ) throw new RuntimeException ( "Failed to allocate " + bytes + " bytes from DEVICE [" + Nd4j . getAffinityManager ( ) . getDeviceForCurrentThread ( ) + "] memory" ) ; if ( initialize ) { CudaContext context = ( CudaContext ) AtomicAllocator . getInstance ( ) . getDeviceContext ( ) . getContext ( ) ; int i = NativeOpsHolder . getInstance ( ) . getDeviceNativeOps ( ) . memsetAsync ( ptr , 0 , bytes , 0 , context . getSpecialStream ( ) ) ; if ( i == 0 ) throw new ND4JIllegalStateException ( "memset failed on device_" + Nd4j . getAffinityManager ( ) . getDeviceForCurrentThread ( ) ) ; context . getSpecialStream ( ) . synchronize ( ) ; } return ptr ; //allocator.getMemoryHandler().alloc(AllocationStatus.HOST, null, null, initialize).getDevicePointer(); } else throw new RuntimeException ( "Unknown MemoryKind requested: " + kind ) ; }


Input: Set the data for the underlying array . If the underlying buffer s array is equivalent to this array it returns ( avoiding an unneccessary copy ) [CODESPLIT] public static void setData ( float [ ] data , INDArray toSet ) { if ( toSet . data ( ) . dataType ( ) != DataBuffer . Type . FLOAT ) { throw new IllegalArgumentException ( "Unable to set double data for opType " + toSet . data ( ) . dataType ( ) ) ; } if ( toSet . data ( ) . allocationMode ( ) == DataBuffer . AllocationMode . HEAP ) { Object array = toSet . data ( ) . array ( ) ; //data is assumed to have already been updated if ( array == data ) return ; else { //copy the data over directly to the underlying array float [ ] d = ( float [ ] ) array ; if ( toSet . offset ( ) == 0 && toSet . length ( ) == data . length ) System . arraycopy ( data , 0 , d , 0 , d . length ) ; else { int count = 0 ; //need to do strided access with offset for ( int i = 0 ; i < data . length ; i ++ ) { // FIXME: LONG int dIndex = ( int ) toSet . offset ( ) + ( i * toSet . majorStride ( ) ) ; d [ dIndex ] = data [ count ++ ] ; } } } } else { //assumes the underlying data is in the right order DataBuffer underlyingData = toSet . data ( ) ; if ( data . length == toSet . length ( ) && toSet . offset ( ) == 0 ) { for ( int i = 0 ; i < toSet . length ( ) ; i ++ ) { underlyingData . put ( i , data [ i ] ) ; } } else { int count = 0 ; //need to do strided access with offset for ( int i = 0 ; i < data . length ; i ++ ) { // FIXME: LONG int dIndex = ( int ) toSet . offset ( ) + ( i * toSet . majorStride ( ) ) ; underlyingData . put ( dIndex , data [ count ++ ] ) ; } } } }


Input: Returns the length for the given data opType [CODESPLIT] public static int lengthForDtype ( DataBuffer . Type type ) { switch ( type ) { case DOUBLE : return 8 ; case FLOAT : return 4 ; case INT : return 4 ; case HALF : return 2 ; case LONG : return 8 ; case COMPRESSED : default : throw new IllegalArgumentException ( "Illegal opType for length" ) ; } }


Input: Get the allocation mode from the context [CODESPLIT] public static DataBuffer . Type getDtypeFromContext ( String dType ) { switch ( dType ) { case "double" : return DataBuffer . Type . DOUBLE ; case "float" : return DataBuffer . Type . FLOAT ; case "int" : return DataBuffer . Type . INT ; case "half" : return DataBuffer . Type . HALF ; default : return DataBuffer . Type . FLOAT ; } }


Input: Gets the name of the alocation mode [CODESPLIT] public static String getDTypeForName ( DataBuffer . Type allocationMode ) { switch ( allocationMode ) { case DOUBLE : return "double" ; case FLOAT : return "float" ; case INT : return "int" ; case HALF : return "half" ; default : return "float" ; } }


Input: get the allocation mode from the context [CODESPLIT] public static DataBuffer . Type getDtypeFromContext ( ) { try { lock . readLock ( ) . lock ( ) ; if ( dtype == null ) { lock . readLock ( ) . unlock ( ) ; lock . writeLock ( ) . lock ( ) ; if ( dtype == null ) dtype = getDtypeFromContext ( Nd4jContext . getInstance ( ) . getConf ( ) . getProperty ( "dtype" ) ) ; lock . writeLock ( ) . unlock ( ) ; lock . readLock ( ) . lock ( ) ; } return dtype ; } finally { lock . readLock ( ) . unlock ( ) ; } }


Input: Set the allocation mode for the nd4j context The value must be one of : heap java cpp or direct or an [CODESPLIT] public static void setDTypeForContext ( DataBuffer . Type allocationModeForContext ) { try { lock . writeLock ( ) . lock ( ) ; dtype = allocationModeForContext ; setDTypeForContext ( getDTypeForName ( allocationModeForContext ) ) ; } finally { lock . writeLock ( ) . unlock ( ) ; } }


Input: This method returns op id number for given opName [CODESPLIT] @ Override public int getOpNumByName ( String opName ) { try { DifferentialFunction op = DifferentialFunctionClassHolder . getInstance ( ) . getInstance ( opName ) ; return op . opNum ( ) ; } catch ( Exception e ) { throw new RuntimeException ( "OpName failed: [" + opName + "]" , e ) ; } }


Input: / * @Override public MemoryWorkspace getWorkspaceForCurrentThread ( @NonNull WorkspaceConfiguration configuration @NonNull String id ) { ensureThreadExistense () ; [CODESPLIT] protected void pickReference ( MemoryWorkspace workspace ) { Nd4jWorkspace . GarbageWorkspaceReference reference = new Nd4jWorkspace . GarbageWorkspaceReference ( workspace , queue ) ; referenceMap . put ( reference . getKey ( ) , reference ) ; }


Input: This method destroys all workspaces allocated in current thread [CODESPLIT] @ Override public void destroyAllWorkspacesForCurrentThread ( ) { ensureThreadExistense ( ) ; List < MemoryWorkspace > workspaces = new ArrayList <> ( ) ; workspaces . addAll ( backingMap . get ( ) . values ( ) ) ; for ( MemoryWorkspace workspace : workspaces ) { destroyWorkspace ( workspace ) ; } System . gc ( ) ; }


Input: This method prints out basic statistics for workspaces allocated in current thread [CODESPLIT] public synchronized void printAllocationStatisticsForCurrentThread ( ) { ensureThreadExistense ( ) ; Map < String , MemoryWorkspace > map = backingMap . get ( ) ; log . info ( "Workspace statistics: ---------------------------------" ) ; log . info ( "Number of workspaces in current thread: {}" , map . size ( ) ) ; log . info ( "Workspace name: Allocated / external (spilled) / external (pinned)" ) ; for ( String key : map . keySet ( ) ) { long current = ( ( Nd4jWorkspace ) map . get ( key ) ) . getCurrentSize ( ) ; long spilled = ( ( Nd4jWorkspace ) map . get ( key ) ) . getSpilledSize ( ) ; long pinned = ( ( Nd4jWorkspace ) map . get ( key ) ) . getPinnedSize ( ) ; log . info ( String . format ( "%-26s %8s / %8s / %8s (%11d / %11d / %11d)" , ( key + ":" ) , StringUtils . TraditionalBinaryPrefix . long2String ( current , "" , 2 ) , StringUtils . TraditionalBinaryPrefix . long2String ( spilled , "" , 2 ) , StringUtils . TraditionalBinaryPrefix . long2String ( pinned , "" , 2 ) , current , spilled , pinned ) ) ; } }


Input: gemv computes a matrix - vector product using a general matrix and performs one of the following matrix - vector operations : y : = alpha * a * x + beta * y for trans = N or n ; y : = alpha * a * x + beta * y for trans = T or t ; y : = alpha * conjg ( a ) * x + beta * y for trans = C or c . Here a is an m - by - n band matrix x and y are vectors alpha and beta are scalars . [CODESPLIT] @ Override public void gemv ( char order , char transA , double alpha , INDArray A , INDArray X , double beta , INDArray Y ) { if ( Nd4j . getExecutioner ( ) . getProfilingMode ( ) == OpExecutioner . ProfilingMode . ALL ) OpProfiler . getInstance ( ) . processBlasCall ( false , A , X , Y ) ; if ( A . isSparse ( ) && ! X . isSparse ( ) ) { Nd4j . getSparseBlasWrapper ( ) . level2 ( ) . gemv ( order , transA , alpha , A , X , beta , Y ) ; return ; } GemvParameters parameters = new GemvParameters ( A , X , Y ) ; if ( A . data ( ) . dataType ( ) == DataBuffer . Type . DOUBLE ) { DefaultOpExecutioner . validateDataType ( DataBuffer . Type . DOUBLE , parameters . getA ( ) , parameters . getX ( ) , parameters . getY ( ) ) ; dgemv ( order , parameters . getAOrdering ( ) , parameters . getM ( ) , parameters . getN ( ) , alpha , parameters . getA ( ) , parameters . getLda ( ) , parameters . getX ( ) , parameters . getIncx ( ) , beta , parameters . getY ( ) , parameters . getIncy ( ) ) ; } else { DefaultOpExecutioner . validateDataType ( DataBuffer . Type . FLOAT , parameters . getA ( ) , parameters . getX ( ) , parameters . getY ( ) ) ; sgemv ( order , parameters . getAOrdering ( ) , parameters . getM ( ) , parameters . getN ( ) , ( float ) alpha , parameters . getA ( ) , parameters . getLda ( ) , parameters . getX ( ) , parameters . getIncx ( ) , ( float ) beta , parameters . getY ( ) , parameters . getIncy ( ) ) ; } OpExecutionerUtil . checkForAny ( Y ) ; }


Input: gemv computes a matrix - vector product using a general matrix and performs one of the following matrix - vector operations : y : = alpha * a * x + beta * y for trans = N or n ; y : = alpha * a * x + beta * y for trans = T or t ; y : = alpha * conjg ( a ) * x + beta * y for trans = C or c . Here a is an m - by - n band matrix x and y are vectors alpha and beta are scalars . [CODESPLIT] @ Override public void gemv ( char order , char transA , IComplexNumber alpha , IComplexNDArray A , IComplexNDArray X , IComplexNumber beta , IComplexNDArray Y ) { if ( Nd4j . getExecutioner ( ) . getProfilingMode ( ) == OpExecutioner . ProfilingMode . ALL ) OpProfiler . getInstance ( ) . processBlasCall ( false , A , X , Y ) ; GemvParameters parameters = new GemvParameters ( A , X , Y ) ; if ( A . data ( ) . dataType ( ) == DataBuffer . Type . DOUBLE ) zgemv ( order , transA , parameters . getM ( ) , parameters . getN ( ) , alpha . asDouble ( ) , A , parameters . getLda ( ) , X , parameters . getIncx ( ) , beta . asDouble ( ) , Y , parameters . getIncy ( ) ) ; else cgemv ( order , transA , parameters . getM ( ) , parameters . getN ( ) , alpha . asFloat ( ) , A , parameters . getLda ( ) , X , parameters . getIncx ( ) , beta . asFloat ( ) , Y , parameters . getIncy ( ) ) ; }


Input: gbmv computes a matrix - vector product using a general band matrix and performs one of the following matrix - vector operations : y : = alpha * a * x + beta * y for trans = N or n ; y : = alpha * a * x + beta * y for trans = T or t ; y : = alpha * conjg ( a ) * x + beta * y for trans = C or c . Here a is an m - by - n band matrix with ku superdiagonals and kl subdiagonals x and y are vectors alpha and beta are scalars . [CODESPLIT] @ Override public void gbmv ( char order , char TransA , int KL , int KU , IComplexNumber alpha , IComplexNDArray A , IComplexNDArray X , IComplexNumber beta , IComplexNDArray Y ) { // FIXME: int cast if ( A . data ( ) . dataType ( ) == DataBuffer . Type . DOUBLE ) { zgbmv ( order , TransA , ( int ) A . rows ( ) , ( int ) A . columns ( ) , KL , KU , alpha . asDouble ( ) , A , ( int ) A . size ( 0 ) , X , X . majorStride ( ) / 2 , beta . asDouble ( ) , Y , Y . majorStride ( ) / 2 ) ; } else { cgbmv ( order , TransA , ( int ) A . rows ( ) , ( int ) A . columns ( ) , KL , KU , alpha . asFloat ( ) , A , ( int ) A . size ( 0 ) , X , X . majorStride ( ) / 2 , beta . asFloat ( ) , Y , Y . majorStride ( ) / 2 ) ; } }


Input: performs a rank - 1 update of a general m - by - n matrix a without conjugation : a : = alpha * x * y + a . [CODESPLIT] @ Override public void geru ( char order , IComplexNumber alpha , IComplexNDArray X , IComplexNDArray Y , IComplexNDArray A ) { // FIXME: int cast if ( X . data ( ) . dataType ( ) == DataBuffer . Type . DOUBLE ) zgeru ( order , ( int ) A . rows ( ) , ( int ) A . columns ( ) , alpha . asDouble ( ) , X , X . majorStride ( ) / 2 , Y , Y . majorStride ( ) / 2 , A , ( int ) A . size ( 0 ) ) ; else cgeru ( order , ( int ) A . rows ( ) , ( int ) A . columns ( ) , alpha . asFloat ( ) , X , X . majorStride ( ) / 2 , Y , Y . majorStride ( ) / 2 , A , ( int ) A . size ( 0 ) ) ; }


Input: performs a rank - 1 update of a general m - by - n matrix a without conjugation : a : = alpha * x * y + a . [CODESPLIT] @ Override public void hbmv ( char order , char Uplo , IComplexNumber alpha , IComplexNDArray A , IComplexNDArray X , IComplexNumber beta , IComplexNDArray Y ) { // FIXME: int cast if ( A . data ( ) . dataType ( ) == DataBuffer . Type . DOUBLE ) zhbmv ( order , Uplo , ( int ) X . length ( ) , ( int ) A . columns ( ) , alpha . asDouble ( ) , A , ( int ) A . size ( 0 ) , X , X . majorStride ( ) / 2 , beta . asDouble ( ) , Y , Y . majorStride ( ) / 2 ) ; else chbmv ( order , Uplo , ( int ) X . length ( ) , ( int ) A . columns ( ) , alpha . asFloat ( ) , A , ( int ) A . size ( 0 ) , X , X . majorStride ( ) / 2 , beta . asFloat ( ) , Y , Y . majorStride ( ) / 2 ) ; }


Input: hemv computes a matrix - vector product using a Hermitian matrix : y : = alpha * a * x + beta * y . Here a is an n - by - n Hermitian band matrix with k superdiagonals x and y are n - element vectors alpha and beta are scalars . [CODESPLIT] @ Override public void hemv ( char order , char Uplo , IComplexNumber alpha , IComplexNDArray A , IComplexNDArray X , IComplexNumber beta , IComplexNDArray Y ) { // FIXME: int cast if ( A . data ( ) . dataType ( ) == DataBuffer . Type . DOUBLE ) zhemv ( order , Uplo , ( int ) A . rows ( ) , alpha . asDouble ( ) , A , ( int ) A . size ( 0 ) , X , X . majorStride ( ) / 2 , beta . asDouble ( ) , Y , Y . majorStride ( ) / 2 ) ; else chemv ( order , Uplo , ( int ) A . rows ( ) , alpha . asFloat ( ) , A , ( int ) A . size ( 0 ) , X , X . majorStride ( ) / 2 , beta . asFloat ( ) , Y , Y . majorStride ( ) / 2 ) ; }


Input: ?her2 performs a rank - 2 update of an n - by - n Hermitian matrix a : a : = alpha * x * conjg ( y ) + conjg ( alpha ) * y * conjg ( x ) + a . [CODESPLIT] @ Override public void her2 ( char order , char Uplo , IComplexNumber alpha , IComplexNDArray X , IComplexNDArray Y , IComplexNDArray A ) { // FIXME: int cast if ( X . data ( ) . dataType ( ) == DataBuffer . Type . DOUBLE ) zher2 ( order , Uplo , ( int ) A . rows ( ) , alpha . asDouble ( ) , X , X . majorStride ( ) / 2 , Y , Y . majorStride ( ) / 2 , A , ( int ) A . size ( 0 ) ) ; else cher2 ( order , Uplo , ( int ) A . rows ( ) , alpha . asFloat ( ) , X , X . majorStride ( ) / 2 , Y , Y . majorStride ( ) / 2 , A , ( int ) A . size ( 0 ) ) ; }


Input: ?hpmv computes a matrix - vector product using a Hermitian packed matrix : y : = alpha * a * x + beta * y . Here a is an n - by - n packed Hermitian matrix x and y are n - element vectors alpha and beta are scalars . [CODESPLIT] @ Override public void hpmv ( char order , char Uplo , int N , IComplexNumber alpha , IComplexNDArray Ap , IComplexNDArray X , IComplexNumber beta , IComplexNDArray Y ) { // FIXME: int cast if ( Ap . data ( ) . dataType ( ) == DataBuffer . Type . DOUBLE ) zhpmv ( order , Uplo , ( int ) Ap . rows ( ) , alpha . asDouble ( ) , Ap , X , X . majorStride ( ) / 2 , beta . asDouble ( ) , Y , Y . majorStride ( ) / 2 ) ; else chpmv ( order , Uplo , ( int ) Ap . rows ( ) , alpha . asFloat ( ) , Ap , X , X . majorStride ( ) / 2 , beta . asFloat ( ) , Y , Y . majorStride ( ) / 2 ) ; }


Input: hpr2 performs a rank - 2 update of an n - by - n packed Hermitian matrix a : a : = alpha * x * conjg ( y ) + conjg ( alpha ) * y * conjg ( x ) + a . [CODESPLIT] @ Override public void hpr2 ( char order , char Uplo , IComplexNumber alpha , IComplexNDArray X , IComplexNDArray Y , IComplexNDArray Ap ) { // FIXME: int cast if ( X . data ( ) . dataType ( ) == DataBuffer . Type . DOUBLE ) zhpr2 ( order , Uplo , ( int ) Ap . rows ( ) , alpha . asDouble ( ) , X , X . majorStride ( ) / 2 , Y , Y . majorStride ( ) / 2 , Ap ) ; else chpr2 ( order , Uplo , ( int ) Ap . rows ( ) , alpha . asFloat ( ) , X , X . majorStride ( ) / 2 , Y , Y . majorStride ( ) / 2 , Ap ) ; }


Input: sbmv computes a matrix - vector product using a symmetric band matrix : y : = alpha * a * x + beta * y . Here a is an n - by - n symmetric band matrix with k superdiagonals x and y are n - element vectors alpha and beta are scalars . [CODESPLIT] @ Override public void sbmv ( char order , char Uplo , double alpha , INDArray A , INDArray X , double beta , INDArray Y ) { if ( Nd4j . getExecutioner ( ) . getProfilingMode ( ) == OpExecutioner . ProfilingMode . ALL ) OpProfiler . getInstance ( ) . processBlasCall ( false , A , X , Y ) ; // FIXME: int cast if ( X . data ( ) . dataType ( ) == DataBuffer . Type . DOUBLE ) { DefaultOpExecutioner . validateDataType ( DataBuffer . Type . DOUBLE , A , X , Y ) ; dsbmv ( order , Uplo , ( int ) X . length ( ) , ( int ) A . columns ( ) , alpha , A , ( int ) A . size ( 0 ) , X , X . majorStride ( ) , beta , Y , ( int ) Y . majorStride ( ) ) ; } else { DefaultOpExecutioner . validateDataType ( DataBuffer . Type . FLOAT , A , X , Y ) ; ssbmv ( order , Uplo , ( int ) X . length ( ) , ( int ) A . columns ( ) , ( float ) alpha , A , ( int ) A . size ( 0 ) , X , X . majorStride ( ) , ( float ) beta , Y , Y . majorStride ( ) ) ; } OpExecutionerUtil . checkForAny ( Y ) ; }


Input: syr2 performs a rank - 2 update of an n - by - n symmetric matrix a : a : = alpha * x * y + alpha * y * x + a . [CODESPLIT] @ Override public void tbmv ( char order , char Uplo , char TransA , char Diag , INDArray A , INDArray X ) { if ( Nd4j . getExecutioner ( ) . getProfilingMode ( ) == OpExecutioner . ProfilingMode . ALL ) OpProfiler . getInstance ( ) . processBlasCall ( false , A , X ) ; // FIXME: int cast if ( X . data ( ) . dataType ( ) == DataBuffer . Type . DOUBLE ) { DefaultOpExecutioner . validateDataType ( DataBuffer . Type . DOUBLE , A , X ) ; dtbmv ( order , Uplo , TransA , Diag , ( int ) X . length ( ) , ( int ) A . columns ( ) , A , ( int ) A . size ( 0 ) , X , X . majorStride ( ) ) ; } else { DefaultOpExecutioner . validateDataType ( DataBuffer . Type . FLOAT , A , X ) ; stbmv ( order , Uplo , TransA , Diag , ( int ) X . length ( ) , ( int ) A . columns ( ) , A , ( int ) A . size ( 0 ) , X , X . majorStride ( ) ) ; } }


Input: trmv computes a matrix - vector product using a triangular matrix . [CODESPLIT] @ Override public void trmv ( char order , char Uplo , char TransA , char Diag , INDArray A , INDArray X ) { if ( Nd4j . getExecutioner ( ) . getProfilingMode ( ) == OpExecutioner . ProfilingMode . ALL ) OpProfiler . getInstance ( ) . processBlasCall ( false , A , X ) ; // FIXME: int cast if ( A . data ( ) . dataType ( ) == DataBuffer . Type . DOUBLE ) { DefaultOpExecutioner . validateDataType ( DataBuffer . Type . DOUBLE , A , X ) ; dtrmv ( order , Uplo , TransA , Diag , ( int ) X . length ( ) , A , ( int ) A . size ( 0 ) , X , X . majorStride ( ) ) ; } else { DefaultOpExecutioner . validateDataType ( DataBuffer . Type . FLOAT , A , X ) ; strmv ( order , Uplo , TransA , Diag , ( int ) X . length ( ) , A , ( int ) A . size ( 0 ) , X , X . majorStride ( ) ) ; } OpExecutionerUtil . checkForAny ( X ) ; }


Input: Receive an ndarray [CODESPLIT] public INDArray receive ( ) { if ( consumerTemplate == null ) consumerTemplate = camelContext . createConsumerTemplate ( ) ; return consumerTemplate . receiveBody ( "direct:receive" , INDArray . class ) ; }


Input: / * Given a list of labels return the bernoulli prob that the masks will be sampled at to meet the target minority label distribution [CODESPLIT] private INDArray calculateBernoulli ( INDArray minorityLabels , INDArray labelMask , double targetMinorityDist ) { INDArray minorityClass = minorityLabels . dup ( ) . muli ( labelMask ) ; INDArray majorityClass = Transforms . not ( minorityLabels ) . muli ( labelMask ) ; //all minorityLabel class, keep masks as is //presence of minoriy class and donotmask minority windows set to true return label as is if ( majorityClass . sumNumber ( ) . intValue ( ) == 0 || ( minorityClass . sumNumber ( ) . intValue ( ) > 0 && donotMaskMinorityWindows ) ) return labelMask ; //all majority class and set to not mask all majority windows sample majority class by 1-targetMinorityDist if ( minorityClass . sumNumber ( ) . intValue ( ) == 0 && ! maskAllMajorityWindows ) return labelMask . muli ( 1 - targetMinorityDist ) ; //Probabilities to be used for bernoulli sampling INDArray minoritymajorityRatio = minorityClass . sum ( 1 ) . div ( majorityClass . sum ( 1 ) ) ; INDArray majorityBernoulliP = minoritymajorityRatio . muli ( 1 - targetMinorityDist ) . divi ( targetMinorityDist ) ; BooleanIndexing . replaceWhere ( majorityBernoulliP , 1.0 , Conditions . greaterThan ( 1.0 ) ) ; //if minority ratio is already met round down to 1.0 return majorityClass . muliColumnVector ( majorityBernoulliP ) . addi ( minorityClass ) ; }


Input: Execute an accumulation along one or more dimensions [CODESPLIT] @ Override public INDArray exec ( Variance accumulation , boolean biasCorrected , int ... dimension ) { return processOp ( accumulation ) . z ( ) ; }


Input: This method executes specific RandomOp against specified RNG [CODESPLIT] @ Override public INDArray exec ( RandomOp op , Random rng ) { return processOp ( op ) . z ( ) ; }


Input: This method encodes array as thresholds updating input array at the same time [CODESPLIT] @ Override public INDArray thresholdEncode ( INDArray input , double threshold ) { return backendExecutioner . thresholdEncode ( input , threshold ) ; }


Input: This method encodes array as thresholds updating input array at the same time [CODESPLIT] @ Override public INDArray thresholdEncode ( INDArray input , double threshold , Integer boundary ) { return backendExecutioner . thresholdEncode ( input , threshold , boundary ) ; }


Input: This method decodes thresholds array and puts it into target array [CODESPLIT] @ Override public INDArray thresholdDecode ( INDArray encoded , INDArray target ) { return backendExecutioner . thresholdDecode ( encoded , target ) ; }


Input: Normalize a data array [CODESPLIT] @ Override public void preProcess ( INDArray array , INDArray maskArray , DistributionStats stats ) { if ( array . rank ( ) <= 2 ) { array . subiRowVector ( stats . getMean ( ) ) ; array . diviRowVector ( filteredStd ( stats ) ) ; } // if array Rank is 3 (time series) samplesxfeaturesxtimesteps // if array Rank is 4 (images) samplesxchannelsxrowsxcols // both cases operations should be carried out in dimension 1 else { Nd4j . getExecutioner ( ) . execAndReturn ( new BroadcastSubOp ( array , stats . getMean ( ) , array , 1 ) ) ; Nd4j . getExecutioner ( ) . execAndReturn ( new BroadcastDivOp ( array , filteredStd ( stats ) , array , 1 ) ) ; } if ( maskArray != null ) { DataSetUtil . setMaskedValuesToZero ( array , maskArray ) ; } }


Input: Denormalize a data array [CODESPLIT] @ Override public void revert ( INDArray array , INDArray maskArray , DistributionStats stats ) { if ( array . rank ( ) <= 2 ) { array . muliRowVector ( filteredStd ( stats ) ) ; array . addiRowVector ( stats . getMean ( ) ) ; } else { Nd4j . getExecutioner ( ) . execAndReturn ( new BroadcastMulOp ( array , filteredStd ( stats ) , array , 1 ) ) ; Nd4j . getExecutioner ( ) . execAndReturn ( new BroadcastAddOp ( array , stats . getMean ( ) , array , 1 ) ) ; } if ( maskArray != null ) { DataSetUtil . setMaskedValuesToZero ( array , maskArray ) ; } }


Input: Map a tensorflow node name to the samediff equivalent for import [CODESPLIT] public String getNodeName ( String name ) { //tensorflow adds colons to the end of variables representing input index, this strips those off String ret = name ; if ( ret . startsWith ( "^" ) ) ret = ret . substring ( 1 ) ; if ( ret . endsWith ( "/read" ) ) { ret = ret . replace ( "/read" , "" ) ; } return ret ; }


Input:
ScalarOp along dimension [CODESPLIT] private void invoke ( ScalarOp op , int [ ] dimension ) { dimension = Shape . normalizeAxis ( op . x ( ) . rank ( ) , dimension ) ; // do tad magic /**
         * Returns the {@link Shape#createShapeInformation(int[], int[], int, int, char)}
         * and the associated offsets for each {@link INDArray#tensorAlongDimension(int, int...)}
         * The first item is the shape information. The second one is the offsets.
         */ Pair < DataBuffer , DataBuffer > tadBuffers = tadManager . getTADOnlyShapeInfo ( op . x ( ) , dimension ) ; Pointer hostTadShapeInfo = tadBuffers . getFirst ( ) . addressPointer ( ) ; Pointer hostTadOffsets = tadBuffers . getSecond ( ) . addressPointer ( ) ; Pointer devTadShapeInfoZ = null ; Pointer devTadOffsetsZ = null ; /**
         * Returns the {@link Shape#createShapeInformation(int[], int[], int, int, char)}
         * and the associated offsets for each {@link INDArray#tensorAlongDimension(int, int...)}
         * The first item is the shape information. The second one is the offsets.
         *
         * Note that this is the *result* TAD information. An op is always input (x) and output (z)
         * for result.
         * This is for assigning the result to of the operation along
         * the proper dimension.
         */ Pair < DataBuffer , DataBuffer > tadBuffersZ = tadManager . getTADOnlyShapeInfo ( op . z ( ) , dimension ) ; devTadShapeInfoZ = tadBuffersZ . getFirst ( ) . addressPointer ( ) ; devTadOffsetsZ = tadBuffersZ . getSecond ( ) . addressPointer ( ) ; if ( extraz . get ( ) == null ) extraz . set ( new PointerPointer ( 32 ) ) ; PointerPointer dummy = extraz . get ( ) . put ( hostTadShapeInfo , hostTadOffsets , devTadShapeInfoZ , devTadOffsetsZ ) ; if ( op . x ( ) . data ( ) . dataType ( ) == DataBuffer . Type . FLOAT ) { loop . execScalarFloat ( dummy , op . opNum ( ) , ( FloatPointer ) op . x ( ) . data ( ) . addressPointer ( ) , ( LongPointer ) op . x ( ) . shapeInfoDataBuffer ( ) . addressPointer ( ) , ( FloatPointer ) op . z ( ) . data ( ) . addressPointer ( ) , ( LongPointer ) op . z ( ) . shapeInfoDataBuffer ( ) . addressPointer ( ) , ( FloatPointer ) op . y ( ) . data ( ) . addressPointer ( ) , ( FloatPointer ) getPointerForExtraArgs ( op ) , ( IntPointer ) Nd4j . getConstantHandler ( ) . getConstantBuffer ( dimension ) . addressPointer ( ) , dimension . length ) ; } else if ( op . x ( ) . data ( ) . dataType ( ) == DataBuffer . Type . DOUBLE ) { loop . execScalarDouble ( dummy , op . opNum ( ) , ( DoublePointer ) op . x ( ) . data ( ) . addressPointer ( ) , ( LongPointer ) op . x ( ) . shapeInfoDataBuffer ( ) . addressPointer ( ) , ( DoublePointer ) op . z ( ) . data ( ) . addressPointer ( ) , ( LongPointer ) op . z ( ) . shapeInfoDataBuffer ( ) . addressPointer ( ) , ( DoublePointer ) op . y ( ) . data ( ) . addressPointer ( ) , ( DoublePointer ) getPointerForExtraArgs ( op ) , ( IntPointer ) Nd4j . getConstantHandler ( ) . getConstantBuffer ( dimension ) . addressPointer ( ) , dimension . length ) ; } }


Input: This method takes arbitrary sized list of { [CODESPLIT] @ Override public void exec ( Aggregate op ) { // long st = profilingHookIn(op); if ( memoryBlocks . get ( ) == null ) memoryBlocks . set ( new HashMap < Integer , AggregateMemoryBlock > ( ) ) ; if ( memoryBlocks . get ( ) . get ( op . opNum ( ) ) == null ) memoryBlocks . get ( ) . put ( op . opNum ( ) , new AggregateMemoryBlock ( op ) ) ; AggregateMemoryBlock block = memoryBlocks . get ( ) . get ( op . opNum ( ) ) ; int numArguments = op . getArguments ( ) . size ( ) ; int numIndexArguments = op . getIndexingArguments ( ) . size ( ) ; int numRealArguments = op . getRealArguments ( ) . size ( ) ; int numShapes = op . getShapes ( ) . size ( ) ; int numIntArrays = op . getIntArrayArguments ( ) . size ( ) ; PointerPointer arguments = block . getArgumentsPointer ( ) ; //new PointerPointer(numArguments); List < IntPointer > pointers = new ArrayList <> ( ) ; PointerPointer intArrays = block . getArraysPointer ( ) ; //new PointerPointer(numIntArrays); for ( int x = 0 ; x < numArguments ; x ++ ) { arguments . put ( x , op . getArguments ( ) . get ( x ) == null ? null : op . getArguments ( ) . get ( x ) . data ( ) . addressPointer ( ) ) ; } PointerPointer shapes = block . getShapesPointer ( ) ; //new PointerPointer(numShapes); for ( int x = 0 ; x < numShapes ; x ++ ) { if ( op . getShapes ( ) . get ( x ) . dataType ( ) != DataBuffer . Type . INT ) throw new RuntimeException ( "ShapeBuffers should have INT data opType" ) ; shapes . put ( x , op . getShapes ( ) . get ( x ) == null ? null : op . getShapes ( ) . get ( x ) . addressPointer ( ) ) ; } //int[] indexes = new int[numIndexArguments]; IntPointer pointer = block . getIndexingPointer ( ) ; for ( int x = 0 ; x < numIndexArguments ; x ++ ) { pointer . put ( x , op . getIndexingArguments ( ) . get ( x ) ) ; } //IntPointer pointer = new IntPointer(indexes); double [ ] reals = new double [ numRealArguments ] ; for ( int x = 0 ; x < numRealArguments ; x ++ ) { //reals[x] = op.getRealArguments().get(x).doubleValue(); if ( Nd4j . dataType ( ) == DataBuffer . Type . FLOAT ) ( ( FloatPointer ) block . getRealArgumentsPointer ( ) ) . put ( x , op . getRealArguments ( ) . get ( x ) . floatValue ( ) ) ; else ( ( DoublePointer ) block . getRealArgumentsPointer ( ) ) . put ( x , op . getRealArguments ( ) . get ( x ) . doubleValue ( ) ) ; } for ( int x = 0 ; x < numIntArrays ; x ++ ) { IntPointer intPtr = block . getIntArrays ( ) . get ( x ) ; //new IntPointer(op.getIntArrayArguments().get(x)); intPtr . put ( op . getIntArrayArguments ( ) . get ( x ) , 0 , op . getIntArrayArguments ( ) . get ( x ) . length ) ; intArrays . put ( x , intPtr ) ; pointers . add ( intPtr ) ; } //INDArray realsBuffer = Nd4j.create(reals); if ( Nd4j . dataType ( ) == DataBuffer . Type . FLOAT ) { loop . execAggregateFloat ( null , op . opNum ( ) , arguments , numArguments , shapes , numShapes , pointer , numIndexArguments , intArrays , numIntArrays , ( FloatPointer ) block . getRealArgumentsPointer ( ) , numRealArguments ) ; } else if ( Nd4j . dataType ( ) == DataBuffer . Type . DOUBLE ) { loop . execAggregateDouble ( null , op . opNum ( ) , arguments , numArguments , shapes , numShapes , pointer , numIndexArguments , intArrays , numIntArrays , ( DoublePointer ) block . getRealArgumentsPointer ( ) , numRealArguments ) ; } else { throw new UnsupportedOperationException ( "Half precision isn't supported on CPU" ) ; } }


Input: This method return set of key / value and key / key / value objects describing current environment [CODESPLIT] @ Override public Properties getEnvironmentInformation ( ) { Properties properties = super . getEnvironmentInformation ( ) ; properties . put ( Nd4jEnvironment . BACKEND_KEY , "CPU" ) ; properties . put ( Nd4jEnvironment . OMP_THREADS_KEY , loop . ompGetMaxThreads ( ) ) ; properties . put ( Nd4jEnvironment . BLAS_THREADS_KEY , Nd4j . factory ( ) . blas ( ) . getMaxThreads ( ) ) ; properties . put ( Nd4jEnvironment . BLAS_VENDOR_KEY , ( Nd4j . factory ( ) . blas ( ) ) . getBlasVendor ( ) . toString ( ) ) ; properties . put ( Nd4jEnvironment . HOST_FREE_MEMORY_KEY , Pointer . maxBytes ( ) - Pointer . totalBytes ( ) ) ; // fill bandwidth information properties . put ( Nd4jEnvironment . MEMORY_BANDWIDTH_KEY , PerformanceTracker . getInstance ( ) . getCurrentBandwidth ( ) ) ; return properties ; }