Skip to content

Fix int downcasting in dynamic mode - #6480

Open
rostan-t wants to merge 5 commits into
NVIDIA:mainfrom
rostan-t:ndd-int-overflow
Open

Fix int downcasting in dynamic mode#6480
rostan-t wants to merge 5 commits into
NVIDIA:mainfrom
rostan-t:ndd-int-overflow

Conversation

@rostan-t

Copy link
Copy Markdown
Collaborator

Category:

Bug fix (non-breaking change which fixes an issue)

Description:

Currently, 64 bits integers are cast down to int32 in dynamic mode, creating silent overflows. Python big integers aren't detected as such, go through the enum path and raise an obscure exception. Since the downcasting is performed immediately when consuming data (in the Tensor ctor), this can lead to surprising behavior:

>>> ndd.cast(1 << 31, dtype=ndd.uint32)
Tensor(
    0,
    dtype=u32,
    device="cpu",
    shape=())

This PR fixes this by raising an exception if an integer that doesn't fit on 32 bits is used. Data that can be represented as signed int32 is stored as such and uint32 is used instead.

Generally, the entry point for data in dynamic mode is always transitively ndd.Tensor so the fix is done there. Batch.broadcast re-implemented conversions on its own and now uses ndd.as_tensor instead.

Additional information:

Affected modules and functionalities:

Dynamic mode.

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: Rostan Tabet <rtabet@nvidia.com>
Signed-off-by: Rostan Tabet <rtabet@nvidia.com>
Signed-off-by: Rostan Tabet <rtabet@nvidia.com>
@rostan-t rostan-t added the dynamic mode Related to dynamic mode label Sep 11, 2026
@greptile-apps

greptile-apps Bot commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge; the latest change fixes the remaining docstring typo and no outstanding findings remain.

Summary

  • Infers int32 or uint32 while rejecting inferred integers outside the supported 32-bit ranges.
  • Reuses the conversion path in Batch.broadcast.
  • Adds boundary, overflow, explicit-cast, and broadcast tests.
  • Fixes the duplicated word in the integer-conversion docstring.

Reviews (3) · Last reviewed commit: "Align explicit integer casting with NumP..."

Comment thread dali/python/nvidia/dali/experimental/dynamic/_tensor.py Outdated
Comment thread dali/python/nvidia/dali/experimental/dynamic/_tensor.py Outdated
Comment on lines -473 to -486
with Batch._nvtx_to_numpy_and_stack:
arr = np.array(unwrap_invariants(sample))
converted_dtype_id = None
if arr.dtype == np.float64:
arr = arr.astype(np.float32)
elif arr.dtype == np.int64:
arr = arr.astype(np.int32)
elif arr.dtype == np.uint64:
arr = arr.astype(np.uint32)
elif arr.dtype == object:
arr, converted_dtype_id = _try_convert_enums(arr)
if dtype is not None and dtype.kind != DType.Kind.enum:
arr = arr.astype(_dali_types.to_numpy_type(dtype.type_id))
arr = np.repeat(arr[np.newaxis], batch_size, axis=0)

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.

This was an optimization to keep batches of scalars contiguous. Frankly, I haven't measured it but some operations still perform better if they work with contiguous data (e.g. issuing fewer H2D copies).

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.

Done in ed84df7.

@mzient mzient left a comment

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.

Please restore the optimization that broadcasts scalars to contiguous batches. This results in fewer D2H copies when such a batch is subsequently used in a GPU operator.

This way we can restore the optimization broadcasting scalars contiguous while preserving the
casting rules

Signed-off-by: Rostan Tabet <rtabet@nvidia.com>
@rostan-t
rostan-t requested a review from mzient September 11, 2026 14:56
@rostan-t

Copy link
Copy Markdown
Collaborator Author

!build

Comment thread dali/python/nvidia/dali/experimental/dynamic/_tensor.py Outdated
@dali-automaton

Copy link
Copy Markdown
Collaborator

CI MESSAGE: [67382482]: BUILD STARTED

Signed-off-by: Rostan Tabet <rtabet@nvidia.com>
@dali-automaton

Copy link
Copy Markdown
Collaborator

CI MESSAGE: [67382482]: BUILD FAILED

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

Labels

dynamic mode Related to dynamic mode

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants