embodichain.learning.rl.algo#
Algorithm registry and construction helpers (BaseAlgorithm, PPO, GRPO, compute_gae, build_algo).
Overview#
Algorithm registry and algorithm-construction helpers for RL training. The
on-policy algorithms PPO and GRPO and the differentiable
algorithm APG all derive from BaseAlgorithm. Each algorithm
declares a RolloutKind that selects the compatible trainer.
build_algo() looks up a registered algorithm by name and wires it to a
policy, while compute_gae() provides generalized advantage estimation.
Classes
Base class for RL algorithms.
Rollout semantics required by an algorithm.
Analytic policy-gradient config.
Optimize policy parameters through differentiable rollout rewards.
Configuration for the PPO algorithm.
PPO algorithm consuming TensorDict rollouts.
Configuration for GRPO.
Group Relative Policy Optimization on top of TensorDict rollouts.
Functions
build_algo(name, cfg_kwargs, policy, device, *)
compute_gae(rollout, gamma, gae_lambda)Compute GAE over a rollout stored as [num_envs, time + 1].
segmented_discounted_return(rollout, gamma)Compute one discounted return per environment within a rollout segment.
Algorithm registry and construction helpers (BaseAlgorithm, PPO, GRPO, compute_gae, build_algo).
Classes:
Optimize policy parameters through differentiable rollout rewards. |
|
Analytic policy-gradient config. |
|
Base class for RL algorithms. |
|
Group Relative Policy Optimization on top of TensorDict rollouts. |
|
Configuration for GRPO. |
|
PPO algorithm consuming TensorDict rollouts. |
|
Configuration for the PPO algorithm. |
|
Rollout semantics required by an algorithm. |
Functions:
|
|
|
Compute GAE over a rollout stored as [num_envs, time + 1]. |
|
Compute one discounted return per environment within a rollout segment. |
- class embodichain.learning.rl.algo.APG[source]#
Bases:
BaseAlgorithm[DifferentiableRollout]Optimize policy parameters through differentiable rollout rewards.
Methods:
__init__(cfg, policy)accumulate_segment(rollout)Accumulate gradients from one TBPTT segment without stepping the optimizer.
Clip gradients and apply one optimizer step.
update(rollout)Apply one pathwise-gradient update from a rollout segment.
Attributes:
- accumulate_segment(rollout)[source]#
Accumulate gradients from one TBPTT segment without stepping the optimizer.
- Return type:
None
-
device:
device#
-
lr_scheduler:
LRScheduler|None#
-
optimizer:
Optimizer#
- rollout_kind = 'differentiable'#
- class embodichain.learning.rl.algo.APGCfg[source]#
Bases:
AlgorithmCfgAnalytic policy-gradient config.
gammaapplies within each TBPTT segment and restarts after done.Methods:
__init__([device, optimizer, lr_scheduler, ...])copy(**kwargs)Return a new object replacing specified fields with new values.
replace(**kwargs)Return a new object replacing specified fields with new values.
to_dict()Convert an object into dictionary recursively.
validate([prefix])Check the validity of configclass object.
Attributes:
- __init__(device=<factory>, optimizer=<factory>, lr_scheduler=<factory>, batch_size=<factory>, gamma=<factory>, gae_lambda=<factory>, max_grad_norm=<factory>, ent_coef=<factory>, skip_nonfinite_updates=<factory>)#
- batch_size: int#
- copy(**kwargs)#
Return a new object replacing specified fields with new values.
This is especially useful for frozen classes. Example usage:
@configclass(frozen=True) class C: x: int y: int c = C(1, 2) c1 = c.replace(x=3) assert c1.x == 3 and c1.y == 2
- Parameters:
obj (
object) – The object to replace.**kwargs – The fields to replace and their new values.
- Return type:
object- Returns:
The new object.
- device: str#
- ent_coef: float#
- gae_lambda: float#
- gamma: float#
- lr_scheduler: LRSchedulerCfg#
- max_grad_norm: float#
- optimizer: OptimizerCfg#
- replace(**kwargs)#
Return a new object replacing specified fields with new values.
This is especially useful for frozen classes. Example usage:
@configclass(frozen=True) class C: x: int y: int c = C(1, 2) c1 = c.replace(x=3) assert c1.x == 3 and c1.y == 2
- Parameters:
obj (
object) – The object to replace.**kwargs – The fields to replace and their new values.
- Return type:
object- Returns:
The new object.
- skip_nonfinite_updates: bool#
- to_dict()#
Convert an object into dictionary recursively.
Note
Ignores all names starting with “__” (i.e. built-in methods).
- Parameters:
obj (
object) – An instance of a class to convert.- Raises:
ValueError – When input argument is not an object.
- Return type:
dict[str,Any]- Returns:
Converted dictionary mapping.
- validate(prefix='')#
Check the validity of configclass object.
This function checks if the object is a valid configclass object. A valid configclass object contains no MISSING entries.
- Parameters:
obj (
object) – The object to check.prefix (
str) – The prefix to add to the missing fields. Defaults to ‘’.
- Return type:
list[str]- Returns:
A list of missing fields.
- Raises:
TypeError – When the object is not a valid configuration object.
- class embodichain.learning.rl.algo.BaseAlgorithm[source]#
Bases:
ABC,Generic[RolloutT]Base class for RL algorithms.
Methods:
bind_schedule(*, total_updates)Bind horizon-dependent LR schedules from the training budget.
update(rollout)Update policy using collected data and return training losses.
Attributes:
- bind_schedule(*, total_updates)[source]#
Bind horizon-dependent LR schedules from the training budget.
- Return type:
None
-
device:
device#
-
lr_scheduler:
LRScheduler|None#
-
optimizer:
Optimizer#
- rollout_kind = 'standard'#
- class embodichain.learning.rl.algo.GRPO[source]#
Bases:
BaseAlgorithm[TensorDict]Group Relative Policy Optimization on top of TensorDict rollouts.
Methods:
- class embodichain.learning.rl.algo.GRPOCfg[source]#
Bases:
AlgorithmCfgConfiguration for GRPO.
Methods:
__init__([device, optimizer, lr_scheduler, ...])copy(**kwargs)Return a new object replacing specified fields with new values.
replace(**kwargs)Return a new object replacing specified fields with new values.
to_dict()Convert an object into dictionary recursively.
validate([prefix])Check the validity of configclass object.
Attributes:
- __init__(device=<factory>, optimizer=<factory>, lr_scheduler=<factory>, batch_size=<factory>, gamma=<factory>, gae_lambda=<factory>, max_grad_norm=<factory>, n_epochs=<factory>, clip_coef=<factory>, ent_coef=<factory>, kl_coef=<factory>, group_size=<factory>, eps=<factory>, reset_every_rollout=<factory>, truncate_at_first_done=<factory>)#
- batch_size: int#
- clip_coef: float#
- copy(**kwargs)#
Return a new object replacing specified fields with new values.
This is especially useful for frozen classes. Example usage:
@configclass(frozen=True) class C: x: int y: int c = C(1, 2) c1 = c.replace(x=3) assert c1.x == 3 and c1.y == 2
- Parameters:
obj (
object) – The object to replace.**kwargs – The fields to replace and their new values.
- Return type:
object- Returns:
The new object.
- device: str#
- ent_coef: float#
- eps: float#
- gae_lambda: float#
- gamma: float#
- group_size: int#
- kl_coef: float#
- lr_scheduler: LRSchedulerCfg#
- max_grad_norm: float#
- n_epochs: int#
- optimizer: OptimizerCfg#
- replace(**kwargs)#
Return a new object replacing specified fields with new values.
This is especially useful for frozen classes. Example usage:
@configclass(frozen=True) class C: x: int y: int c = C(1, 2) c1 = c.replace(x=3) assert c1.x == 3 and c1.y == 2
- Parameters:
obj (
object) – The object to replace.**kwargs – The fields to replace and their new values.
- Return type:
object- Returns:
The new object.
- reset_every_rollout: bool#
- to_dict()#
Convert an object into dictionary recursively.
Note
Ignores all names starting with “__” (i.e. built-in methods).
- Parameters:
obj (
object) – An instance of a class to convert.- Raises:
ValueError – When input argument is not an object.
- Return type:
dict[str,Any]- Returns:
Converted dictionary mapping.
- truncate_at_first_done: bool#
- validate(prefix='')#
Check the validity of configclass object.
This function checks if the object is a valid configclass object. A valid configclass object contains no MISSING entries.
- Parameters:
obj (
object) – The object to check.prefix (
str) – The prefix to add to the missing fields. Defaults to ‘’.
- Return type:
list[str]- Returns:
A list of missing fields.
- Raises:
TypeError – When the object is not a valid configuration object.
- class embodichain.learning.rl.algo.PPO[source]#
Bases:
BaseAlgorithm[TensorDict]PPO algorithm consuming TensorDict rollouts.
Methods:
- class embodichain.learning.rl.algo.PPOCfg[source]#
Bases:
AlgorithmCfgConfiguration for the PPO algorithm.
Methods:
__init__([device, optimizer, lr_scheduler, ...])copy(**kwargs)Return a new object replacing specified fields with new values.
replace(**kwargs)Return a new object replacing specified fields with new values.
to_dict()Convert an object into dictionary recursively.
validate([prefix])Check the validity of configclass object.
Attributes:
- __init__(device=<factory>, optimizer=<factory>, lr_scheduler=<factory>, batch_size=<factory>, gamma=<factory>, gae_lambda=<factory>, max_grad_norm=<factory>, n_epochs=<factory>, clip_coef=<factory>, ent_coef=<factory>, vf_coef=<factory>)#
- batch_size: int#
- clip_coef: float#
- copy(**kwargs)#
Return a new object replacing specified fields with new values.
This is especially useful for frozen classes. Example usage:
@configclass(frozen=True) class C: x: int y: int c = C(1, 2) c1 = c.replace(x=3) assert c1.x == 3 and c1.y == 2
- Parameters:
obj (
object) – The object to replace.**kwargs – The fields to replace and their new values.
- Return type:
object- Returns:
The new object.
- device: str#
- ent_coef: float#
- gae_lambda: float#
- gamma: float#
- lr_scheduler: LRSchedulerCfg#
- max_grad_norm: float#
- n_epochs: int#
- optimizer: OptimizerCfg#
- replace(**kwargs)#
Return a new object replacing specified fields with new values.
This is especially useful for frozen classes. Example usage:
@configclass(frozen=True) class C: x: int y: int c = C(1, 2) c1 = c.replace(x=3) assert c1.x == 3 and c1.y == 2
- Parameters:
obj (
object) – The object to replace.**kwargs – The fields to replace and their new values.
- Return type:
object- Returns:
The new object.
- to_dict()#
Convert an object into dictionary recursively.
Note
Ignores all names starting with “__” (i.e. built-in methods).
- Parameters:
obj (
object) – An instance of a class to convert.- Raises:
ValueError – When input argument is not an object.
- Return type:
dict[str,Any]- Returns:
Converted dictionary mapping.
- validate(prefix='')#
Check the validity of configclass object.
This function checks if the object is a valid configclass object. A valid configclass object contains no MISSING entries.
- Parameters:
obj (
object) – The object to check.prefix (
str) – The prefix to add to the missing fields. Defaults to ‘’.
- Return type:
list[str]- Returns:
A list of missing fields.
- Raises:
TypeError – When the object is not a valid configuration object.
- vf_coef: float#
- class embodichain.learning.rl.algo.RolloutKind[source]#
Bases:
str,EnumRollout semantics required by an algorithm.
Attributes:
Methods:
__new__(value)- DIFFERENTIABLE = 'differentiable'#
- STANDARD = 'standard'#
- __new__(value)#
- embodichain.learning.rl.algo.build_algo(name, cfg_kwargs, policy, device, *, distributed=False)[source]#
- embodichain.learning.rl.algo.compute_gae(rollout, gamma, gae_lambda)[source]#
Compute GAE over a rollout stored as [num_envs, time + 1].
- Parameters:
rollout (
TensorDict) – Rollout TensorDict where value[:, -1] stores the bootstrap value for the final observation and transition-only fields reserve their last slot as padding.gamma (
float) – Discount factor.gae_lambda (
float) – GAE lambda coefficient.
- Return type:
tuple[Tensor,Tensor]- Returns:
Tuple of (advantages, returns), both shaped [num_envs, time].