﻿# Hydra partial factories: fresh outer objects can still share a captured nested instance

Test Hydra partial-instantiation factories for shared nested state before using one factory to construct supposedly independent evaluators or model wrappers.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/fg2-hydra-partial-factory-shared-nested-state/
Language: en

## Inspect independence below the outer object

A researcher constructs several evaluators from the same Hydra partial factory. Each evaluator is a new object, but they unexpectedly share a mutable statistics collector. The [instantiation guide](https://hydra.cc/docs/advanced/instantiate_objects/overview/) documents reuse of captured keyword arguments in partial factories, including a nested instance shared by distinct outer results. The correct independence question therefore concerns the object graph, not merely whether the top-level evaluator identities differ after construction.

## Distinguish deferred construction from deferred everything

Partial instantiation postpones the selected call while retaining its prepared arguments. It does not imply that every nested dependency will be constructed afresh on every later factory invocation. Identify what is already instantiated when the factory is created and what is supplied when it is called. This boundary matters for mutable caches, counters and random generators. A stateful dependency can carry information from one evaluation into another even when both wrappers were created with otherwise identical configuration.

## Understand captured argument identity

The [Python partial reference](https://docs.python.org/3/library/functools.html#functools.partial) explains a callable with bound arguments. Binding an object is different from specifying a recipe to create a fresh object. The [copy documentation](https://docs.python.org/3/library/copy.html) likewise distinguishes object bindings and copying. Do not assume a new containing object recursively duplicates its dependencies. Decide whether sharing is intentional: reusing an immutable vocabulary can be useful, while sharing a mutable accumulator changes the meaning of supposedly isolated evaluation results.

## Choose an explicit ownership policy

For dependencies that must reset per evaluation, construct them inside the per-evaluation boundary or pass a new instance at the intended call site. If sharing is required, make the shared lifetime visible and define reset behavior. Blindly deep-copying an arbitrary model stack is not a general substitute for design; devices, handles and external resources can have their own ownership constraints. The policy should describe which state is local to a trial and which resource is deliberately common.

## Test mutation across factory results

Construct separate outer objects, mutate a recognizable nested counter in one and inspect the other. Repeat through the chosen independent construction path. Related material explains [random-state replay](/journal/data-guide-data-rng-state-replay/) and [autograd graph state](/journal/autograd-chain-rule-neural-graphs/). This fixture establishes the sharing boundary without requiring a long model evaluation. Distinct object names and identical configuration text are useful descriptors, but only an explicit dependency lifetime explains whether the resulting evaluations are isolated.
