Skip to content

Add a variation build context, a host asset and per-environment build-time records - #1433

Closed
cvolkcvolk wants to merge 25 commits into
mainfrom
cvolkcvolk/feature/variation-build-context
Closed

cvolkcvolk wants to merge 25 commits into
mainfrom
cvolkcvolk/feature/variation-build-context

Conversation

@cvolkcvolk

@cvolkcvolk cvolkcvolk commented Oct 12, 2026 •

Copy link
Copy Markdown
Collaborator

Pass every enabled variation a VariationBuildContext with the build's environment count, seed and variation key, and add make_generator() for seeded draws that leave the global random state alone. Let VariationRecorder keep a build-time value per environment, and let a variation reach the asset it was added to through host.

Why this change?

Asset selection is a build-time variation that seeds its choice from the build seed and installs the chosen asset on the object it was added to. Today a build-time variation cannot do that. It draws from the global torch random state and cannot see the seed. The base class also gives a variation no way to reach the asset it was added to. The light variations get their light through their constructor. Asset selection does not: in the API proposed in #1425 it is built from its candidates and then added to an object, as in pickup.add_variation(AssetSelectionVariation(...)). So add_variation is the only place it can learn which object it belongs to.

A build-time variation also cannot give each environment its own value. It cannot see num_envs, the recorder keeps one shared build-time value, and on main a build-time draw with env_ids fails during the build because no env is bound to the recorder yet.

This PR adds these general pieces and no new variation. AssetSelectionVariation, in a draft PR stacked on this one, uses set_host() and host, VariationBuildContext.seed, make_generator() and configure_at_build_time(context); it selects one asset for all environments of a build, so it does not use the per-environment records.

What changes?

  • VariationBuildContext(num_envs, seed, variation_key) is a frozen dataclass with three typed fields: the build's environment count, ArenaEnvBuilderCfg.seed and the variation's key. ArenaEnvBuilder builds one for each enabled variation, build-time and run-time, and passes it to configure_at_build_time(context) as a new required argument. _prepare_at_build_time and _realize_at_build_time keep their signatures and read it through the build_context property, which fails an assertion if read before the first configure_at_build_time call.
  • VariationBuildContext.make_generator() returns a new CPU torch.Generator seeded from the build seed and the variation key. The same seed repeats the draws, each variation gets its own stream, and the global torch random state is neither read nor advanced. Each call starts the stream again, so a variation makes all its draws in a build from one generator. The experiment runner already adds the rebuild index to the seed, so each rebuild draws new values. Only ChoiceSampler.sample() takes the generator, through a new keyword-only generator argument. UniformSampler and BernoulliSampler still draw from the global torch random state.
  • get_variation_key(asset_name, variation_name) builds the "<asset>.<variation>" key in the builder, the recorder and replay.
  • VariationRecorder keeps the per-environment draw of a build-time variation (a draw with env_ids by a BuildTimeVariationBase) and reports each environment's value for every episode of that environment. It needs no bind_env() for that draw. release_episode from Unify placement sampling and replay #1427 still drops only run-time samples, so these values stay. Shared draws and run-time draws are recorded as before.
  • VariationRecord.record_buildtime_sample is renamed to record_shared_sample, because it also records run-time draws made without env_ids. A later shared draw replaces the earlier one. The new record_per_env_build_time_sample records a build-time variation's per-environment draw. A build makes one such draw for all its environments: a second per-environment draw, a mix of shared and per-environment draws, or a draw whose rows do not match its env_ids fails an assertion and records nothing.
  • Asset.add_variation() now calls the new VariationBase.set_host(asset) before it stores the variation, so variation.host returns the asset the variation was added to. Subclasses can override set_host to check that asset. Adding a variation to a second asset fails an assertion that names both assets. So does reading host before the variation is added, or after its asset was garbage-collected. The variation holds its host through a weak reference: add_variation gives every variation on every asset this back-reference, and Isaac Lab's configclass validation follows object attributes without cycle detection. With a strong reference, test_embodiment_provides_robot_collision_mesh in test_embodiment_collision_mesh.py fails: it validates an EventTermCfg whose params hold a DROID embodiment, and validation goes from the embodiment to its camera variations and back through their host references until it raises RecursionError.
  • Recorded variation replay still does not support per-environment build-time values. As on main, a recording whose build-time value differs between environments fails to load ("changes across episodes"), and a per-environment draw against a replayed build-time value fails ("received per-env ids"). This PR does not add replay of per-environment values.

Suggested reading order: variation_base.py, variation_recorder.py, arena_env_builder.py, then the tests.

Usage example

Before:

def _realize_at_build_time(self) -> None:
    # One value shared by all environments, drawn from the global torch random state.
    chosen = self.sampler.sample(1, choices=candidate_names)
    # Apply chosen[0] to the asset passed to __init__ (self._light in HDRImageVariation).

After:

def _realize_at_build_time(self) -> None:
    context = self.build_context
    env_ids = torch.arange(context.num_envs)
    chosen = self.sampler.sample(
        context.num_envs, choices=candidate_names, env_ids=env_ids, generator=context.make_generator()
    )
    # Give environment i the value chosen[i] on self.host, the asset this variation was added to.

Because this variation passes env_ids, VariationRecorder keeps chosen[i] for every episode of environment i. The same seed, variation key, environment count and candidates give the same chosen. No variation in this PR applies per-environment values to the scene.

Validation

  • pre-commit run --all-files passes on the current head, with main merged in.
  • With main merged in, the five variation test files (test_variation_base.py, test_variation_recorder.py, test_build_time_variations.py, test_choice_samplers.py and test_recorded_variation_samples.py) and the two builder tests that main changed or added next to this PR's builder change (test_arena_env_graph_conversion.py, and test_placement_after_build_time_variations.py from Run placement after build-time variations #1452) pass in the Arena container: 78 passed.
  • Before main was merged in, the 30 test files that touch variations, the recorder or the builder's variation path passed in the Arena container, run in the three phases' marker groups: camera tests (8 passed), subprocess tests (15 passed, including the experiment runner with recorded variation replay) and plain tests, one file per process. Two results there come from the test setup, not from this change:
    • test_legacy_graph_builder_keeps_namespace_inside_graph_compatibility fails when its file runs alone, on main as well, because nothing has started Isaac Sim yet (No module named 'omni.usd'). It passes in the full phase run.
    • Two files hit the known intermittent Timeout (0:02:00)! in simulation_app.close() after all their tests passed.
  • With a strong host reference in place of the weak one, test_embodiment_provides_robot_collision_mesh and test_variation_with_a_host_does_not_create_a_config_validation_cycle in test_variation_base.py fail with RecursionError.
  • The full test suite (all three phases) last ran on 8d8036a, before the eight commits that followed it and the merge with main. Every failing test either passed when rerun on its own or also fails on main (4be2c00) in the same container.
    • Failures from the machine: OmniHub failed to start in the /tmp that all Arena containers on that machine share, so robot and object USDs loaded without their prims.
    • Failures that also occur on main: four tests need the optional tetrahedralization dependency (pytetwild), the multi-env mimic test hits its 900 s timeout, and the last five bring-ups in test_kitchen_bench_yaml_env.py fail with "Failed to get rigid body transforms from backend".
  • New tests: test_variation_base.py covers the build context and key, the generator and host; test_choice_samplers.py the generator argument of ChoiceSampler; test_variation_recorder.py the per-environment records, their rejection rules and replay rejection with a real per-environment recording (test_replay_rejects_a_recording_with_per_env_build_time_values); test_recorded_variation_samples.py replay rejection of per-environment draws; and test_builder_passes_build_context_and_records_per_env_samples in test_build_time_variations.py the builder for both variation lifecycles.

Add VariationBuildContext with the build's num_envs, seed and the
variation key, and get_variation_key for the "<asset>.<variation>"
key. configure_at_build_time() takes an optional context and the
hooks read it through build_context, so the hook signatures stay the
same and existing callers without a context keep working.

Signed-off-by: Clemens Volk <cvolk@nvidia.com>
VariationBuildContext.make_generator() seeds a CPU generator from
the build seed and the variation key, so draws repeat for the same
seed, differ between variations and leave the global torch random
state alone. ChoiceSampler.sample() accepts the generator.

Nothing calls them yet. AssetSelectionVariation uses both in a
later PR.

Signed-off-by: Clemens Volk <cvolk@nvidia.com>
Asset.add_variation() now calls VariationBase.attach(), and the
variation exposes the asset as host. A variation can be attached
only once. The reference is weak because configclass validation
follows attributes without cycle detection.

No existing variation reads host or overrides attach.

Signed-off-by: Clemens Volk <cvolk@nvidia.com>
A build-time variation can now draw one value per environment by
passing env_ids. VariationRecorder stores those values per env,
without needing bind_env(), and reports each one for every episode
of its env. _make_listener takes a build_time flag that attach()
sets for BuildTimeVariationBase; the placement sampler and run-time
variations are routed as before. release_episode drops only
run-time samples, so each env keeps its build-time value.

VariationRecord rejects mixed shared and per-env draws and a second
value for an env that already has one, and validates a draw before
writing any of it.

Signed-off-by: Clemens Volk <cvolk@nvidia.com>
Recorded variation replay already rejects per-env draws against a
replayed build-time value ("received per-env ids") and build-time
values that differ between environments ("changes across episodes").
Pin the first, which no test covered, and feed a recording made by a
per-env build-time variation into the second.

Signed-off-by: Clemens Volk <cvolk@nvidia.com>
Recorded variation replay now builds its "<asset>.<variation>" key
with the same helper as the builder and the recorder.

Signed-off-by: Clemens Volk <cvolk@nvidia.com>
_apply_build_time_variations() gives every enabled variation, build
time and run time, a VariationBuildContext with the builder's
num_envs and seed and the variation's key. The seed already changes
per rebuild and per rank. Existing variations do not read the
context, so their behaviour is unchanged.

Signed-off-by: Clemens Volk <cvolk@nvidia.com>
@cvolkcvolk
cvolkcvolk force-pushed the cvolkcvolk/feature/variation-build-context branch from 60034bf to 978c347 Compare October 13, 2026 08:26
Drop the caller name from the configure_at_build_time context argument,
name the subject of the in-place mutation sentence in
_apply_build_time_variations, and replace a test comment that referred
to the main branch with the reason behind the behaviour.

Signed-off-by: Clemens Volk <cvolk@nvidia.com>
VariationRecord used "build-time" for two things: the store for draws
without env ids, which run-time variations fill too, and the per-env
store of build-time variations. Split record_buildtime_sample into
record_shared_sample and record_per_env_build_time_samples, rename the
shared store to _shared_sample and the listener flag to
is_build_time_variation, so on_sample shows its three routes.

The shared-draw assertion now points to env_ids, and the bind_env()
assertion says it applies to per-env run-time draws.

Signed-off-by: Clemens Volk <cvolk@nvidia.com>
The setter now pairs with the host property and follows
set_replay_sampler. VariationRecorder.attach(variations) keeps its
meaning, so "attach" no longer reads in two directions.

The weak-reference comment names the case it guards against: an asset
stored in a configclass field, which validation walks without cycle
detection. The light variations hold their light strongly and never
end up in such a field. The second-host assertion names the asset the
variation was being added to, and the add_variation and host
docstrings state their preconditions and when host is None.

Signed-off-by: Clemens Volk <cvolk@nvidia.com>
BuildTimeVariationBase now gives the whole recipe for a per-env draw:
num_envs rows, env_ids, the build-seeded generator and the replay
limit, and names host as a way to reach the asset. make_generator says
that each call restarts the stream. VariationBuildContext.seed names
the builder seed and says what None does.

build_context says it stays set until the next configure_at_build_time
call, and its assertion tells the caller what to pass instead of naming
ArenaEnvBuilder. get_variation_key states the key format instead of
listing where the key is used.

Signed-off-by: Clemens Volk <cvolk@nvidia.com>
Name the test variations after their lifecycle and draw scope
(_SharedBuildTimeVariation, _PerEnvBuildTimeVariation,
_RunTimeVariation, ...), so a test shows which kind of draw it makes.

Split the build-time record tests so each accepted or rejected draw has
its own test name, and run the per-env rejections from one starting
record so each also checks that nothing was written. Rename the
run-time routing test after its behaviour.

The replay rejection test now uses the seeded choice variation, so the
context seed decides the two per-env values, and its docstring says why
the loader rejects them.

Signed-off-by: Clemens Volk <cvolk@nvidia.com>
Give the context's immutability and its generator seed derivation their
own tests, and parametrize the invalid-field cases on the field being
broken, so each case shows which check fires. Name the choice sampler
tests after ChoiceSampler and say why the global random state is moved.
Update the test module docstring to cover the build context and host.

The make_generator comment now says why it uses a digest: Python's
hash() of a str changes between processes.

Signed-off-by: Clemens Volk <cvolk@nvidia.com>
ContextRecordingVariation read like part of the variation recorder.
Rename the helpers to PerEnvContextReadingVariation and
RunTimeContextReadingVariation, and give the per-env draws their own
value, TEST_PER_ENV_VALUE, so it is clear the per-env record holds that
variation's draws rather than the sphere radius.

Name the env outside the build used for the shared-record check, and
say which variation draws the shared value.

Signed-off-by: Clemens Volk <cvolk@nvidia.com>
Adding one variation instance to a second asset now reports both
assets. Also drop "only" from the VariationBase docstring, which now
lists host and build_context, write self.build_context consistently in
the BuildTimeVariationBase recipe, note that changing the generator
seed derivation changes every seeded draw, and drop the Sphinx
reference from the test module docstring.

Signed-off-by: Clemens Volk <cvolk@nvidia.com>
Rename the shared-draw recorder test to say "shared", give the
VariationRecord rule tests one test_record_ prefix, and spell the
builder test's run-time local like its class. The placement test now
uses a bare sampler instead of borrowing a shared build-time variation.

The shared-after-per-env rejection test again checks that the rejected
draw does not block a later per-env draw, which the combined test
covered before it was split.

Signed-off-by: Clemens Volk <cvolk@nvidia.com>
@cvolkcvolk cvolkcvolk changed the title Give build-time variations a build context and per-environment records Add a variation build context, a host asset and per-environment build-time records Oct 14, 2026
The None default only let a few tests skip building a context; the
builder always passes one. Requiring it leaves one rule: after
configure_at_build_time, build_context is set. The reset comment, the
"without one" clause and the context-free test variation go away, and
the build_context assertion now covers only a read before the first
call.

Signed-off-by: Clemens Volk <cvolk@nvidia.com>
Drop __post_init__: the builder, its only producer, passes values that
are already checked where they enter Arena (ArenaEnvBuilderCfg asserts
num_envs > 0; argparse and the typed YAML schema make the seed an int).

Make seed an int. Production builds always pass ArenaEnvBuilderCfg.seed,
and in-order asset selection computes candidate positions from it, so a
None seed would only need an assert there. make_generator loses its
system-entropy branch.

Name the generator tests after the rules they check: the stream depends
only on seed and variation_key, and the global random state is left
alone.

Signed-off-by: Clemens Volk <cvolk@nvidia.com>
host now returns the Asset or fails with a message saying what to do,
instead of returning None before the variation is added or after its
asset is garbage-collected. Variations that read their asset through
host, as asset selection will, need no None check.

set_host says it runs before add_variation stores the variation, which
an override that rejects a second variation of its kind relies on.
The weak-reference comment cites a config that recurses today with a
strong reference: an EventTermCfg whose params hold a DROID embodiment
(test_embodiment_collision_mesh fails with RecursionError). The
add_variation docstring names environment code as a caller too.

Signed-off-by: Clemens Volk <cvolk@nvidia.com>
It takes one multi-row sample with env_ids, like its siblings
record_runtime_sample and record_shared_sample, so it reads as their
per-env build-time counterpart.

Signed-off-by: Clemens Volk <cvolk@nvidia.com>
A build-time variation makes one per-env draw for all its environments,
as the BuildTimeVariationBase recipe asks, so VariationRecord now
rejects any second per-env draw instead of merging a later draw for
other environments. The write becomes one assignment after the checks,
so the overlap check and the validate-first ordering go away.

Drop _has_shared_sample: every reader already treats a None shared
sample as "nothing drawn". The sample_for_episode docstring says that
VariationRecorder, not the record, keeps per-episode and per-env
build-time draws apart. Direct VariationRecord tests no longer pass a
cfg that nothing reads.

Signed-off-by: Clemens Volk <cvolk@nvidia.com>
The _realize_at_build_time docstrings said "bound asset config(s)" and
"target configuration" next to the new host. They now say the hooks
apply values to asset configs, and the BuildTimeVariationBase recipe
asks for one per-env draw and says replay supports only draws without
env_ids, instead of the undefined "shared form".

Signed-off-by: Clemens Volk <cvolk@nvidia.com>
The per-env build-time test now checks only that each env keeps its
value for every episode, also after release_episode, and names the env
outside the build. The choice test is renamed after what it adds: each
env's episode row holds the chosen item itself. The CPU and every-episode
checks it repeated are covered elsewhere.

Signed-off-by: Clemens Volk <cvolk@nvidia.com>
set_host built its message through the host property, which now
asserts once the first host is garbage-collected, so that case lost the
"cannot also be added to" message. It reads the weak reference
directly again, and a test covers the case. The garbage-collected host
message now has the same form as its neighbours.

Signed-off-by: Clemens Volk <cvolk@nvidia.com>
Signed-off-by: Clemens Volk <cvolk@nvidia.com>
@cvolkcvolk

Copy link
Copy Markdown
Collaborator Author

Closing this in favor of two smaller PRs:

  • #1467 seeds each build-time variation from --seed and its variation key. configure_at_build_time(seed, variation_key) seeds the variation's build_time_generator, and ChoiceSampler.sample and ContinuousSampler.sample take a generator. The HDR image and light variations draw from it, so their values become reproducible too.
  • #1458 is now stacked on Seed variations with --seed #1467. AssetSelectionVariation learns its object through a new no-op hook, VariationBase.attach_to_asset(asset), which Asset.add_variation() calls, and keeps the object itself.

The rest of this PR is not carried over: VariationBuildContext, get_variation_key, set_host and host with the weak reference, the per-environment build-time records and the record_shared_sample rename. Asset selection picks one asset per build, so no variation needs per-environment build-time values, and only AssetSelectionVariation needs to know its object.

@cvolkcvolk cvolkcvolk closed this Oct 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant