Support true on-policy (#12058)

This commit is contained in:
fzyzcjy
2025-10-25 10:23:42 +08:00
committed by GitHub
parent 649949807f
commit 20bd2271e2
16 changed files with 151 additions and 11 deletions
+13 -1
View File
@@ -11,6 +11,7 @@ import triton
import triton.language as tl
from sglang.srt.custom_op import CustomOp
from sglang.srt.server_args import get_global_server_args
from sglang.srt.utils import (
cpu_has_amx_support,
get_bool_env_var,
@@ -124,18 +125,29 @@ class RotaryEmbedding(CustomOp):
self.cos_sin_cache: torch.Tensor
self.register_buffer("cos_sin_cache", cache, persistent=False)
if get_global_server_args().rl_on_policy_target == "fsdp":
self._forward_method = self.forward_native
def _compute_inv_freq(self, base: Union[int, float]) -> torch.Tensor:
"""Compute the inverse frequency."""
# NOTE(woosuk): To exactly match the HF implementation, we need to
# use CPU to compute the cache and then move it to GPU. However, we
# create the cache on GPU for faster initialization. This may cause
# a slight numerical difference between the HF implementation and ours.
init_device = (
"cpu" if get_global_server_args().rl_on_policy_target == "fsdp" else None
)
inv_freq = 1.0 / (
base
** (
torch.arange(0, self.rotary_dim, 2, dtype=torch.float) / self.rotary_dim
torch.arange(
0, self.rotary_dim, 2, dtype=torch.float, device=init_device
)
/ self.rotary_dim
)
)
if get_global_server_args().rl_on_policy_target == "fsdp":
inv_freq = inv_freq.cuda()
return inv_freq
def _compute_cos_sin_cache(self) -> torch.Tensor: