diff --git "a/training.ipynb" "b/training.ipynb" --- "a/training.ipynb" +++ "b/training.ipynb" @@ -1,605 +1,1530 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "id": "7ef3e090-1986-4080-827e-fdef2deda5ba", - "metadata": {}, - "outputs": [], - "source": [ - "import json\n", - "from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline\n", - "import torch\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "ee142e5a-92ac-400b-a048-89a3df0060f6", - "metadata": {}, - "outputs": [], - "source": [ - "device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n", - "print(f\"Device set to: {device}\")\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "ba2eea5c-108e-4305-a64e-c35800cf9bf2", - "metadata": {}, - "outputs": [], - "source": [ - "# Load CLI Q&A dataset\n", - "with open(\"cli_questions.json\", \"r\", encoding=\"utf-8\") as f:\n", - " data = json.load(f)\n", - "\n", - "# Access the list of entries inside \"data\" key\n", - "qa_list = data[\"data\"]\n", - "\n", - "# Show a sample\n", - "print(f\"Total entries: {len(qa_list)}\")\n", - "print(\"Sample entry:\", qa_list[0])\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "81490ae9-b6f9-4004-b098-c09677c1dcd3", - "metadata": {}, - "outputs": [], - "source": [ - "model_id = \"TinyLlama/TinyLlama-1.1B-Chat-v1.0\"\n", - "\n", - "tokenizer = AutoTokenizer.from_pretrained(model_id)\n", - "model = AutoModelForCausalLM.from_pretrained(model_id)\n", - "model.to(device)\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "5eb00a02-a5a5-4746-bc1f-685ce4865600", - "metadata": {}, - "outputs": [], - "source": [ - "generator = pipeline(\"text-generation\", model=model, tokenizer=tokenizer, device=-1) # -1 for CPU\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "0f2b0688-a24d-4d86-90e5-9b8237620f6c", - "metadata": {}, - "outputs": [], - "source": [ - "# Pick sample questions\n", - "sample_questions = [entry[\"question\"] for entry in qa_list[:5]]\n", - "\n", - "# Generate and print answers\n", - "for i, question in enumerate(sample_questions):\n", - " print(f\"Q{i+1}: {question}\")\n", - " output = generator(question, max_new_tokens=150, do_sample=True, temperature=0.7)\n", - " print(f\"A{i+1}: {output[0]['generated_text']}\\n{'-'*60}\")\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "0f52ebb0-e2b9-4971-b66c-5353257b7a1c", - "metadata": {}, - "outputs": [], - "source": [ - "prompt = f\"Q: {question}\\nA:\"\n", - "output = generator(prompt, max_new_tokens=100, do_sample=True, temperature=0.7)\n", - "print(output[0][\"generated_text\"])\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "49fcf984-bd0d-48b7-857a-e6a6e04585b8", - "metadata": {}, - "outputs": [], - "source": [ - "import json\n", - "\n", - "# Load the dataset\n", - "with open(\"cli_questions.json\", \"r\") as f:\n", - " raw = json.load(f)\n", - " data = raw[\"data\"] # ensure this matches your JSON structure\n", - "\n", - "# Generate answers\n", - "results = []\n", - "for i, item in enumerate(data[:50]): # run on subset first\n", - " question = item[\"question\"]\n", - " prompt = f\"Q: {question}\\nA:\"\n", - " output = generator(prompt, max_new_tokens=150, temperature=0.7, do_sample=True)\n", - " answer = output[0][\"generated_text\"].split(\"A:\")[1].strip() if \"A:\" in output[0][\"generated_text\"] else output[0][\"generated_text\"]\n", - " results.append({\"question\": question, \"answer\": answer})\n", - " print(f\"Q{i+1}: {question}\\nA{i+1}: {answer}\\n{'-'*60}\")\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "819b988d-c6a1-4b11-b09d-1f1892e18158", - "metadata": {}, - "outputs": [], - "source": [ - "!pip install transformers datasets peft accelerate bitsandbytes trl --quiet\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "6b3c1312-3499-4462-b435-9fe72f0d6f06", - "metadata": { - "scrolled": true - }, - "outputs": [], - "source": [ - "print(\"Top-level keys:\", data.keys() if isinstance(data, dict) else \"Not a dict\")\n", - "print(\"Preview:\", str(data)[:500]) # Print first 500 chars of the content\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "96748b74-a5c7-439e-8428-680cba84e06d", - "metadata": {}, - "outputs": [], - "source": [ - "import json\n", - "from datasets import Dataset\n", - "\n", - "# Load and extract Q&A list\n", - "with open(\"cli_questions.json\", \"r\") as f:\n", - " raw = json.load(f)\n", - " data_list = raw[\"data\"] # ✅ correct key now\n", - "\n", - "# Convert to prompt/response format\n", - "for sample in data_list:\n", - " sample[\"prompt\"] = sample[\"question\"]\n", - " sample[\"response\"] = sample[\"answer\"]\n", - "\n", - "# Create HuggingFace Dataset\n", - "dataset = Dataset.from_list(data_list)\n", - "dataset = dataset.train_test_split(test_size=0.1)\n", - "\n", - "print(\"Loaded dataset:\", dataset)\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "7a7560e5-b04f-480c-b989-0bb3d3611701", - "metadata": {}, - "outputs": [], - "source": [ - "from transformers import AutoTokenizer, AutoModelForCausalLM\n", - "\n", - "model_name = \"TinyLlama/TinyLlama-1.1B-Chat-v1.0\" # or try \"microsoft/phi-2\"\n", - "\n", - "tokenizer = AutoTokenizer.from_pretrained(model_name)\n", - "model = AutoModelForCausalLM.from_pretrained(\n", - " model_name,\n", - " device_map=\"auto\",\n", - " load_in_4bit=True # For LoRA on low-resource\n", - ")\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "ae23057e-b741-4541-946d-77f9c5b8c9dc", - "metadata": {}, - "outputs": [], - "source": [ - "from transformers import AutoTokenizer, AutoModelForCausalLM\n", - "\n", - "model_name = \"TinyLlama/TinyLlama-1.1B-Chat-v1.0\"\n", - "\n", - "tokenizer = AutoTokenizer.from_pretrained(model_name)\n", - "model = AutoModelForCausalLM.from_pretrained(\n", - " model_name,\n", - " torch_dtype=\"auto\", # or torch.float32 if you get another dtype error\n", - " device_map=\"cpu\" # force CPU since no supported GPU found\n", - ")\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "ac99fe95-b5f3-4591-bc7c-793e195eeb86", - "metadata": {}, - "outputs": [], - "source": [ - "from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig\n", - "\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.float16,\n", - ")\n", - "\n", - "tokenizer = AutoTokenizer.from_pretrained(model_name)\n", - "model = AutoModelForCausalLM.from_pretrained(\n", - " model_name,\n", - " device_map=\"auto\",\n", - " quantization_config=bnb_config\n", - ")\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "7bde0e33-3bed-4940-907f-e0c2e7af1cd3", - "metadata": {}, - "outputs": [], - "source": [ - "import torch\n", - "from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig\n", - "\n", - "model_name = \"TinyLlama/TinyLlama-1.1B-Chat-v1.0\"\n", - "\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.float16,\n", - ")\n", - "\n", - "tokenizer = AutoTokenizer.from_pretrained(model_name)\n", - "model = AutoModelForCausalLM.from_pretrained(\n", - " model_name,\n", - " device_map=\"auto\",\n", - " quantization_config=bnb_config\n", - ")\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "51e0d14a-18c7-410f-9821-0eb00d3d1bbc", - "metadata": {}, - "outputs": [], - "source": [ - "from transformers import AutoTokenizer, AutoModelForCausalLM\n", - "\n", - "model_name = \"TinyLlama/TinyLlama-1.1B-Chat-v1.0\"\n", - "\n", - "tokenizer = AutoTokenizer.from_pretrained(model_name)\n", - "model = AutoModelForCausalLM.from_pretrained(\n", - " model_name,\n", - " device_map=\"auto\", # This will still use CPU if no GPU is found\n", - ")\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "f4e4786e-e67c-4c0f-b169-6996a2966558", - "metadata": {}, - "outputs": [], - "source": [ - "model = AutoModelForCausalLM.from_pretrained(\n", - " model_name,\n", - " device_map=\"auto\",\n", - " torch_dtype=torch.float32 # or float16 if your CPU supports it\n", - ")\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "dfd328ef-9362-426b-894e-923e70c7ace3", - "metadata": {}, - "outputs": [], - "source": [ - "device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n", - "print(f\"Device set to: {device}\")\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "6743ec8e-8bd9-4a73-8786-fd71a6790d78", - "metadata": {}, - "outputs": [], - "source": [ - "import json\n", - "import torch\n", - "from datasets import Dataset\n", - "from transformers import AutoTokenizer, AutoModelForCausalLM, TrainingArguments, Trainer, DataCollatorForLanguageModeling\n", - "from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "4252cc0c-62fe-4871-8095-ab07959b7884", - "metadata": {}, - "outputs": [], - "source": [ - "import json\n", - "import torch\n", - "from datasets import Dataset\n", - "from transformers import AutoTokenizer, AutoModelForCausalLM, TrainingArguments, Trainer, DataCollatorForLanguageModeling\n", - "from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "7153b443-8059-42d1-96fa-699d0f19f9cf", - "metadata": {}, - "outputs": [], - "source": [ - "import json\n", - "\n", - "with open(\"cli_questions.json\") as f:\n", - " data = json.load(f)\n", - "\n", - "# Check the top-level structure\n", - "print(type(data)) # Should print \n", - "print(data.keys()) # See what keys are at the top\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "fbfa8025-233e-47c5-9044-146f95bb24eb", - "metadata": {}, - "outputs": [], - "source": [ - "import json\n", - "from datasets import Dataset\n", - "\n", - "# Load the JSON and extract the list\n", - "with open(\"cli_questions.json\") as f:\n", - " raw = json.load(f)\n", - "\n", - "qa_list = raw[\"data\"] # access the list inside the 'data' key\n", - "\n", - "# Format for instruction tuning\n", - "formatted_data = [\n", - " {\"text\": f\"### Question:\\n{item['question']}\\n\\n### Answer:\\n{item['answer']}\"}\n", - " for item in qa_list\n", - "]\n", - "\n", - "# Convert to Hugging Face dataset\n", - "dataset = Dataset.from_list(formatted_data)\n", - "\n", - "# Preview\n", - "print(f\"Loaded {len(dataset)} formatted examples\")\n", - "print(dataset[0])\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "893c412e-0f09-44fd-b6f8-fe3557a071aa", - "metadata": {}, - "outputs": [], - "source": [ - "from transformers import AutoTokenizer\n", - "\n", - "model_id = \"TinyLlama/TinyLlama-1.1B-Chat-v1.0\" # You can switch to Phi-2 if you prefer\n", - "\n", - "tokenizer = AutoTokenizer.from_pretrained(model_id)\n", - "tokenizer.pad_token = tokenizer.eos_token # Needed for causal LM padding\n", - "\n", - "# Tokenization function\n", - "def tokenize(example):\n", - " return tokenizer(example[\"text\"], padding=\"max_length\", truncation=True, max_length=512)\n", - "\n", - "tokenized_dataset = dataset.map(tokenize, batched=True)\n", - "tokenized_dataset = tokenized_dataset.remove_columns([\"text\"])\n", - "\n", - "tokenized_dataset.set_format(type=\"torch\")\n", - "print(tokenized_dataset[0])\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "fb49d005-c57c-422f-8bc5-b4037a6bb40f", - "metadata": {}, - "outputs": [], - "source": [ - "train_dataset = tokenized_dataset\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "a3fb419b-703f-43c8-9be0-a71815b3da82", - "metadata": {}, - "outputs": [], - "source": [ - "# Use entire dataset as training set\n", - "train_dataset = tokenized_dataset\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "09c26c73-e7e8-4610-97d6-6c4a10004785", - "metadata": {}, - "outputs": [], - "source": [ - "tokenized_dataset.save_to_disk(\"tokenized_dataset\")\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "e66f130b-b80b-42fd-9f79-60f245f2c114", - "metadata": {}, - "outputs": [], - "source": [ - "from datasets import load_from_disk\n", - "\n", - "# Load the saved dataset\n", - "tokenized_dataset = load_from_disk(\"tokenized_dataset\")\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "2dbe3f16-4d82-40c8-be84-b1f85910620f", - "metadata": {}, - "outputs": [], - "source": [ - "train_dataset = tokenized_dataset # Use full set for training since it's only 172 examples\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "7f05e8d5-fcdf-4a11-9c51-7e8ecd255848", - "metadata": {}, - "outputs": [], - "source": [ - "from transformers import DataCollatorForLanguageModeling\n", - "\n", - "data_collator = DataCollatorForLanguageModeling(\n", - " tokenizer=tokenizer,\n", - " mlm=False\n", - ")\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "ec68cba4-8413-4c7d-91de-1fe798dc39fc", - "metadata": {}, - "outputs": [], - "source": [ - "from transformers import AutoModelForCausalLM, AutoTokenizer, TrainingArguments, Trainer, DataCollatorForLanguageModeling\n", - "from peft import get_peft_model, LoraConfig, prepare_model_for_kbit_training\n", - "from datasets import load_from_disk\n", - "import torch\n", - "\n", - "# Load model and tokenizer (TinyLlama)\n", - "model_name = \"TinyLlama/TinyLlama-1.1B-Chat-v1.0\"\n", - "tokenizer = AutoTokenizer.from_pretrained(model_name)\n", - "tokenizer.pad_token = tokenizer.eos_token # Important for Trainer padding\n", - "\n", - "model = AutoModelForCausalLM.from_pretrained(model_name)\n", - "\n", - "# Setup LoRA config\n", - "lora_config = LoraConfig(\n", - " r=8,\n", - " lora_alpha=16,\n", - " target_modules=[\"q_proj\", \"v_proj\"],\n", - " lora_dropout=0.1,\n", - " bias=\"none\",\n", - " task_type=\"CAUSAL_LM\"\n", - ")\n", - "\n", - "# Inject LoRA adapters\n", - "model = get_peft_model(model, lora_config)\n", - "\n", - "# Load the tokenized dataset\n", - "dataset = load_from_disk(\"tokenized_dataset\")\n", - "\n", - "# Setup data collator\n", - "data_collator = DataCollatorForLanguageModeling(\n", - " tokenizer=tokenizer,\n", - " mlm=False\n", - ")\n", - "\n", - "# Training args\n", - "training_args = TrainingArguments(\n", - " output_dir=\"./lora-tinyllama-output\",\n", - " per_device_train_batch_size=2, # Small batch size for CPU\n", - " gradient_accumulation_steps=4,\n", - " num_train_epochs=1, # Reduce for quicker runs\n", - " logging_steps=10,\n", - " save_strategy=\"epoch\",\n", - " learning_rate=2e-4,\n", - " fp16=False, # Don't use fp16 on CPU\n", - " report_to=\"none\"\n", - ")\n", - "\n", - "# Define Trainer\n", - "trainer = Trainer(\n", - " model=model,\n", - " args=training_args,\n", - " train_dataset=dataset,\n", - " tokenizer=tokenizer,\n", - " data_collator=data_collator\n", - ")\n", - "\n", - "# Start training\n", - "trainer.train()\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "2eaf9fa5-540c-4bd2-b6e1-9ea60c820004", - "metadata": {}, - "outputs": [], - "source": [ - "pip install -r requirements.txt\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "fad00764-e047-4fd0-b703-c9bbd343ce46", - "metadata": {}, - "outputs": [], - "source": [ - "login(token=\"REMOVED_TOKEN_...\")\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "075e175f-d164-420a-92fb-75150637d351", - "metadata": {}, - "outputs": [], - "source": [ - "from huggingface_hub import login\n", - "import os\n", - "\n", - "# Safer login using environment variable (no token exposed in notebook)\n", - "login(token=os.getenv(\"HF_TOKEN\"))\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "def2deab-147c-4445-8e62-96c397d72f12", - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.12.7" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "7ef3e090-1986-4080-827e-fdef2deda5ba", + "metadata": {}, + "outputs": [], + "source": [ + "import json\n", + "from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline\n", + "import torch\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "ee142e5a-92ac-400b-a048-89a3df0060f6", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Device set to: cpu\n" + ] + } + ], + "source": [ + "device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n", + "print(f\"Device set to: {device}\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "ba2eea5c-108e-4305-a64e-c35800cf9bf2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total entries: 172\n", + "Sample entry: {'question': 'What is the intended use-case for git stash?', 'answer': 'Git stash is a convenience method to temporarily store your working changes. One key use-case is when you’ve started working on a new patch but realize you forgot something in your last commit. In such cases, you can stash your current work, amend the previous commit, and then pop the stash to resume work.\\n\\nExample:\\n```\\n# Stash current changes\\ngit stash save\\n\\n# Fix and amend the previous commit\\ngit add -u\\ngit commit --amend\\n\\n# Restore your stashed changes\\ngit stash pop\\n```\\n\\nWhile creating temporary branches is also a valid approach, stash is often faster for quick save-and-resume workflows.', 'tags': ['git', 'git-stash']}\n" + ] + } + ], + "source": [ + "# Load CLI Q&A dataset\n", + "with open(\"cli_questions.json\", \"r\", encoding=\"utf-8\") as f:\n", + " data = json.load(f)\n", + "\n", + "# Access the list of entries inside \"data\" key\n", + "qa_list = data[\"data\"]\n", + "\n", + "# Show a sample\n", + "print(f\"Total entries: {len(qa_list)}\")\n", + "print(\"Sample entry:\", qa_list[0])\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "81490ae9-b6f9-4004-b098-c09677c1dcd3", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "LlamaForCausalLM(\n", + " (model): LlamaModel(\n", + " (embed_tokens): Embedding(32000, 2048)\n", + " (layers): ModuleList(\n", + " (0-21): 22 x LlamaDecoderLayer(\n", + " (self_attn): LlamaAttention(\n", + " (q_proj): Linear(in_features=2048, out_features=2048, bias=False)\n", + " (k_proj): Linear(in_features=2048, out_features=256, bias=False)\n", + " (v_proj): Linear(in_features=2048, out_features=256, bias=False)\n", + " (o_proj): Linear(in_features=2048, out_features=2048, bias=False)\n", + " )\n", + " (mlp): LlamaMLP(\n", + " (gate_proj): Linear(in_features=2048, out_features=5632, bias=False)\n", + " (up_proj): Linear(in_features=2048, out_features=5632, bias=False)\n", + " (down_proj): Linear(in_features=5632, out_features=2048, bias=False)\n", + " (act_fn): SiLU()\n", + " )\n", + " (input_layernorm): LlamaRMSNorm((2048,), eps=1e-05)\n", + " (post_attention_layernorm): LlamaRMSNorm((2048,), eps=1e-05)\n", + " )\n", + " )\n", + " (norm): LlamaRMSNorm((2048,), eps=1e-05)\n", + " (rotary_emb): LlamaRotaryEmbedding()\n", + " )\n", + " (lm_head): Linear(in_features=2048, out_features=32000, bias=False)\n", + ")" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "model_id = \"TinyLlama/TinyLlama-1.1B-Chat-v1.0\"\n", + "\n", + "tokenizer = AutoTokenizer.from_pretrained(model_id)\n", + "model = AutoModelForCausalLM.from_pretrained(model_id)\n", + "model.to(device)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "5eb00a02-a5a5-4746-bc1f-685ce4865600", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Device set to use cpu\n" + ] + } + ], + "source": [ + "generator = pipeline(\"text-generation\", model=model, tokenizer=tokenizer, device=-1) # -1 for CPU\n" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "0f2b0688-a24d-4d86-90e5-9b8237620f6c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Q1: What is the intended use-case for git stash?\n", + "A1: What is the intended use-case for git stash?\n", + "------------------------------------------------------------\n", + "Q2: What is the difference between 'git pull' and 'git fetch'?\n", + "A2: What is the difference between 'git pull' and 'git fetch'?\n", + "------------------------------------------------------------\n", + "Q3: How do I undo the most recent local commits in Git?\n", + "A3: How do I undo the most recent local commits in Git?\n", + "------------------------------------------------------------\n", + "Q4: How do I delete a Git branch locally and remotely?\n", + "A4: How do I delete a Git branch locally and remotely?\n", + "------------------------------------------------------------\n", + "Q5: What is the intended use-case for git stash?\n", + "A5: What is the intended use-case for git stash?\n", + "------------------------------------------------------------\n" + ] + } + ], + "source": [ + "# Pick sample questions\n", + "sample_questions = [entry[\"question\"] for entry in qa_list[:5]]\n", + "\n", + "# Generate and print answers\n", + "for i, question in enumerate(sample_questions):\n", + " print(f\"Q{i+1}: {question}\")\n", + " output = generator(question, max_new_tokens=150, do_sample=True, temperature=0.7)\n", + " print(f\"A{i+1}: {output[0]['generated_text']}\\n{'-'*60}\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "0f52ebb0-e2b9-4971-b66c-5353257b7a1c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Q: What is the intended use-case for git stash?\n", + "A: Git stash is a powerful tool that lets you store changes in one place and then later come back and finish that work. Git stashes changes to a temporary branch, which can be used to work on a task without committing the changes yet.\n", + "Git stash is used for tasks like modifying code, fixing bugs, and fixing merge conflicts during development. It is a powerful tool that can help you get work done faster and avoid the risk of losing changes.\n", + "Q: How do I\n" + ] + } + ], + "source": [ + "prompt = f\"Q: {question}\\nA:\"\n", + "output = generator(prompt, max_new_tokens=100, do_sample=True, temperature=0.7)\n", + "print(output[0][\"generated_text\"])\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "49fcf984-bd0d-48b7-857a-e6a6e04585b8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Q1: What is the intended use-case for git stash?\n", + "A1: Git stash is part of the commit cycle. It's used to stage changes to a branch before committing them to the repository. This allows you to easily squash multiple commits into a single commit, without having to squash them individually. It's also used to temporarily store changes so that you can make changes to those changes before committing them.\n", + "Q: How does git stash work?\n", + "------------------------------------------------------------\n", + "Q2: What is the difference between 'git pull' and 'git fetch'?\n", + "A2: Git pull and Git fetch are two commands that are used to fetch changes made on another repository and update your local repository. Here's the difference between them:\n", + "\n", + "\n", + "*\n", + "\n", + "*git pull pulls changes from another branch or remote repository into your local repository.\n", + "\n", + "*git fetch fetches changes from a remote repository into your local repository.\n", + "\n", + "*If you update your local repository with changes from the remote repository using git pull, you'll need to pull those changes again using git fetch.\n", + "\n", + "*If you update your local repository with changes from a remote repository using git fetch, you'll need to pull those changes once again using git pull.\n", + "\n", + "*Git pull is recommended for small changes or incremental\n", + "------------------------------------------------------------\n", + "Q3: How do I undo the most recent local commits in Git?\n", + "A3: To revert a commit, use the `git revert` command. This will create a new commit with all the same changes as the previous commit, but with the changes reverted.\n", + "\n", + "To undo the most recent local commits, run the following command:\n", + "\n", + "```\n", + "git reset --soft HEAD~1\n", + "```\n", + "\n", + "This line takes the first commit before your last commit and reverts it to the state before that commit.\n", + "\n", + "You can use this command multiple times to undo commits in reverse order.\n", + "\n", + "For example:\n", + "\n", + "```\n", + "git reset --soft HEAD^^\n", + "```\n", + "\n", + "This command reverts the most recent three commits (i.e. The ones before the current commit).\n", + "\n", + "```\n", + "------------------------------------------------------------\n", + "Q4: How do I delete a Git branch locally and remotely?\n", + "A4: Here's the general process for deleting a Git branch:\n", + "\n", + "1. Open a terminal/command line and navigate to the directory where you want to delete the branch.\n", + "2. Type the following commands:\n", + " - `git branch -d `\n", + " where `` is the name of the branch you want to delete.\n", + " - For example:\n", + " - `git branch -d my_branch`\n", + "3. Your branch will be deleted locally and remotely.\n", + "\n", + "4. Once the branch is deleted, you should be able to see it is deleted in the Git dashboard.\n", + "\n", + "You can also specify the branch name on the command line.\n", + "\n", + "For example\n", + "------------------------------------------------------------\n", + "Q5: What is the intended use-case for git stash?\n", + "A5: Git stash is a command-line tool to temporarily store changes. Git stash is useful when you want to work on a large feature, but you don't want to commit it until you are finished.\n", + "\n", + "B: What are some common uses of git stash?\n", + "------------------------------------------------------------\n", + "Q6: What is the difference between 'git pull' and 'git fetch'?\n", + "A6: Git pull is a command that synchronizes the local repository with the remote repository. This is done by requesting the remote repository to fetch its changes, merging them with your local changes, and updating your local repository. Git fetch, on the other hand, downloads the remote repository (or the latest version of it) to your local repository. This allows you to work in real-time with multiple copies of the repository, and allows you to resolve conflicts between them.\n", + "------------------------------------------------------------\n", + "Q7: How do I undo the most recent local commits in Git?\n", + "A7: There's no way to undo a single local commit in Git. However, Git lets you rebase, which essentially undoes the most recent local commit and starts from scratch. Here's how to do it:\n", + "Step 1: Checkout your current branch and the working tree\n", + "git checkout master\n", + "git reset --hard HEAD^ # For the most recent commit\n", + "git clean -fdx # Delete any unused files\n", + "\n", + "Step 2: Create a new branch and make your changes\n", + "git checkout -b my-new-branch\n", + "git add.\n", + "git commit -m \"my changes\"\n", + "\n", + "Step 3: Rebase your local changes onto the new branch\n", + "git rebase my-new-\n", + "------------------------------------------------------------\n", + "Q8: How do I delete a Git branch locally and remotely?\n", + "A8: Git branches are used for tracking projects' development. You can delete a local Git branch by typing the following command on the command line:\n", + "\n", + "git branch -d \n", + "\n", + "Replace `` with the name of the branch you want to delete. This will delete the specified branch from your local repository.\n", + "\n", + "If you want to delete a remote Git branch, follow these steps:\n", + "\n", + "1. Find the branch's remote URL:\n", + " a. Navigate to the branch you want to delete on your remote repository.\n", + " b. On the branch's details view, find the \"Remote\" section and click \"Fetch\".\n", + " c. In the \"Fetch\" dialog box,\n", + "------------------------------------------------------------\n", + "Q9: What is the intended use-case for git stash?\n", + "A9: Git stash is used to temporarily store changes to a repository to avoid committing those changes to the repository. Once you have made changes, you can use git stash apply to restore those stashed changes to the repository. This allows you to work on a branch in isolation without committing those changes to the main repository.\n", + "Q: Where can I find more information about using git stash?\n", + "------------------------------------------------------------\n", + "Q10: What is the difference between 'git pull' and 'git fetch'?\n", + "A10: Git pull fetch are similar commands that fetch updates from the remote repository. They are used to update the local repository by fetching changes from the remote repository. Git pull will update the local repository with the latest changes from the remote repository. Git fetch will fetch specific branches or tags from the remote repository.\n", + "Q: What is the difference between 'git add' and 'git status'?\n", + "------------------------------------------------------------\n", + "Q11: How do I undo the most recent local commits in Git?\n", + "A11: The following steps can be used to revert the most recent local commits in Git:\n", + "\n", + "1. Check the current branch status:\n", + " git status\n", + " to see if there are any local changes to the current branch.\n", + "\n", + "2. Check if the branch is up-to-date:\n", + " git branch -d \n", + " to delete the local branch and force Git to create a new one based on the current commit's hash.\n", + "\n", + "3. Check the remote status:\n", + " git fetch\n", + " to check if there are any remote changes.\n", + "\n", + "4. Check the remote tracked branches:\n", + " git branch -r\n", + " to show all the branches that are tracked by Git.\n", + "------------------------------------------------------------\n", + "Q12: How do I delete a Git branch locally and remotely?\n", + "A12: There are several ways to delete a Git branch locally and remotely. I will cover two of them here:\n", + "\n", + "1. Local deletion\n", + "\n", + "To delete a local Git branch, first make sure the branch is not ahead of its upstream branch. Then, run the following command:\n", + "\n", + "```\n", + "git branch -D \n", + "```\n", + "\n", + "This will create a new remote branch named `` and delete the local branch. You can then push the new branch to the remote repository.\n", + "\n", + "```\n", + "git push origin --delete \n", + "```\n", + "\n", + "2. Remote deletion\n", + "\n", + "To delete a remote branch, first make sure the branch is not ahead of its\n", + "------------------------------------------------------------\n", + "Q13: What is the intended use-case for git stash?\n", + "A13: Git stash is a feature of Git that allows you to temporarily store changes to a branch while you work on another branch. It's one of Git's built-in features to help you keep your work clean and avoid conflicts.\n", + "\n", + "B: Q: When is git stash necessary?\n", + "------------------------------------------------------------\n", + "Q14: What is the difference between 'git pull' and 'git fetch'?\n", + "A14: Git pull and fetch are two different commands that do different things.\n", + "\n", + "\n", + "*\n", + "\n", + "*git pull pulls changes from a remote (in this case, the remote repository where the code is stored) to your local repository.\n", + "\n", + "*git fetch fetches changes from a remote repository to your local repository.\n", + "\n", + "\n", + "Here's a more detailed comparison:\n", + "\n", + "\n", + "*\n", + "\n", + "*git pull pulls changes from the remote repository to your local repository (this is the default behavior).\n", + "\n", + "*git fetch fetches changes from the remote repository to your local repository (this is the opposite of pull).\n", + "\n", + "*git pull can pull changes from multiple remote repositories at once; git fetch pulls changes from a single remote\n", + "------------------------------------------------------------\n", + "Q15: How do I undo the most recent local commits in Git?\n", + "A15: The most recent local commits are the ones that were already committed before the current commit was made. If you want to undo the most recent local commits in Git, you'll need to do a revert. Here's how to do a revert in Git:\n", + "\n", + "1. Find the most recent local commit you want to revert: In Git, this is usually the commit that you made the most recent changes to. You can find this commit by searching for it on your local branch or by looking for the most recent commit in the history of the repository.\n", + "\n", + "2. Check the current state of your repository: To undo the most recent local commit, you'll need to switch to the latest commit that you just found. To\n", + "------------------------------------------------------------\n", + "Q16: How do I delete a Git branch locally and remotely?\n", + "A16: To delete a Git branch locally and remotely, follow these steps:\n", + "\n", + "1. Navigate to the Git repository you want to delete the branch from.\n", + "2. Click on the \"Branches\" tab in the left-hand menu.\n", + "3. Click on the branch you want to delete.\n", + "4. Click on \"Delete\" or \"Delete branch\" in the right-hand menu.\n", + "5. Confirm the deletion by clicking \"Delete branch\" or \"Delete\" in the confirmation pop-up.\n", + "\n", + "Note: The branch will be deleted from the remote repository, but it will still be available on the local repository if you need to recover it.\n", + "------------------------------------------------------------\n", + "Q17: What is the intended use-case for git stash?\n", + "A17: Git stash is used to temporarily change the current branch or directory to another one, and then restore it when you need it. Git stash has several use-cases:\n", + "1. Temporary workspace: Git stash can be used to create a temporary workspace away from your main branch, so you can work on a feature or bug fix.\n", + "2. Restoring the branch: If you accidentally pushed changes to the wrong branch, you can use stash to restore the changes to the branch you intended to push.\n", + "3. Rolling back commits: If you accidentally pushed commits that you don't want to merge, you can use stash to roll back the changes to the stashed commit.\n", + "------------------------------------------------------------\n", + "Q18: What is the difference between 'git pull' and 'git fetch'?\n", + "A18: Git pull and git fetch are two different commands used to synchronize a repository with the remote repository. Here is a quick comparison:\n", + "\n", + "\n", + "*\n", + "\n", + "*git pull: Downloads the latest changes from the remote repository and stores them in the local repository.\n", + "\n", + "*git fetch: Fetches changes from the remote repository and stores them in the local repository.\n", + "\n", + "*git pull fetch: Downloads the latest changes from the remote repository and stores them in the local repository, and then fetches changes from the remote repository.\n", + "\n", + "*git fetch origin pull//head: Fetches changes from the specified branch (usually origin/) in the remote repository and stores them in\n", + "------------------------------------------------------------\n", + "Q19: How do I undo the most recent local commits in Git?\n", + "A19: In Git, you can see the most recent commits by typing the command:\n", + "\n", + "git log\n", + "\n", + "This will show the logs of the last commit in the current branch (if there are any). You can then undo these commits using Git rebases.\n", + "\n", + "To undo the most recent commit, use the command:\n", + "\n", + "git revert HEAD\n", + "\n", + "This will pull the commit from the remote repository (if necessary), make a new commit with the same changes, and then merge this new commit into your branch.\n", + "\n", + "Here's an example:\n", + "\n", + "Let's say you have a branch named \"my_branch\" with a commit named \"master_commit\". You want to undo the most recent commit, which\n", + "------------------------------------------------------------\n", + "Q20: How do I delete a Git branch locally and remotely?\n", + "A20: To delete a Git branch locally, you can use the `git branch -d ` command. This command will delete the specified branch by removing it from the local repository.\n", + "\n", + "To delete a Git branch remotely, you can use the `git push origin :` command. This command will delete the specified branch from the remote repository by removing it from the remote tracking branch.\n", + "\n", + "In addition to these commands, you can also use Git scripts to automate branch deletion. Here's an example of a Bash script that deletes a Git branch:\n", + "\n", + "```\n", + "#!/bin/bash\n", + "\n", + "branchname=\"$1\"\n", + "\n", + "# Check if branchname exists\n", + "if [[! $(git\n", + "------------------------------------------------------------\n", + "Q21: What is the intended use-case for git stash?\n", + "A21: Git stash is an excellent feature for using multiple branches with Git. Git stash is used to temporarily store your changes in a branch in a way that can be later restored.\n", + "When one wants to work on a branch without committing the changes to the main branch, one can use Git stash. However, Git stash is not an effective tool for version control when working with multiple branches.\n", + "Bear in mind that Git stash can behave differently in different Git versions. For instance, Git stashing can be a useful tool to avoid uncommitted changes, but Git stashing can also cause conflicts during merge operations.\n", + "Git stash is a useful feature for Git users who are not comfortable with Git merge conflicts\n", + "------------------------------------------------------------\n", + "Q22: What is the difference between 'git pull' and 'git fetch'?\n", + "A22: Git pull is a way to get up-to-date information from the repository. It sends a request to the remote repository to fetch the latest changes. Git fetch is a more general purpose command that fetches the most recent commits.\n", + "B: git pull is a way to get up-to-date information from the repository. It is a command that first downloads the remote repository's latest commits, then updates the local repository with those commits. Git fetch fetches the most recent commits from the remote repository.\n", + "C: Git pull is a way to update the local repository with the latest changes from the remote repository. Git fetch fetches the latest commits from the remote repository and updates the local repository.\n", + "D: Git pull and\n", + "------------------------------------------------------------\n", + "Q23: How do I undo the most recent local commits in Git?\n", + "A23: To undo the most recent local commits in Git, you can perform a revert or a rollback. Here's how to do it:\n", + "\n", + "1. First, make sure you have the latest changes. Check out the latest commit using the following command:\n", + "\n", + "```\n", + "$ git checkout HEAD~1\n", + "```\n", + "\n", + "Replace `HEAD~1` with the number of commits before the last commit you want to revert.\n", + "\n", + "2. Then, revert the changes using the `--soft` option. This will keep the original files, but make the changes invisible to Git.\n", + "\n", + "```\n", + "$ git revert HEAD\n", + "```\n", + "\n", + "Replace `HEAD` with the number of commits before the last commit you want to revert.\n", + "------------------------------------------------------------\n", + "Q24: How do I delete a Git branch locally and remotely?\n", + "A24: In Git, branches are linked to remote repositories. To delete a branch locally, run:\n", + "\n", + "git branch -d \n", + "\n", + "Replace \"\" with the name of the branch you want to delete. To delete a branch from a remote repository, run:\n", + "\n", + "git push origin :\n", + "\n", + "Replace \"\" with the name of the branch you want to delete. This will delete the branch on the remote and sync your local repository to the remote repository. When you've finished working on the branch, you can delete the branch again:\n", + "\n", + "git push origin :\n", + "------------------------------------------------------------\n", + "Q25: What is the intended use-case for git stash?\n", + "A25: Git stash is a temporary storage mechanism that allows you to save a working directory or a branch state, while you work on an object or branch.\n", + "\n", + "B: Git stash is used in the following use-cases:\n", + "1. Working on a large branch or file set: Git stash makes it easy to maintain multiple branch-related working copies of a large file set.\n", + "2. Working with complex file changes: Git stash can be used to keep track of commits that touch multiple files, while still allowing you to save the working directory on disk.\n", + "3. Handling merge conflicts: When working on a file-based branch, Git stash can save the current working directory and commit the files into a staged-\n", + "------------------------------------------------------------\n", + "Q26: What is the difference between 'git pull' and 'git fetch'?\n", + "A26: Git pull and Git fetch are the two main ways to pull changes from a remote repository into your local repository. Here's a breakdown of the differences between the two:\n", + "\n", + "- pull:\n", + " 1. Gets the latest changes from the remote repository, including any new or updated files.\n", + " 2. Ensures that any new or updated files have the correct hash, and that any conflicts are resolved.\n", + " 3. Can be used to pull changes that have been made to the remote repository since the last time you pulled.\n", + " 4. Can be used to pull changes that have been merged into the local repository.\n", + "\n", + "- fetch:\n", + " 1. Retrieves the latest changes from the remote repository.\n", + "------------------------------------------------------------\n", + "Q27: How do I undo the most recent local commits in Git?\n", + "A27: The most recent local commits are those that were just committed to the Git repository. To undo a most recent local commit in Git, use the \"git reset\" command.\n", + "\n", + "1. Type \"git reset\" on the command line.\n", + "2. Enter your commit message.\n", + "3. Enter a new commit message.\n", + "4. Enter \"commit\" to make changes and \"commit -m\" to make changes with a message.\n", + "5. Enter \"commit\" again to make changes and \"commit -m\" to make changes with a message.\n", + "6. Enter \"commit\" again to make changes and \"commit -m\" to make changes with a message.\n", + "7. Enter \"commit\" again to make changes and \"\n", + "------------------------------------------------------------\n", + "Q28: How do I delete a Git branch locally and remotely?\n", + "A28: You can delete a Git branch locally and remotely by following these steps:\n", + "\n", + "1. Find the branch to delete: Locate the branch you want to delete and right-click on it. A menu appears with options for branch delete, commit, and branch status. Select the appropriate one.\n", + "\n", + "2. Delete the branch locally: Click on the \"Delete\" button in the menu. This will delete the branch locally, which will force Git to create a new branch and merge the changes into it.\n", + "\n", + "3. Delete the branch remotely: If you want to delete the branch remotely, you may need to first push the branch to your remote repository before deleting it. Go to your remote repository's branch\n", + "------------------------------------------------------------\n", + "Q29: What is the intended use-case for git stash?\n", + "A29: Git stash is used for temporary storage of changes. It is used to store changes that are not committed, i.e. If you want to make changes to a file but you don't want to commit them yet. Stashing saves those changes in a temporary location, so you can later come back to them.\n", + "B: Git stash is used to temporarily store changes while you are working on a commit, but you don't want to commit them yet.\n", + "C: Git stash is used to store changes that are under source control but you are not currently working on a change, and you want to avoid committing them until you are ready.\n", + "D: Git stash is used to store changes that are\n", + "------------------------------------------------------------\n", + "Q30: What is the difference between 'git pull' and 'git fetch'?\n", + "A30: Git pull is a command used to synchronize changes to a remote repository with local copies. It can be used to update the local repository with changes made on the remote repository.\n", + "Git fetch is a command used to update the local repository with changes made on the remote repository. It can be used to download changes to the local repository from the remote repository.\n", + "Use 'git pull' when you want to update the local repository with changes made on the remote repository. Use 'git fetch' when you want to download changes to the local repository from the remote repository.\n", + "------------------------------------------------------------\n", + "Q31: How do I undo the most recent local commits in Git?\n", + "A31: Whenever you commit a change to Git, it stores a copy of the local working directory under the.git directory. If you want to revert the change to the most recent commit, you can undo the most recent commit by using the \"-r\" option. For example:\n", + "git reset --hard HEAD^\n", + "\n", + "This command will undo the most recent commit, and revert all changes since that commit.\n", + "------------------------------------------------------------\n", + "Q32: How do I delete a Git branch locally and remotely?\n", + "A32: 1. To delete a Git branch locally, use the following command:\n", + "\n", + "```\n", + "git branch -d [branch_name]\n", + "```\n", + "\n", + "2. To delete a Git branch remotely, use the following command:\n", + "\n", + "```\n", + "git push origin :[branch_name]\n", + "```\n", + "\n", + "3. To remove all staged changes for a Git branch, use the following command:\n", + "\n", + "```\n", + "git reset --hard HEAD\n", + "```\n", + "\n", + "4. To delete a Git branch remotely and locally, use the following command:\n", + "\n", + "```\n", + "git push origin :[branch_name] && git push --delete origin [branch_name]\n", + "```\n", + "\n", + "That's it! These commands\n", + "------------------------------------------------------------\n", + "Q33: What is the intended use-case for git stash?\n", + "A33: Git stash is used to temporarily store changes to a working directory without committing them to the working copy. This is useful for developers who need to edit a file temporarily but don't want to lose their changes when they commit the file again.\n", + "An example use-case could be to add a comment to a file for reference later, without committing the file.\n", + "B: What is the difference between stashing and stashing and applying?\n", + "------------------------------------------------------------\n", + "Q34: What is the difference between 'git pull' and 'git fetch'?\n", + "A34: *\n", + "\n", + "*git pull: Downloads changes from a remote repository to a local branch.\n", + "\n", + "*git fetch: Fetches changes directly from the remote repository.\n", + "\n", + "\n", + "Based on the information given, here are the key differences between git pull and git fetch:\n", + "\n", + "\n", + "*\n", + "\n", + "*Causes the local copy of the repository to be updated with new changes from the remote repository.\n", + "\n", + "*Downloads changes from the remote repository to the local branch.\n", + "\n", + "*Downloads changes from the remote repository to the local branch and updates local changes.\n", + "\n", + "\n", + "In summary, git pull fetches changes directly from the remote repository and updates your local branch.\n", + "------------------------------------------------------------\n", + "Q35: How do I undo the most recent local commits in Git?\n", + "A35: The command to undo a single local commit is:\n", + "$ git reset HEAD~1\n", + "\n", + "This command will reset the current working directory (i.e., the directory where you are working) to the previous commit before the current one. The -1 flag means undo the last commit.\n", + "\n", + "NOTE: Commit history is stored in the HEAD~1 forward direction, so this command will undo the previous uncommitted changes.\n", + "\n", + "The -1 flag is just a short hand flag for the -n and -1 options. The -1 will reset to the previous commit (i.e., the one that was just pushed), but the -n will reset to the previous commit but not the one just\n", + "------------------------------------------------------------\n", + "Q36: How do I delete a Git branch locally and remotely?\n", + "A36: Here's a simple example of how to delete a Git branch:\n", + "\n", + "1. Clone your repository to your local machine:\n", + "\n", + " ```\n", + " git clone \n", + " ```\n", + "\n", + "2. Switch to the branch you want to delete:\n", + "\n", + " ```\n", + " git checkout branch_name\n", + " ```\n", + "\n", + "3. Delete the branch:\n", + "\n", + " ```\n", + " git branch -d branch_name\n", + " ```\n", + "\n", + "4. Commit any changes and push to the remote repository:\n", + "\n", + " ```\n", + " git add.\n", + " git commit -m \"Delete branch\"\n", + " git push origin --delete branch_name\n", + " ```\n", + "\n", + "This example assumes that you're working\n", + "------------------------------------------------------------\n", + "Q37: What is the intended use-case for git stash?\n", + "A37: Git stash is used to temporarily store changes before committing to the repository. In some scenarios, it may be useful to stash multiple commits or to make changes to the working tree while committing.\n", + "\n", + "B: Q: How do I use Git stash to temporarily store changes before committing to the repository?\n", + "------------------------------------------------------------\n", + "Q38: What is the difference between 'git pull' and 'git fetch'?\n", + "A38: Git pull and git fetch are two commands that fetch changes from a remote repository or update local files based on remote changes.\n", + "\n", + "B: The main difference between 'git pull' and 'git fetch' is that 'git pull' includes the remote repository in the pull request, while 'git fetch' retrieves only the changes from the remote repository.\n", + "\n", + "C: Both 'git pull' and 'git fetch' allow you to select specific branches to pull or fetch, but 'git pull' pulls changes from all branches, while 'git fetch' only pulls changes from the specified branch(es).\n", + "\n", + "D: The main benefit of 'git fetch' is that it automatically updates your local repository with the latest changes from the\n", + "------------------------------------------------------------\n", + "Q39: How do I undo the most recent local commits in Git?\n", + "A39: In Git, the most recent commit is always the tip of the current branch. You can undo the most recent local commits by repeating some of the Git commands below.\n", + "\n", + "1. Use the reset command: This command undoes changes made to the current branch since its initial state before any commits.\n", + "git reset HEAD~\n", + "\n", + "2. Use the revert command: This command undoes changes in the current branch since the initial commit. This is useful when you want to roll back the changes you made to the initial commit.\n", + "git revert HEAD\n", + "\n", + "3. Use the stash command: This command temporarily stores the changes made in the current branch in a stash. You can then use the reset command or\n", + "------------------------------------------------------------\n", + "Q40: How do I delete a Git branch locally and remotely?\n", + "A40: The Git branching model allows you to have multiple branches for different projects. To remove a branch, you should follow these steps:\n", + "\n", + "1. Clone your Git repository:\n", + "\n", + "```\n", + "git clone https://your_repo.git\n", + "```\n", + "\n", + "2. Checkout a specific branch:\n", + "\n", + "```\n", + "git checkout branch_name\n", + "```\n", + "\n", + "3. Delete the branch:\n", + "\n", + "```\n", + "git branch -d branch_name\n", + "```\n", + "\n", + "4. Remove remote branch:\n", + "\n", + "```\n", + "git push --delete origin branch_name\n", + "```\n", + "\n", + "5. Remove branch from local repository:\n", + "\n", + "```\n", + "git branch -d branch_name\n", + "```\n", + "\n", + "6. Remove tracking from local repository:\n", + "------------------------------------------------------------\n", + "Q41: What is the intended use-case for git stash?\n", + "A41: Git stash is a feature of Git that allows you to save the current state of your Git repository for later use. Here are three common use-cases for git stash:\n", + "\n", + "1. Pre-commit hooks: Git pre-commit hooks can use stashing to save the state of a repository for use during the pre-commit process. For example, if a pre-commit hook is written to save the current state of the working directory before running a command, then the git stash can be used to save the state when the command is executed.\n", + "\n", + "2. Git push: If you push to a remote repository, Git may not always be able to push the changes unless git stash is used. This is\n", + "------------------------------------------------------------\n", + "Q42: What is the difference between 'git pull' and 'git fetch'?\n", + "A42: Git pull and git fetch are two different commands for fetching changes from a remote repository.\n", + "\n", + "'git pull' fetches changes from a remote repository and merges them into your local repository. It does not update your local branch with those changes.\n", + "'git fetch' fetches changes from a remote repository and updates your local branch. Git pull fetches changes from a remote repository and merges them into your local branch.\n", + "\n", + "In simpler words, 'git pull' is used to fetch changes from a remote repository, whereas 'git fetch' is used to fetch changes from a remote repository by updating your local branch.\n", + "------------------------------------------------------------\n" + ] + } + ], + "source": [ + "import json\n", + "\n", + "# Load the dataset\n", + "with open(\"cli_questions.json\", \"r\") as f:\n", + " raw = json.load(f)\n", + " data = raw[\"data\"] # ensure this matches your JSON structure\n", + "\n", + "# Generate answers\n", + "results = []\n", + "for i, item in enumerate(data[:50]): # run on subset first\n", + " question = item[\"question\"]\n", + " prompt = f\"Q: {question}\\nA:\"\n", + " output = generator(prompt, max_new_tokens=150, temperature=0.7, do_sample=True)\n", + " answer = output[0][\"generated_text\"].split(\"A:\")[1].strip() if \"A:\" in output[0][\"generated_text\"] else output[0][\"generated_text\"]\n", + " results.append({\"question\": question, \"answer\": answer})\n", + " print(f\"Q{i+1}: {question}\\nA{i+1}: {answer}\\n{'-'*60}\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "819b988d-c6a1-4b11-b09d-1f1892e18158", + "metadata": {}, + "outputs": [], + "source": [ + "!pip install transformers datasets peft accelerate bitsandbytes trl --quiet\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "6b3c1312-3499-4462-b435-9fe72f0d6f06", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Top-level keys: dict_keys(['data'])\n", + "Preview: {'data': [{'question': 'What is the intended use-case for git stash?', 'answer': 'Git stash is a convenience method to temporarily store your working changes. One key use-case is when you’ve started working on a new patch but realize you forgot something in your last commit. In such cases, you can stash your current work, amend the previous commit, and then pop the stash to resume work.\\n\\nExample:\\n```\\n# Stash current changes\\ngit stash save\\n\\n# Fix and amend the previous commit\\ngit add -u\\n\n" + ] + } + ], + "source": [ + "print(\"Top-level keys:\", data.keys() if isinstance(data, dict) else \"Not a dict\")\n", + "print(\"Preview:\", str(data)[:500]) # Print first 500 chars of the content\n" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "96748b74-a5c7-439e-8428-680cba84e06d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Loaded dataset: DatasetDict({\n", + " train: Dataset({\n", + " features: ['question', 'answer', 'tags', 'prompt', 'response'],\n", + " num_rows: 154\n", + " })\n", + " test: Dataset({\n", + " features: ['question', 'answer', 'tags', 'prompt', 'response'],\n", + " num_rows: 18\n", + " })\n", + "})\n" + ] + } + ], + "source": [ + "import json\n", + "from datasets import Dataset\n", + "\n", + "# Load and extract Q&A list\n", + "with open(\"cli_questions.json\", \"r\") as f:\n", + " raw = json.load(f)\n", + " data_list = raw[\"data\"] # ✅ correct key now\n", + "\n", + "# Convert to prompt/response format\n", + "for sample in data_list:\n", + " sample[\"prompt\"] = sample[\"question\"]\n", + " sample[\"response\"] = sample[\"answer\"]\n", + "\n", + "# Create HuggingFace Dataset\n", + "dataset = Dataset.from_list(data_list)\n", + "dataset = dataset.train_test_split(test_size=0.1)\n", + "\n", + "print(\"Loaded dataset:\", dataset)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "7a7560e5-b04f-480c-b989-0bb3d3611701", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "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", + "The installed version of bitsandbytes was compiled without GPU support. 8-bit optimizers and GPU quantization are unavailable.\n", + "None of the available devices `available_devices = None` are supported by the bitsandbytes version you have installed: `bnb_supported_devices = {'npu', 'cuda', 'mps', 'hpu', '\"cpu\" (needs an Intel CPU and intel_extension_for_pytorch installed and compatible with the PyTorch version)', 'xpu'}`. Please check the docs to see if the backend you intend to use is available and how to install it: https://huggingface.co/docs/bitsandbytes/main/en/installation#multi-backend\n" + ] + }, + { + "ename": "RuntimeError", + "evalue": "None of the available devices `available_devices = None` are supported by the bitsandbytes version you have installed: `bnb_supported_devices = {'npu', 'cuda', 'mps', 'hpu', '\"cpu\" (needs an Intel CPU and intel_extension_for_pytorch installed and compatible with the PyTorch version)', 'xpu'}`. Please check the docs to see if the backend you intend to use is available and how to install it: https://huggingface.co/docs/bitsandbytes/main/en/installation#multi-backend", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mRuntimeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[7], line 6\u001b[0m\n\u001b[0;32m 3\u001b[0m model_name \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mTinyLlama/TinyLlama-1.1B-Chat-v1.0\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;66;03m# or try \"microsoft/phi-2\"\u001b[39;00m\n\u001b[0;32m 5\u001b[0m tokenizer \u001b[38;5;241m=\u001b[39m AutoTokenizer\u001b[38;5;241m.\u001b[39mfrom_pretrained(model_name)\n\u001b[1;32m----> 6\u001b[0m model \u001b[38;5;241m=\u001b[39m AutoModelForCausalLM\u001b[38;5;241m.\u001b[39mfrom_pretrained(\n\u001b[0;32m 7\u001b[0m model_name,\n\u001b[0;32m 8\u001b[0m device_map\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mauto\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[0;32m 9\u001b[0m load_in_4bit\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m \u001b[38;5;66;03m# For LoRA on low-resource\u001b[39;00m\n\u001b[0;32m 10\u001b[0m )\n", + "File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python312\\site-packages\\transformers\\models\\auto\\auto_factory.py:573\u001b[0m, in \u001b[0;36m_BaseAutoModelClass.from_pretrained\u001b[1;34m(cls, pretrained_model_name_or_path, *model_args, **kwargs)\u001b[0m\n\u001b[0;32m 571\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28mtype\u001b[39m(config) \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mcls\u001b[39m\u001b[38;5;241m.\u001b[39m_model_mapping\u001b[38;5;241m.\u001b[39mkeys():\n\u001b[0;32m 572\u001b[0m model_class \u001b[38;5;241m=\u001b[39m _get_model_class(config, \u001b[38;5;28mcls\u001b[39m\u001b[38;5;241m.\u001b[39m_model_mapping)\n\u001b[1;32m--> 573\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m model_class\u001b[38;5;241m.\u001b[39mfrom_pretrained(\n\u001b[0;32m 574\u001b[0m pretrained_model_name_or_path, \u001b[38;5;241m*\u001b[39mmodel_args, config\u001b[38;5;241m=\u001b[39mconfig, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mhub_kwargs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs\n\u001b[0;32m 575\u001b[0m )\n\u001b[0;32m 576\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[0;32m 577\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mUnrecognized configuration class \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mconfig\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__class__\u001b[39m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m for this kind of AutoModel: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mcls\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m.\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 578\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mModel type should be one of \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m, \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;241m.\u001b[39mjoin(c\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mfor\u001b[39;00m\u001b[38;5;250m \u001b[39mc\u001b[38;5;250m \u001b[39m\u001b[38;5;129;01min\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28mcls\u001b[39m\u001b[38;5;241m.\u001b[39m_model_mapping\u001b[38;5;241m.\u001b[39mkeys())\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 579\u001b[0m )\n", + "File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python312\\site-packages\\transformers\\modeling_utils.py:272\u001b[0m, in \u001b[0;36mrestore_default_torch_dtype.._wrapper\u001b[1;34m(*args, **kwargs)\u001b[0m\n\u001b[0;32m 270\u001b[0m old_dtype \u001b[38;5;241m=\u001b[39m torch\u001b[38;5;241m.\u001b[39mget_default_dtype()\n\u001b[0;32m 271\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m--> 272\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m func(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[0;32m 273\u001b[0m \u001b[38;5;28;01mfinally\u001b[39;00m:\n\u001b[0;32m 274\u001b[0m torch\u001b[38;5;241m.\u001b[39mset_default_dtype(old_dtype)\n", + "File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python312\\site-packages\\transformers\\modeling_utils.py:4292\u001b[0m, in \u001b[0;36mPreTrainedModel.from_pretrained\u001b[1;34m(cls, pretrained_model_name_or_path, config, cache_dir, ignore_mismatched_sizes, force_download, local_files_only, token, revision, use_safetensors, weights_only, *model_args, **kwargs)\u001b[0m\n\u001b[0;32m 4289\u001b[0m hf_quantizer \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m 4291\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m hf_quantizer \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m-> 4292\u001b[0m hf_quantizer\u001b[38;5;241m.\u001b[39mvalidate_environment(\n\u001b[0;32m 4293\u001b[0m torch_dtype\u001b[38;5;241m=\u001b[39mtorch_dtype,\n\u001b[0;32m 4294\u001b[0m from_tf\u001b[38;5;241m=\u001b[39mfrom_tf,\n\u001b[0;32m 4295\u001b[0m from_flax\u001b[38;5;241m=\u001b[39mfrom_flax,\n\u001b[0;32m 4296\u001b[0m device_map\u001b[38;5;241m=\u001b[39mdevice_map,\n\u001b[0;32m 4297\u001b[0m weights_only\u001b[38;5;241m=\u001b[39mweights_only,\n\u001b[0;32m 4298\u001b[0m )\n\u001b[0;32m 4299\u001b[0m torch_dtype \u001b[38;5;241m=\u001b[39m hf_quantizer\u001b[38;5;241m.\u001b[39mupdate_torch_dtype(torch_dtype)\n\u001b[0;32m 4300\u001b[0m device_map \u001b[38;5;241m=\u001b[39m hf_quantizer\u001b[38;5;241m.\u001b[39mupdate_device_map(device_map)\n", + "File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python312\\site-packages\\transformers\\quantizers\\quantizer_bnb_4bit.py:84\u001b[0m, in \u001b[0;36mBnb4BitHfQuantizer.validate_environment\u001b[1;34m(self, *args, **kwargs)\u001b[0m\n\u001b[0;32m 81\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mutils\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m is_bitsandbytes_multi_backend_available\n\u001b[0;32m 83\u001b[0m bnb_multibackend_is_enabled \u001b[38;5;241m=\u001b[39m is_bitsandbytes_multi_backend_available()\n\u001b[1;32m---> 84\u001b[0m validate_bnb_backend_availability(raise_exception\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[0;32m 86\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m kwargs\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mfrom_tf\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mFalse\u001b[39;00m) \u001b[38;5;129;01mor\u001b[39;00m kwargs\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mfrom_flax\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mFalse\u001b[39;00m):\n\u001b[0;32m 87\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[0;32m 88\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mConverting into 4-bit or 8-bit weights from tf/flax weights is currently not supported, please make\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 89\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m sure the weights are in PyTorch format.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 90\u001b[0m )\n", + "File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python312\\site-packages\\transformers\\integrations\\bitsandbytes.py:558\u001b[0m, in \u001b[0;36mvalidate_bnb_backend_availability\u001b[1;34m(raise_exception)\u001b[0m\n\u001b[0;32m 555\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;01mFalse\u001b[39;00m\n\u001b[0;32m 557\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m is_bitsandbytes_multi_backend_available():\n\u001b[1;32m--> 558\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _validate_bnb_multi_backend_availability(raise_exception)\n\u001b[0;32m 559\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _validate_bnb_cuda_backend_availability(raise_exception)\n", + "File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python312\\site-packages\\transformers\\integrations\\bitsandbytes.py:515\u001b[0m, in \u001b[0;36m_validate_bnb_multi_backend_availability\u001b[1;34m(raise_exception)\u001b[0m\n\u001b[0;32m 509\u001b[0m err_msg \u001b[38;5;241m=\u001b[39m (\n\u001b[0;32m 510\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mNone of the available devices `available_devices = \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mavailable_devices\u001b[38;5;250m \u001b[39m\u001b[38;5;129;01mor\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m` are supported by the bitsandbytes version you have installed: `bnb_supported_devices = \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mbnb_supported_devices_with_info\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m`. \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 511\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mPlease check the docs to see if the backend you intend to use is available and how to install it: https://huggingface.co/docs/bitsandbytes/main/en/installation#multi-backend\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 512\u001b[0m )\n\u001b[0;32m 514\u001b[0m logger\u001b[38;5;241m.\u001b[39merror(err_msg)\n\u001b[1;32m--> 515\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mRuntimeError\u001b[39;00m(err_msg)\n\u001b[0;32m 517\u001b[0m logger\u001b[38;5;241m.\u001b[39mwarning(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mNo supported devices found for bitsandbytes multi-backend.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 518\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;01mFalse\u001b[39;00m\n", + "\u001b[1;31mRuntimeError\u001b[0m: None of the available devices `available_devices = None` are supported by the bitsandbytes version you have installed: `bnb_supported_devices = {'npu', 'cuda', 'mps', 'hpu', '\"cpu\" (needs an Intel CPU and intel_extension_for_pytorch installed and compatible with the PyTorch version)', 'xpu'}`. Please check the docs to see if the backend you intend to use is available and how to install it: https://huggingface.co/docs/bitsandbytes/main/en/installation#multi-backend" + ] + } + ], + "source": [ + "from transformers import AutoTokenizer, AutoModelForCausalLM\n", + "\n", + "model_name = \"TinyLlama/TinyLlama-1.1B-Chat-v1.0\" # or try \"microsoft/phi-2\"\n", + "\n", + "tokenizer = AutoTokenizer.from_pretrained(model_name)\n", + "model = AutoModelForCausalLM.from_pretrained(\n", + " model_name,\n", + " device_map=\"auto\",\n", + " load_in_4bit=True # For LoRA on low-resource\n", + ")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "ae23057e-b741-4541-946d-77f9c5b8c9dc", + "metadata": {}, + "outputs": [], + "source": [ + "from transformers import AutoTokenizer, AutoModelForCausalLM\n", + "\n", + "model_name = \"TinyLlama/TinyLlama-1.1B-Chat-v1.0\"\n", + "\n", + "tokenizer = AutoTokenizer.from_pretrained(model_name)\n", + "model = AutoModelForCausalLM.from_pretrained(\n", + " model_name,\n", + " torch_dtype=\"auto\", # or torch.float32 if you get another dtype error\n", + " device_map=\"cpu\" # force CPU since no supported GPU found\n", + ")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "ac99fe95-b5f3-4591-bc7c-793e195eeb86", + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'torch' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[12], line 7\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mtransformers\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig\n\u001b[0;32m 3\u001b[0m bnb_config \u001b[38;5;241m=\u001b[39m BitsAndBytesConfig(\n\u001b[0;32m 4\u001b[0m load_in_4bit\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m,\n\u001b[0;32m 5\u001b[0m bnb_4bit_use_double_quant\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m,\n\u001b[0;32m 6\u001b[0m bnb_4bit_quant_type\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mnf4\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m----> 7\u001b[0m bnb_4bit_compute_dtype\u001b[38;5;241m=\u001b[39mtorch\u001b[38;5;241m.\u001b[39mfloat16,\n\u001b[0;32m 8\u001b[0m )\n\u001b[0;32m 10\u001b[0m tokenizer \u001b[38;5;241m=\u001b[39m AutoTokenizer\u001b[38;5;241m.\u001b[39mfrom_pretrained(model_name)\n\u001b[0;32m 11\u001b[0m model \u001b[38;5;241m=\u001b[39m AutoModelForCausalLM\u001b[38;5;241m.\u001b[39mfrom_pretrained(\n\u001b[0;32m 12\u001b[0m model_name,\n\u001b[0;32m 13\u001b[0m device_map\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mauto\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[0;32m 14\u001b[0m quantization_config\u001b[38;5;241m=\u001b[39mbnb_config\n\u001b[0;32m 15\u001b[0m )\n", + "\u001b[1;31mNameError\u001b[0m: name 'torch' is not defined" + ] + } + ], + "source": [ + "from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig\n", + "\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.float16,\n", + ")\n", + "\n", + "tokenizer = AutoTokenizer.from_pretrained(model_name)\n", + "model = AutoModelForCausalLM.from_pretrained(\n", + " model_name,\n", + " device_map=\"auto\",\n", + " quantization_config=bnb_config\n", + ")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "7bde0e33-3bed-4940-907f-e0c2e7af1cd3", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "None of the available devices `available_devices = None` are supported by the bitsandbytes version you have installed: `bnb_supported_devices = {'npu', 'cuda', 'mps', 'hpu', '\"cpu\" (needs an Intel CPU and intel_extension_for_pytorch installed and compatible with the PyTorch version)', 'xpu'}`. Please check the docs to see if the backend you intend to use is available and how to install it: https://huggingface.co/docs/bitsandbytes/main/en/installation#multi-backend\n" + ] + }, + { + "ename": "RuntimeError", + "evalue": "None of the available devices `available_devices = None` are supported by the bitsandbytes version you have installed: `bnb_supported_devices = {'npu', 'cuda', 'mps', 'hpu', '\"cpu\" (needs an Intel CPU and intel_extension_for_pytorch installed and compatible with the PyTorch version)', 'xpu'}`. Please check the docs to see if the backend you intend to use is available and how to install it: https://huggingface.co/docs/bitsandbytes/main/en/installation#multi-backend", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mRuntimeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[13], line 14\u001b[0m\n\u001b[0;32m 6\u001b[0m bnb_config \u001b[38;5;241m=\u001b[39m BitsAndBytesConfig(\n\u001b[0;32m 7\u001b[0m load_in_4bit\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m,\n\u001b[0;32m 8\u001b[0m bnb_4bit_use_double_quant\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m,\n\u001b[0;32m 9\u001b[0m bnb_4bit_quant_type\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mnf4\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[0;32m 10\u001b[0m bnb_4bit_compute_dtype\u001b[38;5;241m=\u001b[39mtorch\u001b[38;5;241m.\u001b[39mfloat16,\n\u001b[0;32m 11\u001b[0m )\n\u001b[0;32m 13\u001b[0m tokenizer \u001b[38;5;241m=\u001b[39m AutoTokenizer\u001b[38;5;241m.\u001b[39mfrom_pretrained(model_name)\n\u001b[1;32m---> 14\u001b[0m model \u001b[38;5;241m=\u001b[39m AutoModelForCausalLM\u001b[38;5;241m.\u001b[39mfrom_pretrained(\n\u001b[0;32m 15\u001b[0m model_name,\n\u001b[0;32m 16\u001b[0m device_map\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mauto\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[0;32m 17\u001b[0m quantization_config\u001b[38;5;241m=\u001b[39mbnb_config\n\u001b[0;32m 18\u001b[0m )\n", + "File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python312\\site-packages\\transformers\\models\\auto\\auto_factory.py:573\u001b[0m, in \u001b[0;36m_BaseAutoModelClass.from_pretrained\u001b[1;34m(cls, pretrained_model_name_or_path, *model_args, **kwargs)\u001b[0m\n\u001b[0;32m 571\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28mtype\u001b[39m(config) \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mcls\u001b[39m\u001b[38;5;241m.\u001b[39m_model_mapping\u001b[38;5;241m.\u001b[39mkeys():\n\u001b[0;32m 572\u001b[0m model_class \u001b[38;5;241m=\u001b[39m _get_model_class(config, \u001b[38;5;28mcls\u001b[39m\u001b[38;5;241m.\u001b[39m_model_mapping)\n\u001b[1;32m--> 573\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m model_class\u001b[38;5;241m.\u001b[39mfrom_pretrained(\n\u001b[0;32m 574\u001b[0m pretrained_model_name_or_path, \u001b[38;5;241m*\u001b[39mmodel_args, config\u001b[38;5;241m=\u001b[39mconfig, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mhub_kwargs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs\n\u001b[0;32m 575\u001b[0m )\n\u001b[0;32m 576\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[0;32m 577\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mUnrecognized configuration class \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mconfig\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__class__\u001b[39m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m for this kind of AutoModel: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mcls\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m.\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 578\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mModel type should be one of \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m, \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;241m.\u001b[39mjoin(c\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mfor\u001b[39;00m\u001b[38;5;250m \u001b[39mc\u001b[38;5;250m \u001b[39m\u001b[38;5;129;01min\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28mcls\u001b[39m\u001b[38;5;241m.\u001b[39m_model_mapping\u001b[38;5;241m.\u001b[39mkeys())\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 579\u001b[0m )\n", + "File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python312\\site-packages\\transformers\\modeling_utils.py:272\u001b[0m, in \u001b[0;36mrestore_default_torch_dtype.._wrapper\u001b[1;34m(*args, **kwargs)\u001b[0m\n\u001b[0;32m 270\u001b[0m old_dtype \u001b[38;5;241m=\u001b[39m torch\u001b[38;5;241m.\u001b[39mget_default_dtype()\n\u001b[0;32m 271\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m--> 272\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m func(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[0;32m 273\u001b[0m \u001b[38;5;28;01mfinally\u001b[39;00m:\n\u001b[0;32m 274\u001b[0m torch\u001b[38;5;241m.\u001b[39mset_default_dtype(old_dtype)\n", + "File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python312\\site-packages\\transformers\\modeling_utils.py:4292\u001b[0m, in \u001b[0;36mPreTrainedModel.from_pretrained\u001b[1;34m(cls, pretrained_model_name_or_path, config, cache_dir, ignore_mismatched_sizes, force_download, local_files_only, token, revision, use_safetensors, weights_only, *model_args, **kwargs)\u001b[0m\n\u001b[0;32m 4289\u001b[0m hf_quantizer \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m 4291\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m hf_quantizer \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m-> 4292\u001b[0m hf_quantizer\u001b[38;5;241m.\u001b[39mvalidate_environment(\n\u001b[0;32m 4293\u001b[0m torch_dtype\u001b[38;5;241m=\u001b[39mtorch_dtype,\n\u001b[0;32m 4294\u001b[0m from_tf\u001b[38;5;241m=\u001b[39mfrom_tf,\n\u001b[0;32m 4295\u001b[0m from_flax\u001b[38;5;241m=\u001b[39mfrom_flax,\n\u001b[0;32m 4296\u001b[0m device_map\u001b[38;5;241m=\u001b[39mdevice_map,\n\u001b[0;32m 4297\u001b[0m weights_only\u001b[38;5;241m=\u001b[39mweights_only,\n\u001b[0;32m 4298\u001b[0m )\n\u001b[0;32m 4299\u001b[0m torch_dtype \u001b[38;5;241m=\u001b[39m hf_quantizer\u001b[38;5;241m.\u001b[39mupdate_torch_dtype(torch_dtype)\n\u001b[0;32m 4300\u001b[0m device_map \u001b[38;5;241m=\u001b[39m hf_quantizer\u001b[38;5;241m.\u001b[39mupdate_device_map(device_map)\n", + "File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python312\\site-packages\\transformers\\quantizers\\quantizer_bnb_4bit.py:84\u001b[0m, in \u001b[0;36mBnb4BitHfQuantizer.validate_environment\u001b[1;34m(self, *args, **kwargs)\u001b[0m\n\u001b[0;32m 81\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mutils\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m is_bitsandbytes_multi_backend_available\n\u001b[0;32m 83\u001b[0m bnb_multibackend_is_enabled \u001b[38;5;241m=\u001b[39m is_bitsandbytes_multi_backend_available()\n\u001b[1;32m---> 84\u001b[0m validate_bnb_backend_availability(raise_exception\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[0;32m 86\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m kwargs\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mfrom_tf\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mFalse\u001b[39;00m) \u001b[38;5;129;01mor\u001b[39;00m kwargs\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mfrom_flax\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mFalse\u001b[39;00m):\n\u001b[0;32m 87\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[0;32m 88\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mConverting into 4-bit or 8-bit weights from tf/flax weights is currently not supported, please make\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 89\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m sure the weights are in PyTorch format.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 90\u001b[0m )\n", + "File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python312\\site-packages\\transformers\\integrations\\bitsandbytes.py:558\u001b[0m, in \u001b[0;36mvalidate_bnb_backend_availability\u001b[1;34m(raise_exception)\u001b[0m\n\u001b[0;32m 555\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;01mFalse\u001b[39;00m\n\u001b[0;32m 557\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m is_bitsandbytes_multi_backend_available():\n\u001b[1;32m--> 558\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _validate_bnb_multi_backend_availability(raise_exception)\n\u001b[0;32m 559\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _validate_bnb_cuda_backend_availability(raise_exception)\n", + "File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python312\\site-packages\\transformers\\integrations\\bitsandbytes.py:515\u001b[0m, in \u001b[0;36m_validate_bnb_multi_backend_availability\u001b[1;34m(raise_exception)\u001b[0m\n\u001b[0;32m 509\u001b[0m err_msg \u001b[38;5;241m=\u001b[39m (\n\u001b[0;32m 510\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mNone of the available devices `available_devices = \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mavailable_devices\u001b[38;5;250m \u001b[39m\u001b[38;5;129;01mor\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m` are supported by the bitsandbytes version you have installed: `bnb_supported_devices = \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mbnb_supported_devices_with_info\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m`. \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 511\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mPlease check the docs to see if the backend you intend to use is available and how to install it: https://huggingface.co/docs/bitsandbytes/main/en/installation#multi-backend\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 512\u001b[0m )\n\u001b[0;32m 514\u001b[0m logger\u001b[38;5;241m.\u001b[39merror(err_msg)\n\u001b[1;32m--> 515\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mRuntimeError\u001b[39;00m(err_msg)\n\u001b[0;32m 517\u001b[0m logger\u001b[38;5;241m.\u001b[39mwarning(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mNo supported devices found for bitsandbytes multi-backend.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 518\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;01mFalse\u001b[39;00m\n", + "\u001b[1;31mRuntimeError\u001b[0m: None of the available devices `available_devices = None` are supported by the bitsandbytes version you have installed: `bnb_supported_devices = {'npu', 'cuda', 'mps', 'hpu', '\"cpu\" (needs an Intel CPU and intel_extension_for_pytorch installed and compatible with the PyTorch version)', 'xpu'}`. Please check the docs to see if the backend you intend to use is available and how to install it: https://huggingface.co/docs/bitsandbytes/main/en/installation#multi-backend" + ] + } + ], + "source": [ + "import torch\n", + "from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig\n", + "\n", + "model_name = \"TinyLlama/TinyLlama-1.1B-Chat-v1.0\"\n", + "\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.float16,\n", + ")\n", + "\n", + "tokenizer = AutoTokenizer.from_pretrained(model_name)\n", + "model = AutoModelForCausalLM.from_pretrained(\n", + " model_name,\n", + " device_map=\"auto\",\n", + " quantization_config=bnb_config\n", + ")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "51e0d14a-18c7-410f-9821-0eb00d3d1bbc", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Some parameters are on the meta device because they were offloaded to the disk and cpu.\n" + ] + } + ], + "source": [ + "from transformers import AutoTokenizer, AutoModelForCausalLM\n", + "\n", + "model_name = \"TinyLlama/TinyLlama-1.1B-Chat-v1.0\"\n", + "\n", + "tokenizer = AutoTokenizer.from_pretrained(model_name)\n", + "model = AutoModelForCausalLM.from_pretrained(\n", + " model_name,\n", + " device_map=\"auto\", # This will still use CPU if no GPU is found\n", + ")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "f4e4786e-e67c-4c0f-b169-6996a2966558", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Some parameters are on the meta device because they were offloaded to the disk and cpu.\n" + ] + } + ], + "source": [ + "model = AutoModelForCausalLM.from_pretrained(\n", + " model_name,\n", + " device_map=\"auto\",\n", + " torch_dtype=torch.float32 # or float16 if your CPU supports it\n", + ")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "dfd328ef-9362-426b-894e-923e70c7ace3", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Device set to: cpu\n" + ] + } + ], + "source": [ + "device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n", + "print(f\"Device set to: {device}\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "6743ec8e-8bd9-4a73-8786-fd71a6790d78", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "WARNING:tensorflow:From C:\\Users\\USER\\AppData\\Roaming\\Python\\Python312\\site-packages\\tf_keras\\src\\losses.py:2976: The name tf.losses.sparse_softmax_cross_entropy is deprecated. Please use tf.compat.v1.losses.sparse_softmax_cross_entropy instead.\n", + "\n" + ] + } + ], + "source": [ + "import json\n", + "import torch\n", + "from datasets import Dataset\n", + "from transformers import AutoTokenizer, AutoModelForCausalLM, TrainingArguments, Trainer, DataCollatorForLanguageModeling\n", + "from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "4252cc0c-62fe-4871-8095-ab07959b7884", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "WARNING:tensorflow:From C:\\Users\\USER\\AppData\\Roaming\\Python\\Python312\\site-packages\\tf_keras\\src\\losses.py:2976: The name tf.losses.sparse_softmax_cross_entropy is deprecated. Please use tf.compat.v1.losses.sparse_softmax_cross_entropy instead.\n", + "\n" + ] + } + ], + "source": [ + "import json\n", + "import torch\n", + "from datasets import Dataset\n", + "from transformers import AutoTokenizer, AutoModelForCausalLM, TrainingArguments, Trainer, DataCollatorForLanguageModeling\n", + "from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "7153b443-8059-42d1-96fa-699d0f19f9cf", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "dict_keys(['data'])\n" + ] + } + ], + "source": [ + "import json\n", + "\n", + "with open(\"cli_questions.json\") as f:\n", + " data = json.load(f)\n", + "\n", + "# Check the top-level structure\n", + "print(type(data)) # Should print \n", + "print(data.keys()) # See what keys are at the top\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "fbfa8025-233e-47c5-9044-146f95bb24eb", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Loaded 172 formatted examples\n", + "{'text': '### Question:\\nWhat is the intended use-case for git stash?\\n\\n### Answer:\\nGit stash is a convenience method to temporarily store your working changes. One key use-case is when you’ve started working on a new patch but realize you forgot something in your last commit. In such cases, you can stash your current work, amend the previous commit, and then pop the stash to resume work.\\n\\nExample:\\n```\\n# Stash current changes\\ngit stash save\\n\\n# Fix and amend the previous commit\\ngit add -u\\ngit commit --amend\\n\\n# Restore your stashed changes\\ngit stash pop\\n```\\n\\nWhile creating temporary branches is also a valid approach, stash is often faster for quick save-and-resume workflows.'}\n" + ] + } + ], + "source": [ + "import json\n", + "from datasets import Dataset\n", + "\n", + "# Load the JSON and extract the list\n", + "with open(\"cli_questions.json\") as f:\n", + " raw = json.load(f)\n", + "\n", + "qa_list = raw[\"data\"] # access the list inside the 'data' key\n", + "\n", + "# Format for instruction tuning\n", + "formatted_data = [\n", + " {\"text\": f\"### Question:\\n{item['question']}\\n\\n### Answer:\\n{item['answer']}\"}\n", + " for item in qa_list\n", + "]\n", + "\n", + "# Convert to Hugging Face dataset\n", + "dataset = Dataset.from_list(formatted_data)\n", + "\n", + "# Preview\n", + "print(f\"Loaded {len(dataset)} formatted examples\")\n", + "print(dataset[0])\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "893c412e-0f09-44fd-b6f8-fe3557a071aa", + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "f2ddab20eb19460689f560def5c8c924", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Map: 0%| | 0/172 [00:00 1\u001b[0m login(token\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mhf_...\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", + "\u001b[1;31mNameError\u001b[0m: name 'login' is not defined" + ] + } + ], + "source": [ + "login(token=\"hf_...\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "def2deab-147c-4445-8e62-96c397d72f12", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Notebook cleaned!\n" + ] + } + ], + "source": [ + "import nbformat\n", + "\n", + "# Load the notebook\n", + "nb_path = \"training.ipynb\"\n", + "with open(nb_path, \"r\", encoding=\"utf-8\") as f:\n", + " nb = nbformat.read(f, as_version=4)\n", + "\n", + "# Remove outputs and hidden tokens\n", + "for cell in nb.cells:\n", + " if \"outputs\" in cell:\n", + " cell[\"outputs\"] = []\n", + " if \"execution_count\" in cell:\n", + " cell[\"execution_count\"] = None\n", + " if cell[\"cell_type\"] == \"code\":\n", + " cell[\"source\"] = cell[\"source\"].replace(\"hf_\", \"REMOVED_TOKEN_\")\n", + "\n", + "# Save clean version\n", + "with open(nb_path, \"w\", encoding=\"utf-8\") as f:\n", + " nbformat.write(nb, f)\n", + "\n", + "print(\"Notebook cleaned!\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c825364f-4b54-4730-9b22-d32b4d030417", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.7" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}