MODELCATALOG

Build & train

Build & train

Hugging Face training pattern

Connects NLP model catalog knowledge to SageMaker-managed training.

Inputs

tokenized text + label mapping

Outputs

fine-tuned transformer + tokenizer + config

Core explanation

The model/framework layer changes, but the SageMaker lifecycle remains input data, managed training, artifact, registry and deployment.

01

Mental model

What this means before worrying about syntax.

A Hugging Face model and tokenizer can be fine-tuned inside a SageMaker training job using an appropriate framework/container setup.

  • Tokenizer + pretrained checkpoint + task head + label map form one inference contract.
  • Fine-tuning changes weights; tokenizer/config compatibility must remain stable.
  • Model size must earn its latency/cost with measurable quality gain.
Lifecycle position
dataset -> tokenizer -> transformer -> fine-tune -> model artifact
02

Implementation anatomy

The sequence you would actually build or configure.

  1. 1

    Choose task-appropriate checkpoint.

  2. 2

    Load matching tokenizer.

  3. 3

    Tokenize with explicit truncation/padding/max length.

  4. 4

    Map business labels to IDs.

  5. 5

    Fine-tune and validate.

  6. 6

    Save model + tokenizer + config together.

Objects / terms worth remembering
AutoTokenizerAutoModelForSequenceClassificationTrainerModelTrainer
03

Concrete example

A real model-development or production flow.

1

Complaint narrative input

↓
2

BERT/DistilBERT-style classifier

↓
3

Labels: Fraud_Unauthorized_Transaction, Card_Payment_Issue, Account_Access, Fees_Charges

↓
4

Compare macro F1 and semantic robustness against TF-IDF baseline

↓
5

Measure p95 serving latency before promotion

04

Implementation lab

Concrete code, data manipulation and configuration for this lifecycle step.

01

Load tokenizer and classification head

python
from transformers import (
    AutoTokenizer,
    AutoModelForSequenceClassification,
)

checkpoint = "distilbert-base-uncased"

tokenizer = AutoTokenizer.from_pretrained(checkpoint)

label_names = sorted(train_df["label"].unique())
label2id = {label: i for i, label in enumerate(label_names)}
id2label = {i: label for label, i in label2id.items()}

model = AutoModelForSequenceClassification.from_pretrained(
    checkpoint,
    num_labels=len(label_names),
    label2id=label2id,
    id2label=id2label,
)
02

Tokenize with an explicit sequence-length policy

python
def tokenize_batch(batch):
    return tokenizer(
        batch["text"],
        truncation=True,
        max_length=256,
        padding="max_length",
    )

tokenized_train = train_ds.map(tokenize_batch, batched=True)
tokenized_val = val_ds.map(tokenize_batch, batched=True)
03

Fine-tune and save model + tokenizer together

python

The tokenizer and label mapping are part of the deployable inference contract, not optional development metadata.

from transformers import TrainingArguments, Trainer
import os

args_hf = TrainingArguments(
    output_dir="/tmp/hf-checkpoints",
    learning_rate=2e-5,
    per_device_train_batch_size=16,
    per_device_eval_batch_size=32,
    num_train_epochs=3,
    weight_decay=0.01,
    eval_strategy="epoch",
    save_strategy="epoch",
    load_best_model_at_end=True,
)

trainer_hf = Trainer(
    model=model,
    args=args_hf,
    train_dataset=tokenized_train,
    eval_dataset=tokenized_val,
    tokenizer=tokenizer,
)

trainer_hf.train()

model_dir = os.environ["SM_MODEL_DIR"]
trainer_hf.save_model(model_dir)
tokenizer.save_pretrained(model_dir)

Design decisions that matter

  • Smallest model that meets quality target.
  • Sequence length from real text distribution.
  • Domain-specific checkpoint only if validation proves value.

Common failure modes

  • Tokenizer/checkpoint mismatch can silently hurt quality.
  • Label-ID ordering must be persisted.
  • Transformer gains may not justify serving cost/latency.

Production readiness check

Items to close before treating this step as production-ready.

✓ checkpoint✓ tokenizer✓ label map✓ max length✓ framework version✓ latency✓ batch size✓ license