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 NDArray
s 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 enum
An 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 specifiedNDArray
s.NDList
(Collection<NDArray> other) Constructs and initiates an NDList with the specifiedNDArray
s. -
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.void
Attaches thisNDResource
to the specifiedNDManager
.void
close()
boolean
Returnstrue
if this NDList contains an NDArray with the specified name.static NDList
Decodes NDList from byte array.static NDList
decode
(NDManager manager, InputStream is) Decodes NDList fromInputStream
.void
detach()
Detaches theNDResource
from currentNDManager
's lifecycle.byte[]
encode()
Encodes the NDList to byte array.byte[]
encode
(NDList.Encoding encoding) Encodes the NDList to byte array.void
encode
(OutputStream os) Writes the encoded NDList toOutputStream
.void
encode
(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 theNDManager
that 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.void
tempAttach
(NDManager manager) Temporarily attaches thisNDResource
to the specifiedNDManager
.Returns theByteBuffer
presentation 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, trimToSize
Methods inherited from class java.util.AbstractCollection
containsAll
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 java.util.Collection
parallelStream, stream, toArray
Methods inherited from interface java.util.List
containsAll
Methods 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 specifiedNDArray
s.- Parameters:
arrays
- theNDArray
s
-
NDList
Constructs and initiates an NDList with the specifiedNDArray
s.- Parameters:
other
- theNDArray
s
-
-
Method Details
-
decode
Decodes NDList from byte array.- Parameters:
manager
- manager assigned toNDArray
byteArray
- byte array to load from- Returns:
NDList
-
decode
Decodes NDList fromInputStream
.- Parameters:
manager
- manager assigned toNDArray
is
- 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
i
such 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
Returnstrue
if 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:
true
if 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 theNDManager
that manages this.- Specified by:
getManager
in interfaceNDResource
- Returns:
- the
NDManager
that manages this.
-
getResourceNDArrays
- Specified by:
getResourceNDArrays
in interfaceNDResource
- Returns:
- the
NDArray
orNDArray
s contained within this resource
-
attach
Attaches thisNDResource
to the specifiedNDManager
.Attached resource will be closed when the
NDManager
is closed.- Specified by:
attach
in interfaceNDResource
- Parameters:
manager
- theNDManager
to be attached to
-
tempAttach
Temporarily attaches thisNDResource
to the specifiedNDManager
.Attached resource will be returned to the original manager when the
NDManager
is closed.- Specified by:
tempAttach
in interfaceNDResource
- Parameters:
manager
- theNDManager
to be attached to
-
detach
public void detach()Detaches theNDResource
from 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:
detach
in 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
- theOutputStream
to be written to- Throws:
IOException
- if failed on IO operation
-
encode
Writes the encoded NDList toOutputStream
.- Parameters:
os
- theOutputStream
to 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:
getAsBytes
in interfaceBytesSupplier
- Returns:
- the
byte[]
presentation of the object
-
toByteBuffer
Returns theByteBuffer
presentation of the object.- Specified by:
toByteBuffer
in interfaceBytesSupplier
- Returns:
- the
ByteBuffer
presentation of the object
-
getShapes
Gets all of shapes in theNDList
.- Returns:
- shapes in
NDList
-
close
public void close()- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceNDResource
-
toString
- Overrides:
toString
in classAbstractCollection<NDArray>
-