# 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

| Name | Description |
| --- | --- |
| [`ModuleFactory`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/blocks/utils.md#utils-Classes-ModuleFactory) | Type of a callable that builds a block from its channel counts. |

### class `ModuleFactory`

```python
class ModuleFactory(Protocol)
```

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#blocks-Classes-GeneralReparameterizableBlock)
matches.
[BottleRep](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/blocks/blocks.md#blocks-Classes-BottleRep)
calls its factory with keyword arguments.

## Functions

| Name | Description |
| --- | --- |
| [`autopad`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/blocks/utils.md#utils-Functions-autopad) | Compute the padding that keeps the size of a convolution. |
| [`forward_gather`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/blocks/utils.md#utils-Functions-forward_gather) | Run modules in sequence and collect the output of each module. |

### function `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
```

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `kernel_size` | `T` | The kernel size, as one value or one value for each axis. | required |
| `padding` | `T | None` | An explicit padding. `None` selects the computed padding. | `None` |

Returns

| Type | Description |
| --- | --- |
| `T` | `padding` when it is not `None`. Otherwise `kernel_size // 2` for each value, with the type of `kernel_size`. |

### function `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)]
```

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `x` | `Tensor` | The input of the first module. | required |
| `modules` | `Iterable[nn.Module]` | The modules, in the order in which they run. | required |

Returns

| Type | Description |
| --- | --- |
| `list[Tensor]` | The output of each module, in the same order. The list does not hold `x`. |

## Module Attributes

| Name | Description |
| --- | --- |
| [`T`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/blocks/utils.md#utils-Module-Attributes-T) |  |

### type variable `T`

```python
T = TypeVar('T', int, tuple[int, ...])
```
