Skip to content

Torchvision normalize#6278

Open
mdabek-nvidia wants to merge 47 commits intoNVIDIA:mainfrom
mdabek-nvidia:torchvision_normalize
Open

Torchvision normalize#6278
mdabek-nvidia wants to merge 47 commits intoNVIDIA:mainfrom
mdabek-nvidia:torchvision_normalize

Conversation

@mdabek-nvidia
Copy link
Copy Markdown
Collaborator

Category:

New feature

Description:

Torchvision's normalize operators implementation

Additional information:

Affected modules and functionalities:

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: N/A

mdabek-nvidia and others added 10 commits March 19, 2026 13:30
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
Co-authored-by: Kamil Tokarski <kamiltokarski04@gmail.com>
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
Signed-of-by: Marek Dabek <mdabek@nvidia.com>
@mdabek-nvidia mdabek-nvidia force-pushed the torchvision_normalize branch 2 times, most recently from 810618b to 52af0c6 Compare March 25, 2026 13:49
@mdabek-nvidia
Copy link
Copy Markdown
Collaborator Author

@greptileai please review

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Mar 25, 2026

Greptile Summary

This PR adds Normalize and Pad operators to NVIDIA DALI's experimental torchvision-compatibility layer, along with their functional equivalents (normalize and pad). Both operators mirror the corresponding torchvision.transforms.v2 APIs and are backed by fn.normalize / ndd.normalize and fn.slice / ndd.slice respectively. Several issues from prior review rounds have been addressed (scalar-hue negativity guard in _ValidateHue, argument validation ordering in functional/normalize.py, isinstance fixes in gaussian_blur.py).

Key findings:

  • P1 – functional/pad.py type-contract violation: The pad function's signature declares ndd.Batch as a valid input type, but the body immediately raises TypeError for any input that is not Image.Image or torch.Tensor. Any caller relying on the type hint for batch inputs will hit a confusing runtime error.
  • P2 – Dead inplace parameter in _normalize: The private helper _normalize in functional/normalize.py accepts inplace but never uses it; the outer normalize already guards against this before calling the helper.
  • P2 – Misleading error in get_padding: When a sequence of wrong length is passed (e.g. length 3), the error message shows the type (got <class 'list'>) rather than the length, making the cause harder to diagnose.
  • Test coverage for both operators is thorough — CPU/GPU, multiple channel counts, PIL images, all four padding modes, and edge-case validation are all exercised.

Confidence Score: 4/5

Safe to merge after resolving the ndd.Batch type-contract violation in functional/pad.py.

A P1 issue remains: the pad functional advertises ndd.Batch support in its type annotation but raises TypeError unconditionally for it. This is a real API contract violation that will silently mislead users. The remaining findings are P2 style/clarity items that don't affect correctness for the supported input types. Prior P0/P1 concerns from earlier rounds have all been addressed.

dali/python/nvidia/dali/experimental/torchvision/v2/functional/pad.py — the pad function's type annotation and implementation are inconsistent regarding ndd.Batch.

Important Files Changed

Filename Overview
dali/python/nvidia/dali/experimental/torchvision/v2/functional/pad.py New functional pad wrapper; type signature advertises ndd.Batch support but implementation raises TypeError for it — a P1 contract violation.
dali/python/nvidia/dali/experimental/torchvision/v2/functional/normalize.py New functional normalize wrapper; validates args before reshape (previous P1 fixed); minor unused inplace parameter in _normalize helper.
dali/python/nvidia/dali/experimental/torchvision/v2/normalize.py New Normalize operator; correctly handles both HWC and CHW layouts by inspecting the last layout character; validators and inplace guard look correct.
dali/python/nvidia/dali/experimental/torchvision/v2/pad.py New Pad operator with constant/edge/reflect/symmetric modes; get_padding error message is slightly misleading for wrong-length sequences; otherwise logic is sound.
dali/python/nvidia/dali/experimental/torchvision/v2/color.py Adds scalar-hue negativity guard in _ValidateHue (fixing the previous P1) and renames internal classes to private naming convention.
dali/test/python/torchvision/test_tv_normalize.py Comprehensive normalize tests covering channel counts, CPU/GPU, sequence types, mismatch errors, zero-std rejection, and spatial-broadcast correctness.
dali/test/python/torchvision/test_tv_pads.py Thorough pad tests across all four padding modes, single and multi-border paddings, PIL images, and both CPU/GPU paths.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["User calls normalize(inpt, mean, std)
    or Normalize(mean, std)(inpt)"] --> B{Validate args}
    B -- "Invalid std/mean type\nor std==0" --> E1[Raise TypeError / ValueError]
    B -- Valid --> C{Input type}
    C -- "torch.Tensor (CHW)" --> D1["Reshape mean/std as\nmean[:, None, None]"]
    C -- "Pipeline DataNode\n(CHW layout)" --> D2["Reshape mean/std as\nmean[:, None, None]"]
    C -- "Pipeline DataNode\n(HWC layout)" --> D3["Reshape mean/std as\nmean[None, None, :]"]
    D1 --> F["ndd.normalize(input, mean, stddev)"]
    D2 --> G["fn.normalize(input, mean, stddev)"]
    D3 --> G

    A2["User calls pad(inpt, padding, mode)
    or Pad(padding, mode)(inpt)"] --> B2{Validate\npadding_mode\n& non-negative}
    B2 -- Invalid --> E2[Raise ValueError / TypeError]
    B2 -- Valid --> C2{Input type}
    C2 -- "Image.Image" --> P1["axes=[-3,-2]"]
    C2 -- "torch.Tensor" --> P2["axes=[-2,-1]"]
    C2 -- "ndd.Batch" --> P3["TypeError raised\n(type hint mismatch)"]
    P1 & P2 --> Q["ndd.slice with\nnegative anchor for padding\nborder_type from mode"]
Loading

Reviews (9): Last reviewed commit: "Improving type hints" | Re-trigger Greptile

@mdabek-nvidia mdabek-nvidia force-pushed the torchvision_normalize branch from 6567dda to 160ec66 Compare March 25, 2026 14:00
@mdabek-nvidia
Copy link
Copy Markdown
Collaborator Author

@greptileai please re-review

@mdabek-nvidia mdabek-nvidia force-pushed the torchvision_normalize branch 2 times, most recently from b4e11d2 to 6adc717 Compare March 25, 2026 17:56
@mdabek-nvidia mdabek-nvidia marked this pull request as ready for review March 25, 2026 17:56
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
@mdabek-nvidia mdabek-nvidia force-pushed the torchvision_normalize branch from 6adc717 to 64809e3 Compare March 31, 2026 08:50
@jantonguirao jantonguirao self-requested a review March 31, 2026 09:16
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
@mdabek-nvidia mdabek-nvidia force-pushed the torchvision_normalize branch from 64809e3 to 0d6c170 Compare April 2, 2026 17:31
@mdabek-nvidia mdabek-nvidia force-pushed the torchvision_normalize branch from f5ac5fa to c0f9a56 Compare April 3, 2026 09:16
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
@mdabek-nvidia mdabek-nvidia force-pushed the torchvision_normalize branch from c0f9a56 to e86e551 Compare April 3, 2026 12:21
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
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.

7 participants