Skip to content

Commit 9d0279a

Browse files
authored
[API Compatibility] update and enhanced all apis that can be aligned to use ChangePrefixMatcher Edit by AI Agent (#900)
* update all apis that can be changed to * code consistency fix
1 parent 504c572 commit 9d0279a

18 files changed

Lines changed: 669 additions & 160 deletions

paconvert/api_mapping.json

Lines changed: 15 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -514,13 +514,7 @@
514514
"Matcher": "ChangePrefixMatcher"
515515
},
516516
"torch.Tensor.aminmax": {
517-
"Matcher": "AMinMaxMatcher",
518-
"min_input_args": 0,
519-
"args_list": [
520-
"*",
521-
"dim",
522-
"keepdim"
523-
]
517+
"Matcher": "ChangePrefixMatcher"
524518
},
525519
"torch.Tensor.angle": {
526520
"Matcher": "ChangePrefixMatcher"
@@ -1236,13 +1230,7 @@
12361230
},
12371231
"torch.Tensor.index_copy": {},
12381232
"torch.Tensor.index_copy_": {
1239-
"Matcher": "TensorIndexCopyMatcher",
1240-
"min_input_args": 3,
1241-
"args_list": [
1242-
"dim",
1243-
"index",
1244-
"source"
1245-
]
1233+
"Matcher": "ChangePrefixMatcher"
12461234
},
12471235
"torch.Tensor.index_fill": {
12481236
"Matcher": "ChangePrefixMatcher"
@@ -1906,11 +1894,7 @@
19061894
"Matcher": "ChangePrefixMatcher"
19071895
},
19081896
"torch.Tensor.reshape_as": {
1909-
"Matcher": "TensorReshape_asMatcher",
1910-
"min_input_args": 1,
1911-
"args_list": [
1912-
"other"
1913-
]
1897+
"Matcher": "ChangePrefixMatcher"
19141898
},
19151899
"torch.Tensor.resize_": {
19161900
"Matcher": "ChangePrefixMatcher"
@@ -2245,13 +2229,7 @@
22452229
"Matcher": "ChangePrefixMatcher"
22462230
},
22472231
"torch.Tensor.transpose_": {
2248-
"Matcher": "TensorTransposeMatcher",
2249-
"paddle_api": "paddle.Tensor.transpose_",
2250-
"min_input_args": 2,
2251-
"args_list": [
2252-
"dim0",
2253-
"dim1"
2254-
]
2232+
"Matcher": "ChangePrefixMatcher"
22552233
},
22562234
"torch.Tensor.triangular_solve": {
22572235
"Matcher": "TensorTriangularSolveMatcher",
@@ -2322,19 +2300,7 @@
23222300
"Matcher": "ChangePrefixMatcher"
23232301
},
23242302
"torch.Tensor.unique": {
2325-
"Matcher": "GenericMatcher",
2326-
"paddle_api": "paddle.Tensor.unique",
2327-
"min_input_args": 0,
2328-
"args_list": [
2329-
"sorted",
2330-
"return_inverse",
2331-
"return_counts",
2332-
"dim"
2333-
],
2334-
"kwargs_change": {
2335-
"sorted": "",
2336-
"dim": "axis"
2337-
}
2303+
"Matcher": "ChangePrefixMatcher"
23382304
},
23392305
"torch.Tensor.unique_consecutive": {
23402306
"Matcher": "ChangePrefixMatcher"
@@ -4444,17 +4410,7 @@
44444410
}
44454411
},
44464412
"torch.distributions.distribution.Distribution": {
4447-
"Matcher": "GenericMatcher",
4448-
"paddle_api": "paddle.distribution.Distribution",
4449-
"min_input_args": 0,
4450-
"args_list": [
4451-
"batch_shape",
4452-
"event_shape",
4453-
"validate_args"
4454-
],
4455-
"kwargs_change": {
4456-
"validate_args": ""
4457-
}
4413+
"Matcher": "ChangePrefixMatcher"
44584414
},
44594415
"torch.distributions.distribution.Distribution.cdf": {
44604416
"min_input_args": 1
@@ -4663,18 +4619,7 @@
46634619
]
46644620
},
46654621
"torch.distributions.multivariate_normal.MultivariateNormal": {
4666-
"Matcher": "GenericMatcher",
4667-
"paddle_api": "paddle.distribution.MultivariateNormal",
4668-
"args_list": [
4669-
"loc",
4670-
"covariance_matrix",
4671-
"precision_matrix",
4672-
"scale_tril",
4673-
"validate_args"
4674-
],
4675-
"kwargs_change": {
4676-
"validate_args": ""
4677-
}
4622+
"Matcher": "ChangePrefixMatcher"
46784623
},
46794624
"torch.distributions.normal.Normal": {
46804625
"Matcher": "GenericMatcher",
@@ -5450,11 +5395,7 @@
54505395
"Matcher": "ChangePrefixMatcher"
54515396
},
54525397
"torch.inference_mode": {
5453-
"Matcher": "InferenceModeMatcher",
5454-
"min_input_args": 0,
5455-
"args_list": [
5456-
"mode"
5457-
]
5398+
"Matcher": "ChangePrefixMatcher"
54585399
},
54595400
"torch.initial_seed": {
54605401
"Matcher": "ChangePrefixMatcher"
@@ -6748,12 +6689,7 @@
67486689
]
67496690
},
67506691
"torch.nn.Hardswish": {
6751-
"Matcher": "GenericMatcher",
6752-
"paddle_api": "paddle.nn.Hardswish",
6753-
"min_input_args": 0,
6754-
"args_list": [
6755-
"inplace"
6756-
]
6692+
"Matcher": "ChangePrefixMatcher"
67576693
},
67586694
"torch.nn.Hardtanh": {
67596695
"Matcher": "GenericMatcher",
@@ -7268,12 +7204,7 @@
72687204
"Matcher": "ChangePrefixMatcher"
72697205
},
72707206
"torch.nn.ReLU6": {
7271-
"Matcher": "GenericMatcher",
7272-
"paddle_api": "paddle.nn.ReLU6",
7273-
"min_input_args": 0,
7274-
"args_list": [
7275-
"inplace"
7276-
]
7207+
"Matcher": "ChangePrefixMatcher"
72777208
},
72787209
"torch.nn.ReflectionPad1d": {
72797210
"Matcher": "ChangePrefixMatcher"
@@ -9178,15 +9109,7 @@
91789109
"Matcher": "ChangePrefixMatcher"
91799110
},
91809111
"torch.optim.lr_scheduler.LambdaLR": {
9181-
"Matcher": "Optim2LrSchedulerMatcher",
9182-
"paddle_api": "paddle.optimizer.lr.LambdaDecay",
9183-
"min_input_args": 2,
9184-
"args_list": [
9185-
"optimizer",
9186-
"lr_lambda",
9187-
"last_epoch",
9188-
"verbose"
9189-
]
9112+
"Matcher": "ChangePrefixMatcher"
91909113
},
91919114
"torch.optim.lr_scheduler.LinearLR": {
91929115
"Matcher": "Optim2LrSchedulerMatcher",
@@ -9407,15 +9330,7 @@
94079330
"Matcher": "ChangePrefixMatcher"
94089331
},
94099332
"torch.relu": {
9410-
"Matcher": "GenericMatcher",
9411-
"paddle_api": "paddle.nn.functional.relu",
9412-
"min_input_args": 1,
9413-
"args_list": [
9414-
"input"
9415-
],
9416-
"kwargs_change": {
9417-
"input": "x"
9418-
}
9333+
"Matcher": "ChangePrefixMatcher"
94199334
},
94209335
"torch.remainder": {
94219336
"Matcher": "ChangePrefixMatcher"
@@ -9999,17 +9914,7 @@
99999914
},
100009915
"torch.special.entr": {},
100019916
"torch.special.erf": {
10002-
"Matcher": "GenericMatcher",
10003-
"paddle_api": "paddle.erf",
10004-
"min_input_args": 1,
10005-
"args_list": [
10006-
"input",
10007-
"*",
10008-
"out"
10009-
],
10010-
"kwargs_change": {
10011-
"input": "x"
10012-
}
9917+
"Matcher": "ChangePrefixMatcher"
100139918
},
100149919
"torch.special.erfc": {
100159920
"Matcher": "ErfCMatcher",
@@ -10207,17 +10112,7 @@
1020710112
"torch.special.scaled_modified_bessel_k0": {},
1020810113
"torch.special.scaled_modified_bessel_k1": {},
1020910114
"torch.special.sinc": {
10210-
"Matcher": "GenericMatcher",
10211-
"paddle_api": "paddle.sinc",
10212-
"min_input_args": 1,
10213-
"args_list": [
10214-
"input",
10215-
"*",
10216-
"out"
10217-
],
10218-
"kwargs_change": {
10219-
"input": "x"
10220-
}
10115+
"Matcher": "ChangePrefixMatcher"
1022110116
},
1022210117
"torch.special.softmax": {
1022310118
"Matcher": "ChangePrefixMatcher"
@@ -10672,24 +10567,7 @@
1067210567
"Matcher": "ChangePrefixMatcher"
1067310568
},
1067410569
"torch.utils.data.distributed.DistributedSampler": {
10675-
"Matcher": "GenericMatcher",
10676-
"paddle_api": "paddle.io.DistributedBatchSampler",
10677-
"min_input_args": 1,
10678-
"args_list": [
10679-
"dataset",
10680-
"num_replicas",
10681-
"rank",
10682-
"shuffle",
10683-
"seed",
10684-
"drop_last"
10685-
],
10686-
"kwargs_change": {
10687-
"seed": ""
10688-
},
10689-
"paddle_default_kwargs": {
10690-
"shuffle": "True",
10691-
"batch_size": "1"
10692-
}
10570+
"Matcher": "ChangePrefixMatcher"
1069310571
},
1069410572
"torch.utils.data.get_worker_info": {
1069510573
"Matcher": "ChangePrefixMatcher"

tests/code_library/code_case/paddle_code/api_paddle_index_copy_.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,5 @@
1-
import numpy as np
21
import paddle
32

4-
############################## 相关utils函数,如下 ##############################
5-
6-
def _Tensor_index_copy_(self, dim, index, source):
7-
if dim == 0:
8-
return self.scatter_(index, source)
9-
10-
shape = self.shape
11-
12-
new_index = []
13-
for i in range(0, np.prod(shape[:dim])):
14-
new_index.append(index + i * len(index))
15-
new_index = paddle.concat(new_index)
16-
new_self = self.reshape_([-1] + shape[dim+1:])
17-
new_source = source.reshape([-1] + shape[dim+1:])
18-
19-
return new_self.scatter_(new_index, new_source).reshape_(shape)
20-
21-
setattr(paddle.Tensor, "index_copy_", _Tensor_index_copy_)
22-
############################## 相关utils函数,如上 ##############################
23-
24-
253
print("#########################case1#########################")
264
x = paddle.zeros(5, 3)
275
t = paddle.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=paddle.float)

tests/test_Tensor_aminmax.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,55 @@ def test_case_5():
7272
"""
7373
)
7474
obj.run(pytorch_code, ["result"])
75+
76+
77+
def test_case_6():
78+
# integer tensor support
79+
pytorch_code = textwrap.dedent(
80+
"""
81+
import torch
82+
t = torch.tensor([[-12, 23, 34], [-45, 56, -67], [78, -89, 100]], dtype=torch.int64)
83+
result = t.aminmax()
84+
"""
85+
)
86+
obj.run(pytorch_code, ["result"])
87+
88+
89+
def test_case_7():
90+
# three-dimensional tensor along middle axis
91+
pytorch_code = textwrap.dedent(
92+
"""
93+
import torch
94+
t = torch.tensor([
95+
[[1., 2., 3.], [9., 8., 7.]],
96+
[[4., 5., 6.], [10., 11., 12.]]], dtype=torch.float32)
97+
result = t.aminmax(dim=1, keepdim=True)
98+
"""
99+
)
100+
obj.run(pytorch_code, ["result"])
101+
102+
103+
def test_case_8():
104+
# mix of positional and keyword args
105+
pytorch_code = textwrap.dedent(
106+
"""
107+
import torch
108+
t = torch.tensor([[1.5, 2.5, 3.5]], dtype=torch.float64)
109+
result = t.aminmax(dim=-1, keepdim=False)
110+
"""
111+
)
112+
obj.run(pytorch_code, ["result"])
113+
114+
115+
def test_case_9():
116+
# expression argument passed as keyword args
117+
pytorch_code = textwrap.dedent(
118+
"""
119+
import torch
120+
t = torch.tensor([-3.0, -1.0, 0.0, 2.0])
121+
d = 1 - 1
122+
k = False or True
123+
result = t.aminmax(dim=d, keepdim=k)
124+
"""
125+
)
126+
obj.run(pytorch_code, ["result"])

tests/test_Tensor_index_copy_.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,45 @@ def test_case_7():
118118
"""
119119
)
120120
obj.run(pytorch_code, ["result"])
121+
122+
123+
def test_case_8():
124+
# integer source tensor with int64 target
125+
pytorch_code = textwrap.dedent(
126+
"""
127+
import torch
128+
x = torch.zeros(6, 2, dtype=torch.int64)
129+
t = torch.tensor([[10, 20], [30, 40], [50, 60]], dtype=torch.int64)
130+
index = torch.tensor([0, 2, 5])
131+
result = x.index_copy_(0, index, t)
132+
"""
133+
)
134+
obj.run(pytorch_code, ["result"])
135+
136+
137+
def test_case_9():
138+
# float64 along last dimension of a 2D tensor
139+
pytorch_code = textwrap.dedent(
140+
"""
141+
import torch
142+
x = torch.zeros(2, 4, dtype=torch.float64)
143+
t = torch.tensor([[1.1, 9.9], [2.2, 8.8]], dtype=torch.float64)
144+
idx = torch.tensor([0, 3])
145+
result = x.index_copy_(dim=1, index=idx, source=t)
146+
"""
147+
)
148+
obj.run(pytorch_code, ["result"])
149+
150+
151+
def test_case_10():
152+
# keyword-only call (no positional args) using default-name keywords reordered
153+
pytorch_code = textwrap.dedent(
154+
"""
155+
import torch
156+
x = torch.ones(4, 3) * -1.0
157+
t = torch.arange(6).reshape(2, 3).to(torch.float32)
158+
i = torch.tensor([0, 3])
159+
result = x.index_copy_(source=t, index=i, dim=0)
160+
"""
161+
)
162+
obj.run(pytorch_code, ["result"])

0 commit comments

Comments
 (0)