Created
November 28, 2025 03:37
-
-
Save sumantro93/0b60f05efb6984e9e9b18f1acc8398de to your computer and use it in GitHub Desktop.
batch-norm-unsafe-op.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import torch | |
| ... import torch.nn as nn | |
| ... | |
| ... # The kernel at line 941 computes: buffer_data[t * n_channel + c] | |
| ... # If n_channel is extremely large, t * n_channel can overflow int64_t | |
| ... | |
| ... # Demonstration of overflow math: | |
| ... int64_max = (1 << 63) - 1 | |
| ... n_channel = 2**40 # Hypothetical huge channel count | |
| ... num_threads = 8 | |
| ... | |
| ... print(f"int64_t MAX: {int64_max}") | |
| ... for t in range(num_threads): | |
| ... index = t * n_channel | |
| ... if index > int64_max: | |
| ... print(f"Thread {t}: OVERFLOW → wraps to negative!") | |
| ... | |
| ... # Empty tensor shows N=0 path: | |
| ... x = torch.randn(0, 4, 2, 2) # N = numel/n_channel = 0 | |
| ... bn = nn.BatchNorm2d(4) | |
| ... print(f"N = {x.numel()} / {x.size(1)} = {x.numel() // x.size(1)}") | |
| ... # In kernel: sum/N = 0/0 = undefined | |
| ... | |
| int64_t MAX: 9223372036854775807 | |
| N = 0 / 4 = 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment