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

[AMP Prim OP]support dropout prim ops bfloat16 dtype #54175

Merged
Merged
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
117 changes: 91 additions & 26 deletions test/legacy_test/test_dropout_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,6 @@ def setUp(self):
'is_test': True,
}
self.outputs = {'Out': out}
# Because prim op compare res with dygraph
# when p = 0 dropout api return x,in dygraph mode x_grad = out_grad,
# but in static mode x_grad = []
self.enable_check_static_comp = False

def init_test_case(self):
self.input_size = [32, 64]
Expand Down Expand Up @@ -362,22 +358,10 @@ def setUp(self):
}

def test_check_output(self):
self.check_output()
self.check_output(check_prim=True)

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

def test_check_output_for_prim(self):
# greater_equal does't support bfloat16 in cpu
if core.is_compiled_with_cuda():
self.check_output_with_place(core.CUDAPlace(0))

def test_check_grad_for_prim(self):
# greater_equal does't support bfloat16 in cpu
if core.is_compiled_with_cuda():
self.check_grad_with_place(
core.CUDAPlace(0), ['X'], 'Out', only_check_prim=True
)
self.check_grad(['X'], 'Out', check_prim=True)


class TestDropoutOpWithSeedOnCPUPlace(unittest.TestCase):
Expand Down Expand Up @@ -1451,8 +1435,9 @@ def forward(
training=True,
mode="upscale_in_train",
):
y = paddle.assign(x)
out = paddle.nn.functional.dropout(
x=x, p=p, axis=axis, training=training, mode=mode
x=y, p=p, axis=axis, training=training, mode=mode
)
return out

Expand All @@ -1476,6 +1461,16 @@ def apply_to_static(net, use_cinn):
'float32',
places,
),
(
'bfp16',
np.random.rand(100000),
0.3,
False,
'upscale_in_train',
1002,
'bfloat16',
places,
),
(
'fp64',
np.random.rand(100000),
Expand Down Expand Up @@ -1506,6 +1501,16 @@ def apply_to_static(net, use_cinn):
'float32',
places,
),
(
'p=1.0,dtype=bfp16',
np.random.rand(100000),
1.0,
True,
'upscale_in_train',
1002,
'bfloat16',
places,
),
(
'p=1.0,test=False',
np.random.rand(100000),
Expand All @@ -1517,15 +1522,35 @@ def apply_to_static(net, use_cinn):
places,
),
(
'p=0.0',
'p=1.0,test=False,dtype=bfp16',
np.random.rand(100000),
1.0,
False,
'upscale_in_train',
1002,
'bfloat16',
places,
),
(
'p=0.0',
np.random.rand(100000),
0,
True,
'upscale_in_train',
1002,
'float32',
places,
),
(
'p=0.0,dtype=bfp16',
np.random.rand(100000),
0,
True,
'upscale_in_train',
1002,
'bfloat16',
places,
),
(
'downgrade_train',
np.random.rand(100000),
Expand All @@ -1536,6 +1561,16 @@ def apply_to_static(net, use_cinn):
'float32',
places,
),
(
'downgrade_train,dtype=bfp16',
np.random.rand(100000),
0.5,
False,
'downscale_in_infer',
1002,
'bfloat16',
places,
),
(
'fp32_cpu',
np.random.rand(100000),
Expand Down Expand Up @@ -1571,7 +1606,11 @@ def apply_to_static(net, use_cinn):
class TestCompositeDropout(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.x = cls.x.astype(cls.dtype)
cls.x = (
cls.x.astype(cls.dtype)
if cls.dtype != "bfloat16"
else cls.x.astype("float32")
)
core._set_prim_all_enabled(True)

@classmethod
Expand All @@ -1596,12 +1635,18 @@ def get_eager_desire(self, place):
paddle.set_device("gpu")
core.set_prim_eager_enabled(False)
input_ = paddle.to_tensor(
data=self.x, dtype=self.dtype, place=place, stop_gradient=False
data=self.x,
dtype=self.dtype if self.dtype != "bfloat16" else "float32",
place=place,
stop_gradient=False,
)
output = paddle.nn.functional.dropout(
input_, self.p, training=(not self.is_test), mode=self.mode
)
grad = paddle.grad(output, input_)
if self.dtype == "bfloat16":
output = paddle.cast(output, "float32")
grad[0] = paddle.cast(grad[0], "float32")
return output, grad[0]

def test_static_comp(self):
Expand All @@ -1614,18 +1659,26 @@ def test_static_comp(self):
mp, sp = paddle.static.Program(), paddle.static.Program()
with paddle.static.program_guard(mp, sp):
input_ = paddle.static.data(
'x', shape=self.x.shape, dtype=self.x.dtype
'x',
shape=self.x.shape,
dtype=self.x.dtype
if self.dtype != "bfloat16"
else "float32",
)
input_.stop_gradient = False
y = paddle.assign(input_)
output = paddle.nn.functional.dropout(
input_,
y,
self.p,
training=(not self.is_test),
mode=self.mode,
)
if core._is_fwd_prim_enabled():
primapi.to_prim(mp.blocks)
grad = paddle.static.gradients(output, input_)[0]
if self.dtype == "bfloat16":
output = paddle.cast(output, "float32")
grad = paddle.cast(grad, "float32")
exe = paddle.static.Executor(place)
exe.run(sp)
fwd, rev = exe.run(
Expand Down Expand Up @@ -1662,14 +1715,20 @@ def test_jit_comp(self):
paddle.set_device("gpu")
paddle.seed(self.seed)
input_ = paddle.to_tensor(
data=self.x, dtype=self.dtype, place=place, stop_gradient=False
data=self.x,
dtype=self.dtype if self.dtype != "bfloat16" else "float32",
place=place,
stop_gradient=False,
)
net = PrimNet()
net = apply_to_static(net, False)
output = net(
input_, self.p, training=(not self.is_test), mode=self.mode
)
grad = paddle.grad(output, input_)
if self.dtype == "bfloat16":
output = paddle.cast(output, "float32")
grad[0] = paddle.cast(grad[0], "float32")
fwd_actual.append(output.numpy())
rev_actual.append(grad[0].numpy())
for i in range(len(self.places)):
Expand All @@ -1696,14 +1755,20 @@ def test_jit_comp_with_cinn(self):
paddle.set_device("gpu")
paddle.seed(self.seed)
input_ = paddle.to_tensor(
data=self.x, dtype=self.dtype, place=place, stop_gradient=False
data=self.x,
dtype=self.dtype if self.dtype != "bfloat16" else "float32",
place=place,
stop_gradient=False,
)
net = PrimNet()
net = apply_to_static(net, True)
output = net(
input_, self.p, training=(not self.is_test), mode=self.mode
)
grad = paddle.grad(output, input_)
if self.dtype == "bfloat16":
output = paddle.cast(output, "float32")
grad[0] = paddle.cast(grad[0], "float32")
fwd_actual.append(output.numpy())
rev_actual.append(grad[0].numpy())
i = 0
Expand Down