Skip to content

Commit b18ead0

Browse files
committed
opcd oel: fix bug of attn mask in topk padding
1 parent 0ff0ef6 commit b18ead0

2 files changed

Lines changed: 38 additions & 20 deletions

File tree

oel/verl/verl/workers/actor/dp_actor.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -303,22 +303,31 @@ def _forward_micro_batch(self, micro_batch, temperature, calculate_entropy=False
303303
R = response_length
304304
gathered = torch.full((batch_size, R, K), -1e20, dtype=log_probs.dtype, device=log_probs.device)
305305

306+
logit_slice_start = seqlen - R - 1
306307
for si in range(batch_size):
307308
actual_len = (cu_seqlens[si + 1] - cu_seqlens[si]).item()
308-
n_valid = min(R, actual_len - 1)
309-
if n_valid <= 0:
309+
if actual_len <= 0:
310310
continue
311-
rmpad_start = (cu_seqlens[si + 1]).item() - n_valid - 1
312-
rmpad_end = (cu_seqlens[si + 1]).item() - 1
313-
sample_log_probs = log_probs[rmpad_start:rmpad_end] # (n_valid, V)
311+
first_valid_col = indices[cu_seqlens[si]].item() - si * seqlen
312+
last_valid_col = first_valid_col + actual_len - 1
314313

315-
resp_offset = R - n_valid
316-
sample_kl_indices = kl_indices[si, resp_offset:] # (n_valid, K)
314+
r_start = max(0, first_valid_col - logit_slice_start)
315+
r_end = min(R - 1, last_valid_col - logit_slice_start)
316+
n_valid_logits = r_end - r_start + 1
317+
if n_valid_logits <= 0:
318+
continue
319+
320+
rmpad_offset = (logit_slice_start + r_start) - first_valid_col
321+
rmpad_start = cu_seqlens[si].item() + rmpad_offset
322+
rmpad_end = rmpad_start + n_valid_logits
323+
sample_log_probs = log_probs[rmpad_start:rmpad_end] # (n_valid_logits, V)
324+
325+
sample_kl_indices = kl_indices[si, r_start:r_end + 1] # (n_valid_logits, K)
317326
valid_mask = sample_kl_indices != -1
318327
safe_idx = torch.where(valid_mask, sample_kl_indices, torch.zeros_like(sample_kl_indices))
319-
sample_gathered = torch.gather(sample_log_probs, -1, safe_idx.long()) # (n_valid, K)
320-
gathered[si, resp_offset:] = torch.where(valid_mask, sample_gathered,
321-
torch.full_like(sample_gathered, -1e20))
328+
sample_gathered = torch.gather(sample_log_probs, -1, safe_idx.long())
329+
gathered[si, r_start:r_end + 1] = torch.where(valid_mask, sample_gathered,
330+
torch.full_like(sample_gathered, -1e20))
322331

323332
log_probs = gathered # (bs, R, K)
324333

opcd/verl/verl/workers/actor/dp_actor.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -303,22 +303,31 @@ def _forward_micro_batch(self, micro_batch, temperature, calculate_entropy=False
303303
R = response_length
304304
gathered = torch.full((batch_size, R, K), -1e20, dtype=log_probs.dtype, device=log_probs.device)
305305

306+
logit_slice_start = seqlen - R - 1
306307
for si in range(batch_size):
307308
actual_len = (cu_seqlens[si + 1] - cu_seqlens[si]).item()
308-
n_valid = min(R, actual_len - 1)
309-
if n_valid <= 0:
309+
if actual_len <= 0:
310310
continue
311-
rmpad_start = (cu_seqlens[si + 1]).item() - n_valid - 1
312-
rmpad_end = (cu_seqlens[si + 1]).item() - 1
313-
sample_log_probs = log_probs[rmpad_start:rmpad_end] # (n_valid, V)
311+
first_valid_col = indices[cu_seqlens[si]].item() - si * seqlen
312+
last_valid_col = first_valid_col + actual_len - 1
314313

315-
resp_offset = R - n_valid
316-
sample_kl_indices = kl_indices[si, resp_offset:] # (n_valid, K)
314+
r_start = max(0, first_valid_col - logit_slice_start)
315+
r_end = min(R - 1, last_valid_col - logit_slice_start)
316+
n_valid_logits = r_end - r_start + 1
317+
if n_valid_logits <= 0:
318+
continue
319+
320+
rmpad_offset = (logit_slice_start + r_start) - first_valid_col
321+
rmpad_start = cu_seqlens[si].item() + rmpad_offset
322+
rmpad_end = rmpad_start + n_valid_logits
323+
sample_log_probs = log_probs[rmpad_start:rmpad_end] # (n_valid_logits, V)
324+
325+
sample_kl_indices = kl_indices[si, r_start:r_end + 1] # (n_valid_logits, K)
317326
valid_mask = sample_kl_indices != -1
318327
safe_idx = torch.where(valid_mask, sample_kl_indices, torch.zeros_like(sample_kl_indices))
319-
sample_gathered = torch.gather(sample_log_probs, -1, safe_idx.long()) # (n_valid, K)
320-
gathered[si, resp_offset:] = torch.where(valid_mask, sample_gathered,
321-
torch.full_like(sample_gathered, -1e20))
328+
sample_gathered = torch.gather(sample_log_probs, -1, safe_idx.long())
329+
gathered[si, r_start:r_end + 1] = torch.where(valid_mask, sample_gathered,
330+
torch.full_like(sample_gathered, -1e20))
322331

323332
log_probs = gathered # (bs, R, K)
324333

0 commit comments

Comments
 (0)