未验证 提交 457defe7 编写于 作者: C Charles-hit 提交者: GitHub

[OpTest] support prim test in OpTest (#50509)

* support prim test in OpTest

* fix cmake

* fix op test

* fix test_input_spec

* disable cinn in reduce_sum unit test

* add bfloat16 dtype for sum

* polish code

* add clear jit program function

* convert grad out from tensor to numpy

* remove unnecessary code

* add only_prim flag

* fix flag

* fix op test

* fix optest comp inplace error

* fix op test

* fix op test with guard

* add initialization of check_comp flag

* fix comp inplace error in op test

* rename check_comp with check_prim and add bfloat16 dtype convert

* rename comp_op_type to prim_op_type

* rename comp to prim

* remove useless code

* skip ci check for only prim

* add no_grad_vars and grad_outputs in prim test

* fix var_dict

* fix op test for only_prim

* fix dy2static bugs

* polish some code
上级 2135020a
...@@ -1204,6 +1204,14 @@ if($ENV{USE_STANDALONE_EXECUTOR}) ...@@ -1204,6 +1204,14 @@ if($ENV{USE_STANDALONE_EXECUTOR})
PROPERTIES ENVIRONMENT FLAGS_USE_STANDALONE_EXECUTOR=0) PROPERTIES ENVIRONMENT FLAGS_USE_STANDALONE_EXECUTOR=0)
endif() endif()
set(TEST_CINN_OPS test_softmax_op test_expand_v2_op test_reduce_op)
foreach(TEST_CINN_OPS ${TEST_CINN_OPS})
if(WITH_CINN)
set_tests_properties(${TEST_CINN_OPS} PROPERTIES LABELS "RUN_TYPE=CINN")
endif()
endforeach()
if(WITH_CINN AND WITH_TESTING) if(WITH_CINN AND WITH_TESTING)
set_tests_properties( set_tests_properties(
test_resnet50_with_cinn test_resnet50_with_cinn
......
# 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.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import numpy as np
TOLERANCE = {
np.dtype('float64'): {
"jit_comp": {"rtol": 1e-15, "atol": 1e-15},
"fw_comp": {"rtol": 1e-15, "atol": 1e-15},
"rev_comp": {"rtol": 1e-15, "atol": 1e-15},
"cinn": {"rtol": 1e-14, "atol": 1e-14},
},
np.dtype('float32'): {
"jit_comp": {"rtol": 1e-6, "atol": 1e-6},
"fw_comp": {"rtol": 1e-6, "atol": 1e-6},
"rev_comp": {"rtol": 1e-6, "atol": 1e-6},
"cinn": {"rtol": 1e-5, "atol": 1e-5},
},
np.dtype('float16'): {
"jit_comp": {"rtol": 1e-3, "atol": 1e-3},
"fw_comp": {"rtol": 1e-3, "atol": 1e-3},
"rev_comp": {"rtol": 1e-3, "atol": 1e-3},
"cinn": {"rtol": 1e-2, "atol": 1e-2},
},
np.dtype('uint16'): {
"jit_comp": {"rtol": 1e-2, "atol": 1e-2},
"fw_comp": {"rtol": 1e-2, "atol": 1e-2},
"rev_comp": {"rtol": 1e-2, "atol": 1e-2},
"cinn": {"rtol": 1e-1, "atol": 1e-1},
},
}
...@@ -34,13 +34,12 @@ from paddle.fluid.framework import ( ...@@ -34,13 +34,12 @@ from paddle.fluid.framework import (
OpProtoHolder, OpProtoHolder,
Program, Program,
_current_expected_place, _current_expected_place,
_dygraph_tracer,
in_dygraph_mode, in_dygraph_mode,
) )
from paddle.fluid.op import Operator from paddle.fluid.op import Operator
from paddle.jit.dy2static.utils import parse_arg_and_kwargs
sys.path.append(os.path.abspath(os.path.dirname(__file__))) sys.path.append(os.path.abspath(os.path.dirname(__file__)))
from prim_op_test import OpTestUtils, PrimForwardChecker, PrimGradChecker
from testsuite import append_input_output, append_loss_ops, create_op, set_input from testsuite import append_input_output, append_loss_ops, create_op, set_input
from white_list import ( from white_list import (
check_shape_white_list, check_shape_white_list,
...@@ -321,6 +320,7 @@ class OpTest(unittest.TestCase): ...@@ -321,6 +320,7 @@ class OpTest(unittest.TestCase):
cls.dtype = None cls.dtype = None
cls.outputs = {} cls.outputs = {}
cls.input_shape_is_large = True cls.input_shape_is_large = True
cls.check_prim = False
np.random.seed(123) np.random.seed(123)
random.seed(124) random.seed(124)
...@@ -401,6 +401,7 @@ class OpTest(unittest.TestCase): ...@@ -401,6 +401,7 @@ class OpTest(unittest.TestCase):
and not is_npu_op_test() and not is_npu_op_test()
and not is_mlu_op_test() and not is_mlu_op_test()
and not is_custom_device_op_test() and not is_custom_device_op_test()
and not cls.check_prim
): ):
raise AssertionError( raise AssertionError(
"This test of %s op needs check_grad with fp64 precision." "This test of %s op needs check_grad with fp64 precision."
...@@ -579,7 +580,6 @@ class OpTest(unittest.TestCase): ...@@ -579,7 +580,6 @@ class OpTest(unittest.TestCase):
type=core.VarDesc.VarType.RAW, type=core.VarDesc.VarType.RAW,
stop_gradient=True, stop_gradient=True,
) )
op = block.append_op( op = block.append_op(
type=self.op_type, type=self.op_type,
inputs=inputs, inputs=inputs,
...@@ -806,100 +806,6 @@ class OpTest(unittest.TestCase): ...@@ -806,100 +806,6 @@ class OpTest(unittest.TestCase):
def _calc_python_api_output(self, place, egr_inps=None, egr_oups=None): def _calc_python_api_output(self, place, egr_inps=None, egr_oups=None):
"""set egr_inps and egr_oups = None if you want to create it by yourself.""" """set egr_inps and egr_oups = None if you want to create it by yourself."""
def prepare_python_api_arguments(
api, op_proto_ins, op_proto_attrs, kernel_sig
):
"""map from `op proto inputs and attrs` to `api input list and api attrs dict`
NOTE: the op_proto_attrs and op_proto_ins is a default dict. default value is []
"""
class Empty:
pass
def is_empty(a):
return isinstance(a, Empty)
def get_default(idx, defaults):
assert not isinstance(defaults[idx], Empty), (
"%d-th params of python api don't have default value." % idx
)
return defaults[idx]
def to_defaults_list(params, defaults):
return [defaults[p] for p in params if p in defaults]
def parse_attri_value(name, op_inputs, op_attrs):
"""parse true value from inputs and attrs, if there is no name passed by OpTest, return Empty
1. if the name in op_attrs, use the op_attrs[name]
2. if the name in op_inputs, convert the op_inputs to [type of default value]
3. if the name not in op_attrs ans op_inputs, return Empty. (this will use the default value from python api)
"""
if name in op_proto_attrs:
return op_proto_attrs[name]
elif name in op_inputs:
if len(op_inputs[name]) == 1:
# why don't use numpy().item() : if the Tensor is float64, we will change it to python.float32, where we loss accuracy: [allclose_op]
# why we reconstruct a tensor: because we want the tensor in cpu.
return paddle.to_tensor(
op_inputs[name][0].numpy(), place='cpu'
)
else:
# if this is a list (test_unsqueeze2_op): we just pass it into the python api.
return op_inputs[name]
else:
return Empty()
# NOTE(xiongkun): the logic of constructing parameters:
# for example:
# python api: cumprod(x, dim, dtype=None, name=None)
# kernel sig: [["x"], ["dim"], ["out"]]"
#
# we will construct a lot of list with the same length : len == len(api_params), here is 4
# api_params = ["x", "dim", "dtype", "name"]
# api_defaults = [Empty, Empty, None, None]; empty means no defaults.
# inputs_and_attrs = ["x", "dim"] , the length may shorter or longer than api_params
# input_arguments = [RealValue in self.inputs and self.attrs]
# then ,we will loop for the api_params, construct a result list:
# if the name in ['name', 'dtype', 'out', 'output'], we will use the default value
# else, we will consume a input_arguments. (because the name is not corresponding, so we only use the order)
api_params, api_defaults = parse_arg_and_kwargs(api)
api_defaults = to_defaults_list(api_params, api_defaults)
api_defaults = [
Empty() for i in range(len(api_params) - len(api_defaults))
] + api_defaults
assert len(api_defaults) == len(
api_params
), "Error happens. contack xiongkun03 to solve."
inputs_sig, attrs_sig, outputs_sig = kernel_sig
inputs_and_attrs = inputs_sig + attrs_sig
input_arguments = [
op_proto_ins.get(name, Empty()) for name in inputs_sig
] + [
parse_attri_value(name, op_proto_ins, op_proto_attrs)
for name in attrs_sig
]
results = []
api_ignore_param_list = set(['name', 'dtype', 'out', 'output'])
idx_of_op_proto_arguments = 0
for idx, arg_name in enumerate(api_params):
if arg_name in api_ignore_param_list:
results.append(get_default(idx, api_defaults))
else:
if idx_of_op_proto_arguments < len(input_arguments):
tmp = input_arguments[idx_of_op_proto_arguments]
idx_of_op_proto_arguments += 1
else:
tmp = Empty() # use the default value
if isinstance(tmp, Empty):
results.append(get_default(idx, api_defaults))
else:
results.append(tmp)
assert len(results) == len(api_params)
return results
def construct_output_dict_by_kernel_sig(ret_tuple, output_sig): def construct_output_dict_by_kernel_sig(ret_tuple, output_sig):
if hasattr(self, "python_out_sig"): if hasattr(self, "python_out_sig"):
output_sig = self.python_out_sig output_sig = self.python_out_sig
...@@ -915,50 +821,11 @@ class OpTest(unittest.TestCase): ...@@ -915,50 +821,11 @@ class OpTest(unittest.TestCase):
), "Don't support multi-output with multi-tensor output. (May be you can use set `python_out_sig`, see `test_squeeze2_op` as a example.)" ), "Don't support multi-output with multi-tensor output. (May be you can use set `python_out_sig`, see `test_squeeze2_op` as a example.)"
return {output_sig[0]: ret_tuple} return {output_sig[0]: ret_tuple}
def assumption_assert_and_transform(args, inp_num):
"""
transform inputs by the following rules:
1. [Tensor] -> Tensor
2. [Tensor, Tensor, ...] -> list of Tensors
3. None -> None
4. Others: raise Error
only support "X" is list of Tensor, currently don't support other structure like dict.
"""
inp_args = [
[inp] if inp is None else inp for inp in args[:inp_num]
] # convert None -> [None]
for inp in inp_args:
assert isinstance(
inp, list
), "currently only support `X` is [Tensor], don't support other structure."
args = [
inp[0] if len(inp) == 1 else inp for inp in inp_args
] + args[inp_num:]
return args
def _get_kernel_signature(
dygraph_tensor_inputs, dygraph_tensor_outputs, attrs_outputs
):
try:
kernel_sig = _dygraph_tracer()._get_kernel_signature(
self.op_type,
dygraph_tensor_inputs,
dygraph_tensor_outputs,
attrs_outputs,
)
except RuntimeError as re:
"""we think the kernel_sig is missing."""
kernel_sig = None
print(
"[Warning: op_test.py] Kernel Signature is not found for %s, fall back to intermediate state."
% self.op_type
)
return kernel_sig
def cal_python_api(python_api, args, kernel_sig): def cal_python_api(python_api, args, kernel_sig):
inputs_sig, attrs_sig, outputs_sig = kernel_sig inputs_sig, attrs_sig, outputs_sig = kernel_sig
args = assumption_assert_and_transform(args, len(inputs_sig)) args = OpTestUtils.assumption_assert_and_transform(
args, len(inputs_sig)
)
ret_tuple = python_api(*args) ret_tuple = python_api(*args)
return construct_output_dict_by_kernel_sig(ret_tuple, outputs_sig) return construct_output_dict_by_kernel_sig(ret_tuple, outputs_sig)
...@@ -989,8 +856,11 @@ class OpTest(unittest.TestCase): ...@@ -989,8 +856,11 @@ class OpTest(unittest.TestCase):
if self.attrs[attrs_name] is not None: if self.attrs[attrs_name] is not None:
attrs_outputs[attrs_name] = self.attrs[attrs_name] attrs_outputs[attrs_name] = self.attrs[attrs_name]
kernel_sig = _get_kernel_signature( kernel_sig = OpTestUtils._get_kernel_signature(
dygraph_tensor_inputs, dygraph_tensor_outputs, attrs_outputs self.op_type,
dygraph_tensor_inputs,
dygraph_tensor_outputs,
attrs_outputs,
) )
if not kernel_sig: if not kernel_sig:
return None return None
...@@ -998,7 +868,7 @@ class OpTest(unittest.TestCase): ...@@ -998,7 +868,7 @@ class OpTest(unittest.TestCase):
"Detect there is KernelSignature for `%s` op, please set the `self.python_api` if you set check_dygraph = True" "Detect there is KernelSignature for `%s` op, please set the `self.python_api` if you set check_dygraph = True"
% self.op_type % self.op_type
) )
args = prepare_python_api_arguments( args = OpTestUtils.prepare_python_api_arguments(
self.python_api, self.python_api,
dygraph_tensor_inputs, dygraph_tensor_inputs,
attrs_outputs, attrs_outputs,
...@@ -1050,6 +920,7 @@ class OpTest(unittest.TestCase): ...@@ -1050,6 +920,7 @@ class OpTest(unittest.TestCase):
enable_inplace=None, enable_inplace=None,
for_inplace_test=None, for_inplace_test=None,
): ):
with paddle.fluid.framework._dygraph_guard(None):
program = Program() program = Program()
block = program.global_block() block = program.global_block()
op = self._append_ops(block) op = self._append_ops(block)
...@@ -1072,7 +943,9 @@ class OpTest(unittest.TestCase): ...@@ -1072,7 +943,9 @@ class OpTest(unittest.TestCase):
use_cuda = False use_cuda = False
if isinstance(place, fluid.CUDAPlace): if isinstance(place, fluid.CUDAPlace):
use_cuda = True use_cuda = True
compiled_prog = fluid.CompiledProgram(program).with_data_parallel( compiled_prog = fluid.CompiledProgram(
program
).with_data_parallel(
loss_name=loss.name if loss else None, places=place loss_name=loss.name if loss else None, places=place
) )
program = compiled_prog program = compiled_prog
...@@ -1097,14 +970,19 @@ class OpTest(unittest.TestCase): ...@@ -1097,14 +970,19 @@ class OpTest(unittest.TestCase):
build_strategy = fluid.BuildStrategy() build_strategy = fluid.BuildStrategy()
build_strategy.enable_inplace = enable_inplace build_strategy.enable_inplace = enable_inplace
compiled_prog = fluid.CompiledProgram(program).with_data_parallel( compiled_prog = fluid.CompiledProgram(
program
).with_data_parallel(
build_strategy=build_strategy, places=place build_strategy=build_strategy, places=place
) )
program = compiled_prog program = compiled_prog
executor = Executor(place) executor = Executor(place)
outs = executor.run( outs = executor.run(
program, feed=feed_map, fetch_list=fetch_list, return_numpy=False program,
feed=feed_map,
fetch_list=fetch_list,
return_numpy=False,
) )
self.op = op self.op = op
self.program = original_program self.program = original_program
...@@ -1371,6 +1249,7 @@ class OpTest(unittest.TestCase): ...@@ -1371,6 +1249,7 @@ class OpTest(unittest.TestCase):
Returns: Returns:
res (tuple(outs, fetch_list, feed_map, program, op_desc)): The results of given grad_op_desc. res (tuple(outs, fetch_list, feed_map, program, op_desc)): The results of given grad_op_desc.
""" """
with paddle.fluid.framework._dygraph_guard(None):
( (
fwd_outs, fwd_outs,
fwd_fetch_list, fwd_fetch_list,
...@@ -1465,7 +1344,6 @@ class OpTest(unittest.TestCase): ...@@ -1465,7 +1344,6 @@ class OpTest(unittest.TestCase):
has_infer_inplace = fluid.core.has_infer_inplace(self.op_type) has_infer_inplace = fluid.core.has_infer_inplace(self.op_type)
has_grad_op_maker = fluid.core.has_grad_op_maker(self.op_type) has_grad_op_maker = fluid.core.has_grad_op_maker(self.op_type)
fwd_res = self._calc_output( fwd_res = self._calc_output(
place, no_check_set=no_check_set, for_inplace_test=True place, no_check_set=no_check_set, for_inplace_test=True
) )
...@@ -1518,8 +1396,11 @@ class OpTest(unittest.TestCase): ...@@ -1518,8 +1396,11 @@ class OpTest(unittest.TestCase):
no_check_set=None, no_check_set=None,
equal_nan=False, equal_nan=False,
check_dygraph=True, check_dygraph=True,
check_prim=False,
inplace_atol=None, inplace_atol=None,
): ):
core._set_prim_all_enabled(False)
def find_imperative_actual(target_name, dygraph_outs, place): def find_imperative_actual(target_name, dygraph_outs, place):
for name in dygraph_outs: for name in dygraph_outs:
if name == target_name: if name == target_name:
...@@ -1785,6 +1666,15 @@ class OpTest(unittest.TestCase): ...@@ -1785,6 +1666,15 @@ class OpTest(unittest.TestCase):
return True return True
return super()._is_skip_name(name) return super()._is_skip_name(name)
if check_prim:
prim_checker = PrimForwardChecker(self, place)
prim_checker.check()
# Support operators which are not in the NO_FP64_CHECK_GRAD_OP_LIST list can be test prim with fp32
setattr(self.__class__, 'check_prim', True)
self.__class__.op_type = self.op_type
if prim_checker.is_only_check_prim():
self.only_prim = True
return
# set some flags by the combination of arguments. # set some flags by the combination of arguments.
self.infer_dtype_from_inputs_outputs(self.inputs, self.outputs) self.infer_dtype_from_inputs_outputs(self.inputs, self.outputs)
if ( if (
...@@ -1930,6 +1820,7 @@ class OpTest(unittest.TestCase): ...@@ -1930,6 +1820,7 @@ class OpTest(unittest.TestCase):
no_check_set=None, no_check_set=None,
equal_nan=False, equal_nan=False,
check_dygraph=True, check_dygraph=True,
check_prim=False,
inplace_atol=None, inplace_atol=None,
): ):
...@@ -1948,8 +1839,11 @@ class OpTest(unittest.TestCase): ...@@ -1948,8 +1839,11 @@ class OpTest(unittest.TestCase):
no_check_set, no_check_set,
equal_nan, equal_nan,
check_dygraph=check_dygraph, check_dygraph=check_dygraph,
check_prim=check_prim,
inplace_atol=inplace_atol, inplace_atol=inplace_atol,
) )
if hasattr(self, 'only_prim') and self.only_prim:
continue
if check_dygraph: if check_dygraph:
outs, dygraph_dygraph_outs, fetch_list = res outs, dygraph_dygraph_outs, fetch_list = res
else: else:
...@@ -2063,8 +1957,8 @@ class OpTest(unittest.TestCase): ...@@ -2063,8 +1957,8 @@ class OpTest(unittest.TestCase):
user_defined_grads=None, user_defined_grads=None,
user_defined_grad_outputs=None, user_defined_grad_outputs=None,
check_dygraph=True, check_dygraph=True,
check_prim=False,
): ):
self._check_grad_helper() self._check_grad_helper()
places = self._get_places() places = self._get_places()
for place in places: for place in places:
...@@ -2079,6 +1973,7 @@ class OpTest(unittest.TestCase): ...@@ -2079,6 +1973,7 @@ class OpTest(unittest.TestCase):
user_defined_grads, user_defined_grads,
user_defined_grad_outputs, user_defined_grad_outputs,
check_dygraph=check_dygraph, check_dygraph=check_dygraph,
check_prim=check_prim,
) )
def check_grad_with_place( def check_grad_with_place(
...@@ -2093,9 +1988,26 @@ class OpTest(unittest.TestCase): ...@@ -2093,9 +1988,26 @@ class OpTest(unittest.TestCase):
user_defined_grads=None, user_defined_grads=None,
user_defined_grad_outputs=None, user_defined_grad_outputs=None,
check_dygraph=True, check_dygraph=True,
check_prim=False,
numeric_place=None, numeric_place=None,
): ):
core._set_prim_all_enabled(False)
if check_prim:
prim_grad_checker = PrimGradChecker(
self,
place,
inputs_to_check,
output_names,
no_grad_set,
user_defined_grad_outputs,
)
prim_grad_checker.check()
# Support operators which are not in the NO_FP64_CHECK_GRAD_OP_LIST list can be test prim with fp32
setattr(self.__class__, 'check_prim', True)
self._check_grad_helper()
if prim_grad_checker.is_only_check_prim():
self.only_prim = True
return
self.scope = core.Scope() self.scope = core.Scope()
op_inputs = self.inputs if hasattr(self, "inputs") else dict() op_inputs = self.inputs if hasattr(self, "inputs") else dict()
op_outputs = self.outputs if hasattr(self, "outputs") else dict() op_outputs = self.outputs if hasattr(self, "outputs") else dict()
...@@ -2448,6 +2360,7 @@ class OpTest(unittest.TestCase): ...@@ -2448,6 +2360,7 @@ class OpTest(unittest.TestCase):
user_defined_grad_outputs=None, user_defined_grad_outputs=None,
parallel=False, parallel=False,
): ):
with paddle.fluid.framework._dygraph_guard(None):
prog = Program() prog = Program()
scope = core.Scope() scope = core.Scope()
block = prog.global_block() block = prog.global_block()
...@@ -2504,7 +2417,9 @@ class OpTest(unittest.TestCase): ...@@ -2504,7 +2417,9 @@ class OpTest(unittest.TestCase):
targets = [ targets = [
outputs[name] for name in outputs if name in output_names outputs[name] for name in outputs if name in output_names
] ]
inputs = [inputs[name] for name in input_to_check if name in inputs] inputs = [
inputs[name] for name in input_to_check if name in inputs
]
grad_inputs = paddle.static.gradients( grad_inputs = paddle.static.gradients(
targets, inputs, grad_outputs, no_grad_set targets, inputs, grad_outputs, no_grad_set
) )
...@@ -2519,14 +2434,19 @@ class OpTest(unittest.TestCase): ...@@ -2519,14 +2434,19 @@ class OpTest(unittest.TestCase):
) )
prog = compiled_prog prog = compiled_prog
executor = fluid.Executor(place) executor = fluid.Executor(place)
return list( res = list(
map( map(
np.array, np.array,
executor.run( executor.run(
prog, feed_dict, fetch_list, scope=scope, return_numpy=False prog,
feed_dict,
fetch_list,
scope=scope,
return_numpy=False,
), ),
) )
) )
return res
class OpTestTool: class OpTestTool:
......
...@@ -35,16 +35,15 @@ from paddle.fluid.framework import ( ...@@ -35,16 +35,15 @@ from paddle.fluid.framework import (
Program, Program,
_current_expected_place, _current_expected_place,
_disable_legacy_dygraph, _disable_legacy_dygraph,
_dygraph_tracer,
_enable_legacy_dygraph, _enable_legacy_dygraph,
_in_eager_without_dygraph_check, _in_eager_without_dygraph_check,
_test_eager_guard, _test_eager_guard,
in_dygraph_mode, in_dygraph_mode,
) )
from paddle.fluid.op import Operator from paddle.fluid.op import Operator
from paddle.jit.dy2static.utils import parse_arg_and_kwargs
sys.path.append(os.path.abspath(os.path.dirname(__file__))) sys.path.append(os.path.abspath(os.path.dirname(__file__)))
from prim_op_test import OpTestUtils, PrimForwardChecker, PrimGradChecker
from testsuite import append_input_output, append_loss_ops, create_op, set_input from testsuite import append_input_output, append_loss_ops, create_op, set_input
from white_list import ( from white_list import (
check_shape_white_list, check_shape_white_list,
...@@ -334,6 +333,7 @@ class OpTest(unittest.TestCase): ...@@ -334,6 +333,7 @@ class OpTest(unittest.TestCase):
cls.dtype = None cls.dtype = None
cls.outputs = {} cls.outputs = {}
cls.input_shape_is_large = True cls.input_shape_is_large = True
cls.check_prim = False
np.random.seed(123) np.random.seed(123)
random.seed(124) random.seed(124)
...@@ -414,6 +414,7 @@ class OpTest(unittest.TestCase): ...@@ -414,6 +414,7 @@ class OpTest(unittest.TestCase):
and not is_npu_op_test() and not is_npu_op_test()
and not is_mlu_op_test() and not is_mlu_op_test()
and not is_custom_device_op_test() and not is_custom_device_op_test()
and not cls.check_prim
): ):
raise AssertionError( raise AssertionError(
"This test of %s op needs check_grad with fp64 precision." "This test of %s op needs check_grad with fp64 precision."
...@@ -819,100 +820,6 @@ class OpTest(unittest.TestCase): ...@@ -819,100 +820,6 @@ class OpTest(unittest.TestCase):
def _calc_python_api_output(self, place, egr_inps=None, egr_oups=None): def _calc_python_api_output(self, place, egr_inps=None, egr_oups=None):
"""set egr_inps and egr_oups = None if you want to create it by yourself.""" """set egr_inps and egr_oups = None if you want to create it by yourself."""
def prepare_python_api_arguments(
api, op_proto_ins, op_proto_attrs, kernel_sig
):
"""map from `op proto inputs and attrs` to `api input list and api attrs dict`
NOTE: the op_proto_attrs and op_proto_ins is a default dict. default value is []
"""
class Empty:
pass
def is_empty(a):
return isinstance(a, Empty)
def get_default(idx, defaults):
assert not isinstance(defaults[idx], Empty), (
"%d-th params of python api don't have default value." % idx
)
return defaults[idx]
def to_defaults_list(params, defaults):
return [defaults[p] for p in params if p in defaults]
def parse_attri_value(name, op_inputs, op_attrs):
"""parse true value from inputs and attrs, if there is no name passed by OpTest, return Empty
1. if the name in op_attrs, use the op_attrs[name]
2. if the name in op_inputs, convert the op_inputs to [type of default value]
3. if the name not in op_attrs ans op_inputs, return Empty. (this will use the default value from python api)
"""
if name in op_proto_attrs:
return op_proto_attrs[name]
elif name in op_inputs:
if len(op_inputs[name]) == 1:
# why don't use numpy().item() : if the Tensor is float64, we will change it to python.float32, where we loss accuracy: [allclose_op]
# why we reconstruct a tensor: because we want the tensor in cpu.
return paddle.to_tensor(
op_inputs[name][0].numpy(), place='cpu'
)
else:
# if this is a list (test_unsqueeze2_op): we just pass it into the python api.
return op_inputs[name]
else:
return Empty()
# NOTE(xiongkun): the logic of constructing parameters:
# for example:
# python api: cumprod(x, dim, dtype=None, name=None)
# kernel sig: [["x"], ["dim"], ["out"]]"
#
# we will construct a lot of list with the same length : len == len(api_params), here is 4
# api_params = ["x", "dim", "dtype", "name"]
# api_defaults = [Empty, Empty, None, None]; empty means no defaults.
# inputs_and_attrs = ["x", "dim"] , the length may shorter or longer than api_params
# input_arguments = [RealValue in self.inputs and self.attrs]
# then ,we will loop for the api_params, construct a result list:
# if the name in ['name', 'dtype', 'out', 'output'], we will use the default value
# else, we will consume a input_arguments. (because the name is not corresponding, so we only use the order)
api_params, api_defaults = parse_arg_and_kwargs(api)
api_defaults = to_defaults_list(api_params, api_defaults)
api_defaults = [
Empty() for i in range(len(api_params) - len(api_defaults))
] + api_defaults
assert len(api_defaults) == len(
api_params
), "Error happens. contack xiongkun03 to solve."
inputs_sig, attrs_sig, outputs_sig = kernel_sig
inputs_and_attrs = inputs_sig + attrs_sig
input_arguments = [
op_proto_ins.get(name, Empty()) for name in inputs_sig
] + [
parse_attri_value(name, op_proto_ins, op_proto_attrs)
for name in attrs_sig
]
results = []
api_ignore_param_list = set(['name', 'dtype', 'out', 'output'])
idx_of_op_proto_arguments = 0
for idx, arg_name in enumerate(api_params):
if arg_name in api_ignore_param_list:
results.append(get_default(idx, api_defaults))
else:
if idx_of_op_proto_arguments < len(input_arguments):
tmp = input_arguments[idx_of_op_proto_arguments]
idx_of_op_proto_arguments += 1
else:
tmp = Empty() # use the default value
if isinstance(tmp, Empty):
results.append(get_default(idx, api_defaults))
else:
results.append(tmp)
assert len(results) == len(api_params)
return results
def construct_output_dict_by_kernel_sig(ret_tuple, output_sig): def construct_output_dict_by_kernel_sig(ret_tuple, output_sig):
if hasattr(self, "python_out_sig"): if hasattr(self, "python_out_sig"):
output_sig = self.python_out_sig output_sig = self.python_out_sig
...@@ -928,50 +835,11 @@ class OpTest(unittest.TestCase): ...@@ -928,50 +835,11 @@ class OpTest(unittest.TestCase):
), "Don't support multi-output with multi-tensor output. (May be you can use set `python_out_sig`, see `test_squeeze2_op` as a example.)" ), "Don't support multi-output with multi-tensor output. (May be you can use set `python_out_sig`, see `test_squeeze2_op` as a example.)"
return {output_sig[0]: ret_tuple} return {output_sig[0]: ret_tuple}
def assumption_assert_and_transform(args, inp_num):
"""
transform inputs by the following rules:
1. [Tensor] -> Tensor
2. [Tensor, Tensor, ...] -> list of Tensors
3. None -> None
4. Others: raise Error
only support "X" is list of Tensor, currently don't support other structure like dict.
"""
inp_args = [
[inp] if inp is None else inp for inp in args[:inp_num]
] # convert None -> [None]
for inp in inp_args:
assert isinstance(
inp, list
), "currently only support `X` is [Tensor], don't support other structure."
args = [
inp[0] if len(inp) == 1 else inp for inp in inp_args
] + args[inp_num:]
return args
def _get_kernel_signature(
eager_tensor_inputs, eager_tensor_outputs, attrs_outputs
):
try:
kernel_sig = _dygraph_tracer()._get_kernel_signature(
self.op_type,
eager_tensor_inputs,
eager_tensor_outputs,
attrs_outputs,
)
except RuntimeError as re:
"""we think the kernel_sig is missing."""
kernel_sig = None
print(
"[Warning: op_test.py] Kernel Signature is not found for %s, fall back to intermediate state."
% self.op_type
)
return kernel_sig
def cal_python_api(python_api, args, kernel_sig): def cal_python_api(python_api, args, kernel_sig):
inputs_sig, attrs_sig, outputs_sig = kernel_sig inputs_sig, attrs_sig, outputs_sig = kernel_sig
args = assumption_assert_and_transform(args, len(inputs_sig)) args = OpTestUtils.assumption_assert_and_transform(
args, len(inputs_sig)
)
ret_tuple = python_api(*args) ret_tuple = python_api(*args)
return construct_output_dict_by_kernel_sig(ret_tuple, outputs_sig) return construct_output_dict_by_kernel_sig(ret_tuple, outputs_sig)
...@@ -994,7 +862,6 @@ class OpTest(unittest.TestCase): ...@@ -994,7 +862,6 @@ class OpTest(unittest.TestCase):
op_proto, self.outputs, False, False, block op_proto, self.outputs, False, False, block
) )
) )
# prepare attributes # prepare attributes
attrs_outputs = {} attrs_outputs = {}
if hasattr(self, "attrs"): if hasattr(self, "attrs"):
...@@ -1002,8 +869,11 @@ class OpTest(unittest.TestCase): ...@@ -1002,8 +869,11 @@ class OpTest(unittest.TestCase):
if self.attrs[attrs_name] is not None: if self.attrs[attrs_name] is not None:
attrs_outputs[attrs_name] = self.attrs[attrs_name] attrs_outputs[attrs_name] = self.attrs[attrs_name]
kernel_sig = _get_kernel_signature( kernel_sig = OpTestUtils._get_kernel_signature(
eager_tensor_inputs, eager_tensor_outputs, attrs_outputs self.op_type,
eager_tensor_inputs,
eager_tensor_outputs,
attrs_outputs,
) )
if not kernel_sig: if not kernel_sig:
return None return None
...@@ -1011,7 +881,7 @@ class OpTest(unittest.TestCase): ...@@ -1011,7 +881,7 @@ class OpTest(unittest.TestCase):
"Detect there is KernelSignature for `%s` op, please set the `self.python_api` if you set check_eager = True" "Detect there is KernelSignature for `%s` op, please set the `self.python_api` if you set check_eager = True"
% self.op_type % self.op_type
) )
args = prepare_python_api_arguments( args = OpTestUtils.prepare_python_api_arguments(
self.python_api, eager_tensor_inputs, attrs_outputs, kernel_sig self.python_api, eager_tensor_inputs, attrs_outputs, kernel_sig
) )
""" we directly return the cal_python_api value because the value is already tensor. """ we directly return the cal_python_api value because the value is already tensor.
...@@ -1060,6 +930,7 @@ class OpTest(unittest.TestCase): ...@@ -1060,6 +930,7 @@ class OpTest(unittest.TestCase):
enable_inplace=None, enable_inplace=None,
for_inplace_test=None, for_inplace_test=None,
): ):
with paddle.fluid.framework._dygraph_guard(None):
program = Program() program = Program()
block = program.global_block() block = program.global_block()
op = self._append_ops(block) op = self._append_ops(block)
...@@ -1082,7 +953,9 @@ class OpTest(unittest.TestCase): ...@@ -1082,7 +953,9 @@ class OpTest(unittest.TestCase):
use_cuda = False use_cuda = False
if isinstance(place, fluid.CUDAPlace): if isinstance(place, fluid.CUDAPlace):
use_cuda = True use_cuda = True
compiled_prog = fluid.CompiledProgram(program).with_data_parallel( compiled_prog = fluid.CompiledProgram(
program
).with_data_parallel(
loss_name=loss.name if loss else None, places=place loss_name=loss.name if loss else None, places=place
) )
program = compiled_prog program = compiled_prog
...@@ -1107,14 +980,19 @@ class OpTest(unittest.TestCase): ...@@ -1107,14 +980,19 @@ class OpTest(unittest.TestCase):
build_strategy = fluid.BuildStrategy() build_strategy = fluid.BuildStrategy()
build_strategy.enable_inplace = enable_inplace build_strategy.enable_inplace = enable_inplace
compiled_prog = fluid.CompiledProgram(program).with_data_parallel( compiled_prog = fluid.CompiledProgram(
program
).with_data_parallel(
build_strategy=build_strategy, places=place build_strategy=build_strategy, places=place
) )
program = compiled_prog program = compiled_prog
executor = Executor(place) executor = Executor(place)
outs = executor.run( outs = executor.run(
program, feed=feed_map, fetch_list=fetch_list, return_numpy=False program,
feed=feed_map,
fetch_list=fetch_list,
return_numpy=False,
) )
self.op = op self.op = op
self.program = original_program self.program = original_program
...@@ -1381,6 +1259,7 @@ class OpTest(unittest.TestCase): ...@@ -1381,6 +1259,7 @@ class OpTest(unittest.TestCase):
Returns: Returns:
res (tuple(outs, fetch_list, feed_map, program, op_desc)): The results of given grad_op_desc. res (tuple(outs, fetch_list, feed_map, program, op_desc)): The results of given grad_op_desc.
""" """
with paddle.fluid.framework._dygraph_guard(None):
( (
fwd_outs, fwd_outs,
fwd_fetch_list, fwd_fetch_list,
...@@ -1530,8 +1409,18 @@ class OpTest(unittest.TestCase): ...@@ -1530,8 +1409,18 @@ class OpTest(unittest.TestCase):
check_dygraph=True, check_dygraph=True,
inplace_atol=None, inplace_atol=None,
check_eager=False, check_eager=False,
check_prim=False,
): ):
core._set_prim_all_enabled(False)
if check_prim:
prim_checker = PrimForwardChecker(self, place)
prim_checker.check()
# Support operators which not in the NO_FP64_CHECK_GRAD_OP_LIST list can be test prim with fp32
setattr(self.__class__, 'check_prim', True)
self.__class__.op_type = self.op_type
if prim_checker.is_only_check_prim():
self.only_prim = True
return
# disable legacy dygraph check when check_eager is True # disable legacy dygraph check when check_eager is True
if check_eager: if check_eager:
check_dygraph = False check_dygraph = False
...@@ -1990,6 +1879,7 @@ class OpTest(unittest.TestCase): ...@@ -1990,6 +1879,7 @@ class OpTest(unittest.TestCase):
check_dygraph=True, check_dygraph=True,
inplace_atol=None, inplace_atol=None,
check_eager=False, check_eager=False,
check_prim=False,
): ):
# disable legacy dygraph check when check_eager is True # disable legacy dygraph check when check_eager is True
...@@ -2013,7 +1903,10 @@ class OpTest(unittest.TestCase): ...@@ -2013,7 +1903,10 @@ class OpTest(unittest.TestCase):
check_dygraph, check_dygraph,
inplace_atol, inplace_atol,
check_eager=check_eager, check_eager=check_eager,
check_prim=check_prim,
) )
if hasattr(self, 'only_prim') and self.only_prim:
continue
if check_eager: if check_eager:
assert not check_dygraph assert not check_dygraph
outs, eager_dygraph_outs, fetch_list = res outs, eager_dygraph_outs, fetch_list = res
...@@ -2131,8 +2024,8 @@ class OpTest(unittest.TestCase): ...@@ -2131,8 +2024,8 @@ class OpTest(unittest.TestCase):
user_defined_grad_outputs=None, user_defined_grad_outputs=None,
check_dygraph=True, check_dygraph=True,
check_eager=False, check_eager=False,
check_prim=False,
): ):
# disable legacy dygraph check when check_eager is True # disable legacy dygraph check when check_eager is True
if check_eager: if check_eager:
check_dygraph = False check_dygraph = False
...@@ -2152,6 +2045,7 @@ class OpTest(unittest.TestCase): ...@@ -2152,6 +2045,7 @@ class OpTest(unittest.TestCase):
user_defined_grad_outputs, user_defined_grad_outputs,
check_dygraph, check_dygraph,
check_eager=check_eager, check_eager=check_eager,
check_prim=check_prim,
) )
def check_grad_with_place( def check_grad_with_place(
...@@ -2168,8 +2062,25 @@ class OpTest(unittest.TestCase): ...@@ -2168,8 +2062,25 @@ class OpTest(unittest.TestCase):
check_dygraph=True, check_dygraph=True,
numeric_place=None, numeric_place=None,
check_eager=False, check_eager=False,
check_prim=False,
): ):
core._set_prim_all_enabled(False)
if check_prim:
prim_grad_checker = PrimGradChecker(
self,
place,
inputs_to_check,
output_names,
no_grad_set,
user_defined_grad_outputs,
)
prim_grad_checker.check()
# Support operators which not in the NO_FP64_CHECK_GRAD_OP_LIST list can be test prim with fp32
setattr(self.__class__, 'check_prim', True)
self._check_grad_helper()
if prim_grad_checker.is_only_check_prim():
self.only_prim = True
return
# disable legacy dygraph check when check_eager is True # disable legacy dygraph check when check_eager is True
if check_eager: if check_eager:
check_dygraph = False check_dygraph = False
...@@ -2561,6 +2472,7 @@ class OpTest(unittest.TestCase): ...@@ -2561,6 +2472,7 @@ class OpTest(unittest.TestCase):
user_defined_grad_outputs=None, user_defined_grad_outputs=None,
parallel=False, parallel=False,
): ):
with paddle.fluid.framework._dygraph_guard(None):
prog = Program() prog = Program()
scope = core.Scope() scope = core.Scope()
block = prog.global_block() block = prog.global_block()
...@@ -2617,7 +2529,9 @@ class OpTest(unittest.TestCase): ...@@ -2617,7 +2529,9 @@ class OpTest(unittest.TestCase):
targets = [ targets = [
outputs[name] for name in outputs if name in output_names outputs[name] for name in outputs if name in output_names
] ]
inputs = [inputs[name] for name in input_to_check if name in inputs] inputs = [
inputs[name] for name in input_to_check if name in inputs
]
grad_inputs = paddle.static.gradients( grad_inputs = paddle.static.gradients(
targets, inputs, grad_outputs, no_grad_set targets, inputs, grad_outputs, no_grad_set
) )
...@@ -2632,14 +2546,19 @@ class OpTest(unittest.TestCase): ...@@ -2632,14 +2546,19 @@ class OpTest(unittest.TestCase):
) )
prog = compiled_prog prog = compiled_prog
executor = fluid.Executor(place) executor = fluid.Executor(place)
return list( res = list(
map( map(
np.array, np.array,
executor.run( executor.run(
prog, feed_dict, fetch_list, scope=scope, return_numpy=False prog,
feed_dict,
fetch_list,
scope=scope,
return_numpy=False,
), ),
) )
) )
return res
class OpTestTool: class OpTestTool:
......
此差异已折叠。
...@@ -28,13 +28,14 @@ from paddle.fluid import Program, core, program_guard ...@@ -28,13 +28,14 @@ from paddle.fluid import Program, core, program_guard
class TestExpandV2OpRank1(OpTest): class TestExpandV2OpRank1(OpTest):
def setUp(self): def setUp(self):
self.op_type = "expand_v2" self.op_type = "expand_v2"
self.prim_op_type = "prim"
self.init_data() self.init_data()
self.python_api = paddle.expand self.python_api = paddle.expand
self.inputs = {'X': np.random.random(self.ori_shape).astype("float64")} self.inputs = {'X': np.random.random(self.ori_shape).astype("float64")}
self.attrs = {'shape': self.shape} self.attrs = {'shape': self.shape}
output = np.tile(self.inputs['X'], self.expand_times) output = np.tile(self.inputs['X'], self.expand_times)
self.outputs = {'Out': output} self.outputs = {'Out': output}
self.enable_cinn = False
def init_data(self): def init_data(self):
self.ori_shape = [100] self.ori_shape = [100]
...@@ -42,10 +43,10 @@ class TestExpandV2OpRank1(OpTest): ...@@ -42,10 +43,10 @@ class TestExpandV2OpRank1(OpTest):
self.expand_times = [1] self.expand_times = [1]
def test_check_output(self): def test_check_output(self):
self.check_output() self.check_output(check_prim=True)
def test_check_grad(self): def test_check_grad(self):
self.check_grad(['X'], 'Out') self.check_grad(['X'], 'Out', check_prim=True)
class TestExpandV2OpRank2_DimExpanding(TestExpandV2OpRank1): class TestExpandV2OpRank2_DimExpanding(TestExpandV2OpRank1):
...@@ -80,6 +81,7 @@ class TestExpandV2OpRank4(TestExpandV2OpRank1): ...@@ -80,6 +81,7 @@ class TestExpandV2OpRank4(TestExpandV2OpRank1):
class TestExpandV2OpRank1_tensor_attr(OpTest): class TestExpandV2OpRank1_tensor_attr(OpTest):
def setUp(self): def setUp(self):
self.op_type = "expand_v2" self.op_type = "expand_v2"
self.prim_op_type = "prim"
self.python_api = paddle.expand self.python_api = paddle.expand
self.init_data() self.init_data()
expand_shapes_tensor = [] expand_shapes_tensor = []
...@@ -103,10 +105,10 @@ class TestExpandV2OpRank1_tensor_attr(OpTest): ...@@ -103,10 +105,10 @@ class TestExpandV2OpRank1_tensor_attr(OpTest):
self.infer_expand_shape = [-1] self.infer_expand_shape = [-1]
def test_check_output(self): def test_check_output(self):
self.check_output() self.check_output(check_prim=True)
def test_check_grad(self): def test_check_grad(self):
self.check_grad(['X'], 'Out') self.check_grad(['X'], 'Out', check_prim=True)
class TestExpandV2OpRank2_Corner_tensor_attr(TestExpandV2OpRank1_tensor_attr): class TestExpandV2OpRank2_Corner_tensor_attr(TestExpandV2OpRank1_tensor_attr):
...@@ -121,6 +123,7 @@ class TestExpandV2OpRank2_Corner_tensor_attr(TestExpandV2OpRank1_tensor_attr): ...@@ -121,6 +123,7 @@ class TestExpandV2OpRank2_Corner_tensor_attr(TestExpandV2OpRank1_tensor_attr):
class TestExpandV2OpRank1_tensor(OpTest): class TestExpandV2OpRank1_tensor(OpTest):
def setUp(self): def setUp(self):
self.op_type = "expand_v2" self.op_type = "expand_v2"
self.prim_op_type = "prim"
self.python_api = paddle.expand self.python_api = paddle.expand
self.init_data() self.init_data()
...@@ -148,6 +151,7 @@ class TestExpandV2OpRank1_tensor(OpTest): ...@@ -148,6 +151,7 @@ class TestExpandV2OpRank1_tensor(OpTest):
class TestExpandV2OpInteger(OpTest): class TestExpandV2OpInteger(OpTest):
def setUp(self): def setUp(self):
self.op_type = "expand_v2" self.op_type = "expand_v2"
self.prim_op_type = "prim"
self.python_api = paddle.expand self.python_api = paddle.expand
self.inputs = { self.inputs = {
'X': np.random.randint(10, size=(2, 4, 5)).astype("int32") 'X': np.random.randint(10, size=(2, 4, 5)).astype("int32")
...@@ -164,6 +168,7 @@ class TestExpandV2OpInteger(OpTest): ...@@ -164,6 +168,7 @@ class TestExpandV2OpInteger(OpTest):
class TestExpandV2OpBoolean(OpTest): class TestExpandV2OpBoolean(OpTest):
def setUp(self): def setUp(self):
self.op_type = "expand_v2" self.op_type = "expand_v2"
self.prim_op_type = "prim"
self.python_api = paddle.expand self.python_api = paddle.expand
self.inputs = {'X': np.random.randint(2, size=(2, 4, 5)).astype("bool")} self.inputs = {'X': np.random.randint(2, size=(2, 4, 5)).astype("bool")}
self.attrs = {'shape': [2, 4, 5]} self.attrs = {'shape': [2, 4, 5]}
...@@ -178,6 +183,7 @@ class TestExpandV2OpBoolean(OpTest): ...@@ -178,6 +183,7 @@ class TestExpandV2OpBoolean(OpTest):
class TestExpandV2OpInt64_t(OpTest): class TestExpandV2OpInt64_t(OpTest):
def setUp(self): def setUp(self):
self.op_type = "expand_v2" self.op_type = "expand_v2"
self.prim_op_type = "prim"
self.python_api = paddle.expand self.python_api = paddle.expand
self.inputs = { self.inputs = {
'X': np.random.randint(10, size=(2, 4, 5)).astype("int64") 'X': np.random.randint(10, size=(2, 4, 5)).astype("int64")
......
...@@ -76,10 +76,6 @@ class TestInputSpec(unittest.TestCase): ...@@ -76,10 +76,6 @@ class TestInputSpec(unittest.TestCase):
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
tensor_spec = InputSpec(4, dtype='int8') tensor_spec = InputSpec(4, dtype='int8')
# 3. len(shape) should be greater than 0.
with self.assertRaises(ValueError):
tensor_spec = InputSpec([], dtype='int8')
def test_batch_and_unbatch(self): def test_batch_and_unbatch(self):
tensor_spec = InputSpec([10]) tensor_spec = InputSpec([10])
# insert batch_size # insert batch_size
...@@ -90,15 +86,11 @@ class TestInputSpec(unittest.TestCase): ...@@ -90,15 +86,11 @@ class TestInputSpec(unittest.TestCase):
unbatch_spec = batch_tensor_spec.unbatch() unbatch_spec = batch_tensor_spec.unbatch()
self.assertEqual(unbatch_spec.shape, (10,)) self.assertEqual(unbatch_spec.shape, (10,))
# 1. `unbatch` requires len(shape) > 1 # 1. `batch` requires len(batch_size) == 1
with self.assertRaises(ValueError):
unbatch_spec.unbatch()
# 2. `batch` requires len(batch_size) == 1
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
tensor_spec.batch([16, 12]) tensor_spec.batch([16, 12])
# 3. `batch` requires type(batch_size) == int # 2. `batch` requires type(batch_size) == int
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
tensor_spec.batch('16') tensor_spec.batch('16')
......
...@@ -28,36 +28,25 @@ class TestSumOp(OpTest): ...@@ -28,36 +28,25 @@ class TestSumOp(OpTest):
def setUp(self): def setUp(self):
self.python_api = paddle.sum self.python_api = paddle.sum
self.op_type = "reduce_sum" self.op_type = "reduce_sum"
self.prim_op_type = "prim"
self.inputs = {'X': np.random.random((5, 6, 10)).astype("float64")} self.inputs = {'X': np.random.random((5, 6, 10)).astype("float64")}
self.outputs = {'Out': self.inputs['X'].sum(axis=0)} self.outputs = {'Out': self.inputs['X'].sum(axis=0)}
self.attrs = {'dim': [0]} self.attrs = {'dim': [0]}
# reduce doesn't support float64 in cinn
self.enable_cinn = False
def test_check_output(self): def test_check_output(self):
self.check_output(check_eager=True) self.check_output(check_eager=True)
def test_check_grad(self): def test_check_grad(self):
self.check_grad(['X'], 'Out', check_eager=True) self.check_grad(['X'], 'Out', check_eager=True, check_prim=True)
class TestSumOp_ZeroDim(OpTest):
def setUp(self):
self.python_api = paddle.sum
self.op_type = "reduce_sum"
self.inputs = {'X': np.random.random([]).astype("float64")}
self.outputs = {'Out': self.inputs['X'].sum(axis=None)}
self.attrs = {'dim': [], 'reduce_all': True}
def test_check_output(self):
self.check_output(check_eager=True)
def test_check_grad(self):
self.check_grad(['X'], 'Out', check_eager=True)
class TestSumOpFp32(OpTest):
class TestSumOp_fp16(OpTest):
def setUp(self): def setUp(self):
self.python_api = paddle.sum self.python_api = paddle.sum
self.op_type = "reduce_sum" self.op_type = "reduce_sum"
self.prim_op_type = "prim"
self.inputs = { self.inputs = {
'X': np.random.uniform(0, 0.1, (5, 6, 10)).astype("float16") 'X': np.random.uniform(0, 0.1, (5, 6, 10)).astype("float16")
} }
...@@ -66,6 +55,8 @@ class TestSumOp_fp16(OpTest): ...@@ -66,6 +55,8 @@ class TestSumOp_fp16(OpTest):
'Out': self.inputs['X'].sum(axis=tuple(self.attrs['dim'])) 'Out': self.inputs['X'].sum(axis=tuple(self.attrs['dim']))
} }
self.gradient = self.calc_gradient() self.gradient = self.calc_gradient()
# error occurred in cinn
self.enable_cinn = False
def test_check_output(self): def test_check_output(self):
self.check_output(check_eager=True) self.check_output(check_eager=True)
...@@ -77,10 +68,33 @@ class TestSumOp_fp16(OpTest): ...@@ -77,10 +68,33 @@ class TestSumOp_fp16(OpTest):
def test_check_grad(self): def test_check_grad(self):
self.check_grad( self.check_grad(
['X'], 'Out', user_defined_grads=self.gradient, check_eager=True ['X'],
'Out',
user_defined_grads=self.gradient,
check_eager=True,
check_prim=True,
) )
class TestSumOp_ZeroDim(OpTest):
def setUp(self):
self.python_api = paddle.sum
self.op_type = "reduce_sum"
self.prim_op_type = "prim"
self.inputs = {'X': np.random.random([]).astype("float64")}
self.outputs = {'Out': self.inputs['X'].sum(axis=None)}
self.attrs = {'dim': [], 'reduce_all': True}
# reduce doesn't support float64 in cinn.
# 0-D tensor doesn't support in cinn
self.enable_cinn = False
def test_check_output(self):
self.check_output(check_eager=True)
def test_check_grad(self):
self.check_grad(['X'], 'Out', check_eager=True)
@unittest.skipIf( @unittest.skipIf(
not core.is_compiled_with_cuda(), "core is not compiled with CUDA" not core.is_compiled_with_cuda(), "core is not compiled with CUDA"
) )
...@@ -89,6 +103,7 @@ class TestSumOp_bf16(OpTest): ...@@ -89,6 +103,7 @@ class TestSumOp_bf16(OpTest):
np.random.seed(100) np.random.seed(100)
self.python_api = paddle.sum self.python_api = paddle.sum
self.op_type = "reduce_sum" self.op_type = "reduce_sum"
self.prim_op_type = "prim"
self.dtype = np.uint16 self.dtype = np.uint16
self.x = np.random.uniform(0, 0.1, (2, 5, 10)).astype(np.float32) self.x = np.random.uniform(0, 0.1, (2, 5, 10)).astype(np.float32)
self.attrs = {'dim': [0, 1, 2]} self.attrs = {'dim': [0, 1, 2]}
...@@ -98,6 +113,7 @@ class TestSumOp_bf16(OpTest): ...@@ -98,6 +113,7 @@ class TestSumOp_bf16(OpTest):
self.inputs = {'X': convert_float_to_uint16(self.x)} self.inputs = {'X': convert_float_to_uint16(self.x)}
self.outputs = {'Out': convert_float_to_uint16(self.out)} self.outputs = {'Out': convert_float_to_uint16(self.out)}
self.gradient = self.calc_gradient() self.gradient = self.calc_gradient()
self.enable_cinn = False
def test_check_output(self): def test_check_output(self):
place = core.CUDAPlace(0) place = core.CUDAPlace(0)
...@@ -111,6 +127,7 @@ class TestSumOp_bf16(OpTest): ...@@ -111,6 +127,7 @@ class TestSumOp_bf16(OpTest):
'Out', 'Out',
user_defined_grads=self.gradient, user_defined_grads=self.gradient,
check_eager=True, check_eager=True,
check_prim=True,
) )
def calc_gradient(self): def calc_gradient(self):
...@@ -123,6 +140,7 @@ class TestSumOp_fp16_withInt(OpTest): ...@@ -123,6 +140,7 @@ class TestSumOp_fp16_withInt(OpTest):
def setUp(self): def setUp(self):
self.python_api = paddle.sum self.python_api = paddle.sum
self.op_type = "reduce_sum" self.op_type = "reduce_sum"
self.prim_op_type = "prim"
self.inputs = { self.inputs = {
# ref to https://en.wikipedia.org/wiki/Half-precision_floating-point_format # ref to https://en.wikipedia.org/wiki/Half-precision_floating-point_format
# Precision limitations on integer values between 0 and 2048 can be exactly represented # Precision limitations on integer values between 0 and 2048 can be exactly represented
...@@ -133,6 +151,7 @@ class TestSumOp_fp16_withInt(OpTest): ...@@ -133,6 +151,7 @@ class TestSumOp_fp16_withInt(OpTest):
'Out': self.inputs['X'].sum(axis=tuple(self.attrs['dim'])) 'Out': self.inputs['X'].sum(axis=tuple(self.attrs['dim']))
} }
self.gradient = self.calc_gradient() self.gradient = self.calc_gradient()
self.enable_cinn = False
def test_check_output(self): def test_check_output(self):
self.check_output(check_eager=True) self.check_output(check_eager=True)
...@@ -144,7 +163,11 @@ class TestSumOp_fp16_withInt(OpTest): ...@@ -144,7 +163,11 @@ class TestSumOp_fp16_withInt(OpTest):
def test_check_grad(self): def test_check_grad(self):
self.check_grad( self.check_grad(
['X'], 'Out', user_defined_grads=self.gradient, check_eager=True ['X'],
'Out',
user_defined_grads=self.gradient,
check_eager=True,
check_prim=True,
) )
...@@ -152,34 +175,40 @@ class TestSumOp5D(OpTest): ...@@ -152,34 +175,40 @@ class TestSumOp5D(OpTest):
def setUp(self): def setUp(self):
self.python_api = paddle.sum self.python_api = paddle.sum
self.op_type = "reduce_sum" self.op_type = "reduce_sum"
self.prim_op_type = "prim"
self.inputs = { self.inputs = {
'X': np.random.random((1, 2, 5, 6, 10)).astype("float64") 'X': np.random.random((1, 2, 5, 6, 10)).astype("float64")
} }
self.attrs = {'dim': [0]} self.attrs = {'dim': [0]}
self.outputs = {'Out': self.inputs['X'].sum(axis=0)} self.outputs = {'Out': self.inputs['X'].sum(axis=0)}
# error occurred in cinn
self.enable_cinn = False
def test_check_output(self): def test_check_output(self):
self.check_output(check_eager=True) self.check_output(check_eager=True)
def test_check_grad(self): def test_check_grad(self):
self.check_grad(['X'], 'Out', check_eager=True) self.check_grad(['X'], 'Out', check_eager=True, check_prim=True)
class TestSumOp6D(OpTest): class TestSumOp6D(OpTest):
def setUp(self): def setUp(self):
self.python_api = paddle.sum self.python_api = paddle.sum
self.op_type = "reduce_sum" self.op_type = "reduce_sum"
self.prim_op_type = "prim"
self.inputs = { self.inputs = {
'X': np.random.random((1, 1, 2, 5, 6, 10)).astype("float64") 'X': np.random.random((1, 1, 2, 5, 6, 10)).astype("float64")
} }
self.attrs = {'dim': [0]} self.attrs = {'dim': [0]}
self.outputs = {'Out': self.inputs['X'].sum(axis=0)} self.outputs = {'Out': self.inputs['X'].sum(axis=0)}
# error occurred in cinn
self.enable_cinn = False
def test_check_output(self): def test_check_output(self):
self.check_output(check_eager=True) self.check_output(check_eager=True)
def test_check_grad(self): def test_check_grad(self):
self.check_grad(['X'], 'Out', check_eager=True) self.check_grad(['X'], 'Out', check_eager=True, check_prim=True)
class TestSumOp8D(OpTest): class TestSumOp8D(OpTest):
...@@ -193,7 +222,7 @@ class TestSumOp8D(OpTest): ...@@ -193,7 +222,7 @@ class TestSumOp8D(OpTest):
self.outputs = {'Out': self.inputs['X'].sum(axis=(0, 3))} self.outputs = {'Out': self.inputs['X'].sum(axis=(0, 3))}
def test_check_output(self): def test_check_output(self):
self.check_output(check_eager=True) self.check_output()
def test_check_grad(self): def test_check_grad(self):
self.check_grad(['X'], 'Out', check_eager=True) self.check_grad(['X'], 'Out', check_eager=True)
...@@ -633,72 +662,100 @@ class TestAnyOpError(unittest.TestCase): ...@@ -633,72 +662,100 @@ class TestAnyOpError(unittest.TestCase):
class Test1DReduce(OpTest): class Test1DReduce(OpTest):
def setUp(self): def setUp(self):
self.op_type = "reduce_sum" self.op_type = "reduce_sum"
self.python_api = paddle.sum
self.prim_op_type = "prim"
self.inputs = {'X': np.random.random(120).astype("float64")} self.inputs = {'X': np.random.random(120).astype("float64")}
self.outputs = {'Out': self.inputs['X'].sum(axis=0)} self.outputs = {'Out': self.inputs['X'].sum(axis=0)}
# reduce doesn't support float64 in cinn.
self.enable_cinn = False
def test_check_output(self): def test_check_output(self):
self.check_output() self.check_output()
def test_check_grad(self): def test_check_grad(self):
self.check_grad(['X'], 'Out') self.check_grad(['X'], 'Out', check_prim=True)
class Test2DReduce0(Test1DReduce): class Test2DReduce0(Test1DReduce):
def setUp(self): def setUp(self):
self.op_type = "reduce_sum" self.op_type = "reduce_sum"
self.python_api = paddle.sum
self.prim_op_type = "prim"
self.attrs = {'dim': [0]} self.attrs = {'dim': [0]}
self.inputs = {'X': np.random.random((20, 10)).astype("float64")} self.inputs = {'X': np.random.random((20, 10)).astype("float64")}
self.outputs = {'Out': self.inputs['X'].sum(axis=0)} self.outputs = {'Out': self.inputs['X'].sum(axis=0)}
# reduce doesn't support float64 in cinn.
self.enable_cinn = False
class Test2DReduce1(Test1DReduce): class Test2DReduce1(Test1DReduce):
def setUp(self): def setUp(self):
self.op_type = "reduce_sum" self.op_type = "reduce_sum"
self.python_api = paddle.sum
self.prim_op_type = "prim"
self.attrs = {'dim': [1]} self.attrs = {'dim': [1]}
self.inputs = {'X': np.random.random((20, 10)).astype("float64")} self.inputs = {'X': np.random.random((20, 10)).astype("float64")}
self.outputs = { self.outputs = {
'Out': self.inputs['X'].sum(axis=tuple(self.attrs['dim'])) 'Out': self.inputs['X'].sum(axis=tuple(self.attrs['dim']))
} }
# reduce doesn't support float64 in cinn.
self.enable_cinn = False
class Test3DReduce0(Test1DReduce): class Test3DReduce0(Test1DReduce):
def setUp(self): def setUp(self):
self.op_type = "reduce_sum" self.op_type = "reduce_sum"
self.python_api = paddle.sum
self.prim_op_type = "prim"
self.attrs = {'dim': [1]} self.attrs = {'dim': [1]}
self.inputs = {'X': np.random.random((5, 6, 7)).astype("float64")} self.inputs = {'X': np.random.random((5, 6, 7)).astype("float64")}
self.outputs = { self.outputs = {
'Out': self.inputs['X'].sum(axis=tuple(self.attrs['dim'])) 'Out': self.inputs['X'].sum(axis=tuple(self.attrs['dim']))
} }
# reduce doesn't support float64 in cinn.
self.enable_cinn = False
class Test3DReduce1(Test1DReduce): class Test3DReduce1(Test1DReduce):
def setUp(self): def setUp(self):
self.op_type = "reduce_sum" self.op_type = "reduce_sum"
self.python_api = paddle.sum
self.prim_op_type = "prim"
self.attrs = {'dim': [2]} self.attrs = {'dim': [2]}
self.inputs = {'X': np.random.random((5, 6, 7)).astype("float64")} self.inputs = {'X': np.random.random((5, 6, 7)).astype("float64")}
self.outputs = { self.outputs = {
'Out': self.inputs['X'].sum(axis=tuple(self.attrs['dim'])) 'Out': self.inputs['X'].sum(axis=tuple(self.attrs['dim']))
} }
# reduce doesn't support float64 in cinn.
self.enable_cinn = False
class Test3DReduce2(Test1DReduce): class Test3DReduce2(Test1DReduce):
def setUp(self): def setUp(self):
self.op_type = "reduce_sum" self.op_type = "reduce_sum"
self.python_api = paddle.sum
self.prim_op_type = "prim"
self.attrs = {'dim': [-2]} self.attrs = {'dim': [-2]}
self.inputs = {'X': np.random.random((5, 6, 7)).astype("float64")} self.inputs = {'X': np.random.random((5, 6, 7)).astype("float64")}
self.outputs = { self.outputs = {
'Out': self.inputs['X'].sum(axis=tuple(self.attrs['dim'])) 'Out': self.inputs['X'].sum(axis=tuple(self.attrs['dim']))
} }
# reduce doesn't support float64 in cinn.
self.enable_cinn = False
class Test3DReduce3(Test1DReduce): class Test3DReduce3(Test1DReduce):
def setUp(self): def setUp(self):
self.op_type = "reduce_sum" self.op_type = "reduce_sum"
self.python_api = paddle.sum
self.prim_op_type = "prim"
self.attrs = {'dim': [1, 2]} self.attrs = {'dim': [1, 2]}
self.inputs = {'X': np.random.random((5, 6, 7)).astype("float64")} self.inputs = {'X': np.random.random((5, 6, 7)).astype("float64")}
self.outputs = { self.outputs = {
'Out': self.inputs['X'].sum(axis=tuple(self.attrs['dim'])) 'Out': self.inputs['X'].sum(axis=tuple(self.attrs['dim']))
} }
# reduce doesn't support float64 in cinn.
self.enable_cinn = False
class Test8DReduce0(Test1DReduce): class Test8DReduce0(Test1DReduce):
...@@ -712,10 +769,18 @@ class Test8DReduce0(Test1DReduce): ...@@ -712,10 +769,18 @@ class Test8DReduce0(Test1DReduce):
'Out': self.inputs['X'].sum(axis=tuple(self.attrs['dim'])) 'Out': self.inputs['X'].sum(axis=tuple(self.attrs['dim']))
} }
def test_check_output(self):
self.check_output()
def test_check_grad(self):
self.check_grad(['X'], 'Out')
class TestKeepDimReduce(Test1DReduce): class TestKeepDimReduce(Test1DReduce):
def setUp(self): def setUp(self):
self.op_type = "reduce_sum" self.op_type = "reduce_sum"
self.python_api = paddle.sum
self.prim_op_type = "prim"
self.inputs = {'X': np.random.random((5, 6, 10)).astype("float64")} self.inputs = {'X': np.random.random((5, 6, 10)).astype("float64")}
self.attrs = {'dim': [1], 'keep_dim': True} self.attrs = {'dim': [1], 'keep_dim': True}
self.outputs = { self.outputs = {
...@@ -723,6 +788,8 @@ class TestKeepDimReduce(Test1DReduce): ...@@ -723,6 +788,8 @@ class TestKeepDimReduce(Test1DReduce):
axis=tuple(self.attrs['dim']), keepdims=self.attrs['keep_dim'] axis=tuple(self.attrs['dim']), keepdims=self.attrs['keep_dim']
) )
} }
# reduce doesn't support float64 in cinn.
self.enable_cinn = False
class TestKeepDim8DReduce(Test1DReduce): class TestKeepDim8DReduce(Test1DReduce):
...@@ -738,6 +805,12 @@ class TestKeepDim8DReduce(Test1DReduce): ...@@ -738,6 +805,12 @@ class TestKeepDim8DReduce(Test1DReduce):
) )
} }
def test_check_output(self):
self.check_output()
def test_check_grad(self):
self.check_grad(['X'], 'Out')
@skip_check_grad_ci( @skip_check_grad_ci(
reason="reduce_max is discontinuous non-derivable function," reason="reduce_max is discontinuous non-derivable function,"
...@@ -782,6 +855,8 @@ class TestReduceMinOpMultiAxises(OpTest): ...@@ -782,6 +855,8 @@ class TestReduceMinOpMultiAxises(OpTest):
class TestKeepDimReduceSumMultiAxises(OpTest): class TestKeepDimReduceSumMultiAxises(OpTest):
def setUp(self): def setUp(self):
self.op_type = "reduce_sum" self.op_type = "reduce_sum"
self.python_api = paddle.sum
self.prim_op_type = "prim"
self.inputs = {'X': np.random.random((5, 6, 10)).astype("float64")} self.inputs = {'X': np.random.random((5, 6, 10)).astype("float64")}
self.attrs = {'dim': [-2, -1], 'keep_dim': True} self.attrs = {'dim': [-2, -1], 'keep_dim': True}
self.outputs = { self.outputs = {
...@@ -794,12 +869,15 @@ class TestKeepDimReduceSumMultiAxises(OpTest): ...@@ -794,12 +869,15 @@ class TestKeepDimReduceSumMultiAxises(OpTest):
self.check_output() self.check_output()
def test_check_grad(self): def test_check_grad(self):
# rev_comp error
self.check_grad(['X'], 'Out') self.check_grad(['X'], 'Out')
class TestReduceSumWithDimOne(OpTest): class TestReduceSumWithDimOne(OpTest):
def setUp(self): def setUp(self):
self.op_type = "reduce_sum" self.op_type = "reduce_sum"
self.python_api = paddle.sum
self.prim_op_type = "prim"
self.inputs = {'X': np.random.random((100, 1, 1)).astype("float64")} self.inputs = {'X': np.random.random((100, 1, 1)).astype("float64")}
self.attrs = {'dim': [1, 2], 'keep_dim': True} self.attrs = {'dim': [1, 2], 'keep_dim': True}
self.outputs = { self.outputs = {
...@@ -807,17 +885,21 @@ class TestReduceSumWithDimOne(OpTest): ...@@ -807,17 +885,21 @@ class TestReduceSumWithDimOne(OpTest):
axis=tuple(self.attrs['dim']), keepdims=True axis=tuple(self.attrs['dim']), keepdims=True
) )
} }
# reduce doesn't support float64 in cinn
self.enable_cinn = False
def test_check_output(self): def test_check_output(self):
self.check_output() self.check_output()
def test_check_grad(self): def test_check_grad(self):
self.check_grad(['X'], 'Out') self.check_grad(['X'], 'Out', check_prim=True)
class TestReduceSumWithNumelOne(OpTest): class TestReduceSumWithNumelOne(OpTest):
def setUp(self): def setUp(self):
self.op_type = "reduce_sum" self.op_type = "reduce_sum"
self.python_api = paddle.sum
self.prim_op_type = "prim"
self.inputs = {'X': np.random.random((100, 1)).astype("float64")} self.inputs = {'X': np.random.random((100, 1)).astype("float64")}
self.attrs = {'dim': [1], 'keep_dim': False} self.attrs = {'dim': [1], 'keep_dim': False}
self.outputs = { self.outputs = {
...@@ -825,45 +907,74 @@ class TestReduceSumWithNumelOne(OpTest): ...@@ -825,45 +907,74 @@ class TestReduceSumWithNumelOne(OpTest):
axis=tuple(self.attrs['dim']), keepdims=False axis=tuple(self.attrs['dim']), keepdims=False
) )
} }
# reduce doesn't support float64 in cinn
self.enable_cinn = False
def test_check_output(self): def test_check_output(self):
self.check_output() self.check_output()
def test_check_grad(self): def test_check_grad(self):
self.check_grad(['X'], 'Out') self.check_grad(['X'], 'Out', check_prim=False)
class TestReduceAll(OpTest): class TestReduceAll(OpTest):
def setUp(self): def setUp(self):
self.op_type = "reduce_sum" self.op_type = "reduce_sum"
self.python_api = paddle.sum
self.prim_op_type = "prim"
self.inputs = {'X': np.random.random((100, 1, 1)).astype("float64")} self.inputs = {'X': np.random.random((100, 1, 1)).astype("float64")}
self.attrs = {'reduce_all': True, 'keep_dim': False} self.attrs = {'reduce_all': True, 'keep_dim': False}
self.outputs = {'Out': self.inputs['X'].sum()} self.outputs = {'Out': self.inputs['X'].sum()}
# reduce doesn't support float64 in cinn
self.enable_cinn = False
def test_check_output(self): def test_check_output(self):
self.check_output() self.check_output()
def test_check_grad(self): def test_check_grad(self):
self.check_grad(['X'], 'Out') self.check_grad(['X'], 'Out', check_prim=True)
class TestReduceAllFp32(OpTest):
def setUp(self):
self.op_type = "reduce_sum"
self.python_api = paddle.sum
self.prim_op_type = "prim"
self.inputs = {'X': np.random.random((100, 1, 1)).astype("float32")}
self.attrs = {'reduce_all': True, 'keep_dim': False}
self.outputs = {'Out': self.inputs['X'].sum()}
# reduce doesn't support float64 in cinn
self.enable_cinn = False
def test_check_output(self):
self.check_output()
def test_check_grad(self):
self.check_grad(['X'], 'Out', check_prim=True)
class Test1DReduceWithAxes1(OpTest): class Test1DReduceWithAxes1(OpTest):
def setUp(self): def setUp(self):
self.op_type = "reduce_sum" self.op_type = "reduce_sum"
self.python_api = paddle.sum
self.prim_op_type = "prim"
self.inputs = {'X': np.random.random(100).astype("float64")} self.inputs = {'X': np.random.random(100).astype("float64")}
self.attrs = {'dim': [0], 'keep_dim': False} self.attrs = {'dim': [0], 'keep_dim': False}
self.outputs = {'Out': self.inputs['X'].sum(axis=0)} self.outputs = {'Out': self.inputs['X'].sum(axis=0)}
self.enable_cinn = False
def test_check_output(self): def test_check_output(self):
self.check_output() self.check_output(check_prim=True)
def test_check_grad(self): def test_check_grad(self):
self.check_grad(['X'], 'Out') self.check_grad(['X'], 'Out', check_prim=True)
class TestReduceWithDtype(OpTest): class TestReduceWithDtype(OpTest):
def setUp(self): def setUp(self):
self.op_type = "reduce_sum" self.op_type = "reduce_sum"
self.python_api = paddle.sum
self.prim_op_type = "prim"
self.inputs = {'X': np.random.random((6, 2, 10)).astype("float64")} self.inputs = {'X': np.random.random((6, 2, 10)).astype("float64")}
self.outputs = {'Out': self.inputs['X'].sum().astype('float64')} self.outputs = {'Out': self.inputs['X'].sum().astype('float64')}
self.attrs = {'reduce_all': True} self.attrs = {'reduce_all': True}
...@@ -873,17 +984,26 @@ class TestReduceWithDtype(OpTest): ...@@ -873,17 +984,26 @@ class TestReduceWithDtype(OpTest):
'out_dtype': int(convert_np_dtype_to_dtype_(np.float64)), 'out_dtype': int(convert_np_dtype_to_dtype_(np.float64)),
} }
) )
self.enable_cinn = False
def test_check_output(self): def test_check_output(self):
self.check_output() self.check_output(check_prim=True)
def test_check_grad(self): def test_check_grad(self):
self.check_grad(['X'], 'Out') self.check_grad(['X'], 'Out', check_prim=True)
def reduce_sum_wrapper(
x, axis=None, dtype_rename=None, keepdim=False, name=None
):
return paddle.sum(x, axis, "float64", keepdim, name)
class TestReduceWithDtype1(TestReduceWithDtype): class TestReduceWithDtype1(TestReduceWithDtype):
def setUp(self): def setUp(self):
self.op_type = "reduce_sum" self.op_type = "reduce_sum"
self.python_api = reduce_sum_wrapper
self.prim_op_type = "prim"
self.inputs = {'X': np.random.random((6, 2, 10)).astype("float64")} self.inputs = {'X': np.random.random((6, 2, 10)).astype("float64")}
self.outputs = {'Out': self.inputs['X'].sum(axis=1)} self.outputs = {'Out': self.inputs['X'].sum(axis=1)}
self.attrs = {'dim': [1]} self.attrs = {'dim': [1]}
...@@ -893,11 +1013,20 @@ class TestReduceWithDtype1(TestReduceWithDtype): ...@@ -893,11 +1013,20 @@ class TestReduceWithDtype1(TestReduceWithDtype):
'out_dtype': int(convert_np_dtype_to_dtype_(np.float64)), 'out_dtype': int(convert_np_dtype_to_dtype_(np.float64)),
} }
) )
self.enable_cinn = False
def test_check_output(self):
self.check_output()
def test_check_grad(self):
self.check_grad(['X'], 'Out', check_prim=True)
class TestReduceWithDtype2(TestReduceWithDtype): class TestReduceWithDtype2(TestReduceWithDtype):
def setUp(self): def setUp(self):
self.op_type = "reduce_sum" self.op_type = "reduce_sum"
self.prim_op_type = "prim"
self.python_api = reduce_sum_wrapper
self.inputs = {'X': np.random.random((6, 2, 10)).astype("float64")} self.inputs = {'X': np.random.random((6, 2, 10)).astype("float64")}
self.outputs = {'Out': self.inputs['X'].sum(axis=1, keepdims=True)} self.outputs = {'Out': self.inputs['X'].sum(axis=1, keepdims=True)}
self.attrs = {'dim': [1], 'keep_dim': True} self.attrs = {'dim': [1], 'keep_dim': True}
...@@ -907,6 +1036,13 @@ class TestReduceWithDtype2(TestReduceWithDtype): ...@@ -907,6 +1036,13 @@ class TestReduceWithDtype2(TestReduceWithDtype):
'out_dtype': int(convert_np_dtype_to_dtype_(np.float64)), 'out_dtype': int(convert_np_dtype_to_dtype_(np.float64)),
} }
) )
self.enable_cinn = False
def test_check_output(self):
self.check_output()
def test_check_grad(self):
self.check_grad(['X'], 'Out', check_prim=True)
class TestReduceSumOpError(unittest.TestCase): class TestReduceSumOpError(unittest.TestCase):
......
...@@ -43,12 +43,6 @@ def ref_softmax(x, axis=None, dtype=None): ...@@ -43,12 +43,6 @@ def ref_softmax(x, axis=None, dtype=None):
return np.apply_along_axis(stable_softmax, axis, x_t) return np.apply_along_axis(stable_softmax, axis, x_t)
def softmax_wrapper(
x, axis=-1, dtype=None, name=None, use_cudnn=False, use_mkldnn=False
):
return paddle.nn.functional.softmax(x, axis=axis, dtype=dtype)
class TestSoftmaxOp(OpTest): class TestSoftmaxOp(OpTest):
def get_x_shape(self): def get_x_shape(self):
return [10, 10] return [10, 10]
...@@ -58,7 +52,8 @@ class TestSoftmaxOp(OpTest): ...@@ -58,7 +52,8 @@ class TestSoftmaxOp(OpTest):
def setUp(self): def setUp(self):
self.op_type = "softmax" self.op_type = "softmax"
self.python_api = softmax_wrapper self.prim_op_type = "comp"
self.python_api = F.softmax
self.use_cudnn = False self.use_cudnn = False
self.use_mkldnn = False self.use_mkldnn = False
# explicilty use float32 for ROCm, as MIOpen does not yet support float64 # explicilty use float32 for ROCm, as MIOpen does not yet support float64
...@@ -78,6 +73,7 @@ class TestSoftmaxOp(OpTest): ...@@ -78,6 +73,7 @@ class TestSoftmaxOp(OpTest):
'use_cudnn': self.use_cudnn, 'use_cudnn': self.use_cudnn,
'use_mkldnn': self.use_mkldnn, 'use_mkldnn': self.use_mkldnn,
} }
self.enable_cinn = False
def init_kernel_type(self): def init_kernel_type(self):
pass pass
...@@ -86,11 +82,9 @@ class TestSoftmaxOp(OpTest): ...@@ -86,11 +82,9 @@ class TestSoftmaxOp(OpTest):
# TODO(wangzhongpu): support mkldnn op in dygraph mode # TODO(wangzhongpu): support mkldnn op in dygraph mode
if self.use_cudnn: if self.use_cudnn:
place = core.CUDAPlace(0) place = core.CUDAPlace(0)
self.check_output_with_place( self.check_output_with_place(place, atol=1e-5)
place, atol=1e-5, check_dygraph=(not self.use_mkldnn)
)
else: else:
self.check_output(check_dygraph=(not self.use_mkldnn)) self.check_output(check_prim=True)
def test_check_grad(self): def test_check_grad(self):
# TODO(wangzhongpu): support mkldnn op in dygraph mode # TODO(wangzhongpu): support mkldnn op in dygraph mode
...@@ -110,13 +104,20 @@ class TestSoftmaxOp(OpTest): ...@@ -110,13 +104,20 @@ class TestSoftmaxOp(OpTest):
"Out", "Out",
max_relative_error=0.01, max_relative_error=0.01,
check_dygraph=(not self.use_mkldnn), check_dygraph=(not self.use_mkldnn),
check_prim=True,
) )
class TestSoftmaxOpfp32(TestSoftmaxOp):
def init_kernel_type(self):
self.dtype = np.float32
class TestSoftmaxOp_ZeroDim1(TestSoftmaxOp): class TestSoftmaxOp_ZeroDim1(TestSoftmaxOp):
def setUp(self): def setUp(self):
self.op_type = "softmax" self.op_type = "softmax"
self.python_api = softmax_wrapper self.prim_op_type = "comp"
self.python_api = F.softmax
self.use_cudnn = False self.use_cudnn = False
self.use_mkldnn = False self.use_mkldnn = False
# explicilty use float32 for ROCm, as MIOpen does not yet support float64 # explicilty use float32 for ROCm, as MIOpen does not yet support float64
...@@ -133,6 +134,15 @@ class TestSoftmaxOp_ZeroDim1(TestSoftmaxOp): ...@@ -133,6 +134,15 @@ class TestSoftmaxOp_ZeroDim1(TestSoftmaxOp):
'use_cudnn': self.use_cudnn, 'use_cudnn': self.use_cudnn,
'use_mkldnn': self.use_mkldnn, 'use_mkldnn': self.use_mkldnn,
} }
self.enable_cinn = False
def test_check_output(self):
# TODO(wangzhongpu): support mkldnn op in dygraph mode
if self.use_cudnn:
place = core.CUDAPlace(0)
self.check_output_with_place(place, atol=1e-5)
else:
self.check_output(check_prim=True)
@unittest.skipIf( @unittest.skipIf(
...@@ -141,7 +151,7 @@ class TestSoftmaxOp_ZeroDim1(TestSoftmaxOp): ...@@ -141,7 +151,7 @@ class TestSoftmaxOp_ZeroDim1(TestSoftmaxOp):
class TestSoftmaxOp_ZeroDim2(TestSoftmaxOp): class TestSoftmaxOp_ZeroDim2(TestSoftmaxOp):
def setUp(self): def setUp(self):
self.op_type = "softmax" self.op_type = "softmax"
self.python_api = softmax_wrapper self.python_api = F.softmax
self.use_cudnn = True self.use_cudnn = True
self.use_mkldnn = False self.use_mkldnn = False
# explicilty use float32 for ROCm, as MIOpen does not yet support float64 # explicilty use float32 for ROCm, as MIOpen does not yet support float64
...@@ -158,6 +168,15 @@ class TestSoftmaxOp_ZeroDim2(TestSoftmaxOp): ...@@ -158,6 +168,15 @@ class TestSoftmaxOp_ZeroDim2(TestSoftmaxOp):
'use_cudnn': self.use_cudnn, 'use_cudnn': self.use_cudnn,
'use_mkldnn': self.use_mkldnn, 'use_mkldnn': self.use_mkldnn,
} }
self.enable_cinn = False
def test_check_output(self):
# TODO(wangzhongpu): support mkldnn op in dygraph mode
if self.use_cudnn:
place = core.CUDAPlace(0)
self.check_output_with_place(place, atol=1e-5)
else:
self.check_output(check_prim=True)
class TestSoftmaxOp2(TestSoftmaxOp): class TestSoftmaxOp2(TestSoftmaxOp):
...@@ -375,7 +394,7 @@ class TestSoftmaxFP16CUDNNOp2(TestSoftmaxFP16CUDNNOp): ...@@ -375,7 +394,7 @@ class TestSoftmaxFP16CUDNNOp2(TestSoftmaxFP16CUDNNOp):
class TestSoftmaxBF16Op(OpTest): class TestSoftmaxBF16Op(OpTest):
def setUp(self): def setUp(self):
self.op_type = "softmax" self.op_type = "softmax"
self.python_api = softmax_wrapper self.python_api = F.softmax
self.use_cudnn = self.init_cudnn() self.use_cudnn = self.init_cudnn()
self.use_mkldnn = False self.use_mkldnn = False
self.dtype = np.uint16 self.dtype = np.uint16
......
...@@ -1243,6 +1243,9 @@ class ProgramCache: ...@@ -1243,6 +1243,9 @@ class ProgramCache:
def concrete_programs(self): def concrete_programs(self):
return [cp for key, (cp, _) in self._caches.items()] return [cp for key, (cp, _) in self._caches.items()]
def clear(self):
self._caches = collections.OrderedDict()
class ProgramTranslator: class ProgramTranslator:
""" """
......
...@@ -298,12 +298,6 @@ class InputSpec: ...@@ -298,12 +298,6 @@ class InputSpec:
type(shape).__name__ type(shape).__name__
) )
) )
if len(shape) == 0:
raise ValueError(
"`shape` in InputSpec should contain at least 1 element, but received {}.".format(
shape
)
)
for i, ele in enumerate(shape): for i, ele in enumerate(shape):
if ele is not None: if ele is not None:
......
...@@ -1265,6 +1265,7 @@ def sum(x, axis=None, dtype=None, keepdim=False, name=None): ...@@ -1265,6 +1265,7 @@ def sum(x, axis=None, dtype=None, keepdim=False, name=None):
'x', 'x',
[ [
'bool', 'bool',
'uint16',
'float16', 'float16',
'float32', 'float32',
'float64', 'float64',
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册