Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -34,25 +34,25 @@ def benchmark(model_name, dataset_name):
|
|
| 34 |
sequence_lengths = [len(sample["input_ids"]) for sample in tokenized_dataset]
|
| 35 |
total_tokens = sum(sequence_lengths)
|
| 36 |
|
| 37 |
-
print("Computing the truncation
|
| 38 |
-
|
| 39 |
recommended = None
|
| 40 |
for max_len in TRUNCATION_LENGTHS:
|
| 41 |
total_truncated_tokens = sum(max(length - max_len, 0) for length in sequence_lengths)
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
if recommended is None and
|
| 45 |
recommended = max_len
|
| 46 |
|
| 47 |
hist = np.histogram(sequence_lengths, bins=50)
|
| 48 |
lengths_distribution = pd.DataFrame({
|
| 49 |
"max_length": (hist[1][:-1] + hist[1][1:])/2,
|
| 50 |
-
"
|
| 51 |
})
|
| 52 |
|
| 53 |
truncation_data = pd.DataFrame({
|
| 54 |
"max_length": [str(value) for value in TRUNCATION_LENGTHS],
|
| 55 |
-
"
|
| 56 |
})
|
| 57 |
|
| 58 |
return lengths_distribution, truncation_data, CODE_TEMPLATE.format(recommended)
|
|
@@ -61,12 +61,12 @@ with gr.Blocks() as demo:
|
|
| 61 |
model_input = gr.Textbox(label="Model Name", value="Qwen/Qwen3-0.6B")
|
| 62 |
dataset_input = gr.Textbox(label="Dataset Name", value="trl-lib/tldr")
|
| 63 |
run_button = gr.Button("Run estimation")
|
| 64 |
-
lengths_plot = gr.BarPlot(None, title="Length distribution", x="max_length", y="
|
| 65 |
-
|
| 66 |
|
| 67 |
recommended_code = gr.Code(CODE_TEMPLATE.format("..."), language="python", label="Recommended configuration")
|
| 68 |
|
| 69 |
-
run_button.click(fn=benchmark, inputs=[model_input, dataset_input], outputs=[lengths_plot,
|
| 70 |
|
| 71 |
with gr.Accordion("See details", open=False):
|
| 72 |
gr.Markdown("""
|
|
@@ -77,7 +77,7 @@ This tool helps you choose an appropriate `max_length` value for your SFT traini
|
|
| 77 |
- Prepares and tokenizes the data exactly as `SFTTrainer` would.
|
| 78 |
- Generates two visualizations:
|
| 79 |
- **Sequence Length Distribution:** Shows how long your tokenized sequences are.
|
| 80 |
-
- **Truncation
|
| 81 |
- Recommends the smallest `max_length` where truncation affects less than 5% of the tokens.
|
| 82 |
|
| 83 |
Use this tool to balance efficiency and memory usage when setting your `max_length` parameter.
|
|
|
|
| 34 |
sequence_lengths = [len(sample["input_ids"]) for sample in tokenized_dataset]
|
| 35 |
total_tokens = sum(sequence_lengths)
|
| 36 |
|
| 37 |
+
print("Computing the truncation percentages")
|
| 38 |
+
truncation_percentages = []
|
| 39 |
recommended = None
|
| 40 |
for max_len in TRUNCATION_LENGTHS:
|
| 41 |
total_truncated_tokens = sum(max(length - max_len, 0) for length in sequence_lengths)
|
| 42 |
+
truncation_percentage = total_truncated_tokens / total_tokens * 100
|
| 43 |
+
truncation_percentages.append(truncation_percentage)
|
| 44 |
+
if recommended is None and truncation_percentage < 5.0:
|
| 45 |
recommended = max_len
|
| 46 |
|
| 47 |
hist = np.histogram(sequence_lengths, bins=50)
|
| 48 |
lengths_distribution = pd.DataFrame({
|
| 49 |
"max_length": (hist[1][:-1] + hist[1][1:])/2,
|
| 50 |
+
"Percentage (%)": hist[0]/N_SAMPLES*100,
|
| 51 |
})
|
| 52 |
|
| 53 |
truncation_data = pd.DataFrame({
|
| 54 |
"max_length": [str(value) for value in TRUNCATION_LENGTHS],
|
| 55 |
+
"Percentage (%)": truncation_percentages,
|
| 56 |
})
|
| 57 |
|
| 58 |
return lengths_distribution, truncation_data, CODE_TEMPLATE.format(recommended)
|
|
|
|
| 61 |
model_input = gr.Textbox(label="Model Name", value="Qwen/Qwen3-0.6B")
|
| 62 |
dataset_input = gr.Textbox(label="Dataset Name", value="trl-lib/tldr")
|
| 63 |
run_button = gr.Button("Run estimation")
|
| 64 |
+
lengths_plot = gr.BarPlot(None, title="Length distribution", x="max_length", y="Percentage (%)")
|
| 65 |
+
truncation_percentage_plot = gr.BarPlot(None, title="Truncation percentage (how many tokens are discarded)", x="max_length", y="Percentage (%)")
|
| 66 |
|
| 67 |
recommended_code = gr.Code(CODE_TEMPLATE.format("..."), language="python", label="Recommended configuration")
|
| 68 |
|
| 69 |
+
run_button.click(fn=benchmark, inputs=[model_input, dataset_input], outputs=[lengths_plot, truncation_percentage_plot, recommended_code])
|
| 70 |
|
| 71 |
with gr.Accordion("See details", open=False):
|
| 72 |
gr.Markdown("""
|
|
|
|
| 77 |
- Prepares and tokenizes the data exactly as `SFTTrainer` would.
|
| 78 |
- Generates two visualizations:
|
| 79 |
- **Sequence Length Distribution:** Shows how long your tokenized sequences are.
|
| 80 |
+
- **Truncation Percentage:** Estimates the percentage of tokens that would be discarded (truncated) for different `max_length` values.
|
| 81 |
- Recommends the smallest `max_length` where truncation affects less than 5% of the tokens.
|
| 82 |
|
| 83 |
Use this tool to balance efficiency and memory usage when setting your `max_length` parameter.
|