Skip to content

[Doc] Document composite spec shapes for batched envs - #4473

Open
YeonwooSung wants to merge 1 commit into
pytorch:mainfrom
YeonwooSung:doc/1896-env-complex-spec-shapes
Open

YeonwooSung wants to merge 1 commit into
pytorch:mainfrom
YeonwooSung:doc/1896-env-complex-spec-shapes

Conversation

@YeonwooSung

Copy link
Copy Markdown
Contributor

Description

Document the composite vs leaf spec-shape rule for unbatched and batched environments.

Issue #1896 asked how to set observation_spec / action_spec when observations have a feature shape such as [8, 8, 13] or an image. The env tutorial explained batch_size but not that the composite carries the env batch (empty if unbatched) and each leaf is that batch plus the feature. Maintainer wording on the issue is now in the tutorial and spec docs:

  • Unbatched env: composite shape [], image leaf [3, 64, 64].
  • Batched env (ParallelEnv with N workers, or batch_size=[N]): every spec has a leading [N, *]; composite [N], image leaf [N, 3, 64, 64].
  • Nested MARL groups: the env can stay unbatched while inner composites carry a per-group agent dimension.

The examples construct specs a custom env would assign (GymEnv / ParallelEnv / Composite); they do not add a new env class.

Code example

from torchrl.data import Bounded, Composite

# Unbatched env: empty composite shape, image leaf is feature-only.
observation_spec = Composite(
    pixels=Bounded(low=0, high=255, shape=(3, 64, 64), dtype=torch.uint8),
    shape=(),
)
assert observation_spec.shape == ()
assert observation_spec["pixels"].shape == (3, 64, 64)

# Batched env with N=2: every spec carries a leading [2].
batched_observation_spec = Composite(
    pixels=Bounded(low=0, high=255, shape=(2, 3, 64, 64), dtype=torch.uint8),
    shape=(2,),
)
assert batched_observation_spec.shape == (2,)
assert batched_observation_spec["pixels"].shape == (2, 3, 64, 64)

Motivation and Context

Why is this change required? What problem does it solve?
If it fixes an open issue, please link to the issue here.
You can use the syntax close #15213 if this solves the issue #15213

  • I have raised an issue to propose this change (required for new features and bug fixes)

close #1896

Types of changes

What types of changes does your code introduce? Remove all that do not apply:

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds core functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation (a change to docs/website/examples)

Checklist

  • I have read the docs and, if possible, made changes to improve them
  • I have read the examples and, if possible, made changes to improve them
  • I have tested my changes against the tutorial and/or examples
  • My PR is focused and contains directly related changes only (if not, break it down into several PRs)

@pytorch-bot

pytorch-bot Bot commented Sep 22, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/4473

Note: Links to docs will display an error until the docs builds have been completed.

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

⚠️ 16 Awaiting Approval

As of commit be06786 with merge base 84d4602 (image):

AWAITING APPROVAL - The following workflows need approval before CI can run:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@github-actions github-actions Bot added Documentation Improvements or additions to documentation tutorials/ labels Sep 22, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Sep 22, 2026

This branch has not been deployed

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

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Documentation Improvements or additions to documentation tutorials/

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request] Tutorial for custom env with complex shapes

1 participant