Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed Season2.step_into_llm/07.RWKV/RWKV.pdf
Binary file not shown.
Binary file added Season2.step_into_llm/07.RWKV/RWKV.pptx
Binary file not shown.
352 changes: 352 additions & 0 deletions Season2.step_into_llm/07.RWKV/run_rwkv.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,352 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "56a97ac0",
"metadata": {},
"source": [
"环境安装 \n"
]
},
{
"cell_type": "markdown",
"id": "a7e595f1",
"metadata": {},
"source": [
" python 3.9.0 \n",
" mindspore 2.2.14 \n",
" mindnlp 0.4daily "
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "f9b08a2d-26ac-4028-9eb7-87cb1d12d7d1",
"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": "fff10db6-08d5-4105-8874-353ac2ffca7d",
"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": "da946ffb-f044-4877-ac13-e15de024dd49",
"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",
"# 0.4 daily 网址:https://repo.mindspore.cn/mindspore-lab/mindnlp/newest/any/\n",
"!pip install mindnlp-0.4.0-py3-none-any.whl\n",
"!pip install ipywidgets -i https://pypi.tuna.tsinghua.edu.cn/simple"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "f300d660-4cbe-4c87-8c6f-cafbab7f1c27",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"env: HF_ENDPOINT=https://hf-mirror.com\n"
]
}
],
"source": [
"%env HF_ENDPOINT=https://hf-mirror.com"
]
},
{
"cell_type": "markdown",
"id": "72e5ecd2",
"metadata": {},
"source": [
"示例测试运行"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "93231ee6-5fbd-4ed8-b0a2-a222f16b5327",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Building prefix dict from the default dictionary ...\n",
"Dumping model to file cache /tmp/jieba.cache\n",
"Loading model cost 0.782 seconds.\n",
"Prefix dict has been built successfully.\n"
]
}
],
"source": [
"import mindspore\n",
"from mindnlp.transformers import RwkvForCausalLM, AutoTokenizer"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "f490cd8a-948e-4c1d-bb6e-8223e3389402",
"metadata": {},
"outputs": [],
"source": [
"expected_output = \"Hello my name is Jasmine and I am a newbie to the\""
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "c14a2b61-cb45-4c5a-bd15-03f8d88abc91",
"metadata": {},
"outputs": [],
"source": [
"model_id = \"RWKV/rwkv-4-169m-pile\""
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "f80ccca8-f659-470f-8870-435ad29acf2b",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "6bd7d8931181452394ff4a60811ede70",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0.00/264 [00:00<?, ?B/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "f6b8c02c49824a1e8ec16a6e2e74ad39",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"0.00B [00:00, ?B/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "907da33ee3e746adb3fcca8ff6b2fbc4",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0.00/99.0 [00:00<?, ?B/s]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"tokenizer = AutoTokenizer.from_pretrained(model_id)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "2023f160-f7d2-462c-9b6f-2c918eaadcbe",
"metadata": {},
"outputs": [],
"source": [
"input_ids = tokenizer(\"Hello my name is\", return_tensors=\"ms\").input_ids"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "fe457473-0df2-40b6-98f9-c517b9135c73",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "2c346d0aef084103beba96b97d612fe6",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0.00/338 [00:00<?, ?B/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "1d61b55255b540108f7bcd4e29b23b83",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0.00/646M [00:00<?, ?B/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"/usr/local/cuda/bin/nvcc /home/ma-user/anaconda3/envs/python-3.9.0/lib/python3.9/site-packages/mindnlp/_csrc/cuda/wkv.cu -o /home/ma-user/anaconda3/envs/python-3.9.0/lib/python3.9/site-packages/mindnlp/_csrc/cuda/wkv.so --shared -Xcompiler -fPIC -res-usage --maxrregcount 60 --use_fast_math -O3 -Xptxas -O3 --extra-device-vectorization -DTmax=1024\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"[WARNING] ME(3771:140571154990912,MainProcess):2024-10-08-10:38:41.522.653 [mindspore/ops/operations/custom_ops.py:637] For 'Custom', 'func_type': aot, 'func': /home/ma-user/anaconda3/envs/python-3.9.0/lib/python3.9/site-packages/mindnlp/_csrc/cuda/wkv.so:wkv_forward_with_state, no white list is set and it might cause problems. Set the legal path of the file in MS_CUSTOM_AOT_WHITE_LIST\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "b2bcf506f5fe427ab7b4a2e405499079",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0.00/116 [00:00<?, ?B/s]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# model = RwkvForCausalLM.from_pretrained(model_id,from_pt=True)\n",
"model = RwkvForCausalLM.from_pretrained(model_id, ms_dtype=mindspore.float32)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "69626a12-1969-4767-a006-0a1181d4da66",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"The attention mask and the pad token id were not set. As a consequence, you may observe unexpected behavior. Please pass your input's `attention_mask` to obtain reliable results.\n",
"Setting `pad_token_id` to `eos_token_id`:2 for open-end generation.\n",
"[WARNING] DEVICE(3771,7fd945c24740,python):2024-10-08-10:39:01.622.351 [mindspore/ccsrc/plugin/device/gpu/hal/device/kernel_info_setter.cc:241] SelectCustomKernel] Not find operator information for op[Custom__/home/ma-user/anaconda3/envs/python-3.9.0/lib/python3.9/site-packages/mindnlp/_csrc/cuda/wkv.so:wkv_forward_with_state]. Infer operator information from inputs.\n"
]
}
],
"source": [
"output = model.generate(input_ids, max_new_tokens=10)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "a8de2ab0-a3b6-470d-a586-ce17ee2fb8e5",
"metadata": {},
"outputs": [],
"source": [
"output_sentence = tokenizer.decode(output[0].tolist())"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "83849e07-1b7f-4a82-a5b0-4b2204ce09c2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello my name is.twitter&\n",
"\n",
"................………… ✓\t \n",
" Hello my name is Jasmine and I am a newbie to the\n"
]
}
],
"source": [
"print(output_sentence,\"\\n\", expected_output)"
]
}
],
"metadata": {
"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
}
13 changes: 0 additions & 13 deletions Season2.step_into_llm/07.RWKV/run_rwkv.py

This file was deleted.