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

Closed

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.

✅ No Failures

As of commit 5fd22bf with merge base c1055f4 (image):
💚 Looks good so far! There are no failures yet. 💚

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 py 8000 torch-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

@shiyang-weng
Copy link
Contributor Author

Could someone help review this pr?
@Skylion007 @colesbury @malfet

@malfet
Copy link
Contributor
malfet commented May 23, 2025

@shiyang-weng do you mind adding the test?

Copy link
Contributor
@malfet malfet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it'll pass the tests, sure

@shiyang-weng
Copy link
Contributor Author

@shiyang-weng do you mind adding the test?

This is error checking issue. Not related to functionality and performance
Is it not need add unit tests for this issue?

@shiyang-weng
Copy link
Contributor Author

@pytorchbot merge

@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label May 23, 2025
@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ciflow/trunk Trigger trunk jobs on your pull request Merged 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
6 participants
0