Skip to content

Torchvision padding#6276

Merged
mdabek-nvidia merged 35 commits intoNVIDIA:mainfrom
mdabek-nvidia:torchvision_pad
Apr 7, 2026
Merged

Torchvision padding#6276
mdabek-nvidia merged 35 commits intoNVIDIA:mainfrom
mdabek-nvidia:torchvision_pad

Conversation

@mdabek-nvidia
Copy link
Copy Markdown
Collaborator

Category:

New feature

Description:

Torchvision pad operator 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
Copy link
Copy Markdown
Collaborator Author

@greptileai please review

@mdabek-nvidia
Copy link
Copy Markdown
Collaborator Author

!build

@dali-automaton
Copy link
Copy Markdown
Collaborator

CI MESSAGE: [46880686]: BUILD STARTED

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Mar 24, 2026

Greptile Summary

This PR adds a torchvision-compatible Pad operator (class-based and functional) to DALI's experimental torchvision layer, backed by fn.slice / ndd.slice with explicit axis naming to correctly map left/top/right/bottom padding to W and H dimensions. It also includes a rename of two internal validator classes in color.py for naming consistency, and minor test/comment cleanups. Previous review concerns (axis-ordering, non-negative validation, padding-mode validation) have all been addressed.

Confidence Score: 5/5

Safe to merge; the only remaining finding is a P2 type-annotation mismatch that does not affect runtime correctness for documented use cases.

All prior P0/P1 concerns have been resolved. The single new finding is that ndd.Batch appears in pad()'s type hint but raises TypeError at runtime — this is P2 since no public test or documented workflow passes ndd.Batch directly to the functional API. Core padding logic, axis ordering, and validation are correct and cross-validated against torchvision.

dali/python/nvidia/dali/experimental/torchvision/v2/functional/pad.py — minor type-hint cleanup needed.

Important Files Changed

Filename Overview
dali/python/nvidia/dali/experimental/torchvision/v2/pad.py New _PadBase, PadConstant/Edge/Reflect/Symmetric, _ValidatePaddingMode, and Pad classes; uses axis_names='WH' to correctly map anchor/shape to W and H dimensions; non-negative padding validation is present.
dali/python/nvidia/dali/experimental/torchvision/v2/functional/pad.py New functional pad() API; correctly selects axes per input layout; has a type-hint/implementation mismatch where ndd.Batch is advertised but raises TypeError.
dali/test/python/torchvision/test_tv_pads.py New test file with thorough coverage of all four padding modes, single-axis and multi-axis padding, PIL images, and CPU/GPU variants; cross-validates against torchvision reference outputs.
dali/python/nvidia/dali/experimental/torchvision/v2/color.py Minor rename of two internal validator classes to use consistent _Validate* naming convention; no logic changes.
dali/test/python/torchvision/test_tv_color.py Removes a dead img, batch = make_tensor_inputs() assignment that was never used in the function body; correct cleanup.
dali/test/python/torchvision/test_tv_gaussian_blur.py Single-line comment cleanup removing stray [web:69][web:73] artifact; no functional change.
dali/python/nvidia/dali/experimental/torchvision/init.py Exports new Pad class and adds it to all; import ordering adjusted alphabetically.
dali/python/nvidia/dali/experimental/torchvision/v2/functional/init.py Exports new functional pad and adds it to all.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["User input\n(PIL.Image | torch.Tensor)"] --> B{pad or Pad?}
    B -->|"functional.pad()"| C{Input type}
    B -->|"Pad()()"| D["Pad.__init__\n_ValidatePaddingMode\n_ValidateIfNonNegative\nselect PadConstant/Edge/Reflect/Symmetric"]
    C -->|PIL.Image| E["axes=[-3,-2] HWC"]
    C -->|torch.Tensor| F["axes=[-2,-1] CHW"]
    C -->|other| G["raise TypeError"]
    E --> H["_ValidatePaddingMode + _ValidateIfNonNegative"]
    F --> H
    H --> I["_pad() @adjust_input\nndd.slice(anchor=(-top,-left),\nshape=(H+top+bottom,W+left+right),\naxes, out_of_bounds_policy)"]
    D --> J["_PadBase._kernel()\nfn.slice(axis_names='WH'\nanchor=(-pad_left,-pad_top)\nshape=(W+l+r, H+t+b))"]
    I --> K["Output: ndd.Tensor or ndd.Batch"]
    J --> K
Loading

Reviews (11): Last reviewed commit: "Review fixes" | Re-trigger Greptile

Comment thread dali/python/nvidia/dali/experimental/torchvision/v2/pad.py
Comment thread dali/python/nvidia/dali/experimental/torchvision/v2/color.py
Comment thread dali/python/nvidia/dali/experimental/torchvision/v2/pad.py
@dali-automaton
Copy link
Copy Markdown
Collaborator

CI MESSAGE: [46880686]: BUILD PASSED

@mdabek-nvidia
Copy link
Copy Markdown
Collaborator Author

@greptileai please re-review

@mdabek-nvidia mdabek-nvidia marked this pull request as ready for review March 25, 2026 13:10
Comment thread dali/python/nvidia/dali/experimental/torchvision/v2/gaussian_blur.py Outdated
@mdabek-nvidia mdabek-nvidia force-pushed the torchvision_pad branch 4 times, most recently from 4c57d97 to a48d7cc Compare March 25, 2026 17:54
Comment thread dali/python/nvidia/dali/experimental/torchvision/v2/functional/pad.py Outdated
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
mdabek-nvidia and others added 15 commits April 2, 2026 09:57
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
…VIDIA#6262)

* Improve call stack depth handling for error tracebacks in dynamic mode
* Rework exception_tester to automatically infer expected line and message
---------

Signed-off-by: Rostan Tabet <rtabet@nvidia.com>
Add uniform_sample option to VideoReaderDecoder

  Adds a `uniform_sample` argument that samples exactly `sequence_length`                                                               
  frames uniformly across the full video (or ROI) using linspace with
  std::round rounding. Each video produces one sample per epoch; `stride`                                                               
  and `step` are ignored (with a DALI_WARN if `pad_mode` is also set).                                                                
  `sequence_length` must be >= 1 when `uniform_sample=True`.                                                                            
                                                                                                                                        
  To avoid a per-sample heap allocation, VideoSampleDesc::frame_idxs_                                                                   
  changes from std::vector<int> to span<const int>. Owned storage moves                                                                 
  to all_frame_idxs_ (vector<vector<int>>) in the loader, which is                                                                      
  populated once in PrepareMetadataImpl and stable for the loader's                                                                     
  lifetime. The copy in ReadSample becomes a cheap 16-byte span copy.                                                                   
  all_frame_idxs_ is declared before samples_ so its lifetime encloses                                                                  
  the spans, preventing dangling references on destruction.                                                                             
                                                                                                                                        
  New tests:                                                                                                                            
  - test_uniform_sample_basic: verifies frame indices match expected linspace                                                           
  - test_uniform_sample_file_list_roi: ROI with non-zero start_frame via file_list                                                      
  - test_uniform_sample_sequence_length_zero_raises: sequence_length=0 raises                                                           
  - test_uniform_sample_stride_step_ignored: stride/step have no effect                                                                 
                                                                                                                                        
  Signed-off-by: Joaquin Anton Guirao <janton@nvidia.com>
- cudaDeviceSynchronize returns cudaErrorStreamCaptureUnsupported when any
  stream on the device is being captured. DLTensorGraveyard now puts pending
  deletions back in the queue and retries after a short wait instead of
  propagating the error.

Signed-off-by: Janusz Lisiecki <jlisiecki@nvidia.com>
* Torchvision ColorJitter and Grayscale implementations

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>
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: [47558804]: BUILD STARTED

@dali-automaton
Copy link
Copy Markdown
Collaborator

CI MESSAGE: [47558804]: BUILD PASSED

Comment thread dali/python/nvidia/dali/experimental/torchvision/v2/pad.py
Comment thread dali/python/nvidia/dali/experimental/torchvision/v2/pad.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: [47618654]: BUILD STARTED

@dali-automaton
Copy link
Copy Markdown
Collaborator

CI MESSAGE: [47618654]: BUILD PASSED

@mdabek-nvidia mdabek-nvidia merged commit 1db6751 into NVIDIA:main Apr 7, 2026
7 checks 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.

7 participants