Skip to content

Conversation

@Daraan
Copy link
Contributor

@Daraan Daraan commented Aug 6, 2025

Resolves: #55288 (wrong np.array in TensorType)

Furthermore changes:

  • Changed comments to (semi)docstring which will be displayed as tooltips by IDEs (e.g. VSCode + Pylance) making that information available to the user.
  • AgentID: Any -> Hashable as it used for dict keys
  • changed DeviceType to be not a TypeVar (makes no sense in the way it is currently used), also includes DeviceLikeType (int | str | device) from torch. IMO it can fully replace the current type but being defensive I only added it as an extra possible type
  • Used updated DeviceType to improve type of Runner._device and make it more correct
  • Used torch's own type in data, current code supports more than just str. I refrained from adding a reference to rllib despite it being nice if they would be in sync.
  • Some extra formatting that is forced by pre-commit

Note

Revamps rllib.utils.typing (NDArray-based TensorType, broader DeviceType, AgentID as Hashable, docstring cleanups) and updates call sites to use optional device typing and improved hints.

  • Types:
    • Overhaul rllib/utils/typing.py:
      • TensorType now uses numpy.typing.NDArray; heavy use of TYPE_CHECKING to avoid runtime deps on torch/tf/jax.
      • DeviceType widened to Union[str, torch.device, int] (was TypeVar).
      • AgentID tightened to Hashable; NetworkType uses keras.Model.
      • Refined aliases (e.g., FromConfigSpec, SpaceStruct) and added concise docstrings.
  • Runners:
    • Runner._device now Optional (Union[DeviceType, None]) with updated docstring; same change in offline runners’ _device properties.
  • Connectors:
    • NumpyToTensor: device param typed as Optional[DeviceType] (via TYPE_CHECKING).
  • Utils:
    • from_config: typed config: Optional[FromConfigSpec] with TYPE_CHECKING import.
  • Misc:
    • Minor formatting/import ordering and comment typo fixes.

Written by Cursor Bugbot for commit ae2e422. This will update automatically on new commits. Configure here.

@Daraan Daraan requested review from a team as code owners August 6, 2025 15:31
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Summary of Changes

Hello @Daraan, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

I've made significant updates to RLlib's typing module, focusing on improving type accuracy and consistency, particularly for device handling. This includes resolving an issue with np.array in TensorType, refining AgentID and DeviceType definitions, and enhancing device type annotations across the codebase, especially within ray.data and RLlib's Runner classes.

Highlights

  • Refined RLlib Typing: Overhauled the rllib/utils/typing.py module to improve type definitions for tensors, networks, and devices, making them more robust and explicit.
  • Enhanced Device Typing: Updated DeviceType to be a Union of str, torch.device, and torch._prims_common.DeviceLikeType, providing more comprehensive device type support.
  • Corrected TensorType: Fixed an incorrect np.array reference in TensorType to use numpy.typing.NDArray, ensuring proper NumPy array type hinting.
  • Improved AgentID Definition: Changed AgentID from Any to Hashable to accurately reflect its usage as a dictionary key.
  • Consistent Device Usage in Ray Data: Integrated torch's DeviceLikeType into ray.data's iter_torch_batches function for more precise device parameter typing.
  • Clarified Runner Device Property: Modified the _device property in RLlib's Runner classes to explicitly allow None, indicating cases where a device might not be supported or set.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments or fill out our survey to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request provides a significant overhaul of the typing in rllib/utils/typing.py and improves device typing across ray.data and rllib. The changes are excellent, enhancing the correctness and clarity of type hints. Key improvements include correcting TensorType to use NDArray, fixing DeviceType from an incorrect TypeVar to a Union, and making AgentID more specific. The addition of docstrings to many type aliases is also a valuable documentation improvement. I have a couple of suggestions to make the docstrings for TensorType and DeviceType even more consistent with their type hints. Overall, this is a solid contribution.

@Daraan Daraan force-pushed the fix-tensor-type branch 2 times, most recently from ab251b3 to 1d51a4e Compare August 6, 2025 15:41
@ray-gardener ray-gardener bot added community-contribution Contributed by the community rllib RLlib related issues docs An issue or change related to documentation labels Aug 6, 2025
Daraan and others added 9 commits August 12, 2025 12:03
- Use strings instead of comments for IDE support
- rework DeviceType to be a Union of str, torch.device, and DeviceLikeType
- Minor corrections

Signed-off-by: Daniel Sperber <[email protected]>
also formatting

Signed-off-by: Daniel Sperber <[email protected]>
Signed-off-by: Daniel Sperber <[email protected]>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Signed-off-by: Daniel Sperber <[email protected]>
Signed-off-by: Daniel Sperber <[email protected]>
Signed-off-by: Daniel Sperber <[email protected]>
Signed-off-by: Daniel Sperber <[email protected]>
@Daraan Daraan requested review from a team as code owners August 12, 2025 14:04
@Daraan Daraan mentioned this pull request Aug 18, 2025
8 tasks
@kamil-kaczmarek kamil-kaczmarek changed the title Overhaul of RLlib typing module & better device typing [RLlib] Overhaul of RLlib typing module & better device typing Aug 18, 2025
@kamil-kaczmarek kamil-kaczmarek changed the title [RLlib] Overhaul of RLlib typing module & better device typing [RLlib] Overhaul of the typing module & better device typing Aug 18, 2025
@github-actions
Copy link

github-actions bot commented Sep 6, 2025

This pull request has been automatically marked as stale because it has not had
any activity for 14 days. It will be closed in another 14 days if no further activity occurs.
Thank you for your contributions.

You can always ask for help on our discussion forum or Ray's public slack channel.

If you'd like to keep this open, just leave any comment, and the stale label will be removed.

@github-actions github-actions bot added the stale The issue is stale. It will be closed within 7 days unless there are further conversation label Sep 6, 2025
@Daraan

This comment was marked as off-topic.

@Daraan
Copy link
Contributor Author

Daraan commented Sep 19, 2025

Follow up PRs:

@kamil-kaczmarek kamil-kaczmarek removed request for a team September 24, 2025 01:06
@ArturNiederfahrenhorst ArturNiederfahrenhorst added the go add ONLY when ready to merge, run all tests label Sep 24, 2025
@Daraan
Copy link
Contributor Author

Daraan commented Oct 6, 2025

@kamil-kaczmarek the failing test / example is not related to this PR right? Hard to tell from the (missing) logs, locally it is fine for me.

@kamil-kaczmarek
Copy link
Contributor

@Daraan yes, we're currently fixing an upstream issue. Fix will unblock this PR.

@pseudo-rnd-thoughts
Copy link
Member

@Daraan We have fixed the upstream issue, could you resolve the merge conflict then we can hopefully merge the PR

@simonsays1980 simonsays1980 merged commit b375907 into ray-project:master Oct 22, 2025
6 checks passed
@Daraan Daraan deleted the fix-tensor-type branch October 23, 2025 09:28
landscapepainter pushed a commit to landscapepainter/ray that referenced this pull request Nov 17, 2025
…ject#55291)

Resolves: ray-project#55288 (wrong `np.array` in `TensorType`)

Furthermore changes:
- Changed comments to (semi)docstring which will be displayed as
tooltips by IDEs (e.g. VSCode + Pylance) making that information
available to the user.
- `AgentID: Any -> Hashable` as it used for dict keys
- changed `DeviceType` to be not a TypeVar (makes no sense in the way it
is currently used), also includes DeviceLikeType (`int | str | device`)
from `torch`. IMO it can fully replace the current type but being
defensive I only added it as an extra possible type
- Used updated DeviceType to improve type of Runner._device and make it
more correct
- Used torch's own type in `data`, current code supports more than just
`str`. I refrained from adding a reference to `rllib` despite it being
nice if they would be in sync.
- Some extra formatting that is forced by pre-commit

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> Revamps `rllib.utils.typing` (NDArray-based `TensorType`, broader
`DeviceType`, `AgentID` as `Hashable`, docstring cleanups) and updates
call sites to use optional device typing and improved hints.
> 
> - **Types**:
>   - Overhaul `rllib/utils/typing.py`:
> - `TensorType` now uses `numpy.typing.NDArray`; heavy use of
`TYPE_CHECKING` to avoid runtime deps on torch/tf/jax.
> - `DeviceType` widened to `Union[str, torch.device, int]` (was
`TypeVar`).
> - `AgentID` tightened to `Hashable`; `NetworkType` uses `keras.Model`.
> - Refined aliases (e.g., `FromConfigSpec`, `SpaceStruct`) and added
concise docstrings.
> - **Runners**:
> - `Runner._device` now `Optional` (`Union[DeviceType, None]`) with
updated docstring; same change in offline runners’ `_device` properties.
> - **Connectors**:
> - `NumpyToTensor`: `device` param typed as `Optional[DeviceType]` (via
`TYPE_CHECKING`).
> - **Utils**:
> - `from_config`: typed `config: Optional[FromConfigSpec]` with
`TYPE_CHECKING` import.
> - **Misc**:
>   - Minor formatting/import ordering and comment typo fixes.
> 
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
ae2e422. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->

---------

Signed-off-by: Daniel Sperber <[email protected]>
Signed-off-by: Daraan <[email protected]>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: Kamil Kaczmarek <[email protected]>
Co-authored-by: Kamil Kaczmarek <[email protected]>
Aydin-ab pushed a commit to Aydin-ab/ray-aydin that referenced this pull request Nov 19, 2025
…ject#55291)

Resolves: ray-project#55288 (wrong `np.array` in `TensorType`)

Furthermore changes:
- Changed comments to (semi)docstring which will be displayed as
tooltips by IDEs (e.g. VSCode + Pylance) making that information
available to the user.
- `AgentID: Any -> Hashable` as it used for dict keys
- changed `DeviceType` to be not a TypeVar (makes no sense in the way it
is currently used), also includes DeviceLikeType (`int | str | device`)
from `torch`. IMO it can fully replace the current type but being
defensive I only added it as an extra possible type
- Used updated DeviceType to improve type of Runner._device and make it
more correct
- Used torch's own type in `data`, current code supports more than just
`str`. I refrained from adding a reference to `rllib` despite it being
nice if they would be in sync.
- Some extra formatting that is forced by pre-commit

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> Revamps `rllib.utils.typing` (NDArray-based `TensorType`, broader
`DeviceType`, `AgentID` as `Hashable`, docstring cleanups) and updates
call sites to use optional device typing and improved hints.
>
> - **Types**:
>   - Overhaul `rllib/utils/typing.py`:
> - `TensorType` now uses `numpy.typing.NDArray`; heavy use of
`TYPE_CHECKING` to avoid runtime deps on torch/tf/jax.
> - `DeviceType` widened to `Union[str, torch.device, int]` (was
`TypeVar`).
> - `AgentID` tightened to `Hashable`; `NetworkType` uses `keras.Model`.
> - Refined aliases (e.g., `FromConfigSpec`, `SpaceStruct`) and added
concise docstrings.
> - **Runners**:
> - `Runner._device` now `Optional` (`Union[DeviceType, None]`) with
updated docstring; same change in offline runners’ `_device` properties.
> - **Connectors**:
> - `NumpyToTensor`: `device` param typed as `Optional[DeviceType]` (via
`TYPE_CHECKING`).
> - **Utils**:
> - `from_config`: typed `config: Optional[FromConfigSpec]` with
`TYPE_CHECKING` import.
> - **Misc**:
>   - Minor formatting/import ordering and comment typo fixes.
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
ae2e422. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->

---------

Signed-off-by: Daniel Sperber <[email protected]>
Signed-off-by: Daraan <[email protected]>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: Kamil Kaczmarek <[email protected]>
Co-authored-by: Kamil Kaczmarek <[email protected]>
Signed-off-by: Aydin Abiar <[email protected]>
raulchen pushed a commit that referenced this pull request Nov 19, 2025
## Why are these changes needed?

Follow up of RLlib PR: #55291
Torch `device` allows input types of `str | int | torch.device`, this PR
unifies the type in a type variable and allows for the `int` type as
well.

Upstream air PR: #56745

## Checks

- [x] I've signed off every commit(by using the -s flag, i.e., `git
commit -s`) in this PR.
- [x] I've run `scripts/format.sh` to lint the changes in this PR.
- [x] I've included any doc changes needed for
https://docs.ray.io/en/master/.
- [ x I've added any new APIs to the API Reference. For example, if I
added a
method in Tune, I've added it in `doc/source/tune/api/` under the
           corresponding `.rst` file.
- [x] I've made sure the tests are passing. Note that there might be a
few flaky tests, see the recent failures at https://flakey-tests.ray.io/
- Testing Strategy
   - [x] This PR is not tested :(

---------

Signed-off-by: Daniel Sperber <[email protected]>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
400Ping pushed a commit to 400Ping/ray that referenced this pull request Nov 21, 2025
…ct#56743)

## Why are these changes needed?

Follow up of RLlib PR: ray-project#55291
Torch `device` allows input types of `str | int | torch.device`, this PR
unifies the type in a type variable and allows for the `int` type as
well.

Upstream air PR: ray-project#56745

## Checks

- [x] I've signed off every commit(by using the -s flag, i.e., `git
commit -s`) in this PR.
- [x] I've run `scripts/format.sh` to lint the changes in this PR.
- [x] I've included any doc changes needed for
https://docs.ray.io/en/master/.
- [ x I've added any new APIs to the API Reference. For example, if I
added a
method in Tune, I've added it in `doc/source/tune/api/` under the
           corresponding `.rst` file.
- [x] I've made sure the tests are passing. Note that there might be a
few flaky tests, see the recent failures at https://flakey-tests.ray.io/
- Testing Strategy
   - [x] This PR is not tested :(

---------

Signed-off-by: Daniel Sperber <[email protected]>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
ykdojo pushed a commit to ykdojo/ray that referenced this pull request Nov 27, 2025
…ct#56743)

## Why are these changes needed?

Follow up of RLlib PR: ray-project#55291
Torch `device` allows input types of `str | int | torch.device`, this PR
unifies the type in a type variable and allows for the `int` type as
well.

Upstream air PR: ray-project#56745

## Checks

- [x] I've signed off every commit(by using the -s flag, i.e., `git
commit -s`) in this PR.
- [x] I've run `scripts/format.sh` to lint the changes in this PR.
- [x] I've included any doc changes needed for
https://docs.ray.io/en/master/.
- [ x I've added any new APIs to the API Reference. For example, if I
added a
method in Tune, I've added it in `doc/source/tune/api/` under the
           corresponding `.rst` file.
- [x] I've made sure the tests are passing. Note that there might be a
few flaky tests, see the recent failures at https://flakey-tests.ray.io/
- Testing Strategy
   - [x] This PR is not tested :(

---------

Signed-off-by: Daniel Sperber <[email protected]>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Signed-off-by: YK <[email protected]>
SheldonTsen pushed a commit to SheldonTsen/ray that referenced this pull request Dec 1, 2025
…ct#56743)

## Why are these changes needed?

Follow up of RLlib PR: ray-project#55291
Torch `device` allows input types of `str | int | torch.device`, this PR
unifies the type in a type variable and allows for the `int` type as
well.

Upstream air PR: ray-project#56745

## Checks

- [x] I've signed off every commit(by using the -s flag, i.e., `git
commit -s`) in this PR.
- [x] I've run `scripts/format.sh` to lint the changes in this PR.
- [x] I've included any doc changes needed for
https://docs.ray.io/en/master/.
- [ x I've added any new APIs to the API Reference. For example, if I
added a
method in Tune, I've added it in `doc/source/tune/api/` under the
           corresponding `.rst` file.
- [x] I've made sure the tests are passing. Note that there might be a
few flaky tests, see the recent failures at https://flakey-tests.ray.io/
- Testing Strategy
   - [x] This PR is not tested :(

---------

Signed-off-by: Daniel Sperber <[email protected]>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Future-Outlier pushed a commit to Future-Outlier/ray that referenced this pull request Dec 7, 2025
…ject#55291)

Resolves: ray-project#55288 (wrong `np.array` in `TensorType`)

Furthermore changes:
- Changed comments to (semi)docstring which will be displayed as
tooltips by IDEs (e.g. VSCode + Pylance) making that information
available to the user.
- `AgentID: Any -> Hashable` as it used for dict keys
- changed `DeviceType` to be not a TypeVar (makes no sense in the way it
is currently used), also includes DeviceLikeType (`int | str | device`)
from `torch`. IMO it can fully replace the current type but being
defensive I only added it as an extra possible type
- Used updated DeviceType to improve type of Runner._device and make it
more correct
- Used torch's own type in `data`, current code supports more than just
`str`. I refrained from adding a reference to `rllib` despite it being
nice if they would be in sync.
- Some extra formatting that is forced by pre-commit

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> Revamps `rllib.utils.typing` (NDArray-based `TensorType`, broader
`DeviceType`, `AgentID` as `Hashable`, docstring cleanups) and updates
call sites to use optional device typing and improved hints.
>
> - **Types**:
>   - Overhaul `rllib/utils/typing.py`:
> - `TensorType` now uses `numpy.typing.NDArray`; heavy use of
`TYPE_CHECKING` to avoid runtime deps on torch/tf/jax.
> - `DeviceType` widened to `Union[str, torch.device, int]` (was
`TypeVar`).
> - `AgentID` tightened to `Hashable`; `NetworkType` uses `keras.Model`.
> - Refined aliases (e.g., `FromConfigSpec`, `SpaceStruct`) and added
concise docstrings.
> - **Runners**:
> - `Runner._device` now `Optional` (`Union[DeviceType, None]`) with
updated docstring; same change in offline runners’ `_device` properties.
> - **Connectors**:
> - `NumpyToTensor`: `device` param typed as `Optional[DeviceType]` (via
`TYPE_CHECKING`).
> - **Utils**:
> - `from_config`: typed `config: Optional[FromConfigSpec]` with
`TYPE_CHECKING` import.
> - **Misc**:
>   - Minor formatting/import ordering and comment typo fixes.
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
ae2e422. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->

---------

Signed-off-by: Daniel Sperber <[email protected]>
Signed-off-by: Daraan <[email protected]>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: Kamil Kaczmarek <[email protected]>
Co-authored-by: Kamil Kaczmarek <[email protected]>
Signed-off-by: Future-Outlier <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Contributed by the community docs An issue or change related to documentation go add ONLY when ready to merge, run all tests rllib RLlib related issues unstale A PR that has been marked unstale. It will not get marked stale again if this label is on it.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[RLlib][P3] typing.TensorType is written incorrectly causing pyright errors

5 participants