From e7837eacc4e56f14862511c9cc291e82ad17d2a7 Mon Sep 17 00:00:00 2001 From: co63oc Date: Thu, 24 Aug 2023 18:09:42 +0800 Subject: [PATCH] Add test --- paconvert/api_alias_mapping.json | 1 + tests/test_distributions_Distribution.py | 86 ++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 tests/test_distributions_Distribution.py diff --git a/paconvert/api_alias_mapping.json b/paconvert/api_alias_mapping.json index 035f4f43a..9394ed908 100644 --- a/paconvert/api_alias_mapping.json +++ b/paconvert/api_alias_mapping.json @@ -7,6 +7,7 @@ "torch.distributions.cauchy.Cauchy": "torch.distributions.Cauchy", "torch.distributions.continuous_bernoulli.ContinuousBernoulli": "torch.distributions.ContinuousBernoulli", "torch.distributions.dirichlet.Dirichlet": "torch.distributions.Dirichlet", + "torch.distributions.distribution.Distribution": "torch.distributions.Distribution", "torch.distributions.exp_family.ExponentialFamily": "torch.distributions.ExponentialFamily", "torch.distributions.exponential.Exponential": "torch.distributions.Exponential", "torch.distributions.geometric.Geometric": "torch.distributions.Geometric", diff --git a/tests/test_distributions_Distribution.py b/tests/test_distributions_Distribution.py new file mode 100644 index 000000000..219391d49 --- /dev/null +++ b/tests/test_distributions_Distribution.py @@ -0,0 +1,86 @@ +# 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 + +import paddle +from apibase import APIBase + + +class ResultAPIBase(APIBase): + def compare( + self, + name, + pytorch_result, + paddle_result, + check_value=True, + check_dtype=True, + check_stop_gradient=True, + rtol=1.0e-6, + atol=0.0, + ): + assert isinstance(paddle_result, paddle.distribution.Distribution) + + +obj = ResultAPIBase("torch.distributions.Distribution") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.distributions.Distribution() + """ + ) + obj.run(pytorch_code, ["result"], check_value=False) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.distributions.Distribution(batch_shape=torch.Size([]), event_shape=torch.Size([])) + """ + ) + obj.run(pytorch_code, ["result"], check_value=False) + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.distributions.Distribution(event_shape=torch.Size([]), batch_shape=torch.Size([]), validate_args=None) + """ + ) + obj.run(pytorch_code, ["result"], check_value=False) + + +def test_case_4(): + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.distributions.Distribution(torch.Size([]), torch.Size([]), validate_args=None) + """ + ) + obj.run(pytorch_code, ["result"], check_value=False) + + +def test_case_5(): + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.distributions.Distribution(batch_shape=torch.Size([]), event_shape=torch.Size([]), validate_args=None) + """ + ) + obj.run(pytorch_code, ["result"], check_value=False)