You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2022/12/16 00:57:40 UTC

[GitHub] [tvm] guberti opened a new pull request, #13627: [docs] Add "Open with Colab" button to documentation

guberti opened a new pull request, #13627:
URL: https://github.com/apache/tvm/pull/13627

   Details to be announced at Hackathon presentation tomorrow!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] guberti commented on a diff in pull request #13627: [docs] Add "Open with Colab" button to documentation

Posted by GitBox <gi...@apache.org>.
guberti commented on code in PR #13627:
URL: https://github.com/apache/tvm/pull/13627#discussion_r1063753264


##########
docs/conf.py:
##########
@@ -84,6 +86,148 @@ def git_describe_version(original_version):
 version = git_describe_version(tvm.__version__)
 release = version
 
+
+def monkey_patch(module_name, func_name):

Review Comment:
   I agree that `reset_modules` is a valid approach, but I think going that route would make the changes less readable. Permanently altering `sphinx-gallery` is not desirable, but it may be the cleanest option other than forking `sphinx-gallery`.
   
   This could also be improved in a follow-on PR.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] guberti commented on a diff in pull request #13627: [docs] Add "Open with Colab" button to documentation

Posted by GitBox <gi...@apache.org>.
guberti commented on code in PR #13627:
URL: https://github.com/apache/tvm/pull/13627#discussion_r1063793894


##########
gallery/how_to/work_with_microtvm/micro_train.py:
##########
@@ -71,7 +60,7 @@
 #
 #     .. code-block:: bash
 #
-#       %%bash
+#       %%shell

Review Comment:
   I've added a section about this to `docs/README.md`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] mehrdadh commented on a diff in pull request #13627: [docs] Add "Open with Colab" button to documentation

Posted by GitBox <gi...@apache.org>.
mehrdadh commented on code in PR #13627:
URL: https://github.com/apache/tvm/pull/13627#discussion_r1060820946


##########
apps/microtvm/pyproject.toml:
##########
@@ -98,22 +95,23 @@ onnxoptimizer = { version = "==0.2.6", optional = true }
 onnxruntime = { version = "==1.9.0", optional = true }
 
 # Pytorch (also used by ONNX)
-torch = { version = "==1.11.0", optional = true }
-torchvision = { version = "==0.12.0", optional = true }
+torch = { version = "==1.11.0" }

Review Comment:
   this was a temporary hack to be able to export the dependencies in a requirement.txt file. We should revet this change and figure out how to generate dependencies file with extra dependencies.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] guberti commented on a diff in pull request #13627: [docs] Add "Open with Colab" button to documentation

Posted by GitBox <gi...@apache.org>.
guberti commented on code in PR #13627:
URL: https://github.com/apache/tvm/pull/13627#discussion_r1057998312


##########
docs/conf.py:
##########
@@ -84,6 +86,148 @@ def git_describe_version(original_version):
 version = git_describe_version(tvm.__version__)
 release = version
 
+
+def monkey_patch(module_name, func_name):
+    """Helper function for monkey-patching library functions.
+
+    Used to modify a few sphinx-gallery behaviors to make the "Open in Colab"
+    button work correctly. Should be used as a decorator with arguments.
+    """
+    module = import_module(module_name)
+    original_func = getattr(module, func_name)
+
+    def decorator(function):
+        updated_func = partial(function, real_func=original_func)
+        setattr(module, func_name, updated_func)
+        return updated_func
+
+    return decorator
+
+
+# This header replaces the default sphinx-gallery one in sphinx_gallery/gen_rst.py.
+COLAB_HTML_HEADER = """
+.. DO NOT EDIT. THIS FILE WAS AUTOMATICALLY GENERATED BY
+.. TVM'S MONKEY-PATCHED VERSION OF SPHINX-GALLERY. TO MAKE
+.. CHANGES, EDIT THE SOURCE PYTHON FILE:
+.. "{0}"
+
+.. only:: html
+
+    .. note::
+        :class: sphx-glr-download-link-note
+
+        This tutorial can be used interactively with Google Colab! You can also click
+        :ref:`here <sphx_glr_download_{1}>` to run the Jupyter notebook locally.
+
+        .. image:: https://raw.githubusercontent.com/apache/web-data/main/images/utilities/colab_button.svg
+            :align: center
+            :target: {2}
+            :width: 300px
+
+.. rst-class:: sphx-glr-example-title
+
+.. _sphx_glr_{1}:
+
+"""
+
+# Google Colab allows opening .ipynb files on GitHub by appending the GitHub path to its own url.
+COLAB_URL_BASE = "https://colab.research.google.com/github"
+IPYTHON_GITHUB_BASE = "apache/tvm-site/blob/asf-site/docs/_downloads/"
+
+
+@monkey_patch("sphinx_gallery.gen_rst", "save_rst_example")
+def save_rst_example(example_rst, example_file, time_elapsed, memory_used, gallery_conf, real_func):
+    """Monkey-patch save_rst_example to include the "Open in Colab" button."""
+
+    # The url is the md5 hash of the notebook path.
+    example_fname = os.path.relpath(example_file, gallery_conf["src_dir"])
+    ref_fname = example_fname.replace(os.path.sep, "_")
+    notebook_path = example_fname[:-2] + "ipynb"
+    digest = md5(notebook_path.encode()).hexdigest()
+
+    # Fixed documentation versions must link to different (earlier) .ipynb notebooks.
+    colab_url = f"{COLAB_URL_BASE}/{IPYTHON_GITHUB_BASE}"
+    if "dev" not in version:
+        colab_url += version + "/"
+    colab_url += digest + "/" + os.path.basename(notebook_path)
+
+    new_header = COLAB_HTML_HEADER.format(example_fname, ref_fname, colab_url)
+    with patch("sphinx_gallery.gen_rst.EXAMPLE_HEADER", new_header):
+        real_func(example_rst, example_file, time_elapsed, memory_used, gallery_conf)
+
+
+@monkey_patch("sphinx_gallery.notebook", "rst2md")
+def rst2md(text, gallery_conf, target_dir, heading_levels, real_func):
+    """Monkey-patch rst2md to add limited include directive support to sphinx-gallery."""
+
+    include_re = re.compile(r"^([ \t]*)\.\. include::\s*(.+)\n", flags=re.M)
+
+    def load_include(match):
+        full_path = os.path.join(target_dir, match.group(2))
+        with open(full_path) as f:
+            lines = f.read()
+        indented = textwrap.indent(lines, match.group(1)) + "\n"
+        return indented
+
+    text = re.sub(include_re, load_include, text)
+
+    return real_func(text, gallery_conf, target_dir, heading_levels)
+
+
+INSTALL_TVM_DEV = f"""%%shell
+# Installs the latest dev build of TVM from PyPI. If you wish to build
+# from source, see https://tvm.apache.org/docs/install/from_source.html
+pip install apache-tvm --pre"""
+
+INSTALL_TVM_FIXED = f"""%%shell
+# Installs TVM version {version} from PyPI. If you wish to build
+# from source, see https://tvm.apache.org/docs/install/from_source.html
+pip install apache-tvm=={version}"""
+
+INSTALL_TVM_CUDA_DEV = f"""%%shell
+# Installs the latest dev build of TVM from PyPI, with CUDA enabled. To use this,
+# you must request a Google Colab instance with a GPU by going to Runtime ->
+# Change runtime type -> Hardware accelerator -> GPU. If you wish to build from
+# source, see see https://tvm.apache.org/docs/install/from_source.html
+pip install tlcpack-nightly-cu113 --pre -f https://tlcpack.ai/wheels"""
+
+INSTALL_TVM_CUDA_FIXED = f"""%%shell
+# Installs TVM version {version} from PyPI, with CUDA enabled. To use this,
+# you must request a Google Colab instance with a GPU by going to Runtime ->
+# Change runtime type -> Hardware accelerator -> GPU. If you wish to build from
+# source, see see https://tvm.apache.org/docs/install/from_source.html
+pip install apache-tvm-cu113=={version} -f https://tlcpack.ai/wheels"""

Review Comment:
   That would run fine, but I don't think that's as clear to read. I'd prefer to keep explicitly specifying `tlcpack-nightly-cu113`.
   
   If we want to reduce the amount of copied text, we could also have one string and template it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] mehrdadh commented on a diff in pull request #13627: [docs] Add "Open with Colab" button to documentation

Posted by GitBox <gi...@apache.org>.
mehrdadh commented on code in PR #13627:
URL: https://github.com/apache/tvm/pull/13627#discussion_r1061966967


##########
gallery/how_to/work_with_microtvm/micro_tflite.py:
##########
@@ -26,109 +26,36 @@
 """
 
 ######################################################################
-# .. note::
-#     If you want to run this tutorial on the microTVM Reference VM, download the Jupyter
-#     notebook using the link at the bottom of this page and save it into the TVM directory. Then:
 #
-#     #. Login to the reference VM with a modified ``vagrant ssh`` command:
+#     .. include:: ../../../../gallery/how_to/work_with_microtvm/install_dependencies.rst
 #
-#         ``$ vagrant ssh -- -L8888:localhost:8888``
-#
-#     #. Install jupyter:  ``pip install jupyterlab``
-#     #. ``cd`` to the TVM directory.
-#     #. Install tflite: poetry install -E importer-tflite
-#     #. Launch Jupyter Notebook: ``jupyter notebook``
-#     #. Copy the localhost URL displayed, and paste it into your browser.
-#     #. Navigate to saved Jupyter Notebook (``.ipynb`` file).
-#
-#
-# Setup
-# -----
-#
-# Install TFLite
-# ^^^^^^^^^^^^^^
-#
-# To get started, TFLite package needs to be installed as prerequisite. You can do this in two ways:
-#
-# 1. Install tflite with ``pip``
-#
-#     .. code-block:: bash
-#
-#       pip install tflite=2.1.0 --user
-#
-# 2. Generate the TFLite package yourself. The steps are the following:
-#
-#     Get the flatc compiler.
-#     Please refer to https://github.com/google/flatbuffers for details
-#     and make sure it is properly installed.
-#
-#     .. code-block:: bash
-#
-#       flatc --version
-#
-#     Get the TFLite schema.
-#
-#     .. code-block:: bash
-#
-#       wget https://raw.githubusercontent.com/tensorflow/tensorflow/r1.13/tensorflow/lite/schema/schema.fbs
-#
-#     Generate TFLite package.
-#
-#     .. code-block:: bash
-#
-#       flatc --python schema.fbs
-#
-#     Add the current folder (which contains generated tflite module) to PYTHONPATH.
-#
-#     .. code-block:: bash
-#
-#       export PYTHONPATH=${PYTHONPATH:+$PYTHONPATH:}$(pwd)
-#
-# To validate that the TFLite package was installed successfully, ``python -c "import tflite"``
-#
-# Install Zephyr (physical hardware only)
-# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-#
-# When running this tutorial with a host simulation (the default), you can use the host ``gcc`` to
-# build a firmware image that simulates the device. When compiling to run on physical hardware, you
-# need to install a *toolchain* plus some target-specific dependencies. microTVM allows you to
-# supply any compiler and runtime that can launch the TVM RPC server, but to get started, this
-# tutorial relies on the Zephyr RTOS to provide these pieces.
-#
-# You can install Zephyr by following the
-# `Installation Instructions <https://docs.zephyrproject.org/latest/getting_started/index.html>`_.
-#
-# Aside: Recreating your own Pre-Trained TFLite model
-#  The tutorial downloads a pretrained TFLite model. When working with microcontrollers
-#  you need to be mindful these are highly resource constrained devices as such standard
-#  models like MobileNet may not fit into their modest memory.
-#
-#  For this tutorial, we'll make use of one of the TF Micro example models.
-#
-#  If you wish to replicate the training steps see:
-#  https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/micro/examples/hello_world/train
-#
-#    .. note::
-#
-#      If you accidentally download the example pretrained model from:
-#
-#      ``wget https://storage.googleapis.com/download.tensorflow.org/models/tflite/micro/hello_world_2020_04_13.zip``
-#
-#      this will fail due to an unimplemented opcode (114)
-#
-# Load and prepare the Pre-Trained Model
-# --------------------------------------
-#
-# Load the pretrained TFLite model from a file in your current
-# directory into a buffer
 
 # sphinx_gallery_start_ignore
 from tvm import testing
 
 testing.utils.install_request_hook(depth=3)
 # sphinx_gallery_end_ignore
 
+# You can skip the following two sections (installing Zephyr and CMSIS-NN) if the following flag is False.

Review Comment:
   This is because it will run on CRT, so Zephyr and CMSIS-NN dependencies are not required
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] guberti commented on a diff in pull request #13627: [docs] Add "Open with Colab" button to documentation

Posted by GitBox <gi...@apache.org>.
guberti commented on code in PR #13627:
URL: https://github.com/apache/tvm/pull/13627#discussion_r1063759973


##########
gallery/how_to/work_with_microtvm/micro_tflite.py:
##########
@@ -26,109 +26,36 @@
 """
 
 ######################################################################
-# .. note::
-#     If you want to run this tutorial on the microTVM Reference VM, download the Jupyter
-#     notebook using the link at the bottom of this page and save it into the TVM directory. Then:
 #
-#     #. Login to the reference VM with a modified ``vagrant ssh`` command:
+#     .. include:: ../../../../gallery/how_to/work_with_microtvm/install_dependencies.rst
 #
-#         ``$ vagrant ssh -- -L8888:localhost:8888``
-#
-#     #. Install jupyter:  ``pip install jupyterlab``
-#     #. ``cd`` to the TVM directory.
-#     #. Install tflite: poetry install -E importer-tflite
-#     #. Launch Jupyter Notebook: ``jupyter notebook``
-#     #. Copy the localhost URL displayed, and paste it into your browser.
-#     #. Navigate to saved Jupyter Notebook (``.ipynb`` file).
-#
-#
-# Setup
-# -----
-#
-# Install TFLite
-# ^^^^^^^^^^^^^^
-#
-# To get started, TFLite package needs to be installed as prerequisite. You can do this in two ways:
-#
-# 1. Install tflite with ``pip``
-#
-#     .. code-block:: bash
-#
-#       pip install tflite=2.1.0 --user
-#
-# 2. Generate the TFLite package yourself. The steps are the following:
-#
-#     Get the flatc compiler.
-#     Please refer to https://github.com/google/flatbuffers for details
-#     and make sure it is properly installed.
-#
-#     .. code-block:: bash
-#
-#       flatc --version
-#
-#     Get the TFLite schema.
-#
-#     .. code-block:: bash
-#
-#       wget https://raw.githubusercontent.com/tensorflow/tensorflow/r1.13/tensorflow/lite/schema/schema.fbs
-#
-#     Generate TFLite package.
-#
-#     .. code-block:: bash
-#
-#       flatc --python schema.fbs
-#
-#     Add the current folder (which contains generated tflite module) to PYTHONPATH.
-#
-#     .. code-block:: bash
-#
-#       export PYTHONPATH=${PYTHONPATH:+$PYTHONPATH:}$(pwd)
-#
-# To validate that the TFLite package was installed successfully, ``python -c "import tflite"``
-#
-# Install Zephyr (physical hardware only)
-# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-#
-# When running this tutorial with a host simulation (the default), you can use the host ``gcc`` to
-# build a firmware image that simulates the device. When compiling to run on physical hardware, you
-# need to install a *toolchain* plus some target-specific dependencies. microTVM allows you to
-# supply any compiler and runtime that can launch the TVM RPC server, but to get started, this
-# tutorial relies on the Zephyr RTOS to provide these pieces.
-#
-# You can install Zephyr by following the
-# `Installation Instructions <https://docs.zephyrproject.org/latest/getting_started/index.html>`_.
-#
-# Aside: Recreating your own Pre-Trained TFLite model
-#  The tutorial downloads a pretrained TFLite model. When working with microcontrollers
-#  you need to be mindful these are highly resource constrained devices as such standard
-#  models like MobileNet may not fit into their modest memory.
-#
-#  For this tutorial, we'll make use of one of the TF Micro example models.
-#
-#  If you wish to replicate the training steps see:
-#  https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/micro/examples/hello_world/train
-#
-#    .. note::
-#
-#      If you accidentally download the example pretrained model from:
-#
-#      ``wget https://storage.googleapis.com/download.tensorflow.org/models/tflite/micro/hello_world_2020_04_13.zip``
-#
-#      this will fail due to an unimplemented opcode (114)
-#
-# Load and prepare the Pre-Trained Model
-# --------------------------------------
-#
-# Load the pretrained TFLite model from a file in your current
-# directory into a buffer
 
 # sphinx_gallery_start_ignore
 from tvm import testing
 
 testing.utils.install_request_hook(depth=3)
 # sphinx_gallery_end_ignore
 
+# You can skip the following two sections (installing Zephyr and CMSIS-NN) if the following flag is False.

Review Comment:
   Rewrote this comment to be clearer.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] guberti commented on a diff in pull request #13627: [docs] Add "Open with Colab" button to documentation

Posted by GitBox <gi...@apache.org>.
guberti commented on code in PR #13627:
URL: https://github.com/apache/tvm/pull/13627#discussion_r1063756803


##########
gallery/how_to/work_with_microtvm/micro_aot.py:
##########
@@ -139,6 +162,12 @@
         "board": BOARD,
         "serial_number": SERIAL,
         "config_main_stack_size": 4096,
+        "cmsis_path": "/content/cmsis"

Review Comment:
   Replaced this to use `os.getenv` to be cleaner.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] alanmacd commented on a diff in pull request #13627: [docs] Add "Open with Colab" button to documentation

Posted by GitBox <gi...@apache.org>.
alanmacd commented on code in PR #13627:
URL: https://github.com/apache/tvm/pull/13627#discussion_r1060875037


##########
gallery/how_to/work_with_microtvm/micro_train.py:
##########
@@ -71,7 +60,7 @@
 #
 #     .. code-block:: bash
 #
-#       %%bash
+#       %%shell

Review Comment:
   This is some good info, I wonder if we could capture it somewhere in a comment? (not sure where would be best, so maybe not)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] areusch merged pull request #13627: [docs] Add "Open with Colab" button to documentation

Posted by GitBox <gi...@apache.org>.
areusch merged PR #13627:
URL: https://github.com/apache/tvm/pull/13627


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] guberti commented on a diff in pull request #13627: [DRAFT][docs] Add "Open with Colab" button to documentation

Posted by GitBox <gi...@apache.org>.
guberti commented on code in PR #13627:
URL: https://github.com/apache/tvm/pull/13627#discussion_r1057398959


##########
gallery/how_to/work_with_microtvm/micro_aot.py:
##########
@@ -30,6 +30,29 @@
 or on Zephyr platform on a microcontroller/board supported by Zephyr.
 """
 
+######################################################################
+#
+#     .. include:: ../../../../gallery/how_to/work_with_microtvm/install_zephyr.rst

Review Comment:
   I'd be in favor of this, but I'd love to get thoughts from @mehrdadh.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] guberti commented on a diff in pull request #13627: [docs] Add "Open with Colab" button to documentation

Posted by GitBox <gi...@apache.org>.
guberti commented on code in PR #13627:
URL: https://github.com/apache/tvm/pull/13627#discussion_r1057909768


##########
apps/microtvm/pyproject.toml:
##########
@@ -98,22 +95,23 @@ onnxoptimizer = { version = "==0.2.6", optional = true }
 onnxruntime = { version = "==1.9.0", optional = true }
 
 # Pytorch (also used by ONNX)
-torch = { version = "==1.11.0", optional = true }
-torchvision = { version = "==0.12.0", optional = true }
+torch = { version = "==1.11.0" }

Review Comment:
   cc @mehrdadh who made this change



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] guberti commented on a diff in pull request #13627: [docs] Add "Open with Colab" button to documentation

Posted by GitBox <gi...@apache.org>.
guberti commented on code in PR #13627:
URL: https://github.com/apache/tvm/pull/13627#discussion_r1058000392


##########
docs/conf.py:
##########
@@ -506,6 +650,12 @@ def process_docstring(app, what, name, obj, options, lines):
 from legacy_redirect import build_legacy_redirect
 
 
+def strip_ipython_magic(app, docname, source):

Review Comment:
   Yep - IPython magic is not always one word (e.g. `%%writefile output.txt`) but it is always exactly one line.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] guberti commented on a diff in pull request #13627: [docs] Add "Open with Colab" button to documentation

Posted by GitBox <gi...@apache.org>.
guberti commented on code in PR #13627:
URL: https://github.com/apache/tvm/pull/13627#discussion_r1058647242


##########
docs/conf.py:
##########
@@ -84,6 +86,148 @@ def git_describe_version(original_version):
 version = git_describe_version(tvm.__version__)
 release = version
 
+
+def monkey_patch(module_name, func_name):
+    """Helper function for monkey-patching library functions.
+
+    Used to modify a few sphinx-gallery behaviors to make the "Open in Colab"
+    button work correctly. Should be used as a decorator with arguments.
+    """
+    module = import_module(module_name)
+    original_func = getattr(module, func_name)
+
+    def decorator(function):
+        updated_func = partial(function, real_func=original_func)
+        setattr(module, func_name, updated_func)
+        return updated_func
+
+    return decorator
+
+
+# This header replaces the default sphinx-gallery one in sphinx_gallery/gen_rst.py.
+COLAB_HTML_HEADER = """
+.. DO NOT EDIT. THIS FILE WAS AUTOMATICALLY GENERATED BY
+.. TVM'S MONKEY-PATCHED VERSION OF SPHINX-GALLERY. TO MAKE
+.. CHANGES, EDIT THE SOURCE PYTHON FILE:
+.. "{0}"
+
+.. only:: html
+
+    .. note::
+        :class: sphx-glr-download-link-note
+
+        This tutorial can be used interactively with Google Colab! You can also click
+        :ref:`here <sphx_glr_download_{1}>` to run the Jupyter notebook locally.
+
+        .. image:: https://raw.githubusercontent.com/apache/web-data/main/images/utilities/colab_button.svg
+            :align: center
+            :target: {2}
+            :width: 300px
+
+.. rst-class:: sphx-glr-example-title
+
+.. _sphx_glr_{1}:
+
+"""
+
+# Google Colab allows opening .ipynb files on GitHub by appending the GitHub path to its own url.
+COLAB_URL_BASE = "https://colab.research.google.com/github"
+IPYTHON_GITHUB_BASE = "apache/tvm-site/blob/asf-site/docs/_downloads/"
+
+
+@monkey_patch("sphinx_gallery.gen_rst", "save_rst_example")
+def save_rst_example(example_rst, example_file, time_elapsed, memory_used, gallery_conf, real_func):
+    """Monkey-patch save_rst_example to include the "Open in Colab" button."""
+
+    # The url is the md5 hash of the notebook path.
+    example_fname = os.path.relpath(example_file, gallery_conf["src_dir"])
+    ref_fname = example_fname.replace(os.path.sep, "_")
+    notebook_path = example_fname[:-2] + "ipynb"
+    digest = md5(notebook_path.encode()).hexdigest()
+
+    # Fixed documentation versions must link to different (earlier) .ipynb notebooks.
+    colab_url = f"{COLAB_URL_BASE}/{IPYTHON_GITHUB_BASE}"
+    if "dev" not in version:
+        colab_url += version + "/"
+    colab_url += digest + "/" + os.path.basename(notebook_path)
+
+    new_header = COLAB_HTML_HEADER.format(example_fname, ref_fname, colab_url)
+    with patch("sphinx_gallery.gen_rst.EXAMPLE_HEADER", new_header):
+        real_func(example_rst, example_file, time_elapsed, memory_used, gallery_conf)
+
+
+@monkey_patch("sphinx_gallery.notebook", "rst2md")
+def rst2md(text, gallery_conf, target_dir, heading_levels, real_func):
+    """Monkey-patch rst2md to add limited include directive support to sphinx-gallery."""
+
+    include_re = re.compile(r"^([ \t]*)\.\. include::\s*(.+)\n", flags=re.M)
+
+    def load_include(match):
+        full_path = os.path.join(target_dir, match.group(2))
+        with open(full_path) as f:
+            lines = f.read()
+        indented = textwrap.indent(lines, match.group(1)) + "\n"
+        return indented
+
+    text = re.sub(include_re, load_include, text)
+
+    return real_func(text, gallery_conf, target_dir, heading_levels)
+
+
+INSTALL_TVM_DEV = f"""%%shell
+# Installs the latest dev build of TVM from PyPI. If you wish to build
+# from source, see https://tvm.apache.org/docs/install/from_source.html
+pip install apache-tvm --pre"""
+
+INSTALL_TVM_FIXED = f"""%%shell
+# Installs TVM version {version} from PyPI. If you wish to build
+# from source, see https://tvm.apache.org/docs/install/from_source.html
+pip install apache-tvm=={version}"""
+
+INSTALL_TVM_CUDA_DEV = f"""%%shell
+# Installs the latest dev build of TVM from PyPI, with CUDA enabled. To use this,
+# you must request a Google Colab instance with a GPU by going to Runtime ->
+# Change runtime type -> Hardware accelerator -> GPU. If you wish to build from
+# source, see see https://tvm.apache.org/docs/install/from_source.html
+pip install tlcpack-nightly-cu113 --pre -f https://tlcpack.ai/wheels"""
+
+INSTALL_TVM_CUDA_FIXED = f"""%%shell
+# Installs TVM version {version} from PyPI, with CUDA enabled. To use this,
+# you must request a Google Colab instance with a GPU by going to Runtime ->
+# Change runtime type -> Hardware accelerator -> GPU. If you wish to build from
+# source, see see https://tvm.apache.org/docs/install/from_source.html
+pip install apache-tvm-cu113=={version} -f https://tlcpack.ai/wheels"""
+
+
+@monkey_patch("sphinx_gallery.gen_rst", "jupyter_notebook")
+def jupyter_notebook(script_blocks, gallery_conf, target_dir, real_func):
+    """Monkey-patch sphinx-gallery to add a TVM import block to each IPython notebook.
+
+    If we had only one import block, we could skip the patching and just set first_notebook_cell.
+    However, how we import TVM depends on if we are using a fixed or dev version, and whether we
+    will use the GPU.
+
+    We install a CUDA version of TVM if and only if the tutorial calls "cuda" as a function. It
+    would be cleaner specify this with a config flag, but sphinx-gallery does not support this.
+    """
+
+    call_cuda_re = re.compile(r"cuda\([0-9]*\)", re.IGNORECASE)

Review Comment:
   I agree this regex approach is gross. I've added a new sphinx-gallery flag to determine whether a tutorial requires CUDA, and I've set it in all relevant tutorials:
   ```python
   # sphinx_gallery_requires_cuda = True
   ```
   
   Unfortunately, monkey-patching the extra flag is a bit of a hack, but I still think it is nicer than the regex approach.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] guberti commented on a diff in pull request #13627: [docs] Add "Open with Colab" button to documentation

Posted by GitBox <gi...@apache.org>.
guberti commented on code in PR #13627:
URL: https://github.com/apache/tvm/pull/13627#discussion_r1063794348


##########
gallery/how_to/work_with_microtvm/install_zephyr.rst:
##########
@@ -0,0 +1,34 @@
+Install the Prerequisites
+----------------------------
+
+    .. code-block:: bash

Review Comment:
   There is now a comment clarifying this.



##########
apps/microtvm/poetry.lock:
##########
@@ -1,28 +1,9 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements.  See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership.  The ASF licenses this file
-# to you 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.
-
-# This `poetry.lock` file is generated from `poetry install`.
-
 [[package]]
 name = "absl-py"
-version = "1.2.0"
+version = "1.3.0"

Review Comment:
   Fixed.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] mehrdadh commented on a diff in pull request #13627: [docs] Add "Open with Colab" button to documentation

Posted by GitBox <gi...@apache.org>.
mehrdadh commented on code in PR #13627:
URL: https://github.com/apache/tvm/pull/13627#discussion_r1061965740


##########
apps/microtvm/pyproject.toml:
##########
@@ -98,22 +95,23 @@ onnxoptimizer = { version = "==0.2.6", optional = true }
 onnxruntime = { version = "==1.9.0", optional = true }
 
 # Pytorch (also used by ONNX)
-torch = { version = "==1.11.0", optional = true }
-torchvision = { version = "==0.12.0", optional = true }
+torch = { version = "==1.11.0" }

Review Comment:
   Looks like we no longer use requirement.txt file, so you can just revert changes in this file



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] guberti commented on a diff in pull request #13627: [docs] Add "Open with Colab" button to documentation

Posted by GitBox <gi...@apache.org>.
guberti commented on code in PR #13627:
URL: https://github.com/apache/tvm/pull/13627#discussion_r1063760366


##########
gallery/how_to/work_with_microtvm/install_dependencies.rst:
##########
@@ -0,0 +1,33 @@
+..  Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you 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.
+
+..  Boilerplate script for installing Zephyr in the microTVM
+    tutorials that use it. Does not show up as a separate file
+    on the documentation website.
+
+
+Install microTVM Python dependencies
+------------------------------------
+
+TVM does not include a package for Python serial communication, so
+we must install one before using microTVM. We will also need TFLite
+to load models.
+
+    .. code-block:: bash
+
+        %%shell

Review Comment:
   Yes - these are the only additional dependencies we need.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] tvm-bot commented on pull request #13627: [docs] Add "Open with Colab" button to documentation

Posted by GitBox <gi...@apache.org>.
tvm-bot commented on PR #13627:
URL: https://github.com/apache/tvm/pull/13627#issuecomment-1354023333

   <!---bot-comment-->
   
   Thanks for contributing to TVM! Please refer to the contributing guidelines https://tvm.apache.org/docs/contribute/ for useful information and tips. Please request code reviews from [Reviewers](https://github.com/apache/incubator-tvm/blob/master/CONTRIBUTORS.md#reviewers) by @-ing them in a comment.
   
   <!--bot-comment-ccs-start-->
    * No users to tag found in teams: `docs` <sub>See [#10317](https://github.com/apache/tvm/issues/10317) for details</sub><!--bot-comment-ccs-end-->
   
   <sub>Generated by [tvm-bot](https://github.com/apache/tvm/blob/main/ci/README.md#github-actions)</sub>


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] areusch commented on a diff in pull request #13627: [docs] Add "Open with Colab" button to documentation

Posted by GitBox <gi...@apache.org>.
areusch commented on code in PR #13627:
URL: https://github.com/apache/tvm/pull/13627#discussion_r1057808388


##########
docs/conf.py:
##########
@@ -84,6 +86,148 @@ def git_describe_version(original_version):
 version = git_describe_version(tvm.__version__)
 release = version
 
+
+def monkey_patch(module_name, func_name):

Review Comment:
   you might consider using `unittest.mock.patch` here since it's in the stdlib



##########
docs/conf.py:
##########
@@ -506,6 +650,12 @@ def process_docstring(app, what, name, obj, options, lines):
 from legacy_redirect import build_legacy_redirect
 
 
+def strip_ipython_magic(app, docname, source):

Review Comment:
   add a docstring comment just to clarify the function. should this be removing the whole line?



##########
gallery/how_to/work_with_microtvm/micro_tflite.py:
##########
@@ -26,109 +26,36 @@
 """
 
 ######################################################################
-# .. note::
-#     If you want to run this tutorial on the microTVM Reference VM, download the Jupyter
-#     notebook using the link at the bottom of this page and save it into the TVM directory. Then:
 #
-#     #. Login to the reference VM with a modified ``vagrant ssh`` command:
+#     .. include:: ../../../../gallery/how_to/work_with_microtvm/install_dependencies.rst
 #
-#         ``$ vagrant ssh -- -L8888:localhost:8888``
-#
-#     #. Install jupyter:  ``pip install jupyterlab``
-#     #. ``cd`` to the TVM directory.
-#     #. Install tflite: poetry install -E importer-tflite
-#     #. Launch Jupyter Notebook: ``jupyter notebook``
-#     #. Copy the localhost URL displayed, and paste it into your browser.
-#     #. Navigate to saved Jupyter Notebook (``.ipynb`` file).
-#
-#
-# Setup
-# -----
-#
-# Install TFLite
-# ^^^^^^^^^^^^^^
-#
-# To get started, TFLite package needs to be installed as prerequisite. You can do this in two ways:
-#
-# 1. Install tflite with ``pip``
-#
-#     .. code-block:: bash
-#
-#       pip install tflite=2.1.0 --user
-#
-# 2. Generate the TFLite package yourself. The steps are the following:
-#
-#     Get the flatc compiler.
-#     Please refer to https://github.com/google/flatbuffers for details
-#     and make sure it is properly installed.
-#
-#     .. code-block:: bash
-#
-#       flatc --version
-#
-#     Get the TFLite schema.
-#
-#     .. code-block:: bash
-#
-#       wget https://raw.githubusercontent.com/tensorflow/tensorflow/r1.13/tensorflow/lite/schema/schema.fbs
-#
-#     Generate TFLite package.
-#
-#     .. code-block:: bash
-#
-#       flatc --python schema.fbs
-#
-#     Add the current folder (which contains generated tflite module) to PYTHONPATH.
-#
-#     .. code-block:: bash
-#
-#       export PYTHONPATH=${PYTHONPATH:+$PYTHONPATH:}$(pwd)
-#
-# To validate that the TFLite package was installed successfully, ``python -c "import tflite"``
-#
-# Install Zephyr (physical hardware only)
-# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-#
-# When running this tutorial with a host simulation (the default), you can use the host ``gcc`` to
-# build a firmware image that simulates the device. When compiling to run on physical hardware, you
-# need to install a *toolchain* plus some target-specific dependencies. microTVM allows you to
-# supply any compiler and runtime that can launch the TVM RPC server, but to get started, this
-# tutorial relies on the Zephyr RTOS to provide these pieces.
-#
-# You can install Zephyr by following the
-# `Installation Instructions <https://docs.zephyrproject.org/latest/getting_started/index.html>`_.
-#
-# Aside: Recreating your own Pre-Trained TFLite model
-#  The tutorial downloads a pretrained TFLite model. When working with microcontrollers
-#  you need to be mindful these are highly resource constrained devices as such standard
-#  models like MobileNet may not fit into their modest memory.
-#
-#  For this tutorial, we'll make use of one of the TF Micro example models.
-#
-#  If you wish to replicate the training steps see:
-#  https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/micro/examples/hello_world/train
-#
-#    .. note::
-#
-#      If you accidentally download the example pretrained model from:
-#
-#      ``wget https://storage.googleapis.com/download.tensorflow.org/models/tflite/micro/hello_world_2020_04_13.zip``
-#
-#      this will fail due to an unimplemented opcode (114)
-#
-# Load and prepare the Pre-Trained Model
-# --------------------------------------
-#
-# Load the pretrained TFLite model from a file in your current
-# directory into a buffer
 
 # sphinx_gallery_start_ignore
 from tvm import testing
 
 testing.utils.install_request_hook(depth=3)
 # sphinx_gallery_end_ignore
 
+# You can skip the following two sections (installing Zephyr and CMSIS-NN) if the following flag is False.

Review Comment:
   could you explain why maybe?



##########
docs/conf.py:
##########
@@ -84,6 +86,148 @@ def git_describe_version(original_version):
 version = git_describe_version(tvm.__version__)
 release = version
 
+
+def monkey_patch(module_name, func_name):
+    """Helper function for monkey-patching library functions.
+
+    Used to modify a few sphinx-gallery behaviors to make the "Open in Colab"
+    button work correctly. Should be used as a decorator with arguments.
+    """
+    module = import_module(module_name)
+    original_func = getattr(module, func_name)
+
+    def decorator(function):
+        updated_func = partial(function, real_func=original_func)
+        setattr(module, func_name, updated_func)
+        return updated_func
+
+    return decorator
+
+
+# This header replaces the default sphinx-gallery one in sphinx_gallery/gen_rst.py.
+COLAB_HTML_HEADER = """
+.. DO NOT EDIT. THIS FILE WAS AUTOMATICALLY GENERATED BY
+.. TVM'S MONKEY-PATCHED VERSION OF SPHINX-GALLERY. TO MAKE
+.. CHANGES, EDIT THE SOURCE PYTHON FILE:
+.. "{0}"
+
+.. only:: html
+
+    .. note::
+        :class: sphx-glr-download-link-note
+
+        This tutorial can be used interactively with Google Colab! You can also click
+        :ref:`here <sphx_glr_download_{1}>` to run the Jupyter notebook locally.
+
+        .. image:: https://raw.githubusercontent.com/apache/web-data/main/images/utilities/colab_button.svg
+            :align: center
+            :target: {2}
+            :width: 300px
+
+.. rst-class:: sphx-glr-example-title
+
+.. _sphx_glr_{1}:
+
+"""
+
+# Google Colab allows opening .ipynb files on GitHub by appending the GitHub path to its own url.
+COLAB_URL_BASE = "https://colab.research.google.com/github"
+IPYTHON_GITHUB_BASE = "apache/tvm-site/blob/asf-site/docs/_downloads/"
+
+
+@monkey_patch("sphinx_gallery.gen_rst", "save_rst_example")
+def save_rst_example(example_rst, example_file, time_elapsed, memory_used, gallery_conf, real_func):
+    """Monkey-patch save_rst_example to include the "Open in Colab" button."""
+
+    # The url is the md5 hash of the notebook path.
+    example_fname = os.path.relpath(example_file, gallery_conf["src_dir"])
+    ref_fname = example_fname.replace(os.path.sep, "_")
+    notebook_path = example_fname[:-2] + "ipynb"
+    digest = md5(notebook_path.encode()).hexdigest()
+
+    # Fixed documentation versions must link to different (earlier) .ipynb notebooks.
+    colab_url = f"{COLAB_URL_BASE}/{IPYTHON_GITHUB_BASE}"
+    if "dev" not in version:
+        colab_url += version + "/"
+    colab_url += digest + "/" + os.path.basename(notebook_path)
+
+    new_header = COLAB_HTML_HEADER.format(example_fname, ref_fname, colab_url)
+    with patch("sphinx_gallery.gen_rst.EXAMPLE_HEADER", new_header):
+        real_func(example_rst, example_file, time_elapsed, memory_used, gallery_conf)
+
+
+@monkey_patch("sphinx_gallery.notebook", "rst2md")
+def rst2md(text, gallery_conf, target_dir, heading_levels, real_func):
+    """Monkey-patch rst2md to add limited include directive support to sphinx-gallery."""
+
+    include_re = re.compile(r"^([ \t]*)\.\. include::\s*(.+)\n", flags=re.M)
+
+    def load_include(match):
+        full_path = os.path.join(target_dir, match.group(2))
+        with open(full_path) as f:
+            lines = f.read()
+        indented = textwrap.indent(lines, match.group(1)) + "\n"
+        return indented
+
+    text = re.sub(include_re, load_include, text)
+
+    return real_func(text, gallery_conf, target_dir, heading_levels)
+
+
+INSTALL_TVM_DEV = f"""%%shell
+# Installs the latest dev build of TVM from PyPI. If you wish to build
+# from source, see https://tvm.apache.org/docs/install/from_source.html
+pip install apache-tvm --pre"""
+
+INSTALL_TVM_FIXED = f"""%%shell
+# Installs TVM version {version} from PyPI. If you wish to build
+# from source, see https://tvm.apache.org/docs/install/from_source.html
+pip install apache-tvm=={version}"""
+
+INSTALL_TVM_CUDA_DEV = f"""%%shell
+# Installs the latest dev build of TVM from PyPI, with CUDA enabled. To use this,
+# you must request a Google Colab instance with a GPU by going to Runtime ->
+# Change runtime type -> Hardware accelerator -> GPU. If you wish to build from
+# source, see see https://tvm.apache.org/docs/install/from_source.html
+pip install tlcpack-nightly-cu113 --pre -f https://tlcpack.ai/wheels"""
+
+INSTALL_TVM_CUDA_FIXED = f"""%%shell
+# Installs TVM version {version} from PyPI, with CUDA enabled. To use this,
+# you must request a Google Colab instance with a GPU by going to Runtime ->
+# Change runtime type -> Hardware accelerator -> GPU. If you wish to build from
+# source, see see https://tvm.apache.org/docs/install/from_source.html
+pip install apache-tvm-cu113=={version} -f https://tlcpack.ai/wheels"""

Review Comment:
   i wonder if `version == "*"` if this could work for CUDA_DEV too



##########
apps/microtvm/pyproject.toml:
##########
@@ -98,22 +95,23 @@ onnxoptimizer = { version = "==0.2.6", optional = true }
 onnxruntime = { version = "==1.9.0", optional = true }
 
 # Pytorch (also used by ONNX)
-torch = { version = "==1.11.0", optional = true }
-torchvision = { version = "==0.12.0", optional = true }
+torch = { version = "==1.11.0" }

Review Comment:
   how come you removed `optional = true` here? we use this so that you don't have to install these packages, but may opt to do so with e.g. `poetry install -E importer-torch`



##########
docs/conf.py:
##########
@@ -84,6 +86,148 @@ def git_describe_version(original_version):
 version = git_describe_version(tvm.__version__)
 release = version
 
+
+def monkey_patch(module_name, func_name):
+    """Helper function for monkey-patching library functions.
+
+    Used to modify a few sphinx-gallery behaviors to make the "Open in Colab"
+    button work correctly. Should be used as a decorator with arguments.
+    """
+    module = import_module(module_name)
+    original_func = getattr(module, func_name)
+
+    def decorator(function):
+        updated_func = partial(function, real_func=original_func)
+        setattr(module, func_name, updated_func)
+        return updated_func
+
+    return decorator
+
+
+# This header replaces the default sphinx-gallery one in sphinx_gallery/gen_rst.py.
+COLAB_HTML_HEADER = """
+.. DO NOT EDIT. THIS FILE WAS AUTOMATICALLY GENERATED BY
+.. TVM'S MONKEY-PATCHED VERSION OF SPHINX-GALLERY. TO MAKE
+.. CHANGES, EDIT THE SOURCE PYTHON FILE:
+.. "{0}"

Review Comment:
   suggest using a named reference e.g. {SOURCE_FILE} instead of {0} 
   since this comment string is so long. here and below



##########
docs/conf.py:
##########
@@ -84,6 +86,148 @@ def git_describe_version(original_version):
 version = git_describe_version(tvm.__version__)
 release = version
 
+
+def monkey_patch(module_name, func_name):
+    """Helper function for monkey-patching library functions.
+
+    Used to modify a few sphinx-gallery behaviors to make the "Open in Colab"
+    button work correctly. Should be used as a decorator with arguments.
+    """
+    module = import_module(module_name)
+    original_func = getattr(module, func_name)
+
+    def decorator(function):
+        updated_func = partial(function, real_func=original_func)
+        setattr(module, func_name, updated_func)
+        return updated_func
+
+    return decorator
+
+
+# This header replaces the default sphinx-gallery one in sphinx_gallery/gen_rst.py.
+COLAB_HTML_HEADER = """
+.. DO NOT EDIT. THIS FILE WAS AUTOMATICALLY GENERATED BY
+.. TVM'S MONKEY-PATCHED VERSION OF SPHINX-GALLERY. TO MAKE
+.. CHANGES, EDIT THE SOURCE PYTHON FILE:
+.. "{0}"
+
+.. only:: html
+
+    .. note::
+        :class: sphx-glr-download-link-note
+
+        This tutorial can be used interactively with Google Colab! You can also click
+        :ref:`here <sphx_glr_download_{1}>` to run the Jupyter notebook locally.
+
+        .. image:: https://raw.githubusercontent.com/apache/web-data/main/images/utilities/colab_button.svg
+            :align: center
+            :target: {2}
+            :width: 300px
+
+.. rst-class:: sphx-glr-example-title
+
+.. _sphx_glr_{1}:
+
+"""
+
+# Google Colab allows opening .ipynb files on GitHub by appending the GitHub path to its own url.
+COLAB_URL_BASE = "https://colab.research.google.com/github"
+IPYTHON_GITHUB_BASE = "apache/tvm-site/blob/asf-site/docs/_downloads/"
+
+
+@monkey_patch("sphinx_gallery.gen_rst", "save_rst_example")
+def save_rst_example(example_rst, example_file, time_elapsed, memory_used, gallery_conf, real_func):
+    """Monkey-patch save_rst_example to include the "Open in Colab" button."""
+
+    # The url is the md5 hash of the notebook path.
+    example_fname = os.path.relpath(example_file, gallery_conf["src_dir"])
+    ref_fname = example_fname.replace(os.path.sep, "_")
+    notebook_path = example_fname[:-2] + "ipynb"
+    digest = md5(notebook_path.encode()).hexdigest()
+
+    # Fixed documentation versions must link to different (earlier) .ipynb notebooks.
+    colab_url = f"{COLAB_URL_BASE}/{IPYTHON_GITHUB_BASE}"
+    if "dev" not in version:
+        colab_url += version + "/"
+    colab_url += digest + "/" + os.path.basename(notebook_path)
+
+    new_header = COLAB_HTML_HEADER.format(example_fname, ref_fname, colab_url)
+    with patch("sphinx_gallery.gen_rst.EXAMPLE_HEADER", new_header):
+        real_func(example_rst, example_file, time_elapsed, memory_used, gallery_conf)
+
+
+@monkey_patch("sphinx_gallery.notebook", "rst2md")
+def rst2md(text, gallery_conf, target_dir, heading_levels, real_func):
+    """Monkey-patch rst2md to add limited include directive support to sphinx-gallery."""
+
+    include_re = re.compile(r"^([ \t]*)\.\. include::\s*(.+)\n", flags=re.M)
+
+    def load_include(match):
+        full_path = os.path.join(target_dir, match.group(2))
+        with open(full_path) as f:
+            lines = f.read()
+        indented = textwrap.indent(lines, match.group(1)) + "\n"
+        return indented
+
+    text = re.sub(include_re, load_include, text)
+
+    return real_func(text, gallery_conf, target_dir, heading_levels)
+
+
+INSTALL_TVM_DEV = f"""%%shell
+# Installs the latest dev build of TVM from PyPI. If you wish to build
+# from source, see https://tvm.apache.org/docs/install/from_source.html
+pip install apache-tvm --pre"""
+
+INSTALL_TVM_FIXED = f"""%%shell
+# Installs TVM version {version} from PyPI. If you wish to build
+# from source, see https://tvm.apache.org/docs/install/from_source.html
+pip install apache-tvm=={version}"""
+
+INSTALL_TVM_CUDA_DEV = f"""%%shell
+# Installs the latest dev build of TVM from PyPI, with CUDA enabled. To use this,
+# you must request a Google Colab instance with a GPU by going to Runtime ->
+# Change runtime type -> Hardware accelerator -> GPU. If you wish to build from
+# source, see see https://tvm.apache.org/docs/install/from_source.html
+pip install tlcpack-nightly-cu113 --pre -f https://tlcpack.ai/wheels"""
+
+INSTALL_TVM_CUDA_FIXED = f"""%%shell
+# Installs TVM version {version} from PyPI, with CUDA enabled. To use this,
+# you must request a Google Colab instance with a GPU by going to Runtime ->
+# Change runtime type -> Hardware accelerator -> GPU. If you wish to build from
+# source, see see https://tvm.apache.org/docs/install/from_source.html
+pip install apache-tvm-cu113=={version} -f https://tlcpack.ai/wheels"""
+
+
+@monkey_patch("sphinx_gallery.gen_rst", "jupyter_notebook")
+def jupyter_notebook(script_blocks, gallery_conf, target_dir, real_func):
+    """Monkey-patch sphinx-gallery to add a TVM import block to each IPython notebook.
+
+    If we had only one import block, we could skip the patching and just set first_notebook_cell.
+    However, how we import TVM depends on if we are using a fixed or dev version, and whether we
+    will use the GPU.
+
+    We install a CUDA version of TVM if and only if the tutorial calls "cuda" as a function. It
+    would be cleaner specify this with a config flag, but sphinx-gallery does not support this.
+    """
+
+    call_cuda_re = re.compile(r"cuda\([0-9]*\)", re.IGNORECASE)

Review Comment:
   could you make this a constant with a comment describing the expected line it's looking for? Also, I'm wondering if we should look for something more explicit, like a tag at the top saying:
   ```
   # Requires: cuda115
   ```
   
   or something



##########
docs/conf.py:
##########
@@ -84,6 +86,148 @@ def git_describe_version(original_version):
 version = git_describe_version(tvm.__version__)
 release = version
 
+
+def monkey_patch(module_name, func_name):
+    """Helper function for monkey-patching library functions.
+
+    Used to modify a few sphinx-gallery behaviors to make the "Open in Colab"
+    button work correctly. Should be used as a decorator with arguments.
+    """
+    module = import_module(module_name)
+    original_func = getattr(module, func_name)
+
+    def decorator(function):
+        updated_func = partial(function, real_func=original_func)
+        setattr(module, func_name, updated_func)
+        return updated_func
+
+    return decorator
+
+
+# This header replaces the default sphinx-gallery one in sphinx_gallery/gen_rst.py.
+COLAB_HTML_HEADER = """
+.. DO NOT EDIT. THIS FILE WAS AUTOMATICALLY GENERATED BY
+.. TVM'S MONKEY-PATCHED VERSION OF SPHINX-GALLERY. TO MAKE
+.. CHANGES, EDIT THE SOURCE PYTHON FILE:
+.. "{0}"
+
+.. only:: html
+
+    .. note::
+        :class: sphx-glr-download-link-note
+
+        This tutorial can be used interactively with Google Colab! You can also click
+        :ref:`here <sphx_glr_download_{1}>` to run the Jupyter notebook locally.
+
+        .. image:: https://raw.githubusercontent.com/apache/web-data/main/images/utilities/colab_button.svg
+            :align: center
+            :target: {2}
+            :width: 300px
+
+.. rst-class:: sphx-glr-example-title
+
+.. _sphx_glr_{1}:
+
+"""
+
+# Google Colab allows opening .ipynb files on GitHub by appending the GitHub path to its own url.
+COLAB_URL_BASE = "https://colab.research.google.com/github"
+IPYTHON_GITHUB_BASE = "apache/tvm-site/blob/asf-site/docs/_downloads/"

Review Comment:
   can you comment this one?



##########
docs/conf.py:
##########
@@ -84,6 +86,148 @@ def git_describe_version(original_version):
 version = git_describe_version(tvm.__version__)
 release = version
 
+
+def monkey_patch(module_name, func_name):
+    """Helper function for monkey-patching library functions.
+
+    Used to modify a few sphinx-gallery behaviors to make the "Open in Colab"
+    button work correctly. Should be used as a decorator with arguments.
+    """
+    module = import_module(module_name)
+    original_func = getattr(module, func_name)
+
+    def decorator(function):
+        updated_func = partial(function, real_func=original_func)
+        setattr(module, func_name, updated_func)
+        return updated_func
+
+    return decorator
+
+
+# This header replaces the default sphinx-gallery one in sphinx_gallery/gen_rst.py.
+COLAB_HTML_HEADER = """
+.. DO NOT EDIT. THIS FILE WAS AUTOMATICALLY GENERATED BY
+.. TVM'S MONKEY-PATCHED VERSION OF SPHINX-GALLERY. TO MAKE
+.. CHANGES, EDIT THE SOURCE PYTHON FILE:
+.. "{0}"
+
+.. only:: html
+
+    .. note::
+        :class: sphx-glr-download-link-note
+
+        This tutorial can be used interactively with Google Colab! You can also click
+        :ref:`here <sphx_glr_download_{1}>` to run the Jupyter notebook locally.
+
+        .. image:: https://raw.githubusercontent.com/apache/web-data/main/images/utilities/colab_button.svg
+            :align: center
+            :target: {2}
+            :width: 300px
+
+.. rst-class:: sphx-glr-example-title
+
+.. _sphx_glr_{1}:
+
+"""
+
+# Google Colab allows opening .ipynb files on GitHub by appending the GitHub path to its own url.
+COLAB_URL_BASE = "https://colab.research.google.com/github"
+IPYTHON_GITHUB_BASE = "apache/tvm-site/blob/asf-site/docs/_downloads/"
+
+
+@monkey_patch("sphinx_gallery.gen_rst", "save_rst_example")
+def save_rst_example(example_rst, example_file, time_elapsed, memory_used, gallery_conf, real_func):
+    """Monkey-patch save_rst_example to include the "Open in Colab" button."""
+
+    # The url is the md5 hash of the notebook path.
+    example_fname = os.path.relpath(example_file, gallery_conf["src_dir"])
+    ref_fname = example_fname.replace(os.path.sep, "_")
+    notebook_path = example_fname[:-2] + "ipynb"
+    digest = md5(notebook_path.encode()).hexdigest()
+
+    # Fixed documentation versions must link to different (earlier) .ipynb notebooks.
+    colab_url = f"{COLAB_URL_BASE}/{IPYTHON_GITHUB_BASE}"
+    if "dev" not in version:
+        colab_url += version + "/"
+    colab_url += digest + "/" + os.path.basename(notebook_path)
+
+    new_header = COLAB_HTML_HEADER.format(example_fname, ref_fname, colab_url)
+    with patch("sphinx_gallery.gen_rst.EXAMPLE_HEADER", new_header):
+        real_func(example_rst, example_file, time_elapsed, memory_used, gallery_conf)
+
+
+@monkey_patch("sphinx_gallery.notebook", "rst2md")
+def rst2md(text, gallery_conf, target_dir, heading_levels, real_func):
+    """Monkey-patch rst2md to add limited include directive support to sphinx-gallery."""
+
+    include_re = re.compile(r"^([ \t]*)\.\. include::\s*(.+)\n", flags=re.M)
+
+    def load_include(match):
+        full_path = os.path.join(target_dir, match.group(2))
+        with open(full_path) as f:
+            lines = f.read()
+        indented = textwrap.indent(lines, match.group(1)) + "\n"
+        return indented
+
+    text = re.sub(include_re, load_include, text)
+
+    return real_func(text, gallery_conf, target_dir, heading_levels)
+
+
+INSTALL_TVM_DEV = f"""%%shell

Review Comment:
   one thing i kind of like to do is 
   ```
   """\
   %%shell
   ```
   then it's a bit harder to forget to see the "%%shell" part



##########
gallery/how_to/compile_models/from_coreml.py:
##########
@@ -23,13 +23,12 @@
 
 This article is an introductory tutorial to deploy CoreML models with Relay.
 
-For us to begin with, coremltools module is required to be installed.
-
-A quick solution is to install via pip
+To begin, we must install coremltools:
 
 .. code-block:: bash
 
-    pip install -U coremltools --user

Review Comment:
   i'm curious, does `--user` not work with colab? It's not the worst thing to tell folks new to managing python venvs.



##########
gallery/how_to/work_with_microtvm/micro_tflite.py:
##########
@@ -292,7 +217,18 @@
 
 if use_physical_hw:
     template_project_path = pathlib.Path(tvm.micro.get_microtvm_template_projects("zephyr"))
-    project_options = {"project_type": "host_driven", "board": BOARD, "serial_number": SERIAL}
+    project_options = {
+        "project_type": "host_driven",
+        "board": BOARD,
+        "serial_number": SERIAL,
+        "config_main_stack_size": 4096,
+        "cmsis_path": "/content/cmsis"

Review Comment:
   add parens around the ternary, here and below



##########
gallery/how_to/work_with_microtvm/micro_aot.py:
##########
@@ -139,6 +162,12 @@
         "board": BOARD,
         "serial_number": SERIAL,
         "config_main_stack_size": 4096,
+        "cmsis_path": "/content/cmsis"

Review Comment:
   add parens around the ternary, here and below



##########
gallery/how_to/work_with_microtvm/install_cmsis.rst:
##########
@@ -0,0 +1,30 @@
+..  Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you 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.
+Install CMSIS-NN

Review Comment:
   should we add some clarifying text that this is for CoLab only, in case folks run across it as a standalone doc? or is it not linked from the tutorial index and won't be found through auto-indexing?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] mehrdadh commented on a diff in pull request #13627: [docs] Add "Open with Colab" button to documentation

Posted by GitBox <gi...@apache.org>.
mehrdadh commented on code in PR #13627:
URL: https://github.com/apache/tvm/pull/13627#discussion_r1061965045


##########
gallery/how_to/work_with_microtvm/micro_aot.py:
##########
@@ -30,6 +30,29 @@
 or on Zephyr platform on a microcontroller/board supported by Zephyr.
 """
 
+######################################################################
+#
+#     .. include:: ../../../../gallery/how_to/work_with_microtvm/install_zephyr.rst

Review Comment:
   This file should be added to all microTVM Zephyr tutorials. I think some of them are missing in this PR. That's why we refactored it to a separate file



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] mehrdadh commented on a diff in pull request #13627: [docs] Add "Open with Colab" button to documentation

Posted by GitBox <gi...@apache.org>.
mehrdadh commented on code in PR #13627:
URL: https://github.com/apache/tvm/pull/13627#discussion_r1060822861


##########
gallery/how_to/work_with_microtvm/install_dependencies.rst:
##########
@@ -0,0 +1,33 @@
+..  Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you 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.
+
+..  Boilerplate script for installing Zephyr in the microTVM
+    tutorials that use it. Does not show up as a separate file
+    on the documentation website.
+
+
+Install microTVM Python dependencies
+------------------------------------
+
+TVM does not include a package for Python serial communication, so
+we must install one before using microTVM. We will also need TFLite
+to load models.
+
+    .. code-block:: bash
+
+        %%shell

Review Comment:
   @guberti are these the only dependencies for microTVM tutorials?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] alanmacd commented on a diff in pull request #13627: [docs] Add "Open with Colab" button to documentation

Posted by GitBox <gi...@apache.org>.
alanmacd commented on code in PR #13627:
URL: https://github.com/apache/tvm/pull/13627#discussion_r1060871485


##########
gallery/how_to/work_with_microtvm/micro_aot.py:
##########
@@ -30,16 +30,40 @@
 or on Zephyr platform on a microcontroller/board supported by Zephyr.
 """
 
+######################################################################
+#
+#     .. include:: ../../../../gallery/how_to/work_with_microtvm/install_dependencies.rst
+#
+
 # sphinx_gallery_start_ignore
 from tvm import testing
 
 testing.utils.install_request_hook(depth=3)
 # sphinx_gallery_end_ignore
 
+# You can skip the following two sections (installing Zephyr and CMSIS-NN) if the following flag is False.
+# Installing Zephyr takes ~20 min.
+import os
+
+use_physical_hw = bool(os.getenv("TVM_MICRO_USE_HW"))

Review Comment:
   I think this could be worded / setup clearer for the user, since the code doesn't check `use_physical_hw` (I assume at least in this section) it's unclear who is doing the skipping.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] guberti commented on a diff in pull request #13627: [DRAFT][docs] Add "Open with Colab" button to documentation

Posted by GitBox <gi...@apache.org>.
guberti commented on code in PR #13627:
URL: https://github.com/apache/tvm/pull/13627#discussion_r1057398674


##########
docs/conf.py:
##########
@@ -84,6 +84,100 @@ def git_describe_version(original_version):
 version = git_describe_version(tvm.__version__)
 release = version
 
+
+# Generate the
+COLAB_HTML_HEADER = """
+.. DO NOT EDIT.
+.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
+.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
+.. "{0}"
+.. LINE NUMBERS ARE GIVEN BELOW.
+
+.. only:: html
+
+    .. note::
+        :class: sphx-glr-download-link-note
+
+        This tutorial can be used interactively with Google Colab! You can also click
+        :ref:`here <sphx_glr_download_{1}>` to run the Jupyter notebook locally.
+
+        .. image:: https://raw.githubusercontent.com/guberti/web-data/main/images/utilities/colab_button.svg

Review Comment:
   During the Hackathon, I redid this image to be a cleaner SVG. I've got a PR out to update this and remove the `png` version - see https://github.com/tlc-pack/web-data/pull/18.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] guberti commented on a diff in pull request #13627: [DRAFT][docs] Add "Open with Colab" button to documentation

Posted by GitBox <gi...@apache.org>.
guberti commented on code in PR #13627:
URL: https://github.com/apache/tvm/pull/13627#discussion_r1057398808


##########
gallery/how_to/work_with_microtvm/install_zephyr.py:
##########
@@ -0,0 +1,29 @@
+%%shell

Review Comment:
   These are now reStructuredText files.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] guberti commented on a diff in pull request #13627: [docs] Add "Open with Colab" button to documentation

Posted by GitBox <gi...@apache.org>.
guberti commented on code in PR #13627:
URL: https://github.com/apache/tvm/pull/13627#discussion_r1063765019


##########
gallery/how_to/work_with_microtvm/micro_aot.py:
##########
@@ -30,16 +30,40 @@
 or on Zephyr platform on a microcontroller/board supported by Zephyr.
 """
 
+######################################################################
+#
+#     .. include:: ../../../../gallery/how_to/work_with_microtvm/install_dependencies.rst
+#
+
 # sphinx_gallery_start_ignore
 from tvm import testing
 
 testing.utils.install_request_hook(depth=3)
 # sphinx_gallery_end_ignore
 
+# You can skip the following two sections (installing Zephyr and CMSIS-NN) if the following flag is False.
+# Installing Zephyr takes ~20 min.
+import os
+
+use_physical_hw = bool(os.getenv("TVM_MICRO_USE_HW"))

Review Comment:
   Expanded on this comment a bit.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] guberti commented on a diff in pull request #13627: [docs] Add "Open with Colab" button to documentation

Posted by GitBox <gi...@apache.org>.
guberti commented on code in PR #13627:
URL: https://github.com/apache/tvm/pull/13627#discussion_r1063753264


##########
docs/conf.py:
##########
@@ -84,6 +86,148 @@ def git_describe_version(original_version):
 version = git_describe_version(tvm.__version__)
 release = version
 
+
+def monkey_patch(module_name, func_name):

Review Comment:
   I agree that `reset_modules` is a valid approach, but I think going that route would make the changes less readable. Permanently altering `sphinx-gallery` is not desirable, but it may be the cleanest option other than forking `sphinx-gallery`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] guberti commented on a diff in pull request #13627: [DRAFT][docs] Add "Open with Colab" button to documentation

Posted by GitBox <gi...@apache.org>.
guberti commented on code in PR #13627:
URL: https://github.com/apache/tvm/pull/13627#discussion_r1057399680


##########
gallery/how_to/work_with_microtvm/micro_train.py:
##########
@@ -71,7 +60,7 @@
 #
 #     .. code-block:: bash
 #
-#       %%bash
+#       %%shell

Review Comment:
   Both do similar, but slightly different things. `%%bash` is the "true" IPython directive, but it doesn't display the script's output in real time. `%%shell` is specific to Google's extension of IPython, but it **does** display the script output in real time.
   
   I've used `%%shell` in these tutorials, as these Jupyter magic commands won't show up in the HTML files anyway, and `%%shell` makes the "Open in Colab" function work better. But if people feel strongly, I'd be OK using `%%bash` instead. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] guberti commented on a diff in pull request #13627: [docs] Add "Open with Colab" button to documentation

Posted by GitBox <gi...@apache.org>.
guberti commented on code in PR #13627:
URL: https://github.com/apache/tvm/pull/13627#discussion_r1058000486


##########
gallery/how_to/work_with_microtvm/install_cmsis.rst:
##########
@@ -0,0 +1,30 @@
+..  Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you 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.
+Install CMSIS-NN

Review Comment:
   This is not linked in the tutorial index and won't be found unless someone goes digging through the code. I'll add a comment though to clarify this.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] areusch commented on a diff in pull request #13627: [docs] Add "Open with Colab" button to documentation

Posted by GitBox <gi...@apache.org>.
areusch commented on code in PR #13627:
URL: https://github.com/apache/tvm/pull/13627#discussion_r1060783055


##########
docs/conf.py:
##########
@@ -84,6 +86,148 @@ def git_describe_version(original_version):
 version = git_describe_version(tvm.__version__)
 release = version
 
+
+def monkey_patch(module_name, func_name):

Review Comment:
   ahh i see. potentially you could theoretically override sphinx-gallery's `reset_modules` and use the [optionally-passed `when` parameter](https://github.com/sphinx-gallery/sphinx-gallery/blob/4484d35bb556bfe081156f210904abce8732ffbf/sphinx_gallery/scrapers.py#L592) to decide whether to monkey-patch or un-monkey-patch. i agree it's sort of equivalent both ways, i just wish there was a way to avoid "poisoning" sphinx-gallery permanently by importing conf.py. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] guberti commented on a diff in pull request #13627: [docs] Add "Open with Colab" button to documentation

Posted by GitBox <gi...@apache.org>.
guberti commented on code in PR #13627:
URL: https://github.com/apache/tvm/pull/13627#discussion_r1057978156


##########
docs/conf.py:
##########
@@ -84,6 +86,148 @@ def git_describe_version(original_version):
 version = git_describe_version(tvm.__version__)
 release = version
 
+
+def monkey_patch(module_name, func_name):

Review Comment:
   Using stdlib functions is always good, but `unittest.mock.patch` does not quite fit this use case. I can't use `@patch` as a decorator, as the decorator must be used on the function **calling** the function we wish to monkey-patch (e.g. `save_rst_example`).
   
   I could just call `patch` as a function afterwards, but `patch` also does not include functionality for wrapping functions. It would be possible to make `patch()` work, but I think writing our own decorator is cleaner.
   
   I've clarified the comment to better explain this.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] areusch commented on a diff in pull request #13627: [docs] Add "Open with Colab" button to documentation

Posted by GitBox <gi...@apache.org>.
areusch commented on code in PR #13627:
URL: https://github.com/apache/tvm/pull/13627#discussion_r1060859591


##########
gallery/how_to/compile_models/from_coreml.py:
##########
@@ -23,13 +23,12 @@
 
 This article is an introductory tutorial to deploy CoreML models with Relay.
 
-For us to begin with, coremltools module is required to be installed.
-
-A quick solution is to install via pip
+To begin, we must install coremltools:
 
 .. code-block:: bash
 
-    pip install -U coremltools --user

Review Comment:
   ah yeah just wanted to understand the why here



##########
gallery/how_to/work_with_microtvm/install_cmsis.rst:
##########
@@ -0,0 +1,30 @@
+..  Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you 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.
+Install CMSIS-NN

Review Comment:
   thanks!



##########
gallery/how_to/work_with_microtvm/micro_aot.py:
##########
@@ -139,6 +162,12 @@
         "board": BOARD,
         "serial_number": SERIAL,
         "config_main_stack_size": 4096,
+        "cmsis_path": "/content/cmsis"

Review Comment:
   this one still needs doing



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] guberti commented on a diff in pull request #13627: [docs] Add "Open with Colab" button to documentation

Posted by GitBox <gi...@apache.org>.
guberti commented on code in PR #13627:
URL: https://github.com/apache/tvm/pull/13627#discussion_r1063747387


##########
apps/microtvm/pyproject.toml:
##########
@@ -98,22 +95,23 @@ onnxoptimizer = { version = "==0.2.6", optional = true }
 onnxruntime = { version = "==1.9.0", optional = true }
 
 # Pytorch (also used by ONNX)
-torch = { version = "==1.11.0", optional = true }
-torchvision = { version = "==0.12.0", optional = true }
+torch = { version = "==1.11.0" }

Review Comment:
   Changes to `pyproject.toml` and `poetry.lock` have been reverted.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] guberti commented on a diff in pull request #13627: [DRAFT][docs] Add "Open with Colab" button to documentation

Posted by GitBox <gi...@apache.org>.
guberti commented on code in PR #13627:
URL: https://github.com/apache/tvm/pull/13627#discussion_r1057398849


##########
apps/microtvm/poetry.lock:
##########
@@ -1,28 +1,9 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements.  See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership.  The ASF licenses this file
-# to you 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.
-
-# This `poetry.lock` file is generated from `poetry install`.
-
 [[package]]
 name = "absl-py"
-version = "1.2.0"
+version = "1.3.0"

Review Comment:
   cc @mehrdadh.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] guberti commented on pull request #13627: [DRAFT][docs] Add "Open with Colab" button to documentation

Posted by GitBox <gi...@apache.org>.
guberti commented on PR #13627:
URL: https://github.com/apache/tvm/pull/13627#issuecomment-1365551558

   @echuraev thanks for the very fast review! We weren't quite done with the PR at the time, so a few of your comments are now outdated. I've addressed the others, though - thanks for the feedback!
   
   You also asked about CPU versus GPU runtimes for Google Colab. Unfortunately, these can only be selected from the drop-down menu. However, I modified `docs/conf.py` to look for usage of `cuda` - now, we install a CUDA-enabled TVM version if and only if the tutorial requires it.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] guberti commented on a diff in pull request #13627: [docs] Add "Open with Colab" button to documentation

Posted by GitBox <gi...@apache.org>.
guberti commented on code in PR #13627:
URL: https://github.com/apache/tvm/pull/13627#discussion_r1057997928


##########
gallery/how_to/compile_models/from_coreml.py:
##########
@@ -23,13 +23,12 @@
 
 This article is an introductory tutorial to deploy CoreML models with Relay.
 
-For us to begin with, coremltools module is required to be installed.
-
-A quick solution is to install via pip
+To begin, we must install coremltools:
 
 .. code-block:: bash
 
-    pip install -U coremltools --user

Review Comment:
   `--user` works as you would expect in Colab. However, the other tutorials don't use that flag for their dependencies, so I changed this to be consistent. Might be out of scope - what do you think?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] alanmacd commented on a diff in pull request #13627: [docs] Add "Open with Colab" button to documentation

Posted by GitBox <gi...@apache.org>.
alanmacd commented on code in PR #13627:
URL: https://github.com/apache/tvm/pull/13627#discussion_r1060871485


##########
gallery/how_to/work_with_microtvm/micro_aot.py:
##########
@@ -30,16 +30,40 @@
 or on Zephyr platform on a microcontroller/board supported by Zephyr.
 """
 
+######################################################################
+#
+#     .. include:: ../../../../gallery/how_to/work_with_microtvm/install_dependencies.rst
+#
+
 # sphinx_gallery_start_ignore
 from tvm import testing
 
 testing.utils.install_request_hook(depth=3)
 # sphinx_gallery_end_ignore
 
+# You can skip the following two sections (installing Zephyr and CMSIS-NN) if the following flag is False.
+# Installing Zephyr takes ~20 min.
+import os
+
+use_physical_hw = bool(os.getenv("TVM_MICRO_USE_HW"))

Review Comment:
   I think this could be worded / setup clearer for the user, since the code doesn't check `use_physical_hw` (I assume) it's unclear who is doing the skipping.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] mehrdadh commented on a diff in pull request #13627: [docs] Add "Open with Colab" button to documentation

Posted by GitBox <gi...@apache.org>.
mehrdadh commented on code in PR #13627:
URL: https://github.com/apache/tvm/pull/13627#discussion_r1061964453


##########
apps/microtvm/poetry.lock:
##########
@@ -1,28 +1,9 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements.  See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership.  The ASF licenses this file
-# to you 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.
-
-# This `poetry.lock` file is generated from `poetry install`.
-
 [[package]]
 name = "absl-py"
-version = "1.2.0"
+version = "1.3.0"

Review Comment:
   This was a hack, please revert it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] alanmacd commented on a diff in pull request #13627: [docs] Add "Open with Colab" button to documentation

Posted by GitBox <gi...@apache.org>.
alanmacd commented on code in PR #13627:
URL: https://github.com/apache/tvm/pull/13627#discussion_r1060862812


##########
gallery/how_to/compile_models/from_onnx.py:
##########
@@ -21,13 +21,12 @@
 
 This article is an introductory tutorial to deploy ONNX models with Relay.
 
-For us to begin with, ONNX package must be installed.
-
-A quick solution is to install protobuf compiler, and
+For us to begin with, ONNX package must be installed:

Review Comment:
   nit: match the wording update of the other files:
   
   ```suggestion
   To begin, install the ONNX package:
   ```
   or something like that..



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] echuraev commented on a diff in pull request #13627: [docs] Add "Open with Colab" button to documentation

Posted by GitBox <gi...@apache.org>.
echuraev commented on code in PR #13627:
URL: https://github.com/apache/tvm/pull/13627#discussion_r1050370999


##########
docs/conf.py:
##########
@@ -249,76 +343,76 @@ def git_describe_version(original_version):
 # The unlisted files are sorted by filenames.
 # The unlisted files always appear after listed files.
 within_subsection_order = {
-    "tutorial": [
-        "introduction.py",
-        "install.py",
-        "tvmc_command_line_driver.py",
-        "tvmc_python.py",
-        "autotvm_relay_x86.py",
-        "tensor_expr_get_started.py",
-        "autotvm_matmul_x86.py",
-        "auto_scheduler_matmul_x86.py",
-        "tensor_ir_blitz_course.py",
-        "topi.pi",
-        "cross_compilation_and_rpc.py",
-        "relay_quick_start.py",
-        "uma.py",
-    ],
-    "compile_models": [
-        "from_pytorch.py",
-        "from_tensorflow.py",
-        "from_mxnet.py",
-        "from_onnx.py",
-        "from_keras.py",
-        "from_tflite.py",
-        "from_coreml.py",
-        "from_darknet.py",
-        "from_caffe2.py",
-        "from_paddle.py",
-    ],
-    "work_with_schedules": [
-        "schedule_primitives.py",
-        "reduction.py",
-        "intrin_math.py",
-        "scan.py",
-        "extern_op.py",
-        "tensorize.py",
-        "tuple_inputs.py",
-        "tedd.py",
-    ],
-    "optimize_operators": [
-        "opt_gemm.py",
-        "opt_conv_cuda.py",
-        "opt_conv_tensorcore.py",
-    ],
-    "tune_with_autotvm": [
-        "tune_conv2d_cuda.py",
-        "tune_relay_cuda.py",
-        "tune_relay_x86.py",
-        "tune_relay_arm.py",
-        "tune_relay_mobile_gpu.py",
-    ],
-    "tune_with_autoscheduler": [
-        "tune_matmul_x86.py",
-        "tune_conv2d_layer_cuda.py",
-        "tune_network_x86.py",
-        "tune_network_cuda.py",
-    ],
-    "extend_tvm": [
-        "low_level_custom_pass.py",
-        "use_pass_infra.py",
-        "use_pass_instrument.py",
-        "bring_your_own_datatypes.py",
-    ],
+    # "tutorial": [

Review Comment:
   Why did you comment it?



##########
docs/conf.py:
##########
@@ -84,6 +84,100 @@ def git_describe_version(original_version):
 version = git_describe_version(tvm.__version__)
 release = version
 
+
+# Generate the
+COLAB_HTML_HEADER = """
+.. DO NOT EDIT.
+.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
+.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
+.. "{0}"
+.. LINE NUMBERS ARE GIVEN BELOW.
+
+.. only:: html
+
+    .. note::
+        :class: sphx-glr-download-link-note
+
+        This tutorial can be used interactively with Google Colab! You can also click
+        :ref:`here <sphx_glr_download_{1}>` to run the Jupyter notebook locally.
+
+        .. image:: https://raw.githubusercontent.com/guberti/web-data/main/images/utilities/colab_button.svg

Review Comment:
   Not sure about this path to the image. Probably, we should upload this image file to https://github.com/tlc-pack/web-data



##########
gallery/how_to/work_with_microtvm/install_zephyr.py:
##########
@@ -0,0 +1,29 @@
+%%shell

Review Comment:
   Probably it would be better to format this script as a documentation file in sphinx format?



##########
tests/lint/check_request_hook.py:
##########
@@ -142,7 +142,7 @@ def find_code_block_line(lines: List[str]) -> Optional[int]:
                 else:
                     actual, expected = line_info
                     print(f"{file} (misplaced hook at {actual}, expected at {expected})")
-            exit(1)
+            #exit(1)

Review Comment:
   Why did you comment this line?



##########
docs/conf.py:
##########
@@ -84,6 +84,100 @@ def git_describe_version(original_version):
 version = git_describe_version(tvm.__version__)
 release = version
 
+
+# Generate the
+COLAB_HTML_HEADER = """
+.. DO NOT EDIT.
+.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
+.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
+.. "{0}"
+.. LINE NUMBERS ARE GIVEN BELOW.
+
+.. only:: html
+
+    .. note::
+        :class: sphx-glr-download-link-note
+
+        This tutorial can be used interactively with Google Colab! You can also click
+        :ref:`here <sphx_glr_download_{1}>` to run the Jupyter notebook locally.
+
+        .. image:: https://raw.githubusercontent.com/guberti/web-data/main/images/utilities/colab_button.svg

Review Comment:
   While I had reviewed your PR, I found that the same image file was already used in `gallery/how_to/work_with_microtvm/micro_train.py`. So probably, you can reuse this link: https://raw.githubusercontent.com/tlc-pack/web-data/main/images/utilities/colab_button.png



##########
docs/Makefile:
##########
@@ -20,7 +20,7 @@
 
 # You can set these variables from the command line.
 SPHINXOPTS    =
-SPHINXBUILD   = python3 -m sphinx
+SPHINXBUILD   = python3.7 -m sphinx

Review Comment:
   Why we need to specify exact version of python instead of `python3`?



##########
gallery/how_to/work_with_microtvm/micro_aot.py:
##########
@@ -30,6 +30,29 @@
 or on Zephyr platform on a microcontroller/board supported by Zephyr.
 """
 
+######################################################################
+#
+#     .. include:: ../../../../gallery/how_to/work_with_microtvm/install_zephyr.rst

Review Comment:
   Tried to find where else you have included this `install_zephyr.rst`. But didn't find it. Probably you can just move the context of `install_zephyr.rst` to this file?



##########
gallery/how_to/work_with_microtvm/install_zephyr.rst:
##########
@@ -0,0 +1,34 @@
+Install the Prerequisites
+----------------------------
+
+    .. code-block:: bash

Review Comment:
   The same question here. Probably we should design this file as a documentation and add more text information? Because it looks more like a bash script for now.



##########
gallery/how_to/work_with_microtvm/micro_train.py:
##########
@@ -71,7 +60,7 @@
 #
 #     .. code-block:: bash
 #
-#       %%bash
+#       %%shell

Review Comment:
   What is the difference between `%%bash` and `%%shell`?



##########
apps/microtvm/poetry.lock:
##########
@@ -1,28 +1,9 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements.  See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership.  The ASF licenses this file
-# to you 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.
-
-# This `poetry.lock` file is generated from `poetry install`.
-
 [[package]]
 name = "absl-py"
-version = "1.2.0"
+version = "1.3.0"

Review Comment:
   How these changes in `apps/microtvm` are related to adding "Open with Colab" button?



##########
gallery/how_to/compile_models/from_keras.py:
##########
@@ -102,8 +102,8 @@
 shape_dict = {"input_1": data.shape}
 mod, params = relay.frontend.from_keras(keras_resnet50, shape_dict)
 # compile the model
-target = "cuda"
-dev = tvm.cuda(0)
+target = "llvm"
+dev = tvm.cpu(0)

Review Comment:
   I know that colab provides several runtimes (`CPU` or `GPU`), which can be selected manually. Can we automatically select preferable runtime for some scripts, e.g. where we want to use `GPU`?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org