# utils

Python API: `luxonis_train.nodes.blocks.utils`

Helpers for building blocks: the padding that keeps the size of a convolution, the type of a block factory, and a forward pass
that collects intermediate outputs.

## Classes

### ModuleFactory

Type of a callable that builds a block from its channel counts.

A matching callable takes `in_channels` and `out_channels` and returns a `torch.nn.Module`. A block class such as
[GeneralReparameterizableBlock](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/blocks/blocks.md)
matches.
[BottleRep](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/blocks/blocks.md)
calls its factory with keyword arguments.

## Functions

### autopad

```python
def autopad(kernel_size: T, padding: T | None = None) -> T:
```

Compute the padding that keeps the size of a convolution.

The padding is half of the kernel size, rounded down. It keeps the size for an odd kernel size, a stride of `1`, and a dilation of
`1`.

> **Example**
> ```pycon
>>> from luxonis_train.nodes.blocks import autopad
>>> autopad(3)
1
>>> autopad((3, 5))
(1, 2)
>>> autopad(3, padding=0)
0
```

Parameters

 * `kernel_size` (`T`): The kernel size, as one value or one value for each axis.
 * `padding` (`T | None`): An explicit padding. `None` selects the computed padding.

Returns

 * `T`: `padding` when it is not `None`. Otherwise `kernel_size // 2` for each value, with the type of `kernel_size`.

### forward_gather

```python
def forward_gather(x: Tensor, modules: Iterable[nn.Module]) -> list[Tensor]:
```

Run modules in sequence and collect the output of each module.

Each module takes the output of the module before it. The first module takes `x`.

> **Example**
> ```pycon
>>> import torch
>>> from torch import nn
>>> from luxonis_train.nodes.blocks.utils import forward_gather
>>> pools = [nn.MaxPool2d(2), nn.MaxPool2d(2)]
>>> outputs = forward_gather(torch.zeros(1, 1, 8, 8), pools)
>>> [tuple(output.shape) for output in outputs]
[(1, 1, 4, 4), (1, 1, 2, 2)]
```

Parameters

 * `x` (`Tensor`): The input of the first module.
 * `modules` (`Iterable[nn.Module]`): The modules, in the order in which they run.

Returns

 * `list[Tensor]`: The output of each module, in the same order. The list does not hold `x`.

## Attributes

### T
