nmmursit commited on
Commit
1faa658
·
verified ·
1 Parent(s): 117fb0f

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +198 -1
README.md CHANGED
@@ -19,6 +19,203 @@ datasets:
19
  library_name: transformers
20
  pipeline_tag: text-generation
21
  model-index:
22
- - name: Llama-3.1-8B-Instruct_w16a8_tensorwise
23
  results: []
24
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  library_name: transformers
20
  pipeline_tag: text-generation
21
  model-index:
22
+ - name: Llama-3.1-8B-Instruct_w16a8_rw_with_gw_hp
23
  results: []
24
  ---
25
+
26
+ # Abstract
27
+
28
+ This repository provides a domain-adapted Turkish legal instruction-tuned model derived from meta-llama/Llama-3.1-8B-Instruct. The model was developed as part of the “Harnessing Fully Sharded Data Parallelism v2 with Float8 Precision for Faster Training” experiment, which compared FSDP2 + FP8 v.s. FSDP2 + BF16 configurations where using FSDP2 with FP8 reduced training time by roughly 39 minutes (13.75% faster) for the llama_3.1_8B model compared with its respective BF16 training.
29
+ The model was fine-tuned on the newmindai/EuroHPC-Legal corpus (Q/A format) to enhance legal-domain reasoning across multiple subfields. This model was trained in bfloat16 precision using multi-GPU data parallelism on a high-performance computing (HPC) cluster.
30
+ The resulting model aims to deliver accurate, context-aware legal text generation and question answering in Turkish while demonstrating the viability of FP8 training for production use.
31
+
32
+ ### Setups
33
+ - **Precision**: Used Half-precision bfloat16 as data type and for computation.
34
+ - **Hardware**: HPC (EuroHPC/BSC-class) node with 4 × NVIDIA H100 GPUs.
35
+ - **Framework**: PyTorch with `torchrun` for distributed training.
36
+
37
+ ### Experiment Context
38
+
39
+ This model was trained as part of our study for comparing **FSDP2 with bfloat16 precision** against **FSDP2 with FP8 mixed precision bfp16-fp8**.
40
+ We used `meta-llama/Llama-3.1-8B-Instruct`. The model has been loaded using `torch_dtype = bfloat16` and wrapped at once, also during forward/backward passes `bfloat16` has been used for computations.
41
+
42
+
43
+ ```python
44
+
45
+ from torchao.float8 import (
46
+ convert_to_float8_training,
47
+ Float8LinearConfig
48
+ )
49
+ from dataclasses import replace
50
+
51
+ config = Float8LinearConfig.from_recipe_name("rowwise_with_gw_hp")
52
+ model = convert_to_float8_training(model, config=config)
53
+ if use_fp8:
54
+ for i, layer in enumerate(model.model.layers):
55
+ fully_shard(layer, **fsdp_kwargs)
56
+ fully_shard(model.model.embed_tokens, **fsdp_kwargs)
57
+ fully_shard(model.lm_head, **fsdp_kwargs)
58
+ config = replace(config, pad_inner_dim=True)
59
+ ````
60
+
61
+
62
+ ## Base Model Technical Specifications
63
+ - **Parameters**: 8 Billion
64
+ - **Architecture Family**: Llama 3.1
65
+ - **Maximum Position Embeddings**: 131,072
66
+ - **Attention Heads**: 32 (`num_attention_heads`)
67
+ - **Key-Value Heads**: 8 (`num_key_value_heads`)
68
+ - **Hidden Layers**: 32 (`num_hidden_layers`)
69
+ - **Hidden Size**: 4,096 (`hidden_size`)
70
+ - **Intermediate Size**: 14,336
71
+ - **Vocabulary Size**: 128,256
72
+ - **Precision**: bfloat16
73
+ - **RoPE Scaling**: type `llama3`, factor = 8.0
74
+ - **RMS Norm Epsilon**: 1e-05
75
+ - **Activation**: SiLU
76
+
77
+ ## Training Methodology
78
+
79
+ ### Training Configuration
80
+ - **Model**: `meta-llama/Llama-3.1-8B-Instruct`
81
+ - **Sequence Length**: 4,096 (`seq_len`)
82
+ - **Epochs**: 1
83
+ - **Per-Device Micro Batch Size**: 2
84
+ - **Gradient Accumulation**: 4
85
+ - **GPUs**: 4 (via `CUDA_VISIBLE_DEVICES=0,1,2,3`)
86
+ - **dtype**: `bf16` && `fp8=false`
87
+ - Weights: bfloat16
88
+ - Activations: bfloat16
89
+ - **Optimizer**: AdamW
90
+ - Learning Rate: 2e-5
91
+ - Weight Decay: 0.01
92
+ - Betas: (0.9, 0.95)
93
+ - Epsilon: 1e-8
94
+ - **LR Scheduler**: Cosine; warmup = 10% (`warmup_ratio=0.1`) | also `warmup_steps=100`
95
+ - **Max Grad Norm**: 1.0
96
+ - **Gradient Checkpointing**: Enabled
97
+ - **Evaluation**: every 5 steps (`eval_steps=5`, `eval_samples=1000`)
98
+ - **Checkpointing**: every 10 steps; keep last 5; select best by `eval_loss`
99
+ - **Logging**: every step to file; Weights & Biases in offline mode
100
+ - **Seed**: 100
101
+ - **Distributed Training**: `torch.distributed.run` (single node, multi-GPU)
102
+ - FSDP2 (Optimized Fully Sharded Data Parallel)
103
+
104
+ ### Dependencies
105
+ | package | Version |
106
+ |-------|--------|
107
+ | Transformers | 4.57.1 |
108
+ | torch | 2.9.0+cu128 |
109
+ | accelerate | 0.14.1 |
110
+ | datasets | 4.3.0 |
111
+ | huggingface-hub | 0.36.0 |
112
+ | tensorboard | 2.20.0 |
113
+ | tensorboard-data-server | 0.7.2 |
114
+ | wandb | 0.22.1 |
115
+ ## **Performance Evaluation**
116
+ # *2-models trained on 1Node with both (bfp16-fp8) with tensorwise && rowwise configurations*
117
+ | perplexity metric results for bfp16-fp8 tensorwise && rowwise configurations | Accuracy metric results for bfp16-fp8 tensorwise && rowwise configurations | Loss metric results for bfp16-fp8 tensorwise && rowwise configurations | Memory allocation for bfp16-fp8 tensorwise && rowwise configurations | Utilization for bfp16-fp8 tensorwise && rowwise configurations |
118
+ |---------|---------|---------|---------|---------|
119
+ |![W&B Chart 11_6_2025, 1_28_06 PM](https://cdn-uploads.huggingface.co/production/uploads/683d4880e639f8d647355997/DOvLRT82pszUOYStDOBRG.png)| ![image](https://cdn-uploads.huggingface.co/production/uploads/683d4880e639f8d647355997/27loZAUgU-V0gQ0jAZz4o.png) | ![image](https://cdn-uploads.huggingface.co/production/uploads/683d4880e639f8d647355997/NX_Ni-U6spKd4R7PzBi8i.png)|![W&B Chart 11_6_2025, 1_24_01 PM](https://cdn-uploads.huggingface.co/production/uploads/683d4880e639f8d647355997/YezrVMnv1CoJchKNhDA1V.png)| ![W&B Chart 11_6_2025, 1_25_41 PM](https://cdn-uploads.huggingface.co/production/uploads/683d4880e639f8d647355997/qPkBYPC9rNKeyz0ONHmlO.png)|
120
+
121
+
122
+
123
+ | Method | Total Training Time | Avg GPU Utilization | Final Val Loss | Final Val Perplexity | Final Val Accuracy | Memory Allocated (avg %) | **Speed vs Tensorwise** |
124
+ | :------------------- | :------------------ | :-----------------: | :------------: | :------------------: | :----------------: | :----------------------: | :-----------------------------: |
125
+ | **w16a8 Tensorwise** | **123 min 50 s** | ≈ 98 % | 0.8394 | 2.3151 | 0.7934 | ~68 – 80 % | — |
126
+ | **w16a8 Rowwise** | **115 min 25 s** | ≈ 99 % | 0.8392 | 2.3144 | 0.7941 | ~66 – 78 % | 7% |
127
+
128
+
129
+ ## Implementation
130
+ ### Gpu && Memory usage Profiling
131
+ To visualize the usage of the memory and gpu's vram/utilis and more you can profile them with **pytorch-profiler** tool or **nsight systems** profiler tool .
132
+ - follow the steps to visualize with **Pytorch-profiler** :
133
+ 1. pip install the versions that mentioned in the dependencies section of these libs tensorboard and tensorboard-data-server.
134
+ 2. Visualize pytorch profiles by runing the command provided below.
135
+ ```python
136
+ tensorboard --logdir="/gpfs/scratch/ehpc317/esavirdi/startClean/profiles/bfp16_1node" --port="6006" --bind_all
137
+ ````
138
+
139
+ - follow the steps to visualize with **nsight systems**:
140
+ 1. download the files on to you local machine.
141
+ 2. download the full_version of the nsys profiler tool from here : https://developer.nvidia.com/nsight-systems/get-started.
142
+ 3. open the nsys-rep profiles using the nsight systems gui .
143
+
144
+ also you can get a summary of the report using these command lines up to your porpuse :
145
+ ```python
146
+ nsys stats --report cuda_gpu_kern_sum /gpfs/scratch/ehpc317/esavirdi/startClean/profiles/bfp16_1node/lama3.1_fp16_baseline.nsys-rep
147
+ ````
148
+ ```python
149
+ nsys stats --report cuda_gpu_trace /gpfs/scratch/ehpc317/esavirdi/startClean/profiles/bfp16_1node/lama3.1_fp16_baseline.nsys-rep
150
+ ````
151
+ ```python
152
+ nsys stats --report cuda_gpu_mem_size_sum /gpfs/scratch/ehpc317/esavirdi/startClean/profiles/bfp16_1node/lama3.1_fp16_baseline.nsys-rep
153
+ ````
154
+
155
+
156
+ ### Usage
157
+
158
+ **Note**: the final model has been saved in bfloat16 format. For inference, load the model in bfloat16 or float16 as shown below:
159
+
160
+ ```python
161
+ from transformers import AutoModelForCausalLM, AutoTokenizer
162
+ import torch
163
+
164
+ model_name = "newmindai/Llama-3.1-8B-Instruct_w16a8_rw_with_gw_hp"
165
+ dtype = torch.bfloat16
166
+
167
+ tok = AutoTokenizer.from_pretrained(model_name)
168
+ model = AutoModelForCausalLM.from_pretrained(
169
+ model_name,
170
+ torch_dtype=dtype,
171
+ device_map="auto"
172
+ )
173
+
174
+ prompt = "Soru: Kişisel Verilerin Korunması Kanunu uyarınca hangi durumlarda açık rıza aranmaz? Cevap:"
175
+
176
+ inputs = tok(prompt, return_tensors="pt").to(model.device)
177
+ with torch.no_grad():
178
+ out = model.generate(
179
+ **inputs,
180
+ max_new_tokens=256,
181
+ do_sample=False
182
+ )
183
+
184
+ print(tok.decode(out[0], skip_special_tokens=True))
185
+ ````
186
+
187
+ ## Ethical Considerations and Disclaimers
188
+ * Research & development purposes only; not a substitute for professional legal counsel.
189
+ * Users must ensure compliance with data protection and sector regulations.
190
+ * Potential biases may exist in domain data and model outputs.
191
+
192
+ ## Model & Data Card Metadata
193
+
194
+ * **Total Parameters**: 8,030,261,248
195
+ * **Serialized Size (approx.)**: 16,060,522,496 bytes
196
+ * **Config precision**: bfloat16
197
+ * **RoPE**: llama3 scaling, factor 8.0
198
+
199
+ ## References and Citations
200
+
201
+ ### Base Model
202
+
203
+ ```bibtex
204
+ @misc{meta_llama31_8b_instruct,
205
+ title={Llama 3.1 8B Instruct},
206
+ author={Meta AI},
207
+ year={2024},
208
+ howpublished={\url{https://huggingface.co/meta-llama/Llama-3.1-8B-Instruct}}
209
+ }
210
+ ```
211
+
212
+ ### Training Dataset
213
+
214
+ ```bibtex
215
+ @misc{euro_hpc_legal,
216
+ title={EuroHPC-Legal},
217
+ author={newmindai},
218
+ year={2025},
219
+ howpublished={\url{https://huggingface.co/datasets/newmindai/EuroHPC-Legal}}
220
+ }
221
+ ```