diff --git a/Season2.step_into_llm/08.MoE/MoE.pptx b/Season2.step_into_llm/08.MoE/MoE.pptx new file mode 100644 index 0000000..fad1a02 Binary files /dev/null and b/Season2.step_into_llm/08.MoE/MoE.pptx differ diff --git a/Season2.step_into_llm/08.MoE/mistral-7b.ipynb b/Season2.step_into_llm/08.MoE/mistral-7b.ipynb new file mode 100644 index 0000000..0735539 --- /dev/null +++ b/Season2.step_into_llm/08.MoE/mistral-7b.ipynb @@ -0,0 +1,286 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "f0d1fbfa", + "metadata": {}, + "source": [ + "环境安装\n", + "python3.9.0\n", + "mindspore2.2.14" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "356a6c98-d42a-4470-a1e9-40714a17642e", + "metadata": {}, + "outputs": [], + "source": [ + "%%capture captured_output\n", + "!/home/ma-user/anaconda3/bin/conda create -n python-3.9.0 python=3.9.0 -y --override-channels --channel https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main\n", + "!/home/ma-user/anaconda3/envs/python-3.9.0/bin/pip install ipykernel" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "66b31f07-953d-4932-9e70-00c5afaef166", + "metadata": {}, + "outputs": [], + "source": [ + "import json\n", + "import os\n", + "\n", + "data = {\n", + " \"display_name\": \"python-3.9.0\",\n", + " \"env\": {\n", + " \"PATH\": \"/home/ma-user/anaconda3/envs/python-3.9.0/bin:/home/ma-user/anaconda3/envs/python-3.7.10/bin:/modelarts/authoring/notebook-conda/bin:/opt/conda/bin:/usr/local/nvidia/bin:/usr/local/cuda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/ma-user/modelarts/ma-cli/bin:/home/ma-user/modelarts/ma-cli/bin\"\n", + " },\n", + " \"language\": \"python\",\n", + " \"argv\": [\n", + " \"/home/ma-user/anaconda3/envs/python-3.9.0/bin/python\",\n", + " \"-m\",\n", + " \"ipykernel\",\n", + " \"-f\",\n", + " \"{connection_file}\"\n", + " ]\n", + "}\n", + "\n", + "if not os.path.exists(\"/home/ma-user/anaconda3/share/jupyter/kernels/python-3.9.0/\"):\n", + " os.mkdir(\"/home/ma-user/anaconda3/share/jupyter/kernels/python-3.9.0/\")\n", + "\n", + "with open('/home/ma-user/anaconda3/share/jupyter/kernels/python-3.9.0/kernel.json', 'w') as f:\n", + " json.dump(data, f, indent=4)" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "017e7025-185e-402d-9fc6-9c7530c6be5e", + "metadata": {}, + "outputs": [], + "source": [ + "%%capture captured_output\n", + "\n", + "!pip install https://ms-release.obs.cn-north-4.myhuaweicloud.com/2.2.14/MindSpore/unified/x86_64/mindspore-2.2.14-cp39-cp39-linux_x86_64.whl --trusted-host ms-release.obs.cn-north-4.myhuaweicloud.com -i https://pypi.tuna.tsinghua.edu.cn/simple\n", + "!pip install ipywidgets -i https://pypi.tuna.tsinghua.edu.cn/simple\n" + ] + }, + { + "cell_type": "markdown", + "id": "6b2562d4", + "metadata": {}, + "source": [ + "下面用到的文件见:https://github.com/lvyufeng/mistral-mindspore/tree/12109d82ae9ff0fa854a5ebccba9ff62325e8fd7" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "86924f54-61be-473e-8e39-6899a0ce617e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Looking in indexes: http://repo.myhuaweicloud.com/repository/pypi/simple\n", + "Collecting fire (from -r requirements.txt (line 1))\n", + " Downloading http://repo.myhuaweicloud.com/repository/pypi/packages/1b/1b/84c63f592ecdfbb3d77d22a8d93c9b92791e4fa35677ad71a7d6449100f8/fire-0.6.0.tar.gz (88 kB)\n", + " Preparing metadata (setup.py) ... \u001b[?25ldone\n", + "\u001b[?25hCollecting sentencepiece (from -r requirements.txt (line 2))\n", + " Downloading http://repo.myhuaweicloud.com/repository/pypi/packages/5f/01/c95e42eb86282b2c79305d3e0b0ca5a743f85a61262bb7130999c70b9374/sentencepiece-0.2.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.3 MB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.3/1.3 MB\u001b[0m \u001b[31m53.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25hRequirement already satisfied: mindspore>=2.2.0 in /home/ma-user/anaconda3/envs/python-3.9.0/lib/python3.9/site-packages (from -r requirements.txt (line 3)) (2.2.14)\n", + "Collecting ml_dtypes (from -r requirements.txt (line 4))\n", + " Downloading http://repo.myhuaweicloud.com/repository/pypi/packages/a8/6f/49effaafbc24c7665bcea42cacb22e7198bbab5b473d908c5900c6bb6a59/ml_dtypes-0.5.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.5 MB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m4.5/4.5 MB\u001b[0m \u001b[31m97.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25hCollecting simple-parsing (from -r requirements.txt (line 5))\n", + " Downloading http://repo.myhuaweicloud.com/repository/pypi/packages/f8/62/0194ea1d8245d76bfd2d5c6bebec7f824af00a6e03bec22697d12712fff0/simple_parsing-0.1.6-py3-none-any.whl (112 kB)\n", + "Requirement already satisfied: six in /home/ma-user/anaconda3/envs/python-3.9.0/lib/python3.9/site-packages (from fire->-r requirements.txt (line 1)) (1.16.0)\n", + "Collecting termcolor (from fire->-r requirements.txt (line 1))\n", + " Downloading http://repo.myhuaweicloud.com/repository/pypi/packages/d9/5f/8c716e47b3a50cbd7c146f45881e11d9414def768b7cd9c5e6650ec2a80a/termcolor-2.4.0-py3-none-any.whl (7.7 kB)\n", + "Requirement already satisfied: numpy>=1.17.0 in /home/ma-user/anaconda3/envs/python-3.9.0/lib/python3.9/site-packages (from mindspore>=2.2.0->-r requirements.txt (line 3)) (2.0.2)\n", + "Requirement already satisfied: protobuf>=3.13.0 in /home/ma-user/anaconda3/envs/python-3.9.0/lib/python3.9/site-packages (from mindspore>=2.2.0->-r requirements.txt (line 3)) (5.28.2)\n", + "Requirement already satisfied: asttokens>=2.0.4 in /home/ma-user/anaconda3/envs/python-3.9.0/lib/python3.9/site-packages (from mindspore>=2.2.0->-r requirements.txt (line 3)) (2.4.1)\n", + "Requirement already satisfied: pillow>=6.2.0 in /home/ma-user/anaconda3/envs/python-3.9.0/lib/python3.9/site-packages (from mindspore>=2.2.0->-r requirements.txt (line 3)) (10.4.0)\n", + "Requirement already satisfied: scipy>=1.5.4 in /home/ma-user/anaconda3/envs/python-3.9.0/lib/python3.9/site-packages (from mindspore>=2.2.0->-r requirements.txt (line 3)) (1.13.1)\n", + "Requirement already satisfied: packaging>=20.0 in /home/ma-user/anaconda3/envs/python-3.9.0/lib/python3.9/site-packages (from mindspore>=2.2.0->-r requirements.txt (line 3)) (24.1)\n", + "Requirement already satisfied: psutil>=5.6.1 in /home/ma-user/anaconda3/envs/python-3.9.0/lib/python3.9/site-packages (from mindspore>=2.2.0->-r requirements.txt (line 3)) (6.0.0)\n", + "Requirement already satisfied: astunparse>=1.6.3 in /home/ma-user/anaconda3/envs/python-3.9.0/lib/python3.9/site-packages (from mindspore>=2.2.0->-r requirements.txt (line 3)) (1.6.3)\n", + "Collecting docstring-parser<1.0,>=0.15 (from simple-parsing->-r requirements.txt (line 5))\n", + " Downloading http://repo.myhuaweicloud.com/repository/pypi/packages/d5/7c/e9fcff7623954d86bdc17782036cbf715ecab1bec4847c008557affe1ca8/docstring_parser-0.16-py3-none-any.whl (36 kB)\n", + "Requirement already satisfied: typing-extensions>=4.5.0 in /home/ma-user/anaconda3/envs/python-3.9.0/lib/python3.9/site-packages (from simple-parsing->-r requirements.txt (line 5)) (4.12.2)\n", + "Requirement already satisfied: wheel<1.0,>=0.23.0 in /home/ma-user/anaconda3/envs/python-3.9.0/lib/python3.9/site-packages (from astunparse>=1.6.3->mindspore>=2.2.0->-r requirements.txt (line 3)) (0.44.0)\n", + "Building wheels for collected packages: fire\n", + " Building wheel for fire (setup.py) ... \u001b[?25ldone\n", + "\u001b[?25h Created wheel for fire: filename=fire-0.6.0-py2.py3-none-any.whl size=117030 sha256=efee33833adcfa639c6020824e2be68c9cbc448fc600eceb23275b8b4e68e6f2\n", + " Stored in directory: /home/ma-user/.cache/pip/wheels/98/30/dc/451d7dc76e61600afff0b46661fbcc0f395e47ea2064e9c1e6\n", + "Successfully built fire\n", + "Installing collected packages: sentencepiece, termcolor, ml_dtypes, docstring-parser, simple-parsing, fire\n", + "Successfully installed docstring-parser-0.16 fire-0.6.0 ml_dtypes-0.5.0 sentencepiece-0.2.0 simple-parsing-0.1.6 termcolor-2.4.0\n" + ] + } + ], + "source": [ + "!pip install -r requirements.txt" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0704291b-8d30-4a3c-aade-97a3f695b1bd", + "metadata": {}, + "outputs": [], + "source": [ + "!tar -xf mistral.tar" + ] + }, + { + "cell_type": "markdown", + "id": "ff0b2299", + "metadata": {}, + "source": [ + "下载mistral-7B-v0.1模型" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "90a355fb-5071-46f6-951e-97ce68be3d93", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "--2024-09-21 22:31:54-- https://files.mistral-7b-v0-1.mistral.ai/mistral-7B-v0.1.tar\n", + "Resolving proxy.modelarts.com (proxy.modelarts.com)... 192.168.6.3\n", + "Connecting to proxy.modelarts.com (proxy.modelarts.com)|192.168.6.3|:80... connected.\n", + "Proxy request sent, awaiting response... 302 Moved Temporarily\n", + "Location: https://models.mistralcdn.com/mistral-7b-v0-1/mistral-7B-v0.1.tar [following]\n", + "--2024-09-21 22:31:55-- https://models.mistralcdn.com/mistral-7b-v0-1/mistral-7B-v0.1.tar\n", + "Connecting to proxy.modelarts.com (proxy.modelarts.com)|192.168.6.3|:80... connected.\n", + "Proxy request sent, awaiting response... 200 OK\n", + "Length: 14484023296 (13G) [application/x-tar]\n", + "Saving to: ‘mistral-7B-v0.1.tar’\n", + "\n", + "mistral-7B-v0.1.tar 100%[===================>] 13.49G 3.32MB/s in 67m 36s \n", + "\n", + "2024-09-21 23:39:32 (3.41 MB/s) - ‘mistral-7B-v0.1.tar’ saved [14484023296/14484023296]\n", + "\n" + ] + } + ], + "source": [ + "!wget https://files.mistral-7b-v0-1.mistral.ai/mistral-7B-v0.1.tar" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "28dc62ff-8714-40b3-a7cf-782c528b4099", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "tar: Ignoring unknown extended header keyword 'LIBARCHIVE.xattr.com.apple.provenance'\n", + "tar: Ignoring unknown extended header keyword 'LIBARCHIVE.xattr.com.apple.provenance'\n", + "tar: Ignoring unknown extended header keyword 'LIBARCHIVE.xattr.com.apple.provenance'\n", + "tar: Ignoring unknown extended header keyword 'LIBARCHIVE.xattr.com.apple.provenance'\n", + "tar: Ignoring unknown extended header keyword 'LIBARCHIVE.xattr.com.apple.provenance'\n" + ] + } + ], + "source": [ + "!tar -xf mistral-7B-v0.1.tar" + ] + }, + { + "cell_type": "markdown", + "id": "8895cefb", + "metadata": {}, + "source": [ + "运行模型" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "6e5a8601-16ec-4684-b765-a924f66718cd", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This is a test of the emergency broadcast system. This is only a test.\n", + "\n", + "If this were a real emergency, you would be told what to do.\n", + "\n", + "This is a test\n", + "=====================\n", + "This is another great testament to the power of the internet.\n", + "\n", + "I was looking for a new pair of running shoes. I’ve been running in the same pair of shoes for about \n", + "=====================\n", + "This is a third test, mistral AI is very good at testing. 🙂\n", + "\n", + "This is a third test, mistral AI is very good at testing. 🙂\n", + "\n", + "This is a third test, mistral AI is very good at\n", + "=====================\n" + ] + } + ], + "source": [ + "!python -m main demo /mistral-7B-v0.1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "718bb5cc-609b-45f8-8762-aa59e87a5c13", + "metadata": {}, + "outputs": [], + "source": [ + "# !python -m main interactive /mistral-7B-v0.1" + ] + } + ], + "metadata": { + "AIGalleryInfo": { + "item_id": "cdc88c83-1ac2-4862-b822-3ab200b01736" + }, + "flavorInfo": { + "architecture": "X86_64", + "category": "GPU" + }, + "imageInfo": { + "id": "e1a07296-22a8-4f05-8bc8-e936c8e54202", + "name": "mindspore1.7.0-cuda10.1-py3.7-ubuntu18.04" + }, + "kernelspec": { + "display_name": "python-3.9.0", + "language": "python", + "name": "python-3.9.0" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.0" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}