445 lines
36 KiB
Plaintext
445 lines
36 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"🦥 Unsloth: Will patch your computer to enable 2x faster free finetuning.\n",
|
|
"🦥 Unsloth Zoo will now patch everything to make training faster!\n",
|
|
"INFO 03-30 08:37:14 [__init__.py:239] Automatically detected platform cuda.\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# Unsloth should be imported before transformers to ensure all optimizations are applied.\n",
|
|
"from unsloth import FastLanguageModel, is_bfloat16_supported"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from dataclasses import dataclass\n",
|
|
"from pathlib import Path\n",
|
|
"from transformers import TrainingArguments\n",
|
|
"from trl import SFTTrainer, DataCollatorForCompletionOnlyLM\n",
|
|
"from typing import Optional, List\n",
|
|
"import argparse\n",
|
|
"import os"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from train import qwq"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"args = qwq.Args([\"--output-dir\", \"/root/models/notebook\"])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Validating 20 XML files...\n",
|
|
"file: /root/sia/training/clean_start/iteration_20250116_134549_655.xml\n",
|
|
"file: /root/sia/training/clean_start/iteration_20250116_134555_680.xml\n",
|
|
"file: /root/sia/training/delete_indicated_entries/iteration_20250116_141241_092.xml\n",
|
|
"file: /root/sia/training/delete_indicated_entries/iteration_20250116_141252_317.xml\n",
|
|
"file: /root/sia/training/delete_indicated_entries/iteration_20250116_141302_940.xml\n",
|
|
"file: /root/sia/training/delete_indicated_entries/iteration_20250116_141329_886.xml\n",
|
|
"file: /root/sia/training/delete_indicated_entries/iteration_20250116_141343_416.xml\n",
|
|
"file: /root/sia/training/delete_indicated_entries/iteration_20250116_141357_412.xml\n",
|
|
"file: /root/sia/training/delete_indicated_entries/iteration_20250116_141410_965.xml\n",
|
|
"file: /root/sia/training/delete_indicated_entries/iteration_20250116_141428_204.xml\n",
|
|
"file: /root/sia/training/delete_indicated_entries/iteration_20250116_141441_443.xml\n",
|
|
"file: /root/sia/training/delete_indicated_entries/iteration_20250116_141447_231.xml\n",
|
|
"file: /root/sia/training/delete_indicated_entries/iteration_20250116_141454_509.xml\n",
|
|
"file: /root/sia/training/delete_indicated_entries/iteration_20250116_141458_495.xml\n",
|
|
"file: /root/sia/training/delete_indicated_entries/iteration_20250116_141503_889.xml\n",
|
|
"file: /root/sia/training/delete_indicated_entries/iteration_20250116_141516_718.xml\n",
|
|
"file: /root/sia/training/delete_indicated_entries/iteration_20250116_141533_231.xml\n",
|
|
"file: /root/sia/training/delete_indicated_entries/iteration_20250116_141603_549.xml\n",
|
|
"file: /root/sia/training/delete_indicated_entries/iteration_20250116_141633_083.xml\n",
|
|
"file: /root/sia/training/list_entries_to_delete/iteration_20250116_141227_271.xml\n",
|
|
"Validation complete. Found 20 valid files.\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"dataset = qwq.Dataset(args.config_path)\n",
|
|
"dataset.validate()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"max_seq_length = 2048 # Can increase for longer reasoning traces\n",
|
|
"lora_rank = 64 # Larger rank = smarter, but slower"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 7,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"==((====))== Unsloth 2025.3.19: Fast Qwen2 patching. Transformers: 4.50.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",
|
|
"Unsloth: vLLM loading unsloth/qwq-32b-unsloth-bnb-4bit with actual GPU utilization = 49.51%\n",
|
|
"Unsloth: Your GPU has CUDA compute capability 8.9 with VRAM = 47.5 GB.\n",
|
|
"Unsloth: Using conservativeness = 1.0. Chunked prefill tokens = 2048. Num Sequences = 128.\n",
|
|
"Unsloth: vLLM's KV Cache can use up to 1.46 GB. Also swap space = 6 GB.\n",
|
|
"INFO 03-30 08:37:24 [config.py:585] This model supports multiple tasks: {'score', 'classify', 'reward', 'generate', 'embed'}. Defaulting to 'generate'.\n",
|
|
"WARNING 03-30 08:37:24 [arg_utils.py:1854] --quantization bitsandbytes is not supported by the V1 Engine. Falling back to V0. \n",
|
|
"Unsloth: vLLM Bitsandbytes config using kwargs = {'load_in_8bit': False, 'load_in_4bit': True, 'bnb_4bit_compute_dtype': 'bfloat16', 'bnb_4bit_quant_storage': 'uint8', 'bnb_4bit_quant_type': 'nf4', 'bnb_4bit_use_double_quant': True, 'llm_int8_enable_fp32_cpu_offload': False, 'llm_int8_has_fp16_weight': False, 'llm_int8_skip_modules': ['lm_head', 'multi_modal_projector', 'merger', 'modality_projection', 'model.layers.4.mlp', 'model.layers.0.mlp', 'model.layers.60.mlp', 'model.layers.62.mlp', 'model.layers.5.mlp', 'model.layers.43.self_attn'], 'llm_int8_threshold': 6.0}\n",
|
|
"INFO 03-30 08:37:24 [llm_engine.py:241] Initializing a V0 LLM engine (v0.8.2) with config: model='unsloth/qwq-32b-unsloth-bnb-4bit', speculative_config=None, tokenizer='unsloth/qwq-32b-unsloth-bnb-4bit', skip_tokenizer_init=False, tokenizer_mode=auto, revision=None, override_neuron_config=None, tokenizer_revision=None, trust_remote_code=False, dtype=torch.bfloat16, max_seq_len=2048, download_dir=None, load_format=bitsandbytes, tensor_parallel_size=1, pipeline_parallel_size=1, disable_custom_all_reduce=False, quantization=bitsandbytes, enforce_eager=False, kv_cache_dtype=auto, device_config=cuda:0, decoding_config=DecodingConfig(guided_decoding_backend='xgrammar', reasoning_backend=None), observability_config=ObservabilityConfig(show_hidden_metrics=False, otlp_traces_endpoint=None, collect_model_forward_time=False, collect_model_execute_time=False), seed=0, served_model_name=unsloth/qwq-32b-unsloth-bnb-4bit, num_scheduler_steps=1, multi_step_stream_outputs=True, enable_prefix_caching=True, chunked_prefill_enabled=False, use_async_output_proc=True, disable_mm_preprocessor_cache=False, mm_processor_kwargs=None, pooler_config=None, compilation_config={\"level\":0,\"splitting_ops\":[],\"compile_sizes\":[],\"cudagraph_capture_sizes\":[128,120,112,104,96,88,80,72,64,56,48,40,32,24,16,8,4,2,1],\"max_capture_size\":128}, use_cached_outputs=False, \n",
|
|
"INFO 03-30 08:37:24 [cuda.py:291] Using Flash Attention backend.\n",
|
|
"INFO 03-30 08:37:25 [parallel_state.py:954] rank 0 in world size 1 is assigned as DP rank 0, PP rank 0, TP rank 0\n",
|
|
"INFO 03-30 08:37:25 [model_runner.py:1110] Starting to load model unsloth/qwq-32b-unsloth-bnb-4bit...\n",
|
|
"INFO 03-30 08:37:25 [loader.py:1155] Loading weights with BitsAndBytes quantization. May take a while ...\n",
|
|
"INFO 03-30 08:37:25 [weight_utils.py:265] Using model weights format ['*.safetensors']\n"
|
|
]
|
|
},
|
|
{
|
|
"data": {
|
|
"application/vnd.jupyter.widget-view+json": {
|
|
"model_id": "198d11b20e02412cb2833c5042d2af97",
|
|
"version_major": 2,
|
|
"version_minor": 0
|
|
},
|
|
"text/plain": [
|
|
"Loading safetensors checkpoint shards: 0% Completed | 0/5 [00:00<?, ?it/s]\n"
|
|
]
|
|
},
|
|
"metadata": {},
|
|
"output_type": "display_data"
|
|
},
|
|
{
|
|
"data": {
|
|
"application/vnd.jupyter.widget-view+json": {
|
|
"model_id": "d22e249f0ff84b97bbf8ed38cd66adda",
|
|
"version_major": 2,
|
|
"version_minor": 0
|
|
},
|
|
"text/plain": [
|
|
"Loading safetensors checkpoint shards: 0% Completed | 0/5 [00:00<?, ?it/s]\n"
|
|
]
|
|
},
|
|
"metadata": {},
|
|
"output_type": "display_data"
|
|
},
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"INFO 03-30 08:37:30 [punica_selector.py:18] Using PunicaWrapperGPU.\n",
|
|
"INFO 03-30 08:37:31 [model_runner.py:1146] Model loading took 22.0157 GB and 5.861333 seconds\n",
|
|
"INFO 03-30 08:37:34 [worker.py:267] Memory profiling takes 2.57 seconds\n",
|
|
"INFO 03-30 08:37:34 [worker.py:267] the current vLLM instance can use total_gpu_memory (47.50GiB) x gpu_memory_utilization (0.50) = 23.52GiB\n",
|
|
"INFO 03-30 08:37:34 [worker.py:267] model weights take 22.02GiB; non_torch_memory takes 0.08GiB; PyTorch activation peak memory takes 0.72GiB; the rest of the memory reserved for KV Cache is 0.70GiB.\n",
|
|
"INFO 03-30 08:37:34 [executor_base.py:111] # cuda blocks: 180, # CPU blocks: 1536\n",
|
|
"INFO 03-30 08:37:34 [executor_base.py:116] Maximum concurrency for 2048 tokens per request: 1.41x\n",
|
|
"INFO 03-30 08:37:38 [model_runner.py:1442] Capturing cudagraphs for decoding. This may lead to unexpected consequences if the model is not static. To run the model in eager mode, set 'enforce_eager=True' or use '--enforce-eager' in the CLI. If out-of-memory error occurs during cudagraph capture, consider decreasing `gpu_memory_utilization` or switching to eager mode. You can also reduce the `max_num_seqs` as needed to decrease memory usage.\n"
|
|
]
|
|
},
|
|
{
|
|
"name": "stderr",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Capturing CUDA graph shapes: 100%|██████████| 19/19 [00:18<00:00, 1.01it/s]"
|
|
]
|
|
},
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"INFO 03-30 08:37:57 [model_runner.py:1570] Graph capturing finished in 19 secs, took 0.96 GiB\n",
|
|
"INFO 03-30 08:37:57 [llm_engine.py:447] init engine (profile, create kv cache, warmup model) took 26.54 seconds\n"
|
|
]
|
|
},
|
|
{
|
|
"name": "stderr",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\n",
|
|
"Sliding Window Attention is enabled but not implemented for `eager`; unexpected results may be encountered.\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"model, tokenizer = FastLanguageModel.from_pretrained(\n",
|
|
" model_name = args.base_model,\n",
|
|
" max_seq_length = max_seq_length,\n",
|
|
" load_in_4bit = True, # False for LoRA 16bit\n",
|
|
" fast_inference = True, # Enable vLLM fast inference\n",
|
|
" max_lora_rank = lora_rank,\n",
|
|
" gpu_memory_utilization = 0.5, # Reduce if out of memory\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 8,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stderr",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Unsloth 2025.3.19 patched 64 layers with 64 QKV layers, 64 O layers and 64 MLP layers.\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"model = FastLanguageModel.get_peft_model(\n",
|
|
" model,\n",
|
|
" r = lora_rank, # Choose any number > 0 ! Suggested 8, 16, 32, 64, 128\n",
|
|
" target_modules = [\n",
|
|
" \"q_proj\", \"k_proj\", \"v_proj\", \"o_proj\",\n",
|
|
" \"gate_proj\", \"up_proj\", \"down_proj\",\n",
|
|
" ], # Remove QKVO if out of memory\n",
|
|
" lora_alpha = lora_rank,\n",
|
|
" lora_dropout = 0, # Supports any, but = 0 is optimized\n",
|
|
" bias = \"none\", # Supports any, but = \"none\" is optimized\n",
|
|
" use_gradient_checkpointing = \"unsloth\", # True or \"unsloth\" for very long context\n",
|
|
" random_state = 3407,\n",
|
|
" use_rslora = False, # We support rank stabilized LoRA\n",
|
|
" loftq_config = None, # And LoftQ\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 9,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"{'messages': [{'role': 'system',\n",
|
|
" 'content': 'You are SIA, the Self Improving Agent.\\nYour goal is to autonomously complete complex tasks by writing and executing scripts.\\nYou can solve any problem.\\n\\nEach iteration, the context is updated with the result of your previous actions.\\nYou modify the context by issuing a command using XML.\\nParameters and scripts may be long and complex.\\nUse correct XML escaping or CDATA sections.\\nIt is very important that you always respond with one action adhering to the XML schema!\\nDo not respond with anything else after the first action.\\n\\nThe next iteration starts when all scripts have finished.\\nThese are repeat scripts from the previous iterations and possibly one new single-shot script.\\nAvoid blocking scripts so you can iterate quickly.\\n\\n# Context\\n\\nThe context has a limited length.\\nThe `context_usage` attribute of the main context element indicates how much of the context is used in %.\\nThis should never reach 100%!\\nUse the delete action to remove unnecessary items from the context.\\nBut keep interesting information.\\nYou can\\'t learn from your mistakes if you delete them before fixing.\\n\\n# Linux Environment\\n\\nYou have access to the Linux environment that runs the SAI process.\\nIn this environment you can run scripts by issuing the right actions.\\nScripts and their output appear in the context.\\nYou can use a script for starting a detached process that runs in the background.\\nAll processes can be managed by the usual Linux tools.\\nThe scripts defined in the script actions all run in a `bash` shell.\\nYou are logged in as root.\\n\\n# File system\\n\\nThe file system helps you structure your thoughts.\\nBecause of the limited context window you can\\'t remember everything you\\'ve done and learned.\\nWriting and updating files will help you in:\\n- remembering tasks\\n- planning solution strategies\\n- keeping track of progress\\n- managing overview of large projects\\n- using tools you\\'ve created\\n\\nIt is important to bring a lot of structure to the files and directories.\\nThis will help you find the right info when needed.\\nWhen solving a problem, make sure to load the relevant info in context before planning.\\nYou can load a single file with a `cat` command executed in a `single` action.\\n`head`, `tail`, `grep`, `find`, `tree`, ... all have their uses.\\n\\nFor code source files it may be interesting to add line numbers.\\nMore advanced scripts can be used, for instance to extract documentation from source files.\\nThis helps you to know how to use a file without loading all the code in context too.\\n\\nIf it isn\\'t clear what you should do next, check the filesystem for notes that may guide you!\\n\\n# Iterative Problem Solving\\n\\nTake small steps and verify your work.\\nCreate unit tests for all your work so you can do regression tests after each step.\\n\\nKeep notes of when you started on a subtask and which solutions you tried.\\nThis way you avoid repeating yourself and decide when to look for an alternative approach to a problem.\\n\\nVersion control tools help remember steps taken, solutions tried and files modified.\\nMake extensive use of `git`!\\n\\nYour most important tool is the reasoning action.\\nYou should reason about everything you\\'ll do before issuing a command in the next iteration!\\nInspect your previous actions in detail.\\nIf something didn\\'t work, try to understand why and don\\'t repeat the same mistake.\\nDon\\'t delete mistakes until you understand them.\\n\\nIf you notice parse_error entries in the context you have made a mistake.\\nReason about the error before trying again.\\n\\n# User interaction\\n\\nYou are always working for a user.\\nGet to know them and make notes about what you learn from them.\\nBe a helpful assistant to the user.\\nOpen the relevant user notes when you interact with them.\\n\\nThe main way to communicate is using standard io.\\nThe user may want you to set up alternative communication methods.\\nUse scripts and background processes to do so.\\n\\nThe user may take some time to respond or may forget to respond.\\nKeep detailed notes of your interactions and your expectations regarding time!\\nAvoid overflowing the user with many messages.\\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\\n<!--\\n Always answer with a single element.\\n-->\\n<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" elementFormDefault=\"qualified\">\\n\\n <!--\\n Delete command removes an entry from the context by its ID.\\n Use it to remove unnecessary items and stop background processes.\\n When you delete something, it is gone.\\n Make sure all important info is stored in files.\\n Example:\\n <delete id=\"1234567890\"/>\\n -->\\n <xs:element name=\"delete\">\\n <xs:complexType>\\n <xs:attribute name=\"id\" type=\"xs:string\" use=\"required\"/>\\n </xs:complexType>\\n </xs:element>\\n\\n <!--\\n Stop command terminates the agent gracefully.\\n For the main SIA instance this will trigger an update and restart.\\n For sub-instances this is the correct way to stop after all tasks are complete.\\n Example:\\n <stop id=\"1234567890\"/>\\n -->\\n <xs:element name=\"stop\">\\n <xs:complexType/>\\n </xs:element>\\n\\n <!--\\n Single script that runs once and completes.\\n Output is stored in context until explicitly deleted.\\n Used for one-time operations like file manipulation.\\n Single scripts are limited to 1024 characters and 1 second timeout by default.\\n These limits can be changed with attributes.\\n Example:\\n <single>\\n ls /\\n </single>\\n -->\\n <xs:element name=\"single\">\\n <xs:complexType mixed=\"true\">\\n <xs:sequence>\\n <xs:any minOccurs=\"0\" maxOccurs=\"unbounded\" processContents=\"skip\"/>\\n </xs:sequence>\\n <xs:attribute name=\"timeout\" type=\"xs:float\" use=\"optional\"/>\\n <xs:attribute name=\"limit\" type=\"xs:integer\" use=\"optional\"/>\\n </xs:complexType>\\n </xs:element>\\n\\n <!--\\n Repeat script runs each time the context is generated.\\n After a command is issued, all repeat scripts in context are run again.\\n Useful for monitoring changing files or viewing results immediately after changing a file.\\n Repeat scripts should execute quickly to avoid blocking the agent.\\n Repeat scripts are limited to 1024 characters and 1 second timeout by default.\\n These limits can be changed with attributes.\\n Example:\\n <repeat>\\n ls /\\n </repeat>\\n -->\\n <xs:element name=\"repeat\">\\n <xs:complexType mixed=\"true\">\\n <xs:sequence>\\n <xs:any minOccurs=\"0\" maxOccurs=\"unbounded\" processContents=\"skip\"/>\\n </xs:sequence>\\n <xs:attribute name=\"timeout\" type=\"xs:float\" use=\"optional\"/>\\n <xs:attribute name=\"limit\" type=\"xs:integer\" use=\"optional\"/>\\n </xs:complexType>\\n </xs:element>\\n\\n <!--\\n As an agent it is important to reason about your actions and their results.\\n In a reasoning action you can write freeform text.\\n This is also stored in context until deleted.\\n Example:\\n <reasoning>\\n I should explore the file system for interesting files.\\n </reasoning>\\n -->\\n <xs:element name=\"reasoning\">\\n <xs:complexType mixed=\"true\">\\n <xs:sequence>\\n <xs:any minOccurs=\"0\" maxOccurs=\"unbounded\" processContents=\"skip\"/>\\n </xs:sequence>\\n </xs:complexType>\\n </xs:element>\\n\\n <!--\\n Read all available text on stdin and store it in context.\\n Do this only if the context indicates there is data in the stdin buffer.\\n Example:\\n <read_stdin/>\\n -->\\n <xs:element name=\"read_stdin\">\\n <xs:complexType/>\\n </xs:element>\\n\\n <!--\\n Write to stdout.\\n This is your main way of contacting the user.\\n Make sure you have properly reasoned about what to say and if it is necessary before issuing a write_stdout command.\\n Example:\\n <write_stdout>\\n Hello world!\\n </write_stdout>\\n -->\\n <xs:element name=\"write_stdout\">\\n <xs:complexType mixed=\"true\">\\n <xs:sequence>\\n <xs:any minOccurs=\"0\" maxOccurs=\"unbounded\" processContents=\"skip\"/>\\n </xs:sequence>\\n </xs:complexType>\\n </xs:element>\\n</xs:schema>\\n'},\n",
|
|
" {'role': 'user',\n",
|
|
" 'content': '<context\\n context=\"14.46%\"\\n disk_total=\"1081101176832\"\\n disk_used=\"42638553088\"\\n memory_total=\"16630435840\"\\n memory_used=\"1089228800\"\\n stdin=\"0\"\\n time=\"2025-01-16T14:13:02Z\">\\n <single exit_code=\"0\" id=\"20250116_134644_445\">\\n <![CDATA[\\n ls /root\\n]]>\\n <stdout>\\n <![CDATA[iterations\\nmodel\\nsia\\n]]>\\n </stdout>\\n <stderr/>\\n </single>\\n <reasoning id=\"20250116_134729_674\">\\n <![CDATA[\\n The /root directory contains three items: iterations, model, and sia.\\n I should explore the contents of these directories to understand their purpose and relevance.\\n I will start by listing the contents of the /root/iterations directory using the `ls /root/iterations` command.\\n]]>\\n </reasoning>\\n <single exit_code=\"0\" id=\"20250116_134736_148\">\\n <![CDATA[\\n ls /root/iterations\\n]]>\\n <stdout>\\n <![CDATA[iteration_20250116_134549_655.xml\\niteration_20250116_134555_680.xml\\niteration_20250116_134639_663.xml\\niteration_20250116_134644_445.xml\\niteration_20250116_134729_674.xml\\niteration_20250116_134736_148.xml\\n]]>\\n </stdout>\\n <stderr/>\\n </single>\\n <reasoning id=\"20250116_135016_476\">\\n <![CDATA[\\n There are not tasks in the root directory and the iterations directory only contains what I\\'just done.\\n This is a clean start of SIA.\\n I should look for procedures to follow in the `/root/sia/procedures` directory.\\n]]>\\n </reasoning>\\n <single exit_code=\"0\" id=\"20250116_135024_642\">\\n <![CDATA[\\n ls /root/sia/procedures\\n]]>\\n <stdout>\\n <![CDATA[README.md\\nuser_communication\\nusing_procedures\\n]]>\\n </stdout>\\n <stderr/>\\n </single>\\n <reasoning id=\"20250116_135232_795\">\\n <![CDATA[\\n There are no specific procedures to follow.\\n I will ask the user what I should do.\\n I\\'ll load the `using_procedures` and `user_communication` procedures.\\n]]>\\n </reasoning>\\n <single exit_code=\"0\" id=\"20250116_135253_387\">\\n <![CDATA[\\n ls -la /root/sia/procedures/using_procedures\\n ls -la /root/sia/procedures/user_communication\\n]]>\\n <stdout>\\n <![CDATA[total 16\\ndrwxr-xr-x 2 root root 4096 Jan 16 13:44 .\\ndrwxr-xr-x 4 root root 4096 Jan 8 08:47 ..\\n-rwxr-xr-x 1 root root 1758 Jan 8 21:40 procedure.md\\n-rwxr-xr-x 1 root root 1831 Jan 8 21:03 reasoning.md\\ntotal 20\\ndrwxr-xr-x 2 root root 4096 Jan 16 13:44 .\\ndrwxr-xr-x 4 root root 4096 Jan 8 08:47 ..\\n-rwxr-xr-x 1 root root 244 Jan 7 12:18 description.md\\n-rwxr-xr-x 1 root root 3643 Jan 8 21:49 procedure.md\\n-rwxr-xr-x 1 root root 1035 Jan 7 12:23 reasoning.md\\n]]>\\n </stdout>\\n <stderr/>\\n </single>\\n <single exit_code=\"0\" id=\"20250116_135343_982\" limit=\"1758\">\\n <![CDATA[\\n cat /root/sia/procedures/using_procedures/procedure.md\\n]]>\\n <stdout>\\n <![CDATA[# Using Procedures\\n\\n## Core Guidelines\\n\\nWhen following a procedure\\'s flowchart:\\n\\n1. Start with reasoning:\\n - State current position in flowchart explicitly\\n - Focus on immediate next step, don\\'t go ahead of the chart\\n - Evaluate current context and task state\\n - Often state entry id\\'s that can be removed and explain why to avoid mistakes\\n - State expected output and runtime for scripts\\n\\n2. After script execution:\\n - Analyze the actual output and compare with the expected output\\n - Reevaluate situation based on results\\n - Return to flowchart for next step\\n - Consider if current path is still appropriate\\n\\n3. When a procedure fails:\\n - Create a task explaining the issue and the need to fix it\\n - Add timestamps and id\\'s of relevant entries\\n\\n## Attention Management\\n\\nLLMs pay more attention to recently mentioned information.\\nReasoning entries should mention what needs attention now.\\n\\nTo maintain focus:\\n1. State current flowchart position in each reasoning\\n2. Quote relevant parts of important entries\\n3. Reference specific entry IDs when using their information\\n4. Periodically remind about ongoing tasks or future needs\\n5. Clean up entries that aren\\'t needed for current step\\n\\nExample of good attention management:\\n```xml\\n<reasoning>\\nAt node \"evaluate_test_results\". Entry 45f3d2 shows failed test: \"Error: Connection timeout\". \\nWill need to check system logs soon (noted in /tasks/reminders.txt, check at 14:00).\\nFirst focusing on this error.\\n</reasoning>\\n```\\n\\n## Reasons to Switch Procedures\\n\\nCommon triggers:\\n - Data available on stdin\\n - Time matching scheduled task\\n - Error conditions in script output\\n - Resource constraints detected\\n - User input needed]]>\\n </stdout>\\n <stderr/>\\n </single>\\n <single exit_code=\"0\" id=\"20250116_135409_816\" limit=\"3643\">\\n <![CDATA[\\n cat /root/sia/procedures/user_communication/procedure.md\\n]]>\\n <stdout>\\n <![CDATA[# User Communication\\n\\n## Prerequisites\\n- User information is stored in the /user directory\\n- Tasks and their progress are documented in the /tasks directory\\n\\n## Flow\\n```mermaid\\nflowchart TD\\n Start([Start])\\n LoadUserBasic[The /user/basic.md file should contain info to identify and address the user properly<br>Also load the last 10 messages from /user/conversation_history/ ]\\n PrepareForDraft{Have everything needed for drafting a message?}\\n DraftMessage[Draft message in reasoning entry]\\n ReadInput[Read input from standard input]\\n ReasonCleanContext[List id\\'s of entries that are no longer needed<br>Explain for each entry why it is no longer needed]\\n DeleteEntries[Remove the entries that are no longer needed<br>End by deleting the ReasonCleanContext entry]\\n AddHistoryUser[Add the message to the /user/conversation_history/ directory<br>The filename is the id of the stdin entry with .user extension]\\n LoadTask[Look for the task in the /tasks directory and load relevant files]\\n LoadUserDetails[Look in the /user directory for relevant files]\\n EstimateScript[Draft the script in a reasoning block and estimate its runtime and output length]\\n ScriptAcceptable{Does the draft script make sense and are the estimations short enough to not hinder the conversation?}\\n RunScript[Run the script, make sure to set appropriate timeout and output limits]\\n ReviewDraft{Is the message well structured and free of logical errors?}\\n SendMessage[Send the message using standard output]\\n AddHistoryAgent[Add the message to the /user/conversation_history/ directory<br>The filename is the id of the stdout entry with .agent extension]\\n ReasonResponse[Is the conversation ongoing?<br>How long is the user expected to take to respond?]\\n NeedAwaitResponse{Is it likely to get a response within a minute?}\\n BusyWait[Wait 1 second for the first busy wait, double the time each iteration until a response is received<br>Make sure to set the timout]\\n\\n End([Clean the context])\\n\\n Start --> LoadUserBasic\\n LoadUserBasic --> PrepareForDraft\\n\\n PrepareForDraft -->|Got all needed info| DraftMessage\\n PrepareForDraft -->|Getting the required info would slow the conversation| DraftMessage\\n PrepareForDraft -->|Input available on stdin| ReadInput\\n PrepareForDraft -->|Context usage more than 50%| ReasonCleanContext\\n PrepareForDraft -->|Task mentioned but not loaded| LoadTask\\n PrepareForDraft -->|Personal or social info mentioned but not loaded| LoadUserDetails\\n PrepareForDraft -->|Calculations, system info or other numerical values that can be scripted are mentioned| EstimateScript\\n\\n ReasonCleanContext --> DeleteEntries\\n DeleteEntries --> PrepareForDraft\\n\\n ReadInput --> AddHistoryUser\\n AddHistoryUser --> PrepareForDraft\\n\\n LoadTask --> PrepareForDraft\\n LoadUserDetails --> PrepareForDraft\\n\\n EstimateScript --> ScriptAcceptable\\n ScriptAcceptable -->|Acceptable| RunScript\\n ScriptAcceptable -->|Not acceptable| PrepareForDraft\\n RunScript --> PrepareForDraft\\n \\n DraftMessage --> ReviewDraft{Is this really what I want to say?}\\n ReviewDraft -->|Rewrite better| DraftMessage\\n ReviewDraft -->|Good message| SendMessage\\n \\n SendMessage --> AddHistoryAgent\\n AddHistoryAgent --> ReasonResponse\\n ReasonResponse --> NeedAwaitResponse\\n \\n NeedAwaitResponse -->|Quick response is unlikely| End\\n NeedAwaitResponse -->|Input available on stdin| PrepareForDraft\\n NeedAwaitResponse -->|Quick response is likely| BusyWait\\n BusyWait --> NeedAwaitResponse\\n```]]>\\n </stdout>\\n <stderr/>\\n </single>\\n <reasoning id=\"20250116_135726_364\">\\n <![CDATA[\\n Let\\'s prepare for user communication.\\n I\\'ll create a reasoning block with my current findings.\\n Next I\\'ll reason about which context elements I can clear.\\n After cleaning, I\\'ll end up with the findings and the procedures.\\n This will be a good starting point for communication.\\n]]>\\n </reasoning>\\n <reasoning id=\"20250116_140036_848\">\\n <![CDATA[\\n \\n I\\'ve explored the filesystem and found this is a clean start of SIA.\\n I\\'ll introduce myself to the user and report my findings.\\n I can wait for user input for a long time since I don\\'t have any other tasks.\\n\\n]]>\\n </reasoning>\\n <reasoning id=\"20250116_141227_271\">\\n <![CDATA[\\n 20250116_134549_655: I\\'ve done this and found nothing. I\\'ve noted my conclusions. This entry can be deleted.\\n 20250116_134555_680: No need to keep this standard directory listing in context.\\n 20250116_134639_663: I\\'ve done this and found nothing. I\\'ve noted my conclusions. This entry can be deleted.\\n 20250116_134644_445: No need to keep this standard directory listing in context.\\n 20250116_134729_674: I\\'ve done this and found nothing. I\\'ve noted my conclusions. This entry can be deleted.\\n 20250116_134736_148: This listing is likely stale by now. Best to remove it.\\n 20250116_135016_476: I\\'ve loaded the only interesing procedures. This entry can be deleted.\\n 20250116_135024_642: No need to keep this standard directory listing in context.\\n 20250116_135232_795: I\\'m in the process of doing this. This reasoning is already summarized so the entry can be deleted.\\n 20250116_135253_387: No need to keep this standard directory listing in context.\\n 20250116_135726_364: I\\'m in the process of doing this. I\\'ll remove this right before removing this reasoning entry about what to delete.\\n]]>\\n </reasoning>\\n</context>'},\n",
|
|
" {'role': 'assistant', 'content': '<delete id=\"20250116_134644_445\"/>'}]}"
|
|
]
|
|
},
|
|
"execution_count": 9,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"dataset[5]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 10,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"training_args = TrainingArguments(\n",
|
|
" output_dir=str(args.output_dir),\n",
|
|
" num_train_epochs=3,\n",
|
|
" per_device_train_batch_size=1,\n",
|
|
" gradient_accumulation_steps=16,\n",
|
|
" gradient_checkpointing=True,\n",
|
|
" learning_rate=2e-5,\n",
|
|
" lr_scheduler_type=\"cosine\",\n",
|
|
" warmup_ratio=0.05,\n",
|
|
" weight_decay=0.01,\n",
|
|
" fp16=not is_bfloat16_supported(),\n",
|
|
" bf16=is_bfloat16_supported(),\n",
|
|
" logging_steps=10,\n",
|
|
" save_steps=200,\n",
|
|
" save_total_limit=3,\n",
|
|
" report_to=\"none\",\n",
|
|
" optim=\"adamw_8bit\",\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 11,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stderr",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"num_proc must be <= 20. Reducing num_proc to 20 for dataset of size 20.\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"trainer = SFTTrainer(\n",
|
|
" model=model,\n",
|
|
" tokenizer=tokenizer,\n",
|
|
" args=training_args,\n",
|
|
" train_dataset=dataset.to_transformers_dataset(tokenizer),\n",
|
|
" dataset_text_field=\"messages\",\n",
|
|
" max_seq_length=max_seq_length,\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 12,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stderr",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"==((====))== Unsloth - 2x faster free finetuning | Num GPUs used = 1\n",
|
|
" \\\\ /| Num examples = 20 | Num Epochs = 3 | Total steps = 3\n",
|
|
"O^O/ \\_/ \\ Batch size per device = 1 | Gradient accumulation steps = 16\n",
|
|
"\\ / Data Parallel GPUs = 1 | Total batch size (1 x 16 x 1) = 16\n",
|
|
" \"-____-\" Trainable parameters = 536,870,912/32,000,000,000 (1.68% trained)\n"
|
|
]
|
|
},
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Unsloth: Will smartly offload gradients to save VRAM!\n"
|
|
]
|
|
},
|
|
{
|
|
"data": {
|
|
"text/html": [
|
|
"\n",
|
|
" <div>\n",
|
|
" \n",
|
|
" <progress value='3' max='3' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
|
|
" [3/3 01:45, Epoch 1/3]\n",
|
|
" </div>\n",
|
|
" <table border=\"1\" class=\"dataframe\">\n",
|
|
" <thead>\n",
|
|
" <tr style=\"text-align: left;\">\n",
|
|
" <th>Step</th>\n",
|
|
" <th>Training Loss</th>\n",
|
|
" </tr>\n",
|
|
" </thead>\n",
|
|
" <tbody>\n",
|
|
" </tbody>\n",
|
|
"</table><p>"
|
|
],
|
|
"text/plain": [
|
|
"<IPython.core.display.HTML object>"
|
|
]
|
|
},
|
|
"metadata": {},
|
|
"output_type": "display_data"
|
|
},
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"TrainOutput(global_step=3, training_loss=1.7514243125915527, metrics={'train_runtime': 187.9691, 'train_samples_per_second': 0.319, 'train_steps_per_second': 0.016, 'total_flos': 7927521441988608.0, 'train_loss': 1.7514243125915527})"
|
|
]
|
|
},
|
|
"execution_count": 12,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"trainer.train()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 15,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Unsloth: Merging 4bit and LoRA weights to 16bit...\n",
|
|
"Unsloth: Will use up to 312.94 out of 503.54 RAM for saving.\n",
|
|
"Unsloth: Saving model... This might take 5 minutes ...\n"
|
|
]
|
|
},
|
|
{
|
|
"name": "stderr",
|
|
"output_type": "stream",
|
|
"text": [
|
|
" 12%|█▎ | 8/64 [00:00<00:02, 22.74it/s]\n",
|
|
"We will save to Disk and not RAM now.\n",
|
|
"100%|██████████| 64/64 [00:43<00:00, 1.48it/s]\n"
|
|
]
|
|
},
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Unsloth: Saving tokenizer... Done.\n",
|
|
"Done.\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"model.save_pretrained_merged(\n",
|
|
" str(args.output_dir), \n",
|
|
" tokenizer=tokenizer,\n",
|
|
" #save_method=\"merged_4bit_forced\"\n",
|
|
")"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "train",
|
|
"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.10.12"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|