Skip to content

Conversation

@rostan-t
Copy link
Collaborator

@rostan-t rostan-t commented Nov 18, 2025

Category: New feature (non-breaking change which adds functionality)

Description:

Currently stub (*.pyi) files are only generated for "fn" and the legacy "ops" API. This PR adds stubs generation for dynamic mode.

Additional information:

Affected modules and functionalities:

New __init__.pyi files get created during the build process.

Key points relevant for the review:

  • Does the IDE / language server complain when using the API normally?

  • Does the IDE / language server properly report incorrect types?

  • Does the IDE properly suggest code completion?

  • You can use typing.reveal_type to see what type the type checker infers. For example:

    example

Tests:

  • Existing tests apply
  • New tests added
    • Python tests
    • GTests
    • Benchmark
    • Other
  • N/A

Checklist

Documentation

DALI team only

Requirements

  • Implements new requirements
  • Affects existing requirements
  • N/A

REQ IDs: N/A

JIRA TASK: DALI-4439

@rostan-t rostan-t marked this pull request as ready for review November 21, 2025 16:59
@greptile-apps
Copy link

greptile-apps bot commented Nov 21, 2025

Greptile Overview

Greptile Summary

This PR extends DALI's stub file (.pyi) generation to support the dynamic mode API, which previously only had stubs for the fn and ops APIs.

Key changes:

  • Added comprehensive stub generation for dynamic mode operators and functions in _signatures.py with proper handling of overloaded signatures based on input types (Tensor, Batch, TensorLike)
  • Correctly splits keyword arguments between __init__ (non-tensor kwargs) and __call__ (tensor kwargs only) to match runtime behavior in _op_builder.py
  • Added DLPack protocol and TensorLike type alias to _typing.py for dynamic mode type annotations
  • Made __cuda_array_interface__ a property in dynamic Tensor class for protocol compatibility
  • Updated fn and ops modules to pass api parameter for proper signature generation
  • Added _generated attribute to dynamically created operator classes/functions for stub detection
  • Modified stub generator script to include dynamic mode in the build process

The implementation properly handles the complex type system for dynamic mode, including tensor/batch broadcasting semantics and proper type narrowing through overloads.

Confidence Score: 4/5

  • This PR is safe to merge with very low risk - changes are well-structured and previous critical issues have been addressed
  • The PR adds a significant new feature (dynamic mode stub generation) with careful attention to type correctness. All previously identified critical issues (builtins module handling, tensor/non-tensor kwarg split, API parameter passing) have been fixed in recent commits. The main signature generation file (_signatures.py) is complex but the logic correctly mirrors the runtime behavior. Minor complexity in the code is inherent to handling multiple overloads and API variants.
  • Pay close attention to dali/python/nvidia/dali/ops/_signatures.py due to its complexity, though no critical issues were found

Important Files Changed

File Analysis

Filename Score Overview
dali/python/nvidia/dali/ops/_signatures.py 4/5 Adds comprehensive stub generation for dynamic mode with proper tensor/non-tensor kwarg split, API parameter threading, and type annotations
dali/python/nvidia/dali/experimental/dynamic/_op_builder.py 5/5 Adds _generated attribute to dynamically built operator classes and functions for stub generation detection
internal_tools/python_stub_generator.py 5/5 Adds dynamic mode stub generation to the build process

Sequence Diagram

sequenceDiagram
    participant Build as Build Process
    participant StubGen as python_stub_generator.py
    participant Sigs as _signatures.py
    participant OpBuilder as _op_builder.py
    participant Backend as DALI Backend
    
    Build->>StubGen: Generate stubs for wheel
    StubGen->>Sigs: gen_all_signatures(path, "fn")
    StubGen->>Sigs: gen_all_signatures(path, "ops")
    StubGen->>Sigs: gen_all_signatures(path, "dynamic")
    
    Sigs->>Backend: _registry._all_registered_ops()
    Backend-->>Sigs: List of schema names
    
    loop For each operator schema
        Sigs->>Backend: _b.TryGetSchema(schema_name)
        Backend-->>Sigs: OpSchema
        
        alt Dynamic API
            Sigs->>Sigs: _gen_dynamic_signature(schema, name)
            Sigs->>Sigs: _gen_dynamic_cls_signature() or _gen_dynamic_fun_signature()
            Sigs->>Sigs: _gen_dynamic_call_signature(include_only_inputs=True)
            Note over Sigs: __call__ gets tensor kwargs only
            Sigs->>Sigs: _call_signature(include_kwarg_inputs=False)
            Note over Sigs: __init__ gets non-tensor kwargs only
        else Fn API
            Sigs->>Sigs: _gen_fn_signature(schema, name)
            Sigs->>Sigs: _call_signature(api="fn", include_inputs=True)
        else Ops API
            Sigs->>Sigs: _gen_ops_signature(schema, name)
            Sigs->>Sigs: _call_signature(api="ops") for __init__ and __call__
        end
        
        Sigs->>Sigs: Write signature to .pyi file
    end
    
    alt Dynamic mode operators
        OpBuilder->>OpBuilder: build_operator_class(schema)
        OpBuilder->>OpBuilder: Set _generated = True
        Note over OpBuilder: Runtime: __init__ skips tensor args,<br/>__call__ includes tensor args only
    end
    
    Sigs-->>StubGen: Stub files created
    StubGen-->>Build: Stubs ready for packaging
Loading

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

7 files reviewed, 2 comments

Edit Code Review Agent Settings | Greptile

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

7 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

7 files reviewed, 2 comments

Edit Code Review Agent Settings | Greptile

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

7 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

@rostan-t
Copy link
Collaborator Author

!build

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [39065210]: BUILD STARTED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [39065210]: BUILD FAILED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [39065347]: BUILD STARTED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [39065347]: BUILD FAILED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [39070781]: BUILD STARTED

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

7 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [39070781]: BUILD FAILED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [39076892]: BUILD STARTED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [39076892]: BUILD FAILED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [39079519]: BUILD STARTED

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

8 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [39079519]: BUILD FAILED

@rostan-t
Copy link
Collaborator Author

!build

Signed-off-by: Rostan Tabet <[email protected]>
Signed-off-by: Rostan Tabet <[email protected]>
Signed-off-by: Rostan Tabet <[email protected]>
Signed-off-by: Rostan Tabet <[email protected]>
The stubs generation script imports dali.exerimental.dynamic,
which requires NVTX and makefun to be importable.

Signed-off-by: Rostan Tabet <[email protected]>
Signed-off-by: Rostan Tabet <[email protected]>
Signed-off-by: Rostan Tabet <[email protected]>
Signed-off-by: Rostan Tabet <[email protected]>
Signed-off-by: Rostan Tabet <[email protected]>
Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

9 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@rostan-t
Copy link
Collaborator Author

!build

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [39226828]: BUILD STARTED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [39226828]: BUILD PASSED

@rostan-t rostan-t merged commit 8cb9b6d into NVIDIA:main Nov 27, 2025
7 checks passed
@rostan-t rostan-t deleted the ndd_pyi branch November 27, 2025 09:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants