Skip to content
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
35 changes: 23 additions & 12 deletions optimum/habana/transformers/generation/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2801,7 +2801,7 @@ def resolve_beam(beam):
return rest

prev_beams = [[root] * num_beams] * bs
best = [root] * bs
best = [[] for _ in range(bs)]

def beam_score(beam):
return (beam[3], beam[0])
Expand All @@ -2823,8 +2823,12 @@ def beam_score(beam):
if not is_finished:
cur_beams[batch].append(beam)
if is_finished or (step + 1 == beam_trace_idx):
if beam_score(best[batch]) < beam_score(beam):
best[batch] = beam
if len(best[batch]) < num_beams:
best[batch].append(beam)
best[batch] = sorted(best[batch], key=lambda x: beam_score(x))
elif beam_score(best[batch][0]) < beam_score(beam):
best[batch][0] = beam
best[batch] = sorted(best[batch], key=lambda x: beam_score(x))
prev_beams = cur_beams

def expand_if_needed(tensor, new_size, value, dim=-1):
Expand All @@ -2841,15 +2845,22 @@ def expand_if_needed(tensor, new_size, value, dim=-1):
assert False, f"Unsupported dim value: {dim}"
return tensor

result = [
torch.cat(
[initial_ids[i], torch.tensor(resolve_beam(b), dtype=initial_ids.dtype, device=initial_ids.device)]
)
for i, b in enumerate(best)
]
max_length = max([t.shape[-1] for t in result])
result = [expand_if_needed(res, max_length, model_config.pad_token_id) for res in result]
input_ids = torch.stack(result)
results = []
for i, beam_hyp in enumerate(best):
sorted_hyps = sorted(beam_hyp, key=lambda x: beam_score(x))
res = []
for j in range(beam_scorer.num_beam_hyps_to_keep):
best_hyp_tuple = sorted_hyps.pop()
resolve = resolve_beam(best_hyp_tuple)
res.append(torch.cat((initial_ids[i], torch.tensor(resolve))))
results.append(res)

max_length = max([n.shape[-1] for m in results for n in m])
return_res = []
for i, res in enumerate(results):
for j in range(beam_scorer.num_beam_hyps_to_keep):
return_res.append(expand_if_needed(res[j], max_length, model_config.pad_token_id))
input_ids = torch.stack(return_res)
return input_ids

hb_profer = HabanaProfile(
Expand Down
7 changes: 7 additions & 0 deletions tests/test_text_generation_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ def _test_text_generation(
parallel_strategy: str = None,
contrastive_search: bool = False,
num_beams: int = 1,
num_return_sequences: int = 1,
check_output: bool = False,
):
command = ["python3"]
Expand Down Expand Up @@ -248,6 +249,11 @@ def _test_text_generation(
"--bucket_internal --bucket_size 64",
]

if num_return_sequences > 1:
command += [
f"--num_return_sequences {num_return_sequences}",
]

if fp8:
if "--trim_logits" not in command:
command += ["--trim_logits"]
Expand Down Expand Up @@ -473,6 +479,7 @@ def test_text_generation_contrastive_search(
@pytest.mark.parametrize("model_name, batch_size, reuse_cache, baseline", MODELS_TO_TEST["beam_search"])
def test_text_generation_beam_search(model_name: str, baseline: float, batch_size: int, reuse_cache: bool, token: str):
_test_text_generation(model_name, baseline, token, batch_size, reuse_cache, num_beams=3)
_test_text_generation(model_name, baseline, token, batch_size, reuse_cache, num_beams=3, num_return_sequences=2)


class TextGenPipeline(TestCase):
Expand Down