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 OP&Test] add fp16 test for linspace #52161

Merged
merged 10 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 6 additions & 3 deletions paddle/phi/kernels/gpu/linspace_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ __global__ void LinspaceKernelInner(

for (; index < size; index += blockDim.x * gridDim.x) {
if (index < size / 2) {
out[index] = static_cast<T>(start + step * index);
out[index] = static_cast<T>(static_cast<double>(start) + step * index);
} else {
out[index] = static_cast<T>(stop - step * (size - index - 1));
out[index] =
static_cast<T>(static_cast<double>(stop) - step * (size - index - 1));
}
}
}
Expand Down Expand Up @@ -111,7 +112,9 @@ PD_REGISTER_KERNEL(linspace,
float,
int32_t,
int64_t,
double) {
double,
phi::dtype::float16,
phi::dtype::bfloat16) {
kernel->InputAt(0).SetBackend(phi::Backend::ALL_BACKEND);
kernel->InputAt(1).SetBackend(phi::Backend::ALL_BACKEND);
kernel->InputAt(2).SetBackend(phi::Backend::ALL_BACKEND);
Expand Down
70 changes: 47 additions & 23 deletions python/paddle/fluid/tests/unittests/test_linspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,56 +26,80 @@ class TestLinspaceOpCommonCase(OpTest):
def setUp(self):
self.op_type = "linspace"
self.python_api = paddle.linspace
dtype = 'float32'
self._set_dtype()
self.inputs = {
'Start': np.array([0]).astype(dtype),
'Stop': np.array([10]).astype(dtype),
'Start': np.array([0]).astype(self.dtype),
'Stop': np.array([10]).astype(self.dtype),
'Num': np.array([11]).astype('int32'),
}
self.attrs = {'dtype': int(core.VarDesc.VarType.FP32)}
self.attrs = {'dtype': self.attr_dtype}
self.outputs = {'Out': np.arange(0, 11).astype(self.dtype)}

self.outputs = {'Out': np.arange(0, 11).astype(dtype)}
def _set_dtype(self):
self.dtype = "float32"
self.attr_dtype = int(core.VarDesc.VarType.FP32)

def test_check_output(self):
self.check_output()


class TestLinspaceOpReverseCase(OpTest):
class TestLinspaceOpReverseCase(TestLinspaceOpCommonCase):
def setUp(self):
self.op_type = "linspace"
self.python_api = paddle.linspace
dtype = 'float32'
super().setUp()
Copy link
Contributor

@ZzSean ZzSean Mar 28, 2023

Choose a reason for hiding this comment

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

这个setUp可以直接删掉,下同

Copy link
Contributor Author

Choose a reason for hiding this comment

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

下面需要input和output跟父类不一样,这个setUp还是留着吧

Copy link
Contributor

Choose a reason for hiding this comment

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

那可以把input和ouput的赋值都抽出来,跟set_dtype结合一下

self.inputs = {
'Start': np.array([10]).astype(dtype),
'Stop': np.array([0]).astype(dtype),
'Start': np.array([10]).astype(self.dtype),
'Stop': np.array([0]).astype(self.dtype),
'Num': np.array([11]).astype('int32'),
}
self.attrs = {'dtype': int(core.VarDesc.VarType.FP32)}

self.outputs = {'Out': np.arange(10, -1, -1).astype(dtype)}
self.outputs = {'Out': np.arange(10, -1, -1).astype(self.dtype)}
Copy link
Contributor

@ZzSean ZzSean Mar 28, 2023

Choose a reason for hiding this comment

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

这里是不是多删了self.attrs,下同

Copy link
Contributor Author

Choose a reason for hiding this comment

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

self.attrs定义写在 _set_dtype 里了


def test_check_output(self):
self.check_output()


class TestLinspaceOpNumOneCase(OpTest):
class TestLinspaceOpNumOneCase(TestLinspaceOpCommonCase):
def setUp(self):
self.op_type = "linspace"
self.python_api = paddle.linspace
dtype = 'float32'
super().setUp()
self.inputs = {
'Start': np.array([10]).astype(dtype),
'Stop': np.array([0]).astype(dtype),
'Start': np.array([10]).astype(self.dtype),
'Stop': np.array([0]).astype(self.dtype),
'Num': np.array([1]).astype('int32'),
}
self.attrs = {'dtype': int(core.VarDesc.VarType.FP32)}

self.outputs = {'Out': np.array(10, dtype=dtype)}
self.outputs = {'Out': np.array(10, dtype=self.dtype)}

def test_check_output(self):
self.check_output()


class TestLinspaceOpCommonCaseFP16(TestLinspaceOpCommonCase):
def _set_dtype(self):
self.dtype = "float16"
self.attr_dtype = int(core.VarDesc.VarType.FP16)


class TestLinspaceOpReverseCaseFP16(TestLinspaceOpReverseCase):
def _set_dtype(self):
self.dtype = "float16"
self.attr_dtype = int(core.VarDesc.VarType.FP16)


class TestLinspaceOpNumOneCaseFP16(TestLinspaceOpNumOneCase):
def _set_dtype(self):
self.dtype = "float16"
self.attr_dtype = int(core.VarDesc.VarType.FP16)


# class TestLinspaceOpCommonCaseBF16(TestLinspaceOpCommonCase):
# def _set_dtype(self):
# self.dtype="bfloat16"
# self.attr_dtype = int(core.VarDesc.VarType.BF16)

# class TestLinspaceOpReverseCaseBF16(TestLinspaceOpReverseCase):
# def _set_dtype(self):
# self.dtype="bfloat16"
# self.attr_dtype = int(core.VarDesc.VarType.BF16)


class TestLinspaceAPI(unittest.TestCase):
def test_variable_input1(self):
with paddle_static_guard():
Expand Down