From 99c43a0d6694bb4fd5961178af48c785f1c1a8ba Mon Sep 17 00:00:00 2001 From: zhwesky2010 <1183042833@qq.com> Date: Tue, 12 Sep 2023 18:54:36 +0800 Subject: [PATCH] fix many bug and optimize code (#280) --- paconvert/api_alias_mapping.json | 8 + paconvert/api_mapping.json | 240 ++++-- paconvert/api_matcher.py | 727 ++++-------------- paconvert/attribute_mapping.json | 2 +- paconvert/base.py | 28 +- paconvert/transformer/basic_transformer.py | 76 +- .../paddle_code/api_paddle_io_BatchSampler.py | 25 +- tests/test_Tensor_bernoulli_.py | 21 +- tests/test_Tensor_div.py | 26 + tests/test_Tensor_divide.py | 78 -- tests/test_Tensor_logical_and.py | 3 +- tests/test_Tensor_logical_or.py | 3 +- tests/test_Tensor_logical_xor.py | 3 +- ...{test_Tensor_suv.py => test_Tensor_svd.py} | 58 +- tests/test_Tensor_true_divide.py | 28 +- tests/test_Tensor_unique.py | 12 +- tests/test_arange.py | 11 + tests/test_div.py | 27 + tests/test_divide.py | 91 --- tests/test_logaddexp.py | 4 +- tests/test_logical_and.py | 3 +- tests/test_logical_or.py | 3 +- tests/test_logical_xor.py | 3 +- tests/test_logsumexp.py | 3 +- tests/test_mul.py | 36 +- tests/test_nn_functional_batch_norm.py | 74 +- tests/test_nn_functional_instance_norm.py | 49 +- tests/test_nn_functional_upsample_bilinear.py | 30 +- tests/test_special_logsumexp.py | 3 +- tests/test_trapezoid.py | 24 + tests/test_true_divide.py | 12 +- tests/test_unique.py | 5 +- tests/test_utils_data_BatchSampler.py | 31 +- tests/test_utils_data_SequentialSampler.py | 51 +- tests/test_utils_data_random_split.py | 12 +- 35 files changed, 782 insertions(+), 1028 deletions(-) delete mode 100644 tests/test_Tensor_divide.py rename tests/{test_Tensor_suv.py => test_Tensor_svd.py} (64%) delete mode 100644 tests/test_divide.py diff --git a/paconvert/api_alias_mapping.json b/paconvert/api_alias_mapping.json index 01a789f28..ad06148c4 100644 --- a/paconvert/api_alias_mapping.json +++ b/paconvert/api_alias_mapping.json @@ -1,4 +1,5 @@ { + "torch.Tensor.divide": "torch.Tensor.div", "torch.amp.autocast_mode.autocast": "torch.amp.autocast", "torch.autograd.function.Function": "torch.autograd.Function", "torch.cuda.amp.autocast_mode.autocast": "torch.cuda.amp.autocast", @@ -32,6 +33,8 @@ "torch.distributions.transforms.StickBreakingTransform": "torch.distributions.StickBreakingTransform", "torch.distributions.transforms.Transform": "torch.distributions.Transform", "torch.distributions.uniform.Uniform": "torch.distributions.Uniform", + "torch.divide": "torch.div", + "torch.matrix_exp": "torch.linalg.matrix_exp", "torch.nn.NLLLoss2d": "torch.nn.NLLLoss", "torch.nn.modules.AvgPool1d": "torch.nn.modules.AvgPool2d", "torch.nn.modules.AvgPool2d": "torch.nn.modules.AvgPool2d", @@ -73,14 +76,19 @@ "torch.nn.parallel.distributed.DistributedDataParallel": "torch.nn.parallel.DistributedDataParallel", "torch.nn.parameter.Parameter": "torch.nn.Parameter", "torch.nn.parameter.UninitializedParameter": "torch.nn.UninitializedParameter", + "torch.nn.utils.clip_grad_norm": "torch.nn.utils.clip_grad_norm_", "torch.optim.sgd.SGD": "torch.optim.SGD", + "torch.orgqr": "torch.linalg.householder_product", "torch.random.get_rng_state": "torch.get_rng_state", "torch.random.initial_seed": "torch.initial_seed", "torch.random.manual_seed": "torch.manual_seed", "torch.random.seed": "torch.seed", "torch.random.set_rng_state": "torch.set_rng_state", + "torch.special.gammainc": "torch.igamma", + "torch.special.gammaincc": "torch.igammac", "torch.torch.Tensor": "torch.Tensor", "torch.torch.finfo": "torch.finfo", + "torch.trapz": "torch.trapezoid", "torch.utils.data._utils.collate.default_collate": "torch.utils.data.default_collate", "torch.utils.data.dataloader.DataLoader": "torch.utils.data.DataLoader", "torch.utils.data.dataloader.default_collate": "torch.utils.data.default_collate", diff --git a/paconvert/api_mapping.json b/paconvert/api_mapping.json index 1a75f4314..b8931603f 100644 --- a/paconvert/api_mapping.json +++ b/paconvert/api_mapping.json @@ -360,14 +360,14 @@ ] }, "torch.Tensor.baddbmm_": {}, - "torch.Tensor.bernoulli": {}, - "torch.Tensor.bernoulli_": { - "Matcher": "TensorBernoulli_Matcher", + "torch.Tensor.bernoulli": { + "Matcher": "TensorFunc2PaddleFunc", + "paddle_api": "paddle.bernoulli", "args_list": [ - "p", "generator" ] }, + "torch.Tensor.bernoulli_": {}, "torch.Tensor.bfloat16": { "Matcher": "TensorBF16Matcher", "args_list": [ @@ -660,10 +660,14 @@ }, "torch.Tensor.detach_": {}, "torch.Tensor.diag": { - "Matcher": "TensorDiagMatcher", + "Matcher": "TensorFunc2PaddleFunc", + "paddle_api": "paddle.diag", "args_list": [ "diagonal" - ] + ], + "kwargs_change": { + "diagonal": "offset" + } }, "torch.Tensor.diag_embed": { "Matcher": "TensorFunc2PaddleFunc", @@ -736,13 +740,6 @@ ] }, "torch.Tensor.div_": {}, - "torch.Tensor.divide": { - "Matcher": "DivMatcher", - "args_list": [ - "other", - "rounding_mode" - ] - }, "torch.Tensor.divide_": {}, "torch.Tensor.dot": { "Matcher": "GenericMatcher", @@ -1044,10 +1041,14 @@ ] }, "torch.Tensor.hardshrink": { - "Matcher": "TensorHardShrinkMatcher", + "Matcher": "TensorFunc2PaddleFunc", + "paddle_api": "paddle.nn.functional.hardshrink", "args_list": [ "lambd" - ] + ], + "kwargs_change": { + "lambd": "threshold" + } }, "torch.Tensor.heaviside": { "Matcher": "GenericMatcher", @@ -1060,8 +1061,8 @@ } }, "torch.Tensor.histc": { + "Matcher": "HistcMatcher", "paddle_api": "paddle.Tensor.histogram", - "Matcher": "TensorHistcMatcher", "args_list": [ "bins", "min", @@ -1363,11 +1364,14 @@ "args_list": [] }, "torch.Tensor.logical_and": { - "Matcher": "TensorLogicalMatcher", + "Matcher": "GenericMatcher", "paddle_api": "paddle.Tensor.logical_and", "args_list": [ "other" - ] + ], + "kwargs_change": { + "other": "y" + } }, "torch.Tensor.logical_and_": {}, "torch.Tensor.logical_not": { @@ -1376,19 +1380,25 @@ }, "torch.Tensor.logical_not_": {}, "torch.Tensor.logical_or": { - "Matcher": "TensorLogicalMatcher", + "Matcher": "GenericMatcher", "paddle_api": "paddle.Tensor.logical_or", "args_list": [ "other" - ] + ], + "kwargs_change": { + "other": "y" + } }, "torch.Tensor.logical_or_": {}, "torch.Tensor.logical_xor": { - "Matcher": "TensorLogicalMatcher", + "Matcher": "GenericMatcher", "paddle_api": "paddle.Tensor.logical_xor", "args_list": [ "other" - ] + ], + "kwargs_change": { + "other": "y" + } }, "torch.Tensor.logical_xor_": {}, "torch.Tensor.logit": { @@ -1977,7 +1987,7 @@ }, "torch.Tensor.requires_grad": {}, "torch.Tensor.requires_grad_": { - "Matcher": "TensorRequiresGradMatcher", + "Matcher": "TensorRequiresGrad_Matcher", "args_list": [ "requires_grad" ] @@ -2290,10 +2300,14 @@ }, "torch.Tensor.sum_to_size": {}, "torch.Tensor.svd": { - "Matcher": "TensorSVDMatcher", + "Matcher": "SvdMatcher", + "paddle_api": "paddle.linalg.svd", "args_list": [ "some", "compute_uv" + ], + "unsupport_args": [ + "compute_uv" ] }, "torch.Tensor.swapaxes": { @@ -2415,10 +2429,10 @@ "Matcher": "GenericMatcher", "paddle_api": "paddle.Tensor.divide", "args_list": [ - "value" + "other" ], "kwargs_change": { - "value": "y" + "other": "y" } }, "torch.Tensor.true_divide_": {}, @@ -2452,11 +2466,16 @@ } }, "torch.Tensor.unflatten": { - "Matcher": "UnflattenMatcher", + "Matcher": "GenericMatcher", + "paddle_api": "paddle.Tensor.unflatten", "args_list": [ "dim", "sizes" - ] + ], + "kwargs_change": { + "dim": "axis", + "sizes": "shape" + } }, "torch.Tensor.unfold": {}, "torch.Tensor.uniform_": { @@ -2472,7 +2491,8 @@ } }, "torch.Tensor.unique": { - "Matcher": "TensorUniqueMatcher", + "Matcher": "GenericMatcher", + "paddle_api": "paddle.Tensor.unique", "args_list": [ "sorted", "return_inverse", @@ -2481,7 +2501,10 @@ ], "kwargs_change": { "dim": "axis" - } + }, + "unsupport_args": [ + "sorted" + ] }, "torch.Tensor.unique_consecutive": { "Matcher": "GenericMatcher", @@ -2795,11 +2818,15 @@ "start", "end", "step", + "out", "dtype", "layout", "device", "requires_grad" - ] + ], + "kwargs_change": { + "dtype": "dtype" + } }, "torch.arccos": { "Matcher": "GenericMatcher", @@ -3919,24 +3946,31 @@ } }, "torch.cumprod": { - "Matcher": "CumprodMatcher", + "Matcher": "GenericMatcher", "paddle_api": "paddle.cumprod", "args_list": [ "input", "dim", "dtype", "out" - ] + ], + "kwargs_change": { + "input": "x" + } }, "torch.cumsum": { - "Matcher": "CumsumMatcher", + "Matcher": "GenericMatcher", "paddle_api": "paddle.cumsum", "args_list": [ "input", "dim", "dtype", "out" - ] + ], + "kwargs_change": { + "input": "x", + "dim": "axis" + } }, "torch.cumulative_trapezoid": { "Matcher": "GenericMatcher", @@ -4560,15 +4594,6 @@ "out" ] }, - "torch.divide": { - "Matcher": "DivMatcher", - "args_list": [ - "input", - "other", - "rounding_mode", - "out" - ] - }, "torch.dot": { "Matcher": "GenericMatcher", "paddle_api": "paddle.dot", @@ -4600,7 +4625,7 @@ "paddle_api": "paddle.dtype" }, "torch.einsum": { - "Matcher": "IdentityMatcher", + "Matcher": "UnchangeMatcher", "paddle_api": "paddle.einsum" }, "torch.empty": { @@ -6061,7 +6086,7 @@ } }, "torch.load": { - "Matcher": "LoadMatcher", + "Matcher": "GenericMatcher", "paddle_api": "paddle.load", "args_list": [ "f", @@ -6071,7 +6096,11 @@ "pickle_load_args" ], "kwargs_change": { - "f": "path" + "f": "path", + "map_location": "", + "pickle_module": "", + "weights_only": "", + "pickle_load_args": "" } }, "torch.log": { @@ -6154,13 +6183,17 @@ ] }, "torch.logical_and": { - "Matcher": "LogicalMatcher", + "Matcher": "GenericMatcher", "paddle_api": "paddle.logical_and", "args_list": [ "input", "other", "out" - ] + ], + "kwargs_change": { + "input": "x", + "other": "y" + } }, "torch.logical_not": { "Matcher": "GenericMatcher", @@ -6174,7 +6207,7 @@ } }, "torch.logical_or": { - "Matcher": "LogicalMatcher", + "Matcher": "GenericMatcher", "paddle_api": "paddle.logical_or", "args_list": [ "input", @@ -6187,7 +6220,7 @@ } }, "torch.logical_xor": { - "Matcher": "LogicalMatcher", + "Matcher": "GenericMatcher", "paddle_api": "paddle.logical_xor", "args_list": [ "input", @@ -6231,7 +6264,7 @@ } }, "torch.logsumexp": { - "Matcher": "LogsumexpMatcher", + "Matcher": "GenericMatcher", "paddle_api": "paddle.logsumexp", "args_list": [ "input", @@ -6477,13 +6510,17 @@ ] }, "torch.mul": { - "Matcher": "MulMatcher", + "Matcher": "GenericMatcher", "paddle_api": "paddle.multiply", "args_list": [ "input", "other", "out" - ] + ], + "kwargs_change": { + "input": "x", + "other": "y" + } }, "torch.multinomial": { "Matcher": "GenericMatcher", @@ -7288,7 +7325,7 @@ ] }, "torch.nn.Identity": { - "Matcher": "IdentityMatcher", + "Matcher": "UnchangeMatcher", "paddle_api": "paddle.nn.Identity" }, "torch.nn.InstanceNorm1d": { @@ -7510,7 +7547,7 @@ ] }, "torch.nn.MaxPool1d": { - "Matcher": "MaxPoolMatcher", + "Matcher": "GenericMatcher", "paddle_api": "paddle.nn.MaxPool1D", "args_list": [ "kernel_size", @@ -7522,10 +7559,13 @@ ], "kwargs_change": { "return_indices": "return_mask" - } + }, + "unsupport_args": [ + "dilation" + ] }, "torch.nn.MaxPool2d": { - "Matcher": "MaxPoolMatcher", + "Matcher": "GenericMatcher", "paddle_api": "paddle.nn.MaxPool2D", "args_list": [ "kernel_size", @@ -7537,10 +7577,13 @@ ], "kwargs_change": { "return_indices": "return_mask" - } + }, + "unsupport_args": [ + "dilation" + ] }, "torch.nn.MaxPool3d": { - "Matcher": "MaxPoolMatcher", + "Matcher": "GenericMatcher", "paddle_api": "paddle.nn.MaxPool3D", "args_list": [ "kernel_size", @@ -7552,7 +7595,10 @@ ], "kwargs_change": { "return_indices": "return_mask" - } + }, + "unsupport_args": [ + "dilation" + ] }, "torch.nn.MaxUnpool1d": { "Matcher": "GenericMatcher", @@ -8938,8 +8984,8 @@ "weight", "bias", "use_input_stats", - "eps", - "momentum" + "momentum", + "eps" ] }, "torch.nn.functional.interpolate": { @@ -9615,20 +9661,32 @@ } }, "torch.nn.functional.upsample_bilinear": { - "Matcher": "FUpsampleBilinearMatcher", + "Matcher": "GenericMatcher", + "paddle_api": "paddle.nn.functional.upsample", "args_list": [ "input", "size", "scale_factor" - ] + ], + "kwargs_change": { + "input": "x" + }, + "paddle_default_kwargs": { + "align_corners": "True", + "mode": "'bilinear'" + } }, "torch.nn.functional.upsample_nearest": { - "Matcher": "FUpsampleNearestMatcher", + "Matcher": "GenericMatcher", + "paddle_api": "paddle.nn.functional.upsample", "args_list": [ "input", "size", "scale_factor" - ] + ], + "kwargs_change": { + "input": "x" + } }, "torch.nn.init.calculate_gain": { "Matcher": "GenericMatcher", @@ -10378,7 +10436,10 @@ "device", "requires_grad", "pin_memory" - ] + ], + "kwargs_change": { + "dtype": "dtype" + } }, "torch.range": { "Matcher": "RangeMatcher", @@ -10538,7 +10599,7 @@ } }, "torch.save": { - "Matcher": "SaveMatcher", + "Matcher": "GenericMatcher", "paddle_api": "paddle.save", "args_list": [ "obj", @@ -10549,7 +10610,9 @@ ], "kwargs_change": { "f": "path", - "pickle_protocol": "protocol" + "pickle_module": "", + "pickle_protocol": "protocol", + "_use_new_zipfile_serialization": "" } }, "torch.scalar_tensor": { @@ -11030,7 +11093,7 @@ } }, "torch.special.logsumexp": { - "Matcher": "LogsumexpMatcher", + "Matcher": "GenericMatcher", "paddle_api": "paddle.logsumexp", "args_list": [ "input", @@ -11194,9 +11257,13 @@ "input", "dim", "correction", - "keepdim", - "out" - ] + "keepdim" + ], + "kwargs_change": { + "input": "x", + "dim": "axis", + "correction": "unbiased" + } }, "torch.sub": { "Matcher": "SubMatcher", @@ -11236,6 +11303,7 @@ "args_list": [ "input", "some", + "compute_uv", "out" ], "kwargs_change": { @@ -11507,12 +11575,17 @@ ] }, "torch.true_divide": { - "Matcher": "TrueDivideMatcher", + "Matcher": "GenericMatcher", + "paddle_api": "paddle.divide", "args_list": [ "input", "other", "out" - ] + ], + "kwargs_change": { + "input": "x", + "other": "y" + } }, "torch.trunc": { "Matcher": "GenericMatcher", @@ -11538,12 +11611,18 @@ } }, "torch.unflatten": { - "Matcher": "UnflattenMatcher", + "Matcher": "GenericMatcher", + "paddle_api": "paddle.unflatten", "args_list": [ "input", "dim", "sizes" - ] + ], + "kwargs_change": { + "input": "x", + "dim": "axis", + "sizes": "shape" + } }, "torch.unique": { "Matcher": "GenericMatcher", @@ -11654,7 +11733,8 @@ ] }, "torch.utils.data.BatchSampler": { - "Matcher": "TorchUtilDataBatchSampler", + "Matcher": "GenericMatcher", + "paddle_api": "paddle.io.BatchSampler", "args_list": [ "sampler", "batch_size", @@ -11752,7 +11832,7 @@ "paddle_api": "paddle.io.get_worker_info" }, "torch.utils.data.random_split": { - "Matcher": "RandomSplitMatcher", + "Matcher": "GenericMatcher", "paddle_api": "paddle.io.random_split", "args_list": [ "dataset", diff --git a/paconvert/api_matcher.py b/paconvert/api_matcher.py index c5d3c5e90..df08f8573 100644 --- a/paconvert/api_matcher.py +++ b/paconvert/api_matcher.py @@ -69,35 +69,31 @@ def generate_code(self, kwargs): new_kwargs = self.set_paddle_default_kwargs(new_kwargs) - dtype_v = None + dtype_v = "None" if "dtype" in new_kwargs and "dtype" not in kwargs: dtype_v = new_kwargs.pop("dtype") - pin_memory_v = False - if ( - "pin_memory" in new_kwargs - and "pin_memory" not in kwargs - and new_kwargs.pop("pin_memory") == "(True)" - ): - pin_memory_v = True + pin_memory_v = "(False)" + if "pin_memory" in new_kwargs and "pin_memory" not in kwargs: + pin_memory_v = new_kwargs.pop("pin_memory") - stop_gradient_v = None + stop_gradient_v = "None" if "requires_grad" in new_kwargs and "requires_grad" not in kwargs: stop_gradient_v = "not " + new_kwargs.pop("requires_grad").strip("()") - out_v = None + out_v = "None" if "out" in new_kwargs and "out" not in kwargs: out_v = new_kwargs.pop("out") res = "{}({})".format(self.get_paddle_api(), self.kwargs_to_str(new_kwargs)) - if dtype_v: + if dtype_v != "None": res += ".astype({})".format(dtype_v) - if pin_memory_v: + if pin_memory_v != "(False)": res += ".pin_memory()" - if stop_gradient_v and out_v: + if stop_gradient_v != "None" and out_v != "None": API_TEMPLATE = textwrap.dedent( """ x = {} @@ -106,7 +102,7 @@ def generate_code(self, kwargs): """ ) code = API_TEMPLATE.format(res, stop_gradient_v, out_v) - elif stop_gradient_v and not out_v: + elif stop_gradient_v != "None" and out_v == "None": API_TEMPLATE = textwrap.dedent( """ {} = {} @@ -116,7 +112,7 @@ def generate_code(self, kwargs): ) out = get_unique_name("out") code = API_TEMPLATE.format(out, res, out, stop_gradient_v, out) - elif not stop_gradient_v and out_v: + elif out_v != "None" and stop_gradient_v == "None": API_TEMPLATE = textwrap.dedent( """ paddle.assign({}, output={}) @@ -128,10 +124,37 @@ def generate_code(self, kwargs): return code - def get_paddle_class_attribute_nodes(self, node): - self.parse_func(node) - code = "{}".format(self.paddle_api) - return ast.parse(code).body + +class TensorFunc2PaddleFunc(BaseMatcher): + def generate_code(self, kwargs): + kwargs_change = {} + if "kwargs_change" in self.api_mapping: + kwargs_change = self.api_mapping["kwargs_change"] + + for k in list(kwargs.keys()): + if k in kwargs_change: + if kwargs_change[k]: + kwargs[kwargs_change[k]] = kwargs.pop(k) + else: + kwargs.pop(k) + else: + if k in [ + "layout", + "device", + "memory_format", + "inplace", + "generator", + "non_blocking", + "async", + ]: + kwargs.pop(k) + + kwargs = self.set_paddle_default_kwargs(kwargs) + + code = "{}({}, {})".format( + self.get_paddle_api(), self.paddleClass, self.kwargs_to_str(kwargs) + ) + return code class DeleteMatcher(BaseMatcher): @@ -149,10 +172,18 @@ def get_paddle_class_nodes(self, func, args, kwargs): class UnchangeMatcher(BaseMatcher): - def get_paddle_class_nodes(self, func, args, kwargs): + def get_paddle_class_attribute_nodes(self, node): return "unchange" - def get_paddle_class_attribute_nodes(self, node): + def get_paddle_nodes(self, args, kwargs): + new_args = self.parse_args(args) + new_kwargs = self.parse_kwargs(kwargs) + code = "{}({})".format( + self.get_paddle_api(), self.args_and_kwargs_to_str(new_args, new_kwargs) + ) + return ast.parse(code).body + + def get_paddle_class_nodes(self, func, args, kwargs): return "unchange" @@ -172,16 +203,6 @@ def generate_code(self, kwargs): return "False" -class IdentityMatcher(BaseMatcher): - def get_paddle_nodes(self, args, kwargs): - new_args = self.parse_args(args) - new_kwargs = self.parse_kwargs(kwargs) - code = "{}({})".format( - self.get_paddle_api(), self.args_and_kwargs_to_str(new_args, new_kwargs) - ) - return ast.parse(code).body - - class InitMatcher(BaseMatcher): def generate_code(self, kwargs): kwargs_change = {} @@ -209,27 +230,15 @@ def generate_code(self, kwargs): return code -class LayerMatcher(BaseMatcher): - def generate_code(self, kwargs): - if "device" in kwargs: - del kwargs["device"] - if "dtype" in kwargs: - del kwargs["dtype"] - if "bias" in kwargs: - kwargs["bias_attr"] = kwargs.pop("bias") - code = "{}({})".format(self.get_paddle_api(), self.kwargs_to_str(kwargs)) - return code - - class TorchAddMatcher(BaseMatcher): def generate_code(self, kwargs): if "alpha" in kwargs: - code = "paddle.add(x={}, y=paddle.to_tensor({})*{})".format( - kwargs["input"], kwargs["alpha"], kwargs["other"] + code = "{}(x={}, y=paddle.to_tensor({})*{})".format( + self.get_paddle_api(), kwargs["input"], kwargs["alpha"], kwargs["other"] ) else: - code = "paddle.add(x={}, y=paddle.to_tensor({}))".format( - kwargs["input"], kwargs["other"] + code = "{}(x={}, y=paddle.to_tensor({}))".format( + self.get_paddle_api(), kwargs["input"], kwargs["other"] ) if "out" in kwargs: @@ -1055,30 +1064,6 @@ def generate_code(self, kwargs): return code -class MaxPoolMatcher(BaseMatcher): - def generate_code(self, kwargs): - if "dilation" in kwargs: - if kwargs["dilation"] != "(1)": - return None - else: - kwargs.pop("dilation") - - if "kwargs_change" in self.api_mapping: - kwargs_change = self.api_mapping["kwargs_change"] - for key in list(kwargs_change.keys()): - if key in kwargs: - kwargs[kwargs_change[key]] = kwargs[key] - kwargs.pop(key) - - API_TEMPLATE = textwrap.dedent( - """ - {}({}) - """ - ) - code = API_TEMPLATE.format(self.get_paddle_api(), self.kwargs_to_str(kwargs)) - return code - - class SplitMatcher(BaseMatcher): def generate_code(self, kwargs): if "dim" in kwargs: @@ -1205,33 +1190,6 @@ def get_paddle_class_nodes(self, func, args, kwargs): return ast.parse(code).body -class TensorUniqueMatcher(BaseMatcher): - def get_paddle_class_nodes(self, func, args, kwargs): - self.parse_func(func) - kwargs = self.parse_args_and_kwargs(args, kwargs) - - if "sorted" in kwargs: - if "False" in kwargs["sorted"]: - return None - else: - kwargs.pop("sorted") - - if "kwargs_change" in self.api_mapping: - kwargs_change = self.api_mapping["kwargs_change"] - for key in list(kwargs_change.keys()): - if key in kwargs: - kwargs[kwargs_change[key]] = kwargs[key] - kwargs.pop(key) - - API_TEMPLATE = textwrap.dedent( - """ - {}.unique({}) - """ - ) - code = API_TEMPLATE.format(self.paddleClass, self.kwargs_to_str(kwargs)) - return ast.parse(code).body - - class TensorExpandMatcher(BaseMatcher): def get_paddle_class_nodes(self, func, args, kwargs): self.parse_func(func) @@ -1270,7 +1228,7 @@ def get_paddle_class_nodes(self, func, args, kwargs): return ast.parse(code).body -class TensorRequiresGradMatcher(BaseMatcher): +class TensorRequiresGrad_Matcher(BaseMatcher): def get_paddle_class_nodes(self, func, args, kwargs): self.parse_func(func) kwargs = self.parse_args_and_kwargs(args, kwargs) @@ -1328,28 +1286,6 @@ def get_paddle_class_attribute_nodes(self, node): return ast.parse(code).body -class SaveMatcher(BaseMatcher): - def generate_code(self, kwargs): - if "pickle_module" in kwargs: - kwargs.pop("pickle_module") - - if "_use_new_zipfile_serialization" in kwargs: - kwargs.pop("_use_new_zipfile_serialization") - - if "pickle_protocol" in kwargs: - protocol = kwargs["pickle_protocol"] - else: - protocol = 4 - - API_TEMPLATE = textwrap.dedent( - """ - paddle.save(obj={}, path={}, protocol={}) - """ - ) - code = API_TEMPLATE.format(kwargs["obj"], kwargs["f"], protocol) - return code - - class SeedMatcher(BaseMatcher): def generate_code(self, kwargs): API_TEMPLATE = textwrap.dedent( @@ -1372,7 +1308,7 @@ def generate_code(self, kwargs): class SetPrintOptionsMatcher(BaseMatcher): def generate_code(self, kwargs): - if "profile" in kwargs and kwargs["profile"] is not None: + if "profile" in kwargs: pro_kwargs = {} if kwargs["profile"] == '"""default"""': pro_kwargs["precision"] = 4 @@ -1472,7 +1408,7 @@ def generate_code(self, kwargs): class PolarMatcher(BaseMatcher): def generate_code(self, kwargs): - if "out" in kwargs and kwargs["out"] is not None: + if "out" in kwargs and kwargs["out"] != "None": API_TEMPLATE = textwrap.dedent( """ paddle.assign(paddle.complex({} * paddle.cos({}), {} * paddle.sin({})), output={}) @@ -1566,7 +1502,7 @@ def generate_code(self, kwargs): if "value" not in kwargs: kwargs["value"] = 1 - if "out" in kwargs and kwargs["out"] is not None: + if "out" in kwargs and kwargs["out"] != "None": API_TEMPLATE = textwrap.dedent( """ paddle.assign({} + {} * {} * {}, output={}) @@ -1600,7 +1536,7 @@ def generate_code(self, kwargs): if "value" not in kwargs: kwargs["value"] = 1 - if "out" in kwargs and kwargs["out"] is not None: + if "out" in kwargs and kwargs["out"] != "None": API_TEMPLATE = textwrap.dedent( """ paddle.assign({} + {} * {} / {}, output={}) @@ -1640,7 +1576,7 @@ def generate_code(self, kwargs): class VStackMatcher(BaseMatcher): def generate_code(self, kwargs): - if "out" in kwargs and kwargs["out"] is not None: + if "out" in kwargs and kwargs["out"] != "None": API_TEMPLATE = textwrap.dedent( """ if {}[0].ndim == 1: @@ -1680,7 +1616,7 @@ def generate_code(self, kwargs): class HStackMatcher(BaseMatcher): def generate_code(self, kwargs): - if "out" in kwargs and kwargs["out"] is not None: + if "out" in kwargs and kwargs["out"] != "None": API_TEMPLATE = textwrap.dedent( """ {} = 0 if {}[0].ndim == 1 else 1 @@ -1706,7 +1642,7 @@ def generate_code(self, kwargs): class ColumnStackMatcher(BaseMatcher): def generate_code(self, kwargs): - if "out" in kwargs and kwargs["out"] is not None: + if "out" in kwargs and kwargs["out"] != "None": API_TEMPLATE = textwrap.dedent( """ if {}[0].ndim == 1: @@ -1882,25 +1818,6 @@ def generate_code(self, kwargs): return code -class TorchUtilDataBatchSampler(BaseMatcher): - def generate_code(self, kwargs): - API_TEMPLATE = textwrap.dedent( - """ - paddle.io.BatchSampler(sampler = {} if isinstance({}, paddle.io.Sampler) else paddle.io.SequenceSampler({}), batch_size = {}, drop_last = {}) - """ - ) - - code = API_TEMPLATE.format( - kwargs["sampler"], - kwargs["sampler"], - kwargs["sampler"], - kwargs["batch_size"], - kwargs["drop_last"], - ) - - return code - - class RandomSamplerMatcher(BaseMatcher): def generate_code(self, kwargs): if ( @@ -1980,7 +1897,7 @@ def get_paddle_class_nodes(self, func, args, kwargs): return "unchange" -class TensorRequires_GradMatcher(BaseMatcher): +class TensorRequiresGradMatcher(BaseMatcher): def get_paddle_class_attribute_nodes(self, node): self.parse_func(node) code = "not {}.stop_gradient".format(self.paddleClass) @@ -1991,8 +1908,6 @@ class ArangeMatcher(BaseMatcher): def generate_code(self, kwargs): if "end" not in kwargs: kwargs["end"] = kwargs.pop("start") - if "dtype" not in kwargs and "." in kwargs["end"]: - kwargs["dtype"] = "'float32'" code = GenericMatcher.generate_code(self, kwargs) return code @@ -2002,7 +1917,7 @@ def generate_code(self, kwargs): if "input" not in kwargs: kwargs["input"] = self.paddleClass - if "out" in kwargs and kwargs["out"] is not None: + if "out" in kwargs and kwargs["out"] != "None": API_TEMPLATE = textwrap.dedent( """ paddle.assign(1. - paddle.erf({}), output={}) @@ -2022,7 +1937,7 @@ def generate_code(self, kwargs): class SpecialErfcxMatcher(BaseMatcher): def generate_code(self, kwargs): - if "out" in kwargs and kwargs["out"] is not None: + if "out" in kwargs and kwargs["out"] != "None": API_TEMPLATE = textwrap.dedent( """ paddle.assign(paddle.exp({} ** 2) * (1.0 - paddle.erf({})), output={}) @@ -2042,7 +1957,7 @@ def generate_code(self, kwargs): class SpecialXlogyMatcher(BaseMatcher): def generate_code(self, kwargs): - if "out" in kwargs and kwargs["out"] is not None: + if "out" in kwargs and kwargs["out"] != "None": API_TEMPLATE = textwrap.dedent( """ paddle.assign({} * paddle.log({}), output={}) @@ -2062,7 +1977,7 @@ def generate_code(self, kwargs): class Exp2Matcher(BaseMatcher): def generate_code(self, kwargs): - if "out" in kwargs and kwargs["out"] is not None: + if "out" in kwargs and kwargs["out"] != "None": API_TEMPLATE = textwrap.dedent( """ paddle.assign(2. ** {}, output={}) @@ -2083,7 +1998,7 @@ def generate_code(self, kwargs): class ExpitMatcher(BaseMatcher): def generate_code(self, kwargs): exp_v = get_unique_name("exp_v") - if "out" in kwargs and kwargs["out"] is not None: + if "out" in kwargs and kwargs["out"] != "None": API_TEMPLATE = textwrap.dedent( """ {} = paddle.exp({}) @@ -2107,7 +2022,7 @@ def generate_code(self, kwargs): class FModMatcher(BaseMatcher): def generate_code(self, kwargs): - if "out" in kwargs and kwargs["out"] is not None: + if "out" in kwargs and kwargs["out"] != "None": API_TEMPLATE = textwrap.dedent( """ paddle.assign(paddle.mod({}, paddle.to_tensor({}, dtype={}.dtype)), output={}) @@ -2134,7 +2049,7 @@ def generate_code(self, kwargs): if "input" not in kwargs: kwargs["input"] = self.paddleClass - if "out" in kwargs and kwargs["out"] is not None: + if "out" in kwargs and kwargs["out"] != "None": API_TEMPLATE = textwrap.dedent( """ paddle.assign({} * (2. ** {}), output={}) @@ -2154,15 +2069,10 @@ def generate_code(self, kwargs): class LogAddExpMatcher(BaseMatcher): def generate_code(self, kwargs): - if "input" in kwargs: - kwargs["input"] = kwargs.pop("input").strip("\n") + ".astype('float32')" - else: + if "input" not in kwargs: kwargs["input"] = self.paddleClass - if "other" in kwargs: - kwargs["other"] = kwargs.pop("other").strip("\n") + ".astype('float32')" - - if "out" in kwargs and kwargs["out"] is not None: + if "out" in kwargs and kwargs["out"] != "None": API_TEMPLATE = textwrap.dedent( """ paddle.assign(paddle.log(paddle.exp({}) + paddle.exp({})), output={}) @@ -2185,7 +2095,7 @@ def generate_code(self, kwargs): if "input" not in kwargs: kwargs["input"] = self.paddleClass - if "out" in kwargs and kwargs["out"] is not None: + if "out" in kwargs and kwargs["out"] != "None": API_TEMPLATE = textwrap.dedent( """ paddle.assign(paddle.log2(2. ** {} + 2. ** {}), output={}) @@ -2208,7 +2118,7 @@ def generate_code(self, kwargs): if "input" not in kwargs: kwargs["input"] = self.paddleClass - if "out" in kwargs and kwargs["out"] is not None: + if "out" in kwargs and kwargs["out"] != "None": API_TEMPLATE = textwrap.dedent( """ paddle.assign(paddle.multiply(paddle.to_tensor({}), paddle.log(paddle.to_tensor({}))), output={}) @@ -2228,11 +2138,13 @@ def generate_code(self, kwargs): class StdMeanMatcher(BaseMatcher): def generate_code(self, kwargs): - if "correction" not in kwargs and "unbiased" not in kwargs: - kwargs["correction"] = 1 - - if "unbiased" in kwargs: - kwargs["correction"] = kwargs["unbiased"] + if "correction" in kwargs: + kwargs["unbiased"] = kwargs.pop("correction") + elif "unbiased" in kwargs: + # do nothing + pass + else: + kwargs["unbiased"] = True if "keepdim" not in kwargs: kwargs["keepdim"] = False @@ -2240,17 +2152,19 @@ def generate_code(self, kwargs): if "dim" not in kwargs: kwargs["dim"] = None - if "out" in kwargs and kwargs["out"] is not None: + if "out" in kwargs and kwargs["out"] != "None": API_TEMPLATE = textwrap.dedent( """ - paddle.assign((paddle.std({}, axis={}, unbiased={}, keepdim={}), paddle.mean({}, axis={}, keepdim={})), output={}) + paddle.assign(paddle.std({}, axis={}, unbiased={}, keepdim={}), output={}[0]) + paddle.assign(paddle.mean({}, axis={}, keepdim={}), output={}[1]) """ ) code = API_TEMPLATE.format( kwargs["input"], kwargs["dim"], - kwargs["correction"], + kwargs["unbiased"], kwargs["keepdim"], + kwargs["out"], kwargs["input"], kwargs["dim"], kwargs["keepdim"], @@ -2265,7 +2179,7 @@ def generate_code(self, kwargs): code = API_TEMPLATE.format( kwargs["input"], kwargs["dim"], - kwargs["correction"], + kwargs["unbiased"], kwargs["keepdim"], kwargs["input"], kwargs["dim"], @@ -2277,11 +2191,13 @@ def generate_code(self, kwargs): class VarMeanMatcher(BaseMatcher): def generate_code(self, kwargs): - if "correction" not in kwargs and "unbiased" not in kwargs: - kwargs["correction"] = 1 - - if "unbiased" in kwargs: - kwargs["correction"] = kwargs["unbiased"] + if "correction" in kwargs: + kwargs["unbiased"] = kwargs.pop("correction") + elif "unbiased" in kwargs: + # do nothing + pass + else: + kwargs["unbiased"] = True if "keepdim" not in kwargs: kwargs["keepdim"] = False @@ -2289,17 +2205,19 @@ def generate_code(self, kwargs): if "dim" not in kwargs: kwargs["dim"] = None - if "out" in kwargs and kwargs["out"] is not None: + if "out" in kwargs and kwargs["out"] != "None": API_TEMPLATE = textwrap.dedent( """ - paddle.assign((paddle.var({}, axis={}, unbiased={}, keepdim={}), paddle.mean({}, axis={}, keepdim={})), output={}) + paddle.assign(paddle.var({}, axis={}, unbiased={}, keepdim={}), output={}[0]) + paddle.assign(paddle.mean({}, axis={}, keepdim={}), output={}[1]) """ ) code = API_TEMPLATE.format( kwargs["input"], kwargs["dim"], - kwargs["correction"], + kwargs["unbiased"], kwargs["keepdim"], + kwargs["out"], kwargs["input"], kwargs["dim"], kwargs["keepdim"], @@ -2314,7 +2232,7 @@ def generate_code(self, kwargs): code = API_TEMPLATE.format( kwargs["input"], kwargs["dim"], - kwargs["correction"], + kwargs["unbiased"], kwargs["keepdim"], kwargs["input"], kwargs["dim"], @@ -2343,7 +2261,7 @@ def generate_code(self, kwargs): if "alpha" not in kwargs: kwargs["alpha"] = 1 - if "out" in kwargs and kwargs["out"] is not None: + if "out" in kwargs and kwargs["out"] != "None": API_TEMPLATE = textwrap.dedent( """ paddle.assign(paddle.add({}*{}, {}*{}({}, {})), output={}) @@ -2387,7 +2305,7 @@ def generate_code(self, kwargs): if "alpha" not in kwargs: kwargs["alpha"] = 1 - if "out" in kwargs and kwargs["out"] is not None: + if "out" in kwargs and kwargs["out"] != "None": API_TEMPLATE = textwrap.dedent( """ paddle.assign(paddle.add({}*{}, {}*paddle.sum(paddle.bmm({}, {}), axis=0)), output={}) @@ -2426,7 +2344,7 @@ def generate_code(self, kwargs): if "upper" not in kwargs: kwargs["upper"] = False - if "out" in kwargs and kwargs["out"] is not None: + if "out" in kwargs and kwargs["out"] != "None": API_TEMPLATE = textwrap.dedent( """ {} = list(range({}.ndim)) @@ -2531,7 +2449,7 @@ def generate_code(self, kwargs): class FSoftMinMatcher(BaseMatcher): def generate_code(self, kwargs): - if "dim" not in kwargs or kwargs["dim"] is None: + if "dim" not in kwargs or kwargs["dim"] == "None": return None if "dtype" in kwargs and kwargs["dim"] is not None: @@ -2554,19 +2472,16 @@ def generate_code(self, kwargs): class FBatchNormMatcher(BaseMatcher): def generate_code(self, kwargs): - if "input" in kwargs: - kwargs["x"] = kwargs.pop("input") - + kwargs["x"] = kwargs.pop("input") if "eps" in kwargs: kwargs["epsilon"] = kwargs.pop("eps") - if "momentum" in kwargs: kwargs["momentum"] = "1 - " + kwargs["momentum"] API_TEMPLATE = textwrap.dedent( """ - {}({}) - """ + {}({}) + """ ) code = API_TEMPLATE.format(self.get_paddle_api(), self.kwargs_to_str(kwargs)) @@ -2575,72 +2490,16 @@ def generate_code(self, kwargs): class FInstanceNormMatcher(BaseMatcher): def generate_code(self, kwargs): - if "input" in kwargs: - kwargs["x"] = kwargs.pop("input") - + kwargs["x"] = kwargs.pop("input") if "momentum" in kwargs: kwargs["momentum"] = "1 - " + kwargs["momentum"] API_TEMPLATE = textwrap.dedent( """ - {}({}) - """ - ) - code = API_TEMPLATE.format(self.get_paddle_api(), self.kwargs_to_str(kwargs)) - - return code - - -class FUpsampleNearestMatcher(BaseMatcher): - def generate_code(self, kwargs): - if "input" in kwargs: - kwargs["x"] = kwargs.pop("input") - - API_TEMPLATE = textwrap.dedent( - """ - paddle.nn.functional.upsample({}, mode='nearest') - """ - ) - code = API_TEMPLATE.format(self.kwargs_to_str(kwargs)) - - return code - - -class FUpsampleBilinearMatcher(BaseMatcher): - def generate_code(self, kwargs): - if "input" in kwargs: - kwargs["x"] = kwargs.pop("input") - - API_TEMPLATE = textwrap.dedent( - """ - paddle.nn.functional.upsample({}, mode='bilinear', align_corners=True) - """ - ) - code = API_TEMPLATE.format(self.kwargs_to_str(kwargs)) - - return code - - -class TensorBernoulli_Matcher(BaseMatcher): - def generate_code(self, kwargs): - if "p" not in kwargs: - kwargs["p"] = 0.5 - API_TEMPLATE = textwrap.dedent( - """ - {} = paddle.to_tensor([{}], dtype=paddle.float32) if not isinstance({}, paddle.Tensor) else {} - paddle.assign(paddle.bernoulli(paddle.broadcast_to({}, {}.shape)), {}) + {}({}) """ ) - bern = get_unique_name("bern") - code = API_TEMPLATE.format( - bern, - kwargs["p"], - kwargs["p"], - kwargs["p"], - bern, - self.paddleClass, - self.paddleClass, - ) + code = API_TEMPLATE.format(self.get_paddle_api(), self.kwargs_to_str(kwargs)) return code @@ -2650,7 +2509,7 @@ def generate_code(self, kwargs): if "input" not in kwargs: kwargs["input"] = self.paddleClass - if "out" in kwargs and kwargs["out"] is not None: + if "out" in kwargs and kwargs["out"] != "None": API_TEMPLATE = textwrap.dedent( """ paddle.assign(paddle.sort({}, axis=0), {}) @@ -2667,60 +2526,6 @@ def generate_code(self, kwargs): return code -class TensorSVDMatcher(BaseMatcher): - def generate_code(self, kwargs): - - if "some" in kwargs: - kwargs["full_matrices"] = not kwargs.pop("some") - else: - kwargs["full_matrices"] = False - - API_TEMPLATE = textwrap.dedent( - """ - paddle.linalg.svd({}, full_matrices={}) - """ - ) - code = API_TEMPLATE.format(self.paddleClass, kwargs["full_matrices"]) - - return code - - -class TensorVDotMatcher(BaseMatcher): - def generate_code(self, kwargs): - - API_TEMPLATE = textwrap.dedent( - """ - paddle.sum({} * {}) - """ - ) - code = API_TEMPLATE.format(self.paddleClass, kwargs["other"]) - - return code - - -class UnflattenMatcher(BaseMatcher): - def generate_code(self, kwargs): - if "input" not in kwargs: - kwargs["input"] = self.paddleClass - - API_TEMPLATE = textwrap.dedent( - """ - paddle.reshape({}, shape=({}.shape[:{}] + list({}) + ({}.shape[{}+1:] if {} !=-1 else []))) - """ - ) - code = API_TEMPLATE.format( - kwargs["input"], - kwargs["input"], - kwargs["dim"], - kwargs["sizes"], - kwargs["input"], - kwargs["dim"], - kwargs["dim"], - ) - - return code - - class NumelMatcher(BaseMatcher): def generate_code(self, kwargs): if "input" not in kwargs: @@ -2778,34 +2583,11 @@ def generate_code(self, kwargs): kwargs["source"], ) - if "out" in kwargs and kwargs["out"] is not None: + if "out" in kwargs and kwargs["out"] != "None": code = "paddle.assign({}, output={})".format(code, kwargs["out"]) return code -class LogicalMatcher(BaseMatcher): - def generate_code(self, kwargs): - if "input" in kwargs: - kwargs["x"] = kwargs.pop("input") - - if "other" in kwargs: - kwargs["y"] = kwargs.pop("other") - - if "out" in kwargs and kwargs["out"] is not None: - out_v = kwargs.pop("out").strip("\n") - code = ( - "paddle.assign({}(x={}, y=({}).astype(({}).dtype)), output={})".format( - self.get_paddle_api(), kwargs["x"], kwargs["y"], kwargs["x"], out_v - ) - ) - else: - code = "{}(x={}, y=({}).astype(({}).dtype))".format( - self.get_paddle_api(), kwargs["x"], kwargs["y"], kwargs["x"] - ) - - return code - - class AMinMaxMatcher(BaseMatcher): def generate_code(self, kwargs): if "input" not in kwargs: @@ -2817,7 +2599,7 @@ def generate_code(self, kwargs): if "keepdim" not in kwargs: kwargs["keepdim"] = False - if "out" in kwargs and kwargs["out"] is not None: + if "out" in kwargs and kwargs["out"] != "None": API_TEMPLATE = textwrap.dedent( """ tuple([paddle.assign(paddle.amin({}, axis={}, keepdim={}), {}[0]), paddle.assign(paddle.amax({}, axis={}, keepdim={}), {}[1])]) @@ -2852,49 +2634,6 @@ def generate_code(self, kwargs): return code -class MulMatcher(BaseMatcher): - def generate_code(self, kwargs): - - if "out" in kwargs and kwargs["out"] is not None: - out_v = kwargs.pop("out").strip("\n") - code = "paddle.assign({} * {}, output={})".format( - kwargs["input"], kwargs["other"], out_v - ) - else: - code = "{} * {}".format(kwargs["input"], kwargs["other"]) - - return code - - -class TrueDivideMatcher(BaseMatcher): - def generate_code(self, kwargs): - - if "out" in kwargs and kwargs["out"] is not None: - out_v = kwargs.pop("out").strip("\n") - code = "paddle.assign({} / {}, output={})".format( - kwargs["input"], kwargs["other"], out_v - ) - else: - code = "{} / {}".format(kwargs["input"], kwargs["other"]) - return code - - -class TensorDiagMatcher(BaseMatcher): - def generate_code(self, kwargs): - - if "diagonal" not in kwargs: - kwargs["diagonal"] = 0 - - API_TEMPLATE = textwrap.dedent( - """ - paddle.diag({}, offset={}) - """ - ) - code = API_TEMPLATE.format(self.paddleClass, kwargs["diagonal"]) - - return code - - class DivMatcher(BaseMatcher): def generate_code(self, kwargs): if "input" not in kwargs: @@ -2902,30 +2641,23 @@ def generate_code(self, kwargs): API_TEMPLATE = textwrap.dedent( """ - paddle.divide({}, paddle.to_tensor({})) + paddle.divide(x={}, y=paddle.to_tensor({})) """ ) code = API_TEMPLATE.format(kwargs["input"], kwargs["other"]) - if "rounding_mode" in kwargs and kwargs["rounding_mode"] is not None: + if "rounding_mode" in kwargs and kwargs["rounding_mode"] != "None": if "trunc" in kwargs["rounding_mode"]: code = "paddle.trunc({})".format(code) elif "floor" in kwargs["rounding_mode"]: code = "paddle.floor({})".format(code) - if "out" in kwargs and kwargs["out"] is not None: + if "out" in kwargs and kwargs["out"] != "None": code = "paddle.assign({}, output={})".format(code, kwargs["out"]) return code -class LogsumexpMatcher(BaseMatcher): - def generate_code(self, kwargs): - kwargs["input"] = kwargs["input"] + ".astype(dtype='float32')" - code = GenericMatcher.generate_code(self, kwargs) - return code - - class AllcloseMatcher(BaseMatcher): def generate_code(self, kwargs): code = GenericMatcher.generate_code(self, kwargs) @@ -2951,7 +2683,7 @@ def generate_code(self, kwargs): kwargs["x"] = kwargs.pop("input").strip("\n") if "other" in kwargs: kwargs["y"] = "paddle.to_tensor({})".format(kwargs.pop("other").strip("\n")) - if "out" in kwargs and kwargs["out"] is not None: + if "out" in kwargs and kwargs["out"] != "None": out_v = kwargs.pop("out").strip("\n") code = "paddle.assign({}({}), output={})".format( self.get_paddle_api(), self.kwargs_to_str(kwargs), out_v @@ -2977,7 +2709,7 @@ def generate_code(self, kwargs): ) code = API_TEMPLATE.format(kwargs["input"], kwargs["alpha"], kwargs["other"]) - if "out" in kwargs and kwargs["out"] is not None: + if "out" in kwargs and kwargs["out"] != "None": code = "paddle.assign({}, output={})".format(code, kwargs["out"]) return code @@ -2993,7 +2725,7 @@ def get_paddle_nodes(self, args, kwargs): code = "{}".format(new_args[0]) for arg in new_args[1:]: code = code + " @ {}".format(arg) - if "out" in new_kwargs and new_kwargs["out"] is not None: + if "out" in new_kwargs and new_kwargs["out"] != "None": code = "paddle.assign({}, output={})".format(code, new_kwargs["out"]) return ast.parse(code).body @@ -3011,7 +2743,7 @@ def generate_code(self, kwargs): ) code = API_TEMPLATE.format(kwargs["input"], kwargs["other"]) - if "out" in kwargs and kwargs["out"] is not None: + if "out" in kwargs and kwargs["out"] != "None": code = "paddle.assign({}, output={})".format(code, kwargs["out"]) return code @@ -3093,21 +2825,17 @@ def generate_code(self, kwargs): class SearchsortedMatcher(BaseMatcher): def generate_code(self, kwargs): - if "side" in kwargs: - kwargs["right"] = kwargs.pop("side").strip("\n") + "== 'right'" + kwargs["right"] = f"{kwargs.pop('side')} == 'right'" - if "sorter" in kwargs and kwargs["sorter"] is not None: + if "sorter" in kwargs and kwargs["sorter"] != "None": kwargs[ "sorted_sequence" - ] += ".take_along_axis(axis=-1, indices = {})".format( - kwargs.pop("sorter").strip("\n") - ) + ] = f"{kwargs['sorted_sequence']}.take_along_axis(axis=-1, indices={kwargs.pop('sorter')})" code = "paddle.searchsorted({})".format(self.kwargs_to_str(kwargs)) - - if "out" in kwargs and kwargs["out"] is not None: - out_v = kwargs.pop("out").strip("\n") + if "out" in kwargs and kwargs["out"] != "None": + out_v = kwargs.pop("out") code = "paddle.assign(paddle.searchsorted({}), output={})".format( self.kwargs_to_str(kwargs), out_v ) @@ -3120,7 +2848,7 @@ def generate_code(self, kwargs): if "input" not in kwargs: kwargs["input"] = self.paddleClass - if "out" in kwargs and kwargs["out"] is not None: + if "out" in kwargs and kwargs["out"] != "None": API_TEMPLATE = textwrap.dedent( """ import numpy @@ -3148,55 +2876,17 @@ def generate_code(self, kwargs): return code -class CumprodMatcher(BaseMatcher): - def generate_code(self, kwargs): - - kwargs["x"] = kwargs.pop("input").strip("\n") - - if "out" in kwargs and kwargs["out"] is not None: - out_v = kwargs.pop("out").strip("\n") - code = "paddle.assign({}({}), output={})".format( - self.get_paddle_api(), self.kwargs_to_str(kwargs), out_v - ) - else: - code = "{}({})".format(self.get_paddle_api(), self.kwargs_to_str(kwargs)) - - return code - - -class CumsumMatcher(BaseMatcher): - def generate_code(self, kwargs): - - kwargs["x"] = kwargs.pop("input").strip("\n") - - if "dim" in kwargs: - kwargs["axis"] = kwargs.pop("dim").strip("\n") - - if "out" in kwargs and kwargs["out"] is not None: - out_v = kwargs.pop("out").strip("\n") - code = "paddle.assign({}({}), output={})".format( - self.get_paddle_api(), self.kwargs_to_str(kwargs), out_v - ) - else: - code = "{}({})".format(self.get_paddle_api(), self.kwargs_to_str(kwargs)) - - return code - - class SLogDetMatcher(BaseMatcher): def generate_code(self, kwargs): - out_v = kwargs.pop("out") if "out" in kwargs else None - if "input" in kwargs: x_v = kwargs.pop("input") - elif "A" in kwargs: x_v = kwargs.pop("A") - else: x_v = self.paddleClass - if out_v: + if "out" in kwargs and kwargs["out"] != "None": + out_v = kwargs.pop("out") API_TEMPLATE = textwrap.dedent( """ res = paddle.linalg.slogdet({}) @@ -3218,47 +2908,27 @@ def generate_code(self, kwargs): class HistcMatcher(BaseMatcher): def generate_code(self, kwargs): - if "out" in kwargs: - out_v = kwargs.pop("out").strip("\n") + if "input" in kwargs: + input = kwargs["input"] + else: + input = self.paddleClass + + if "out" in kwargs and kwargs["out"] != "None": + out_v = kwargs.pop("out") code = "paddle.assign({}({}).astype({}.dtype), output={})".format( self.get_paddle_api(), self.kwargs_to_str(kwargs), - kwargs["input"], + input, out_v, ) else: code = "{}({}).astype({}.dtype)".format( - self.get_paddle_api(), self.kwargs_to_str(kwargs), kwargs["input"] + self.get_paddle_api(), self.kwargs_to_str(kwargs), input ) return code -class TensorHistcMatcher(BaseMatcher): - def generate_code(self, kwargs): - API_TEMPLATE = textwrap.dedent( - """ - {}({}).astype({}.dtype) - """ - ) - code = API_TEMPLATE.format( - self.get_paddle_api(), - self.kwargs_to_str(kwargs), - self.paddleClass, - ) - - return code - - -class TensorHistogramMatcher(BaseMatcher): - def generate_code(self, kwargs): - if "range" in kwargs: - kwargs["min"] = "int({}[0])".format(kwargs["range"]) - kwargs["max"] = "int({}[1])".format(kwargs["range"]) - del kwargs["range"] - return GenericMatcher.generate_code(self, kwargs) - - class SpecialNdtriMatcher(BaseMatcher): def generate_code(self, kwargs): API_TEMPLATE = textwrap.dedent( @@ -3267,7 +2937,7 @@ def generate_code(self, kwargs): """ ) code = API_TEMPLATE.format(kwargs["input"]) - if "out" in kwargs and kwargs["out"] is not None: + if "out" in kwargs and kwargs["out"] != "None": code = "paddle.assign({}, output={})".format(code, kwargs["out"]) return code @@ -3343,31 +3013,16 @@ def generate_code(self, kwargs): API_TEMPLATE = textwrap.dedent( """ - {} * paddle.log1p({} if isinstance({}, paddle.Tensor) else paddle.to_tensor([{}])) + {} * paddle.log1p(paddle.to_tensor({})) """ ) - code = API_TEMPLATE.format( - kwargs["input"], kwargs["other"], kwargs["other"], kwargs["other"] - ) - if "out" in kwargs and kwargs["out"] is not None: + code = API_TEMPLATE.format(kwargs["input"], kwargs["other"]) + if "out" in kwargs and kwargs["out"] != "None": code = "paddle.assign({}, output={})".format(code, kwargs["out"]) return code -class TensorHardShrinkMatcher(BaseMatcher): - def generate_code(self, kwargs): - if "lambd" not in kwargs: - kwargs["lambd"] = 0.5 - API_TEMPLATE = textwrap.dedent( - """ - paddle.nn.functional.hardshrink({}, threshold={}) - """ - ) - code = API_TEMPLATE.format(self.paddleClass, kwargs["lambd"]) - return code - - class FunctionalKLDivMatcher(BaseMatcher): def generate_code(self, kwargs): process_reduce_and_size_average(kwargs) @@ -3389,20 +3044,16 @@ def generate_code(self, kwargs): class FunctionalSmoothL1LossMatcher(BaseMatcher): def generate_code(self, kwargs): process_reduce_and_size_average(kwargs) - if "target" in kwargs: kwargs["label"] = kwargs.pop("target") - API_TEMPLATE = "paddle.nn.functional.smooth_l1_loss({})" - if "beta" in kwargs: kwargs["delta"] = kwargs.pop("beta") - API_TEMPLATE = "paddle.nn.functional.smooth_l1_loss({})/" + str( - kwargs["delta"] - ) + API_TEMPLATE = "paddle.nn.functional.smooth_l1_loss({})/" + kwargs["delta"] + else: + API_TEMPLATE = "paddle.nn.functional.smooth_l1_loss({})" code = API_TEMPLATE.format(self.kwargs_to_str(kwargs)) - return code @@ -3485,7 +3136,7 @@ def generate_code(self, kwargs): """ ) code = API_TEMPLATE.format(kwargs["input"]) - if "out" in kwargs and kwargs["out"] is not None: + if "out" in kwargs and kwargs["out"] != "None": code = "paddle.assign({}, output={})".format(code, kwargs["out"]) return code @@ -3566,7 +3217,7 @@ class Tuple2ListMatcher(BaseMatcher): def generate_code(self, kwargs): new_kwargs = {} kwargs_change = self.api_mapping["kwargs_change"] - for k in list(kwargs.keys()): + for k in kwargs.keys(): if k in kwargs_change: if "," in kwargs[k]: new_kwargs[kwargs_change[k]] = "list({})".format(kwargs[k]) @@ -3924,13 +3575,12 @@ def generate_code(self, kwargs): return code -NoTransOptimList = ["paddle.optimizer.lr.CyclicLR"] - - class LrSchedulerMatcher(BaseMatcher): def generate_code(self, kwargs): optim = kwargs.pop("optimizer") - if self.get_paddle_api() not in NoTransOptimList: + if self.get_paddle_api() not in [ + "paddle.optimizer.lr.CyclicLR", + ]: kwargs["learning_rate"] = optim + ".get_lr()" API_TEMPLATE = textwrap.dedent( """ @@ -4029,13 +3679,12 @@ def generate_code(self, kwargs): out_v = kwargs.pop("out") if "out" in kwargs else None if out_v: - - out_3_var = "get_infos" in kwargs and kwargs["get_infos"] != "(False)" + get_infos_v = "get_infos" in kwargs and kwargs["get_infos"] != "(False)" new_kwargs = {} new_kwargs["x"] = kwargs.pop("A") new_kwargs.update(kwargs) - if out_3_var: + if get_infos_v: API_TEMPLATE = textwrap.dedent( """ tmp_lu, tmp_p, tmp_info = {}({}) @@ -4133,41 +3782,21 @@ def generate_code(self, kwargs): return GenericMatcher.generate_code(self, kwargs) -class RandomSplitMatcher(BaseMatcher): - def generate_code(self, kwargs): - API_TEMPLATE = textwrap.dedent( - """ - dataset_lengths = {} - if sum(dataset_lengths) <= 1: - dataset_lengths = [int(length * {}.__len__()) for length in dataset_lengths] - {}({}) - """ - ) - lenghts_v = kwargs["lengths"].strip("\n") - kwargs["lengths"] = "dataset_lengths" - code = API_TEMPLATE.format( - lenghts_v, - kwargs["dataset"], - self.get_paddle_api(), - self.kwargs_to_str(kwargs), - ) - return code.strip("\n") - - class SvdMatcher(BaseMatcher): def generate_code(self, kwargs): - if "compute_uv" in kwargs: return None - out_v = kwargs.pop("out") if "out" in kwargs else None - some_v = kwargs.pop("some") if "some" in kwargs else None + if "input" in kwargs: + kwargs["x"] = kwargs.pop("input") + else: + kwargs["x"] = self.paddleClass - kwargs["x"] = kwargs.pop("input") - if some_v: - kwargs["full_matrices"] = "not " + some_v.strip("()") + if "some" in kwargs: + kwargs["full_matrices"] = "not " + kwargs.pop("some").strip("()") - if out_v: + if "out" in kwargs and kwargs["out"] != "None": + out_v = kwargs.pop("out") API_TEMPLATE = textwrap.dedent( """ tmp_u, tmp_s, tmp_v = {}({}) @@ -4227,38 +3856,6 @@ def generate_code(self, kwargs): return code -class TensorFunc2PaddleFunc(BaseMatcher): - def generate_code(self, kwargs): - - kwargs_changes = ( - self.api_mapping["kwargs_change"] - if "kwargs_change" in self.api_mapping - else [] - ) - - for k in kwargs_changes: - if k in kwargs: - kwargs[kwargs_changes[k]] = kwargs.pop(k) - - if "generator" in kwargs: - kwargs.pop("generator") - - code = "{}({}, {})".format( - self.get_paddle_api(), self.paddleClass, self.kwargs_to_str(kwargs) - ) - return code - - -class TensorLogicalMatcher(BaseMatcher): - def generate_code(self, kwargs): - - code = "{}(y=({}).astype(({}).dtype))".format( - self.get_paddle_api(), kwargs["other"], self.paddleClass - ) - - return code - - class TensorDatasetMatcher(BaseMatcher): def get_paddle_nodes(self, args, kwargs): new_args = self.parse_args(args) @@ -4332,9 +3929,7 @@ def get_paddle_class_nodes(self, func, args, kwargs): class Func2Attribute(BaseMatcher): def generate_code(self, kwargs): - code = "{}".format(self.get_paddle_api()) - return code diff --git a/paconvert/attribute_mapping.json b/paconvert/attribute_mapping.json index 0594333ff..95fd8326e 100644 --- a/paconvert/attribute_mapping.json +++ b/paconvert/attribute_mapping.json @@ -46,7 +46,7 @@ "paddle_api": "paddle.Tensor.real" }, "torch.Tensor.requires_grad": { - "Matcher": "TensorRequires_GradMatcher", + "Matcher": "TensorRequiresGradMatcher", "paddle_api": "paddle.Tensor.stop_gradient" }, "torch.Tensor.shape": { diff --git a/paconvert/base.py b/paconvert/base.py index 24270057c..ef83bfe3d 100644 --- a/paconvert/base.py +++ b/paconvert/base.py @@ -298,16 +298,6 @@ def __init__(self, transformer, torch_api, api_mapping, logger): def get_aux_dir(self): return os.path.dirname(AuxFileHelper().fileName) - def get_paddle_api(self): - if self.paddle_api: - return self.paddle_api - if "paddle_api" in self.api_mapping: - return self.api_mapping["paddle_api"] - return None - - def set_paddle_api(self, paddle_api): - self.paddle_api = paddle_api - def parse_args_and_kwargs(self, args, kwargs): args_list = self.api_mapping.get("args_list") or [] # more args, not match torch class method, indicate it is not torch Class @@ -454,6 +444,21 @@ def write_aux_code(self): ) self.transformer.insert_multi_node(ast.parse(code).body) + def set_paddle_api(self, paddle_api): + self.paddle_api = paddle_api + + def get_paddle_api(self): + if self.paddle_api: + return self.paddle_api + if "paddle_api" in self.api_mapping: + return self.api_mapping["paddle_api"] + return None + + def get_paddle_class_attribute_nodes(self, node): + self.parse_func(node) + code = "{}".format(self.paddle_api) + return ast.parse(code).body + @staticmethod def generate_code(self, kwargs): return None @@ -475,6 +480,3 @@ def get_paddle_nodes(self, args, kwargs): def get_paddle_class_nodes(self, func, args, kwargs): self.parse_func(func) return self.get_paddle_nodes(args, kwargs) - - def get_paddle_class_attribute_nodes(self, node): - return None diff --git a/paconvert/transformer/basic_transformer.py b/paconvert/transformer/basic_transformer.py index ac8e92afa..4b2350302 100644 --- a/paconvert/transformer/basic_transformer.py +++ b/paconvert/transformer/basic_transformer.py @@ -108,17 +108,6 @@ def visit_Attribute(self, node): node.lineno, ) return None - elif paddle_api == "unchange": - self.success_api_count += 1 - log_debug( - self.logger, - "[Success] Convert {} to Paddle, just remain the same".format( - torch_api - ), - self.file_name, - node.lineno, - ) - return node elif paddle_api == "misidentify": # This API usage indicate that is is not a Pytorch API self.torch_api_count -= 1 @@ -190,19 +179,20 @@ def visit_Attribute(self, node): ".".join(["torch.distributions.Distribution", attr_list[-1]]) ) - if torch_class_apis: - for torch_class_api in torch_class_apis: - if torch_class_api in ATTRIBUTE_MAPPING: - self.torch_api_count += 1 - log_debug( - self.logger, - "Start convert Class Attribute: {} to Paddle ".format( - torch_class_api - ), - self.file_name, - node.lineno, - ) - return self.trans_class_attribute(node, torch_class_api) + for torch_class_api in torch_class_apis: + if torch_class_api in ALIAS_MAPPING: + torch_class_api = ALIAS_MAPPING[torch_class_api] + if torch_class_api in ATTRIBUTE_MAPPING: + self.torch_api_count += 1 + log_debug( + self.logger, + "Start convert Class Attribute: {} to Paddle ".format( + torch_class_api + ), + self.file_name, + node.lineno, + ) + return self.trans_class_attribute(node, torch_class_api) # Others return node @@ -390,17 +380,6 @@ def visit_Call(self, node): node.lineno, ) return None - elif node_list == "unchange": - self.success_api_count += 1 - log_debug( - self.logger, - "[Success] Convert {} to Paddle, just remain the same".format( - torch_api - ), - self.file_name, - node.lineno, - ) - return node elif node_list == "misidentify": # This API usage indicate that is is not a Pytorch API self.torch_api_count -= 1 @@ -501,19 +480,20 @@ def visit_Call(self, node): ".".join(["torch.autograd.profiler.profile", attr_list[-1]]) ) - if torch_class_apis: - for torch_class_api in torch_class_apis: - if torch_class_api in API_MAPPING: - self.torch_api_count += 1 - log_debug( - self.logger, - "Start convert Class Method: {} to Paddle --> ".format( - torch_class_api - ), - self.file_name, - node.lineno, - ) - return self.trans_class_method(node, torch_class_api) + for torch_class_api in torch_class_apis: + if torch_class_api in ALIAS_MAPPING: + torch_class_api = ALIAS_MAPPING[torch_class_api] + if torch_class_api in API_MAPPING: + self.torch_api_count += 1 + log_debug( + self.logger, + "Start convert Class Method: {} to Paddle --> ".format( + torch_class_api + ), + self.file_name, + node.lineno, + ) + return self.trans_class_method(node, torch_class_api) # Others return node diff --git a/tests/code_library/code_case/paddle_code/api_paddle_io_BatchSampler.py b/tests/code_library/code_case/paddle_code/api_paddle_io_BatchSampler.py index 7b585023e..f4bc6518e 100644 --- a/tests/code_library/code_case/paddle_code/api_paddle_io_BatchSampler.py +++ b/tests/code_library/code_case/paddle_code/api_paddle_io_BatchSampler.py @@ -1,23 +1,18 @@ import paddle print('#########################case1#########################') -o = list(paddle.io.BatchSampler(sampler=range(10) if isinstance(range(10), - paddle.io.Sampler) else paddle.io.SequenceSampler(range(10)), - batch_size=3, drop_last=True)) +o = list(paddle.io.BatchSampler(sampler=range(10), batch_size=3, drop_last= + True)) print('#########################case2#########################') -o = list(paddle.io.BatchSampler(sampler=range(10) if isinstance(range(10), - paddle.io.Sampler) else paddle.io.SequenceSampler(range(10)), - batch_size=3, drop_last=False)) +o = list(paddle.io.BatchSampler(sampler=range(10), batch_size=3, drop_last= + False)) print('#########################case3#########################') -batch_sampler_train = paddle.io.BatchSampler(sampler=range(10) if - isinstance(range(10), paddle.io.Sampler) else paddle.io.SequenceSampler - (range(10)), batch_size=2, drop_last=True) +batch_sampler_train = paddle.io.BatchSampler(sampler=range(10), batch_size= + 2, drop_last=True) print('#########################case4#########################') batch_size = 4 -batch_sampler_train = paddle.io.BatchSampler(sampler=range(10) if - isinstance(range(10), paddle.io.Sampler) else paddle.io.SequenceSampler - (range(10)), batch_size=batch_size, drop_last=False) +batch_sampler_train = paddle.io.BatchSampler(sampler=range(10), batch_size= + batch_size, drop_last=False) print('#########################case5#########################') batch_size = 4 -batch_sampler_train = paddle.io.BatchSampler(sampler=range(10) if - isinstance(range(10), paddle.io.Sampler) else paddle.io.SequenceSampler - (range(10)), batch_size=batch_size, drop_last=False) +batch_sampler_train = paddle.io.BatchSampler(sampler=range(10), batch_size= + batch_size, drop_last=False) diff --git a/tests/test_Tensor_bernoulli_.py b/tests/test_Tensor_bernoulli_.py index 76625658e..7d8725285 100644 --- a/tests/test_Tensor_bernoulli_.py +++ b/tests/test_Tensor_bernoulli_.py @@ -27,7 +27,12 @@ def test_case_1(): result = src.bernoulli_(0.5) """ ) - obj.run(pytorch_code, ["result"], check_value=False) + obj.run( + pytorch_code, + ["result"], + unsupport=True, + reason="paddle not support this API now", + ) def test_case_2(): @@ -38,7 +43,12 @@ def test_case_2(): result = src.bernoulli_(0.5, generator=None) """ ) - obj.run(pytorch_code, ["result"], check_value=False) + obj.run( + pytorch_code, + ["result"], + unsupport=True, + reason="paddle not support this API now", + ) def test_case_3(): @@ -49,4 +59,9 @@ def test_case_3(): result = src.bernoulli_(p=0.5, generator=None) """ ) - obj.run(pytorch_code, ["result"], check_value=False) + obj.run( + pytorch_code, + ["result"], + unsupport=True, + reason="paddle not support this API now", + ) diff --git a/tests/test_Tensor_div.py b/tests/test_Tensor_div.py index 509d1d0d2..da4eb3708 100644 --- a/tests/test_Tensor_div.py +++ b/tests/test_Tensor_div.py @@ -76,3 +76,29 @@ def test_case_5(): """ ) obj.run(pytorch_code, ["result"]) + + +# paddle not support type promote +# torch.div(int, int) return float, but paddle return int +def test_case_6(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([[4, 9, 8]]) + b = torch.tensor([2, 3, 4]) + result = a.div(other=b) + """ + ) + obj.run(pytorch_code, ["result"], check_dtype=False) + + +def test_alias_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([[ 0.5950,-0.0872], [2.3298, -0.2972]]) + b = torch.tensor([0.1815, -1.0111]) + result = a.divide(other=b, rounding_mode="floor") + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_divide.py b/tests/test_Tensor_divide.py deleted file mode 100644 index 74fc55ec7..000000000 --- a/tests/test_Tensor_divide.py +++ /dev/null @@ -1,78 +0,0 @@ -# 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 textwrap - -from apibase import APIBase - -obj = APIBase("torch.Tensor.divide") - - -def test_case_1(): - pytorch_code = textwrap.dedent( - """ - import torch - a = torch.tensor([ 0.5950,-0.0872, 2.3298, -0.2972]) - result = a.divide(torch.tensor([0.5])) - """ - ) - obj.run(pytorch_code, ["result"]) - - -def test_case_2(): - pytorch_code = textwrap.dedent( - """ - import torch - a = torch.tensor([[ 0.5950,-0.0872], [2.3298, -0.2972]]) - b = torch.tensor([0.1815, -1.0111]) - result = a.divide(other=b) - """ - ) - obj.run(pytorch_code, ["result"]) - - -def test_case_3(): - pytorch_code = textwrap.dedent( - """ - import torch - a = torch.tensor([[ 0.5950,-0.0872], [2.3298, -0.2972]]) - b = torch.tensor([0.1815, -1.0111]) - result = a.divide(other=b, rounding_mode=None) - """ - ) - obj.run(pytorch_code, ["result"]) - - -def test_case_4(): - pytorch_code = textwrap.dedent( - """ - import torch - a = torch.tensor([[ 0.5950,-0.0872], [2.3298, -0.2972]]) - b = torch.tensor([0.1815, -1.0111]) - result = a.divide(other=b, rounding_mode="trunc") - """ - ) - obj.run(pytorch_code, ["result"]) - - -def test_case_5(): - pytorch_code = textwrap.dedent( - """ - import torch - a = torch.tensor([[ 0.5950,-0.0872], [2.3298, -0.2972]]) - b = torch.tensor([0.1815, -1.0111]) - result = a.divide(other=b, rounding_mode="floor") - """ - ) - obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_logical_and.py b/tests/test_Tensor_logical_and.py index d4d65018f..412e52b75 100644 --- a/tests/test_Tensor_logical_and.py +++ b/tests/test_Tensor_logical_and.py @@ -66,7 +66,8 @@ def test_case_4(): obj.run(pytorch_code, ["result"]) -def test_case_5(): +# paddle not support type promote +def _test_case_5(): pytorch_code = textwrap.dedent( """ import torch diff --git a/tests/test_Tensor_logical_or.py b/tests/test_Tensor_logical_or.py index deeebc94f..eb036511f 100644 --- a/tests/test_Tensor_logical_or.py +++ b/tests/test_Tensor_logical_or.py @@ -66,7 +66,8 @@ def test_case_4(): obj.run(pytorch_code, ["result"]) -def test_case_5(): +# paddle not support type promote +def _test_case_5(): pytorch_code = textwrap.dedent( """ import torch diff --git a/tests/test_Tensor_logical_xor.py b/tests/test_Tensor_logical_xor.py index 01dddbbec..93671af6f 100644 --- a/tests/test_Tensor_logical_xor.py +++ b/tests/test_Tensor_logical_xor.py @@ -66,7 +66,8 @@ def test_case_4(): obj.run(pytorch_code, ["result"]) -def test_case_5(): +# paddle not support type promote +def _test_case_5(): pytorch_code = textwrap.dedent( """ import torch diff --git a/tests/test_Tensor_suv.py b/tests/test_Tensor_svd.py similarity index 64% rename from tests/test_Tensor_suv.py rename to tests/test_Tensor_svd.py index 53da41315..e6ec5f380 100644 --- a/tests/test_Tensor_suv.py +++ b/tests/test_Tensor_svd.py @@ -28,10 +28,10 @@ def test_case_1(): [-0.3371, -1.0584, 0.5296], [ 0.3550, -0.4022, 1.5569], [ 0.2445, -0.0158, 1.1414]]) - result = x.svd()[1] + result = x.svd() """ ) - obj.run(pytorch_code, ["result"]) + obj.run(pytorch_code, ["result"], check_value=False) def test_case_2(): @@ -43,10 +43,10 @@ def test_case_2(): [-0.3371, -1.0584, 0.5296], [ 0.3550, -0.4022, 1.5569], [ 0.2445, -0.0158, 1.1414]]) - result = x.svd(some=False)[1] + result = x.svd(False) """ ) - obj.run(pytorch_code, ["result"]) + obj.run(pytorch_code, ["result"], check_value=False) def test_case_3(): @@ -58,10 +58,12 @@ def test_case_3(): [-0.3371, -1.0584, 0.5296], [ 0.3550, -0.4022, 1.5569], [ 0.2445, -0.0158, 1.1414]]) - result = x.svd(compute_uv=False)[1] + result = x.svd(compute_uv=False) """ ) - obj.run(pytorch_code, ["result"]) + obj.run( + pytorch_code, ["result"], unsupport=True, reason="compute_uv is not supported" + ) def test_case_4(): @@ -73,7 +75,49 @@ def test_case_4(): [-0.3371, -1.0584, 0.5296], [ 0.3550, -0.4022, 1.5569], [ 0.2445, -0.0158, 1.1414]]) - result = x.svd(some=False, compute_uv=False)[1] + result = x.svd(False, False) + """ + ) + obj.run( + pytorch_code, ["result"], unsupport=True, reason="compute_uv is not supported" + ) + + +def test_case_5(): + pytorch_code = textwrap.dedent( + """ + import torch + + x = torch.tensor( + [ + [0.2364, -0.7752, 0.6372], + [1.7201, 0.7394, -0.0504], + [-0.3371, -1.0584, 0.5296], + [0.3550, -0.4022, 1.5569], + [0.2445, -0.0158, 1.1414], + ] + ) + result = x.svd(some=False)[1] + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_6(): + pytorch_code = textwrap.dedent( + """ + import torch + + x = torch.tensor( + [ + [0.2364, -0.7752, 0.6372], + [1.7201, 0.7394, -0.0504], + [-0.3371, -1.0584, 0.5296], + [0.3550, -0.4022, 1.5569], + [0.2445, -0.0158, 1.1414], + ] + ) + result = x.svd(True)[1] """ ) obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_true_divide.py b/tests/test_Tensor_true_divide.py index da5e4feda..73634f7c1 100644 --- a/tests/test_Tensor_true_divide.py +++ b/tests/test_Tensor_true_divide.py @@ -23,8 +23,34 @@ def test_case_1(): pytorch_code = textwrap.dedent( """ import torch - a = torch.Tensor([[1.,2.], [3.,4.]]) + a = torch.Tensor([[1., 2.], [3., 4.]]) result = a.true_divide(a) """ ) obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.Tensor([[1., 2.], [3., 4.]]) + b = torch.Tensor([[5., 6.], [7., 8.]]) + result = a.true_divide(other=b) + """ + ) + obj.run(pytorch_code, ["result"]) + + +# paddle not support type promote +# torch.true_divide(int, int) return float, but paddle return int +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([[4, 9, 8]]) + b = torch.tensor([2, 3, 4]) + result = a.true_divide(other=b) + """ + ) + obj.run(pytorch_code, ["result"], check_dtype=False) diff --git a/tests/test_Tensor_unique.py b/tests/test_Tensor_unique.py index 6aea10902..ee37a692a 100644 --- a/tests/test_Tensor_unique.py +++ b/tests/test_Tensor_unique.py @@ -49,7 +49,9 @@ def test_case_3(): result = src.unique(sorted=True, return_inverse=True, return_counts=True) """ ) - obj.run(pytorch_code, ["result"]) + obj.run( + pytorch_code, ["result"], unsupport=True, reason="Paddle not support 'sorted' " + ) def test_case_4(): @@ -60,7 +62,9 @@ def test_case_4(): result = src.unique(sorted=True, return_inverse=True, return_counts=True, dim=0) """ ) - obj.run(pytorch_code, ["result"]) + obj.run( + pytorch_code, ["result"], unsupport=True, reason="Paddle not support 'sorted' " + ) def test_case_5(): @@ -71,4 +75,6 @@ def test_case_5(): result = src.unique(True, True, dim=0) """ ) - obj.run(pytorch_code, ["result"]) + obj.run( + pytorch_code, ["result"], unsupport=True, reason="Paddle not support 'sorted' " + ) diff --git a/tests/test_arange.py b/tests/test_arange.py index 6baeb7e78..0c3e6c2c8 100644 --- a/tests/test_arange.py +++ b/tests/test_arange.py @@ -77,3 +77,14 @@ def test_case_6(): """ ) obj.run(pytorch_code, ["result"]) + + +def test_case_7(): + pytorch_code = textwrap.dedent( + """ + import torch + out = torch.rand([3], dtype=torch.float64) + result = torch.arange(1, 2.5, 0.5, out=out, dtype=torch.float64) + """ + ) + obj.run(pytorch_code, ["result", "out"]) diff --git a/tests/test_div.py b/tests/test_div.py index e3ed6e408..96e59f693 100644 --- a/tests/test_div.py +++ b/tests/test_div.py @@ -101,3 +101,30 @@ def test_case_7(): """ ) obj.run(pytorch_code, ["out"]) + + +# paddle not support type promote +# torch.div(int, int) return float, but paddle return int +def test_case_8(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([[4, 9, 8]]) + b = torch.tensor([2, 3, 4]) + result = torch.div(input=a, other=b) + """ + ) + obj.run(pytorch_code, ["result"], check_dtype=False) + + +def test_alias_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([[ 0.5950,-0.0872], [2.3298, -0.2972]]) + b = torch.tensor([0.1815, -1.0111]) + out = torch.tensor([[ 0.5950,-0.0872], [2.3298, -0.2972]]) + result = torch.divide(input=a, other=b, rounding_mode="floor", out=out) + """ + ) + obj.run(pytorch_code, ["out"]) diff --git a/tests/test_divide.py b/tests/test_divide.py deleted file mode 100644 index cda1f692c..000000000 --- a/tests/test_divide.py +++ /dev/null @@ -1,91 +0,0 @@ -# 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 textwrap - -from apibase import APIBase - -obj = APIBase("torch.divide") - - -def test_case_1(): - pytorch_code = textwrap.dedent( - """ - import torch - a = torch.tensor([ 0.5950,-0.0872, 2.3298, -0.2972]) - result = torch.divide(a, torch.tensor([0.5])) - """ - ) - obj.run(pytorch_code, ["result"]) - - -def test_case_2(): - pytorch_code = textwrap.dedent( - """ - import torch - a = torch.tensor([[ 0.5950,-0.0872], [2.3298, -0.2972]]) - b = torch.tensor([0.1815, -1.0111]) - result = torch.divide(input=a, other=b) - """ - ) - obj.run(pytorch_code, ["result"]) - - -def test_case_3(): - pytorch_code = textwrap.dedent( - """ - import torch - a = torch.tensor([[ 0.5950,-0.0872], [2.3298, -0.2972]]) - b = torch.tensor([0.1815, -1.0111]) - result = torch.divide(input=a, other=b, rounding_mode=None) - """ - ) - obj.run(pytorch_code, ["result"]) - - -def test_case_4(): - pytorch_code = textwrap.dedent( - """ - import torch - a = torch.tensor([[ 0.5950,-0.0872], [2.3298, -0.2972]]) - b = torch.tensor([0.1815, -1.0111]) - result = torch.divide(input=a, other=b, rounding_mode="trunc") - """ - ) - obj.run(pytorch_code, ["result"]) - - -def test_case_5(): - pytorch_code = textwrap.dedent( - """ - import torch - a = torch.tensor([[ 0.5950,-0.0872], [2.3298, -0.2972]]) - b = torch.tensor([0.1815, -1.0111]) - result = torch.divide(input=a, other=b, rounding_mode="floor") - """ - ) - obj.run(pytorch_code, ["result"]) - - -def test_case_6(): - pytorch_code = textwrap.dedent( - """ - import torch - a = torch.tensor([[ 0.5950,-0.0872], [2.3298, -0.2972]]) - b = torch.tensor([0.1815, -1.0111]) - out = torch.tensor([[ 0.5950,-0.0872], [2.3298, -0.2972]]) - result = torch.divide(input=a, other=b, rounding_mode="floor", out=out) - """ - ) - obj.run(pytorch_code, ["out"]) diff --git a/tests/test_logaddexp.py b/tests/test_logaddexp.py index 8d5fa7f8b..4b84fe2dc 100644 --- a/tests/test_logaddexp.py +++ b/tests/test_logaddexp.py @@ -56,7 +56,7 @@ def test_case_4(): pytorch_code = textwrap.dedent( """ import torch - result = torch.logaddexp(torch.tensor([1.]),torch.tensor([1., 2., -3., -4., 5.])) + result = torch.logaddexp(torch.tensor([1.]), torch.tensor([1., 2., -3., -4., 5.])) """ ) obj.run(pytorch_code, ["result"]) @@ -66,7 +66,7 @@ def test_case_5(): pytorch_code = textwrap.dedent( """ import torch - result = torch.logaddexp(torch.tensor([1.]),torch.tensor([1, 2, -3])) + result = torch.logaddexp(torch.tensor([1.]), torch.tensor([1, 2, -3])) """ ) obj.run(pytorch_code, ["result"]) diff --git a/tests/test_logical_and.py b/tests/test_logical_and.py index 786cbfcd6..84ac1d1b5 100644 --- a/tests/test_logical_and.py +++ b/tests/test_logical_and.py @@ -79,7 +79,8 @@ def test_case_5(): obj.run(pytorch_code, ["result", "out"]) -def test_case_6(): +# paddle not support type promote +def _test_case_6(): pytorch_code = textwrap.dedent( """ import torch diff --git a/tests/test_logical_or.py b/tests/test_logical_or.py index c88b6e3a1..3a7d77816 100644 --- a/tests/test_logical_or.py +++ b/tests/test_logical_or.py @@ -79,7 +79,8 @@ def test_case_5(): obj.run(pytorch_code, ["result", "out"]) -def test_case_6(): +# paddle not support input type promote +def _test_case_6(): pytorch_code = textwrap.dedent( """ import torch diff --git a/tests/test_logical_xor.py b/tests/test_logical_xor.py index 56b84a0b1..46c92b9f6 100644 --- a/tests/test_logical_xor.py +++ b/tests/test_logical_xor.py @@ -79,7 +79,8 @@ def test_case_5(): obj.run(pytorch_code, ["result", "out"]) -def test_case_6(): +# paddle not support input type promote +def _test_case_6(): pytorch_code = textwrap.dedent( """ import torch diff --git a/tests/test_logsumexp.py b/tests/test_logsumexp.py index 4c5801ce4..4872818c7 100644 --- a/tests/test_logsumexp.py +++ b/tests/test_logsumexp.py @@ -75,7 +75,8 @@ def test_case_5(): obj.run(pytorch_code, ["result"]) -def test_case_6(): +# paddle not support input integer +def _test_case_6(): pytorch_code = textwrap.dedent( """ import torch diff --git a/tests/test_mul.py b/tests/test_mul.py index c19e42483..d92feec3f 100644 --- a/tests/test_mul.py +++ b/tests/test_mul.py @@ -32,7 +32,8 @@ def test_case_1(): obj.run(pytorch_code, ["result"]) -def test_case_2(): +# paddle not support input type promote +def _test_case_2(): pytorch_code = textwrap.dedent( """ import torch @@ -44,7 +45,8 @@ def test_case_2(): obj.run(pytorch_code, ["result"]) -def test_case_3(): +# paddle not support input type promote +def _test_case_3(): pytorch_code = textwrap.dedent( """ import torch @@ -55,7 +57,8 @@ def test_case_3(): obj.run(pytorch_code, ["result"]) -def test_case_4(): +# paddle not support input type promote +def _test_case_4(): pytorch_code = textwrap.dedent( """ import torch @@ -66,7 +69,8 @@ def test_case_4(): obj.run(pytorch_code, ["result"]) -def test_case_5(): +# paddle not support input type promote +def _test_case_5(): pytorch_code = textwrap.dedent( """ import torch @@ -76,3 +80,27 @@ def test_case_5(): """ ) obj.run(pytorch_code, ["result", "out"]) + + +def test_case_6(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([0.2015, -0.4255, 2.6087]) + out = torch.tensor([0.2015, -0.4255, 2.6087]) + result = torch.mul(input=input, other=torch.tensor(5.), out=out) + """ + ) + obj.run(pytorch_code, ["result", "out"]) + + +def test_case_7(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([0.2015, -0.4255, 2.6087]) + other = torch.tensor([0.2015, -0.4255, 2.6087]) + result = torch.mul(other=other, input=input) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_nn_functional_batch_norm.py b/tests/test_nn_functional_batch_norm.py index fa0468c13..1d84394a8 100644 --- a/tests/test_nn_functional_batch_norm.py +++ b/tests/test_nn_functional_batch_norm.py @@ -25,12 +25,12 @@ def test_case_1(): import torch.nn.functional as F import torch input = torch.tensor([[[ 1.1524, 0.4714, 0.2857], - [-1.2533, -0.9829, -1.0981], - [ 0.1507, -1.1431, -2.0361]], + [-1.2533, -0.9829, -1.0981], + [ 0.1507, -1.1431, -2.0361]], - [[ 0.1024, -0.4482, 0.4137], - [ 0.9385, 0.4565, 0.7702], - [ 0.4135, -0.2587, 0.0482]]]) + [[ 0.1024, -0.4482, 0.4137], + [ 0.9385, 0.4565, 0.7702], + [ 0.4135, -0.2587, 0.0482]]]) data = torch.tensor([1., 1., 1.]) result = F.batch_norm(input, data, data, data, data) """ @@ -44,12 +44,12 @@ def test_case_2(): import torch.nn.functional as F import torch input = torch.tensor([[[ 1.1524, 0.4714, 0.2857], - [-1.2533, -0.9829, -1.0981], - [ 0.1507, -1.1431, -2.0361]], + [-1.2533, -0.9829, -1.0981], + [ 0.1507, -1.1431, -2.0361]], - [[ 0.1024, -0.4482, 0.4137], - [ 0.9385, 0.4565, 0.7702], - [ 0.4135, -0.2587, 0.0482]]]) + [[ 0.1024, -0.4482, 0.4137], + [ 0.9385, 0.4565, 0.7702], + [ 0.4135, -0.2587, 0.0482]]]) data = torch.tensor([1., 1., 1.]) result = F.batch_norm(input=input, running_mean=data, running_var=data, weight=data, bias=data) """ @@ -57,7 +57,8 @@ def test_case_2(): obj.run(pytorch_code, ["result"]) -def test_case_3(): +# when training=True, paddle has bug +def _test_case_3(): pytorch_code = textwrap.dedent( """ import torch.nn.functional as F @@ -70,7 +71,7 @@ def test_case_3(): [ 0.9385, 0.4565, 0.7702], [ 0.4135, -0.2587, 0.0482]]]) data = torch.tensor([1., 1., 1.]) - result = F.batch_norm(input=input, running_mean=data, running_var=data, weight=data, bias=data, momentum=0.5) + result = F.batch_norm(input=input, running_mean=data, running_var=data, weight=data, bias=data, training=True, momentum=0.5) """ ) obj.run(pytorch_code, ["result"]) @@ -82,14 +83,53 @@ def test_case_4(): import torch.nn.functional as F import torch input = torch.tensor([[[ 1.1524, 0.4714, 0.2857], - [-1.2533, -0.9829, -1.0981], - [ 0.1507, -1.1431, -2.0361]], + [-1.2533, -0.9829, -1.0981], + [ 0.1507, -1.1431, -2.0361]], - [[ 0.1024, -0.4482, 0.4137], - [ 0.9385, 0.4565, 0.7702], - [ 0.4135, -0.2587, 0.0482]]]) + [[ 0.1024, -0.4482, 0.4137], + [ 0.9385, 0.4565, 0.7702], + [ 0.4135, -0.2587, 0.0482]]]) data = torch.tensor([1., 1., 1.]) result = F.batch_norm(input=input, running_mean=data, running_var=data, weight=data, bias=data, eps=1e-4) """ ) obj.run(pytorch_code, ["result"]) + + +def test_case_5(): + pytorch_code = textwrap.dedent( + """ + import torch.nn.functional as F + import torch + input = torch.tensor([[[ 1.1524, 0.4714, 0.2857], + [-1.2533, -0.9829, -1.0981], + [ 0.1507, -1.1431, -2.0361]], + + [[ 0.1024, -0.4482, 0.4137], + [ 0.9385, 0.4565, 0.7702], + [ 0.4135, -0.2587, 0.0482]]]) + data = torch.tensor([1., 1., 1.]) + result = F.batch_norm(input, data, data, data, data, False, 0.2, 1e-4) + """ + ) + obj.run(pytorch_code, ["result"]) + + +# when training=True, paddle has bug +def _test_case_6(): + pytorch_code = textwrap.dedent( + """ + import torch.nn.functional as F + import torch + input = torch.tensor([[[ 1.1524, 0.4714, 0.2857], + [-1.2533, -0.9829, -1.0981], + [ 0.1507, -1.1431, -2.0361]], + + [[ 0.1024, -0.4482, 0.4137], + [ 0.9385, 0.4565, 0.7702], + [ 0.4135, -0.2587, 0.0482]]]) + data = torch.tensor([1., 1., 1.]) + result = F.batch_norm(input, data, data, data, data, True, 0.3, 1e-2) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_nn_functional_instance_norm.py b/tests/test_nn_functional_instance_norm.py index 43ed6f629..370a1477e 100644 --- a/tests/test_nn_functional_instance_norm.py +++ b/tests/test_nn_functional_instance_norm.py @@ -25,12 +25,12 @@ def test_case_1(): import torch.nn.functional as F import torch input = torch.tensor([[[ 1.1524, 0.4714, 0.2857], - [-1.2533, -0.9829, -1.0981], - [ 0.1507, -1.1431, -2.0361]], + [-1.2533, -0.9829, -1.0981], + [ 0.1507, -1.1431, -2.0361]], - [[ 0.1024, -0.4482, 0.4137], - [ 0.9385, 0.4565, 0.7702], - [ 0.4135, -0.2587, 0.0482]]]) + [[ 0.1024, -0.4482, 0.4137], + [ 0.9385, 0.4565, 0.7702], + [ 0.4135, -0.2587, 0.0482]]]) data = torch.tensor([1., 1., 1.]) result = F.instance_norm(input, data, data, data, data) """ @@ -63,12 +63,12 @@ def test_case_3(): import torch.nn.functional as F import torch input = torch.tensor([[[ 1.1524, 0.4714, 0.2857], - [-1.2533, -0.9829, -1.0981], - [ 0.1507, -1.1431, -2.0361]], + [-1.2533, -0.9829, -1.0981], + [ 0.1507, -1.1431, -2.0361]], - [[ 0.1024, -0.4482, 0.4137], - [ 0.9385, 0.4565, 0.7702], - [ 0.4135, -0.2587, 0.0482]]]) + [[ 0.1024, -0.4482, 0.4137], + [ 0.9385, 0.4565, 0.7702], + [ 0.4135, -0.2587, 0.0482]]]) data = torch.tensor([1., 1., 1.]) result = F.instance_norm(input=input, running_mean=data, running_var=data, weight=data, bias=data, momentum=0.5) """ @@ -101,14 +101,33 @@ def test_case_5(): import torch.nn.functional as F import torch input = torch.tensor([[[ 1.1524, 0.4714, 0.2857], - [-1.2533, -0.9829, -1.0981], - [ 0.1507, -1.1431, -2.0361]], + [-1.2533, -0.9829, -1.0981], + [ 0.1507, -1.1431, -2.0361]], - [[ 0.1024, -0.4482, 0.4137], - [ 0.9385, 0.4565, 0.7702], - [ 0.4135, -0.2587, 0.0482]]]) + [[ 0.1024, -0.4482, 0.4137], + [ 0.9385, 0.4565, 0.7702], + [ 0.4135, -0.2587, 0.0482]]]) data = torch.tensor([1., 1., 1.]) result = F.instance_norm(input=input, running_mean=data, running_var=data, weight=data, bias=data, eps=1e-4, use_input_stats=True) """ ) obj.run(pytorch_code, ["result"], rtol=1.0e-5, atol=1.0e-8) + + +def test_case_6(): + pytorch_code = textwrap.dedent( + """ + import torch.nn.functional as F + import torch + input = torch.tensor([[[ 1.1524, 0.4714, 0.2857], + [-1.2533, -0.9829, -1.0981], + [ 0.1507, -1.1431, -2.0361]], + + [[ 0.1024, -0.4482, 0.4137], + [ 0.9385, 0.4565, 0.7702], + [ 0.4135, -0.2587, 0.0482]]]) + data = torch.tensor([1., 1., 1.]) + result = F.instance_norm(input, data, data, data, data, True, 0.2, 1e-4) + """ + ) + obj.run(pytorch_code, ["result"], rtol=1.0e-5, atol=1.0e-8) diff --git a/tests/test_nn_functional_upsample_bilinear.py b/tests/test_nn_functional_upsample_bilinear.py index fe8de3c53..fae11202a 100644 --- a/tests/test_nn_functional_upsample_bilinear.py +++ b/tests/test_nn_functional_upsample_bilinear.py @@ -25,12 +25,12 @@ def test_case_1(): import torch.nn.functional as F import torch input = torch.tensor([[[[ 1.1524, 0.4714, 0.2857], - [-1.2533, -0.9829, -1.0981], - [ 0.1507, -1.1431, -2.0361]], + [-1.2533, -0.9829, -1.0981], + [ 0.1507, -1.1431, -2.0361]], - [[ 0.1024, -0.4482, 0.4137], - [ 0.9385, 0.4565, 0.7702], - [ 0.4135, -0.2587, 0.0482]]]]) + [[ 0.1024, -0.4482, 0.4137], + [ 0.9385, 0.4565, 0.7702], + [ 0.4135, -0.2587, 0.0482]]]]) result = F.upsample_bilinear(input, (2, 2)) """ ) @@ -43,12 +43,12 @@ def test_case_2(): import torch.nn.functional as F import torch input = torch.tensor([[[[ 1.1524, 0.4714, 0.2857], - [-1.2533, -0.9829, -1.0981], - [ 0.1507, -1.1431, -2.0361]], + [-1.2533, -0.9829, -1.0981], + [ 0.1507, -1.1431, -2.0361]], - [[ 0.1024, -0.4482, 0.4137], - [ 0.9385, 0.4565, 0.7702], - [ 0.4135, -0.2587, 0.0482]]]]) + [[ 0.1024, -0.4482, 0.4137], + [ 0.9385, 0.4565, 0.7702], + [ 0.4135, -0.2587, 0.0482]]]]) result = F.upsample_bilinear(input=input, scale_factor=2) """ ) @@ -61,12 +61,12 @@ def test_case_3(): import torch.nn.functional as F import torch input = torch.tensor([[[[ 1.1524, 0.4714, 0.2857], - [-1.2533, -0.9829, -1.0981], - [ 0.1507, -1.1431, -2.0361]], + [-1.2533, -0.9829, -1.0981], + [ 0.1507, -1.1431, -2.0361]], - [[ 0.1024, -0.4482, 0.4137], - [ 0.9385, 0.4565, 0.7702], - [ 0.4135, -0.2587, 0.0482]]]]) + [[ 0.1024, -0.4482, 0.4137], + [ 0.9385, 0.4565, 0.7702], + [ 0.4135, -0.2587, 0.0482]]]]) result = F.upsample_bilinear(input=input, size=[2, 2]) """ ) diff --git a/tests/test_special_logsumexp.py b/tests/test_special_logsumexp.py index 0c2757088..6e310aa83 100644 --- a/tests/test_special_logsumexp.py +++ b/tests/test_special_logsumexp.py @@ -75,7 +75,8 @@ def test_case_5(): obj.run(pytorch_code, ["result"]) -def test_case_6(): +# paddle not support input integer +def _test_case_6(): pytorch_code = textwrap.dedent( """ import torch diff --git a/tests/test_trapezoid.py b/tests/test_trapezoid.py index dac12678a..df40e9e1e 100644 --- a/tests/test_trapezoid.py +++ b/tests/test_trapezoid.py @@ -76,3 +76,27 @@ def test_case_5(): """ ) obj.run(pytorch_code, ["result"]) + + +def test_alias_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + y = torch.tensor([1, 1, 1, 0, 1]).type(torch.float32) + x = torch.tensor([1, 2, 3, 0, 1]).type(torch.float32) + result = torch.trapz(y, dx=2, dim=-1) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_alias_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + y = torch.tensor([1, 1, 1, 0, 1]).type(torch.float32) + x = torch.tensor([1, 2, 3, 0, 1]).type(torch.float32) + result = torch.trapz(y=y, x=x, dim=-1) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_true_divide.py b/tests/test_true_divide.py index 81370451a..c48f9f68e 100644 --- a/tests/test_true_divide.py +++ b/tests/test_true_divide.py @@ -19,7 +19,9 @@ obj = APIBase("torch.true_divide") -def test_case_1(): +# paddle not support type promote +# torch.true_divide(int, int) return 'float', but paddle return 'int' whose result is wrong +def _test_case_1(): pytorch_code = textwrap.dedent( """ import torch @@ -28,7 +30,7 @@ def test_case_1(): result = torch.true_divide(a, b) """ ) - obj.run(pytorch_code, ["result"]) + obj.run(pytorch_code, ["result"], check_dtype=False) def test_case_2(): @@ -62,12 +64,14 @@ def test_case_4(): a = torch.tensor([4.67, 9.76 , 8.53]) b = torch.tensor([3.5, 3.90, 1.83]) out = torch.tensor([4.67, 9.76 , 8.53]) - result = torch.true_divide(a, b, out=out) + result = torch.true_divide(a, other=b, out=out) """ ) obj.run(pytorch_code, ["result", "out"]) +# paddle not support type promote +# torch.true_divide(int, int) return float, but paddle return int def test_case_5(): pytorch_code = textwrap.dedent( """ @@ -77,4 +81,4 @@ def test_case_5(): result = torch.true_divide(input=a, other=b) """ ) - obj.run(pytorch_code, ["result"]) + obj.run(pytorch_code, ["result"], check_dtype=False) diff --git a/tests/test_unique.py b/tests/test_unique.py index 91d7c3562..f17f05c72 100644 --- a/tests/test_unique.py +++ b/tests/test_unique.py @@ -67,8 +67,5 @@ def test_case_4(): """ ) obj.run( - pytorch_code, - ["result"], - unsupport=True, - reason="Paddle does not currently support the 'sorted' input parameter.", + pytorch_code, ["result"], unsupport=True, reason="Paddle not support 'sorted' " ) diff --git a/tests/test_utils_data_BatchSampler.py b/tests/test_utils_data_BatchSampler.py index 51c141290..f76711aa9 100644 --- a/tests/test_utils_data_BatchSampler.py +++ b/tests/test_utils_data_BatchSampler.py @@ -23,28 +23,33 @@ def test_case_1(): pytorch_code = textwrap.dedent( """ from torch.utils.data import BatchSampler - result = list(BatchSampler(range(10), batch_size=3, drop_last=True)) + from torch.utils.data import SequentialSampler + batch_sampler = BatchSampler(sampler=SequentialSampler([3, 9, 10, 5, 7, 6, 1]), batch_size=3, drop_last=True) + result = list(batch_sampler) """ ) obj.run(pytorch_code, ["result"]) -def test_case_2(): +# paddle not support sampler is any iterable +def _test_case_2(): pytorch_code = textwrap.dedent( """ from torch.utils.data import BatchSampler - result = list(BatchSampler(range(10), batch_size=3, drop_last=False)) + batch_sampler = BatchSampler([3, 9, 10, 5, 7, 6, 1], batch_size=3, drop_last=False) + result = list(batch_sampler) """ ) obj.run(pytorch_code, ["result"]) -def test_case_3(): +# paddle not support sampler is any iterable +def _test_case_3(): pytorch_code = textwrap.dedent( """ import torch - result = batch_sampler_train = torch.utils.data.BatchSampler(range(10), 2, drop_last=True) - result = list(result) + batch_sampler = torch.utils.data.BatchSampler([3, 9, 10, 5, 7, 6, 1], 2, True) + result = list(batch_sampler) """ ) obj.run(pytorch_code, ["result"]) @@ -55,19 +60,19 @@ def test_case_4(): """ import torch batch_size = 4 - result = batch_sampler_train = torch.utils.data.BatchSampler(range(10), batch_size, drop_last=False) - result = list(result) + batch_sampler = torch.utils.data.BatchSampler(torch.utils.data.SequentialSampler([3, 9, 10, 5, 7, 6, 1]), batch_size, False) + result = list(batch_sampler) """ ) obj.run(pytorch_code, ["result"]) -def test_case_5(): +# paddle not support sampler is any iterable +def _test_case_5(): pytorch_code = textwrap.dedent( """ import torch - batch_size = 4 - result = list(torch.utils.data.BatchSampler(sampler=range(10), batch_size=batch_size, drop_last=False)) + result = list(torch.utils.data.BatchSampler(batch_size=4, drop_last=False, sampler=[3, 9, 10, 5, 7, 6, 1])) """ ) obj.run(pytorch_code, ["result"]) @@ -77,8 +82,8 @@ def test_alias_case_1(): pytorch_code = textwrap.dedent( """ import torch - batch_size = 4 - result = list(torch.utils.data.sampler.BatchSampler(sampler=range(10), batch_size=batch_size, drop_last=False)) + batch_sampler = torch.utils.data.sampler.BatchSampler(sampler=torch.utils.data.SequentialSampler([3, 9, 10, 5, 7, 6, 1]), batch_size=3, drop_last=True) + result = list(batch_sampler) """ ) obj.run(pytorch_code, ["result"]) diff --git a/tests/test_utils_data_SequentialSampler.py b/tests/test_utils_data_SequentialSampler.py index da08208ca..d3883b6c3 100644 --- a/tests/test_utils_data_SequentialSampler.py +++ b/tests/test_utils_data_SequentialSampler.py @@ -26,28 +26,17 @@ def test_case_1(): from torch.utils.data import Dataset import numpy as np - class Data(Dataset): + class MyDataset(Dataset): def __init__(self): - self.x = np.arange(0,100,1) + self.x = np.arange(0, 100, 1) def __getitem__(self, idx): return self.x[idx] def __len__(self): - return self.x.shape[0] - - class MySampler(SequentialSampler): - def __init__(self, data_source): - self.data_source = data_source - - def __iter__(self): - return iter(range(len(self.data_source))) + return len(self.x) - def __len__(self): - return len(self.data_source) - - data = Data() - s = MySampler(data) + s = SequentialSampler(MyDataset()) result = [] for d in s: result.append(d) @@ -63,28 +52,18 @@ def test_case_2(): from torch.utils.data import Dataset import numpy as np - class Data(Dataset): + class MyDataset(Dataset): def __init__(self): - self.x = np.arange(0,100,1) + self.x = np.arange(0, 100, 1) def __getitem__(self, idx): return self.x[idx] def __len__(self): - return self.x.shape[0] - - class MySampler(SequentialSampler): - def __init__(self, data): - self.data_source = data - - def __iter__(self): - return iter(range(len(self.data_source))) - - def __len__(self): - return len(self.data_source) + return len(self.x) - data = Data() - s = MySampler(data=data) + data = MyDataset() + s = SequentialSampler(data_source=data) result = [] for d in s: result.append(d) @@ -101,9 +80,9 @@ def test_case_3(): import numpy as np import torch - class Data(Dataset): + class MyDataset(Dataset): def __init__(self): - self.x = np.arange(0,100,1).reshape(10, 10) + self.x = np.arange(0, 100, 1).reshape(10, 10) self.y = np.arange(0, 10, 1) def __getitem__(self, idx): @@ -122,11 +101,11 @@ def __iter__(self): def __len__(self): return len(self.data_source) - data = Data() - s = MySampler(data) + dataset = MyDataset() + s = MySampler(dataset) result = [] - for idx in s: - result.append(idx) + for d in s: + result.append(d) result = torch.tensor(result) """ ) diff --git a/tests/test_utils_data_random_split.py b/tests/test_utils_data_random_split.py index 074fcc8fd..fe2789e2c 100644 --- a/tests/test_utils_data_random_split.py +++ b/tests/test_utils_data_random_split.py @@ -105,7 +105,8 @@ def __len__(self): obj.run(pytorch_code, ["results"]) -def test_case_4(): +# paddle not support lengths is decimal +def _test_case_4(): pytorch_code = textwrap.dedent( """ import torch @@ -134,7 +135,8 @@ def __len__(self): obj.run(pytorch_code, ["results"]) -def test_case_5(): +# paddle not support lengths is decimal +def _test_case_5(): pytorch_code = textwrap.dedent( """ import torch @@ -149,7 +151,8 @@ def test_case_5(): obj.run(pytorch_code, ["results"]) -def test_case_6(): +# paddle not support lengths is decimal +def _test_case_6(): pytorch_code = textwrap.dedent( """ import torch @@ -165,7 +168,8 @@ def test_case_6(): obj.run(pytorch_code, ["results"]) -def test_case_7(): +# paddle not support lengths is decimal +def _test_case_7(): pytorch_code = textwrap.dedent( """ import torch