From 8ae5a4a00625ef6f4471eee2a566254a1b154313 Mon Sep 17 00:00:00 2001 From: Ekaterina Aidova Date: Fri, 17 May 2024 16:48:20 +0400 Subject: [PATCH] fix musicgen for transformers 4.40 (#2026) --- .../music-generation/music-generation.ipynb | 133 +++++++++++++----- 1 file changed, 95 insertions(+), 38 deletions(-) diff --git a/notebooks/music-generation/music-generation.ipynb b/notebooks/music-generation/music-generation.ipynb index da45e57b020..fb4e7817a52 100644 --- a/notebooks/music-generation/music-generation.ipynb +++ b/notebooks/music-generation/music-generation.ipynb @@ -1,7 +1,6 @@ { "cells": [ { - "attachments": {}, "cell_type": "markdown", "id": "2990976f-be06-4068-bf49-56d39a3f93a8", "metadata": {}, @@ -20,7 +19,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "c3c318e0-3828-4b23-aefc-9ccd9b093be2", "metadata": {}, @@ -46,7 +44,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "501e64a4-62a8-4de6-b1c1-a8f41199ac07", "metadata": {}, @@ -56,7 +53,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "af1463e7-68f4-40e2-a151-4d6d7678f1e0", "metadata": {}, @@ -67,17 +63,16 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "id": "40d29d27-919b-407b-846d-f025b5e99878", "metadata": {}, "outputs": [], "source": [ "%pip install -q \"openvino>=2023.3.0\"\n", - "%pip install -q \"torch>=2.1\" \"gradio>=4.19\" transformers --extra-index-url https://download.pytorch.org/whl/cpu" + "%pip install -q \"torch>=2.1\" \"gradio>=4.19\" \"transformers\" packaging --extra-index-url https://download.pytorch.org/whl/cpu" ] }, { - "attachments": {}, "cell_type": "markdown", "id": "dfe2e282-19f1-47c4-bbac-03177db00243", "metadata": {}, @@ -91,7 +86,20 @@ "execution_count": 2, "id": "d8494fc4-78f6-4f43-a4d3-f2ec18210355", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "2024-05-17 15:00:26.783507: I tensorflow/core/util/port.cc:110] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.\n", + "2024-05-17 15:00:26.785229: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.\n", + "2024-05-17 15:00:26.820125: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.\n", + "2024-05-17 15:00:26.821207: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.\n", + "To enable the following instructions: AVX2 AVX512F AVX512_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.\n", + "2024-05-17 15:00:27.544961: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT\n" + ] + } + ], "source": [ "from collections import namedtuple\n", "from functools import partial\n", @@ -116,7 +124,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "523545c2-3e07-466e-bce8-85152ed2f1f4", "metadata": {}, @@ -137,18 +144,33 @@ "name": "stderr", "output_type": "stream", "text": [ - "/home/ea/work/genai_env/lib/python3.8/site-packages/torch/nn/utils/weight_norm.py:30: UserWarning: torch.nn.utils.weight_norm is deprecated in favor of torch.nn.utils.parametrizations.weight_norm.\n", - " warnings.warn(\"torch.nn.utils.weight_norm is deprecated in favor of torch.nn.utils.parametrizations.weight_norm.\")\n" + "/home/ea/work/my_optimum_intel/optimum_env/lib/python3.8/site-packages/torch/nn/utils/weight_norm.py:28: UserWarning: torch.nn.utils.weight_norm is deprecated in favor of torch.nn.utils.parametrizations.weight_norm.\n", + " warnings.warn(\"torch.nn.utils.weight_norm is deprecated in favor of torch.nn.utils.parametrizations.weight_norm.\")\n", + "/home/ea/work/my_optimum_intel/optimum_env/lib/python3.8/site-packages/transformers/models/encodec/modeling_encodec.py:123: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).\n", + " self.register_buffer(\"padding_total\", torch.tensor(kernel_size - stride, dtype=torch.int64), persistent=False)\n" ] } ], "source": [ + "import sys\n", + "from packaging.version import parse\n", + "\n", + "\n", + "if sys.version_info < (3, 8):\n", + " import importlib_metadata\n", + "else:\n", + " import importlib.metadata as importlib_metadata\n", + "loading_kwargs = {}\n", + "\n", + "if parse(importlib_metadata.version(\"transformers\")) >= parse(\"4.40.0\"):\n", + " loading_kwargs[\"attn_implementation\"] = \"eager\"\n", + "\n", + "\n", "# Load the pipeline\n", - "model = MusicgenForConditionalGeneration.from_pretrained(\"facebook/musicgen-small\", torchscript=True, return_dict=False)" + "model = MusicgenForConditionalGeneration.from_pretrained(\"facebook/musicgen-small\", torchscript=True, return_dict=False, **loading_kwargs)" ] }, { - "attachments": {}, "cell_type": "markdown", "id": "18a36ade-6ab4-4540-8766-54b29dfb2dc6", "metadata": {}, @@ -182,7 +204,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "83117816-3ec2-43a0-8fcd-1eb09c6e21e9", "metadata": {}, @@ -204,7 +225,7 @@ "text/html": [ "\n", " \n", " " @@ -232,7 +253,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "698f4db8-759d-47f8-912c-b11d7ba9b632", "metadata": {}, @@ -252,7 +272,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "019d43b6-5a9f-41ae-aad5-c66e832ddd84", "metadata": {}, @@ -276,7 +295,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "5312460c-fb6f-471f-b2ff-4caf13047866", "metadata": {}, @@ -296,7 +314,24 @@ "execution_count": 7, "id": "5dadad3c-06ce-43e5-b059-b16238f3963f", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "WARNING:tensorflow:Please fix your imports. Module tensorflow.python.training.tracking.base has been moved to tensorflow.python.trackable.base. The old module will be deleted in version 2.11.\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "[ WARNING ] Please fix your imports. Module %s has been moved to %s. The old module will be deleted in version %s.\n", + "/home/ea/work/my_optimum_intel/optimum_env/lib/python3.8/site-packages/transformers/modeling_utils.py:4371: FutureWarning: `_is_quantized_training_enabled` is going to be deprecated in transformers 4.39.0. Please use `model.hf_quantizer.is_trainable` instead\n", + " warnings.warn(\n" + ] + } + ], "source": [ "if not t5_ir_path.exists():\n", " t5_ov = ov.convert_model(model.text_encoder, example_input={\"input_ids\": inputs[\"input_ids\"]})\n", @@ -307,7 +342,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "98019b03-8c03-47e4-b311-8bb6af45b70c", "metadata": {}, @@ -344,7 +378,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "1882240a-3300-4c72-9a6b-fe72fd6820b3", "metadata": {}, @@ -355,7 +388,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 10, "id": "5cd21b7d-2026-4781-849f-a7bff22e63a2", "metadata": {}, "outputs": [], @@ -388,7 +421,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "dc93a8a2-312b-46c3-85b9-df0cd96feeb6", "metadata": {}, @@ -401,7 +433,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 11, "id": "5ca233ad-d8a2-46ca-ba99-a054647df626", "metadata": {}, "outputs": [], @@ -426,7 +458,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "d1172fd5-ebc8-4cec-a42e-1a69ec684226", "metadata": {}, @@ -439,7 +470,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 12, "id": "0231dea8-24dd-46bc-b5d6-399e72b3c11d", "metadata": {}, "outputs": [], @@ -448,7 +479,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "134b4437-653e-4cb1-90fc-0028fd68083b", "metadata": {}, @@ -461,14 +491,14 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 13, "id": "fde35263-6a70-45d3-8ddc-8b5cf9c1fc35", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "f1918ecce69c447c9ed009e3b53b8440", + "model_id": "c56f9a2f20704363bd062f0c6f274e65", "version_major": 2, "version_minor": 0 }, @@ -476,7 +506,7 @@ "Dropdown(description='Device:', index=3, options=('CPU', 'GPU.0', 'GPU.1', 'AUTO'), value='AUTO')" ] }, - "execution_count": 12, + "execution_count": 13, "metadata": {}, "output_type": "execute_result" } @@ -495,7 +525,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "13f35868-70a2-40b9-a23a-32823883ba49", "metadata": {}, @@ -512,7 +541,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 14, "id": "aaee5ead-f0ce-40be-b678-99518e1b9a98", "metadata": {}, "outputs": [], @@ -587,7 +616,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "846c9c7e-1fda-47e7-b0ca-47cb5c7f5c9d", "metadata": {}, @@ -680,7 +708,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "48d8462f", "metadata": {}, @@ -699,7 +726,7 @@ "text/html": [ "\n", " \n", " " @@ -727,7 +754,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "549e0891-01fc-41d9-9ba5-44bea855cced", "metadata": {}, @@ -760,15 +786,45 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "id": "04059aa3-8549-4635-a51a-74c026f1d740", "metadata": { + "tags": [], "test_replace": { " demo.launch(debug=True)": " demo.launch()", " demo.launch(share=True, debug=True)": " demo.launch(share=True)" } }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Running on local URL: http://127.0.0.1:7860\n", + "\n", + "To create a public link, set `share=True` in `launch()`.\n" + ] + }, + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Keyboard interruption in main thread... closing server.\n" + ] + } + ], "source": [ "import gradio as gr\n", "\n", @@ -784,6 +840,7 @@ " [\"90s rock song with loud guitars and heavy drums\"],\n", " [\"Heartful EDM with beautiful synths and chords\"],\n", " ],\n", + " allow_flagging=\"never\",\n", ")\n", "try:\n", " demo.launch(debug=True)\n",