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 handling empty list statistics #1094

Merged
merged 2 commits into from
Jan 10, 2025
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
12 changes: 8 additions & 4 deletions src/distilabel/models/llms/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,15 @@ def prepare_output(
"""
output: "GenerateOutput" = {
"generations": generations,
"statistics": {
"input_tokens": input_tokens or [],
"output_tokens": output_tokens or [],
},
"statistics": {},
}

if input_tokens:
output["statistics"]["input_tokens"] = input_tokens

if output_tokens:
output["statistics"]["output_tokens"] = output_tokens

if logprobs:
output["logprobs"] = logprobs
return output
12 changes: 8 additions & 4 deletions src/distilabel/steps/tasks/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,15 +521,15 @@ def normalize_statistics(output: "GenerateOutput") -> "GenerateOutput":
gen_length = len(output["generations"])

for stat_key, stat_values in output["statistics"].items():
current_length = len(stat_values)
current_length = len(stat_values) # type: ignore

if current_length < gen_length:
if current_length > 0 and current_length < gen_length:
# Calculate how many times to repeat the tokens
repeats = gen_length // current_length
remainder = gen_length % current_length

# Create new list with repeated values
new_values = stat_values * repeats + stat_values[:remainder]
new_values = stat_values * repeats + stat_values[:remainder] # type: ignore
output["statistics"][stat_key] = new_values

return output
Expand All @@ -552,7 +552,11 @@ def iterate_generations_with_stats(
]
for i, generation in enumerate(outputs["generations"]):
# Create a new dictionary with the statistics for this index
stats = {key: values[i] for key, values in outputs["statistics"].items()} # type: ignore
stats = {
key: values[i] # type: ignore
for key, values in outputs["statistics"].items()
if values
}
# Extra keys returned by the `LLM`
extra = {key: outputs[key][i] for key in extra_keys}
yield generation, stats, extra
Loading