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

【complex op】 add complex support for partial_concat #58336

Merged
merged 1 commit into from
Nov 9, 2023
Merged
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
8 changes: 6 additions & 2 deletions paddle/fluid/operators/partial_concat_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,16 @@ PD_REGISTER_STRUCT_KERNEL(partial_concat,
float,
double,
int,
int64_t) {}
int64_t,
phi::dtype::complex<float>,
phi::dtype::complex<double>) {}
PD_REGISTER_STRUCT_KERNEL(partial_concat_grad,
CPU,
ALL_LAYOUT,
ops::PartialConcatGradientOpKernel,
float,
double,
int,
int64_t) {}
int64_t,
phi::dtype::complex<float>,
phi::dtype::complex<double>) {}
8 changes: 6 additions & 2 deletions paddle/fluid/operators/partial_concat_op.cu
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,9 @@ PD_REGISTER_STRUCT_KERNEL(partial_concat,
double,
int,
int64_t,
plat::float16) {}
plat::float16,
phi::dtype::complex<float>,
phi::dtype::complex<double>) {}
PD_REGISTER_STRUCT_KERNEL(partial_concat_grad,
GPU,
ALL_LAYOUT,
Expand All @@ -249,4 +251,6 @@ PD_REGISTER_STRUCT_KERNEL(partial_concat_grad,
double,
int,
int64_t,
plat::float16) {}
plat::float16,
phi::dtype::complex<float>,
phi::dtype::complex<double>) {}
12 changes: 10 additions & 2 deletions python/paddle/incubate/layers/nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ def partial_concat(input, start_index=0, length=-1):

Args:
input(list): List of input Tensors with data type float32, float64, int32,
int64.
int64, complex64, complex128.
start_index(int32, optional): The start index of each instance for partial concatenation.
Default is 0.
length(int32, optional): The length of each instance for partial concatenation. Default is -1.
Expand Down Expand Up @@ -560,7 +560,15 @@ def partial_concat(input, start_index=0, length=-1):
check_variable_and_dtype(
x,
'input[' + str(id) + ']',
['float16', 'float32', 'float64', 'int32', 'int64'],
[
'float16',
'float32',
'float64',
'int32',
'int64',
'complex64',
'complex128',
],
'partial_concat',
)
check_type(start_index, 'start_index', (int), 'partial_concat')
Expand Down
81 changes: 81 additions & 0 deletions test/legacy_test/test_partial_concat_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ def setUp(self):
np.random.random((self.batch_size, self.column)).astype(self.dtype)
for num in range(self.var_num)
]
if self.dtype == np.complex64 or self.dtype == np.complex128:
self.vars = [
(
np.random.uniform(-1, 1, (self.batch_size, self.column))
+ 1j
* np.random.uniform(-1, 1, (self.batch_size, self.column))
).astype(self.dtype)
for num in range(self.var_num)
]
self.inputs = {'X': list(zip(self.var_names, self.vars))}
self.attrs = {'start_index': self.start_index, 'length': self.length}
y = np_partial_concat(self.vars[:], self.start_index, self.length)
Expand Down Expand Up @@ -98,5 +107,77 @@ def init_para(self):
self.var_num = 1


class TestPartialConcatOp2_Complex64(TestPartialConcatOp):
def init_para(self):
self.batch_size = random.randint(1, 10)
self.column = random.randint(101, 200)
self.start_index = -5
self.length = -1
self.var_num = 3

def init_kernel_type(self):
self.dtype = np.complex64


class TestPartialConcatOp3_Complex64(TestPartialConcatOp):
def init_para(self):
self.batch_size = random.randint(1, 10)
self.column = random.randint(101, 200)
self.start_index = 10
self.length = 20
self.var_num = 2

def init_kernel_type(self):
self.dtype = np.complex64


class TestPartialConcatOp4_Complex64(TestPartialConcatOp):
def init_para(self):
self.batch_size = random.randint(1, 10)
self.column = random.randint(101, 200)
self.start_index = -1
self.length = -1
self.var_num = 1

def init_kernel_type(self):
self.dtype = np.complex64


class TestPartialConcatOp2_Complex128(TestPartialConcatOp):
def init_para(self):
self.batch_size = random.randint(1, 10)
self.column = random.randint(101, 200)
self.start_index = -5
self.length = -1
self.var_num = 3

def init_kernel_type(self):
self.dtype = np.complex128


class TestPartialConcatOp3_Complex128(TestPartialConcatOp):
def init_para(self):
self.batch_size = random.randint(1, 10)
self.column = random.randint(101, 200)
self.start_index = 10
self.length = 20
self.var_num = 2

def init_kernel_type(self):
self.dtype = np.complex128


class TestPartialConcatOp4_Complex128(TestPartialConcatOp):
def init_para(self):
self.batch_size = random.randint(1, 10)
self.column = random.randint(101, 200)
self.start_index = -1
self.length = -1
self.var_num = 1

def init_kernel_type(self):
self.dtype = np.complex128


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