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: AdaptiveAvgPool2d

Canonical Lattice global average pool.

The only supported form of average pooling for Lattice hardware is AdaptiveAvgPool2d with output_size == (1, 1). The constructor takes no arguments — every instance has the canonical configuration declared by OUTPUT_SIZE.

OUTPUT_SIZE: tuple[int, int] = (1, 1)#

The single permitted output size.

classmethod is_compatible(pool: AdaptiveAvgPool2d) bool[source]#

Return True when pool already matches Lattice’s supported set.

Parameters:

pool – Pool to check.

Returns:

True when the output size of pool equals (1, 1); False otherwise.

class embedl_deploy.lattice.modules.LatticeCBSR(conv: Conv2d, bn: BatchNorm2d | None, act: ReLU | LatticeLeakyReLU)[source]#

Bases: FusedModule

Fused Conv2d -> [BatchNorm2d] -> Activation for Lattice hardware.

The Lattice accelerator implements convolution, optional batch normalization, and activation as a single fused CBSR block. The only activations accepted are ReLU and LatticeLeakyReLU (negative slope fixed at 1/16). This module validates that the convolution parameters are within the hardware’s supported set and raises ValueError otherwise.

Supported convolution parameters:

  • Kernel sizes: 1×1, 3×3

  • Strides: 1, 2 (stride > 1 requires 3×3 kernel)

  • Dilation: 1 only

Parameters:
  • conv – The convolution layer to fuse.

  • bn – Optional batch normalization layer.

  • actReLU or LatticeLeakyReLU activation.

Raises:

ValueError – If conv has unsupported kernel size, stride, or dilation.

forward(x: Tensor) Tensor[source]#

Apply conv -> [bn] -> act.

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 WeightFakeQuantize must override this to return the corresponding weight tensor.

class embedl_deploy.lattice.modules.LatticeCBSRAdvanced(conv: Conv2d, bn: BatchNorm2d | None, act: ReLU | LatticeLeakyReLU)[source]#

Bases: LatticeCBSR

Fused Conv2d -> [BatchNorm2d] -> Activation for advanced CNN IP.

Like LatticeCBSR but accepts the broader kernel set supported by advanced Lattice accelerators: 1×1, 3×3, 5×5, and 7×7. Only 3×3 allows 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.

  • actReLU or LatticeLeakyReLU activation.

Raises:

ValueError – If conv has unsupported kernel size, stride, or dilation.

class embedl_deploy.lattice.modules.LatticeConv2d(conv: Conv2d)[source]#

Bases: Conv2d

Conv2d snapped to Lattice’s supported set.

Lattice hardware accepts only 1×1 and 3×3 convolutions with stride 1 or 2 (and stride 1 is mandatory for the 1×1 kernel). The constructor takes an arbitrary source Conv2d and forwards its in_channels, out_channels, dilation, groups, and bias presence; kernel size and stride are snapped to the nearest support values.

A 1×1 kernel is promoted to 3×3 whenever its stride exceeds 1. Padding is set to kernel_size // 2 on 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 True when conv already matches Lattice’s supported set.

A convolution is compatible when its kernel size, stride, and padding equal what snapped_params would return for it.

Parameters:

conv – Convolution to check.

Returns:

True when conv already conforms to Lattice constraints; False otherwise.

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: LatticeConv2d

Conv2d snapped to Lattice’s advanced-kernel set.

Lattice advanced hardware supports 1×1, 3×3, 5×5, and 7×7 convolutions. 3×3 allows stride 1 or 2; all other kernel sizes require stride 1. Padding is always kernel_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. Only 3×3 allows stride 2; all other kernel sizes are restricted to stride 1.

class embedl_deploy.lattice.modules.LatticeLeakyReLU(act: LeakyReLU)[source]#

Bases: ConvertedModule, LeakyReLU

LeakyReLU snapped 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 ConvertedModule so that FX tracing keeps this module as a leaf node.

NEGATIVE_SLOPE: float = 0.0625#

The single permitted negative slope.

classmethod is_compatible(act: LeakyReLU) bool[source]#

Return True when act already uses the supported slope.

Parameters:

act – Activation to check.

Returns:

True when the negative slope of act equals NEGATIVE_SLOPE; False otherwise.

class embedl_deploy.lattice.modules.LatticeMaxPool2d(pool: MaxPool2d)[source]#

Bases: MaxPool2d

MaxPool2d snapped to Lattice’s supported set.

Lattice hardware supports only a single max-pool configuration: a 2×2 kernel with stride 2 and zero padding. The constructor takes an arbitrary source MaxPool2d and emits that canonical configuration, preserving only dilation and ceil_mode.

KERNEL_SIZE: int = 2#

The single permitted kernel size.

PADDING: int = 0#

The single permitted padding.

STRIDE: int = 2#

The single permitted stride.

classmethod is_compatible(pool: MaxPool2d) bool[source]#

Return True when pool already matches Lattice’s supported set.

Parameters:

pool – Pool to check.

Returns:

True when kernel size, stride, and padding of pool equal the single supported configuration; False otherwise.