Skip to content

Instantly share code, notes, and snippets.

@daviddwlee84
Last active June 13, 2024 09:00
Show Gist options
  • Save daviddwlee84/f5243d0954da2e95f60404b81b518090 to your computer and use it in GitHub Desktop.
Save daviddwlee84/f5243d0954da2e95f60404b81b518090 to your computer and use it in GitHub Desktop.
fingpt_forecaster.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"gpuType": "T4",
"authorship_tag": "ABX9TyN00GQ+Kl9OhNp5ut5RM5Vd",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
},
"accelerator": "GPU",
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"f7e7b11d5ecd47b88fbb20de4b78020c": {
"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_197a2e7128a649c99f1de3ad7836246f",
"IPY_MODEL_76b709237aac4c029c08eb261d08210b",
"IPY_MODEL_0ae72f5ac5454f05bff38594eba7d3cd"
],
"layout": "IPY_MODEL_b68d1cc2de23472fadb81e0b34b27421"
}
},
"197a2e7128a649c99f1de3ad7836246f": {
"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_f09a5e607ecb4420837c77add0911a51",
"placeholder": "​",
"style": "IPY_MODEL_acbf0d755773467e8584e07f31198bde",
"value": "Loading checkpoint shards: 100%"
}
},
"76b709237aac4c029c08eb261d08210b": {
"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_35aeb162ae764427a9681b301ef85b80",
"max": 2,
"min": 0,
"orientation": "horizontal",
"style": "IPY_MODEL_d89866ec88024736966067822000b873",
"value": 2
}
},
"0ae72f5ac5454f05bff38594eba7d3cd": {
"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_4b470534228d4ed6a4c58ba82938a583",
"placeholder": "​",
"style": "IPY_MODEL_13aae05903f3480081b1511441a751d5",
"value": " 2/2 [00:56<00:00, 25.93s/it]"
}
},
"b68d1cc2de23472fadb81e0b34b27421": {
"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
}
},
"f09a5e607ecb4420837c77add0911a51": {
"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
}
},
"acbf0d755773467e8584e07f31198bde": {
"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": ""
}
},
"35aeb162ae764427a9681b301ef85b80": {
"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
}
},
"d89866ec88024736966067822000b873": {
"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": ""
}
},
"4b470534228d4ed6a4c58ba82938a583": {
"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
}
},
"13aae05903f3480081b1511441a751d5": {
"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/daviddwlee84/f5243d0954da2e95f60404b81b518090/fingpt_forecaster.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
"# FinGPT\n",
"\n",
"- Demo: [FinGPT Forecaster Chinese - a Hugging Face Space by FinGPT](https://huggingface.co/spaces/FinGPT/FinGPT-Forecaster-Chinese)\n",
" - [app.py · FinGPT/FinGPT-Forecaster-Chinese at main](https://huggingface.co/spaces/FinGPT/FinGPT-Forecaster-Chinese/blob/main/app.py)\n",
"- Model:\n",
" - [FinGPT/fingpt-forecaster_sz50_llama2-7B_lora · Hugging Face](https://huggingface.co/FinGPT/fingpt-forecaster_sz50_llama2-7B_lora)\n",
" - [FinGPT/fingpt-forecaster_dow30_llama2-7b_lora · Hugging Face](https://huggingface.co/FinGPT/fingpt-forecaster_dow30_llama2-7b_lora)\n",
"- Code:\n",
" - [FinGPT/fingpt/FinGPT_Forecaster at master · AI4Finance-Foundation/FinGPT](https://github.com/AI4Finance-Foundation/FinGPT/tree/master/fingpt/FinGPT_Forecaster)"
],
"metadata": {
"id": "-dhoHRrVvUzK"
}
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "XzPOtkopu-FV",
"outputId": "7c13ed82-f94f-46f3-cc77-0f9c4a569a11"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Requirement already satisfied: transformers in /usr/local/lib/python3.10/dist-packages (4.41.2)\n",
"Requirement already satisfied: peft in /usr/local/lib/python3.10/dist-packages (0.11.1)\n",
"Requirement already satisfied: torchinfo in /usr/local/lib/python3.10/dist-packages (1.8.0)\n",
"Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from transformers) (3.14.0)\n",
"Requirement already satisfied: huggingface-hub<1.0,>=0.23.0 in /usr/local/lib/python3.10/dist-packages (from transformers) (0.23.3)\n",
"Requirement already satisfied: numpy>=1.17 in /usr/local/lib/python3.10/dist-packages (from transformers) (1.25.2)\n",
"Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.10/dist-packages (from transformers) (24.1)\n",
"Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.10/dist-packages (from transformers) (6.0.1)\n",
"Requirement already satisfied: regex!=2019.12.17 in /usr/local/lib/python3.10/dist-packages (from transformers) (2024.5.15)\n",
"Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from transformers) (2.31.0)\n",
"Requirement already satisfied: tokenizers<0.20,>=0.19 in /usr/local/lib/python3.10/dist-packages (from transformers) (0.19.1)\n",
"Requirement already satisfied: safetensors>=0.4.1 in /usr/local/lib/python3.10/dist-packages (from transformers) (0.4.3)\n",
"Requirement already satisfied: tqdm>=4.27 in /usr/local/lib/python3.10/dist-packages (from transformers) (4.66.4)\n",
"Requirement already satisfied: psutil in /usr/local/lib/python3.10/dist-packages (from peft) (5.9.5)\n",
"Requirement already satisfied: torch>=1.13.0 in /usr/local/lib/python3.10/dist-packages (from peft) (2.3.0+cu121)\n",
"Requirement already satisfied: accelerate>=0.21.0 in /usr/local/lib/python3.10/dist-packages (from peft) (0.31.0)\n",
"Requirement already satisfied: fsspec>=2023.5.0 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub<1.0,>=0.23.0->transformers) (2023.6.0)\n",
"Requirement already satisfied: typing-extensions>=3.7.4.3 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub<1.0,>=0.23.0->transformers) (4.12.2)\n",
"Requirement already satisfied: sympy in /usr/local/lib/python3.10/dist-packages (from torch>=1.13.0->peft) (1.12.1)\n",
"Requirement already satisfied: networkx in /usr/local/lib/python3.10/dist-packages (from torch>=1.13.0->peft) (3.3)\n",
"Requirement already satisfied: jinja2 in /usr/local/lib/python3.10/dist-packages (from torch>=1.13.0->peft) (3.1.4)\n",
"Requirement already satisfied: nvidia-cuda-nvrtc-cu12==12.1.105 in /usr/local/lib/python3.10/dist-packages (from torch>=1.13.0->peft) (12.1.105)\n",
"Requirement already satisfied: nvidia-cuda-runtime-cu12==12.1.105 in /usr/local/lib/python3.10/dist-packages (from torch>=1.13.0->peft) (12.1.105)\n",
"Requirement already satisfied: nvidia-cuda-cupti-cu12==12.1.105 in /usr/local/lib/python3.10/dist-packages (from torch>=1.13.0->peft) (12.1.105)\n",
"Requirement already satisfied: nvidia-cudnn-cu12==8.9.2.26 in /usr/local/lib/python3.10/dist-packages (from torch>=1.13.0->peft) (8.9.2.26)\n",
"Requirement already satisfied: nvidia-cublas-cu12==12.1.3.1 in /usr/local/lib/python3.10/dist-packages (from torch>=1.13.0->peft) (12.1.3.1)\n",
"Requirement already satisfied: nvidia-cufft-cu12==11.0.2.54 in /usr/local/lib/python3.10/dist-packages (from torch>=1.13.0->peft) (11.0.2.54)\n",
"Requirement already satisfied: nvidia-curand-cu12==10.3.2.106 in /usr/local/lib/python3.10/dist-packages (from torch>=1.13.0->peft) (10.3.2.106)\n",
"Requirement already satisfied: nvidia-cusolver-cu12==11.4.5.107 in /usr/local/lib/python3.10/dist-packages (from torch>=1.13.0->peft) (11.4.5.107)\n",
"Requirement already satisfied: nvidia-cusparse-cu12==12.1.0.106 in /usr/local/lib/python3.10/dist-packages (from torch>=1.13.0->peft) (12.1.0.106)\n",
"Requirement already satisfied: nvidia-nccl-cu12==2.20.5 in /usr/local/lib/python3.10/dist-packages (from torch>=1.13.0->peft) (2.20.5)\n",
"Requirement already satisfied: nvidia-nvtx-cu12==12.1.105 in /usr/local/lib/python3.10/dist-packages (from torch>=1.13.0->peft) (12.1.105)\n",
"Requirement already satisfied: triton==2.3.0 in /usr/local/lib/python3.10/dist-packages (from torch>=1.13.0->peft) (2.3.0)\n",
"Requirement already satisfied: nvidia-nvjitlink-cu12 in /usr/local/lib/python3.10/dist-packages (from nvidia-cusolver-cu12==11.4.5.107->torch>=1.13.0->peft) (12.5.40)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->transformers) (3.3.2)\n",
"Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->transformers) (3.7)\n",
"Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests->transformers) (2.0.7)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests->transformers) (2024.6.2)\n",
"Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from jinja2->torch>=1.13.0->peft) (2.1.5)\n",
"Requirement already satisfied: mpmath<1.4.0,>=1.1.0 in /usr/local/lib/python3.10/dist-packages (from sympy->torch>=1.13.0->peft) (1.3.0)\n"
]
}
],
"source": [
"!pip install transformers peft torchinfo"
]
},
{
"cell_type": "markdown",
"source": [
"\n",
"```txt\n",
"UserWarning:\n",
"The secret `HF_TOKEN` does not exist in your Colab secrets.\n",
"To authenticate with the Hugging Face Hub, create a token in your settings tab (https://huggingface.co/settings/tokens), set it as secret in your Google Colab and restart your session.\n",
"You will be able to reuse this secret in all of your notebooks.\n",
"Please note that authentication is recommended but still optional to access public models or datasets.\n",
"```\n",
"\n",
"- [Setting Tokens - Hugging Face](https://huggingface.co/settings/tokens)\n",
"- [User access tokens](https://huggingface.co/docs/hub/security-tokens)"
],
"metadata": {
"id": "M7k116I5wZKM"
}
},
{
"cell_type": "code",
"source": [
"# https://chat.openai.com/share/8542fc55-df98-41f0-8cc8-cd81e1a7193b\n",
"from getpass import getpass\n",
"access_token = getpass(\"Enter Huggingface Hub Token (https://huggingface.co/settings/tokens): \")"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "vncEZoGyxF90",
"outputId": "8bd8da84-6045-4fde-fee9-cdaee0a344fa"
},
"execution_count": 2,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter Huggingface Hub Token (https://huggingface.co/settings/tokens): ··········\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"\n",
"```txt\n",
"OSError: You are trying to access a gated repo.\n",
"Make sure to have access to it at https://huggingface.co/meta-llama/Llama-2-7b-chat-hf.\n",
"401 Client Error. (Request ID: Root=1-6628627e-0845c29b3b9ffb484e7945f4;eca71148-3927-4211-a0d8-1566df43e057)\n",
"\n",
"Cannot access gated repo for url https://huggingface.co/meta-llama/Llama-2-7b-chat-hf/resolve/main/config.json.\n",
"Access to model meta-llama/Llama-2-7b-chat-hf is restricted. You must be authenticated to access it.\n",
"```\n",
"\n",
"- [meta-llama/Llama-2-7b-chat-hf · Hugging Face](https://huggingface.co/meta-llama/Llama-2-7b-chat-hf)\n",
"\n",
"> Model requires a Pro subscription; check out hf.co/pricing to learn more. Make sure to include your HF token in your query.\n",
"\n",
"---\n",
"\n",
"- [NousResearch/Llama-2-7b-chat-hf · Hugging Face](https://huggingface.co/NousResearch/Llama-2-7b-chat-hf)\n",
"- [NousResearch/Llama-2-7b-chat-hf · HF Mirror](https://hf-mirror.com/NousResearch/Llama-2-7b-chat-hf)"
],
"metadata": {
"id": "BNAZaWknzSgf"
}
},
{
"cell_type": "code",
"source": [
"# !pip install huggingface_hub"
],
"metadata": {
"id": "oiLDtBlo2KsF"
},
"execution_count": 3,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# !huggingface-cli login"
],
"metadata": {
"id": "EIOWfSHO27hz"
},
"execution_count": 4,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# !HF_ENDPOINT=https://hf-mirror.com huggingface-cli download --resume-download meta-llama/Llama-2-7b-chat-hf --local-dir meta-llama/Llama-2-7b-chat-hf"
],
"metadata": {
"id": "UK7FCkBn2XAI"
},
"execution_count": 5,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"\n",
"```txt\n",
"OSError: You are trying to access a gated repo.\n",
"Make sure to have access to it at https://huggingface.co/meta-llama/Llama-2-7b-chat-hf.\n",
"401 Client Error. (Request ID: Root=1-6628627e-0845c29b3b9ffb484e7945f4;eca71148-3927-4211-a0d8-1566df43e057)\n",
"\n",
"Cannot access gated repo for url https://huggingface.co/meta-llama/Llama-2-7b-chat-hf/resolve/main/config.json.\n",
"Access to model meta-llama/Llama-2-7b-chat-hf is restricted. You must be authenticated to access it.\n",
"```\n",
"\n",
"- [meta-llama/Llama-2-7b-chat-hf · Hugging Face](https://huggingface.co/meta-llama/Llama-2-7b-chat-hf)\n",
"\n",
"> Model requires a Pro subscription; check out hf.co/pricing to learn more. Make sure to include your HF token in your query."
],
"metadata": {
"id": "IlKztQJc2cBm"
}
},
{
"cell_type": "markdown",
"source": [
"\n",
"```txt\n",
"OSError: You are trying to access a gated repo.\n",
"Make sure to have access to it at https://huggingface.co/meta-llama/Llama-2-7b-chat-hf.\n",
"401 Client Error. (Request ID: Root=1-6628627e-0845c29b3b9ffb484e7945f4;eca71148-3927-4211-a0d8-1566df43e057)\n",
"\n",
"Cannot access gated repo for url https://huggingface.co/meta-llama/Llama-2-7b-chat-hf/resolve/main/config.json.\n",
"Access to model meta-llama/Llama-2-7b-chat-hf is restricted. You must be authenticated to access it.\n",
"```\n",
"\n",
"- [meta-llama/Llama-2-7b-chat-hf · Hugging Face](https://huggingface.co/meta-llama/Llama-2-7b-chat-hf)\n",
"\n",
"> Model requires a Pro subscription; check out hf.co/pricing to learn more. Make sure to include your HF token in your query."
],
"metadata": {
"id": "L9EyPjQ_2bdg"
}
},
{
"cell_type": "code",
"source": [
"# !git lfs install"
],
"metadata": {
"id": "gBtN2Wtt1gtm"
},
"execution_count": 6,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# https://github.com/TylerYep/torchinfo\n",
"from torchinfo import summary"
],
"metadata": {
"id": "HjBC_8MG2bPN"
},
"execution_count": 7,
"outputs": []
},
{
"cell_type": "code",
"source": [
"!pip install accelerate\n",
"!pip install -i https://pypi.org/simple/ bitsandbytes"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "lx5eOFHv5ydh",
"outputId": "4b2b6626-79d8-4aee-a34d-ca4e2c413c68"
},
"execution_count": 8,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Requirement already satisfied: accelerate in /usr/local/lib/python3.10/dist-packages (0.31.0)\n",
"Requirement already satisfied: numpy>=1.17 in /usr/local/lib/python3.10/dist-packages (from accelerate) (1.25.2)\n",
"Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.10/dist-packages (from accelerate) (24.1)\n",
"Requirement already satisfied: psutil in /usr/local/lib/python3.10/dist-packages (from accelerate) (5.9.5)\n",
"Requirement already satisfied: pyyaml in /usr/local/lib/python3.10/dist-packages (from accelerate) (6.0.1)\n",
"Requirement already satisfied: torch>=1.10.0 in /usr/local/lib/python3.10/dist-packages (from accelerate) (2.3.0+cu121)\n",
"Requirement already satisfied: huggingface-hub in /usr/local/lib/python3.10/dist-packages (from accelerate) (0.23.3)\n",
"Requirement already satisfied: safetensors>=0.3.1 in /usr/local/lib/python3.10/dist-packages (from accelerate) (0.4.3)\n",
"Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (3.14.0)\n",
"Requirement already satisfied: typing-extensions>=4.8.0 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (4.12.2)\n",
"Requirement already satisfied: sympy in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (1.12.1)\n",
"Requirement already satisfied: networkx in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (3.3)\n",
"Requirement already satisfied: jinja2 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (3.1.4)\n",
"Requirement already satisfied: fsspec in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (2023.6.0)\n",
"Requirement already satisfied: nvidia-cuda-nvrtc-cu12==12.1.105 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (12.1.105)\n",
"Requirement already satisfied: nvidia-cuda-runtime-cu12==12.1.105 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (12.1.105)\n",
"Requirement already satisfied: nvidia-cuda-cupti-cu12==12.1.105 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (12.1.105)\n",
"Requirement already satisfied: nvidia-cudnn-cu12==8.9.2.26 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (8.9.2.26)\n",
"Requirement already satisfied: nvidia-cublas-cu12==12.1.3.1 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (12.1.3.1)\n",
"Requirement already satisfied: nvidia-cufft-cu12==11.0.2.54 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (11.0.2.54)\n",
"Requirement already satisfied: nvidia-curand-cu12==10.3.2.106 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (10.3.2.106)\n",
"Requirement already satisfied: nvidia-cusolver-cu12==11.4.5.107 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (11.4.5.107)\n",
"Requirement already satisfied: nvidia-cusparse-cu12==12.1.0.106 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (12.1.0.106)\n",
"Requirement already satisfied: nvidia-nccl-cu12==2.20.5 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (2.20.5)\n",
"Requirement already satisfied: nvidia-nvtx-cu12==12.1.105 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (12.1.105)\n",
"Requirement already satisfied: triton==2.3.0 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (2.3.0)\n",
"Requirement already satisfied: nvidia-nvjitlink-cu12 in /usr/local/lib/python3.10/dist-packages (from nvidia-cusolver-cu12==11.4.5.107->torch>=1.10.0->accelerate) (12.5.40)\n",
"Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from huggingface-hub->accelerate) (2.31.0)\n",
"Requirement already satisfied: tqdm>=4.42.1 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub->accelerate) (4.66.4)\n",
"Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from jinja2->torch>=1.10.0->accelerate) (2.1.5)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface-hub->accelerate) (3.3.2)\n",
"Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface-hub->accelerate) (3.7)\n",
"Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface-hub->accelerate) (2.0.7)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface-hub->accelerate) (2024.6.2)\n",
"Requirement already satisfied: mpmath<1.4.0,>=1.1.0 in /usr/local/lib/python3.10/dist-packages (from sympy->torch>=1.10.0->accelerate) (1.3.0)\n",
"Looking in indexes: https://pypi.org/simple/\n",
"Requirement already satisfied: bitsandbytes in /usr/local/lib/python3.10/dist-packages (0.43.1)\n",
"Requirement already satisfied: torch in /usr/local/lib/python3.10/dist-packages (from bitsandbytes) (2.3.0+cu121)\n",
"Requirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages (from bitsandbytes) (1.25.2)\n",
"Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from torch->bitsandbytes) (3.14.0)\n",
"Requirement already satisfied: typing-extensions>=4.8.0 in /usr/local/lib/python3.10/dist-packages (from torch->bitsandbytes) (4.12.2)\n",
"Requirement already satisfied: sympy in /usr/local/lib/python3.10/dist-packages (from torch->bitsandbytes) (1.12.1)\n",
"Requirement already satisfied: networkx in /usr/local/lib/python3.10/dist-packages (from torch->bitsandbytes) (3.3)\n",
"Requirement already satisfied: jinja2 in /usr/local/lib/python3.10/dist-packages (from torch->bitsandbytes) (3.1.4)\n",
"Requirement already satisfied: fsspec in /usr/local/lib/python3.10/dist-packages (from torch->bitsandbytes) (2023.6.0)\n",
"Requirement already satisfied: nvidia-cuda-nvrtc-cu12==12.1.105 in /usr/local/lib/python3.10/dist-packages (from torch->bitsandbytes) (12.1.105)\n",
"Requirement already satisfied: nvidia-cuda-runtime-cu12==12.1.105 in /usr/local/lib/python3.10/dist-packages (from torch->bitsandbytes) (12.1.105)\n",
"Requirement already satisfied: nvidia-cuda-cupti-cu12==12.1.105 in /usr/local/lib/python3.10/dist-packages (from torch->bitsandbytes) (12.1.105)\n",
"Requirement already satisfied: nvidia-cudnn-cu12==8.9.2.26 in /usr/local/lib/python3.10/dist-packages (from torch->bitsandbytes) (8.9.2.26)\n",
"Requirement already satisfied: nvidia-cublas-cu12==12.1.3.1 in /usr/local/lib/python3.10/dist-packages (from torch->bitsandbytes) (12.1.3.1)\n",
"Requirement already satisfied: nvidia-cufft-cu12==11.0.2.54 in /usr/local/lib/python3.10/dist-packages (from torch->bitsandbytes) (11.0.2.54)\n",
"Requirement already satisfied: nvidia-curand-cu12==10.3.2.106 in /usr/local/lib/python3.10/dist-packages (from torch->bitsandbytes) (10.3.2.106)\n",
"Requirement already satisfied: nvidia-cusolver-cu12==11.4.5.107 in /usr/local/lib/python3.10/dist-packages (from torch->bitsandbytes) (11.4.5.107)\n",
"Requirement already satisfied: nvidia-cusparse-cu12==12.1.0.106 in /usr/local/lib/python3.10/dist-packages (from torch->bitsandbytes) (12.1.0.106)\n",
"Requirement already satisfied: nvidia-nccl-cu12==2.20.5 in /usr/local/lib/python3.10/dist-packages (from torch->bitsandbytes) (2.20.5)\n",
"Requirement already satisfied: nvidia-nvtx-cu12==12.1.105 in /usr/local/lib/python3.10/dist-packages (from torch->bitsandbytes) (12.1.105)\n",
"Requirement already satisfied: triton==2.3.0 in /usr/local/lib/python3.10/dist-packages (from torch->bitsandbytes) (2.3.0)\n",
"Requirement already satisfied: nvidia-nvjitlink-cu12 in /usr/local/lib/python3.10/dist-packages (from nvidia-cusolver-cu12==11.4.5.107->torch->bitsandbytes) (12.5.40)\n",
"Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from jinja2->torch->bitsandbytes) (2.1.5)\n",
"Requirement already satisfied: mpmath<1.4.0,>=1.1.0 in /usr/local/lib/python3.10/dist-packages (from sympy->torch->bitsandbytes) (1.3.0)\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"import torch\n",
"from transformers import AutoTokenizer, AutoModelForCausalLM\n",
"from peft import PeftModel\n",
"\n",
"# base_model = AutoModelForCausalLM.from_pretrained(\n",
"# # 'meta-llama/Llama-2-7b-chat-hf',\n",
"# 'NousResearch/Llama-2-7b-chat-hf',\n",
"# trust_remote_code=True,\n",
"# device_map=\"auto\",\n",
"# torch_dtype=torch.float16, # optional if you have enough VRAM\n",
"# token=access_token,\n",
"# )\n",
"\n",
"# The `load_in_4bit` and `load_in_8bit` arguments are deprecated and will be removed in the future versions. Please, pass a `BitsAndBytesConfig` object in `quantization_config` argument instead.\n",
"\n",
"base_model = AutoModelForCausalLM.from_pretrained(\n",
" 'NousResearch/Llama-2-7b-chat-hf',\n",
" trust_remote_code=True,\n",
" token=access_token,\n",
" device_map=\"cuda\",\n",
" # ImportError: Using `bitsandbytes` 8-bit quantization requires Accelerate: `pip install accelerate` and the latest version of bitsandbytes: `pip install -i https://pypi.org/simple/ bitsandbytes`\n",
" # load_in_8bit=True,\n",
" torch_dtype=torch.float16,\n",
" offload_folder=\"offload/\",\n",
")\n",
"summary(base_model)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 972,
"referenced_widgets": [
"f7e7b11d5ecd47b88fbb20de4b78020c",
"197a2e7128a649c99f1de3ad7836246f",
"76b709237aac4c029c08eb261d08210b",
"0ae72f5ac5454f05bff38594eba7d3cd",
"b68d1cc2de23472fadb81e0b34b27421",
"f09a5e607ecb4420837c77add0911a51",
"acbf0d755773467e8584e07f31198bde",
"35aeb162ae764427a9681b301ef85b80",
"d89866ec88024736966067822000b873",
"4b470534228d4ed6a4c58ba82938a583",
"13aae05903f3480081b1511441a751d5"
]
},
"id": "qfF7mkO_vs-a",
"outputId": "5dc380c5-075b-4d5d-f2eb-6df54e6b774b"
},
"execution_count": 9,
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"/usr/local/lib/python3.10/dist-packages/huggingface_hub/utils/_token.py:89: UserWarning: \n",
"The secret `HF_TOKEN` does not exist in your Colab secrets.\n",
"To authenticate with the Hugging Face Hub, create a token in your settings tab (https://huggingface.co/settings/tokens), set it as secret in your Google Colab and restart your session.\n",
"You will be able to reuse this secret in all of your notebooks.\n",
"Please note that authentication is recommended but still optional to access public models or datasets.\n",
" warnings.warn(\n"
]
},
{
"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": "f7e7b11d5ecd47b88fbb20de4b78020c"
}
},
"metadata": {}
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"================================================================================\n",
"Layer (type:depth-idx) Param #\n",
"================================================================================\n",
"LlamaForCausalLM --\n",
"├─LlamaModel: 1-1 --\n",
"│ └─Embedding: 2-1 131,072,000\n",
"│ └─ModuleList: 2-2 --\n",
"│ │ └─LlamaDecoderLayer: 3-1 202,383,360\n",
"│ │ └─LlamaDecoderLayer: 3-2 202,383,360\n",
"│ │ └─LlamaDecoderLayer: 3-3 202,383,360\n",
"│ │ └─LlamaDecoderLayer: 3-4 202,383,360\n",
"│ │ └─LlamaDecoderLayer: 3-5 202,383,360\n",
"│ │ └─LlamaDecoderLayer: 3-6 202,383,360\n",
"│ │ └─LlamaDecoderLayer: 3-7 202,383,360\n",
"│ │ └─LlamaDecoderLayer: 3-8 202,383,360\n",
"│ │ └─LlamaDecoderLayer: 3-9 202,383,360\n",
"│ │ └─LlamaDecoderLayer: 3-10 202,383,360\n",
"│ │ └─LlamaDecoderLayer: 3-11 202,383,360\n",
"│ │ └─LlamaDecoderLayer: 3-12 202,383,360\n",
"│ │ └─LlamaDecoderLayer: 3-13 202,383,360\n",
"│ │ └─LlamaDecoderLayer: 3-14 202,383,360\n",
"│ │ └─LlamaDecoderLayer: 3-15 202,383,360\n",
"│ │ └─LlamaDecoderLayer: 3-16 202,383,360\n",
"│ │ └─LlamaDecoderLayer: 3-17 202,383,360\n",
"│ │ └─LlamaDecoderLayer: 3-18 202,383,360\n",
"│ │ └─LlamaDecoderLayer: 3-19 202,383,360\n",
"│ │ └─LlamaDecoderLayer: 3-20 202,383,360\n",
"│ │ └─LlamaDecoderLayer: 3-21 202,383,360\n",
"│ │ └─LlamaDecoderLayer: 3-22 202,383,360\n",
"│ │ └─LlamaDecoderLayer: 3-23 202,383,360\n",
"│ │ └─LlamaDecoderLayer: 3-24 202,383,360\n",
"│ │ └─LlamaDecoderLayer: 3-25 202,383,360\n",
"│ │ └─LlamaDecoderLayer: 3-26 202,383,360\n",
"│ │ └─LlamaDecoderLayer: 3-27 202,383,360\n",
"│ │ └─LlamaDecoderLayer: 3-28 202,383,360\n",
"│ │ └─LlamaDecoderLayer: 3-29 202,383,360\n",
"│ │ └─LlamaDecoderLayer: 3-30 202,383,360\n",
"│ │ └─LlamaDecoderLayer: 3-31 202,383,360\n",
"│ │ └─LlamaDecoderLayer: 3-32 202,383,360\n",
"│ └─LlamaRMSNorm: 2-3 4,096\n",
"├─Linear: 1-2 131,072,000\n",
"================================================================================\n",
"Total params: 6,738,415,616\n",
"Trainable params: 6,738,415,616\n",
"Non-trainable params: 0\n",
"================================================================================"
]
},
"metadata": {},
"execution_count": 9
}
]
},
{
"cell_type": "code",
"source": [
"# tokenizer = AutoTokenizer.from_pretrained('meta-llama/Llama-2-7b-chat-hf', token=access_token)\n",
"tokenizer = AutoTokenizer.from_pretrained('NousResearch/Llama-2-7b-chat-hf', token=access_token, trust_remote_code=True)\n",
"tokenizer.pad_token = tokenizer.eos_token\n",
"tokenizer.padding_side = \"right\"\n",
"tokenizer"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "LyBAELER3-Ou",
"outputId": "32b88a25-952c-4959-d6e6-840f44caf2cd"
},
"execution_count": 10,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"LlamaTokenizerFast(name_or_path='NousResearch/Llama-2-7b-chat-hf', vocab_size=32000, model_max_length=1000000000000000019884624838656, is_fast=True, padding_side='right', truncation_side='right', special_tokens={'bos_token': '<s>', 'eos_token': '</s>', 'unk_token': '<unk>', 'pad_token': '</s>'}, clean_up_tokenization_spaces=False), added_tokens_decoder={\n",
"\t0: AddedToken(\"<unk>\", rstrip=False, lstrip=False, single_word=False, normalized=True, special=True),\n",
"\t1: AddedToken(\"<s>\", rstrip=False, lstrip=False, single_word=False, normalized=True, special=True),\n",
"\t2: AddedToken(\"</s>\", rstrip=False, lstrip=False, single_word=False, normalized=True, special=True),\n",
"\t32000: AddedToken(\"<pad>\", rstrip=False, lstrip=False, single_word=False, normalized=True, special=False),\n",
"}"
]
},
"metadata": {},
"execution_count": 10
}
]
},
{
"cell_type": "code",
"source": [
"# model = AutoModelForCausalLM.from_pretrained('meta-llama/Llama-2-7b-chat-hf', trust_remote_code=True, token = access_token, device_map=\"cuda\", load_in_8bit=True, offload_folder=\"offload/\")\n",
"# model = PeftModel.from_pretrained('meta-llama/Llama-2-7b-chat-hf', 'FinGPT/fingpt-forecaster_dow30_llama2-7b_lora', offload_folder=\"offload/\")\n",
"\n",
"# ValueError: We need an `offload_dir` to dispatch this model according to this `device_map`, the following submodules need to be offloaded: base_model.model.model.layers.23, base_model.model.model.layers.24, base_model.model.model.layers.25, base_model.model.model.layers.26, base_model.model.model.layers.27, base_model.model.model.layers.28, base_model.model.model.layers.29, base_model.model.model.layers.30, base_model.model.model.layers.31, base_model.model.model.norm, base_model.model.lm_head.\n",
"# model = PeftModel.from_pretrained(base_model, 'FinGPT/fingpt-forecaster_dow30_llama2-7b_lora', token=access_token)\n",
"\n",
"model = PeftModel.from_pretrained(base_model, 'FinGPT/fingpt-forecaster_dow30_llama2-7b_lora', token = access_token, offload_folder=\"offload/\")\n",
"model = model.eval()\n",
"summary(model)"
],
"metadata": {
"id": "ozFUZyRDwJgh",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "0de50420-adec-4076-af79-190656487cf9"
},
"execution_count": 11,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"==========================================================================================\n",
"Layer (type:depth-idx) Param #\n",
"==========================================================================================\n",
"PeftModelForCausalLM --\n",
"├─LoraModel: 1-1 --\n",
"│ └─LlamaForCausalLM: 2-1 --\n",
"│ │ └─LlamaModel: 3-1 (6,627,332,096)\n",
"│ │ └─Linear: 3-2 (131,072,000)\n",
"==========================================================================================\n",
"Total params: 6,758,404,096\n",
"Trainable params: 0\n",
"Non-trainable params: 6,758,404,096\n",
"=========================================================================================="
]
},
"metadata": {},
"execution_count": 11
}
]
},
{
"cell_type": "code",
"source": [
"!pwd"
],
"metadata": {
"id": "nor2_8Mvthho",
"outputId": "8af40d51-ebf4-4138-9b5c-a884e20069d1",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"execution_count": 12,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"/content\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"## Merge LoRA with Base Model and Download\n",
"\n",
"- [Help with merging LoRA weights back into base model :-) - Beginners - Hugging Face Forums](https://discuss.huggingface.co/t/help-with-merging-lora-weights-back-into-base-model/40968/4)"
],
"metadata": {
"id": "4dM-D6PWwLSb"
}
},
{
"cell_type": "code",
"source": [
"(merged_model := model.merge_and_unload())"
],
"metadata": {
"id": "lRWXBeJMuRZ6",
"outputId": "fca66da9-e008-44d5-f859-0c9a9f202cd1",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"execution_count": 13,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"LlamaForCausalLM(\n",
" (model): LlamaModel(\n",
" (embed_tokens): Embedding(32000, 4096, padding_idx=0)\n",
" (layers): ModuleList(\n",
" (0-31): 32 x LlamaDecoderLayer(\n",
" (self_attn): LlamaSdpaAttention(\n",
" (q_proj): Linear(in_features=4096, out_features=4096, bias=False)\n",
" (k_proj): Linear(in_features=4096, out_features=4096, bias=False)\n",
" (v_proj): Linear(in_features=4096, out_features=4096, bias=False)\n",
" (o_proj): Linear(in_features=4096, out_features=4096, bias=False)\n",
" (rotary_emb): LlamaRotaryEmbedding()\n",
" )\n",
" (mlp): LlamaMLP(\n",
" (gate_proj): Linear(in_features=4096, out_features=11008, bias=False)\n",
" (up_proj): Linear(in_features=4096, out_features=11008, bias=False)\n",
" (down_proj): Linear(in_features=11008, out_features=4096, bias=False)\n",
" (act_fn): SiLU()\n",
" )\n",
" (input_layernorm): LlamaRMSNorm()\n",
" (post_attention_layernorm): LlamaRMSNorm()\n",
" )\n",
" )\n",
" (norm): LlamaRMSNorm()\n",
" )\n",
" (lm_head): Linear(in_features=4096, out_features=32000, bias=False)\n",
")"
]
},
"metadata": {},
"execution_count": 13
}
]
},
{
"cell_type": "code",
"source": [
"# base_model.save_pretrained('./integrated_model')\n",
"# model.save_pretrained('./integrated_model')\n",
"merged_model.save_pretrained('./integrated_model')\n",
"tokenizer.save_pretrained('./integrated_model')"
],
"metadata": {
"id": "OKl2wIWj4ViD",
"outputId": "65a75f91-2c20-4867-e061-76b6e68a17c1",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"execution_count": 14,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"('./integrated_model/tokenizer_config.json',\n",
" './integrated_model/special_tokens_map.json',\n",
" './integrated_model/tokenizer.model',\n",
" './integrated_model/added_tokens.json',\n",
" './integrated_model/tokenizer.json')"
]
},
"metadata": {},
"execution_count": 14
}
]
},
{
"cell_type": "code",
"source": [
"# https://stackoverflow.com/questions/48774285/how-to-download-file-created-in-colaboratory-workspace\n",
"# https://stackoverflow.com/questions/50453428/how-do-i-download-multiple-files-or-an-entire-folder-from-google-colab\n",
"!zip -r fingpt_model.zip ./integrated_model"
],
"metadata": {
"id": "iHEFeGa2r1pr",
"outputId": "541132a5-b271-4fa3-8518-748337320e69",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"execution_count": 15,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"updating: integrated_model/ (stored 0%)\n",
"updating: integrated_model/adapter_model.safetensors (deflated 9%)\n",
"updating: integrated_model/README.md (deflated 66%)\n",
"updating: integrated_model/tokenizer.json (deflated 74%)\n",
"updating: integrated_model/tokenizer_config.json (deflated 72%)\n",
"updating: integrated_model/adapter_config.json (deflated 54%)\n",
"updating: integrated_model/special_tokens_map.json (deflated 72%)\n",
"updating: integrated_model/added_tokens.json (stored 0%)\n",
"updating: integrated_model/tokenizer.model (deflated 55%)\n",
" adding: integrated_model/model.safetensors.index.json (deflated 95%)\n",
" adding: integrated_model/model-00002-of-00003.safetensors (deflated 8%)\n",
" adding: integrated_model/generation_config.json (deflated 35%)\n",
" adding: integrated_model/model-00001-of-00003.safetensors (deflated 9%)\n",
" adding: integrated_model/config.json (deflated 48%)\n",
" adding: integrated_model/model-00003-of-00003.safetensors (deflated 9%)\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"from google.colab import files\n",
"files.download(\"fingpt_model.zip\")"
],
"metadata": {
"id": "UDCJA05-sO-d",
"outputId": "361e56ac-6552-4826-9cad-0e07782a565f",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"execution_count": 16,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.Javascript object>"
],
"application/javascript": [
"\n",
" async function download(id, filename, size) {\n",
" if (!google.colab.kernel.accessAllowed) {\n",
" return;\n",
" }\n",
" const div = document.createElement('div');\n",
" const label = document.createElement('label');\n",
" label.textContent = `Downloading \"${filename}\": `;\n",
" div.appendChild(label);\n",
" const progress = document.createElement('progress');\n",
" progress.max = size;\n",
" div.appendChild(progress);\n",
" document.body.appendChild(div);\n",
"\n",
" const buffers = [];\n",
" let downloaded = 0;\n",
"\n",
" const channel = await google.colab.kernel.comms.open(id);\n",
" // Send a message to notify the kernel that we're ready.\n",
" channel.send({})\n",
"\n",
" for await (const message of channel.messages) {\n",
" // Send a message to notify the kernel that we're ready.\n",
" channel.send({})\n",
" if (message.buffers) {\n",
" for (const buffer of message.buffers) {\n",
" buffers.push(buffer);\n",
" downloaded += buffer.byteLength;\n",
" progress.value = downloaded;\n",
" }\n",
" }\n",
" }\n",
" const blob = new Blob(buffers, {type: 'application/binary'});\n",
" const a = document.createElement('a');\n",
" a.href = window.URL.createObjectURL(blob);\n",
" a.download = filename;\n",
" div.appendChild(a);\n",
" a.click();\n",
" div.remove();\n",
" }\n",
" "
]
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.Javascript object>"
],
"application/javascript": [
"download(\"download_005ffa06-4a87-473d-bbb4-1d3cc47aba52\", \"fingpt_model.zip\", 12366064336)"
]
},
"metadata": {}
}
]
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "hgQnSshb1Bjh"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## Inference"
],
"metadata": {
"id": "uyGNHFgKwPnp"
}
},
{
"cell_type": "code",
"source": [
"prompt = \"000001 是什麼股票?\"\n",
"tokenizer.tokenize(prompt)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "vPShRRnc41kn",
"outputId": "e96d23c0-cbad-4ff1-ccd3-22ccfe5fd562"
},
"execution_count": 17,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['▁',\n",
" '0',\n",
" '0',\n",
" '0',\n",
" '0',\n",
" '0',\n",
" '1',\n",
" '▁',\n",
" '是',\n",
" '<0xE4>',\n",
" '<0xBB>',\n",
" '<0x80>',\n",
" '<0xE9>',\n",
" '<0xBA>',\n",
" '<0xBC>',\n",
" '<0xE8>',\n",
" '<0x82>',\n",
" '<0xA1>',\n",
" '<0xE7>',\n",
" '<0xA5>',\n",
" '<0xA8>',\n",
" '?']"
]
},
"metadata": {},
"execution_count": 17
}
]
},
{
"cell_type": "code",
"source": [
"inputs = tokenizer(prompt, return_tensors='pt')\n",
"inputs = {key: value.to(model.device) for key, value in inputs.items()}\n",
"inputs"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "9RGYg99a1TEg",
"outputId": "2db334c4-7a2f-4bb7-d25f-53217e554aa1"
},
"execution_count": 18,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'input_ids': tensor([[ 1, 29871, 29900, 29900, 29900, 29900, 29900, 29896, 29871, 30392,\n",
" 231, 190, 131, 236, 189, 191, 235, 133, 164, 234,\n",
" 168, 171, 29973]], device='cuda:0'),\n",
" 'attention_mask': tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]],\n",
" device='cuda:0')}"
]
},
"metadata": {},
"execution_count": 18
}
]
},
{
"cell_type": "code",
"source": [
"from transformers import TextStreamer\n",
"streamer = TextStreamer(tokenizer)"
],
"metadata": {
"id": "AWgUsA9C27zZ"
},
"execution_count": 19,
"outputs": []
},
{
"cell_type": "code",
"source": [
"B_INST, E_INST = \"[INST]\", \"[/INST]\"\n",
"B_SYS, E_SYS = \"<<SYS>>\\n\", \"\\n<</SYS>>\\n\\n\"\n",
"# SYSTEM_PROMPT = \"你是一名经验丰富的股票市场分析师。你的任务是根据公司在过去几周内的相关新闻和季度财务状况,列出公司的积极发展和潜在担忧,然后结合你对整体金融经济市场的判断,对公司未来一周的股价变化提供预测和分析。\" \\\n",
"# \"你的回答语言应为中文。你的回答格式应该如下:\\n\\n[积极发展]:\\n1. ...\\n\\n[潜在担忧]:\\n1. ...\\n\\n[预测和分析]:\\n...\\n\"\n",
"\n",
"# prompt = B_INST + B_SYS + new_system_prompt + E_SYS + info + f\"\\n\\n基于在{end_date}之前的所有信息,让我们首先分析{stock}的积极发展和潜在担忧。请简洁地陈述,分别提出2-4个最重要的因素。大部分所提及的因素应该从公司的相关新闻中推断出来。\" \\\n",
"# f\"接下来请预测{symbol}下周({period})的股票涨跌幅,并提供一个总结分析来支持你的预测。\" + E_INST\n",
"\n",
"prompt = B_INST + B_SYS + \"你是一名經驗豐富的股票市場分析師\" + E_SYS + prompt + E_INST\n",
"print(prompt)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "nJWirQCR_hol",
"outputId": "6929c504-a11f-45c6-a524-9bdfab8a8fd1"
},
"execution_count": 20,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[INST]<<SYS>>\n",
"你是一名經驗豐富的股票市場分析師\n",
"<</SYS>>\n",
"\n",
"000001 是什麼股票?[/INST]\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"inputs = tokenizer(prompt, return_tensors='pt')\n",
"inputs = {key: value.to(model.device) for key, value in inputs.items()}\n",
"inputs"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "4pua9JzbAGrl",
"outputId": "f951f73e-e31f-49ea-fb83-91a37785b5d3"
},
"execution_count": 21,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'input_ids': tensor([[ 1, 518, 25580, 29962, 9314, 14816, 29903, 6778, 13, 30919,\n",
" 30392, 30287, 30548, 31773, 236, 172, 154, 235, 180, 147,\n",
" 31730, 30210, 235, 133, 164, 234, 168, 171, 30461, 31045,\n",
" 30748, 233, 161, 147, 31549, 13, 29966, 829, 14816, 29903,\n",
" 6778, 13, 13, 29900, 29900, 29900, 29900, 29900, 29896, 29871,\n",
" 30392, 231, 190, 131, 236, 189, 191, 235, 133, 164,\n",
" 234, 168, 171, 29973, 29961, 29914, 25580, 29962]],\n",
" device='cuda:0'),\n",
" 'attention_mask': tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
" 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
" 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]],\n",
" device='cuda:0')}"
]
},
"metadata": {},
"execution_count": 21
}
]
},
{
"cell_type": "code",
"source": [
"response = model.generate(\n",
" **inputs,\n",
" # use_cache=True,\n",
" streamer=streamer,\n",
" # ValueError: Input length of input_ids is 23, but `max_length` is set to 20. This can lead to unexpected behavior. You should consider increasing `max_length` or, better yet, setting `max_new_tokens`.\n",
" # Both `max_new_tokens` (=500) and `max_length`(=500) seem to have been set. `max_new_tokens` will take precedence. Please refer to the documentation for more information. (https://huggingface.co/docs/transformers/main/en/main_classes/text_generation)\n",
" # max_length=500,\n",
" max_new_tokens=500,\n",
" # /usr/local/lib/python3.10/dist-packages/transformers/generation/configuration_utils.py:492: UserWarning: `do_sample` is set to `False`. However, `temperature` is set to `0.3` -- this flag is only used in sample-based generation modes. You should set `do_sample=True` or unset `temperature`.\n",
" # /usr/local/lib/python3.10/dist-packages/transformers/generation/configuration_utils.py:497: UserWarning: `do_sample` is set to `False`. However, `top_p` is set to `0.85` -- this flag is only used in sample-based generation modes. You should set `do_sample=True` or unset `top_p`.\n",
" # /usr/local/lib/python3.10/dist-packages/transformers/generation/configuration_utils.py:509: UserWarning: `do_sample` is set to `False`. However, `top_k` is set to `30` -- this flag is only used in sample-based generation modes. You should set `do_sample=True` or unset `top_k`.\n",
" do_sample=True,\n",
" # Additional configures\n",
" top_k=30,\n",
" top_p=0.85,\n",
" temperature=0.3,\n",
" repetition_penalty=1.2,\n",
")\n",
"response"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "eG9r7O5t2qTF",
"outputId": "c5b11823-eef7-4129-8687-a0b492c03c84"
},
"execution_count": 22,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"<s> [INST]<<SYS>>\n",
"你是一名經驗豐富的股票市場分析師\n",
"<</SYS>>\n",
"\n",
"000001 是什麼股票?[/INST]</s>\n"
]
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"tensor([[ 1, 518, 25580, 29962, 9314, 14816, 29903, 6778, 13, 30919,\n",
" 30392, 30287, 30548, 31773, 236, 172, 154, 235, 180, 147,\n",
" 31730, 30210, 235, 133, 164, 234, 168, 171, 30461, 31045,\n",
" 30748, 233, 161, 147, 31549, 13, 29966, 829, 14816, 29903,\n",
" 6778, 13, 13, 29900, 29900, 29900, 29900, 29900, 29896, 29871,\n",
" 30392, 231, 190, 131, 236, 189, 191, 235, 133, 164,\n",
" 234, 168, 171, 29973, 29961, 29914, 25580, 29962, 2]],\n",
" device='cuda:0')"
]
},
"metadata": {},
"execution_count": 22
}
]
},
{
"cell_type": "code",
"source": [
"tokenizer.decode(response[0])"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 53
},
"id": "9nYa1kEjAXmv",
"outputId": "b60a9d9a-9e32-4cd2-fcba-e96dc27fe9a8"
},
"execution_count": 23,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'<s> [INST]<<SYS>>\\n你是一名經驗豐富的股票市場分析師\\n<</SYS>>\\n\\n000001 是什麼股票?[/INST]</s>'"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
}
},
"metadata": {},
"execution_count": 23
}
]
},
{
"cell_type": "code",
"source": [
"tokenizer.decode(response[0], skip_special_tokens=True)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 53
},
"id": "T3eIIFYHAZTe",
"outputId": "4418574a-df86-42ba-a1e0-fe4d863bf676"
},
"execution_count": 24,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'[INST]<<SYS>>\\n你是一名經驗豐富的股票市場分析師\\n<</SYS>>\\n\\n000001 是什麼股票?[/INST]'"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
}
},
"metadata": {},
"execution_count": 24
}
]
},
{
"cell_type": "code",
"source": [
"outputs = tokenizer.decode(response[0], skip_special_tokens=True)\n",
"outputs"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 53
},
"id": "mIYjaN9y24FT",
"outputId": "b13789f2-ac9b-4ac1-d5e2-18e1b02efd15"
},
"execution_count": 25,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'[INST]<<SYS>>\\n你是一名經驗豐富的股票市場分析師\\n<</SYS>>\\n\\n000001 是什麼股票?[/INST]'"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
}
},
"metadata": {},
"execution_count": 25
}
]
},
{
"cell_type": "code",
"source": [
"import re\n",
"output_cur = re.sub(r'.*\\[/INST\\]\\s*', '', outputs, flags=re.DOTALL)\n",
"output_cur"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"id": "ulYS7F7t_Tfw",
"outputId": "e71b2b83-5b84-46e4-8736-d47254c0714e"
},
"execution_count": 26,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"''"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
}
},
"metadata": {},
"execution_count": 26
}
]
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "PFI727a9_Vkf"
},
"execution_count": 26,
"outputs": []
},
{
"cell_type": "code",
"source": [
"response = model.generate(\n",
" **{key: value.to(model.device) for key, value in tokenizer(\"Please generate number from 0 to 9\", return_tensors='pt').items()},\n",
" # use_cache=True,\n",
" # streamer=streamer,\n",
" # ValueError: Input length of input_ids is 23, but `max_length` is set to 20. This can lead to unexpected behavior. You should consider increasing `max_length` or, better yet, setting `max_new_tokens`.\n",
" # Both `max_new_tokens` (=500) and `max_length`(=500) seem to have been set. `max_new_tokens` will take precedence. Please refer to the documentation for more information. (https://huggingface.co/docs/transformers/main/en/main_classes/text_generation)\n",
" # max_length=500,\n",
" max_new_tokens=999999,\n",
" # /usr/local/lib/python3.10/dist-packages/transformers/generation/configuration_utils.py:492: UserWarning: `do_sample` is set to `False`. However, `temperature` is set to `0.3` -- this flag is only used in sample-based generation modes. You should set `do_sample=True` or unset `temperature`.\n",
" # /usr/local/lib/python3.10/dist-packages/transformers/generation/configuration_utils.py:497: UserWarning: `do_sample` is set to `False`. However, `top_p` is set to `0.85` -- this flag is only used in sample-based generation modes. You should set `do_sample=True` or unset `top_p`.\n",
" # /usr/local/lib/python3.10/dist-packages/transformers/generation/configuration_utils.py:509: UserWarning: `do_sample` is set to `False`. However, `top_k` is set to `30` -- this flag is only used in sample-based generation modes. You should set `do_sample=True` or unset `top_k`.\n",
" do_sample=True,\n",
" # Additional configures\n",
" # top_k=30,\n",
" # top_p=0.85,\n",
" # temperature=0.3,\n",
" # repetition_penalty=1.2,\n",
")\n",
"tokenizer.decode(response[0])"
],
"metadata": {
"outputId": "8544a56d-0265-4f2f-8f40-cc40a626f9bc",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"id": "jELdweodBOh_"
},
"execution_count": 27,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'<s> Please generate number from 0 to 9</s>'"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
}
},
"metadata": {},
"execution_count": 27
}
]
},
{
"cell_type": "code",
"source": [
"# from transformers import pipeline\n",
"# # RuntimeError: Inferring the task automatically requires to check the hub with a model_id defined as a `str`.\n",
"# pipe = pipeline(model=model, return_full_text=False)\n",
"# print(pipe(\"Please generate number from 0 to 9\"))"
],
"metadata": {
"id": "9sFR6GvPBecj"
},
"execution_count": 28,
"outputs": []
},
{
"cell_type": "code",
"source": [
"greedy_short_outputs = model.generate(**inputs, max_new_tokens=3)\n",
"print(tokenizer.batch_decode(greedy_short_outputs))"
],
"metadata": {
"id": "ZBNoZO51DYWv",
"outputId": "93a7f979-d6bd-494d-f34d-67d042ba4b9d",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"execution_count": 29,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"['<s> [INST]<<SYS>>\\n你是一名經驗豐富的股票市場分析師\\n<</SYS>>\\n\\n000001 是什麼股票?[/INST]</s>']\n"
]
}
]
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "Qn3vktlZD2PS"
},
"execution_count": 29,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment