embedl_deploy.lattice.modules package#
Module contents:
Public re-exports of Lattice fused nn.Module classes.
Users import from here:
from embedl_deploy.lattice.modules import LatticeConv2d
- class embedl_deploy.lattice.modules.LatticeAdaptiveAvgPool2d[source]#
Bases:
AdaptiveAvgPool2dCanonical Lattice global average pool.
The only supported form of average pooling for Lattice hardware is
AdaptiveAvgPool2dwithoutput_size == (1, 1). The constructor takes no arguments — every instance has the canonical configuration declared byOUTPUT_SIZE.- OUTPUT_SIZE: tuple[int, int] = (1, 1)#
The single permitted output size.
- class embedl_deploy.lattice.modules.LatticeCBSR(conv: Conv2d, bn: BatchNorm2d | None, act: ReLU | LatticeLeakyReLU)[source]#
Bases:
FusedModuleFused
Conv2d -> [BatchNorm2d] -> Activationfor Lattice hardware.The Lattice accelerator implements convolution, optional batch normalization, and activation as a single fused CBSR block. The only activations accepted are
ReLUandLatticeLeakyReLU(negative slope fixed at 1/16). This module validates that the convolution parameters are within the hardware’s supported set and raisesValueErrorotherwise.Supported convolution parameters:
Kernel sizes:
1×1,3×3Strides:
1,2(stride > 1 requires3×3kernel)Dilation:
1only
- Parameters:
conv – The convolution layer to fuse.
bn – Optional batch normalization layer.
act –
ReLUorLatticeLeakyReLUactivation.
- Raises:
ValueError – If conv has unsupported kernel size, stride, or dilation.
- inputs_to_quantize: set[int] = {0}#
Positional argument indices that should receive a
QuantStub. The Q/DQ insertion pass uses this to decide which inputs of the fused node to quantize. Every subclass must set this explicitly.
- property quantized_weight: Tensor | None#
Return the weight tensor covered by
weight_fake_quant.Subclasses that attach a
WeightFakeQuantizemust override this to return the corresponding weight tensor.
- class embedl_deploy.lattice.modules.LatticeCBSRAdvanced(conv: Conv2d, bn: BatchNorm2d | None, act: ReLU | LatticeLeakyReLU)[source]#
Bases:
LatticeCBSRFused
Conv2d -> [BatchNorm2d] -> Activationfor advanced CNN IP.Like
LatticeCBSRbut accepts the broader kernel set supported by advanced Lattice accelerators:1×1,3×3,5×5, and7×7. Only3×3allows stride 2; all other kernel sizes require stride 1. Dilation must be 1.- Parameters:
conv – The convolution layer to fuse.
bn – Optional batch normalization layer.
act –
ReLUorLatticeLeakyReLUactivation.
- Raises:
ValueError – If conv has unsupported kernel size, stride, or dilation.
- class embedl_deploy.lattice.modules.LatticeConv2d(conv: Conv2d)[source]#
Bases:
Conv2dConv2dsnapped to Lattice’s supported set.Lattice hardware accepts only
1×1and3×3convolutions with stride 1 or 2 (and stride 1 is mandatory for the1×1kernel). The constructor takes an arbitrary sourceConv2dand forwards itsin_channels,out_channels,dilation,groups, and bias presence; kernel size and stride are snapped to the nearest support values.A
1×1kernel is promoted to3×3whenever its stride exceeds 1. Padding is set tokernel_size // 2on each spatial axis to preserve the output shape under the common"same"-style convention used by ResNet-family stems and downsamples.Weights and bias are copied from the source convolution whenever the snapped weight tensor has the same shape as the source’s (i.e., only stride and/or padding changed). If the kernel size was snapped — and the weight tensor shape therefore changed — the instance keeps freshly initialized weights, since there is no well-defined way to reuse the original kernel values.
- KERNEL_SIZES: tuple[int, ...] = (1, 3)#
Permitted spatial kernel sizes.
- STRIDES: tuple[int, ...] = (1, 2)#
Permitted spatial strides.
- classmethod is_compatible(conv: Conv2d) bool[source]#
Return
Truewhen conv already matches Lattice’s supported set.A convolution is compatible when its kernel size, stride, and padding equal what
snapped_paramswould return for it.- Parameters:
conv – Convolution to check.
- Returns:
Truewhen conv already conforms to Lattice constraints;Falseotherwise.
- classmethod snapped_params(conv: Conv2d) tuple[tuple[int, int], tuple[int, int], tuple[int, int]][source]#
Return
(kernel_size, stride, padding)after Lattice snapping.- Parameters:
conv – Source convolution whose parameters are snapped to the nearest values accepted by Lattice hardware.
- Returns:
A three-tuple
(kernel_size, stride, padding)of the snapped parameters where each element is itself an(h, w)pair.
- class embedl_deploy.lattice.modules.LatticeConv2dAdvanced(conv: Conv2d)[source]#
Bases:
LatticeConv2dConv2dsnapped to Lattice’s advanced-kernel set.Lattice advanced hardware supports
1×1,3×3,5×5, and7×7convolutions.3×3allows stride 1 or 2; all other kernel sizes require stride 1. Padding is alwayskernel_size // 2.Like
LatticeConv2d, weights and bias are preserved when only stride and/or padding changed, and freshly initialized when the kernel size was snapped.- KERNEL_SIZES: tuple[int, ...] = (1, 3, 5, 7)#
Permitted spatial kernel sizes.
- classmethod snapped_params(conv: Conv2d) tuple[tuple[int, int], tuple[int, int], tuple[int, int]][source]#
Return
(kernel_size, stride, padding)after snapping.Kernel size is snapped to the nearest square kernel from the supported set (
1×1,3×3,5×5,7×7) using the larger spatial dimension. Only3×3allows stride 2; all other kernel sizes are restricted to stride 1.
- class embedl_deploy.lattice.modules.LatticeLeakyReLU(act: LeakyReLU)[source]#
Bases:
ConvertedModule,LeakyReLULeakyReLUsnapped to Lattice’s supported slope.Lattice hardware implements leaky ReLU with a fixed negative slope of
1/16(0.0625). The constructor always sets the slope to the supported value; the conversion pattern logs a warning when the source slope differs.Subclasses
ConvertedModuleso that FX tracing keeps this module as a leaf node.- NEGATIVE_SLOPE: float = 0.0625#
The single permitted negative slope.
- class embedl_deploy.lattice.modules.LatticeMaxPool2d(pool: MaxPool2d)[source]#
Bases:
MaxPool2dMaxPool2dsnapped to Lattice’s supported set.Lattice hardware supports only a single max-pool configuration: a
2×2kernel with stride 2 and zero padding. The constructor takes an arbitrary sourceMaxPool2dand emits that canonical configuration, preserving onlydilationandceil_mode.- KERNEL_SIZE: int = 2#
The single permitted kernel size.
- PADDING: int = 0#
The single permitted padding.
- STRIDE: int = 2#
The single permitted stride.