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: LatticeConv2d, bn: BatchNorm2d | None = None, act: ReLU | LatticeLeakyReLU | None = None, *, output_precision: Precision = Precision.INT8, output_fixed_calibration: tuple[float, int] | None = None)[source]#
Bases:
FusedModuleFused
Conv2d -> [BatchNorm2d] -> [Activation]for Lattice hardware.The Lattice accelerator implements convolution, optional batch normalization, and optional activation as a single fused CBSR block. When an activation is present it must be
ReLUorLatticeLeakyReLU(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 – Optional
ReLUorLatticeLeakyReLUactivation.output_precision – Precision for the output quantizer.
output_fixed_calibration – Optional fixed
(scale, zero_point)pair for the output quantizer.
- Raises:
ValueError – If conv has unsupported kernel size, stride, or dilation.
- inputs_to_quantize: set[int] = {}#
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: LatticeConv2d, bn: BatchNorm2d | None = None, act: ReLU | LatticeLeakyReLU | None = None, *, output_precision: Precision = Precision.INT8, output_fixed_calibration: tuple[float, int] | None = None)[source]#
Bases:
LatticeCBSRFused
Conv2d -> [BatchNorm2d] -> [Activation]for 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 – Optional
ReLUorLatticeLeakyReLUactivation.output_precision – Precision for the output quantizer.
output_fixed_calibration – Optional fixed
(scale, zero_point)pair for the output quantizer.
- 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.LatticeFusedAddAct(act: ReLU | LatticeLeakyReLU | None = None, *, output_precision: Precision = Precision.INT8, output_fixed_calibration: tuple[float, int] | None = None)[source]#
Bases:
FusedModuleFused
add(·, residual) -> [Activation]for Lattice hardware.Fuses the element-wise addition of two branches with an optional trailing activation (
ReLUorLatticeLeakyReLU) into a single block. This captures the residual merge point in ResNet-style architectures where the convolution path has already been fused separately.Unlike
LatticeCBSRthere is no convolution, batch normalization, or weight tensor, so noWeightFakeQuantizeis attached.- Parameters:
act – Optional
ReLUorLatticeLeakyReLUactivation applied after the element-wise addition.output_precision – Precision for the output quantizer.
output_fixed_calibration – Optional fixed
(scale, zero_point)pair for the output quantizer.
- class embedl_deploy.lattice.modules.LatticeFusedLinear(linear: Linear, bn: BatchNorm1d | None = None, act: ReLU | LatticeLeakyReLU | None = None, *, output_precision: Precision = Precision.INT8, output_fixed_calibration: tuple[float, int] | None = None)[source]#
Bases:
FusedModuleFused
Linear -> [BatchNorm1d] -> [Activation]for Lattice hardware.The Lattice accelerator implements a linear (GEMM) layer with optional batch normalization and optional activation as a single fused block. When an activation is present it must be
ReLUorLatticeLeakyReLU.- Parameters:
linear – The
nn.Linearfrom the matched chain.bn – Optional batch normalization layer.
act – Optional
ReLUorLatticeLeakyReLUactivation.output_precision – Initial precision for
output_quant_stub.STATE_PREPwill override this unless output_fixed_calibration is also supplied.output_fixed_calibration – If provided,
(scale, zero_point)fixes the output quantizer so thatSTATE_PREPand calibration leave it unchanged.
- inputs_to_quantize: set[int] = {}#
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.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.
- class embedl_deploy.lattice.modules.LatticeQuant(consumers: set[Module], precision: Precision = Precision.INT8, symmetric: bool = True, calibration_method: CalibrationMethod = CalibrationMethod.MINMAX, *, channel_axis: int = 1, fixed_calibration: tuple[float, int] | None = None, canonical_name: str | None = None, producer_qualified_name: str | None = None)[source]#
Bases:
QuantStubQuantize activations and keep Lattice export identity metadata.
Extends
QuantStubwith two Lattice-specific capabilities:Tracks per-channel calibration maxima from observed activations.
Stores stable identity strings used when remapping metadata to ONNX node names during export.
- Parameters:
consumers – Set of modules that consume this quantizer’s output.
precision – Quantized format.
symmetric – Whether quantization is symmetric.
calibration_method – Activation calibration algorithm.
channel_axis – Channel axis used when tracking per-channel calibration maxima.
fixed_calibration – Optional fixed
(scale, zero_point)pair.canonical_name – Optional canonical metadata key for this quantizer.
producer_qualified_name – Optional qualified name of the producer module.
- property calibration_max: Tensor | None#
Return tracked per-channel calibration maxima, if available.
- compute_parameters() None[source]#
Compute quant parameters and synchronize Lattice export buffers.
- Raises:
ValueError – If the computed zero-point is nonzero. Lattice hardware does not support asymmetric quantization with a nonzero zero-point; the eager and ONNX export paths both assume zero-point is zero.
- set_export_identity(canonical_name: str, producer_qualified_name: str | None = None) None[source]#
Set canonical identity strings used during metadata export.
- Parameters:
canonical_name – Canonical key used for export metadata maps.
producer_qualified_name – Optional fully-qualified producer module name.