Package ai.djl.nn
Class SequentialBlock
java.lang.Object
ai.djl.nn.AbstractBaseBlock
ai.djl.nn.AbstractBlock
ai.djl.nn.SequentialBlock
- All Implemented Interfaces:
StreamingBlock,Block
- Direct Known Subclasses:
PointwiseFeedForwardBlock
SequentialBlock is a Block whose children form a chain of blocks with each child
block feeding its output to the next. The output of the last child is returned as the output of
the SequentialBlock.
SequentialBlock has no direct parameters.
-
Field Summary
Fields inherited from class ai.djl.nn.AbstractBlock
children, parametersFields inherited from class ai.djl.nn.AbstractBaseBlock
inputNames, inputShapes, outputDataTypes, version -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAdds the givenBlockto the block to be executed in order.Adds aLambdaBlockthat applies the given function to the sequence of blocks.Adds aLambdaBlockthat applies the given function to the sequence of blocks.Adds an array of blocks to be executed in sequence, in order.addAll(Collection<Block> blocks) Adds aCollectionof blocks to be executed in sequence, in order.Adds aLambdaBlock.singleton(Function)that applies the given function to the sequence of blocks.addSingleton(Function<NDArray, NDArray> f, String name) Adds aLambdaBlock.singleton(Function)that applies the given function to the sequence of blocks.protected NDListforwardInternal(ParameterStore parameterStore, NDList inputs, boolean training, ai.djl.util.PairList<String, Object> params) A helper forBlock.forward(ParameterStore, NDList, boolean, PairList)after initialization.protected NDListforwardInternal(ParameterStore parameterStore, NDList data, NDList labels, ai.djl.util.PairList<String, Object> params) A helper forBlock.forward(ParameterStore, NDList, NDList, PairList)after initialization.forwardStreamIter(ParameterStore parameterStore, NDList inputs, boolean training, ai.djl.util.PairList<String, Object> params) Applies the operating function of the block once, but returns the result in chunks.Shape[]getOutputShapes(Shape[] inputs) Returns the expected output shapes of the block for the specified input shapes.voidinitializeChildBlocks(NDManager manager, DataType dataType, Shape... inputShapes) Initializes the Child blocks of this block.booleanReturns whether the block returns all intermediate block results or only the end of the sequential chain.voidloadMetadata(byte loadVersion, DataInputStream is) Overwrite this to load additional metadata with the parameter values.voidRemoves theBlockadded last from the sequence of blocks.voidreplaceLastBlock(Block block) Replaces theBlocklast added from the sequence of blocks, and adds the given block.protected voidOverride this method to save additional data apart from parameter values.setReturnIntermediate(boolean returnIntermediate) Sets whether the block returns all intermediate sequence results.Methods inherited from class ai.djl.nn.AbstractBlock
addChildBlock, addChildBlock, addChildBlockSingleton, addParameter, getChildren, getDirectParametersMethods inherited from class ai.djl.nn.AbstractBaseBlock
beforeInitialize, cast, clear, describeInput, forward, forward, getInputShapes, getOutputDataTypes, getParameters, initialize, isInitialized, loadParameters, prepare, readInputShapes, saveInputShapes, saveParameters, setInitializer, setInitializer, setInitializer, toStringMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface ai.djl.nn.Block
cast, clear, describeInput, forward, forward, forward, freezeParameters, freezeParameters, getChildren, getCustomMetadata, getDirectParameters, getInputShapes, getOutputDataTypes, getOutputShapes, getParameters, initialize, isInitialized, loadParameters, saveParameters, setInitializer, setInitializer, setInitializerMethods inherited from interface ai.djl.inference.streaming.StreamingBlock
forwardStream, forwardStream
-
Constructor Details
-
SequentialBlock
public SequentialBlock()Creates an empty sequential block. UseaddandaddAllto add blocks to be executed in sequence.
-
-
Method Details
-
addAll
Adds an array of blocks to be executed in sequence, in order.- Parameters:
blocks- the array of blocks- Returns:
- this block
-
addAll
Adds aCollectionof blocks to be executed in sequence, in order.- Parameters:
blocks- theCollectionof blocks- Returns:
- this block
-
add
Adds the givenBlockto the block to be executed in order.- Parameters:
block- the block to be added to the sequence of blocks- Returns:
- this block
-
add
Adds aLambdaBlockthat applies the given function to the sequence of blocks.- Parameters:
f- the function forms theLambdaBlock- Returns:
- this block
-
add
Adds aLambdaBlockthat applies the given function to the sequence of blocks.- Parameters:
f- the function forms theLambdaBlockname- the function name- Returns:
- this block
-
addSingleton
Adds aLambdaBlock.singleton(Function)that applies the given function to the sequence of blocks.- Parameters:
f- the function forms theLambdaBlock- Returns:
- this block
- See Also:
-
addSingleton
Adds aLambdaBlock.singleton(Function)that applies the given function to the sequence of blocks.- Parameters:
f- the function forms theLambdaBlockname- the function name- Returns:
- this block
- See Also:
-
removeLastBlock
public void removeLastBlock()Removes theBlockadded last from the sequence of blocks. -
replaceLastBlock
Replaces theBlocklast added from the sequence of blocks, and adds the given block.- Parameters:
block- the block to replace the last block with
-
isReturnIntermediate
public boolean isReturnIntermediate()Returns whether the block returns all intermediate block results or only the end of the sequential chain.- Returns:
- whether the block returns all intermediate block results or only the end of the sequential chain
-
setReturnIntermediate
Sets whether the block returns all intermediate sequence results.- Parameters:
returnIntermediate- true for intermediates, false for only chain result (default and typical behavior is false)- Returns:
- this
SequentialBlock
-
forwardInternal
protected NDList forwardInternal(ParameterStore parameterStore, NDList inputs, boolean training, ai.djl.util.PairList<String, Object> params) A helper forBlock.forward(ParameterStore, NDList, boolean, PairList)after initialization.- Specified by:
forwardInternalin classAbstractBaseBlock- Parameters:
parameterStore- the parameter storeinputs- the input NDListtraining- true for a training forward passparams- optional parameters- Returns:
- the output of the forward pass
-
forwardInternal
protected NDList forwardInternal(ParameterStore parameterStore, NDList data, NDList labels, ai.djl.util.PairList<String, Object> params) A helper forBlock.forward(ParameterStore, NDList, NDList, PairList)after initialization.- Overrides:
forwardInternalin classAbstractBaseBlock- Parameters:
parameterStore- the parameter storedata- the input data NDListlabels- the input labels NDListparams- optional parameters- Returns:
- the output of the forward pass
- See Also:
-
forwardStreamIter
public Iterator<NDList> forwardStreamIter(ParameterStore parameterStore, NDList inputs, boolean training, ai.djl.util.PairList<String, Object> params) Applies the operating function of the block once, but returns the result in chunks. This method should only be called on blocks that are initialized.- Specified by:
forwardStreamIterin interfaceStreamingBlock- Parameters:
parameterStore- the parameter storeinputs- the input NDListtraining- true for a training forward pass (turn on dropout and layerNorm)params- optional parameters- Returns:
- the output of the forward pass
-
initializeChildBlocks
Initializes the Child blocks of this block. You need to override this method if your subclass has child blocks. Used to determine the correct input shapes for child blocks based on the requested input shape for this block.- Overrides:
initializeChildBlocksin classAbstractBaseBlock- Parameters:
manager- the manager to use for initializationdataType- the requested data typeinputShapes- the expected input shapes for this block
-
getOutputShapes
Returns the expected output shapes of the block for the specified input shapes.- Specified by:
getOutputShapesin interfaceBlock- Parameters:
inputs- the shapes of the inputs- Returns:
- the expected output shapes of the block
-
saveMetadata
Override this method to save additional data apart from parameter values.This default implementation saves the currently set input shapes.
- Overrides:
saveMetadatain classAbstractBaseBlock- Parameters:
os- the non-null output stream the parameter values and metadata are written to- Throws:
IOException- saving failed
-
loadMetadata
public void loadMetadata(byte loadVersion, DataInputStream is) throws IOException, MalformedModelException Overwrite this to load additional metadata with the parameter values.If you overwrite
AbstractBaseBlock.saveMetadata(DataOutputStream)or need to provide backward compatibility to older binary formats, you probably need to overwrite this. This default implementation checks if the version number fits, if not it throws anMalformedModelException. After that it restores the input shapes.- Overrides:
loadMetadatain classAbstractBaseBlock- Parameters:
loadVersion- the version used for loading this metadata.is- the input stream we are loading from- Throws:
IOException- loading failedMalformedModelException- data can be loaded but has wrong format
-