Skip to content

Memory profiler #2061

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

Merged
merged 23 commits into from
Apr 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
8b22a68
Update
jainapurva Apr 4, 2025
04f39ef
Add profiler
jainapurva Apr 8, 2025
4b7ea5d
Add support for different models and different shapes
jainapurva Apr 10, 2025
33fa3ca
Add ruff fixes
jainapurva Apr 10, 2025
5ee6b58
Updates
jainapurva Apr 10, 2025
345a00c
Updates
jainapurva Apr 10, 2025
6e88306
Merge remote-tracking branch 'origin/bench-gpu-profiling' into model_…
jainapurva Apr 10, 2025
5895b7e
Updates
jainapurva Apr 10, 2025
bbcba36
Updates
jainapurva Apr 10, 2025
62a1e70
Memory profiler
jainapurva Apr 14, 2025
d5bdb4a
updates
jainapurva Apr 14, 2025
7677902
Merge remote-tracking branch 'origin/bench-gpu-profiling' into model_…
jainapurva Apr 14, 2025
7c15006
Updates to memory_profiler
jainapurva Apr 14, 2025
06f5ee7
Merge remote-tracking branch 'origin/main' into model_shapes_config
jainapurva Apr 18, 2025
784ec94
Added a future todo
jainapurva Apr 18, 2025
ceded86
Merge remote-tracking branch 'origin/model_shapes_config' into memory…
jainapurva Apr 18, 2025
19dcb3d
Update benchmarks/microbenchmarks/test/test_benchmark_profiler.py
jainapurva Apr 21, 2025
aadcd91
Merge remote-tracking branch 'origin/main' into memory_profiler
jainapurva Apr 23, 2025
1ae84a8
Test fix
jainapurva Apr 23, 2025
391f6d8
Merge remote-tracking branch 'origin/main' into memory_profiler
jainapurva Apr 25, 2025
b894064
Update file names
jainapurva Apr 28, 2025
965b4e8
Merge remote-tracking branch 'origin/main' into memory_profiler
jainapurva Apr 28, 2025
77e980c
Ruff fixes
jainapurva Apr 28, 2025
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
15 changes: 13 additions & 2 deletions benchmarks/microbenchmarks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ python -m benchmarks.microbenchmarks.benchmark_runner --config path/to/config.ym

```yaml
# Sample configuration for inference benchmarks
benchmark_mode: "inference"
quantization_config_recipe_names:
- "baseline"
- "int8wo"
- "int4wo-128"
- "int4wo-128-hqq"
- "float8wo"
- "float8dq-tensor"

output_dir: "benchmarks/microbenchmarks/results"

Expand All @@ -50,10 +51,20 @@ model_params:
compile: "max-autotune" # Options: "default", "max-autotune", "false"
device: "cuda" # Options: "cuda", "mps", "xpu", "cpu"
model_type: "linear" # Options: "linear", "ln_linear_sigmoid"
enable_profiler: true # Enable standard profiling
enable_memory_profiler: true # Enable CUDA memory profiling
```

## Configuration Options

### Profiling Options
- `enable_profiler`: Enable standard PyTorch profiling (default: false)
- `enable_memory_profiler`: Enable CUDA memory profiling (default: false)
- Only works when device is set to "cuda"
- Generates memory snapshots before and after inference
- Creates visualizations of memory usage
- Outputs are saved in the memory_profiler subdirectory

### Quantization Methods
Currently, quantization string is in same format as the one being passed in llama/generate.py.
- `baseline`: No quantization
Expand Down
47 changes: 44 additions & 3 deletions benchmarks/microbenchmarks/benchmark_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
- run() function is the main entry point for running inference benchmarks.
"""

import os
from copy import deepcopy
from pathlib import Path

import torch

from benchmarks.microbenchmarks.profiler import (
generate_memory_profile,
generate_model_profile,
visualize_memory_profile,
)
from benchmarks.microbenchmarks.utils import (
BenchmarkConfig,
Expand Down Expand Up @@ -98,11 +101,49 @@ def run(config: BenchmarkConfig) -> BenchmarkResult:
if config.enable_profiler:
print("Running profiler...")
try:
result.profiler_json_path = generate_model_profile(
m_copy, input_data, config.profiler_file_name
profiler_json_path = generate_model_profile(
model=m_copy,
input_data=input_data,
profile_file_path=os.path.join(
config.output_dir,
"profiler",
f"{config._file_name}_profile.json",
),
)
result.profiler_json_path = profiler_json_path
except Exception as e:
print(f"Error running profiler for {config.name} with error: {e}")
print(f"Error running profiler: {e}")

# Run memory profiler if enabled
if config.enable_memory_profiler:
print("Running memory profiler...")
try:
result.memory_profile_path, result.memory_stats = (
generate_memory_profile(
model=m_copy,
input_data=input_data,
profile_file_path=os.path.join(
config.output_dir,
"memory_profiler/pickle",
f"{config._file_name}_memory_profile.pickle",
),
)
)

if result.memory_profile_path:
result.memory_visualization_path = visualize_memory_profile(
result.memory_profile_path
)
except ValueError as e:
if "not enough values to unpack" in e:
print(
"Failed due to existing bugs, re-run the code to generate memory profile. Please raise an issue if it persists."
)
except Exception as e:
print(f"Error running memory profiler: {e}")
import traceback

traceback.print_exc()

return result
except Exception as e:
Expand Down
Loading
Loading