How DF-Finetune Works
Training data flow, key design decisions, and how the worker integrates with DeepFellow Server.
DF-Finetune trains LoRA adapters for causal language models on top of the Hugging Face Trainer and PEFT libraries. It runs in two modes: as a standalone command line tool against a local dataset, and as a long-running worker that processes fine-tuning jobs submitted through DeepFellow Server's API.
Training Data Flow
- Load the JSONL dataset with
LLMInstructionDataset, validating each line and skipping malformed entries. - Format each prompt-response pair with
format_instruction(), using the template### Instruction:\n{prompt}\n\n### Response:\n{response}. - Tokenize each example and mask instruction and padding tokens with
-100inlabels, so cross-entropy loss counts only response tokens. See Response-only loss. - Train with Hugging Face
Trainer, using a LoRA adapter applied throughPEFT. - Save the adapter, tokenizer, and a
metadata.jsonprovenance file toadapters/MODEL_NAME/. - Convert the adapter to GGUF format with
convert_to_gguf.py, when requested, for use withllama.cpp.
Two Ways to Run DF-Finetune
- Local CLI: run
train.py,run.py,generate.py, andconvert_to_gguf.pydirectly against a config file and a local dataset. See the CLI reference. - DeepFellow fine-tuning worker: run
worker.pyas a background service. It polls a MongoDBfine_tuning_jobscollection for queued jobs, claims them in FIFO order, downloads the training file from DeepFellow Server, and trains with the sameLLMTrainerclass the local CLI path uses.
Both paths converge on the same trainer, so a job submitted through DeepFellow Server trains identically to one submitted through the CLI with an equivalent configuration.
Key Design Decisions
Response-Only Loss
DF-Finetune masks instruction (prompt) tokens and padding with -100 in the training labels, so the loss penalizes errors in response tokens only. This keeps the model focused on generating better responses instead of memorizing prompts.
Device Selection
DF-Finetune selects a compute device automatically, in this priority order: CUDA, then Apple Silicon MPS, then CPU.
Quantization
Set architecture.quantization in the config to reduce memory usage during training.
| Value | Description |
|---|---|
4bit | 4-bit quantization through bitsandbytes (NF4). Requires CUDA. |
8bit | 8-bit quantization through bitsandbytes. |
fp32 | Full precision. |
null or omitted | fp16, the default. |
Idempotent Orchestration
run.py checks for existing artifacts before running expensive steps. It treats an adapter as trained once adapter_config.json and adapter_model.safetensors exist, and treats an adapter as converted once gguf/adapter.gguf exists. Use --force-retrain or --force-convert to rerun a step anyway.
Adapter Metadata
Every trained adapter includes a metadata.json file recording the base model, LoRA configuration, training hyperparameters, dataset info, and environment details such as device, quantization mode, timestamp, and git commit. Use this file to trace which configuration produced a given adapter.
LoRA Target Modules by Model
LoRA attaches to different module names depending on the model architecture. Set architecture.lora.target_modules in the config accordingly.
| Model Family | Target Modules |
|---|---|
| GPT-2 | c_attn, c_proj |
| Llama, Mistral | q_proj, k_proj, v_proj, o_proj |
| Phi | q_proj, k_proj, v_proj, dense |
How the Worker Integrates with DeepFellow Server
upload_job.py and worker.py connect DF-Finetune to a running DeepFellow Server.
upload_job.pylogs into DeepFellow Server, sets up an organization, project, and API key when none are provided, uploads the training file toPOST /v1/files, and creates a job withPOST /v1/fine_tuning/jobs.worker.pypolls DeepFellow Server's MongoDBfine_tuning_jobscollection for jobs withstatus: "queued", and claims the oldest one.worker.pydownloads the training file fromGET /v1/files/{file_id}/content, using the job'sdfft_token for authorization.worker.pybuilds a training config from the job's hyperparameters, auto-detecting the prompt and response field names in the training file, and trains withLLMTrainer.worker.pyupdates the job's status tosucceededorfailed, and appends progress events, which DeepFellow Server exposes throughGET /v1/fine_tuning/jobs/{job_id}/events.
DF-Finetune calls DeepFellow Server's existing fine-tuning API. It does not implement those endpoints. See the API reference for /v1/fine_tuning/jobs and related endpoints.
worker.py reads its MongoDB and server connection settings from environment variables shared with DeepFellow Server, so both processes point at the same MongoDB instance. See worker.py in the CLI reference for the full list.
Module Map
| Path | Role |
|---|---|
train.py | Trains a single adapter from a config file. |
run.py | Trains and converts to GGUF, idempotently. |
generate.py | Compares base and fine-tuned model output. |
convert_to_gguf.py | Converts a trained adapter to GGUF format. |
worker.py | Polls MongoDB and trains queued jobs. |
upload_job.py | Uploads a file and creates a job through DeepFellow Server's API. |
trainers/llm_trainer.py | LLMTrainer, the core training class wrapping Hugging Face Trainer. |
trainers/finetuning_strategies.py | Builds and applies the LoRA configuration. |
data_loaders/llm_dataset.py | Loads and tokenizes the JSONL dataset, with response-only masking. |
utils/device.py | Selects the compute device. |
We use cookies on our website. We use them to ensure proper functioning of the site and, if you agree, for purposes such as analytics, marketing, and targeting ads.