diff --git a/examples/ML+DL-Examples/Spark-DL/dl_inference/huggingface/conditional_generation_tf.ipynb b/examples/ML+DL-Examples/Spark-DL/dl_inference/huggingface/conditional_generation_tf.ipynb index d494b288e..a5dcc33a3 100644 --- a/examples/ML+DL-Examples/Spark-DL/dl_inference/huggingface/conditional_generation_tf.ipynb +++ b/examples/ML+DL-Examples/Spark-DL/dl_inference/huggingface/conditional_generation_tf.ipynb @@ -116,7 +116,7 @@ ], "source": [ "tokenizer = AutoTokenizer.from_pretrained(\"google-t5/t5-small\")\n", - "model = TFT5ForConditionalGeneration.from_pretrained(\"google-t5/t5-small\")\n", + "model = TFT5ForConditionalGeneration.from_pretrained(\"google-t5/t5-small\", use_safetensors=False)\n", "\n", "task_prefix = \"translate English to German: \"\n", "\n", @@ -581,7 +581,7 @@ " except RuntimeError as e:\n", " print(e)\n", "\n", - " model = TFT5ForConditionalGeneration.from_pretrained(\"google-t5/t5-small\")\n", + " model = TFT5ForConditionalGeneration.from_pretrained(\"google-t5/t5-small\", use_safetensors=False)\n", " tokenizer = AutoTokenizer.from_pretrained(\"google-t5/t5-small\")\n", "\n", " def predict(inputs):\n", @@ -1033,7 +1033,7 @@ " print(e)\n", " \n", " tokenizer = AutoTokenizer.from_pretrained(\"google-t5/t5-small\")\n", - " model = TFT5ForConditionalGeneration.from_pretrained(\"google-t5/t5-small\")\n", + " model = TFT5ForConditionalGeneration.from_pretrained(\"google-t5/t5-small\", use_safetensors=False)\n", "\n", " @batch\n", " def _infer_fn(**inputs):\n", diff --git a/examples/ML+DL-Examples/Spark-DL/dl_inference/huggingface/pipelines_tf.ipynb b/examples/ML+DL-Examples/Spark-DL/dl_inference/huggingface/pipelines_tf.ipynb index 97d88cfc0..2820fb492 100644 --- a/examples/ML+DL-Examples/Spark-DL/dl_inference/huggingface/pipelines_tf.ipynb +++ b/examples/ML+DL-Examples/Spark-DL/dl_inference/huggingface/pipelines_tf.ipynb @@ -44,7 +44,9 @@ ], "source": [ "import tensorflow as tf\n", - "from transformers import pipeline\n", + "from transformers import AutoTokenizer, TFAutoModelForSequenceClassification, pipeline\n", + "\n", + "HF_DEFAULT_SENTIMENT_MODEL = \"distilbert/distilbert-base-uncased-finetuned-sst-2-english\"\n", "\n", "# Manually enable Huggingface tokenizer parallelism to avoid disabling with PySpark parallelism.\n", "# See (https://github.com/huggingface/transformers/issues/5486) for more info. \n", @@ -133,7 +135,12 @@ } ], "source": [ - "classifier = pipeline(\"sentiment-analysis\", device=device)" + "tokenizer = AutoTokenizer.from_pretrained(HF_DEFAULT_SENTIMENT_MODEL)\n", + "model = TFAutoModelForSequenceClassification.from_pretrained(\n", + " HF_DEFAULT_SENTIMENT_MODEL,\n", + " use_safetensors=False,\n", + ")\n", + "classifier = pipeline(\"sentiment-analysis\", model=model, tokenizer=tokenizer, dtype=None)" ] }, { @@ -216,7 +223,7 @@ "source": [ "from transformers import AutoTokenizer, TFAutoModelForSequenceClassification\n", "\n", - "model = TFAutoModelForSequenceClassification.from_pretrained(model_name)\n", + "model = TFAutoModelForSequenceClassification.from_pretrained(model_name, use_safetensors=False)\n", "tokenizer = AutoTokenizer.from_pretrained(model_name)" ] }, @@ -238,7 +245,7 @@ } ], "source": [ - "classifier = pipeline(\"sentiment-analysis\", model=model, tokenizer=tokenizer, device=device)\n", + "classifier = pipeline(\"sentiment-analysis\", model=model, tokenizer=tokenizer, dtype=None)\n", "classifier(\"Nous sommes très heureux de vous présenter la bibliothèque 🤗 Transformers.\")" ] }, @@ -557,7 +564,7 @@ "source": [ "def predict_batch_fn():\n", " import tensorflow as tf\n", - " from transformers import pipeline\n", + " from transformers import AutoTokenizer, TFAutoModelForSequenceClassification, pipeline\n", "\n", " # Enable GPU memory growth\n", " gpus = tf.config.experimental.list_physical_devices('GPU')\n", @@ -569,7 +576,10 @@ " print(e)\n", " \n", " device = 0 if tf.config.list_physical_devices('GPU') else -1\n", - " pipe = pipeline(\"sentiment-analysis\", device=device)\n", + " model_name = \"distilbert/distilbert-base-uncased-finetuned-sst-2-english\"\n", + " tokenizer = AutoTokenizer.from_pretrained(model_name)\n", + " model = TFAutoModelForSequenceClassification.from_pretrained(model_name, use_safetensors=False)\n", + " pipe = pipeline(\"sentiment-analysis\", model=model, tokenizer=tokenizer, dtype=None)\n", " def predict(inputs):\n", " return pipe(inputs.tolist())\n", " return predict" @@ -822,7 +832,7 @@ " import signal\n", " import numpy as np\n", " import tensorflow as tf\n", - " from transformers import pipeline\n", + " from transformers import AutoTokenizer, TFAutoModelForSequenceClassification, pipeline\n", " from pytriton.decorators import batch\n", " from pytriton.model_config import DynamicBatcher, ModelConfig, Tensor\n", " from pytriton.triton import Triton, TritonConfig\n", @@ -840,7 +850,10 @@ " \n", " device = 0 if tf.config.list_physical_devices('GPU') else -1\n", " \n", - " pipe = pipeline(\"sentiment-analysis\", device=device)\n", + " model_name = \"distilbert/distilbert-base-uncased-finetuned-sst-2-english\"\n", + " tokenizer = AutoTokenizer.from_pretrained(model_name)\n", + " model = TFAutoModelForSequenceClassification.from_pretrained(model_name, use_safetensors=False)\n", + " pipe = pipeline(\"sentiment-analysis\", model=model, tokenizer=tokenizer, dtype=None)\n", " print(f\"SERVER: Using {device} device.\")\n", "\n", " @batch\n", diff --git a/examples/ML+DL-Examples/Spark-DL/dl_inference/requirements.txt b/examples/ML+DL-Examples/Spark-DL/dl_inference/requirements.txt index 44c223f1e..f76d3b8e1 100644 --- a/examples/ML+DL-Examples/Spark-DL/dl_inference/requirements.txt +++ b/examples/ML+DL-Examples/Spark-DL/dl_inference/requirements.txt @@ -1,4 +1,4 @@ -# Copyright (c) 2025, NVIDIA CORPORATION. +# Copyright (c) 2025-2026, NVIDIA CORPORATION. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -numpy +numpy>=1.26.4,<2 pandas matplotlib portalocker @@ -23,7 +23,8 @@ scikit-learn jupyterlab pyspark>=3.4.0 huggingface -datasets +datasets==3.* +huggingface-hub<1.0 transformers ipywidgets nvidia-pytriton diff --git a/examples/ML+DL-Examples/Spark-DL/dl_inference/tensorflow/keras_preprocessing_tf.ipynb b/examples/ML+DL-Examples/Spark-DL/dl_inference/tensorflow/keras_preprocessing_tf.ipynb index c989b8087..3cc2d37c0 100644 --- a/examples/ML+DL-Examples/Spark-DL/dl_inference/tensorflow/keras_preprocessing_tf.ipynb +++ b/examples/ML+DL-Examples/Spark-DL/dl_inference/tensorflow/keras_preprocessing_tf.ipynb @@ -351,7 +351,12 @@ } ], "source": [ - "train, val, test = np.split(dataframe.sample(frac=1), [int(0.8*len(dataframe)), int(0.9*len(dataframe))])" + "shuffled = dataframe.sample(frac=1)\n", + "train_end = int(0.8 * len(shuffled))\n", + "val_end = int(0.9 * len(shuffled))\n", + "train = shuffled.iloc[:train_end]\n", + "val = shuffled.iloc[train_end:val_end]\n", + "test = shuffled.iloc[val_end:]" ] }, { @@ -394,7 +399,7 @@ "def df_to_dataset(dataframe, shuffle=True, batch_size=32):\n", " df = dataframe.copy()\n", " labels = df.pop('target')\n", - " df = {key: value.to_numpy()[:,tf.newaxis] for key, value in dataframe.items()}\n", + " df = {key: value.to_numpy()[:,tf.newaxis] for key, value in df.items()}\n", " ds = tf.data.Dataset.from_tensor_slices((dict(df), labels))\n", " if shuffle:\n", " ds = ds.shuffle(buffer_size=len(dataframe))\n", diff --git a/examples/ML+DL-Examples/Spark-DL/dl_inference/tensorflow/text_classification_tf.ipynb b/examples/ML+DL-Examples/Spark-DL/dl_inference/tensorflow/text_classification_tf.ipynb index d5bcf2bed..5cfc14b52 100644 --- a/examples/ML+DL-Examples/Spark-DL/dl_inference/tensorflow/text_classification_tf.ipynb +++ b/examples/ML+DL-Examples/Spark-DL/dl_inference/tensorflow/text_classification_tf.ipynb @@ -47,6 +47,7 @@ "import re\n", "import shutil\n", "import string\n", + "import unicodedata\n", "import matplotlib.pyplot as plt\n", "\n", "import tensorflow as tf\n", @@ -977,6 +978,18 @@ "metadata": {}, "outputs": [], "source": [ + "def normalize_vocabulary(vocab):\n", + " normalized_vocab = []\n", + " seen = set()\n", + " for word in vocab:\n", + " word = unicodedata.normalize('NFKD', word).encode('ascii', 'ignore').decode('utf-8')\n", + " if word in (\"\", \"[UNK]\") or word in seen:\n", + " continue\n", + " seen.add(word)\n", + " normalized_vocab.append(word)\n", + " return normalized_vocab\n", + "\n", + "vectorize_layer.set_vocabulary(normalize_vocabulary(vectorize_layer.get_vocabulary()))\n", "export_model.save('models/text_model.keras')" ] }, @@ -1624,25 +1637,8 @@ "metadata": {}, "outputs": [], "source": [ - "import unicodedata\n", - "\n", - "def normalize_vocabulary(vocab):\n", - " # Normalize each word in the vocabulary to remove non-ASCII characters\n", - " normalized_vocab = [\n", - " unicodedata.normalize('NFKD', word).encode('ascii', 'ignore').decode('utf-8')\n", - " for word in vocab\n", - " ]\n", - " normalized_vocab = filter(lambda x: x != '', normalized_vocab)\n", - " normalized_vocab = list(set(normalized_vocab)) \n", - "\n", - "\n", - " return normalized_vocab\n", - "\n", - "vocab = vectorize_layer.get_vocabulary()\n", - "normalized_vocab = normalize_vocabulary(vocab)\n", - "\n", - "# Reassign the cleaned vocabulary to the TextVectorization layer\n", - "vectorize_layer.set_vocabulary(normalized_vocab)" + "# Reuse the cleaned vocabulary for the Triton model as well.\n", + "vectorize_layer.set_vocabulary(normalize_vocabulary(vectorize_layer.get_vocabulary()))" ] }, { diff --git a/examples/ML+DL-Examples/Spark-DL/dl_inference/torch_requirements.txt b/examples/ML+DL-Examples/Spark-DL/dl_inference/torch_requirements.txt index 708d13870..bf836c021 100644 --- a/examples/ML+DL-Examples/Spark-DL/dl_inference/torch_requirements.txt +++ b/examples/ML+DL-Examples/Spark-DL/dl_inference/torch_requirements.txt @@ -1,4 +1,4 @@ -# Copyright (c) 2025, NVIDIA CORPORATION. +# Copyright (c) 2025-2026, NVIDIA CORPORATION. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -13,10 +13,10 @@ # limitations under the License. -r requirements.txt -torch<=2.5.1 -torchvision -torch-tensorrt +torch==2.8.0 +torchvision==0.23.0 +torch-tensorrt==2.8.0 tensorrt --extra-index-url https://download.pytorch.org/whl/cu121 sentence_transformers sentencepiece -nvidia-modelopt[all] --extra-index-url https://pypi.nvidia.com \ No newline at end of file +nvidia-modelopt[all] --extra-index-url https://pypi.nvidia.com diff --git a/examples/ML+DL-Examples/Spark-DL/dl_inference/vllm/qwen-2.5-7b_vllm.ipynb b/examples/ML+DL-Examples/Spark-DL/dl_inference/vllm/qwen-2.5-7b_vllm.ipynb index 778d56d63..e31021027 100644 --- a/examples/ML+DL-Examples/Spark-DL/dl_inference/vllm/qwen-2.5-7b_vllm.ipynb +++ b/examples/ML+DL-Examples/Spark-DL/dl_inference/vllm/qwen-2.5-7b_vllm.ipynb @@ -598,9 +598,8 @@ "source": [ "server_manager.start_servers(gpu_memory_utilization=0.95,\n", " max_model_len=6600,\n", - " task=\"generate\",\n", " enforce_eager=enforce_eager,\n", - " wait_retries=60)" + " wait_retries=180)" ] }, {