未验证 提交 92080699 编写于 作者: M megemini 提交者: GitHub

[Change] 不再兼容旧格式示例代码 (#56573)

* [Change] forbid legacy sample code style

* [Change] remove legacy sampcd_processor.py

* [Fix] fix command

* [Change] check plain style sample code

* [Fix] fix logger

* [Change] remove arguments

* [Change] remove cb_required

* [Change] change parse args

* [Change] restore creation.py
上级 e0841332
......@@ -3140,10 +3140,10 @@ function exec_samplecode_test() {
cd ${PADDLE_ROOT}/tools
if [ "$1" = "cpu" ] ; then
python sampcd_processor.py --debug cpu; example_error=$?
python sampcd_processor.py --debug --mode cpu; example_error=$?
elif [ "$1" = "gpu" ] ; then
SAMPLE_CODE_EXEC_THREADS=${SAMPLE_CODE_EXEC_THREADS:-2}
python sampcd_processor.py --threads=${SAMPLE_CODE_EXEC_THREADS} --debug gpu; example_error=$?
python sampcd_processor.py --threads=${SAMPLE_CODE_EXEC_THREADS} --debug --mode gpu; example_error=$?
fi
if [ "$example_error" != "0" ];then
echo "Code instance execution failed" >&2
......
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
# 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.
......@@ -13,486 +13,440 @@
# limitations under the License.
"""
please make sure to run in the tools path
usage: python sample_test.py {cpu or gpu}
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 cpu
python sampcd_processor.py --mode cpu
"""
import functools
import logging
import multiprocessing
import os
import platform
import queue
import re
import shutil
import subprocess
import sys
import threading
import time
import typing
from sampcd_processor_utils import ENV_KEY_TEST_CAPACITY # noqa: F401
import xdoctest
from sampcd_processor_utils import (
API_DIFF_SPEC_FN,
extract_code_blocks_from_docstr,
get_full_api_from_pr_spec,
get_incrementapi,
TEST_TIMEOUT,
DocTester,
TestResult,
logger,
parse_args,
run_doctest,
)
from sampcd_processor_xdoctest import Xdoctester
logger = logging.getLogger()
if logger.handlers:
console = logger.handlers[
0
] # we assume the first handler is the one we want to configure
else:
console = logging.StreamHandler(stream=sys.stderr)
logger.addHandler(console)
console.setFormatter(logging.Formatter("%(message)s"))
RUN_ON_DEVICE = 'cpu'
SAMPLE_CODE_TEST_CAPACITY = set()
GPU_ID = 0
whl_error = []
SAMPLECODE_TEMPDIR = 'samplecode_temp'
ENV_KEY_CODES_FRONTEND = 'CODES_INSERTED_INTO_FRONTEND'
SUMMARY_INFO = {
'success': [],
'failed': [],
'skiptest': [],
'nocodes': [],
# ... required not-match
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 find_all(srcstr, substr):
"""
to find all desired substring in the source string
and return their starting indices as a list
Args:
srcstr(str): the parent string
substr(str): substr
Returns:
list: a list of the indices of the substrings
found
"""
indices = []
gotone = srcstr.find(substr)
while gotone != -1:
indices.append(gotone)
gotone = srcstr.find(substr, gotone + 1)
return indices
def find_last_future_line_end(cbstr):
"""
find the last `__future__` line.
Args:
docstr(str): docstring
Return:
index of the line end or None.
"""
pat = re.compile('__future__.*\n')
lastmo = None
it = re.finditer(pat, cbstr)
while True:
try:
lastmo = next(it)
except StopIteration:
break
if lastmo:
return lastmo.end()
else:
return None
def get_test_capacity():
"""
collect capacities and set to SAMPLE_CODE_TEST_CAPACITY
"""
global SAMPLE_CODE_TEST_CAPACITY # write
global ENV_KEY_TEST_CAPACITY, RUN_ON_DEVICE # readonly
if ENV_KEY_TEST_CAPACITY in os.environ:
for r in os.environ[ENV_KEY_TEST_CAPACITY].split(','):
rr = r.strip().lower()
if r:
SAMPLE_CODE_TEST_CAPACITY.add(rr)
if 'cpu' not in SAMPLE_CODE_TEST_CAPACITY:
SAMPLE_CODE_TEST_CAPACITY.add('cpu')
if RUN_ON_DEVICE:
SAMPLE_CODE_TEST_CAPACITY.add(RUN_ON_DEVICE)
def is_required_match(requirestr, cbtitle='not-specified'):
"""
search the required instruction in the code-block, and check it match the current running environment.
environment values of equipped: cpu, gpu, xpu, distributed, skip
the 'skip' is the special flag to skip the test, so is_required_match will return False directly.
Args:
requirestr(str): the required string.
cbtitle(str): the title of the code-block.
returns:
True - yes, matched
False - not match
None - skipped # trick
"""
global SAMPLE_CODE_TEST_CAPACITY, RUN_ON_DEVICE # readonly
requires = {'cpu'}
if requirestr:
for r in requirestr.split(','):
rr = r.strip().lower()
if rr:
requires.add(rr)
else:
requires.add(RUN_ON_DEVICE)
if 'skip' in requires or 'skiptest' in requires:
logger.info('%s: skipped', cbtitle)
return None
if all(
k in SAMPLE_CODE_TEST_CAPACITY
for k in requires
if k not in ['skip', 'skiptest']
):
return True
def _patch_global_state(debug, verbose):
# patch xdoctest global_state
from xdoctest import global_state
logger.info(
'%s: the equipments [%s] not match the required [%s].',
cbtitle,
','.join(SAMPLE_CODE_TEST_CAPACITY),
','.join(requires),
)
return False
def insert_codes_into_codeblock(codeblock, apiname='not-specified'):
"""
insert some codes in the frontend and backend into the code-block.
"""
global ENV_KEY_CODES_FRONTEND, GPU_ID, RUN_ON_DEVICE # readonly
inserted_codes_f = ''
inserted_codes_b = ''
if (
ENV_KEY_CODES_FRONTEND in os.environ
and os.environ[ENV_KEY_CODES_FRONTEND]
):
inserted_codes_f = os.environ[ENV_KEY_CODES_FRONTEND]
else:
cpu_str = '\nimport os\nos.environ["CUDA_VISIBLE_DEVICES"] = ""\n'
gpu_str = (
'\nimport os\nos.environ["CUDA_VISIBLE_DEVICES"] = "{}"\n'.format(
GPU_ID
)
)
if 'required' in codeblock and codeblock['required']:
if codeblock['required'] == 'cpu':
inserted_codes_f = cpu_str
elif codeblock['required'] == 'gpu':
inserted_codes_f = gpu_str
else:
if RUN_ON_DEVICE == "cpu":
inserted_codes_f = cpu_str
elif RUN_ON_DEVICE == "gpu":
inserted_codes_f = gpu_str
inserted_codes_b = '\nprint("{}\'s sample code (name:{}, id:{}) is executed successfully!")'.format(
apiname, codeblock['name'], codeblock['id']
_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.X | re.S,
)
cb = codeblock['codes']
last_future_line_end = find_last_future_line_end(cb)
if last_future_line_end:
return (
cb[:last_future_line_end]
+ inserted_codes_f
+ cb[last_future_line_end:]
+ inserted_codes_b
_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,
)
else:
return inserted_codes_f + cb + inserted_codes_b
def is_ps_wrapped_codeblock(codeblock):
"""If the codeblock is wrapped by PS1(>>> ),
we skip test and use xdoctest instead.
"""
codes = codeblock['codes']
match_obj = re.search(r"\n>>>\s?", "\n" + codes)
return match_obj is not None
def sampcd_extract_to_file(srccom, name, htype="def", hname=""):
"""
Extract sample codes from __doc__, and write them to files.
Args:
srccom(str): the source comment of some API whose
example codes will be extracted and run.
name(str): the name of the API.
htype(str): the type of hint banners, def/class/method.
hname(str): the name of the hint banners , e.t. def hname.
Returns:
sample_code_filenames(list of str)
"""
global GPU_ID, RUN_ON_DEVICE, SAMPLECODE_TEMPDIR # readonly
global SUMMARY_INFO # update
codeblocks = extract_code_blocks_from_docstr(srccom)
if len(codeblocks) == 0:
SUMMARY_INFO['nocodes'].append(name)
# detect sample codes using >>> to format and consider this situation as wrong
logger.info(htype + " name:" + name)
logger.info("-----------------------")
if srccom.find("Examples:") != -1:
logger.info("----example code check----")
if srccom.find(">>>") != -1:
logger.warning(
r"""Deprecated sample code style:
Examples:
>>>codeline
>>>codeline
Please use '.. code-block:: python' to format the sample code."""
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
)
)
return []
else:
logger.error(
"Error: No sample code found! Please check if the API comment contais string 'Examples:' correctly"
(?:[Ee][+-]?\d+)?
)
return []
(?: # complex-imag
(?:
(?:
[+-]?
(?:
(?: \d*\.\d+) | (?: \d+\.?)
)
)
(?:[Ee][+-]?\d+)?
)
(?:[Jj])
)?
)
""",
re.X | re.S,
)
sample_code_filenames = []
for y, cb in enumerate(codeblocks):
if is_ps_wrapped_codeblock(cb):
SUMMARY_INFO['skiptest'].append("{}-{}".format(name, cb['id']))
logger.info(
'{}\' code block (name:{}, id:{}) is wrapped by PS1(>>> ), which will be tested by xdoctest.'.format(
name, cb['name'], cb['id']
_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(')')
)
continue
matched = is_required_match(cb['required'], name)
# matched has three states:
# True - please execute it;
# None - no sample code found;
# False - it need other special equipment or environment.
# so, the following conditional statements are intentionally arranged.
if matched:
tfname = os.path.join(
SAMPLECODE_TEMPDIR,
'{}_example{}'.format(
name,
'.py' if len(codeblocks) == 1 else f'_{y + 1}.py',
),
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 direvtives just for `xdoctest`."""
pattern: typing.Pattern
def parse_directive(self, docstring: str) -> typing.Tuple[str, typing.Any]:
pass
class TimeoutDirective(Directive):
pattern = re.compile(
r"""
(?:
(?:
\s*\>{3}\s*\#\s*x?doctest\:\s*
)
with open(tfname, 'w') as tempf:
sampcd = insert_codes_into_codeblock(cb, name)
tempf.write(sampcd)
sample_code_filenames.append(tfname)
elif matched is None:
logger.info(
'{}\' code block (name:{}, id:{}) is skipped.'.format(
name, cb['name'], cb['id']
)
(?P<op>[\+\-])
(?:
TIMEOUT
)
SUMMARY_INFO['skiptest'].append("{}-{}".format(name, cb['id']))
elif not matched:
logger.info(
'{}\' code block (name:{}, id:{}) required({}) not match capacity({}).'.format(
name,
cb['name'],
cb['id'],
cb['required'],
SAMPLE_CODE_TEST_CAPACITY,
)
\(
(?P<time>\d+)
\)
(?:
\s*?
)
if cb['required'] not in SUMMARY_INFO:
SUMMARY_INFO[cb['required']] = []
SUMMARY_INFO[cb['required']].append("{}-{}".format(name, cb['id']))
return sample_code_filenames
def execute_samplecode(tfname):
"""
Execute a sample-code test
Args:
tfname: the filename of the sample code
Returns:
result: success or not
tfname: same as the input argument
msg: the stdout output of the sample code executing
time: time consumed by sample code
"""
result = True
msg = None
if platform.python_version()[0] in ["3"]:
cmd = [sys.executable, tfname]
else:
logger.error("Error: fail to parse python version!")
result = False
sys.exit(1)
logger.info("----example code check----")
logger.info("executing sample code: %s", tfname)
start_time = time.time()
subprc = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
""",
re.X | re.S,
)
output, error = subprc.communicate()
msg = "".join(output.decode(encoding='utf-8'))
err = "".join(error.decode(encoding='utf-8'))
end_time = time.time()
if subprc.returncode != 0:
with open(tfname, 'r') as f:
logger.warning(
"""Sample code error found in %s:
-----------------------
%s
-----------------------
subprocess return code: %d
Error Raised from Sample Code:
stderr: %s
stdout: %s
""",
tfname,
f.read(),
subprc.returncode,
err,
msg,
def __init__(self, timeout):
self._timeout = timeout
def parse_directive(self, docstring):
match_obj = self.pattern.search(docstring)
if match_obj is not None:
op_time = match_obj.group('time')
match_start = match_obj.start()
match_end = match_obj.end()
return (
(docstring[:match_start] + '\n' + docstring[match_end:]),
float(op_time),
)
logger.info("----example code check failed----")
result = False
else:
logger.info("----example code check success----")
# msg is the returned code execution report
return result, tfname, msg, end_time - start_time
return docstring, float(self._timeout)
def get_filenames(full_test=False):
'''
this function will get the sample code files that pending for check.
class Xdoctester(DocTester):
"""A Xdoctest doctester."""
Args:
full_test: the full apis or the increment
directives: typing.Dict[str, typing.Tuple[typing.Type[Directive], ...]] = {
'timeout': (TimeoutDirective, TEST_TIMEOUT)
}
Returns:
def __init__(
self,
debug=False,
style='freeform',
target='codeblock',
mode='native',
verbose=2,
patch_global_state=True,
patch_tensor_place=True,
patch_float_precision=5,
use_multiprocessing=True,
**config,
):
self.debug = debug
dict: the sample code files pending for check .
self.style = style
self.target = target
self.mode = mode
self.verbose = verbose
self.config = {**XDOCTEST_CONFIG, **(config or {})}
'''
global whl_error
import paddle # noqa: F401
import paddle.static.quantization # noqa: F401
self._patch_global_state = patch_global_state
self._patch_tensor_place = patch_tensor_place
self._patch_float_precision = patch_float_precision
self._use_multiprocessing = use_multiprocessing
whl_error = []
if full_test:
get_full_api_from_pr_spec()
else:
get_incrementapi()
all_sample_code_filenames = {}
with open(API_DIFF_SPEC_FN) as f:
for line in f.readlines():
api = line.replace('\n', '')
try:
api_obj = eval(api)
except AttributeError:
whl_error.append(api)
continue
except SyntaxError:
logger.warning('line:%s, api:%s', line, api)
# paddle.Tensor.<lambda>
continue
if hasattr(api_obj, '__doc__') and api_obj.__doc__:
sample_code_filenames = sampcd_extract_to_file(
api_obj.__doc__, api
)
for tfname in sample_code_filenames:
all_sample_code_filenames[tfname] = api
return all_sample_code_filenames
# patch xdoctest before `xdoctest.core.parse_docstr_examples`
self._patch_xdoctest()
self.docstring_parser = functools.partial(
xdoctest.core.parse_docstr_examples, style=self.style
)
if __name__ == '__main__':
args = parse_args()
if args.debug:
logger.setLevel(logging.DEBUG)
else:
logger.setLevel(logging.INFO)
if args.logf:
logfHandler = logging.FileHandler(args.logf)
logfHandler.setFormatter(
logging.Formatter(
"%(asctime)s - %(funcName)s:%(lineno)d - %(levelname)s - %(message)s"
)
self.directive_pattern = re.compile(
r"""
(?<=(\#\s)) # positive lookbehind, directive begins
(doctest) # directive prefix, which should be replaced
(?=(:\s*.*\n)) # positive lookahead, directive content
""",
re.X,
)
logger.addHandler(logfHandler)
if args.mode == "gpu":
GPU_ID = args.gpu_id
logger.info("using GPU_ID %d", GPU_ID)
elif args.mode != "cpu":
logger.error(
"Unrecognized argument:%s, 'cpu' or 'gpu' is desired.", args.mode
self.directive_prefix = 'xdoctest'
def _patch_xdoctest(self):
if self._patch_global_state:
_patch_global_state(self.debug, self.verbose)
if self._patch_tensor_place:
_patch_tensor_place()
if self._patch_float_precision is not None:
_patch_float_precision(self._patch_float_precision)
def _parse_directive(
self, docstring: str
) -> typing.Tuple[str, typing.Dict[str, Directive]]:
directives = {}
for name, directive_cls in self.directives.items():
docstring, direct = directive_cls[0](
*directive_cls[1:]
).parse_directive(docstring)
directives[name] = direct
return docstring, directives
def convert_directive(self, docstring: str) -> str:
"""Replace directive prefix with xdoctest"""
return self.directive_pattern.sub(self.directive_prefix, docstring)
def prepare(self, test_capacity: set):
"""Set environs for xdoctest directive.
The keys in environs, which also used in `# xdoctest: +REQUIRES(env:XX)`, should be UPPER case.
If `test_capacity = {"cpu"}`, then we set:
- `os.environ["CPU"] = "True"`
which makes this SKIPPED:
- # xdoctest: +REQUIRES(env:GPU)
If `test_capacity = {"cpu", "gpu"}`, then we set:
- `os.environ["CPU"] = "True"`
- `os.environ["GPU"] = "True"`
which makes this SUCCESS:
- # xdoctest: +REQUIRES(env:GPU)
"""
logger.info("Set xdoctest environ ...")
for capacity in test_capacity:
key = capacity.upper()
os.environ[key] = "True"
logger.info("Environ: %s , set to True.", key)
logger.info("API check using Xdoctest prepared!-- Example Code")
logger.info("running under python %s", platform.python_version())
logger.info("running under xdoctest %s", xdoctest.__version__)
def run(self, api_name: str, docstring: str) -> typing.List[TestResult]:
"""Run the xdoctest with a docstring."""
# parse global directive
docstring, directives = self._parse_directive(docstring)
# extract xdoctest examples
examples_to_test, examples_nocode = self._extract_examples(
api_name, docstring, **directives
)
sys.exit("Invalid arguments")
RUN_ON_DEVICE = args.mode
get_test_capacity()
logger.info("API check -- Example Code")
logger.info(
"sample_test running under python %s", platform.python_version()
)
if os.path.exists(SAMPLECODE_TEMPDIR):
if not os.path.isdir(SAMPLECODE_TEMPDIR):
os.remove(SAMPLECODE_TEMPDIR)
os.mkdir(SAMPLECODE_TEMPDIR)
else:
os.mkdir(SAMPLECODE_TEMPDIR)
# run xdoctest
try:
result = self._execute_xdoctest(
examples_to_test, examples_nocode, **directives
)
except queue.Empty:
result = [
TestResult(
name=api_name,
timeout=True,
time=directives.get('timeout', TEST_TIMEOUT),
)
]
return result
def _extract_examples(self, api_name, docstring, **directives):
"""Extract code block examples from docstring."""
examples_to_test = {}
examples_nocode = {}
for example_idx, example in enumerate(
self.docstring_parser(docstr=docstring, callname=api_name)
):
example.mode = self.mode
example.config.update(self.config)
example_key = f"{api_name}_{example_idx}"
# check whether there are some parts parsed by xdoctest
if not example._parts:
examples_nocode[example_key] = example
continue
filenames = get_filenames(args.full_test)
if len(filenames) == 0 and len(whl_error) == 0:
logger.info("-----API_PR.spec is the same as API_DEV.spec-----")
# not exit if no filenames, we should do xdoctest later.
# sys.exit(0)
examples_to_test[example_key] = example
# delete temp files
if not args.debug:
shutil.rmtree(SAMPLECODE_TEMPDIR)
if not examples_nocode and not examples_to_test:
examples_nocode[api_name] = api_name
else:
logger.info("API_PR is diff from API_DEV: %s", filenames)
return examples_to_test, examples_nocode
threads = multiprocessing.cpu_count()
if args.threads:
threads = args.threads
po = multiprocessing.Pool(threads)
results = po.map_async(execute_samplecode, filenames.keys())
po.close()
po.join()
def _execute_xdoctest(
self, examples_to_test, examples_nocode, **directives
):
if self._use_multiprocessing:
_ctx = multiprocessing.get_context('spawn')
result_queue = _ctx.Queue()
exec_processer = functools.partial(_ctx.Process, daemon=True)
else:
result_queue = queue.Queue()
exec_processer = functools.partial(threading.Thread, daemon=True)
processer = exec_processer(
target=self._execute_with_queue,
args=(
result_queue,
examples_to_test,
examples_nocode,
),
)
result = results.get()
processer.start()
result = result_queue.get(
timeout=directives.get('timeout', TEST_TIMEOUT)
)
processer.join()
return result
def _execute(self, examples_to_test, examples_nocode):
"""Run xdoctest for each example"""
# patch xdoctest first in each process
self._patch_xdoctest()
# run the xdoctest
test_results = []
for _, example in examples_to_test.items():
start_time = time.time()
result = example.run(verbose=self.verbose, on_error='return')
end_time = time.time()
test_results.append(
TestResult(
name=str(example),
passed=result['passed'],
skipped=result['skipped'],
failed=result['failed'],
test_msg=str(result['exc_info']),
time=end_time - start_time,
)
)
for _, example in examples_nocode.items():
test_results.append(TestResult(name=str(example), nocode=True))
return test_results
# delete temp files
if not args.debug:
shutil.rmtree(SAMPLECODE_TEMPDIR)
def _execute_with_queue(self, queue, examples_to_test, examples_nocode):
queue.put(self._execute(examples_to_test, examples_nocode))
def print_summary(self, test_results, whl_error=None):
summary_success = []
summary_failed = []
summary_skiptest = []
summary_timeout = []
summary_nocodes = []
stdout_handler = logging.StreamHandler(stream=sys.stdout)
logger.addHandler(stdout_handler)
logger.info("----------------End of the Check--------------------")
if len(whl_error) != 0:
if whl_error is not None and whl_error:
logger.info("%s is not in whl.", whl_error)
logger.info("")
logger.info("Please check the whl package and API_PR.spec!")
......@@ -504,62 +458,71 @@ if __name__ == '__main__':
logger.info(
"3. run 'python tools/print_signatures.py paddle > paddle/fluid/API.spec'."
)
for temp in result:
if not temp[0]:
for test_result in test_results:
if test_result.failed:
logger.info(
"In addition, mistakes found in sample codes: %s",
temp[1],
test_result.name,
)
logger.info("----------------------------------------------------")
sys.exit(1)
else:
timeovered_test = {}
for temp in result:
if not temp[0]:
logger.info(
"In addition, mistakes found in sample codes: %s",
temp[1],
)
SUMMARY_INFO['failed'].append(temp[1])
for test_result in test_results:
if not test_result.nocode:
if test_result.passed:
summary_success.append(test_result.name)
if test_result.skipped:
summary_skiptest.append(test_result.name)
if test_result.failed:
summary_failed.append(test_result.name)
if test_result.timeout:
summary_timeout.append(
{
'api_name': test_result.name,
'run_time': test_result.time,
}
)
else:
SUMMARY_INFO['success'].append(temp[1])
if temp[3] > 10:
timeovered_test[temp[1]] = temp[3]
summary_nocodes.append(test_result.name)
if len(timeovered_test):
if len(summary_success):
logger.info(
"%d sample codes ran time over 10s", len(timeovered_test)
">>> %d sample codes ran success", len(summary_success)
)
if args.debug:
for k, v in timeovered_test.items():
logger.info(f'{k} - {v}s')
if len(SUMMARY_INFO['success']):
logger.info('\n'.join(summary_success))
if len(summary_skiptest):
logger.info(
"%d sample codes ran success", len(SUMMARY_INFO['success'])
">>> %d sample codes skipped", len(summary_skiptest)
)
for k, v in SUMMARY_INFO.items():
if k not in ['success', 'failed', 'skiptest', 'nocodes']:
logger.info(
"%d sample codes required not match for %s", len(v), k
)
if len(SUMMARY_INFO['skiptest']):
logger.info('\n'.join(summary_skiptest))
if len(summary_nocodes):
logger.info(
"%d sample codes skipped", len(SUMMARY_INFO['skiptest'])
">>> %d apis could not run test or don't have sample codes",
len(summary_nocodes),
)
if args.debug:
logger.info('\n'.join(SUMMARY_INFO['skiptest']))
if len(SUMMARY_INFO['nocodes']):
logger.info('\n'.join(summary_nocodes))
if len(summary_timeout):
logger.info(
"%d apis don't have sample codes",
len(SUMMARY_INFO['nocodes']),
">>> %d sample codes ran timeout", len(summary_timeout)
)
if args.debug:
logger.info('\n'.join(SUMMARY_INFO['nocodes']))
if len(SUMMARY_INFO['failed']):
for _result in summary_timeout:
logger.info(
f"{_result['api_name']} - more than {_result['run_time']}s"
)
if len(summary_failed):
logger.info(
"%d sample codes ran failed", len(SUMMARY_INFO['failed'])
">>> %d sample codes ran failed", len(summary_failed)
)
logger.info('\n'.join(SUMMARY_INFO['failed']))
logger.info('\n'.join(summary_failed))
if summary_failed or summary_timeout or summary_nocodes:
logger.info(
"Mistakes found in sample codes. Please recheck the sample codes."
)
......@@ -567,5 +530,7 @@ if __name__ == '__main__':
logger.info("Sample code check is successful!")
# run xdoctest
if __name__ == '__main__':
args = parse_args()
run_doctest(args, doctester=Xdoctester(debug=args.debug))
......@@ -265,7 +265,7 @@ def extract_code_blocks_from_docstr(docstr, google_style=True):
Return:
code_blocks: A list of code-blocks, indent removed.
element {'name': the code-block's name, 'id': sequence id.
'codes': codes, 'required': 'gpu', 'in_examples': bool, code block in `Examples` or not,}
'codes': codes, 'in_examples': bool, code block in `Examples` or not,}
"""
code_blocks = []
......@@ -290,7 +290,6 @@ def extract_code_blocks_from_docstr(docstr, google_style=True):
cb_start_pat = re.compile(r"code-block::\s*python")
cb_param_pat = re.compile(r"^\s*:(\w+):\s*(\S*)\s*$")
cb_required_pat = re.compile(r"^\s*#\s*require[s|d]\s*:\s*(\S+)\s*$")
cb_info = {}
cb_info['cb_started'] = False
......@@ -298,23 +297,20 @@ def extract_code_blocks_from_docstr(docstr, google_style=True):
cb_info['cb_cur_indent'] = -1
cb_info['cb_cur_name'] = None
cb_info['cb_cur_seq_id'] = 0
cb_info['cb_required'] = None
def _cb_started():
# nonlocal cb_started, cb_cur_name, cb_required, cb_cur_seq_id
# nonlocal cb_started, cb_cur_name, cb_cur_seq_id
cb_info['cb_started'] = True
cb_info['cb_cur_seq_id'] += 1
cb_info['cb_cur_name'] = None
cb_info['cb_required'] = None
def _append_code_block(in_examples):
# nonlocal code_blocks, cb_cur, cb_cur_name, cb_cur_seq_id, cb_required
# nonlocal code_blocks, cb_cur, cb_cur_name, cb_cur_seq_id
code_blocks.append(
{
'codes': inspect.cleandoc("\n" + "\n".join(cb_info['cb_cur'])),
'name': cb_info['cb_cur_name'],
'id': cb_info['cb_cur_seq_id'],
'required': cb_info['cb_required'],
'in_examples': in_examples,
}
)
......@@ -339,10 +335,6 @@ def extract_code_blocks_from_docstr(docstr, google_style=True):
if mo_p.group(1) == 'name':
cb_info['cb_cur_name'] = mo_p.group(2)
continue
# read the required directive
mo_r = cb_required_pat.match(linecont)
if mo_r:
cb_info['cb_required'] = mo_r.group(1)
# docstring end
if lineno == lastlineindex:
mo = re.search(r"\S", linecont)
......@@ -479,6 +471,49 @@ def get_docstring(full_test=False):
return docstrings_to_test, whl_error
def check_old_style(docstrings_to_test: typing.Dict[str, str]):
old_style_apis = []
for api_name, raw_docstring in docstrings_to_test.items():
for codeblock in extract_code_blocks_from_docstr(
raw_docstring, google_style=False
):
old_style = True
for line in codeblock['codes'].splitlines():
if line.strip().startswith('>>>'):
old_style = False
break
if old_style:
codeblock_name = codeblock['name']
codeblock_id = codeblock['id']
docstring_name = '{}:{}'.format(
api_name, codeblock_name or codeblock_id
)
old_style_apis.append(docstring_name)
if old_style_apis:
stdout_handler = logging.StreamHandler(stream=sys.stdout)
logger.addHandler(stdout_handler)
logger.info(
">>> %d apis use plain sample code style.",
len(old_style_apis),
)
logger.info('=======================')
logger.info('\n'.join(old_style_apis))
logger.info('=======================')
logger.info("Check Failed!")
logger.info("DEPRECATION: Please do not use plain sample code style.")
logger.info(
"For more information: https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/dev_guides/style_guide_and_references/code_example_writing_specification_cn.html "
)
logger.info("----------------End of the Check--------------------")
sys.exit(1)
def exec_gen_doc():
result = True
cmd = ["bash", "document_preview.sh"]
......@@ -568,27 +603,30 @@ def get_test_results(
def run_doctest(args, doctester: DocTester):
logger.info("----------------Codeblock Check Start--------------------")
# init logger
init_logger(debug=args.debug, log_file=args.logf)
logger.info("Check test mode ...")
logger.info("----------------Codeblock Check Start--------------------")
logger.info(">>> Check test mode ...")
run_on_device = check_test_mode(mode=args.mode, gpu_id=args.gpu_id)
logger.info("Get test capacity ...")
logger.info(">>> Get test capacity ...")
sample_code_test_capacity = get_test_capacity(run_on_device)
logger.info("Get docstring from api ...")
logger.info(">>> Get docstring from api ...")
docstrings_to_test, whl_error = get_docstring(full_test=args.full_test)
logger.info("Prepare doctester ...")
logger.info(">>> Checking plain sample code style before Paddle 2.5 ...")
check_old_style(docstrings_to_test)
logger.info(">>> Prepare doctester ...")
doctester.prepare(sample_code_test_capacity)
logger.info("Running doctester ...")
logger.info(">>> Running doctester ...")
test_results = get_test_results(doctester, docstrings_to_test)
logger.info("Print summary ...")
logger.info(">>> Print summary ...")
doctester.print_summary(test_results, whl_error)
if args.mode == "cpu":
......@@ -596,37 +634,39 @@ def run_doctest(args, doctester: DocTester):
exec_gen_doc()
arguments = [
# flags, dest, type, default, help
['--gpu_id', 'gpu_id', int, 0, 'GPU device id to use [0]'],
['--logf', 'logf', str, None, 'file for logging'],
['--threads', 'threads', int, 0, 'sub processes number'],
]
def parse_args():
"""
Parse input arguments
"""
global arguments
parser = argparse.ArgumentParser(description='run Sample Code Test')
parser.add_argument('--debug', dest='debug', action="store_true")
parser.add_argument('--full-test', dest='full_test', action="store_true")
parser.add_argument('mode', type=str, help='run on device', default='cpu')
parser.add_argument(
'--mode', dest='mode', type=str, default='cpu', help='run on device'
)
parser.add_argument(
'--build-doc',
dest='build_doc',
action='store_true',
help='build doc if need.',
)
for item in arguments:
parser.add_argument(
item[0], dest=item[1], help=item[4], type=item[2], default=item[3]
)
if len(sys.argv) == 1:
args = parser.parse_args(['cpu'])
return args
parser.add_argument(
'--gpu_id',
dest='gpu_id',
type=int,
default=0,
help='GPU device id to use [0]',
)
parser.add_argument(
'--logf', dest='logf', type=str, default=None, help='file for logging'
)
parser.add_argument(
'--threads',
dest='threads',
type=int,
default=0,
help='sub processes number',
)
args = parser.parse_args()
return args
# 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_xdoctest.py {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_xdoctest.py cpu
"""
import functools
import logging
import multiprocessing
import os
import platform
import queue
import re
import sys
import threading
import time
import typing
import xdoctest
from sampcd_processor_utils import (
TEST_TIMEOUT,
DocTester,
TestResult,
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.X | re.S,
)
_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.X | re.S,
)
_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 direvtives just for `xdoctest`."""
pattern: typing.Pattern
def parse_directive(self, docstring: str) -> typing.Tuple[str, typing.Any]:
pass
class TimeoutDirective(Directive):
pattern = re.compile(
r"""
(?:
(?:
\s*\>{3}\s*\#\s*x?doctest\:\s*
)
(?P<op>[\+\-])
(?:
TIMEOUT
)
\(
(?P<time>\d+)
\)
(?:
\s*?
)
)
""",
re.X | re.S,
)
def __init__(self, timeout):
self._timeout = timeout
def parse_directive(self, docstring):
match_obj = self.pattern.search(docstring)
if match_obj is not None:
op_time = match_obj.group('time')
match_start = match_obj.start()
match_end = match_obj.end()
return (
(docstring[:match_start] + '\n' + docstring[match_end:]),
float(op_time),
)
return docstring, float(self._timeout)
class Xdoctester(DocTester):
"""A Xdoctest doctester."""
directives: typing.Dict[str, typing.Tuple[typing.Type[Directive], ...]] = {
'timeout': (TimeoutDirective, TEST_TIMEOUT)
}
def __init__(
self,
debug=False,
style='freeform',
target='codeblock',
mode='native',
verbose=2,
patch_global_state=True,
patch_tensor_place=True,
patch_float_precision=5,
use_multiprocessing=True,
**config,
):
self.debug = debug
self.style = style
self.target = target
self.mode = mode
self.verbose = verbose
self.config = {**XDOCTEST_CONFIG, **(config or {})}
self._patch_global_state = patch_global_state
self._patch_tensor_place = patch_tensor_place
self._patch_float_precision = patch_float_precision
self._use_multiprocessing = use_multiprocessing
# patch xdoctest before `xdoctest.core.parse_docstr_examples`
self._patch_xdoctest()
self.docstring_parser = functools.partial(
xdoctest.core.parse_docstr_examples, style=self.style
)
self.directive_pattern = re.compile(
r"""
(?<=(\#\s)) # positive lookbehind, directive begins
(doctest) # directive prefix, which should be replaced
(?=(:\s*.*\n)) # positive lookahead, directive content
""",
re.X,
)
self.directive_prefix = 'xdoctest'
def _patch_xdoctest(self):
if self._patch_global_state:
_patch_global_state(self.debug, self.verbose)
if self._patch_tensor_place:
_patch_tensor_place()
if self._patch_float_precision is not None:
_patch_float_precision(self._patch_float_precision)
def _parse_directive(
self, docstring: str
) -> typing.Tuple[str, typing.Dict[str, Directive]]:
directives = {}
for name, directive_cls in self.directives.items():
docstring, direct = directive_cls[0](
*directive_cls[1:]
).parse_directive(docstring)
directives[name] = direct
return docstring, directives
def convert_directive(self, docstring: str) -> str:
"""Replace directive prefix with xdoctest"""
return self.directive_pattern.sub(self.directive_prefix, docstring)
def prepare(self, test_capacity: set):
"""Set environs for xdoctest directive.
The keys in environs, which also used in `# xdoctest: +REQUIRES(env:XX)`, should be UPPER case.
If `test_capacity = {"cpu"}`, then we set:
- `os.environ["CPU"] = "True"`
which makes this SKIPPED:
- # xdoctest: +REQUIRES(env:GPU)
If `test_capacity = {"cpu", "gpu"}`, then we set:
- `os.environ["CPU"] = "True"`
- `os.environ["GPU"] = "True"`
which makes this SUCCESS:
- # xdoctest: +REQUIRES(env:GPU)
"""
logger.info("Set xdoctest environ ...")
for capacity in test_capacity:
key = capacity.upper()
os.environ[key] = "True"
logger.info("Environ: %s , set to True.", key)
logger.info("API check using Xdoctest prepared!-- Example Code")
logger.info("running under python %s", platform.python_version())
logger.info("running under xdoctest %s", xdoctest.__version__)
def run(self, api_name: str, docstring: str) -> typing.List[TestResult]:
"""Run the xdoctest with a docstring."""
# parse global directive
docstring, directives = self._parse_directive(docstring)
# extract xdoctest examples
examples_to_test, examples_nocode = self._extract_examples(
api_name, docstring, **directives
)
# run xdoctest
try:
result = self._execute_xdoctest(
examples_to_test, examples_nocode, **directives
)
except queue.Empty:
result = [
TestResult(
name=api_name,
timeout=True,
time=directives.get('timeout', TEST_TIMEOUT),
)
]
return result
def _extract_examples(self, api_name, docstring, **directives):
"""Extract code block examples from docstring."""
examples_to_test = {}
examples_nocode = {}
for example_idx, example in enumerate(
self.docstring_parser(docstr=docstring, callname=api_name)
):
example.mode = self.mode
example.config.update(self.config)
example_key = f"{api_name}_{example_idx}"
# check whether there are some parts parsed by xdoctest
if not example._parts:
examples_nocode[example_key] = example
continue
examples_to_test[example_key] = example
if not examples_nocode and not examples_to_test:
examples_nocode[api_name] = api_name
return examples_to_test, examples_nocode
def _execute_xdoctest(
self, examples_to_test, examples_nocode, **directives
):
if self._use_multiprocessing:
_ctx = multiprocessing.get_context('spawn')
result_queue = _ctx.Queue()
exec_processer = functools.partial(_ctx.Process, daemon=True)
else:
result_queue = queue.Queue()
exec_processer = functools.partial(threading.Thread, daemon=True)
processer = exec_processer(
target=self._execute_with_queue,
args=(
result_queue,
examples_to_test,
examples_nocode,
),
)
processer.start()
result = result_queue.get(
timeout=directives.get('timeout', TEST_TIMEOUT)
)
processer.join()
return result
def _execute(self, examples_to_test, examples_nocode):
"""Run xdoctest for each example"""
# patch xdoctest first in each process
self._patch_xdoctest()
# run the xdoctest
test_results = []
for _, example in examples_to_test.items():
start_time = time.time()
result = example.run(verbose=self.verbose, on_error='return')
end_time = time.time()
test_results.append(
TestResult(
name=str(example),
passed=result['passed'],
skipped=result['skipped'],
failed=result['failed'],
test_msg=str(result['exc_info']),
time=end_time - start_time,
)
)
for _, example in examples_nocode.items():
test_results.append(TestResult(name=str(example), nocode=True))
return test_results
def _execute_with_queue(self, queue, examples_to_test, examples_nocode):
queue.put(self._execute(examples_to_test, examples_nocode))
def print_summary(self, test_results, whl_error=None):
summary_success = []
summary_failed = []
summary_skiptest = []
summary_timeout = []
summary_nocodes = []
stdout_handler = logging.StreamHandler(stream=sys.stdout)
logger.addHandler(stdout_handler)
logger.info("----------------End of the Check--------------------")
if whl_error is not None and whl_error:
logger.info("%s is not in whl.", whl_error)
logger.info("")
logger.info("Please check the whl package and API_PR.spec!")
logger.info(
"You can follow these steps in order to generate API.spec:"
)
logger.info("1. cd ${paddle_path}, compile paddle;")
logger.info("2. pip install build/python/dist/(build whl package);")
logger.info(
"3. run 'python tools/print_signatures.py paddle > paddle/fluid/API.spec'."
)
for test_result in test_results:
if test_result.failed:
logger.info(
"In addition, mistakes found in sample codes: %s",
test_result.name,
)
logger.info("----------------------------------------------------")
sys.exit(1)
else:
for test_result in test_results:
if not test_result.nocode:
if test_result.passed:
summary_success.append(test_result.name)
if test_result.skipped:
summary_skiptest.append(test_result.name)
if test_result.failed:
summary_failed.append(test_result.name)
if test_result.timeout:
summary_timeout.append(
{
'api_name': test_result.name,
'run_time': test_result.time,
}
)
else:
summary_nocodes.append(test_result.name)
if len(summary_success):
logger.info("%d sample codes ran success", len(summary_success))
logger.info('\n'.join(summary_success))
if len(summary_skiptest):
logger.info("%d sample codes skipped", len(summary_skiptest))
logger.info('\n'.join(summary_skiptest))
if len(summary_nocodes):
logger.info(
"%d apis could not run test or don't have sample codes",
len(summary_nocodes),
)
logger.info('\n'.join(summary_nocodes))
if len(summary_timeout):
logger.info("%d sample codes ran timeout", len(summary_timeout))
for _result in summary_timeout:
logger.info(
f"{_result['api_name']} - more than {_result['run_time']}s"
)
if len(summary_failed):
logger.info("%d sample codes ran failed", len(summary_failed))
logger.info('\n'.join(summary_failed))
logger.info(
"Mistakes found in sample codes. Please recheck the sample codes."
)
sys.exit(1)
logger.info("Sample code check is successful!")
if __name__ == '__main__':
args = parse_args()
run_doctest(args, doctester=Xdoctester(debug=args.debug))
#! python
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
# 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.
......@@ -14,486 +14,23 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import importlib
import os
import re
import shutil
import unittest
import sampcd_processor
from sampcd_processor import (
execute_samplecode,
extract_code_blocks_from_docstr,
find_all,
find_last_future_line_end,
get_test_capacity,
insert_codes_into_codeblock,
is_required_match,
sampcd_extract_to_file,
import xdoctest
from sampcd_processor import Xdoctester
from sampcd_processor_utils import (
get_api_md5,
get_incrementapi,
get_test_results,
)
from sampcd_processor_utils import get_api_md5, get_incrementapi
class Test_find_all(unittest.TestCase):
def test_find_none(self):
self.assertEqual(0, len(find_all('hello', 'world')))
def test_find_one(self):
self.assertListEqual([0], find_all('hello', 'hello'))
def test_find_two(self):
self.assertListEqual(
[1, 15], find_all(' hello, world; hello paddle!', 'hello')
)
class Test_find_last_future_line_end(unittest.TestCase):
def test_no_instant(self):
samplecodes = """
print(10//3)
"""
self.assertIsNone(find_last_future_line_end(samplecodes))
def test_1_instant(self):
samplecodes = """
from __future__ import print_function
print(10//3)
"""
mo = re.search("print_function\n", samplecodes)
self.assertIsNotNone(mo)
self.assertGreaterEqual(
find_last_future_line_end(samplecodes), mo.end()
)
def test_2_instant(self):
samplecodes = """
from __future__ import print_function
from __future__ import division
print(10//3)
"""
mo = re.search("division\n", samplecodes)
self.assertIsNotNone(mo)
self.assertGreaterEqual(
find_last_future_line_end(samplecodes), mo.end()
)
class Test_extract_code_blocks_from_docstr(unittest.TestCase):
def test_no_samplecode(self):
docstr = """
placeholder
"""
codeblocks = extract_code_blocks_from_docstr(docstr)
self.assertListEqual([], codeblocks)
def test_codeblock_before_examples_is_ignored(self):
docstr = """
.. code-block:: python
print(1+1)
Examples:
"""
codeblocks = extract_code_blocks_from_docstr(docstr)
self.assertListEqual(codeblocks, [])
def test_1_samplecode(self):
docstr = """
Examples:
.. code-block:: python
print(1+1)
"""
codeblocks = extract_code_blocks_from_docstr(docstr)
self.assertListEqual(
codeblocks,
[
{
'codes': """print(1+1)""",
'name': None,
'id': 1,
'required': None,
'in_examples': True,
}
],
)
def test_2_samplecodes(self):
docstr = """
placeholder
Examples:
.. code-block:: python
print(1/0)
.. code-block:: python
:name: one_plus_one
:linenos:
# required: gpu
print(1+1)
"""
codeblocks = extract_code_blocks_from_docstr(docstr)
self.assertListEqual(
codeblocks,
[
{
'codes': """print(1/0)""",
'name': None,
'id': 1,
'required': None,
'in_examples': True,
},
{
'codes': """# required: gpu
print(1+1)""",
'name': 'one_plus_one',
'id': 2,
'required': 'gpu',
'in_examples': True,
},
],
)
class Test_insert_codes_into_codeblock(unittest.TestCase):
def test_required_None(self):
codeblock = {
'codes': """print(1/0)""",
'name': None,
'id': 1,
'required': None,
}
self.assertEqual(
"""
import os
os.environ["CUDA_VISIBLE_DEVICES"] = ""
print(1/0)
print("not-specified's sample code (name:None, id:1) is executed successfully!")""",
insert_codes_into_codeblock(codeblock),
)
def test_required_gpu(self):
codeblock = {
'codes': """# required: gpu
print(1+1)""",
'name': None,
'id': 1,
'required': 'gpu',
}
self.assertEqual(
"""
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
# required: gpu
print(1+1)
print("not-specified's sample code (name:None, id:1) is executed successfully!")""",
insert_codes_into_codeblock(codeblock),
)
def test_from_future(self):
codeblock = {
'codes': """
from __future__ import print_function
from __future__ import division
print(10//3)""",
'name': 'future',
'id': 1,
'required': None,
}
self.assertEqual(
"""
from __future__ import print_function
from __future__ import division
import os
os.environ["CUDA_VISIBLE_DEVICES"] = ""
print(10//3)
print("not-specified's sample code (name:future, id:1) is executed successfully!")""",
insert_codes_into_codeblock(codeblock),
)
def clear_capacity():
sampcd_processor.SAMPLE_CODE_TEST_CAPACITY = set()
sampcd_processor.RUN_ON_DEVICE = 'cpu'
if sampcd_processor.ENV_KEY_TEST_CAPACITY in os.environ:
del os.environ[sampcd_processor.ENV_KEY_TEST_CAPACITY]
class Test_get_test_capacity(unittest.TestCase):
def setUp(self):
clear_capacity()
get_test_capacity()
def tearDown(self):
clear_capacity()
get_test_capacity()
def test_NoEnvVar(self):
clear_capacity()
get_test_capacity()
self.assertCountEqual(
[
'cpu',
],
sampcd_processor.SAMPLE_CODE_TEST_CAPACITY,
)
def test_NoEnvVar_RUN_ON_DEVICE_gpu(self):
clear_capacity()
sampcd_processor.RUN_ON_DEVICE = 'gpu'
get_test_capacity()
self.assertCountEqual(
['cpu', 'gpu'], sampcd_processor.SAMPLE_CODE_TEST_CAPACITY
)
def test_EnvVar_gpu(self):
clear_capacity()
os.environ[sampcd_processor.ENV_KEY_TEST_CAPACITY] = 'gpu'
get_test_capacity()
self.assertCountEqual(
['cpu', 'gpu'], sampcd_processor.SAMPLE_CODE_TEST_CAPACITY
)
def test_EnvVar_gpu_and_distributed(self):
clear_capacity()
os.environ[sampcd_processor.ENV_KEY_TEST_CAPACITY] = 'gpu,distributed'
get_test_capacity()
self.assertCountEqual(
['cpu', 'gpu', 'distributed'],
sampcd_processor.SAMPLE_CODE_TEST_CAPACITY,
)
class Test_is_required_match(unittest.TestCase):
def setUp(self):
clear_capacity()
def tearDown(self):
clear_capacity()
get_test_capacity()
def test_alldefault(self):
clear_capacity()
get_test_capacity()
self.assertTrue(is_required_match(''))
self.assertTrue(is_required_match(None))
self.assertTrue(is_required_match('cpu'))
self.assertFalse(is_required_match('gpu'))
self.assertIsNone(is_required_match('skiptest'))
self.assertIsNone(is_required_match('skip'))
self.assertIsNone(is_required_match('cpu,skiptest'))
def test_gpu_equipped(self):
clear_capacity()
os.environ[sampcd_processor.ENV_KEY_TEST_CAPACITY] = 'gpu'
get_test_capacity()
self.assertTrue(is_required_match('cpu'))
self.assertTrue(is_required_match('gpu'))
self.assertTrue(is_required_match('gpu,cpu'))
self.assertIsNone(is_required_match('skiptest'))
self.assertFalse(is_required_match('distributed'))
def test_gpu_distributed_equipped(self):
clear_capacity()
os.environ[sampcd_processor.ENV_KEY_TEST_CAPACITY] = 'gpu,distributed'
get_test_capacity()
self.assertTrue(is_required_match('cpu'))
self.assertTrue(is_required_match('gpu'))
self.assertTrue(is_required_match('distributed'))
self.assertFalse(is_required_match('xpu'))
self.assertIsNone(is_required_match('skiptest'))
class Test_execute_samplecode(unittest.TestCase):
def setUp(self):
if not os.path.exists(sampcd_processor.SAMPLECODE_TEMPDIR):
os.mkdir(sampcd_processor.SAMPLECODE_TEMPDIR)
self.successSampleCodeFile = os.path.join(
sampcd_processor.SAMPLECODE_TEMPDIR, 'samplecode_success.py'
)
with open(self.successSampleCodeFile, 'w') as f:
f.write('print(1+1)')
self.failedSampleCodeFile = os.path.join(
sampcd_processor.SAMPLECODE_TEMPDIR, 'samplecode_failed.py'
)
with open(self.failedSampleCodeFile, 'w') as f:
f.write('print(1/0)')
def tearDown(self):
os.remove(self.successSampleCodeFile)
os.remove(self.failedSampleCodeFile)
def test_run_success(self):
result, tfname, msg, exec_time = execute_samplecode(
self.successSampleCodeFile
)
self.assertTrue(result)
self.assertEqual(self.successSampleCodeFile, tfname)
self.assertIsNotNone(msg)
self.assertLess(msg.find('skipped'), 0)
self.assertLess(exec_time, 10)
def test_run_failed(self):
result, tfname, msg, exec_time = execute_samplecode(
self.failedSampleCodeFile
)
self.assertFalse(result)
self.assertEqual(self.failedSampleCodeFile, tfname)
self.assertIsNotNone(msg)
self.assertLess(msg.find('skipped'), 0)
self.assertLess(exec_time, 10)
def clear_summary_info():
for k in sampcd_processor.SUMMARY_INFO.keys():
sampcd_processor.SUMMARY_INFO[k].clear()
class Test_sampcd_extract_to_file(unittest.TestCase):
def setUp(self):
if not os.path.exists(sampcd_processor.SAMPLECODE_TEMPDIR):
os.mkdir(sampcd_processor.SAMPLECODE_TEMPDIR)
clear_capacity()
os.environ[sampcd_processor.ENV_KEY_TEST_CAPACITY] = 'gpu,distributed'
get_test_capacity()
def tearDown(self):
shutil.rmtree(sampcd_processor.SAMPLECODE_TEMPDIR)
clear_capacity()
get_test_capacity()
def test_1_samplecode(self):
comments = """
Examples:
.. code-block:: python
print(1+1)
"""
funcname = 'one_plus_one'
sample_code_filenames = sampcd_extract_to_file(comments, funcname)
self.assertCountEqual(
[
os.path.join(
sampcd_processor.SAMPLECODE_TEMPDIR,
funcname + '_example.py',
)
],
sample_code_filenames,
)
def test_no_samplecode(self):
comments = """
placeholder
"""
funcname = 'one_plus_one'
sample_code_filenames = sampcd_extract_to_file(comments, funcname)
self.assertCountEqual([], sample_code_filenames)
def test_2_samplecodes(self):
comments = """
placeholder
Examples:
.. code-block:: python
print(1/0)
.. code-block:: python
print(1+1)
"""
funcname = 'one_plus_one'
sample_code_filenames = sampcd_extract_to_file(comments, funcname)
self.assertCountEqual(
[
os.path.join(
sampcd_processor.SAMPLECODE_TEMPDIR,
funcname + '_example_1.py',
),
os.path.join(
sampcd_processor.SAMPLECODE_TEMPDIR,
funcname + '_example_2.py',
),
],
sample_code_filenames,
)
def test_2_samplecodes_has_skipped(self):
comments = """
placeholder
Examples:
.. code-block:: python
# required: skiptest
print(1/0)
.. code-block:: python
print(1+1)
.. code-block:: python
# required: gpu
print(1//1)
.. code-block:: python
# required: xpu
print(1//1)
.. code-block:: python
# required: distributed
print(1//1)
.. code-block:: python
# required: gpu
print(1//1)
"""
funcname = 'one_plus_one'
clear_summary_info()
clear_capacity()
get_test_capacity()
sample_code_filenames = sampcd_extract_to_file(comments, funcname)
self.assertCountEqual(
[
os.path.join(
sampcd_processor.SAMPLECODE_TEMPDIR,
funcname + '_example_2.py',
)
],
sample_code_filenames,
)
self.assertCountEqual(
sampcd_processor.SUMMARY_INFO['skiptest'], [funcname + '-1']
)
self.assertCountEqual(
sampcd_processor.SUMMARY_INFO['gpu'],
[funcname + '-3', funcname + '-6'],
)
self.assertCountEqual(
sampcd_processor.SUMMARY_INFO['xpu'], [funcname + '-4']
)
self.assertCountEqual(
sampcd_processor.SUMMARY_INFO['distributed'], [funcname + '-5']
)
def test_skip_ps_wrapped_code(self):
comments = """
placeholder
Examples:
.. code-block:: python
>>> print(1 + 1)
2
"""
funcname = 'one_plus_one'
sample_code_filenames = sampcd_extract_to_file(comments, funcname)
self.assertCountEqual([], sample_code_filenames)
def _clear_environ():
for k in {'CPU', 'GPU', 'XPU', 'DISTRIBUTED'}:
if k in os.environ:
del os.environ[k]
class Test_get_api_md5(unittest.TestCase):
......@@ -586,8 +123,2045 @@ class Test_get_incrementapi(unittest.TestCase):
)
# https://github.com/PaddlePaddle/Paddle/blob/develop/python/paddle/fluid/layers/ops.py
# why? unabled to use the ast module. emmmmm
class TestXdoctester(unittest.TestCase):
def setUp(self):
self.maxDiff = None
def test_init(self):
doctester = Xdoctester()
self.assertEqual(doctester.debug, False)
self.assertEqual(doctester.style, 'freeform')
self.assertEqual(doctester.target, 'codeblock')
self.assertEqual(doctester.mode, 'native')
doctester = Xdoctester(analysis='static')
self.assertEqual(doctester.config['analysis'], 'static')
def test_convert_directive(self):
doctester = Xdoctester()
docstring_input = "# doctest: -SKIP\n"
docstring_output = doctester.convert_directive(docstring_input)
docstring_target = "# xdoctest: -SKIP\n"
self.assertEqual(docstring_output, docstring_target)
docstring_input = '# doctest: +SKIP("skip this test...")\n'
docstring_output = doctester.convert_directive(docstring_input)
docstring_target = '# xdoctest: +SKIP("skip this test...")\n'
self.assertEqual(docstring_output, docstring_target)
docstring_input = """
placeholder
.. code-block:: python
:name: code-example-0
this is some blabla...
>>> # doctest: +SKIP
>>> print(1+1)
2
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> # doctest: -REQUIRES(env:GPU)
>>> print(1-1)
0
.. code-block:: python
:name: code-example-2
this is some blabla...
>>> # doctest: +REQUIRES(env:GPU, env:XPU, env: DISTRIBUTED)
>>> print(1-1)
0
"""
docstring_output = doctester.convert_directive(docstring_input)
docstring_target = """
placeholder
.. code-block:: python
:name: code-example-0
this is some blabla...
>>> # xdoctest: +SKIP
>>> print(1+1)
2
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> # xdoctest: -REQUIRES(env:GPU)
>>> print(1-1)
0
.. code-block:: python
:name: code-example-2
this is some blabla...
>>> # xdoctest: +REQUIRES(env:GPU, env:XPU, env: DISTRIBUTED)
>>> print(1-1)
0
"""
self.assertEqual(docstring_output, docstring_target)
def test_prepare(self):
doctester = Xdoctester()
_clear_environ()
test_capacity = {'cpu'}
doctester.prepare(test_capacity)
self.assertTrue(os.environ['CPU'])
self.assertFalse(os.environ.get('GPU'))
_clear_environ()
test_capacity = {'cpu', 'gpu'}
doctester.prepare(test_capacity)
self.assertTrue(os.environ['CPU'])
self.assertTrue(os.environ['GPU'])
self.assertFalse(os.environ.get('cpu'))
self.assertFalse(os.environ.get('gpu'))
self.assertFalse(os.environ.get('XPU'))
_clear_environ()
class TestGetTestResults(unittest.TestCase):
def test_global_exec(self):
_clear_environ()
# test set_default_dtype
docstrings_to_test = {
'before_set_default': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> a = paddle.to_tensor(.2)
>>> print(a)
Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
[0.20000000])
""",
'set_default': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.set_default_dtype('float64')
>>> a = paddle.to_tensor(.2)
>>> print(a)
Tensor(shape=[1], dtype=float64, place=Place(cpu), stop_gradient=True,
[0.20000000])
""",
'after_set_default': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> a = paddle.to_tensor(.2)
>>> print(a)
Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
[0.20000000])
""",
}
# test old global_exec
test_capacity = {'cpu'}
doctester = Xdoctester(
style='freeform',
target='codeblock',
global_exec=r"\n".join(
[
"import paddle",
"paddle.device.set_device('cpu')",
]
),
)
doctester.prepare(test_capacity)
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 3)
tr_0, tr_1, tr_2 = test_results
self.assertIn('before_set_default', tr_0.name)
self.assertTrue(tr_0.passed)
self.assertIn('set_default', tr_1.name)
self.assertTrue(tr_1.passed)
# tr_2 is passed, because of multiprocessing
self.assertIn('after_set_default', tr_2.name)
self.assertTrue(tr_2.passed)
# test new default global_exec
doctester = Xdoctester(
style='freeform',
target='codeblock',
)
doctester.prepare(test_capacity)
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 3)
tr_0, tr_1, tr_2 = test_results
self.assertIn('before_set_default', tr_0.name)
self.assertTrue(tr_0.passed)
self.assertIn('set_default', tr_1.name)
self.assertTrue(tr_1.passed)
self.assertIn('after_set_default', tr_2.name)
self.assertTrue(tr_2.passed)
# test disable static
docstrings_to_test = {
'before_enable_static': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> print(paddle.in_dynamic_mode())
True
""",
'enable_static': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.enable_static()
>>> print(paddle.in_dynamic_mode())
False
""",
'after_enable_static': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> print(paddle.in_dynamic_mode())
True
""",
}
# test old global_exec
test_capacity = {'cpu'}
doctester = Xdoctester(
style='freeform',
target='codeblock',
global_exec=r"\n".join(
[
"import paddle",
"paddle.device.set_device('cpu')",
]
),
)
doctester.prepare(test_capacity)
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 3)
tr_0, tr_1, tr_2 = test_results
self.assertIn('before_enable_static', tr_0.name)
self.assertTrue(tr_0.passed)
self.assertIn('enable_static', tr_1.name)
self.assertTrue(tr_1.passed)
# tr_2 is passed, because of multiprocessing
self.assertIn('after_enable_static', tr_2.name)
self.assertTrue(tr_2.passed)
# test new default global_exec
doctester = Xdoctester(
style='freeform',
target='codeblock',
)
doctester.prepare(test_capacity)
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 3)
tr_0, tr_1, tr_2 = test_results
self.assertIn('before_enable_static', tr_0.name)
self.assertTrue(tr_0.passed)
self.assertIn('enable_static', tr_1.name)
self.assertTrue(tr_1.passed)
self.assertIn('after_enable_static', tr_2.name)
self.assertTrue(tr_2.passed)
def test_patch_xdoctest(self):
# test patch tensor place
_clear_environ()
test_capacity = {'cpu'}
doctester = Xdoctester(style='freeform', target='codeblock')
doctester.prepare(test_capacity)
docstrings_to_test = {
'gpu_to_gpu': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('gpu')
>>> a = paddle.to_tensor(.2)
>>> # Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True, [0.20000000])
>>> print(a)
Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
[0.20000000])
""",
'cpu_to_cpu': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('cpu')
>>> a = paddle.to_tensor(.2)
>>> # Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True, [0.20000000])
>>> print(a)
Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
[0.20000000])
""",
'gpu_to_cpu': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('gpu')
>>> a = paddle.to_tensor(.2)
>>> # Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True, [0.20000000])
>>> print(a)
Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
[0.20000000])
""",
'cpu_to_gpu': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('cpu')
>>> a = paddle.to_tensor(.2)
>>> # Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True, [0.20000000])
>>> print(a)
Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
[0.20000000])
""",
'gpu_to_cpu_array': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('gpu')
>>> a = paddle.to_tensor([[1,2,3], [2,3,4], [3,4,5]])
>>> # Tensor(shape=[3, 3], dtype=int64, place=Place(gpu:0), stop_gradient=True,
>>> # [[1, 2, 3],
>>> # [2, 3, 4],
>>> # [3, 4, 5]])
>>> print(a)
Tensor(shape=[3, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
[[1, 2, 3],
[2, 3, 4],
[3, 4, 5]])
""",
'cpu_to_gpu_array': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('cpu')
>>> a = paddle.to_tensor([[1,2,3], [2,3,4], [3,4,5]])
>>> # Tensor(shape=[3, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
>>> # [[1, 2, 3],
>>> # [2, 3, 4],
>>> # [3, 4, 5]])
>>> print(a)
Tensor(shape=[3, 3], dtype=int64, place=Place(gpu:0), stop_gradient=True,
[[1, 2, 3],
[2, 3, 4],
[3, 4, 5]])
""",
}
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 6)
tr_0, tr_1, tr_2, tr_3, tr_4, tr_5 = test_results
self.assertIn('gpu_to_gpu', tr_0.name)
self.assertTrue(tr_0.passed)
self.assertIn('cpu_to_cpu', tr_1.name)
self.assertTrue(tr_1.passed)
self.assertIn('gpu_to_cpu', tr_2.name)
self.assertTrue(tr_2.passed)
self.assertIn('cpu_to_gpu', tr_3.name)
self.assertTrue(tr_3.passed)
self.assertIn('gpu_to_cpu_array', tr_4.name)
self.assertTrue(tr_4.passed)
self.assertIn('cpu_to_gpu_array', tr_5.name)
self.assertTrue(tr_5.passed)
# reload xdoctest.checker
importlib.reload(xdoctest.checker)
_clear_environ()
test_capacity = {'cpu'}
doctester = Xdoctester(
style='freeform', target='codeblock', patch_tensor_place=False
)
doctester.prepare(test_capacity)
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 6)
tr_0, tr_1, tr_2, tr_3, tr_4, tr_5 = test_results
self.assertIn('gpu_to_gpu', tr_0.name)
self.assertTrue(tr_0.passed)
self.assertIn('cpu_to_cpu', tr_1.name)
self.assertTrue(tr_1.passed)
self.assertIn('gpu_to_cpu', tr_2.name)
self.assertFalse(tr_2.passed)
self.assertIn('cpu_to_gpu', tr_3.name)
self.assertFalse(tr_3.passed)
self.assertIn('gpu_to_cpu_array', tr_4.name)
self.assertFalse(tr_4.passed)
self.assertIn('cpu_to_gpu_array', tr_5.name)
self.assertFalse(tr_5.passed)
# test patch float precision
# reload xdoctest.checker
importlib.reload(xdoctest.checker)
_clear_environ()
test_capacity = {'cpu'}
doctester = Xdoctester(style='freeform', target='codeblock')
doctester.prepare(test_capacity)
docstrings_to_test = {
'gpu_to_gpu': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('gpu')
>>> a = paddle.to_tensor(.123456789)
>>> print(a)
Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
[0.123456780])
""",
'cpu_to_cpu': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('cpu')
>>> a = paddle.to_tensor(.123456789)
>>> print(a)
Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
[0.123456780])
""",
'gpu_to_cpu': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('gpu')
>>> a = paddle.to_tensor(.123456789)
>>> print(a)
Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
[0.123456780])
""",
'cpu_to_gpu': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('cpu')
>>> a = paddle.to_tensor(.123456789)
>>> print(a)
Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
[0.123456780])
""",
'gpu_to_cpu_array': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('gpu')
>>> a = paddle.to_tensor([[1.123456789 ,2,3], [2,3,4], [3,4,5]])
>>> print(a)
Tensor(shape=[3, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[1.123456780, 2., 3.],
[2., 3., 4.],
[3., 4., 5.]])
""",
'cpu_to_gpu_array': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('cpu')
>>> a = paddle.to_tensor([[1.123456789,2,3], [2,3,4], [3,4,5]])
>>> print(a)
Tensor(shape=[3, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
[[1.123456780, 2., 3.],
[2., 3., 4.],
[3., 4., 5.]])
""",
'mass_array': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('gpu')
>>> a = paddle.to_tensor(
... [[1.123456780, 2., -3, .3],
... [2, 3, +4., 1.2+10.34e-5j],
... [3, 5.e-3, 1e2, 3e-8]]
... )
>>> # Tensor(shape=[3, 4], dtype=complex64, place=Place(gpu:0), stop_gradient=True,
>>> # [[ (1.1234568357467651+0j) ,
>>> # (2+0j) ,
>>> # (-3+0j) ,
>>> # (0.30000001192092896+0j) ],
>>> # [ (2+0j) ,
>>> # (3+0j) ,
>>> # (4+0j) ,
>>> # (1.2000000476837158+0.00010340000153519213j)],
>>> # [ (3+0j) ,
>>> # (0.004999999888241291+0j) ,
>>> # (100+0j) ,
>>> # (2.999999892949745e-08+0j) ]])
>>> print(a)
Tensor(shape=[3, 4], dtype=complex64, place=Place(AAA), stop_gradient=True,
[[ (1.123456+0j),
(2+0j),
(-3+0j),
(0.3+0j)],
[ (2+0j),
(3+0j),
(4+0j),
(1.2+0.00010340j)],
[ (3+0j),
(0.00499999+0j),
(100+0j),
(2.999999e-08+0j)]])
""",
'float_array': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('cpu')
>>> x = [[2, 3, 4], [7, 8, 9]]
>>> x = paddle.to_tensor(x, dtype='float32')
>>> print(paddle.log(x))
Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[0.69314718, 1.09861231, 1.38629436],
[1.94591010, 2.07944155, 2.19722462]])
""",
'float_array_diff': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('cpu')
>>> x = [[2, 3, 4], [7, 8, 9]]
>>> x = paddle.to_tensor(x, dtype='float32')
>>> print(paddle.log(x))
Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[0.69314712, 1.09861221, 1.386294],
[1.94591032, 2.07944156, 2.1972246]])
""",
}
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 9)
tr_0, tr_1, tr_2, tr_3, tr_4, tr_5, tr_6, tr_7, tr_8 = test_results
self.assertIn('gpu_to_gpu', tr_0.name)
self.assertTrue(tr_0.passed)
self.assertIn('cpu_to_cpu', tr_1.name)
self.assertTrue(tr_1.passed)
self.assertIn('gpu_to_cpu', tr_2.name)
self.assertTrue(tr_2.passed)
self.assertIn('cpu_to_gpu', tr_3.name)
self.assertTrue(tr_3.passed)
self.assertIn('gpu_to_cpu_array', tr_4.name)
self.assertTrue(tr_4.passed)
self.assertIn('cpu_to_gpu_array', tr_5.name)
self.assertTrue(tr_5.passed)
self.assertIn('mass_array', tr_6.name)
self.assertTrue(tr_6.passed)
self.assertIn('float_array', tr_7.name)
self.assertTrue(tr_7.passed)
self.assertIn('float_array_diff', tr_8.name)
self.assertTrue(tr_8.passed)
# reload xdoctest.checker
importlib.reload(xdoctest.checker)
_clear_environ()
test_capacity = {'cpu'}
doctester = Xdoctester(
style='freeform', target='codeblock', patch_float_precision=None
)
doctester.prepare(test_capacity)
docstrings_to_test = {
'gpu_to_gpu': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('gpu')
>>> a = paddle.to_tensor(.123456789)
>>> print(a)
Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
[0.123456780])
""",
'cpu_to_cpu': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('cpu')
>>> a = paddle.to_tensor(.123456789)
>>> print(a)
Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
[0.123456780])
""",
'gpu_to_cpu': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('gpu')
>>> a = paddle.to_tensor(.123456789)
>>> print(a)
Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
[0.123456780])
""",
'cpu_to_gpu': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('cpu')
>>> a = paddle.to_tensor(.123456789)
>>> print(a)
Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
[0.123456780])
""",
'gpu_to_cpu_array': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('gpu')
>>> a = paddle.to_tensor([[1.123456789 ,2,3], [2,3,4], [3,4,5]])
>>> print(a)
Tensor(shape=[3, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[1.123456780, 2., 3.],
[2., 3., 4.],
[3., 4., 5.]])
""",
'cpu_to_gpu_array': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('cpu')
>>> a = paddle.to_tensor([[1.123456789,2,3], [2,3,4], [3,4,5]])
>>> print(a)
Tensor(shape=[3, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
[[1.123456780, 2., 3.],
[2., 3., 4.],
[3., 4., 5.]])
""",
'mass_array': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('gpu')
>>> a = paddle.to_tensor(
... [[1.123456780, 2., -3, .3],
... [2, 3, +4., 1.2+10.34e-5j],
... [3, 5.e-3, 1e2, 3e-8]]
... )
>>> # Tensor(shape=[3, 4], dtype=complex64, place=Place(gpu:0), stop_gradient=True,
>>> # [[ (1.1234568357467651+0j) ,
>>> # (2+0j) ,
>>> # (-3+0j) ,
>>> # (0.30000001192092896+0j) ],
>>> # [ (2+0j) ,
>>> # (3+0j) ,
>>> # (4+0j) ,
>>> # (1.2000000476837158+0.00010340000153519213j)],
>>> # [ (3+0j) ,
>>> # (0.004999999888241291+0j) ,
>>> # (100+0j) ,
>>> # (2.999999892949745e-08+0j) ]])
>>> print(a)
Tensor(shape=[3, 4], dtype=complex64, place=Place(AAA), stop_gradient=True,
[[ (1.123456+0j),
(2+0j),
(-3+0j),
(0.3+0j)],
[ (2+0j),
(3+0j),
(4+0j),
(1.2+0.00010340j)],
[ (3+0j),
(0.00499999+0j),
(100+0j),
(2.999999e-08+0j)]])
""",
'float_array': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('cpu')
>>> x = [[2, 3, 4], [7, 8, 9]]
>>> x = paddle.to_tensor(x, dtype='float32')
>>> print(paddle.log(x))
Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[0.69314718, 1.09861231, 1.38629436],
[1.94591010, 2.07944155, 2.19722462]])
""",
'float_array_diff': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('cpu')
>>> x = [[2, 3, 4], [7, 8, 9]]
>>> x = paddle.to_tensor(x, dtype='float32')
>>> print(paddle.log(x))
Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[0.69314712, 1.09861221, 1.386294],
[1.94591032, 2.07944156, 2.1972246]])
""",
}
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 9)
tr_0, tr_1, tr_2, tr_3, tr_4, tr_5, tr_6, tr_7, tr_8 = test_results
self.assertIn('gpu_to_gpu', tr_0.name)
self.assertFalse(tr_0.passed)
self.assertIn('cpu_to_cpu', tr_1.name)
self.assertFalse(tr_1.passed)
self.assertIn('gpu_to_cpu', tr_2.name)
self.assertFalse(tr_2.passed)
self.assertIn('cpu_to_gpu', tr_3.name)
self.assertFalse(tr_3.passed)
self.assertIn('gpu_to_cpu_array', tr_4.name)
self.assertFalse(tr_4.passed)
self.assertIn('cpu_to_gpu_array', tr_5.name)
self.assertFalse(tr_5.passed)
self.assertIn('mass_array', tr_6.name)
self.assertFalse(tr_6.passed)
self.assertIn('float_array', tr_7.name)
self.assertTrue(tr_7.passed)
self.assertIn('float_array_diff', tr_8.name)
self.assertFalse(tr_8.passed)
def test_run_cpu(self):
_clear_environ()
test_capacity = {'cpu'}
doctester = Xdoctester(style='freeform', target='codeblock')
doctester.prepare(test_capacity)
docstrings_to_test = {
'one_plus_one': """
placeholder
.. code-block:: python
:name: code-example-0
this is some blabla...
>>> # doctest: +SKIP
>>> print(1+1)
2
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> # doctest: +REQUIRES(env:CPU)
>>> print(1-1)
0
""",
'one_minus_one': """
placeholder
.. code-block:: python
:name: code-example-0
this is some blabla...
>>> print(1+1)
2
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> # doctest: +REQUIRES(env:GPU)
>>> print(1-1)
0
""",
}
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 4)
tr_0, tr_1, tr_2, tr_3 = test_results
self.assertIn('one_plus_one', tr_0.name)
self.assertIn('code-example-0', tr_0.name)
self.assertFalse(tr_0.nocode)
self.assertFalse(tr_0.passed)
self.assertTrue(tr_0.skipped)
self.assertFalse(tr_0.failed)
self.assertIn('one_plus_one', tr_1.name)
self.assertIn('code-example-1', tr_1.name)
self.assertFalse(tr_1.nocode)
self.assertTrue(tr_1.passed)
self.assertFalse(tr_1.skipped)
self.assertFalse(tr_1.failed)
self.assertIn('one_minus_one', tr_2.name)
self.assertIn('code-example-0', tr_2.name)
self.assertFalse(tr_2.nocode)
self.assertTrue(tr_2.passed)
self.assertFalse(tr_2.skipped)
self.assertFalse(tr_2.failed)
self.assertIn('one_minus_one', tr_3.name)
self.assertIn('code-example-1', tr_3.name)
self.assertFalse(tr_3.nocode)
self.assertFalse(tr_3.passed)
self.assertTrue(tr_3.skipped)
self.assertFalse(tr_3.failed)
def test_run_gpu(self):
_clear_environ()
test_capacity = {'cpu', 'gpu'}
doctester = Xdoctester(style='freeform', target='codeblock')
doctester.prepare(test_capacity)
docstrings_to_test = {
'one_plus_one': """
placeholder
.. code-block:: python
:name: code-example-0
this is some blabla...
>>> # doctest: +REQUIRES(env: GPU)
>>> print(1+1)
2
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> # doctest: +REQUIRES(env:XPU)
>>> print(1-1)
0
""",
'one_minus_one': """
placeholder
.. code-block:: python
:name: code-example-0
this is some blabla...
>>> print(1-1)
2
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> # doctest: +REQUIRES(env:GPU, env: XPU)
>>> print(1-1)
0
""",
}
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 4)
tr_0, tr_1, tr_2, tr_3 = test_results
self.assertIn('one_plus_one', tr_0.name)
self.assertIn('code-example-0', tr_0.name)
self.assertFalse(tr_0.nocode)
self.assertTrue(tr_0.passed)
self.assertFalse(tr_0.skipped)
self.assertFalse(tr_0.failed)
self.assertIn('one_plus_one', tr_1.name)
self.assertIn('code-example-1', tr_1.name)
self.assertFalse(tr_1.nocode)
self.assertFalse(tr_1.passed)
self.assertTrue(tr_1.skipped)
self.assertFalse(tr_1.failed)
self.assertIn('one_minus_one', tr_2.name)
self.assertIn('code-example-0', tr_2.name)
self.assertFalse(tr_2.nocode)
self.assertFalse(tr_2.passed)
self.assertFalse(tr_2.skipped)
self.assertTrue(tr_2.failed)
self.assertIn('one_minus_one', tr_3.name)
self.assertIn('code-example-1', tr_3.name)
self.assertFalse(tr_3.nocode)
self.assertFalse(tr_3.passed)
self.assertTrue(tr_3.skipped)
self.assertFalse(tr_3.failed)
def test_run_xpu_distributed(self):
_clear_environ()
test_capacity = {'cpu', 'xpu'}
doctester = Xdoctester(style='freeform', target='codeblock')
doctester.prepare(test_capacity)
docstrings_to_test = {
'one_plus_one': """
placeholder
.. code-block:: python
:name: code-example-0
this is some blabla...
>>> # doctest: +REQUIRES(env: GPU)
>>> print(1+1)
2
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> # doctest: +REQUIRES(env:XPU)
>>> print(1-1)
0
""",
'one_minus_one': """
placeholder
.. code-block:: python
:name: code-example-0
this is some blabla...
>>> print(1-1)
2
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> # doctest: +REQUIRES(env:CPU, env: XPU)
>>> print(1-1)
0
""",
}
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 4)
tr_0, tr_1, tr_2, tr_3 = test_results
self.assertIn('one_plus_one', tr_0.name)
self.assertIn('code-example-0', tr_0.name)
self.assertFalse(tr_0.nocode)
self.assertFalse(tr_0.passed)
self.assertTrue(tr_0.skipped)
self.assertFalse(tr_0.failed)
self.assertIn('one_plus_one', tr_1.name)
self.assertIn('code-example-1', tr_1.name)
self.assertFalse(tr_1.nocode)
self.assertTrue(tr_1.passed)
self.assertFalse(tr_1.skipped)
self.assertFalse(tr_1.failed)
self.assertIn('one_minus_one', tr_2.name)
self.assertIn('code-example-0', tr_2.name)
self.assertFalse(tr_2.nocode)
self.assertFalse(tr_2.passed)
self.assertFalse(tr_2.skipped)
self.assertTrue(tr_2.failed)
self.assertIn('one_minus_one', tr_3.name)
self.assertIn('code-example-1', tr_3.name)
self.assertFalse(tr_3.nocode)
self.assertTrue(tr_3.passed)
self.assertFalse(tr_3.skipped)
self.assertFalse(tr_3.failed)
def test_style_google(self):
_clear_environ()
test_capacity = {'cpu'}
doctester = Xdoctester(style='google', target='docstring')
doctester.prepare(test_capacity)
docstrings_to_test = {
'one_plus_one': """
placeholder
.. code-block:: python
:name: code-example-0
this is some blabla...
>>> # doctest: +SKIP
>>> print(1+1)
2
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> # doctest: +REQUIRES(env:CPU)
>>> print(1-1)
0
Examples:
.. code-block:: python
:name: code-example-2
>>> print(1+2)
3
""",
'one_minus_one': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> # doctest: +REQUIRES(env:GPU)
>>> print(1-1)
0
Examples:
.. code-block:: python
:name: code-example-2
>>> print(1+1)
3
""",
}
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 4)
tr_0, tr_1, tr_2, tr_3 = test_results
self.assertIn('one_plus_one', tr_0.name)
self.assertNotIn('code-example-1', tr_0.name)
self.assertFalse(tr_0.nocode)
self.assertTrue(tr_0.passed)
self.assertFalse(tr_0.skipped)
self.assertFalse(tr_0.failed)
self.assertIn('one_plus_one', tr_1.name)
self.assertNotIn('code-example-2', tr_1.name)
self.assertFalse(tr_1.nocode)
self.assertTrue(tr_1.passed)
self.assertFalse(tr_1.skipped)
self.assertFalse(tr_1.failed)
self.assertIn('one_minus_one', tr_2.name)
self.assertNotIn('code-example-1', tr_2.name)
self.assertFalse(tr_2.nocode)
self.assertFalse(tr_2.passed)
self.assertTrue(tr_2.skipped)
self.assertFalse(tr_2.failed)
self.assertIn('one_minus_one', tr_3.name)
self.assertNotIn('code-example-2', tr_3.name)
self.assertFalse(tr_3.nocode)
self.assertFalse(tr_3.passed)
self.assertFalse(tr_3.skipped)
self.assertTrue(tr_3.failed)
_clear_environ()
test_capacity = {'cpu', 'gpu'}
doctester = Xdoctester(style='google', target='codeblock')
doctester.prepare(test_capacity)
docstrings_to_test = {
'one_plus_one': """
placeholder
.. code-block:: python
:name: code-example-0
this is some blabla...
>>> # doctest: +SKIP
>>> print(1+1)
2
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> # doctest: +REQUIRES(env:CPU)
>>> print(1-1)
0
Examples:
.. code-block:: python
:name: code-example-2
>>> print(1+2)
3
""",
'one_minus_one': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> # doctest: +REQUIRES(env:GPU)
>>> print(1-1)
0
Examples:
.. code-block:: python
:name: code-example-2
>>> print(1+1)
3
""",
}
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 4)
tr_0, tr_1, tr_2, tr_3 = test_results
self.assertIn('one_plus_one', tr_0.name)
self.assertIn('code-example-1', tr_0.name)
self.assertFalse(tr_0.nocode)
self.assertTrue(tr_0.passed)
self.assertFalse(tr_0.skipped)
self.assertFalse(tr_0.failed)
self.assertIn('one_plus_one', tr_1.name)
self.assertIn('code-example-2', tr_1.name)
self.assertFalse(tr_1.nocode)
self.assertTrue(tr_1.passed)
self.assertFalse(tr_1.skipped)
self.assertFalse(tr_1.failed)
self.assertIn('one_minus_one', tr_2.name)
self.assertIn('code-example-1', tr_2.name)
self.assertFalse(tr_2.nocode)
self.assertTrue(tr_2.passed)
self.assertFalse(tr_2.skipped)
self.assertFalse(tr_2.failed)
self.assertIn('one_minus_one', tr_3.name)
self.assertIn('code-example-2', tr_3.name)
self.assertFalse(tr_3.nocode)
self.assertFalse(tr_3.passed)
self.assertFalse(tr_3.skipped)
self.assertTrue(tr_3.failed)
def test_style_freeform(self):
_clear_environ()
test_capacity = {'cpu'}
doctester = Xdoctester(style='freeform', target='docstring')
doctester.prepare(test_capacity)
docstrings_to_test = {
'one_plus_one': """
placeholder
.. code-block:: python
:name: code-example-0
this is some blabla...
>>> # doctest: +SKIP
>>> print(1+1)
2
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> # doctest: +REQUIRES(env:CPU)
>>> print(1-1)
0
Examples:
.. code-block:: python
:name: code-example-2
>>> print(1+2)
3
""",
'one_minus_one': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> # doctest: +REQUIRES(env:CPU)
>>> print(1-1)
0
Examples:
.. code-block:: python
:name: code-example-2
>>> print(1+1)
3
""",
}
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 2)
tr_0, tr_1 = test_results
self.assertIn('one_plus_one', tr_0.name)
self.assertNotIn('code-example', tr_0.name)
self.assertFalse(tr_0.nocode)
self.assertFalse(tr_0.passed)
self.assertTrue(tr_0.skipped)
self.assertFalse(tr_0.failed)
self.assertIn('one_minus_one', tr_1.name)
self.assertNotIn('code-example', tr_1.name)
self.assertFalse(tr_1.nocode)
self.assertFalse(tr_1.passed)
self.assertFalse(tr_1.skipped)
self.assertTrue(tr_1.failed)
_clear_environ()
test_capacity = {'cpu', 'gpu'}
doctester = Xdoctester(style='freeform', target='codeblock')
doctester.prepare(test_capacity)
docstrings_to_test = {
'one_plus_one': """
placeholder
.. code-block:: python
:name: code-example-0
this is some blabla...
>>> # doctest: +SKIP
>>> print(1+1)
2
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> # doctest: +REQUIRES(env:CPU)
>>> for i in range(2):
... print(i)
0
1
Examples:
.. code-block:: python
:name: code-example-2
>>> print(1+2)
3
""",
'one_minus_one': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> # doctest: +REQUIRES(env:CPU)
>>> print(1-1)
0
Examples:
.. code-block:: python
:name: code-example-2
>>> print(1+1)
3
""",
}
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 5)
tr_0, tr_1, tr_2, tr_3, tr_4 = test_results
self.assertIn('one_plus_one', tr_0.name)
self.assertIn('code-example-0', tr_0.name)
self.assertFalse(tr_0.nocode)
self.assertFalse(tr_0.passed)
self.assertTrue(tr_0.skipped)
self.assertFalse(tr_0.failed)
self.assertIn('one_plus_one', tr_1.name)
self.assertIn('code-example-1', tr_1.name)
self.assertFalse(tr_1.nocode)
self.assertTrue(tr_1.passed)
self.assertFalse(tr_1.skipped)
self.assertFalse(tr_1.failed)
self.assertIn('one_plus_one', tr_2.name)
self.assertIn('code-example-2', tr_2.name)
self.assertFalse(tr_2.nocode)
self.assertTrue(tr_2.passed)
self.assertFalse(tr_2.skipped)
self.assertFalse(tr_2.failed)
self.assertIn('one_minus_one', tr_3.name)
self.assertIn('code-example-1', tr_3.name)
self.assertFalse(tr_3.nocode)
self.assertTrue(tr_3.passed)
self.assertFalse(tr_3.skipped)
self.assertFalse(tr_3.failed)
self.assertIn('one_minus_one', tr_4.name)
self.assertIn('code-example-2', tr_4.name)
self.assertFalse(tr_4.nocode)
self.assertFalse(tr_4.passed)
self.assertFalse(tr_4.skipped)
self.assertTrue(tr_4.failed)
def test_no_code(self):
_clear_environ()
test_capacity = {'cpu'}
doctester = Xdoctester(style='google', target='docstring')
doctester.prepare(test_capacity)
docstrings_to_test = {
'one_plus_one': """
placeholder
.. code-block:: python
:name: code-example-0
this is some blabla...
>>> # doctest: +SKIP
>>> print(1+1)
2
""",
'one_minus_one': """
placeholder
Examples:
""",
}
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 2)
tr_0, tr_1 = test_results
self.assertIn('one_plus_one', tr_0.name)
self.assertNotIn('code-example', tr_0.name)
self.assertTrue(tr_0.nocode)
self.assertFalse(tr_0.passed)
self.assertFalse(tr_0.skipped)
self.assertFalse(tr_0.failed)
self.assertIn('one_minus_one', tr_1.name)
self.assertNotIn('code-example', tr_1.name)
self.assertTrue(tr_1.nocode)
self.assertFalse(tr_1.passed)
self.assertFalse(tr_1.skipped)
self.assertFalse(tr_1.failed)
_clear_environ()
test_capacity = {'cpu'}
doctester = Xdoctester(style='google', target='codeblock')
doctester.prepare(test_capacity)
docstrings_to_test = {
'one_plus_one': """
placeholder
.. code-block:: python
:name: code-example-0
this is some blabla...
>>> # doctest: +SKIP
>>> print(1+1)
2
""",
'one_minus_one': """
placeholder
Examples:
""",
}
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 0)
_clear_environ()
test_capacity = {'cpu'}
doctester = Xdoctester(style='freeform', target='docstring')
doctester.prepare(test_capacity)
docstrings_to_test = {
'one_plus_one': """
placeholder
.. code-block:: python
:name: code-example-0
this is some blabla...
>>> # doctest: +SKIP
>>> print(1+1)
2
""",
'one_minus_one': """
placeholder
Examples:
""",
}
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 2)
tr_0, tr_1 = test_results
self.assertIn('one_plus_one', tr_0.name)
self.assertNotIn('code-example', tr_0.name)
self.assertFalse(tr_0.nocode)
self.assertFalse(tr_0.passed)
self.assertTrue(tr_0.skipped)
self.assertFalse(tr_0.failed)
self.assertIn('one_minus_one', tr_1.name)
self.assertNotIn('code-example', tr_1.name)
self.assertTrue(tr_1.nocode)
self.assertFalse(tr_1.passed)
self.assertFalse(tr_1.skipped)
self.assertFalse(tr_1.failed)
_clear_environ()
test_capacity = {'cpu'}
doctester = Xdoctester(style='freeform', target='codeblock')
doctester.prepare(test_capacity)
docstrings_to_test = {
'one_plus_one': """
placeholder
.. code-block:: python
:name: code-example-0
this is some blabla...
>>> # doctest: +SKIP
>>> print(1+1)
2
""",
'one_minus_one': """
placeholder
Examples:
""",
}
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 1)
tr_0 = test_results[0]
self.assertIn('one_plus_one', tr_0.name)
self.assertIn('code-example', tr_0.name)
self.assertFalse(tr_0.nocode)
self.assertFalse(tr_0.passed)
self.assertTrue(tr_0.skipped)
self.assertFalse(tr_0.failed)
def test_multiprocessing_xdoctester(self):
docstrings_to_test = {
'static_0': """
this is docstring...
Examples:
.. code-block:: python
>>> import numpy as np
>>> import paddle
>>> paddle.enable_static()
>>> data = paddle.static.data(name='X', shape=[None, 2, 28, 28], dtype='float32')
""",
'static_1': """
this is docstring...
Examples:
.. code-block:: python
>>> import numpy as np
>>> import paddle
>>> paddle.enable_static()
>>> data = paddle.static.data(name='X', shape=[None, 1, 28, 28], dtype='float32')
""",
}
_clear_environ()
test_capacity = {'cpu'}
doctester = Xdoctester()
doctester.prepare(test_capacity)
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 2)
tr_0, tr_1 = test_results
self.assertIn('static_0', tr_0.name)
self.assertTrue(tr_0.passed)
self.assertIn('static_1', tr_1.name)
self.assertTrue(tr_1.passed)
_clear_environ()
test_capacity = {'cpu'}
doctester = Xdoctester(use_multiprocessing=False)
doctester.prepare(test_capacity)
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 2)
tr_0, tr_1 = test_results
self.assertIn('static_0', tr_0.name)
self.assertTrue(tr_0.passed)
self.assertIn('static_1', tr_1.name)
self.assertFalse(tr_1.passed)
self.assertTrue(tr_1.failed)
def test_timeout(self):
docstrings_to_test = {
'timeout_false': """
this is docstring...
Examples:
.. code-block:: python
>>> # doctest: +TIMEOUT(2)
>>> import time
>>> time.sleep(0.1)
""",
'timeout_true': """
this is docstring...
Examples:
.. code-block:: python
>>> # doctest: +TIMEOUT(2)
>>> import time
>>> time.sleep(3)
""",
'timeout_false_with_skip_0': """
this is docstring...
Examples:
.. code-block:: python
>>> # doctest: +TIMEOUT(2)
>>> # doctest: +SKIP
>>> import time
>>> time.sleep(0.1)
""",
'timeout_false_with_skip_1': """
this is docstring...
Examples:
.. code-block:: python
>>> # doctest: +SKIP
>>> # doctest: +TIMEOUT(2)
>>> import time
>>> time.sleep(0.1)
""",
'timeout_true_with_skip_0': """
this is docstring...
Examples:
.. code-block:: python
>>> # doctest: +TIMEOUT(2)
>>> # doctest: +SKIP
>>> import time
>>> time.sleep(3)
""",
'timeout_true_with_skip_1': """
this is docstring...
Examples:
.. code-block:: python
>>> # doctest: +SKIP
>>> # doctest: +TIMEOUT(2)
>>> import time
>>> time.sleep(3)
""",
'timeout_more_codes': """
this is docstring...
Examples:
.. code-block:: python
>>> # doctest: +TIMEOUT(2)
>>> import time
>>> time.sleep(0.1)
.. code-block:: python
>>> # doctest: +TIMEOUT(2)
>>> import time
>>> time.sleep(3)
""",
}
_clear_environ()
test_capacity = {'cpu'}
doctester = Xdoctester()
doctester.prepare(test_capacity)
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 8)
tr_0, tr_1, tr_2, tr_3, tr_4, tr_5, tr_6, tr_7 = test_results
self.assertIn('timeout_false', tr_0.name)
self.assertTrue(tr_0.passed)
self.assertFalse(tr_0.timeout)
self.assertIn('timeout_true', tr_1.name)
self.assertFalse(tr_1.passed)
self.assertTrue(tr_1.timeout)
self.assertIn('timeout_false_with_skip_0', tr_2.name)
self.assertFalse(tr_2.passed)
self.assertFalse(tr_2.timeout)
self.assertTrue(tr_2.skipped)
self.assertIn('timeout_false_with_skip_1', tr_3.name)
self.assertFalse(tr_3.passed)
self.assertFalse(tr_3.timeout)
self.assertTrue(tr_3.skipped)
self.assertIn('timeout_true_with_skip_0', tr_4.name)
self.assertFalse(tr_4.passed)
self.assertFalse(tr_4.timeout)
self.assertTrue(tr_4.skipped)
self.assertIn('timeout_true_with_skip_1', tr_5.name)
self.assertFalse(tr_5.passed)
self.assertFalse(tr_5.timeout)
self.assertTrue(tr_5.skipped)
self.assertIn('timeout_more_codes', tr_6.name)
self.assertTrue(tr_6.passed)
self.assertFalse(tr_6.timeout)
self.assertIn('timeout_more_codes', tr_7.name)
self.assertFalse(tr_7.passed)
self.assertTrue(tr_7.timeout)
_clear_environ()
test_capacity = {'cpu'}
doctester = Xdoctester(use_multiprocessing=False)
doctester.prepare(test_capacity)
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 8)
tr_0, tr_1, tr_2, tr_3, tr_4, tr_5, tr_6, tr_7 = test_results
self.assertIn('timeout_false', tr_0.name)
self.assertTrue(tr_0.passed)
self.assertFalse(tr_0.timeout)
self.assertIn('timeout_true', tr_1.name)
self.assertFalse(tr_1.passed)
self.assertTrue(tr_1.timeout)
self.assertIn('timeout_false_with_skip_0', tr_2.name)
self.assertFalse(tr_2.passed)
self.assertFalse(tr_2.timeout)
self.assertTrue(tr_2.skipped)
self.assertIn('timeout_false_with_skip_1', tr_3.name)
self.assertFalse(tr_3.passed)
self.assertFalse(tr_3.timeout)
self.assertTrue(tr_3.skipped)
self.assertIn('timeout_true_with_skip_0', tr_4.name)
self.assertFalse(tr_4.passed)
self.assertFalse(tr_4.timeout)
self.assertTrue(tr_4.skipped)
self.assertIn('timeout_true_with_skip_1', tr_5.name)
self.assertFalse(tr_5.passed)
self.assertFalse(tr_5.timeout)
self.assertTrue(tr_5.skipped)
self.assertIn('timeout_more_codes', tr_6.name)
self.assertTrue(tr_6.passed)
self.assertFalse(tr_6.timeout)
self.assertIn('timeout_more_codes', tr_7.name)
self.assertFalse(tr_7.passed)
self.assertTrue(tr_7.timeout)
if __name__ == '__main__':
unittest.main()
#! python
# 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 importlib
import os
import unittest
import xdoctest
from sampcd_processor_utils import get_test_results
from sampcd_processor_xdoctest import Xdoctester
def _clear_environ():
for k in {'CPU', 'GPU', 'XPU', 'DISTRIBUTED'}:
if k in os.environ:
del os.environ[k]
class TestXdoctester(unittest.TestCase):
def setUp(self):
self.maxDiff = None
def test_init(self):
doctester = Xdoctester()
self.assertEqual(doctester.debug, False)
self.assertEqual(doctester.style, 'freeform')
self.assertEqual(doctester.target, 'codeblock')
self.assertEqual(doctester.mode, 'native')
doctester = Xdoctester(analysis='static')
self.assertEqual(doctester.config['analysis'], 'static')
def test_convert_directive(self):
doctester = Xdoctester()
docstring_input = "# doctest: -SKIP\n"
docstring_output = doctester.convert_directive(docstring_input)
docstring_target = "# xdoctest: -SKIP\n"
self.assertEqual(docstring_output, docstring_target)
docstring_input = '# doctest: +SKIP("skip this test...")\n'
docstring_output = doctester.convert_directive(docstring_input)
docstring_target = '# xdoctest: +SKIP("skip this test...")\n'
self.assertEqual(docstring_output, docstring_target)
docstring_input = """
placeholder
.. code-block:: python
:name: code-example-0
this is some blabla...
>>> # doctest: +SKIP
>>> print(1+1)
2
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> # doctest: -REQUIRES(env:GPU)
>>> print(1-1)
0
.. code-block:: python
:name: code-example-2
this is some blabla...
>>> # doctest: +REQUIRES(env:GPU, env:XPU, env: DISTRIBUTED)
>>> print(1-1)
0
"""
docstring_output = doctester.convert_directive(docstring_input)
docstring_target = """
placeholder
.. code-block:: python
:name: code-example-0
this is some blabla...
>>> # xdoctest: +SKIP
>>> print(1+1)
2
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> # xdoctest: -REQUIRES(env:GPU)
>>> print(1-1)
0
.. code-block:: python
:name: code-example-2
this is some blabla...
>>> # xdoctest: +REQUIRES(env:GPU, env:XPU, env: DISTRIBUTED)
>>> print(1-1)
0
"""
self.assertEqual(docstring_output, docstring_target)
def test_prepare(self):
doctester = Xdoctester()
_clear_environ()
test_capacity = {'cpu'}
doctester.prepare(test_capacity)
self.assertTrue(os.environ['CPU'])
self.assertFalse(os.environ.get('GPU'))
_clear_environ()
test_capacity = {'cpu', 'gpu'}
doctester.prepare(test_capacity)
self.assertTrue(os.environ['CPU'])
self.assertTrue(os.environ['GPU'])
self.assertFalse(os.environ.get('cpu'))
self.assertFalse(os.environ.get('gpu'))
self.assertFalse(os.environ.get('XPU'))
_clear_environ()
class TestGetTestResults(unittest.TestCase):
def test_global_exec(self):
_clear_environ()
# test set_default_dtype
docstrings_to_test = {
'before_set_default': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> a = paddle.to_tensor(.2)
>>> print(a)
Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
[0.20000000])
""",
'set_default': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.set_default_dtype('float64')
>>> a = paddle.to_tensor(.2)
>>> print(a)
Tensor(shape=[1], dtype=float64, place=Place(cpu), stop_gradient=True,
[0.20000000])
""",
'after_set_default': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> a = paddle.to_tensor(.2)
>>> print(a)
Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
[0.20000000])
""",
}
# test old global_exec
test_capacity = {'cpu'}
doctester = Xdoctester(
style='freeform',
target='codeblock',
global_exec=r"\n".join(
[
"import paddle",
"paddle.device.set_device('cpu')",
]
),
)
doctester.prepare(test_capacity)
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 3)
tr_0, tr_1, tr_2 = test_results
self.assertIn('before_set_default', tr_0.name)
self.assertTrue(tr_0.passed)
self.assertIn('set_default', tr_1.name)
self.assertTrue(tr_1.passed)
# tr_2 is passed, because of multiprocessing
self.assertIn('after_set_default', tr_2.name)
self.assertTrue(tr_2.passed)
# test new default global_exec
doctester = Xdoctester(
style='freeform',
target='codeblock',
)
doctester.prepare(test_capacity)
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 3)
tr_0, tr_1, tr_2 = test_results
self.assertIn('before_set_default', tr_0.name)
self.assertTrue(tr_0.passed)
self.assertIn('set_default', tr_1.name)
self.assertTrue(tr_1.passed)
self.assertIn('after_set_default', tr_2.name)
self.assertTrue(tr_2.passed)
# test disable static
docstrings_to_test = {
'before_enable_static': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> print(paddle.in_dynamic_mode())
True
""",
'enable_static': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.enable_static()
>>> print(paddle.in_dynamic_mode())
False
""",
'after_enable_static': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> print(paddle.in_dynamic_mode())
True
""",
}
# test old global_exec
test_capacity = {'cpu'}
doctester = Xdoctester(
style='freeform',
target='codeblock',
global_exec=r"\n".join(
[
"import paddle",
"paddle.device.set_device('cpu')",
]
),
)
doctester.prepare(test_capacity)
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 3)
tr_0, tr_1, tr_2 = test_results
self.assertIn('before_enable_static', tr_0.name)
self.assertTrue(tr_0.passed)
self.assertIn('enable_static', tr_1.name)
self.assertTrue(tr_1.passed)
# tr_2 is passed, because of multiprocessing
self.assertIn('after_enable_static', tr_2.name)
self.assertTrue(tr_2.passed)
# test new default global_exec
doctester = Xdoctester(
style='freeform',
target='codeblock',
)
doctester.prepare(test_capacity)
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 3)
tr_0, tr_1, tr_2 = test_results
self.assertIn('before_enable_static', tr_0.name)
self.assertTrue(tr_0.passed)
self.assertIn('enable_static', tr_1.name)
self.assertTrue(tr_1.passed)
self.assertIn('after_enable_static', tr_2.name)
self.assertTrue(tr_2.passed)
def test_patch_xdoctest(self):
# test patch tensor place
_clear_environ()
test_capacity = {'cpu'}
doctester = Xdoctester(style='freeform', target='codeblock')
doctester.prepare(test_capacity)
docstrings_to_test = {
'gpu_to_gpu': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('gpu')
>>> a = paddle.to_tensor(.2)
>>> # Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True, [0.20000000])
>>> print(a)
Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
[0.20000000])
""",
'cpu_to_cpu': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('cpu')
>>> a = paddle.to_tensor(.2)
>>> # Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True, [0.20000000])
>>> print(a)
Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
[0.20000000])
""",
'gpu_to_cpu': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('gpu')
>>> a = paddle.to_tensor(.2)
>>> # Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True, [0.20000000])
>>> print(a)
Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
[0.20000000])
""",
'cpu_to_gpu': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('cpu')
>>> a = paddle.to_tensor(.2)
>>> # Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True, [0.20000000])
>>> print(a)
Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
[0.20000000])
""",
'gpu_to_cpu_array': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('gpu')
>>> a = paddle.to_tensor([[1,2,3], [2,3,4], [3,4,5]])
>>> # Tensor(shape=[3, 3], dtype=int64, place=Place(gpu:0), stop_gradient=True,
>>> # [[1, 2, 3],
>>> # [2, 3, 4],
>>> # [3, 4, 5]])
>>> print(a)
Tensor(shape=[3, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
[[1, 2, 3],
[2, 3, 4],
[3, 4, 5]])
""",
'cpu_to_gpu_array': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('cpu')
>>> a = paddle.to_tensor([[1,2,3], [2,3,4], [3,4,5]])
>>> # Tensor(shape=[3, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
>>> # [[1, 2, 3],
>>> # [2, 3, 4],
>>> # [3, 4, 5]])
>>> print(a)
Tensor(shape=[3, 3], dtype=int64, place=Place(gpu:0), stop_gradient=True,
[[1, 2, 3],
[2, 3, 4],
[3, 4, 5]])
""",
}
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 6)
tr_0, tr_1, tr_2, tr_3, tr_4, tr_5 = test_results
self.assertIn('gpu_to_gpu', tr_0.name)
self.assertTrue(tr_0.passed)
self.assertIn('cpu_to_cpu', tr_1.name)
self.assertTrue(tr_1.passed)
self.assertIn('gpu_to_cpu', tr_2.name)
self.assertTrue(tr_2.passed)
self.assertIn('cpu_to_gpu', tr_3.name)
self.assertTrue(tr_3.passed)
self.assertIn('gpu_to_cpu_array', tr_4.name)
self.assertTrue(tr_4.passed)
self.assertIn('cpu_to_gpu_array', tr_5.name)
self.assertTrue(tr_5.passed)
# reload xdoctest.checker
importlib.reload(xdoctest.checker)
_clear_environ()
test_capacity = {'cpu'}
doctester = Xdoctester(
style='freeform', target='codeblock', patch_tensor_place=False
)
doctester.prepare(test_capacity)
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 6)
tr_0, tr_1, tr_2, tr_3, tr_4, tr_5 = test_results
self.assertIn('gpu_to_gpu', tr_0.name)
self.assertTrue(tr_0.passed)
self.assertIn('cpu_to_cpu', tr_1.name)
self.assertTrue(tr_1.passed)
self.assertIn('gpu_to_cpu', tr_2.name)
self.assertFalse(tr_2.passed)
self.assertIn('cpu_to_gpu', tr_3.name)
self.assertFalse(tr_3.passed)
self.assertIn('gpu_to_cpu_array', tr_4.name)
self.assertFalse(tr_4.passed)
self.assertIn('cpu_to_gpu_array', tr_5.name)
self.assertFalse(tr_5.passed)
# test patch float precision
# reload xdoctest.checker
importlib.reload(xdoctest.checker)
_clear_environ()
test_capacity = {'cpu'}
doctester = Xdoctester(style='freeform', target='codeblock')
doctester.prepare(test_capacity)
docstrings_to_test = {
'gpu_to_gpu': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('gpu')
>>> a = paddle.to_tensor(.123456789)
>>> print(a)
Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
[0.123456780])
""",
'cpu_to_cpu': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('cpu')
>>> a = paddle.to_tensor(.123456789)
>>> print(a)
Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
[0.123456780])
""",
'gpu_to_cpu': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('gpu')
>>> a = paddle.to_tensor(.123456789)
>>> print(a)
Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
[0.123456780])
""",
'cpu_to_gpu': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('cpu')
>>> a = paddle.to_tensor(.123456789)
>>> print(a)
Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
[0.123456780])
""",
'gpu_to_cpu_array': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('gpu')
>>> a = paddle.to_tensor([[1.123456789 ,2,3], [2,3,4], [3,4,5]])
>>> print(a)
Tensor(shape=[3, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[1.123456780, 2., 3.],
[2., 3., 4.],
[3., 4., 5.]])
""",
'cpu_to_gpu_array': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('cpu')
>>> a = paddle.to_tensor([[1.123456789,2,3], [2,3,4], [3,4,5]])
>>> print(a)
Tensor(shape=[3, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
[[1.123456780, 2., 3.],
[2., 3., 4.],
[3., 4., 5.]])
""",
'mass_array': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('gpu')
>>> a = paddle.to_tensor(
... [[1.123456780, 2., -3, .3],
... [2, 3, +4., 1.2+10.34e-5j],
... [3, 5.e-3, 1e2, 3e-8]]
... )
>>> # Tensor(shape=[3, 4], dtype=complex64, place=Place(gpu:0), stop_gradient=True,
>>> # [[ (1.1234568357467651+0j) ,
>>> # (2+0j) ,
>>> # (-3+0j) ,
>>> # (0.30000001192092896+0j) ],
>>> # [ (2+0j) ,
>>> # (3+0j) ,
>>> # (4+0j) ,
>>> # (1.2000000476837158+0.00010340000153519213j)],
>>> # [ (3+0j) ,
>>> # (0.004999999888241291+0j) ,
>>> # (100+0j) ,
>>> # (2.999999892949745e-08+0j) ]])
>>> print(a)
Tensor(shape=[3, 4], dtype=complex64, place=Place(AAA), stop_gradient=True,
[[ (1.123456+0j),
(2+0j),
(-3+0j),
(0.3+0j)],
[ (2+0j),
(3+0j),
(4+0j),
(1.2+0.00010340j)],
[ (3+0j),
(0.00499999+0j),
(100+0j),
(2.999999e-08+0j)]])
""",
'float_array': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('cpu')
>>> x = [[2, 3, 4], [7, 8, 9]]
>>> x = paddle.to_tensor(x, dtype='float32')
>>> print(paddle.log(x))
Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[0.69314718, 1.09861231, 1.38629436],
[1.94591010, 2.07944155, 2.19722462]])
""",
'float_array_diff': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('cpu')
>>> x = [[2, 3, 4], [7, 8, 9]]
>>> x = paddle.to_tensor(x, dtype='float32')
>>> print(paddle.log(x))
Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[0.69314712, 1.09861221, 1.386294],
[1.94591032, 2.07944156, 2.1972246]])
""",
}
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 9)
tr_0, tr_1, tr_2, tr_3, tr_4, tr_5, tr_6, tr_7, tr_8 = test_results
self.assertIn('gpu_to_gpu', tr_0.name)
self.assertTrue(tr_0.passed)
self.assertIn('cpu_to_cpu', tr_1.name)
self.assertTrue(tr_1.passed)
self.assertIn('gpu_to_cpu', tr_2.name)
self.assertTrue(tr_2.passed)
self.assertIn('cpu_to_gpu', tr_3.name)
self.assertTrue(tr_3.passed)
self.assertIn('gpu_to_cpu_array', tr_4.name)
self.assertTrue(tr_4.passed)
self.assertIn('cpu_to_gpu_array', tr_5.name)
self.assertTrue(tr_5.passed)
self.assertIn('mass_array', tr_6.name)
self.assertTrue(tr_6.passed)
self.assertIn('float_array', tr_7.name)
self.assertTrue(tr_7.passed)
self.assertIn('float_array_diff', tr_8.name)
self.assertTrue(tr_8.passed)
# reload xdoctest.checker
importlib.reload(xdoctest.checker)
_clear_environ()
test_capacity = {'cpu'}
doctester = Xdoctester(
style='freeform', target='codeblock', patch_float_precision=None
)
doctester.prepare(test_capacity)
docstrings_to_test = {
'gpu_to_gpu': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('gpu')
>>> a = paddle.to_tensor(.123456789)
>>> print(a)
Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
[0.123456780])
""",
'cpu_to_cpu': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('cpu')
>>> a = paddle.to_tensor(.123456789)
>>> print(a)
Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
[0.123456780])
""",
'gpu_to_cpu': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('gpu')
>>> a = paddle.to_tensor(.123456789)
>>> print(a)
Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
[0.123456780])
""",
'cpu_to_gpu': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('cpu')
>>> a = paddle.to_tensor(.123456789)
>>> print(a)
Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
[0.123456780])
""",
'gpu_to_cpu_array': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('gpu')
>>> a = paddle.to_tensor([[1.123456789 ,2,3], [2,3,4], [3,4,5]])
>>> print(a)
Tensor(shape=[3, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[1.123456780, 2., 3.],
[2., 3., 4.],
[3., 4., 5.]])
""",
'cpu_to_gpu_array': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('cpu')
>>> a = paddle.to_tensor([[1.123456789,2,3], [2,3,4], [3,4,5]])
>>> print(a)
Tensor(shape=[3, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
[[1.123456780, 2., 3.],
[2., 3., 4.],
[3., 4., 5.]])
""",
'mass_array': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('gpu')
>>> a = paddle.to_tensor(
... [[1.123456780, 2., -3, .3],
... [2, 3, +4., 1.2+10.34e-5j],
... [3, 5.e-3, 1e2, 3e-8]]
... )
>>> # Tensor(shape=[3, 4], dtype=complex64, place=Place(gpu:0), stop_gradient=True,
>>> # [[ (1.1234568357467651+0j) ,
>>> # (2+0j) ,
>>> # (-3+0j) ,
>>> # (0.30000001192092896+0j) ],
>>> # [ (2+0j) ,
>>> # (3+0j) ,
>>> # (4+0j) ,
>>> # (1.2000000476837158+0.00010340000153519213j)],
>>> # [ (3+0j) ,
>>> # (0.004999999888241291+0j) ,
>>> # (100+0j) ,
>>> # (2.999999892949745e-08+0j) ]])
>>> print(a)
Tensor(shape=[3, 4], dtype=complex64, place=Place(AAA), stop_gradient=True,
[[ (1.123456+0j),
(2+0j),
(-3+0j),
(0.3+0j)],
[ (2+0j),
(3+0j),
(4+0j),
(1.2+0.00010340j)],
[ (3+0j),
(0.00499999+0j),
(100+0j),
(2.999999e-08+0j)]])
""",
'float_array': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('cpu')
>>> x = [[2, 3, 4], [7, 8, 9]]
>>> x = paddle.to_tensor(x, dtype='float32')
>>> print(paddle.log(x))
Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[0.69314718, 1.09861231, 1.38629436],
[1.94591010, 2.07944155, 2.19722462]])
""",
'float_array_diff': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> import paddle
>>> paddle.device.set_device('cpu')
>>> x = [[2, 3, 4], [7, 8, 9]]
>>> x = paddle.to_tensor(x, dtype='float32')
>>> print(paddle.log(x))
Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[0.69314712, 1.09861221, 1.386294],
[1.94591032, 2.07944156, 2.1972246]])
""",
}
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 9)
tr_0, tr_1, tr_2, tr_3, tr_4, tr_5, tr_6, tr_7, tr_8 = test_results
self.assertIn('gpu_to_gpu', tr_0.name)
self.assertFalse(tr_0.passed)
self.assertIn('cpu_to_cpu', tr_1.name)
self.assertFalse(tr_1.passed)
self.assertIn('gpu_to_cpu', tr_2.name)
self.assertFalse(tr_2.passed)
self.assertIn('cpu_to_gpu', tr_3.name)
self.assertFalse(tr_3.passed)
self.assertIn('gpu_to_cpu_array', tr_4.name)
self.assertFalse(tr_4.passed)
self.assertIn('cpu_to_gpu_array', tr_5.name)
self.assertFalse(tr_5.passed)
self.assertIn('mass_array', tr_6.name)
self.assertFalse(tr_6.passed)
self.assertIn('float_array', tr_7.name)
self.assertTrue(tr_7.passed)
self.assertIn('float_array_diff', tr_8.name)
self.assertFalse(tr_8.passed)
def test_run_cpu(self):
_clear_environ()
test_capacity = {'cpu'}
doctester = Xdoctester(style='freeform', target='codeblock')
doctester.prepare(test_capacity)
docstrings_to_test = {
'one_plus_one': """
placeholder
.. code-block:: python
:name: code-example-0
this is some blabla...
>>> # doctest: +SKIP
>>> print(1+1)
2
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> # doctest: +REQUIRES(env:CPU)
>>> print(1-1)
0
""",
'one_minus_one': """
placeholder
.. code-block:: python
:name: code-example-0
this is some blabla...
>>> print(1+1)
2
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> # doctest: +REQUIRES(env:GPU)
>>> print(1-1)
0
""",
}
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 4)
tr_0, tr_1, tr_2, tr_3 = test_results
self.assertIn('one_plus_one', tr_0.name)
self.assertIn('code-example-0', tr_0.name)
self.assertFalse(tr_0.nocode)
self.assertFalse(tr_0.passed)
self.assertTrue(tr_0.skipped)
self.assertFalse(tr_0.failed)
self.assertIn('one_plus_one', tr_1.name)
self.assertIn('code-example-1', tr_1.name)
self.assertFalse(tr_1.nocode)
self.assertTrue(tr_1.passed)
self.assertFalse(tr_1.skipped)
self.assertFalse(tr_1.failed)
self.assertIn('one_minus_one', tr_2.name)
self.assertIn('code-example-0', tr_2.name)
self.assertFalse(tr_2.nocode)
self.assertTrue(tr_2.passed)
self.assertFalse(tr_2.skipped)
self.assertFalse(tr_2.failed)
self.assertIn('one_minus_one', tr_3.name)
self.assertIn('code-example-1', tr_3.name)
self.assertFalse(tr_3.nocode)
self.assertFalse(tr_3.passed)
self.assertTrue(tr_3.skipped)
self.assertFalse(tr_3.failed)
def test_run_gpu(self):
_clear_environ()
test_capacity = {'cpu', 'gpu'}
doctester = Xdoctester(style='freeform', target='codeblock')
doctester.prepare(test_capacity)
docstrings_to_test = {
'one_plus_one': """
placeholder
.. code-block:: python
:name: code-example-0
this is some blabla...
>>> # doctest: +REQUIRES(env: GPU)
>>> print(1+1)
2
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> # doctest: +REQUIRES(env:XPU)
>>> print(1-1)
0
""",
'one_minus_one': """
placeholder
.. code-block:: python
:name: code-example-0
this is some blabla...
>>> print(1-1)
2
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> # doctest: +REQUIRES(env:GPU, env: XPU)
>>> print(1-1)
0
""",
}
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 4)
tr_0, tr_1, tr_2, tr_3 = test_results
self.assertIn('one_plus_one', tr_0.name)
self.assertIn('code-example-0', tr_0.name)
self.assertFalse(tr_0.nocode)
self.assertTrue(tr_0.passed)
self.assertFalse(tr_0.skipped)
self.assertFalse(tr_0.failed)
self.assertIn('one_plus_one', tr_1.name)
self.assertIn('code-example-1', tr_1.name)
self.assertFalse(tr_1.nocode)
self.assertFalse(tr_1.passed)
self.assertTrue(tr_1.skipped)
self.assertFalse(tr_1.failed)
self.assertIn('one_minus_one', tr_2.name)
self.assertIn('code-example-0', tr_2.name)
self.assertFalse(tr_2.nocode)
self.assertFalse(tr_2.passed)
self.assertFalse(tr_2.skipped)
self.assertTrue(tr_2.failed)
self.assertIn('one_minus_one', tr_3.name)
self.assertIn('code-example-1', tr_3.name)
self.assertFalse(tr_3.nocode)
self.assertFalse(tr_3.passed)
self.assertTrue(tr_3.skipped)
self.assertFalse(tr_3.failed)
def test_run_xpu_distributed(self):
_clear_environ()
test_capacity = {'cpu', 'xpu'}
doctester = Xdoctester(style='freeform', target='codeblock')
doctester.prepare(test_capacity)
docstrings_to_test = {
'one_plus_one': """
placeholder
.. code-block:: python
:name: code-example-0
this is some blabla...
>>> # doctest: +REQUIRES(env: GPU)
>>> print(1+1)
2
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> # doctest: +REQUIRES(env:XPU)
>>> print(1-1)
0
""",
'one_minus_one': """
placeholder
.. code-block:: python
:name: code-example-0
this is some blabla...
>>> print(1-1)
2
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> # doctest: +REQUIRES(env:CPU, env: XPU)
>>> print(1-1)
0
""",
}
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 4)
tr_0, tr_1, tr_2, tr_3 = test_results
self.assertIn('one_plus_one', tr_0.name)
self.assertIn('code-example-0', tr_0.name)
self.assertFalse(tr_0.nocode)
self.assertFalse(tr_0.passed)
self.assertTrue(tr_0.skipped)
self.assertFalse(tr_0.failed)
self.assertIn('one_plus_one', tr_1.name)
self.assertIn('code-example-1', tr_1.name)
self.assertFalse(tr_1.nocode)
self.assertTrue(tr_1.passed)
self.assertFalse(tr_1.skipped)
self.assertFalse(tr_1.failed)
self.assertIn('one_minus_one', tr_2.name)
self.assertIn('code-example-0', tr_2.name)
self.assertFalse(tr_2.nocode)
self.assertFalse(tr_2.passed)
self.assertFalse(tr_2.skipped)
self.assertTrue(tr_2.failed)
self.assertIn('one_minus_one', tr_3.name)
self.assertIn('code-example-1', tr_3.name)
self.assertFalse(tr_3.nocode)
self.assertTrue(tr_3.passed)
self.assertFalse(tr_3.skipped)
self.assertFalse(tr_3.failed)
def test_style_google(self):
_clear_environ()
test_capacity = {'cpu'}
doctester = Xdoctester(style='google', target='docstring')
doctester.prepare(test_capacity)
docstrings_to_test = {
'one_plus_one': """
placeholder
.. code-block:: python
:name: code-example-0
this is some blabla...
>>> # doctest: +SKIP
>>> print(1+1)
2
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> # doctest: +REQUIRES(env:CPU)
>>> print(1-1)
0
Examples:
.. code-block:: python
:name: code-example-2
>>> print(1+2)
3
""",
'one_minus_one': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> # doctest: +REQUIRES(env:GPU)
>>> print(1-1)
0
Examples:
.. code-block:: python
:name: code-example-2
>>> print(1+1)
3
""",
}
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 4)
tr_0, tr_1, tr_2, tr_3 = test_results
self.assertIn('one_plus_one', tr_0.name)
self.assertNotIn('code-example-1', tr_0.name)
self.assertFalse(tr_0.nocode)
self.assertTrue(tr_0.passed)
self.assertFalse(tr_0.skipped)
self.assertFalse(tr_0.failed)
self.assertIn('one_plus_one', tr_1.name)
self.assertNotIn('code-example-2', tr_1.name)
self.assertFalse(tr_1.nocode)
self.assertTrue(tr_1.passed)
self.assertFalse(tr_1.skipped)
self.assertFalse(tr_1.failed)
self.assertIn('one_minus_one', tr_2.name)
self.assertNotIn('code-example-1', tr_2.name)
self.assertFalse(tr_2.nocode)
self.assertFalse(tr_2.passed)
self.assertTrue(tr_2.skipped)
self.assertFalse(tr_2.failed)
self.assertIn('one_minus_one', tr_3.name)
self.assertNotIn('code-example-2', tr_3.name)
self.assertFalse(tr_3.nocode)
self.assertFalse(tr_3.passed)
self.assertFalse(tr_3.skipped)
self.assertTrue(tr_3.failed)
_clear_environ()
test_capacity = {'cpu', 'gpu'}
doctester = Xdoctester(style='google', target='codeblock')
doctester.prepare(test_capacity)
docstrings_to_test = {
'one_plus_one': """
placeholder
.. code-block:: python
:name: code-example-0
this is some blabla...
>>> # doctest: +SKIP
>>> print(1+1)
2
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> # doctest: +REQUIRES(env:CPU)
>>> print(1-1)
0
Examples:
.. code-block:: python
:name: code-example-2
>>> print(1+2)
3
""",
'one_minus_one': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> # doctest: +REQUIRES(env:GPU)
>>> print(1-1)
0
Examples:
.. code-block:: python
:name: code-example-2
>>> print(1+1)
3
""",
}
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 4)
tr_0, tr_1, tr_2, tr_3 = test_results
self.assertIn('one_plus_one', tr_0.name)
self.assertIn('code-example-1', tr_0.name)
self.assertFalse(tr_0.nocode)
self.assertTrue(tr_0.passed)
self.assertFalse(tr_0.skipped)
self.assertFalse(tr_0.failed)
self.assertIn('one_plus_one', tr_1.name)
self.assertIn('code-example-2', tr_1.name)
self.assertFalse(tr_1.nocode)
self.assertTrue(tr_1.passed)
self.assertFalse(tr_1.skipped)
self.assertFalse(tr_1.failed)
self.assertIn('one_minus_one', tr_2.name)
self.assertIn('code-example-1', tr_2.name)
self.assertFalse(tr_2.nocode)
self.assertTrue(tr_2.passed)
self.assertFalse(tr_2.skipped)
self.assertFalse(tr_2.failed)
self.assertIn('one_minus_one', tr_3.name)
self.assertIn('code-example-2', tr_3.name)
self.assertFalse(tr_3.nocode)
self.assertFalse(tr_3.passed)
self.assertFalse(tr_3.skipped)
self.assertTrue(tr_3.failed)
def test_style_freeform(self):
_clear_environ()
test_capacity = {'cpu'}
doctester = Xdoctester(style='freeform', target='docstring')
doctester.prepare(test_capacity)
docstrings_to_test = {
'one_plus_one': """
placeholder
.. code-block:: python
:name: code-example-0
this is some blabla...
>>> # doctest: +SKIP
>>> print(1+1)
2
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> # doctest: +REQUIRES(env:CPU)
>>> print(1-1)
0
Examples:
.. code-block:: python
:name: code-example-2
>>> print(1+2)
3
""",
'one_minus_one': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> # doctest: +REQUIRES(env:CPU)
>>> print(1-1)
0
Examples:
.. code-block:: python
:name: code-example-2
>>> print(1+1)
3
""",
}
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 2)
tr_0, tr_1 = test_results
self.assertIn('one_plus_one', tr_0.name)
self.assertNotIn('code-example', tr_0.name)
self.assertFalse(tr_0.nocode)
self.assertFalse(tr_0.passed)
self.assertTrue(tr_0.skipped)
self.assertFalse(tr_0.failed)
self.assertIn('one_minus_one', tr_1.name)
self.assertNotIn('code-example', tr_1.name)
self.assertFalse(tr_1.nocode)
self.assertFalse(tr_1.passed)
self.assertFalse(tr_1.skipped)
self.assertTrue(tr_1.failed)
_clear_environ()
test_capacity = {'cpu', 'gpu'}
doctester = Xdoctester(style='freeform', target='codeblock')
doctester.prepare(test_capacity)
docstrings_to_test = {
'one_plus_one': """
placeholder
.. code-block:: python
:name: code-example-0
this is some blabla...
>>> # doctest: +SKIP
>>> print(1+1)
2
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> # doctest: +REQUIRES(env:CPU)
>>> for i in range(2):
... print(i)
0
1
Examples:
.. code-block:: python
:name: code-example-2
>>> print(1+2)
3
""",
'one_minus_one': """
placeholder
Examples:
.. code-block:: python
:name: code-example-1
this is some blabla...
>>> # doctest: +REQUIRES(env:CPU)
>>> print(1-1)
0
Examples:
.. code-block:: python
:name: code-example-2
>>> print(1+1)
3
""",
}
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 5)
tr_0, tr_1, tr_2, tr_3, tr_4 = test_results
self.assertIn('one_plus_one', tr_0.name)
self.assertIn('code-example-0', tr_0.name)
self.assertFalse(tr_0.nocode)
self.assertFalse(tr_0.passed)
self.assertTrue(tr_0.skipped)
self.assertFalse(tr_0.failed)
self.assertIn('one_plus_one', tr_1.name)
self.assertIn('code-example-1', tr_1.name)
self.assertFalse(tr_1.nocode)
self.assertTrue(tr_1.passed)
self.assertFalse(tr_1.skipped)
self.assertFalse(tr_1.failed)
self.assertIn('one_plus_one', tr_2.name)
self.assertIn('code-example-2', tr_2.name)
self.assertFalse(tr_2.nocode)
self.assertTrue(tr_2.passed)
self.assertFalse(tr_2.skipped)
self.assertFalse(tr_2.failed)
self.assertIn('one_minus_one', tr_3.name)
self.assertIn('code-example-1', tr_3.name)
self.assertFalse(tr_3.nocode)
self.assertTrue(tr_3.passed)
self.assertFalse(tr_3.skipped)
self.assertFalse(tr_3.failed)
self.assertIn('one_minus_one', tr_4.name)
self.assertIn('code-example-2', tr_4.name)
self.assertFalse(tr_4.nocode)
self.assertFalse(tr_4.passed)
self.assertFalse(tr_4.skipped)
self.assertTrue(tr_4.failed)
def test_no_code(self):
_clear_environ()
test_capacity = {'cpu'}
doctester = Xdoctester(style='google', target='docstring')
doctester.prepare(test_capacity)
docstrings_to_test = {
'one_plus_one': """
placeholder
.. code-block:: python
:name: code-example-0
this is some blabla...
>>> # doctest: +SKIP
>>> print(1+1)
2
""",
'one_minus_one': """
placeholder
Examples:
""",
}
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 2)
tr_0, tr_1 = test_results
self.assertIn('one_plus_one', tr_0.name)
self.assertNotIn('code-example', tr_0.name)
self.assertTrue(tr_0.nocode)
self.assertFalse(tr_0.passed)
self.assertFalse(tr_0.skipped)
self.assertFalse(tr_0.failed)
self.assertIn('one_minus_one', tr_1.name)
self.assertNotIn('code-example', tr_1.name)
self.assertTrue(tr_1.nocode)
self.assertFalse(tr_1.passed)
self.assertFalse(tr_1.skipped)
self.assertFalse(tr_1.failed)
_clear_environ()
test_capacity = {'cpu'}
doctester = Xdoctester(style='google', target='codeblock')
doctester.prepare(test_capacity)
docstrings_to_test = {
'one_plus_one': """
placeholder
.. code-block:: python
:name: code-example-0
this is some blabla...
>>> # doctest: +SKIP
>>> print(1+1)
2
""",
'one_minus_one': """
placeholder
Examples:
""",
}
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 0)
_clear_environ()
test_capacity = {'cpu'}
doctester = Xdoctester(style='freeform', target='docstring')
doctester.prepare(test_capacity)
docstrings_to_test = {
'one_plus_one': """
placeholder
.. code-block:: python
:name: code-example-0
this is some blabla...
>>> # doctest: +SKIP
>>> print(1+1)
2
""",
'one_minus_one': """
placeholder
Examples:
""",
}
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 2)
tr_0, tr_1 = test_results
self.assertIn('one_plus_one', tr_0.name)
self.assertNotIn('code-example', tr_0.name)
self.assertFalse(tr_0.nocode)
self.assertFalse(tr_0.passed)
self.assertTrue(tr_0.skipped)
self.assertFalse(tr_0.failed)
self.assertIn('one_minus_one', tr_1.name)
self.assertNotIn('code-example', tr_1.name)
self.assertTrue(tr_1.nocode)
self.assertFalse(tr_1.passed)
self.assertFalse(tr_1.skipped)
self.assertFalse(tr_1.failed)
_clear_environ()
test_capacity = {'cpu'}
doctester = Xdoctester(style='freeform', target='codeblock')
doctester.prepare(test_capacity)
docstrings_to_test = {
'one_plus_one': """
placeholder
.. code-block:: python
:name: code-example-0
this is some blabla...
>>> # doctest: +SKIP
>>> print(1+1)
2
""",
'one_minus_one': """
placeholder
Examples:
""",
}
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 1)
tr_0 = test_results[0]
self.assertIn('one_plus_one', tr_0.name)
self.assertIn('code-example', tr_0.name)
self.assertFalse(tr_0.nocode)
self.assertFalse(tr_0.passed)
self.assertTrue(tr_0.skipped)
self.assertFalse(tr_0.failed)
def test_multiprocessing_xdoctester(self):
docstrings_to_test = {
'static_0': """
this is docstring...
Examples:
.. code-block:: python
>>> import numpy as np
>>> import paddle
>>> paddle.enable_static()
>>> data = paddle.static.data(name='X', shape=[None, 2, 28, 28], dtype='float32')
""",
'static_1': """
this is docstring...
Examples:
.. code-block:: python
>>> import numpy as np
>>> import paddle
>>> paddle.enable_static()
>>> data = paddle.static.data(name='X', shape=[None, 1, 28, 28], dtype='float32')
""",
}
_clear_environ()
test_capacity = {'cpu'}
doctester = Xdoctester()
doctester.prepare(test_capacity)
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 2)
tr_0, tr_1 = test_results
self.assertIn('static_0', tr_0.name)
self.assertTrue(tr_0.passed)
self.assertIn('static_1', tr_1.name)
self.assertTrue(tr_1.passed)
_clear_environ()
test_capacity = {'cpu'}
doctester = Xdoctester(use_multiprocessing=False)
doctester.prepare(test_capacity)
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 2)
tr_0, tr_1 = test_results
self.assertIn('static_0', tr_0.name)
self.assertTrue(tr_0.passed)
self.assertIn('static_1', tr_1.name)
self.assertFalse(tr_1.passed)
self.assertTrue(tr_1.failed)
def test_timeout(self):
docstrings_to_test = {
'timeout_false': """
this is docstring...
Examples:
.. code-block:: python
>>> # doctest: +TIMEOUT(2)
>>> import time
>>> time.sleep(0.1)
""",
'timeout_true': """
this is docstring...
Examples:
.. code-block:: python
>>> # doctest: +TIMEOUT(2)
>>> import time
>>> time.sleep(3)
""",
'timeout_false_with_skip_0': """
this is docstring...
Examples:
.. code-block:: python
>>> # doctest: +TIMEOUT(2)
>>> # doctest: +SKIP
>>> import time
>>> time.sleep(0.1)
""",
'timeout_false_with_skip_1': """
this is docstring...
Examples:
.. code-block:: python
>>> # doctest: +SKIP
>>> # doctest: +TIMEOUT(2)
>>> import time
>>> time.sleep(0.1)
""",
'timeout_true_with_skip_0': """
this is docstring...
Examples:
.. code-block:: python
>>> # doctest: +TIMEOUT(2)
>>> # doctest: +SKIP
>>> import time
>>> time.sleep(3)
""",
'timeout_true_with_skip_1': """
this is docstring...
Examples:
.. code-block:: python
>>> # doctest: +SKIP
>>> # doctest: +TIMEOUT(2)
>>> import time
>>> time.sleep(3)
""",
'timeout_more_codes': """
this is docstring...
Examples:
.. code-block:: python
>>> # doctest: +TIMEOUT(2)
>>> import time
>>> time.sleep(0.1)
.. code-block:: python
>>> # doctest: +TIMEOUT(2)
>>> import time
>>> time.sleep(3)
""",
}
_clear_environ()
test_capacity = {'cpu'}
doctester = Xdoctester()
doctester.prepare(test_capacity)
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 8)
tr_0, tr_1, tr_2, tr_3, tr_4, tr_5, tr_6, tr_7 = test_results
self.assertIn('timeout_false', tr_0.name)
self.assertTrue(tr_0.passed)
self.assertFalse(tr_0.timeout)
self.assertIn('timeout_true', tr_1.name)
self.assertFalse(tr_1.passed)
self.assertTrue(tr_1.timeout)
self.assertIn('timeout_false_with_skip_0', tr_2.name)
self.assertFalse(tr_2.passed)
self.assertFalse(tr_2.timeout)
self.assertTrue(tr_2.skipped)
self.assertIn('timeout_false_with_skip_1', tr_3.name)
self.assertFalse(tr_3.passed)
self.assertFalse(tr_3.timeout)
self.assertTrue(tr_3.skipped)
self.assertIn('timeout_true_with_skip_0', tr_4.name)
self.assertFalse(tr_4.passed)
self.assertFalse(tr_4.timeout)
self.assertTrue(tr_4.skipped)
self.assertIn('timeout_true_with_skip_1', tr_5.name)
self.assertFalse(tr_5.passed)
self.assertFalse(tr_5.timeout)
self.assertTrue(tr_5.skipped)
self.assertIn('timeout_more_codes', tr_6.name)
self.assertTrue(tr_6.passed)
self.assertFalse(tr_6.timeout)
self.assertIn('timeout_more_codes', tr_7.name)
self.assertFalse(tr_7.passed)
self.assertTrue(tr_7.timeout)
_clear_environ()
test_capacity = {'cpu'}
doctester = Xdoctester(use_multiprocessing=False)
doctester.prepare(test_capacity)
test_results = get_test_results(doctester, docstrings_to_test)
self.assertEqual(len(test_results), 8)
tr_0, tr_1, tr_2, tr_3, tr_4, tr_5, tr_6, tr_7 = test_results
self.assertIn('timeout_false', tr_0.name)
self.assertTrue(tr_0.passed)
self.assertFalse(tr_0.timeout)
self.assertIn('timeout_true', tr_1.name)
self.assertFalse(tr_1.passed)
self.assertTrue(tr_1.timeout)
self.assertIn('timeout_false_with_skip_0', tr_2.name)
self.assertFalse(tr_2.passed)
self.assertFalse(tr_2.timeout)
self.assertTrue(tr_2.skipped)
self.assertIn('timeout_false_with_skip_1', tr_3.name)
self.assertFalse(tr_3.passed)
self.assertFalse(tr_3.timeout)
self.assertTrue(tr_3.skipped)
self.assertIn('timeout_true_with_skip_0', tr_4.name)
self.assertFalse(tr_4.passed)
self.assertFalse(tr_4.timeout)
self.assertTrue(tr_4.skipped)
self.assertIn('timeout_true_with_skip_1', tr_5.name)
self.assertFalse(tr_5.passed)
self.assertFalse(tr_5.timeout)
self.assertTrue(tr_5.skipped)
self.assertIn('timeout_more_codes', tr_6.name)
self.assertTrue(tr_6.passed)
self.assertFalse(tr_6.timeout)
self.assertIn('timeout_more_codes', tr_7.name)
self.assertFalse(tr_7.passed)
self.assertTrue(tr_7.timeout)
if __name__ == '__main__':
unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册