Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ppo_gpt): prevent position_ids being None #451

Merged
merged 5 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/hh/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Launch training of [GPT-J](https://huggingface.co/EleutherAI/gpt-j-6B) on 7 GPUs
```sh
accelerate launch --num_processes 7 --config_file ../../configs/accelerate/zero2-bf16.yaml ppo_hh.py
```
Or if you want to train a smaller model or start from a supervised checkpoint, you can use one of the [configs](./configs)
Or if you want to train a smaller model or start from a supervised checkpoint, you can use one of the [configs](../../configs)
```sh
CONFIG_NAME=125M accelerate launch --num_processes 7 --config_file ../../configs/accelerate/zero2-bf16.yaml ppo_hh.py
```
Expand Down
59 changes: 37 additions & 22 deletions trlx/models/modeling_ppo.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class PPOConfig(MethodConfig):
:param vf_coef: Value loss scale w.r.t policy loss
:type vf_coef: float

:param gen_kwargs: Additioanl kwargs for the generation
:param gen_kwargs: Additional kwargs for the generation
:type gen_kwargs: Dict[str, Any]

:param gen_experience_kwargs: if this is not None, then the experience is generated using this
Expand Down Expand Up @@ -445,7 +445,7 @@ def forward( # noqa: max-complexity
"""Reference:
https://github.com/huggingface/transformers/blob/2411f0e465e761790879e605a4256f3d4afb7f82/src/transformers/models/gpt2/modeling_gpt2.py#L743 # noqa: E501
"""
batch_size = hidden_states.size()[0]
batch_size, seq_length = hidden_states.shape[:2]

output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
output_hidden_states = (
Expand All @@ -457,7 +457,16 @@ def forward( # noqa: max-complexity
device = hidden_states.device

if past_key_values is None:
past_length = 0
past_key_values = tuple([None] * len(self.decoder_blocks))
else:
past_length = past_key_values[0][0].size(-2)

if position_ids is None:
position_ids = torch.arange(past_length, seq_length + past_length, dtype=torch.long, device=device)
position_ids = position_ids.unsqueeze(0).view(-1, seq_length)
else:
position_ids = position_ids.view(-1, seq_length)

if attention_mask is not None:
if batch_size <= 0:
Expand Down Expand Up @@ -498,28 +507,27 @@ def forward( # noqa: max-complexity
if output_hidden_states:
all_hidden_states = all_hidden_states + (hidden_states,)

kwargs = dict(
layer_past=layer_past,
attention_mask=attention_mask,
position_ids=position_ids,
head_mask=head_mask[i],
encoder_hidden_states=encoder_hidden_states,
encoder_attention_mask=encoder_attention_mask,
use_cache=use_cache,
output_attentions=output_attentions,
)

# Assumes we are never training the branch
block_params = inspect.getfullargspec(block.forward).args
if "encoder_hidden_states" in block_params:
outputs = block(
hidden_states,
layer_past=layer_past,
attention_mask=attention_mask,
head_mask=head_mask[i],
encoder_hidden_states=encoder_hidden_states,
encoder_attention_mask=encoder_attention_mask,
use_cache=use_cache,
output_attentions=output_attentions,
)
else:
outputs = block(
hidden_states,
layer_past=layer_past,
attention_mask=attention_mask,
head_mask=head_mask[i],
use_cache=use_cache,
output_attentions=output_attentions,
)
if "encoder_hidden_states" not in block_params:
kwargs.pop("encoder_hidden_states")
kwargs.pop("encoder_attention_mask")
# Remove position_ids for GPT2Block
if "position_ids" not in block_params:
kwargs.pop("position_ids")

outputs = block(hidden_states, **kwargs)

hidden_states = outputs[0]
if use_cache is True:
Expand Down Expand Up @@ -594,10 +602,17 @@ def forward( # noqa: max-complexity
input_shape = hidden_states.size()[:-1]
combined_attention_mask = None
if input_shape[-1] > 1:
# `modeling_opt._make_causal_mask` @ transformers==4.27.1 doesn't have the `device` argument
if "device" in inspect.getfullargspec(modeling_opt._make_causal_mask).args:
kwargs = dict(device=hidden_state.device)
else:
kwargs = {}

combined_attention_mask = modeling_opt._make_causal_mask(
input_shape,
hidden_states.dtype,
past_key_values_length=past_key_values_length,
**kwargs,
).to(hidden_states.device)

if attention_mask is not None:
Expand Down