SubModel<T>

Models & Types · AiDotNet.Interfaces

Represents a contiguous sub-model extracted from a larger ILayeredModel. Contains a slice of layers that can be independently forwarded through and further partitioned.

Implements: ILayeredModel<T>

For Beginners

When you split a neural network across GPUs for pipeline parallelism, each GPU gets a sub-model - a consecutive sequence of layers from the original network. This class represents that slice.

How It Works

A sub-model implements ILayeredModel itself, so you can extract sub-models of sub-models, enabling hierarchical partitioning (e.g., virtual pipeline stages in Megatron-LM).

Reference: Inspired by PyTorch’s split_module() which returns nn.Sequential modules for each pipeline stage.

Constructors

Constructor Summary
SubModel(IReadOnlyList<ILayer<T>> layers, IReadOnlyList<LayerInfo<T>> layerInfos, int startIndex, int endIndex) Creates a new sub-model from the specified layers and metadata.

Properties

Property Summary
IReadOnlyList<ILayer<T>> Layers { get; } Gets the ordered list of layers in this sub-model.
IReadOnlyList<LayerInfo<T>> LayerInfos { get; } Gets the layer metadata for all layers in this sub-model.
int EndIndex { get; } Gets the end index (inclusive) of this sub-model within the parent model.
int LayerCount { get; } Gets the number of layers in this sub-model.
int StartIndex { get; } Gets the start index of this sub-model within the parent model.
long ParameterCount { get; } Gets the total parameter count across all layers in this sub-model.
long TotalEstimatedFlops { get; } Gets the total estimated FLOPs across all layers in this sub-model.

Methods

Method Summary
IReadOnlyList<LayerInfo<T>> GetAllLayerInfo()
LayerInfo<T> GetLayerInfo(int layerIndex)
SubModel<T> ExtractSubModel(int startLayer, int endLayer)
Tensor<T> Forward(Tensor<T> input) Performs a sequential forward pass through all layers in this sub-model.
bool ValidatePartitionPoint(int afterLayerIndex)
int[] GetInputShape() Gets the input shape expected by the first layer in this sub-model.
int[] GetOutputShape() Gets the output shape produced by the last layer in this sub-model.