Hello HuggingFace

This example demonstrates how to use NVIDIA FLARE with the HuggingFace Client API to run federated PEFT/LoRA fine-tuning with a Qwen causal language model. The complete example code is in examples/hello-world/hello-huggingface.

Install NVFLARE and Dependencies

For complete installation instructions, see Installation. Install the PyTorch extra on the same NVFLARE distribution that supplied this example:

# Stable installation
python -m pip install "nvflare[PT]"

# Editable installation, run from the NVFlare checkout
python -m pip install -e ".[PT]"

Then install the remaining example dependencies from the example directory:

python -m pip install -r requirements.txt

Code Structure

hello-huggingface
|
|-- client.py        # HuggingFace/TRL local training script
|-- model.py         # Qwen LoRA server-side model
|-- prepare_data.py  # writes synthetic per-site JSONL data
|-- job.py           # job recipe for simulation
|-- requirements.txt
|-- README.md

Data

Prepare the default two-client synthetic JSONL dataset:

python prepare_data.py

By default this writes:

/tmp/nvflare/hello-huggingface/data
|
|-- site-1
|   |-- train.jsonl
|   |-- valid.jsonl
|-- site-2
|   |-- train.jsonl
|   |-- valid.jsonl

You can use your own prepared data by passing --data_root to prepare_data.py and job.py.

Client Code

The client script is a normal HuggingFace/TRL SFTTrainer script. The federated adaptation is intentionally small:

import nvflare.client.hf as flare

flare.init()
site_name = flare.get_site_name()

flare.patch(trainer)

while flare.is_running():
    trainer.evaluate()
    trainer.train()

flare.patch(trainer) wraps the trainer methods so the script can receive global parameters, evaluate, run the local training budget, and send the result back to the FL server.

Run Job

After preparing data, run the simulation:

python job.py

The job runs two simulated clients for two FL rounds using PEFT/LoRA. Each client resolves its data from <data_root>/<site_name>/ after initializing the Client API. For full-model and multi-node Qwen workflows, see examples/advanced/llm_hf.

Learn More

For the HuggingFace Client API contract and options, see HuggingFace Client API.