Skip to content
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

Automatically choose ModelStatistics output unit #316

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions torchinfo/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class Units(str, Enum):
__slots__ = ()

AUTO = "auto"
KILOBYTES = "K"
MEGABYTES = "M"
GIGABYTES = "G"
TERABYTES = "T"
Expand Down
1 change: 1 addition & 0 deletions torchinfo/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
Units.TERABYTES: 1e12,
Units.GIGABYTES: 1e9,
Units.MEGABYTES: 1e6,
Units.KILOBYTES: 1e3,
Units.NONE: 1,
}

Expand Down
20 changes: 11 additions & 9 deletions torchinfo/model_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,18 @@ def __repr__(self) -> str:
macs = ModelStatistics.format_output_num(
self.total_mult_adds, self.formatting.macs_units
)
input_size = self.to_megabytes(self.total_input)
output_bytes = self.to_megabytes(self.total_output_bytes)
param_bytes = self.to_megabytes(self.total_param_bytes)
total_bytes = self.to_megabytes(
input_size = self.to_readable(self.total_input)
output_bytes = self.to_readable(self.total_output_bytes)
param_bytes = self.to_readable(self.total_param_bytes)
total_bytes = self.to_readable(
self.total_input + self.total_output_bytes + self.total_param_bytes
)
summary_str += (
f"Total mult-adds{macs}\n{divider}\n"
f"Input size (MB): {input_size:0.2f}\n"
f"Forward/backward pass size (MB): {output_bytes:0.2f}\n"
f"Params size (MB): {param_bytes:0.2f}\n"
f"Estimated Total Size (MB): {total_bytes:0.2f}\n"
f"Input size ({input_size[0]}B): {input_size[1]:0.2f}\n"
f"Forward/backward pass size ({output_bytes[0]}B): {output_bytes[1]:0.2f}\n"
f"Params size ({param_bytes[0]}B): {param_bytes[1]:0.2f}\n"
f"Estimated Total Size ({total_bytes[0]}B): {total_bytes[1]:0.2f}\n"
)
summary_str += divider
return summary_str
Expand All @@ -109,7 +109,9 @@ def to_readable(num: int, units: Units = Units.AUTO) -> tuple[Units, float]:
return Units.TERABYTES, num / 1e12
if num >= 1e9:
return Units.GIGABYTES, num / 1e9
return Units.MEGABYTES, num / 1e6
if num >= 1e6:
return Units.MEGABYTES, num / 1e6
return Units.KILOBYTES, num / 1e3
return units, num / CONVERSION_FACTORS[units]

@staticmethod
Expand Down
Loading