Package ai.djl.ndarray
Class NDList
- All Implemented Interfaces:
BytesSupplier,NDResource,Serializable,AutoCloseable,Cloneable,Iterable<NDArray>,Collection<NDArray>,List<NDArray>,RandomAccess
An
NDList represents a sequence of NDArrays with names.
Each NDArray in this list can optionally have a name. You can use the name to look up
an NDArray in the NDList.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumAn enum represents NDList serialization format. -
Field Summary
Fields inherited from class java.util.AbstractList
modCount -
Constructor Summary
ConstructorsConstructorDescriptionNDList()Constructs an empty NDList.NDList(int initialCapacity) Constructs an empty NDList with the specified initial capacity.Constructs and initiates an NDList with the specifiedNDArrays.NDList(Collection<NDArray> other) Constructs and initiates an NDList with the specifiedNDArrays. -
Method Summary
Modifier and TypeMethodDescriptionAppends 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.voidAttaches thisNDResourceto the specifiedNDManager.voidclose()booleanReturnstrueif this NDList contains an NDArray with the specified name.static NDListDecodes NDList from byte array.static NDListdecode(NDManager manager, 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(OutputStream os) Writes the encoded NDList toOutputStream.voidencode(OutputStream os, NDList.Encoding encoding) Writes the encoded NDList toOutputStream.Returns the first occurrence of the specified element from this NDList if it is present.byte[]Returns thebyte[]presentation of the object.Returns theNDManagerthat manages this.Shape[]Gets all of shapes in theNDList.head()Returns the head index of the NDList.Removes the first occurrence of the specified element from this NDList if it is present.Returns the only element if this is a singleton NDList or throws an exception if multiple elements.subNDList(int fromIndex) Returns a view of the portion of this NDList between the specified fromIndex, inclusive, and to the end.subNDList(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.Returns theByteBufferpresentation of the object.toString()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, trimToSizeMethods inherited from class java.util.AbstractCollection
containsAllMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface ai.djl.ndarray.BytesSupplier
getAsObject, getAsStringMethods inherited from interface java.util.Collection
parallelStream, stream, toArrayMethods inherited from interface java.util.List
containsAllMethods inherited from interface ai.djl.ndarray.NDResource
returnResource
-
Constructor Details
-
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:
IllegalArgumentException- if the specified initial capacity is negative
-
NDList
Constructs and initiates an NDList with the specifiedNDArrays.- Parameters:
arrays- theNDArrays
-
NDList
Constructs and initiates an NDList with the specifiedNDArrays.- Parameters:
other- theNDArrays
-
-
Method Details
-
decode
Decodes NDList from byte array.- Parameters:
manager- manager assigned toNDArraybyteArray- byte array to load from- Returns:
NDList
-
decode
Decodes NDList fromInputStream.- Parameters:
manager- manager assigned toNDArrayis- input stream contains the ndlist information- Returns:
NDList
-
get
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
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
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
Returns the head index of the NDList.- Returns:
- the head NDArray
- Throws:
IndexOutOfBoundsException- if the index is out of range (index < 0 || index >= size())
-
singletonOrThrow
Returns the only element if this is a singleton NDList or throws an exception if multiple elements.- Returns:
- the head NDArray
- Throws:
IndexOutOfBoundsException- if the list does not contain exactly one element
-
addAll
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
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
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
-
toDevice
-
getManager
Returns theNDManagerthat manages this.- Specified by:
getManagerin interfaceNDResource- Returns:
- the
NDManagerthat manages this.
-
getResourceNDArrays
- Specified by:
getResourceNDArraysin interfaceNDResource- Returns:
- the
NDArrayorNDArrays contained within this resource
-
attach
Attaches thisNDResourceto the specifiedNDManager.Attached resource will be closed when the
NDManageris closed.- Specified by:
attachin interfaceNDResource- Parameters:
manager- theNDManagerto be attached to
-
tempAttach
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:
-
encode
public byte[] encode()Encodes the NDList to byte array.- Returns:
- the byte array
-
encode
Encodes the NDList to byte array.- Parameters:
encoding- encode mode, one of ndlist/npz/safetensor format- Returns:
- the byte array
-
encode
Writes the encoded NDList toOutputStream.- Parameters:
os- theOutputStreamto be written to- Throws:
IOException- if failed on IO operation
-
encode
Writes the encoded NDList toOutputStream.- Parameters:
os- theOutputStreamto be written toencoding- encode mode, one of ndlist/npz/safetensor format- Throws:
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
Returns theByteBufferpresentation of the object.- Specified by:
toByteBufferin interfaceBytesSupplier- Returns:
- the
ByteBufferpresentation of the object
-
getShapes
Gets all of shapes in theNDList.- Returns:
- shapes in
NDList
-
close
public void close()- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceNDResource
-
toString
- Overrides:
toStringin classAbstractCollection<NDArray>
-