WZMIAOMIAO/deep-learning-for-image-processing · error · ValueError

not support data format '{self.data_format}'

Error message

not support data format '{self.data_format}'

What it means

LayerNorm (ConvNeXt variant) validates its data_format argument at construction. Only 'channels_last' and 'channels_first' are supported; any other string fails immediately in __init__. This guards the forward pass, which branches on that exact value.

Source

Thrown at pytorch_classification/ConvNeXt/model.py:56

    def forward(self, x):
        return drop_path(x, self.drop_prob, self.training)


class LayerNorm(nn.Module):
    r""" LayerNorm that supports two data formats: channels_last (default) or channels_first.
    The ordering of the dimensions in the inputs. channels_last corresponds to inputs with
    shape (batch_size, height, width, channels) while channels_first corresponds to inputs
    with shape (batch_size, channels, height, width).
    """

    def __init__(self, normalized_shape, eps=1e-6, data_format="channels_last"):
        super().__init__()
        self.weight = nn.Parameter(torch.ones(normalized_shape), requires_grad=True)
        self.bias = nn.Parameter(torch.zeros(normalized_shape), requires_grad=True)
        self.eps = eps
        self.data_format = data_format
        if self.data_format not in ["channels_last", "channels_first"]:
            raise ValueError(f"not support data format '{self.data_format}'")
        self.normalized_shape = (normalized_shape,)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        if self.data_format == "channels_last":
            return F.layer_norm(x, self.normalized_shape, self.weight, self.bias, self.eps)
        elif self.data_format == "channels_first":
            # [batch_size, channels, height, width]
            mean = x.mean(1, keepdim=True)
            var = (x - mean).pow(2).mean(1, keepdim=True)
            x = (x - mean) / torch.sqrt(var + self.eps)
            x = self.weight[:, None, None] * x + self.bias[:, None, None]
            return x


class Block(nn.Module):
    r""" ConvNeXt Block. There are two equivalent implementations:
    (1) DwConv -> LayerNorm (channels_first) -> 1x1 Conv -> GELU -> 1x1 Conv; all in (N, C, H, W)
    (2) DwConv -> Permute to (N, H, W, C); LayerNorm (channels_last) -> Linear -> GELU -> Linear; Permute back

View on GitHub (pinned to 1ec3fe6f37)

Solutions

  1. Pass data_format='channels_last' (NCHW tensors should use 'channels_first').
  2. Check spelling and case of the data_format string against ['channels_last','channels_first'].
  3. Fix any config/dict lookup that supplies a wrong or missing data_format default.

Example fix

// before
norm = LayerNorm(dim, eps=1e-6, data_format='channel_last')
// after
norm = LayerNorm(dim, eps=1e-6, data_format='channels_last')
Defensive patterns

Strategy: validation

Validate before calling

def make_ln(dim, data_format):
    assert data_format in ("channels_last", "channels_first"), f"bad data_format: {data_format!r}"
    return LayerNorm(dim, eps=1e-6, data_format=data_format)

Type guard

def is_valid_data_format(f) -> bool:
    return isinstance(f, str) and f in ("channels_last", "channels_first")

Try / catch

try:
    norm = LayerNorm(dim, eps=1e-6, data_format=fmt)
except ValueError as e:
    print(f"bad data_format {fmt!r}, defaulting to channels_last")
    norm = LayerNorm(dim, eps=1e-6, data_format="channels_last")

Prevention

When it happens

Trigger: Calling LayerNorm(normalized_shape, eps, data_format='channel_last') or any misspelled/None value instead of 'channels_last' or 'channels_first'.

Common situations: Typos like 'channels_last ' (trailing space), 'channel_last', or copying code from a version where the argument was renamed; passing a config value that defaults to None.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of WZMIAOMIAO/deep-learning-for-image-processing@1ec3fe6f37 (2026-08-30). Data as JSON: /api/errors/b6fd4c36239d25dc. Report an issue: GitHub.