A Complete Guide to the Top 3 NLP Models (Llama, ChatGLM, T5) with Hands‑On Deployment for Beginners
This article breaks down the core principles, architectures, and formulas of Llama, ChatGLM, and T5, compares their strengths and weaknesses, and provides a zero‑data, no‑VPN beginner project that trains and evaluates all three models on sentiment analysis and summarization, visualizes the results, and offers practical deployment tips.
Foundations of Llama, ChatGLM, and T5
Llama series
Decoder‑only Transformer. Each layer contains Multi‑Head Self‑Attention, Feed‑Forward Network, residual connection, LayerNorm, and a causal mask that prevents attention to future tokens. Token prediction follows P(t_n\mid t_{1..n-1}) = \text{softmax}(W\cdot h_n) where the vocabulary size is about 100 k. The model is open‑source, scales from 8 B to 70 B parameters, supports low‑cost fine‑tuning (e.g., LoRA), has a default 8 K context window, and shows weaker performance on Chinese text.
ChatGLM
Encoder‑decoder Transformer. The encoder applies bidirectional self‑attention to capture full‑sentence semantics; the decoder uses masked self‑attention plus cross‑attention to generate responses. Three Chinese‑specific optimizations are applied: Unicode‑aware tokenization, semantic alignment, and dialogue‑logic modeling. Training loss is cross‑entropy over the generated token sequence. The model supports up to 32 K context, excels at Chinese dialogue, and requires less GPU memory for fine‑tuning (6 B model fits on a single card). English performance is lower than Llama.
T5
Encoder‑decoder Transformer that adopts a unified “text‑to‑text” paradigm. Pre‑training uses the Span‑Corruption task: random spans in the input are replaced with a special token <X>, and the model learns to predict the missing span via cross‑entropy. Downstream tasks are cast as prompt → target text, keeping the same loss function. T5 is trained on ~10 trillion tokens, supports many languages, and offers strong transferability, though the base model (11 B) is large and inference is slower than Llama or ChatGLM.
Core comparison
Llama : decoder‑only, open‑source, high fluency, limited Chinese handling, 8 K context.
ChatGLM : encoder‑decoder, Chinese‑centric optimizations, 32 K context, weaker English.
T5 : encoder‑decoder, universal text‑to‑text framework, large model size, slower inference.
Hands‑on comparative experiment
Goal
Fine‑tune Llama 3, ChatGLM‑6B, and T5‑small on the amazon_polarity dataset for (a) sentiment analysis (positive/negative) and (b) summarization of long reviews, then compare accuracy, generation quality, and inference speed.
Environment
pip install torch transformers datasets matplotlib scikit-learn pandas tqdmGPU with at least 16 GB memory (8 GB works with the smaller models).
Code workflow
Define a MODELS dictionary mapping model names to HuggingFace identifiers, tokenizer classes, model classes, and task types.
Download the amazon_polarity dataset automatically (train 5 000 samples, test 20%). preprocess_data creates prompts:
Sentiment: sentiment analysis: {text} → label “positive” or “negative”.
Summarization: summarize: {text} → reference summary is the first 50 characters of the review.
Tokenize inputs with the model’s tokenizer; encode labels using as_target_tokenizer.
Train with Trainer (2 epochs, batch size 16, learning rate 2e‑5, fp16 when CUDA is available). Compute sentiment accuracy by decoding predictions and applying accuracy_score.
Inference per model:
Llama 3: build an Instruct‑style chat template, call model.generate (max_new_tokens 64, temperature 0.7), decode and strip the assistant prefix.
ChatGLM: use pipeline with conditional-generation, generate, then remove the original prompt from the output.
T5: use pipeline with text2text-generation, generate, and strip whitespace.
Measure per‑sample inference time and collect the first 10 generated summaries.
Store results for each model: sentiment accuracy, average inference time, list of summaries, and truncated sample texts.
Visualize with Matplotlib:
Bar chart of sentiment accuracy.
Horizontal bar chart of average inference time.
Table showing one example summary per model.
Radar chart combining three normalized scores: accuracy × 5, summary‑length score (length/10 capped at 5), and speed score (1/time × 0.5 capped at 5).
Save the figure as nlp_models_comparison.png.
Results interpretation
Sentiment accuracy: T5 ≈ ChatGLM > Llama 3 (Chinese weakness of Llama).
Inference speed: T5‑small > ChatGLM‑6B > Llama 3‑8B (larger models are slower).
Summarization quality: Llama 3 produces the most fluent text, followed by ChatGLM (accurate Chinese semantics), then T5‑small (simpler output).
Overall trade‑offs: ChatGLM offers a balanced profile for Chinese scenarios; Llama 3 excels in English generation; T5 provides the most flexible multitask pipeline.
Model‑selection guidance
Chinese dialogue or customer‑service: choose ChatGLM for superior Chinese understanding and low fine‑tuning cost.
Multitask pipelines (translation + summarization + sentiment): choose T5 to reuse a single model with prompt engineering.
Creative English generation (novels, code): choose Llama 3 for open‑source flexibility and high fluency.
Limited hardware (single 16 GB GPU): select ChatGLM‑6B or T5‑small to stay within memory limits.
Conclusion
All three models share the Transformer backbone but emphasize different strengths: Llama provides open‑source scalability and fluent English output; ChatGLM delivers Chinese‑optimized dialogue performance; T5 unifies all text tasks under a single architecture. The step‑by‑step experiment demonstrates data download, fine‑tuning, evaluation, and visualization, enabling practitioners to understand both the theoretical foundations and practical performance differences, and to select the model that best fits their specific NLP scenario.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
