# Channels Last training


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

With
[`MixedPrecision`](https://docs.fast.ai/callback.fp16.html#mixedprecision),
image models trained in channels last format on Tensor Cores can
increase training throughput over contiguous format. PyTorch observed a
[22%
improvment](https://pytorch.org/tutorials/intermediate/memory_format_tutorial.html#performance-gains)
in ResNet50 training speed using channels last and 8-35% improvement
across a selection of models tested on a V100.

Channels last format is compatible with modern GPUs (Volta, Turing, or
newer) and modern CPUs (Ice Lake or newer).

Channels last memory format currently is implemented for NCHW Tensors.
Not all PyTorch operators have been converted to support channels last.
See [(Beta) Channels Last Memory Format in
PyTorch](https://pytorch.org/tutorials/intermediate/memory_format_tutorial.html)
tutorial for more details.

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/callback/channelslast.py#L15"
target="_blank" style="float:right; font-size:smaller">source</a>

### ChannelsLast

``` python

def ChannelsLast(
    after_create:NoneType=None, before_fit:NoneType=None, before_epoch:NoneType=None, before_train:NoneType=None,
    before_batch:NoneType=None, after_pred:NoneType=None, after_loss:NoneType=None, before_backward:NoneType=None,
    after_cancel_backward:NoneType=None, after_backward:NoneType=None, before_step:NoneType=None,
    after_cancel_step:NoneType=None, after_step:NoneType=None, after_cancel_batch:NoneType=None,
    after_batch:NoneType=None, after_cancel_train:NoneType=None, after_train:NoneType=None,
    before_validate:NoneType=None, after_cancel_validate:NoneType=None, after_validate:NoneType=None,
    after_cancel_epoch:NoneType=None, after_epoch:NoneType=None, after_cancel_fit:NoneType=None,
    after_fit:NoneType=None
):

```

*Channels last training using PyTorch’s Channels Last Memory Format
(beta)*

When a PyTorch model is set to channels last format, PyTorch will
automatically convert any compatible NCHW input tensors to NHWC format.
[`ChannelsLast`](https://docs.fast.ai/callback.channelslast.html#channelslast)
sets the model to channels last format, so no changes to dataloaders or
inputs are required.

<div>

> **Note**
>
> [`ChannelsLast`](https://docs.fast.ai/callback.channelslast.html#channelslast)
> should work with most convolutional `timm` models.
>
> However, it is advised to test each model, as supported operations
> differ across PyTorch versions.

</div>

Using
[`ChannelsLast`](https://docs.fast.ai/callback.channelslast.html#channelslast)
with unsupported PyTorch operations can lead to “channel thrashing”,
where channels last input is converted to contiguous format in an
unsupported PyTorch operation, then back to channels last for execution
on the tensor core, back to contiguous when returned to the operation,
and finally to channels last for the next layer. Too many unsupported
operations in a model can lead to reduced performance.

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/callback/channelslast.py#L24"
target="_blank" style="float:right; font-size:smaller">source</a>

### Learner.to_channelslast

``` python

def to_channelslast(
    use_amp:bool=True, # Add [`MixedPrecision`](https://docs.fast.ai/callback.fp16.html#mixedprecision) with `amp_mode`. Recommended for full channels last performance
    amp_mode:str | fastai.callback.fp16.AMPMode=<AMPMode.FP16: 'fp16'>, # Mixed Precision training mode. Supports fp16 and bf16.
    init_scale:float=65536.0, growth_factor:float=2.0, backoff_factor:float=0.5, growth_interval:int=2000,
    enabled:bool=True
):

```

*Set [`Learner`](https://docs.fast.ai/learner.html#learner) and inputs
to `channels_last` format and float16 Mixed Precision by default*

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/callback/channelslast.py#L37"
target="_blank" style="float:right; font-size:smaller">source</a>

### Learner.to_contiguous

``` python

def to_contiguous(
    to_fp32:bool=False
):

```

*Set [`Learner`](https://docs.fast.ai/learner.html#learner) and inputs
to `contiguous_format` (default format), optionally to single precision*
