Skip to content
Merged
Changes from all commits
Commits
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
40 changes: 20 additions & 20 deletions src/hf_mem/print.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def _format_short_number(n: float) -> str:
return f"{n:.2f}P"


def _bytes_to_gb(nbytes: int) -> float:
def _bytes_to_gib(nbytes: int) -> float:
return nbytes / (1024**3)


Expand All @@ -127,33 +127,33 @@ def print_report(
for name, nested_metadata in metadata.components.items():
if len(metadata.components) > 1:
centered_rows.append(
f"{name.upper()} ({_format_short_number(nested_metadata.param_count)} PARAMS, {_bytes_to_gb(nested_metadata.bytes_count):.2f} GB)"
f"{name.upper()} ({_format_short_number(nested_metadata.param_count)} PARAMS, {_bytes_to_gib(nested_metadata.bytes_count):.2f} GiB)"
)
elif cache:
centered_rows.append(
f"MODEL ({_format_short_number(nested_metadata.param_count)} PARAMS, {_bytes_to_gb(nested_metadata.bytes_count):.2f} GB)"
f"MODEL ({_format_short_number(nested_metadata.param_count)} PARAMS, {_bytes_to_gib(nested_metadata.bytes_count):.2f} GiB)"
)
if cache:
centered_rows.append(
f"KV CACHE ({cache['max_model_len'] * cache['batch_size']} TOKENS, {_bytes_to_gb(cache['cache_size']):.2f} GB)"
f"KV CACHE ({cache['max_model_len'] * cache['batch_size']} TOKENS, {_bytes_to_gib(cache['cache_size']):.2f} GiB)"
)

data_rows = []
if cache:
data_rows.append(
f"{_bytes_to_gb(combined_total):.2f} GB ({_format_short_number(metadata.param_count)} PARAMS + KV CACHE)"
f"{_bytes_to_gib(combined_total):.2f} GiB ({_format_short_number(metadata.param_count)} PARAMS + KV CACHE)"
)
else:
data_rows.append(
f"{_bytes_to_gb(metadata.bytes_count):.2f} GB ({_format_short_number(metadata.param_count)} PARAMS)"
f"{_bytes_to_gib(metadata.bytes_count):.2f} GiB ({_format_short_number(metadata.param_count)} PARAMS)"
)
for _, nested_metadata in metadata.components.items():
for dtype, dtype_metadata in nested_metadata.dtypes.items():
data_rows.append(
f"{_bytes_to_gb(dtype_metadata.bytes_count):.2f} / {_bytes_to_gb(combined_total):.2f} GB"
f"{_bytes_to_gib(dtype_metadata.bytes_count):.2f} / {_bytes_to_gib(combined_total):.2f} GiB"
)
if cache:
data_rows.append(f"{_bytes_to_gb(cache['cache_size']):.2f} / {_bytes_to_gb(combined_total):.2f} GB")
data_rows.append(f"{_bytes_to_gib(cache['cache_size']):.2f} / {_bytes_to_gib(combined_total):.2f} GiB")

max_centered_len = max(len(r) for r in centered_rows)
max_data_len = max(len(r) for r in data_rows)
Expand All @@ -180,14 +180,12 @@ def print_report(
_print_divider(data_col_width + 1, "top")

if cache:
total_text = f"{_bytes_to_gb(combined_total):.2f} GB ({_format_short_number(metadata.param_count)} PARAMS + KV CACHE)"
total_text = f"{_bytes_to_gib(combined_total):.2f} GiB ({_format_short_number(metadata.param_count)} PARAMS + KV CACHE)"
total_bar = _make_bar(combined_total, combined_total, data_col_width)
_print_row("TOTAL MEMORY", total_text, data_col_width)
_print_row("REQUIREMENTS", total_bar, data_col_width)
else:
model_text = (
f"{_bytes_to_gb(metadata.bytes_count):.2f} GB ({_format_short_number(metadata.param_count)} PARAMS)"
)
model_text = f"{_bytes_to_gib(metadata.bytes_count):.2f} GiB ({_format_short_number(metadata.param_count)} PARAMS)"
model_bar = _make_bar(metadata.bytes_count, metadata.bytes_count, data_col_width)
_print_row("TOTAL MEMORY", model_text, data_col_width)
_print_row("REQUIREMENTS", model_bar, data_col_width)
Expand All @@ -196,14 +194,14 @@ def print_report(
if len(metadata.components) > 1:
_print_divider(data_col_width + 1, "top-continue")
_print_centered(
f"{key.upper()} ({_format_short_number(value.param_count)} PARAMS, {_bytes_to_gb(value.bytes_count):.2f} GB)",
f"{key.upper()} ({_format_short_number(value.param_count)} PARAMS, {_bytes_to_gib(value.bytes_count):.2f} GiB)",
current_len,
)
_print_divider(data_col_width + 1, "top")
elif cache:
_print_divider(data_col_width + 1, "top-continue")
_print_centered(
f"MODEL ({_format_short_number(value.param_count)} PARAMS, {_bytes_to_gb(value.bytes_count):.2f} GB)",
f"MODEL ({_format_short_number(value.param_count)} PARAMS, {_bytes_to_gib(value.bytes_count):.2f} GiB)",
current_len,
)
_print_divider(data_col_width + 1, "top")
Expand All @@ -217,16 +215,18 @@ def print_report(
]
)
for idx, (dtype, dtype_metadata) in enumerate(value.dtypes.items()):
gb_text = f"{_bytes_to_gb(dtype_metadata.bytes_count):.2f} / {_bytes_to_gb(combined_total):.2f} GB"
gib_text = (
f"{_bytes_to_gib(dtype_metadata.bytes_count):.2f} / {_bytes_to_gib(combined_total):.2f} GiB"
)
_print_row(
dtype.upper() + " " * (max_length - len(dtype)),
gb_text,
gib_text,
data_col_width,
)

bar = _make_bar(
_bytes_to_gb(dtype_metadata.bytes_count),
_bytes_to_gb(combined_total),
_bytes_to_gib(dtype_metadata.bytes_count),
_bytes_to_gib(combined_total),
data_col_width,
)
_print_row(
Expand All @@ -241,12 +241,12 @@ def print_report(
if cache:
_print_divider(data_col_width + 1, "top-continue")
_print_centered(
f"KV CACHE ({cache['max_model_len'] * cache['batch_size']} TOKENS, {_bytes_to_gb(cache['cache_size']):.2f} GB)",
f"KV CACHE ({cache['max_model_len'] * cache['batch_size']} TOKENS, {_bytes_to_gib(cache['cache_size']):.2f} GiB)",
current_len,
)
_print_divider(data_col_width + 1, "top")

kv_text = f"{_bytes_to_gb(cache['cache_size']):.2f} / {_bytes_to_gb(combined_total):.2f} GB"
kv_text = f"{_bytes_to_gib(cache['cache_size']):.2f} / {_bytes_to_gib(combined_total):.2f} GiB"
_print_row(
cache["cache_dtype"].upper() + " " * (max_length - len(cache["cache_dtype"])), # type: ignore
kv_text,
Expand Down