Skip to content

[Torchvision API] Remove Torchvision's dependency - InterpolationMode#6383

Merged
mdabek-nvidia merged 5 commits into
NVIDIA:mainfrom
mdabek-nvidia:torchvision_enums
Jun 8, 2026
Merged

[Torchvision API] Remove Torchvision's dependency - InterpolationMode#6383
mdabek-nvidia merged 5 commits into
NVIDIA:mainfrom
mdabek-nvidia:torchvision_enums

Conversation

@mdabek-nvidia

Copy link
Copy Markdown
Collaborator

Category:

Refactoring

Description:

Remove currently the only Torchvision's dependency - InterpolationMode to make DALI Torchvision standalone solution:

  • Introduce internal _enums.py
  • Remove InterpolationMode dependencies from crops, resize and tests

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: DALI-4713

* Introduced internal _enums.py
* Removed InterpolationMode dependencies from crops, resize and tests

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

Copy link
Copy Markdown
Collaborator Author

@greptileai review

@mdabek-nvidia mdabek-nvidia marked this pull request as ready for review June 3, 2026 08:46
@greptile-apps

greptile-apps Bot commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR removes DALI's only remaining torchvision import by introducing a DALI-native InterpolationMode enum (matching torchvision's name→value mapping) and a _normalize_enum_like_interpolation_mode helper that coerces foreign enum instances—including torchvision's—without importing torchvision at all.

  • v2/_enums.py (new): defines InterpolationMode with the same seven string values as torchvision's enum, plus the private _normalize_enum_like_interpolation_mode that tries name-based then value-based lookup against the local enum, raising a descriptive ValueError for anything unrecognised.
  • resize.py / randomcrop.py / functional/*.py: swap the from torchvision.transforms import InterpolationMode import for the local one; validate_interpolation (typo fix from validate_interpoliation) now calls normalize_interpolation first so torchvision enum instances pass through transparently.
  • Tests: test_tv_interpolation_mode.py (new) verifies enum parity with torchvision, DALIInterpType mapping, normalization of torchvision-like enums, and error paths; test_tv_randomresizedcrop.py migrates most call sites to use the DALI-native enum.

Confidence Score: 5/5

Safe to merge; all changes are import swaps, a typo fix, and a new normalisation helper with no effect on existing operator behaviour.

The refactoring is mechanical: every torchvision InterpolationMode import is replaced by the DALI-native enum, and the normalisation helper is idempotent and well-tested. The typo rename (validate_interpoliation → validate_interpolation) is verified to have no remaining callers under the old name. No data-path logic changed.

No files require special attention.

Important Files Changed

Filename Overview
dali/python/nvidia/dali/experimental/torchvision/v2/_enums.py New file: defines DALI-native InterpolationMode enum (same values as torchvision's) and _normalize_enum_like_interpolation_mode helper to coerce foreign enum instances without importing torchvision.
dali/python/nvidia/dali/experimental/torchvision/v2/resize.py Swaps torchvision import for local _enums; renames validate_interpoliation→validate_interpolation (typo fix); validate_interpolation now normalizes before checking, enabling torchvision enum passthrough.
dali/python/nvidia/dali/experimental/torchvision/v2/functional/resize.py Swaps torchvision import; adds explicit normalize_interpolation call before verify_args so the local variable is an InterpolationMode member for the subsequent interpolation_modes dict lookup.
dali/python/nvidia/dali/experimental/torchvision/v2/functional/crop.py Import-only change: torchvision.InterpolationMode replaced by local _enums.InterpolationMode; resized_crop body unchanged.
dali/python/nvidia/dali/experimental/torchvision/v2/randomcrop.py Import swapped to local enum; validate_interpoliation typo corrected to validate_interpolation in _ValidateRandomResizedCropInterpolation.verify.
dali/python/nvidia/dali/experimental/torchvision/init.py Exports InterpolationMode at the package level via init.py and all, making it the public entry point for users who previously depended on torchvision's enum.
dali/test/python/torchvision/test_tv_interpolation_mode.py New test file: covers enum value parity with torchvision, DALIInterpType mapping, normalization of torchvision-like enums, and error paths; uses private torchvision internal (_check_interpolation) for exact error-message comparison.
dali/test/python/torchvision/test_tv_randomresizedcrop.py Migrates most InterpolationMode references from transforms.InterpolationMode to DALI's InterpolationMode; adds import of DALI's InterpolationMode.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["User passes interpolation\n(int, DALI InterpolationMode,\nor torchvision-like enum)"] --> B{isinstance int?}
    B -- Yes --> C["int_to_interpolation_mode lookup\n(PIL codes 0-5)"]
    C -- KeyError --> D["ValueError: not a valid PIL code"]
    C -- Found --> E["InterpolationMode member"]
    B -- No --> F["_normalize_enum_like_interpolation_mode"]
    F --> G{Already InterpolationMode?}
    G -- Yes --> E
    G -- No --> H["Try InterpolationMode[obj.name]"]
    H -- KeyError --> I["Try InterpolationMode(obj.value)"]
    I -- ValueError --> J["ValueError: unrecognised input"]
    H -- Found --> E
    I -- Found --> E
    E --> K["validate_interpolation\n(check supported modes)"]
    K --> L["interpolation_modes dict\n→ DALIInterpType"]
    L --> M["fn.resize / ndd.resize"]
Loading

Reviews (8): Last reviewed commit: "Imoroved InterpolationMode error handlin..." | Re-trigger Greptile

Comment thread dali/python/nvidia/dali/experimental/torchvision/v2/_enums.py
@greptile-apps

greptile-apps Bot commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This refactoring removes the sole Torchvision import dependency from DALI's experimental Torchvision API layer by introducing a DALI-native InterpolationMode enum in _enums.py and a compatibility bridge (normalize_enum_like_interpolation_mode) that converts torchvision-style enum objects to the local enum without an explicit import.

  • _enums.py mirrors every member of torchvision.transforms.InterpolationMode with the same string values, enabling duck-type lookup by .name or .value so existing torchvision enum objects still work transparently.
  • Resize.normalize_interpolation is updated to call the bridge at the end (instead of a bare return), and validate_interpoliation now normalizes internally before its membership checks; the functional entry-points (functional/resize.py, functional/crop.py) and RandomResizedCrop pick up the new DALI enum via updated imports.
  • Tests update two assertion lines to use the DALI-native enum but continue to pass torchvision enums as inputs, providing backward-compatibility coverage.

Confidence Score: 4/5

Safe to merge; the compatibility bridge is correct and all callers normalize before using the result as a dict key.

The enum values in _enums.py mirror torchvision's exactly, name- and value-based duck-type lookups are tested implicitly by existing tests, and all code paths that use the normalized result as an interpolation_modes dict key are guarded by validate_interpoliation raising before the lookup. The remaining comments are cosmetic (helper naming, typo, missing smoke-test for the DALI-native path).

_enums.py (internal helper naming) and the test file (no end-to-end test using the new DALI-native enum as input).

Important Files Changed

Filename Overview
dali/python/nvidia/dali/experimental/torchvision/v2/_enums.py New file introducing DALI-native InterpolationMode enum with a compatibility bridge function; bridge has a silent passthrough for unrecognized types (caught downstream by validation).
dali/python/nvidia/dali/experimental/torchvision/v2/resize.py Switches import from torchvision; adds normalize_interpolation call inside validate_interpoliation and updates normalize_interpolation to call the bridge. Double normalization is redundant but harmless due to idempotency.
dali/python/nvidia/dali/experimental/torchvision/v2/functional/resize.py Adds normalize_interpolation call before verify_args; normalize is also called internally inside validate_interpoliation — double call is safe due to idempotency.
dali/python/nvidia/dali/experimental/torchvision/v2/functional/crop.py Import-only change; resized_crop already had normalize_interpolation call before this PR.
dali/python/nvidia/dali/experimental/torchvision/v2/randomcrop.py Import-only change; no functional behavior differences.
dali/python/nvidia/dali/experimental/torchvision/init.py Exports the new InterpolationMode enum from the package's public API surface.
dali/test/python/torchvision/test_tv_randomresizedcrop.py Updates two assertions to use DALI's InterpolationMode; most test cases still pass torchvision enums as inputs (intentional backward-compatibility check), but no end-to-end test exercises the DALI-native enum as the primary input.

Sequence Diagram

sequenceDiagram
    participant User
    participant Resize/RRC as Resize / RandomResizedCrop
    participant normalize_interpolation as normalize_interpolation()
    participant normalize_bridge as normalize_enum_like_interpolation_mode()
    participant validate as validate_interpoliation()
    participant DALI as fn.resize / fn.random_resized_crop

    User->>Resize/RRC: interpolation (TV enum, DALI enum, or int)
    Resize/RRC->>normalize_interpolation: interpolation
    alt int input
        normalize_interpolation-->>Resize/RRC: InterpolationMode via int_to_interpolation_mode
    else non-int input
        normalize_interpolation->>normalize_bridge: interpolation
        alt already InterpolationMode
            normalize_bridge-->>normalize_interpolation: pass-through
        else has matching .name
            normalize_bridge-->>normalize_interpolation: InterpolationMode[name]
        else has matching .value
            normalize_bridge-->>normalize_interpolation: InterpolationMode(value)
        else unrecognized
            normalize_bridge-->>normalize_interpolation: original object (caught downstream)
        end
        normalize_interpolation-->>Resize/RRC: InterpolationMode enum
    end
    Resize/RRC->>validate: normalized interpolation
    validate->>normalize_interpolation: re-normalizes internally
    validate-->>Resize/RRC: raises NotImplementedError or ValueError if unsupported
    Resize/RRC->>DALI: interpolation_modes[InterpolationMode] → DALIInterpType
Loading

Reviews (1): Last reviewed commit: "Removed Torchvision dependency - Interpo..." | Re-trigger Greptile

Comment thread dali/python/nvidia/dali/experimental/torchvision/v2/_enums.py Outdated
Comment thread dali/test/python/torchvision/test_tv_randomresizedcrop.py
Comment thread dali/python/nvidia/dali/experimental/torchvision/v2/resize.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: [53516601]: BUILD STARTED

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

Copy link
Copy Markdown
Collaborator

CI MESSAGE: [53516601]: BUILD PASSED

Comment thread dali/python/nvidia/dali/experimental/torchvision/v2/resize.py Outdated
Comment thread dali/python/nvidia/dali/experimental/torchvision/v2/_enums.py
Comment thread dali/python/nvidia/dali/experimental/torchvision/v2/_enums.py Outdated
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
Comment thread dali/python/nvidia/dali/experimental/torchvision/v2/_enums.py Outdated
Comment thread dali/test/python/torchvision/test_tv_interpolation_mode.py Outdated
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
@mdabek-nvidia mdabek-nvidia merged commit e8ad6b5 into NVIDIA:main Jun 8, 2026
5 of 6 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.

4 participants