Skip to content

Add BF16 Softmax/SoftmaxGrad and fix accuracy issue by accum type #23

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

Merged
merged 1 commit into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion tensorflow/core/kernels/mkl_tmp_bf16_ops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ namespace tensorflow {
REGISTER_KERNEL_BUILDER( \
Name("_FusedMatMul").Device(DEVICE_CPU).TypeConstraint<T>("T"), NoOp); \
REGISTER_KERNEL_BUILDER( \
Name("BatchMatMulV2").Device(DEVICE_CPU).TypeConstraint<T>("T"), NoOp);
Name("BatchMatMulV2").Device(DEVICE_CPU).TypeConstraint<T>("T"), NoOp); \
REGISTER_KERNEL_BUILDER( \
Name("Softmax").Device(DEVICE_CPU).TypeConstraint<T>("T"), NoOp);

TF_CALL_bfloat16(REGISTER_CPU);
#undef REGISTER_CPU
Expand Down
25 changes: 24 additions & 1 deletion tensorflow/core/kernels/reduction_ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ limitations under the License.
// Functor definitions for Reduction ops, must be compilable by nvcc.

#include <iostream>
#include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
#include "tensorflow/core/framework/op_kernel.h"
#include "tensorflow/core/framework/tensor_types.h"
#include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"

namespace tensorflow {
namespace functor {
Expand Down Expand Up @@ -58,6 +58,29 @@ struct ReduceEigenImpl {
}
};

// Specialization for BF16 Reducer to fix accuracy.
// TODO: all BF16 Reducer should have specialization to fix accuracy.
#define CASTING_SPECIALIZATION(Reducer, ScalarType, IntermediateType) \
template <typename Device, typename OUT_T, typename IN_T, \
typename ReductionAxes> \
struct ReduceEigenImpl<Device, OUT_T, IN_T, ReductionAxes, \
Reducer<ScalarType>> { \
void operator()(const Device& d, OUT_T out, IN_T in, \
const ReductionAxes& reduction_axes, \
const Reducer<ScalarType>& reducer) { \
static_assert(std::is_same<ScalarType, typename OUT_T::Scalar>::value, \
""); \
Reducer<IntermediateType> intermediate_reducer; \
auto in_as_intermediate = in.template cast<IntermediateType>(); \
out.device(d) = \
in_as_intermediate.reduce(reduction_axes, intermediate_reducer) \
.template cast<ScalarType>(); \
} \
};

CASTING_SPECIALIZATION(Eigen::internal::SumReducer, bfloat16, float);
#undef CASTING_SPECIALIZATION

template <typename Device, typename OUT_T, typename IN_T,
typename ReductionAxes, typename Scalar>
struct ReduceEigenImpl<Device, OUT_T, IN_T, ReductionAxes,
Expand Down
4 changes: 4 additions & 0 deletions tensorflow/core/ops/nn_grad.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ Status SoftmaxGrad(const AttrSlice& attrs, FunctionDef* g) {
// Ret val defs
{"grad_x: T"},
// Attr defs
#if defined(INTEL_MKL) && defined(ENABLE_INTEL_MKL_BFLOAT16)
{{"T: {float, double, bfloat16}"}},
#else
{{"T: {float, double}"}},
#endif
// Nodes
// Based on _SoftmaxGrad in nn_grad.py.
{
Expand Down