Skip to content

Commit

Permalink
[XPU] add fp16 support for assign. update xccl to 1.0.9. (#50702)
Browse files Browse the repository at this point in the history
  • Loading branch information
houj04 authored Feb 22, 2023
1 parent d884573 commit 613a3ff
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 66 deletions.
2 changes: 1 addition & 1 deletion cmake/external/xpu.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ set(XPU_API_LIB_NAME "libxpuapi.so")
set(XPU_RT_LIB_NAME "libxpurt.so")

set(XPU_BASE_DATE "20230220")
set(XPU_XCCL_BASE_VERSION "1.0.8")
set(XPU_XCCL_BASE_VERSION "1.0.9")

if(NOT DEFINED XPU_BASE_URL)
set(XPU_BASE_URL_WITHOUT_DATE
Expand Down
1 change: 1 addition & 0 deletions paddle/phi/backends/xpu/xpu2_op_list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ XPUOpMap& get_kl2_ops() {
XPUKernelSet({phi::DataType::FLOAT32,
phi::DataType::FLOAT64,
phi::DataType::INT32,
phi::DataType::FLOAT16,
phi::DataType::INT64,
phi::DataType::BOOL})},
{"assign_value", XPUKernelSet({phi::DataType::FLOAT32})},
Expand Down
3 changes: 2 additions & 1 deletion paddle/phi/kernels/assign_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -179,5 +179,6 @@ PD_REGISTER_KERNEL(assign_value,
bool,
int,
float,
int64_t) {}
int64_t,
phi::dtype::float16) {}
#endif
132 changes: 68 additions & 64 deletions python/paddle/fluid/tests/unittests/xpu/test_assign_op_xpu.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -13,72 +13,76 @@
# limitations under the License.

import sys
import unittest

import numpy as np

sys.path.append("..")
import unittest

from op_test_xpu import XPUOpTest
from xpu.get_test_cover_info import (
XPUOpTestWrapper,
create_test_class,
get_xpu_op_support_types,
)

import paddle

'''
class TestAssignOp(op_test.OpTest):
def setUp(self):
self.op_type = "assign"
x = np.random.random(size=(100, 10)).astype('float32')
self.inputs = {'X': x}
self.outputs = {'Out': x}
def test_forward(self):
if paddle.is_compiled_with_xpu():
place = paddle.XPUPlace(0)
self.check_output_with_place(place)
def test_backward(self):
if paddle.is_compiled_with_xpu():
place = paddle.XPUPlace(0)
self.check_grad_with_place(place, ['X'], 'Out')
class TestAssignOpWithLoDTensorArray(unittest.TestCase):
def test_assign_LoDTensorArray(self):
main_program = Program()
startup_program = Program()
with program_guard(main_program):
x = fluid.data(name='x', shape=[100, 10], dtype='float32')
x.stop_gradient = False
y = fluid.layers.fill_constant(
shape=[100, 10], dtype='float32', value=1)
z = paddle.add(x=x, y=y)
i = fluid.layers.fill_constant(shape=[1], dtype='int64', value=0)
init_array = paddle.tensor.array_write(x=z, i=i)
array = fluid.layers.assign(init_array)
sums = paddle.tensor.array_read(array=init_array, i=i)
mean = paddle.mean(sums)
append_backward(mean)
place = fluid.CUDAPlace(0) if core.is_compiled_with_cuda(
) else fluid.CPUPlace()
exe = fluid.Executor(place)
feed_x = np.random.random(size=(100, 10)).astype('float32')
ones = np.ones((100, 10)).astype('float32')
feed_add = feed_x + ones
res = exe.run(main_program,
feed={'x': feed_x},
fetch_list=[sums.name, x.grad_name])
np.testing.assert_allclose(res[0], feed_add)
np.testing.assert_allclose(res[1], ones / 1000.0)
class TestAssignOpError(unittest.TestCase):
def test_errors(self):
with program_guard(Program(), Program()):
# The type of input must be Variable or numpy.ndarray.
x1 = fluid.create_lod_tensor(
np.array([[-1]]), [[1]], fluid.XPUPlace(0))
self.assertRaises(TypeError, fluid.layers.assign, x1)
x2 = np.array([[2.5, 2.5]], dtype='uint8')
self.assertRaises(TypeError, fluid.layers.assign, x2)
'''

if __name__ == '__main__':
paddle.enable_static()
paddle.enable_static()


class XPUTestAssignOP(XPUOpTestWrapper):
def __init__(self):
self.op_name = 'assign'
self.use_dynamic_create_class = False

class TestAssignOPBase(XPUOpTest):
def setUp(self):
self.place = paddle.XPUPlace(0)
self.init_dtype()
self.set_case()

def set_case(self):
self.op_type = 'assign'
self.init_config()

x = np.random.random(size=self.input_shape).astype(self.dtype)
self.inputs = {'X': x}
self.attrs = {}
self.outputs = {'Out': x}

def init_dtype(self):
self.dtype = self.in_type

def test_check_output(self):
self.check_output_with_place(self.place)

def test_check_grad(self):
self.check_grad_with_place(self.place, ['X'], 'Out')

def init_config(self):
self.input_shape = (2, 5)

class XPUTestAssign1(TestAssignOPBase):
def init_config(self):
self.input_shape = [2, 768]

class XPUTestAssign2(TestAssignOPBase):
def init_config(self):
self.input_shape = [3, 8, 4096]

class XPUTestAssign3(TestAssignOPBase):
def init_config(self):
self.input_shape = [1024]

class XPUTestAssign4(TestAssignOPBase):
def init_config(self):
self.input_shape = [2, 2, 255]


support_types = get_xpu_op_support_types('assign')
for stype in support_types:
create_test_class(globals(), XPUTestAssignOP, stype)

if __name__ == "__main__":
unittest.main()

0 comments on commit 613a3ff

Please sign in to comment.