From 458cfc0074eb675994d29c31fdd748a98b2e5b3d Mon Sep 17 00:00:00 2001 From: Jack Clayton Date: Wed, 28 Aug 2024 22:07:05 -0700 Subject: [PATCH] [max-examples] GUI: Improvements and fixes - Bert: display top 5 examples by default - Llamma3: use 3.1 models by default - Rag: put example data in `ragdata` folder so it doesn't crash - Stable Diffusion: fix data type being incorrect - Yolo: move yoloconstants.py CLASS_NAMES into yolo.py MODULAR_ORIG_COMMIT_REV_ID: 5567ef1680b1f697f5b1cacbcd897f1d08564301 --- examples/gui/pages/bert.py | 2 +- examples/gui/pages/llama3.py | 6 +- examples/gui/pages/rag.py | 11 ++-- examples/gui/pages/stable-diffusion.py | 4 +- examples/gui/pages/yolo.py | 83 +++++++++++++++++++++++++ examples/gui/ragdata/mojo_functions.txt | 28 +++++++++ 6 files changed, 123 insertions(+), 11 deletions(-) create mode 100644 examples/gui/ragdata/mojo_functions.txt diff --git a/examples/gui/pages/bert.py b/examples/gui/pages/bert.py index c62cb2ee..782e1482 100644 --- a/examples/gui/pages/bert.py +++ b/examples/gui/pages/bert.py @@ -92,6 +92,7 @@ def softmax(logits): model_state = st.empty() mlm = st.sidebar.checkbox("Masked Language Model", True) +show_predictions = st.sidebar.checkbox("Show top 5 predictions", True) filename = "bert.torchscript" if mlm: filename = "bert-mlm.torchscript" @@ -102,7 +103,6 @@ def softmax(logits): batch = st.sidebar.number_input("Batch Size", 1, 64) seq_len = st.sidebar.slider("Sequence Length", 128, 1024) input_text = st.text_input("Text Input", "Don't [MASK] about it") -show_predictions = st.sidebar.checkbox("Show top 5 predictions") compile_torchscript(batch, seq_len, mlm) diff --git a/examples/gui/pages/llama3.py b/examples/gui/pages/llama3.py index a8a3bcf1..ece3666e 100644 --- a/examples/gui/pages/llama3.py +++ b/examples/gui/pages/llama3.py @@ -92,11 +92,11 @@ def start_llama3( ) if quantization == "q4_0": - model_url = "https://huggingface.co/QuantFactory/Meta-Llama-3-8B-Instruct-GGUF/resolve/main/Meta-Llama-3-8B-Instruct.Q4_0.gguf" + model_url = "https://huggingface.co/QuantFactory/Meta-Llama-3-8B-Instruct-GGUF/resolve/main/Meta-Llama-3.1-8B-Instruct.Q4_0.gguf" elif quantization == "q4_k": - model_url = "https://huggingface.co/bartowski/Meta-Llama-3-8B-Instruct-GGUF/resolve/main/Meta-Llama-3-8B-Instruct-Q4_K_M.gguf" + model_url = "https://huggingface.co/bartowski/Meta-Llama-3-8B-Instruct-GGUF/resolve/main/Meta-Llama-3.1-8B-Instruct-Q4_K_M.gguf" elif quantization == "q6_k": - model_url = "https://huggingface.co/bartowski/Meta-Llama-3-8B-Instruct-GGUF/resolve/main/Meta-Llama-3-8B-Instruct-Q6_K.gguf" + model_url = "https://huggingface.co/bartowski/Meta-Llama-3-8B-Instruct-GGUF/resolve/main/Meta-Llama-3.1-8B-Instruct-Q6_K.gguf" model_url = st.sidebar.text_input("Model URL", value=model_url) os.makedirs(modular_cache_dir(), exist_ok=True) diff --git a/examples/gui/pages/rag.py b/examples/gui/pages/rag.py index c656bd27..3ae593d2 100644 --- a/examples/gui/pages/rag.py +++ b/examples/gui/pages/rag.py @@ -10,16 +10,15 @@ # See the License for the specific language governing permissions and # limitations under the License. # ===----------------------------------------------------------------------=== # -from pathlib import Path import os import subprocess +from pathlib import Path +import chromadb import openai import streamlit as st -import chromadb from fastembed import TextEmbedding from llama_index.core import SimpleDirectoryReader - from shared import download_file, kill_process, menu, modular_cache_dir st.set_page_config("RAG with Llama3", page_icon="🗣️ 📄") @@ -153,11 +152,11 @@ def start_llama3( ) if quantization == "q4_0": - model_url = "https://huggingface.co/QuantFactory/Meta-Llama-3-8B-GGUF/resolve/main/Meta-Llama-3-8B.Q4_0.gguf" + model_url = "https://huggingface.co/QuantFactory/Meta-Llama-3-8B-Instruct-GGUF/resolve/main/Meta-Llama-3.1-8B-Instruct.Q4_0.gguf" elif quantization == "q4_k": - model_url = "https://huggingface.co/bartowski/Meta-Llama-3-8B-Instruct-GGUF/resolve/main/Meta-Llama-3-8B-Instruct-Q4_K_M.gguf" + model_url = "https://huggingface.co/bartowski/Meta-Llama-3-8B-Instruct-GGUF/resolve/main/Meta-Llama-3.1-8B-Instruct-Q4_K_M.gguf" elif quantization == "q6_k": - model_url = "https://huggingface.co/bartowski/Meta-Llama-3-8B-Instruct-GGUF/resolve/main/Meta-Llama-3-8B-Instruct-Q6_K.gguf" + model_url = "https://huggingface.co/bartowski/Meta-Llama-3-8B-Instruct-GGUF/resolve/main/Meta-Llama-3.1-8B-Instruct-Q6_K.gguf" model_url = st.sidebar.text_input("Model URL", value=model_url) os.makedirs(modular_cache_dir(), exist_ok=True) diff --git a/examples/gui/pages/stable-diffusion.py b/examples/gui/pages/stable-diffusion.py index 3ffe7f02..1b05643d 100644 --- a/examples/gui/pages/stable-diffusion.py +++ b/examples/gui/pages/stable-diffusion.py @@ -120,7 +120,9 @@ def load_tokenizer(path): padding="max_length", max_length=tokenizer.model_max_length, ) - input_ids = np.stack((prompt_p.input_ids, prompt_n.input_ids)) + input_ids = np.stack((prompt_p.input_ids, prompt_n.input_ids)).astype( + np.int32 + ) encoder_hidden_states = txt_encoder.execute(input_ids=input_ids)[ "last_hidden_state" ] diff --git a/examples/gui/pages/yolo.py b/examples/gui/pages/yolo.py index 955c13c8..713c9735 100644 --- a/examples/gui/pages/yolo.py +++ b/examples/gui/pages/yolo.py @@ -127,6 +127,89 @@ ONNX, then compiles it with MAX for faster inference! """ +CLASS_NAMES = [ + "person", + "bicycle", + "car", + "motorcycle", + "airplane", + "bus", + "train", + "truck", + "boat", + "traffic light", + "fire hydrant", + "stop sign", + "parking meter", + "bench", + "bird", + "cat", + "dog", + "horse", + "sheep", + "cow", + "elephant", + "bear", + "zebra", + "giraffe", + "backpack", + "umbrella", + "handbag", + "tie", + "suitcase", + "frisbee", + "skis", + "snowboard", + "sports ball", + "kite", + "baseball bat", + "baseball glove", + "skateboard", + "surfboard", + "tennis racket", + "bottle", + "wine glass", + "cup", + "fork", + "knife", + "spoon", + "bowl", + "banana", + "apple", + "sandwich", + "orange", + "broccoli", + "carrot", + "hot dog", + "pizza", + "donut", + "cake", + "chair", + "couch", + "potted plant", + "bed", + "dining table", + "toilet", + "tv", + "laptop", + "mouse", + "remote", + "keyboard", + "cell phone", + "microwave", + "oven", + "toaster", + "sink", + "refrigerator", + "book", + "clock", + "vase", + "scissors", + "teddy bear", + "hair drier", + "toothbrush", +] + @st.cache_data(show_spinner="Downloading YOLO and exporting to ONNX") def download_and_export_yolo(model_path, height, width): diff --git a/examples/gui/ragdata/mojo_functions.txt b/examples/gui/ragdata/mojo_functions.txt new file mode 100644 index 00000000..f053dcb6 --- /dev/null +++ b/examples/gui/ragdata/mojo_functions.txt @@ -0,0 +1,28 @@ +# Mojo Functions + +Mojo functions can be declared with either fn or def. + +The fn declaration enforces type-checking and memory-safe behaviors (Rust style), while def allows no type declarations and dynamic behaviors (Python style). + +For example, this def function doesn't require declaration of argument types or the return type: + +```mojo +def greet(name): + return "Hello, " + name + "!" +``` + +While the same thing as an fn function requires that you specify the argument type and the return type like this: + +```mojo +fn greet2(name: String) -> String: + return "Hello, " + name + "!" +``` + +Both functions have the same result, but the fn function provides compile-time checks to ensure the function receives and returns the correct types. Whereas, the def function might fail at runtime if it receives the wrong type. + +Currently, Mojo doesn't support top-level code in a .mojo (or .🔥) file, so every program must include a function named main() as the entry point. You can declare it with either def or fn: + +```mojo +def main(): + print("Hello, world!") +```