Text Generation
Transformers
Safetensors
PyTorch
Turkish
llama
legal
turkish
llama-3.1
fp8
bfloat16
mixed-precision
question-answering
fsdp-v2
distributed-training
conversational
text-generation-inference
Instructions to use newmindai/Llama-3.1-8B-Instruct_w16a8_rw_with_gw_hp with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use newmindai/Llama-3.1-8B-Instruct_w16a8_rw_with_gw_hp with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="newmindai/Llama-3.1-8B-Instruct_w16a8_rw_with_gw_hp") messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("newmindai/Llama-3.1-8B-Instruct_w16a8_rw_with_gw_hp") model = AutoModelForCausalLM.from_pretrained("newmindai/Llama-3.1-8B-Instruct_w16a8_rw_with_gw_hp", device_map="auto") messages = [ {"role": "user", "content": "Who are you?"}, ] inputs = tokenizer.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=40) print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use newmindai/Llama-3.1-8B-Instruct_w16a8_rw_with_gw_hp with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "newmindai/Llama-3.1-8B-Instruct_w16a8_rw_with_gw_hp" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "newmindai/Llama-3.1-8B-Instruct_w16a8_rw_with_gw_hp", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/newmindai/Llama-3.1-8B-Instruct_w16a8_rw_with_gw_hp
- SGLang
How to use newmindai/Llama-3.1-8B-Instruct_w16a8_rw_with_gw_hp with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "newmindai/Llama-3.1-8B-Instruct_w16a8_rw_with_gw_hp" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "newmindai/Llama-3.1-8B-Instruct_w16a8_rw_with_gw_hp", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "newmindai/Llama-3.1-8B-Instruct_w16a8_rw_with_gw_hp" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "newmindai/Llama-3.1-8B-Instruct_w16a8_rw_with_gw_hp", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use newmindai/Llama-3.1-8B-Instruct_w16a8_rw_with_gw_hp with Docker Model Runner:
docker model run hf.co/newmindai/Llama-3.1-8B-Instruct_w16a8_rw_with_gw_hp
Update README.md
Browse files
README.md
CHANGED
|
@@ -25,9 +25,8 @@ model-index:
|
|
| 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
|
| 29 |
-
The
|
| 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.
|
|
@@ -36,29 +35,14 @@ The resulting model aims to deliver accurate, context-aware legal text generatio
|
|
| 36 |
|
| 37 |
### Experiment Context
|
| 38 |
|
| 39 |
-
This model was trained
|
| 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
|
|
@@ -76,7 +60,7 @@ if use_fp8:
|
|
| 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
|
|
@@ -101,7 +85,7 @@ if use_fp8:
|
|
| 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 |
|
|
@@ -112,68 +96,62 @@ if use_fp8:
|
|
| 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 |
-
||  | || |
|
| 120 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
| Model | Training Time (mins) | Memory Allocated (avg %) | GPU Utilization (avg %) | Speed vs bf16 |
|
| 122 |
| :-------------------------------------------------- | --------------------: | -----------------------: | -----------------------: | -------------: |
|
| 123 |
| **Llama-3.1-8B-Instruct_w16a16** | 138.75267 | 74.4189 | 56.6059% | _ |
|
| 124 |
| **Llama-3.1-8B-Instruct-w16a8-tw** | 123.75267 | 68.8982 | 97.5364% | 12.11% |
|
| 125 |
| **Llama-3.1-8B-Instruct_w16a8_rw** | 115.75364 | 69.6132 | 97.7689% | 19.87% |
|
| 126 |
-
| **Llama-3.1-8B-
|
| 127 |
| **Llama-3.1-8B-Instruct-w16a8-mxtw** | 64.00328 | 68.8982 | 95.5661% | 116.82% |
|
| 128 |
|
| 129 |
-
|
| 130 |
-
##
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
- follow the steps to visualize with **Pytorch-profiler** :
|
| 134 |
1. pip install the versions that mentioned in the dependencies section of these libs tensorboard and tensorboard-data-server.
|
| 135 |
2. Visualize pytorch profiles by runing the command provided below.
|
| 136 |
```python
|
| 137 |
-
tensorboard --logdir="/gpfs/scratch/ehpc317/esavirdi/
|
| 138 |
-
````
|
| 139 |
-
|
| 140 |
-
- follow the steps to visualize with **nsight systems**:
|
| 141 |
-
1. download the files on to you local machine.
|
| 142 |
-
2. download the full_version of the nsys profiler tool from here : https://developer.nvidia.com/nsight-systems/get-started.
|
| 143 |
-
3. open the nsys-rep profiles using the nsight systems gui .
|
| 144 |
-
|
| 145 |
-
also you can get a summary of the report using these command lines up to your porpuse :
|
| 146 |
-
```python
|
| 147 |
-
nsys stats --report cuda_gpu_kern_sum /gpfs/scratch/ehpc317/esavirdi/startClean/profiles/bfp16_1node/lama3.1_fp16_baseline.nsys-rep
|
| 148 |
-
````
|
| 149 |
-
```python
|
| 150 |
-
nsys stats --report cuda_gpu_trace /gpfs/scratch/ehpc317/esavirdi/startClean/profiles/bfp16_1node/lama3.1_fp16_baseline.nsys-rep
|
| 151 |
-
````
|
| 152 |
-
```python
|
| 153 |
-
nsys stats --report cuda_gpu_mem_size_sum /gpfs/scratch/ehpc317/esavirdi/startClean/profiles/bfp16_1node/lama3.1_fp16_baseline.nsys-rep
|
| 154 |
````
|
| 155 |
|
| 156 |
|
| 157 |
-
### Usage
|
| 158 |
|
| 159 |
**Note**: the final model has been saved in bfloat16 format. For inference, load the model in bfloat16 or float16 as shown below:
|
| 160 |
|
| 161 |
```python
|
| 162 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 163 |
import torch
|
| 164 |
-
|
| 165 |
model_name = "newmindai/Llama-3.1-8B-Instruct_w16a8_rw_with_gw_hp"
|
| 166 |
dtype = torch.bfloat16
|
| 167 |
-
|
| 168 |
tok = AutoTokenizer.from_pretrained(model_name)
|
| 169 |
model = AutoModelForCausalLM.from_pretrained(
|
| 170 |
model_name,
|
| 171 |
torch_dtype=dtype,
|
| 172 |
device_map="auto"
|
| 173 |
)
|
| 174 |
-
|
| 175 |
prompt = "Soru: Kişisel Verilerin Korunması Kanunu uyarınca hangi durumlarda açık rıza aranmaz? Cevap:"
|
| 176 |
-
|
| 177 |
inputs = tok(prompt, return_tensors="pt").to(model.device)
|
| 178 |
with torch.no_grad():
|
| 179 |
out = model.generate(
|
|
@@ -184,7 +162,6 @@ with torch.no_grad():
|
|
| 184 |
|
| 185 |
print(tok.decode(out[0], skip_special_tokens=True))
|
| 186 |
````
|
| 187 |
-
|
| 188 |
## Ethical Considerations and Disclaimers
|
| 189 |
* Research & development purposes only; not a substitute for professional legal counsel.
|
| 190 |
* Users must ensure compliance with data protection and sector regulations.
|
|
@@ -200,7 +177,6 @@ print(tok.decode(out[0], skip_special_tokens=True))
|
|
| 200 |
## References and Citations
|
| 201 |
|
| 202 |
### Base Model
|
| 203 |
-
|
| 204 |
```bibtex
|
| 205 |
@misc{meta_llama31_8b_instruct,
|
| 206 |
title={Llama 3.1 8B Instruct},
|
|
@@ -209,7 +185,6 @@ print(tok.decode(out[0], skip_special_tokens=True))
|
|
| 209 |
howpublished={\url{https://huggingface.co/meta-llama/Llama-3.1-8B-Instruct}}
|
| 210 |
}
|
| 211 |
```
|
| 212 |
-
|
| 213 |
### Training Dataset
|
| 214 |
|
| 215 |
```bibtex
|
|
|
|
| 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 for the llama_3.1_8B model compared with its respective BF16 training.
|
| 29 |
+
The training experiments were conducted on the newmindai/EuroHPC-Legal corpus (Q/A format) to enhance legal-domain reasoning across multiple subfields. using mainly 4 recipies : fp8 mixed precision default cast configurations using “tensorwise” and “rowwise”,"rowwise_with_gw_hp" recipes, also a “costum” cast configuration for each of the input, weight, grad_output and compared with the baseline FSDP2+bfp16. In fp8 mixed precision, the model weights to be updated were stored in BF16, and a copy of the model input, weight and grad_output beside the activations were quantized to FP8 varients using Torchao.
|
|
|
|
| 30 |
|
| 31 |
### Setups
|
| 32 |
- **Precision**: Used Half-precision bfloat16 as data type and for computation.
|
|
|
|
| 35 |
|
| 36 |
### Experiment Context
|
| 37 |
|
| 38 |
+
This model was trained with the Float8 modified rowwise scaling method that preserves the gradient weight computation in high precision (bfloat16) to enhance accuracy recipe. In this recipe during the forward pass, both the input and weight granularities are set to AXISWISE with a default E4M3 data type, while in the backward pass for gradient input, the input granularity remains AXISWISE and the weight granularity is TENSORWISE, both using E4M3; finally, in the backward pass for gradient weight, scaling is disabled for both input and grad output—keeping them in high precision and E4M3 respectively—and the configuration further enables round_scales_to_power_of_2=True to ensure numerical stability.
|
|
|
|
|
|
|
|
|
|
| 39 |
```python
|
|
|
|
| 40 |
from torchao.float8 import (
|
| 41 |
convert_to_float8_training,
|
| 42 |
+
Float8LinearConfig)
|
|
|
|
|
|
|
|
|
|
| 43 |
config = Float8LinearConfig.from_recipe_name("rowwise_with_gw_hp")
|
| 44 |
model = convert_to_float8_training(model, config=config)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
````
|
|
|
|
|
|
|
| 46 |
## Base Model Technical Specifications
|
| 47 |
- **Parameters**: 8 Billion
|
| 48 |
- **Architecture Family**: Llama 3.1
|
|
|
|
| 60 |
|
| 61 |
## Training Methodology
|
| 62 |
|
| 63 |
+
### *Training Configuration*
|
| 64 |
- **Model**: `meta-llama/Llama-3.1-8B-Instruct`
|
| 65 |
- **Sequence Length**: 4,096 (`seq_len`)
|
| 66 |
- **Epochs**: 1
|
|
|
|
| 85 |
- **Distributed Training**: `torch.distributed.run` (single node, multi-GPU)
|
| 86 |
- FSDP2 (Optimized Fully Sharded Data Parallel)
|
| 87 |
|
| 88 |
+
### *Dependencies*
|
| 89 |
| package | Version |
|
| 90 |
|-------|--------|
|
| 91 |
| Transformers | 4.57.1 |
|
|
|
|
| 96 |
| tensorboard | 2.20.0 |
|
| 97 |
| tensorboard-data-server | 0.7.2 |
|
| 98 |
| wandb | 0.22.1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
+
## **Performance Evaluation**
|
| 101 |
+
### *2-models trained on 1Node with fp8 recipes*
|
| 102 |
+
| Loss metric results for w16a8 & tensorwise recipe| Memory allocation for w16a8 & tensorwise recipe | Utilization for w16a8 & tensorwise recipe |
|
| 103 |
+
|---------|---------|---------|
|
| 104 |
+
|  |  |  |
|
| 105 |
+
|
| 106 |
+
| Loss metric results for w16a8 recipes| Memory allocation for w16a8 recipes | Utilization for w16a8 recipes |
|
| 107 |
+
|---------|---------|---------|
|
| 108 |
+
|  |  |  |
|
| 109 |
+
|
| 110 |
+
### *Loss Analysision*
|
| 111 |
+
| Model | Max Loss (train) | Min Loss (train) | Avg Loss (train) | Final Loss (train) | ± Std (train) | Max Loss (val) | Min Loss (val) | Avg Loss (val) | Final Loss (val) | ± Std (val) |
|
| 112 |
+
| ---------------------------------------------- | ---------------- | ---------------- | ---------------- | ------------------ | ------------- | -------------- | -------------- | -------------- | ---------------- | ----------- |
|
| 113 |
+
| Llama-3.1-8B-Instruct_w16a16 | 3.1462 | 0.5710 | 0.8048 | 0.6374 | 0.2716 | 1.0517 | 0.8335 | 0.8876 | 0.8335 | 0.0678 |
|
| 114 |
+
| Llama-3.1-8B-Instruct-w16a8-tw | 3.1983 | 0.5759 | 0.8113 | 0.6419 | 0.2756 | 1.0566 | 0.8390 | 0.8925 | 0.8391 | 0.0675 |
|
| 115 |
+
| Llama-3.1-8B-Instruct_w16a8_4nodes_rw | 3.1682 | 0.5740 | 0.8118 | 0.6431 | 0.2746 | 1.0613 | 0.8394 | 0.8937 | 0.8394 | 0.0688 |
|
| 116 |
+
| Llama-3.1-8B-Instruct_w16a8_rw_with_gw_hp | 3.1837 | 0.5763 | 0.8116 | 0.6420 | 0.2751 | 1.0599 | 0.8391 | 0.8933 | 0.8391 | 0.0685 |
|
| 117 |
+
| Llama-3.1-8B-Instruct-w16a8-mxtw | 3.1983 | 0.5747 | 0.8115 | 0.6446 | 0.2758 | 1.0562 | 0.8384 | 0.8923 | 0.8384 | 0.0677 |
|
| 118 |
+
|
| 119 |
+
### *Training Time Analysision*
|
| 120 |
| Model | Training Time (mins) | Memory Allocated (avg %) | GPU Utilization (avg %) | Speed vs bf16 |
|
| 121 |
| :-------------------------------------------------- | --------------------: | -----------------------: | -----------------------: | -------------: |
|
| 122 |
| **Llama-3.1-8B-Instruct_w16a16** | 138.75267 | 74.4189 | 56.6059% | _ |
|
| 123 |
| **Llama-3.1-8B-Instruct-w16a8-tw** | 123.75267 | 68.8982 | 97.5364% | 12.11% |
|
| 124 |
| **Llama-3.1-8B-Instruct_w16a8_rw** | 115.75364 | 69.6132 | 97.7689% | 19.87% |
|
| 125 |
+
| **Llama-3.1-8B-Instruct_w16a8_rw_with_gw_hp** | 109.00364 | 69.4806 | 97.3312% | 27.33% |
|
| 126 |
| **Llama-3.1-8B-Instruct-w16a8-mxtw** | 64.00328 | 68.8982 | 95.5661% | 116.82% |
|
| 127 |
|
| 128 |
+
## **Implementation**
|
| 129 |
+
### *Gpu && Memory usage Profiling*
|
| 130 |
+
The training progress has been profiled using **pytorch-profiler** tool.
|
| 131 |
+
- follow the steps to visualize the profiles:
|
|
|
|
| 132 |
1. pip install the versions that mentioned in the dependencies section of these libs tensorboard and tensorboard-data-server.
|
| 133 |
2. Visualize pytorch profiles by runing the command provided below.
|
| 134 |
```python
|
| 135 |
+
tensorboard --logdir="/gpfs/scratch/ehpc317/esavirdi/dual_precision_training/profiles/Llama-3.1-8B-Instruct_tensorwise" --port="6006"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
````
|
| 137 |
|
| 138 |
|
| 139 |
+
### *Usage*
|
| 140 |
|
| 141 |
**Note**: the final model has been saved in bfloat16 format. For inference, load the model in bfloat16 or float16 as shown below:
|
| 142 |
|
| 143 |
```python
|
| 144 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 145 |
import torch
|
|
|
|
| 146 |
model_name = "newmindai/Llama-3.1-8B-Instruct_w16a8_rw_with_gw_hp"
|
| 147 |
dtype = torch.bfloat16
|
|
|
|
| 148 |
tok = AutoTokenizer.from_pretrained(model_name)
|
| 149 |
model = AutoModelForCausalLM.from_pretrained(
|
| 150 |
model_name,
|
| 151 |
torch_dtype=dtype,
|
| 152 |
device_map="auto"
|
| 153 |
)
|
|
|
|
| 154 |
prompt = "Soru: Kişisel Verilerin Korunması Kanunu uyarınca hangi durumlarda açık rıza aranmaz? Cevap:"
|
|
|
|
| 155 |
inputs = tok(prompt, return_tensors="pt").to(model.device)
|
| 156 |
with torch.no_grad():
|
| 157 |
out = model.generate(
|
|
|
|
| 162 |
|
| 163 |
print(tok.decode(out[0], skip_special_tokens=True))
|
| 164 |
````
|
|
|
|
| 165 |
## Ethical Considerations and Disclaimers
|
| 166 |
* Research & development purposes only; not a substitute for professional legal counsel.
|
| 167 |
* Users must ensure compliance with data protection and sector regulations.
|
|
|
|
| 177 |
## References and Citations
|
| 178 |
|
| 179 |
### Base Model
|
|
|
|
| 180 |
```bibtex
|
| 181 |
@misc{meta_llama31_8b_instruct,
|
| 182 |
title={Llama 3.1 8B Instruct},
|
|
|
|
| 185 |
howpublished={\url{https://huggingface.co/meta-llama/Llama-3.1-8B-Instruct}}
|
| 186 |
}
|
| 187 |
```
|
|
|
|
| 188 |
### Training Dataset
|
| 189 |
|
| 190 |
```bibtex
|