From 9c0133c74426a6220487227af4b0b2599bc6b3e7 Mon Sep 17 00:00:00 2001 From: Scott Main Date: Wed, 28 Aug 2024 23:02:44 -0700 Subject: [PATCH] Remove examples of deprecated max CLI tools The `max` CLI is deprecated in v24.5 and will be removed in a future version. MODULAR_ORIG_COMMIT_REV_ID: 46a294310d8732239c5f4c2a6141d559e3af0ab8 --- README.md | 10 +- examples/README.md | 10 -- examples/notebooks/gemma2b-python-onnx.ipynb | 2 +- .../notebooks/mistral7b-python-onnx.ipynb | 4 +- .../notebooks/roberta-python-pytorch.ipynb | 8 +- examples/tools/README.md | 46 ------ examples/tools/benchmark-pytorch/.gitignore | 1 - .../tools/benchmark-pytorch/pyproject.toml | 22 --- examples/tools/benchmark-pytorch/run.sh | 28 ---- .../common/openclip-onnx/download-model.py | 138 ------------------ .../common/openclip-onnx/download-model.sh | 33 ----- .../common/resnet50-pytorch/download-model.py | 132 ----------------- .../common/resnet50-pytorch/download-model.sh | 33 ----- .../common/resnet50-pytorch/input-spec.yaml | 17 --- .../tools/common/roberta-pytorch/.gitignore | 1 - .../common/roberta-pytorch/download-model.py | 134 ----------------- .../common/roberta-pytorch/download-model.sh | 33 ----- .../common/roberta-pytorch/input-spec.yaml | 29 ---- examples/tools/visualize-pytorch/.gitignore | 1 - .../tools/visualize-pytorch/pyproject.toml | 22 --- examples/tools/visualize-pytorch/run.sh | 30 ---- 21 files changed, 11 insertions(+), 723 deletions(-) delete mode 100644 examples/tools/README.md delete mode 100644 examples/tools/benchmark-pytorch/.gitignore delete mode 100644 examples/tools/benchmark-pytorch/pyproject.toml delete mode 100755 examples/tools/benchmark-pytorch/run.sh delete mode 100755 examples/tools/common/openclip-onnx/download-model.py delete mode 100755 examples/tools/common/openclip-onnx/download-model.sh delete mode 100755 examples/tools/common/resnet50-pytorch/download-model.py delete mode 100755 examples/tools/common/resnet50-pytorch/download-model.sh delete mode 100644 examples/tools/common/resnet50-pytorch/input-spec.yaml delete mode 100644 examples/tools/common/roberta-pytorch/.gitignore delete mode 100755 examples/tools/common/roberta-pytorch/download-model.py delete mode 100755 examples/tools/common/roberta-pytorch/download-model.sh delete mode 100644 examples/tools/common/roberta-pytorch/input-spec.yaml delete mode 100644 examples/tools/visualize-pytorch/.gitignore delete mode 100644 examples/tools/visualize-pytorch/pyproject.toml delete mode 100755 examples/tools/visualize-pytorch/run.sh diff --git a/README.md b/README.md index 15f1e9d9..6e8bea09 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ your experiences and chat with the team and other community members. In the [examples directory](https://github.com/modularml/max/tree/main/examples), you will find code examples and Jupyter notebooks that show how to run inference -with MAX, and demos using the benchmarking and visualization tools. +with MAX. ## Getting Started @@ -60,12 +60,8 @@ that they can be customized, built upon, or learned from. ### Examples In addition to the end-to-end pipelines, there are many [examples](./examples/) -that exercise various aspects of MAX, from -[performing inference using PyTorch and ONNX models](./examples/inference/) to -[demonstrating command-line tooling capabilities](./examples/tools/). - -You can follow the instructions in the README for each example, -notebook or tool you want to run. +that exercise various aspects of MAX. You can follow the instructions in the +README for each example or notebook you want to run. ### Notebooks diff --git a/examples/README.md b/examples/README.md index 9c454c9c..9f5355c2 100644 --- a/examples/README.md +++ b/examples/README.md @@ -26,13 +26,3 @@ Stay tuned for an improved extensibility API that works on both CPUs and GPUs. Jupyter notebooks that showcase PyTorch and ONNX models being accelerated through MAX. - -## [Performance showcase](performance-showcase/) - -A head-to-head benchmark of inference performance for common models that -demonstrates the advantages of running them with MAX. - -## [Command-line tools](tools/) - -MAX provides useful command-line tooling, and these examples demonstrate the -capabilities of these tools. diff --git a/examples/notebooks/gemma2b-python-onnx.ipynb b/examples/notebooks/gemma2b-python-onnx.ipynb index 60b2b431..3efa3688 100644 --- a/examples/notebooks/gemma2b-python-onnx.ipynb +++ b/examples/notebooks/gemma2b-python-onnx.ipynb @@ -15,7 +15,7 @@ "\n", "### Requirements\n", "\n", - "1. Please make sure you have installed `max` as described in our [getting started](https://docs.modular.com/engine/get-started/) guide\n", + "1. Please make sure you have installed `max` as described in our [getting started](https://docs.modular.com/max/get-started/) guide\n", "2. Valid [huggingface access token](https://huggingface.co/docs/hub/security-tokens)\n", "3. Gemma-2b is a gated model. Make sure you have accepted the [terms of use](https://huggingface.co/google/gemma-2b) to be able to continue\n", "4. **The model size is 13G**. Please make sure you have enough disk space to download the model and for the converted ONNX counterpart as we will use them later in this tutorial.\n", diff --git a/examples/notebooks/mistral7b-python-onnx.ipynb b/examples/notebooks/mistral7b-python-onnx.ipynb index b0e8cdbd..1b7f83ae 100644 --- a/examples/notebooks/mistral7b-python-onnx.ipynb +++ b/examples/notebooks/mistral7b-python-onnx.ipynb @@ -15,9 +15,9 @@ "**Caveats:**\n", "\n", "* **The model size is 28G**. Please make sure you have enough disk space to download the model and for the converted ONNX counterpart as we will use them later in this tutorial.\n", - "* **The runtime memory requirment is around 65G** to be able finish the notebook and run all the cells.\n", + "* **The runtime memory requirement is around 65G** to be able finish the notebook and run all the cells.\n", "\n", - "First, make sure you have installed `max` as described in the [getting started](https://docs.staging.modular.com/engine/get-started/) as well as `PyTorch` and `transformers` packages" + "First, make sure you have installed `max` as described in the [getting started](https://docs.modular.com/max/get-started/) as well as `PyTorch` and `transformers` packages" ] }, { diff --git a/examples/notebooks/roberta-python-pytorch.ipynb b/examples/notebooks/roberta-python-pytorch.ipynb index ec28c2bc..9a5cf5d6 100644 --- a/examples/notebooks/roberta-python-pytorch.ipynb +++ b/examples/notebooks/roberta-python-pytorch.ipynb @@ -66,7 +66,7 @@ "installed with the `modular` CLI tool.\n", "\n", "For instructions, see\n", - "[Get started with MAX Engine](https://docs.modular.com/engine/get-started)." + "[Get started with MAX Engine](https://docs.modular.com/max/get-started)." ] }, { @@ -357,9 +357,11 @@ "source": [ "Ta-da! 🎉\n", "\n", - "If you're running this notebook yourself, beware that this notebook does not illustrate MAX Engine's runtime performance. For actual benchmark results, try [our benchmark tool](https://docs.modular.com/engine/benchmark) or check out our [performance dashboard](https://performance.modular.com).\n", + "If you're running this notebook yourself, beware that this notebook does not\n", + "illustrate MAX Engine's runtime performance. For actual benchmark results,\n", + "check out our [performance dashboard](https://performance.modular.com).\n", "\n", - "For more details about the inferencing API, see the [Python API reference](https://docs.modular.com/engine/reference/python/engine)." + "For more details about the inferencing API, see the [Python API reference](https://docs.modular.com/max/api/python/engine)." ] } ], diff --git a/examples/tools/README.md b/examples/tools/README.md deleted file mode 100644 index 6a15adda..00000000 --- a/examples/tools/README.md +++ /dev/null @@ -1,46 +0,0 @@ -# MAX Engine Tools - -MAX comes bundled with a super-handy `max` CLI tool that lets you perform a -couple of common tasks on many ML models without writing a single line of code. - -We currently provide two tools: - -- `benchmark`: Benchmark models with MLPerf load generation. -- `visualize`: Show a model graph as interpreted by MAX Engine in Netron. - -Try one out with `benchmark-pytorch/run.sh` or `visualize-pytorch/run.sh` to -benchmark or visualize a PyTorch model. - -## What's going on? - -Either of these scripts start by downloading a ResNet50 model (a -model typically used for image classification) and converting it into the -proper format -- for PyTorch, this is [TorchScript]. This model will be stored -in `../models/resnet50.torchscript`. - -TorchScript does not include any metadata for shapes the model expects as -input, so we need to provide an `--input-data-schema`. For this ResNet-50 -model, we've already written one for you in -`common/resnet50-pytorch/input-spec.yaml`, which looks like this: - -```yaml -inputs: - - input_name: pixel_values - shape: 1x3x224x224xf32 - compile_shape: ?x3x224x224xf32 -``` - -Benchmarking and visualization works as follows: - -```sh -% max benchmark --input-data-schema=common/resnet50-pytorch/input-spec.yaml ../models/resnet50.torchscript -% # Benchmarking results will be printed by above command -% max visualize --input-data-schema=common/resnet50-pytorch/input-spec.yaml ../models/resnet50.torchscript -% # Above command will generate a file that can be opened by netron.app -``` - -Learn more about `max benchmark`, `max visualize`, and input data schemas in -the [MAX documentation]. - - [TorchScript]: https://pytorch.org/docs/stable/jit.html - [MAX documentation]: https://docs.modular.com/engine/ diff --git a/examples/tools/benchmark-pytorch/.gitignore b/examples/tools/benchmark-pytorch/.gitignore deleted file mode 100644 index 72ee1c78..00000000 --- a/examples/tools/benchmark-pytorch/.gitignore +++ /dev/null @@ -1 +0,0 @@ -mlperf_log_* diff --git a/examples/tools/benchmark-pytorch/pyproject.toml b/examples/tools/benchmark-pytorch/pyproject.toml deleted file mode 100644 index ae3a332c..00000000 --- a/examples/tools/benchmark-pytorch/pyproject.toml +++ /dev/null @@ -1,22 +0,0 @@ -[project] -name = "benchmark-pytorch" -version = "1.0.0" -description = "MAX Examples" -requires-python = ">=3.9,<3.13" -authors = [{ name = "Modular", email = "hello@modular.com" }] -dependencies = ["torch==2.4.0"] - -[build-system] -requires = ["setuptools"] -build-backend = "setuptools.build_meta" - -[tool.pixi.project] -channels = ["conda-forge", "https://conda.modular.com/max-nightly"] -platforms = ["osx-arm64", "linux-aarch64", "linux-64"] - -[tool.pixi.pypi-dependencies] -benchmark-pytorch = { path = ".", editable = true } - -[tool.pixi.dependencies] -max = ">=24.4.0,<25" -numpy = "<2.0" diff --git a/examples/tools/benchmark-pytorch/run.sh b/examples/tools/benchmark-pytorch/run.sh deleted file mode 100755 index a62e8c1a..00000000 --- a/examples/tools/benchmark-pytorch/run.sh +++ /dev/null @@ -1,28 +0,0 @@ -#!/bin/bash -##===----------------------------------------------------------------------===## -# Copyright (c) 2024, Modular Inc. All rights reserved. -# -# Licensed under the Apache License v2.0 with LLVM Exceptions: -# https://llvm.org/LICENSE.txt -# -# 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. -##===----------------------------------------------------------------------===## - -# If anything goes wrong, stop running the script. -set -e - -# Make sure we're running from inside the directory containing this file. -cd "$(dirname "$0")" - -# If RoBERTa hasn't been downloaded yet, download it. -if ! [ -f ../../models/roberta.torchscript ]; then - ../common/roberta-pytorch/download-model.sh -o ../../models/roberta.torchscript -fi - -# Now for the easy part -- benchmarking ;) -# PyTorch models require --input-data-schema to be specified. -max benchmark --input-data-schema=../common/roberta-pytorch/input-spec.yaml ../../models/roberta.torchscript diff --git a/examples/tools/common/openclip-onnx/download-model.py b/examples/tools/common/openclip-onnx/download-model.py deleted file mode 100755 index 05324fe5..00000000 --- a/examples/tools/common/openclip-onnx/download-model.py +++ /dev/null @@ -1,138 +0,0 @@ -#!/usr/bin/env python3 -# ===----------------------------------------------------------------------=== # -# Copyright (c) 2024, Modular Inc. All rights reserved. -# -# Licensed under the Apache License v2.0 with LLVM Exceptions: -# https://llvm.org/LICENSE.txt -# -# 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. -# ===----------------------------------------------------------------------=== # - -"""Download OpenCLIP ONNX model from HuggingFace.""" - -import argparse -import os -import sys -import tempfile -import traceback -from pathlib import Path - -INSTALL_PROSE = """Is it installed? - -If not, try: - - python3 -m venv venv; source venv/bin/activate # if you aren't already in a virtual environment - python3 -m pip install torch - -""" - - -def log_fatal(prefix, msg_fn): - with tempfile.NamedTemporaryFile( - mode="w", prefix=prefix, suffix=".log", delete=False - ) as log_file: - traceback.print_exc(file=log_file) - print(msg_fn(log_file), file=sys.stderr) - sys.exit(1) - - -def main(): - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument( - "-o", - "--output_dir", - help="directory to save ONNX model to", - required=True, - ) - args = parser.parse_args() - - output_dir = Path(args.output_dir) - model_onnx_path = output_dir / "clip.onnx" - model_out_path = output_dir / "clip-dynamic-quint8.onnx" - if os.path.exists(model_onnx_path): - print(f"output {model_onnx_path!r} already exists") - return - if os.path.exists(model_out_path): - print(f"output {model_out_path!r} already exists") - return - - print("Importing PyTorch...", flush=True) - try: - import torch - except ModuleNotFoundError: - log_fatal( - "torchimport", - lambda log_file: f"Torch module was not found. {INSTALL_PROSE}Detailed error info in {log_file.name}", - ) - except ImportError: - log_fatal( - "torchimport", - lambda log_file: f"PyTorch installation seems to be broken.\nMake sure you can 'import torch' from a Python prompt and try again.\nDetailed error info in {log_file.name}", - ) - - print("Importing ONNX Runtime...", flush=True) - try: - from onnxruntime.quantization import QuantType, quantize_dynamic - except ModuleNotFoundError: - log_fatal( - "onnxruntime", - lambda log_file: f"ONNX Runtime module was not found. {INSTALL_PROSE}Detailed error info in {log_file.name}", - ) - except ImportError: - log_fatal( - "onnxruntime", - lambda log_file: f"ONNX Runtime installation seems to be broken.\nMake sure you can 'import onnxruntime' from a Python prompt and try again.\nDetailed error info in {log_file.name}", - ) - - print("Importing OpenCLIP...", flush=True) - try: - import open_clip - except ModuleNotFoundError: - log_fatal( - "openclip", - lambda log_file: f"OpenCLIP module was not found. {INSTALL_PROSE}Detailed error info in {log_file.name}", - ) - except ImportError: - log_fatal( - "openclip", - lambda log_file: f"OpenCLIP installation seems to be broken.\nMake sure you can 'import open_clip' from a Python prompt and try again.\nDetailed error info in {log_file.name}", - ) - - print("Getting pre-trained model...", flush=True) - model, _, preprocess = open_clip.create_model_and_transforms( - "ViT-B-32", pretrained="laion2b_s34b_b79k" - ) - tokenizer = open_clip.get_tokenizer("ViT-B-32") - print("Exporting ONNX...", flush=True) - image_size = model.visual.image_size - image = torch.randn(10, 3, image_size[0], image_size[1]) - text = tokenizer(["cat", "dog", "fish"]) - text = text.int() - with open(model_onnx_path, "wb") as out_fstream: - torch.onnx.export( - model, - (image, text), - model_onnx_path, - dynamic_axes={"image": {0: "batch"}}, - verbose=True, - input_names=["image", "text"], - output_names=["image_features", "text_features", "logit_scale"], - ) - print("Quantizing ONNX...", flush=True) - quantize_dynamic( - model_onnx_path, - model_out_path, - optimize_model=True, - per_channel=False, - weight_type=QuantType.QUInt8, - extra_options={"WeightSymmetric": False}, - ) - print("All done!", flush=True) - - -if __name__ == "__main__": - main() diff --git a/examples/tools/common/openclip-onnx/download-model.sh b/examples/tools/common/openclip-onnx/download-model.sh deleted file mode 100755 index c4995e79..00000000 --- a/examples/tools/common/openclip-onnx/download-model.sh +++ /dev/null @@ -1,33 +0,0 @@ -#!/bin/bash -##===----------------------------------------------------------------------===## -# Copyright (c) 2024, Modular Inc. All rights reserved. -# -# Licensed under the Apache License v2.0 with LLVM Exceptions: -# https://llvm.org/LICENSE.txt -# -# 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. -##===----------------------------------------------------------------------===## -# Wrapper around download-model.py that sets up a temporary venv & installs -# dependencies if needed. - -set -e - -script="$(dirname "$0")/download-model.py" - -# Check to see if PyTorch, ONNX Runtime, and OpenCLIP are installed. -if python3 -c 'import torch; import onnxruntime; import open_clip' >&/dev/null; then - python3 "$script" "$@" -else - # Required dependencies not installed -- set up a venv and install them - # in there, then run the script (cleaning up the venv afterwards). - venv_dir="$(mktemp -d)" - python3 -m venv "$venv_dir" - "$venv_dir/bin/pip" install -U setuptools pip wheel - "$venv_dir/bin/pip" install torch onnxruntime open_clip_torch - "$venv_dir/bin/python3" "$script" "$@" - rm -rf "$venv_dir" -fi diff --git a/examples/tools/common/resnet50-pytorch/download-model.py b/examples/tools/common/resnet50-pytorch/download-model.py deleted file mode 100755 index 01ce474f..00000000 --- a/examples/tools/common/resnet50-pytorch/download-model.py +++ /dev/null @@ -1,132 +0,0 @@ -#!/usr/bin/env python3 -# ===----------------------------------------------------------------------=== # -# Copyright (c) 2024, Modular Inc. All rights reserved. -# -# Licensed under the Apache License v2.0 with LLVM Exceptions: -# https://llvm.org/LICENSE.txt -# -# 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. -# ===----------------------------------------------------------------------=== # - -"""Download ResNet-50 PyTorch model from HuggingFace.""" - -import argparse -import os -import sys -import tempfile -import traceback - - -SOURCE_URL = ( - "https://huggingface.co/microsoft/resnet-50/resolve/main/pytorch_model.bin" -) - -INSTALL_PROSE = """Is it installed? - -If not, try: - - python3 -m venv venv; source venv/bin/activate # if you aren't already in a virtual environment - python3 -m pip install torch - -""" - - -def main(): - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument( - "-o", - "--output", - help="path to save TorchScript model to", - required=True, - ) - args = parser.parse_args() - - if os.path.exists(args.output): - print(f"output {args.output!r} already exists") - return - - print("Importing PyTorch...", flush=True) - try: - import torch - except ModuleNotFoundError: - with tempfile.NamedTemporaryFile( - mode="w", prefix="torchimport", suffix=".log", delete=False - ) as log_file: - traceback.print_exc(file=log_file) - print( - ( - f"Torch module was not found. {INSTALL_PROSE}" - f"Detailed error info in {log_file.name}" - ), - file=sys.stderr, - ) - sys.exit(1) - except ImportError: - with tempfile.NamedTemporaryFile( - mode="w", prefix="torchimport", suffix=".log", delete=False - ) as log_file: - traceback.print_exc(file=log_file) - print( - ( - "PyTorch installation seems to be broken.\n" - "Make sure you can 'import torch' " - "from a Python prompt and try again.\n" - f"Detailed error info in {log_file.name}" - ), - file=sys.stderr, - ) - sys.exit(1) - - print("Importing HuggingFace transformers...", flush=True) - try: - from transformers import AutoModelForImageClassification - except ModuleNotFoundError: - with tempfile.NamedTemporaryFile( - mode="w", prefix="hfimport", suffix=".log", delete=False - ) as log_file: - traceback.print_exc(file=log_file) - print( - ( - "HuggingFace transformers module was not found. " - f" {INSTALL_PROSE}Detailed error info in {log_file.name}" - ), - file=sys.stderr, - ) - sys.exit(1) - except ImportError: - with tempfile.NamedTemporaryFile( - mode="w", prefix="hfimport", suffix=".log", delete=False - ) as log_file: - traceback.print_exc(file=log_file) - print( - ( - "HuggingFace transformers installation seems to be broken.\n" - "Make sure you can 'import transformers' " - "from a Python prompt and try again.\n" - f"Detailed error info in {log_file.name}" - ), - file=sys.stderr, - ) - sys.exit(1) - - torch.set_default_device("cpu") - - print("Getting pre-trained model...", flush=True) - model = AutoModelForImageClassification.from_pretrained( - "microsoft/resnet-50" - ) - print("Tracing model...", flush=True) - traced_model = torch.jit.trace( - model, [torch.zeros(1, 3, 224, 224)], strict=False - ) - print("Saving TorchScript...", flush=True) - traced_model.save(args.output) - print("All done!", flush=True) - - -if __name__ == "__main__": - main() diff --git a/examples/tools/common/resnet50-pytorch/download-model.sh b/examples/tools/common/resnet50-pytorch/download-model.sh deleted file mode 100755 index 53155ec1..00000000 --- a/examples/tools/common/resnet50-pytorch/download-model.sh +++ /dev/null @@ -1,33 +0,0 @@ -#!/bin/bash -##===----------------------------------------------------------------------===## -# Copyright (c) 2024, Modular Inc. All rights reserved. -# -# Licensed under the Apache License v2.0 with LLVM Exceptions: -# https://llvm.org/LICENSE.txt -# -# 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. -##===----------------------------------------------------------------------===## -# Wrapper around download-model.py that sets up a temporary venv & installs -# dependencies if needed. - -set -e - -script="$(dirname "$0")/download-model.py" - -# Check to see if PyTorch and HuggingFace Transformers are installed. -if python3 -c 'import torch; import transformers' >& /dev/null; then - python3 "$script" "$@" -else - # Required dependencies not installed -- set up a venv and install them - # in there, then run the script (cleaning up the venv afterwards). - venv_dir="$(mktemp -d)" - python3 -m venv "$venv_dir" - "$venv_dir/bin/pip" install -U setuptools pip wheel - "$venv_dir/bin/pip" install torch transformers - "$venv_dir/bin/python3" "$script" "$@" - rm -rf "$venv_dir" -fi diff --git a/examples/tools/common/resnet50-pytorch/input-spec.yaml b/examples/tools/common/resnet50-pytorch/input-spec.yaml deleted file mode 100644 index 583144e7..00000000 --- a/examples/tools/common/resnet50-pytorch/input-spec.yaml +++ /dev/null @@ -1,17 +0,0 @@ -##===----------------------------------------------------------------------===## -# Copyright (c) 2024, Modular Inc. All rights reserved. -# -# Licensed under the Apache License v2.0 with LLVM Exceptions: -# https://llvm.org/LICENSE.txt -# -# 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. -##===----------------------------------------------------------------------===## - -inputs: - - input_name: pixel_values - shape: 1x3x224x224xf32 - compile_shape: ?x3x224x224xf32 diff --git a/examples/tools/common/roberta-pytorch/.gitignore b/examples/tools/common/roberta-pytorch/.gitignore deleted file mode 100644 index a8a0dcec..00000000 --- a/examples/tools/common/roberta-pytorch/.gitignore +++ /dev/null @@ -1 +0,0 @@ -*.bin diff --git a/examples/tools/common/roberta-pytorch/download-model.py b/examples/tools/common/roberta-pytorch/download-model.py deleted file mode 100755 index 37b3330c..00000000 --- a/examples/tools/common/roberta-pytorch/download-model.py +++ /dev/null @@ -1,134 +0,0 @@ -#!/usr/bin/env python3 -# ===----------------------------------------------------------------------=== # -# Copyright (c) 2024, Modular Inc. All rights reserved. -# -# Licensed under the Apache License v2.0 with LLVM Exceptions: -# https://llvm.org/LICENSE.txt -# -# 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. -# ===----------------------------------------------------------------------=== # - -"""Download RoBERTa PyTorch model from HuggingFace.""" - -import argparse -import os -import sys -import tempfile -import traceback - - -INSTALL_PROSE = """Is it installed? - -If not, try: - - python3 -m venv venv; source venv/bin/activate # if you aren't already in a virtual environment - pip install torch - -""" - - -def main(): - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument( - "-o", - "--output", - help="path to save TorchScript model to", - required=True, - ) - args = parser.parse_args() - - if os.path.exists(args.output): - print(f"output {args.output!r} already exists") - return - - print("Importing PyTorch...", flush=True) - try: - import torch - except ModuleNotFoundError: - with tempfile.NamedTemporaryFile( - mode="w", prefix="torchimport", suffix=".log", delete=False - ) as log_file: - traceback.print_exc(file=log_file) - print( - ( - f"Torch module was not found. {INSTALL_PROSE}" - f"Detailed error info in {log_file.name}" - ), - file=sys.stderr, - ) - sys.exit(1) - except ImportError: - with tempfile.NamedTemporaryFile( - mode="w", prefix="torchimport", suffix=".log", delete=False - ) as log_file: - traceback.print_exc(file=log_file) - print( - ( - "PyTorch installation seems to be broken.\n" - "Make sure you can 'import torch' " - "from a Python prompt and try again.\n" - f"Detailed error info in {log_file.name}" - ), - file=sys.stderr, - ) - sys.exit(1) - - print("Importing HuggingFace transformers...", flush=True) - try: - from transformers import RobertaForSequenceClassification - except ModuleNotFoundError: - with tempfile.NamedTemporaryFile( - mode="w", prefix="hfimport", suffix=".log", delete=False - ) as log_file: - traceback.print_exc(file=log_file) - print( - ( - "HuggingFace transformers module was not found. " - f" {INSTALL_PROSE}Detailed error info in {log_file.name}" - ), - file=sys.stderr, - ) - sys.exit(1) - except ImportError: - with tempfile.NamedTemporaryFile( - mode="w", prefix="hfimport", suffix=".log", delete=False - ) as log_file: - traceback.print_exc(file=log_file) - print( - ( - "HuggingFace transformers installation seems to be broken.\n" - "Make sure you can 'import transformers' " - "from a Python prompt and try again.\n" - f"Detailed error info in {log_file.name}" - ), - file=sys.stderr, - ) - sys.exit(1) - - torch.set_default_device("cpu") - - print("Getting pre-trained model...", flush=True) - model = RobertaForSequenceClassification.from_pretrained( - "cardiffnlp/twitter-roberta-base-emotion-multilabel-latest" - ) - print("Tracing model...", flush=True) - traced_model = torch.jit.trace( - model, - [ - torch.zeros(1, 128, dtype=torch.int64), - torch.ones(1, 128, dtype=torch.float32), - torch.zeros(1, 128, dtype=torch.int64), - ], - strict=False, - ) - print("Saving TorchScript...", flush=True) - traced_model.save(args.output) - print("All done!", flush=True) - - -if __name__ == "__main__": - main() diff --git a/examples/tools/common/roberta-pytorch/download-model.sh b/examples/tools/common/roberta-pytorch/download-model.sh deleted file mode 100755 index 53155ec1..00000000 --- a/examples/tools/common/roberta-pytorch/download-model.sh +++ /dev/null @@ -1,33 +0,0 @@ -#!/bin/bash -##===----------------------------------------------------------------------===## -# Copyright (c) 2024, Modular Inc. All rights reserved. -# -# Licensed under the Apache License v2.0 with LLVM Exceptions: -# https://llvm.org/LICENSE.txt -# -# 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. -##===----------------------------------------------------------------------===## -# Wrapper around download-model.py that sets up a temporary venv & installs -# dependencies if needed. - -set -e - -script="$(dirname "$0")/download-model.py" - -# Check to see if PyTorch and HuggingFace Transformers are installed. -if python3 -c 'import torch; import transformers' >& /dev/null; then - python3 "$script" "$@" -else - # Required dependencies not installed -- set up a venv and install them - # in there, then run the script (cleaning up the venv afterwards). - venv_dir="$(mktemp -d)" - python3 -m venv "$venv_dir" - "$venv_dir/bin/pip" install -U setuptools pip wheel - "$venv_dir/bin/pip" install torch transformers - "$venv_dir/bin/python3" "$script" "$@" - rm -rf "$venv_dir" -fi diff --git a/examples/tools/common/roberta-pytorch/input-spec.yaml b/examples/tools/common/roberta-pytorch/input-spec.yaml deleted file mode 100644 index 6397ee38..00000000 --- a/examples/tools/common/roberta-pytorch/input-spec.yaml +++ /dev/null @@ -1,29 +0,0 @@ -##===----------------------------------------------------------------------===## -# Copyright (c) 2024, Modular Inc. All rights reserved. -# -# Licensed under the Apache License v2.0 with LLVM Exceptions: -# https://llvm.org/LICENSE.txt -# -# 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. -##===----------------------------------------------------------------------===## - -inputs: - - input_name: input_ids - shape: 1x128xsi64 - data: - random: - uniform: - min: 0 - max: 50264 - - input_name: token_type_ids - shape: 1x128xsi64 - data: - constant: 0 - - input_name: attention_mask - shape: 1x128xf32 - data: - constant: 1 diff --git a/examples/tools/visualize-pytorch/.gitignore b/examples/tools/visualize-pytorch/.gitignore deleted file mode 100644 index 1be61631..00000000 --- a/examples/tools/visualize-pytorch/.gitignore +++ /dev/null @@ -1 +0,0 @@ -*.maxviz diff --git a/examples/tools/visualize-pytorch/pyproject.toml b/examples/tools/visualize-pytorch/pyproject.toml deleted file mode 100644 index 68e4e970..00000000 --- a/examples/tools/visualize-pytorch/pyproject.toml +++ /dev/null @@ -1,22 +0,0 @@ -[project] -name = "visualize-pytorch" -version = "1.0.0" -description = "MAX Examples" -requires-python = ">=3.9,<3.13" -authors = [{ name = "Modular", email = "hello@modular.com" }] -dependencies = ["torch==2.4.0"] - -[build-system] -requires = ["setuptools"] -build-backend = "setuptools.build_meta" - -[tool.pixi.project] -channels = ["conda-forge", "https://conda.modular.com/max-nightly"] -platforms = ["osx-arm64", "linux-aarch64", "linux-64"] - -[tool.pixi.pypi-dependencies] -visualize-pytorch = { path = ".", editable = true } - -[tool.pixi.dependencies] -max = ">=24.4.0,<25" -numpy = "<2.0" diff --git a/examples/tools/visualize-pytorch/run.sh b/examples/tools/visualize-pytorch/run.sh deleted file mode 100755 index c614f158..00000000 --- a/examples/tools/visualize-pytorch/run.sh +++ /dev/null @@ -1,30 +0,0 @@ -#!/bin/bash -##===----------------------------------------------------------------------===## -# Copyright (c) 2024, Modular Inc. All rights reserved. -# -# Licensed under the Apache License v2.0 with LLVM Exceptions: -# https://llvm.org/LICENSE.txt -# -# 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. -##===----------------------------------------------------------------------===## - -# If anything goes wrong, stop running the script. -set -e - -# Make sure we're running from inside the directory containing this file. -cd "$(dirname "$0")" - -# If ResNet50 hasn't been downloaded yet, download it. -if ! [ -f ../../models/resnet50.torchscript ]; then - ../common/resnet50-pytorch/download-model.sh -o ../../models/resnet50.torchscript -fi - -# Now for the easy part -- visualization ;) -max visualize --input-data-schema=../common/resnet50-pytorch/input-spec.yaml ../../models/resnet50.torchscript -if [ "$CI" != true ]; then - python3 -m webbrowser https://netron.app || true -fi