jburtoft commited on
Commit
8dc4e67
·
verified ·
1 Parent(s): f61d529

Upload README.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +222 -0
README.md ADDED
@@ -0,0 +1,222 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ base_model: Qwen/Qwen2.5-7B-Instruct
4
+ tags:
5
+ - neuronx-distributed-inference
6
+ - neuron
7
+ - aws-inferentia
8
+ - inf2
9
+ - pre-compiled
10
+ - qwen2
11
+ language:
12
+ - en
13
+ pipeline_tag: text-generation
14
+ ---
15
+
16
+ # Qwen2.5-7B-Instruct Pre-Compiled for AWS Inferentia2 (TP=2)
17
+
18
+ Pre-compiled and pre-sharded [Qwen2.5-7B-Instruct](https://huggingface.co/Qwen/Qwen2.5-7B-Instruct) for AWS Neuron SDK 2.28, ready to load on **inf2.xlarge** (16 GB system RAM) or any larger Inferentia2/Trainium instance.
19
+
20
+ ## Why Pre-Sharded?
21
+
22
+ The standard NxDI load path loads the full HuggingFace checkpoint (~14 GB BF16) into CPU RAM for weight conversion and sharding. On inf2.xlarge (16 GB system RAM), this causes an OOM kill at ~14 GB RSS.
23
+
24
+ Pre-sharded weights bypass this entirely — NxDI reads directly from the per-rank sharded files, peaking at **~13.5 GB RSS** during load (tight but viable on 16 GB) and settling to **~4.3 GB RSS** after device transfer.
25
+
26
+ ## Contents
27
+
28
+ | File | Size | Description |
29
+ |------|------|-------------|
30
+ | `model.pt` | ~153 MB | Compiled Neuron NEFF graphs |
31
+ | `neuron_config.json` | ~8 KB | NxDI configuration (TP=2, BS=1, seq_len=8192, BF16) |
32
+ | `weights/tp0_sharded_checkpoint.safetensors` | ~7.3 GB | Pre-sharded model weights for rank 0 |
33
+ | `weights/tp1_sharded_checkpoint.safetensors` | ~7.3 GB | Pre-sharded model weights for rank 1 |
34
+ | `config.json` | <1 KB | HuggingFace model config |
35
+ | `tokenizer.json` | ~6.8 MB | Tokenizer |
36
+ | `tokenizer_config.json` | ~7 KB | Tokenizer configuration |
37
+ | `generation_config.json` | <1 KB | Default generation parameters |
38
+ | `vocab.json` | ~2.7 MB | Vocabulary |
39
+ | `merges.txt` | ~1.6 MB | BPE merges |
40
+
41
+ ## Performance
42
+
43
+ Measured on inf2.xlarge (2 NeuronCores, 32 GB HBM, 4 vCPU, 16 GB system RAM):
44
+
45
+ | Metric | Value |
46
+ |--------|-------|
47
+ | Throughput | 24.1 tok/s |
48
+ | Latency (4K in / 4K out) | 169.8 s |
49
+ | Load time | ~330 s |
50
+ | Peak RSS during load | ~13.5 GB |
51
+ | RSS after load | ~4.3 GB |
52
+ | Cost | $8.76/M output tokens at $0.76/hr |
53
+
54
+ Benchmark: batch_size=1, 4095 input tokens, 4096 output tokens, greedy decoding, 2 warmup + 10 measured requests.
55
+
56
+ ## Quick Start
57
+
58
+ ### Prerequisites
59
+
60
+ - AWS instance with Inferentia2: **inf2.xlarge** (minimum), inf2.8xlarge, or larger
61
+ - [Deep Learning AMI Neuron (Ubuntu 24.04) 20260227](https://aws.amazon.com/marketplace/) (SDK 2.28)
62
+ - Activate the pre-installed venv:
63
+ ```bash
64
+ source /opt/aws_neuronx_venv_pytorch_inference_vllm_0_13/bin/activate
65
+ ```
66
+
67
+ ### 1. Download the model
68
+
69
+ ```bash
70
+ pip install -q huggingface_hub
71
+ python3 -c "
72
+ from huggingface_hub import snapshot_download
73
+ snapshot_download('jburtoft/Qwen2.5-7B-Instruct-Neuron-TP2',
74
+ local_dir='/data/Qwen2.5-7B-Instruct-Neuron-TP2')
75
+ "
76
+ ```
77
+
78
+ ### 2. Load and run inference
79
+
80
+ ```python
81
+ import os
82
+ import torch
83
+ from transformers import AutoTokenizer, GenerationConfig
84
+ from neuronx_distributed_inference.models.config import NeuronConfig, OnDeviceSamplingConfig
85
+ from neuronx_distributed_inference.models.qwen2.modeling_qwen2 import (
86
+ NeuronQwen2ForCausalLM, Qwen2InferenceConfig,
87
+ )
88
+ from neuronx_distributed_inference.utils.hf_adapter import load_pretrained_config
89
+ from neuronx_distributed_inference.utils.accuracy import get_generate_outputs
90
+
91
+ MODEL_DIR = "/data/Qwen2.5-7B-Instruct-Neuron-TP2"
92
+
93
+ os.environ["NEURON_LOGICAL_NC_CONFIG"] = "1"
94
+
95
+ neuron_config = NeuronConfig(
96
+ tp_degree=2,
97
+ batch_size=1,
98
+ seq_len=8192,
99
+ n_positions=8192,
100
+ max_context_length=8192,
101
+ torch_dtype=torch.bfloat16,
102
+ on_device_sampling_config=OnDeviceSamplingConfig(),
103
+ fused_qkv=True,
104
+ attn_kernel_enabled=False, # inf2 does not support flash attention
105
+ enable_bucketing=True,
106
+ logical_nc_config=1, # inf2 requires LNC=1
107
+ save_sharded_checkpoint=True, # must match how the model was compiled
108
+ )
109
+
110
+ config = Qwen2InferenceConfig(
111
+ neuron_config,
112
+ load_config=load_pretrained_config(MODEL_DIR),
113
+ )
114
+
115
+ model = NeuronQwen2ForCausalLM(MODEL_DIR, config)
116
+ model.load(MODEL_DIR) # loads from pre-sharded weights
117
+
118
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_DIR, padding_side="right")
119
+ tokenizer.pad_token = tokenizer.eos_token
120
+
121
+ # Generate
122
+ prompt = "Explain quantum computing in simple terms."
123
+ generation_config = GenerationConfig(
124
+ max_new_tokens=256,
125
+ do_sample=False,
126
+ pad_token_id=tokenizer.eos_token_id,
127
+ )
128
+
129
+ outputs, decoded_texts = get_generate_outputs(
130
+ model, [prompt], tokenizer, is_hf=False, generation_config=generation_config,
131
+ )
132
+
133
+ print(decoded_texts[0])
134
+ ```
135
+
136
+ ### 3. Important notes
137
+
138
+ - **LNC=1 is required** on inf2. Set `NEURON_LOGICAL_NC_CONFIG=1` before loading.
139
+ - **Flash attention is not supported** on inf2 (trn1-era cores). Use `attn_kernel_enabled=False`.
140
+ - **First load takes ~5-6 minutes** as the sharded weights (14.6 GB total) are read from disk and transferred to device.
141
+ - **First import may be slow** (~3-5 min) on a fresh DLAMI instance due to library rehydration.
142
+ - **`save_sharded_checkpoint=True` must be set** in the NeuronConfig when loading — this tells NxDI to use the per-rank sharded files instead of the standard HF checkpoint loading path.
143
+
144
+ ## Compilation Details
145
+
146
+ | Parameter | Value |
147
+ |-----------|-------|
148
+ | SDK | 2.28 (NxDI 0.8.0, neuronx-cc 2.22, torch-neuronx 2.9.0) |
149
+ | TP degree | 2 |
150
+ | Batch size | 1 |
151
+ | Sequence length | 8192 |
152
+ | Dtype | bfloat16 |
153
+ | Flash attention | Disabled (inf2 constraint) |
154
+ | LNC | 1 (inf2 constraint) |
155
+ | `save_sharded_checkpoint` | True |
156
+ | Compiled on | inf2.8xlarge (32 vCPU, 128 GB RAM) |
157
+
158
+ ## Compiling Your Own
159
+
160
+ To compile for different configurations (e.g., different TP, batch size, or sequence length), use a larger instance (inf2.8xlarge or trn2.3xlarge):
161
+
162
+ ```python
163
+ import os
164
+ import torch
165
+ from neuronx_distributed_inference.models.config import NeuronConfig, OnDeviceSamplingConfig
166
+ from neuronx_distributed_inference.models.qwen2.modeling_qwen2 import (
167
+ NeuronQwen2ForCausalLM, Qwen2InferenceConfig,
168
+ )
169
+ from neuronx_distributed_inference.utils.hf_adapter import load_pretrained_config
170
+
171
+ # Download the base model first
172
+ # pip install huggingface_hub
173
+ # from huggingface_hub import snapshot_download
174
+ # snapshot_download("Qwen/Qwen2.5-7B-Instruct", local_dir="/data/models/Qwen2.5-7B-Instruct")
175
+
176
+ MODEL_PATH = "/data/models/Qwen2.5-7B-Instruct"
177
+ OUTPUT_PATH = "/data/compiled/Qwen2.5-7B-TP2-sharded"
178
+
179
+ os.environ["NEURON_LOGICAL_NC_CONFIG"] = "1"
180
+
181
+ neuron_config = NeuronConfig(
182
+ tp_degree=2, # adjust as needed
183
+ batch_size=1, # adjust as needed
184
+ seq_len=8192, # adjust as needed
185
+ n_positions=8192,
186
+ max_context_length=8192,
187
+ torch_dtype=torch.bfloat16,
188
+ on_device_sampling_config=OnDeviceSamplingConfig(),
189
+ fused_qkv=True,
190
+ attn_kernel_enabled=False, # False for inf2, True for trn2
191
+ enable_bucketing=True,
192
+ logical_nc_config=1, # 1 for inf2, 1 or 2 for trn2
193
+ save_sharded_checkpoint=True, # REQUIRED for pre-sharded deployment
194
+ )
195
+
196
+ config = Qwen2InferenceConfig(
197
+ neuron_config,
198
+ load_config=load_pretrained_config(MODEL_PATH),
199
+ )
200
+
201
+ model = NeuronQwen2ForCausalLM(MODEL_PATH, config)
202
+ model.compile(OUTPUT_PATH)
203
+
204
+ # Output:
205
+ # OUTPUT_PATH/model.pt (compiled NEFFs)
206
+ # OUTPUT_PATH/neuron_config.json (NxDI config)
207
+ # OUTPUT_PATH/weights/tp0_sharded_checkpoint.safetensors (rank 0 weights)
208
+ # OUTPUT_PATH/weights/tp1_sharded_checkpoint.safetensors (rank 1 weights)
209
+ ```
210
+
211
+ Compilation takes approximately 8-9 minutes on inf2.8xlarge.
212
+
213
+ ## Base Model
214
+
215
+ - **Model**: [Qwen/Qwen2.5-7B-Instruct](https://huggingface.co/Qwen/Qwen2.5-7B-Instruct)
216
+ - **Architecture**: Qwen2 (decoder-only transformer)
217
+ - **Parameters**: 7.6B
218
+ - **License**: Apache 2.0
219
+
220
+ ## Acknowledgments
221
+
222
+ Part of the [Flav-benchmark](https://github.com/jimburtoft) project benchmarking Qwen2.5 inference across Neuron frameworks (NxDI, vLLM-neuron, optimum-neuron) and GPU baselines.