Skip to content

[PyTorch Debug] Fixed the empty tensor bug in statistics computation #1843

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

Open
wants to merge 9 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
30 changes: 30 additions & 0 deletions tests/pytorch/debug/test_numerics.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,18 @@ def _get_tensors():
return x, weight


LOGGING_CONFIG = """logging_config:
enabled: True
layers:
layer_types: [linear]
transformer_engine:
LogTensorStats:
enabled: True
tensors: [activation, gradient, weight, output, wgrad, dgrad]
stats: [min, max, mean, std, l1_norm, l2_norm, cur_amax, dynamic_range]
"""


DISABLE_FP8_CONFIG = Template(
"""disable_fp8_config:
enabled: True
Expand All @@ -275,6 +287,24 @@ def _get_tensors():
)


@create_config_file
def run_logging_zero_numel_tensor(feature_dirs, **kwargs):
kwargs["config_file"].write(LOGGING_CONFIG)
kwargs["config_file"].flush()

_init_debug(kwargs["config_file"].name, kwargs["log_dir"], feature_dirs)

x, weight = _get_tensors()
x1 = x[:0, :]
model = _init_model(weight)
_ = _run_forward_backward(x1, model)
_ = _run_forward_backward(x, model)


def test_logging_zero_numel_tensor(feature_dirs):
run_logging_zero_numel_tensor(feature_dirs)


@pytest.mark.parametrize("fprop_fp8", all_boolean)
@pytest.mark.parametrize("dgrad_fp8", all_boolean)
@pytest.mark.parametrize("wgrad_fp8", all_boolean)
Expand Down
7 changes: 7 additions & 0 deletions transformer_engine/debug/features/utils/stats_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ def feed(self, tensor, iteration):
if self.modified[0] and not self.reduce_within_microbatch:
return

if (
tensor.numel() == 0
if hasattr(tensor, "numel")
else all((t is None or t.numel() == 0) for t in tensor.get_data_tensors())
):
return

# save stats for tensor to tmp buffer
for stat_name in self.stats_to_compute:
fn, _ = STATS[stat_name]
Expand Down
4 changes: 3 additions & 1 deletion transformer_engine/debug/features/utils/stats_computation.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def _compute_dynamic_range_top(tensor):
"""Computes the log2 of the amax of the tensor"""
tensor_abs = tensor.abs()
tensor_abs = tensor_abs[tensor_abs != 0]
if tensor_abs.numel() == 0:
return torch.inf
amax = tensor_abs.max().float()
if not amax.all():
amax = torch.tensor(1, device=tensor.device).to(torch.float)
Expand Down Expand Up @@ -125,7 +127,7 @@ def _get(buffers, stat_name):
lambda buffers: min(_get(buffers, "dynamic_range_bottom")),
),
"underflows_num": (
lambda x: (x._data == 0).sum(),
lambda x: (x.get_data_tensors()[0] == 0).sum(),
lambda buffers: sum(_get(buffers, "underflows_num")),
),
"std": (
Expand Down