code: def create_and_prepare_trainer(model, tokenizer, dataset, args):
      training_arguments = TrainingArguments(
          output_dir=args.output_dir,
          num_train_epochs=args.num_train_epochs,
          per_device_train_batch_size=args.per_device_train_batch_size,
          gradient_accumulation_steps=args.gradient_accumulation_steps,
          optim=args.optim,
          save_steps=args.save_steps,
          logging_steps=args.logging_steps,
          learning_rate=args.learning_rate,
          weight_decay=args.weight_decay,
          fp16=args.fp16,
          bf16=args.bf16,
          max_grad_norm=args.max_grad_norm,
          max_steps=args.max_steps,
          warmup_ratio=args.warmup_ratio,
          group_by_length=args.group_by_length,
          lr_scheduler_type=args.lr_scheduler_type,
          report_to=args.report_to,
      )
  
      peft_config = LoraConfig(
          lora_alpha=args.lora_alpha,
          lora_dropout=args.lora_dropout,
          r=args.lora_r,
          bias="none",
          task_type="CAUSAL_LM",
      )
  
      trainer = SFTTrainer(
          model=model,
          train_dataset=dataset,
          peft_config=peft_config,
          dataset_text_field="text",
          max_seq_length=args.max_seq_length,
          tokenizer=tokenizer,
          args=training_arguments,
          packing=args.packing,
      )
  
      return trainer
  
file_path: dstack/src/dstack/api/_public/huggingface/finetuning/sft/train.py
imports: from dataclasses import dataclass, field
  from typing import Optional, Union
  import torch
  from datasets import load_dataset
  from peft import LoraConfig, PeftModel
  from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig, HfArgumentParser, TrainingArguments
  from trl import SFTTrainer
label_desc: Use image node_id 2 for calling a global function with example usage: create_and_prepare_trainer(model, tokenizer, dataset, args) and returns: trainer