diff --git a/src/lightning/pytorch/profilers/simple.py b/src/lightning/pytorch/profilers/simple.py index 50b37624bb944..74d68039d255b 100644 --- a/src/lightning/pytorch/profilers/simple.py +++ b/src/lightning/pytorch/profilers/simple.py @@ -19,7 +19,7 @@ from pathlib import Path from typing import Dict, List, Optional, Tuple, Union -import numpy as np +import torch from lightning.pytorch.profilers.profiler import Profiler @@ -79,16 +79,28 @@ def stop(self, action_name: str) -> None: def _make_report_extended(self) -> Tuple[_TABLE_DATA_EXTENDED, float, float]: total_duration = time.monotonic() - self.start_time - report = [ - (a, np.mean(d), len(d), np.sum(d), 100.0 * np.sum(d) / total_duration) - for a, d in self.recorded_durations.items() - ] + report = [] + + for a, d in self.recorded_durations.items(): + d_tensor = torch.tensor(d) + len_d = len(d) + sum_d = torch.sum(d_tensor).item() + percentage_d = 100.0 * sum_d / total_duration + + report.append((a, sum_d / len_d, len_d, sum_d, percentage_d)) + report.sort(key=lambda x: x[4], reverse=True) total_calls = sum(x[2] for x in report) return report, total_calls, total_duration def _make_report(self) -> _TABLE_DATA: - report = [(action, np.mean(d), np.sum(d)) for action, d in self.recorded_durations.items()] + report = [] + for action, d in self.recorded_durations.items(): + d_tensor = torch.tensor(d) + sum_d = torch.sum(d_tensor).item() + + report.append((action, sum_d / len(d), sum_d)) + report.sort(key=lambda x: x[1], reverse=True) return report diff --git a/src/lightning/pytorch/utilities/model_summary/model_summary.py b/src/lightning/pytorch/utilities/model_summary/model_summary.py index cde2ffe20262f..15437ec913d3b 100644 --- a/src/lightning/pytorch/utilities/model_summary/model_summary.py +++ b/src/lightning/pytorch/utilities/model_summary/model_summary.py @@ -12,12 +12,13 @@ # See the License for the specific language governing permissions and # limitations under the License. """Utilities related to model weights summary.""" + import contextlib import logging +import math from collections import OrderedDict -from typing import Any, cast, Dict, List, Optional, Tuple, Union +from typing import Any, Dict, List, Optional, Tuple, Union -import numpy as np import torch import torch.nn as nn from torch import Tensor @@ -120,9 +121,7 @@ def layer_type(self) -> str: @property def num_parameters(self) -> int: """Returns the number of parameters in this module.""" - return sum( - cast(int, np.prod(p.shape)) if not _is_lazy_weight_tensor(p) else 0 for p in self._module.parameters() - ) + return sum(math.prod(p.shape) if not _is_lazy_weight_tensor(p) else 0 for p in self._module.parameters()) class ModelSummary: @@ -392,8 +391,8 @@ def get_human_readable_count(number: int) -> str: """ assert number >= 0 labels = PARAMETER_NUM_UNITS - num_digits = int(np.floor(np.log10(number)) + 1 if number > 0 else 1) - num_groups = int(np.ceil(num_digits / 3)) + num_digits = int(math.floor(math.log10(number)) + 1 if number > 0 else 1) + num_groups = int(math.ceil(num_digits / 3)) num_groups = min(num_groups, len(labels)) # don't abbreviate beyond trillions shift = -3 * (num_groups - 1) number = number * (10**shift)