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

Faster & memory-efficient logprobs calculation #583

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions trlx/models/modeling_nemo_ppo.py
Original file line number Diff line number Diff line change
Expand Up @@ -993,7 +993,7 @@ def loss_func(model_output):
start = batch.query_tensors.shape[1]
end = start + response_length

label_logprobs = logprobs_of_labels(logits[:, :-1, :], inputs[:, 1:])
label_logprobs = logprobs_of_labels(logits, inputs[:, 1:])
label_logprobs = label_logprobs[:, start:end]

advantages, returns = self.ppo_config.get_advantages_and_returns(
Expand Down Expand Up @@ -1079,11 +1079,11 @@ def ppo_postprocess(model_output):
# to save memory

if run_policy_model and compute_logprobs:
logprobs = logprobs_of_labels(logits[:, :-1, :], tokens[:, 1:])
logprobs = logprobs_of_labels(logits, tokens[:, 1:])
return logprobs, dict(logprobs=logprobs, values=values)

if run_reference_model and compute_logprobs:
ref_logprobs = logprobs_of_labels(ref_logits[:, :-1, :], tokens[:, 1:])
ref_logprobs = logprobs_of_labels(ref_logits, tokens[:, 1:])
return ref_logprobs, dict(ref_logprobs=ref_logprobs)

return logits, {"logits": logits, "values": values, "ref_logits": ref_logits}
Expand Down
12 changes: 6 additions & 6 deletions trlx/trainer/accelerate_ppo_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def loss(self, batch: PPORLBatch) -> Tuple[float, Dict[str, Any]]:

logits = outputs.logits
values_pred = outputs.value
logprobs = logprobs_of_labels(logits[:, :-1, :], decoder_input_ids[:, 1:])
logprobs = logprobs_of_labels(logits, decoder_input_ids[:, 1:])
mask = decoder_input_ids.ne(self.tokenizer.pad_token_id).long().to(self.accelerator.device)
start = 0
end = start + response_length
Expand All @@ -181,7 +181,7 @@ def loss(self, batch: PPORLBatch) -> Tuple[float, Dict[str, Any]]:
logits = outputs.logits
values_pred = outputs.value
values_pred = values_pred[:, :-1]
logprobs = logprobs_of_labels(logits[:, :-1, :], tokens[:, 1:])
logprobs = logprobs_of_labels(logits, tokens[:, 1:])

start = query_tensors.shape[1] - 1
end = start + response_length
Expand Down Expand Up @@ -438,12 +438,12 @@ def make_experience(self, num_rollouts: int = 1024, iter_count: int = 0): # noq
ref_logits = ref_logits.to(device)

if self.config.model.model_arch_type == "seq2seq":
logprobs = logprobs_of_labels(logits[:, :-1, :], sample_outputs[:, 1:])
ref_logprobs = logprobs_of_labels(ref_logits[:, :-1, :], sample_outputs[:, 1:])
logprobs = logprobs_of_labels(logits, sample_outputs[:, 1:])
ref_logprobs = logprobs_of_labels(ref_logits, sample_outputs[:, 1:])
else:
# NOTE: logprob[i] is (log)prob at which all_token[i+1] was sampled
logprobs = logprobs_of_labels(logits[:, :-1, :], all_tokens[:, 1:])
ref_logprobs = logprobs_of_labels(ref_logits[:, :-1, :], all_tokens[:, 1:])
logprobs = logprobs_of_labels(logits, all_tokens[:, 1:])
ref_logprobs = logprobs_of_labels(ref_logits, all_tokens[:, 1:])

n_samples: int = samples.shape[0]

Expand Down
Loading