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

[Clean fluid] Clean unittest test_model_cast_to_bf16 #48705

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
22 changes: 11 additions & 11 deletions python/paddle/fluid/contrib/tests/test_model_cast_to_bf16.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,26 +97,26 @@ def _graph_common(self, _amp_fun, startup_prog=None):

with self.static_graph():
t_bf16 = layers.data(
name='t_bf16', shape=[size, size], dtype=np.uint16
name='t_bf16', shape=[size, size], dtype=np.int32
Copy link
Contributor

Choose a reason for hiding this comment

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

这个测试16位数值的测试,换成32还能达到目的吗

Copy link
Contributor Author

Choose a reason for hiding this comment

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

虽然此处设置为 int32,但是静态图下传入的实际上是经过 convert 后的 n_bf16 = amp.bf16.convert_float_to_uint16(n),相当于数据向上兼容了,所以还是可以达到目的

)
tt_bf16 = layers.data(
name='tt_bf16', shape=[size, size], dtype=np.uint16
name='tt_bf16', shape=[size, size], dtype=np.int32
)
t = layers.data(name='t', shape=[size, size], dtype='float32')
tt = layers.data(name='tt', shape=[size, size], dtype='float32')

ret = layers.elementwise_add(t, tt)
ret = layers.elementwise_mul(ret, t)
ret = paddle.add(t, tt)
ret = paddle.multiply(ret, t)
ret = paddle.reshape(ret, [0, 0])

with amp.bf16.bf16_guard():
ret_bf16 = layers.elementwise_add(t_bf16, tt_bf16)
ret_bf16 = layers.elementwise_mul(ret_bf16, t_bf16)
ret_bf16 = paddle.add(t_bf16, tt_bf16)
ret_bf16 = paddle.multiply(ret_bf16, t_bf16)
ret_bf16 = paddle.reshape(ret_bf16, [0, 0])

with amp.bf16.bf16_guard():
ret_fp32bf16 = layers.elementwise_add(t, tt)
ret_fp32bf16 = layers.elementwise_mul(ret_fp32bf16, t)
ret_fp32bf16 = paddle.add(t, tt)
ret_fp32bf16 = paddle.multiply(ret_fp32bf16, t)
ret_fp32bf16 = paddle.reshape(ret_fp32bf16, [0, 0])

(
Expand Down Expand Up @@ -147,11 +147,11 @@ def _graph_common(self, _amp_fun, startup_prog=None):
tt = layers.data(name='tt', shape=[size, size], dtype='float32')

with amp.bf16.bf16_guard():
ret = layers.elementwise_add(t, tt)
ret = paddle.add(t, tt)
ret = paddle.reshape(ret, [0, 0])
ret = paddle.nn.functional.elu(ret)
ret = layers.elementwise_mul(ret, t)
ret = layers.elementwise_add(ret, tt)
ret = paddle.multiply(ret, t)
ret = paddle.add(ret, tt)

static_ret_bf16 = self.get_static_graph_result(
feed={'t': n, 'tt': nn},
Expand Down