Gemma 3 270M is a 270-million-parameter model suited to task-specific fine-tuning. Its instruction-tuned checkpoint, `google/gemma-3-270m-it`, serves as a starting point for two narrow jobs: making replies more empathetic and prosocial, and translating English into Spanish. The trade-off is specialization rather than broad coverage, with modest training compute and deployment options that include INT4 quantization and on-device use. section 7.4 section 7.5
Both adaptations use supervised fine-tuning (SFT), but give the chat roles different jobs:
- For prosocial dialogue, load ProsocialDialog and make each training example a system–user–assistant exchange. The system message supplies empathy and safety context; an optional builder retains earlier turns in a dialogue. Check a base-model reply before training. section 7.4
- For translation, select English–Spanish pairs from OPUS-100, filter long examples, and optionally downsample. The system message requests translation, the user supplies English, and the assistant supplies the Spanish target. section 7.5
- Train with TRL’s `SFTTrainer`, save the checkpoint, then compare it with the starting model. The prosocial run uses three epochs, a batch size of four, and a 512-token maximum length; the translation run configures batch size and gradient accumulation for limited memory. section 7.4 section 7.5
Listing 7.16 Running SFT with TRL on chat-formatted samples
torch_dtype = model.dtype
args = SFTConfig(
output_dir=checkpoint_dir,
max_length=512,
packing=False,
num_train_epochs=3,
per_device_train_batch_size=4,
gradient_checkpointing=False,
optim="adamw_torch_fused",
logging_steps=10,
save_strategy="epoch",
eval_strategy="epoch",
learning_rate=learning_rate,
fp16=(torch_dtype == torch.float16),
bf16=(torch_dtype == torch.bfloat16),
lr_scheduler_type="cosine",
push_to_hub=False,
report_to="tensorboard",
dataset_kwargs={
"add_special_tokens": False,
"append_concat_token": True,
},
)
trainer = SFTTrainer(
model=model,
args=args,
train_dataset=train_built,
eval_dataset=eval_built,
processing_class=tokenizer,
)
trainer.train()
trainer.save_model()
The prosocial training configuration shows how chat-formatted examples, a tokenizer, evaluation data, and an `SFTConfig` are passed to `SFTTrainer`. section 7.4
Listing 7.25 Dataset preparation with OPUS-100
def row_ok(r):
s = r["translation"][src_lang]
t = r["translation"][tgt_lang]
return len(s) <= max_chars and len(t) <= max_chars
train_raw = train_raw.filter(row_ok)
if valid_raw is not None:
valid_raw = valid_raw.filter(row_ok)
if train_take and len(train_raw) > train_take: #1
train_raw = train_raw.shuffle(seed=42).select(range(train_take))
if valid_raw and eval_take and len(valid_raw) > eval_take:
valid_raw = valid_raw.shuffle(seed=43).select(range(eval_take))
else:
tmp = train_raw.train_test_split(test_size=0.01, seed=42) #2
train_raw, valid_raw = tmp["train"], tmp["test"]
def to_text(row):
src = row["translation"][src_lang]
tgt = row["translation"][tgt_lang]
# training text includes the assistant reply
txt = tokenizer.apply_chat_template(
[
{"role": "system", "content": "You are a professional translator
from English to Spanish. Respond with only the translation."},
{"role": "user", "content": src},
{"role": "assistant", "content": tgt},
],
tokenize=False,
)
row["text"] = txt + tokenizer.eos_token
return row
train_ds = train_raw.map(to_text, num_proc=1, load_from_cache_file=False)
eval_ds = valid_raw.map(to_text, num_proc=1, load_from_cache_file=False)
The translation preparation code shows the corresponding change in supervision: filtered bilingual rows become chat-template text containing the Spanish assistant reply. section 7.5
Check behavior, not just completion
A baseline translation probe sometimes repeats the English input or answers as a general assistant. After fine-tuning, “Good morning, everyone” becomes “Buenos días a todos,” although another displayed translation remains awkward. For prosocial dialogue, compare replies before and after SFT and calculate perplexity on a held-out slice; inspect the wording as well as the metric. section 7.4 section 7.5
