-
Notifications
You must be signed in to change notification settings - Fork 94
Improve norm kernel performance for 16-bit types on XPU #3263
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,33 +18,31 @@ | |
|
|
||
| namespace at::native::xpu { | ||
|
|
||
| // This reduction accumulates results as the type `acc_t`. By default, when | ||
| // `scalar_t` is complex, `acc_t` is the downgraded real number type. | ||
| // Otherwise, `acc_t` and `scalar_t` are the same type. | ||
| template < | ||
| typename scalar_t, | ||
| typename acc_t = typename scalar_value_type<scalar_t>::type, | ||
| typename out_t = typename scalar_value_type<scalar_t>::type> | ||
| void norm_kernel_xpu_impl(TensorIterator& iter, double p) { | ||
| constexpr int vt0 = 8; | ||
| if (p == static_cast<double>(0)) { | ||
| gpu_reduce_kernel<scalar_t, out_t>( | ||
| gpu_reduce_kernel<scalar_t, out_t, vt0>( | ||
|
Comment on lines
25
to
+28
|
||
| iter, NormZeroOps<scalar_t, acc_t, out_t>(), 0); | ||
| } else if (p == static_cast<double>(1)) { | ||
| gpu_reduce_kernel<scalar_t, out_t>( | ||
| gpu_reduce_kernel<scalar_t, out_t, vt0>( | ||
| iter, NormOneOps<scalar_t, acc_t, out_t>(), 0); | ||
| } else if (p == static_cast<double>(2)) { | ||
| gpu_reduce_kernel<scalar_t, out_t>( | ||
| gpu_reduce_kernel<scalar_t, out_t, vt0>( | ||
| iter, NormTwoOps<scalar_t, acc_t, out_t>(), 0); | ||
| } else if (p == static_cast<double>(INFINITY)) { | ||
| gpu_reduce_kernel<scalar_t, out_t>( | ||
| gpu_reduce_kernel<scalar_t, out_t, vt0>( | ||
| iter, AbsMaxOps<scalar_t, acc_t, out_t>(), 0); | ||
| } else if (p == static_cast<double>(-INFINITY)) { | ||
| gpu_reduce_kernel<scalar_t, out_t>( | ||
| gpu_reduce_kernel<scalar_t, out_t, vt0>( | ||
| iter, | ||
| AbsMinOps<scalar_t, acc_t, out_t>(), | ||
| std::numeric_limits<acc_t>::infinity()); | ||
| } else { | ||
| gpu_reduce_kernel<scalar_t, out_t>( | ||
| gpu_reduce_kernel<scalar_t, out_t, vt0>( | ||
| iter, NormOps<scalar_t, acc_t, out_t>{acc_t(p)}, 0); | ||
| } | ||
| } | ||
|
|
@@ -53,12 +51,10 @@ void norm_launch_kernel(TensorIterator& iter, double ord) { | |
| if (iter.dtype(0) == kHalf) { | ||
| return norm_kernel_xpu_impl<at::Half, float>(iter, ord); | ||
| } else if (iter.input_dtype() == kHalf && iter.dtype(0) == kFloat) { | ||
| // type promotion that does cast and reduction in a single kernel | ||
| return norm_kernel_xpu_impl<at::Half, float, float>(iter, ord); | ||
| } else if (iter.dtype(0) == kBFloat16) { | ||
| return norm_kernel_xpu_impl<at::BFloat16, float>(iter, ord); | ||
| } else if (iter.input_dtype() == kBFloat16 && iter.dtype(0) == kFloat) { | ||
| // type promotion that does cast and reduction in a single kernel | ||
| return norm_kernel_xpu_impl<at::BFloat16, float, float>(iter, ord); | ||
| } | ||
| AT_DISPATCH_FLOATING_AND_COMPLEX_TYPES(iter.input_dtype(), "norm_xpu", [&] { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new threshold scaling introduces an extra scope block solely to declare
vf. This makes the control flow slightly harder to scan in a hot configuration section. Consider removing the braces and makingvfaconstlocal (orauto) declared in the surrounding scope so the split decision reads more directly.