Skip to content

Instantly share code, notes, and snippets.

@masuidrive
Last active June 6, 2023 13:44
Show Gist options
  • Save masuidrive/3d8d6a8d98652231e9029ac57fec6ddc to your computer and use it in GitHub Desktop.
Save masuidrive/3d8d6a8d98652231e9029ac57fec6ddc to your computer and use it in GitHub Desktop.
opencalm-7b-lora-inference.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"gpuType": "A100",
"machine_shape": "hm",
"authorship_tag": "ABX9TyOLzNNFL3SuC0LCvJAJCxbL",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
},
"accelerator": "GPU",
"gpuClass": "standard",
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"d8e0a7c0437c45ac8dc4b519dd876eeb": {
"model_module": "@jupyter-widgets/controls",
"model_name": "HBoxModel",
"model_module_version": "1.5.0",
"state": {
"_dom_classes": [],
"_model_module": "@jupyter-widgets/controls",
"_model_module_version": "1.5.0",
"_model_name": "HBoxModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/controls",
"_view_module_version": "1.5.0",
"_view_name": "HBoxView",
"box_style": "",
"children": [
"IPY_MODEL_f62a072ec8b44cbebf16fcda2816295f",
"IPY_MODEL_53a1dcfe844a4e11b3febecaa58e68fb",
"IPY_MODEL_f8e4848dd5c54f0d9d500dd998cb1d7f"
],
"layout": "IPY_MODEL_ff79cc06d33e4fd2a32afe6c9f068e3f"
}
},
"f62a072ec8b44cbebf16fcda2816295f": {
"model_module": "@jupyter-widgets/controls",
"model_name": "HTMLModel",
"model_module_version": "1.5.0",
"state": {
"_dom_classes": [],
"_model_module": "@jupyter-widgets/controls",
"_model_module_version": "1.5.0",
"_model_name": "HTMLModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/controls",
"_view_module_version": "1.5.0",
"_view_name": "HTMLView",
"description": "",
"description_tooltip": null,
"layout": "IPY_MODEL_e6c677efddca4d2fae8a478007db7775",
"placeholder": "​",
"style": "IPY_MODEL_10b3ab4c59554dd98e7128bb1b8090c0",
"value": "Loading checkpoint shards: 100%"
}
},
"53a1dcfe844a4e11b3febecaa58e68fb": {
"model_module": "@jupyter-widgets/controls",
"model_name": "FloatProgressModel",
"model_module_version": "1.5.0",
"state": {
"_dom_classes": [],
"_model_module": "@jupyter-widgets/controls",
"_model_module_version": "1.5.0",
"_model_name": "FloatProgressModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/controls",
"_view_module_version": "1.5.0",
"_view_name": "ProgressView",
"bar_style": "success",
"description": "",
"description_tooltip": null,
"layout": "IPY_MODEL_462c093593894dc491caae4a1fa61ad0",
"max": 2,
"min": 0,
"orientation": "horizontal",
"style": "IPY_MODEL_70a4ded50cd54e1b913c229de8aa8064",
"value": 2
}
},
"f8e4848dd5c54f0d9d500dd998cb1d7f": {
"model_module": "@jupyter-widgets/controls",
"model_name": "HTMLModel",
"model_module_version": "1.5.0",
"state": {
"_dom_classes": [],
"_model_module": "@jupyter-widgets/controls",
"_model_module_version": "1.5.0",
"_model_name": "HTMLModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/controls",
"_view_module_version": "1.5.0",
"_view_name": "HTMLView",
"description": "",
"description_tooltip": null,
"layout": "IPY_MODEL_21f70cd741074de693e65d09b369d50d",
"placeholder": "​",
"style": "IPY_MODEL_99b4eb57cc4c4c38a6b18b324f8d580a",
"value": " 2/2 [01:13<00:00, 31.00s/it]"
}
},
"ff79cc06d33e4fd2a32afe6c9f068e3f": {
"model_module": "@jupyter-widgets/base",
"model_name": "LayoutModel",
"model_module_version": "1.2.0",
"state": {
"_model_module": "@jupyter-widgets/base",
"_model_module_version": "1.2.0",
"_model_name": "LayoutModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/base",
"_view_module_version": "1.2.0",
"_view_name": "LayoutView",
"align_content": null,
"align_items": null,
"align_self": null,
"border": null,
"bottom": null,
"display": null,
"flex": null,
"flex_flow": null,
"grid_area": null,
"grid_auto_columns": null,
"grid_auto_flow": null,
"grid_auto_rows": null,
"grid_column": null,
"grid_gap": null,
"grid_row": null,
"grid_template_areas": null,
"grid_template_columns": null,
"grid_template_rows": null,
"height": null,
"justify_content": null,
"justify_items": null,
"left": null,
"margin": null,
"max_height": null,
"max_width": null,
"min_height": null,
"min_width": null,
"object_fit": null,
"object_position": null,
"order": null,
"overflow": null,
"overflow_x": null,
"overflow_y": null,
"padding": null,
"right": null,
"top": null,
"visibility": null,
"width": null
}
},
"e6c677efddca4d2fae8a478007db7775": {
"model_module": "@jupyter-widgets/base",
"model_name": "LayoutModel",
"model_module_version": "1.2.0",
"state": {
"_model_module": "@jupyter-widgets/base",
"_model_module_version": "1.2.0",
"_model_name": "LayoutModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/base",
"_view_module_version": "1.2.0",
"_view_name": "LayoutView",
"align_content": null,
"align_items": null,
"align_self": null,
"border": null,
"bottom": null,
"display": null,
"flex": null,
"flex_flow": null,
"grid_area": null,
"grid_auto_columns": null,
"grid_auto_flow": null,
"grid_auto_rows": null,
"grid_column": null,
"grid_gap": null,
"grid_row": null,
"grid_template_areas": null,
"grid_template_columns": null,
"grid_template_rows": null,
"height": null,
"justify_content": null,
"justify_items": null,
"left": null,
"margin": null,
"max_height": null,
"max_width": null,
"min_height": null,
"min_width": null,
"object_fit": null,
"object_position": null,
"order": null,
"overflow": null,
"overflow_x": null,
"overflow_y": null,
"padding": null,
"right": null,
"top": null,
"visibility": null,
"width": null
}
},
"10b3ab4c59554dd98e7128bb1b8090c0": {
"model_module": "@jupyter-widgets/controls",
"model_name": "DescriptionStyleModel",
"model_module_version": "1.5.0",
"state": {
"_model_module": "@jupyter-widgets/controls",
"_model_module_version": "1.5.0",
"_model_name": "DescriptionStyleModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/base",
"_view_module_version": "1.2.0",
"_view_name": "StyleView",
"description_width": ""
}
},
"462c093593894dc491caae4a1fa61ad0": {
"model_module": "@jupyter-widgets/base",
"model_name": "LayoutModel",
"model_module_version": "1.2.0",
"state": {
"_model_module": "@jupyter-widgets/base",
"_model_module_version": "1.2.0",
"_model_name": "LayoutModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/base",
"_view_module_version": "1.2.0",
"_view_name": "LayoutView",
"align_content": null,
"align_items": null,
"align_self": null,
"border": null,
"bottom": null,
"display": null,
"flex": null,
"flex_flow": null,
"grid_area": null,
"grid_auto_columns": null,
"grid_auto_flow": null,
"grid_auto_rows": null,
"grid_column": null,
"grid_gap": null,
"grid_row": null,
"grid_template_areas": null,
"grid_template_columns": null,
"grid_template_rows": null,
"height": null,
"justify_content": null,
"justify_items": null,
"left": null,
"margin": null,
"max_height": null,
"max_width": null,
"min_height": null,
"min_width": null,
"object_fit": null,
"object_position": null,
"order": null,
"overflow": null,
"overflow_x": null,
"overflow_y": null,
"padding": null,
"right": null,
"top": null,
"visibility": null,
"width": null
}
},
"70a4ded50cd54e1b913c229de8aa8064": {
"model_module": "@jupyter-widgets/controls",
"model_name": "ProgressStyleModel",
"model_module_version": "1.5.0",
"state": {
"_model_module": "@jupyter-widgets/controls",
"_model_module_version": "1.5.0",
"_model_name": "ProgressStyleModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/base",
"_view_module_version": "1.2.0",
"_view_name": "StyleView",
"bar_color": null,
"description_width": ""
}
},
"21f70cd741074de693e65d09b369d50d": {
"model_module": "@jupyter-widgets/base",
"model_name": "LayoutModel",
"model_module_version": "1.2.0",
"state": {
"_model_module": "@jupyter-widgets/base",
"_model_module_version": "1.2.0",
"_model_name": "LayoutModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/base",
"_view_module_version": "1.2.0",
"_view_name": "LayoutView",
"align_content": null,
"align_items": null,
"align_self": null,
"border": null,
"bottom": null,
"display": null,
"flex": null,
"flex_flow": null,
"grid_area": null,
"grid_auto_columns": null,
"grid_auto_flow": null,
"grid_auto_rows": null,
"grid_column": null,
"grid_gap": null,
"grid_row": null,
"grid_template_areas": null,
"grid_template_columns": null,
"grid_template_rows": null,
"height": null,
"justify_content": null,
"justify_items": null,
"left": null,
"margin": null,
"max_height": null,
"max_width": null,
"min_height": null,
"min_width": null,
"object_fit": null,
"object_position": null,
"order": null,
"overflow": null,
"overflow_x": null,
"overflow_y": null,
"padding": null,
"right": null,
"top": null,
"visibility": null,
"width": null
}
},
"99b4eb57cc4c4c38a6b18b324f8d580a": {
"model_module": "@jupyter-widgets/controls",
"model_name": "DescriptionStyleModel",
"model_module_version": "1.5.0",
"state": {
"_model_module": "@jupyter-widgets/controls",
"_model_module_version": "1.5.0",
"_model_name": "DescriptionStyleModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/base",
"_view_module_version": "1.2.0",
"_view_name": "StyleView",
"description_width": ""
}
}
}
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/masuidrive/3d8d6a8d98652231e9029ac57fec6ddc/opencalm-7b-lora-inference.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"source": [
"! pip install transformers peft accelerate bitsandbytes"
],
"metadata": {
"id": "8X7u3VEXddyA",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "43ff14e4-f9bb-4d73-9b6b-59ecb598222f"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
]
}
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "ou0vc_WldHuh",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 49,
"referenced_widgets": [
"d8e0a7c0437c45ac8dc4b519dd876eeb",
"f62a072ec8b44cbebf16fcda2816295f",
"53a1dcfe844a4e11b3febecaa58e68fb",
"f8e4848dd5c54f0d9d500dd998cb1d7f",
"ff79cc06d33e4fd2a32afe6c9f068e3f",
"e6c677efddca4d2fae8a478007db7775",
"10b3ab4c59554dd98e7128bb1b8090c0",
"462c093593894dc491caae4a1fa61ad0",
"70a4ded50cd54e1b913c229de8aa8064",
"21f70cd741074de693e65d09b369d50d",
"99b4eb57cc4c4c38a6b18b324f8d580a"
]
},
"outputId": "894335ec-44f4-4ab1-8f52-7c308f7651ee"
},
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
"Loading checkpoint shards: 0%| | 0/2 [00:00<?, ?it/s]"
],
"application/vnd.jupyter.widget-view+json": {
"version_major": 2,
"version_minor": 0,
"model_id": "d8e0a7c0437c45ac8dc4b519dd876eeb"
}
},
"metadata": {}
}
],
"source": [
"import torch\n",
"from peft import PeftModel, PeftConfig\n",
"from transformers import AutoModelForCausalLM, AutoTokenizer\n",
"\n",
"hf_peft_repo = \"masuidrive/open-calm-instruct-lora-20230525-r4-alpha16-batch32-epoch1\"\n",
"peft_config = PeftConfig.from_pretrained(hf_peft_repo)\n",
"\n",
"model = AutoModelForCausalLM.from_pretrained(\n",
" peft_config.base_model_name_or_path,\n",
" return_dict=True,\n",
" torch_dtype=torch.float16,\n",
" device_map='auto'\n",
" )\n",
"model = PeftModel.from_pretrained(model, hf_peft_repo)\n",
"\n",
"tokenizer = AutoTokenizer.from_pretrained(peft_config.base_model_name_or_path)\n"
]
},
{
"cell_type": "code",
"source": [
"\"\"\"\n",
"input/instruction/outputからpromptを作る\n",
"\"\"\"\n",
"def generate_prompt(instruction, input=None, response=None):\n",
" def add_escape(text):\n",
" return text.replace('### Response', '### Response')\n",
"\n",
" if input:\n",
" prompt = f\"\"\"\n",
"Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.\n",
"\n",
"### Instruction:\n",
"{add_escape(instruction.strip())}\n",
"\n",
"### Input:\n",
"{add_escape(input.strip())}\n",
"\"\"\".strip()\n",
" else:\n",
" prompt = f\"\"\"\n",
"Below is an instruction that describes a task. Write a response that appropriately completes the request.\n",
"\n",
"### Instruction:\n",
"{add_escape(instruction.strip())}\n",
"\"\"\".strip()\n",
"\n",
" if response:\n",
" prompt += f\"\\n\\n### Response:\\n{add_escape(response.strip())}<|endoftext|>\"\n",
" else:\n",
" prompt += f\"\\n\\n### Response:\\n\"\n",
"\n",
" return prompt\n",
"\n",
"def get_response(text):\n",
" marker = f\"### Response:\\n\"\n",
" pos = text.find(marker)\n",
" if pos == -1: # marker not found\n",
" return None\n",
" return text[pos + len(marker):].strip()\n",
"\n",
"def qa(instruction, context=None):\n",
" prompt = generate_prompt(instruction, context)\n",
"\n",
" batch = tokenizer(prompt, return_tensors='pt')\n",
" with torch.cuda.amp.autocast():\n",
" output_tokens = model.generate(\n",
" **batch,\n",
" max_new_tokens=256,\n",
" temperature = 0.7,\n",
" repetition_penalty=1.05\n",
" )\n",
"\n",
" text = tokenizer.decode(output_tokens[0],pad_token_id=tokenizer.pad_token_id,\n",
"skip_special_tokens=True)\n",
" return get_response(text)"
],
"metadata": {
"id": "Nu81EeNCd8NW"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"instruction = \"トヨタ自動車は何年設立ですか?\"\n",
"context = \"豊田自動織機製作所自動車部時代は、社名中の「豊田」の読みが「トヨダ」であったため、ロゴや刻印も英語は「TOYODA」であった。エンブレムは漢字の「豊田」を使用していた。しかし、1936年夏に行われた新トヨダマークの公募で、約27,000点の応募作品から選ばれたのは「トヨダ」ではなく「トヨタ」のマークだった。理由として、デザイン的にスマートであること、画数が8画で縁起がいいこと、個人名から離れ社会的存在へと発展することなどが挙げられている[11]。1936年9月25日に「トヨタ(TOYOTA)」の使用が開始され、翌年の自動車部門独立時も「トヨタ自動車工業株式会社」が社名に採用された。\"\n",
"print(\"----\")\n",
"print(instruction)\n",
"print(qa(instruction))\n",
"print(\"\")\n",
"print(qa(instruction, context))\n",
"print(\"\\n\")\n",
"\n",
"instruction = \"マイナンバーカードの受け取りは免許書を持って行けばいいですか?\"\n",
"context = \"マイナンバーカードの受け取りに必要な書類は以下のとおりです。\\n交付通知書\\n本人確認書類(有効期間内のもの)\\n顔写真付きの本人確認書類は1点\\nその他は2点(例:健康保険証+年金手帳)\\nお持ちの方のみ\\n通知カード\\n住民基本台帳カード\\nマイナンバーカード\\n詳しくは必要な持ち物をご確認ください。\"\n",
"print(\"\\n----\")\n",
"print(instruction)\n",
"print(qa(instruction))\n",
"print(\"\")\n",
"print(qa(instruction, context))\n",
"print(\"\\n\")\n",
"\n",
"\n",
"instruction = \"情報セキュリティ対策に関わる責任者と担当者の役割や権限が明確になっていますか?具体的には、個人情報保護責任者、個人情報保護担当者は任命されていますか?\"\n",
"context = \"個人情報保護管理者: 山形太郎\\n責任と権限\\n・JISQ15001に適合したPMSを構築、運用する\\n・PMSの運用状況や成果を、トップマネジメントに報告する\\n----\\n個人情報保護監査責任者\\n宮崎健吾\\n責任と権限\\n・内部監査計画書の作成\\n・内部監査員の選定\\n・内部監査の指揮 ・内部監査報告書の作成及びトップマネジメントへの報告\\n\"\n",
"print(\"\\n----\")\n",
"print(instruction)\n",
"print(qa(instruction))\n",
"print(\"\")\n",
"print(qa(instruction, context))\n",
"print(\"\\n\")\n",
"\n",
"\n",
"instruction = \"情報セキュリティ対策に関わる責任者と担当者の役割や権限が明確になっていますか?具体的には、個人情報保護責任者、個人情報保護担当者は任命されていますか?\"\n",
"context = \"個人情報保護管理者: 任命なし\\n責任と権限\\n・JISQ15001に適合したPMSを構築、運用する\\n・PMSの運用状況や成果を、トップマネジメントに報告する\\n----\\n個人情報保護監査責任者\\n宮崎健吾\\n責任と権限\\n・内部監査計画書の作成\\n・内部監査員の選定\\n・内部監査の指揮 ・内部監査報告書の作成及びトップマネジメントへの報告\\n\"\n",
"print(\"\\n----\")\n",
"print(instruction)\n",
"print(qa(instruction))\n",
"print(\"\")\n",
"print(qa(instruction, context))\n",
"print(\"\\n\")\n",
"\n",
"instruction = \"地球温暖化とはなんですか?\"\n",
"context = \"\"\"リモートワークでリモートワークができた\n",
"毎日でも食べたいということは毎日でも食べているというわけではない\n",
"今のままではいけないと思っています。だからこそ日本は今のままではいけないと思っている\n",
"約束は守るためにありますから、約束を守るために全力を尽くします\"\"\"\n",
"print(\"\\n----\")\n",
"print(instruction)\n",
"print(qa(instruction))\n",
"print(\"\")\n",
"print(qa(instruction, context))\n",
"print(\"\\n\")"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "oVTMFNPx8YZn",
"outputId": "c4252785-7663-4dfa-abf8-c06706783034"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"Setting `pad_token_id` to `eos_token_id`:0 for open-end generation.\n"
]
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"----\n",
"トヨタ自動車は何年設立ですか?\n"
]
},
{
"output_type": "stream",
"name": "stderr",
"text": [
"/usr/local/lib/python3.10/dist-packages/transformers/generation/utils.py:1445: UserWarning: You are calling .generate() with the `input_ids` being on a device type different than your model's device. `input_ids` is on cpu, whereas the model is on cuda. You may experience unexpected behaviors or slower generation. Please make sure that you have put `input_ids` to the correct device by calling for example input_ids = input_ids.to('cuda') before running `.generate()`.\n",
" warnings.warn(\n",
"Setting `pad_token_id` to `eos_token_id`:0 for open-end generation.\n"
]
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"1948年に設立され、現在は世界最大の自動車製造会社であり、世界で最も有名なブランドの一つです。\n",
"\n"
]
},
{
"output_type": "stream",
"name": "stderr",
"text": [
"Setting `pad_token_id` to `eos_token_id`:0 for open-end generation.\n"
]
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"1938年\n",
"\n",
"\n",
"\n",
"----\n",
"マイナンバーカードの受け取りは免許書を持って行けばいいですか?\n"
]
},
{
"output_type": "stream",
"name": "stderr",
"text": [
"Setting `pad_token_id` to `eos_token_id`:0 for open-end generation.\n"
]
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"運転免許証があれば、マイナンバーカードを受け取ることができます。\n",
"\n"
]
},
{
"output_type": "stream",
"name": "stderr",
"text": [
"Setting `pad_token_id` to `eos_token_id`:0 for open-end generation.\n"
]
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"運転免許証、パスポート、顔写真付きの本人確認書類は1点、その他の2点は2点必要です。\n",
"\n",
"\n",
"\n",
"----\n",
"情報セキュリティ対策に関わる責任者と担当者の役割や権限が明確になっていますか?具体的には、個人情報保護責任者、個人情報保護担当者は任命されていますか?\n"
]
},
{
"output_type": "stream",
"name": "stderr",
"text": [
"Setting `pad_token_id` to `eos_token_id`:0 for open-end generation.\n"
]
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"はい。情報セキュリティ管理の責任者として任命された人がいます。\n",
"\n"
]
},
{
"output_type": "stream",
"name": "stderr",
"text": [
"Setting `pad_token_id` to `eos_token_id`:0 for open-end generation.\n"
]
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"はい。\n",
"\n",
"\n",
"\n",
"----\n",
"情報セキュリティ対策に関わる責任者と担当者の役割や権限が明確になっていますか?具体的には、個人情報保護責任者、個人情報保護担当者は任命されていますか?\n"
]
},
{
"output_type": "stream",
"name": "stderr",
"text": [
"Setting `pad_token_id` to `eos_token_id`:0 for open-end generation.\n"
]
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"はい。情報セキュリティ管理の責任者として任命された人がいます。\n",
"\n"
]
},
{
"output_type": "stream",
"name": "stderr",
"text": [
"Setting `pad_token_id` to `eos_token_id`:0 for open-end generation.\n"
]
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"いいえ。\n",
"\n",
"\n",
"\n",
"----\n",
"地球温暖化とはなんですか?\n"
]
},
{
"output_type": "stream",
"name": "stderr",
"text": [
"Setting `pad_token_id` to `eos_token_id`:0 for open-end generation.\n"
]
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"地球温暖化は、大気中の温室効果ガスの濃度が上昇することを指します。温室効果ガスは、太陽からの熱を宇宙に逃がすのを妨げるため、地球の気温が上昇します。\n",
"\n",
"気候変動は、地球の温度を上昇させることです。\n",
"\n",
"\n"
]
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment