File size: 9,670 Bytes
dc80200 3c7eec1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
import gradio as gr
from enum import Enum
from throughput_utils import create_throughput_plot
class AttentionType(Enum):
LOCAL = 0
GLOBAL = 1
class PhoneBandwidth(Enum):
Sixteen = 60
Fifteen = 51.2
Fourteen = 34.1
custom_css = """
#plot-container {
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1), 0 1px 3px rgba(0, 0, 0, 0.08);
padding: 1rem;
background-color: white;
height: 100%;
margin-bottom: 1.5rem;
}
#generate-button {
background-color: #2563eb;
color: white;
border-radius: 8px;
font-weight: bold;
padding: 10px 20px;
box-shadow: 0 4px 6px rgba(37, 99, 235, 0.1);
transition: all 0.2s ease;
width: 100%;
max-width: 400px;
margin: 0 auto;
font-size: 16px;
}
#generate-button:hover {
background-color: #1d4ed8;
box-shadow: 0 6px 8px rgba(37, 99, 235, 0.2);
transform: translateY(-2px);
}
.gradio-container {
background-color: #f5f7fa;
}
/* Custom styles for sliders containers */
.sliders-container {
border: 1px solid rgba(0, 0, 0, 0.1);
border-radius: 8px;
padding: 1rem;
margin-top: 0.5rem;
background-color: rgba(255, 255, 255, 0.8);
}
#error-status {
color: #b91c1c;
background-color: #fee2e2;
border-radius: 8px;
padding: 0.75rem;
margin-top: 0.5rem;
border: 1px solid #f87171;
font-weight: 500;
}
"""
with gr.Blocks(css=custom_css) as demo:
gqa_sliders = []
mla_sliders = []
with gr.Column():
gr.Markdown(
"""# ๐ On-Device LLM Throughput Calculator
This tool estimates the throughput (tokens per second) of Large Language Models on devices with memory bandwidth constraints.
It visualizes how different attention mechanisms (GQA, MLA) and context lengths affect throughput.
"""
)
with gr.Row():
plot_output = gr.Image(label="Throughput Plot", type="pil", elem_id="plot-container")
# Add status element to display validation errors
status_output = gr.Markdown(visible=False, elem_id="error-status")
with gr.Row():
plot_button = gr.Button("Generate Throughput Plot", size="lg", elem_id="generate-button", variant="primary")
with gr.Row():
with gr.Column(scale=1):
with gr.Group():
gr.Markdown("### Device Configuration")
model_name = gr.Textbox(label="Model Name", value="TinyLLM")
iphone_model = gr.Dropdown(
label="iPhone Model",
choices=[e.name for e in PhoneBandwidth],
value=PhoneBandwidth.Sixteen.name,
interactive=True
)
with gr.Group():
gr.Markdown("### Attention Configurations to Plot")
gr.Markdown("#### GQA Head Configurations")
gr.Markdown("*Note: GQA head count must be less than or equal to the total number of heads*")
with gr.Column(elem_classes="sliders-container"):
gqa_slider1 = gr.Slider(minimum=1, maximum=32, step=2, value=4,
label="GQA Head Count #1")
gqa_slider2 = gr.Slider(minimum=1, maximum=32, step=2, value=8,
label="GQA Head Count #2")
gqa_sliders.extend([gqa_slider1, gqa_slider2])
gr.Markdown("#### MLA Compressed Dimensions")
gr.Markdown("*Note: MLA dimension must be less than or equal to d_model*")
with gr.Column(elem_classes="sliders-container"):
mla_slider1 = gr.Slider(minimum=64, maximum=1024, step=64, value=256,
label="MLA Dimension #1")
mla_slider2 = gr.Slider(minimum=64, maximum=1024, step=64, value=512,
label="MLA Dimension #2")
mla_sliders.extend([mla_slider1, mla_slider2])
with gr.Column(scale=1):
with gr.Group():
gr.Markdown("### Model Configuration")
num_parameters = gr.Number(label="Parameters (Billions)", value=3)
parameter_size = gr.Slider(minimum=1, maximum=16.0, step=1.0, label="Parameter Size (bits per param)", value=5)
kv_parameter_size = gr.Slider(minimum=0.25, maximum=4.0, step=0.25,
label="KV Cache Size (bytes per value)", value=2.0)
num_layers = gr.Number(label="Number of Layers", value=36)
num_heads = gr.Number(label="Number of Heads", value=16,
info="GQA head counts must be less than or equal to this value")
d_model = gr.Number(label="D Model", value=2048,
info="MLA dimensions must be less than or equal to this value")
with gr.Group():
gr.Markdown("### Context Configuration")
ctx_length = gr.Slider(minimum=1024, maximum=131072, step=1024,
label="Max Context Length", value=65536)
local_layers = gr.Number(label="Local Attention Layers", value=0)
global_layers = gr.Number(label="Global Attention Layers", value=1)
swa_size = gr.Slider(minimum=1024, maximum=32768, step=1024,
label="Sliding Window Size", value=4096)
gr.Markdown(
"""
For more information, see [JAX ML Scaling Book](https://jax-ml.github.io/scaling-book/inference/#theoretical-estimates-for-llm-latency-and-throughput).
"""
)
def generate_throughput_plot(
model_name, iphone_model, num_parameters, parameter_size,
kv_parameter_size, num_layers, num_heads, d_model, ctx_length,
local_layers, global_layers, swa_size, gqa_1, gqa_2, mla_1, mla_2
):
memory_bandwidth = PhoneBandwidth[iphone_model].value
if "iPhone" not in model_name:
model_name = f"iPhone {iphone_model}: {model_name}"
try:
# Validate GQA head counts must be less than total attention heads
for gqa_heads, label in [(gqa_1, "GQA Head Count #1"), (gqa_2, "GQA Head Count #2")]:
if gqa_heads > num_heads:
raise ValueError(f"{label} ({gqa_heads}) cannot be greater than the total number of attention heads ({num_heads})")
# Validate MLA compressed dimensions must be less than d_model
for mla_dim, label in [(mla_1, "MLA Dimension #1"), (mla_2, "MLA Dimension #2")]:
if mla_dim > d_model:
raise ValueError(f"{label} ({mla_dim}) cannot be greater than the model dimension (d_model = {d_model})")
plot_img = create_throughput_plot(
model_name,
memory_bandwidth,
num_parameters,
parameter_size,
kv_parameter_size,
num_layers,
num_heads,
d_model,
ctx_length,
local_layers,
global_layers,
swa_size,
[gqa_1, gqa_2],
[mla_1, mla_2],
)
# Hide error message, show plot
return [
gr.update(value=plot_img),
gr.update(visible=False, value="")
]
except Exception as e:
err_string = f"Error generating plot: {str(e)}"
print(err_string)
# Show error message, clear plot
return [
gr.update(value=None),
gr.update(visible=True, value=f"โ ๏ธ {err_string}")
]
# Function to update GQA sliders based on number of heads
def update_gqa_sliders(heads_value):
if not heads_value or heads_value < 1:
heads_value = 1
return [gr.update(maximum=heads_value, value=min(slider.value, heads_value)) for slider in gqa_sliders]
# Function to update MLA sliders based on d_model
def update_mla_sliders(d_model_value):
if not d_model_value or d_model_value < 64:
d_model_value = 64
return [gr.update(maximum=d_model_value, value=min(slider.value, d_model_value)) for slider in mla_sliders]
# Add event handlers to update sliders when model configuration changes
num_heads.change(
update_gqa_sliders,
inputs=[num_heads],
outputs=gqa_sliders
)
d_model.change(
update_mla_sliders,
inputs=[d_model],
outputs=mla_sliders
)
plot_button.click(
generate_throughput_plot,
inputs=[
model_name,
iphone_model,
num_parameters,
parameter_size,
kv_parameter_size,
num_layers,
num_heads,
d_model,
ctx_length,
local_layers,
global_layers,
swa_size,
*gqa_sliders,
*mla_sliders,
],
outputs=[plot_output, status_output]
)
demo.launch()
|