protomotions.agents.base_agent.model module#
Base model interface for agent neural networks.
This module defines the abstract base class that all agent models must implement. It provides a TensorDictModule interface for clean, compilable models.
- Key Classes:
BaseModel: Abstract base class for all agent models (TensorDictModule)
- class protomotions.agents.base_agent.model.RolloutStateSpec(shape=(), init='zeros', dtype=<Mock object>)[source]#
Bases:
objectDeclared model-owned per-env rollout context.
A model declares the tensor shape, dtype, and initial-value distribution for per-env context that must be carried during rollout and replayed during optimization. The framework owns when context is initialized and which rows are reset after episode boundaries.
- __init__(shape=(), init='zeros', dtype=<Mock object>)#
- class protomotions.agents.base_agent.model.ProtoMotionsTensorDictModule(*args, **kwargs)[source]#
Bases:
TensorDictModuleBaseCommon contract for ProtoMotions-owned TensorDict modules.
Use this for modules that read and write named TensorDict keys and may own rollout state that must be saved in the experience buffer. Examples include policy/critic modules, normalizing observation processors, container modules, and models with rollout-local latent state.
- forward(tensordict, log_internals=False)[source]#
Forward pass for ProtoMotions TensorDict modules.
log_internalsis part of the common contract. Modules that produce diagnostics can honor it; stateless modules can ignore it.
- forward_deployable(tensordict)[source]#
The deployable forward: by default the same graph the policy runs.
A policy family whose exported pass differs from its training forward (an actor that samples exploration noise) overrides this; everything else inherits it, so the exporter, evaluator and teacher call one name without an isinstance check on the policy class.
- reset_rollout_context(
- env_ids=None,
- num_envs=None,
- device=None,
Initialize or reseed model-owned context carried across rollout steps.
num_envsanddeviceare supplied once during setup, after the simulator is built. Later calls pass onlyenv_idsto reseed rows for environments that just ended. This method intentionally does not infer shape/device changes mid-run; rebuilding the simulator should call setup again and reinitialize explicitly.
- save_rollout_context()[source]#
Copy every rollout-context buffer in this module tree.
A caller that runs its own episodes on the training envs, such as evaluation, saves this first and passes it to
load_rollout_contextafterwards, so training resumes with the context it had.
- read_rollout_state(tensordict)[source]#
Inject carried rollout context into
tensordictwhen absent.Rollout forwards read from model-owned buffers; optimization forwards read replayed values already present in the batch. This helper makes the distinction explicit and avoids each model hand-writing buffer fallbacks.
- experience_buffer_keys()[source]#
Keys produced during rollout that the experience buffer must store.
- build_training_targets(rollout)[source]#
Derive model-owned targets from [time, env, …] rollout tensors.
BaseAgent calls this once per rollout under no_grad, after agent preprocessing and before flattening/shuffling destroys time adjacency. Inputs are borrowed experience-buffer views: do not mutate them. Return additional [time, env, …] targets/masks for compute_model_loss; the agent owns batching, not their meaning. The default gathers child targets; overrides should call super() to preserve those results.
- class protomotions.agents.base_agent.model.BaseModel(*args, **kwargs)[source]#
Bases:
ProtoMotionsTensorDictModuleBase class for all agent models.
All models are TensorDictModules with a single forward method that processes observations and returns all model outputs in a TensorDict.
- Parameters:
config (BaseModelConfig) – Model configuration with architecture parameters.
- Variables:
config – Stored configuration for the model.
in_keys – Input keys for TensorDict (set by subclasses).
out_keys – Output keys for TensorDict (default: [“action”]).
- materialize_from_state_dict(state_dict)[source]#
Create lazily-owned modules needed for strict state-dict loading.