Skip to content

Commit d2a65a7

Browse files
committed
feat: support additional Tensor accuracy APIs
1 parent 42d714b commit d2a65a7

4 files changed

Lines changed: 90 additions & 3 deletions

File tree

tester/base.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,12 @@ def get_arg(api_config, arg_pos, arg_name, default=None):
406406
# differs materially from any public API.
407407
no_signature_api_mappings.update(
408408
{
409+
# copy_ 是内建方法,无法通过 inspect 获取签名。
410+
"paddle.Tensor.copy_": {
411+
"self": lambda cfg: get_arg(cfg, 0, "self"),
412+
"other": lambda cfg: get_arg(cfg, 1, "other"),
413+
"blocking": lambda cfg: get_arg(cfg, 2, "blocking", True),
414+
},
409415
# adamw_(param, grad, lr, moment1, moment2, moment2_max,
410416
# beta1_pow, beta2_pow, master_param, skip_update,
411417
# beta1, beta2, epsilon, lr_ratio, coeff, with_decay,

tester/base_config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ handle_axes_api:
173173

174174
forward_only_apis:
175175
- _run_custom_op
176+
- copy_
176177
- moe_permute
177178
- moe_unpermute
178179
- fp8_quant_blockwise

tester/paddle_to_torch/mapping.json

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5049,5 +5049,48 @@
50495049
},
50505050
"paddle._C_ops._run_custom_op": {
50515051
"Rule": "CopsRunCustomOpRule"
5052+
},
5053+
"paddle.Tensor.add_": {
5054+
"Rule": "CopsAdd_Rule"
5055+
},
5056+
"paddle.Tensor.contiguous": {
5057+
"torch_api": "torch.Tensor.contiguous"
5058+
},
5059+
"paddle.Tensor.copy_": {
5060+
"torch_api": "torch.Tensor.copy_",
5061+
"torch_args": [],
5062+
"torch_kwargs": {
5063+
"other": "other",
5064+
"non_blocking": "not blocking"
5065+
}
5066+
},
5067+
"paddle.Tensor.flatten_": {
5068+
"Rule": "CopsFlatten_Rule"
5069+
},
5070+
"paddle.Tensor.multiply_": {
5071+
"Rule": "CopsMultiply_Rule"
5072+
},
5073+
"paddle.Tensor.put_along_axis_": {
5074+
"Rule": "CopsPutAlongAxis_Rule"
5075+
},
5076+
"paddle.Tensor.scale_": {
5077+
"Rule": "CopsScale_Rule"
5078+
},
5079+
"paddle.Tensor.subtract_": {
5080+
"Rule": "CopsSubtract_Rule"
5081+
},
5082+
"paddle.Tensor.to": {
5083+
"Rule": "TensorToRule"
5084+
},
5085+
"paddle.randint": {
5086+
"torch_api": "torch.randint",
5087+
"torch_args": [
5088+
"low",
5089+
"high",
5090+
"tuple(shape) if isinstance(shape, list) else shape"
5091+
],
5092+
"torch_kwargs": {
5093+
"dtype": "locals().get(\"dtype\") if locals().get(\"dtype\") is not None else torch.int64"
5094+
}
50525095
}
50535096
}

tester/paddle_to_torch/rules.py

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7400,8 +7400,9 @@ def apply(self, paddle_api: str) -> ConvertResult:
74007400
core = """
74017401
x = locals().get("x")
74027402
y = locals().get("y")
7403+
alpha = locals().get("alpha", 1)
74037404
with torch.no_grad():
7404-
x.sub_(y)
7405+
x.sub_(y.to(x.dtype), alpha=alpha)
74057406
result = x
74067407
"""
74077408
code = Code(core=core.splitlines())
@@ -7421,8 +7422,9 @@ def apply(self, paddle_api: str) -> ConvertResult:
74217422
core = """
74227423
x = locals().get("x")
74237424
y = locals().get("y")
7425+
alpha = locals().get("alpha", 1)
74247426
with torch.no_grad():
7425-
x.add_(y.to(x.dtype))
7427+
x.add_(y.to(x.dtype), alpha=alpha)
74267428
result = x
74277429
"""
74287430
code = Code(core=core.splitlines())
@@ -7513,7 +7515,7 @@ class CopsPutAlongAxis_Rule(BaseRule):
75137515

75147516
def apply(self, paddle_api: str) -> ConvertResult:
75157517
core = """
7516-
arr = locals().get("arr")
7518+
arr = locals().get("arr", locals().get("x"))
75177519
indices = locals().get("indices")
75187520
values = locals().get("values")
75197521
axis = locals().get("axis")
@@ -7606,6 +7608,41 @@ def apply(self, paddle_api: str) -> ConvertResult:
76067608
return ConvertResult.success(paddle_api, code, is_torch_corresponding=False)
76077609

76087610

7611+
class TensorToRule(BaseRule):
7612+
"""Translate Paddle Tensor.to device and dtype values for Torch."""
7613+
7614+
def apply(self, paddle_api: str) -> ConvertResult:
7615+
core = """
7616+
tensor = locals().get("self")
7617+
to_args = list(locals().get("args", ()))
7618+
to_kwargs = dict(locals().get("kwargs", {}))
7619+
to_kwargs.pop("self", None)
7620+
to_kwargs.pop("args", None)
7621+
dtype_names = {
7622+
"bool", "float16", "float32", "float64", "bfloat16",
7623+
"int8", "int16", "int32", "int64", "uint8", "complex64", "complex128",
7624+
}
7625+
7626+
def _translate_to_value(value, dtype_names=dtype_names):
7627+
if isinstance(value, str) and value.startswith("gpu"):
7628+
return "cuda" + value[3:]
7629+
dtype_name = str(value).split(".")[-1]
7630+
if dtype_name in dtype_names:
7631+
return getattr(torch, dtype_name)
7632+
return value
7633+
7634+
if to_args:
7635+
to_args[0] = _translate_to_value(to_args[0])
7636+
if "device" in to_kwargs:
7637+
to_kwargs["device"] = _translate_to_value(to_kwargs["device"])
7638+
if "dtype" in to_kwargs:
7639+
to_kwargs["dtype"] = _translate_to_value(to_kwargs["dtype"])
7640+
result = tensor.to(*to_args, **to_kwargs) if to_args or to_kwargs else tensor
7641+
"""
7642+
code = Code(core=core.splitlines())
7643+
return ConvertResult.success(paddle_api, code)
7644+
7645+
76097646
class CopsTransposeRule(BaseRule):
76107647
"""paddle._C_ops.transpose(x, perm) → torch.permute(x, dims)
76117648

0 commit comments

Comments
 (0)