Skip to content

Commit

Permalink
【Hackathon 5th No.45】convert 75-81 (#341)
Browse files Browse the repository at this point in the history
* convert IndependentTransform

* fix bug

* fix bug

* fix bug

* fix bug

* fix bug

* transfer ReshapeTransform

* Update test_distributions_transforms_ReshapeTransform.py

* transfer ExpTransform and PowerTransform

* fix bug

* fix bug

* transfer TanhTransform, AbsTransform, AffineTransform
  • Loading branch information
longranger2 authored Dec 22, 2023
1 parent 21e5bd1 commit 98a03ff
Show file tree
Hide file tree
Showing 8 changed files with 353 additions and 0 deletions.
75 changes: 75 additions & 0 deletions paconvert/api_mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -5529,6 +5529,30 @@
"tag"
]
},
"torch.distributions.AbsTransform": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.distribution.AbsTransform",
"args_list": [
"cache_size"
],
"kwargs_change": {
"cache_size": ""
}
},
"torch.distributions.AffineTransform": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.distribution.AffineTransform",
"args_list": [
"loc",
"scale",
"event_dim",
"cache_size"
],
"kwargs_change": {
"event_dim": "",
"cache_size": ""
}
},
"torch.distributions.Bernoulli": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.distribution.Bernoulli",
Expand Down Expand Up @@ -5663,6 +5687,11 @@
},
"torch.distributions.Distribution.sample_n": {},
"torch.distributions.Distribution.set_default_validate_args": {},
"torch.distributions.ExpTransform": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.distribution.ExpTransform",
"args_list": []
},
"torch.distributions.ExponentialFamily": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.distribution.ExponentialFamily",
Expand Down Expand Up @@ -5720,6 +5749,18 @@
"validate_args": ""
}
},
"torch.distributions.IndependentTransform": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.distribution.IndependentTransform",
"args_list": [
"base_transform",
"reinterpreted_batch_ndims"
],
"kwargs_change": {
"base_transform": "base",
"reinterpreted_batch_ndims": "reinterpreted_batch_rank"
}
},
"torch.distributions.Laplace": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.distribution.Laplace",
Expand Down Expand Up @@ -5776,6 +5817,30 @@
"validate_args": ""
}
},
"torch.distributions.PowerTransform": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.distribution.PowerTransform",
"args_list": [
"exponent",
"cache_size"
],
"kwargs_change": {
"exponent": "power",
"cache_size": ""
}
},
"torch.distributions.ReshapeTransform": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.distribution.ReshapeTransform",
"args_list": [
"in_shape",
"out_shape"
],
"kwargs_change": {
"in_shape": "in_event_shape",
"out_shape": "out_event_shape"
}
},
"torch.distributions.SigmoidTransform": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.distribution.SigmoidTransform",
Expand Down Expand Up @@ -5824,6 +5889,16 @@
"cache_size": ""
}
},
"torch.distributions.TanhTransform": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.distribution.TanhTransform",
"args_list": [
"cache_size"
],
"kwargs_change": {
"cache_size": ""
}
},
"torch.distributions.Transform": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.distribution.Transform",
Expand Down
43 changes: 43 additions & 0 deletions tests/test_distributions_AbsTransform.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# 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.distributions.AbsTransform")


def test_case_1():
pytorch_code = textwrap.dedent(
"""
import torch
x = torch.ones((2,3))
t = torch.distributions.AbsTransform()
result = t(x)
"""
)
obj.run(pytorch_code, ["result"])


def test_case_2():
pytorch_code = textwrap.dedent(
"""
import torch
x = torch.ones((2,3))
t = torch.distributions.AbsTransform(cache_size=0)
result = t(x)
"""
)
obj.run(pytorch_code, ["result"])
47 changes: 47 additions & 0 deletions tests/test_distributions_AffineTransform.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# 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.distributions.AffineTransform")


def test_case_1():
pytorch_code = textwrap.dedent(
"""
import torch
x = torch.tensor(1.)
y = torch.tensor(2.)
affine = torch.distributions.AffineTransform(x, y)
result = affine.forward_shape([1, 2])
"""
)
obj.run(pytorch_code, ["result"])


def test_case_2():
pytorch_code = textwrap.dedent(
"""
import torch
x = torch.tensor(1.)
y = torch.tensor(2.)
affine = torch.distributions.AffineTransform(x, y, 1)
result = affine.forward_shape([1, 2])
"""
)
obj.run(pytorch_code, ["result"])
31 changes: 31 additions & 0 deletions tests/test_distributions_ExpTransform.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# 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.distributions.ExpTransform")


def test_case_1():
pytorch_code = textwrap.dedent(
"""
import torch
exp = torch.distributions.ExpTransform()
result = exp.forward_shape([1, 2])
"""
)
obj.run(pytorch_code, ["result"])
52 changes: 52 additions & 0 deletions tests/test_distributions_IndependentTransform.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# 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.distributions.IndependentTransform")


def test_case_1():
pytorch_code = textwrap.dedent(
"""
import torch
multi_exp = torch.distributions.IndependentTransform(torch.distributions.Transform(), 1)
result = multi_exp.forward_shape([1, 2])
"""
)
obj.run(pytorch_code, ["result"])


def test_case_2():
pytorch_code = textwrap.dedent(
"""
import torch
multi_exp = torch.distributions.IndependentTransform(torch.distributions.SoftmaxTransform(), 2)
result = multi_exp.forward_shape([1, 2])
"""
)
obj.run(pytorch_code, ["result"])


def test_case_3():
pytorch_code = textwrap.dedent(
"""
import torch
multi_exp = torch.distributions.IndependentTransform(torch.distributions.SoftmaxTransform(), 2)
result = multi_exp.forward_shape([1, 2, 3])
"""
)
obj.run(pytorch_code, ["result"])
31 changes: 31 additions & 0 deletions tests/test_distributions_PowerTransform.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# 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.distributions.PowerTransform")


def test_case_1():
pytorch_code = textwrap.dedent(
"""
import torch
power = torch.distributions.PowerTransform(torch.tensor(2.))
result = power.forward_shape([1, 2])
"""
)
obj.run(pytorch_code, ["result"])
31 changes: 31 additions & 0 deletions tests/test_distributions_ReshapeTransform.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# 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.distributions.ReshapeTransform")


def test_case_1():
pytorch_code = textwrap.dedent(
"""
import torch
reshape_transform = torch.distributions.ReshapeTransform((2, 3), (3, 2))
result = reshape_transform.forward_shape((1,2,3))
"""
)
obj.run(pytorch_code, ["result"])
43 changes: 43 additions & 0 deletions tests/test_distributions_TanhTransform.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# 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.distributions.TanhTransform")


def test_case_1():
pytorch_code = textwrap.dedent(
"""
import torch
x = torch.ones((2,3))
t = torch.distributions.TanhTransform()
result = t(x)
"""
)
obj.run(pytorch_code, ["result"])


def test_case_2():
pytorch_code = textwrap.dedent(
"""
import torch
x = torch.ones((2,3))
t = torch.distributions.TanhTransform(cache_size=0)
result = t(x)
"""
)
obj.run(pytorch_code, ["result"])

0 comments on commit 98a03ff

Please sign in to comment.