@ -15,9 +15,11 @@
import unittest
import numpy as np
from numpy . random import random as rand
from paddle import complex as cpx
import paddle
import paddle . fluid as fluid
import paddle . fluid . dygraph as dg
from paddle import complex as cpx
layers = {
" add " : cpx . elementwise_add ,
@ -26,121 +28,135 @@ layers = {
" div " : cpx . elementwise_div ,
}
fluid_layer s = {
" add " : fluid. layers . elementwise_ add,
" sub " : fluid. layers . elementwise_sub ,
" mul " : fluid. layers . elementwise_mul ,
" div " : fluid. layers . elementwise_div ,
paddle_api s = {
" add " : paddle. add,
" sub " : paddle. subtract ,
" mul " : paddle. multiply ,
" div " : paddle. divide ,
}
class TestComplexElementwiseLayers ( unittest . TestCase ) :
def setUp ( self ) :
self . _dtype = " float64 "
self . _places = [ fluid . CPUPlace ( ) ]
self . _dtype s = [ " float32 " , " float64 " ]
self . _places = [ paddle . CPUPlace ( ) ]
if fluid . core . is_compiled_with_cuda ( ) :
self . _places . append ( fluid . CUDAPlace ( 0 ) )
self . _places . append ( paddle . CUDAPlace ( 0 ) )
def calc ( self , x , y , layer_type , place ) :
def calc ( self , x , y , op , place ) :
with dg . guard ( place ) :
var_x = dg . to_variable ( x )
var_y = dg . to_variable ( y )
return layers [ layer_type ] ( var_x , var_y ) . numpy ( )
return layers [ op ] ( var_x , var_y ) . numpy ( )
def fuild_calc( self , x , y , layer_type , place ) :
def paddle_calc( self , x , y , op , place ) :
with dg . guard ( place ) :
var_x = fluid . core . VarBase (
x_t = paddle . Tensor (
value = x ,
place = fluid. framework . _current_expected_ place( ) ,
place = place,
persistable = False ,
zero_copy = Non e,
name= ' ' )
var_y = fluid . core . VarBase (
zero_copy = Fals e,
stop_gradient= True )
y_t = paddle . Tensor (
value = y ,
place = fluid. framework . _current_expected_ place( ) ,
place = place,
persistable = False ,
zero_copy = None ,
name = ' ' )
return fluid_layers [ layer_type ] ( var_x , var_y ) . numpy ( )
def compare ( self , x , y ) :
zero_copy = False ,
stop_gradient = True )
return paddle_apis [ op ] ( x_t , y_t ) . numpy ( )
def assert_check ( self , pd_result , np_result , place ) :
self . assertTrue (
np . allclose ( pd_result , np_result ) ,
" \n place: {} \n paddle diff result: \n {} \n numpy diff result: \n {} \n " .
format ( place , pd_result [ ~ np . isclose ( pd_result , np_result ) ] ,
np_result [ ~ np . isclose ( pd_result , np_result ) ] ) )
def compare_by_complex_api ( self , x , y ) :
for place in self . _places :
self . assertTrue ( np . allclose ( self . calc ( x , y , " add " , place ) , x + y ) )
self . assertTrue ( np . allclose ( self . calc ( x , y , " sub " , place ) , x - y ) )
self . assertTrue ( np . allclose ( self . calc ( x , y , " mul " , place ) , x * y ) )
self . assertTrue ( np . allclose ( self . calc ( x , y , " div " , place ) , x / y ) )
self . assert _check ( self . calc ( x , y , " add " , place ) , x + y , place )
self . assert _check ( self . calc ( x , y , " sub " , place ) , x - y , place )
self . assert _check ( self . calc ( x , y , " mul " , place ) , x * y , place )
self . assert _check ( self . calc ( x , y , " div " , place ) , x / y , place )
def compare_1 ( self , x , y ) :
def compare_ by_basic_api ( self , x , y ) :
for place in self . _places :
self . assert True (
np . allclose ( self . fuild _calc( x , y , " add " , place ) , x + y ) )
self . assert True (
np . allclose ( self . fuild _calc( x , y , " sub " , place ) , x - y ) )
self . assert True (
np . allclose ( self . fuild _calc( x , y , " mul " , place ) , x * y ) )
self . assert True (
np . allclose ( self . fuild _calc( x , y , " div " , place ) , x / y ) )
def compare_op ( self , x , y ) :
self . assert _check (
self . paddle _calc( x , y , " add " , place ) , x + y , place )
self . assert _check (
self . paddle _calc( x , y , " sub " , place ) , x - y , place )
self . assert _check (
self . paddle _calc( x , y , " mul " , place ) , x * y , place )
self . assert _check (
self . paddle _calc( x , y , " div " , place ) , x / y , place )
def compare_op _by_complex_api ( self , x , y ) :
for place in self . _places :
with dg . guard ( place ) :
var_x = dg . to_variable ( x )
var_y = dg . to_variable ( y )
self . assert True ( var_x + var_y , x + y )
self . assert True ( var_x - var_y , x - y )
self . assert True ( var_x * var_y , x * y )
self . assert True ( var_x / var_y , x / y )
self . assert _check( ( var_x + var_y ). numpy ( ) , x + y , place )
self . assert _check( ( var_x - var_y ). numpy ( ) , x - y , place )
self . assert _check( ( var_x * var_y ). numpy ( ) , x * y , place )
self . assert _check( ( var_x / var_y ). numpy ( ) , x / y , place )
def compare_op_ 1 ( self , x , y ) :
def compare_op_ by_basic_api ( self , x , y ) :
for place in self . _places :
with dg . guard ( place ) :
var_x = fluid . core . VarBase (
x_t = paddle . Tensor (
value = x ,
place = fluid. framework . _current_expected_ place( ) ,
place = place,
persistable = False ,
zero_copy = Non e,
name= ' ' )
var_y = fluid . core . VarBase (
zero_copy = Fals e,
stop_gradient= True )
y_t = paddle . Tensor (
value = y ,
place = fluid. framework . _current_expected_ place( ) ,
place = place,
persistable = False ,
zero_copy = Non e,
name= ' ' )
self . assert True( np . allclose ( ( var_x + var_y ) . numpy ( ) , x + y ) )
self . assert True( np . allclose ( ( var_x - var_y ) . numpy ( ) , x - y ) )
self . assert True( np . allclose ( ( var_x * var_y ) . numpy ( ) , x * y ) )
self . assert True( np . allclose ( ( var_x / var_y ) . numpy ( ) , x / y ) )
zero_copy = Fals e,
stop_gradient= True )
self . assert _check( ( x_t + y_t ) . numpy ( ) , x + y , place )
self . assert _check( ( x_t - y_t ) . numpy ( ) , x - y , place )
self . assert _check( ( x_t * y_t ) . numpy ( ) , x * y , place )
self . assert _check( ( x_t / y_t ) . numpy ( ) , x / y , place )
def test_complex_xy ( self ) :
x = rand ( [ 2 , 3 , 4 , 5 ] ) . astype ( self . _dtype ) + 1 j * rand (
[ 2 , 3 , 4 , 5 ] ) . astype ( self . _dtype )
y = rand ( [ 2 , 3 , 4 , 5 ] ) . astype ( self . _dtype ) + 1 j * rand (
[ 2 , 3 , 4 , 5 ] ) . astype ( self . _dtype )
self . compare ( x , y )
self . compare_op ( x , y )
self . compare_1 ( x , y )
self . compare_op_1 ( x , y )
for dtype in self . _dtypes :
x = rand ( [ 2 , 3 , 4 , 5 ] ) . astype ( dtype ) + 1 j * rand (
[ 2 , 3 , 4 , 5 ] ) . astype ( dtype )
y = rand ( [ 2 , 3 , 4 , 5 ] ) . astype ( dtype ) + 1 j * rand (
[ 2 , 3 , 4 , 5 ] ) . astype ( dtype )
self . compare_by_complex_api ( x , y )
self . compare_op_by_complex_api ( x , y )
self . compare_op_by_complex_api ( x , y )
self . compare_op_by_basic_api ( x , y )
def test_complex_x_real_y ( self ) :
x = rand ( [ 2 , 3 , 4 , 5 ] ) . astype ( self . _dtype ) + 1 j * rand (
[ 2 , 3 , 4 , 5 ] ) . astype ( self . _dtype )
y = rand ( [ 4 , 5 ] ) . astype ( self . _dtype )
self . compare ( x , y )
self . compare_op ( x , y )
for dtype in self . _dtypes :
x = rand ( [ 2 , 3 , 4 , 5 ] ) . astype ( dtype ) + 1 j * rand (
[ 2 , 3 , 4 , 5 ] ) . astype ( dtype )
y = rand ( [ 4 , 5 ] ) . astype ( dtype )
self . compare_by_complex_api ( x , y )
self . compare_op_by_complex_api ( x , y )
# promote types cases
self . compare_by_basic_api ( x , y )
self . compare_op_by_basic_api ( x , y )
def test_real_x_complex_y ( self ) :
x = rand ( [ 2 , 3 , 4 , 5 ] ) . astype ( self . _dtype )
y = rand ( [ 5 ] ) . astype ( self . _dtype ) + 1 j * rand ( [ 5 ] ) . astype ( self . _dtype )
self . compare ( x , y )
self . compare_op ( x , y )
def test_complex64_xy ( self ) :
x = rand ( [ 2 , 3 , 4 , 5 ] ) . astype ( " float32 " ) + 1 j * rand (
[ 2 , 3 , 4 , 5 ] ) . astype ( " float32 " )
y = rand ( [ 2 , 3 , 4 , 5 ] ) . astype ( " float32 " ) + 1 j * rand (
[ 2 , 3 , 4 , 5 ] ) . astype ( " float32 " )
self . compare_1 ( x , y )
self . compare_op_1 ( x , y )
for dtype in self . _dtypes :
x = rand ( [ 2 , 3 , 4 , 5 ] ) . astype ( dtype )
y = rand ( [ 5 ] ) . astype ( dtype ) + 1 j * rand ( [ 5 ] ) . astype ( dtype )
self . compare_by_complex_api ( x , y )
self . compare_op_by_complex_api ( x , y )
# promote types cases
self . compare_by_basic_api ( x , y )
self . compare_op_by_basic_api ( x , y )
if __name__ == ' __main__ ' :