Skip to content

[Torchvision API] Support for volumetric data - #6544

Open
mdabek-nvidia wants to merge 14 commits into
NVIDIA:mainfrom
mdabek-nvidia:torchvision_support_generic_CHW
Open

mdabek-nvidia wants to merge 14 commits into
NVIDIA:mainfrom
mdabek-nvidia:torchvision_support_generic_CHW

Conversation

@mdabek-nvidia

@mdabek-nvidia mdabek-nvidia commented Oct 22, 2026 •

Copy link
Copy Markdown
Collaborator

Category:

Bug fix

Description:

Torchvision.v2 accepts arbitrary leading dimensions: (..., C,H,W) the current implementation of Torchvision API uses only CHW or NCHW. That does not allow to process other data layouts like NFCHW (e.g. video frames)

The default behavior, to match the original Torchvision is to set CHW or FCHW layouts for tensors, depending on the first input data (changing layout mid-pipeline run would cause huge performance penalty, so we are not doing that). Next, the API is extended (by explicit layout and batch size arguments) to allow passing NCHW and NFCHW.

Currently, we do infer HWC only from PIL images and they do not support batching, so we do not allow: FHWC, NHWC or NFHWC, since it is unlikely to find them in the existing SW, that currently uses Torchvision. However, it is possible that these layouts will be implemented in the future as the enhancement to the existing Torchvison capabilities.

Additional information:

Affected modules and functionalities:

Reshaping the input and recovering the input shape were added to the data layout transformation.

Key points relevant for the review:

Tests:

  • Existing tests apply
  • New tests added
    • Python tests
    • GTests
    • Benchmark
    • Other
  • N/A

Checklist

Documentation

  • Existing documentation applies
  • Documentation updated
    • Docstring
    • Doxygen
    • RST
    • Jupyter
    • Other
  • N/A

DALI team only

Requirements

  • Implements new requirements
  • Affects existing requirements
  • N/A

REQ IDs: N/A

JIRA TASK: DALI-4891

Signed-off-by: Marek Dabek <mdabek@nvidia.com>
@copy-pr-bot

copy-pr-bot Bot commented Oct 22, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

Signed-off-by: Marek Dabek <mdabek@nvidia.com>
@review-notebook-app

Copy link
Copy Markdown

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@mdabek-nvidia

Copy link
Copy Markdown
Collaborator Author

!build

@greptile-apps

greptile-apps Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The latest changes appear safe to merge, with a non-blocking mismatch between the documented and actual unsupported-layout exception.

Findings

  1. P2 Unsupported layouts raise another error ▶

Summary

Adds higher-rank tensor support to the TorchVision object API.

  • Torchvision transforms keep frame sequences and batches in their original shape.
  • Normalize works across frame layouts and rejects PIL images.

The unsupported-layout exception no longer matches the guide. mdabek-nvidia rejected adding layout enums because DALI has no existing layout enums; Greptile withdrew that finding.

Reviews (8) · Last reviewed commit: "Review fixeS" · Reviewed by Greptile

Comment thread dali/test/python/torchvision/test_tv_compose.py Outdated
@dali-automaton

Copy link
Copy Markdown
Collaborator

CI MESSAGE: [75843191]: BUILD STARTED

TorchVision samples a single parameter set per call and applies it
across all leading dimensions (..., C, H, W). Compose drew random values
per sample instead, so images in a batch and frames of a video were
transformed differently.

PipelineCHW now flattens the leading dimensions into frames of FCHW
sequences:
- GPU: the whole call is processed as a single sample, which is also
  faster than per-sample processing (1.2-1.9x in RRC+flip+normalize).
- CPU: frames are split into up to min(batch_size, num_threads)
  sequences to keep thread parallelism. Random values drawn for the
  first sequence are broadcast with fn.permute_batch through the new
  share_random_value() helper.

RandomResizedCrop on CPU generates the crop window once with
fn.random_crop_generator and resizes with an ROI. Normalize uses a 4D
mean/std for FCHW input. batch_size no longer limits the leading
dimensions of the input.

Signed-off-by: Marek Dabek <mdabek@nvidia.com>

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dali-automaton

Copy link
Copy Markdown
Collaborator

CI MESSAGE: [75843191]: BUILD FAILED

@dali-automaton

Copy link
Copy Markdown
Collaborator

CI MESSAGE: [75843191]: BUILD PASSED

Signed-off-by: Marek Dabek <mdabek@nvidia.com>
Comment thread dali/python/nvidia/dali/experimental/torchvision/v2/operator.py Outdated
Comment thread dali/python/nvidia/dali/experimental/torchvision/v2/compose.py Outdated
Comment thread dali/test/python/torchvision/test_tv_compose.py Outdated
Comment thread dali/test/python/torchvision/test_tv_compose.py Outdated
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
@mdabek-nvidia

Copy link
Copy Markdown
Collaborator Author

!build

@dali-automaton

Copy link
Copy Markdown
Collaborator

CI MESSAGE: [76146454]: BUILD STARTED

@mdabek-nvidia

Copy link
Copy Markdown
Collaborator Author

!build

@dali-automaton

Copy link
Copy Markdown
Collaborator

CI MESSAGE: [76151558]: BUILD STARTED

@dali-automaton

Copy link
Copy Markdown
Collaborator

CI MESSAGE: [76151558]: BUILD PASSED

@dali-automaton

Copy link
Copy Markdown
Collaborator

CI MESSAGE: [76146454]: BUILD PASSED

@mdabek-nvidia
mdabek-nvidia marked this pull request as draft October 26, 2026 08:38
This reverts commit 89af03c.
* Supporting CHW and FCHW as default layouts for tensors

* Supporting NCHW and NFCHW as extensions to default layouts

Signed-off-by: Marek Dabek <mdabek@nvidia.com>
@mdabek-nvidia

Copy link
Copy Markdown
Collaborator Author

@greptile-apps please review

Comment thread dali/python/nvidia/dali/experimental/torchvision/v2/normalize.py
Comment thread dali/test/python/torchvision/test_tv_compose.py Outdated
Comment thread dali/python/nvidia/dali/experimental/torchvision/v2/compose.py
mdabek-nvidia and others added 2 commits October 27, 2026 13:39
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
@mdabek-nvidia

Copy link
Copy Markdown
Collaborator Author

@greptile-apps please re-review

@mdabek-nvidia

Copy link
Copy Markdown
Collaborator Author

!build

@dali-automaton

Copy link
Copy Markdown
Collaborator

CI MESSAGE: [77011372]: BUILD STARTED

Signed-off-by: Marek Dabek <mdabek@nvidia.com>
@mdabek-nvidia
mdabek-nvidia marked this pull request as ready for review October 27, 2026 19:15
Comment thread dali/test/python/torchvision/test_tv_normalize.py Outdated
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
@mdabek-nvidia

Copy link
Copy Markdown
Collaborator Author

!build

@dali-automaton

Copy link
Copy Markdown
Collaborator

CI MESSAGE: [77264341]: BUILD STARTED

Comment on lines +318 to +327
elif self.sample_layout == "FCHW":
# Any leading dimensions are flattened into the frames dimension, as in torchvision
tail = 3
supported = data_input.ndim > tail
expected = "at least 4 dimensions"
else:
raise ValueError("CHW layout is currently supported for torch.Tensor only.\
Please check if samples have the same format.")
tail = 3
supported = data_input.ndim == tail
expected = "exactly 3 dimensions"
if not supported:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
elif self.sample_layout == "FCHW":
# Any leading dimensions are flattened into the frames dimension, as in torchvision
tail = 3
supported = data_input.ndim > tail
expected = "at least 4 dimensions"
else:
raise ValueError("CHW layout is currently supported for torch.Tensor only.\
Please check if samples have the same format.")
tail = 3
supported = data_input.ndim == tail
expected = "exactly 3 dimensions"
if not supported:
else:
tail = 3 # we always need at least CHW
# Any leading dimensions are flattened into the frames dimension, as in torchvision
expected_dims = 4 if sample_layout == "FCHW" else 3
supported = data_input.ndim >= expected_dims
expected = f"at least {expected_dims} dimensions"
if not supported:

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's actually invalid fix - the supported will pass layout "CHW" and input tensor (4, 3, 8, 8,) leading to DALI throwing exception about the invalid batch size.
I've managed to simplify the code anyway.

"NFCHW": (PipelineFCHW, True),
}
_UNSUPPORTED_LAYOUTS = ("NHWC", "FHWC", "NFHWC")
_ALLOWED_LAYOUTS = (None, "HWC", "CHW", "NHWC", "NCHW", "FCHW", "FHWC", "NFCHW", "NFHWC")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a user I wouldn't be happy if the program first showed me an "allowed" layout only to find out it's not supported.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's correct - I removed unsupported layouts from allowed layouts, but I am leaving the _UNSUPPORTED_LAYOUTS, for the future support.

**dali_pipeline_kwargs,
):
if layout not in _ALLOWED_LAYOUTS:
raise ValueError(f"Unknown layout {layout!r}. Allowed values: {_ALLOWED_LAYOUTS}.")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First we say a layout is allowed, but it may yet be unsupported. This is user-hostile.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed the _ALLOWED_LAYOUTS to reflect what is supported.

Signed-off-by: Marek Dabek <mdabek@nvidia.com>
"NFCHW": (PipelineFCHW, True),
}
_UNSUPPORTED_LAYOUTS = ("NHWC", "FHWC", "NFHWC")
_ALLOWED_LAYOUTS = (None, "HWC", "CHW", "NCHW", "FCHW", "NFCHW")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Unsupported layouts raise another error

[Bug] Removing NHWC, FHWC, and NFHWC from _ALLOWED_LAYOUTS makes them raise ValueError before reaching the NotImplementedError check. The input-layout guide still promises NotImplementedError, so users following that guide would catch the wrong exception. Keep the documented exception or update the guide and remove the unreachable check.

Suggested change
_ALLOWED_LAYOUTS = (None, "HWC", "CHW", "NCHW", "FCHW", "NFCHW")
_ALLOWED_LAYOUTS = (None, "HWC", "CHW", "NHWC", "NCHW", "FCHW", "FHWC", "NFCHW", "NFHWC")

@dali-automaton

Copy link
Copy Markdown
Collaborator

CI MESSAGE: [77264341]: BUILD FAILED

@dali-automaton

Copy link
Copy Markdown
Collaborator

CI MESSAGE: [77264341]: BUILD PASSED

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants