Skip to content

Commit

Permalink
转换规则 No.228/229 (#248)
Browse files Browse the repository at this point in the history
* Add tests
  • Loading branch information
co63oc authored Aug 28, 2023
1 parent e50ee48 commit 7c8b5d6
Show file tree
Hide file tree
Showing 3 changed files with 203 additions and 0 deletions.
27 changes: 27 additions & 0 deletions paconvert/api_mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -10427,6 +10427,33 @@
"stable": ""
}
},
"torch.sparse.addmm": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.sparse.addmm",
"args_list": [
"input",
"mat1",
"mat2",
"alpha",
"beta"
],
"kwargs_change": {
"mat1": "x",
"mat2": "y"
}
},
"torch.sparse.mm": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.sparse.matmul",
"args_list": [
"sparse",
"dense"
],
"kwargs_change": {
"sparse": "x",
"dense": "y"
}
},
"torch.sparse.sum": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.sparse.sum",
Expand Down
109 changes: 109 additions & 0 deletions tests/test_sparse_addmm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# 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.sparse.addmm")


def test_case_1():
pytorch_code = textwrap.dedent(
"""
import torch
x = torch.tensor([[1., 2, 3], [3, 4, 5], [3, 4, 5]])
i = torch.tensor([[0, 1, 2],
[1, 0, 1]])
v = torch.tensor([3, 4, 5], dtype=torch.float32)
mat1 = torch.sparse_coo_tensor(i, v, [3, 3])
mat2 = torch.tensor([[1., 2, 3], [3, 4, 5], [3, 4, 5]])
result = None
if torch.cuda.is_available():
result = torch.sparse.addmm(x, mat1, mat2)
"""
)
obj.run(pytorch_code, ["result"])


def test_case_2():
pytorch_code = textwrap.dedent(
"""
import torch
x = torch.tensor([[1., 2, 3], [3, 4, 5], [3, 4, 5]])
i = torch.tensor([[0, 1, 2],
[1, 0, 1]])
v = torch.tensor([3, 4, 5], dtype=torch.float32)
mat1 = torch.sparse_coo_tensor(i, v, [3, 3])
mat2 = torch.tensor([[1., 2, 3], [3, 4, 5], [3, 4, 5]])
result = None
if torch.cuda.is_available():
result = torch.sparse.addmm(input=x, mat1=mat1, mat2=mat2, beta=0.6, alpha=0.7)
"""
)
obj.run(pytorch_code, ["result"])


def test_case_3():
pytorch_code = textwrap.dedent(
"""
import torch
x = torch.tensor([[1., 2, 3], [3, 4, 5], [3, 4, 5]])
i = torch.tensor([[0, 1, 2],
[1, 0, 1]])
v = torch.tensor([3, 4, 5], dtype=torch.float32)
mat1 = torch.sparse_coo_tensor(i, v, [3, 3])
mat2 = torch.tensor([[1., 2, 3], [3, 4, 5], [3, 4, 5]])
result = None
if torch.cuda.is_available():
result = torch.sparse.addmm(beta=0.6, alpha=0.7, input=x, mat1=mat1, mat2=mat2)
"""
)
obj.run(pytorch_code, ["result"])


def test_case_4():
pytorch_code = textwrap.dedent(
"""
import torch
x = torch.tensor([[1., 2, 3], [3, 4, 5], [3, 4, 5]])
i = torch.tensor([[0, 1, 2],
[1, 0, 1]])
v = torch.tensor([3, 4, 5], dtype=torch.float32)
mat1 = torch.sparse_coo_tensor(i, v, [3, 3])
mat2 = torch.tensor([[1., 2, 3], [3, 4, 5], [3, 4, 5]])
result = None
if torch.cuda.is_available():
result = torch.sparse.addmm(x, mat1, mat2, beta=0.6, alpha=0.7)
"""
)
obj.run(pytorch_code, ["result"])


def test_case_5():
pytorch_code = textwrap.dedent(
"""
import torch
x = torch.tensor([[1., 2, 3], [3, 4, 5], [3, 4, 5]])
i = torch.tensor([[0, 1, 2],
[1, 0, 1]])
v = torch.tensor([3, 4, 5], dtype=torch.float32)
mat1 = torch.sparse_coo_tensor(i, v, [3, 3])
mat2 = torch.tensor([[1., 2, 3], [3, 4, 5], [3, 4, 5]])
result = None
if torch.cuda.is_available():
result = torch.sparse.addmm(input=x, mat1=mat1, mat2=mat2, beta=0.6, alpha=0.7)
"""
)
obj.run(pytorch_code, ["result"])
67 changes: 67 additions & 0 deletions tests/test_sparse_mm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# 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.sparse.mm")


def test_case_1():
pytorch_code = textwrap.dedent(
"""
import torch
indices = [[0, 1, 2], [1, 2, 0]]
values = [1., 2., 3.]
x = torch.sparse_coo_tensor(indices, values, [3, 3])
dense = torch.ones([3, 2])
result = None
if torch.cuda.is_available():
result = torch.sparse.mm(x, dense)
"""
)
obj.run(pytorch_code, ["result"])


def test_case_2():
pytorch_code = textwrap.dedent(
"""
import torch
indices = [[0, 1, 2], [1, 2, 0]]
values = [1., 2., 3.]
x = torch.sparse_coo_tensor(indices, values, [3, 3])
dense = torch.ones([3, 2])
result = None
if torch.cuda.is_available():
result = torch.sparse.mm(sparse=x, dense=dense)
"""
)
obj.run(pytorch_code, ["result"])


def test_case_3():
pytorch_code = textwrap.dedent(
"""
import torch
indices = [[0, 1, 2], [1, 2, 0]]
values = [1., 2., 3.]
x = torch.sparse_coo_tensor(indices, values, [3, 3])
dense = torch.ones([3, 2])
result = None
if torch.cuda.is_available():
result = torch.sparse.mm(dense=dense, sparse=x)
"""
)
obj.run(pytorch_code, ["result"])

0 comments on commit 7c8b5d6

Please sign in to comment.