Geoffrey Martin-Noble | fb7f7d1 | 2022-12-12 16:10:36 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Scott Todd | b5b9026 | 2021-06-28 16:29:43 -0700 | [diff] [blame] | 2 | # Copyright 2021 The IREE Authors |
| 3 | # |
| 4 | # Licensed under the Apache License v2.0 with LLVM Exceptions. |
| 5 | # See https://llvm.org/LICENSE.txt for license information. |
| 6 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | |
| 8 | import glob |
Stella Laurenzo | 95b3150 | 2022-05-09 09:15:50 -0700 | [diff] [blame] | 9 | import logging |
Scott Todd | b5b9026 | 2021-06-28 16:29:43 -0700 | [diff] [blame] | 10 | import os |
| 11 | import subprocess |
| 12 | import unittest |
| 13 | |
Scott Todd | f0636f4 | 2023-05-17 10:49:30 -0700 | [diff] [blame] | 14 | NOTEBOOKS_TO_SKIP = [ |
Scott Todd | bd97cc5 | 2023-10-23 16:14:35 -0700 | [diff] [blame] | 15 | # matplotlib error when testing: |
| 16 | # FileNotFoundError: [Errno 2] No such file or directory: 'seaborn-whitegrid' |
| 17 | # support level for TF-code and samples is also low |
| 18 | "tensorflow_mnist_training.ipynb", |
Scott Todd | 326aca2 | 2023-08-31 17:11:39 -0700 | [diff] [blame] | 19 | # tflite_runtime requires some deps ("version `GLIBC_2.29' not found") that |
Scott Todd | f0636f4 | 2023-05-17 10:49:30 -0700 | [diff] [blame] | 20 | # samples.Dockerfile does not currently include. |
| 21 | "tflite_text_classification.ipynb", |
Scott Todd | c397258 | 2023-10-13 15:18:33 -0700 | [diff] [blame] | 22 | # PyTorch notebooks using SHARK-Turbine require Python 3.10+ in Docker. |
Scott Todd | 2f47c08 | 2023-11-09 11:03:36 -0800 | [diff] [blame] | 23 | "pytorch_aot_advanced.ipynb", |
Scott Todd | c397258 | 2023-10-13 15:18:33 -0700 | [diff] [blame] | 24 | "pytorch_aot_simple.ipynb", |
Scott Todd | b3cd60a | 2023-10-12 09:40:30 -0700 | [diff] [blame] | 25 | "pytorch_jit.ipynb", |
Scott Todd | f0636f4 | 2023-05-17 10:49:30 -0700 | [diff] [blame] | 26 | ] |
Scott Todd | b5b9026 | 2021-06-28 16:29:43 -0700 | [diff] [blame] | 27 | |
Scott Todd | 1941216 | 2022-01-19 11:51:29 -0800 | [diff] [blame] | 28 | NOTEBOOKS_EXPECTED_TO_FAIL = [ |
Scott Todd | 326aca2 | 2023-08-31 17:11:39 -0700 | [diff] [blame] | 29 | # Error: |
| 30 | # ``` |
| 31 | # module 'tensorflow.python.pywrap_mlir' has no attribute |
| 32 | # 'experimental_convert_saved_model_v1' |
| 33 | # ``` |
| 34 | # convert_saved_model_v1 may be broken, but convert_saved_model works? |
| 35 | "tensorflow_hub_import.ipynb", |
Scott Todd | 9d0a179 | 2023-12-14 11:19:49 -0800 | [diff] [blame] | 36 | # error: 'stablehlo.pad' op attribute 'edge_padding_low' failed to satisfy |
| 37 | # constraint: 64-bit signless integer elements attribute |
| 38 | "tensorflow_resnet.ipynb", |
Scott Todd | 1941216 | 2022-01-19 11:51:29 -0800 | [diff] [blame] | 39 | ] |
Scott Todd | b5b9026 | 2021-06-28 16:29:43 -0700 | [diff] [blame] | 40 | |
| 41 | |
Stella Laurenzo | 95b3150 | 2022-05-09 09:15:50 -0700 | [diff] [blame] | 42 | class ColabNotebookTests(unittest.TestCase): |
Jakub Kuderski | be24f02 | 2023-06-21 14:44:18 -0400 | [diff] [blame] | 43 | """Tests running all Colab notebooks in this directory.""" |
Scott Todd | b5b9026 | 2021-06-28 16:29:43 -0700 | [diff] [blame] | 44 | |
Jakub Kuderski | be24f02 | 2023-06-21 14:44:18 -0400 | [diff] [blame] | 45 | @classmethod |
| 46 | def generateTests(cls): |
| 47 | repo_root = os.path.dirname( |
| 48 | os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 49 | ) |
| 50 | script_path = os.path.join( |
| 51 | repo_root, "build_tools/testing/run_python_notebook.sh" |
| 52 | ) |
Scott Todd | b5b9026 | 2021-06-28 16:29:43 -0700 | [diff] [blame] | 53 | |
Jakub Kuderski | be24f02 | 2023-06-21 14:44:18 -0400 | [diff] [blame] | 54 | # Create a test case for each notebook in this folder. |
| 55 | notebooks_path = os.path.join(repo_root, "samples/colab/") |
| 56 | for notebook_path in glob.glob(notebooks_path + "*.ipynb"): |
| 57 | notebook_name = os.path.basename(notebook_path) |
Scott Todd | b5b9026 | 2021-06-28 16:29:43 -0700 | [diff] [blame] | 58 | |
Jakub Kuderski | be24f02 | 2023-06-21 14:44:18 -0400 | [diff] [blame] | 59 | def unit_test(self, notebook_path=notebook_path): |
| 60 | completed_process = subprocess.run([script_path, notebook_path]) |
| 61 | self.assertEqual(completed_process.returncode, 0) |
Scott Todd | b5b9026 | 2021-06-28 16:29:43 -0700 | [diff] [blame] | 62 | |
Jakub Kuderski | be24f02 | 2023-06-21 14:44:18 -0400 | [diff] [blame] | 63 | if notebook_name in NOTEBOOKS_TO_SKIP: |
| 64 | unit_test = unittest.skip("Skip requested")(unit_test) |
| 65 | elif notebook_name in NOTEBOOKS_EXPECTED_TO_FAIL: |
| 66 | unit_test = unittest.expectedFailure(unit_test) |
Scott Todd | b5b9026 | 2021-06-28 16:29:43 -0700 | [diff] [blame] | 67 | |
Jakub Kuderski | be24f02 | 2023-06-21 14:44:18 -0400 | [diff] [blame] | 68 | # Add 'unit_test' to this class, so the test runner runs it. |
| 69 | unit_test.__name__ = f"test_{notebook_name}" |
| 70 | setattr(cls, unit_test.__name__, unit_test) |
Scott Todd | b5b9026 | 2021-06-28 16:29:43 -0700 | [diff] [blame] | 71 | |
| 72 | |
| 73 | if __name__ == "__main__": |
Jakub Kuderski | be24f02 | 2023-06-21 14:44:18 -0400 | [diff] [blame] | 74 | ColabNotebookTests.generateTests() |
| 75 | logging.basicConfig(level=logging.DEBUG) |
| 76 | unittest.main() |