Skip to content

Torchvision API RandomApply implementation#6342

Merged
mdabek-nvidia merged 4 commits into
NVIDIA:mainfrom
mdabek-nvidia:torchvision_random_apply
May 14, 2026
Merged

Torchvision API RandomApply implementation#6342
mdabek-nvidia merged 4 commits into
NVIDIA:mainfrom
mdabek-nvidia:torchvision_random_apply

Conversation

@mdabek-nvidia

Copy link
Copy Markdown
Collaborator

Category:

New feature

Description:

Torchvision API implementation of RandomApply operator

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

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

Copy link
Copy Markdown
Collaborator Author

@greptileai please review

@greptile-apps

greptile-apps Bot commented May 8, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds a RandomApply operator to DALI's experimental torchvision-compatible API, allowing a list of transforms to be applied with a configurable probability p. The implementation follows the established RandomGrayscale pattern using fn.random.coin_flip with if convert: inside enable_conditionals=True pipelines.

  • rand_apply.py: New RandomApply class that validates p via _ValidateIfZeroOneRange, stores the op list as _op_list, and uses a DALI coin-flip node as a pipeline-graph conditional to selectively apply the ops.
  • test_tv_rand_apply.py: Tests covering p=0, p=1, multi-op, shape preservation, invalid probability, and a sanity probability-distribution test.
  • __init__.py: Wires RandomApply into the module's public __all__.

Confidence Score: 5/5

Safe to merge; the implementation correctly follows the established RandomGrayscale pattern for DALI conditional execution.

The RandomApply operator faithfully mirrors the existing RandomGrayscale conditional pattern (fn.random.coin_flip + if convert: inside enable_conditionals=True) which is already working in production. The p=0 and p=1 boundary tests provide strong correctness coverage. The only remaining gap is missing deterministic seeds in the two probabilistic sanity tests, which does not affect the operator's runtime correctness.

test_tv_rand_apply.py: probabilistic tests should set a seed for reproducibility.

Important Files Changed

Filename Overview
dali/python/nvidia/dali/experimental/torchvision/v2/rand_apply.py New RandomApply operator; follows the established RandomGrayscale conditional pattern correctly; p validation and _op_list storage are correct.
dali/test/python/torchvision/test_tv_rand_apply.py Tests for p=0/1/multi-op are solid; probabilistic tests (test_random_apply_p, test_random_apply_p_sanity) run without a deterministic seed, violating test-set-seed.
dali/python/nvidia/dali/experimental/torchvision/init.py Adds RandomApply import and all entry; straightforward.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["Compose.__call__(img)"] --> B["PipelineHWC / PipelineCHW.run()"]
    B --> C["_pipeline_function (enable_conditionals=True)"]
    C --> D["fn.external_source → DataNode"]
    D --> E["RandomApply.__call__(DataNode)"]
    E --> F["Operator.__call__: verify_data, device placement"]
    F --> G["RandomApply._kernel(DataNode)"]
    G --> H["fn.random.coin_flip(probability=p) → convert:DataNode"]
    H --> I{"if convert:\n(DALI conditional branch)"}
    I -- "True (at runtime)" --> J["for op in _op_list:\n  output = op(output)"]
    J --> K["Nested op._kernel(DataNode) × N"]
    K --> M["return output (transformed)"]
    I -- "False (at runtime)" --> N["return output (unchanged = data_input)"]
    M --> O["Pipeline output → PIL / Tensor"]
    N --> O
Loading

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

Comment thread dali/test/python/torchvision/test_tv_rand_apply.py
Comment thread dali/test/python/torchvision/test_tv_rand_apply.py Outdated
Comment thread dali/python/nvidia/dali/experimental/torchvision/v2/rand_apply.py Outdated
Comment thread dali/python/nvidia/dali/experimental/torchvision/v2/rand_apply.py
@mdabek-nvidia mdabek-nvidia force-pushed the torchvision_random_apply branch from 28cff89 to 2ac9994 Compare May 8, 2026 12:12
@mdabek-nvidia

Copy link
Copy Markdown
Collaborator Author

@greptileai please re-review

@mdabek-nvidia

Copy link
Copy Markdown
Collaborator Author

!build

@dali-automaton

Copy link
Copy Markdown
Collaborator

CI MESSAGE: [50671844]: BUILD STARTED

@dali-automaton

Copy link
Copy Markdown
Collaborator

CI MESSAGE: [50671844]: BUILD FAILED

Signed-off-by: Marek Dabek <mdabek@nvidia.com>
@mdabek-nvidia mdabek-nvidia force-pushed the torchvision_random_apply branch from 2ac9994 to b0c754b Compare May 8, 2026 12:56
@mdabek-nvidia

Copy link
Copy Markdown
Collaborator Author

!build

@mdabek-nvidia mdabek-nvidia marked this pull request as ready for review May 8, 2026 12:57
@dali-automaton

Copy link
Copy Markdown
Collaborator

CI MESSAGE: [50675590]: BUILD STARTED

@dali-automaton

Copy link
Copy Markdown
Collaborator

CI MESSAGE: [50675590]: BUILD PASSED

Comment thread dali/test/python/torchvision/test_tv_rand_apply.py Outdated
@mdabek-nvidia mdabek-nvidia force-pushed the torchvision_random_apply branch from 600e74b to c52284f Compare May 13, 2026 08:21
Comment thread dali/test/python/torchvision/test_tv_rand_apply.py Fixed
Comment thread dali/test/python/torchvision/test_tv_rand_apply.py Fixed
Comment thread dali/test/python/torchvision/test_tv_rand_apply.py Outdated
@mdabek-nvidia mdabek-nvidia force-pushed the torchvision_random_apply branch from c52284f to a6ed90a Compare May 13, 2026 08:29
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
@mdabek-nvidia mdabek-nvidia force-pushed the torchvision_random_apply branch from a6ed90a to 57302e8 Compare May 13, 2026 08:36
Comment thread dali/test/python/torchvision/test_tv_rand_apply.py Outdated
@mdabek-nvidia mdabek-nvidia force-pushed the torchvision_random_apply branch from 7bfbf70 to 4bbc069 Compare May 13, 2026 12:14
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
@mdabek-nvidia mdabek-nvidia force-pushed the torchvision_random_apply branch from 4bbc069 to a4d6209 Compare May 13, 2026 12:16
@mdabek-nvidia

Copy link
Copy Markdown
Collaborator Author

!build

@dali-automaton

Copy link
Copy Markdown
Collaborator

CI MESSAGE: [51158133]: BUILD STARTED

@dali-automaton

Copy link
Copy Markdown
Collaborator

CI MESSAGE: [51158133]: BUILD PASSED

@mdabek-nvidia mdabek-nvidia merged commit 2429dd1 into NVIDIA:main May 14, 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.

5 participants