Amirhossein75 commited on
Commit
05be445
·
unverified ·
1 Parent(s): 5d19c90

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +133 -93
README.md CHANGED
@@ -31,11 +31,11 @@
31
 
32
  ---
33
 
34
- # Model Card for Model ID
35
 
36
  <!-- Provide a quick summary of what the model is/does. -->
37
 
38
- This modelcard aims to be a base template for new models. It has been generated using [this raw template](https://github.com/huggingface/huggingface_hub/blob/main/src/huggingface_hub/templates/modelcard_template.md?plain=1).
39
 
40
  ## Model Details
41
 
@@ -43,185 +43,225 @@ This modelcard aims to be a base template for new models. It has been generated
43
 
44
  <!-- Provide a longer summary of what this model is. -->
45
 
 
46
 
 
 
 
 
 
47
 
48
- - **Developed by:** [More Information Needed]
49
- - **Funded by [optional]:** [More Information Needed]
50
- - **Shared by [optional]:** [More Information Needed]
51
- - **Model type:** [More Information Needed]
52
- - **Language(s) (NLP):** [More Information Needed]
53
- - **License:** [More Information Needed]
54
- - **Finetuned from model [optional]:** [More Information Needed]
55
-
56
- ### Model Sources [optional]
57
 
58
  <!-- Provide the basic links for the model. -->
59
 
60
- - **Repository:** [More Information Needed]
61
- - **Paper [optional]:** [More Information Needed]
62
- - **Demo [optional]:** [More Information Needed]
63
 
64
  ## Uses
65
 
66
- <!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
67
-
68
  ### Direct Use
69
 
70
- <!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
 
 
71
 
72
- [More Information Needed]
73
 
74
- ### Downstream Use [optional]
 
 
75
 
76
- <!-- This section is for the model use when fine-tuned for a task, or when plugged into a larger ecosystem/app -->
77
 
78
- [More Information Needed]
 
 
79
 
80
- ### Out-of-Scope Use
81
 
82
- <!-- This section addresses misuse, malicious use, and uses that the model will not work well for. -->
 
 
 
83
 
84
- [More Information Needed]
85
 
86
- ## Bias, Risks, and Limitations
 
 
 
87
 
88
- <!-- This section is meant to convey both technical and sociotechnical limitations. -->
89
 
90
- [More Information Needed]
91
 
92
- ### Recommendations
 
 
 
 
93
 
94
- <!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->
 
95
 
96
- Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
 
 
97
 
98
- ## How to Get Started with the Model
 
 
 
 
99
 
100
- Use the code below to get started with the model.
101
 
102
- [More Information Needed]
 
 
 
 
 
 
103
 
104
  ## Training Details
105
 
106
  ### Training Data
107
 
108
- <!-- This should link to a Dataset Card, perhaps with a short stub of information on what the training data is all about as well as documentation related to data pre-processing or additional filtering. -->
109
-
110
- [More Information Needed]
111
 
112
  ### Training Procedure
113
 
114
- <!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
115
-
116
- #### Preprocessing [optional]
117
-
118
- [More Information Needed]
119
 
 
 
 
120
 
121
  #### Training Hyperparameters
122
 
123
- - **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
124
-
125
- #### Speeds, Sizes, Times [optional]
 
 
 
 
 
 
 
126
 
127
- <!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
128
 
129
- [More Information Needed]
 
 
130
 
131
  ## Evaluation
132
 
133
- <!-- This section describes the evaluation protocols and provides the results. -->
134
-
135
  ### Testing Data, Factors & Metrics
136
 
137
  #### Testing Data
138
 
139
- <!-- This should link to a Dataset Card if possible. -->
140
-
141
- [More Information Needed]
142
 
143
  #### Factors
144
 
145
- <!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
146
-
147
- [More Information Needed]
148
 
149
  #### Metrics
150
 
151
- <!-- These are the evaluation metrics being used, ideally with a description of why. -->
152
-
153
- [More Information Needed]
154
 
155
  ### Results
156
 
157
- [More Information Needed]
 
 
 
 
 
 
158
 
159
  #### Summary
160
 
 
161
 
 
162
 
163
- ## Model Examination [optional]
164
-
165
- <!-- Relevant interpretability work for the model goes here -->
166
-
167
- [More Information Needed]
168
 
169
  ## Environmental Impact
170
 
171
- <!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
172
-
173
- Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
174
 
175
- - **Hardware Type:** [More Information Needed]
176
- - **Hours used:** [More Information Needed]
177
- - **Cloud Provider:** [More Information Needed]
178
- - **Compute Region:** [More Information Needed]
179
- - **Carbon Emitted:** [More Information Needed]
180
 
181
- ## Technical Specifications [optional]
182
 
183
  ### Model Architecture and Objective
184
 
185
- [More Information Needed]
 
 
186
 
187
  ### Compute Infrastructure
188
 
189
- [More Information Needed]
190
-
191
  #### Hardware
192
 
193
- [More Information Needed]
 
194
 
195
  #### Software
196
 
197
- [More Information Needed]
 
198
 
199
- ## Citation [optional]
200
 
201
- <!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
202
 
203
- **BibTeX:**
204
-
205
- [More Information Needed]
206
-
207
- **APA:**
 
 
 
 
 
208
 
209
- [More Information Needed]
 
210
 
211
- ## Glossary [optional]
212
 
213
- <!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
214
 
215
- [More Information Needed]
 
 
 
216
 
217
- ## More Information [optional]
218
 
219
- [More Information Needed]
 
220
 
221
- ## Model Card Authors [optional]
222
 
223
- [More Information Needed]
 
224
 
225
  ## Model Card Contact
226
 
227
- [More Information Needed]
 
31
 
32
  ---
33
 
34
+ # Model Card for LLM Instruction‑Tuning for Text Classification (LoRA + QLoRA)
35
 
36
  <!-- Provide a quick summary of what the model is/does. -->
37
 
38
+ This repository provides code and configuration to fine‑tune a decoder‑only LLM (default: `meta-llama/Llama-3.2-1B`) for **instruction‑style text classification** using **LoRA/QLoRA**. Rather than training a task‑specific classifier head, the project formulates classification as a short instruction → answer generation task and evaluates by **exact string match** against the label. It includes simple training/inference scripts, a 5‑label arXiv‑style demo, and optional Amazon SageMaker entrypoints.
39
 
40
  ## Model Details
41
 
 
43
 
44
  <!-- Provide a longer summary of what this model is. -->
45
 
46
+ This project instruction‑tunes a base, decoder‑only LLM with **LoRA adapters** loaded in **4‑bit NF4** precision for memory‑efficient training and inference. Supervised fine‑tuning is performed with TRL’s `SFTTrainer`. Prompts ask the model to “return the answer as the exact text label,” so predictions are decoded as plain text and compared by string match.
47
 
48
+ - **Developed by:** Amirhossein Yousefi (GitHub: `amirhossein-yousefi`)
49
+ - **Model type:** Decoder‑only LLM fine‑tuned with LoRA for single‑label text classification via instruction‑following
50
+ - **Language(s) (NLP):** English by default (demo dataset uses arXiv titles/abstracts); broader multilingual coverage depends on the chosen base model
51
+ - **License:** The repository itself does not include an explicit OSS license; the **base model** `meta-llama/Llama-3.2-1B` is governed by the **Llama 3.2 Community License**. You must accept and comply with Meta’s license to access and use the weights.
52
+ - **Finetuned from model :** `meta-llama/Llama-3.2-1B` (configurable)
53
 
54
+ ### Model Sources
 
 
 
 
 
 
 
 
55
 
56
  <!-- Provide the basic links for the model. -->
57
 
58
+ - **Repository:** https://github.com/amirhossein-yousefi/LLM-Instruction-Tuning-Text-Classification
59
+ - **Demo :** The repo includes an arXiv‑style 5‑label demo and example results; no hosted demo is provided.
 
60
 
61
  ## Uses
62
 
 
 
63
  ### Direct Use
64
 
65
+ - Fine‑tune LoRA adapters on your own CSV dataset for **single‑label text classification** (e.g., topic/category detection) using the provided `scripts/train.py`.
66
+ - Run inference/evaluation with `scripts/predict.py` to generate deterministic label strings and compute **accuracy**, **micro/macro F1**, a **classification report**, and a **confusion matrix**.
67
+ - Optional **Amazon SageMaker** utilities let you run managed training and deploy a real‑time endpoint with the LoRA adapters attached at load time.
68
 
69
+ ### Downstream Use
70
 
71
+ - Integrate the trained LoRA adapters into applications where explainable, instruction‑driven classification is helpful (e.g., routing, tagging, moderation).
72
+ - Swap the base model (any compatible decoder‑only LLM on the Hugging Face Hub) and re‑train with the same prompt template.
73
+ - Extend label sets without architectural changes—only prompt/label lists need to be updated.
74
 
75
+ ### Out-of-Scope Use
76
 
77
+ - **CPU‑only** training/inference with this repo as‑is (4‑bit `bitsandbytes` path expects NVIDIA CUDA GPUs).
78
+ - **Multi‑label** classification (comma‑separated outputs) is not implemented out of the box (listed as a roadmap idea).
79
+ - **Open‑domain generation** or safety‑critical decision‑making; this project focuses on label selection with short inputs.
80
 
81
+ ## Bias, Risks, and Limitations
82
 
83
+ - Outputs mirror biases in the **training corpus** you provide and in the **base model**. If your labels or examples are imbalanced or ambiguous, the model may propagate that bias.
84
+ - Exact‑match decoding can be brittle to **tokenization/typo** effects—ensure labels are short, canonical strings and restrict the decoding space.
85
+ - The base Llama 3.2 model has its own safety limitations and license‑based usage constraints (e.g., attribution and acceptable‑use provisions).
86
+ - The demo dataset is limited to **5 arXiv‑style labels** and relatively short academic texts; generalizing beyond this domain requires additional data.
87
 
88
+ ### Recommendations
89
 
90
+ - Curate balanced datasets; consider **stratified splits** and per‑class metrics.
91
+ - Keep **temperature = 0.0** for deterministic label decoding; constrain generation length (e.g., `max_new_tokens=8`).
92
+ - Validate robustness with **label synonyms/aliases** and adversarial cases; consider post‑processing that maps variants to canonical labels.
93
+ - Review and comply with the **Llama 3.2 Community License** (and any other upstream licenses) when distributing adapters/derivatives.
94
 
95
+ ## How to Get Started with the Model
96
 
97
+ **Install & train**
98
 
99
+ ```bash
100
+ python -m venv .venv
101
+ source .venv/bin/activate # Windows: .venv\Scripts\Activate.ps1
102
+ pip install --upgrade pip
103
+ pip install -r requirements.txt
104
 
105
+ # If the base model is gated, export an HF token
106
+ export HF_TOKEN=YOUR_HF_ACCESS_TOKEN
107
 
108
+ # One‑command training on CSVs
109
+ python scripts/train.py --base_path dataset --train_file train.csv --val_file validation.csv --test_file test.csv --label_column label_name --text_fields title abstract --base_model_name meta-llama/Llama-3.2-1B --output_dir llama-3.2-1b-arxiver-lora
110
+ ```
111
 
112
+ **Inference & evaluation**
113
+
114
+ ```bash
115
+ python scripts/predict.py --base_path dataset --test_file test.csv --base_model_name meta-llama/Llama-3.2-1B --output_dir llama-3.2-1b-arxiver-lora --save_csv predictions.csv
116
+ ```
117
 
118
+ **SageMaker **
119
 
120
+ ```bash
121
+ # Train a managed job
122
+ python sagemaker/train_sm.py --source_dir . --dataset_dir dataset --train_file train.csv --val_file validation.csv --test_file test.csv --label_column label_name --text_fields title abstract --base_model_id meta-llama/Llama-3.2-1B --instance_type ml.g5.2xlarge --instance_count 1
123
+
124
+ # Deploy a real‑time endpoint
125
+ python sagemaker/deploy_sm.py --training_job_name <your-job> --base_model_id meta-llama/Llama-3.2-1B --instance_type ml.g5.2xlarge --default_labels_json '["cs.CL","cs.CV","cs.LG","hep-ph","quant-ph"]'
126
+ ```
127
 
128
  ## Training Details
129
 
130
  ### Training Data
131
 
132
+ - Expected input: three CSV files under a base path: `train.csv`, `validation.csv`, `test.csv`.
133
+ - Required columns: a **label** column (default `label_name`) and one or more text fields (defaults: `title`, `abstract`). Missing/blank text fields are skipped; text fields are concatenated with punctuation.
134
+ - The repository ships utilities to prepare a **5‑class arXiv‑style demo** (labels: `['cs.CL','cs.CV','cs.LG','hep-ph','quant-ph']`).
135
 
136
  ### Training Procedure
137
 
138
+ #### Preprocessing
 
 
 
 
139
 
140
+ - Prompts are constructed as short instruction → answer pairs:
141
+ - **Train:** includes the gold label after `label:`.
142
+ - **Inference:** leaves `label:` empty and decodes the generated label.
143
 
144
  #### Training Hyperparameters
145
 
146
+ - **Training regime:** mixed precision with `fp16=True`, `tf32=True`; 4‑bit NF4 quantization with bfloat16 compute (QLoRA‑style).
147
+ - **Selected defaults (single‑GPU):**
148
+ - `num_train_epochs=1`
149
+ - `per_device_train_batch_size=8`, `per_device_eval_batch_size=8`
150
+ - `gradient_accumulation_steps=2` (effective 16 per step, per device)
151
+ - `learning_rate=2e-4`, `weight_decay=1e-3`, `warmup_ratio=0.03`
152
+ - `logging_steps=10`, `evaluation_strategy="epoch"`, `save_strategy="epoch"`, `save_total_limit=2`
153
+ - LoRA: `r=2`, `alpha=2`, `dropout=0.0`
154
+ - Quantization: `load_in_4bit=True`, `bnb_4bit_quant_type="nf4"`, `bnb_4bit_compute_dtype="bfloat16"`, `bnb_4bit_use_double_quant=True`
155
+ - Generation (eval): `temperature=0.0`, `max_new_tokens=8`, `do_sample=False`
156
 
157
+ #### Speeds, Sizes, Times
158
 
159
+ - Example environment: Laptop RTX 3080 Ti (16 GB VRAM), CUDA 12.9, PyTorch 2.8.0+cu129.
160
+ - Example run stats: ~6,314 seconds wall‑clock training, with TensorBoard logs under the run directory.
161
+ - Total training FLOPs (example): ~3.69e16 (as reported by the training logs).
162
 
163
  ## Evaluation
164
 
 
 
165
  ### Testing Data, Factors & Metrics
166
 
167
  #### Testing Data
168
 
169
+ - The example evaluation uses the provided arXiv‑style 5‑label test split.
 
 
170
 
171
  #### Factors
172
 
173
+ - Per‑class metrics are reported for `cs.CL`, `cs.CV`, `cs.LG`, `hep-ph`, `quant-ph`.
 
 
174
 
175
  #### Metrics
176
 
177
+ - Accuracy, micro F1, macro F1, per‑class precision/recall/F1, and a confusion matrix.
 
 
178
 
179
  ### Results
180
 
181
+ - **Overall:** Accuracy 93.8%, Micro‑F1 0.938, Macro‑F1 0.950.
182
+ - **Per‑class (Precision / Recall / F1 / Support):**
183
+ - `cs.CL`: 0.914 / 0.963 / 0.938 / 432
184
+ - `cs.CV`: 0.935 / 0.923 / 0.929 / 545
185
+ - `cs.LG`: 0.917 / 0.890 / 0.903 / 536
186
+ - `hep-ph`: 0.994 / 0.988 / 0.991 / 164
187
+ - `quant-ph`: 0.986 / 0.990 / 0.988 / 293
188
 
189
  #### Summary
190
 
191
+ The LoRA‑tuned 1B parameter Llama 3.2 model achieves strong performance on short academic texts while keeping training/inference affordable due to 4‑bit quantization. Performance is consistent across most classes, with particularly high scores for physics categories.
192
 
193
+ ## Model Examination
194
 
195
+ - The repo includes utilities for a **classification report** and **confusion matrix**. Inspect misclassifications to refine label definitions or add examples. Consider probing sensitivity to prompt wording.
 
 
 
 
196
 
197
  ## Environmental Impact
198
 
199
+ *(Approximate; depends on your hardware and run length.)*
200
+ Use the [MLCO2 Impact calculator](https://mlco2.github.io/impact#compute) with your GPU model, power draw, and wall‑clock runtime.
 
201
 
202
+ - **Hardware Type:** Single NVIDIA GPU (example: RTX 3080 Ti Laptop 16 GB)
203
+ - **Hours used:** ~1.75 hours (example)
204
+ - **Cloud Provider:** N/A (local) in example; SageMaker supported
205
+ - **Compute Region:** N/A (local) or your chosen AWS region
206
+ - **Carbon Emitted:** Not estimated
207
 
208
+ ## Technical Specifications
209
 
210
  ### Model Architecture and Objective
211
 
212
+ - **Architecture:** Decoder‑only Transformer (Llama 3.2 family when using the default base)
213
+ - **Objective:** Supervised instruction‑tuning for **single‑label classification** via generative decoding with exact‑match evaluation
214
+ - **Context length:** 512 tokens (config default; pass explicitly to trainer to ensure enforcement)
215
 
216
  ### Compute Infrastructure
217
 
 
 
218
  #### Hardware
219
 
220
+ - NVIDIA CUDA GPU required for 4‑bit `bitsandbytes` training/inference
221
+ (CPU‑only runs are not supported by the included scripts).
222
 
223
  #### Software
224
 
225
+ - Python ≥ 3.10, PyTorch, `transformers`, `trl`, `peft`, `bitsandbytes`, `accelerate`, and standard scientific Python packages.
226
+ - Optional: Astral’s `uv` for faster, reproducible dependency management (the repo also ships `requirements.txt`).
227
 
228
+ ## Citation
229
 
230
+ If you use this repository, please cite the GitHub project and the base model as appropriate.
231
 
232
+ **BibTeX (project):**
233
+ ```bibtex
234
+ @software{yousefi_2025_llm_instruction_tuning_text_classification,
235
+ author = {Yousefi, Amirhossein},
236
+ title = {LLM Instruction-Tuning for Text Classification (LoRA + QLoRA)},
237
+ year = {2025},
238
+ publisher = {GitHub},
239
+ url = {https://github.com/amirhossein-yousefi/LLM-Instruction-Tuning-Text-Classification}
240
+ }
241
+ ```
242
 
243
+ **APA (project):**
244
+ Yousefi, A. (2025). *LLM Instruction‑Tuning for Text Classification (LoRA + QLoRA)*. GitHub. https://github.com/amirhossein-yousefi/LLM-Instruction-Tuning-Text-Classification
245
 
246
+ **Base model:** Meta AI. (2024). *Llama 3.2‑1B* [Computer software]. Meta. https://huggingface.co/meta-llama/Llama-3.2-1B
247
 
248
+ ## Glossary
249
 
250
+ - **LoRA:** Low‑Rank Adapters for parameter‑efficient fine‑tuning.
251
+ - **QLoRA:** LoRA training with quantized base weights (typically 4‑bit NF4) and higher‑precision compute.
252
+ - **SFT:** Supervised Fine‑Tuning.
253
+ - **Exact‑match decoding:** Evaluates whether the generated label text exactly matches the gold label string.
254
 
255
+ ## More Information
256
 
257
+ - Amazon SageMaker scripts are included for managed training and deployment.
258
+ - Roadmap ideas include multi‑label support and few‑shot exemplars in prompts.
259
 
260
+ ## Model Card Authors
261
 
262
+ - Drafted by: ChatGPT (based on the repository’s README and code structure)
263
+ - Repository author: Amirhossein Yousefi
264
 
265
  ## Model Card Contact
266
 
267
+ - Open an issue on the GitHub repository for questions or contributions.