Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b7b1f874b5 | ||
|
|
d334ea5294 | ||
|
|
57d2b84330 |
@@ -117,6 +117,9 @@ class DebugModelConfig(BaseConfig):
|
||||
|
||||
|
||||
class ModelConfig(BaseModelConfig):
|
||||
conversion_dir: Path | None = None
|
||||
"""Directory for the auto-converted weights (written to a `prime`/`hf` subdirectory). If not set, we write into the model snapshot directory."""
|
||||
|
||||
seq_len: int = 2048
|
||||
"""Sequence length the model is trained on."""
|
||||
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
[project]
|
||||
name = "prime-rl"
|
||||
version = "0.6.0"
|
||||
version = "0.7.0"
|
||||
description = "Async RL Training at Scale"
|
||||
readme = "README.md"
|
||||
requires-python = "~=3.12.0"
|
||||
|
||||
@@ -834,19 +834,21 @@ def load_dcp_from_hf(model: nn.Module, config: ModelConfig, parallel_dims: Paral
|
||||
# All ranks read just the key names (cheap) to determine the path independently.
|
||||
# Only master loads the full state dict when conversion is actually needed.
|
||||
if isinstance(model, PreTrainedModelPrimeRL):
|
||||
snapshot_keys = dict.fromkeys(load_state_dict_keys(snapshot_path))
|
||||
source_path = snapshot_path
|
||||
convert_dir = config.conversion_dir or source_path
|
||||
snapshot_keys = dict.fromkeys(load_state_dict_keys(source_path))
|
||||
model_keys = dict.fromkeys(model.state_dict().keys())
|
||||
|
||||
if model.is_hf_state_dict(snapshot_keys) and model.is_prime_state_dict(model_keys):
|
||||
logger.warning(
|
||||
"Found HF weight format in snapshot state dict and PrimeRL weight format in model state dict. Trying to auto-convert..."
|
||||
)
|
||||
snapshot_path = snapshot_path / "prime"
|
||||
snapshot_path = convert_dir / "prime"
|
||||
if not snapshot_path.exists() and get_world().is_master:
|
||||
logger.debug(
|
||||
f"Converting snapshot state dict to PrimeRL format and saving to {snapshot_path} on master rank. This is a one-time operation."
|
||||
)
|
||||
snapshot_state_dict = load_state_dict(snapshot_path.parent)
|
||||
snapshot_state_dict = load_state_dict(source_path)
|
||||
model.convert_to_prime(snapshot_state_dict)
|
||||
save_state_dict(snapshot_state_dict, snapshot_path)
|
||||
del snapshot_state_dict
|
||||
@@ -855,12 +857,12 @@ def load_dcp_from_hf(model: nn.Module, config: ModelConfig, parallel_dims: Paral
|
||||
logger.warning(
|
||||
"Found PrimeRL weight format in snapshot state dict and HF weight format in model state dict. Trying to auto-convert..."
|
||||
)
|
||||
snapshot_path = snapshot_path / "hf"
|
||||
snapshot_path = convert_dir / "hf"
|
||||
if not snapshot_path.exists() and get_world().is_master:
|
||||
logger.debug(
|
||||
f"Converting snapshot state dict to HF format and saving to {snapshot_path} on master rank. This is a one-time operation."
|
||||
)
|
||||
snapshot_state_dict = load_state_dict(snapshot_path.parent)
|
||||
snapshot_state_dict = load_state_dict(source_path)
|
||||
model.convert_to_hf(snapshot_state_dict)
|
||||
save_state_dict(snapshot_state_dict, snapshot_path)
|
||||
del snapshot_state_dict
|
||||
|
||||
@@ -238,8 +238,9 @@ class _SequenceChunkedLogProbEntropyFn(torch.autograd.Function):
|
||||
vocab = weight.shape[0]
|
||||
vocab_chunk_size = min(vocab, 8192)
|
||||
|
||||
grad_hidden = torch.zeros_like(hidden)
|
||||
grad_weight = torch.zeros_like(weight)
|
||||
needs_hidden, needs_weight = ctx.needs_input_grad[0], ctx.needs_input_grad[1]
|
||||
grad_hidden = torch.zeros_like(hidden) if needs_hidden else None
|
||||
grad_weight = torch.zeros_like(weight) if needs_weight else None
|
||||
|
||||
for start in range(0, n, chunk_size):
|
||||
end = min(start + chunk_size, n)
|
||||
@@ -263,8 +264,10 @@ class _SequenceChunkedLogProbEntropyFn(torch.autograd.Function):
|
||||
grad_logits[mask, idx] += grad_chunk[mask]
|
||||
grad_logits = grad_logits * inv_t_chunk
|
||||
|
||||
grad_hidden[start:end].add_(grad_logits.to(hidden.dtype) @ weight_chunk)
|
||||
grad_weight[vocab_start:vocab_end].add_(grad_logits.to(weight.dtype).t() @ hidden_chunk)
|
||||
if needs_hidden:
|
||||
grad_hidden[start:end].add_(grad_logits.to(hidden.dtype) @ weight_chunk)
|
||||
if needs_weight:
|
||||
grad_weight[vocab_start:vocab_end].add_(grad_logits.to(weight.dtype).t() @ hidden_chunk)
|
||||
|
||||
return grad_hidden, grad_weight, None, None, None
|
||||
|
||||
|
||||
@@ -61,6 +61,34 @@ def test_fused_lm_head_matches_full_logits_forward_and_backward_cpu():
|
||||
torch.testing.assert_close(grad_weight1, grad_weight0, rtol=0, atol=1e-5)
|
||||
|
||||
|
||||
def test_fused_lm_head_frozen_weight_backward_cpu():
|
||||
"""Frozen LM-head weight: hidden gradient matches baseline, no weight gradient is produced."""
|
||||
torch.manual_seed(0)
|
||||
b, s, h, v = 2, 4, 8, 37
|
||||
temperature = torch.full((b, s), 1.7, dtype=torch.float32)
|
||||
chunk_size = 3
|
||||
|
||||
hidden0 = torch.randn(b, s, h, dtype=torch.float32, requires_grad=True)
|
||||
labels = torch.randint(0, v, (b, s), dtype=torch.long)
|
||||
weight0 = torch.randn(v, h, dtype=torch.float32)
|
||||
|
||||
# Baseline (weight frozen)
|
||||
logp0, _ = _baseline_logprobs_and_entropy(hidden0, weight0, labels, temperature=temperature)
|
||||
logp0.sum().backward()
|
||||
grad_hidden0 = hidden0.grad.detach().clone()
|
||||
|
||||
# Fused (weight frozen)
|
||||
hidden1 = hidden0.detach().clone().requires_grad_(True)
|
||||
lm = FusedOutputLinear(in_features=h, out_features=v, chunk_size=chunk_size)
|
||||
lm.weight = torch.nn.Parameter(weight0.clone(), requires_grad=False)
|
||||
|
||||
out = lm(hidden1, labels, temperature=temperature)
|
||||
out["logprobs"].sum().backward()
|
||||
|
||||
torch.testing.assert_close(hidden1.grad, grad_hidden0, rtol=0, atol=1e-5)
|
||||
assert lm.weight.grad is None
|
||||
|
||||
|
||||
def test_fused_lm_head_requires_labels():
|
||||
"""Test that FusedOutputLinear raises assertion error when labels is None."""
|
||||
torch.manual_seed(0)
|
||||
|
||||
Reference in New Issue
Block a user