Package ai.djl.ndarray
Class NDList
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractList<E>
-
- java.util.ArrayList<NDArray>
-
- ai.djl.ndarray.NDList
-
- All Implemented Interfaces:
BytesSupplier,NDResource,java.io.Serializable,java.lang.AutoCloseable,java.lang.Cloneable,java.lang.Iterable<NDArray>,java.util.Collection<NDArray>,java.util.List<NDArray>,java.util.RandomAccess
public class NDList extends java.util.ArrayList<NDArray> implements NDResource, BytesSupplier
AnNDListrepresents a sequence ofNDArrays with names.Each
NDArrayin this list can optionally have a name. You can use the name to look up an NDArray in the NDList.- See Also:
NDArray, Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classNDList.EncodingAn enum represents NDList serialization format.
-
Constructor Summary
Constructors Constructor Description NDList()Constructs an empty NDList.NDList(int initialCapacity)Constructs an empty NDList with the specified initial capacity.NDList(NDArray... arrays)Constructs and initiates an NDList with the specifiedNDArrays.NDList(java.util.Collection<NDArray> other)Constructs and initiates an NDList with the specifiedNDArrays.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description NDListaddAll(NDList other)Appends all of the NDArrays in the specified NDList to the end of this NDList, in the order that they are returned by the specified NDList's iterator.voidattach(NDManager manager)Attaches thisNDResourceto the specifiedNDManager.voidclose()booleancontains(java.lang.String name)Returnstrueif this NDList contains an NDArray with the specified name.static NDListdecode(NDManager manager, byte[] byteArray)Decodes NDList from byte array.static NDListdecode(NDManager manager, java.io.InputStream is)Decodes NDList fromInputStream.voiddetach()Detaches theNDResourcefrom currentNDManager's lifecycle.byte[]encode()Encodes the NDList to byte array.byte[]encode(NDList.Encoding encoding)Encodes the NDList to byte array.voidencode(java.io.OutputStream os)Writes the encoded NDList toOutputStream.voidencode(java.io.OutputStream os, NDList.Encoding encoding)Writes the encoded NDList toOutputStream.NDArrayget(java.lang.String name)Returns the first occurrence of the specified element from this NDList if it is present.byte[]getAsBytes()Returns thebyte[]presentation of the object.NDManagergetManager()Returns theNDManagerthat manages this.java.util.List<NDArray>getResourceNDArrays()Shape[]getShapes()Gets all of shapes in theNDList.NDArrayhead()Returns the head index of the NDList.NDArrayremove(java.lang.String name)Removes the first occurrence of the specified element from this NDList if it is present.NDArraysingletonOrThrow()Returns the only element if this is a singleton NDList or throws an exception if multiple elements.NDListsubNDList(int fromIndex)Returns a view of the portion of this NDList between the specified fromIndex, inclusive, and to the end.NDListsubNDList(int fromIndex, int toIndex)Returns a view of the portion of this NDList between the specified fromIndex, inclusive, and toIndex, exclusive.voidtempAttach(NDManager manager)Temporarily attaches thisNDResourceto the specifiedNDManager.java.nio.ByteBuffertoByteBuffer()Returns theByteBufferpresentation of the object.NDListtoDevice(Device device, boolean copy)java.lang.StringtoString()-
Methods inherited from class java.util.ArrayList
add, add, addAll, addAll, clear, clone, contains, ensureCapacity, equals, forEach, get, hashCode, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, removeIf, removeRange, replaceAll, retainAll, set, size, sort, spliterator, subList, toArray, toArray, trimToSize
-
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface ai.djl.ndarray.BytesSupplier
getAsObject, getAsString
-
Methods inherited from interface ai.djl.ndarray.NDResource
returnResource
-
-
-
-
Constructor Detail
-
NDList
public NDList()
Constructs an empty NDList.
-
NDList
public NDList(int initialCapacity)
Constructs an empty NDList with the specified initial capacity.- Parameters:
initialCapacity- the initial capacity of the list- Throws:
java.lang.IllegalArgumentException- if the specified initial capacity is negative
-
NDList
public NDList(NDArray... arrays)
Constructs and initiates an NDList with the specifiedNDArrays.- Parameters:
arrays- theNDArrays
-
-
Method Detail
-
decode
public static NDList decode(NDManager manager, byte[] byteArray)
Decodes NDList from byte array.- Parameters:
manager- manager assigned toNDArraybyteArray- byte array to load from- Returns:
NDList
-
decode
public static NDList decode(NDManager manager, java.io.InputStream is)
Decodes NDList fromInputStream.- Parameters:
manager- manager assigned toNDArrayis- input stream contains the ndlist information- Returns:
NDList
-
get
public NDArray get(java.lang.String name)
Returns the first occurrence of the specified element from this NDList if it is present.- Parameters:
name- the name of the NDArray- Returns:
- the first occurrence
-
remove
public NDArray remove(java.lang.String name)
Removes the first occurrence of the specified element from this NDList if it is present.If this list does not contain the element, it is unchanged. More formally, removes the element with the lowest index
isuch that(o==null ? get(i)==null : o.equals(get(i)))(if such an element exists).- Parameters:
name- the name of the NDArray to be removed from this NDList, if present- Returns:
- the element that was removed
-
contains
public boolean contains(java.lang.String name)
Returnstrueif this NDList contains an NDArray with the specified name.- Parameters:
name- the name of the NDArray to be removed from this NDList, if present- Returns:
trueif this list contains the specified element
-
head
public NDArray head()
Returns the head index of the NDList.- Returns:
- the head NDArray
- Throws:
java.lang.IndexOutOfBoundsException- if the index is out of range (index < 0 || index >= size())
-
singletonOrThrow
public NDArray singletonOrThrow()
Returns the only element if this is a singleton NDList or throws an exception if multiple elements.- Returns:
- the head NDArray
- Throws:
java.lang.IndexOutOfBoundsException- if the list does not contain exactly one element
-
addAll
public NDList addAll(NDList other)
Appends all of the NDArrays in the specified NDList to the end of this NDList, in the order that they are returned by the specified NDList's iterator.- Parameters:
other- the NDList containing NDArray to be added to this list- Returns:
- this NDList after the addition
-
subNDList
public NDList subNDList(int fromIndex)
Returns a view of the portion of this NDList between the specified fromIndex, inclusive, and to the end.- Parameters:
fromIndex- the start index (inclusive)- Returns:
- a view of the portion of this NDList
-
subNDList
public NDList subNDList(int fromIndex, int toIndex)
Returns a view of the portion of this NDList between the specified fromIndex, inclusive, and toIndex, exclusive.- Parameters:
fromIndex- the start index (inclusive)toIndex- the end index (exclusive)- Returns:
- a view of the portion of this NDList
-
getManager
public NDManager getManager()
Returns theNDManagerthat manages this.- Specified by:
getManagerin interfaceNDResource- Returns:
- the
NDManagerthat manages this.
-
getResourceNDArrays
public java.util.List<NDArray> getResourceNDArrays()
- Specified by:
getResourceNDArraysin interfaceNDResource- Returns:
- the
NDArrayorNDArrays contained within this resource
-
attach
public void attach(NDManager manager)
Attaches thisNDResourceto the specifiedNDManager.Attached resource will be closed when the
NDManageris closed.- Specified by:
attachin interfaceNDResource- Parameters:
manager- theNDManagerto be attached to
-
tempAttach
public void tempAttach(NDManager manager)
Temporarily attaches thisNDResourceto the specifiedNDManager.Attached resource will be returned to the original manager when the
NDManageris closed.- Specified by:
tempAttachin interfaceNDResource- Parameters:
manager- theNDManagerto be attached to
-
detach
public void detach()
Detaches theNDResourcefrom currentNDManager's lifecycle.This becomes un-managed and it is the user's responsibility to close this. Failure to close the resource might cause your machine to run out of native memory.
- Specified by:
detachin interfaceNDResource- See Also:
NDManager
-
encode
public byte[] encode()
Encodes the NDList to byte array.- Returns:
- the byte array
-
encode
public byte[] encode(NDList.Encoding encoding)
Encodes the NDList to byte array.- Parameters:
encoding- encode mode, one of ndlist/npz/safetensor format- Returns:
- the byte array
-
encode
public void encode(java.io.OutputStream os) throws java.io.IOExceptionWrites the encoded NDList toOutputStream.- Parameters:
os- theOutputStreamto be written to- Throws:
java.io.IOException- if failed on IO operation
-
encode
public void encode(java.io.OutputStream os, NDList.Encoding encoding) throws java.io.IOExceptionWrites the encoded NDList toOutputStream.- Parameters:
os- theOutputStreamto be written toencoding- encode mode, one of ndlist/npz/safetensor format- Throws:
java.io.IOException- if failed on IO operation
-
getAsBytes
public byte[] getAsBytes()
Returns thebyte[]presentation of the object.- Specified by:
getAsBytesin interfaceBytesSupplier- Returns:
- the
byte[]presentation of the object
-
toByteBuffer
public java.nio.ByteBuffer toByteBuffer()
Returns theByteBufferpresentation of the object.- Specified by:
toByteBufferin interfaceBytesSupplier- Returns:
- the
ByteBufferpresentation of the object
-
getShapes
public Shape[] getShapes()
Gets all of shapes in theNDList.- Returns:
- shapes in
NDList
-
close
public void close()
- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfaceNDResource
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.util.AbstractCollection<NDArray>
-
-