Input: This method executes specific RandomOp against specified RNG [CODESPLIT] @ Override public INDArray exec ( RandomOp op , Random rng ) { if ( rng . getStateBuffer ( ) == null ) throw new IllegalStateException ( "You should use one of NativeRandom classes for NativeOperations execution" ) ; long st = profilingHookIn ( op ) ; validateDataType ( Nd4j . dataType ( ) , op ) ; if ( op . x ( ) != null && op . y ( ) != null && op . z ( ) != null ) { // triple arg call if ( Nd4j . dataType ( ) == DataBuffer . Type . FLOAT ) { loop . execRandomFloat ( null , op . opNum ( ) , rng . getStatePointer ( ) , // rng state ptr ( FloatPointer ) op . x ( ) . data ( ) . addressPointer ( ) , ( LongPointer ) op . x ( ) . shapeInfoDataBuffer ( ) . addressPointer ( ) , ( FloatPointer ) op . y ( ) . data ( ) . addressPointer ( ) , ( LongPointer ) op . y ( ) . shapeInfoDataBuffer ( ) . addressPointer ( ) , ( FloatPointer ) op . z ( ) . data ( ) . addressPointer ( ) , ( LongPointer ) op . z ( ) . shapeInfoDataBuffer ( ) . addressPointer ( ) , ( FloatPointer ) op . extraArgsDataBuff ( ) . addressPointer ( ) ) ; } else if ( Nd4j . dataType ( ) == DataBuffer . Type . DOUBLE ) { loop . execRandomDouble ( null , op . opNum ( ) , rng . getStatePointer ( ) , // rng state ptr ( DoublePointer ) op . x ( ) . data ( ) . addressPointer ( ) , ( LongPointer ) op . x ( ) . shapeInfoDataBuffer ( ) . addressPointer ( ) , ( DoublePointer ) op . y ( ) . data ( ) . addressPointer ( ) , ( LongPointer ) op . y ( ) . shapeInfoDataBuffer ( ) . addressPointer ( ) , ( DoublePointer ) op . z ( ) . data ( ) . addressPointer ( ) , ( LongPointer ) op . z ( ) . shapeInfoDataBuffer ( ) . addressPointer ( ) , ( DoublePointer ) op . extraArgsDataBuff ( ) . addressPointer ( ) ) ; } } else if ( op . x ( ) != null && op . z ( ) != null ) { //double arg call if ( Nd4j . dataType ( ) == DataBuffer . Type . FLOAT ) { loop . execRandomFloat ( null , op . opNum ( ) , rng . getStatePointer ( ) , // rng state ptr ( FloatPointer ) op . x ( ) . data ( ) . addressPointer ( ) , ( LongPointer ) op . x ( ) . shapeInfoDataBuffer ( ) . addressPointer ( ) , ( FloatPointer ) op . z ( ) . data ( ) . addressPointer ( ) , ( LongPointer ) op . z ( ) . shapeInfoDataBuffer ( ) . addressPointer ( ) , ( FloatPointer ) op . extraArgsDataBuff ( ) . addressPointer ( ) ) ; } else if ( Nd4j . dataType ( ) == DataBuffer . Type . DOUBLE ) { loop . execRandomDouble ( null , op . opNum ( ) , rng . getStatePointer ( ) , // rng state ptr ( DoublePointer ) op . x ( ) . data ( ) . addressPointer ( ) , ( LongPointer ) op . x ( ) . shapeInfoDataBuffer ( ) . addressPointer ( ) , ( DoublePointer ) op . z ( ) . data ( ) . addressPointer ( ) , ( LongPointer ) op . z ( ) . shapeInfoDataBuffer ( ) . addressPointer ( ) , ( DoublePointer ) op . extraArgsDataBuff ( ) . addressPointer ( ) ) ; } } else { // single arg call if ( Nd4j . dataType ( ) == DataBuffer . Type . FLOAT ) { loop . execRandomFloat ( null , op . opNum ( ) , rng . getStatePointer ( ) , // rng state ptr ( FloatPointer ) op . z ( ) . data ( ) . addressPointer ( ) , ( LongPointer ) op . z ( ) . shapeInfoDataBuffer ( ) . addressPointer ( ) , ( FloatPointer ) op . extraArgsDataBuff ( ) . addressPointer ( ) ) ; } else if ( Nd4j . dataType ( ) == DataBuffer . Type . DOUBLE ) { loop . execRandomDouble ( null , op . opNum ( ) , rng . getStatePointer ( ) , // rng state ptr ( DoublePointer ) op . z ( ) . data ( ) . addressPointer ( ) , ( LongPointer ) op . z ( ) . shapeInfoDataBuffer ( ) . addressPointer ( ) , ( DoublePointer ) op . extraArgsDataBuff ( ) . addressPointer ( ) ) ; } } profilingHookOut ( op , st ) ; return op . z ( ) ; }


Input: This method executes given CustomOp [CODESPLIT] public void exec ( @ NonNull CustomOp op ) { long st = profilingHookIn ( op ) ; if ( op . numOutputArguments ( ) == 0 && ! op . isInplaceCall ( ) ) throw new ND4JIllegalStateException ( "Op name " + op . opName ( ) + " failed to execute. You can't execute non-inplace CustomOp without outputs being specified" ) ; val name = op . opName ( ) . toLowerCase ( ) ; val hash = op . opHash ( ) ; val inputShapes = getInputShapes ( op . numInputArguments ( ) ) ; val inputBuffers = getInputBuffers ( op . numInputArguments ( ) ) ; int cnt = 0 ; val inputArgs = op . inputArguments ( ) ; for ( val in : inputArgs ) { if ( in == null ) { throw new NullPointerException ( "Input argument is null" ) ; } inputBuffers . put ( cnt , in . data ( ) . addressPointer ( ) ) ; inputShapes . put ( cnt ++ , in . shapeInfoDataBuffer ( ) . addressPointer ( ) ) ; } val outputArgs = op . outputArguments ( ) ; for ( int i = 0 ; i < outputArgs . length ; i ++ ) { if ( outputArgs [ i ] == null ) throw new ND4JIllegalStateException ( "Op output arguments must not be null!" ) ; } val outputShapes = getOutputShapes ( op . numOutputArguments ( ) ) ; val outputBuffers = getOutputBuffers ( op . numOutputArguments ( ) ) ; cnt = 0 ; for ( val out : outputArgs ) { outputBuffers . put ( cnt , out . data ( ) . addressPointer ( ) ) ; outputShapes . put ( cnt ++ , out . shapeInfoDataBuffer ( ) . addressPointer ( ) ) ; } val iArgs = op . numIArguments ( ) > 0 ? new LongPointer ( op . numIArguments ( ) ) : null ; cnt = 0 ; val iArgs1 = op . iArgs ( ) ; for ( val i : iArgs1 ) iArgs . put ( cnt ++ , i ) ; if ( Nd4j . dataType ( ) == DataBuffer . Type . FLOAT ) { val tArgs = op . numTArguments ( ) > 0 ? new FloatPointer ( op . numTArguments ( ) ) : null ; val tArgs1 = op . tArgs ( ) ; cnt = 0 ; for ( val t : tArgs1 ) tArgs . put ( cnt ++ , ( float ) t ) ; OpStatus status = OpStatus . byNumber ( loop . execCustomOpFloat ( null , hash , inputBuffers , inputShapes , op . numInputArguments ( ) , outputBuffers , outputShapes , op . numOutputArguments ( ) , tArgs , op . numTArguments ( ) , iArgs , op . numIArguments ( ) , op . isInplaceCall ( ) ) ) ; if ( status != OpStatus . ND4J_STATUS_OK ) throw new ND4JIllegalStateException ( "Op execution failed: " + status ) ; } else if ( Nd4j . dataType ( ) == DataBuffer . Type . DOUBLE ) { val tArgs = op . numTArguments ( ) > 0 ? getDoublePointerFrom ( tArgsPointer , op . numTArguments ( ) ) : null ; val tArgs1 = op . tArgs ( ) ; cnt = 0 ; for ( val t : tArgs1 ) tArgs . put ( cnt ++ , t ) ; val t = op . numInputArguments ( ) ; OpStatus status = OpStatus . ND4J_STATUS_OK ; try { status = OpStatus . byNumber ( loop . execCustomOpDouble ( null , hash , inputBuffers , inputShapes , op . numInputArguments ( ) , outputBuffers , outputShapes , op . numOutputArguments ( ) , tArgs , op . numTArguments ( ) , iArgs , op . numIArguments ( ) , op . isInplaceCall ( ) ) ) ; } catch ( Exception e ) { log . error ( "Failed to execute. Please see above message (printed out from c++) for a possible cause of error." ) ; throw e ; } } else if ( Nd4j . dataType ( ) == DataBuffer . Type . HALF ) { val tArgs = op . numTArguments ( ) > 0 ? getShortPointerFrom ( halfArgsPointer , op . numTArguments ( ) ) : null ; cnt = 0 ; val tArgs1 = op . tArgs ( ) ; for ( val t : tArgs1 ) tArgs . put ( cnt ++ , ArrayUtil . toHalf ( t ) ) ; OpStatus status = OpStatus . byNumber ( loop . execCustomOpHalf ( null , hash , inputBuffers , inputShapes , op . numInputArguments ( ) , outputBuffers , outputShapes , op . numOutputArguments ( ) , tArgs , op . numTArguments ( ) , iArgs , op . numIArguments ( ) , op . isInplaceCall ( ) ) ) ; if ( status != OpStatus . ND4J_STATUS_OK ) throw new ND4JIllegalStateException ( "Op execution failed: " + status ) ; } profilingHookOut ( op , st ) ; }


Input: This command is possible to issue only from Shard [CODESPLIT] @ Override protected synchronized void sendCoordinationCommand ( VoidMessage message ) { if ( nodeRole == NodeRole . SHARD && voidConfiguration . getNumberOfShards ( ) == 1 ) { message . setTargetId ( ( short ) - 1 ) ; messages . add ( message ) ; return ; } //log.info("Sending CC: {}", message.getClass().getCanonicalName()); message . setTargetId ( ( short ) - 1 ) ; publicationForShards . offer ( message . asUnsafeBuffer ( ) ) ; }


Input: This command is possible to issue only from Shard [CODESPLIT] @ Override protected synchronized void sendFeedbackToClient ( VoidMessage message ) { if ( nodeRole == NodeRole . SHARD && voidConfiguration . getNumberOfShards ( ) == 1 && message instanceof MeaningfulMessage ) { message . setTargetId ( ( short ) - 1 ) ; completed . put ( message . getTaskId ( ) , ( MeaningfulMessage ) message ) ; return ; } //log.info("Sending FC: {}", message.getClass().getCanonicalName()); message . setTargetId ( ( short ) - 1 ) ; publicationForClients . offer ( message . asUnsafeBuffer ( ) ) ; }


Input: Assert that no workspaces are currently open [CODESPLIT] public static void assertNoWorkspacesOpen ( String msg ) throws ND4JWorkspaceException { if ( Nd4j . getWorkspaceManager ( ) . anyWorkspaceActiveForCurrentThread ( ) ) { List < MemoryWorkspace > l = Nd4j . getWorkspaceManager ( ) . getAllWorkspacesForCurrentThread ( ) ; List < String > workspaces = new ArrayList <> ( l . size ( ) ) ; for ( MemoryWorkspace ws : l ) { if ( ws . isScopeActive ( ) ) { workspaces . add ( ws . getId ( ) ) ; } } throw new ND4JWorkspaceException ( msg + " - Open/active workspaces: " + workspaces ) ; } }


Input: ND Convolution [CODESPLIT] @ Override public IComplexNDArray convn ( IComplexNDArray input , IComplexNDArray kernel , Convolution . Type type , int [ ] axes ) { throw new UnsupportedOperationException ( ) ; }


Input: This method returns an array consisting of each of the training samples for each label in each sample the negative log likelihood of that value falling within the given gaussian mixtures . [CODESPLIT] private INDArray negativeLogLikelihood ( INDArray labels , INDArray alpha , INDArray mu , INDArray sigma ) { INDArray labelsMinusMu = labelsMinusMu ( labels , mu ) ; INDArray diffsquared = labelsMinusMu . mul ( labelsMinusMu ) . sum ( 2 ) ; INDArray phitimesalphasum = phi ( diffsquared , sigma ) . muli ( alpha ) . sum ( 1 ) ; // result = See Bishop(28,29) INDArray result = Transforms . log ( phitimesalphasum ) . negi ( ) ; return result ; }


Input: This method requests to change state to Tick . [CODESPLIT] public void requestTick ( long time , TimeUnit timeUnit ) { long timeframeMs = TimeUnit . MILLISECONDS . convert ( time , timeUnit ) ; long currentTime = System . currentTimeMillis ( ) ; boolean isWaiting = false ; // if we have Toe request queued - we' have to wait till it finishes. try { while ( isToeScheduled . get ( ) || isToeWaiting . get ( ) || getCurrentState ( ) == AccessState . TOE ) { if ( ! isWaiting ) { isWaiting = true ; waitingTicks . incrementAndGet ( ) ; } Thread . sleep ( 50 ) ; } currentState . set ( AccessState . TICK . ordinal ( ) ) ; waitingTicks . decrementAndGet ( ) ; tickRequests . incrementAndGet ( ) ; } catch ( Exception e ) { throw new RuntimeException ( e ) ; } }


Input: This method requests to change state to Toe [CODESPLIT] public boolean tryRequestToe ( ) { scheduleToe ( ) ; if ( isToeWaiting . get ( ) || getCurrentState ( ) == AccessState . TOE ) { //System.out.println("discarding TOE"); discardScheduledToe ( ) ; return false ; } else { //System.out.println("requesting TOE"); discardScheduledToe ( ) ; requestToe ( ) ; return true ; } }


Input: This method requests release Toe status back to Tack . [CODESPLIT] public void releaseToe ( ) { if ( getCurrentState ( ) == AccessState . TOE ) { if ( 1 > 0 ) { //if (toeThread.get() == Thread.currentThread().getId()) { if ( toeRequests . decrementAndGet ( ) == 0 ) { tickRequests . set ( 0 ) ; tackRequests . set ( 0 ) ; currentState . set ( AccessState . TACK . ordinal ( ) ) ; } } else throw new IllegalStateException ( "releaseToe() is called from different thread." ) ; } else throw new IllegalStateException ( "Object is NOT in Toe state!" ) ; }


Input: This method returns the current memory state [CODESPLIT] public AccessState getCurrentState ( ) { if ( AccessState . values ( ) [ currentState . get ( ) ] == AccessState . TOE ) { return AccessState . TOE ; } else { if ( tickRequests . get ( ) <= tackRequests . get ( ) ) { // TODO: looks like this piece of code should be locked :/ tickRequests . set ( 0 ) ; tackRequests . set ( 0 ) ; return AccessState . TACK ; } else return AccessState . TICK ; } }


Input: This method build [CODESPLIT] public static Environment buildEnvironment ( ) { Environment environment = new Environment ( ) ; environment . setJavaVersion ( System . getProperty ( "java.specification.version" ) ) ; environment . setNumCores ( Runtime . getRuntime ( ) . availableProcessors ( ) ) ; environment . setAvailableMemory ( Runtime . getRuntime ( ) . maxMemory ( ) ) ; environment . setOsArch ( System . getProperty ( "os.arch" ) ) ; environment . setOsName ( System . getProperty ( "os.opName" ) ) ; environment . setBackendUsed ( Nd4j . getExecutioner ( ) . getClass ( ) . getSimpleName ( ) ) ; return environment ; }


Input: Vector aggregations are saved only by Shards started aggregation process . All other Shards are ignoring this meesage [CODESPLIT] @ Override public void processMessage ( ) { if ( clipboard . isTracking ( this . originatorId , this . getTaskId ( ) ) ) { clipboard . pin ( this ) ; if ( clipboard . isReady ( this . originatorId , taskId ) ) { VoidAggregation aggregation = clipboard . unpin ( this . originatorId , taskId ) ; // FIXME: probably there's better solution, then "screw-and-forget" one if ( aggregation == null ) return ; VectorCompleteMessage msg = new VectorCompleteMessage ( taskId , aggregation . getAccumulatedResult ( ) ) ; msg . setOriginatorId ( aggregation . getOriginatorId ( ) ) ; transport . sendMessage ( msg ) ; } } }


Input: Initializes this data transform fetcher from the passed in datasets [CODESPLIT] protected void initializeCurrFromList ( List < DataSet > examples ) { if ( examples . isEmpty ( ) ) log . warn ( "Warning: empty dataset from the fetcher" ) ; INDArray inputs = createInputMatrix ( examples . size ( ) ) ; INDArray labels = createOutputMatrix ( examples . size ( ) ) ; for ( int i = 0 ; i < examples . size ( ) ; i ++ ) { inputs . putRow ( i , examples . get ( i ) . getFeatureMatrix ( ) ) ; labels . putRow ( i , examples . get ( i ) . getLabels ( ) ) ; } curr = new DataSet ( inputs , labels ) ; }


Input: Set the value for this function . Note that if value is null an { @link ND4JIllegalStateException } will be thrown . [CODESPLIT] public void setValueFor ( Field target , Object value ) { if ( value == null ) { throw new ND4JIllegalStateException ( "Unable to set field " + target + " using null value!" ) ; } value = ensureProperType ( target , value ) ; try { target . set ( this , value ) ; } catch ( IllegalAccessException e ) { e . printStackTrace ( ) ; } }


Input:
This method executes preconfigured number of host memory garbage collectors [CODESPLIT] protected void initHostCollectors ( ) { for ( int i = 0 ; i < configuration . getNumberOfGcThreads ( ) ; i ++ ) { ReferenceQueue < BaseDataBuffer > queue = new ReferenceQueue <> ( ) ; UnifiedGarbageCollectorThread uThread = new UnifiedGarbageCollectorThread ( i , queue ) ; // all GC threads should be attached to default device Nd4j . getAffinityManager ( ) . attachThreadToDevice ( uThread , getDeviceId ( ) ) ; queueMap . put ( i , queue ) ; uThread . start ( ) ; collectorsUnified . put ( i , uThread ) ; /*
            ZeroGarbageCollectorThread zThread = new ZeroGarbageCollectorThread((long) i, shouldStop);
            zThread.start();

            collectorsZero.put((long) i, zThread);
            */ } }


Input: This method returns actual device pointer valid for current object [CODESPLIT] @ Override public Pointer getPointer ( DataBuffer buffer , CudaContext context ) { return memoryHandler . getDevicePointer ( buffer , context ) ; }


Input: This method should be called to make sure that data on host side is actualized [CODESPLIT] @ Override public void synchronizeHostData ( DataBuffer buffer ) { // we don't want non-committed ops left behind //Nd4j.getExecutioner().push(); // we don't synchronize constant buffers, since we assume they are always valid on host side if ( buffer . isConstant ( ) ) { return ; } // we actually need synchronization only in device-dependant environment. no-op otherwise if ( memoryHandler . isDeviceDependant ( ) ) { AllocationPoint point = getAllocationPoint ( buffer . getTrackingPoint ( ) ) ; if ( point == null ) throw new RuntimeException ( "AllocationPoint is NULL" ) ; memoryHandler . synchronizeThreadDevice ( Thread . currentThread ( ) . getId ( ) , memoryHandler . getDeviceId ( ) , point ) ; } }


Input:
This method allocates required chunk of memory in specific location <p > PLEASE NOTE : Do not use this method unless you re 100% sure what you re doing [CODESPLIT] @ Override public AllocationPoint allocateMemory ( DataBuffer buffer , AllocationShape requiredMemory , AllocationStatus location , boolean initialize ) { AllocationPoint point = new AllocationPoint ( ) ; useTracker . set ( System . currentTimeMillis ( ) ) ; // we use these longs as tracking codes for memory tracking Long allocId = objectsTracker . getAndIncrement ( ) ; //point.attachBuffer(buffer); point . setObjectId ( allocId ) ; point . setShape ( requiredMemory ) ; /*
        if (buffer instanceof CudaIntDataBuffer) {
            buffer.setConstant(true);
            point.setConstant(true);
        }
        */ int numBuckets = configuration . getNumberOfGcThreads ( ) ; int bucketId = RandomUtils . nextInt ( 0 , numBuckets ) ; GarbageBufferReference reference = new GarbageBufferReference ( ( BaseDataBuffer ) buffer , queueMap . get ( bucketId ) , point ) ; point . attachReference ( reference ) ; point . setDeviceId ( - 1 ) ; if ( buffer . isAttached ( ) ) { long reqMem = AllocationUtils . getRequiredMemory ( requiredMemory ) ; //log.info("Allocating {} bytes from attached memory...", reqMem); // workaround for init order getMemoryHandler ( ) . getCudaContext ( ) ; point . setDeviceId ( Nd4j . getAffinityManager ( ) . getDeviceForCurrentThread ( ) ) ; CudaWorkspace workspace = ( CudaWorkspace ) Nd4j . getMemoryManager ( ) . getCurrentWorkspace ( ) ; PointersPair pair = new PointersPair ( ) ; PagedPointer ptrDev = workspace . alloc ( reqMem , MemoryKind . DEVICE , requiredMemory . getDataType ( ) , initialize ) ; PagedPointer ptrHost = workspace . alloc ( reqMem , MemoryKind . HOST , requiredMemory . getDataType ( ) , initialize ) ; pair . setHostPointer ( ptrHost ) ; if ( ptrDev != null ) { pair . setDevicePointer ( ptrDev ) ; point . setAllocationStatus ( AllocationStatus . DEVICE ) ; } else { pair . setDevicePointer ( ptrHost ) ; point . setAllocationStatus ( AllocationStatus . HOST ) ; } //if (!ptrDev.isLeaked()) point . setAttached ( true ) ; point . setPointers ( pair ) ; } else { // we stay naive on PointersPair, we just don't know on this level, which pointers are set. MemoryHandler will be used for that PointersPair pair = memoryHandler . alloc ( location , point , requiredMemory , initialize ) ; point . setPointers ( pair ) ; } allocationsMap . put ( allocId , point ) ; point . tickHostRead ( ) ; point . tickDeviceWrite ( ) ; return point ; }


Input: Convert a { [CODESPLIT] public static INDArray fromTensor ( Tensor tensor ) { byte b = tensor . typeType ( ) ; int [ ] shape = new int [ tensor . shapeLength ( ) ] ; int [ ] stride = new int [ tensor . stridesLength ( ) ] ; for ( int i = 0 ; i < shape . length ; i ++ ) { shape [ i ] = ( int ) tensor . shape ( i ) . size ( ) ; stride [ i ] = ( int ) tensor . strides ( i ) ; } int length = ArrayUtil . prod ( shape ) ; Buffer buffer = tensor . data ( ) ; if ( buffer == null ) { throw new ND4JIllegalStateException ( "Buffer was not serialized properly." ) ; } //deduce element size int elementSize = ( int ) buffer . length ( ) / length ; //nd4j strides aren't  based on element size for ( int i = 0 ; i < stride . length ; i ++ ) { stride [ i ] /= elementSize ; } DataBuffer . Type type = typeFromTensorType ( b , elementSize ) ; DataBuffer dataBuffer = DataBufferStruct . createFromByteBuffer ( tensor . getByteBuffer ( ) , ( int ) tensor . data ( ) . offset ( ) , type , length , elementSize ) ; INDArray arr = Nd4j . create ( dataBuffer , shape ) ; arr . setShapeAndStride ( shape , stride ) ; return arr ; }


Input: Create a { [CODESPLIT] public static int addDataForArr ( FlatBufferBuilder bufferBuilder , INDArray arr ) { int offset = DataBufferStruct . createDataBufferStruct ( bufferBuilder , arr . data ( ) ) ; int ret = Buffer . createBuffer ( bufferBuilder , offset , arr . data ( ) . length ( ) * arr . data ( ) . getElementSize ( ) ) ; return ret ; }


Input: Create thee databuffer type frm the given type relative to the bytes in arrow in class : { [CODESPLIT] public static DataBuffer . Type typeFromTensorType ( byte type , int elementSize ) { if ( type == Type . Decimal || type == Type . FloatingPoint ) { if ( elementSize == 4 ) { return DataBuffer . Type . FLOAT ; } else if ( elementSize == 8 ) { return DataBuffer . Type . DOUBLE ; } } else if ( type == Type . Int ) { if ( elementSize == 4 ) { return DataBuffer . Type . INT ; } else if ( elementSize == 8 ) { return DataBuffer . Type . LONG ; } } else { throw new IllegalArgumentException ( "Only valid types are Type.Decimal and Type.Int" ) ; } throw new IllegalArgumentException ( "Unable to determine data type" ) ; }


Input: This method provides PointersPair to memory chunk specified by AllocationShape [CODESPLIT] @ Override public PointersPair malloc ( AllocationShape shape , AllocationPoint point , AllocationStatus location ) { //log.info("shape onCreate: {}, target: {}", shape, location); switch ( location ) { case HOST : { Pointer devicePointer = new Pointer ( ) ; long reqMem = AllocationUtils . getRequiredMemory ( shape ) ; // FIXME: this is WRONG, and directly leads to memleak if ( reqMem < 1 ) reqMem = 1 ; Pointer pointer = nativeOps . mallocHost ( reqMem , 0 ) ; if ( pointer == null ) throw new RuntimeException ( "Can't allocate [HOST] memory: " + reqMem + "; threadId: " + Thread . currentThread ( ) . getId ( ) ) ; //                log.info("Host allocation, Thread id: {}, ReqMem: {}, Pointer: {}", Thread.currentThread().getId(), reqMem, pointer != null ? pointer.address() : null); Pointer hostPointer = new CudaPointer ( pointer ) ; PointersPair devicePointerInfo = new PointersPair ( ) ; devicePointerInfo . setDevicePointer ( new CudaPointer ( hostPointer , reqMem ) ) ; devicePointerInfo . setHostPointer ( new CudaPointer ( hostPointer , reqMem ) ) ; point . setPointers ( devicePointerInfo ) ; point . setAllocationStatus ( AllocationStatus . HOST ) ; return devicePointerInfo ; } case DEVICE : { // cudaMalloc call int deviceId = AtomicAllocator . getInstance ( ) . getDeviceId ( ) ; long reqMem = AllocationUtils . getRequiredMemory ( shape ) ; // FIXME: this is WRONG, and directly leads to memleak if ( reqMem < 1 ) reqMem = 1 ; //                if (CudaEnvironment.getInstance().getConfiguration().getDebugTriggered() == 119) //                    throw new RuntimeException("Device allocation happened"); Pointer pointer = nativeOps . mallocDevice ( reqMem , null , 0 ) ; //log.info("Device [{}] allocation, Thread id: {}, ReqMem: {}, Pointer: {}", AtomicAllocator.getInstance().getDeviceId(), Thread.currentThread().getId(), reqMem, pointer != null ? pointer.address() : null); if ( pointer == null ) return null ; //throw new RuntimeException("Can't allocate [DEVICE] memory!"); Pointer devicePointer = new CudaPointer ( pointer ) ; PointersPair devicePointerInfo = point . getPointers ( ) ; if ( devicePointerInfo == null ) devicePointerInfo = new PointersPair ( ) ; devicePointerInfo . setDevicePointer ( new CudaPointer ( devicePointer , reqMem ) ) ; point . setAllocationStatus ( AllocationStatus . DEVICE ) ; point . setDeviceId ( deviceId ) ; return devicePointerInfo ; } default : throw new IllegalStateException ( "Unsupported location for malloc: [" + location + "]" ) ; } }


Input: This method frees specific chunk of memory described by AllocationPoint passed in [CODESPLIT] @ Override public void free ( AllocationPoint point ) { switch ( point . getAllocationStatus ( ) ) { case HOST : { // cudaFreeHost call here // FIXME: it would be nice to get rid of typecasting here long reqMem = AllocationUtils . getRequiredMemory ( point . getShape ( ) ) ; //  log.info("Deallocating {} bytes on [HOST]", reqMem); NativeOps nativeOps = NativeOpsHolder . getInstance ( ) . getDeviceNativeOps ( ) ; long result = nativeOps . freeHost ( point . getPointers ( ) . getHostPointer ( ) ) ; //JCuda.cudaFreeHost(new Pointer(point.getPointers().getHostPointer())); if ( result == 0 ) throw new RuntimeException ( "Can't deallocate [HOST] memory..." ) ; } break ; case DEVICE : { // cudaFree call //JCuda.cudaFree(new Pointer(point.getPointers().getDevicePointer().address())); if ( point . isConstant ( ) ) return ; long reqMem = AllocationUtils . getRequiredMemory ( point . getShape ( ) ) ; //       log.info("Deallocating {} bytes on [DEVICE]", reqMem); NativeOps nativeOps = NativeOpsHolder . getInstance ( ) . getDeviceNativeOps ( ) ; long result = nativeOps . freeDevice ( point . getPointers ( ) . getDevicePointer ( ) , new CudaPointer ( 0 ) ) ; if ( result == 0 ) throw new RuntimeException ( "Can't deallocate [DEVICE] memory..." ) ; } break ; default : throw new IllegalStateException ( "Can't free memory on target [" + point . getAllocationStatus ( ) + "]" ) ; } }


Input: Gets feature specific learning rates Adagrad keeps a history of gradients being passed in . Note that each gradient passed in becomes adapted over time hence the opName adagrad [CODESPLIT] @ Override public void applyUpdater ( INDArray gradient , int iteration , int epoch ) { if ( historicalGradient == null ) throw new IllegalStateException ( "Updater has not been initialized with view state" ) ; double learningRate = config . getLearningRate ( iteration , epoch ) ; double epsilon = config . getEpsilon ( ) ; historicalGradient . addi ( gradient . mul ( gradient ) ) ; INDArray sqrtHistory = sqrt ( historicalGradient . dup ( gradientReshapeOrder ) , false ) . addi ( epsilon ) ; // lr * gradient / (sqrt(sumSquaredGradients) + epsilon) gradient . muli ( sqrtHistory . rdivi ( learningRate ) ) ; }


Input: This method makes sure HOST memory contains latest data from GPU [CODESPLIT] @ Override public void synchronizeToHost ( AllocationPoint point ) { if ( ! point . isConstant ( ) && point . isEnqueued ( ) ) { waitTillFinished ( point ) ; } super . synchronizeToHost ( point ) ; }


Input: Create from a matrix . The rows are the indices The columns are the individual element in each ndarrayindex [CODESPLIT] public static INDArrayIndex [ ] create ( INDArray index ) { if ( index . isMatrix ( ) ) { if ( index . rows ( ) > Integer . MAX_VALUE ) throw new ND4JArraySizeException ( ) ; NDArrayIndex [ ] ret = new NDArrayIndex [ ( int ) index . rows ( ) ] ; for ( int i = 0 ; i < index . rows ( ) ; i ++ ) { INDArray row = index . getRow ( i ) ; val nums = new long [ ( int ) index . getRow ( i ) . columns ( ) ] ; for ( int j = 0 ; j < row . columns ( ) ; j ++ ) { nums [ j ] = ( int ) row . getFloat ( j ) ; } NDArrayIndex idx = new NDArrayIndex ( nums ) ; ret [ i ] = idx ; } return ret ; } else if ( index . isVector ( ) ) { long [ ] indices = NDArrayUtil . toLongs ( index ) ; return new NDArrayIndex [ ] { new NDArrayIndex ( indices ) } ; } throw new IllegalArgumentException ( "Passed in ndarray must be a matrix or a vector" ) ; }


Input: Broadcast add op . See : { [CODESPLIT] public static INDArray add ( INDArray x , INDArray y , INDArray z , int ... dimensions ) { if ( dimensions == null ) { Preconditions . checkArgument ( Arrays . equals ( x . shape ( ) , y . shape ( ) ) , getFormattedShapeErrorMessageXy ( x , y ) ) ; Preconditions . checkArgument ( Arrays . equals ( x . shape ( ) , z . shape ( ) ) , getFormattedShapeErrorMessageXResult ( x , z ) ) ; return Nd4j . getExecutioner ( ) . execAndReturn ( new OldAddOp ( x , y , z ) ) ; } return Nd4j . getExecutioner ( ) . execAndReturn ( new BroadcastAddOp ( x , y , z , dimensions ) ) ; }


Input: Broadcast copy op . See : { [CODESPLIT] public static INDArray copy ( INDArray x , INDArray y , INDArray z , int ... dimensions ) { if ( dimensions == null ) { Preconditions . checkArgument ( Arrays . equals ( x . shape ( ) , y . shape ( ) ) , getFormattedShapeErrorMessageXy ( x , y ) ) ; Preconditions . checkArgument ( Arrays . equals ( x . shape ( ) , z . shape ( ) ) , getFormattedShapeErrorMessageXResult ( x , z ) ) ; return Nd4j . getExecutioner ( ) . execAndReturn ( new CopyOp ( x , y , z ) ) ; } return Nd4j . getExecutioner ( ) . execAndReturn ( new BroadcastCopyOp ( x , y , z , dimensions ) ) ; }


Input: Broadcast divide op . See : { [CODESPLIT] public static INDArray div ( INDArray x , INDArray y , INDArray z , int ... dimensions ) { if ( dimensions == null ) { Preconditions . checkArgument ( Arrays . equals ( x . shape ( ) , y . shape ( ) ) , getFormattedShapeErrorMessageXy ( x , y ) ) ; Preconditions . checkArgument ( Arrays . equals ( x . shape ( ) , z . shape ( ) ) , getFormattedShapeErrorMessageXResult ( x , z ) ) ; return Nd4j . getExecutioner ( ) . execAndReturn ( new OldDivOp ( x , y , z ) ) ; } return Nd4j . getExecutioner ( ) . execAndReturn ( new BroadcastDivOp ( x , y , z , dimensions ) ) ; }


Input: Broadcast equal to op . See : { [CODESPLIT] public static INDArray eq ( INDArray x , INDArray y , INDArray z , int ... dimensions ) { if ( dimensions == null ) { Preconditions . checkArgument ( Arrays . equals ( x . shape ( ) , y . shape ( ) ) , getFormattedShapeErrorMessageXy ( x , y ) ) ; Preconditions . checkArgument ( Arrays . equals ( x . shape ( ) , z . shape ( ) ) , getFormattedShapeErrorMessageXResult ( x , z ) ) ; return Nd4j . getExecutioner ( ) . execAndReturn ( new OldEqualTo ( x , y , z , x . length ( ) ) ) ; } return Nd4j . getExecutioner ( ) . execAndReturn ( new BroadcastEqualTo ( x , y , z , dimensions ) ) ; }


Input: Broadcast greater than op . See : { [CODESPLIT] public static INDArray gt ( INDArray x , INDArray y , INDArray z , int ... dimensions ) { if ( dimensions == null ) { Preconditions . checkArgument ( Arrays . equals ( x . shape ( ) , y . shape ( ) ) , getFormattedShapeErrorMessageXy ( x , y ) ) ; Preconditions . checkArgument ( Arrays . equals ( x . shape ( ) , z . shape ( ) ) , getFormattedShapeErrorMessageXResult ( x , z ) ) ; return Nd4j . getExecutioner ( ) . execAndReturn ( new OldGreaterThan ( x , y , z , x . length ( ) ) ) ; } return Nd4j . getExecutioner ( ) . execAndReturn ( new BroadcastGreaterThan ( x , y , z , dimensions ) ) ; }


Input: Broadcast greater than or equal to op . See : { [CODESPLIT] public static INDArray gte ( INDArray x , INDArray y , INDArray z , int ... dimensions ) { if ( dimensions == null ) { Preconditions . checkArgument ( Arrays . equals ( x . shape ( ) , y . shape ( ) ) , getFormattedShapeErrorMessageXy ( x , y ) ) ; Preconditions . checkArgument ( Arrays . equals ( x . shape ( ) , z . shape ( ) ) , getFormattedShapeErrorMessageXResult ( x , z ) ) ; return Nd4j . getExecutioner ( ) . execAndReturn ( new OldGreaterThanOrEqual ( x , y , z , x . length ( ) ) ) ; } return Nd4j . getExecutioner ( ) . execAndReturn ( new BroadcastGreaterThanOrEqual ( x , y , z , dimensions ) ) ; }


Input: Broadcast less than op . See : { [CODESPLIT] public static INDArray lt ( INDArray x , INDArray y , INDArray z , int ... dimensions ) { if ( dimensions == null ) { Preconditions . checkArgument ( Arrays . equals ( x . shape ( ) , y . shape ( ) ) , getFormattedShapeErrorMessageXy ( x , y ) ) ; Preconditions . checkArgument ( Arrays . equals ( x . shape ( ) , z . shape ( ) ) , getFormattedShapeErrorMessageXResult ( x , z ) ) ; return Nd4j . getExecutioner ( ) . execAndReturn ( new OldLessThan ( x , y , z , x . length ( ) ) ) ; } return Nd4j . getExecutioner ( ) . execAndReturn ( new BroadcastLessThan ( x , y , z , dimensions ) ) ; }


Input: Broadcast less than or equal to op . See : { [CODESPLIT] public static INDArray lte ( INDArray x , INDArray y , INDArray z , int ... dimensions ) { if ( dimensions == null ) { Preconditions . checkArgument ( Arrays . equals ( x . shape ( ) , y . shape ( ) ) , getFormattedShapeErrorMessageXy ( x , y ) ) ; Preconditions . checkArgument ( Arrays . equals ( x . shape ( ) , z . shape ( ) ) , getFormattedShapeErrorMessageXResult ( x , z ) ) ; return Nd4j . getExecutioner ( ) . execAndReturn ( new OldLessThanOrEqual ( x , y , z , x . length ( ) ) ) ; } return Nd4j . getExecutioner ( ) . execAndReturn ( new BroadcastLessThanOrEqual ( x , y , z , dimensions ) ) ; }


Input: Broadcast not equal to op . See : { [CODESPLIT] public static INDArray neq ( INDArray x , INDArray y , INDArray z , int ... dimensions ) { if ( dimensions == null ) { Preconditions . checkArgument ( Arrays . equals ( x . shape ( ) , y . shape ( ) ) , getFormattedShapeErrorMessageXy ( x , y ) ) ; Preconditions . checkArgument ( Arrays . equals ( x . shape ( ) , z . shape ( ) ) , getFormattedShapeErrorMessageXResult ( x , z ) ) ; return Nd4j . getExecutioner ( ) . execAndReturn ( new OldNotEqualTo ( x , y , z , x . length ( ) ) ) ; } return Nd4j . getExecutioner ( ) . execAndReturn ( new BroadcastNotEqual ( x , y , z , dimensions ) ) ; }


Input: Broadcast reverse division op . See : { [CODESPLIT] public static INDArray rdiv ( INDArray x , INDArray y , INDArray z , int ... dimensions ) { if ( dimensions == null ) { Preconditions . checkArgument ( Arrays . equals ( x . shape ( ) , y . shape ( ) ) , getFormattedShapeErrorMessageXy ( x , y ) ) ; Preconditions . checkArgument ( Arrays . equals ( x . shape ( ) , z . shape ( ) ) , getFormattedShapeErrorMessageXResult ( x , z ) ) ; return Nd4j . getExecutioner ( ) . execAndReturn ( new OldRDivOp ( x , y , z , x . length ( ) ) ) ; } return Nd4j . getExecutioner ( ) . execAndReturn ( new BroadcastRDivOp ( x , y , z , dimensions ) ) ; }


Input: Broadcast reverse subtraction op . See : { [CODESPLIT] public static INDArray rsub ( INDArray x , INDArray y , INDArray z , int ... dimensions ) { if ( dimensions == null ) { Preconditions . checkArgument ( Arrays . equals ( x . shape ( ) , y . shape ( ) ) , getFormattedShapeErrorMessageXy ( x , y ) ) ; Preconditions . checkArgument ( Arrays . equals ( x . shape ( ) , z . shape ( ) ) , getFormattedShapeErrorMessageXResult ( x , z ) ) ; return Nd4j . getExecutioner ( ) . execAndReturn ( new OldSubOp ( x , y , z , x . length ( ) ) ) ; } return Nd4j . getExecutioner ( ) . execAndReturn ( new BroadcastRSubOp ( x , y , z , dimensions ) ) ; }


Input: Broadcast max op . See : { [CODESPLIT] public static INDArray max ( INDArray x , INDArray y , INDArray z , int ... dimensions ) { if ( dimensions == null ) { Preconditions . checkArgument ( Arrays . equals ( x . shape ( ) , y . shape ( ) ) , getFormattedShapeErrorMessageXy ( x , y ) ) ; Preconditions . checkArgument ( Arrays . equals ( x . shape ( ) , z . shape ( ) ) , getFormattedShapeErrorMessageXResult ( x , z ) ) ; return Nd4j . getExecutioner ( ) . execAndReturn ( new OldMax ( x , y , z , x . length ( ) ) ) ; } return Nd4j . getExecutioner ( ) . execAndReturn ( new BroadcastMax ( x , y , z , dimensions ) ) ; }


Input: Broadcast min op . See : { [CODESPLIT] public static INDArray min ( INDArray x , INDArray y , INDArray z , int ... dimensions ) { if ( dimensions == null ) { Preconditions . checkArgument ( Arrays . equals ( x . shape ( ) , y . shape ( ) ) , getFormattedShapeErrorMessageXy ( x , y ) ) ; Preconditions . checkArgument ( Arrays . equals ( x . shape ( ) , z . shape ( ) ) , getFormattedShapeErrorMessageXResult ( x , z ) ) ; return Nd4j . getExecutioner ( ) . execAndReturn ( new OldMin ( x , y , z , x . length ( ) ) ) ; } return Nd4j . getExecutioner ( ) . execAndReturn ( new BroadcastMin ( x , y , z , dimensions ) ) ; }


Input: Broadcast absolute max op . See : { [CODESPLIT] public static INDArray amax ( INDArray x , INDArray y , INDArray z , int ... dimensions ) { if ( dimensions == null ) { Preconditions . checkArgument ( Arrays . equals ( x . shape ( ) , y . shape ( ) ) , getFormattedShapeErrorMessageXy ( x , y ) ) ; Preconditions . checkArgument ( Arrays . equals ( x . shape ( ) , z . shape ( ) ) , getFormattedShapeErrorMessageXResult ( x , z ) ) ; return Nd4j . getExecutioner ( ) . execAndReturn ( new AMax ( x , y , z , x . length ( ) ) ) . z ( ) ; } return Nd4j . getExecutioner ( ) . execAndReturn ( new BroadcastAMax ( x , y , z , dimensions ) ) ; }


Input: Broadcast absolute min op . See : { [CODESPLIT] public static INDArray amin ( INDArray x , INDArray y , INDArray z , int ... dimensions ) { if ( dimensions == null ) { Preconditions . checkArgument ( Arrays . equals ( x . shape ( ) , y . shape ( ) ) , getFormattedShapeErrorMessageXy ( x , y ) ) ; Preconditions . checkArgument ( Arrays . equals ( x . shape ( ) , z . shape ( ) ) , getFormattedShapeErrorMessageXResult ( x , z ) ) ; return Nd4j . getExecutioner ( ) . execAndReturn ( new AMin ( x , y , z , x . length ( ) ) ) . z ( ) ; } return Nd4j . getExecutioner ( ) . execAndReturn ( new BroadcastAMin ( x , y , z , dimensions ) ) ; }


Input: Returns the properties for a given function [CODESPLIT] public Map < String , Object > propertiesForFunction ( ) { val fields = DifferentialFunctionClassHolder . getInstance ( ) . getFieldsForFunction ( this ) ; Map < String , Object > ret = new LinkedHashMap <> ( ) ; for ( val entry : fields . entrySet ( ) ) { try { ret . put ( entry . getKey ( ) , fields . get ( entry . getKey ( ) ) . get ( this ) ) ; } catch ( IllegalAccessException e ) { e . printStackTrace ( ) ; } } return ret ; }


Input: Returns true if this function has place holder inputs [CODESPLIT] public boolean hasPlaceHolderInputs ( ) { val args = args ( ) ; for ( val arg : args ) if ( sameDiff . hasPlaceHolderVariables ( arg ( ) . getVarName ( ) ) ) return true ; return false ; }


Input: Perform automatic differentiation wrt the input variables [CODESPLIT] public List < SDVariable > diff ( List < SDVariable > i_v1 ) { List < SDVariable > vals = doDiff ( i_v1 ) ; if ( vals == null ) { throw new IllegalStateException ( "Error executing diff operation: doDiff returned null for op: " + this . opName ( ) ) ; } val outputVars = args ( ) ; for ( int i = 0 ; i < vals . size ( ) ; i ++ ) { SDVariable var = outputVars [ i ] ; SDVariable grad = var . getGradient ( ) ; if ( grad != null ) { SDVariable gradVar = f ( ) . add ( grad , vals . get ( i ) ) ; try { vals . set ( i , gradVar ) ; } catch ( UnsupportedOperationException e ) { throw new UnsupportedOperationException ( "Use a mutable list when returning values from " + this . getClass ( ) . getSimpleName ( ) + ".doDiff (e.g. Arrays.asList instead of Collections.singletonList)" , e ) ; } sameDiff . setGradientForVariableName ( var . getVarName ( ) , gradVar ) ; } else { SDVariable gradVar = vals . get ( i ) ; sameDiff . updateVariableNameAndReference ( gradVar , var . getVarName ( ) + "-grad" ) ; sameDiff . setGradientForVariableName ( var . getVarName ( ) , gradVar ) ; sameDiff . setForwardVariableForVarName ( gradVar . getVarName ( ) , var ) ; } } return vals ; }


Input: The offsets ( begin index ) for each index [CODESPLIT] public static long [ ] offsets ( long [ ] shape , INDArrayIndex ... indices ) { //offset of zero for every new axes long [ ] ret = new long [ shape . length ] ; if ( indices . length == shape . length ) { for ( int i = 0 ; i < indices . length ; i ++ ) { if ( indices [ i ] instanceof NDArrayIndexEmpty ) ret [ i ] = 0 ; else { ret [ i ] = indices [ i ] . offset ( ) ; } } if ( ret . length == 1 ) { ret = new long [ ] { ret [ 0 ] , 0 } ; } } else { int numPoints = NDArrayIndex . numPoints ( indices ) ; if ( numPoints > 0 ) { List < Long > nonZeros = new ArrayList <> ( ) ; for ( int i = 0 ; i < indices . length ; i ++ ) if ( indices [ i ] . offset ( ) > 0 ) nonZeros . add ( indices [ i ] . offset ( ) ) ; if ( nonZeros . size ( ) > shape . length ) throw new IllegalStateException ( "Non zeros greater than shape unable to continue" ) ; else { //push all zeros to the back for ( int i = 0 ; i < nonZeros . size ( ) ; i ++ ) ret [ i ] = nonZeros . get ( i ) ; } } else { int shapeIndex = 0 ; for ( int i = 0 ; i < indices . length ; i ++ ) { if ( indices [ i ] instanceof NDArrayIndexEmpty ) ret [ i ] = 0 ; else { ret [ i ] = indices [ shapeIndex ++ ] . offset ( ) ; } } } if ( ret . length == 1 ) { ret = new long [ ] { ret [ 0 ] , 0 } ; } } return ret ; }


Input: Format the given ndarray as a string [CODESPLIT] public String format ( INDArray arr , boolean summarize ) { this . scientificFormat = "0." ; int addPrecision = this . precision ; while ( addPrecision > 0 ) { this . scientificFormat += "#" ; addPrecision -= 1 ; } this . scientificFormat = this . scientificFormat + "E0" ; if ( this . scientificFormat . length ( ) + 2 > this . padding ) this . padding = this . scientificFormat . length ( ) + 2 ; this . maxToPrintWithoutSwitching = Math . pow ( 10 , this . precision ) ; this . minToPrintWithoutSwitching = 1.0 / ( this . maxToPrintWithoutSwitching ) ; if ( summarize && arr . length ( ) > 1000 ) return format ( arr , 0 , true ) ; return format ( arr , 0 , false ) ; }


Input: This method converts given TF [CODESPLIT] @ Override public SameDiff importGraph ( GRAPH_TYPE tfGraph ) { SameDiff diff = SameDiff . create ( ) ; ImportState < GRAPH_TYPE , TENSOR_TYPE > importState = new ImportState <> ( ) ; importState . setSameDiff ( diff ) ; importState . setGraph ( tfGraph ) ; val variablesForGraph = variablesForGraph ( tfGraph ) ; importState . setVariables ( variablesForGraph ) ; //map the names of the nodes while accumulating the vertex ids //for each variable for ( Map . Entry < String , TENSOR_TYPE > entry : variablesForGraph . entrySet ( ) ) { if ( dataTypeForTensor ( entry . getValue ( ) ) == DataBuffer . Type . UNKNOWN ) { val var = importState . getSameDiff ( ) . var ( entry . getKey ( ) , null , new ZeroInitScheme ( ' ' ) ) ; //mark as place holder for validating resolution later. if ( isPlaceHolder ( entry . getValue ( ) ) ) { importState . getSameDiff ( ) . addAsPlaceHolder ( var . getVarName ( ) ) ; if ( var . getShape ( ) != null ) importState . getSameDiff ( ) . setOriginalPlaceHolderShape ( var . getVarName ( ) , var . getShape ( ) ) ; } continue ; } val arr = getNDArrayFromTensor ( entry . getKey ( ) , entry . getValue ( ) , tfGraph ) ; if ( arr != null ) { val var = importState . getSameDiff ( ) . var ( entry . getKey ( ) , arr ) ; //ensure the array is made available for later processing diff . associateArrayWithVariable ( arr , var ) ; } else if ( getShapeFromTensor ( entry . getValue ( ) ) == null ) { val var = importState . getSameDiff ( ) . var ( entry . getKey ( ) , null , new ZeroInitScheme ( ' ' ) ) ; //mark as place holder for validating resolution later. //note that this vertex id can still be a place holder //with a -1 shape. Just because a shape is "known" doesn't mean //that it isn't  a place holder. if ( isPlaceHolder ( entry . getValue ( ) ) ) { val originalShape = getShapeFromTensor ( entry . getValue ( ) ) ; importState . getSameDiff ( ) . addAsPlaceHolder ( var . getVarName ( ) ) ; if ( var . getShape ( ) != null ) importState . getSameDiff ( ) . setOriginalPlaceHolderShape ( var . getVarName ( ) , originalShape ) ; } } else { val originalShape = getShapeFromTensor ( entry . getValue ( ) ) ; val var = importState . getSameDiff ( ) . var ( entry . getKey ( ) , originalShape ) ; //mark as place holder for validating resolution later. //note that this vertex id can still be a place holder //with a -1 shape. Just because a shape is "known" doesn't mean //that it isn't  a place holder. if ( isPlaceHolder ( entry . getValue ( ) ) ) { importState . getSameDiff ( ) . addAsPlaceHolder ( var . getVarName ( ) ) ; importState . getSameDiff ( ) . setOriginalPlaceHolderShape ( var . getVarName ( ) , originalShape ) ; } } } //setup vertex ids for  names //handle mapping vertex ids properly val tfNodesList = getNodeList ( tfGraph ) ; for ( NODE_TYPE tfNode : tfNodesList ) { if ( ! opsToIgnore ( ) . contains ( getOpType ( tfNode ) ) || isOpIgnoreException ( tfNode ) ) mapNodeType ( tfNode , importState ) ; } return diff ; }


Input: Convert an ndarray to a blob [CODESPLIT] @ Override public Blob convert ( IComplexNDArray toConvert ) throws IOException , SQLException { ByteArrayOutputStream bos = new ByteArrayOutputStream ( ) ; DataOutputStream dos = new DataOutputStream ( bos ) ; Nd4j . writeComplex ( toConvert , dos ) ; byte [ ] bytes = bos . toByteArray ( ) ; Connection c = dataSource . getConnection ( ) ; Blob b = c . createBlob ( ) ; b . setBytes ( 1 , bytes ) ; return b ; }


Input: Load a complex ndarray from a blob [CODESPLIT] @ Override public IComplexNDArray loadComplex ( Blob blob ) throws SQLException , IOException { DataInputStream dis = new DataInputStream ( blob . getBinaryStream ( ) ) ; return Nd4j . readComplex ( dis ) ; }


Input: Save the ndarray [CODESPLIT] @ Override public void save ( IComplexNDArray save , String id ) throws IOException , SQLException { doSave ( save , id ) ; }


Input: Copy real numbers to arr [CODESPLIT] protected void copyRealTo ( INDArray arr ) { INDArray linear = arr . linearView ( ) ; IComplexNDArray thisLinear = linearView ( ) ; if ( arr . isScalar ( ) ) arr . putScalar ( 0 , getReal ( 0 ) ) ; else for ( int i = 0 ; i < linear . length ( ) ; i ++ ) { arr . putScalar ( i , thisLinear . getReal ( i ) ) ; } }


Input: Copy imaginary numbers to the given ndarray [CODESPLIT] protected void copyImagTo ( INDArray arr ) { INDArray linear = arr . linearView ( ) ; IComplexNDArray thisLinear = linearView ( ) ; if ( arr . isScalar ( ) ) arr . putScalar ( 0 , getReal ( 0 ) ) ; else for ( int i = 0 ; i < linear . length ( ) ; i ++ ) { arr . putScalar ( i , thisLinear . getImag ( i ) ) ; } }


Input: Returns an ndarray with 1 if the element is epsilon equals [CODESPLIT] @ Override public IComplexNDArray epsi ( Number other ) { IComplexNDArray linear = linearView ( ) ; double otherVal = other . doubleValue ( ) ; for ( int i = 0 ; i < linearView ( ) . length ( ) ; i ++ ) { IComplexNumber n = linear . getComplex ( i ) ; double real = n . realComponent ( ) . doubleValue ( ) ; double diff = Math . abs ( real - otherVal ) ; if ( diff <= Nd4j . EPS_THRESHOLD ) linear . putScalar ( i , Nd4j . createDouble ( 1 , 0 ) ) ; else linear . putScalar ( i , Nd4j . createDouble ( 0 , 0 ) ) ; } return this ; }


Input: In place epsilon equals than comparison : If the given number is less than the comparison number the item is 0 otherwise 1 [CODESPLIT] @ Override public IComplexNDArray epsi ( INDArray other ) { IComplexNDArray linear = linearView ( ) ; if ( other instanceof IComplexNDArray ) { IComplexNDArray otherComplex = ( IComplexNDArray ) other ; IComplexNDArray otherComplexLinear = otherComplex . linearView ( ) ; for ( int i = 0 ; i < linearView ( ) . length ( ) ; i ++ ) { IComplexNumber n = linear . getComplex ( i ) ; IComplexNumber otherComplexNumber = otherComplexLinear . getComplex ( i ) ; double real = n . absoluteValue ( ) . doubleValue ( ) ; double otherAbs = otherComplexNumber . absoluteValue ( ) . doubleValue ( ) ; double diff = Math . abs ( real - otherAbs ) ; if ( diff <= Nd4j . EPS_THRESHOLD ) linear . putScalar ( i , Nd4j . createDouble ( 1 , 0 ) ) ; else linear . putScalar ( i , Nd4j . createDouble ( 0 , 0 ) ) ; } } return this ; }


Input: Returns the squared ( Euclidean ) distance . [CODESPLIT] @ Override public double squaredDistance ( INDArray other ) { double sd = 0.0 ; if ( other instanceof IComplexNDArray ) { IComplexNDArray n = ( IComplexNDArray ) other ; IComplexNDArray nLinear = n . linearView ( ) ; for ( int i = 0 ; i < length ( ) ; i ++ ) { IComplexNumber diff = linearView ( ) . getComplex ( i ) . sub ( nLinear . getComplex ( i ) ) ; double d = diff . absoluteValue ( ) . doubleValue ( ) ; sd += d * d ; } return sd ; } for ( int i = 0 ; i < length ( ) ; i ++ ) { INDArray linear = other . linearView ( ) ; IComplexNumber diff = linearView ( ) . getComplex ( i ) . sub ( linear . getDouble ( i ) ) ; double d = diff . absoluteValue ( ) . doubleValue ( ) ; sd += d * d ; } return sd ; }


Input: Returns the ( 1 - norm ) distance . [CODESPLIT] @ Override public double distance1 ( INDArray other ) { float d = 0.0f ; if ( other instanceof IComplexNDArray ) { IComplexNDArray n2 = ( IComplexNDArray ) other ; IComplexNDArray n2Linear = n2 . linearView ( ) ; for ( int i = 0 ; i < length ( ) ; i ++ ) { IComplexNumber n = getComplex ( i ) . sub ( n2Linear . getComplex ( i ) ) ; d += n . absoluteValue ( ) . doubleValue ( ) ; } return d ; } INDArray linear = other . linearView ( ) ; for ( int i = 0 ; i < length ( ) ; i ++ ) { IComplexNumber n = linearView ( ) . getComplex ( i ) . sub ( linear . getDouble ( i ) ) ; d += n . absoluteValue ( ) . doubleValue ( ) ; } return d ; }


Input: Inserts the element at the specified index [CODESPLIT] @ Override public IComplexNDArray put ( int i , int j , Number element ) { return ( IComplexNDArray ) super . put ( i , j , Nd4j . scalar ( element ) ) ; }


Input: Assigns the given matrix ( put ) to the specified slice [CODESPLIT] @ Override public IComplexNDArray putSlice ( int slice , IComplexNDArray put ) { if ( isScalar ( ) ) { assert put . isScalar ( ) : "Invalid dimension. Can only insert a scalar in to another scalar" ; put ( 0 , put . getScalar ( 0 ) ) ; return this ; } else if ( isVector ( ) ) { assert put . isScalar ( ) || put . isVector ( ) && put . length ( ) == length ( ) : "Invalid dimension on insertion. Can only insert scalars input vectors" ; if ( put . isScalar ( ) ) putScalar ( slice , put . getComplex ( 0 ) ) ; else for ( int i = 0 ; i < length ( ) ; i ++ ) putScalar ( i , put . getComplex ( i ) ) ; return this ; } assertSlice ( put , slice ) ; IComplexNDArray view = slice ( slice ) ; if ( put . length ( ) == 1 ) putScalar ( slice , put . getComplex ( 0 ) ) ; else if ( put . isVector ( ) ) for ( int i = 0 ; i < put . length ( ) ; i ++ ) view . putScalar ( i , put . getComplex ( i ) ) ; else { assert Shape . shapeEquals ( view . shape ( ) , put . shape ( ) ) ; IComplexNDArray linear = view . linearView ( ) ; IComplexNDArray putLinearView = put . linearView ( ) ; for ( int i = 0 ; i < linear . length ( ) ; i ++ ) { linear . putScalar ( i , putLinearView . getComplex ( i ) ) ; } } return this ; }


Input: Compute complex conj ( in - place ) . [CODESPLIT] @ Override public IComplexNDArray conji ( ) { IComplexNDArray reshaped = linearView ( ) ; IComplexDouble c = Nd4j . createDouble ( 0.0 , 0 ) ; for ( int i = 0 ; i < length ( ) ; i ++ ) { IComplexNumber conj = reshaped . getComplex ( i , c ) . conj ( ) ; reshaped . putScalar ( i , conj ) ; } return this ; }


Input: Inserts the element at the specified index [CODESPLIT] @ Override public IComplexNDArray put ( int i , IComplexNDArray element ) { if ( element == null ) throw new IllegalArgumentException ( "Unable to insert null element" ) ; assert element . isScalar ( ) : "Unable to insert non scalar element" ; long idx = linearIndex ( i ) ; IComplexNumber n = element . getComplex ( 0 ) ; data . put ( idx , n . realComponent ( ) . doubleValue ( ) ) ; data . put ( idx + 1 , n . imaginaryComponent ( ) . doubleValue ( ) ) ; return this ; }


Input: Assigns the given matrix ( put ) to the specified slice [CODESPLIT] @ Override public IComplexNDArray putSlice ( int slice , INDArray put ) { return putSlice ( slice , Nd4j . createComplex ( put ) ) ; }


Input: Inserts the element at the specified index [CODESPLIT] @ Override public IComplexNDArray put ( int i , int j , INDArray element ) { return put ( new int [ ] { i , j } , element ) ; }


Input: Assign all of the elements in the given ndarray to this ndarray [CODESPLIT] @ Override public IComplexNDArray assign ( IComplexNDArray arr ) { if ( ! arr . isScalar ( ) ) LinAlgExceptions . assertSameLength ( this , arr ) ; IComplexNDArray linear = linearView ( ) ; IComplexNDArray otherLinear = arr . linearView ( ) ; for ( int i = 0 ; i < linear . length ( ) ; i ++ ) { linear . putScalar ( i , otherLinear . getComplex ( i ) ) ; } return this ; }


Input: Get whole rows from the passed indices . [CODESPLIT] @ Override public IComplexNDArray getRows ( int [ ] rindices ) { INDArray rows = Nd4j . create ( rindices . length , columns ( ) ) ; for ( int i = 0 ; i < rindices . length ; i ++ ) { rows . putRow ( i , getRow ( rindices [ i ] ) ) ; } return ( IComplexNDArray ) rows ; }


Input: Dimshuffle : an extension of permute that adds the ability to broadcast various dimensions . <p / > See theano for more examples . This will only accept integers and xs . <p / > An x indicates a dimension should be broadcasted rather than permuted . [CODESPLIT] @ Override public IComplexNDArray dimShuffle ( Object [ ] rearrange , int [ ] newOrder , boolean [ ] broadCastable ) { return ( IComplexNDArray ) super . dimShuffle ( rearrange , newOrder , broadCastable ) ; }


Input: Insert a row in to this array Will throw an exception if this ndarray is not a matrix [CODESPLIT] @ Override public IComplexNDArray putRow ( long row , INDArray toPut ) { return ( IComplexNDArray ) super . putRow ( row , toPut ) ; }


Input: Insert a column in to this array Will throw an exception if this ndarray is not a matrix [CODESPLIT] @ Override public IComplexNDArray putColumn ( int column , INDArray toPut ) { assert toPut . isVector ( ) && toPut . length ( ) == rows ( ) : "Illegal length for row " + toPut . length ( ) + " should have been " + columns ( ) ; IComplexNDArray r = getColumn ( column ) ; if ( toPut instanceof IComplexNDArray ) { IComplexNDArray putComplex = ( IComplexNDArray ) toPut ; for ( int i = 0 ; i < r . length ( ) ; i ++ ) { IComplexNumber n = putComplex . getComplex ( i ) ; r . putScalar ( i , n ) ; } } else { for ( int i = 0 ; i < r . length ( ) ; i ++ ) r . putScalar ( i , Nd4j . createDouble ( toPut . getDouble ( i ) , 0 ) ) ; } return this ; }


Input: Inserts the element at the specified index [CODESPLIT] @ Override public IComplexNDArray put ( int i , INDArray element ) { if ( element == null ) throw new IllegalArgumentException ( "Unable to insert null element" ) ; assert element . isScalar ( ) : "Unable to insert non scalar element" ; if ( element instanceof IComplexNDArray ) { IComplexNDArray n1 = ( IComplexNDArray ) element ; IComplexNumber n = n1 . getComplex ( 0 ) ; put ( i , n ) ; } else putScalar ( i , Nd4j . createDouble ( element . getDouble ( 0 ) , 0.0 ) ) ; return this ; }


Input: In place addition of a column vector [CODESPLIT] @ Override public IComplexNDArray diviColumnVector ( INDArray columnVector ) { for ( int i = 0 ; i < columns ( ) ; i ++ ) { getColumn ( i ) . divi ( columnVector . getScalar ( i ) ) ; } return this ; }


Input: In place addition of a column vector [CODESPLIT] @ Override public IComplexNDArray diviRowVector ( INDArray rowVector ) { for ( int i = 0 ; i < rows ( ) ; i ++ ) { getRow ( i ) . divi ( rowVector . getScalar ( i ) ) ; } return this ; }


Input: In place addition of a column vector [CODESPLIT] @ Override public IComplexNDArray muliColumnVector ( INDArray columnVector ) { for ( int i = 0 ; i < columns ( ) ; i ++ ) { getColumn ( i ) . muli ( columnVector . getScalar ( i ) ) ; } return this ; }


Input: In place addition of a column vector [CODESPLIT] @ Override public IComplexNDArray muliRowVector ( INDArray rowVector ) { for ( int i = 0 ; i < rows ( ) ; i ++ ) { getRow ( i ) . muli ( rowVector . getScalar ( i ) ) ; } return this ; }


Input: In place addition of a column vector [CODESPLIT] @ Override public IComplexNDArray subiColumnVector ( INDArray columnVector ) { for ( int i = 0 ; i < columns ( ) ; i ++ ) { getColumn ( i ) . subi ( columnVector . getScalar ( i ) ) ; } return this ; }


Input: In place addition of a column vector [CODESPLIT] @ Override public IComplexNDArray subiRowVector ( INDArray rowVector ) { for ( int i = 0 ; i < rows ( ) ; i ++ ) { getRow ( i ) . subi ( rowVector . getScalar ( i ) ) ; } return this ; }


Input: In place addition of a column vector [CODESPLIT] @ Override public IComplexNDArray addiColumnVector ( INDArray columnVector ) { for ( int i = 0 ; i < columns ( ) ; i ++ ) { getColumn ( i ) . addi ( columnVector . getScalar ( i ) ) ; } return this ; }


Input: In place addition of a column vector [CODESPLIT] @ Override public IComplexNDArray addiRowVector ( INDArray rowVector ) { for ( int i = 0 ; i < rows ( ) ; i ++ ) { getRow ( i ) . addi ( rowVector . getScalar ( i ) ) ; } return this ; }


Input: Perform an copy matrix multiplication [CODESPLIT] @ Override public IComplexNDArray mmul ( INDArray other , INDArray result ) { return dup ( ) . mmuli ( other , result ) ; }


Input: copy ( element wise ) division of two matrices [CODESPLIT] @ Override public IComplexNDArray div ( INDArray other , INDArray result ) { return dup ( ) . divi ( other , result ) ; }


Input: copy ( element wise ) multiplication of two matrices [CODESPLIT] @ Override public IComplexNDArray mul ( INDArray other , INDArray result ) { return dup ( ) . muli ( other , result ) ; }


Input: copy subtraction of two matrices [CODESPLIT] @ Override public IComplexNDArray sub ( INDArray other , INDArray result ) { return dup ( ) . subi ( other , result ) ; }


Input: copy addition of two matrices [CODESPLIT] @ Override public IComplexNDArray add ( INDArray other , INDArray result ) { return dup ( ) . addi ( other , result ) ; }


Input:
Perform an copy matrix multiplication [CODESPLIT] @ Override public IComplexNDArray mmuli ( INDArray other , INDArray result ) { IComplexNDArray otherArray = ( IComplexNDArray ) other ; IComplexNDArray resultArray = ( IComplexNDArray ) result ; if ( other . shape ( ) . length > 2 ) { for ( int i = 0 ; i < other . slices ( ) ; i ++ ) { resultArray . putSlice ( i , slice ( i ) . mmul ( otherArray . slice ( i ) ) ) ; } return resultArray ; } LinAlgExceptions . assertMultiplies ( this , other ) ; if ( other . isScalar ( ) ) { return muli ( otherArray . getComplex ( 0 ) , resultArray ) ; } if ( isScalar ( ) ) { return otherArray . muli ( getComplex ( 0 ) , resultArray ) ; } /* check sizes and resize if necessary */ //assertMultipliesWith(other); if ( result == this || result == other ) { /* actually, blas cannot do multiplications in-place. Therefore, we will fake by
             * allocating a temporary object on the side and copy the result later.
             */ IComplexNDArray temp = Nd4j . createComplex ( resultArray . shape ( ) ) ; if ( otherArray . columns ( ) == 1 ) { Nd4j . getBlasWrapper ( ) . level2 ( ) . gemv ( BlasBufferUtil . getCharForTranspose ( temp ) , BlasBufferUtil . getCharForTranspose ( this ) , Nd4j . UNIT , this , otherArray , Nd4j . ZERO , temp ) ; } else { Nd4j . getBlasWrapper ( ) . level3 ( ) . gemm ( BlasBufferUtil . getCharForTranspose ( temp ) , BlasBufferUtil . getCharForTranspose ( this ) , BlasBufferUtil . getCharForTranspose ( other ) , Nd4j . UNIT , this , otherArray , Nd4j . ZERO , temp ) ; } Nd4j . getBlasWrapper ( ) . copy ( temp , resultArray ) ; } else { if ( otherArray . columns ( ) == 1 ) { Nd4j . getBlasWrapper ( ) . level2 ( ) . gemv ( BlasBufferUtil . getCharForTranspose ( resultArray ) , BlasBufferUtil . getCharForTranspose ( this ) , Nd4j . UNIT , this , otherArray , Nd4j . ZERO , resultArray ) ; } else { Nd4j . getBlasWrapper ( ) . level3 ( ) . gemm ( BlasBufferUtil . getCharForTranspose ( resultArray ) , BlasBufferUtil . getCharForTranspose ( this ) , BlasBufferUtil . getCharForTranspose ( other ) , Nd4j . UNIT , this , otherArray , Nd4j . ZERO , resultArray ) ; } } return resultArray ; }


Input: in place ( element wise ) multiplication of two matrices [CODESPLIT] @ Override public IComplexNDArray muli ( INDArray other , INDArray result ) { IComplexNDArray cOther = ( IComplexNDArray ) other ; IComplexNDArray cResult = ( IComplexNDArray ) result ; IComplexNDArray linear = linearView ( ) ; IComplexNDArray cOtherLinear = cOther . linearView ( ) ; IComplexNDArray cResultLinear = cResult . linearView ( ) ; if ( other . isScalar ( ) ) return muli ( cOther . getComplex ( 0 ) , result ) ; IComplexNumber c = Nd4j . createComplexNumber ( 0 , 0 ) ; IComplexNumber d = Nd4j . createComplexNumber ( 0 , 0 ) ; for ( int i = 0 ; i < length ( ) ; i ++ ) cResultLinear . putScalar ( i , linear . getComplex ( i , c ) . muli ( cOtherLinear . getComplex ( i , d ) ) ) ; return cResult ; }


Input: in place subtraction of two matrices [CODESPLIT] @ Override public IComplexNDArray subi ( INDArray other , INDArray result ) { IComplexNDArray cOther = ( IComplexNDArray ) other ; IComplexNDArray cResult = ( IComplexNDArray ) result ; if ( other . isScalar ( ) ) return subi ( cOther . getComplex ( 0 ) , result ) ; if ( result == this ) Nd4j . getBlasWrapper ( ) . axpy ( Nd4j . NEG_UNIT , cOther , cResult ) ; else if ( result == other ) { if ( data . dataType ( ) == ( DataBuffer . Type . DOUBLE ) ) { Nd4j . getBlasWrapper ( ) . scal ( Nd4j . NEG_UNIT . asDouble ( ) , cResult ) ; Nd4j . getBlasWrapper ( ) . axpy ( Nd4j . UNIT , this , cResult ) ; } else { Nd4j . getBlasWrapper ( ) . scal ( Nd4j . NEG_UNIT . asFloat ( ) , cResult ) ; Nd4j . getBlasWrapper ( ) . axpy ( Nd4j . UNIT , this , cResult ) ; } } else { Nd4j . getBlasWrapper ( ) . copy ( this , result ) ; Nd4j . getBlasWrapper ( ) . axpy ( Nd4j . NEG_UNIT , cOther , cResult ) ; } return cResult ; }


Input: in place addition of two matrices [CODESPLIT] @ Override public IComplexNDArray addi ( INDArray other , INDArray result ) { IComplexNDArray cOther = ( IComplexNDArray ) other ; IComplexNDArray cResult = ( IComplexNDArray ) result ; if ( cOther . isScalar ( ) ) { return cResult . addi ( cOther . getComplex ( 0 ) , result ) ; } if ( isScalar ( ) ) { return cOther . addi ( getComplex ( 0 ) , result ) ; } if ( result == this ) { Nd4j . getBlasWrapper ( ) . axpy ( Nd4j . UNIT , cOther , cResult ) ; } else if ( result == other ) { Nd4j . getBlasWrapper ( ) . axpy ( Nd4j . UNIT , this , cResult ) ; } else { INDArray resultLinear = result . linearView ( ) ; INDArray otherLinear = other . linearView ( ) ; INDArray linear = linearView ( ) ; for ( int i = 0 ; i < resultLinear . length ( ) ; i ++ ) { resultLinear . putScalar ( i , otherLinear . getDouble ( i ) + linear . getDouble ( i ) ) ; } } return ( IComplexNDArray ) result ; }


Input: Set the value of the ndarray to the specified value [CODESPLIT] @ Override public IComplexNDArray assign ( Number value ) { IComplexNDArray one = linearView ( ) ; for ( int i = 0 ; i < one . length ( ) ; i ++ ) one . putScalar ( i , Nd4j . createDouble ( value . doubleValue ( ) , 0 ) ) ; return this ; }


Input: Reverse division [CODESPLIT] @ Override public IComplexNDArray rdiv ( INDArray other , INDArray result ) { return dup ( ) . rdivi ( other , result ) ; }


Input: Reverse division ( in - place ) [CODESPLIT] @ Override public IComplexNDArray rdivi ( INDArray other , INDArray result ) { return ( IComplexNDArray ) other . divi ( this , result ) ; }


Input: Reverse subtraction [CODESPLIT] @ Override public IComplexNDArray rsub ( INDArray other , INDArray result ) { return dup ( ) . rsubi ( other , result ) ; }


Input: Reverse subtraction ( in - place ) [CODESPLIT] @ Override public IComplexNDArray rsubi ( INDArray other , INDArray result ) { return ( IComplexNDArray ) other . subi ( this , result ) ; }


Input: Returns a scalar ( individual element ) of a scalar ndarray [CODESPLIT] @ Override public Object element ( ) { if ( ! isScalar ( ) ) throw new IllegalStateException ( "Unable to getScalar the element of a non scalar" ) ; long idx = linearIndex ( 0 ) ; return Nd4j . createDouble ( data . getDouble ( idx ) , data . getDouble ( idx + 1 ) ) ; }


Input: Flattens the array for linear indexing [CODESPLIT] @ Override public IComplexNDArray ravel ( ) { if ( length ( ) >= Integer . MAX_VALUE ) throw new IllegalArgumentException ( "length() can not be >= Integer.MAX_VALUE" ) ; IComplexNDArray ret = Nd4j . createComplex ( ( int ) length ( ) , ordering ( ) ) ; IComplexNDArray linear = linearView ( ) ; for ( int i = 0 ; i < length ( ) ; i ++ ) { ret . putScalar ( i , linear . getComplex ( i ) ) ; } return ret ; }


Input: Computes the eigenvalues of a general matrix . [CODESPLIT] public static IComplexNDArray eigenvalues ( INDArray A ) { assert A . rows ( ) == A . columns ( ) ; INDArray WR = Nd4j . create ( A . rows ( ) , A . rows ( ) ) ; INDArray WI = WR . dup ( ) ; Nd4j . getBlasWrapper ( ) . geev ( ' ' , ' ' , A . dup ( ) , WR , WI , dummy , dummy ) ; return Nd4j . createComplex ( WR , WI ) ; }


Input: Computes the eigenvalues and eigenvectors of a general matrix . <p / > For matlab users note the following from their documentation : The columns of V present eigenvectors of A . The diagonal matrix D contains eigenvalues . <p / > This is in reverse order of the matlab eig ( A ) call . [CODESPLIT] public static IComplexNDArray [ ] eigenvectors ( INDArray A ) { assert A . columns ( ) == A . rows ( ) ; // setting up result arrays INDArray WR = Nd4j . create ( A . rows ( ) ) ; INDArray WI = WR . dup ( ) ; INDArray VR = Nd4j . create ( A . rows ( ) , A . rows ( ) ) ; INDArray VL = Nd4j . create ( A . rows ( ) , A . rows ( ) ) ; Nd4j . getBlasWrapper ( ) . geev ( ' ' , ' ' , A . dup ( ) , WR , WI , VL , VR ) ; // transferring the result IComplexNDArray E = Nd4j . createComplex ( WR , WI ) ; IComplexNDArray V = Nd4j . createComplex ( ( int ) A . rows ( ) , ( int ) A . rows ( ) ) ; for ( int i = 0 ; i < A . rows ( ) ; i ++ ) { if ( E . getComplex ( i ) . isReal ( ) ) { IComplexNDArray column = Nd4j . createComplex ( VR . getColumn ( i ) ) ; V . putColumn ( i , column ) ; } else { IComplexNDArray v = Nd4j . createComplex ( VR . getColumn ( i ) , VR . getColumn ( i + 1 ) ) ; V . putColumn ( i , v ) ; V . putColumn ( i + 1 , v . conji ( ) ) ; i += 1 ; } } return new IComplexNDArray [ ] { Nd4j . diag ( E ) , V } ; }


Input: Compute generalized eigenvalues of the problem A x = L B x . The data will be unchanged no eigenvectors returned . [CODESPLIT] public static INDArray symmetricGeneralizedEigenvalues ( INDArray A , INDArray B ) { assert A . rows ( ) == A . columns ( ) ; assert B . rows ( ) == B . columns ( ) ; INDArray W = Nd4j . create ( A . rows ( ) ) ; A = InvertMatrix . invert ( B , false ) . mmuli ( A ) ; Nd4j . getBlasWrapper ( ) . syev ( ' ' , ' ' , A , W ) ; return W ; }


Input: { [CODESPLIT] public double probability ( int x ) { double ret ; if ( x < 0 || x > numberOfTrials ) { ret = 0.0 ; } else { ret = FastMath . exp ( SaddlePointExpansion . logBinomialProbability ( x , numberOfTrials , probabilityOfSuccess , 1.0 - probabilityOfSuccess ) ) ; } return ret ; }


Input: finds the element of a vector that has the largest absolute value . [CODESPLIT] @ Override public int iamax ( IComplexNDArray arr ) { if ( arr . data ( ) . dataType ( ) == DataBuffer . Type . DOUBLE ) return izamax ( arr . length ( ) , arr , BlasBufferUtil . getBlasStride ( arr ) ) ; return icamax ( arr . length ( ) , arr , BlasBufferUtil . getBlasStride ( arr ) ) ; }


Input: copy a vector to another vector . [CODESPLIT] @ Override public void copy ( IComplexNDArray x , IComplexNDArray y ) { if ( x . data ( ) . dataType ( ) == DataBuffer . Type . DOUBLE ) zcopy ( x . length ( ) , x , BlasBufferUtil . getBlasStride ( x ) , y , BlasBufferUtil . getBlasStride ( y ) ) ; else ccopy ( x . length ( ) , x , BlasBufferUtil . getBlasStride ( x ) , y , BlasBufferUtil . getBlasStride ( y ) ) ; }


Input: computes a vector - scalar product and adds the result to a vector . [CODESPLIT] @ Override public void axpy ( long n , double alpha , INDArray x , INDArray y ) { if ( Nd4j . getExecutioner ( ) . getProfilingMode ( ) == OpExecutioner . ProfilingMode . ALL ) OpProfiler . getInstance ( ) . processBlasCall ( false , x , y ) ; if ( x . isSparse ( ) && ! y . isSparse ( ) ) { Nd4j . getSparseBlasWrapper ( ) . level1 ( ) . axpy ( n , alpha , x , y ) ; } else if ( x . data ( ) . dataType ( ) == DataBuffer . Type . DOUBLE ) { DefaultOpExecutioner . validateDataType ( DataBuffer . Type . DOUBLE , x , y ) ; daxpy ( n , alpha , x , BlasBufferUtil . getBlasStride ( x ) , y , BlasBufferUtil . getBlasStride ( y ) ) ; } else if ( x . data ( ) . dataType ( ) == DataBuffer . Type . FLOAT ) { DefaultOpExecutioner . validateDataType ( DataBuffer . Type . FLOAT , x , y ) ; saxpy ( n , ( float ) alpha , x , BlasBufferUtil . getBlasStride ( x ) , y , BlasBufferUtil . getBlasStride ( y ) ) ; } else { DefaultOpExecutioner . validateDataType ( DataBuffer . Type . HALF , x , y ) ; haxpy ( n , ( float ) alpha , x , BlasBufferUtil . getBlasStride ( x ) , y , BlasBufferUtil . getBlasStride ( y ) ) ; } }


Input: computes a vector - scalar product and adds the result to a vector . [CODESPLIT] @ Override public void axpy ( long n , IComplexNumber alpha , IComplexNDArray x , IComplexNDArray y ) { if ( x . data ( ) . dataType ( ) == DataBuffer . Type . DOUBLE ) zaxpy ( n , alpha . asDouble ( ) , x , BlasBufferUtil . getBlasStride ( x ) , y , BlasBufferUtil . getBlasStride ( y ) ) ; else caxpy ( n , alpha . asFloat ( ) , x , BlasBufferUtil . getBlasStride ( x ) , y , BlasBufferUtil . getBlasStride ( y ) ) ; }


Input: computes a vector by a scalar product . [CODESPLIT] @ Override public void scal ( long N , IComplexNumber alpha , IComplexNDArray X ) { if ( X . data ( ) . dataType ( ) == DataBuffer . Type . DOUBLE ) zscal ( N , alpha . asDouble ( ) , X , BlasBufferUtil . getBlasStride ( X ) ) ; else cscal ( N , alpha . asFloat ( ) , X , BlasBufferUtil . getBlasStride ( X ) ) ; }


Input: Normalize a data array [CODESPLIT] @ Override public void preProcess ( INDArray array , INDArray maskArray , MinMaxStats stats ) { if ( array . rank ( ) <= 2 ) { array . subiRowVector ( stats . getLower ( ) ) ; array . diviRowVector ( stats . getRange ( ) ) ; } // if feature Rank is 3 (time series) samplesxfeaturesxtimesteps // if feature Rank is 4 (images) samplesxchannelsxrowsxcols // both cases operations should be carried out in dimension 1 else { Nd4j . getExecutioner ( ) . execAndReturn ( new BroadcastSubOp ( array , stats . getLower ( ) , array , 1 ) ) ; Nd4j . getExecutioner ( ) . execAndReturn ( new BroadcastDivOp ( array , stats . getRange ( ) , array , 1 ) ) ; } // Scale by target range array . muli ( maxRange - minRange ) ; // Add target range minimum values array . addi ( minRange ) ; if ( maskArray != null ) { DataSetUtil . setMaskedValuesToZero ( array , maskArray ) ; } }


Input: Denormalize a data array [CODESPLIT] @ Override public void revert ( INDArray array , INDArray maskArray , MinMaxStats stats ) { // Subtract target range minimum value array . subi ( minRange ) ; // Scale by target range array . divi ( maxRange - minRange ) ; if ( array . rank ( ) <= 2 ) { array . muliRowVector ( stats . getRange ( ) ) ; array . addiRowVector ( stats . getLower ( ) ) ; } else { Nd4j . getExecutioner ( ) . execAndReturn ( new BroadcastMulOp ( array , stats . getRange ( ) , array , 1 ) ) ; Nd4j . getExecutioner ( ) . execAndReturn ( new BroadcastAddOp ( array , stats . getLower ( ) , array , 1 ) ) ; } if ( maskArray != null ) { DataSetUtil . setMaskedValuesToZero ( array , maskArray ) ; } }


Input: Merge the specified labels and label mask arrays ( i . e . concatenate the examples ) [CODESPLIT] public static Pair < INDArray , INDArray > mergeLabels ( INDArray [ ] labelsToMerge , INDArray [ ] labelMasksToMerge ) { int rankFeatures = labelsToMerge [ 0 ] . rank ( ) ; switch ( rankFeatures ) { case 2 : return DataSetUtil . merge2d ( labelsToMerge , labelMasksToMerge ) ; case 3 : return DataSetUtil . mergeTimeSeries ( labelsToMerge , labelMasksToMerge ) ; case 4 : return DataSetUtil . merge4d ( labelsToMerge , labelMasksToMerge ) ; default : throw new ND4JIllegalStateException ( "Cannot merge examples: labels rank must be in range 2 to 4" + " inclusive. First example features shape: " + Arrays . toString ( labelsToMerge [ 0 ] . shape ( ) ) ) ; } }


Input: Merge the specified 2d arrays and masks . See { @link #mergeFeatures ( INDArray [] INDArray [] ) } and { @link #mergeLabels ( INDArray [] INDArray [] ) } [CODESPLIT] public static Pair < INDArray , INDArray > merge2d ( INDArray [ ] arrays , INDArray [ ] masks ) { long cols = arrays [ 0 ] . columns ( ) ; INDArray [ ] temp = new INDArray [ arrays . length ] ; boolean hasMasks = false ; for ( int i = 0 ; i < arrays . length ; i ++ ) { if ( arrays [ i ] . columns ( ) != cols ) { throw new IllegalStateException ( "Cannot merge 2d arrays with different numbers of columns (firstNCols=" + cols + ", ithNCols=" + arrays [ i ] . columns ( ) + ")" ) ; } temp [ i ] = arrays [ i ] ; if ( masks != null && masks [ i ] != null && masks [ i ] != null ) { hasMasks = true ; } } INDArray out = Nd4j . specialConcat ( 0 , temp ) ; INDArray outMask = null ; if ( hasMasks ) { outMask = DataSetUtil . mergePerOutputMasks2d ( out . shape ( ) , arrays , masks ) ; } return new Pair <> ( out , outMask ) ; }


Input: Merge the specified 4d arrays and masks . See { @link #mergeFeatures ( INDArray [] INDArray [] ) } and { @link #mergeLabels ( INDArray [] INDArray [] ) } [CODESPLIT] public static Pair < INDArray , INDArray > merge4d ( INDArray [ ] arrays , INDArray [ ] masks ) { //4d -> images. In principle: could have 2d mask arrays (per-example masks) int nExamples = 0 ; long [ ] shape = arrays [ 0 ] . shape ( ) ; INDArray [ ] temp = new INDArray [ arrays . length ] ; boolean hasMasks = false ; for ( int i = 0 ; i < arrays . length ; i ++ ) { nExamples += arrays [ i ] . size ( 0 ) ; long [ ] thisShape = arrays [ i ] . shape ( ) ; if ( thisShape . length != 4 ) { throw new IllegalStateException ( "Cannot merge 4d arrays with non 4d arrays" ) ; } for ( int j = 1 ; j < 4 ; j ++ ) { if ( thisShape [ j ] != shape [ j ] ) throw new IllegalStateException ( "Cannot merge 4d arrays with different shape (other than # examples): " + " data[0].shape = " + Arrays . toString ( shape ) + ", data[" + i + "].shape = " + Arrays . toString ( thisShape ) ) ; } temp [ i ] = arrays [ i ] ; if ( masks != null && masks [ i ] != null && masks [ i ] != null ) { hasMasks = true ; if ( masks [ i ] . rank ( ) != 2 ) { throw new UnsupportedOperationException ( "Cannot merged 4d arrays with masks that are not rank 2." + " Got mask array with rank: " + masks [ i ] . rank ( ) ) ; } } } INDArray out = Nd4j . specialConcat ( 0 , temp ) ; INDArray outMask = null ; if ( hasMasks ) { outMask = DataSetUtil . mergePerOutputMasks2d ( out . shape ( ) , arrays , masks ) ; } return new Pair <> ( out , outMask ) ; }


Input: This method executes given graph and returns results [CODESPLIT] @ Override public INDArray [ ] executeGraph ( SameDiff graph , ExecutorConfiguration configuration ) { return new INDArray [ ] { graph . execAndEndResult ( ) } ; }


Input: Can we do the op ( X = Op ( X )) directly on the arrays without breaking X up into 1d tensors first? In general this is possible if the elements of X are contiguous in the buffer OR if every element of X is at position offset + i * elementWiseStride in the buffer [CODESPLIT] public static boolean canDoOpDirectly ( INDArray x ) { if ( x . elementWiseStride ( ) < 1 ) return false ; if ( x . isVector ( ) ) return true ; //For a single NDArray all we require is that the elements are contiguous in the buffer or every nth element //Full buffer -> implies all elements are contiguous (and match) long l1 = x . lengthLong ( ) ; long dl1 = x . data ( ) . length ( ) ; if ( l1 == dl1 ) return true ; //Strides are same as a zero offset NDArray -> all elements are contiguous (even if not offset 0) long [ ] shape1 = x . shape ( ) ; long [ ] stridesAsInit = ( x . ordering ( ) == ' ' ? ArrayUtil . calcStrides ( shape1 ) : ArrayUtil . calcStridesFortran ( shape1 ) ) ; boolean stridesSameAsInit = Arrays . equals ( x . stride ( ) , stridesAsInit ) ; return stridesSameAsInit ; }


Input: Can we do the transform op ( X = Op ( X Y )) directly on the arrays without breaking them up into 1d tensors first? [CODESPLIT] public static boolean canDoOpDirectly ( INDArray x , INDArray y ) { if ( x . isVector ( ) ) return true ; if ( x . ordering ( ) != y . ordering ( ) ) return false ; //other than vectors, elements in f vs. c NDArrays will never line up if ( x . elementWiseStride ( ) < 1 || y . elementWiseStride ( ) < 1 ) return false ; //Full buffer + matching strides -> implies all elements are contiguous (and match) //Need strides to match, otherwise elements in buffer won't line up (i.e., c vs. f order arrays) long l1 = x . lengthLong ( ) ; long dl1 = x . data ( ) . length ( ) ; long l2 = y . lengthLong ( ) ; long dl2 = y . data ( ) . length ( ) ; long [ ] strides1 = x . stride ( ) ; long [ ] strides2 = y . stride ( ) ; boolean equalStrides = Arrays . equals ( strides1 , strides2 ) ; if ( l1 == dl1 && l2 == dl2 && equalStrides ) return true ; //Strides match + are same as a zero offset NDArray -> all elements are contiguous (and match) if ( equalStrides ) { long [ ] shape1 = x . shape ( ) ; long [ ] stridesAsInit = ( x . ordering ( ) == ' ' ? ArrayUtil . calcStrides ( shape1 ) : ArrayUtil . calcStridesFortran ( shape1 ) ) ; boolean stridesSameAsInit = Arrays . equals ( strides1 , stridesAsInit ) ; return stridesSameAsInit ; } return false ; }


Input: Tensor1DStats used to efficiently iterate through tensors on a matrix ( 2d NDArray ) for element - wise ops For example the offset of each 1d tensor can be calculated using only a single tensorAlongDimension method call hence is potentially faster than approaches requiring multiple tensorAlongDimension calls . <br > Note that this can only ( generally ) be used for 2d NDArrays . For certain 3 + d NDArrays the tensor starts may not be in increasing order [CODESPLIT] public static Tensor1DStats get1DTensorStats ( INDArray array , int ... dimension ) { long tensorLength = array . size ( dimension [ 0 ] ) ; //As per tensorssAlongDimension: long numTensors = array . tensorssAlongDimension ( dimension ) ; //First tensor always starts with the first element in the NDArray, regardless of dimension long firstTensorOffset = array . offset ( ) ; //Next: Need to work out the separation between the start (first element) of each 1d tensor long tensorStartSeparation ; int elementWiseStride ; //Separation in buffer between elements in the tensor if ( numTensors == 1 ) { tensorStartSeparation = - 1 ; //Not applicable elementWiseStride = array . elementWiseStride ( ) ; } else { INDArray secondTensor = array . tensorAlongDimension ( 1 , dimension ) ; tensorStartSeparation = secondTensor . offset ( ) - firstTensorOffset ; elementWiseStride = secondTensor . elementWiseStride ( ) ; } return new Tensor1DStats ( firstTensorOffset , tensorStartSeparation , numTensors , tensorLength , elementWiseStride ) ; }


Input: This method calculates dot of gives rows [CODESPLIT] @ Override public void processMessage ( ) { // this only picks up new training round //log.info("sI_{} Processing DistributedSgDotMessage taskId: {}", transport.getShardIndex(), getTaskId()); SkipGramRequestMessage sgrm = new SkipGramRequestMessage ( w1 , w2 , rowsB , codes , negSamples , alpha , 119 ) ; if ( negSamples > 0 ) { // unfortunately we have to get copy of negSamples here int negatives [ ] = Arrays . copyOfRange ( rowsB , codes . length , rowsB . length ) ; sgrm . setNegatives ( negatives ) ; } sgrm . setTaskId ( this . taskId ) ; sgrm . setOriginatorId ( this . getOriginatorId ( ) ) ; // FIXME: get rid of THAT SkipGramTrainer sgt = ( SkipGramTrainer ) trainer ; sgt . pickTraining ( sgrm ) ; //TODO: make this thing a single op, even specialOp is ok // we calculate dot for all involved rows int resultLength = codes . length + ( negSamples > 0 ? ( negSamples + 1 ) : 0 ) ; INDArray result = Nd4j . createUninitialized ( resultLength , 1 ) ; int e = 0 ; for ( ; e < codes . length ; e ++ ) { double dot = Nd4j . getBlasWrapper ( ) . dot ( storage . getArray ( WordVectorStorage . SYN_0 ) . getRow ( w2 ) , storage . getArray ( WordVectorStorage . SYN_1 ) . getRow ( rowsB [ e ] ) ) ; result . putScalar ( e , dot ) ; } // negSampling round for ( ; e < resultLength ; e ++ ) { double dot = Nd4j . getBlasWrapper ( ) . dot ( storage . getArray ( WordVectorStorage . SYN_0 ) . getRow ( w2 ) , storage . getArray ( WordVectorStorage . SYN_1_NEGATIVE ) . getRow ( rowsB [ e ] ) ) ; result . putScalar ( e , dot ) ; } if ( voidConfiguration . getExecutionMode ( ) == ExecutionMode . AVERAGING ) { // just local bypass DotAggregation dot = new DotAggregation ( taskId , ( short ) 1 , shardIndex , result ) ; dot . setTargetId ( ( short ) - 1 ) ; dot . setOriginatorId ( getOriginatorId ( ) ) ; transport . putMessage ( dot ) ; } else if ( voidConfiguration . getExecutionMode ( ) == ExecutionMode . SHARDED ) { // send this message to everyone DotAggregation dot = new DotAggregation ( taskId , ( short ) voidConfiguration . getNumberOfShards ( ) , shardIndex , result ) ; dot . setTargetId ( ( short ) - 1 ) ; dot . setOriginatorId ( getOriginatorId ( ) ) ; transport . sendMessage ( dot ) ; } }


Input: Mean squared error : L = mean ( ( predicted - label ) ^2 ) [CODESPLIT] public static LossInfo mse ( String outputName , SDVariable predictions , SDVariable label , SDVariable weights , Reduction reduction , int ... dimensions ) { LossInfo . Builder b = validate ( "mse" , predictions , label , reduction ) ; SameDiff sd = predictions . getSameDiff ( ) ; if ( weights == null ) { weights = sd . one ( "mse_loss_weights" , SCALAR ) ; } SDVariable diff = predictions . sub ( label ) ; String name = ( reduction == Reduction . NONE ? outputName : null ) ; SDVariable preReduceLoss = sd . square ( diff ) . mul ( name , weights ) ; return doReduce ( sd , outputName , true , b , reduction , preReduceLoss , label , weights , dimensions ) ; }


Input: Multi - Class Cross Entropy loss function : <br > L = sum_i actual_i * log ( predicted_i ) [CODESPLIT] public static LossInfo mcxent ( String outputName , SDVariable predictions , SDVariable label , SDVariable weights , Reduction reduction , int ... dimensions ) { LossInfo . Builder b = validate ( "mcxent" , predictions , label , reduction ) ; SameDiff sd = predictions . getSameDiff ( ) ; if ( weights == null ) { weights = sd . one ( "mcxent_loss_weights" , SCALAR ) ; } String name = ( reduction == Reduction . NONE ? outputName : null ) ; SDVariable weightedLogProd = sd . log ( predictions ) . mul ( label ) . mul ( name , weights ) ; return doReduce ( sd , outputName , false , b , reduction , weightedLogProd , label , weights , dimensions ) ; }


Input: Determine the number of weight entries that are non - zero after broadcasting [CODESPLIT] private static SDVariable nonZeroCount ( SDVariable weights , SDVariable labels ) { SameDiff sd = weights . getSameDiff ( ) ; SDVariable present = sd . neq ( weights , 0.0 ) ; SDVariable presentBroadcast = sd . zerosLike ( labels ) . add ( present ) ; return sd . sum ( presentBroadcast ) ; }


Input: Perform the final reduction on the loss function [CODESPLIT] private static LossInfo doReduce ( SameDiff sd , String outputName , boolean isMean , LossInfo . Builder b , Reduction reduction , SDVariable preReduceLoss , SDVariable label , SDVariable weights , int [ ] dimensions ) { switch ( reduction ) { case NONE : //Return same shape as predictions/labels b . loss ( preReduceLoss ) ; break ; case SPECIFIED_DIMS : //Reduce along specified dimensions if ( isMean ) { //Example: MSE + mean along examples b . loss ( sd . mean ( outputName , preReduceLoss , dimensions ) ) ; } else { //Example: L1 loss (sum) + mean along examples b . loss ( sd . sum ( outputName , preReduceLoss , dimensions ) ) ; } case SUM : if ( isMean ) { //Example: MSE (mean) + sum along examples SDVariable m = sd . mean ( preReduceLoss , dimensions ) ; b . loss ( sd . sum ( outputName , m ) ) ; } else { //Example: L1 loss (sum) + sum along examples -> sum along all dimensions b . loss ( sd . sum ( outputName , preReduceLoss ) ) ; } break ; case MEAN_BY_WEIGHT : SDVariable weightSum = sd . sum ( weights ) ; if ( isMean ) { //Example: MSE (mean) + mean by weights over examples //reduce along dims + reduce along remaining dims == reduce along *all* dims SDVariable m2 = sd . mean ( preReduceLoss ) ; b . loss ( m2 . div ( outputName , weightSum ) ) ; } else { //Example: L1 (sum) + mean by weights over examples SDVariable sum = sd . sum ( preReduceLoss , dimensions ) ; b . loss ( sum . div ( outputName , weightSum ) ) ; } break ; case MEAN_BY_COUNT : SDVariable nonZeroWeights = nonZeroCount ( weights , label ) ; SDVariable r ; if ( isMean ) { //Example: MSE (mean) + mean by count over examples r = sd . sum ( preReduceLoss ) ; } else { //Example: L1 (sum) + mean by count over examples SDVariable sum = sd . sum ( preReduceLoss , dimensions ) ; r = sd . mean ( sum ) ; } b . loss ( r . div ( outputName , nonZeroWeights ) ) ; break ; default : throw new RuntimeException ( "Unknown reduction: " + reduction ) ; } return b . build ( ) ; }


Input: Initialize this CaptureTypeImpl . This is needed for type variable bounds referring to each other : we need the capture of the argument . [CODESPLIT] void init ( VarMap varMap ) { ArrayList < Type > upperBoundsList = new ArrayList <> ( ) ; upperBoundsList . addAll ( Arrays . asList ( varMap . map ( variable . getBounds ( ) ) ) ) ; List < Type > wildcardUpperBounds = Arrays . asList ( wildcard . getUpperBounds ( ) ) ; if ( wildcardUpperBounds . size ( ) > 0 && wildcardUpperBounds . get ( 0 ) == Object . class ) { // skip the Object bound, we already have a first upper bound from 'variable' upperBoundsList . addAll ( wildcardUpperBounds . subList ( 1 , wildcardUpperBounds . size ( ) ) ) ; } else { upperBoundsList . addAll ( wildcardUpperBounds ) ; } upperBounds = new Type [ upperBoundsList . size ( ) ] ; upperBoundsList . toArray ( upperBounds ) ; }


Input: Throw an IllegalStateException if the class does not have a no - arg constructor . [CODESPLIT] public static < T > Constructor < T > getNoArgConstructor ( Class < T > clazz ) { try { Constructor < T > ctor = clazz . getDeclaredConstructor ( new Class [ 0 ] ) ; ctor . setAccessible ( true ) ; return ctor ; } catch ( NoSuchMethodException e ) { // lame there is no way to tell if the class is a nonstatic inner class if ( clazz . isMemberClass ( ) || clazz . isAnonymousClass ( ) || clazz . isLocalClass ( ) ) throw new IllegalStateException ( clazz . getName ( ) + " must be static and must have a no-arg constructor" , e ) ; else throw new IllegalStateException ( clazz . getName ( ) + " must have a no-arg constructor" , e ) ; } }


Input: Gets a constructor that has the specified types of arguments . Throw an IllegalStateException if the class does not have such a constructor . [CODESPLIT] public static MethodHandle getConstructor ( Class < ? > clazz , Class < ? > ... args ) { try { // We use unreflect so that we can make the constructor accessible Constructor < ? > ctor = clazz . getDeclaredConstructor ( args ) ; ctor . setAccessible ( true ) ; return MethodHandles . lookup ( ) . unreflectConstructor ( ctor ) ; } catch ( NoSuchMethodException e ) { throw new IllegalStateException ( clazz . getName ( ) + " has no constructor with args " + Arrays . toString ( args ) , e ) ; } catch ( IllegalAccessException e ) { throw new IllegalStateException ( "Problem getting constructor for " + clazz . getName ( ) + " with args " + Arrays . toString ( args ) , e ) ; } }


Input: Wraps any non - runtime exceptions with a runtime exception [CODESPLIT] public static < T > T invoke ( MethodHandle methodHandle , Object ... params ) { try { return ( T ) methodHandle . invokeWithArguments ( params ) ; } catch ( RuntimeException e ) { throw e ; } catch ( Throwable throwable ) { throw new RuntimeException ( throwable ) ; } }


Input: Checked exceptions are LAME . [CODESPLIT] public static < T > T newInstance ( Constructor < T > ctor , Object ... params ) { try { return ctor . newInstance ( params ) ; } catch ( InstantiationException | IllegalAccessException | InvocationTargetException e ) { throw new RuntimeException ( e ) ; } }


Input: Checked exceptions are LAME . [CODESPLIT] public static Object field_get ( Field field , Object obj ) { try { return field . get ( obj ) ; } catch ( IllegalArgumentException | IllegalAccessException e ) { throw new RuntimeException ( e ) ; } }


Input: Just like Class . isAssignableFrom () but does the right thing when considering autoboxing . [CODESPLIT] public static boolean isAssignableFrom ( Class < ? > to , Class < ? > from ) { Class < ? > notPrimitiveTo = to . isPrimitive ( ) ? PRIMITIVE_TO_WRAPPER . get ( to ) : to ; Class < ? > notPrimitiveFrom = from . isPrimitive ( ) ? PRIMITIVE_TO_WRAPPER . get ( from ) : from ; return notPrimitiveTo . isAssignableFrom ( notPrimitiveFrom ) ; }


Input: Gets the annotation that has the specified type or null if there isn t one [CODESPLIT] @ SuppressWarnings ( "unchecked" ) public static < A extends Annotation > A getAnnotation ( Annotation [ ] annotations , Class < A > annotationType ) { for ( Annotation anno : annotations ) if ( annotationType . isAssignableFrom ( anno . getClass ( ) ) ) return ( A ) anno ; return null ; }


Input: Get the declared annotation ignoring any inherited annotations [CODESPLIT] public static < A extends Annotation > A getDeclaredAnnotation ( Class < ? > onClass , Class < A > annotationType ) { return getAnnotation ( onClass . getDeclaredAnnotations ( ) , annotationType ) ; }


Input: Is the declared annotation present ignoring any inherited annotations [CODESPLIT] public static < A extends Annotation > boolean isDeclaredAnnotationPresent ( Class < ? > onClass , Class < A > annotationType ) { return getDeclaredAnnotation ( onClass , annotationType ) != null ; }


Input: Create a ResultProxy for the given interface . [CODESPLIT] @ SuppressWarnings ( "unchecked" ) public static < S > S create ( Class < ? super S > interf , Result < S > result ) { return ( S ) Proxy . newProxyInstance ( result . getClass ( ) . getClassLoader ( ) , new Class [ ] { interf } , new ResultProxy <> ( result ) ) ; }


Input: / * ( non - Javadoc ) [CODESPLIT] @ Override public Response commit ( ) { // Complete any enlisted operations so that the session becomes consistent. Note that some of the // enlisted load operations might result in further enlistment... so we have to do this in a loop // that protects against concurrent modification exceptions while ( ! enlisted . isEmpty ( ) ) { final List < Result < ? > > last = enlisted ; enlisted = new ArrayList <> ( ) ; for ( final Result < ? > result : last ) result . now ( ) ; } final Response response = transaction . commit ( ) ; afterCommit . run ( ) ; return response ; }


Input: Converts an entity to an object of the appropriate type for this metadata structure . Does not check that the entity is appropriate ; that should be done when choosing which EntityMetadata to call . [CODESPLIT] public P load ( final BaseEntity < ? > ent , final LoadContext ctx ) { try { // The context needs to know the root entity for any given point ctx . setCurrentRoot ( Key . create ( ( com . google . cloud . datastore . Key ) ent . getKey ( ) ) ) ; final EntityValue entityValue = makeLoadEntityValue ( ent ) ; return translator . load ( entityValue , ctx , Path . root ( ) ) ; } catch ( LoadException ex ) { throw ex ; } catch ( Exception ex ) { throw new LoadException ( ent , ex . getMessage ( ) , ex ) ; } }


Input: The problem is ProjectionEntity ; there s no way to create an EntityValue with a ProjectionEntity so we can t use the standard translation system for { [CODESPLIT] private EntityValue makeLoadEntityValue ( final BaseEntity < ? > ent ) { if ( ent instanceof FullEntity < ? > ) { return EntityValue . of ( ( FullEntity < ? > ) ent ) ; } else { // Sadly there's no more graceful way of doing this final Builder < ? > builder = FullEntity . newBuilder ( ent . getKey ( ) ) ; for ( final String name : ent . getNames ( ) ) { final Value < ? > value = ent . getValue ( name ) ; builder . set ( name , value ) ; } return EntityValue . of ( builder . build ( ) ) ; } }


Input: Converts an object to a datastore Entity with the appropriate Key type . [CODESPLIT] public FullEntity < ? > save ( final P pojo , final SaveContext ctx ) { try { return translator . save ( pojo , false , ctx , Path . root ( ) ) . get ( ) ; } catch ( SaveException ex ) { throw ex ; } catch ( Exception ex ) { throw new SaveException ( pojo , ex ) ; } }


Input: / * ( non - Javadoc ) [CODESPLIT] @ Override public QueryImpl < T > filter ( final String condition , final Object value ) { final QueryImpl < T > q = createQuery ( ) ; q . addFilter ( condition , value ) ; return q ; }


Input:
Modifies the instance [CODESPLIT] void addFilter ( final String condition , final Object value ) { final String [ ] parts = condition . trim ( ) . split ( " " ) ; if ( parts . length < 1 || parts . length > 2 ) throw new IllegalArgumentException ( "'" + condition + "' is not a legal filter condition" ) ; final String prop = parts [ 0 ] . trim ( ) ; final FilterOperator op = ( parts . length == 2 ) ? this . translate ( parts [ 1 ] ) : FilterOperator . EQUAL ; // If we have a class restriction, check to see if the property is the @Parent or @Id. We used to try to convert
 // filtering on the id field to a __key__ query, but that tended to confuse users about the real capabilities
 // of GAE and Objectify. So let's force users to use filterKey() instead.
 if ( this . classRestriction != null ) { final KeyMetadata < ? > meta = loader . ofy . factory ( ) . keys ( ) . getMetadataSafe ( this . classRestriction ) ; if ( prop . equals ( meta . getParentFieldName ( ) ) ) { throw new IllegalArgumentException ( "@Parent fields cannot be filtered on. Perhaps you wish to use filterKey() or ancestor() instead?" ) ; } else if ( prop . equals ( meta . getIdFieldName ( ) ) ) { throw new IllegalArgumentException ( "@Id fields cannot be filtered on. Perhaps you wish to use filterKey() instead?" ) ; } } // Convert to something filterable, possibly extracting/converting keys
 final Value < ? > translated = loader . getObjectifyImpl ( ) . makeFilterable ( value ) ; addFilter ( op . of ( prop , translated ) ) ; }


Input:
Converts the textual operator ( > < = etc ) into a FilterOperator . Forgiving about the syntax ; ! = and < > are NOT_EQUAL = and == are EQUAL . [CODESPLIT] protected FilterOperator translate ( String operator ) { operator = operator . trim ( ) ; if ( operator . equals ( "=" ) || operator . equals ( "==" ) ) return FilterOperator . EQUAL ; else if ( operator . equals ( ">" ) ) return FilterOperator . GREATER_THAN ; else if ( operator . equals ( ">=" ) ) return FilterOperator . GREATER_THAN_OR_EQUAL ; else if ( operator . equals ( "<" ) ) return FilterOperator . LESS_THAN ; else if ( operator . equals ( "<=" ) ) return FilterOperator . LESS_THAN_OR_EQUAL ; else if ( operator . equals ( "!=" ) || operator . equals ( "<>" ) ) //return FilterOperator.NOT_EQUAL;
 throw new UnsupportedOperationException ( "The Cloud Datastore SDK does not currently support 'NOT EQUAL' filters" ) ; else if ( operator . toLowerCase ( ) . equals ( "in" ) ) //return FilterOperator.IN;
 throw new UnsupportedOperationException ( "The Cloud Datastore SDK does not currently support 'IN' filters" ) ; else throw new IllegalArgumentException ( "Unknown operator '" + operator + "'" ) ; }


Input:
Modifies the instance [CODESPLIT] void addOrder ( String condition ) { condition = condition . trim ( ) ; boolean descending = false ; if ( condition . startsWith ( "-" ) ) { descending = true ; condition = condition . substring ( 1 ) . trim ( ) ; } // Prevent ordering by @Id or @Parent fields, which are really part of the key
 if ( this . classRestriction != null ) { final KeyMetadata < ? > meta = loader . ofy . factory ( ) . keys ( ) . getMetadataSafe ( this . classRestriction ) ; if ( condition . equals ( meta . getParentFieldName ( ) ) ) throw new IllegalArgumentException ( "You cannot order by @Parent field. Perhaps you wish to order by __key__ instead?" ) ; if ( condition . equals ( meta . getIdFieldName ( ) ) ) { throw new IllegalArgumentException ( "You cannot order by @Id field. Perhaps you wish to order by __key__ instead?" ) ; } } this . actual = actual . orderBy ( descending ? OrderBy . desc ( condition ) : OrderBy . asc ( condition ) ) ; }


Input: Modifies the instance [CODESPLIT] void setAncestor ( final Object keyOrEntity ) { final com . google . cloud . datastore . Key key = loader . ofy . factory ( ) . keys ( ) . anythingToRawKey ( keyOrEntity ) ; this . actual = this . actual . andFilter ( PropertyFilter . hasAncestor ( key ) ) ; }


Input: Modifies the instance [CODESPLIT] void setLimit ( final int value ) { this . actual = this . actual . limit ( value ) ; if ( this . chunk == null ) this . chunk = value ; }


Input: Modifies the instance [CODESPLIT] void addProjection ( final String ... fields ) { if ( this . hybrid != null && this . hybrid ) throw new IllegalStateException ( "You cannot ask for both hybrid and projections in the same query. That makes no sense!" ) ; for ( final String field : fields ) { this . actual = this . actual . project ( field ) ; } }


Input:
/ * ( non - Javadoc ) [CODESPLIT] @ Override public LoadResult < T > first ( ) { // By the way, this is the same thing that PreparedQuery.asSingleEntity() does internally
 final Iterator < T > it = this . limit ( 1 ) . iterator ( ) ; return new LoadResult <> ( null , new IteratorFirstResult <> ( it ) ) ; }


Input: / * ( non - Javadoc ) [CODESPLIT] @ Override public QueryResults < T > iterator ( ) { if ( ! actual . getProjection ( ) . isEmpty ( ) ) return loader . createQueryEngine ( ) . queryProjection ( this . actual . newProjectionQuery ( ) ) ; else if ( shouldHybridize ( ) ) return loader . createQueryEngine ( ) . queryHybrid ( this . actual . newKeyQuery ( ) , chunk == null ? Integer . MAX_VALUE : chunk ) ; else return loader . createQueryEngine ( ) . queryNormal ( this . actual . newEntityQuery ( ) , chunk == null ? Integer . MAX_VALUE : chunk ) ; }


Input: / * ( non - Javadoc ) [CODESPLIT] @ Override public List < T > list ( ) { return ResultProxy . create ( List . class , new MakeListResult <> ( this . chunk ( Integer . MAX_VALUE ) . iterator ( ) ) ) ; }


Input: Get an iterator over the keys . Not part of the public api but used by QueryKeysImpl . Assumes that setKeysOnly () has already been set . [CODESPLIT] QueryResults < Key < T > > keysIterator ( ) { final QueryEngine queryEngine = loader . createQueryEngine ( ) ; final KeyQuery query = this . actual . newKeyQuery ( ) ; return queryEngine . queryKeysOnly ( query ) ; }


Input: The Builder api is programmer - hostile [CODESPLIT] public static void addOrderBy ( final StructuredQuery . Builder < ? > builder , final List < OrderBy > orderBy ) { if ( ! orderBy . isEmpty ( ) ) { builder . addOrderBy ( orderBy . get ( 0 ) , orderBy . subList ( 1 , orderBy . size ( ) ) . toArray ( new OrderBy [ orderBy . size ( ) - 1 ] ) ) ; } }


Input: The Builder api is programmer - hostile [CODESPLIT] public static void addProjection ( final ProjectionEntityQuery . Builder builder , final List < String > projection ) { if ( ! projection . isEmpty ( ) ) { builder . addProjection ( projection . get ( 0 ) , projection . subList ( 1 , projection . size ( ) ) . toArray ( new String [ projection . size ( ) - 1 ] ) ) ; } }


Input: The Builder api is programmer - hostile [CODESPLIT] public static void addDistinctOn ( final ProjectionEntityQuery . Builder builder , final List < String > distinctOn ) { if ( ! distinctOn . isEmpty ( ) ) { builder . addDistinctOn ( distinctOn . get ( 0 ) , distinctOn . subList ( 1 , distinctOn . size ( ) ) . toArray ( new String [ distinctOn . size ( ) - 1 ] ) ) ; } }