Skip to content

Commit

Permalink
Use papermill for running tutorials (#1706)
Browse files Browse the repository at this point in the history
Summary:
## Motivation

We are using nbconvert to run tutorials. nbconvert is not really made for this use case, but papermill is, so we have some handwritten code than can be handled by papermill. With papermill, we can go a bit further and use SMOKE_TEST as a [parameter](https://papermill.readthedocs.io/en/latest/usage-parameterize.html) rather than an environment variable. That would make it easy for people to work with the tutorials as notebooks.

Pull Request resolved: #1706

Test Plan:
Ran tutorials locally and made sure smoke-test flag was getting used appropriately.

## Related pull requests

Enabling papermill will make #1703, which automates running a notebook, a bit easier.

Reviewed By: saitcakmak

Differential Revision: D43631568

Pulled By: esantorella

fbshipit-source-id: e7bfeb68e221fff4f1633af8deb9a11d1ff1c0e6
  • Loading branch information
esantorella authored and facebook-github-bot committed Mar 9, 2023
1 parent af93f40 commit eaa6fb2
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 22 deletions.
25 changes: 3 additions & 22 deletions scripts/run_tutorials.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@
import datetime
import os
import subprocess
import tempfile
import time
from pathlib import Path
from subprocess import CalledProcessError
from typing import Any, Dict, Optional, Tuple

import nbformat
import pandas as pd
from memory_profiler import memory_usage
from nbconvert import PythonExporter


IGNORE_ALWAYS = { # ignored in smoke tests and full runs
Expand Down Expand Up @@ -64,33 +61,18 @@ def get_output_file_path(smoke_test: bool) -> str:
return fname


def parse_ipynb(file: Path) -> str:
with open(file, "r") as nb_file:
nb_str = nb_file.read()
nb = nbformat.reads(nb_str, nbformat.NO_CONVERT)
exporter = PythonExporter()
script, _ = exporter.from_notebook_node(nb)
return script


def run_script(
script: str, timeout_minutes: int, env: Optional[Dict[str, str]] = None
tutorial: Path, timeout_minutes: int, env: Optional[Dict[str, str]] = None
) -> None:
# need to keep the file around & close it so subprocess does not run into I/O issues
with tempfile.NamedTemporaryFile(delete=False) as tf:
tf_name = tf.name
with open(tf_name, "w") as tmp_script:
tmp_script.write(script)
if env is not None:
env = {**os.environ, **env}
run_out = subprocess.run(
["ipython", tf_name],
["papermill", tutorial, "|"],
capture_output=True,
text=True,
env=env,
timeout=timeout_minutes * 60,
)
os.remove(tf_name)
return run_out


Expand All @@ -102,13 +84,12 @@ def run_tutorial(
them as a string, and returns runtime and memory information as a dict.
"""
timeout_minutes = 5 if smoke_test else 30
script = parse_ipynb(tutorial)
tic = time.monotonic()
print(f"Running tutorial {tutorial.name}.")
env = {"SMOKE_TEST": "True"} if smoke_test else None
try:
mem_usage, run_out = memory_usage(
(run_script, (script, timeout_minutes), {"env": env}),
(run_script, (tutorial, timeout_minutes), {"env": env}),
retval=True,
include_children=True,
)
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"kaleido",
"matplotlib",
"memory_profiler",
"papermill",
"pykeops",
"torchvision",
]
Expand Down

0 comments on commit eaa6fb2

Please sign in to comment.