8000 Add assertion to align with cuda by shiyang-weng · Pull Request #153233 · pytorch/pytorch · GitHub
[go: up one dir, main page]

Skip to content

Add assertion to align with cuda #153233

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 2 commits into
base: main
Choose a base branch
from

Conversation

shiyang-weng
Copy link
Contributor
@shiyang-weng shiyang-weng commented May 9, 2025

Fixes #153137

Aligned batch_norm_cpu_out assertion to batch_norm_cuda_out.

cc @malfet

Copy link
pytorch-bot bot commented May 9, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/153233

Note: Links to docs will display an error until the docs builds have been completed.

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@shiyang-weng

This comment was marked as outdated.

@shiyang-weng
Copy link
Contributor Author

@pytorchbot label "module: error checking"

@pytorch-bot pytorch-bot bot added the module: error checking Bugs related to incorrect/lacking error checking label May 9, 2025
@shiyang-weng
Copy link
Contributor Author

@pytorchbot label "module: norms and normalization"

@shiyang-weng shiyang-weng force-pushed the wengshiy/add_bn_assert branch from e402021 to 136ab31 Compare May 12, 2025 02:19
@pytorch-bot pytorch-bot bot added the release notes: mps Release notes category label May 12, 2025
@shiyang-weng shiyang-weng requested a review from Skylion007 May 12, 2025 02:21
@shiyang-weng
Copy link
Contributor Author
import torch

print(f"PyTorch Version: {torch.__version__}")

# Common parameters for torch.batch_norm
weight_param = None
bias_param = None
is_training_param = True # Error occurs with True or False
momentum_param = 0.1
eps_param = 1e-5
cudnn_enabled_param = True # Also occurs with False on GPU

# --- Scenario 1: running_mean is Tensor, running_var is None ---
print("\n--- Scenario 1: running_mean is Tensor, running_var is None ---")
# Input tensor
input_tensor_shape = (3, 4, 5) # N, C, D*
num_features = input_tensor_shape[1]

# CPU
print("  CPU (Scenario 1):")
try:
    input_tensor_cpu = torch.randn(input_tensor_shape)
    running_mean_param_cpu = torch.randn(num_features)
    running_var_param_cpu = None

    torch.batch_norm(
        input_tensor_cpu,
        weight_param,
        bias_param,
        running_mean_param_cpu,
        running_var_param_cpu,
        is_training_param,
        momentum_param,
        eps_param,
        cudnn_enabled_param
    )
    print("    CPU: Error not triggered.")
except ValueError as e:
    print(f"    CPU Error: {e}")
    if "Expected has_running_mean == has_running_var to be true, but got false" in str(e):
        print("    CPU: Successfully triggered the target error (unexpected based on current behavior).")

# GPU
if torch.cuda.is_available():
    print("  GPU (Scenario 1):")
    try:
        input_tensor_gpu = torch.randn(input_tensor_shape).cuda()
        running_mean_param_gpu = torch.randn(num_features).cuda()
        running_var_param_gpu = None

        torch.batch_norm(
            input_tensor_gpu,
            weight_param,
            bias_param,
            running_mean_param_gpu,
            running_var_param_gpu,
            is_training_param,
            momentum_param,
            eps_param,
            cudnn_enabled_param
        )
        print("    GPU: Error not triggered (unexpected for this specific error message).")
    except ValueError as e:
        print(f"    GPU Error: {e}")
        if "Expected has_running_mean == has_running_var to be true, but got false" in str(e):
            print("    GPU: Successfully triggered the target error.")
else:
    print("  GPU (Scenario 1): CUDA not available, skipping GPU test.")

# --- Scenario 2: running_mean is None, running_var is Tensor ---
print("\n--- Scenario 2: running_mean is None, running_var is Tensor ---")

# CPU
print("  CPU (Scenario 2):")
try:
    input_tensor_cpu = torch.randn(input_tensor_shape)
    running_mean_param_cpu = None
    running_var_param_cpu = torch.randn(num_features)

    torch.batch_norm(
        input_tensor_cpu,
        weight_param,
        bias_param,
        running_mean_param_cpu,
        running_var_param_cpu,
        is_training_param,
        momentum_param,
        eps_param,
        cudnn_enabled_param
    )
    print("    CPU: Error not triggered.")
except ValueError as e:
    print(f"    CPU Error: {e}")
    if "Expected has_running_mean == has_running_var to be true, but got false" in str(e):
        print("    CPU: Successfully triggered the target error (unexpected based on current behavior).")

# GPU
if torch.cuda.is_available():
    print("  GPU (Scenario 2):")
    try:
        input_tensor_gpu = torch.randn(input_tensor_shape).cuda()
        running_mean_param_gpu = None
        running_var_param_gpu = torch.randn(num_features).cuda()

        torch.batch_norm(
            input_tensor_gpu,
            weight_param,
            bias_param,
            running_mean_param_gpu,
            running_var_param_gpu,
            is_training_param,
            momentum_param,
            eps_param,
            cudnn_enabled_param
        )
        print("    GPU: Error not triggered (unexpected for this specific error message).")
    except ValueError as e:
        print(f"    GPU Error: {e}")
        if "Expected has_running_mean == has_running_var to be true, but got false" in str(e):
            print("    GPU: Successfully triggered the target error.")
else:
    print("  GPU (Scenario 2): CUDA not available, skipping GPU test.")

Change to use ValueError.
Output is expected:
--- Scenario 1: running_mean is Tensor, running_var is None ---
CPU (Scenario 1):
CPU Error: running_mean and running_var must either both be None or neither be None
GPU (Scenario 1): CUDA not available, skipping GPU test.

--- Scenario 2: running_mean is None, running_var is Tensor ---
CPU (Scenario 2):
CPU Error: running_mean and running_var must either both be None or neither be None
GPU (Scenario 2): CUDA not available, skipping GPU test.

@colesbury colesbury added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label May 13, 2025
@shiyang-weng
Copy link
Contributor Author

Could you help review this pr? @Skylion007

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module: error checking Bugs related to incorrect/lacking error checking module: norms and normalization open source release notes: mps Release notes category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

torch.batch_norm shows inconsistent error behavior between CPU and GPU
4 participants
0