verl icon indicating copy to clipboard operation
verl copied to clipboard

attention_mask seems wrong in fdsp when use_remove_padding is False

Open vincentcoyette opened this issue 6 months ago • 2 comments

System Info

The problematic line is still on main.

Information

  • [ ] The official example scripts
  • [x] My own modified scripts

Tasks

  • [ ] An officially supported task in the examples folder (such as GLUE/SQuAD, ...)
  • [x] My own task or dataset (give details below)

Reproduction

Set use_remove_padding: False and strategy to fsdp2, and run GRPO.

The pg/clipfrac and ppo_kl are unusually high (more than 40% for the clipfrac in my case), even before any optimizer step, so the rollout and training policy should be similar up to some noise.

I think the problem comes from this line.

The attention mask is computed from loss_mask, which has the same shape as the response, but is used as an attention mask on the entire prompt+response.

Expected behavior

I think that something like this would make more sense:

                positions = torch.arange(int(max_seq_len), device=input_ids.device).unsqueeze(0)
                attention_mask = (positions < seq_len_effective.to(input_ids.device).unsqueeze(1)).to(torch.int32)

Or maybe having the attention_mask as None, similar to when use_remove_padding is True ?

Btw, I think that those two lines are also broken, because downstream (in ppo_loss, no_padding_2_padding), a flat tensor of shape (total_nnz,) is expected instead of (bsz, response_length). Maybe use_remove_padding: False is not maintained ?

vincentcoyette avatar May 08 '26 10:05 vincentcoyette

Yeah both look like real bugs, loss_mask being response-only but used as the full sequence attention mask explains the clipfrac/KL blowup. The shape mismatch at 1156 also tracks. I think this path is just untested.

Happy to take a stab at a fix if you haven't already.

matdou avatar May 11 '26 13:05 matdou

I won’t have time to work on it this week, so if you can please go for it ! Thanks

vincentcoyette avatar May 13 '26 18:05 vincentcoyette