File size: 14,464 Bytes
7749830
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70eb9de
9a87cb8
70eb9de
 
7749830
 
5bfd071
 
 
04d059b
4395ceb
04d059b
 
 
 
95d9fdc
 
 
7749830
 
 
 
 
 
 
95d9fdc
4395ceb
95d9fdc
aae861a
95d9fdc
aae861a
 
 
 
 
7749830
95d9fdc
518aafe
 
7749830
70eb9de
 
 
 
 
 
9a87cb8
70eb9de
 
 
9a87cb8
70eb9de
 
 
9a87cb8
 
70eb9de
9a87cb8
70eb9de
 
 
 
9a87cb8
70eb9de
 
9a87cb8
70eb9de
 
 
 
9a87cb8
 
 
 
70eb9de
 
 
 
7749830
 
611c848
7749830
70eb9de
 
4395ceb
611c848
 
 
 
70eb9de
 
7749830
70eb9de
4395ceb
70eb9de
611c848
 
 
 
 
 
 
 
 
70eb9de
 
7749830
70eb9de
 
 
 
7749830
 
 
 
611c848
7749830
 
 
 
70eb9de
 
 
 
 
7749830
70eb9de
518aafe
 
 
9a87cb8
 
 
7749830
70eb9de
 
 
 
 
 
 
 
 
 
 
 
 
7749830
70eb9de
 
 
 
 
 
 
 
 
 
 
9a87cb8
 
 
 
 
 
 
 
70eb9de
 
 
 
 
 
7749830
 
 
 
 
 
611c848
7749830
 
70eb9de
 
611c848
 
 
518aafe
 
 
 
 
 
611c848
 
 
 
 
 
 
 
b21080c
 
518aafe
b21080c
70eb9de
 
 
 
aa6b654
611c848
 
b21080c
70eb9de
 
 
 
 
 
7749830
 
5bfd071
 
7749830
70eb9de
 
5bfd071
9a87cb8
70eb9de
4395ceb
9a87cb8
4395ceb
 
 
 
9a87cb8
4395ceb
 
9a87cb8
4395ceb
 
9a87cb8
5bfd071
 
 
 
611c848
 
95d9fdc
518aafe
 
 
95d9fdc
518aafe
5bfd071
 
 
 
 
518aafe
 
 
5bfd071
 
9a87cb8
518aafe
 
 
 
 
 
95d9fdc
518aafe
95d9fdc
 
 
 
518aafe
 
 
 
 
 
 
 
 
 
 
 
 
 
4395ceb
 
 
 
 
518aafe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95d9fdc
518aafe
95d9fdc
 
518aafe
 
 
 
 
 
 
 
 
 
95d9fdc
 
518aafe
 
95d9fdc
518aafe
 
 
 
95d9fdc
518aafe
 
95d9fdc
 
 
 
 
 
 
518aafe
 
 
 
 
 
 
 
 
 
 
 
70eb9de
 
 
7749830
 
 
 
d1da8fd
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#!/usr/bin/env python3
"""
Fine-tuning script for SmolLM2-135M model using Unsloth.

This script demonstrates how to:
1. Install and configure Unsloth
2. Prepare and format training data
3. Configure and run the training process
4. Save and evaluate the model

To run this script:
1. Install dependencies: pip install -r requirements.txt
2. Run: python train.py
"""

import logging
import os
from datetime import datetime
from pathlib import Path
from typing import Union

import hydra
from omegaconf import DictConfig, OmegaConf

# isort: off
from unsloth import FastLanguageModel, FastModel, is_bfloat16_supported  # noqa: E402
from unsloth.chat_templates import get_chat_template  # noqa: E402

# isort: on

import os

import torch
from datasets import (
    Dataset,
    DatasetDict,
    IterableDataset,
    IterableDatasetDict,
    load_dataset,
)
from peft import PeftModel
from smolagents import CodeAgent, LiteLLMModel, Model, TransformersModel, VLLMModel
from smolagents.monitoring import LogLevel
from transformers import (
    AutoModelForCausalLM,
    AutoTokenizer,
    DataCollatorForLanguageModeling,
    Trainer,
    TrainingArguments,
)
from trl import SFTTrainer

from tools.smart_search.tool import SmartSearchTool


# Setup logging
def setup_logging():
    """Configure logging for the training process."""
    # Create logs directory if it doesn't exist
    log_dir = Path("logs")
    log_dir.mkdir(exist_ok=True)

    # Create a unique log file name with timestamp
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    log_file = log_dir / f"training_{timestamp}.log"

    # Configure logging
    logging.basicConfig(
        level=logging.INFO,
        format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
        handlers=[logging.FileHandler(log_file), logging.StreamHandler()],
    )

    logger = logging.getLogger(__name__)
    logger.info(f"Logging initialized. Log file: {log_file}")
    return logger


logger = setup_logging()


def install_dependencies():
    """Install required dependencies."""
    logger.info("Installing dependencies...")
    try:
        os.system(
            'pip install "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"'
        )
        os.system("pip install --no-deps xformers trl peft accelerate bitsandbytes")
        logger.info("Dependencies installed successfully")
    except Exception as e:
        logger.error(f"Error installing dependencies: {e}")
        raise


def load_model(cfg: DictConfig) -> tuple[FastLanguageModel, AutoTokenizer]:
    """Load and configure the model."""
    logger.info("Loading model and tokenizer...")
    try:
        model, tokenizer = FastModel.from_pretrained(
            model_name=cfg.model.name,
            max_seq_length=cfg.model.max_seq_length,
            dtype=cfg.model.dtype,
            load_in_4bit=cfg.model.load_in_4bit,
        )
        logger.info("Base model loaded successfully")

        # Configure LoRA
        model = FastModel.get_peft_model(
            model,
            r=cfg.peft.r,
            target_modules=cfg.peft.target_modules,
            lora_alpha=cfg.peft.lora_alpha,
            lora_dropout=cfg.peft.lora_dropout,
            bias=cfg.peft.bias,
            use_gradient_checkpointing=cfg.peft.use_gradient_checkpointing,
            random_state=cfg.peft.random_state,
            use_rslora=cfg.peft.use_rslora,
            loftq_config=cfg.peft.loftq_config,
        )
        logger.info("LoRA configuration applied successfully")

        return model, tokenizer
    except Exception as e:
        logger.error(f"Error loading model: {e}")
        raise


def load_and_format_dataset(
    tokenizer: AutoTokenizer,
    cfg: DictConfig,
) -> tuple[
    Union[DatasetDict, Dataset, IterableDatasetDict, IterableDataset], AutoTokenizer
]:
    """Load and format the training dataset."""
    logger.info("Loading and formatting dataset...")
    try:
        # Load the code-act dataset
        dataset = load_dataset("xingyaoww/code-act", split="codeact")
        logger.info(f"Dataset loaded successfully. Size: {len(dataset)} examples")

        # Split into train and validation sets
        dataset = dataset.train_test_split(
            test_size=cfg.dataset.validation_split, seed=cfg.dataset.seed
        )
        logger.info(
            f"Dataset split into train ({len(dataset['train'])} examples) and validation ({len(dataset['test'])} examples) sets"
        )

        # Configure chat template
        tokenizer = get_chat_template(
            tokenizer,
            chat_template="chatml",  # Supports zephyr, chatml, mistral, llama, alpaca, vicuna, vicuna_old, unsloth
            mapping={
                "role": "from",
                "content": "value",
                "user": "human",
                "assistant": "gpt",
            },  # ShareGPT style
            map_eos_token=True,  # Maps <|im_end|> to </s> instead
        )
        logger.info("Chat template configured successfully")

        def formatting_prompts_func(examples):
            convos = examples["conversations"]
            texts = [
                tokenizer.apply_chat_template(
                    convo, tokenize=False, add_generation_prompt=False
                )
                for convo in convos
            ]
            return {"text": texts}

        # Apply formatting to both train and validation sets
        dataset = DatasetDict(
            {
                "train": dataset["train"].map(formatting_prompts_func, batched=True),
                "validation": dataset["test"].map(
                    formatting_prompts_func, batched=True
                ),
            }
        )
        logger.info("Dataset formatting completed successfully")

        return dataset, tokenizer
    except Exception as e:
        logger.error(f"Error loading/formatting dataset: {e}")
        raise


def create_trainer(
    model: FastLanguageModel,
    tokenizer: AutoTokenizer,
    dataset: Union[DatasetDict, Dataset, IterableDatasetDict, IterableDataset],
    cfg: DictConfig,
) -> Trainer:
    """Create and configure the SFTTrainer."""
    logger.info("Creating trainer...")
    try:
        # Create TrainingArguments from config
        training_args_dict = OmegaConf.to_container(cfg.training.args, resolve=True)
        # Add dynamic precision settings
        training_args_dict.update(
            {
                "fp16": not is_bfloat16_supported(),
                "bf16": is_bfloat16_supported(),
            }
        )
        training_args = TrainingArguments(**training_args_dict)

        # Create data collator from config
        data_collator = DataCollatorForLanguageModeling(
            tokenizer=tokenizer,
            **cfg.training.sft.data_collator,
        )

        # Create SFT config without data_collator to avoid duplication
        sft_config = OmegaConf.to_container(cfg.training.sft, resolve=True)
        sft_config.pop("data_collator", None)  # Remove data_collator from config

        trainer = SFTTrainer(
            model=model,
            tokenizer=tokenizer,
            train_dataset=dataset["train"],
            eval_dataset=dataset["validation"],
            args=training_args,
            data_collator=data_collator,
            **sft_config,
        )
        logger.info("Trainer created successfully")
        return trainer
    except Exception as e:
        logger.error(f"Error creating trainer: {e}")
        raise


@hydra.main(version_base=None, config_path="conf", config_name="config")
def main(cfg: DictConfig) -> None:
    """Main training function."""
    try:
        logger.info("Starting training process...")
        logger.info(f"Configuration:\n{OmegaConf.to_yaml(cfg)}")

        # Install dependencies
        # install_dependencies()

        # Train if requested
        if cfg.train:
            # Load model and tokenizer
            model, tokenizer = load_model(cfg)

            # Load and prepare dataset
            dataset, tokenizer = load_and_format_dataset(tokenizer, cfg)

            # Create trainer
            trainer: Trainer = create_trainer(model, tokenizer, dataset, cfg)

            logger.info("Starting training...")
            trainer.train()

            # Save model
            logger.info(f"Saving final model to {cfg.output.dir}...")
            trainer.save_model(cfg.output.dir)

            # Save model in VLLM format
            logger.info("Saving model in VLLM format...")
            model.save_pretrained_merged(
                cfg.output.dir, tokenizer, save_method="merged_16bit"
            )

            # Print final metrics
            final_metrics = trainer.state.log_history[-1]
            logger.info("\nTraining completed!")
            logger.info(f"Final training loss: {final_metrics.get('loss', 'N/A')}")
            logger.info(
                f"Final validation loss: {final_metrics.get('eval_loss', 'N/A')}"
            )
        else:
            logger.info("Training skipped as train=False")

        # Test if requested
        if cfg.test:
            logger.info("\nStarting testing...")
            try:
                # Enable memory history tracking
                torch.cuda.memory._record_memory_history()

                # Set memory allocation configuration
                os.environ["PYTORCH_CUDA_ALLOC_CONF"] = (
                    "expandable_segments:True,max_split_size_mb:128"
                )

                # Load test dataset
                test_dataset = load_dataset(
                    cfg.test_dataset.name,
                    cfg.test_dataset.config,
                    split=cfg.test_dataset.split,
                    trust_remote_code=True,
                )
                logger.info(f"Loaded test dataset with {len(test_dataset)} examples")
                logger.info(f"Dataset features: {test_dataset.features}")

                # Clear CUDA cache before loading model
                torch.cuda.empty_cache()

                # Initialize model
                model: Model = LiteLLMModel(
                    api_base="http://localhost:8000/v1",
                    api_key="not-needed",
                    model_id=f"{cfg.model.provider}/{cfg.model.name}",
                    # model_id=cfg.model.name,
                    # model_id=cfg.output.dir,
                )

                # model: Model = TransformersModel(
                #     model_id=cfg.model.name,
                #     # model_id=cfg.output.dir,
                # )

                # model: Model = VLLMModel(
                #     model_id=cfg.model.name,
                #     # model_id=cfg.output.dir,
                # )

                # Create CodeAgent with SmartSearchTool
                agent = CodeAgent(
                    model=model,
                    tools=[SmartSearchTool()],
                    verbosity_level=LogLevel.ERROR,
                )

                # Format task to get succinct answer
                def format_task(question):
                    return f"""Please provide two answers to the following question:

1. A succinct answer that follows these rules:
   - Contains ONLY the answer, nothing else
   - Does not repeat the question
   - Does not include explanations, reasoning, or context
   - Does not include source attribution or references
   - Does not use phrases like "The answer is" or "I found that"
   - Does not include formatting, bullet points, or line breaks
   - If the answer is a number, return only the number
   - If the answer requires multiple items, separate them with commas
   - If the answer requires ordering, maintain the specified order
   - Uses the most direct and succinct form possible

2. A verbose answer that includes:
   - The complete answer with all relevant details
   - Explanations and reasoning
   - Context and background information
   - Source attribution where appropriate

Question: {question}

Please format your response as a JSON object with two keys:
- "succinct_answer": The concise answer following the rules above
- "verbose_answer": The detailed explanation with context"""

                # Run inference on test samples
                logger.info("Running inference on test samples...")
                for i, example in enumerate(test_dataset):
                    try:
                        # Clear CUDA cache before each sample
                        torch.cuda.empty_cache()

                        # Format the task
                        task = format_task(example["Question"])

                        # Run the agent
                        result = agent.run(
                            task=task,
                            max_steps=3,
                            reset=True,
                            stream=False,
                        )

                        # Parse the result
                        import json

                        json_str = result[result.find("{") : result.rfind("}") + 1]
                        parsed_result = json.loads(json_str)
                        answer = parsed_result["succinct_answer"]

                        logger.info(f"\nTest Sample {i+1}:")
                        logger.info(f"Question: {example['Question']}")
                        logger.info(f"Model Response: {answer}")
                        logger.info("-" * 80)

                        # Log memory usage after each sample
                        logger.info(f"Memory usage after sample {i+1}:")
                        logger.info(
                            f"Allocated: {torch.cuda.memory_allocated() / 1024**2:.2f} MB"
                        )
                        logger.info(
                            f"Reserved: {torch.cuda.memory_reserved() / 1024**2:.2f} MB"
                        )

                    except Exception as e:
                        logger.error(f"Error processing test sample {i+1}: {str(e)}")
                        continue

                # Dump memory snapshot for analysis
                torch.cuda.memory._dump_snapshot("memory_snapshot.pickle")
                logger.info("Memory snapshot saved to memory_snapshot.pickle")

            except Exception as e:
                logger.error(f"Error during testing: {e}")
                raise

    except Exception as e:
        logger.error(f"Error in main training process: {e}")
        raise


if __name__ == "__main__":
    main()

# uv run python train.py