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

【Hackathon + No.59】 addmm 等算子 FP16/BF16算子及单测完善 #51664

Closed
wants to merge 6 commits into from
Closed
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
1 change: 1 addition & 0 deletions paddle/phi/kernels/addmm_grad_kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ limitations under the License. */
#pragma once

#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/core/device_context.h"

namespace phi {

Expand Down
2 changes: 2 additions & 0 deletions paddle/phi/kernels/addmm_kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ limitations under the License. */

#pragma once

#include "paddle/phi/common/scalar.h"
#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/core/device_context.h"

namespace phi {

Expand Down
1 change: 1 addition & 0 deletions paddle/phi/kernels/angle_grad_kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#pragma once

#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/core/device_context.h"

namespace phi {

Expand Down
2 changes: 2 additions & 0 deletions paddle/phi/kernels/angle_kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
#endif
#include <math.h>

#include "paddle/phi/common/scalar.h"
#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/core/device_context.h"

namespace phi {

Expand Down
1 change: 1 addition & 0 deletions paddle/phi/kernels/bmm_kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#pragma once

#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/common/scalar.h"

namespace phi {

Expand Down
2 changes: 1 addition & 1 deletion paddle/phi/kernels/cpu/addmm_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ limitations under the License. */
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/impl/addmm_kernel_impl.h"

PD_REGISTER_KERNEL(addmm, CPU, ALL_LAYOUT, phi::AddmmKernel, float, double) {}
PD_REGISTER_KERNEL(addmm, CPU, ALL_LAYOUT, phi::AddmmKernel, float, double,phi::dtype::float16,phi::dtype::bfloat16) {}
3 changes: 1 addition & 2 deletions paddle/phi/kernels/cpu/angle_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ PD_REGISTER_KERNEL(angle,
phi::AngleKernel,
float,
double,
phi::dtype::complex<float>,
phi::dtype::complex<double>) {
,phi::dtype::float16,phi::dtype::bfloat16) {
kernel->OutputAt(0).SetDataType(phi::DataType::UNDEFINED);
}
6 changes: 5 additions & 1 deletion paddle/phi/kernels/cpu/bmm_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@
#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/impl/bmm_kernel_impl.h"
#include "paddle/phi/core/device_context.h"
#include "paddle/phi/common/data_type.h"
#include "paddle/phi/common/float16.h"
#include "paddle/phi/common/bfloat16.h"

PD_REGISTER_KERNEL(bmm, CPU, ALL_LAYOUT, phi::BmmKernel, float, double) {}
PD_REGISTER_KERNEL(bmm, CPU, ALL_LAYOUT, phi::BmmKernel, float, double,phi::dtype::float16,phi::dtype::bfloat16) {}
1 change: 1 addition & 0 deletions paddle/phi/kernels/cpu/index_add_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ PD_REGISTER_KERNEL(index_add,
float,
double,
phi::dtype::float16,
phi::dtype::bfloat16,
int,
int64_t) {}
4 changes: 3 additions & 1 deletion paddle/phi/kernels/cpu/index_sample_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,6 @@ PD_REGISTER_KERNEL(index_sample,
float,
double,
int,
int64_t) {}
int64_t,
phi::dtype::float16,
phi::dtype::bfloat16) {}
5 changes: 4 additions & 1 deletion paddle/phi/kernels/cpu/put_along_axis_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,7 @@ PD_REGISTER_KERNEL(put_along_axis,
double,
int,
uint8_t,
int64_t) {}
int64_t,
phi::dtype::float16,
phi::dtype::bfloat16
) {}
5 changes: 4 additions & 1 deletion paddle/phi/kernels/cpu/take_along_axis_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,7 @@ PD_REGISTER_KERNEL(take_along_axis,
double,
int,
uint8_t,
int64_t) {}
int64_t,
phi::dtype::float16,
phi::dtype::bfloat16
) {}
3 changes: 2 additions & 1 deletion paddle/phi/kernels/gpu/addmm_grad_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License. */
#include "paddle/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/impl/addmm_grad_kernel_impl.h"
#include "paddle/phi/common/data_type.h"

PD_REGISTER_KERNEL(
addmm_grad, GPU, ALL_LAYOUT, phi::AddmmGradKernel, float, double) {}
addmm_grad, GPU, ALL_LAYOUT, phi::AddmmGradKernel, float, double,phi::dtype::float16,phi::dtype::bfloat16) {}
10 changes: 9 additions & 1 deletion paddle/phi/kernels/gpu/addmm_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,12 @@ limitations under the License. */
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/impl/addmm_kernel_impl.h"

PD_REGISTER_KERNEL(addmm, GPU, ALL_LAYOUT, phi::AddmmKernel, float, double) {}
#include "paddle/phi/core/device_context.h"
#include "paddle/phi/common/data_type.h"
#include "paddle/phi/common/float16.h"
#include "paddle/phi/common/bfloat16.h"


// PD_REGISTER_KERNEL(addmm, GPU, ALL_LAYOUT, phi::AddmmKernel, float, double) {}

PD_REGISTER_KERNEL(addmm, GPU, ALL_LAYOUT, phi::AddmmKernel, float, double,phi::dtype::float16,phi::dtype::bfloat16) {}
1 change: 1 addition & 0 deletions paddle/phi/kernels/gpu/angle_grad_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "paddle/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/common/data_type.h"

PD_REGISTER_KERNEL(angle_grad,
GPU,
Expand Down
10 changes: 8 additions & 2 deletions paddle/phi/kernels/gpu/angle_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,19 @@
#include "paddle/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/core/kernel_registry.h"

#include "paddle/phi/core/device_context.h"
#include "paddle/phi/common/data_type.h"
#include "paddle/phi/common/float16.h"
#include "paddle/phi/common/bfloat16.h"


PD_REGISTER_KERNEL(angle,
GPU,
ALL_LAYOUT,
phi::AngleKernel,
float,
double,
phi::dtype::complex<float>,
phi::dtype::complex<double>) {
phi::dtype::float16,
phi::dtype::bfloat16) {
kernel->OutputAt(0).SetDataType(phi::DataType::UNDEFINED);
}
8 changes: 7 additions & 1 deletion paddle/phi/kernels/gpu/bmm_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,11 @@
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/impl/bmm_kernel_impl.h"

#include "paddle/phi/core/device_context.h"
#include "paddle/phi/common/data_type.h"
#include "paddle/phi/common/float16.h"
#include "paddle/phi/common/bfloat16.h"


PD_REGISTER_KERNEL(
bmm, GPU, ALL_LAYOUT, phi::BmmKernel, float, double, phi::dtype::float16) {}
bmm, GPU, ALL_LAYOUT, phi::BmmKernel, float, double, phi::dtype::float16,phi::dtype::bfloat16) {}
10 changes: 8 additions & 2 deletions paddle/phi/kernels/gpu/index_add_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
#include "paddle/phi/backends/gpu/gpu_launch_config.h"
#include "paddle/phi/backends/gpu/gpu_primitives.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/core/utils/data_type.h"
// #include "paddle/phi/core/utils/data_type.h"

#include "paddle/phi/common/bfloat16.h"
#include "paddle/phi/common/data_type.h"
#include "paddle/phi/common/float16.h"
#include "paddle/phi/core/device_context.h"

DECLARE_bool(cudnn_deterministic);

Expand Down Expand Up @@ -122,4 +127,5 @@ PD_REGISTER_KERNEL(index_add,
double,
phi::dtype::float16,
int,
int64_t) {}
int64_t,
phi::dtype::bfloat16) {}
6 changes: 6 additions & 0 deletions paddle/phi/kernels/gpu/index_sample_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
#include "paddle/phi/core/utils/data_type.h"
#include "paddle/phi/kernels/funcs/math_function.h"

#include "paddle/phi/core/device_context.h"
#include "paddle/phi/common/data_type.h"
#include "paddle/phi/common/float16.h"
#include "paddle/phi/common/bfloat16.h"


namespace phi {

namespace {
Expand Down
9 changes: 8 additions & 1 deletion paddle/phi/kernels/gpu/put_along_axis_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
#include "paddle/phi/core/utils/data_type.h"
#include "paddle/phi/kernels/funcs/gather_scatter_functor.h"

#include "paddle/phi/core/device_context.h"
#include "paddle/phi/common/data_type.h"
#include "paddle/phi/common/float16.h"
#include "paddle/phi/common/bfloat16.h"


namespace phi {

template <typename T, typename Context>
Expand Down Expand Up @@ -82,4 +88,5 @@ PD_REGISTER_KERNEL(put_along_axis,
double,
int64_t,
int,
phi::dtype::float16) {}
phi::dtype::float16,
phi::dtype::bfloat16) {}
10 changes: 9 additions & 1 deletion paddle/phi/kernels/gpu/take_along_axis_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
#include "paddle/phi/core/utils/data_type.h"
#include "paddle/phi/kernels/funcs/gather_scatter_functor.h"

#include "paddle/phi/core/device_context.h"
#include "paddle/phi/common/data_type.h"
#include "paddle/phi/common/float16.h"
#include "paddle/phi/common/bfloat16.h"


namespace phi {

template <typename T, typename Context>
Expand Down Expand Up @@ -54,4 +60,6 @@ PD_REGISTER_KERNEL(take_along_axis,
double,
int64_t,
int,
phi::dtype::float16) {}
phi::dtype::float16,
phi::dtype::bfloat16,
) {}
1 change: 1 addition & 0 deletions paddle/phi/kernels/impl/addmm_kernel_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ void AddmmKernel(const Context& dev_ctx,
alpha,
x.data<T>(),
x_dims[1],
reinterpret_cast<const Eigen::half*>(y.data<float16_t>()),
y.data<T>(),
y_dims[1],
beta,
Expand Down
1 change: 1 addition & 0 deletions paddle/phi/kernels/index_add_kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#pragma once

#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/common/scalar.h"

namespace phi {

Expand Down
1 change: 1 addition & 0 deletions paddle/phi/kernels/index_sample_kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#pragma once

#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/common/scalar.h"

namespace phi {

Expand Down
1 change: 1 addition & 0 deletions paddle/phi/kernels/put_along_axis_kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <string>

#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/common/scalar.h"

namespace phi {

Expand Down
70 changes: 70 additions & 0 deletions python/paddle/fluid/tests/unittests/test_activation_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,76 @@ def test_errors(self):
F.hardtanh(x_fp16)


class TestHardtanhOp(OpTest):
def setUp(self):
self.op_type = "hardtanh"
self.init_input_output()
self.attrs = {'min': self.min, 'max': self.max}
self.inputs = {'X': self.x}
self.outputs = {'Out': self.out}

def init_input_output(self):
self.x = np.random.uniform(-1, 1, [10, 10]).astype("float32")
self.min = -1
self.max = 1
self.out = np.clip(self.x, self.min, self.max)

def test_check_output(self):
self.check_output()

def test_check_grad_normal(self):
self.check_grad(['X'], 'Out')


class TestHardtanhFp16Op(OpTest):
def setUp(self):
self.op_type = "hardtanh"
self.init_input_output()
self.attrs = {'min': self.min, 'max': self.max}
self.inputs = {'X': self.x.astype(np.float16)}
self.outputs = {'Out': self.out.astype(np.float16)}

def init_input_output(self):
self.x = np.random.uniform(-1, 1, [10, 10]).astype(np.float16)
self.min = -1
self.max = 1
self.out = np.clip(self.x, self.min, self.max)

def test_check_output(self):
rtol = 1e-3 if self.dtype == np.float16 else 1e-4
self.check_output(rtol=rtol)

def test_check_grad_normal(self):
rtol = 1e-3 if self.dtype == np.float16 else 1e-4
self.check_grad(['X'], 'Out', rtol=rtol)


class TestHardtanhBF16Op(OpTest):
def setUp(self):
self.op_type = "hardtanh"
self.init_input_output()
self.attrs = {'min': self.min, 'max': self.max}
self.inputs = {'X': convert_float_to_uint16(self.x)}
self.outputs = {'Out': convert_float_to_uint16(self.out)}

def init_input_output(self):
self.x = np.random.uniform(-1, 1, [10, 10]).astype(np.float32)
self.min = -1
self.max = 1
self.out = np.clip(self.x, self.min, self.max)

def test_check_output(self):
self.check_output(atol=0.0,rtol=0.8)

def test_check_grad_normal(self):
self.decr_ratio=0.8
self.check_grad(['X'], 'Out', atol=0.0,rtol=0.8)






def ref_softshrink(x, threshold=0.5):
out = np.copy(x)
out = (out < -threshold) * (out + threshold) + (out > threshold) * (
Expand Down
Loading