trinity.trainer.verl.monkey_patch module#

trinity.trainer.verl.monkey_patch.load_valuehead_model(local_path, torch_dtype, model_config, trust_remote_code, use_meta=False)[source]#
trinity.trainer.verl.monkey_patch.left_right_2_no_padding(data: TensorDict) TensorDict[source]#

Convert TensorDict from left-right padding to no-padding format.

Parameters:

data – TensorDict with ā€œinput_idsā€, ā€œattention_maskā€, ā€œresponse_maskā€, ā€œposition_idsā€

Returns:

TensorDict with - Tensor includes NestedTensors like ā€œinput_idsā€, ā€œloss_maskā€, ā€œposition_idsā€ - NonTensorData includes ā€œmax_seq_lenā€, ā€œmax_response_lenā€, ā€œindicesā€

Return type:

data

Note: 1. the return input_ids/position_ids/loss_mask are nested tensor. 2. we will remove ā€œattention_maskā€, ā€œresponseā€ in the return data, but ā€œresponse_maskā€ is kept.

trinity.trainer.verl.monkey_patch.save_checkpoint(self, local_path: str, hdfs_path: str | None = None, global_step: int = 0, max_ckpt_to_keep: int | None = None, **kwargs) None[source]#

Save FSDP checkpoint, handling parameter offload as needed.

trinity.trainer.verl.monkey_patch.get_seq_idx(cu_seqlens: Tensor, total_nnz: int) Tensor[source]#

Build seq_idx from cu_seqlens, mapping each packed position to its original sequence id.

Parameters:
  • cu_seqlens – Shape (batch + 1,). Cumulative sequence lengths.

  • total_nnz – Total number of packed tokens, i.e. cu_seqlens[-1].

Returns:

Shape (total_nnz,), where each position is the original sequence id (0-indexed). For example, cu_seqlens=[0,3,7,10] -> [0,0,0,1,1,1,1,2,2,2].

trinity.trainer.verl.monkey_patch.prepare_model_inputs(self, micro_batch: TensorDict)[source]#

Rewritten FSDPEngineWithLMHead.prepare_model_inputs that injects seq_idx and cu_seqlens into model_inputs for packed-sequence models (e.g. Qwen3.5 GateDeltaNet).

This is a full rewrite (not a wrapper) so that the Ulysses SP pad_size adjustment on seq_idx / cu_seqlens is handled inline, right after ulysses_pad_and_slice_inputs returns pad_size.

trinity.trainer.verl.monkey_patch.patch_verl_engine()[source]#