# 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. """ please make sure to run in the tools path usage: python sampcd_processor.py --mode {cpu or gpu} {cpu or gpu}: running in cpu version or gpu version for example, you can run cpu version testing like this: python sampcd_processor.py --mode cpu """ from __future__ import annotations import collections import functools import multiprocessing import os import platform import queue import re import threading import time import typing import xdoctest from sampcd_processor_utils import ( TEST_TIMEOUT, DocTester, TestResult, log_exit, logger, parse_args, run_doctest, ) XDOCTEST_CONFIG = { "global_exec": r"\n".join( [ "import paddle", "paddle.device.set_device('cpu')", "paddle.set_default_dtype('float32')", "paddle.disable_static()", ] ), "default_runtime_state": {"IGNORE_WHITESPACE": True}, } def _patch_global_state(debug, verbose): # patch xdoctest global_state from xdoctest import global_state _debug_xdoctest = debug and verbose > 2 global_state.DEBUG = _debug_xdoctest global_state.DEBUG_PARSER = global_state.DEBUG_PARSER and _debug_xdoctest global_state.DEBUG_CORE = global_state.DEBUG_CORE and _debug_xdoctest global_state.DEBUG_RUNNER = global_state.DEBUG_RUNNER and _debug_xdoctest global_state.DEBUG_DOCTEST = global_state.DEBUG_DOCTEST and _debug_xdoctest def _patch_tensor_place(): from xdoctest import checker pattern_tensor = re.compile( r""" (Tensor\(.*?place=) # Tensor start (.*?) # Place=(XXX) (\,.*?\)) """, re.VERBOSE | re.DOTALL, ) _check_output = checker.check_output def check_output(got, want, runstate=None): if not want: # nocover return True return _check_output( got=pattern_tensor.sub(r'\1Place(cpu)\3', got), want=pattern_tensor.sub(r'\1Place(cpu)\3', want), runstate=runstate, ) checker.check_output = check_output def _patch_float_precision(digits): from xdoctest import checker pattern_number = re.compile( r""" (?: (?:(?<=[\s*\[\(\'\"\:])|^) # number starts (?: # int/float or complex-real (?: [+-]? (?: (?: \d*\.\d+) | (?: \d+\.?) # int/float ) ) (?:[Ee][+-]?\d+)? ) (?: # complex-imag (?: (?: [+-]? (?: (?: \d*\.\d+) | (?: \d+\.?) ) ) (?:[Ee][+-]?\d+)? ) (?:[Jj]) )? ) """, re.VERBOSE | re.DOTALL, ) _check_output = checker.check_output def _sub_number(match_obj, digits): match_str = match_obj.group() if 'j' in match_str or 'J' in match_str: try: match_num = complex(match_str) except ValueError: return match_str return ( str( complex( round(match_num.real, digits), round(match_num.imag, digits), ) ) .strip('(') .strip(')') ) else: try: return str(round(float(match_str), digits)) except ValueError: return match_str sub_number = functools.partial(_sub_number, digits=digits) def check_output(got, want, runstate=None): if not want: # nocover return True return _check_output( got=pattern_number.sub(sub_number, got), want=pattern_number.sub(sub_number, want), runstate=runstate, ) checker.check_output = check_output class Directive: """Base class of global directives just for `xdoctest`.""" pattern: typing.Pattern def parse_directive(self, docstring: str) -> tuple[str, typing.Any]: pass class TimeoutDirective(Directive): pattern = re.compile( r""" (?: (?: \s*\>{3}\s*\#\s*x?doctest\:\s* ) (?P[\+\-]) (?: TIMEOUT ) \( (?P