Flash attention in notebook
This commit is contained in:
@@ -1,21 +1,19 @@
|
|||||||
{
|
{
|
||||||
"cells": [
|
"cells": [
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "markdown",
|
||||||
"execution_count": 1,
|
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"source": [
|
||||||
{
|
"source /root/venvs/sia/bin/activate\n",
|
||||||
"name": "stdout",
|
"apt-get update && apt-get install -y cuda-toolkit\n",
|
||||||
"output_type": "stream",
|
"pip install flash-attn --no-build-isolation"
|
||||||
"text": [
|
|
||||||
"🦥 Unsloth: Will patch your computer to enable 2x faster free finetuning.\n",
|
|
||||||
"Unsloth: Failed to patch Gemma3ForConditionalGeneration.\n",
|
|
||||||
"🦥 Unsloth Zoo will now patch everything to make training faster!\n",
|
|
||||||
"INFO 04-23 16:42:57 [__init__.py:239] Automatically detected platform cuda.\n"
|
|
||||||
]
|
]
|
||||||
}
|
},
|
||||||
],
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# Unsloth should be imported before transformers to ensure all optimizations are applied.\n",
|
"# Unsloth should be imported before transformers to ensure all optimizations are applied.\n",
|
||||||
"from unsloth import FastLanguageModel"
|
"from unsloth import FastLanguageModel"
|
||||||
@@ -23,7 +21,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": null,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
@@ -35,126 +33,48 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": null,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"temperature = 0.6\n",
|
"temperature = 0.6\n",
|
||||||
"model_path = \"/root/models/notebook_merged_4bit\"\n",
|
"model_path = \"/root/models/current\"\n",
|
||||||
"xml_schema_text = Path(\"/root/sia/action_schema.xsd\").read_text()"
|
"xml_schema_text = Path(\"/root/sia/action_schema.xsd\").read_text()"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": null,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# Load tokenizer\n",
|
"# Load tokenizer\n",
|
||||||
"tokenizer = AutoTokenizer.from_pretrained(\n",
|
"tokenizer = AutoTokenizer.from_pretrained(\n",
|
||||||
" model_path,\n",
|
" model_path,\n",
|
||||||
|
" legacy=False,\n",
|
||||||
")"
|
")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 5,
|
"execution_count": null,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [],
|
||||||
{
|
|
||||||
"name": "stdout",
|
|
||||||
"output_type": "stream",
|
|
||||||
"text": [
|
|
||||||
"==((====))== Unsloth 2025.3.19: Fast Qwen2 patching. Transformers: 4.51.3. vLLM: 0.8.2.\n",
|
|
||||||
" \\\\ /| NVIDIA RTX 6000 Ada Generation. Num GPUs = 1. Max memory: 47.5 GB. Platform: Linux.\n",
|
|
||||||
"O^O/ \\_/ \\ Torch: 2.6.0+cu124. CUDA: 8.9. CUDA Toolkit: 12.4. Triton: 3.2.0\n",
|
|
||||||
"\\ / Bfloat16 = TRUE. FA [Xformers = 0.0.29.post2. FA2 = False]\n",
|
|
||||||
" \"-____-\" Free license: http://github.com/unslothai/unsloth\n",
|
|
||||||
"Unsloth: Fast downloading is enabled - ignore downloading bars which are red colored!\n"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "stderr",
|
|
||||||
"output_type": "stream",
|
|
||||||
"text": [
|
|
||||||
"Sliding Window Attention is enabled but not implemented for `eager`; unexpected results may be encountered.\n"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "stdout",
|
|
||||||
"output_type": "stream",
|
|
||||||
"text": [
|
|
||||||
"validate_env\n",
|
|
||||||
"device_map sequential\n",
|
|
||||||
"validate_env\n",
|
|
||||||
"device_map OrderedDict([('', 0)])\n"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"data": {
|
|
||||||
"application/vnd.jupyter.widget-view+json": {
|
|
||||||
"model_id": "4f302eb9995d47fab2aa6339aa00a8d8",
|
|
||||||
"version_major": 2,
|
|
||||||
"version_minor": 0
|
|
||||||
},
|
|
||||||
"text/plain": [
|
|
||||||
"Loading checkpoint shards: 0%| | 0/4 [00:00<?, ?it/s]"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"metadata": {},
|
|
||||||
"output_type": "display_data"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"source": [
|
"source": [
|
||||||
"# Load model\n",
|
"# Load model\n",
|
||||||
"model, _returned_tokenizer = FastLanguageModel.from_pretrained(\n",
|
"model, _returned_tokenizer = FastLanguageModel.from_pretrained(\n",
|
||||||
" model_path,\n",
|
" model_path,\n",
|
||||||
" gpu_memory_utilization = 0.5, # Reduce if out of memory\n",
|
" gpu_memory_utilization = 0.5, # Reduce if out of memory\n",
|
||||||
|
" load_in_4bit=True,\n",
|
||||||
|
" attn_implementation=\"flash_attention_2\",\n",
|
||||||
")"
|
")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 6,
|
"execution_count": null,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [],
|
||||||
{
|
|
||||||
"data": {
|
|
||||||
"text/plain": [
|
|
||||||
"Qwen2ForCausalLM(\n",
|
|
||||||
" (model): Qwen2Model(\n",
|
|
||||||
" (embed_tokens): Embedding(152064, 5120, padding_idx=151654)\n",
|
|
||||||
" (layers): ModuleList(\n",
|
|
||||||
" (0-63): 64 x Qwen2DecoderLayer(\n",
|
|
||||||
" (self_attn): Qwen2Attention(\n",
|
|
||||||
" (q_proj): Linear4bit(in_features=5120, out_features=5120, bias=True)\n",
|
|
||||||
" (k_proj): Linear4bit(in_features=5120, out_features=1024, bias=True)\n",
|
|
||||||
" (v_proj): Linear4bit(in_features=5120, out_features=1024, bias=True)\n",
|
|
||||||
" (o_proj): Linear4bit(in_features=5120, out_features=5120, bias=False)\n",
|
|
||||||
" (rotary_emb): LlamaRotaryEmbedding()\n",
|
|
||||||
" )\n",
|
|
||||||
" (mlp): Qwen2MLP(\n",
|
|
||||||
" (gate_proj): Linear4bit(in_features=5120, out_features=27648, bias=False)\n",
|
|
||||||
" (up_proj): Linear4bit(in_features=5120, out_features=27648, bias=False)\n",
|
|
||||||
" (down_proj): Linear4bit(in_features=27648, out_features=5120, bias=False)\n",
|
|
||||||
" (act_fn): SiLU()\n",
|
|
||||||
" )\n",
|
|
||||||
" (input_layernorm): Qwen2RMSNorm((5120,), eps=1e-05)\n",
|
|
||||||
" (post_attention_layernorm): Qwen2RMSNorm((5120,), eps=1e-05)\n",
|
|
||||||
" )\n",
|
|
||||||
" )\n",
|
|
||||||
" (norm): Qwen2RMSNorm((5120,), eps=1e-05)\n",
|
|
||||||
" (rotary_emb): LlamaRotaryEmbedding()\n",
|
|
||||||
" )\n",
|
|
||||||
" (lm_head): Linear(in_features=5120, out_features=152064, bias=False)\n",
|
|
||||||
")"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"execution_count": 6,
|
|
||||||
"metadata": {},
|
|
||||||
"output_type": "execute_result"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"source": [
|
"source": [
|
||||||
"# enable unsloth optimizations\n",
|
"# enable unsloth optimizations\n",
|
||||||
"FastLanguageModel.for_inference(model)"
|
"FastLanguageModel.for_inference(model)"
|
||||||
@@ -162,30 +82,24 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 7,
|
"execution_count": null,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [],
|
||||||
{
|
|
||||||
"name": "stderr",
|
|
||||||
"output_type": "stream",
|
|
||||||
"text": [
|
|
||||||
"Device set to use cuda:0\n"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"source": [
|
"source": [
|
||||||
|
"import torch\n",
|
||||||
"# Create inference pipeline with memory-efficient settings\n",
|
"# Create inference pipeline with memory-efficient settings\n",
|
||||||
"pipeline = pipeline(\n",
|
"pipeline = pipeline(\n",
|
||||||
" \"text-generation\",\n",
|
" \"text-generation\",\n",
|
||||||
" model=model,\n",
|
" model=model,\n",
|
||||||
" tokenizer=tokenizer,\n",
|
" tokenizer=tokenizer,\n",
|
||||||
" return_full_text=False,\n",
|
" return_full_text=False,\n",
|
||||||
|
" torch_dtype=torch.float16,\n",
|
||||||
")"
|
")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 8,
|
"execution_count": null,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
@@ -194,7 +108,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 9,
|
"execution_count": null,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
@@ -207,7 +121,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 10,
|
"execution_count": null,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
@@ -220,7 +134,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 11,
|
"execution_count": null,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
@@ -232,7 +146,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 12,
|
"execution_count": null,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
@@ -247,7 +161,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 13,
|
"execution_count": null,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
@@ -256,7 +170,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 14,
|
"execution_count": null,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
@@ -270,17 +184,9 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 15,
|
"execution_count": null,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [],
|
||||||
{
|
|
||||||
"name": "stdout",
|
|
||||||
"output_type": "stream",
|
|
||||||
"text": [
|
|
||||||
"<write_stdout>Hi, I'm an AI assistant. I don't have feelings, but I'm here to help you. How can I assist you today?</write_stdout><|im_end|>"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"source": [
|
"source": [
|
||||||
"for text in streamer:\n",
|
"for text in streamer:\n",
|
||||||
" print(text, end=\"\")"
|
" print(text, end=\"\")"
|
||||||
@@ -288,7 +194,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 16,
|
"execution_count": null,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
@@ -301,7 +207,7 @@
|
|||||||
"kernelspec": {
|
"kernelspec": {
|
||||||
"display_name": "sia",
|
"display_name": "sia",
|
||||||
"language": "python",
|
"language": "python",
|
||||||
"name": "python3"
|
"name": "sia"
|
||||||
},
|
},
|
||||||
"language_info": {
|
"language_info": {
|
||||||
"codemirror_mode": {
|
"codemirror_mode": {
|
||||||
|
|||||||
Reference in New Issue
Block a user