添加时间:2024-04-07 23:15:04
在 TensorFlow.org 上查看 | 在 Google Colab 中运行 | 在 Github 上查看源代码 | 下载笔记本 |
TensorFlow 同时使用计算图和 Eager Execution 来执行计算。一个 包含一组代表计算单元的 对象(运算)和一组代表在运算之间流动的数据单元的 对象。
Grappler 是 TensorFlow 运行时中的默认计算图优化系统。Grappler 通过计算图简化和其他高级优化(例如利用内嵌函数体实现程序间优化),在计算图模式(在 内)下应用优化以提高 TensorFlow 计算的性能。优化 还可以通过优化计算图节点到计算资源的映射来减少设备峰值内存使用量并提高硬件利用率。
Grappler 通过称为 的顶级驱动程序执行计算图优化。TensorFlow 提供以下计算图优化器:
2022-12-14 21:15:47.322566: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory 2022-12-14 21:15:47.322681: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory 2022-12-14 21:15:47.322698: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.
创建上下文管理器以轻松切换优化器状态。
TensorFlow 2 及更高版本默认情况下会以 Eager 模式执行。使用 可将默认执行切换为“计算图”模式。Grappler 在后台自动运行,以应用上述计算图优化并提高执行性能。
作为一个初步的示例,考虑一个对常量执行运算并返回输出的函数。
关闭常量折叠优化器并执行以下函数:
{'constant_folding': False, 'disable_model_pruning': False, 'disable_meta_optimizer': False} Tracing! Vanilla execution: 0.0011523299999680603 s
启用常量折叠优化器,然后再次执行函数以观察函数执行的加速情况。
{'constant_folding': True, 'disable_model_pruning': False, 'disable_meta_optimizer': False} Tracing! Constant folded execution: 0.0009006219997900189 s
考虑一个检查其输入参数的数值并返回自身的简单函数。
首先,在调试剥离器优化器关闭的情况下执行该函数。
2022-12-14 21:16:07.794488: E tensorflow/core/kernels/check_numerics_op.cc:293] abnormal_detected_host @0x7f49f6600100={0, 1} Bad! Traceback (most recent call last): File "/tmpfs/tmp/ipykernel_95249/3616845043.py", line 4, in <module> test_func(p1) File "/tmpfs/src/tf_docs_env/lib/python3.9/site-packages/tensorflow/python/util/traceback_utils.py", line 153, in error_handler raise e.with_traceback(filtered_tb) from None tensorflow.python.framework.errors_impl.InvalidArgumentError: Graph execution error: Detected at node 'CheckNumerics' defined at (most recent call last): File "/usr/lib/python3.9/runpy.py", line 197, in _run_module_as_main return _run_code(code, main_globals, None, File "/usr/lib/python3.9/runpy.py", line 87, in _run_code exec(code, run_globals) File "/tmpfs/src/tf_docs_env/lib/python3.9/site-packages/ipykernel_launcher.py", line 17, in <module> app.launch_new_instance() File "/tmpfs/src/tf_docs_env/lib/python3.9/site-packages/traitlets/config/application.py", line 992, in launch_instance app.start() File "/tmpfs/src/tf_docs_env/lib/python3.9/site-packages/ipykernel/kernelapp.py", line 711, in start self.io_loop.start() File "/tmpfs/src/tf_docs_env/lib/python3.9/site-packages/tornado/platform/asyncio.py", line 215, in start self.asyncio_loop.run_forever() File "/usr/lib/python3.9/asyncio/base_events.py", line 601, in run_forever self._run_once() File "/usr/lib/python3.9/asyncio/base_events.py", line 1905, in _run_once handle._run() File "/usr/lib/python3.9/asyncio/events.py", line 80, in _run self._context.run(self._callback, *self._args) File "/tmpfs/src/tf_docs_env/lib/python3.9/site-packages/ipykernel/kernelbase.py", line 510, in dispatch_queue await self.process_one() File "/tmpfs/src/tf_docs_env/lib/python3.9/site-packages/ipykernel/kernelbase.py", line 499, in process_one await dispatch(*args) File "/tmpfs/src/tf_docs_env/lib/python3.9/site-packages/ipykernel/kernelbase.py", line 406, in dispatch_shell await result File "/tmpfs/src/tf_docs_env/lib/python3.9/site-packages/ipykernel/kernelbase.py", line 729, in execute_request reply_content=await reply_content File "/tmpfs/src/tf_docs_env/lib/python3.9/site-packages/ipykernel/ipkernel.py", line 411, in do_execute res=shell.run_cell( File "/tmpfs/src/tf_docs_env/lib/python3.9/site-packages/ipykernel/zmqshell.py", line 531, in run_cell return super().run_cell(*args, **kwargs) File "/tmpfs/src/tf_docs_env/lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 2940, in run_cell result=self._run_cell( File "/tmpfs/src/tf_docs_env/lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 2995, in _run_cell return runner(coro) File "/tmpfs/src/tf_docs_env/lib/python3.9/site-packages/IPython/core/async_helpers.py", line 129, in _pseudo_sync_runner coro.send(None) File "/tmpfs/src/tf_docs_env/lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 3194, in run_cell_async has_raised=await self.run_ast_nodes(code_ast.body, cell_name, File "/tmpfs/src/tf_docs_env/lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 3373, in run_ast_nodes if await self.run_code(code, result, async_=asy): File "/tmpfs/src/tf_docs_env/lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 3433, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "/tmpfs/tmp/ipykernel_95249/3616845043.py", line 4, in <module> test_func(p1) File "/tmpfs/tmp/ipykernel_95249/2241890286.py", line 5, in simple_func tf.debugging.check_numerics(output, "Bad!") Node: 'CheckNumerics' Bad! : Tensor had Inf values [[{ {node CheckNumerics} }]] [Op:__inference_simple_func_131]
启用调试剥离器优化器,然后再次执行该函数。
调试剥离器优化器从计算图中剥离 节点并执行该函数,而不会引发任何错误。
TensorFlow 运行时会在执行之前使用 Grappler 自动优化计算图。使用 可启用或停用各个计算图优化器。
有关 Grappler 的更多信息,请参阅 TensorFlow 计算图优化。
地址:海南省海口市电话:0898-08980898传真:0898-1230-5678
Copyright © 2012-2018 耀世娱乐-耀世注册登录入口 版权所有ICP备案编号:琼ICP备xxxxxxxx号