Class LayerBlock
java.lang.Object
smile.deep.layer.LayerBlock
- Direct Known Subclasses:
EfficientNet, FusedMBConv, LlamaModel, MBConv, MiniMaxModel, MiniMaxMoE, MiniMaxVisionTower, QwenModel, QwenMtp, QwenVisionTower, SequentialBlock, SqueezeExcitation
A block is combinations of one or more layers. Blocks form the basis of
more complex network designs. LayerBlock allows treating the whole
container as a single layer, such that performing a transformation on
the LayerBlock applies to each of the layers it contains (which are each
a registered submodule of the block).
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected DeviceThe compute device.protected ScalarTypeThe data type.protected final MemorySegmentThe neural network module (ST_Module). -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionadd(String name, MemorySegment layer) Adds a sub-module.Adds a sub-layer.device()Returns the compute device of module.dtype()Returns the data type of module.voideval()Sets the layer block in the evaluation/inference mode.booleanReturns true if the layer block is in training mode.voidLoads a checkpoint.voidloadStateDict(Map<String, Tensor> stateDict, boolean strict) Loads parameters from a flat state dictionary whose keys matchnamed_parameters()names (e.g.module()Returns the nativeST_Modulehandle for this layer.voidSerialize the layer block as a checkpoint.setRequiresGrad(boolean required) Enables or disablesrequires_gradon every parameter (recursive).Moves the layer block to a device.to(Device device, ScalarType dtype) Moves the layer block to a device.toString()voidtrain()Sets the layer block in the training mode.
-
Field Details
-
module
The neural network module (ST_Module). -
device
The compute device. -
dtype
The data type.
-
-
Constructor Details
-
LayerBlock
public LayerBlock()Constructor. -
LayerBlock
-
-
Method Details
-
module
Description copied from interface:LayerReturns the nativeST_Modulehandle for this layer. -
toString
-
add
Adds a sub-layer.- Parameters:
name- the name of sub-layer.layer- the sub-layer.- Returns:
- this object.
-
add
Adds a sub-module.- Parameters:
name- the name of sub-layer.layer- the nativeST_Modulehandle of the sub-layer.- Returns:
- this object.
-
isTraining
public boolean isTraining()Returns true if the layer block is in training mode.- Returns:
- true if the layer block is in training mode.
-
train
public void train()Sets the layer block in the training mode. -
eval
public void eval()Sets the layer block in the evaluation/inference mode. -
setRequiresGrad
Enables or disablesrequires_gradon every parameter (recursive). Call withfalseafter loading an inference checkpoint so TP worker threads cannot accidentally build autograd graphs (NoGradGuard is thread-local and must also wrap each worker).- Parameters:
required-trueto record gradients on parameters.- Returns:
- this block.
-
device
-
dtype
-
to
Description copied from interface:LayerMoves the layer block to a device. -
to
Description copied from interface:LayerMoves the layer block to a device. -
load
-
loadStateDict
Loads parameters from a flat state dictionary whose keys matchnamed_parameters()names (e.g."layers.0.attention.wq.weight").- Parameters:
stateDict- map from fully-qualified parameter name to tensor.strict- whentrue, every module parameter must be present and every state-dict key must match a module parameter.
-
save
Serialize the layer block as a checkpoint.- Parameters:
path- the checkpoint file path.
-