Skip to content
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

Improve batch_norm fp16 accuracy #1450

Conversation

mfeliz-cruise
Copy link
Contributor

@mfeliz-cruise mfeliz-cruise commented Nov 15, 2022

Description

Use float types for compile-time calculations around batch_norm. Improves fp16 accuracy relative to pytorch. Fixes # (issue)

Type of change

Please delete options that are not relevant and/or add your own.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist:

  • My code follows the style guidelines of this project (You can use the linters)
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas and hacks
  • I have made corresponding changes to the documentation
  • I have added tests to verify my fix or my feature
  • New and existing unit tests pass locally with my changes
  • I have added the relevant labels to my PR in so that relevant reviewers are notified

@github-actions github-actions bot added component: conversion Issues re: Conversion stage component: converters Issues re: Specific op converters component: core Issues re: The core compiler component: tests Issues re: Tests labels Nov 15, 2022
@mfeliz-cruise mfeliz-cruise changed the title Improve batch_norm fp16 accuracy (#70) Improve batch_norm fp16 accuracy Nov 15, 2022
auto calculation_beta = beta;
auto calculation_mean = mean;
auto calculation_var = var;
if (orig_dtype == torch::kHalf) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Quick question is this different than the normal pytorch behavior? If so can we add a debug message here saying that we are doing this to improve accuracy?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It looks like the cudnn implementation at least asserts that the weight is fp32 which would force similar calculations to fp32:
https://github.com/pytorch/pytorch/blob/4bfe2a24505049fa4fe43d24c2e3a5f5d99d9f00/aten/src/ATen/native/cudnn/BatchNorm.cpp#L110

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ok

Copy link
Collaborator

@narendasan narendasan left a comment

Choose a reason for hiding this comment

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

Changes LGTM

@narendasan
Copy link
Collaborator

Seems like the BN FP16 test is failing, maybe we need to change the metric for this test since I guess by definition the numerics will be different?

@mfeliz-cruise
Copy link
Contributor Author

Seems like the BN FP16 test is failing, maybe we need to change the metric for this test since I guess by definition the numerics will be different?

I lowered the bar to 2e-4 which may be more reasonable. Both pass locally for me so it may depend on GPU and the numerics of the tactic chosen by TensorRT.

@mfeliz-cruise mfeliz-cruise force-pushed the michael.feliz/improve_batchnorm_fp16_accuracy branch from 3cd10ec to 609ddc6 Compare January 3, 2023 21:24
@mfeliz-cruise
Copy link
Contributor Author

I rebased this today, but it looks like there's a CI failure on pytorch 1.14 currently:
ERROR: Could not find a version that satisfies the requirement torch==1.14.0.dev20221114+cu116

# Description

Use float types for compile-time calculations around batch_norm. Improves fp16 accuracy relative to pytorch.
Fixes # (issue)

## Type of change

Please delete options that are not relevant and/or add your own.

- Bug fix (non-breaking change which fixes an issue)
- New feature (non-breaking change which adds functionality)
- Breaking change (fix or feature that would cause existing functionality to not work as expected)
- This change requires a documentation update

# Checklist:

- [ ] My code follows the style guidelines of this project (You can use the linters)
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas and hacks
- [ ] I have made corresponding changes to the documentation
- [ ] I have added tests to verify my fix or my feature
- [ ] New and existing unit tests pass locally with my changes
- [ ] I have added the relevant labels to my PR in so that relevant reviewers are notified
@mfeliz-cruise mfeliz-cruise force-pushed the michael.feliz/improve_batchnorm_fp16_accuracy branch from 609ddc6 to 6468416 Compare January 9, 2023 20:10
@mfeliz-cruise mfeliz-cruise force-pushed the michael.feliz/improve_batchnorm_fp16_accuracy branch from 6468416 to 0b0666a Compare January 9, 2023 21:34
@peri044 peri044 removed their request for review February 13, 2023 19:35
@peri044 peri044 merged commit 3bfc052 into pytorch:main Feb 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla signed component: conversion Issues re: Conversion stage component: converters Issues re: Specific op converters component: core Issues re: The core compiler component: tests Issues re: Tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants