Package org.nd4j.serde.binary
Class BinarySerde
- java.lang.Object
-
- org.nd4j.serde.binary.BinarySerde
-
public class BinarySerde extends Object
-
-
Constructor Summary
Constructors Constructor Description BinarySerde()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static int
byteBufferSizeFor(INDArray arr)
Returns the byte buffer size for the given ndarray.static void
doByteBufferPutCompressed(INDArray arr, ByteBuffer allocated, boolean rewind)
Setup the given byte buffer for serialization (note that this is for compressed INDArrays) 4 bytes for rank 4 bytes for data opType shape information codec information data opTypestatic void
doByteBufferPutUnCompressed(INDArray arr, ByteBuffer allocated, boolean rewind)
Setup the given byte buffer for serialization (note that this is for uncompressed INDArrays) 4 bytes int for rank 4 bytes for data opType shape buffer data bufferstatic INDArray
readFromDisk(File readFrom)
Read an ndarray from diskstatic DataBuffer
readShapeFromDisk(File readFrom)
This method returns shape databuffer from saved earlier filestatic INDArray
toArray(ByteBuffer buffer)
Create an ndarray from the unsafe bufferstatic INDArray
toArray(ByteBuffer buffer, int offset)
Create an ndarray from the unsafe bufferprotected static Pair<INDArray,ByteBuffer>
toArrayAndByteBuffer(ByteBuffer buffer, int offset)
Create an ndarray and existing bytebufferstatic ByteBuffer
toByteBuffer(INDArray arr)
Convert an ndarray to an unsafe buffer for use by aeronstatic void
writeArrayToDisk(INDArray arr, File toWrite)
Write an ndarray to disk in binary formatstatic void
writeArrayToOutputStream(INDArray arr, OutputStream outputStream)
Write an array to an output stream.
-
-
-
Method Detail
-
toArray
public static INDArray toArray(ByteBuffer buffer, int offset)
Create an ndarray from the unsafe buffer- Parameters:
buffer
- the buffer to create the array from- Returns:
- the ndarray derived from this buffer
-
toArray
public static INDArray toArray(ByteBuffer buffer)
Create an ndarray from the unsafe buffer- Parameters:
buffer
- the buffer to create the array from- Returns:
- the ndarray derived from this buffer
-
toArrayAndByteBuffer
protected static Pair<INDArray,ByteBuffer> toArrayAndByteBuffer(ByteBuffer buffer, int offset)
Create an ndarray and existing bytebuffer- Parameters:
buffer
- the buffer to create the arrays fromoffset
- position in buffer to create the arrays from.- Returns:
- the created INDArray and Bytebuffer pair.
-
toByteBuffer
public static ByteBuffer toByteBuffer(INDArray arr)
Convert an ndarray to an unsafe buffer for use by aeron- Parameters:
arr
- the array to convert- Returns:
- the unsafebuffer representation of this array
-
byteBufferSizeFor
public static int byteBufferSizeFor(INDArray arr)
Returns the byte buffer size for the given ndarray. This is an auxillary method for determining the size of the buffer size to allocate for sending an ndarray via the aeron media driver. The math break down for uncompressed is: 2 ints for rank of the array and an ordinal representing the data opType of the data buffer The rest is in order: shape information data buffer The math break down for compressed is: 2 ints for rank and an ordinal representing the data opType for the data buffer The rest is in order: shape information codec information data buffer- Parameters:
arr
- the array to compute the size for- Returns:
- the size of the byte buffer that was allocated
-
doByteBufferPutUnCompressed
public static void doByteBufferPutUnCompressed(INDArray arr, ByteBuffer allocated, boolean rewind)
Setup the given byte buffer for serialization (note that this is for uncompressed INDArrays) 4 bytes int for rank 4 bytes for data opType shape buffer data buffer- Parameters:
arr
- the array to setupallocated
- the byte buffer to setuprewind
- whether to rewind the byte buffer or nt
-
doByteBufferPutCompressed
public static void doByteBufferPutCompressed(INDArray arr, ByteBuffer allocated, boolean rewind)
Setup the given byte buffer for serialization (note that this is for compressed INDArrays) 4 bytes for rank 4 bytes for data opType shape information codec information data opType- Parameters:
arr
- the array to setupallocated
- the byte buffer to setuprewind
- whether to rewind the byte buffer or not
-
writeArrayToOutputStream
public static void writeArrayToOutputStream(INDArray arr, OutputStream outputStream)
Write an array to an output stream.- Parameters:
arr
- the array to writeoutputStream
- the output stream to write to
-
writeArrayToDisk
public static void writeArrayToDisk(INDArray arr, File toWrite) throws IOException
Write an ndarray to disk in binary format- Parameters:
arr
- the array to writetoWrite
- the file tow rite to- Throws:
IOException
- on an I/O exception.
-
readFromDisk
public static INDArray readFromDisk(File readFrom) throws IOException
Read an ndarray from disk- Parameters:
readFrom
- file to read- Returns:
- the created INDArray.
- Throws:
IOException
- on an I/O exception.
-
readShapeFromDisk
public static DataBuffer readShapeFromDisk(File readFrom) throws IOException
This method returns shape databuffer from saved earlier file- Parameters:
readFrom
- file to read- Returns:
- the created databuffer,
- Throws:
IOException
- on an I/O exception.
-
-