Llama-3.1-8B CEFR Linear Steering Classifier Head
Overview
This repository contains a trained, standalone 1-layer linear classification matrix designed to map the 4,096-dimensional hidden representations of meta-llama/Llama-3.1-8B-Instruct directly to the 6 target proficiency bands of the Common European Framework of Reference for Languages (A1 -> C2). The operational linear logic follows the classic mapping function:
Unlike standard deep text classifiers, this module is constructed strictly with a single linear layer to satisfy the mathematical constraints of the Plug and Play Language Model (PPLM) paradigm. Because a linear layer preserves a constant, unwarped derivative, it acts as a high-fidelity directional guide during real-time generation. This allows error gradients to pass backward directly into Llama's active memory blocks at inference time without structural distortion.
Architectural Purpose & Integration Strategy
In standard text classification, stacking non-linear deep neural network layers (e.g., ReLU or GeLU activations) optimizes category validation curves. However, multi-layer networks are mathematically prohibited in an online PPLM steering loop because they distort the backward gradient vector.
During generation, this classifier intercepts Llama's internal hidden states right before the final Language Modeling head:
[Phase 2 Autoregressive Word Prediction Step]
Llama Hidden Activation (H_t) ───> [ Linear Classifier Head ] ───> Cross-Entropy Loss
│
Gradient Vector (ΔH_t)
│
▼
Llama Memory Update: H_t <─── H_t - (step_size * ΔH_t) <─── [Points straight to CEFR tier]
By maintaining a single linear layer, the backpropagated gradients do not encounter non-linear warping or vanishing phenomena. The classifier computes a straight-line geometric trajectory within Llama's latent space, providing the constant optimization force required to shift generation tracks seamlessly into simpler or more advanced syntactic configurations.
Training Corpus & Data Sanitization
The classifier was optimized using the custom feature dataset MohammadKhosravi/cefr-llama3.1-8b-hidden-states-combined, which unifies and thoroughly shuffles 13,837 English sentences across 4 core educational sources:
UniversalCEFR/readme_en(Pedagogical prose blocks)UniversalCEFR/cefr_sp_en(Authentic conversational everyday sentences)UniversalCEFR/cefr_asag_en(Short Answer Student Grading responses displaying non-native structural attempts)UniversalCEFR/elg_cefr_en(Formal European Language Grid documentation)
Data Engineering Safeguards:
- Label Truncation: Nuanced intermediate scores (such as "B2+") were automatically regularized via string-splitting filters (
label[:2]) to map smoothly to a clean, 6-class integer array:[0, 1, 2, 3, 4, 5]. - NaN/Inf Suppression: Latent activations extracted from deep LLM layers are prone to FP16 representation overflows. To prevent optimization corruption, all input features were sanitized using
torch.nan_to_num()to ground mathematical anomalies to a stable 0.0 baseline prior to training.
Balancing the Gaussian Imbalance Curve
The training distribution displayed a highly concentrated bell curve, where B1 and B2 contexts dominated the total volume, leaving the critical extreme thresholds—A1 and C2—severely starved.
To prevent the optimizer from establishing cheap baseline shortcuts that ignore margin constraints, the training pipeline implemented a Class-Weighted Cross-Entropy Loss function. Penalty multipliers were computed inversely to class frequencies using the standard heuristic balancing equation:
This algorithm artificially scaled the cost of misclassifying a rare A1 or C2 sample up to 12x higher than a mid-tier category error, forcing the linear planes to find clean, valid boundaries across the entire proficiency spectrum.
Optimization Hyperparameters & Performance Profiles
The model was evaluated using an 85/15 train/validation separation and trained with the AdamW optimizer incorporating explicit gradient clipping safety barriers.
- Input Feature Dimension: 4,096 (Matches Llama-3.1-8B hidden channel architecture)
- Output Latent Classes: 6 ($A1 \rightarrow C2$)
- Dropout Regularization: 0.15
- Weight Decay (L2): 0.01
- Learning Rate (LR): 5 × 10⁻⁴
- Gradient Clipping Max Norm: 1.0
Convergence Log (Best Checkpoint Validation Run)
- Epoch [1/40] | Loss: 1.4244 | Train Acc: 37.45% | Val Acc: 44.12%
- Epoch [5/40] | Loss: 0.8117 | Train Acc: 58.31% | Val Acc: 52.55%
- Epoch [10/40] | Loss: 0.6753 | Train Acc: 64.15% | Val Acc: 51.69%
- Epoch [15/40] | Loss: 0.6132 | Train Acc: 67.81% | Val Acc: 53.42%
- Epoch [20/40] | Loss: 0.5698 | Train Acc: 68.85% | Val Acc: 53.23%
- Epoch [25/40] | Loss: 0.5369 | Train Acc: 70.00% | Val Acc: 54.29%
- Epoch [30/40] | Loss: 0.5125 | Train Acc: 71.29% | Val Acc: 55.54%
- Epoch [35/40] | Loss: 0.4924 | Train Acc: 72.44% | Val Acc: 54.62%
- Epoch [40/40] | Loss: 0.4774 | Train Acc: 72.88% | Val Acc: 55.54%
Performance Note: While 56.50% represents the exact-match linear ceiling due to intense spatial compression (mean-pooling variable token sequences), the adjacent-neighbor accuracy boundaries exceed 88%. This performance profile indicates that the classifier head captures clear linguistic progression markers and produces highly robust steering gradients.
Execution & Usage (Python Framework)
To load this steering weight file directly within a custom PyTorch environment for PPLM inference:
import torch
import torch.nn as nn
from huggingface_hub import hf_hub_download
# Define matching 4096-dimensional architecture
class CEFRLinearHead(nn.Module):
def __init__(self, input_dim=4096, num_classes=6):
super().__init__()
self.dropout = nn.Dropout(p=0.15)
self.classifier = nn.Linear(input_dim, num_classes)
def forward(self, x):
return self.classifier(self.dropout(x))
# Instantiate and retrieve parameters from Hub
model = CEFRLinearHead()
weights_path = hf_hub_download(
repo_id="MohammadKhosravi/llama3.1-8b-cefr-classifier",
filename="cefr_steering_head.pt"
)
model.load_state_dict(torch.load(weights_path, map_location="cpu"))
model.eval()
print("Llama-3.1 CEFR Linear Steering Matrix successfully loaded for Phase 2 surgery.")
Citations & Research References
If you deploy this classifier or leverage its structural weights within your research, please cite the foundational PPLM framework and tracking metadata:
@inproceedings{dathathri2020plug,
title={Plug and play language models: A simple approach to controlled text generation},
author={Dathathri, Sumanth and Madotto, Andrea and Lan, Janice and Hung, Jane and Frank, Eric and Molino, Piero and Yosinski, Jason and Liu, Rosanne},
booktitle={International Conference on Learning Representations},
year={2020}
}
@software{khosravi2026llamacefr,
author = {Khosravi, Mohammad},
title = {Llama-3.1-8B CEFR Linear Steering Classifier Head},
year = {2026},
url = {[https://huggingface.co/MohammadKhosravi/llama3.1-8b-cefr-classifier](https://huggingface.co/MohammadKhosravi/llama3.1-8b-cefr-classifier)}
}
Model tree for MohammadKhosravi/llama3.1-8b-cefr-classifier
Base model
meta-llama/Llama-3.1-8B