{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## IPython Utilities" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Utilities to help work with ipython/jupyter environment." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To import from [`fastai.utils.ipython`](/utils.ipython.html#utils.ipython) do:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "hide_input": true }, "outputs": [], "source": [ "from fastai.gen_doc.nbdoc import *" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "hide_input": false }, "outputs": [], "source": [ "from fastai.utils.ipython import * " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Workarounds to the leaky ipython traceback on exception\n", "\n", "ipython has a feature where it stores tb with all the `locals()` tied in, which\n", "prevents `gc.collect()` from freeing those variables and leading to a leakage.\n", "\n", "Therefore we cleanse the tb before handing it over to ipython. The 2 ways of doing it are by either using the [`gpu_mem_restore`](/utils.ipython.html#gpu_mem_restore) decorator or the [`gpu_mem_restore_ctx`](/utils.ipython.html#gpu_mem_restore_ctx) context manager which are described next:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

gpu_mem_restore[source][test]

\n", "\n", "> gpu_mem_restore(**`func`**)\n", "\n", "
×

No tests found for gpu_mem_restore. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Reclaim GPU RAM if CUDA out of memory happened, or execution was interrupted " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(gpu_mem_restore)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[`gpu_mem_restore`](/utils.ipython.html#gpu_mem_restore) is a decorator to be used with any functions that interact with CUDA (top-level is fine)\n", "\n", "* under non-ipython environment it doesn't do anything.\n", "* under ipython currently it strips tb by default only for the \"CUDA out of memory\" exception.\n", "\n", "The env var `FASTAI_TB_CLEAR_FRAMES` changes this behavior when run under ipython,\n", "depending on its value: \n", "\n", "* \"0\": never strip tb (makes it possible to always use `%debug` magic, but with leaks)\n", "* \"1\": always strip tb (never need to worry about leaks, but `%debug` won't work)\n", "\n", "e.g. `os.environ['FASTAI_TB_CLEAR_FRAMES']=\"0\"` will set it to 0.\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

class gpu_mem_restore_ctx[source][test]

\n", "\n", "> gpu_mem_restore_ctx()\n", "\n", "
×

No tests found for gpu_mem_restore_ctx. To contribute a test please refer to this guide and this discussion.

\n", "\n", "context manager to reclaim RAM if an exception happened under ipython " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(gpu_mem_restore_ctx, title_level=4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "if function decorator is not a good option, you can use a context manager instead. For example:\n", "```\n", "with gpu_mem_restore_ctx():\n", " learn.fit_one_cycle(1,1e-2)\n", "```\n", "This particular one will clear tb on any exception." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "hide_input": true }, "outputs": [], "source": [ "from fastai.gen_doc.nbdoc import *\n", "from fastai.utils.ipython import * " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Undocumented Methods - Methods moved below this line will intentionally be hidden" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.2" } }, "nbformat": 4, "nbformat_minor": 2 }