tensorflow/tensorflow/tools/docs/tf_doctest.py at master · tensorflow/tensorflow · GitHub
Skip to content You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert Expand file tree / tf_doctest.py Copy path More file actions More file actions Latest commit History History History 217 lines (170 loc) · 6.69 KB / tf_doctest.py Copy path File metadata and controls 217 lines (170 loc) · 6.69 KB Raw Copy raw file Download raw file Open symbols panel Edit and raw actions 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 # Copyright 2019 The TensorFlow 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. # ============================================================================== """Run doctests for tensorflow.""" import importlib import os import pkgutil import sys from absl import flags from absl.testing import absltest import numpy as np import tensorflow.compat.v2 as tf from tensorflow.python.eager import context from tensorflow.python.ops import logging_ops from tensorflow.tools.docs import tf_doctest_lib # We put doctest after absltest so that it picks up the unittest monkeypatch. # Otherwise doctest tests aren't runnable at all. import doctest # pylint: disable=g-bad-import-order tf.compat.v1.enable_v2_behavior() # `enable_interactive_logging` must come after `enable_v2_behavior`. logging_ops.enable_interactive_logging() FLAGS = flags.FLAGS flags.DEFINE_list('module', [], 'A list of specific module to run doctest on.') flags.DEFINE_list('module_prefix_skip', [], 'A list of modules to ignore when resolving modules.') flags.DEFINE_boolean('list', None, 'List all the modules in the core package imported.') flags.DEFINE_integer('required_gpus', 0, 'The number of GPUs required for the tests.') # Both --module and --module_prefix_skip are relative to PACKAGE.
PACKAGES = [ 'tensorflow.python.', 'tensorflow.lite.python.', ] def recursive_import(root): """Recursively imports all the sub-modules under a root package. Args: root: A python package. """ for _, name, _ in pkgutil.walk_packages( root.__path__, prefix=root.__name__ + '.'): try: importlib.import_module(name) except (AttributeError, ImportError): pass def find_modules(): """Finds all the modules in the core package imported.
Returns: A list containing all the modules in tensorflow.python. """ tf_modules = [] for name, module in sys.modules.items(): # The below for loop is a constant time loop. for package in PACKAGES: if name.startswith(package): tf_modules.append(module) return tf_modules def filter_on_submodules(all_modules, submodules): """Filters all the modules based on the modules flag. The module flag has to be relative to the core package imported.
For example, if `module=keras.layers` then, this function will return all the modules in the submodule. Args: all_modules: All the modules in the core package. submodules: Submodules to filter from all the modules. Returns: All the modules in the submodule. """ filtered_modules = [] for mod in all_modules: for submodule in submodules: # The below for loop is a constant time loop. for package in PACKAGES: if package + submodule in mod.__name__: filtered_modules.append(mod) return filtered_modules def setup_gpu(required_gpus): """Sets up the GPU devices.
If there're more available GPUs than needed, it hides the additional ones. If there're less, it creates logical devices. This is to make sure the tests see a fixed number of GPUs regardless of the environment. Args: required_gpus: an integer. The number of GPUs required. Raises: ValueError: if num_gpus is larger than zero but no GPU is available. """ if required_gpus == 0: return available_gpus = tf.config.experimental.list_physical_devices('GPU') if not available_gpus: raise ValueError('requires at least one physical GPU') if len(available_gpus) >= required_gpus: tf.config.set_visible_devices(available_gpus[:required_gpus]) else: # Create logical GPUs out of one physical GPU for simplicity.
Note that the # other physical GPUs are still available and corresponds to one logical GPU # each. num_logical_gpus = required_gpus - len(available_gpus) + 1 logical_gpus = [ tf.config.LogicalDeviceConfiguration(memory_limit=256) for _ in range(num_logical_gpus) ] tf.config.set_logical_device_configuration(available_gpus[0], logical_gpus) class TfTestCase(tf.test.TestCase): def set_up(self, test): # Enable soft device placement to run distributed doctests. tf.config.set_soft_device_placement(True) self.setUp() context.async_wait() def tear_down(self, test): self.tearDown() def load_tests(unused_loader, tests, unused_ignore): """Loads all the tests in the docstrings and runs them.""" tf_modules = find_modules() if FLAGS.module: tf_modules = filter_on_submodules(tf_modules, FLAGS.module) if FLAGS.list: print('**************************************************') for mod in tf_modules: print(mod.__name__) print('**************************************************') return tests test_shard_index = int(os.environ.get('TEST_SHARD_INDEX', '0')) total_test_shards = int(os.environ.get('TEST_TOTAL_SHARDS', '1')) tf_modules = sorted(tf_modules, key=lambda mod: mod.__name__) for n, module in enumerate(tf_modules): if (n % total_test_shards) != test_shard_index: continue # If I break the loop comprehension, then the test times out in `small` # size. if any( module.__name__.startswith(package + prefix) # pylint: disable=g-complex-comprehension for prefix in FLAGS.module_prefix_skip for package in PACKAGES): continue testcase = TfTestCase() tests.addTests( doctest.DocTestSuite( module, test_finder=doctest.DocTestFinder(exclude_empty=False), extraglobs={ 'tf': tf, 'np': np, 'os': os }, setUp=testcase.set_up, tearDown=testcase.tear_down, checker=tf_doctest_lib.TfDoctestOutputChecker(), optionflags=(doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE | doctest.IGNORE_EXCEPTION_DETAIL | doctest.DONT_ACCEPT_BLANKLINE), )) return tests # We can only create logical devices before initializing Tensorflow.
This is # called by unittest framework before running any test. # https://docs.python.org/3/library/unittest.html#setupmodule-and-teardownmodule def setUpModule(): setup_gpu(FLAGS.required_gpus) if __name__ == '__main__': absltest.main() You can’t perform that action at this time.