| 1 |
# Copyright 2025 The HuggingFace Team. All rights reserved. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
import argparse |
| 16 |
import json |
| 17 |
|
| 18 |
import matplotlib.pyplot as plt |
| 19 |
|
| 20 |
|
| 21 |
def parse_args(): |
| 22 |
parser = argparse.ArgumentParser() |
| 23 |
parser.add_argument("--dir", type=str, help="Directory containing the memory usage data") |
| 24 |
parser.add_argument( |
| 25 |
"--memory_threshold", |
| 26 |
type=int, |
| 27 |
default=0, |
| 28 |
help="Memory threshold to filter data that is below this value (only filters 1st `--filter_partition` of the points which should roughly correspond to the model loading)", |
| 29 |
) |
| 30 |
parser.add_argument( |
| 31 |
"--filter_partition", |
| 32 |
type=float, |
| 33 |
default=1 / 3, |
| 34 |
help="Partition to drop data from that are below the memory threshold", |
| 35 |
) |
| 36 |
return parser.parse_args() |
| 37 |
|
| 38 |
|
| 39 |
def filter_data(data, memory_threshold, filter_partition, key): |
| 40 |
timestamps = data["timestamps"] |
| 41 |
memory = data[key] |
| 42 |
|
| 43 |
mid_point = int(len(timestamps) * filter_partition) |
| 44 |
filtered_times = [] |
| 45 |
filtered_memory = [] |
| 46 |
for i, (t, m) in enumerate(zip(timestamps, memory)): |
| 47 |
if i < mid_point and m < memory_threshold: |
| 48 |
continue |
| 49 |
filtered_times.append(t) |
| 50 |
filtered_memory.append(m) |
| 51 |
return filtered_times, filtered_memory |
| 52 |
|
| 53 |
|
| 54 |
def compare_memory_usage(data, labels, memory_threshold, filter_partition): |
| 55 |
plt.style.use("seaborn-v0_8") |
| 56 |
colors = ["#2ecc71", "#e74c3c", "#3498db", "#f1c40f"] |
| 57 |
|
| 58 |
fig1, ax1 = plt.subplots(figsize=(15, 5)) |
| 59 |
for data_item, label, color in zip(data, labels, colors): |
| 60 |
timestamps, allocated = filter_data(data_item, memory_threshold, filter_partition, "allocated_memory") |
| 61 |
ax1.plot(timestamps, allocated, label=label, color=color, linewidth=2) |
| 62 |
|
| 63 |
ax1.set_xlabel("Time (s)", fontsize=12) |
| 64 |
ax1.set_ylabel("Allocated Memory (GB)", fontsize=12) |
| 65 |
ax1.set_title("Allocated Memory Usage Over Time", fontsize=14, pad=15) |
| 66 |
ax1.grid(True, linestyle="--", alpha=0.7) |
| 67 |
ax1.legend(frameon=True, fancybox=True, shadow=True, fontsize=10) |
| 68 |
ax1.spines["top"].set_visible(False) |
| 69 |
ax1.spines["right"].set_visible(False) |
| 70 |
plt.tight_layout() |
| 71 |
|
| 72 |
fig2, ax2 = plt.subplots(figsize=(15, 5)) |
| 73 |
for data_item, label, color in zip(data, labels, colors): |
| 74 |
timestamps, reserved = filter_data(data_item, memory_threshold, filter_partition, "reserved_memory") |
| 75 |
ax2.plot(timestamps, reserved, label=label, color=color, linewidth=2) |
| 76 |
|
| 77 |
ax2.set_xlabel("Time (s)", fontsize=12) |
| 78 |
ax2.set_ylabel("Reserved Memory (GB)", fontsize=12) |
| 79 |
ax2.set_title("Reserved Memory Usage Over Time", fontsize=14, pad=15) |
| 80 |
ax2.grid(True, linestyle="--", alpha=0.7) |
| 81 |
ax2.legend(frameon=True, fancybox=True, shadow=True, fontsize=10) |
| 82 |
ax2.spines["top"].set_visible(False) |
| 83 |
ax2.spines["right"].set_visible(False) |
| 84 |
plt.tight_layout() |
| 85 |
|
| 86 |
return fig1, fig2 |
| 87 |
|
| 88 |
|
| 89 |
if __name__ == "__main__": |
| 90 |
args = parse_args() |
| 91 |
DIR = args.dir |
| 92 |
with open(f"{DIR}/torch_optimizer_before_fsdp_not_fixed_memory_usage.json") as f: |
| 93 |
optimizer_before_fsdp_not_fixed = json.load(f) |
| 94 |
|
| 95 |
with open(f"{DIR}/torch_optimizer_after_fsdp_memory_usage.json") as f: |
| 96 |
optimizer_after_fsdp = json.load(f) |
| 97 |
|
| 98 |
with open(f"{DIR}/torch_optimizer_before_fsdp_fixed_memory_usage.json") as f: |
| 99 |
optimizer_before_fsdp_fixed = json.load(f) |
| 100 |
|
| 101 |
with open(f"{DIR}/accelerate_memory_usage.json") as f: |
| 102 |
accelerate = json.load(f) |
| 103 |
|
| 104 |
data = [optimizer_before_fsdp_not_fixed, optimizer_before_fsdp_fixed, optimizer_after_fsdp, accelerate] |
| 105 |
labels = [ |
| 106 |
"Optimizer Before FSDP (w/o fix)", |
| 107 |
"Optimizer Before FSDP (w/ fix)", |
| 108 |
"Optimizer After FSDP", |
| 109 |
"Accelerate", |
| 110 |
] |
| 111 |
|
| 112 |
fig1, fig2 = compare_memory_usage(data, labels, args.memory_threshold, args.filter_partition) |
| 113 |
fig1.savefig(f"{DIR}/allocated_memory.png") |
| 114 |
fig2.savefig(f"{DIR}/reserved_memory.png") |