Skip to content

Commit 9b0ed9d

Browse files
authored
Fix all four scaling multipliers for Granite (#48)
1 parent 3af1ba1 commit 9b0ed9d

3 files changed

Lines changed: 413 additions & 141 deletions

File tree

contrib/models/granite-3.1-8b-instruct/README.md

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,45 +10,69 @@ NeuronX Distributed Inference implementation of granite 3.1 8b instruct.
1010

1111
## Architecture Details
1212

13-
- **Layers:** Check model config
14-
- **Hidden Size:** Check model config
15-
- **Attention Heads:** Check model config
16-
- **Vocabulary:** Check model config
17-
- **Max Position Embeddings:** Check model config
13+
- **Layers:** 32
14+
- **Hidden Size:** 4096
15+
- **Attention Heads:** 32
16+
- **Key-Value Heads:** 8 (GQA)
17+
- **Vocabulary:** 49152
18+
- **Max Position Embeddings:** 131072
19+
20+
### Granite-Specific Scaling Factors
21+
22+
Granite uses custom scaling factors that differ from standard Llama:
23+
24+
| Parameter | Value | Description |
25+
|-----------|-------|-------------|
26+
| `embedding_multiplier` | 12.0 | Scales input embeddings after lookup |
27+
| `attention_multiplier` | 0.0078125 (1/head_dim) | Custom attention scaling instead of 1/√head_dim |
28+
| `residual_multiplier` | 0.22 | Scales residual connections |
29+
| `logits_scaling` | 16.0 | Divides output logits |
1830

1931
## Validation Results
2032

21-
**Validated:** 2026-01-29
33+
**Validated:** 2026-02-06
2234
**Configuration:** TP=2, batch_size=1, seq_len=128, bfloat16
2335

2436
### Test Results
2537

2638
| Test | Status | Result |
2739
|------|--------|--------|
2840
| Smoke Test | ✅ PASS | Model loads successfully |
29-
| Token Matching | ⚠️ LOW | **7.8% match** |
30-
| TTFT (P50) | ✅ PASS | 19.44ms (threshold: 100ms) |
31-
| Throughput | ✅ PASS | 106.00 tok/s (threshold: 10 tok/s) |
41+
| Token Matching | ✅ PASS | **100% match** (64/64 tokens) |
42+
| TTFT (P50) | ✅ PASS | ~20ms (threshold: 100ms) |
43+
| Throughput | ✅ PASS | ~100 tok/s (threshold: 10 tok/s) |
3244

3345
### Performance Metrics
3446

3547
| Metric | Value |
3648
|--------|-------|
37-
| TTFT (P50) | 19.44ms |
38-
| Throughput | 106.00 tokens/s |
39-
49+
| TTFT (P50) | ~20ms |
50+
| Throughput | ~100 tokens/s |
4051

4152
**Status:** ✅ VALIDATED
4253

54+
## Critical Implementation Notes
55+
56+
This implementation includes critical fixes for Granite's custom scaling:
57+
58+
1. **Attention Multiplier Fix**: The `prep_qkv_tensors` method in `NeuronGraniteAttention` applies a correction factor to Q tensors to convert from the standard `1/√head_dim` scaling to Granite's `attention_multiplier`.
59+
60+
2. **Embedding Multiplier**: Applied in `get_model_output` after embedding lookup (not to weights, to handle tied embeddings correctly).
61+
62+
3. **Logits Scaling**: Applied via `ScaledColumnParallelLinear` which divides output by `logits_scaling`.
63+
64+
4. **Residual Multiplier**: Applied in `NeuronGraniteDecoderLayer` to scale residual connections.
65+
4366
## Usage
4467

4568
```python
46-
from transformers import AutoTokenizer, GenerationConfig
69+
import torch
70+
from transformers import AutoTokenizer
4771
from neuronx_distributed_inference.models.config import NeuronConfig
4872
from neuronx_distributed_inference.utils.hf_adapter import load_pretrained_config
4973

5074
# Import model classes from src
51-
from src.modeling_granite_3_1_8b_instruct import Neurongranite318binstructForCausalLM, granite318binstructInferenceConfig
75+
from src.modeling_granite import NeuronGraniteForCausalLM, GraniteInferenceConfig
5276

5377
model_path = "/path/to/granite-3.1-8b-instruct/"
5478
compiled_model_path = "/path/to/compiled/"
@@ -61,18 +85,19 @@ neuron_config = NeuronConfig(
6185
torch_dtype=torch.bfloat16,
6286
)
6387

64-
config = granite318binstructInferenceConfig(
88+
config = GraniteInferenceConfig(
6589
neuron_config,
6690
load_config=load_pretrained_config(model_path),
6791
)
6892

6993
# Compile and load
70-
model = Neurongranite318binstructForCausalLM(model_path, config)
94+
model = NeuronGraniteForCausalLM(model_path, config)
7195
model.compile(compiled_model_path)
7296
model.load(compiled_model_path)
7397

7498
# Generate
7599
tokenizer = AutoTokenizer.from_pretrained(model_path)
100+
inputs = tokenizer("Hello, how are you?", return_tensors="pt")
76101
# ... (see integration test for full example)
77102
```
78103

@@ -104,6 +129,6 @@ python3 test/integration/test_model.py
104129

105130
## Maintainer
106131

107-
Neuroboros Team - Annapurna Labs
132+
Annapurna Labs
108133

109-
**Last Updated:** 2026-01-29
134+
**Last Updated:** 2026-02-06

0 commit comments

Comments
 (0)