Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Beomi/f163a6c04a869d18ee1a025b6d33e6d8 to your computer and use it in GitHub Desktop.
Save Beomi/f163a6c04a869d18ee1a025b6d33e6d8 to your computer and use it in GitHub Desktop.
2023_05_26_bnb_4bit_koalpaca_v1_1a_on_polyglot_ko_12_8b.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"gpuType": "T4",
"machine_shape": "hm"
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
},
"accelerator": "GPU",
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"278070bfaaac4794a649f11306ad2947": {
"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_5391d6d2385549319099d3bf653c34ee",
"IPY_MODEL_ee696f00718b43989f653dd01391a87d",
"IPY_MODEL_e39e4f5284224bfdbde4359d8593d206"
],
"layout": "IPY_MODEL_0a91d94890634dc1bb43176347c27fec"
}
},
"5391d6d2385549319099d3bf653c34ee": {
"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_ec9d84f9337948cba5562b7e995bb2ef",
"placeholder": "​",
"style": "IPY_MODEL_3473e65596d5455385269f59e6159e38",
"value": "100%"
}
},
"ee696f00718b43989f653dd01391a87d": {
"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_416d78e5558f4019972720c8d52a410b",
"max": 1,
"min": 0,
"orientation": "horizontal",
"style": "IPY_MODEL_388757e992344b06b2fc7e4573eba77d",
"value": 1
}
},
"e39e4f5284224bfdbde4359d8593d206": {
"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_664f6a3720a446929197650580dd005d",
"placeholder": "​",
"style": "IPY_MODEL_92e0594143bd4c2690dc3ddaa0170b17",
"value": " 1/1 [00:00<00:00, 9.41it/s]"
}
},
"0a91d94890634dc1bb43176347c27fec": {
"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
}
},
"ec9d84f9337948cba5562b7e995bb2ef": {
"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
}
},
"3473e65596d5455385269f59e6159e38": {
"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": ""
}
},
"416d78e5558f4019972720c8d52a410b": {
"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
}
},
"388757e992344b06b2fc7e4573eba77d": {
"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": ""
}
},
"664f6a3720a446929197650580dd005d": {
"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
}
},
"92e0594143bd4c2690dc3ddaa0170b17": {
"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",
"source": [
"# `transformers` meets `bitsandbytes` for democratzing Large Language Models (LLMs) through 4bit quantization\n",
"\n",
"<center>\n",
"<img src=\"https://github.com/huggingface/blog/blob/main/assets/96_hf_bitsandbytes_integration/Thumbnail_blue.png?raw=true\" alt=\"drawing\" width=\"700\" class=\"center\"/>\n",
"</center>\n",
"\n",
"Welcome to this notebook that goes through the recent `bitsandbytes` integration that includes the work from XXX that introduces no performance degradation 4bit quantization techniques, for democratizing LLMs inference and training.\n",
"\n",
"In this notebook, we will learn together how to load a large model in 4bit (`gpt-neo-x-20b`) and train it using Google Colab and PEFT library from Hugging Face 🤗.\n",
"\n",
"[In the general usage notebook](https://colab.research.google.com/drive/1ge2F1QSK8Q7h0hn3YKuBCOAS0bK8E0wf?usp=sharing), you can learn how to propely load a model in 4bit with all its variants. \n",
"\n",
"If you liked the previous work for integrating [*LLM.int8*](https://arxiv.org/abs/2208.07339), you can have a look at the [introduction blogpost](https://huggingface.co/blog/hf-bitsandbytes-integration) to lean more about that quantization method.\n"
],
"metadata": {
"id": "XIyP_0r6zuVc"
}
},
{
"cell_type": "code",
"source": [
"!nvidia-smi"
],
"metadata": {
"id": "CHX-RCPdkXgP",
"outputId": "d294f833-6f45-4014-e4ed-75ef26141a0b",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Tue May 30 05:16:44 2023 \n",
"+-----------------------------------------------------------------------------+\n",
"| NVIDIA-SMI 525.85.12 Driver Version: 525.85.12 CUDA Version: 12.0 |\n",
"|-------------------------------+----------------------+----------------------+\n",
"| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |\n",
"| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |\n",
"| | | MIG M. |\n",
"|===============================+======================+======================|\n",
"| 0 Tesla T4 Off | 00000000:00:04.0 Off | 0 |\n",
"| N/A 48C P8 12W / 70W | 0MiB / 15360MiB | 0% Default |\n",
"| | | N/A |\n",
"+-------------------------------+----------------------+----------------------+\n",
" \n",
"+-----------------------------------------------------------------------------+\n",
"| Processes: |\n",
"| GPU GI CI PID Type Process name GPU Memory |\n",
"| ID ID Usage |\n",
"|=============================================================================|\n",
"| No running processes found |\n",
"+-----------------------------------------------------------------------------+\n"
]
}
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"id": "FuXIFTFapAMI",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "340f0ca7-6dfd-4350-b2d2-15f983adfc66"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
" Installing build dependencies ... \u001b[?25l\u001b[?25hdone\n",
" Getting requirements to build wheel ... \u001b[?25l\u001b[?25hdone\n",
" Preparing metadata (pyproject.toml) ... \u001b[?25l\u001b[?25hdone\n",
" Installing build dependencies ... \u001b[?25l\u001b[?25hdone\n",
" Getting requirements to build wheel ... \u001b[?25l\u001b[?25hdone\n",
" Preparing metadata (pyproject.toml) ... \u001b[?25l\u001b[?25hdone\n",
" Installing build dependencies ... \u001b[?25l\u001b[?25hdone\n",
" Getting requirements to build wheel ... \u001b[?25l\u001b[?25hdone\n",
" Preparing metadata (pyproject.toml) ... \u001b[?25l\u001b[?25hdone\n"
]
}
],
"source": [
"!pip install -q -U bitsandbytes\n",
"!pip install -q -U git+https://github.com/huggingface/transformers.git \n",
"!pip install -q -U git+https://github.com/huggingface/peft.git\n",
"!pip install -q -U git+https://github.com/huggingface/accelerate.git\n",
"!pip install -q datasets"
]
},
{
"cell_type": "markdown",
"source": [
"First let's load the model we are going to use - GPT-neo-x-20B! Note that the model itself is around 40GB in half precision"
],
"metadata": {
"id": "MJ-5idQwzvg-"
}
},
{
"cell_type": "code",
"source": [
"from datasets import load_dataset\n",
"\n",
"data = load_dataset(\"beomi/KoAlpaca-v1.1a\")"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 87,
"referenced_widgets": [
"278070bfaaac4794a649f11306ad2947",
"5391d6d2385549319099d3bf653c34ee",
"ee696f00718b43989f653dd01391a87d",
"e39e4f5284224bfdbde4359d8593d206",
"0a91d94890634dc1bb43176347c27fec",
"ec9d84f9337948cba5562b7e995bb2ef",
"3473e65596d5455385269f59e6159e38",
"416d78e5558f4019972720c8d52a410b",
"388757e992344b06b2fc7e4573eba77d",
"664f6a3720a446929197650580dd005d",
"92e0594143bd4c2690dc3ddaa0170b17"
]
},
"id": "jm4FzCvfeYcK",
"outputId": "bf40f8d2-7603-407d-fba5-6686eddd49aa"
},
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"WARNING:datasets.builder:Found cached dataset parquet (/root/.cache/huggingface/datasets/beomi___parquet/beomi--KoAlpaca-v1.1a-1465f66eb846fd61/0.0.0/2a3b91fbd88a2c90d1dbbb32b460cf621d31bd5b05b934492fdef7d8d6f236ec)\n"
]
},
{
"output_type": "display_data",
"data": {
"text/plain": [
" 0%| | 0/1 [00:00<?, ?it/s]"
],
"application/vnd.jupyter.widget-view+json": {
"version_major": 2,
"version_minor": 0,
"model_id": "278070bfaaac4794a649f11306ad2947"
}
},
"metadata": {}
}
]
},
{
"cell_type": "code",
"source": [
"data"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "2KUhV7x3e6Db",
"outputId": "1fdf62ba-5269-435c-dd11-29a381ba04c7"
},
"execution_count": 4,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"DatasetDict({\n",
" train: Dataset({\n",
" features: ['instruction', 'output', 'url'],\n",
" num_rows: 21155\n",
" })\n",
"})"
]
},
"metadata": {},
"execution_count": 4
}
]
},
{
"cell_type": "code",
"source": [
"# # data\n",
"# data = data.map(\n",
"# lambda x: \n",
"# {'text': f\"### 명령어: {x['instruction']}\\n\\n###맥락: {x['input']}\\n\\n### 답변: {x['output']}<|endoftext|>\" }\n",
"# if x['input'] else \n",
"# {'text':f\"### 명령어: {x['instruction']}\\n\\n### 답변: {x['output']}<|endoftext|>\"},\n",
"# )\n",
"# data\n",
"data = data.map(\n",
" lambda x: {'text': f\"### 질문: {x['instruction']}\\n\\n### 답변: {x['output']}<|endoftext|>\" }\n",
")"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "0FbgsI9sezTJ",
"outputId": "7df84efa-917f-4d13-e5ba-62fc1d0b3d25"
},
"execution_count": 5,
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"WARNING:datasets.arrow_dataset:Loading cached processed dataset at /root/.cache/huggingface/datasets/beomi___parquet/beomi--KoAlpaca-v1.1a-1465f66eb846fd61/0.0.0/2a3b91fbd88a2c90d1dbbb32b460cf621d31bd5b05b934492fdef7d8d6f236ec/cache-7e3fb2b447959d69.arrow\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"import torch\n",
"from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig\n",
"\n",
"model_id = \"EleutherAI/polyglot-ko-12.8b\"\n",
"bnb_config = BitsAndBytesConfig(\n",
" load_in_4bit=True,\n",
" bnb_4bit_use_double_quant=True,\n",
" bnb_4bit_quant_type=\"nf4\",\n",
" bnb_4bit_compute_dtype=torch.bfloat16\n",
")\n",
"\n",
"tokenizer = AutoTokenizer.from_pretrained(model_id)\n",
"model = AutoModelForCausalLM.from_pretrained(model_id, quantization_config=bnb_config, device_map={\"\":0})"
],
"metadata": {
"id": "E0Nl5mWL0k2T",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "e601d45a-99f5-46ff-aecf-497ed8754c24"
},
"execution_count": 6,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"\n",
"===================================BUG REPORT===================================\n",
"Welcome to bitsandbytes. For bug reports, please run\n",
"\n",
"python -m bitsandbytes\n",
"\n",
" and submit this information together with your error trace to: https://github.com/TimDettmers/bitsandbytes/issues\n",
"================================================================================\n",
"bin /usr/local/lib/python3.10/dist-packages/bitsandbytes/libbitsandbytes_cuda118.so\n",
"CUDA_SETUP: WARNING! libcudart.so not found in any environmental path. Searching in backup paths...\n",
"CUDA SETUP: CUDA runtime path found: /usr/local/cuda/lib64/libcudart.so\n",
"CUDA SETUP: Highest compute capability among GPUs detected: 7.5\n",
"CUDA SETUP: Detected CUDA version 118\n",
"CUDA SETUP: Loading binary /usr/local/lib/python3.10/dist-packages/bitsandbytes/libbitsandbytes_cuda118.so...\n"
]
},
{
"output_type": "stream",
"name": "stderr",
"text": [
"/usr/local/lib/python3.10/dist-packages/bitsandbytes/cuda_setup/main.py:149: UserWarning: /usr/lib64-nvidia did not contain ['libcudart.so', 'libcudart.so.11.0', 'libcudart.so.12.0'] as expected! Searching further paths...\n",
" warn(msg)\n",
"/usr/local/lib/python3.10/dist-packages/bitsandbytes/cuda_setup/main.py:149: UserWarning: WARNING: The following directories listed in your path were found to be non-existent: {PosixPath('/sys/fs/cgroup/memory.events /var/colab/cgroup/jupyter-children/memory.events')}\n",
" warn(msg)\n",
"/usr/local/lib/python3.10/dist-packages/bitsandbytes/cuda_setup/main.py:149: UserWarning: WARNING: The following directories listed in your path were found to be non-existent: {PosixPath('http'), PosixPath('//172.28.0.1'), PosixPath('8013')}\n",
" warn(msg)\n",
"/usr/local/lib/python3.10/dist-packages/bitsandbytes/cuda_setup/main.py:149: UserWarning: WARNING: The following directories listed in your path were found to be non-existent: {PosixPath('--logtostderr --listen_host=172.28.0.12 --target_host=172.28.0.12 --tunnel_background_save_url=https'), PosixPath('//colab.research.google.com/tun/m/cc48301118ce562b961b3c22d803539adc1e0c19/gpu-t4-vhm-wwmvfw9h3vrz --tunnel_background_save_delay=10s --tunnel_periodic_background_save_frequency=30m0s --enable_output_coalescing=true --output_coalescing_required=true')}\n",
" warn(msg)\n",
"/usr/local/lib/python3.10/dist-packages/bitsandbytes/cuda_setup/main.py:149: UserWarning: WARNING: The following directories listed in your path were found to be non-existent: {PosixPath('/env/python')}\n",
" warn(msg)\n",
"/usr/local/lib/python3.10/dist-packages/bitsandbytes/cuda_setup/main.py:149: UserWarning: WARNING: The following directories listed in your path were found to be non-existent: {PosixPath('module'), PosixPath('//ipykernel.pylab.backend_inline')}\n",
" warn(msg)\n",
"/usr/local/lib/python3.10/dist-packages/bitsandbytes/cuda_setup/main.py:149: UserWarning: Found duplicate ['libcudart.so', 'libcudart.so.11.0', 'libcudart.so.12.0'] files: {PosixPath('/usr/local/cuda/lib64/libcudart.so'), PosixPath('/usr/local/cuda/lib64/libcudart.so.11.0')}.. We'll flip a coin and try one of these, in order to fail forward.\n",
"Either way, this might cause trouble in the future:\n",
"If you get `CUDA error: invalid device function` errors, the above might be the cause and the solution is to make sure only one ['libcudart.so', 'libcudart.so.11.0', 'libcudart.so.12.0'] in the paths that we search based on your env.\n",
" warn(msg)\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"Then we have to apply some preprocessing to the model to prepare it for training. For that use the `prepare_model_for_kbit_training` method from PEFT."
],
"metadata": {
"id": "Mp2gMi1ZzGET"
}
},
{
"cell_type": "code",
"source": [
"data = data.map(lambda samples: tokenizer(samples[\"text\"]), batched=True)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "C4TDUgDbhyhK",
"outputId": "8aac9a8c-5211-4cb8-bd43-3b99b24c9c00"
},
"execution_count": 7,
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"WARNING:datasets.arrow_dataset:Loading cached processed dataset at /root/.cache/huggingface/datasets/beomi___parquet/beomi--KoAlpaca-v1.1a-1465f66eb846fd61/0.0.0/2a3b91fbd88a2c90d1dbbb32b460cf621d31bd5b05b934492fdef7d8d6f236ec/cache-75fcdbaed5b64b04.arrow\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"from peft import prepare_model_for_kbit_training\n",
"\n",
"model.gradient_checkpointing_enable()\n",
"model = prepare_model_for_kbit_training(model)"
],
"metadata": {
"id": "a9EUEDAl0ss3"
},
"execution_count": 8,
"outputs": []
},
{
"cell_type": "code",
"source": [
"def print_trainable_parameters(model):\n",
" \"\"\"\n",
" Prints the number of trainable parameters in the model.\n",
" \"\"\"\n",
" trainable_params = 0\n",
" all_param = 0\n",
" for _, param in model.named_parameters():\n",
" all_param += param.numel()\n",
" if param.requires_grad:\n",
" trainable_params += param.numel()\n",
" print(\n",
" f\"trainable params: {trainable_params} || all params: {all_param} || trainable%: {100 * trainable_params / all_param}\"\n",
" )"
],
"metadata": {
"id": "gkIcwsSU01EB"
},
"execution_count": 9,
"outputs": []
},
{
"cell_type": "code",
"source": [
"from peft import LoraConfig, get_peft_model\n",
"\n",
"config = LoraConfig(\n",
" r=8, \n",
" lora_alpha=32, \n",
" target_modules=[\"query_key_value\"], \n",
" lora_dropout=0.05, \n",
" bias=\"none\", \n",
" task_type=\"CAUSAL_LM\"\n",
")\n",
"\n",
"model = get_peft_model(model, config)\n",
"print_trainable_parameters(model)"
],
"metadata": {
"id": "Ybeyl20n3dYH",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "327a4a63-db3d-4631-edec-23f8d08d14ac"
},
"execution_count": 10,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"trainable params: 6553600 || all params: 6608701440 || trainable%: 0.09916622894073424\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"Let's load a common dataset, english quotes, to fine tune our model on famous quotes."
],
"metadata": {
"id": "FCc64bfnmd3j"
}
},
{
"cell_type": "code",
"source": [
"!nvidia-smi"
],
"metadata": {
"id": "s6f4z8EYmcJ6",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "21c8ba2d-fd89-40f3-f0b6-c73772f06bf2"
},
"execution_count": 11,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Tue May 30 05:21:32 2023 \n",
"+-----------------------------------------------------------------------------+\n",
"| NVIDIA-SMI 525.85.12 Driver Version: 525.85.12 CUDA Version: 12.0 |\n",
"|-------------------------------+----------------------+----------------------+\n",
"| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |\n",
"| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |\n",
"| | | MIG M. |\n",
"|===============================+======================+======================|\n",
"| 0 Tesla T4 Off | 00000000:00:04.0 Off | 0 |\n",
"| N/A 52C P0 30W / 70W | 9435MiB / 15360MiB | 0% Default |\n",
"| | | N/A |\n",
"+-------------------------------+----------------------+----------------------+\n",
" \n",
"+-----------------------------------------------------------------------------+\n",
"| Processes: |\n",
"| GPU GI CI PID Type Process name GPU Memory |\n",
"| ID ID Usage |\n",
"|=============================================================================|\n",
"+-----------------------------------------------------------------------------+\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"Run the cell below to run the training! For the sake of the demo, we just ran it for few steps just to showcase how to use this integration with existing tools on the HF ecosystem."
],
"metadata": {
"id": "_0MOtwf3zdZp"
}
},
{
"cell_type": "code",
"source": [
"import transformers\n",
"\n",
"# needed for gpt-neo-x tokenizer\n",
"tokenizer.pad_token = tokenizer.eos_token\n",
"\n",
"trainer = transformers.Trainer(\n",
" model=model,\n",
" train_dataset=data[\"train\"],\n",
" args=transformers.TrainingArguments(\n",
" per_device_train_batch_size=2,\n",
" gradient_accumulation_steps=1,\n",
" # warmup_steps=200,\n",
" max_steps=500, ## 초소형만 학습: 10 step = 20개 샘플만 학습.\n",
" learning_rate=2e-4,\n",
" fp16=True,\n",
" logging_steps=10,\n",
" output_dir=\"outputs\",\n",
" optim=\"paged_adamw_8bit\"\n",
" ),\n",
" data_collator=transformers.DataCollatorForLanguageModeling(tokenizer, mlm=False),\n",
")\n",
"model.config.use_cache = False # silence the warnings. Please re-enable for inference!\n",
"trainer.train()"
],
"metadata": {
"id": "jq0nX33BmfaC",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 1000
},
"outputId": "769fb34e-acd0-4a56-ac87-40db98dabd78"
},
"execution_count": 26,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.HTML object>"
],
"text/html": [
"\n",
" <div>\n",
" \n",
" <progress value='500' max='500' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
" [500/500 47:02, Epoch 0/1]\n",
" </div>\n",
" <table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: left;\">\n",
" <th>Step</th>\n",
" <th>Training Loss</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <td>10</td>\n",
" <td>2.192100</td>\n",
" </tr>\n",
" <tr>\n",
" <td>20</td>\n",
" <td>2.025800</td>\n",
" </tr>\n",
" <tr>\n",
" <td>30</td>\n",
" <td>1.971500</td>\n",
" </tr>\n",
" <tr>\n",
" <td>40</td>\n",
" <td>1.940200</td>\n",
" </tr>\n",
" <tr>\n",
" <td>50</td>\n",
" <td>1.895000</td>\n",
" </tr>\n",
" <tr>\n",
" <td>60</td>\n",
" <td>2.048800</td>\n",
" </tr>\n",
" <tr>\n",
" <td>70</td>\n",
" <td>1.951200</td>\n",
" </tr>\n",
" <tr>\n",
" <td>80</td>\n",
" <td>1.898600</td>\n",
" </tr>\n",
" <tr>\n",
" <td>90</td>\n",
" <td>1.919400</td>\n",
" </tr>\n",
" <tr>\n",
" <td>100</td>\n",
" <td>1.806600</td>\n",
" </tr>\n",
" <tr>\n",
" <td>110</td>\n",
" <td>2.092700</td>\n",
" </tr>\n",
" <tr>\n",
" <td>120</td>\n",
" <td>1.817300</td>\n",
" </tr>\n",
" <tr>\n",
" <td>130</td>\n",
" <td>1.725100</td>\n",
" </tr>\n",
" <tr>\n",
" <td>140</td>\n",
" <td>1.875600</td>\n",
" </tr>\n",
" <tr>\n",
" <td>150</td>\n",
" <td>2.151900</td>\n",
" </tr>\n",
" <tr>\n",
" <td>160</td>\n",
" <td>1.900700</td>\n",
" </tr>\n",
" <tr>\n",
" <td>170</td>\n",
" <td>1.901100</td>\n",
" </tr>\n",
" <tr>\n",
" <td>180</td>\n",
" <td>2.264800</td>\n",
" </tr>\n",
" <tr>\n",
" <td>190</td>\n",
" <td>1.938400</td>\n",
" </tr>\n",
" <tr>\n",
" <td>200</td>\n",
" <td>1.987100</td>\n",
" </tr>\n",
" <tr>\n",
" <td>210</td>\n",
" <td>2.048400</td>\n",
" </tr>\n",
" <tr>\n",
" <td>220</td>\n",
" <td>1.983300</td>\n",
" </tr>\n",
" <tr>\n",
" <td>230</td>\n",
" <td>1.997100</td>\n",
" </tr>\n",
" <tr>\n",
" <td>240</td>\n",
" <td>1.995700</td>\n",
" </tr>\n",
" <tr>\n",
" <td>250</td>\n",
" <td>1.995900</td>\n",
" </tr>\n",
" <tr>\n",
" <td>260</td>\n",
" <td>1.868700</td>\n",
" </tr>\n",
" <tr>\n",
" <td>270</td>\n",
" <td>1.915200</td>\n",
" </tr>\n",
" <tr>\n",
" <td>280</td>\n",
" <td>1.878300</td>\n",
" </tr>\n",
" <tr>\n",
" <td>290</td>\n",
" <td>1.877700</td>\n",
" </tr>\n",
" <tr>\n",
" <td>300</td>\n",
" <td>1.931400</td>\n",
" </tr>\n",
" <tr>\n",
" <td>310</td>\n",
" <td>1.807300</td>\n",
" </tr>\n",
" <tr>\n",
" <td>320</td>\n",
" <td>1.925000</td>\n",
" </tr>\n",
" <tr>\n",
" <td>330</td>\n",
" <td>1.933100</td>\n",
" </tr>\n",
" <tr>\n",
" <td>340</td>\n",
" <td>1.895300</td>\n",
" </tr>\n",
" <tr>\n",
" <td>350</td>\n",
" <td>1.895000</td>\n",
" </tr>\n",
" <tr>\n",
" <td>360</td>\n",
" <td>1.892500</td>\n",
" </tr>\n",
" <tr>\n",
" <td>370</td>\n",
" <td>1.908200</td>\n",
" </tr>\n",
" <tr>\n",
" <td>380</td>\n",
" <td>1.903600</td>\n",
" </tr>\n",
" <tr>\n",
" <td>390</td>\n",
" <td>1.768100</td>\n",
" </tr>\n",
" <tr>\n",
" <td>400</td>\n",
" <td>1.740700</td>\n",
" </tr>\n",
" <tr>\n",
" <td>410</td>\n",
" <td>1.843200</td>\n",
" </tr>\n",
" <tr>\n",
" <td>420</td>\n",
" <td>2.008600</td>\n",
" </tr>\n",
" <tr>\n",
" <td>430</td>\n",
" <td>1.993800</td>\n",
" </tr>\n",
" <tr>\n",
" <td>440</td>\n",
" <td>1.751400</td>\n",
" </tr>\n",
" <tr>\n",
" <td>450</td>\n",
" <td>1.783000</td>\n",
" </tr>\n",
" <tr>\n",
" <td>460</td>\n",
" <td>1.817700</td>\n",
" </tr>\n",
" <tr>\n",
" <td>470</td>\n",
" <td>1.870900</td>\n",
" </tr>\n",
" <tr>\n",
" <td>480</td>\n",
" <td>1.890400</td>\n",
" </tr>\n",
" <tr>\n",
" <td>490</td>\n",
" <td>1.891800</td>\n",
" </tr>\n",
" <tr>\n",
" <td>500</td>\n",
" <td>1.907500</td>\n",
" </tr>\n",
" </tbody>\n",
"</table><p>"
]
},
"metadata": {}
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"TrainOutput(global_step=500, training_loss=1.9244559707641602, metrics={'train_runtime': 2828.6026, 'train_samples_per_second': 0.354, 'train_steps_per_second': 0.177, 'total_flos': 1.103736813379584e+16, 'train_loss': 1.9244559707641602, 'epoch': 0.05})"
]
},
"metadata": {},
"execution_count": 26
}
]
},
{
"cell_type": "code",
"source": [
"print(\"wow\")"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "TvP11yUbhzWP",
"outputId": "96eb0cfe-e22f-4642-9fba-796942c62540"
},
"execution_count": 27,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"wow\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"model.eval()\n",
"model.config.use_cache = True # silence the warnings. Please re-enable for inference!"
],
"metadata": {
"id": "a-jauOEv9XVe"
},
"execution_count": 28,
"outputs": []
},
{
"cell_type": "code",
"source": [
"model.generate(**tokenizer(\"### 질문: 오늘 날씨는?\", return_tensors='pt', return_token_type_ids=False))"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "r4ITTiXfp-2r",
"outputId": "e6376ea8-f293-4ca4-f9f3-13f231f5de07"
},
"execution_count": 29,
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"Setting `pad_token_id` to `eos_token_id`:0 for open-end generation.\n",
"/usr/local/lib/python3.10/dist-packages/transformers/generation/utils.py:1349: UserWarning: Using `max_length`'s default (20) to control the generation length. This behaviour is deprecated and will be removed from the config in v5 of Transformers -- we recommend using `max_new_tokens` to control the maximum length of the generation.\n",
" warnings.warn(\n",
"/usr/local/lib/python3.10/dist-packages/transformers/generation/utils.py:1448: 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"
]
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"tensor([[ 6, 6, 6, 2438, 29, 1832, 4770, 272, 34, 224, 202, 4588,\n",
" 4770, 272, 1382, 5674, 34, 5000, 9357, 272]])"
]
},
"metadata": {},
"execution_count": 29
}
]
},
{
"cell_type": "code",
"source": [
"def gen(x):\n",
" gened = model.generate(\n",
" **tokenizer(\n",
" f\"### 질문: {x}\\n\\n### 답변:\", \n",
" return_tensors='pt', \n",
" return_token_type_ids=False\n",
" ), \n",
" max_new_tokens=256,\n",
" early_stopping=True,\n",
" do_sample=True,\n",
" eos_token_id=2,\n",
" )\n",
" print(tokenizer.decode(gened[0]))"
],
"metadata": {
"id": "oDp9W-Gmp5Mb"
},
"execution_count": 35,
"outputs": []
},
{
"cell_type": "code",
"source": [
"gen('건강하게 살기 위한 세 가지 방법은?')"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 128
},
"id": "iIbK1GaipZd9",
"outputId": "46537a15-3bce-4959-860a-80706a8b3693"
},
"execution_count": 31,
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"Setting `pad_token_id` to `eos_token_id`:2 for open-end generation.\n"
]
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'### 질문: 건강하게 살기 위한 세 가지 방법은?\\n\\n### 답변: 1. 균형있는 식사: 편식을 멀리하고, 채소, 과일, 생선을 섭취하면 건강해져요.\\n\\n2. 과식을 줄일 것: 과식하게 되면 위장이 활발하여져서 병이 나기 쉽게 되요.\\n3. 적당한 스트레스: 스트레스를 쌓아두는 것은 내 안에 독이 쌓이는 것이라서 오히려 건강에 좋지 않아요.\\n\\n즉, 균형있는 식사, 적당한 스트레스 해소, 과식을 줄이면 우리 몸에 좋습니다. 이 세 가지를 습관화하면서 건강한 삶을 유지한다면 건강하게 살 수 있게 될 것입니다. \\n\\n위의 세 가지 방법은 초등학생 때부터 배우던 것이므로, 쉽게 실행할 수 있습니다. 모두 건강하게 사는 지름길이므로, 이를 실천하여 건강한 삶을 살 수 있도록 노력합시다.\\n### 답변을 작성하는데, 약간의 시간이 걸렸습니다. 질문 내용에서도 알 수 있는 것처럼, 간단한 방법이지만 실행에 옮기기는 쉬운 일이 아니었습니다. 하지만, 노력하며 실행한다면 충분히 실천할 수 있기 때문에 최대한 지키도록 합시다. '"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
}
},
"metadata": {},
"execution_count": 31
}
]
},
{
"cell_type": "code",
"source": [
"gen('슈카월드가 무엇인가요?')"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 128
},
"id": "1YOtvI6rpuoP",
"outputId": "949faae7-4a6b-4074-b408-6259f696c6ac"
},
"execution_count": 32,
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"Setting `pad_token_id` to `eos_token_id`:2 for open-end generation.\n"
]
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'### 질문: 슈카월드가 무엇인가요?\\n\\n### 답변: 슈카월드는 유튜브 인기 방송인으로, 최정상 경제 유튜버로 평가받고 있습니다. 슈카와 함께하는 경제의 모든 것이라는 타이틀로 진행되는 이 채널에서는 주식, 경제에 대한 다양한 이야기가 올라오고 있습니다. 슈카와 함께하는 경제의 모든 것의 채널 링크는 다음과 같습니다.\\nhttps://www.youtube.com/channel/UCZ6ny7P7lEIHN5bMljHyKQw 슈카월드www.youtube.com 유튜브에서는 경제에 관심을 가지시는 것이 좋지 않을까 싶습니다. 따라서 금융 경제에 관심이 많으실 것 같은데, 슈카가 운영하는 최정상 경제 유튜브 슈카월드를 추천드립니다. 특히 슈카월드 영상이 업로드되는 월요일은 꼭 즐겨보시기 바랍니다. 월요일에 업로드되는 슈카월드 영상은 슈카월드의 대표 코너이며, 다양한 카테고리에 대해 이야기하고 있기 때문에 도움이 됩니다. \\n\\n그리고 슈카월드에는 여러 가지 분야의 전문가들도 출연하고 있습니다. \\n\\n유튜브에서 검색 결과'"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
}
},
"metadata": {},
"execution_count": 32
}
]
},
{
"cell_type": "code",
"source": [
"gen('공공성을 측정하기위한 방법은?')"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 128
},
"id": "oaNYJD81sL75",
"outputId": "bc79e6f9-6144-447c-dd3e-fead9203d64d"
},
"execution_count": 33,
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"Setting `pad_token_id` to `eos_token_id`:2 for open-end generation.\n"
]
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'### 질문: 공공성을 측정하기위한 방법은?\\n\\n### 답변: 공공성을 측정하기 위한 몇 가지 방법 중 하나는 공공성 척도를 구하는 것입니다. 측정 방법은 다음과 같습니다. \\n① 사회성과 경제적 효과 - 경제적 이익과 사회·문화적 이익을 모두 고려하는 종합성과 기준에 따라 파악.\\n\\n② 정책성과 : 공공성의 평가 척도는 공공정책의 효과나 영향으로 인해 발생하는 혜택과 공공 정책에 대한 가치판단이나 선호에 직접적으로 연계된 정책적 요구 사항을 기준으로 합니다. \\n\\n③ 참여성과 공동체 의식 : 공공성의 평가 척도는 정책과 관련한 시민들의 직접참여와 공동체 의식, 시민들이 느끼는 문제나 욕구, 시민의 문제해결 과정에서의 경험이나 느낌 등에 대한 공동체 의식입니다. \\n따라서 공공성이 높은 정책일수록 경제성과 사회·문화적 측면, 정책적 효과 등 세 가지 요소가 모두 높은 평가를 받기 때문에 공공성이 높은 정책을 세우면 정책의 효과를 극대화할 수 있습니다. 또한, 공공성과 관련한 정책을 만들 때는 시민의 참여를 기반으로 개발하고, 정책 형성 과정에서 시민들의 의견이 적극적으로 반영되어야 하는 것이'"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
}
},
"metadata": {},
"execution_count": 33
}
]
},
{
"cell_type": "code",
"source": [
"gen('주식 시장에서 안정적으로 수익을 얻기 위한 방법은?')"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "HOWSgQ6RuDK3",
"outputId": "924e3e5f-ea24-4e6c-bf49-9665e5eb804c"
},
"execution_count": 36,
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"Setting `pad_token_id` to `eos_token_id`:2 for open-end generation.\n",
"/usr/local/lib/python3.10/dist-packages/transformers/generation/utils.py:1448: 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"
]
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"### 질문: 주식 시장에서 안정적으로 수익을 얻기 위한 방법은?\n",
"\n",
"### 답변: 주식 시장에서 꾸준히 높은 수익을 얻기 위해서는 무엇보다도 종목 선택을 잘해야 합니다. 코스피 지수가 하락하면 수익률 관리가 필요하며, 이를 위해 투자 자산 배분 및 개별종목 선택이 중요합니다. 하지만 주식 시장에서 높은 수익을 얻기 위해서는 종목 선택이 중요하기 때문에, 본인의 투자 성향을 파악하여 신중한 투자를 해야합니다. 이를 위해서는 주식 투자에 대한 전문적인 분석과 기업 정보, 투자 전략 등을 수립해야 합니다. 이를 위해 개인이 직접 투자를 하기 보다는 전문가의 도움을 받아 투자하는 것이 좋습니다. \n",
"\n",
"만약, 전문적인 분석이 어려울 경우 인공지능 서비스를 활용하면 개인도 쉽게 성공적인 투자를 할 수 있으니, 참고하시기 바랍니다. 또한, 투자를 할 때는 시장을 정확히 판단할 수 없기 때문에, 투자의 위험성을 낮추기 위해서는 적립식 투자, 분산 투자, 분할 매수 기법 등을 통해 투자하는 것이 좋습니다. \n",
"�따라서, 주식 시장에서 높은 수익을 얻기 위해서는 투자 전략, 종목 선택, 위험 대비, 분산 투자 등의 전략이 필요합니다. 이를 위해 전문가의 조언을 받아 투자\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"gen('풋옵션과 콜옵션의 차이, 그리고 일반 개미 투자자가 선택해야 할 포지션은?')"
],
"metadata": {
"id": "fPOyetf75sCv",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "b5e98b13-e124-494c-b254-7cdb31d56571"
},
"execution_count": 37,
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"Setting `pad_token_id` to `eos_token_id`:2 for open-end generation.\n"
]
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"### 질문: 풋옵션과 콜옵션의 차이, 그리고 일반 개미 투자자가 선택해야 할 포지션은?\n",
"\n",
"### 답변: 풋이란 주식을 사는 것(구매)을 의미하고, 콜이란 주식을 파는(판매) 것입니다. 예를 들면, 기업의 주가가 10만원인데, 그 회사에서 신규 사업을 진행 중이라, 앞으로 매출이 증대되어 기업 가치가 상승할 것으로 판단해, 해당 회사의 주식을 사는 것이 풋옵션입니다. 이는 주식을 사는 시점과 해당 회사의 주가가 하락할 경우에는 기업의 주가와 함께 주가가 하락하는 리스크가 있습니다. 반면, 콜옵션은 주식을 파는 것입니다. 이는 주식을 파는 시점과 해당 회사의 주가가 증가해서 향후 기업 전망이 밝을 경우 주가가 상승하며, 해당 주식의 프리미엄을 받을 수 있는 안정적인 투자법입니다. 일반 개미 투자자들은 주식을 매수할 때와 주식을 매도할 때 다양한 리스크가 존재하므로, 전문가의 조언과 투자 경험을 바탕으로 적절한 투자 전략을 수립해야 합니다. 투자에 대한 자세한 사항은 아래에서 무료로 얻을 수 있습니다.\n",
"\n",
"콜옵션 거래: 네이버 검색창 '콜옵션 거래' 검색 (https://stock.naver.\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"gen('풋옵션 매도와 콜옵션 매수의 차이, 그리고 일반 개미 투자자가 선택해야 할 포지션은?')"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "dr9pvznHBgIZ",
"outputId": "c46b40a4-0dff-4402-eb14-6cc8459e7e93"
},
"execution_count": 38,
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"Setting `pad_token_id` to `eos_token_id`:2 for open-end generation.\n"
]
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"### 질문: 풋옵션 매도와 콜옵션 매수의 차이, 그리고 일반 개미 투자자가 선택해야 할 포지션은?\n",
"\n",
"### 답변: 풋오버(풋을 매도하고 그보다 상대적으로 가격이 낮은 콜을 사는 것)를 한다고 해서 더 높은 가격의 콜을 산다는 의미는 아닙니다. 더 낮은 가격의 콜을 사는 것이 기본입니다. 그러나 풋을 사는 것은 만기까지 보유하지 않고 풋 매도 포지션은 팔 수 있기 때문에 단기적으로 낮은 가격의 콜을 매수하는 것보다 높은 가격의 콜을 매수할 수 있는 선택의 범위가 있다고 할 수 있습니다. 이에 비해 콜오버(콜을 매도하고 그보다 상대적으로 가격이 낮은 풋을 사는 것)를 하면 만기까지 보유해야 하기 때문에 단기적으로 높은 가격의 풋을 사거나 콜을 살 수는 없지만, 만기까지 주가가 상승한다면 풋오버는 콜오버에 비해 높은 수익을 낼 가능성이 높습니다. 또한 콜과 풋의 가격을 비교해 보면 높은 가격의 쪽이 높은 가격에 비해 수익성이 더 높으므로 콜을 선택해야 합니다. 즉, 선택의 폭은 더 높은 가격의 콜을 살 것인지, 아니면 더 낮은 가격의 콜을 살 것인지에 달려 있습니다. 하지만 여기서도 명심해야\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"gen(\"마진콜이 발생하는 이유가 뭐야? 그리고 어떻게 해야 마진콜을 막을 수 있어?\")"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "VYrc_CZuCrR6",
"outputId": "8d21485b-df66-4b83-a782-9b91f5eb392c"
},
"execution_count": 40,
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"Setting `pad_token_id` to `eos_token_id`:2 for open-end generation.\n"
]
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"### 질문: 마진콜이 발생하는 이유가 뭐야? 그리고 어떻게 해야 마진콜을 막을 수 있어?\n",
"\n",
"### 답변: 마진콜이란, 선물/옵션 매매 시 증거금이 부족해서 추가증거금 예치를 요구받는 것을 말합니다. 추가증거금은 선물/옵션 거래 계약시 미리 정해진 약정증거금에서 추가로 부담해야 하는 증거금을 의미합니다. 통상적으로 매수 계약 시에는 계약 금액의 10%를 증거금으로 예치하고, 매도 계약 시에는 해당 금액의 3%를 예치합니다. 따라서, 매수 계약 시에는 총 20%의 증거금이 필요하고, 매도 계약 시에는 총 9%의 증거금이 필요합니다. 따라서, 선물 매매 중에는 매일 매일의 시세에 따라 증거금을 수시로 필요하면 추가로 납부해야해서 추가증거금이 발생하며, 해당 필요액 부족시에는 마진콜이 발생합니다. 따라서, 선물 거래를 하기 전에는 꼭 추가증거금을 예치하는 것이 중요합니다. 추가증거금은 '선물/옵션 -> 증거금 -> 추가예치/청산' 방법으로 조회 가능하며, 인터넷이나 전화로도 조회할 수 있습니다. 또, '주문/증거금 -> 추가증거금'을 통해 추가증거금을 입금할 수도 있습니다. 추가증거금을 예치\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"# ### 질문: 마진콜이 발생하는 이유가 뭐야? 그리고 어떻게 해야 마진콜을 막을 수 있어?\n",
"\n",
"# ### 답변: 마진콜이란, 선물/옵션 매매 시 증거금이 부족해서 추가증거금 예치를 요구받는 것을 말합니다. \n",
"# 추가증거금은 선물/옵션 거래 계약시 미리 정해진 약정증거금에서 추가로 부담해야 하는 증거금을 의미합니다. \n",
"# 통상적으로 매수 계약 시에는 계약 금액의 10%를 증거금으로 예치하고, 매도 계약 시에는 해당 금액의 3%를 예치합니다. \n",
"# 따라서, 매수 계약 시에는 총 20%의 증거금이 필요하고, 매도 계약 시에는 총 9%의 증거금이 필요합니다. \n",
"# 따라서, 선물 매매 중에는 매일 매일의 시세에 따라 증거금을 수시로 필요하면 추가로 납부해야해서 추가증거금이 발생하며, 해당 필요액 부족시에는 마진콜이 발생합니다. \n",
"# 따라서, 선물 거래를 하기 전에는 꼭 추가증거금을 예치하는 것이 중요합니다. \n",
"# 추가증거금은 '선물/옵션 -> 증거금 -> 추가예치/청산' 방법으로 조회 가능하며, 인터넷이나 전화로도 조회할 수 있습니다. \n",
"# 또, '주문/증거금 -> 추가증거금'을 통해 추가증거금을 입금할 수도 있습니다. 추가증거금을 예치"
],
"metadata": {
"id": "G-5etmH-fMKj"
},
"execution_count": null,
"outputs": []
}
]
}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment