pytorch / pytorch/rl

[Bug] Assert CompositeSpec order guarantees

Open
#2,482 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
3.6k
Forks
487
Avg merge
1d 1h
Merged PRs (30d)
207

Description

Discussed in https://github.com/pytorch/rl/discussions/2341

Originally posted by hesic73 July 31, 2024
I have a question about the order of keys returned by CompositeSpec.keys(include_nested=True, leaves_only=False). Is there any guarantee on the order of the keys, specifically that the iterator iterates from the deepest nodes to the root?

I want to write a function that removes all empty entries from a CompositeSpec:

def _remove_empty_entries(spec: CompositeSpec) -> CompositeSpec:
    for key in list(spec.keys(True, False)):
        if not isinstance(spec[key], CompositeSpec):
            continue
        if spec[key].is_empty():
            del spec[key]
    return spec

As shown in the function above, it assumes that the iterator processes from the deepest nodes to the root. This assumption is crucial for the function to work correctly.

If there is no such guarantee, how can I achieve this goal? A CompositeSpec with empty entries is quite undesirable.

Thank you!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the CompositeSpec.keys(include_nested=True, leaves_only=False) entry point and inspect its traversal behavior. Define regression coverage for nested and empty entries based on the issue's deepest-to-root requirement, then run the existing CompositeSpec tests and verify that the asserted order supports removing empty composites.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
api, machine-learning
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.