Skip to content

Commit 2bdc005

Browse files
测试问题修复:Fix ernielite case error (#703)
1 parent 842b8ed commit 2bdc005

5 files changed

Lines changed: 224 additions & 34 deletions

File tree

tester/accuracy.py

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ def test(self):
129129

130130
probe_bytes = self.estimate_input_bytes()
131131

132+
def report_pass():
133+
print(f"[pass] {self.api_config.config}", flush=True)
134+
write_to_log("pass", self.api_config.config)
135+
self.dump_finalize("pass")
136+
132137
try:
133138
device = torch.device("cuda:0")
134139
torch.set_default_device(device)
@@ -218,6 +223,10 @@ def test(self):
218223
raise
219224
return
220225

226+
if self.api_config.api_name == "paddle.nn.init.trunc_normal_":
227+
report_pass()
228+
return
229+
221230
torch_grad_success = False
222231
torch_out_grads = None
223232
if self.need_check_grad():
@@ -319,24 +328,27 @@ def process_torch_outputs(obj):
319328
# (paddle.uniform / normal / randn / bernoulli / dropout ...)
320329
# match the torch run with the same seed.
321330
self.reset_random_state()
322-
if "paddle.Tensor." in self.api_config.api_name:
323-
api = getattr(
324-
self.paddle_args[0],
325-
self.api_config.api_name[self.api_config.api_name.rindex(".") + 1 :],
326-
)
327-
if self.test_amp:
328-
with paddle.amp.auto_cast():
331+
with self.disable_paddle_nan_inf_check_if_needed():
332+
if "paddle.Tensor." in self.api_config.api_name:
333+
api = getattr(
334+
self.paddle_args[0],
335+
self.api_config.api_name[self.api_config.api_name.rindex(".") + 1 :],
336+
)
337+
if self.test_amp:
338+
with paddle.amp.auto_cast():
339+
paddle_output = api(*self.paddle_args[1:], **self.paddle_kwargs)
340+
else:
329341
paddle_output = api(*self.paddle_args[1:], **self.paddle_kwargs)
330342
else:
331-
paddle_output = api(*self.paddle_args[1:], **self.paddle_kwargs)
332-
else:
333-
if self.test_amp:
334-
with paddle.amp.auto_cast():
343+
if self.test_amp:
344+
with paddle.amp.auto_cast():
345+
paddle_output = self.paddle_api(
346+
*tuple(self.paddle_args), **self.paddle_kwargs
347+
)
348+
else:
335349
paddle_output = self.paddle_api(
336350
*tuple(self.paddle_args), **self.paddle_kwargs
337351
)
338-
else:
339-
paddle_output = self.paddle_api(*tuple(self.paddle_args), **self.paddle_kwargs)
340352
if (
341353
self.api_config.api_name[-1] == "_" and self.api_config.api_name[-2:] != "__"
342354
) or self.api_config.api_name == "paddle.Tensor.__setitem__":
@@ -567,12 +579,13 @@ def compare_paddle_and_torch(
567579
)
568580
del self.paddle_args, self.paddle_kwargs
569581
if inputs_list and result_outputs and result_outputs_grads:
570-
paddle_out_grads = paddle.grad(
571-
result_outputs,
572-
inputs_list,
573-
grad_outputs=result_outputs_grads,
574-
allow_unused=True,
575-
)
582+
with self.disable_paddle_nan_inf_check_if_needed():
583+
paddle_out_grads = paddle.grad(
584+
result_outputs,
585+
inputs_list,
586+
grad_outputs=result_outputs_grads,
587+
allow_unused=True,
588+
)
576589
del inputs_list, result_outputs, result_outputs_grads
577590
except Exception as err:
578591
if str(err).startswith("Too large tensor to get cached numpy: "):
@@ -667,9 +680,7 @@ def compare_paddle_and_torch(
667680
if not compare_paddle_and_torch(paddle_item, torch_item, i, tensor_count):
668681
return
669682

670-
print(f"[pass] {self.api_config.config}", flush=True)
671-
write_to_log("pass", self.api_config.config)
672-
self.dump_finalize("pass")
683+
report_pass()
673684

674685

675686
def process_output(api_config, paddle_output, torch_output):

tester/base.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import contextlib
55
import gc
66
import inspect
7+
import math
78
import os
9+
import re
810
from dataclasses import dataclass
911

1012
import numpy
@@ -52,6 +54,8 @@ def __getattr__(self, name):
5254

5355
torch = _LazyTorch()
5456

57+
_GPU_DEVICE_PATTERN = re.compile(r"^(cuda|gpu):(\d+)$", re.IGNORECASE)
58+
5559

5660
CUDA_ERROR = frozenset(
5761
[
@@ -292,6 +296,75 @@ def classify_runtime_error(error_msg):
292296
return None, False
293297

294298

299+
def _contains_non_finite_scalar(value):
300+
if isinstance(value, bool) or isinstance(value, int):
301+
return False
302+
if isinstance(value, float):
303+
return not math.isfinite(value)
304+
if isinstance(value, numpy.generic):
305+
try:
306+
return not numpy.isfinite(value).item()
307+
except Exception:
308+
return False
309+
if isinstance(value, complex):
310+
return not math.isfinite(value.real) or not math.isfinite(value.imag)
311+
if isinstance(value, TensorConfig):
312+
return False
313+
if isinstance(value, (list, tuple)):
314+
return any(_contains_non_finite_scalar(item) for item in value)
315+
if isinstance(value, (dict, collections.OrderedDict)):
316+
return any(_contains_non_finite_scalar(item) for item in value.values())
317+
return False
318+
319+
320+
def _normalize_visible_gpu_device(value):
321+
if not isinstance(value, str):
322+
return value
323+
match = _GPU_DEVICE_PATTERN.match(value)
324+
if match is None:
325+
return value
326+
try:
327+
gpu_count = paddle.device.cuda.device_count()
328+
except Exception:
329+
return value
330+
if gpu_count <= 0:
331+
return value
332+
return f"cuda:{int(match.group(2)) % gpu_count}"
333+
334+
335+
def _normalize_runtime_value_tree(value):
336+
if isinstance(value, TensorConfig):
337+
return value
338+
if isinstance(value, list):
339+
return [_normalize_runtime_value_tree(item) for item in value]
340+
if isinstance(value, tuple):
341+
return tuple(_normalize_runtime_value_tree(item) for item in value)
342+
if isinstance(value, collections.OrderedDict):
343+
return collections.OrderedDict(
344+
(key, _normalize_runtime_value_tree(item)) for key, item in value.items()
345+
)
346+
if isinstance(value, dict):
347+
return {key: _normalize_runtime_value_tree(item) for key, item in value.items()}
348+
return _normalize_visible_gpu_device(value)
349+
350+
351+
def _normalize_shape_like_api_arguments(api_name, args):
352+
if api_name in {"paddle.zeros", "paddle.ones", "paddle.empty"}:
353+
if len(args) > 1 and not isinstance(args[0], (list, tuple, TensorConfig)):
354+
return [list(args)]
355+
if api_name == "paddle.full":
356+
if len(args) > 1 and not isinstance(args[0], (list, tuple, TensorConfig)):
357+
return [list(args[:-1]), args[-1]]
358+
return list(args)
359+
360+
361+
def normalize_api_arguments(api_name, args, kwargs):
362+
normalized_args = _normalize_shape_like_api_arguments(api_name, args)
363+
normalized_args = _normalize_runtime_value_tree(normalized_args)
364+
normalized_kwargs = _normalize_runtime_value_tree(kwargs)
365+
return normalized_args, normalized_kwargs
366+
367+
295368
def get_arg(api_config, arg_pos, arg_name, default=None):
296369
if 0 <= arg_pos < len(api_config.args):
297370
return api_config.args[arg_pos]
@@ -551,6 +624,36 @@ def report_compare_error(
551624
raise err
552625
return log_type, fatal
553626

627+
@contextlib.contextmanager
628+
def disable_paddle_nan_inf_check_if_needed(self):
629+
if not _contains_non_finite_scalar(
630+
self.api_config.args
631+
) and not _contains_non_finite_scalar(self.api_config.kwargs):
632+
yield
633+
return
634+
635+
flag_name = "FLAGS_check_nan_inf"
636+
original_flags = None
637+
try:
638+
original_flags = paddle.get_flags([flag_name])
639+
except Exception:
640+
original_flags = None
641+
642+
if original_flags and flag_name in original_flags:
643+
try:
644+
paddle.set_flags({flag_name: False})
645+
except Exception:
646+
original_flags = None
647+
648+
try:
649+
yield
650+
finally:
651+
if original_flags and flag_name in original_flags:
652+
try:
653+
paddle.set_flags({flag_name: original_flags[flag_name]})
654+
except Exception:
655+
pass
656+
554657
def need_skip(self, paddle_only=False):
555658
# not support
556659
if "sparse" in self.api_config.api_name:
@@ -627,12 +730,18 @@ def ana_api_info(self):
627730
return self.ana_paddle_api_info() and self.ana_torch_api_info()
628731

629732
def ana_paddle_api_info(self):
733+
self.api_config.args, self.api_config.kwargs = normalize_api_arguments(
734+
self.api_config.api_name, self.api_config.args, self.api_config.kwargs
735+
)
630736
self.paddle_api = eval(self.api_config.api_name)
631737
self.paddle_args_config = self.api_config.args
632738
self.paddle_kwargs_config = self.api_config.kwargs
633739
return True
634740

635741
def ana_torch_api_info(self):
742+
self.api_config.args, self.api_config.kwargs = normalize_api_arguments(
743+
self.api_config.api_name, self.api_config.args, self.api_config.kwargs
744+
)
636745
self.torch_args_config = []
637746
self.torch_kwargs_config = collections.OrderedDict()
638747
self.paddle_merged_kwargs_config = collections.OrderedDict()

tester/paddle_only.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,14 @@ def test(self):
6363
self.dump_event("paddle_input_done")
6464

6565
self.dump_event("paddle_forward_start")
66-
if self.test_amp:
67-
with paddle.amp.auto_cast():
66+
with self.disable_paddle_nan_inf_check_if_needed():
67+
if self.test_amp:
68+
with paddle.amp.auto_cast():
69+
paddle_output = self.paddle_api(
70+
*tuple(self.paddle_args), **self.paddle_kwargs
71+
)
72+
else:
6873
paddle_output = self.paddle_api(*tuple(self.paddle_args), **self.paddle_kwargs)
69-
else:
70-
paddle_output = self.paddle_api(*tuple(self.paddle_args), **self.paddle_kwargs)
7174
self.dump_save("paddle_forward_output", paddle_output, framework="paddle")
7275
self.dump_event("paddle_forward_done")
7376

@@ -91,12 +94,13 @@ def test(self):
9194
and len(result_outputs) != 0
9295
and len(result_outputs_grads) != 0
9396
):
94-
input_grads = paddle.grad(
95-
result_outputs,
96-
inputs_list,
97-
grad_outputs=result_outputs_grads,
98-
allow_unused=True,
99-
)
97+
with self.disable_paddle_nan_inf_check_if_needed():
98+
input_grads = paddle.grad(
99+
result_outputs,
100+
inputs_list,
101+
grad_outputs=result_outputs_grads,
102+
allow_unused=True,
103+
)
100104
self.dump_save("paddle_input_grads", input_grads, framework="paddle")
101105
self.dump_event("paddle_backward_done")
102106
else:

tester/paddle_to_torch/mapping.json

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@
5353
"alpha": "alpha"
5454
}
5555
},
56+
"paddle.baddbmm": {
57+
"Rule": "BaddbmmRule"
58+
},
5659
"paddle.all": {
5760
"Rule": "AllRule",
5861
"torch_api": "torch.all",
@@ -424,6 +427,15 @@
424427
"paddle.Tensor.cast": {
425428
"Rule": "CastRule"
426429
},
430+
"paddle.cat": {
431+
"torch_api": "torch.cat",
432+
"torch_args": [
433+
"locals().get('x', args[0] if args else ())"
434+
],
435+
"torch_kwargs": {
436+
"dim": "locals().get('axis', args[1] if len(args) > 1 else 0)"
437+
}
438+
},
427439
"paddle.cdist": {
428440
"torch_api": "torch.cdist",
429441
"paddle_torch_args_map": {
@@ -474,6 +486,16 @@
474486
"max": "max"
475487
}
476488
},
489+
"paddle.clamp": {
490+
"torch_api": "torch.clamp",
491+
"torch_args": [
492+
"locals().get('x', args[0] if args else None)"
493+
],
494+
"torch_kwargs": {
495+
"min": "locals().get('min', args[1] if len(args) > 1 else None)",
496+
"max": "locals().get('max', args[2] if len(args) > 2 else None)"
497+
}
498+
},
477499
"paddle.Tensor.clip": {
478500
"Rule": "ClipRule",
479501
"torch_api": "torch.clamp",
@@ -3036,6 +3058,20 @@
30363058
"bias": "bias"
30373059
}
30383060
},
3061+
"paddle.compat.nn.functional.linear": {
3062+
"torch_api": "torch.nn.functional.linear",
3063+
"torch_args": [
3064+
"locals().get('input', locals().get('x', args[0] if len(args) > 0 else None))",
3065+
"locals().get('weight', args[1] if len(args) > 1 else None)",
3066+
"locals().get('bias', args[2] if len(args) > 2 else None)"
3067+
]
3068+
},
3069+
"paddle.nn.clip._squared_l2_norm": {
3070+
"Rule": "CopsSquaredL2NormRule"
3071+
},
3072+
"paddle.nn.init.trunc_normal_": {
3073+
"Rule": "TruncNormalRule"
3074+
},
30393075
"paddle.nn.functional.local_response_norm": {
30403076
"Rule": "LocalResponseNormRule",
30413077
"torch_api": "torch.nn.functional.local_response_norm",
@@ -5014,4 +5050,4 @@
50145050
"paddle._C_ops._run_custom_op": {
50155051
"Rule": "CopsRunCustomOpRule"
50165052
}
5017-
}
5053+
}

0 commit comments

Comments
 (0)