Input: Merge the list of datasets in to one list . All the rows are merged in to one dataset [CODESPLIT] public static DataSet merge ( List < DataSet > data ) { if ( data . isEmpty ( ) ) throw new IllegalArgumentException ( "Unable to merge empty dataset" ) ; int nonEmpty = 0 ; boolean anyFeaturesPreset = false ; boolean anyLabelsPreset = false ; boolean first = true ; for ( DataSet ds : data ) { if ( ds . isEmpty ( ) ) { continue ; } nonEmpty ++ ; if ( anyFeaturesPreset && ds . getFeatures ( ) == null || ( ! first && ! anyFeaturesPreset && ds . getFeatures ( ) != null ) ) { throw new IllegalStateException ( "Cannot merge features: encountered null features in one or more DataSets" ) ; } if ( anyLabelsPreset && ds . getLabels ( ) == null || ( ! first && ! anyLabelsPreset && ds . getLabels ( ) != null ) ) { throw new IllegalStateException ( "Cannot merge labels: enountered null labels in one or more DataSets" ) ; } anyFeaturesPreset |= ds . getFeatures ( ) != null ; anyLabelsPreset |= ds . getLabels ( ) != null ; first = false ; } INDArray [ ] featuresToMerge = new INDArray [ nonEmpty ] ; INDArray [ ] labelsToMerge = new INDArray [ nonEmpty ] ; INDArray [ ] featuresMasksToMerge = null ; INDArray [ ] labelsMasksToMerge = null ; int count = 0 ; for ( DataSet ds : data ) { if ( ds . isEmpty ( ) ) continue ; featuresToMerge [ count ] = ds . getFeatureMatrix ( ) ; labelsToMerge [ count ] = ds . getLabels ( ) ; if ( ds . getFeaturesMaskArray ( ) != null ) { if ( featuresMasksToMerge == null ) { featuresMasksToMerge = new INDArray [ data . size ( ) ] ; } featuresMasksToMerge [ count ] = ds . getFeaturesMaskArray ( ) ; } if ( ds . getLabelsMaskArray ( ) != null ) { if ( labelsMasksToMerge == null ) { labelsMasksToMerge = new INDArray [ data . size ( ) ] ; } labelsMasksToMerge [ count ] = ds . getLabelsMaskArray ( ) ; } count ++ ; } INDArray featuresOut ; INDArray labelsOut ; INDArray featuresMaskOut ; INDArray labelsMaskOut ; Pair < INDArray , INDArray > fp = DataSetUtil . mergeFeatures ( featuresToMerge , featuresMasksToMerge ) ; featuresOut = fp . getFirst ( ) ; featuresMaskOut = fp . getSecond ( ) ; Pair < INDArray , INDArray > lp = DataSetUtil . mergeLabels ( labelsToMerge , labelsMasksToMerge ) ; labelsOut = lp . getFirst ( ) ; labelsMaskOut = lp . getSecond ( ) ; DataSet dataset = new DataSet ( featuresOut , labelsOut , featuresMaskOut , labelsMaskOut ) ; List < Serializable > meta = null ; for ( DataSet ds : data ) { if ( ds . getExampleMetaData ( ) == null || ds . getExampleMetaData ( ) . size ( ) != ds . numExamples ( ) ) { meta = null ; break ; } if ( meta == null ) meta = new ArrayList <> ( ) ; meta . addAll ( ds . getExampleMetaData ( ) ) ; } if ( meta != null ) { dataset . setExampleMetaData ( meta ) ; } return dataset ; }


Input: Binarizes the dataset such that any number greater than cutoff is 1 otherwise zero [CODESPLIT] @ Override public void binarize ( double cutoff ) { INDArray linear = getFeatureMatrix ( ) . linearView ( ) ; for ( int i = 0 ; i < getFeatures ( ) . length ( ) ; i ++ ) { double curr = linear . getDouble ( i ) ; if ( curr > cutoff ) getFeatures ( ) . putScalar ( i , 1 ) ; else getFeatures ( ) . putScalar ( i , 0 ) ; } }


Input: Gets a copy of example i [CODESPLIT] @ Override public DataSet get ( int i ) { if ( i > numExamples ( ) || i < 0 ) throw new IllegalArgumentException ( "invalid example number" ) ; if ( i == 0 && numExamples ( ) == 1 ) return this ; if ( getFeatureMatrix ( ) . rank ( ) == 4 ) { //ensure rank is preserved INDArray slice = getFeatureMatrix ( ) . slice ( i ) ; return new DataSet ( slice . reshape ( ArrayUtil . combine ( new long [ ] { 1 } , slice . shape ( ) ) ) , getLabels ( ) . slice ( i ) ) ; } return new DataSet ( getFeatures ( ) . slice ( i ) , getLabels ( ) . slice ( i ) ) ; }


Input: Gets a copy of example i [CODESPLIT] @ Override public DataSet get ( int [ ] i ) { return new DataSet ( getFeatures ( ) . getRows ( i ) , getLabels ( ) . getRows ( i ) ) ; }


Input: Sample a dataset [CODESPLIT] @ Override public DataSet sample ( int numSamples , org . nd4j . linalg . api . rng . Random rng , boolean withReplacement ) { INDArray examples = Nd4j . create ( numSamples , getFeatures ( ) . columns ( ) ) ; INDArray outcomes = Nd4j . create ( numSamples , numOutcomes ( ) ) ; Set < Integer > added = new HashSet <> ( ) ; for ( int i = 0 ; i < numSamples ; i ++ ) { int picked = rng . nextInt ( numExamples ( ) ) ; if ( ! withReplacement ) while ( added . contains ( picked ) ) picked = rng . nextInt ( numExamples ( ) ) ; examples . putRow ( i , get ( picked ) . getFeatures ( ) ) ; outcomes . putRow ( i , get ( picked ) . getLabels ( ) ) ; } return new DataSet ( examples , outcomes ) ; }


Input: This method returns memory used by this DataSet [CODESPLIT] @ Override public long getMemoryFootprint ( ) { long reqMem = features . lengthLong ( ) * Nd4j . sizeOfDataType ( ) ; reqMem += labels == null ? 0 : labels . lengthLong ( ) * Nd4j . sizeOfDataType ( ) ; reqMem += featuresMask == null ? 0 : featuresMask . lengthLong ( ) * Nd4j . sizeOfDataType ( ) ; reqMem += labelsMask == null ? 0 : labelsMask . lengthLong ( ) * Nd4j . sizeOfDataType ( ) ; return reqMem ; }


Input: Make a string representation of the exception . [CODESPLIT] public static String stringifyException ( Throwable e ) { StringWriter stm = new StringWriter ( ) ; PrintWriter wrt = new PrintWriter ( stm ) ; e . printStackTrace ( wrt ) ; wrt . close ( ) ; return stm . toString ( ) ; }


Input: Given a full hostname return the word upto the first dot . [CODESPLIT] public static String simpleHostname ( String fullHostname ) { if ( InetAddresses . isInetAddress ( fullHostname ) ) { return fullHostname ; } int offset = fullHostname . indexOf ( ' ' ) ; if ( offset != - 1 ) { return fullHostname . substring ( 0 , offset ) ; } return fullHostname ; }


Input: The same as String . format ( Locale . ENGLISH format objects ) . [CODESPLIT] public static String format ( final String format , final Object ... objects ) { return String . format ( Locale . ENGLISH , format , objects ) ; }


Input: Given an array of strings return a comma - separated list of its elements . [CODESPLIT] public static String arrayToString ( String [ ] strs ) { if ( strs . length == 0 ) { return "" ; } StringBuilder sbuf = new StringBuilder ( ) ; sbuf . append ( strs [ 0 ] ) ; for ( int idx = 1 ; idx < strs . length ; idx ++ ) { sbuf . append ( "," ) ; sbuf . append ( strs [ idx ] ) ; } return sbuf . toString ( ) ; }


Input: Given an array of bytes it will convert the bytes to a hex string representation of the bytes [CODESPLIT] public static String byteToHexString ( byte [ ] bytes , int start , int end ) { if ( bytes == null ) { throw new IllegalArgumentException ( "bytes == null" ) ; } StringBuilder s = new StringBuilder ( ) ; for ( int i = start ; i < end ; i ++ ) { s . append ( format ( "%02x" , bytes [ i ] ) ) ; } return s . toString ( ) ; }


Input: Formats time in ms and appends difference ( finishTime - startTime ) as returned by formatTimeDiff () . If finish time is 0 empty string is returned if start time is 0 then difference is not appended to return value . [CODESPLIT] public static String getFormattedTimeWithDiff ( String formattedFinishTime , long finishTime , long startTime ) { StringBuilder buf = new StringBuilder ( ) ; if ( 0 != finishTime ) { buf . append ( formattedFinishTime ) ; if ( 0 != startTime ) { buf . append ( " (" + formatTimeDiff ( finishTime , startTime ) + ")" ) ; } } return buf . toString ( ) ; }


Input: Returns an arraylist of strings . [CODESPLIT] public static String [ ] getStrings ( String str , String delim ) { Collection < String > values = getStringCollection ( str , delim ) ; if ( values . size ( ) == 0 ) { return null ; } return values . toArray ( new String [ values . size ( ) ] ) ; }


Input: Returns a collection of strings . [CODESPLIT] public static Collection < String > getStringCollection ( String str ) { String delim = "," ; return getStringCollection ( str , delim ) ; }


Input: Returns a collection of strings . [CODESPLIT] public static Collection < String > getStringCollection ( String str , String delim ) { List < String > values = new ArrayList < String > ( ) ; if ( str == null ) return values ; StringTokenizer tokenizer = new StringTokenizer ( str , delim ) ; while ( tokenizer . hasMoreTokens ( ) ) { values . add ( tokenizer . nextToken ( ) ) ; } return values ; }


Input: Splits a comma separated value <code > String< / code > trimming leading and trailing whitespace on each value . Duplicate and empty values are removed . [CODESPLIT] public static Collection < String > getTrimmedStringCollection ( String str ) { Set < String > set = new LinkedHashSet < String > ( Arrays . asList ( getTrimmedStrings ( str ) ) ) ; set . remove ( "" ) ; return set ; }


Input: Splits a comma or newline separated value <code > String< / code > trimming leading and trailing whitespace on each value . [CODESPLIT] public static String [ ] getTrimmedStrings ( String str ) { if ( null == str || str . trim ( ) . isEmpty ( ) ) { return emptyStringArray ; } return str . trim ( ) . split ( "\\s*[,\n]\\s*" ) ; }


Input: Split a string using the given separator [CODESPLIT] public static String [ ] split ( String str , char escapeChar , char separator ) { if ( str == null ) { return null ; } ArrayList < String > strList = new ArrayList < String > ( ) ; StringBuilder split = new StringBuilder ( ) ; int index = 0 ; while ( ( index = findNext ( str , separator , escapeChar , index , split ) ) >= 0 ) { ++ index ; // move over the separator for next search strList . add ( split . toString ( ) ) ; split . setLength ( 0 ) ; // reset the buffer } strList . add ( split . toString ( ) ) ; // remove trailing empty split(s) int last = strList . size ( ) ; // last split while ( -- last >= 0 && "" . equals ( strList . get ( last ) ) ) { strList . remove ( last ) ; } return strList . toArray ( new String [ strList . size ( ) ] ) ; }


Input: Split a string using the given separator with no escaping performed . [CODESPLIT] public static String [ ] split ( String str , char separator ) { // String.split returns a single empty result for splitting the empty // string. if ( str . isEmpty ( ) ) { return new String [ ] { "" } ; } ArrayList < String > strList = new ArrayList < String > ( ) ; int startIndex = 0 ; int nextIndex = 0 ; while ( ( nextIndex = str . indexOf ( separator , startIndex ) ) != - 1 ) { strList . add ( str . substring ( startIndex , nextIndex ) ) ; startIndex = nextIndex + 1 ; } strList . add ( str . substring ( startIndex ) ) ; // remove trailing empty split(s) int last = strList . size ( ) ; // last split while ( -- last >= 0 && "" . equals ( strList . get ( last ) ) ) { strList . remove ( last ) ; } return strList . toArray ( new String [ strList . size ( ) ] ) ; }


Input: Finds the first occurrence of the separator character ignoring the escaped separators starting from the index . Note the substring between the index and the position of the separator is passed . [CODESPLIT] public static int findNext ( String str , char separator , char escapeChar , int start , StringBuilder split ) { int numPreEscapes = 0 ; for ( int i = start ; i < str . length ( ) ; i ++ ) { char curChar = str . charAt ( i ) ; if ( numPreEscapes == 0 && curChar == separator ) { // separator return i ; } else { split . append ( curChar ) ; numPreEscapes = ( curChar == escapeChar ) ? ( ++ numPreEscapes ) % 2 : 0 ; } } return - 1 ; }


Input: Escape <code > charToEscape< / code > in the string with the escape char <code > escapeChar< / code > [CODESPLIT] public static String escapeString ( String str , char escapeChar , char charToEscape ) { return escapeString ( str , escapeChar , new char [ ] { charToEscape } ) ; }


Input: Unescape <code > charToEscape< / code > in the string with the escape char <code > escapeChar< / code > [CODESPLIT] public static String unEscapeString ( String str , char escapeChar , char charToEscape ) { return unEscapeString ( str , escapeChar , new char [ ] { charToEscape } ) ; }


Input: Escapes HTML Special characters present in the string . [CODESPLIT] public static String escapeHTML ( String string ) { if ( string == null ) { return null ; } StringBuilder sb = new StringBuilder ( ) ; boolean lastCharacterWasSpace = false ; char [ ] chars = string . toCharArray ( ) ; for ( char c : chars ) { if ( c == ' ' ) { if ( lastCharacterWasSpace ) { lastCharacterWasSpace = false ; sb . append ( " " ) ; } else { lastCharacterWasSpace = true ; sb . append ( " " ) ; } } else { lastCharacterWasSpace = false ; switch ( c ) { case ' ' : sb . append ( "<" ) ; break ; case ' ' : sb . append ( ">" ) ; break ; case ' ' : sb . append ( "&" ) ; break ; case ' ' : sb . append ( """ ) ; break ; default : sb . append ( c ) ; break ; } } } return sb . toString ( ) ; }


Input: Concatenates strings using a separator . [CODESPLIT] public static String join ( CharSequence separator , Iterable < ? > strings ) { Iterator < ? > i = strings . iterator ( ) ; if ( ! i . hasNext ( ) ) { return "" ; } StringBuilder sb = new StringBuilder ( i . next ( ) . toString ( ) ) ; while ( i . hasNext ( ) ) { sb . append ( separator ) ; sb . append ( i . next ( ) . toString ( ) ) ; } return sb . toString ( ) ; }


Input: Convert SOME_STUFF to SomeStuff [CODESPLIT] public static String camelize ( String s ) { StringBuilder sb = new StringBuilder ( ) ; String [ ] words = split ( StringUtils . toLowerCase ( s ) , ESCAPE_CHAR , ' ' ) ; for ( String word : words ) sb . append ( org . apache . commons . lang3 . StringUtils . capitalize ( word ) ) ; return sb . toString ( ) ; }


Input: Matches a template string against a pattern replaces matched tokens with the supplied replacements and returns the result . The regular expression must use a capturing group . The value of the first capturing group is used to look up the replacement . If no replacement is found for the token then it is replaced with the empty string . [CODESPLIT] public static String replaceTokens ( String template , Pattern pattern , Map < String , String > replacements ) { StringBuffer sb = new StringBuffer ( ) ; Matcher matcher = pattern . matcher ( template ) ; while ( matcher . find ( ) ) { String replacement = replacements . get ( matcher . group ( 1 ) ) ; if ( replacement == null ) { replacement = "" ; } matcher . appendReplacement ( sb , Matcher . quoteReplacement ( replacement ) ) ; } matcher . appendTail ( sb ) ; return sb . toString ( ) ; }


Input: Get stack trace for a given thread . [CODESPLIT] public static String getStackTrace ( Thread t ) { final StackTraceElement [ ] stackTrace = t . getStackTrace ( ) ; StringBuilder str = new StringBuilder ( ) ; for ( StackTraceElement e : stackTrace ) { str . append ( e . toString ( ) + "\n" ) ; } return str . toString ( ) ; }


Input: Compare strings locale - freely by using String#equalsIgnoreCase . [CODESPLIT] public static boolean equalsIgnoreCase ( String s1 , String s2 ) { Preconditions . checkNotNull ( s1 ) ; // don't check non-null against s2 to make the semantics same as // s1.equals(s2) return s1 . equalsIgnoreCase ( s2 ) ; }


Input: <p > Checks if the String contains only unicode letters . < / p > [CODESPLIT] public static boolean isAlpha ( String str ) { if ( str == null ) { return false ; } int sz = str . length ( ) ; for ( int i = 0 ; i < sz ; i ++ ) { if ( ! Character . isLetter ( str . charAt ( i ) ) ) { return false ; } } return true ; }


Input:
Inverts a matrix [CODESPLIT] public static INDArray invert ( INDArray arr , boolean inPlace ) { if ( ! arr . isSquare ( ) ) { throw new IllegalArgumentException ( "invalid array: must be square matrix" ) ; } //FIX ME: Please /* int[] IPIV = new int[arr.length() + 1];
        int LWORK = arr.length() * arr.length();
        INDArray WORK = Nd4j.create(new double[LWORK]);
        INDArray inverse = inPlace ? arr : arr.dup();
        Nd4j.getBlasWrapper().lapack().getrf(arr);
        Nd4j.getBlasWrapper().lapack().getri(arr.size(0),inverse,arr.size(0),IPIV,WORK,LWORK,0);*/ RealMatrix rm = CheckUtil . convertToApacheMatrix ( arr ) ; RealMatrix rmInverse = new LUDecomposition ( rm ) . getSolver ( ) . getInverse ( ) ; INDArray inverse = CheckUtil . convertFromApacheMatrix ( rmInverse ) ; if ( inPlace ) arr . assign ( inverse ) ; return inverse ; }


Input: Compute the factorial of the non - negative integer . [CODESPLIT] public BigInteger at ( int n ) { while ( a . size ( ) <= n ) { final int lastn = a . size ( ) - 1 ; final BigInteger nextn = BigInteger . valueOf ( lastn + 1 ) ; a . add ( a . get ( lastn ) . multiply ( nextn ) ) ; } return a . get ( n ) ; }


Input: This method returns if host side has actual copy of data [CODESPLIT] public boolean isActualOnHostSide ( ) { //log.info("isActuialOnHostSide() -> Host side: [{}], Device side: [{}]", accessHostRead.get(), accessDeviceRead.get()); boolean result = accessHostWrite . get ( ) >= accessDeviceWrite . get ( ) || accessHostRead . get ( ) >= accessDeviceWrite . get ( ) ; //log.info("isActuialOnHostSide() -> {}, shape: {}", result, shape); return result ; }


Input: This method returns if device side has actual copy of data [CODESPLIT] public boolean isActualOnDeviceSide ( ) { //log.info("isActuialOnDeviceSide() -> Host side: [{}], Device side: [{}]", accessHostWrite.get(), accessDeviceWrite.get()); boolean result = accessDeviceWrite . get ( ) >= accessHostWrite . get ( ) || accessDeviceRead . get ( ) >= accessHostWrite . get ( ) ; //accessHostWrite.get() <= getDeviceAccessTime(); //        log.info("isActuialOnDeviceSide() -> {} ({}), Shape: {}", result, objectId, shape); return result ; }


Input: This method creates shapeInformation buffer based on shape being passed in [CODESPLIT] @ Override public Pair < DataBuffer , long [ ] > createShapeInformation ( int [ ] shape ) { char order = Nd4j . order ( ) ; return createShapeInformation ( shape , order ) ; }


Input: This method creates shapeInformation buffer based on shape & order being passed in [CODESPLIT] @ Override public Pair < DataBuffer , long [ ] > createShapeInformation ( long [ ] shape , char order ) { long [ ] stride = Nd4j . getStrides ( shape , order ) ; // this won't be view, so ews is 1 int ews = 1 ; return createShapeInformation ( shape , stride , 0 , ews , order ) ; }


Input: Returns whether the given shape is a vector [CODESPLIT] public static boolean isVector ( DataBuffer shapeInfo ) { int rank = Shape . rank ( shapeInfo ) ; if ( rank > 2 || rank < 1 ) return false ; else { int len = Shape . length ( shapeInfo ) ; DataBuffer shape = Shape . shapeOf ( shapeInfo ) ; return shape . getInt ( 0 ) == len || shape . getInt ( 1 ) == len ; } }


Input:
A port of numpy s reshaping algorithm that leverages no copy where possible and returns null if the reshape couldn t happen without copying [CODESPLIT] public static INDArray newShapeNoCopy ( INDArray arr , long [ ] newShape , boolean isFOrder ) { int oldnd ; long [ ] olddims = ArrayUtil . copy ( arr . shape ( ) ) ; long [ ] oldstrides = ArrayUtil . copy ( arr . stride ( ) ) ; long np , op , last_stride ; int oi , oj , ok , ni , nj , nk ; long [ ] newStrides = new long [ newShape . length ] ; oldnd = 0 ; /*
         * Remove axes with dimension 1 from the old array. They have no effect
         * but would need special cases since their strides do not matter.
         */ for ( oi = 0 ; oi < arr . rank ( ) ; oi ++ ) { if ( arr . size ( oi ) != 1 ) { olddims [ oldnd ] = arr . size ( oi ) ; oldstrides [ oldnd ] = arr . stride ( oi ) ; oldnd ++ ; } } np = 1 ; for ( ni = 0 ; ni < newShape . length ; ni ++ ) { np *= newShape [ ni ] ; } op = 1 ; for ( oi = 0 ; oi < oldnd ; oi ++ ) { op *= olddims [ oi ] ; } if ( np != op ) { /* different total sizes; no hope */ return null ; } if ( np == 0 ) { /* the current code does not handle 0-sized arrays, so give up */ return null ; } /* oi to oj and ni to nj give the axis ranges currently worked with */ oi = 0 ; oj = 1 ; ni = 0 ; nj = 1 ; while ( ni < newShape . length && oi < oldnd ) { np = newShape [ ni ] ; op = olddims [ oi ] ; while ( np != op ) { if ( np < op ) { /* Misses trailing 1s, these are handled later */ np *= newShape [ nj ++ ] ; } else { op *= olddims [ oj ++ ] ; } } /* Check whether the original axes can be combined */ for ( ok = oi ; ok < oj - 1 ; ok ++ ) { if ( isFOrder ) { if ( oldstrides [ ok + 1 ] != olddims [ ok ] * oldstrides [ ok ] ) { /* not contiguous enough */ return null ; } } else { /* C order */ if ( oldstrides [ ok ] != olddims [ ok + 1 ] * oldstrides [ ok + 1 ] ) { /* not contiguous enough */ return null ; } } } /* Calculate new strides for all axes currently worked with */ if ( isFOrder ) { newStrides [ ni ] = oldstrides [ oi ] ; for ( nk = ni + 1 ; nk < nj ; nk ++ ) { newStrides [ nk ] = newStrides [ nk - 1 ] * newShape [ nk - 1 ] ; } } else { /* C order */ newStrides [ nj - 1 ] = oldstrides [ oj - 1 ] ; for ( nk = nj - 1 ; nk > ni ; nk -- ) { newStrides [ nk - 1 ] = newStrides [ nk ] * newShape [ nk ] ; } } ni = nj ++ ; oi = oj ++ ; } /*
         * Set strides corresponding to trailing 1s of the new shape.
         */ if ( ni >= 1 ) { last_stride = newStrides [ ni - 1 ] ; } else { last_stride = arr . elementStride ( ) ; } if ( isFOrder && ni >= 1 ) { last_stride *= newShape [ ni - 1 ] ; } for ( nk = ni ; nk < newShape . length ; nk ++ ) { newStrides [ nk ] = last_stride ; } if ( arr instanceof IComplexNDArray ) //return Nd4j.createComplex(arr.data(), newShape, newStrides, arr.offset()); throw new UnsupportedOperationException ( ) ; INDArray ret = Nd4j . create ( arr . data ( ) , newShape , newStrides , arr . offset ( ) , isFOrder ? ' ' : ' ' ) ; return ret ; }


Input: Infer the order for the ndarray based on the array s strides [CODESPLIT] public static char getOrder ( INDArray arr ) { return getOrder ( arr . shape ( ) , arr . stride ( ) , arr . elementStride ( ) ) ; }


Input: Compute the offset for the given array given the indices [CODESPLIT] public static long offsetFor ( INDArray arr , int [ ] indexes ) { ShapeOffsetResolution resolution = new ShapeOffsetResolution ( arr ) ; resolution . exec ( Shape . toIndexes ( indexes ) ) ; return resolution . getOffset ( ) ; }


Input: Convert the given int indexes to nd array indexes [CODESPLIT] public static INDArrayIndex [ ] toIndexes ( int [ ] indices ) { INDArrayIndex [ ] ret = new INDArrayIndex [ indices . length ] ; for ( int i = 0 ; i < ret . length ; i ++ ) ret [ i ] = new NDArrayIndex ( indices [ i ] ) ; return ret ; }


Input: Get array shape from an int [] [CODESPLIT] public static int [ ] shape ( int [ ] buffer ) { int [ ] ret = new int [ rank ( buffer ) ] ; for ( int i = 0 ; i < ret . length ; i ++ ) ret [ i ] = buffer [ 1 + i ] ; return ret ; }


Input:
Extracts files to the specified destination [CODESPLIT] public static void unzipFileTo ( String file , String dest ) throws IOException { File target = new File ( file ) ; if ( ! target . exists ( ) ) throw new IllegalArgumentException ( "Archive doesnt exist" ) ; FileInputStream fin = new FileInputStream ( target ) ; int BUFFER = 2048 ; byte data [ ] = new byte [ BUFFER ] ; if ( file . endsWith ( ".zip" ) || file . endsWith ( ".jar" ) ) { try ( ZipInputStream zis = new ZipInputStream ( fin ) ) { //get the zipped file list entry ZipEntry ze = zis . getNextEntry ( ) ; while ( ze != null ) { String fileName = ze . getName ( ) ; File newFile = new File ( dest + File . separator + fileName ) ; if ( ze . isDirectory ( ) ) { newFile . mkdirs ( ) ; zis . closeEntry ( ) ; ze = zis . getNextEntry ( ) ; continue ; } FileOutputStream fos = new FileOutputStream ( newFile ) ; int len ; while ( ( len = zis . read ( data ) ) > 0 ) { fos . write ( data , 0 , len ) ; } fos . close ( ) ; ze = zis . getNextEntry ( ) ; log . debug ( "File extracted: " + newFile . getAbsoluteFile ( ) ) ; } zis . closeEntry ( ) ; } } else if ( file . endsWith ( ".tar.gz" ) || file . endsWith ( ".tgz" ) ) { BufferedInputStream in = new BufferedInputStream ( fin ) ; GzipCompressorInputStream gzIn = new GzipCompressorInputStream ( in ) ; TarArchiveInputStream tarIn = new TarArchiveInputStream ( gzIn ) ; TarArchiveEntry entry ; /* Read the tar entries using the getNextEntry method **/ while ( ( entry = ( TarArchiveEntry ) tarIn . getNextEntry ( ) ) != null ) { log . info ( "Extracting: " + entry . getName ( ) ) ; /* If the entry is a directory, create the directory. */ if ( entry . isDirectory ( ) ) { File f = new File ( dest + File . separator + entry . getName ( ) ) ; f . mkdirs ( ) ; } /*
                 * If the entry is a file,write the decompressed file to the disk
                 * and close destination stream.
                 */ else { int count ; try ( FileOutputStream fos = new FileOutputStream ( dest + File . separator + entry . getName ( ) ) ; BufferedOutputStream destStream = new BufferedOutputStream ( fos , BUFFER ) ; ) { while ( ( count = tarIn . read ( data , 0 , BUFFER ) ) != - 1 ) { destStream . write ( data , 0 , count ) ; } destStream . flush ( ) ; IOUtils . closeQuietly ( destStream ) ; } } } // Close the input stream tarIn . close ( ) ; } else if ( file . endsWith ( ".gz" ) ) { File extracted = new File ( target . getParent ( ) , target . getName ( ) . replace ( ".gz" , "" ) ) ; if ( extracted . exists ( ) ) extracted . delete ( ) ; extracted . createNewFile ( ) ; try ( GZIPInputStream is2 = new GZIPInputStream ( fin ) ; OutputStream fos = FileUtils . openOutputStream ( extracted ) ) { IOUtils . copyLarge ( is2 , fos ) ; fos . flush ( ) ; } } else { throw new IllegalStateException ( "Unable to infer file type (compression format) from source file name: " + file ) ; } target . delete ( ) ; }


Input: Special method for [CODESPLIT] protected short getShort ( long i ) { if ( dataType ( ) != Type . HALF ) throw new UnsupportedOperationException ( "getShort() is supported for Half-precision buffers only" ) ; return fromFloat ( ( ( HalfIndexer ) indexer ) . get ( offset ( ) + i ) ) ; }


Input: Reallocate the native memory of the buffer [CODESPLIT] @ Override public DataBuffer reallocate ( long length ) { Pointer oldPointer = pointer ; if ( isAttached ( ) ) { long capacity = length * getElementSize ( ) ; switch ( dataType ( ) ) { case DOUBLE : pointer = getParentWorkspace ( ) . alloc ( capacity , Type . DOUBLE , false ) . asDoublePointer ( ) ; indexer = DoubleIndexer . create ( ( DoublePointer ) pointer ) ; break ; case FLOAT : pointer = getParentWorkspace ( ) . alloc ( capacity , Type . FLOAT , false ) . asFloatPointer ( ) ; indexer = FloatIndexer . create ( ( FloatPointer ) pointer ) ; break ; case INT : pointer = getParentWorkspace ( ) . alloc ( capacity , Type . INT , false ) . asIntPointer ( ) ; indexer = IntIndexer . create ( ( IntPointer ) pointer ) ; break ; } workspaceGenerationId = getParentWorkspace ( ) . getGenerationId ( ) ; } else { switch ( dataType ( ) ) { case INT : pointer = new IntPointer ( length ) ; indexer = IntIndexer . create ( ( IntPointer ) pointer ) ; break ; case DOUBLE : pointer = new DoublePointer ( length ) ; indexer = DoubleIndexer . create ( ( DoublePointer ) pointer ) ; break ; case FLOAT : pointer = new FloatPointer ( length ) ; indexer = FloatIndexer . create ( ( FloatPointer ) pointer ) ; break ; } } Pointer . memcpy ( pointer , oldPointer , this . length ( ) * getElementSize ( ) ) ; //this.underlyingLength = length; return this ; }


Input: This method executes given CustomOp [CODESPLIT] public void exec ( CustomOp op ) { Nd4j . getExecutioner ( ) . commit ( ) ; if ( op . opName ( ) . equalsIgnoreCase ( "im2col" ) ) { val dtype = Nd4j . dataType ( ) ; val xArr = op . inputArguments ( ) [ 0 ] ; val zArr = op . outputArguments ( ) [ 0 ] ; CudaContext context = AtomicAllocator . getInstance ( ) . getFlowController ( ) . prepareAction ( zArr , xArr ) ; if ( extraz . get ( ) == null ) extraz . set ( new PointerPointer ( 32 ) ) ; PointerPointer xShapeHost = extraz . get ( ) . put ( AddressRetriever . retrieveHostPointer ( xArr . shapeInfoDataBuffer ( ) ) , // 0 context . getOldStream ( ) , // 1 AtomicAllocator . getInstance ( ) . getDeviceIdPointer ( ) , // 2 context . getBufferAllocation ( ) , // 3 context . getBufferReduction ( ) , // 4 context . getBufferScalar ( ) , // 5 context . getBufferSpecial ( ) , null , AddressRetriever . retrieveHostPointer ( zArr . shapeInfoDataBuffer ( ) ) ) ; val x = AtomicAllocator . getInstance ( ) . getPointer ( xArr , context ) ; val z = AtomicAllocator . getInstance ( ) . getPointer ( zArr , context ) ; val xShape = AtomicAllocator . getInstance ( ) . getPointer ( xArr . shapeInfoDataBuffer ( ) , context ) ; val zShape = AtomicAllocator . getInstance ( ) . getPointer ( zArr . shapeInfoDataBuffer ( ) , context ) ; double zeroPad = 0.0 ; if ( op . tArgs ( ) != null && op . tArgs ( ) . length > 0 ) { zeroPad = op . tArgs ( ) [ 0 ] ; } val extrass = new double [ ] { op . iArgs ( ) [ 0 ] , op . iArgs ( ) [ 1 ] , op . iArgs ( ) [ 2 ] , op . iArgs ( ) [ 3 ] , op . iArgs ( ) [ 4 ] , op . iArgs ( ) [ 5 ] , op . iArgs ( ) [ 6 ] , op . iArgs ( ) [ 7 ] , op . iArgs ( ) [ 8 ] , zeroPad } ; val extraArgsBuff = Nd4j . getConstantHandler ( ) . getConstantBuffer ( extrass ) ; val extraArgs = AtomicAllocator . getInstance ( ) . getPointer ( extraArgsBuff , context ) ; if ( dtype == DataBuffer . Type . DOUBLE ) { nativeOps . execTransformDouble ( xShapeHost , 37 , ( DoublePointer ) x , ( LongPointer ) xShape , ( DoublePointer ) z , ( LongPointer ) zShape , ( DoublePointer ) extraArgs ) ; } else if ( dtype == DataBuffer . Type . FLOAT ) { nativeOps . execTransformFloat ( xShapeHost , 37 , ( FloatPointer ) x , ( LongPointer ) xShape , ( FloatPointer ) z , ( LongPointer ) zShape , ( FloatPointer ) extraArgs ) ; } else if ( dtype == DataBuffer . Type . HALF ) { nativeOps . execTransformHalf ( xShapeHost , 37 , ( ShortPointer ) x , ( LongPointer ) xShape , ( ShortPointer ) z , ( LongPointer ) zShape , ( ShortPointer ) extraArgs ) ; } //AtomicAllocator.getInstance().getAllocationPoint(zArr).tickDeviceWrite(); AtomicAllocator . getInstance ( ) . getFlowController ( ) . registerAction ( context , zArr , xArr ) ; //Nd4j.getExecutioner().commit(); return ; } else if ( op . opName ( ) . equalsIgnoreCase ( "col2im" ) ) { val dtype = Nd4j . dataType ( ) ; val xArr = op . inputArguments ( ) [ 0 ] ; val zArr = op . outputArguments ( ) [ 0 ] ; CudaContext context = AtomicAllocator . getInstance ( ) . getFlowController ( ) . prepareAction ( zArr , xArr ) ; if ( extraz . get ( ) == null ) extraz . set ( new PointerPointer ( 32 ) ) ; PointerPointer xShapeHost = extraz . get ( ) . put ( AddressRetriever . retrieveHostPointer ( xArr . shapeInfoDataBuffer ( ) ) , // 0 context . getOldStream ( ) , // 1 AtomicAllocator . getInstance ( ) . getDeviceIdPointer ( ) , // 2 context . getBufferAllocation ( ) , // 3 context . getBufferReduction ( ) , // 4 context . getBufferScalar ( ) , // 5 context . getBufferSpecial ( ) , null , AddressRetriever . retrieveHostPointer ( zArr . shapeInfoDataBuffer ( ) ) ) ; val x = AtomicAllocator . getInstance ( ) . getPointer ( xArr , context ) ; val z = AtomicAllocator . getInstance ( ) . getPointer ( zArr , context ) ; val xShape = AtomicAllocator . getInstance ( ) . getPointer ( xArr . shapeInfoDataBuffer ( ) , context ) ; val zShape = AtomicAllocator . getInstance ( ) . getPointer ( zArr . shapeInfoDataBuffer ( ) , context ) ; val extrass = new double [ ] { op . iArgs ( ) [ 0 ] , op . iArgs ( ) [ 1 ] , op . iArgs ( ) [ 2 ] , op . iArgs ( ) [ 3 ] , op . iArgs ( ) [ 4 ] , op . iArgs ( ) [ 5 ] , op . iArgs ( ) [ 6 ] , op . iArgs ( ) [ 7 ] } ; val extraArgsBuff = Nd4j . getConstantHandler ( ) . getConstantBuffer ( extrass ) ; val extraArgs = AtomicAllocator . getInstance ( ) . getPointer ( extraArgsBuff , context ) ; if ( dtype == DataBuffer . Type . DOUBLE ) { nativeOps . execTransformDouble ( xShapeHost , 36 , ( DoublePointer ) x , ( LongPointer ) xShape , ( DoublePointer ) z , ( LongPointer ) zShape , ( DoublePointer ) extraArgs ) ; } else if ( dtype == DataBuffer . Type . FLOAT ) { nativeOps . execTransformFloat ( xShapeHost , 36 , ( FloatPointer ) x , ( LongPointer ) xShape , ( FloatPointer ) z , ( LongPointer ) zShape , ( FloatPointer ) extraArgs ) ; } else if ( dtype == DataBuffer . Type . HALF ) { nativeOps . execTransformHalf ( xShapeHost , 36 , ( ShortPointer ) x , ( LongPointer ) xShape , ( ShortPointer ) z , ( LongPointer ) zShape , ( ShortPointer ) extraArgs ) ; } //AtomicAllocator.getInstance().getAllocationPoint(zArr).tickDeviceWrite(); AtomicAllocator . getInstance ( ) . getFlowController ( ) . registerAction ( context , zArr , xArr ) ; //Nd4j.getExecutioner().commit(); return ; } else if ( op . opName ( ) . equalsIgnoreCase ( "pooling2d" ) ) { val dtype = Nd4j . dataType ( ) ; val xArr = op . inputArguments ( ) [ 0 ] ; val zArr = op . outputArguments ( ) [ 0 ] ; CudaContext context = AtomicAllocator . getInstance ( ) . getFlowController ( ) . prepareAction ( zArr , xArr ) ; if ( extraz . get ( ) == null ) extraz . set ( new PointerPointer ( 32 ) ) ; PointerPointer xShapeHost = extraz . get ( ) . put ( AddressRetriever . retrieveHostPointer ( xArr . shapeInfoDataBuffer ( ) ) , // 0 context . getOldStream ( ) , // 1 AtomicAllocator . getInstance ( ) . getDeviceIdPointer ( ) , // 2 context . getBufferAllocation ( ) , // 3 context . getBufferReduction ( ) , // 4 context . getBufferScalar ( ) , // 5 context . getBufferSpecial ( ) , null , AddressRetriever . retrieveHostPointer ( zArr . shapeInfoDataBuffer ( ) ) ) ; val x = AtomicAllocator . getInstance ( ) . getPointer ( xArr , context ) ; val z = AtomicAllocator . getInstance ( ) . getPointer ( zArr , context ) ; val xShape = AtomicAllocator . getInstance ( ) . getPointer ( xArr . shapeInfoDataBuffer ( ) , context ) ; val zShape = AtomicAllocator . getInstance ( ) . getPointer ( zArr . shapeInfoDataBuffer ( ) , context ) ; val extrass = new double [ ] { op . iArgs ( ) [ 0 ] , op . iArgs ( ) [ 1 ] , op . iArgs ( ) [ 2 ] , op . iArgs ( ) [ 3 ] , op . iArgs ( ) [ 4 ] , op . iArgs ( ) [ 5 ] , op . iArgs ( ) [ 6 ] , op . iArgs ( ) [ 7 ] , op . iArgs ( ) [ 8 ] } ; val extraArgsBuff = Nd4j . getConstantHandler ( ) . getConstantBuffer ( extrass ) ; val extraArgs = AtomicAllocator . getInstance ( ) . getPointer ( extraArgsBuff , context ) ; if ( dtype == DataBuffer . Type . DOUBLE ) { nativeOps . execTransformDouble ( xShapeHost , 71 , ( DoublePointer ) x , ( LongPointer ) xShape , ( DoublePointer ) z , ( LongPointer ) zShape , ( DoublePointer ) extraArgs ) ; } else if ( dtype == DataBuffer . Type . FLOAT ) { nativeOps . execTransformFloat ( xShapeHost , 71 , ( FloatPointer ) x , ( LongPointer ) xShape , ( FloatPointer ) z , ( LongPointer ) zShape , ( FloatPointer ) extraArgs ) ; } else if ( dtype == DataBuffer . Type . HALF ) { nativeOps . execTransformHalf ( xShapeHost , 71 , ( ShortPointer ) x , ( LongPointer ) xShape , ( ShortPointer ) z , ( LongPointer ) zShape , ( ShortPointer ) extraArgs ) ; } // AtomicAllocator.getInstance().getAllocationPoint(zArr).tickDeviceWrite(); AtomicAllocator . getInstance ( ) . getFlowController ( ) . registerAction ( context , zArr , xArr ) ; //Nd4j.getExecutioner().commit(); return ; } Nd4j . getExecutioner ( ) . commit ( ) ; long st = profilingHookIn ( op ) ; CudaContext context = ( CudaContext ) AtomicAllocator . getInstance ( ) . getDeviceContext ( ) . getContext ( ) ; //AtomicAllocator.getInstance().getFlowController().prepareActionAllWrite(op.outputArguments()); if ( extraz . get ( ) == null ) extraz . set ( new PointerPointer ( 32 ) ) ; PointerPointer extras = extraz . get ( ) . put ( new CudaPointer ( 1 ) , context . getOldStream ( ) , context . getBufferScalar ( ) , context . getBufferReduction ( ) ) ; val outputArgs = op . outputArguments ( ) ; val inputArgs = op . inputArguments ( ) ; if ( outputArgs . length == 0 && ! op . isInplaceCall ( ) ) throw new ND4JIllegalStateException ( "You can't execute non-inplace CustomOp without outputs being specified" ) ; val lc = op . opName ( ) . toLowerCase ( ) ; val hash = op . opHash ( ) ; val inputShapes = new PointerPointer <> ( inputArgs . length * 2 ) ; val inputBuffers = new PointerPointer <> ( inputArgs . length * 2 ) ; int cnt = 0 ; for ( val in : inputArgs ) { val hp = AtomicAllocator . getInstance ( ) . getHostPointer ( in . shapeInfoDataBuffer ( ) ) ; inputBuffers . put ( cnt , AtomicAllocator . getInstance ( ) . getHostPointer ( in ) ) ; inputShapes . put ( cnt , hp ) ; val dp = AtomicAllocator . getInstance ( ) . getPointer ( in . shapeInfoDataBuffer ( ) , context ) ; inputBuffers . put ( cnt + inputArgs . length , AtomicAllocator . getInstance ( ) . getPointer ( in , context ) ) ; inputShapes . put ( cnt + inputArgs . length , dp ) ; if ( op . isInplaceCall ( ) ) AtomicAllocator . getInstance ( ) . getAllocationPoint ( in ) . tickHostWrite ( ) ; cnt ++ ; } val outputShapes = new PointerPointer <> ( outputArgs . length * 2 ) ; val outputBuffers = new PointerPointer <> ( outputArgs . length * 2 ) ; cnt = 0 ; for ( val out : outputArgs ) { outputBuffers . put ( cnt , AtomicAllocator . getInstance ( ) . getHostPointer ( out ) ) ; outputShapes . put ( cnt , AtomicAllocator . getInstance ( ) . getHostPointer ( out . shapeInfoDataBuffer ( ) ) ) ; outputBuffers . put ( cnt + outputArgs . length , AtomicAllocator . getInstance ( ) . getPointer ( out , context ) ) ; outputShapes . put ( cnt + outputArgs . length , AtomicAllocator . getInstance ( ) . getPointer ( out . shapeInfoDataBuffer ( ) , context ) ) ; AtomicAllocator . getInstance ( ) . getAllocationPoint ( out ) . tickHostWrite ( ) ; cnt ++ ; } if ( Nd4j . dataType ( ) == DataBuffer . Type . FLOAT ) { val tArgs = op . tArgs ( ) . length > 0 ? new FloatPointer ( op . tArgs ( ) . length ) : null ; val iArgs = op . iArgs ( ) . length > 0 ? new LongPointer ( op . iArgs ( ) . length ) : null ; cnt = 0 ; for ( val t : op . tArgs ( ) ) tArgs . put ( cnt ++ , ( float ) t ) ; cnt = 0 ; for ( val i : op . iArgs ( ) ) iArgs . put ( cnt ++ , i ) ; val status = OpStatus . byNumber ( nativeOps . execCustomOpFloat ( extras , hash , inputBuffers , inputShapes , inputArgs . length , outputBuffers , outputShapes , outputArgs . length , tArgs , op . tArgs ( ) . length , iArgs , op . iArgs ( ) . length , 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 . tArgs ( ) . length > 0 ? new DoublePointer ( op . tArgs ( ) . length ) : null ; val iArgs = op . iArgs ( ) . length > 0 ? new LongPointer ( op . iArgs ( ) . length ) : null ; cnt = 0 ; for ( val t : op . tArgs ( ) ) tArgs . put ( cnt ++ , t ) ; for ( val i : op . iArgs ( ) ) iArgs . put ( cnt ++ , i ) ; val status = OpStatus . byNumber ( nativeOps . execCustomOpDouble ( extras , hash , inputBuffers , inputShapes , inputArgs . length , outputBuffers , outputShapes , outputArgs . length , tArgs , op . tArgs ( ) . length , iArgs , op . iArgs ( ) . length , op . isInplaceCall ( ) ) ) ; if ( status != OpStatus . ND4J_STATUS_OK ) throw new ND4JIllegalStateException ( "Op execution failed: " + status ) ; } else if ( Nd4j . dataType ( ) == DataBuffer . Type . HALF ) { val tArgs = op . tArgs ( ) . length > 0 ? new ShortPointer ( op . tArgs ( ) . length ) : null ; val iArgs = op . iArgs ( ) . length > 0 ? new LongPointer ( op . iArgs ( ) . length ) : null ; cnt = 0 ; for ( val t : op . tArgs ( ) ) tArgs . put ( cnt ++ , ArrayUtil . toHalf ( ( float ) t ) ) ; cnt = 0 ; for ( val i : op . iArgs ( ) ) iArgs . put ( cnt ++ , i ) ; val status = OpStatus . byNumber ( nativeOps . execCustomOpHalf ( extras , hash , inputBuffers , inputShapes , inputArgs . length , outputBuffers , outputShapes , outputArgs . length , tArgs , op . tArgs ( ) . length , iArgs , op . iArgs ( ) . length , op . isInplaceCall ( ) ) ) ; if ( status != OpStatus . ND4J_STATUS_OK ) throw new ND4JIllegalStateException ( "Op execution failed: " + status ) ; } //AtomicAllocator.getInstance().getFlowController().prepareActionAllWrite(op.outputArguments()); profilingHookOut ( op , st ) ; }


Input: Copy from the given from buffer to the to buffer at the specified offsets and strides [CODESPLIT] public static void copyAtStride ( int n , BufferType bufferType , ByteBuffer from , int fromOffset , int fromStride , ByteBuffer to , int toOffset , int toStride ) { // TODO: implement shape copy for cases where stride == 1 ByteBuffer fromView = from ; ByteBuffer toView = to ; fromView . order ( ByteOrder . nativeOrder ( ) ) ; toView . order ( ByteOrder . nativeOrder ( ) ) ; switch ( bufferType ) { case INT : IntBuffer fromInt = fromView . asIntBuffer ( ) ; IntBuffer toInt = toView . asIntBuffer ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int put = fromInt . get ( fromOffset + i * fromStride ) ; toInt . put ( toOffset + i * toStride , put ) ; } break ; case FLOAT : FloatBuffer fromFloat = fromView . asFloatBuffer ( ) ; FloatBuffer toFloat = toView . asFloatBuffer ( ) ; for ( int i = 0 ; i < n ; i ++ ) { float put = fromFloat . get ( fromOffset + i * fromStride ) ; toFloat . put ( toOffset + i * toStride , put ) ; } break ; case DOUBLE : DoubleBuffer fromDouble = fromView . asDoubleBuffer ( ) ; DoubleBuffer toDouble = toView . asDoubleBuffer ( ) ; for ( int i = 0 ; i < n ; i ++ ) { toDouble . put ( toOffset + i * toStride , fromDouble . get ( fromOffset + i * fromStride ) ) ; } break ; default : throw new IllegalArgumentException ( "Only floats and double supported" ) ; } }


Input: Start a server based on the given subscriber . Note that for the port to start the server on you should set the statusServerPortField on the subscriber either manually or via command line . The server defaults to port 9000 . [CODESPLIT] public static Server startServer ( StatusStorage statusStorage , int statusServerPort ) { log . info ( "Starting server on port " + statusServerPort ) ; RoutingDsl dsl = new RoutingDsl ( ) ; dsl . GET ( "/ids/" ) . routeTo ( new F . Function0 < Result > ( ) { @ Override public Result apply ( ) throws Throwable { List < Integer > ids = statusStorage . ids ( ) ; return ok ( toJson ( ids ) ) ; } } ) ; dsl . GET ( "/state/:id" ) . routeTo ( new F . Function < String , Result > ( ) { @ Override public Result apply ( String id ) throws Throwable { return ok ( toJson ( statusStorage . getState ( Integer . parseInt ( id ) ) ) ) ; } } ) ; dsl . GET ( "/opType/:id" ) . routeTo ( new F . Function < String , Result > ( ) { @ Override public Result apply ( String id ) throws Throwable { return ok ( toJson ( ServerTypeJson . builder ( ) . type ( statusStorage . getState ( Integer . parseInt ( id ) ) . serverType ( ) ) ) ) ; } } ) ; dsl . GET ( "/started/:id" ) . routeTo ( new F . Function < String , Result > ( ) { @ Override public Result apply ( String id ) throws Throwable { return statusStorage . getState ( Integer . parseInt ( id ) ) . isMaster ( ) ? ok ( toJson ( MasterStatus . builder ( ) . master ( statusStorage . getState ( Integer . parseInt ( id ) ) . getServerState ( ) ) //note here that a responder is is + 1 . responder ( statusStorage . getState ( Integer . parseInt ( id ) + 1 ) . getServerState ( ) ) . responderN ( statusStorage . getState ( Integer . parseInt ( id ) ) . getTotalUpdates ( ) ) . build ( ) ) ) : ok ( toJson ( SlaveStatus . builder ( ) . slave ( statusStorage . getState ( Integer . parseInt ( id ) ) . serverType ( ) ) . build ( ) ) ) ; } } ) ; dsl . GET ( "/connectioninfo/:id" ) . routeTo ( new F . Function < String , Result > ( ) { @ Override public Result apply ( String id ) throws Throwable { return ok ( toJson ( statusStorage . getState ( Integer . parseInt ( id ) ) . getConnectionInfo ( ) ) ) ; } } ) ; dsl . POST ( "/updatestatus/:id" ) . routeTo ( new F . Function < String , Result > ( ) { @ Override public Result apply ( String id ) throws Throwable { SubscriberState subscriberState = Json . fromJson ( request ( ) . body ( ) . asJson ( ) , SubscriberState . class ) ; statusStorage . updateState ( subscriberState ) ; return ok ( toJson ( subscriberState ) ) ; } } ) ; Server server = Server . forRouter ( dsl . build ( ) , Mode . PROD , statusServerPort ) ; return server ; }


Input: This method moves specified dataBuffer to CUDA constant memory space . [CODESPLIT] @ Override public synchronized long moveToConstantSpace ( DataBuffer dataBuffer ) { // now, we move things to constant memory Integer deviceId = AtomicAllocator . getInstance ( ) . getDeviceId ( ) ; ensureMaps ( deviceId ) ; AllocationPoint point = AtomicAllocator . getInstance ( ) . getAllocationPoint ( dataBuffer ) ; long requiredMemoryBytes = AllocationUtils . getRequiredMemory ( point . getShape ( ) ) ; //logger.info("shape: " + point.getShape()); // and release device memory :) long currentOffset = constantOffsets . get ( deviceId ) . get ( ) ; CudaContext context = ( CudaContext ) AtomicAllocator . getInstance ( ) . getDeviceContext ( ) . getContext ( ) ; if ( currentOffset + requiredMemoryBytes >= MAX_CONSTANT_LENGTH || requiredMemoryBytes > MAX_BUFFER_LENGTH ) { if ( point . getAllocationStatus ( ) == AllocationStatus . HOST && CudaEnvironment . getInstance ( ) . getConfiguration ( ) . getMemoryModel ( ) == Configuration . MemoryModel . DELAYED ) { AtomicAllocator . getInstance ( ) . getMemoryHandler ( ) . alloc ( AllocationStatus . DEVICE , point , point . getShape ( ) , false ) ; } val profD = PerformanceTracker . getInstance ( ) . helperStartTransaction ( ) ; if ( NativeOpsHolder . getInstance ( ) . getDeviceNativeOps ( ) . memcpyAsync ( point . getPointers ( ) . getDevicePointer ( ) , point . getPointers ( ) . getHostPointer ( ) , requiredMemoryBytes , 1 , context . getSpecialStream ( ) ) == 0 ) { throw new ND4JIllegalStateException ( "memcpyAsync failed" ) ; } flowController . commitTransfer ( context . getSpecialStream ( ) ) ; PerformanceTracker . getInstance ( ) . helperRegisterTransaction ( point . getDeviceId ( ) , profD , point . getNumberOfBytes ( ) , MemcpyDirection . HOST_TO_DEVICE ) ; point . setConstant ( true ) ; point . tickDeviceWrite ( ) ; point . tickHostRead ( ) ; point . setDeviceId ( deviceId ) ; protector . persistDataBuffer ( dataBuffer ) ; return 0 ; } long bytes = requiredMemoryBytes ; // hack for misalignment avoidance for 16bit data opType if ( dataBuffer . dataType ( ) == DataBuffer . Type . HALF ) { if ( bytes % 4 != 0 ) { bytes += 2 ; } } else if ( Nd4j . dataType ( ) == DataBuffer . Type . DOUBLE || dataBuffer . dataType ( ) == DataBuffer . Type . LONG ) { // for double data opType, we must be assured, that all DOUBLE pointers are starting from even addresses, to avoid banks spills long div = bytes / 4 ; if ( div % 2 != 0 ) bytes += 4 ; // for possible changes of dtype in the same jvm, we skip few bytes in constant memory div = currentOffset / 4 ; while ( div % 2 != 0 ) { currentOffset = constantOffsets . get ( deviceId ) . addAndGet ( 4 ) ; div = currentOffset / 4 ; // just break out, if we're stepped beyond constant memory space if ( currentOffset > MAX_CONSTANT_LENGTH ) break ; } } currentOffset = constantOffsets . get ( deviceId ) . getAndAdd ( bytes ) ; if ( currentOffset >= MAX_CONSTANT_LENGTH ) { if ( point . getAllocationStatus ( ) == AllocationStatus . HOST && CudaEnvironment . getInstance ( ) . getConfiguration ( ) . getMemoryModel ( ) == Configuration . MemoryModel . DELAYED ) { AtomicAllocator . getInstance ( ) . getMemoryHandler ( ) . alloc ( AllocationStatus . DEVICE , point , point . getShape ( ) , false ) ; } val profD = PerformanceTracker . getInstance ( ) . helperStartTransaction ( ) ; if ( NativeOpsHolder . getInstance ( ) . getDeviceNativeOps ( ) . memcpyAsync ( point . getPointers ( ) . getDevicePointer ( ) , point . getPointers ( ) . getHostPointer ( ) , requiredMemoryBytes , 1 , context . getSpecialStream ( ) ) == 0 ) { throw new ND4JIllegalStateException ( "memcpyAsync failed" ) ; } flowController . commitTransfer ( context . getSpecialStream ( ) ) ; PerformanceTracker . getInstance ( ) . helperRegisterTransaction ( point . getDeviceId ( ) , profD , point . getNumberOfBytes ( ) , MemcpyDirection . HOST_TO_DEVICE ) ; point . setConstant ( true ) ; point . tickDeviceWrite ( ) ; point . tickHostRead ( ) ; point . setDeviceId ( deviceId ) ; protector . persistDataBuffer ( dataBuffer ) ; return 0 ; } NativeOpsHolder . getInstance ( ) . getDeviceNativeOps ( ) . memcpyConstantAsync ( currentOffset , point . getPointers ( ) . getHostPointer ( ) , requiredMemoryBytes , 1 , context . getSpecialStream ( ) ) ; flowController . commitTransfer ( context . getSpecialStream ( ) ) ; long cAddr = deviceAddresses . get ( deviceId ) . address ( ) + currentOffset ; //if (resetHappened) //    logger.info("copying to constant: {}, bufferLength: {}, bufferDtype: {}, currentOffset: {}, currentAddres: {}", requiredMemoryBytes, dataBuffer.length(), dataBuffer.dataType(), currentOffset, cAddr); point . setAllocationStatus ( AllocationStatus . CONSTANT ) ; point . getPointers ( ) . setDevicePointer ( new CudaPointer ( cAddr ) ) ; point . setConstant ( true ) ; point . tickDeviceWrite ( ) ; point . setDeviceId ( deviceId ) ; point . tickHostRead ( ) ; protector . persistDataBuffer ( dataBuffer ) ; return cAddr ; }


Input: PLEASE NOTE : This method implementation is hardware - dependant . PLEASE NOTE : This method does NOT allow concurrent use of any array [CODESPLIT] @ Override public DataBuffer relocateConstantSpace ( DataBuffer dataBuffer ) { // we always assume that data is sync, and valid on host side Integer deviceId = AtomicAllocator . getInstance ( ) . getDeviceId ( ) ; ensureMaps ( deviceId ) ; if ( dataBuffer instanceof CudaIntDataBuffer ) { int [ ] data = dataBuffer . asInt ( ) ; return getConstantBuffer ( data ) ; } else if ( dataBuffer instanceof CudaFloatDataBuffer ) { float [ ] data = dataBuffer . asFloat ( ) ; return getConstantBuffer ( data ) ; } else if ( dataBuffer instanceof CudaDoubleDataBuffer ) { double [ ] data = dataBuffer . asDouble ( ) ; return getConstantBuffer ( data ) ; } else if ( dataBuffer instanceof CudaHalfDataBuffer ) { float [ ] data = dataBuffer . asFloat ( ) ; return getConstantBuffer ( data ) ; } else if ( dataBuffer instanceof CudaLongDataBuffer ) { long [ ] data = dataBuffer . asLong ( ) ; return getConstantBuffer ( data ) ; } throw new IllegalStateException ( "Unknown CudaDataBuffer opType" ) ; }


Input: This method returns DataBuffer with contant equal to input array . [CODESPLIT] @ Override public DataBuffer getConstantBuffer ( float [ ] array ) { //   logger.info("getConstantBuffer(float[]) called"); ArrayDescriptor descriptor = new ArrayDescriptor ( array ) ; Integer deviceId = AtomicAllocator . getInstance ( ) . getDeviceId ( ) ; ensureMaps ( deviceId ) ; if ( ! buffersCache . get ( deviceId ) . containsKey ( descriptor ) ) { // we create new databuffer //logger.info("Creating new constant buffer..."); DataBuffer buffer = Nd4j . createBufferDetached ( array ) ; if ( constantOffsets . get ( deviceId ) . get ( ) + ( array . length * Nd4j . sizeOfDataType ( ) ) < MAX_CONSTANT_LENGTH ) { buffer . setConstant ( true ) ; // now we move data to constant memory, and keep happy moveToConstantSpace ( buffer ) ; buffersCache . get ( deviceId ) . put ( descriptor , buffer ) ; bytes . addAndGet ( array . length * Nd4j . sizeOfDataType ( ) ) ; } return buffer ; } // else logger.info("Reusing constant buffer..."); return buffersCache . get ( deviceId ) . get ( descriptor ) ; }


Input:
Based on the passed in array compute the shape offsets and strides for the given indexes @param indexes the indexes to compute this based on [CODESPLIT] public void exec ( INDArrayIndex ... indexes ) { val shape = arr . shape ( ) ; if ( arr . isSparse ( ) ) { resolveFixedDimensionsCOO ( indexes ) ; } // Check that given point indexes are not out of bounds for ( int i = 0 ; i < indexes . length ; i ++ ) { INDArrayIndex idx = indexes [ i ] ; // On vectors, the first dimension can be ignored when indexing them with a single point index if ( idx instanceof PointIndex && ( arr . isVector ( ) && indexes . length == 1 ? idx . current ( ) >= shape [ i + 1 ] : idx . current ( ) >= shape [ i ] ) ) { throw new IllegalArgumentException ( "INDArrayIndex[" + i + "] is out of bounds (value: " + idx . current ( ) + ")" ) ; } } indexes = NDArrayIndex . resolve ( arr . shapeInfoDataBuffer ( ) , indexes ) ; if ( tryShortCircuit ( indexes ) ) { return ; } int numIntervals = 0 ; //number of new axes dimensions to prepend to the beginning int newAxesPrepend = 0 ; //whether we have encountered an all so far boolean encounteredAll = false ; int lastPrependIndex = - 1 ; List < Integer > oneDimensionWithAllEncountered = new ArrayList <> ( ) ; //accumulate the results List < Long > accumShape = new ArrayList <> ( ) ; List < Long > accumStrides = new ArrayList <> ( ) ; List < Long > accumOffsets = new ArrayList <> ( ) ; List < Long > intervalStrides = new ArrayList <> ( ) ; //collect the indexes of the points that get removed //for point purposes //this will be used to compute the offset //for the new array List < Long > pointStrides = new ArrayList <> ( ) ; List < Long > pointOffsets = new ArrayList <> ( ) ; int numPointIndexes = 0 ; //bump number to read from the shape int shapeIndex = 0 ; //stride index to read strides from the array int strideIndex = 0 ; //list of indexes to prepend to for new axes //if all is encountered List < Integer > prependNewAxes = new ArrayList <> ( ) ; for ( int i = 0 ; i < indexes . length ; i ++ ) { INDArrayIndex idx = indexes [ i ] ; if ( idx instanceof NDArrayIndexAll ) { encounteredAll = true ; if ( i < arr . rank ( ) && arr . size ( i ) == 1 ) oneDimensionWithAllEncountered . add ( i ) ; //different dimension from new axis (look for new axis dimensions //at at the beginning. track when the last new axis is encountered. if ( newAxesPrepend > 0 && lastPrependIndex < 0 ) { lastPrependIndex = i - 1 ; } } //point: do nothing but move the shape counter //also move the stride counter if ( idx instanceof PointIndex ) { pointOffsets . add ( idx . offset ( ) ) ; pointStrides . add ( ( long ) arr . stride ( strideIndex ) ) ; numPointIndexes ++ ; shapeIndex ++ ; strideIndex ++ ; //different dimension from new axis (look for new axis dimensions //at at the beginning. track when the last new axis is encountered. if ( newAxesPrepend > 0 && lastPrependIndex < 0 ) { lastPrependIndex = i - 1 ; } continue ; } //new axes encountered, need to track whether to prepend or //to set the new axis in the middle else if ( idx instanceof NewAxis ) { //prepend the new axes at different indexes accumShape . add ( 1L ) ; accumOffsets . add ( 0L ) ; accumStrides . add ( 0L ) ; prependNewAxes . add ( i ) ; continue ; } //points and intervals both have a direct desired length else if ( idx instanceof IntervalIndex && ! ( idx instanceof NDArrayIndexAll ) || idx instanceof SpecifiedIndex ) { if ( idx instanceof IntervalIndex ) { accumStrides . add ( arr . stride ( strideIndex ) * idx . stride ( ) ) ; //used in computing an adjusted offset for the augmented strides intervalStrides . add ( idx . stride ( ) ) ; numIntervals ++ ; } else accumStrides . add ( ( long ) arr . stride ( strideIndex ) ) ; accumShape . add ( idx . length ( ) ) ; //the stride stays the same //add the offset for the index if ( idx instanceof IntervalIndex ) { accumOffsets . add ( idx . offset ( ) ) ; } else accumOffsets . add ( idx . offset ( ) ) ; shapeIndex ++ ; strideIndex ++ ; //different dimension from new axis (look for new axis dimensions //at at the beginning. track when the last new axis is encountered. if ( newAxesPrepend > 0 && lastPrependIndex < 0 ) { lastPrependIndex = i - 1 ; } continue ; } //add the shape and stride //based on the original stride/shape accumShape . add ( ( long ) shape [ shapeIndex ++ ] ) ; //account for erroneous strides from dimensions of size 1 //move the stride index if its one and fill it in at the bottom accumStrides . add ( ( long ) arr . stride ( strideIndex ++ ) ) ; //default offsets are zero accumOffsets . add ( idx . offset ( ) ) ; } //fill in missing strides and shapes while ( shapeIndex < shape . length ) { //scalar, should be 1 x 1 rather than the number of columns in the vector if ( Shape . isVector ( shape ) ) { accumShape . add ( 1L ) ; shapeIndex ++ ; } else accumShape . add ( ( long ) shape [ shapeIndex ++ ] ) ; } //fill in the rest of the offsets with zero int delta = ( shape . length <= 2 ? shape . length : shape . length - numPointIndexes ) ; boolean needsFilledIn = accumShape . size ( ) != accumStrides . size ( ) && accumOffsets . size ( ) != accumShape . size ( ) ; while ( accumOffsets . size ( ) < delta && needsFilledIn ) accumOffsets . add ( 0L ) ; while ( accumShape . size ( ) < 2 ) { if ( Shape . isRowVectorShape ( arr . shape ( ) ) ) accumShape . add ( 0 , 1L ) ; else accumShape . add ( 1L ) ; } while ( strideIndex < accumShape . size ( ) ) { accumStrides . add ( ( long ) arr . stride ( strideIndex ++ ) ) ; } /**
         * For each dimension
         * where we want to prepend a dimension
         * we need to add it at the index such that
         * we account for the offset of the number of indexes
         * added up to that point.
         *
         * We do this by doing an offset
         * for each item added "so far"
         *
         * Note that we also have an offset of - 1
         * because we want to prepend to the given index.
         *
         * When prepend new axes for in the middle is triggered
         * i is already > 0
         */ /* int numAdded = 0;
        for (int i = 0; i < prependNewAxes.size(); i++) {
            accumShape.add(prependNewAxes.get(i) - numAdded, 1L);
            //stride for the new axis is zero
            accumStrides.add(prependNewAxes.get(i) - numAdded, 0L);
            numAdded++;
        }
        for (int i = 0; i < newAxesPrepend; i++) {
            prependNewAxes.add(0, i);
        }

        prependAxis = Ints.toArray(prependNewAxes);
*/ /**
         * Need to post process strides and offsets
         * for trailing ones here
         */ //prune off extra zeros for trailing and leading ones int trailingZeroRemove = accumOffsets . size ( ) - 1 ; while ( accumOffsets . size ( ) > accumShape . size ( ) ) { if ( accumOffsets . get ( trailingZeroRemove ) == 0 ) accumOffsets . remove ( accumOffsets . size ( ) - 1 ) ; trailingZeroRemove -- ; } if ( accumStrides . size ( ) < accumOffsets . size ( ) ) accumStrides . addAll ( pointStrides ) ; while ( accumOffsets . size ( ) < accumShape . size ( ) ) { if ( Shape . isRowVectorShape ( arr . shape ( ) ) ) accumOffsets . add ( 0 , 0L ) ; else accumOffsets . add ( 0L ) ; } if ( Shape . isMatrix ( shape ) && indexes [ 0 ] instanceof PointIndex && indexes [ 1 ] instanceof NDArrayIndexAll ) { Collections . reverse ( accumShape ) ; } if ( arr . isMatrix ( ) && indexes [ 0 ] instanceof PointIndex && indexes [ 1 ] instanceof IntervalIndex ) { this . shapes = new long [ 2 ] ; shapes [ 0 ] = 1 ; IntervalIndex idx = ( IntervalIndex ) indexes [ 1 ] ; shapes [ 1 ] = idx . length ( ) ; } else this . shapes = Longs . toArray ( accumShape ) ; boolean isColumnVector = Shape . isColumnVectorShape ( this . shapes ) ; //finally fill in teh rest of the strides if any are left over while ( accumStrides . size ( ) < accumOffsets . size ( ) ) { if ( ! isColumnVector ) accumStrides . add ( 0 , ( long ) arr . elementStride ( ) ) ; else accumStrides . add ( ( long ) arr . elementStride ( ) ) ; } this . strides = Longs . toArray ( accumStrides ) ; this . offsets = Longs . toArray ( accumOffsets ) ; //compute point offsets differently /**
         * We need to prepend the strides for the point indexes
         * such that the point index offsets are counted.
         * Note here that we only use point strides
         * when points strides isn't empty.
         * When point strides is empty, this is
         * because a point index was encountered
         * but it was the lead index and therefore should
         * not be counted with the offset.
         *
         *
         * Another thing of note here is that the strides
         * and offsets should line up such that the point
         * and stride match up.
         */ if ( numPointIndexes > 0 && ! pointStrides . isEmpty ( ) ) { //append to the end for tensors if ( newAxesPrepend >= 1 ) { while ( pointStrides . size ( ) < accumOffsets . size ( ) ) { pointStrides . add ( 1L ) ; } //identify in the original accumulate strides //where zero was set and emulate the //same structure in the point strides for ( int i = 0 ; i < accumStrides . size ( ) ; i ++ ) { if ( accumStrides . get ( i ) == 0 && ! ( indexes [ i ] instanceof NewAxis ) && lastPrependIndex <= 0 ) pointStrides . set ( i , 0L ) ; } } //prepend any missing offsets where relevant for the dot product //note here we are using the point offsets and strides //for computing the offset //the point of a point index is to drop a dimension //and index in to a particular offset while ( pointOffsets . size ( ) < pointStrides . size ( ) ) { pointOffsets . add ( 0L ) ; } //special case where offsets aren't caught if ( arr . isRowVector ( ) && ! intervalStrides . isEmpty ( ) && pointOffsets . get ( 0 ) == 0 && ! ( indexes [ 1 ] instanceof IntervalIndex ) ) this . offset = indexes [ 1 ] . offset ( ) ; else this . offset = ArrayUtil . dotProductLong2 ( pointOffsets , pointStrides ) ; } else { this . offset = 0 ; } if ( numIntervals > 0 && arr . rank ( ) > 2 ) { if ( encounteredAll && arr . size ( 0 ) != 1 || indexes [ 0 ] instanceof PointIndex ) // FIXME: LONG this . offset += ArrayUtil . dotProductLong2 ( accumOffsets , accumStrides ) ; else // FIXME: LONG this . offset += ArrayUtil . dotProductLong2 ( accumOffsets , accumStrides ) ; } else if ( numIntervals > 0 && anyHaveStrideOne ( indexes ) ) this . offset += ArrayUtil . calcOffsetLong2 ( accumShape , accumOffsets , accumStrides ) ; else this . offset += ArrayUtil . calcOffsetLong2 ( accumShape , accumOffsets , accumStrides ) / Math . max ( 1 , numIntervals ) ; //collapse singular dimensions with specified index List < Integer > removeShape = new ArrayList <> ( ) ; for ( int i = 0 ; i < Math . min ( this . shapes . length , indexes . length ) ; i ++ ) { if ( this . shapes [ i ] == 1 && indexes [ i ] instanceof SpecifiedIndex ) { removeShape . add ( i ) ; } } if ( ! removeShape . isEmpty ( ) ) { List < Long > newShape = new ArrayList <> ( ) ; List < Long > newStrides = new ArrayList <> ( ) ; for ( int i = 0 ; i < this . shapes . length ; i ++ ) { if ( ! removeShape . contains ( i ) ) { newShape . add ( this . shapes [ i ] ) ; newStrides . add ( this . strides [ i ] ) ; } } this . shapes = Longs . toArray ( newShape ) ; this . strides = Longs . toArray ( newStrides ) ; } }


Input: Returns a kafka connection uri [CODESPLIT] public String kafkaUri ( ) { return String . format ( "kafka://%s?topic=%s&groupId=%s&zookeeperHost=%s&zookeeperPort=%d&serializerClass=%s&keySerializerClass=%s" , kafkaBrokerList , topicName , groupId , zookeeperHost , zookeeperPort , StringEncoder . class . getName ( ) , StringEncoder . class . getName ( ) ) ; }


Input: Cosine similarity [CODESPLIT] public static double cosineSim ( @ NonNull INDArray d1 , @ NonNull INDArray d2 ) { return Nd4j . getExecutioner ( ) . execAndReturn ( new CosineSimilarity ( d1 , d2 , d1 . length ( ) ) ) . getFinalResult ( ) . doubleValue ( ) ; }


Input: Atan2 operation new INDArray instance will be returned Note the order of x and y parameters is opposite to that of java . lang . Math . atan2 [CODESPLIT] public static INDArray atan2 ( @ NonNull INDArray x , @ NonNull INDArray y ) { return Nd4j . getExecutioner ( ) . execAndReturn ( new OldAtan2Op ( x , y , Nd4j . createUninitialized ( x . shape ( ) , x . ordering ( ) ) ) ) ; }


Input: Ceiling function [CODESPLIT] public static INDArray ceiling ( INDArray ndArray , boolean copyOnOps ) { return exec ( copyOnOps ? new Ceil ( ndArray , ndArray . dup ( ) ) : new Ceil ( ndArray , ndArray ) ) ; }


Input: Sin function [CODESPLIT] public static INDArray sin ( INDArray in , boolean copy ) { return Nd4j . getExecutioner ( ) . execAndReturn ( new Sin ( ( copy ? in . dup ( ) : in ) ) ) ; }


Input: Sin function [CODESPLIT] public static INDArray atanh ( INDArray in , boolean copy ) { return Nd4j . getExecutioner ( ) . execAndReturn ( new ATanh ( ( copy ? in . dup ( ) : in ) ) ) ; }


Input: Sinh function [CODESPLIT] public static INDArray sinh ( INDArray in , boolean copy ) { return Nd4j . getExecutioner ( ) . execAndReturn ( new Sinh ( ( copy ? in . dup ( ) : in ) ) ) ; }


Input: Hard tanh [CODESPLIT] public static INDArray hardTanh ( INDArray ndArray , boolean dup ) { return exec ( dup ? new HardTanh ( ndArray , ndArray . dup ( ) ) : new HardTanh ( ndArray ) ) ; }


Input: Hard tanh [CODESPLIT] public static INDArray hardTanhDerivative ( INDArray ndArray , boolean dup ) { return exec ( dup ? new HardTanhDerivative ( ndArray , ndArray . dup ( ) ) : new HardTanhDerivative ( ndArray ) ) ; }


Input: Element - wise power function - x^y performed element - wise [CODESPLIT] public static INDArray pow ( INDArray ndArray , INDArray power , boolean dup ) { INDArray result = ( dup ? Nd4j . create ( ndArray . shape ( ) , ndArray . ordering ( ) ) : ndArray ) ; return exec ( new Pow ( ndArray , power , result , ndArray . length ( ) , 0 ) ) ; }


Input: Log on arbitrary base [CODESPLIT] public static INDArray log ( INDArray ndArray , double base , boolean duplicate ) { return Nd4j . getExecutioner ( ) . exec ( new LogX ( duplicate ? ndArray . dup ( ndArray . ordering ( ) ) : ndArray , base ) ) . z ( ) ; }


Input: Eps function [CODESPLIT] public static INDArray lessThanOrEqual ( INDArray first , INDArray ndArray , boolean dup ) { return exec ( dup ? new OldLessThanOrEqual ( first . dup ( ) , ndArray ) : new OldLessThanOrEqual ( first , ndArray ) ) ; }


Input: Eps function [CODESPLIT] public static INDArray greaterThanOrEqual ( INDArray first , INDArray ndArray , boolean dup ) { return exec ( dup ? new OldGreaterThanOrEqual ( first . dup ( ) , ndArray ) : new OldGreaterThanOrEqual ( first , ndArray ) ) ; }


Input: Eps function [CODESPLIT] public static INDArray eps ( INDArray ndArray , boolean dup ) { return exec ( dup ? new Eps ( ndArray . dup ( ) ) : new Eps ( ndArray ) ) ; }


Input: Maximum function with a scalar [CODESPLIT] public static INDArray max ( INDArray ndArray , double k , boolean dup ) { return exec ( dup ? new ScalarMax ( ndArray . dup ( ) , k ) : new ScalarMax ( ndArray , k ) ) ; }


Input: Element wise maximum function between 2 INDArrays [CODESPLIT] public static INDArray max ( INDArray first , INDArray second , boolean dup ) { if ( dup ) { first = first . dup ( ) ; } return exec ( new OldMax ( second , first , first , first . length ( ) ) ) ; }


Input: Minimum function with a scalar [CODESPLIT] public static INDArray min ( INDArray ndArray , double k , boolean dup ) { return exec ( dup ? new ScalarMin ( ndArray . dup ( ) , k ) : new ScalarMin ( ndArray , k ) ) ; }


Input: Element wise minimum function between 2 INDArrays [CODESPLIT] public static INDArray min ( INDArray first , INDArray second , boolean dup ) { if ( dup ) { first = first . dup ( ) ; } return exec ( new OldMin ( second , first , first , first . length ( ) ) ) ; }


Input: Stabilize to be within a range of k [CODESPLIT] public static INDArray stabilize ( INDArray ndArray , double k , boolean dup ) { return exec ( dup ? new Stabilize ( ndArray , ndArray . dup ( ) , k ) : new Stabilize ( ndArray , k ) ) ; }


Input: Abs function [CODESPLIT] public static INDArray abs ( INDArray ndArray , boolean dup ) { return exec ( dup ? new Abs ( ndArray , ndArray . dup ( ) ) : new Abs ( ndArray ) ) ; }


Input: Exp function [CODESPLIT] public static INDArray exp ( INDArray ndArray , boolean dup ) { return exec ( dup ? new Exp ( ndArray , ndArray . dup ( ) ) : new Exp ( ndArray ) ) ; }


Input: Elementwise exponential - 1 function [CODESPLIT] public static INDArray expm1 ( INDArray ndArray , boolean dup ) { return exec ( dup ? new Expm1 ( ndArray , ndArray . dup ( ) ) : new Expm1 ( ndArray ) ) ; }


Input: Identity function [CODESPLIT] public static INDArray identity ( INDArray ndArray , boolean dup ) { return exec ( dup ? new OldIdentity ( ndArray , ndArray . dup ( ) ) : new OldIdentity ( ndArray ) ) ; }


Input: Sqrt function [CODESPLIT] public static INDArray sqrt ( INDArray ndArray , boolean dup ) { return exec ( dup ? new Sqrt ( ndArray , ndArray . dup ( ) ) : new Sqrt ( ndArray ) ) ; }


Input: Tanh function [CODESPLIT] public static INDArray tanh ( INDArray ndArray , boolean dup ) { return exec ( dup ? new Tanh ( ndArray , ndArray . dup ( ) ) : new Tanh ( ndArray ) ) ; }


Input: Log function [CODESPLIT] public static INDArray log ( INDArray ndArray , boolean dup ) { return exec ( dup ? new Log ( ndArray , ndArray . dup ( ) ) : new Log ( ndArray ) ) ; }


Input: Log of x + 1 function [CODESPLIT] public static INDArray log1p ( INDArray ndArray , boolean dup ) { return exec ( dup ? new Log1p ( ndArray , ndArray . dup ( ) ) : new Log1p ( ndArray ) ) ; }


Input: Apply the given elementwise op [CODESPLIT] private static INDArray exec ( ScalarOp op ) { if ( op . x ( ) . isCleanedUp ( ) ) throw new IllegalStateException ( "NDArray already freed" ) ; return Nd4j . getExecutioner ( ) . exec ( op ) . z ( ) ; }


Input: Apply the given elementwise op [CODESPLIT] private static INDArray exec ( TransformOp op ) { if ( op . x ( ) . isCleanedUp ( ) ) throw new IllegalStateException ( "NDArray already freed" ) ; return Nd4j . getExecutioner ( ) . execAndReturn ( op ) ; }


Input: Raises a square matrix to a power <i > n< / i > which can be positive negative or zero . The behavior is similar to the numpy matrix_power () function . The algorithm uses repeated squarings to minimize the number of mmul () operations needed <p > If <i > n< / i > is zero the identity matrix is returned . < / p > <p > If <i > n< / i > is negative the matrix is inverted and raised to the abs ( n ) power . < / p > [CODESPLIT] public static INDArray mpow ( INDArray in , int n , boolean dup ) { assert in . rows ( ) == in . columns ( ) ; if ( n == 0 ) { if ( dup ) return Nd4j . eye ( in . rows ( ) ) ; else return in . assign ( Nd4j . eye ( in . rows ( ) ) ) ; } INDArray temp ; if ( n < 0 ) { temp = InvertMatrix . invert ( in , ! dup ) ; n = - n ; } else temp = in . dup ( ) ; INDArray result = temp . dup ( ) ; if ( n < 4 ) { for ( int i = 1 ; i < n ; i ++ ) { result . mmuli ( temp ) ; } if ( dup ) return result ; else return in . assign ( result ) ; } else { // lets try to optimize by squaring itself a bunch of times int squares = ( int ) ( Math . log ( n ) / Math . log ( 2.0 ) ) ; for ( int i = 0 ; i < squares ; i ++ ) result = result . mmul ( result ) ; int diff = ( int ) Math . round ( n - Math . pow ( 2.0 , squares ) ) ; for ( int i = 0 ; i < diff ; i ++ ) result . mmuli ( temp ) ; if ( dup ) return result ; else return in . assign ( result ) ; } }


Input: Prepare the boundaries for processing [CODESPLIT] public static INDArray [ ] prepareBounds ( INDArray bounds , INDArray x ) { return new INDArray [ ] { Nd4j . valueArrayOf ( x . shape ( ) , bounds . getDouble ( 0 ) ) , Nd4j . valueArrayOf ( x . shape ( ) , bounds . getDouble ( 1 ) ) } ; }


Input: Adjust final scheme to presence of bounds [CODESPLIT] public static INDArray [ ] adjustSchemeToBounds ( INDArray x , INDArray h , int numSteps , INDArray lowerBound , INDArray upperBound ) { INDArray oneSided = Nd4j . onesLike ( h ) ; if ( and ( lowerBound . eq ( Double . NEGATIVE_INFINITY ) , upperBound . eq ( Double . POSITIVE_INFINITY ) ) . sumNumber ( ) . doubleValue ( ) > 0 ) { return new INDArray [ ] { h , oneSided } ; } INDArray hTotal = h . mul ( numSteps ) ; INDArray hAdjusted = h . dup ( ) ; INDArray lowerDist = x . sub ( lowerBound ) ; INDArray upperBound2 = upperBound . sub ( x ) ; INDArray central = and ( greaterThanOrEqual ( lowerDist , hTotal ) , greaterThanOrEqual ( upperBound2 , hTotal ) ) ; INDArray forward = and ( greaterThanOrEqual ( upperBound , lowerDist ) , not ( central ) ) ; hAdjusted . put ( forward , min ( h . get ( forward ) , upperBound2 . get ( forward ) . mul ( 0.5 ) . divi ( numSteps ) ) ) ; oneSided . put ( forward , Nd4j . scalar ( 1.0 ) ) ; INDArray backward = and ( upperBound2 . lt ( lowerBound ) , not ( central ) ) ; hAdjusted . put ( backward , min ( h . get ( backward ) , lowerDist . get ( backward ) . mul ( 0.5 ) . divi ( numSteps ) ) ) ; oneSided . put ( backward , Nd4j . scalar ( 1.0 ) ) ; INDArray minDist = min ( upperBound2 , lowerDist ) . divi ( numSteps ) ; INDArray adjustedCentral = and ( not ( central ) , lessThanOrEqual ( abs ( hAdjusted ) , minDist ) ) ; hAdjusted . put ( adjustedCentral , minDist . get ( adjustedCentral ) ) ; oneSided . put ( adjustedCentral , Nd4j . scalar ( 0.0 ) ) ; return new INDArray [ ] { hAdjusted , oneSided } ; }


Input: Returns the next element in the iteration . [CODESPLIT] @ Override public DataSet next ( ) { if ( ! iter . hasNext ( ) && passes < numPasses ) { passes ++ ; batch = 0 ; log . info ( "Epoch " + passes + " batch " + batch ) ; iter . reset ( ) ; } batch ++ ; DataSet next = iter . next ( ) ; if ( preProcessor != null ) preProcessor . preProcess ( next ) ; return next ; }


Input: ND Convolution [CODESPLIT] @ Override public IComplexNDArray convn ( IComplexNDArray input , IComplexNDArray kernel , Convolution . Type type ) { return convn ( input , kernel , type , ArrayUtil . range ( 0 , input . shape ( ) . length ) ) ; }


Input: CHOLESKY DECOMP [CODESPLIT] @ Override public void spotrf ( byte uplo , int N , INDArray A , INDArray INFO ) { INDArray a = A ; if ( Nd4j . dataType ( ) != DataBuffer . Type . FLOAT ) log . warn ( "DOUBLE potrf called in FLOAT environment" ) ; if ( A . ordering ( ) == ' ' ) a = A . dup ( ' ' ) ; if ( Nd4j . getExecutioner ( ) instanceof GridExecutioner ) ( ( GridExecutioner ) Nd4j . getExecutioner ( ) ) . flushQueue ( ) ; // Get context for current thread CudaContext ctx = ( CudaContext ) allocator . getDeviceContext ( ) . getContext ( ) ; // setup the solver handles for cuSolver calls cusolverDnHandle_t handle = ctx . getSolverHandle ( ) ; cusolverDnContext solverDn = new cusolverDnContext ( handle ) ; // synchronized on the solver synchronized ( handle ) { int result = cusolverDnSetStream ( new cusolverDnContext ( handle ) , new CUstream_st ( ctx . getOldStream ( ) ) ) ; if ( result != 0 ) throw new BlasException ( "solverSetStream failed" ) ; // transfer the INDArray into GPU memory CublasPointer xAPointer = new CublasPointer ( a , ctx ) ; // this output - indicates how much memory we'll need for the real operation DataBuffer worksizeBuffer = Nd4j . getDataBufferFactory ( ) . createInt ( 1 ) ; int stat = cusolverDnSpotrf_bufferSize ( solverDn , uplo , N , ( FloatPointer ) xAPointer . getDevicePointer ( ) , N , ( IntPointer ) worksizeBuffer . addressPointer ( ) // we intentionally use host pointer here ) ; if ( stat != CUSOLVER_STATUS_SUCCESS ) { throw new BlasException ( "cusolverDnSpotrf_bufferSize failed" , stat ) ; } int worksize = worksizeBuffer . getInt ( 0 ) ; // Now allocate memory for the workspace, the permutation matrix and a return code Pointer workspace = new Workspace ( worksize * Nd4j . sizeOfDataType ( ) ) ; // Do the actual decomp stat = cusolverDnSpotrf ( solverDn , uplo , N , ( FloatPointer ) xAPointer . getDevicePointer ( ) , N , new CudaPointer ( workspace ) . asFloatPointer ( ) , worksize , new CudaPointer ( allocator . getPointer ( INFO , ctx ) ) . asIntPointer ( ) ) ; if ( stat != CUSOLVER_STATUS_SUCCESS ) { throw new BlasException ( "cusolverDnSpotrf failed" , stat ) ; } } allocator . registerAction ( ctx , a ) ; allocator . registerAction ( ctx , INFO ) ; if ( a != A ) A . assign ( a ) ; if ( uplo == ' ' ) { A . assign ( A . transpose ( ) ) ; INDArrayIndex ix [ ] = new INDArrayIndex [ 2 ] ; for ( int i = 1 ; i < Math . min ( A . rows ( ) , A . columns ( ) ) ; i ++ ) { ix [ 0 ] = NDArrayIndex . point ( i ) ; ix [ 1 ] = NDArrayIndex . interval ( 0 , i ) ; A . put ( ix , 0 ) ; } } else { INDArrayIndex ix [ ] = new INDArrayIndex [ 2 ] ; for ( int i = 0 ; i < Math . min ( A . rows ( ) , A . columns ( ) - 1 ) ; i ++ ) { ix [ 0 ] = NDArrayIndex . point ( i ) ; ix [ 1 ] = NDArrayIndex . interval ( i + 1 , A . columns ( ) ) ; A . put ( ix , 0 ) ; } } log . info ( "A: {}" , A ) ; }


Input: Q R DECOMP [CODESPLIT] @ Override public void sgeqrf ( int M , int N , INDArray A , INDArray R , INDArray INFO ) { INDArray tau = Nd4j . create ( N ) ; int status = LAPACKE_sgeqrf ( getColumnOrder ( A ) , M , N , ( FloatPointer ) A . data ( ) . addressPointer ( ) , getLda ( A ) , ( FloatPointer ) tau . data ( ) . addressPointer ( ) ) ; if ( status != 0 ) { throw new BlasException ( "Failed to execute sgeqrf" , status ) ; } // Copy R ( upper part of Q ) into result if ( R != null ) { R . assign ( A . get ( NDArrayIndex . interval ( 0 , A . columns ( ) ) , NDArrayIndex . all ( ) ) ) ; INDArrayIndex ix [ ] = new INDArrayIndex [ 2 ] ; for ( int i = 1 ; i < Math . min ( A . rows ( ) , A . columns ( ) ) ; i ++ ) { ix [ 0 ] = NDArrayIndex . point ( i ) ; ix [ 1 ] = NDArrayIndex . interval ( 0 , i ) ; R . put ( ix , 0 ) ; } } status = LAPACKE_sorgqr ( getColumnOrder ( A ) , M , N , N , ( FloatPointer ) A . data ( ) . addressPointer ( ) , getLda ( A ) , ( FloatPointer ) tau . data ( ) . addressPointer ( ) ) ; if ( status != 0 ) { throw new BlasException ( "Failed to execute sorgqr" , status ) ; } }


Input: This method returns AllocationShape for specific array that takes in account its real shape : offset length etc [CODESPLIT] public static AllocationShape buildAllocationShape ( INDArray array ) { AllocationShape shape = new AllocationShape ( ) ; shape . setStride ( array . elementWiseStride ( ) ) ; shape . setOffset ( array . originalOffset ( ) ) ; shape . setDataType ( array . data ( ) . dataType ( ) ) ; shape . setLength ( array . length ( ) ) ; shape . setDataType ( array . data ( ) . dataType ( ) ) ; return shape ; }


Input: This method returns AllocationShape for the whole DataBuffer . [CODESPLIT] public static AllocationShape buildAllocationShape ( DataBuffer buffer ) { AllocationShape shape = new AllocationShape ( ) ; shape . setStride ( 1 ) ; shape . setOffset ( buffer . originalOffset ( ) ) ; shape . setDataType ( buffer . dataType ( ) ) ; shape . setLength ( buffer . length ( ) ) ; return shape ; }


Input: Check if a file exists in the path [CODESPLIT] public static boolean nameExistsInPath ( String name ) { String path = System . getenv ( PATH_ENV_VARIABLE ) ; String [ ] dirs = path . split ( File . pathSeparator ) ; for ( String dir : dirs ) { File dirFile = new File ( dir ) ; if ( ! dirFile . exists ( ) ) continue ; if ( dirFile . isFile ( ) && dirFile . getName ( ) . equals ( name ) ) return true ; else { Iterator < File > files = FileUtils . iterateFiles ( dirFile , null , false ) ; while ( files . hasNext ( ) ) { File curr = files . next ( ) ; if ( curr . getName ( ) . equals ( name ) ) return true ; } } } return false ; }


Input: Custom deserialization for Java serialization [CODESPLIT] protected void read ( ObjectInputStream s ) throws IOException , ClassNotFoundException { data = Nd4j . createBuffer ( length , false ) ; data . read ( s ) ; }


Input: This method ensures the events in the beginning of FIFO queues are finished [CODESPLIT] protected void sweepTail ( ) { Integer deviceId = allocator . getDeviceId ( ) ; int cnt = 0 ; // we get number of issued commands for specific device long lastCommandId = deviceClocks . get ( deviceId ) . get ( ) ; for ( int l = 0 ; l < configuration . getCommandLanesNumber ( ) ; l ++ ) { Queue < cudaEvent_t > queue = eventsBarrier . get ( deviceId ) . get ( l ) ; if ( queue . size ( ) >= MAX_EXECUTION_QUEUE || laneClocks . get ( deviceId ) . get ( l ) . get ( ) < lastCommandId - MAX_EXECUTION_QUEUE ) { cudaEvent_t event = queue . poll ( ) ; if ( event != null && ! event . isDestroyed ( ) ) { event . synchronize ( ) ; event . destroy ( ) ; cnt ++ ; } } } deviceClocks . get ( deviceId ) . incrementAndGet ( ) ; //  log.info("Events sweeped: [{}]", cnt); }


Input: Create a complex ndarray from the passed in indarray [CODESPLIT] @ Override public IComplexNDArray createComplex ( IComplexNumber [ ] data , int [ ] shape ) { return new JCublasComplexNDArray ( data , shape , Nd4j . getComplexStrides ( shape , Nd4j . order ( ) ) ) ; }


Input: Create a complex ndarray from the passed in indarray [CODESPLIT] @ Override public IComplexNDArray createComplex ( List < IComplexNDArray > arrs , int [ ] shape ) { return new JCublasComplexNDArray ( arrs , shape ) ; }


Input: Symmetric in place shuffle of an ndarray along a specified set of dimensions . Each array in list should have it s own dimension at the same index of dimensions array [CODESPLIT] @ Override public void shuffle ( List < INDArray > arrays , Random rnd , List < int [ ] > dimensions ) { // no dimension - no shuffle if ( dimensions == null || dimensions . size ( ) == 0 ) throw new RuntimeException ( "Dimension can't be null or 0-length" ) ; if ( arrays == null || arrays . size ( ) == 0 ) throw new RuntimeException ( "No input arrays provided" ) ; if ( dimensions . size ( ) > 1 && arrays . size ( ) != dimensions . size ( ) ) throw new IllegalStateException ( "Number of dimensions do not match number of arrays to shuffle" ) ; Nd4j . getExecutioner ( ) . push ( ) ; // first we build TAD for input array and dimensions AtomicAllocator allocator = AtomicAllocator . getInstance ( ) ; CudaContext context = null ; for ( int x = 0 ; x < arrays . size ( ) ; x ++ ) { context = allocator . getFlowController ( ) . prepareAction ( arrays . get ( x ) ) ; } int tadLength = 1 ; for ( int i = 0 ; i < dimensions . get ( 0 ) . length ; i ++ ) { tadLength *= arrays . get ( 0 ) . shape ( ) [ dimensions . get ( 0 ) [ i ] ] ; } val numTads = arrays . get ( 0 ) . length ( ) / tadLength ; val map = ArrayUtil . buildInterleavedVector ( rnd , ( int ) numTads ) ; val shuffle = new CudaIntDataBuffer ( map ) ; Pointer shuffleMap = allocator . getPointer ( shuffle , context ) ; PointerPointer extras = new PointerPointer ( null , // not used context . getOldStream ( ) , allocator . getDeviceIdPointer ( ) ) ; long [ ] xPointers = new long [ arrays . size ( ) ] ; long [ ] xShapes = new long [ arrays . size ( ) ] ; long [ ] tadShapes = new long [ arrays . size ( ) ] ; long [ ] tadOffsets = new long [ arrays . size ( ) ] ; for ( int i = 0 ; i < arrays . size ( ) ; i ++ ) { INDArray array = arrays . get ( i ) ; Pointer x = AtomicAllocator . getInstance ( ) . getPointer ( array , context ) ; Pointer xShapeInfo = AtomicAllocator . getInstance ( ) . getPointer ( array . shapeInfoDataBuffer ( ) , context ) ; TADManager tadManager = Nd4j . getExecutioner ( ) . getTADManager ( ) ; int [ ] dimension = dimensions . size ( ) > 1 ? dimensions . get ( i ) : dimensions . get ( 0 ) ; Pair < DataBuffer , DataBuffer > tadBuffers = tadManager . getTADOnlyShapeInfo ( array , dimension ) ; //            log.info("Original shape: {}; dimension: {}; TAD shape: {}", array.shapeInfoDataBuffer().asInt(), dimension, tadBuffers.getFirst().asInt()); Pointer tadShapeInfo = AtomicAllocator . getInstance ( ) . getPointer ( tadBuffers . getFirst ( ) , context ) ; DataBuffer offsets = tadBuffers . getSecond ( ) ; if ( offsets . length ( ) != numTads ) throw new ND4JIllegalStateException ( "Can't symmetrically shuffle arrays with non-equal number of TADs" ) ; Pointer tadOffset = AtomicAllocator . getInstance ( ) . getPointer ( offsets , context ) ; xPointers [ i ] = x . address ( ) ; xShapes [ i ] = xShapeInfo . address ( ) ; tadShapes [ i ] = tadShapeInfo . address ( ) ; tadOffsets [ i ] = tadOffset . address ( ) ; } CudaDoubleDataBuffer tempX = new CudaDoubleDataBuffer ( arrays . size ( ) ) ; CudaDoubleDataBuffer tempShapes = new CudaDoubleDataBuffer ( arrays . size ( ) ) ; CudaDoubleDataBuffer tempTAD = new CudaDoubleDataBuffer ( arrays . size ( ) ) ; CudaDoubleDataBuffer tempOffsets = new CudaDoubleDataBuffer ( arrays . size ( ) ) ; AtomicAllocator . getInstance ( ) . memcpyBlocking ( tempX , new LongPointer ( xPointers ) , xPointers . length * 8 , 0 ) ; AtomicAllocator . getInstance ( ) . memcpyBlocking ( tempShapes , new LongPointer ( xShapes ) , xPointers . length * 8 , 0 ) ; AtomicAllocator . getInstance ( ) . memcpyBlocking ( tempTAD , new LongPointer ( tadShapes ) , xPointers . length * 8 , 0 ) ; AtomicAllocator . getInstance ( ) . memcpyBlocking ( tempOffsets , new LongPointer ( tadOffsets ) , xPointers . length * 8 , 0 ) ; if ( Nd4j . dataType ( ) == DataBuffer . Type . DOUBLE ) { nativeOps . shuffleDouble ( extras , new PointerPointer ( allocator . getPointer ( tempX , context ) ) , new PointerPointer ( allocator . getPointer ( tempShapes , context ) ) , new PointerPointer ( allocator . getPointer ( tempX , context ) ) , new PointerPointer ( allocator . getPointer ( tempShapes , context ) ) , arrays . size ( ) , ( IntPointer ) shuffleMap , new PointerPointer ( allocator . getPointer ( tempTAD , context ) ) , new PointerPointer ( allocator . getPointer ( tempOffsets , context ) ) ) ; } else if ( Nd4j . dataType ( ) == DataBuffer . Type . FLOAT ) { nativeOps . shuffleFloat ( extras , new PointerPointer ( allocator . getPointer ( tempX , context ) ) , new PointerPointer ( allocator . getPointer ( tempShapes , context ) ) , new PointerPointer ( allocator . getPointer ( tempX , context ) ) , new PointerPointer ( allocator . getPointer ( tempShapes , context ) ) , arrays . size ( ) , ( IntPointer ) shuffleMap , new PointerPointer ( allocator . getPointer ( tempTAD , context ) ) , new PointerPointer ( allocator . getPointer ( tempOffsets , context ) ) ) ; } else { // HALFs nativeOps . shuffleHalf ( extras , new PointerPointer ( allocator . getPointer ( tempX , context ) ) , new PointerPointer ( allocator . getPointer ( tempShapes , context ) ) , new PointerPointer ( allocator . getPointer ( tempX , context ) ) , new PointerPointer ( allocator . getPointer ( tempShapes , context ) ) , arrays . size ( ) , ( IntPointer ) shuffleMap , new PointerPointer ( allocator . getPointer ( tempTAD , context ) ) , new PointerPointer ( allocator . getPointer ( tempOffsets , context ) ) ) ; } for ( int f = 0 ; f < arrays . size ( ) ; f ++ ) { allocator . getFlowController ( ) . registerAction ( context , arrays . get ( f ) ) ; } // just to keep reference shuffle . address ( ) ; tempX . dataType ( ) ; tempShapes . dataType ( ) ; tempOffsets . dataType ( ) ; tempTAD . dataType ( ) ; }


Input: Create from an in memory numpy pointer [CODESPLIT] @ Override public INDArray createFromNpyPointer ( Pointer pointer ) { Pointer dataPointer = nativeOps . dataPointForNumpy ( pointer ) ; int dataBufferElementSize = nativeOps . elementSizeForNpyArray ( pointer ) ; DataBuffer data = null ; Pointer shapeBufferPointer = nativeOps . shapeBufferForNumpy ( pointer ) ; int length = nativeOps . lengthForShapeBufferPointer ( shapeBufferPointer ) ; shapeBufferPointer . capacity ( 4 * length ) ; shapeBufferPointer . limit ( 4 * length ) ; shapeBufferPointer . position ( 0 ) ; val intPointer = new LongPointer ( shapeBufferPointer ) ; DataBuffer shapeBuffer = Nd4j . createBuffer ( shapeBufferPointer , DataBuffer . Type . LONG , length , LongRawIndexer . create ( intPointer ) ) ; dataPointer . position ( 0 ) ; dataPointer . limit ( dataBufferElementSize * Shape . length ( shapeBuffer ) ) ; dataPointer . capacity ( dataBufferElementSize * Shape . length ( shapeBuffer ) ) ; // we don't care about pointers here, they will be copied in BaseCudaDataBuffer method, and indexer will be recreated if ( dataBufferElementSize == ( Float . SIZE / 8 ) ) { data = Nd4j . createBuffer ( dataPointer , DataBuffer . Type . FLOAT , Shape . length ( shapeBuffer ) , FloatIndexer . create ( new FloatPointer ( dataPointer ) ) ) ; } else if ( dataBufferElementSize == ( Double . SIZE / 8 ) ) { data = Nd4j . createBuffer ( dataPointer , DataBuffer . Type . DOUBLE , Shape . length ( shapeBuffer ) , DoubleIndexer . create ( new DoublePointer ( dataPointer ) ) ) ; } INDArray ret = Nd4j . create ( data , Shape . shape ( shapeBuffer ) , Shape . strideArr ( shapeBuffer ) , Shape . offset ( shapeBuffer ) , Shape . order ( shapeBuffer ) ) ; return ret ; }


Input:
Create from a given numpy file . [CODESPLIT] @ Override public INDArray createFromNpyFile ( File file ) { /*Pointer pointer = nativeOps.numpyFromFile(new BytePointer(file.getAbsolutePath().getBytes()));
        log.info("Pointer here: {}", pointer.address());
        return createFromNpyPointer(pointer);

        */ byte [ ] pathBytes = file . getAbsolutePath ( ) . getBytes ( Charset . forName ( "UTF-8" ) ) ; String otherBytes = new String ( pathBytes ) ; System . out . println ( otherBytes ) ; ByteBuffer directBuffer = ByteBuffer . allocateDirect ( pathBytes . length ) . order ( ByteOrder . nativeOrder ( ) ) ; directBuffer . put ( pathBytes ) ; directBuffer . rewind ( ) ; directBuffer . position ( 0 ) ; Pointer pointer = nativeOps . numpyFromFile ( new BytePointer ( directBuffer ) ) ; INDArray result = createFromNpyPointer ( pointer ) ; // releasing original pointer here nativeOps . releaseNumpy ( pointer ) ; return result ; }


Input: This method provides PointersPair to memory chunk specified by AllocationShape [CODESPLIT] @ Override public PointersPair malloc ( AllocationShape shape , AllocationPoint point , AllocationStatus location ) { long reqMemory = AllocationUtils . getRequiredMemory ( shape ) ; if ( location == AllocationStatus . HOST && reqMemory < CudaEnvironment . getInstance ( ) . getConfiguration ( ) . getMaximumHostCacheableLength ( ) ) { CacheHolder cache = zeroCache . get ( shape ) ; if ( cache != null ) { Pointer pointer = cache . poll ( ) ; if ( pointer != null ) { cacheZeroHit . incrementAndGet ( ) ; // since this memory chunk is going to be used now, remove it's amount from zeroCachedAmount . addAndGet ( - 1 * reqMemory ) ; PointersPair pair = new PointersPair ( ) ; pair . setDevicePointer ( new CudaPointer ( pointer . address ( ) ) ) ; pair . setHostPointer ( new CudaPointer ( pointer . address ( ) ) ) ; point . setAllocationStatus ( AllocationStatus . HOST ) ; return pair ; } } cacheZeroMiss . incrementAndGet ( ) ; if ( CudaEnvironment . getInstance ( ) . getConfiguration ( ) . isUsePreallocation ( ) && zeroCachedAmount . get ( ) < CudaEnvironment . getInstance ( ) . getConfiguration ( ) . getMaximumHostCache ( ) / 10 && reqMemory < 16 * 1024 * 1024L ) { CachePreallocator preallocator = new CachePreallocator ( shape , location , CudaEnvironment . getInstance ( ) . getConfiguration ( ) . getPreallocationCalls ( ) ) ; preallocator . start ( ) ; } cacheZeroMiss . incrementAndGet ( ) ; return super . malloc ( shape , point , location ) ; } return super . malloc ( shape , point , location ) ; }


Input: This method does allocation from a given Workspace [CODESPLIT] @ Override public PagedPointer alloc ( long requiredMemory , MemoryKind kind , DataBuffer . Type dataType , boolean initialize ) { throw new UnsupportedOperationException ( "DummyWorkspace shouldn't be used for allocation" ) ; }


Input: This method notifies locker that specific object was added to tracking list [CODESPLIT] @ Override public void attachObject ( Object object ) { if ( ! objectLocks . containsKey ( object ) ) objectLocks . put ( object , new ReentrantReadWriteLock ( ) ) ; }


Input: Calculate the update based on the given gradient [CODESPLIT] @ Override public void applyUpdater ( INDArray gradient , int iteration , int epoch ) { if ( m == null || v == null ) throw new IllegalStateException ( "Updater has not been initialized with view state" ) ; double beta1 = config . getBeta1 ( ) ; double beta2 = config . getBeta2 ( ) ; double learningRate = config . getLearningRate ( iteration , epoch ) ; double epsilon = config . getEpsilon ( ) ; INDArray oneMinusBeta1Grad = gradient . mul ( 1.0 - beta1 ) ; m . muli ( beta1 ) . addi ( oneMinusBeta1Grad ) ; INDArray oneMinusBeta2GradSquared = gradient . mul ( gradient ) . muli ( 1 - beta2 ) ; v . muli ( beta2 ) . addi ( oneMinusBeta2GradSquared ) ; double beta1t = FastMath . pow ( beta1 , iteration + 1 ) ; double beta2t = FastMath . pow ( beta2 , iteration + 1 ) ; double alphat = learningRate * FastMath . sqrt ( 1 - beta2t ) / ( 1 - beta1t ) ; if ( Double . isNaN ( alphat ) || alphat == 0.0 ) alphat = epsilon ; INDArray sqrtV = Transforms . sqrt ( v . dup ( gradientReshapeOrder ) , false ) . addi ( epsilon ) ; gradient . assign ( m ) . muli ( alphat ) . divi ( sqrtV ) ; }


Input: Writes the bytes for the object to the output . <p > This method should not be called directly instead this serializer can be passed to { @link Kryo } write methods that accept a serialier . [CODESPLIT] @ Override public void write ( Kryo kryo , Output output , INDArray object ) { DataOutputStream dos = new DataOutputStream ( output ) ; try { Nd4j . write ( object , dos ) ; } catch ( IOException e ) { throw new RuntimeException ( e ) ; } //Note: output should NOT be closed manually here - may be needed elsewhere (and closing here will cause serialization to fail) }


Input: Reads bytes and returns a new object of the specified concrete opType . <p > Before Kryo can be used to read child objects { @link Kryo#reference ( Object ) } must be called with the parent object to ensure it can be referenced by the child objects . Any serializer that uses { @link Kryo } to read a child object may need to be reentrant . <p > This method should not be called directly instead this serializer can be passed to { @link Kryo } read methods that accept a serialier . [CODESPLIT] @ Override public INDArray read ( Kryo kryo , Input input , Class < INDArray > type ) { DataInputStream dis = new DataInputStream ( input ) ; try { return Nd4j . read ( dis ) ; } catch ( IOException e ) { throw new RuntimeException ( e ) ; } //Note: input should NOT be closed manually here - may be needed elsewhere (and closing here will cause serialization to fail) }


Input: Returns true if bth the master and responder are started . [CODESPLIT] public boolean started ( ) { return master . equals ( ServerState . STARTED . name ( ) . toLowerCase ( ) ) && responder . equals ( ServerState . STARTED . name ( ) . toLowerCase ( ) ) ; }


Input:
This is one of the main entry points for ops that are executed without respect to dimension . [CODESPLIT] @ Override public Op exec ( Op op ) { /*
            We pass this op to GridProcessor through check for possible MetaOp concatenation
            Also, it's the GriOp entry point
         */ checkForCompression ( op ) ; invokeWatchdog ( op ) ; if ( op instanceof Accumulation ) { exec ( ( Accumulation ) op , new int [ ] { Integer . MAX_VALUE } ) ; } else if ( op instanceof IndexAccumulation ) { exec ( ( IndexAccumulation ) op , new int [ ] { Integer . MAX_VALUE } ) ; } else if ( op instanceof ScalarOp || op instanceof TransformOp ) { // the only entry place for TADless ops processAsGridOp ( op ) ; } else if ( op instanceof BroadcastOp ) { invoke ( ( BroadcastOp ) op ) ; } else { //logger.info("Random op: {}", op.getClass().getSimpleName()); pushToGrid ( new OpDescriptor ( op ) ) ; } return op ; }


Input: FIXME : remove CudaContext return opType . We just don t need it [CODESPLIT] @ Override protected CudaContext invoke ( TransformOp op ) { if ( op . isExecSpecial ( ) ) { flushQueue ( ) ; super . invoke ( op ) ; } else { processAsGridOp ( op , null ) ; } return null ; }


Input: This method forces all currently enqueued ops to be executed immediately [CODESPLIT] @ Override public void flushQueueBlocking ( ) { flushQueue ( ) ; //    logger.info("Blocking flush");n ( ( CudaContext ) AtomicAllocator . getInstance ( ) . getDeviceContext ( ) . getContext ( ) ) . syncOldStream ( ) ; ( ( CudaContext ) AtomicAllocator . getInstance ( ) . getDeviceContext ( ) . getContext ( ) ) . syncSpecialStream ( ) ; }


Input: Retrieves the device pointer for the given data buffer [CODESPLIT] public static long retrieveDeviceAddress ( DataBuffer buffer , CudaContext context ) { return allocator . getPointer ( buffer , context ) . address ( ) ; }


Input: Retrieves the device pointer for the given data buffer [CODESPLIT] public static Pointer retrieveDevicePointer ( DataBuffer buffer , CudaContext context ) { return allocator . getPointer ( buffer , context ) ; }