Skip to content

Fix issue in obs manager for adding new key#169

Merged
yuecideng merged 3 commits intomainfrom
yueci/fix-obs-add-key
Mar 10, 2026
Merged

Fix issue in obs manager for adding new key#169
yuecideng merged 3 commits intomainfrom
yueci/fix-obs-add-key

Conversation

@yuecideng
Copy link
Contributor

@yuecideng yuecideng commented Mar 10, 2026

Description

Refactor assign_data_to_dict to only support TensorDict

This PR modifies the assign_data_to_dict function in embodichain/lab/gym/utils/gym_utils.py to exclusively support TensorDict instead of supporting both regular dict and TensorDict.

Changes made:

  • Changed function signature to accept TensorDict instead of Dict[str, Union[Any, Dict[str, Any]]]
  • Uses current_data.get(key) instead of current_data[key] to avoid key creation during existence check
  • Creates intermediate TensorDict with the same batch_size and device as the parent when missing keys are encountered

This change ensures consistency with other functions in the codebase that work with Tensordict and simplifies the implementation by removing dual-type support.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • Enhancement (non-breaking change which improves an existing functionality)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (existing functionality will not work without user modification)
  • Documentation update

Checklist

  • I have run the black . command to format the code base.
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • Dependencies have been updated, if applicable.

@yuecideng yuecideng added the bug Something isn't working label Mar 10, 2026
Copilot AI review requested due to automatic review settings March 10, 2026 07:47
@yuecideng yuecideng added the gym robot learning env and its related features label Mar 10, 2026
@yuecideng yuecideng requested a review from Jasonxu1225 March 10, 2026 07:47
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors assign_data_to_dict in embodichain/lab/gym/utils/gym_utils.py to operate exclusively on TensorDict, aligning observation assignment with the codebase’s TensorDict-based observation structures.

Changes:

  • Updates assign_data_to_dict signature to accept TensorDict instead of supporting both dict and TensorDict.
  • Creates missing intermediate nested containers as TensorDict instances inheriting batch_size and device from the parent.
  • Uses TensorDict.get() for type checking to avoid side effects during existence checks.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copilot AI review requested due to automatic review settings March 10, 2026 09:39
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 728 to 730
last_key = keys[-1]
current_data.batch_size = batch_size # Ensure the batch size is consistent
current_data[last_key] = value
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

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

current_data.batch_size = batch_size is likely invalid for tensordict.TensorDict (batch_size is typically derived/validated and may be read-only), and even if it works it can silently desynchronize the nested TensorDict from the shapes of its contained tensors. Prefer enforcing batch size by creating intermediate TensorDicts with the intended batch_size (e.g., from the root) and, when traversing existing nested entries, validate their batch_size matches instead of mutating it.

Copilot uses AI. Check for mistakes.
Comment on lines +708 to 716
def assign_data_to_dict(data_dict: TensorDict, name: str, value: Any) -> None:
"""Assign data to a TensorDict using a '/' separated key.
Missing intermediate TensorDicts will be created automatically.

Args:
data_dict (Dict[str, Union[Any, Dict[str, Any]]]): The nested dictionary to assign data to.
data_dict (TensorDict): The TensorDict to assign data to.
name (str): The '/' separated key string.
value (Any): The value to assign.
"""
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

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

This helper now enforces TensorDict for assignment, but the paired fetch_data_from_dict helper (used in ObservationManager) is still typed/documented as operating on Dict[...]. Consider updating the fetch helper’s type hints/docstring (and any referenced docs) to TensorDict/EnvObs too, so callers don’t get misleading typing and the TensorDict-only contract is consistent.

Copilot uses AI. Check for mistakes.
@yuecideng yuecideng merged commit b826cb8 into main Mar 10, 2026
9 checks passed
@yuecideng yuecideng deleted the yueci/fix-obs-add-key branch March 10, 2026 10:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working gym robot learning env and its related features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants