{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "7653981e-5b39-45d7-ad23-aa18de28af12", "metadata": {}, "outputs": [], "source": [ "%cd .." ] }, { "cell_type": "code", "execution_count": null, "id": "5e995d5e-f8bd-411f-a78d-ba03edc60968", "metadata": {}, "outputs": [], "source": [ "base_model = \"unsloth/gemma-3-27b-it\"\n", "adapter_path = \"gemma-27b-thinker/checkpoint-2400\"\n", "output_path = \"output-merged\"\n", "\n", "def merge_lora(base_model, adapter_path, output_path, max_shard_size, precision):\n", " \"\"\"Merge LoRA adapter into base model and save the merged model.\"\"\"\n", " try:\n", " # Load LoRA adapter\n", " adapter_model = PeftModel.from_pretrained(base_model, adapter_path)\n", " \n", " # Merge LoRA weights and free memory\n", " merged_model = adapter_model.merge_and_unload()\n", " del adapter_model\n", " torch.cuda.empty_cache()\n", "\n", " # Convert model precision\n", " dtype = torch.bfloat16 if precision == \"bfloat16\" else torch.float16\n", " for param in merged_model.parameters():\n", " if param.dtype == torch.float32:\n", " param.data = param.data.to(dtype)\n", "\n", " # Save merged model\n", " os.makedirs(output_path, exist_ok=True)\n", " merged_model.save_pretrained(output_path, safe_serialization=True, max_shard_size=max_shard_size)\n", "\n", " # Save tokenizer\n", " tokenizer = AutoTokenizer.from_pretrained(base_model.name_or_path)\n", " tokenizer.save_pretrained(output_path)\n", "\n", " return True\n", "\n", " except Exception as e:\n", " print(f\"Merge failed: {e}\")\n", " return False\n" ] }, { "cell_type": "code", "execution_count": null, "id": "0d59c8fc-9db6-492c-ba98-0e87e728e929", "metadata": {}, "outputs": [], "source": [ "from unsloth.chat_templates import standardize_data_formats\n", "formatted_dataset = standardize_data_formats(formatted_dataset)" ] }, { "cell_type": "code", "execution_count": null, "id": "749a0361-a7f9-4d1a-967d-6f651f4eb1bb", "metadata": {}, "outputs": [], "source": [ "def apply_chat_template(examples):\n", " texts = tokenizer.apply_chat_template(examples[\"messages\"])\n", " return { \"text\" : texts }\n", "pass\n", "formatted_dataset = formatted_dataset.map(apply_chat_template, batched = True)" ] }, { "cell_type": "code", "execution_count": null, "id": "7bb95cac-5d93-4e0f-a124-c0ff48096411", "metadata": {}, "outputs": [], "source": [ "print(formatted_dataset[1])" ] }, { "cell_type": "code", "execution_count": 10, "id": "ed846b5d-b1f6-4197-8401-8797b3ac3508", "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'os' is not defined", "output_type": "error", "traceback": [ "\u001b[31m---------------------------------------------------------------------------\u001b[39m", "\u001b[31mNameError\u001b[39m Traceback (most recent call last)", "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[10]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[43mos\u001b[49m\n", "\u001b[31mNameError\u001b[39m: name 'os' is not defined" ] } ], "source": [ "!git clone --depth=1 https://github.com/ggerganov/llama.cpp\n", "%cd llama.cpp\n", "!cmake -Bbuild\n", "!cmake --build build -D...\n" ] }, { "cell_type": "code", "execution_count": null, "id": "f1fa7b18-c2b0-47a4-a46a-38bdbf5e4582", "metadata": {}, "outputs": [], "source": [ "%cd build\n", "!cpack -G DEB\n", "!dpkg -i *.deb" ] } ], "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.11.5" } }, "nbformat": 4, "nbformat_minor": 5 }