flopscalc 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- flopscalc-0.1.0/.gitignore +31 -0
- flopscalc-0.1.0/CHANGELOG.md +32 -0
- flopscalc-0.1.0/LICENSE +21 -0
- flopscalc-0.1.0/PKG-INFO +291 -0
- flopscalc-0.1.0/README.md +254 -0
- flopscalc-0.1.0/conftest.py +6 -0
- flopscalc-0.1.0/examples/compare_models.py +52 -0
- flopscalc-0.1.0/pyproject.toml +62 -0
- flopscalc-0.1.0/src/flopscalc/__init__.py +72 -0
- flopscalc-0.1.0/src/flopscalc/cli.py +97 -0
- flopscalc-0.1.0/src/flopscalc/config.py +154 -0
- flopscalc-0.1.0/src/flopscalc/empirical.py +139 -0
- flopscalc-0.1.0/src/flopscalc/estimator.py +253 -0
- flopscalc-0.1.0/src/flopscalc/flops.py +168 -0
- flopscalc-0.1.0/src/flopscalc/groq.py +152 -0
- flopscalc-0.1.0/src/flopscalc/hardware.py +141 -0
- flopscalc-0.1.0/src/flopscalc/memory.py +124 -0
- flopscalc-0.1.0/src/flopscalc/performance.py +146 -0
- flopscalc-0.1.0/src/flopscalc/precision.py +57 -0
- flopscalc-0.1.0/src/flopscalc/py.typed +0 -0
- flopscalc-0.1.0/src/flopscalc/registry.py +385 -0
- flopscalc-0.1.0/src/flopscalc/result.py +154 -0
- flopscalc-0.1.0/tests/compare.py +13 -0
- flopscalc-0.1.0/tests/test_cli.py +45 -0
- flopscalc-0.1.0/tests/test_empirical.py +40 -0
- flopscalc-0.1.0/tests/test_estimate.py +103 -0
- flopscalc-0.1.0/tests/test_flops.py +113 -0
- flopscalc-0.1.0/tests/test_groq.py +64 -0
- flopscalc-0.1.0/tests/test_hardware.py +53 -0
- flopscalc-0.1.0/tests/test_memory.py +57 -0
- flopscalc-0.1.0/tests/test_parameters.py +83 -0
- flopscalc-0.1.0/tests/test_performance.py +68 -0
- flopscalc-0.1.0/tests/test_registry.py +98 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
*.egg-info/
|
|
6
|
+
.eggs/
|
|
7
|
+
build/
|
|
8
|
+
dist/
|
|
9
|
+
wheels/
|
|
10
|
+
*.egg
|
|
11
|
+
|
|
12
|
+
# Virtual environments
|
|
13
|
+
.venv/
|
|
14
|
+
venv/
|
|
15
|
+
env/
|
|
16
|
+
|
|
17
|
+
# Test / coverage
|
|
18
|
+
.pytest_cache/
|
|
19
|
+
.coverage
|
|
20
|
+
htmlcov/
|
|
21
|
+
.tox/
|
|
22
|
+
|
|
23
|
+
# Caches
|
|
24
|
+
.flopscalc_cache/
|
|
25
|
+
*.log
|
|
26
|
+
|
|
27
|
+
# Editors / OS
|
|
28
|
+
.vscode/
|
|
29
|
+
.idea/
|
|
30
|
+
.DS_Store
|
|
31
|
+
Thumbs.db
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to FLOPSCalc are documented here. The format follows
|
|
4
|
+
[Keep a Changelog](https://keepachangelog.com/), and the project adheres to
|
|
5
|
+
[Semantic Versioning](https://semver.org/).
|
|
6
|
+
|
|
7
|
+
## [Unreleased]
|
|
8
|
+
|
|
9
|
+
## [0.1.0] - 2026-07-24
|
|
10
|
+
|
|
11
|
+
Initial release.
|
|
12
|
+
|
|
13
|
+
### Added
|
|
14
|
+
- Analytical `estimate()` API returning parameters, forward/backward FLOPs
|
|
15
|
+
(broken down by embedding, attention, MLP, normalization, and output head),
|
|
16
|
+
weight/activation/KV-cache memory, roofline latency, and energy.
|
|
17
|
+
- Component-wise, GQA/MQA-aware FLOP accounting using the `2 * m * k * n`
|
|
18
|
+
matmul convention; exact parameter counts validated against 20+ public models
|
|
19
|
+
(Llama 2/3, Mistral, Qwen2.5, Gemma, Phi-2, GPT-2, Falcon-7B).
|
|
20
|
+
- Bundled model registry plus Hugging Face Hub `config.json` fetch, with a
|
|
21
|
+
robust parser for non-standard architectures (Falcon, GPT-2 field names,
|
|
22
|
+
nested multimodal `text_config`).
|
|
23
|
+
- GPU database (A100/H100/H200/V100/L40S/L4/T4/A10G/A6000/RTX 4090/3090) with
|
|
24
|
+
per-precision peak throughput; roofline model splitting prefill (compute-bound)
|
|
25
|
+
and decode (memory-bound) phases.
|
|
26
|
+
- Precision support: fp32, tf32, fp16, bf16, fp8, int8, int4.
|
|
27
|
+
- `compare()` for side-by-side model comparison and a `flopscalc` CLI.
|
|
28
|
+
- `flopscalc.groq` provider: run estimates on GroqCloud model IDs fully offline
|
|
29
|
+
(no HF fetch), with published throughput attached and MoE models guarded.
|
|
30
|
+
- Optional empirical validation (`flopscalc[torch]`) against a real PyTorch
|
|
31
|
+
forward pass via `FlopCounterMode`.
|
|
32
|
+
- Full pytest suite and usage documentation (README).
|
flopscalc-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 FLOPSCalc contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
flopscalc-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: flopscalc
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Accurate FLOPs, memory, latency, and energy estimation for Large Language Models.
|
|
5
|
+
Author-email: Your Name <you@example.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Keywords: energy,flops,inference,kv-cache,latency,llm,memory,profiling,roofline,transformer
|
|
9
|
+
Classifier: Development Status :: 4 - Beta
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: Intended Audience :: Science/Research
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
21
|
+
Classifier: Typing :: Typed
|
|
22
|
+
Requires-Python: >=3.9
|
|
23
|
+
Provides-Extra: all
|
|
24
|
+
Requires-Dist: huggingface-hub>=0.20; extra == 'all'
|
|
25
|
+
Requires-Dist: pytest>=7.0; extra == 'all'
|
|
26
|
+
Requires-Dist: torch>=2.1; extra == 'all'
|
|
27
|
+
Requires-Dist: transformers>=4.40; extra == 'all'
|
|
28
|
+
Provides-Extra: dev
|
|
29
|
+
Requires-Dist: pytest-cov; extra == 'dev'
|
|
30
|
+
Requires-Dist: pytest>=7.0; extra == 'dev'
|
|
31
|
+
Provides-Extra: hf
|
|
32
|
+
Requires-Dist: huggingface-hub>=0.20; extra == 'hf'
|
|
33
|
+
Provides-Extra: torch
|
|
34
|
+
Requires-Dist: torch>=2.1; extra == 'torch'
|
|
35
|
+
Requires-Dist: transformers>=4.40; extra == 'torch'
|
|
36
|
+
Description-Content-Type: text/markdown
|
|
37
|
+
|
|
38
|
+
# FLOPSCalc
|
|
39
|
+
|
|
40
|
+
**Accurate FLOPs, memory, latency, and energy estimation for Large Language Models.**
|
|
41
|
+
|
|
42
|
+
FLOPSCalc answers questions like *"how much compute, memory, and energy does one
|
|
43
|
+
forward pass of Llama‑3‑8B at bf16 on an A100 actually cost?"* — analytically,
|
|
44
|
+
in milliseconds, without downloading a single weight. It is built for
|
|
45
|
+
transformers specifically (self‑attention, KV cache, GQA/MQA, gated MLPs), gives
|
|
46
|
+
a **layer‑wise breakdown** rather than one opaque total, and models **precision,
|
|
47
|
+
memory, latency, and energy** under different inference settings behind one API.
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
from flopscalc import estimate
|
|
51
|
+
|
|
52
|
+
result = estimate(
|
|
53
|
+
model="meta-llama/Llama-3-8B",
|
|
54
|
+
batch_size=4,
|
|
55
|
+
sequence_length=2048,
|
|
56
|
+
precision="bf16",
|
|
57
|
+
gpu="A100",
|
|
58
|
+
)
|
|
59
|
+
print(result)
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
```
|
|
63
|
+
==============================================================
|
|
64
|
+
FLOPSCalc estimate: meta-llama/Meta-Llama-3-8B
|
|
65
|
+
batch=4 seq_len=2048 precision=bf16 gpu=A100-80GB-SXM
|
|
66
|
+
==============================================================
|
|
67
|
+
Parameters : 8.03B (8,030,261,248)
|
|
68
|
+
--------------------------------------------------------------
|
|
69
|
+
FLOPs (single forward pass over batch x seq)
|
|
70
|
+
Forward : 131.885 TFLOP
|
|
71
|
+
Backward : 263.770 TFLOP
|
|
72
|
+
- attention : 30.880 TFLOP
|
|
73
|
+
- mlp : 92.389 TFLOP
|
|
74
|
+
- embedding : 0 FLOP
|
|
75
|
+
- layernorm : 8.724 GFLOP
|
|
76
|
+
- output head : 8.607 TFLOP
|
|
77
|
+
--------------------------------------------------------------
|
|
78
|
+
Memory
|
|
79
|
+
Total : 17.268 GB
|
|
80
|
+
- weights : 16.061 GB
|
|
81
|
+
- activations : 134.218 MB
|
|
82
|
+
- kv cache : 1.074 GB
|
|
83
|
+
Fits on GPU : yes
|
|
84
|
+
--------------------------------------------------------------
|
|
85
|
+
Performance (roofline)
|
|
86
|
+
Prefill latency : 845.42 ms [compute-bound]
|
|
87
|
+
Decode/token : 10.50 ms (381 tok/s)
|
|
88
|
+
Forward energy : 287.44 J (0.0798 Wh)
|
|
89
|
+
==============================================================
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## Why FLOPSCalc
|
|
93
|
+
|
|
94
|
+
Existing FLOP counters tend to support only CNNs, report a single total with no
|
|
95
|
+
layer breakdown, ignore transformer‑specific structure (self‑attention cost, KV
|
|
96
|
+
cache), and say nothing about precision, memory, latency, or energy. FLOPSCalc
|
|
97
|
+
targets exactly those gaps:
|
|
98
|
+
|
|
99
|
+
- **Transformer‑aware** — separate accounting for embeddings, multi‑head
|
|
100
|
+
attention (with GQA/MQA), the feed‑forward network (standard *and* gated /
|
|
101
|
+
SwiGLU), normalization, and the output head.
|
|
102
|
+
- **More than FLOPs** — parameter count, weight/activation/KV‑cache memory,
|
|
103
|
+
roofline latency (prefill *and* decode), and energy.
|
|
104
|
+
- **Precision‑aware** — fp32, tf32, fp16, bf16, fp8, int8, int4.
|
|
105
|
+
- **One API to compare models** under different batch sizes, sequence lengths,
|
|
106
|
+
precisions, and GPUs.
|
|
107
|
+
- **Hybrid accuracy** — a fast analytical core, plus an optional empirical mode
|
|
108
|
+
that checks the analytical FLOPs against a real PyTorch forward pass.
|
|
109
|
+
|
|
110
|
+
## Install
|
|
111
|
+
|
|
112
|
+
```bash
|
|
113
|
+
pip install flopscalc # analytical core, zero dependencies
|
|
114
|
+
pip install "flopscalc[hf]" # + fetch model configs from the HF Hub
|
|
115
|
+
pip install "flopscalc[torch]" # + empirical validation (torch, transformers)
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
The analytical core is **pure Python with no required dependencies**, so it runs
|
|
119
|
+
anywhere and can size a 70B model on a laptop.
|
|
120
|
+
|
|
121
|
+
## The `estimate()` API
|
|
122
|
+
|
|
123
|
+
```python
|
|
124
|
+
estimate(
|
|
125
|
+
model, # registry name / HF id, ModelConfig, or dict
|
|
126
|
+
batch_size=1,
|
|
127
|
+
sequence_length=2048,
|
|
128
|
+
precision="bf16", # fp32|tf32|fp16|bf16|fp8|int8|int4 (aliases ok)
|
|
129
|
+
gpu="A100", # GPU name / GPUSpec, or None to skip latency/energy
|
|
130
|
+
*,
|
|
131
|
+
include_backward=True, # also report training (backward) FLOPs
|
|
132
|
+
causal=False, # count only causal (lower-triangular) attention
|
|
133
|
+
num_generated_tokens=128, # decode horizon for full-request latency/energy
|
|
134
|
+
kv_cache_precision=None, # defaults to precision (fp16 when weights are int8/int4)
|
|
135
|
+
flash_attention=True, # activation-memory model
|
|
136
|
+
activation_factor=2.0,
|
|
137
|
+
compute_efficiency=0.5, # roofline MFU
|
|
138
|
+
mem_efficiency=0.8, # achievable fraction of peak bandwidth
|
|
139
|
+
power_utilization=0.85, # mean power draw as a fraction of TDP
|
|
140
|
+
allow_hf_fetch=True,
|
|
141
|
+
)
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
The returned `EstimationResult` behaves like a `dict` (so `result["forward_flops"]`
|
|
145
|
+
and `json.dumps(result)` both work) but prints as the report above. Dictionary
|
|
146
|
+
values use SI base units: **FLOPs are raw counts, memory is bytes, latency is
|
|
147
|
+
seconds, energy is joules.** The ten headline keys match the documented shape:
|
|
148
|
+
|
|
149
|
+
```python
|
|
150
|
+
{
|
|
151
|
+
"parameters": 8030261248,
|
|
152
|
+
"forward_flops": 131_885_...,
|
|
153
|
+
"backward_flops": 263_770_...,
|
|
154
|
+
"attention_flops": ..., "mlp_flops": ..., "embedding_flops": ...,
|
|
155
|
+
"memory": ..., "kv_cache": ...,
|
|
156
|
+
"latency": 0.845, "energy": 287.4,
|
|
157
|
+
}
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
A full nested breakdown (per‑component FLOPs, parameter and memory breakdowns,
|
|
161
|
+
prefill/decode performance, and the settings used) lives under
|
|
162
|
+
`result["details"]`.
|
|
163
|
+
|
|
164
|
+
### Comparing models
|
|
165
|
+
|
|
166
|
+
```python
|
|
167
|
+
from flopscalc import compare
|
|
168
|
+
|
|
169
|
+
print(compare(
|
|
170
|
+
["gpt2", "meta-llama/Llama-3-8B", "mistralai/Mistral-7B-v0.1"],
|
|
171
|
+
batch_size=1, sequence_length=4096, precision="bf16", gpu="A100",
|
|
172
|
+
))
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
### Command line
|
|
176
|
+
|
|
177
|
+
```bash
|
|
178
|
+
flopscalc estimate meta-llama/Llama-3-8B -b 4 -s 2048 -p bf16 -g A100
|
|
179
|
+
flopscalc compare gpt2 meta-llama/Llama-3-8B -p bf16 -g A100
|
|
180
|
+
flopscalc estimate meta-llama/Llama-3-8B --json # machine-readable
|
|
181
|
+
flopscalc list-models
|
|
182
|
+
flopscalc list-gpus
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
## Methodology
|
|
186
|
+
|
|
187
|
+
**FLOPs.** A matmul `[m,k]·[k,n]` costs `2·m·k·n` FLOPs (one multiply + one add
|
|
188
|
+
per MAC) — the standard Kaplan/Chinchilla convention, so results are directly
|
|
189
|
+
comparable to published figures. Per layer FLOPSCalc counts the Q/K/V/output
|
|
190
|
+
projections, the `Q·Kᵀ` and `softmax·V` attention terms (`O(s²)`, halved with
|
|
191
|
+
`causal=True`), and the MLP matmuls (2 matrices for a standard MLP, 3 for a gated
|
|
192
|
+
SwiGLU/GeGLU), plus the small softmax/normalization/activation/RoPE element‑wise
|
|
193
|
+
terms. The token‑embedding lookup is a gather (≈0 FLOPs); the output head is
|
|
194
|
+
`2·N·h·V`. This reproduces the familiar `forward ≈ 2·N·D` per‑token result;
|
|
195
|
+
the backward pass is `≈ 2×` the forward pass, giving the `6·N·D` training
|
|
196
|
+
estimate.
|
|
197
|
+
|
|
198
|
+
**Memory.** The parameter count is exact and reproduces published model‑card
|
|
199
|
+
figures (see below). Runtime memory = weights (`params × bytes/precision`) +
|
|
200
|
+
KV cache + a first‑order activation estimate. The **KV cache** is
|
|
201
|
+
`2 · batch · seq · layers · (kv_heads · head_dim) · bytes`, so grouped‑query
|
|
202
|
+
attention shrinks it by `num_heads / num_kv_heads`.
|
|
203
|
+
|
|
204
|
+
**Latency & energy (roofline).** Prefill and decode are modelled separately
|
|
205
|
+
because they behave differently. Prefill (encoding the prompt) is compute‑bound:
|
|
206
|
+
`time = FLOPs / (peak_throughput × MFU)`. Decode (one token at a time) is
|
|
207
|
+
memory‑bandwidth‑bound: each step re‑reads every weight and the KV cache from
|
|
208
|
+
HBM, so `time = bytes_moved / (bandwidth × utilization)`. Each phase takes
|
|
209
|
+
`max(compute_time, memory_time)`. Energy = `time × TDP × power_utilization`.
|
|
210
|
+
The efficiency knobs (`compute_efficiency`/MFU, `mem_efficiency`,
|
|
211
|
+
`power_utilization`) are the dominant source of error and are exposed as
|
|
212
|
+
parameters.
|
|
213
|
+
|
|
214
|
+
### Validation
|
|
215
|
+
|
|
216
|
+
Bundled model parameter counts match their public model cards **exactly**:
|
|
217
|
+
|
|
218
|
+
| Model | FLOPSCalc | Published |
|
|
219
|
+
|---|---|---|
|
|
220
|
+
| GPT‑2 | 124,439,808 | 124M |
|
|
221
|
+
| Llama‑2‑7B | 6,738,415,616 | 6.74B |
|
|
222
|
+
| Mistral‑7B | 7,241,732,096 | 7.24B |
|
|
223
|
+
| Llama‑3‑8B | 8,030,261,248 | 8.03B |
|
|
224
|
+
| Qwen2.5‑7B | 7,615,716,864 | 7.61B |
|
|
225
|
+
| Gemma‑7B | 8,537,680,896 | 8.54B |
|
|
226
|
+
| Llama‑2‑70B | 68,976,648,192 | ~69B |
|
|
227
|
+
| Llama‑3‑70B | 70,553,706,496 | 70.6B |
|
|
228
|
+
|
|
229
|
+
The optional empirical mode checks FLOPs against a real forward pass:
|
|
230
|
+
|
|
231
|
+
```python
|
|
232
|
+
import flopscalc as fc
|
|
233
|
+
cfg = fc.ModelConfig(name="tiny", hidden_size=256, num_layers=4,
|
|
234
|
+
num_attention_heads=8, num_key_value_heads=4,
|
|
235
|
+
vocab_size=1024, intermediate_size=688, model_type="llama")
|
|
236
|
+
print(fc.validate(cfg, batch_size=1, sequence_length=64))
|
|
237
|
+
# {'measured_forward_flops': ..., 'analytical_forward_flops': ..., 'ratio': ~1.0, ...}
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
## Supported models, GPUs, and precisions
|
|
241
|
+
|
|
242
|
+
**Models (bundled).** GPT‑2 (base/medium/large/xl), Llama‑2 (7B/13B/70B),
|
|
243
|
+
Llama‑3 / 3.1 / 3.2 (1B/3B/8B/70B), Mistral‑7B, Qwen2.5 (0.5B/7B/72B), Phi‑2,
|
|
244
|
+
Gemma (2B/7B), Falcon‑7B. Anything else resolves via the Hugging Face Hub —
|
|
245
|
+
the parser normalizes field-name variance across architectures (GPT‑2's
|
|
246
|
+
`n_embd`, Falcon's `multi_query` and implicit `intermediate_size`, nested
|
|
247
|
+
`text_config` for multimodal models, etc.). Or pass your own `ModelConfig` /
|
|
248
|
+
dict, or `register_model(...)`. (Mixture‑of‑Experts models fetch fine but their
|
|
249
|
+
FLOPs are overcounted as dense — MoE is on the roadmap.)
|
|
250
|
+
|
|
251
|
+
**GPUs (bundled).** A100 (40/80GB, SXM/PCIe), H100 (SXM/PCIe), H200, V100, T4,
|
|
252
|
+
L4, L40S, A10G, A6000, RTX 4090, RTX 3090. Define a `GPUSpec` for anything else.
|
|
253
|
+
|
|
254
|
+
**Precisions.** fp32, tf32, fp16, bf16, fp8, int8, int4.
|
|
255
|
+
|
|
256
|
+
## Custom models and hardware
|
|
257
|
+
|
|
258
|
+
```python
|
|
259
|
+
import flopscalc as fc
|
|
260
|
+
|
|
261
|
+
cfg = fc.ModelConfig(
|
|
262
|
+
name="my-model", hidden_size=4096, num_layers=32, num_attention_heads=32,
|
|
263
|
+
num_key_value_heads=8, intermediate_size=14336, vocab_size=128256,
|
|
264
|
+
mlp_type="gated", norm_type="rmsnorm", positional="rope",
|
|
265
|
+
)
|
|
266
|
+
print(fc.estimate(cfg, gpu="H100", precision="fp8"))
|
|
267
|
+
|
|
268
|
+
gpu = fc.GPUSpec(name="MI300X", memory_gb=192, memory_bandwidth_gbps=5300,
|
|
269
|
+
tdp_watts=750, peak_tflops={"bf16": 1307, "fp16": 1307, "fp8": 2615})
|
|
270
|
+
print(fc.estimate("meta-llama/Llama-3-8B", gpu=gpu, precision="bf16"))
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
## Accuracy & limitations
|
|
274
|
+
|
|
275
|
+
Estimates are analytical, first‑order, and meant for planning and comparison,
|
|
276
|
+
not cycle‑accurate profiling. Real throughput depends heavily on the
|
|
277
|
+
implementation, kernel quality, and achieved MFU — hence the tunable efficiency
|
|
278
|
+
knobs. Activation memory is a rough estimate (inference is dominated by weights +
|
|
279
|
+
KV cache). Mixture‑of‑Experts routing, speculative decoding, tensor/pipeline
|
|
280
|
+
parallelism overheads, and multi‑GPU communication are **not** modelled yet.
|
|
281
|
+
Parameter counts and matmul FLOP counts, however, are exact.
|
|
282
|
+
|
|
283
|
+
## Roadmap
|
|
284
|
+
|
|
285
|
+
Mixture‑of‑Experts models, multi‑GPU/parallelism cost models, encoder–decoder and
|
|
286
|
+
vision‑language architectures, a curated empirical‑throughput calibration table,
|
|
287
|
+
and prompt‑vs‑generation energy attribution.
|
|
288
|
+
|
|
289
|
+
## License
|
|
290
|
+
|
|
291
|
+
MIT — see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
# FLOPSCalc
|
|
2
|
+
|
|
3
|
+
**Accurate FLOPs, memory, latency, and energy estimation for Large Language Models.**
|
|
4
|
+
|
|
5
|
+
FLOPSCalc answers questions like *"how much compute, memory, and energy does one
|
|
6
|
+
forward pass of Llama‑3‑8B at bf16 on an A100 actually cost?"* — analytically,
|
|
7
|
+
in milliseconds, without downloading a single weight. It is built for
|
|
8
|
+
transformers specifically (self‑attention, KV cache, GQA/MQA, gated MLPs), gives
|
|
9
|
+
a **layer‑wise breakdown** rather than one opaque total, and models **precision,
|
|
10
|
+
memory, latency, and energy** under different inference settings behind one API.
|
|
11
|
+
|
|
12
|
+
```python
|
|
13
|
+
from flopscalc import estimate
|
|
14
|
+
|
|
15
|
+
result = estimate(
|
|
16
|
+
model="meta-llama/Llama-3-8B",
|
|
17
|
+
batch_size=4,
|
|
18
|
+
sequence_length=2048,
|
|
19
|
+
precision="bf16",
|
|
20
|
+
gpu="A100",
|
|
21
|
+
)
|
|
22
|
+
print(result)
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
```
|
|
26
|
+
==============================================================
|
|
27
|
+
FLOPSCalc estimate: meta-llama/Meta-Llama-3-8B
|
|
28
|
+
batch=4 seq_len=2048 precision=bf16 gpu=A100-80GB-SXM
|
|
29
|
+
==============================================================
|
|
30
|
+
Parameters : 8.03B (8,030,261,248)
|
|
31
|
+
--------------------------------------------------------------
|
|
32
|
+
FLOPs (single forward pass over batch x seq)
|
|
33
|
+
Forward : 131.885 TFLOP
|
|
34
|
+
Backward : 263.770 TFLOP
|
|
35
|
+
- attention : 30.880 TFLOP
|
|
36
|
+
- mlp : 92.389 TFLOP
|
|
37
|
+
- embedding : 0 FLOP
|
|
38
|
+
- layernorm : 8.724 GFLOP
|
|
39
|
+
- output head : 8.607 TFLOP
|
|
40
|
+
--------------------------------------------------------------
|
|
41
|
+
Memory
|
|
42
|
+
Total : 17.268 GB
|
|
43
|
+
- weights : 16.061 GB
|
|
44
|
+
- activations : 134.218 MB
|
|
45
|
+
- kv cache : 1.074 GB
|
|
46
|
+
Fits on GPU : yes
|
|
47
|
+
--------------------------------------------------------------
|
|
48
|
+
Performance (roofline)
|
|
49
|
+
Prefill latency : 845.42 ms [compute-bound]
|
|
50
|
+
Decode/token : 10.50 ms (381 tok/s)
|
|
51
|
+
Forward energy : 287.44 J (0.0798 Wh)
|
|
52
|
+
==============================================================
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Why FLOPSCalc
|
|
56
|
+
|
|
57
|
+
Existing FLOP counters tend to support only CNNs, report a single total with no
|
|
58
|
+
layer breakdown, ignore transformer‑specific structure (self‑attention cost, KV
|
|
59
|
+
cache), and say nothing about precision, memory, latency, or energy. FLOPSCalc
|
|
60
|
+
targets exactly those gaps:
|
|
61
|
+
|
|
62
|
+
- **Transformer‑aware** — separate accounting for embeddings, multi‑head
|
|
63
|
+
attention (with GQA/MQA), the feed‑forward network (standard *and* gated /
|
|
64
|
+
SwiGLU), normalization, and the output head.
|
|
65
|
+
- **More than FLOPs** — parameter count, weight/activation/KV‑cache memory,
|
|
66
|
+
roofline latency (prefill *and* decode), and energy.
|
|
67
|
+
- **Precision‑aware** — fp32, tf32, fp16, bf16, fp8, int8, int4.
|
|
68
|
+
- **One API to compare models** under different batch sizes, sequence lengths,
|
|
69
|
+
precisions, and GPUs.
|
|
70
|
+
- **Hybrid accuracy** — a fast analytical core, plus an optional empirical mode
|
|
71
|
+
that checks the analytical FLOPs against a real PyTorch forward pass.
|
|
72
|
+
|
|
73
|
+
## Install
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
pip install flopscalc # analytical core, zero dependencies
|
|
77
|
+
pip install "flopscalc[hf]" # + fetch model configs from the HF Hub
|
|
78
|
+
pip install "flopscalc[torch]" # + empirical validation (torch, transformers)
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
The analytical core is **pure Python with no required dependencies**, so it runs
|
|
82
|
+
anywhere and can size a 70B model on a laptop.
|
|
83
|
+
|
|
84
|
+
## The `estimate()` API
|
|
85
|
+
|
|
86
|
+
```python
|
|
87
|
+
estimate(
|
|
88
|
+
model, # registry name / HF id, ModelConfig, or dict
|
|
89
|
+
batch_size=1,
|
|
90
|
+
sequence_length=2048,
|
|
91
|
+
precision="bf16", # fp32|tf32|fp16|bf16|fp8|int8|int4 (aliases ok)
|
|
92
|
+
gpu="A100", # GPU name / GPUSpec, or None to skip latency/energy
|
|
93
|
+
*,
|
|
94
|
+
include_backward=True, # also report training (backward) FLOPs
|
|
95
|
+
causal=False, # count only causal (lower-triangular) attention
|
|
96
|
+
num_generated_tokens=128, # decode horizon for full-request latency/energy
|
|
97
|
+
kv_cache_precision=None, # defaults to precision (fp16 when weights are int8/int4)
|
|
98
|
+
flash_attention=True, # activation-memory model
|
|
99
|
+
activation_factor=2.0,
|
|
100
|
+
compute_efficiency=0.5, # roofline MFU
|
|
101
|
+
mem_efficiency=0.8, # achievable fraction of peak bandwidth
|
|
102
|
+
power_utilization=0.85, # mean power draw as a fraction of TDP
|
|
103
|
+
allow_hf_fetch=True,
|
|
104
|
+
)
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
The returned `EstimationResult` behaves like a `dict` (so `result["forward_flops"]`
|
|
108
|
+
and `json.dumps(result)` both work) but prints as the report above. Dictionary
|
|
109
|
+
values use SI base units: **FLOPs are raw counts, memory is bytes, latency is
|
|
110
|
+
seconds, energy is joules.** The ten headline keys match the documented shape:
|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
{
|
|
114
|
+
"parameters": 8030261248,
|
|
115
|
+
"forward_flops": 131_885_...,
|
|
116
|
+
"backward_flops": 263_770_...,
|
|
117
|
+
"attention_flops": ..., "mlp_flops": ..., "embedding_flops": ...,
|
|
118
|
+
"memory": ..., "kv_cache": ...,
|
|
119
|
+
"latency": 0.845, "energy": 287.4,
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
A full nested breakdown (per‑component FLOPs, parameter and memory breakdowns,
|
|
124
|
+
prefill/decode performance, and the settings used) lives under
|
|
125
|
+
`result["details"]`.
|
|
126
|
+
|
|
127
|
+
### Comparing models
|
|
128
|
+
|
|
129
|
+
```python
|
|
130
|
+
from flopscalc import compare
|
|
131
|
+
|
|
132
|
+
print(compare(
|
|
133
|
+
["gpt2", "meta-llama/Llama-3-8B", "mistralai/Mistral-7B-v0.1"],
|
|
134
|
+
batch_size=1, sequence_length=4096, precision="bf16", gpu="A100",
|
|
135
|
+
))
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### Command line
|
|
139
|
+
|
|
140
|
+
```bash
|
|
141
|
+
flopscalc estimate meta-llama/Llama-3-8B -b 4 -s 2048 -p bf16 -g A100
|
|
142
|
+
flopscalc compare gpt2 meta-llama/Llama-3-8B -p bf16 -g A100
|
|
143
|
+
flopscalc estimate meta-llama/Llama-3-8B --json # machine-readable
|
|
144
|
+
flopscalc list-models
|
|
145
|
+
flopscalc list-gpus
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
## Methodology
|
|
149
|
+
|
|
150
|
+
**FLOPs.** A matmul `[m,k]·[k,n]` costs `2·m·k·n` FLOPs (one multiply + one add
|
|
151
|
+
per MAC) — the standard Kaplan/Chinchilla convention, so results are directly
|
|
152
|
+
comparable to published figures. Per layer FLOPSCalc counts the Q/K/V/output
|
|
153
|
+
projections, the `Q·Kᵀ` and `softmax·V` attention terms (`O(s²)`, halved with
|
|
154
|
+
`causal=True`), and the MLP matmuls (2 matrices for a standard MLP, 3 for a gated
|
|
155
|
+
SwiGLU/GeGLU), plus the small softmax/normalization/activation/RoPE element‑wise
|
|
156
|
+
terms. The token‑embedding lookup is a gather (≈0 FLOPs); the output head is
|
|
157
|
+
`2·N·h·V`. This reproduces the familiar `forward ≈ 2·N·D` per‑token result;
|
|
158
|
+
the backward pass is `≈ 2×` the forward pass, giving the `6·N·D` training
|
|
159
|
+
estimate.
|
|
160
|
+
|
|
161
|
+
**Memory.** The parameter count is exact and reproduces published model‑card
|
|
162
|
+
figures (see below). Runtime memory = weights (`params × bytes/precision`) +
|
|
163
|
+
KV cache + a first‑order activation estimate. The **KV cache** is
|
|
164
|
+
`2 · batch · seq · layers · (kv_heads · head_dim) · bytes`, so grouped‑query
|
|
165
|
+
attention shrinks it by `num_heads / num_kv_heads`.
|
|
166
|
+
|
|
167
|
+
**Latency & energy (roofline).** Prefill and decode are modelled separately
|
|
168
|
+
because they behave differently. Prefill (encoding the prompt) is compute‑bound:
|
|
169
|
+
`time = FLOPs / (peak_throughput × MFU)`. Decode (one token at a time) is
|
|
170
|
+
memory‑bandwidth‑bound: each step re‑reads every weight and the KV cache from
|
|
171
|
+
HBM, so `time = bytes_moved / (bandwidth × utilization)`. Each phase takes
|
|
172
|
+
`max(compute_time, memory_time)`. Energy = `time × TDP × power_utilization`.
|
|
173
|
+
The efficiency knobs (`compute_efficiency`/MFU, `mem_efficiency`,
|
|
174
|
+
`power_utilization`) are the dominant source of error and are exposed as
|
|
175
|
+
parameters.
|
|
176
|
+
|
|
177
|
+
### Validation
|
|
178
|
+
|
|
179
|
+
Bundled model parameter counts match their public model cards **exactly**:
|
|
180
|
+
|
|
181
|
+
| Model | FLOPSCalc | Published |
|
|
182
|
+
|---|---|---|
|
|
183
|
+
| GPT‑2 | 124,439,808 | 124M |
|
|
184
|
+
| Llama‑2‑7B | 6,738,415,616 | 6.74B |
|
|
185
|
+
| Mistral‑7B | 7,241,732,096 | 7.24B |
|
|
186
|
+
| Llama‑3‑8B | 8,030,261,248 | 8.03B |
|
|
187
|
+
| Qwen2.5‑7B | 7,615,716,864 | 7.61B |
|
|
188
|
+
| Gemma‑7B | 8,537,680,896 | 8.54B |
|
|
189
|
+
| Llama‑2‑70B | 68,976,648,192 | ~69B |
|
|
190
|
+
| Llama‑3‑70B | 70,553,706,496 | 70.6B |
|
|
191
|
+
|
|
192
|
+
The optional empirical mode checks FLOPs against a real forward pass:
|
|
193
|
+
|
|
194
|
+
```python
|
|
195
|
+
import flopscalc as fc
|
|
196
|
+
cfg = fc.ModelConfig(name="tiny", hidden_size=256, num_layers=4,
|
|
197
|
+
num_attention_heads=8, num_key_value_heads=4,
|
|
198
|
+
vocab_size=1024, intermediate_size=688, model_type="llama")
|
|
199
|
+
print(fc.validate(cfg, batch_size=1, sequence_length=64))
|
|
200
|
+
# {'measured_forward_flops': ..., 'analytical_forward_flops': ..., 'ratio': ~1.0, ...}
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
## Supported models, GPUs, and precisions
|
|
204
|
+
|
|
205
|
+
**Models (bundled).** GPT‑2 (base/medium/large/xl), Llama‑2 (7B/13B/70B),
|
|
206
|
+
Llama‑3 / 3.1 / 3.2 (1B/3B/8B/70B), Mistral‑7B, Qwen2.5 (0.5B/7B/72B), Phi‑2,
|
|
207
|
+
Gemma (2B/7B), Falcon‑7B. Anything else resolves via the Hugging Face Hub —
|
|
208
|
+
the parser normalizes field-name variance across architectures (GPT‑2's
|
|
209
|
+
`n_embd`, Falcon's `multi_query` and implicit `intermediate_size`, nested
|
|
210
|
+
`text_config` for multimodal models, etc.). Or pass your own `ModelConfig` /
|
|
211
|
+
dict, or `register_model(...)`. (Mixture‑of‑Experts models fetch fine but their
|
|
212
|
+
FLOPs are overcounted as dense — MoE is on the roadmap.)
|
|
213
|
+
|
|
214
|
+
**GPUs (bundled).** A100 (40/80GB, SXM/PCIe), H100 (SXM/PCIe), H200, V100, T4,
|
|
215
|
+
L4, L40S, A10G, A6000, RTX 4090, RTX 3090. Define a `GPUSpec` for anything else.
|
|
216
|
+
|
|
217
|
+
**Precisions.** fp32, tf32, fp16, bf16, fp8, int8, int4.
|
|
218
|
+
|
|
219
|
+
## Custom models and hardware
|
|
220
|
+
|
|
221
|
+
```python
|
|
222
|
+
import flopscalc as fc
|
|
223
|
+
|
|
224
|
+
cfg = fc.ModelConfig(
|
|
225
|
+
name="my-model", hidden_size=4096, num_layers=32, num_attention_heads=32,
|
|
226
|
+
num_key_value_heads=8, intermediate_size=14336, vocab_size=128256,
|
|
227
|
+
mlp_type="gated", norm_type="rmsnorm", positional="rope",
|
|
228
|
+
)
|
|
229
|
+
print(fc.estimate(cfg, gpu="H100", precision="fp8"))
|
|
230
|
+
|
|
231
|
+
gpu = fc.GPUSpec(name="MI300X", memory_gb=192, memory_bandwidth_gbps=5300,
|
|
232
|
+
tdp_watts=750, peak_tflops={"bf16": 1307, "fp16": 1307, "fp8": 2615})
|
|
233
|
+
print(fc.estimate("meta-llama/Llama-3-8B", gpu=gpu, precision="bf16"))
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
## Accuracy & limitations
|
|
237
|
+
|
|
238
|
+
Estimates are analytical, first‑order, and meant for planning and comparison,
|
|
239
|
+
not cycle‑accurate profiling. Real throughput depends heavily on the
|
|
240
|
+
implementation, kernel quality, and achieved MFU — hence the tunable efficiency
|
|
241
|
+
knobs. Activation memory is a rough estimate (inference is dominated by weights +
|
|
242
|
+
KV cache). Mixture‑of‑Experts routing, speculative decoding, tensor/pipeline
|
|
243
|
+
parallelism overheads, and multi‑GPU communication are **not** modelled yet.
|
|
244
|
+
Parameter counts and matmul FLOP counts, however, are exact.
|
|
245
|
+
|
|
246
|
+
## Roadmap
|
|
247
|
+
|
|
248
|
+
Mixture‑of‑Experts models, multi‑GPU/parallelism cost models, encoder–decoder and
|
|
249
|
+
vision‑language architectures, a curated empirical‑throughput calibration table,
|
|
250
|
+
and prompt‑vs‑generation energy attribution.
|
|
251
|
+
|
|
252
|
+
## License
|
|
253
|
+
|
|
254
|
+
MIT — see [LICENSE](LICENSE).
|