sAndreotti/SpecialGems

0

stars

7

commits

Jupyter Notebook

primary language

Apr 22, 2026

updated

README

Special Application Models — Fine-Tuning Gemma 3/4 on 26 NLP Tasks

Specialized versions of Google Gemma 3 (and Gemma 4) trained on individual NLP tasks, with systematic comparison against the base model.

Model License: Gemma 1/2/3 → Gemma Terms of Use · Gemma 4 → Apache 2.0
Code License: Apache 2.0


Objective

Each folder in this repository contains:

  1. A fine-tuning script (LoRA/QLoRA) for a specific NLP task.
  2. The dataset used (HuggingFace link or download/preparation script).
  3. An evaluation notebook comparing the fine-tuned model against the corresponding Gemma base model.

Supported Hardware

PlatformRecommended FrameworkNotes
Colab / Kaggle (T4, L4, A100)UnslothOnly framework with fp16 patch for Gemma 3; 50–60% VRAM savings
Mac mini 24 GB (Apple Silicon)MLX-LMmlx_lm.lora + mlx_lm.fuse for LoRA merge
2× GTX 1080 Ti (Pascal, sm_61)LLaMA Factory + DeepSpeed ZeRO-2fp16, attn_implementation=eager; pin PyTorch ≤ 2.4 cu118

Base LoRA Configuration

r             = 16
alpha         = 16
dropout       = 0.05
target_modules = ["q_proj", "k_proj", "v_proj", "o_proj",
                  "gate_proj", "up_proj", "down_proj"]
lr            = 2e-4
scheduler     = "cosine"
warmup        = 0.03   # 3% of total steps

Critical Gemma 3 Gotchas

  • fp16 NaN: activations can exceed 65,504 — use Unsloth or bf16 on Ampere+ GPUs.
  • Chat template: <start_of_turn>user…<end_of_turn><start_of_turn>model…<end_of_turn>. Gemma 3 has no system role; Gemma 4 introduces it.
  • attn_implementation="eager" required for Gemma 3 — SDPA/FA2 cause numerical degradation.
  • Pad token: always set tokenizer.pad_token = tokenizer.eos_token to avoid silent loss masking.

Implemented Tasks

Text Understanding (Tasks 1–5)

#TaskModelMetricsColab
1Sentiment Analysis — pos/neg/neu polarity or 1–5 star ratingGemma 3 1BAccuracy, macro-F1Open In Colab
2Intent Classification — 60–150 intent classes + OODGemma 3 1BTop-1 accuracy, macro-F1, OOS recallOpen In Colab
3Topic Classification — single- or multi-label on news/socialGemma 3 1BAccuracy, macro-F1, Hamming lossOpen In Colab
4Natural Language Inference (NLI) — entailment / neutral / contradictionGemma 3 1B–4BAccuracy matched/mismatched, ANLI per-roundOpen In Colab
5Reading Comprehension (Multi-Choice) — passage + 4 choicesGemma 3 4BAccuracyOpen In Colab

Generation (Tasks 6–12)

#TaskModelMetricsColab
6Text Summarization — abstractive/extractive from articles and dialoguesGemma 3 4B (1B for SAMSum)ROUGE-1/2/L, BERTScoreOpen In Colab
7Question Answering — extractive (span) and generativeGemma 3 1B–4BExact Match, token F1, ROUGE-LOpen In Colab
8Translation EN↔ITGemma 3 4B–12BSacreBLEU, chrF++, COMET-22, TEROpen In Colab
9Paraphrasing — semantically equivalent reformulationGemma 3 1B–4BBLEU, self-BLEU, BERTScore, iBLEUOpen In Colab
10Grammatical Error Correction (GEC)Gemma 3 4B–12BM2/ERRANT F0.5, GLEU+Open In Colab
11Text Simplification — readable rewriting with readability constraintsGemma 3 4BSARI, BLEU, Gulpease, BERTScoreOpen In Colab
12Text Style Transfer — formality, tone, toxicityGemma 3 4BTransfer-accuracy, BERTScore, perplexityOpen In Colab

Structured Extraction (Tasks 13–20)

#TaskModelMetricsColab
13Named Entity Recognition (Generative) — inline or JSON outputGemma 3 1B / 4BSpan micro/macro entity-F1 (seqeval)Open In Colab
14Relation Extraction — JSON triples {head, relation, tail}Gemma 3 4B / 12BMicro-F1 on triplesOpen In Colab
15Text-to-SQL — NL + DB schema → executable SQL queryGemma 3 4B / 12B+Exact Set Match, Execution AccuracyOpen In Colab
16Code Generation / Completion — docstring → implementationGemma 3 4B / 12B+pass@1 / pass@10 (EvalPlus)Open In Colab
17Document Classification — text → label or JSONGemma 3 1BAccuracy, macro-F1, Hamming lossOpen In Colab
18Table Extraction / Table QA — table + question → answerGemma 3 4B / 12BExact-match, BLEU/ROUGE/BERTScoreOpen In Colab
19Structured Data Extraction — text → JSON with schemaGemma 3 4BSchema-validity rate, field-level P/R/F1Open In Colab
20DocVQA Multimodal — page image + question → answerGemma 3 4B / 12B+ANLS, VQA accuracy, entity-F1Open In Colab

Dialogue & Alignment (Tasks 21–25)

#TaskModelMetricsColab
21Instruction Following / General SFT — baseline for DPO/ORPOGemma 3 4B / 12BMT-Bench-IT, AlpacaEval-IT, IFEvalOpen In Colab
22Multi-Turn Chat — history management and long contextGemma 3 4B–12BMT-Bench(-IT), AlpacaEval 2 LC win-rateOpen In Colab
23RAG Fine-Tuning — generator grounded on retrieved passagesGemma 3 4B / 12BRAGAS faithfulness, AIS, EM/F1Open In Colab
24Fact Checking / Claim Verification — SUPPORTS / REFUTES / NEIGemma 3 4B / 12BLabel accuracy, macro-F1, FEVER scoreOpen In Colab
25Hate Speech / Toxic Content DetectionGemma 3 1B / 4BMacro-F1 (EVALITA), HateCheckOpen In Colab

Advanced Extraction (Task 26)

#TaskModelMetricsColab
26Document Segmentation / Page-Stream Segmentation — BIO boundary detection on composite PDFsGemma 3 1B (pairwise) / 4B (stream)Boundary F1, Window-Diff, Pk, Panoptic QualityOpen In Colab

Key Italian Datasets

TaskRecommended IT Dataset
Sentimentevalitahf/sentiment_analysis (SENTIPOLC 2016, 9.4k)
IntentAmazonScience/massive it-IT (16.5k)
NLIMoritzLaurer/multilingual-NLI-26lang-2mil7 IT split
SummarizationARTeLab/fanpage (84k), ARTeLab/ilpost (44k)
QAcrux82/squad_it (54k train / 7.6k test)
TranslationHelsinki-NLP/europarl en-it (1.9M)
GEClang-uk/omnigec IT split
NERBabelscape/wikineural it, dhfbk/KIND
SFTmchl-labs/stambecco_data_it (52k), DeepMount00/italian_conversations
ChatOASST1 filtered lang==it, mii-community/ultrachat-translated-ita
RAGcrux82/squad_it, unicamp-dl/mmarco Italian config
Fact CheckingFEVER-IT (crux82 on GitHub)
Hate Speechevalitahf/hatespeech_detection (HaSpeeDe 2), RiTA-nlp/ami_2020

SFT Note: use a 70% IT / 30% EN data mix to avoid catastrophic forgetting in Italian.


Repository Structure

Special Application Models/
├── README.md
├── LICENSE
├── requirements.txt
├── utils/
│   ├── base_trainer.py     ← shared LoRA/Unsloth utilities
│   └── dry_run.py          ← offline pipeline verifier
├── .github/
│   └── workflows/
│       ├── ci.yml          ← automatic dry run on every push
│       ├── release.yml     ← tag completed tasks
│       └── notify.yml      ← training completion notifications
├── task_01_sentiment/
│   ├── train.py
│   ├── dataset_prep.py
│   └── evaluate.ipynb
├── task_02_intent/
│   └── ...
└── task_26_doc_segmentation/
    └── ...

How to Use

1 — Repository Setup on GitHub

# From the project folder on your PC
git init
git add .
git commit -m "feat: Gemma 3/4 fine-tuning project across 26 NLP tasks"

# Create a repo on github.com, then:
git remote add origin https://github.com/<your-username>/special-application-models.git
git push -u origin main

2 — Training on Google Colab

Open a new notebook at colab.research.google.com, select a GPU (Runtime → Change runtime type → T4/L4/A100) and paste:

# Clone the project
!git clone https://github.com/<your-username>/special-application-models.git
%cd special-application-models

# Install Unsloth (includes fp16 patch for Gemma 3)
!pip install "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
!pip install trl datasets evaluate rouge_score bert_score sacrebleu

# Run training for the desired task
!python task_01_sentiment/train.py --model_size 1b --num_epochs 3 --batch_size 8

# Evaluate
!jupyter nbconvert --to notebook --execute task_01_sentiment/evaluate.ipynb

To pull the latest code after local changes, just run !git pull before launching.

3 — Training on Kaggle

  1. Go to kaggle.com → Notebooks → New Notebook
  2. In the right panel, enable GPU T4 x2 or P100
  3. In a cell at the top:
# Kaggle already has PyTorch and CUDA; install only extra dependencies
!pip install "unsloth[kaggle-new] @ git+https://github.com/unslothai/unsloth.git" trl datasets

!git clone https://github.com/<your-username>/special-application-models.git
%cd special-application-models

!python task_01_sentiment/train.py --model_size 1b --bf16

Checkpoints are saved to /kaggle/working/checkpoints/ and downloadable from the Kaggle UI.


GitHub Actions — CI and Automation

The repository includes three workflows in .github/workflows/:

ci.yml — Automatic check on every push

Runs syntax checking of all Python scripts and a dry run of every task on CPU, without GPU. Triggers on every git push and every Pull Request.

# .github/workflows/ci.yml
name: CI — Dry Run all tasks

on: [push, pull_request]

jobs:
  dry-run:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.10"

      - name: Install lightweight dependencies (no GPU)
        run: pip install datasets transformers trl accelerate tokenizers

      - name: Python syntax check
        run: |
          python -m py_compile utils/base_trainer.py utils/dry_run.py
          for f in task_*/train.py task_*/dataset_prep.py; do
            python -m py_compile "$f" && echo "✓ $f"
          done

      - name: Dry run all tasks (CPU, synthetic data)
        run: |
          for tid in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26; do
            python utils/dry_run.py --task "$tid" --max_steps 5 --output_dir /tmp/checkpoints
          done

release.yml — Automatic tag on task completion

When you push a tag task-NN-done (e.g., git tag task-01-done && git push --tags), it automatically creates a GitHub Release with attached log files.

# .github/workflows/release.yml
name: Release — Task completed

on:
  push:
    tags:
      - "task-*-done"

jobs:
  release:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Create Release
        uses: softprops/action-gh-release@v2
        with:
          name: "✅ ${{ github.ref_name }}"
          body: "Fine-tuning completed for task ${{ github.ref_name }}."
          generate_release_notes: true

notify.yml — Email/Slack notification when training ends

You can add a webhook (e.g., Slack or email via SendGrid) to receive a notification when a Colab training run finishes. At the end of the training script in Colab, add:

# At the end of train.py, notify via GitHub Actions dispatch
import requests, os
requests.post(
    "https://api.github.com/repos/<your-username>/special-application-models/dispatches",
    headers={"Authorization": f"Bearer {os.environ['GITHUB_TOKEN']}",
             "Accept": "application/vnd.github+json"},
    json={"event_type": "training-done", "client_payload": {"task": "01"}}
)

How to create the workflow files

mkdir -p .github/workflows
# Copy the YAML contents shown above into their respective files, then:
git add .github/
git commit -m "ci: add GitHub Actions workflows"
git push

After pushing, go to github.com → your repo → Actions to see live run status.


References

Contributors

sAndreotti

7 commits

sAndreotti/SpecialGems

0

stars

7

commits

Jupyter Notebook

primary language

Apr 22, 2026

updated

README

Special Application Models — Fine-Tuning Gemma 3/4 on 26 NLP Tasks

Specialized versions of Google Gemma 3 (and Gemma 4) trained on individual NLP tasks, with systematic comparison against the base model.

Model License: Gemma 1/2/3 → Gemma Terms of Use · Gemma 4 → Apache 2.0
Code License: Apache 2.0


Objective

Each folder in this repository contains:

  1. A fine-tuning script (LoRA/QLoRA) for a specific NLP task.
  2. The dataset used (HuggingFace link or download/preparation script).
  3. An evaluation notebook comparing the fine-tuned model against the corresponding Gemma base model.

Supported Hardware

PlatformRecommended FrameworkNotes
Colab / Kaggle (T4, L4, A100)UnslothOnly framework with fp16 patch for Gemma 3; 50–60% VRAM savings
Mac mini 24 GB (Apple Silicon)MLX-LMmlx_lm.lora + mlx_lm.fuse for LoRA merge
2× GTX 1080 Ti (Pascal, sm_61)LLaMA Factory + DeepSpeed ZeRO-2fp16, attn_implementation=eager; pin PyTorch ≤ 2.4 cu118

Base LoRA Configuration

r             = 16
alpha         = 16
dropout       = 0.05
target_modules = ["q_proj", "k_proj", "v_proj", "o_proj",
                  "gate_proj", "up_proj", "down_proj"]
lr            = 2e-4
scheduler     = "cosine"
warmup        = 0.03   # 3% of total steps

Critical Gemma 3 Gotchas

  • fp16 NaN: activations can exceed 65,504 — use Unsloth or bf16 on Ampere+ GPUs.
  • Chat template: <start_of_turn>user…<end_of_turn><start_of_turn>model…<end_of_turn>. Gemma 3 has no system role; Gemma 4 introduces it.
  • attn_implementation="eager" required for Gemma 3 — SDPA/FA2 cause numerical degradation.
  • Pad token: always set tokenizer.pad_token = tokenizer.eos_token to avoid silent loss masking.

Implemented Tasks

Text Understanding (Tasks 1–5)

#TaskModelMetricsColab
1Sentiment Analysis — pos/neg/neu polarity or 1–5 star ratingGemma 3 1BAccuracy, macro-F1Open In Colab
2Intent Classification — 60–150 intent classes + OODGemma 3 1BTop-1 accuracy, macro-F1, OOS recallOpen In Colab
3Topic Classification — single- or multi-label on news/socialGemma 3 1BAccuracy, macro-F1, Hamming lossOpen In Colab
4Natural Language Inference (NLI) — entailment / neutral / contradictionGemma 3 1B–4BAccuracy matched/mismatched, ANLI per-roundOpen In Colab
5Reading Comprehension (Multi-Choice) — passage + 4 choicesGemma 3 4BAccuracyOpen In Colab

Generation (Tasks 6–12)

#TaskModelMetricsColab
6Text Summarization — abstractive/extractive from articles and dialoguesGemma 3 4B (1B for SAMSum)ROUGE-1/2/L, BERTScoreOpen In Colab
7Question Answering — extractive (span) and generativeGemma 3 1B–4BExact Match, token F1, ROUGE-LOpen In Colab
8Translation EN↔ITGemma 3 4B–12BSacreBLEU, chrF++, COMET-22, TEROpen In Colab
9Paraphrasing — semantically equivalent reformulationGemma 3 1B–4BBLEU, self-BLEU, BERTScore, iBLEUOpen In Colab
10Grammatical Error Correction (GEC)Gemma 3 4B–12BM2/ERRANT F0.5, GLEU+Open In Colab
11Text Simplification — readable rewriting with readability constraintsGemma 3 4BSARI, BLEU, Gulpease, BERTScoreOpen In Colab
12Text Style Transfer — formality, tone, toxicityGemma 3 4BTransfer-accuracy, BERTScore, perplexityOpen In Colab

Structured Extraction (Tasks 13–20)

#TaskModelMetricsColab
13Named Entity Recognition (Generative) — inline or JSON outputGemma 3 1B / 4BSpan micro/macro entity-F1 (seqeval)Open In Colab
14Relation Extraction — JSON triples {head, relation, tail}Gemma 3 4B / 12BMicro-F1 on triplesOpen In Colab
15Text-to-SQL — NL + DB schema → executable SQL queryGemma 3 4B / 12B+Exact Set Match, Execution AccuracyOpen In Colab
16Code Generation / Completion — docstring → implementationGemma 3 4B / 12B+pass@1 / pass@10 (EvalPlus)Open In Colab
17Document Classification — text → label or JSONGemma 3 1BAccuracy, macro-F1, Hamming lossOpen In Colab
18Table Extraction / Table QA — table + question → answerGemma 3 4B / 12BExact-match, BLEU/ROUGE/BERTScoreOpen In Colab
19Structured Data Extraction — text → JSON with schemaGemma 3 4BSchema-validity rate, field-level P/R/F1Open In Colab
20DocVQA Multimodal — page image + question → answerGemma 3 4B / 12B+ANLS, VQA accuracy, entity-F1Open In Colab

Dialogue & Alignment (Tasks 21–25)

#TaskModelMetricsColab
21Instruction Following / General SFT — baseline for DPO/ORPOGemma 3 4B / 12BMT-Bench-IT, AlpacaEval-IT, IFEvalOpen In Colab
22Multi-Turn Chat — history management and long contextGemma 3 4B–12BMT-Bench(-IT), AlpacaEval 2 LC win-rateOpen In Colab
23RAG Fine-Tuning — generator grounded on retrieved passagesGemma 3 4B / 12BRAGAS faithfulness, AIS, EM/F1Open In Colab
24Fact Checking / Claim Verification — SUPPORTS / REFUTES / NEIGemma 3 4B / 12BLabel accuracy, macro-F1, FEVER scoreOpen In Colab
25Hate Speech / Toxic Content DetectionGemma 3 1B / 4BMacro-F1 (EVALITA), HateCheckOpen In Colab

Advanced Extraction (Task 26)

#TaskModelMetricsColab
26Document Segmentation / Page-Stream Segmentation — BIO boundary detection on composite PDFsGemma 3 1B (pairwise) / 4B (stream)Boundary F1, Window-Diff, Pk, Panoptic QualityOpen In Colab

Key Italian Datasets

TaskRecommended IT Dataset
Sentimentevalitahf/sentiment_analysis (SENTIPOLC 2016, 9.4k)
IntentAmazonScience/massive it-IT (16.5k)
NLIMoritzLaurer/multilingual-NLI-26lang-2mil7 IT split
SummarizationARTeLab/fanpage (84k), ARTeLab/ilpost (44k)
QAcrux82/squad_it (54k train / 7.6k test)
TranslationHelsinki-NLP/europarl en-it (1.9M)
GEClang-uk/omnigec IT split
NERBabelscape/wikineural it, dhfbk/KIND
SFTmchl-labs/stambecco_data_it (52k), DeepMount00/italian_conversations
ChatOASST1 filtered lang==it, mii-community/ultrachat-translated-ita
RAGcrux82/squad_it, unicamp-dl/mmarco Italian config
Fact CheckingFEVER-IT (crux82 on GitHub)
Hate Speechevalitahf/hatespeech_detection (HaSpeeDe 2), RiTA-nlp/ami_2020

SFT Note: use a 70% IT / 30% EN data mix to avoid catastrophic forgetting in Italian.


Repository Structure

Special Application Models/
├── README.md
├── LICENSE
├── requirements.txt
├── utils/
│   ├── base_trainer.py     ← shared LoRA/Unsloth utilities
│   └── dry_run.py          ← offline pipeline verifier
├── .github/
│   └── workflows/
│       ├── ci.yml          ← automatic dry run on every push
│       ├── release.yml     ← tag completed tasks
│       └── notify.yml      ← training completion notifications
├── task_01_sentiment/
│   ├── train.py
│   ├── dataset_prep.py
│   └── evaluate.ipynb
├── task_02_intent/
│   └── ...
└── task_26_doc_segmentation/
    └── ...

How to Use

1 — Repository Setup on GitHub

# From the project folder on your PC
git init
git add .
git commit -m "feat: Gemma 3/4 fine-tuning project across 26 NLP tasks"

# Create a repo on github.com, then:
git remote add origin https://github.com/<your-username>/special-application-models.git
git push -u origin main

2 — Training on Google Colab

Open a new notebook at colab.research.google.com, select a GPU (Runtime → Change runtime type → T4/L4/A100) and paste:

# Clone the project
!git clone https://github.com/<your-username>/special-application-models.git
%cd special-application-models

# Install Unsloth (includes fp16 patch for Gemma 3)
!pip install "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
!pip install trl datasets evaluate rouge_score bert_score sacrebleu

# Run training for the desired task
!python task_01_sentiment/train.py --model_size 1b --num_epochs 3 --batch_size 8

# Evaluate
!jupyter nbconvert --to notebook --execute task_01_sentiment/evaluate.ipynb

To pull the latest code after local changes, just run !git pull before launching.

3 — Training on Kaggle

  1. Go to kaggle.com → Notebooks → New Notebook
  2. In the right panel, enable GPU T4 x2 or P100
  3. In a cell at the top:
# Kaggle already has PyTorch and CUDA; install only extra dependencies
!pip install "unsloth[kaggle-new] @ git+https://github.com/unslothai/unsloth.git" trl datasets

!git clone https://github.com/<your-username>/special-application-models.git
%cd special-application-models

!python task_01_sentiment/train.py --model_size 1b --bf16

Checkpoints are saved to /kaggle/working/checkpoints/ and downloadable from the Kaggle UI.


GitHub Actions — CI and Automation

The repository includes three workflows in .github/workflows/:

ci.yml — Automatic check on every push

Runs syntax checking of all Python scripts and a dry run of every task on CPU, without GPU. Triggers on every git push and every Pull Request.

# .github/workflows/ci.yml
name: CI — Dry Run all tasks

on: [push, pull_request]

jobs:
  dry-run:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.10"

      - name: Install lightweight dependencies (no GPU)
        run: pip install datasets transformers trl accelerate tokenizers

      - name: Python syntax check
        run: |
          python -m py_compile utils/base_trainer.py utils/dry_run.py
          for f in task_*/train.py task_*/dataset_prep.py; do
            python -m py_compile "$f" && echo "✓ $f"
          done

      - name: Dry run all tasks (CPU, synthetic data)
        run: |
          for tid in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26; do
            python utils/dry_run.py --task "$tid" --max_steps 5 --output_dir /tmp/checkpoints
          done

release.yml — Automatic tag on task completion

When you push a tag task-NN-done (e.g., git tag task-01-done && git push --tags), it automatically creates a GitHub Release with attached log files.

# .github/workflows/release.yml
name: Release — Task completed

on:
  push:
    tags:
      - "task-*-done"

jobs:
  release:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Create Release
        uses: softprops/action-gh-release@v2
        with:
          name: "✅ ${{ github.ref_name }}"
          body: "Fine-tuning completed for task ${{ github.ref_name }}."
          generate_release_notes: true

notify.yml — Email/Slack notification when training ends

You can add a webhook (e.g., Slack or email via SendGrid) to receive a notification when a Colab training run finishes. At the end of the training script in Colab, add:

# At the end of train.py, notify via GitHub Actions dispatch
import requests, os
requests.post(
    "https://api.github.com/repos/<your-username>/special-application-models/dispatches",
    headers={"Authorization": f"Bearer {os.environ['GITHUB_TOKEN']}",
             "Accept": "application/vnd.github+json"},
    json={"event_type": "training-done", "client_payload": {"task": "01"}}
)

How to create the workflow files

mkdir -p .github/workflows
# Copy the YAML contents shown above into their respective files, then:
git add .github/
git commit -m "ci: add GitHub Actions workflows"
git push

After pushing, go to github.com → your repo → Actions to see live run status.


References

Contributors

sAndreotti

7 commits

Languages

Jupyter Notebook

59.0%

Python

41.0%