Update README.md
Browse files
README.md
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
-
#
|
|
|
|
|
|
|
| 2 |
|
| 3 |
- MXINT8 dot product
|
| 4 |
```python
|
|
@@ -25,4 +27,78 @@ output = gevm(vector, matrix)
|
|
| 25 |
# other.shape: [k, n]
|
| 26 |
# output.shape: [m, n]
|
| 27 |
output = gemm(input, other)
|
| 28 |
-
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Reference Operations V2
|
| 2 |
+
|
| 3 |
+
## Dot Product
|
| 4 |
|
| 5 |
- MXINT8 dot product
|
| 6 |
```python
|
|
|
|
| 27 |
# other.shape: [k, n]
|
| 28 |
# output.shape: [m, n]
|
| 29 |
output = gemm(input, other)
|
| 30 |
+
```
|
| 31 |
+
|
| 32 |
+
## RoPE constants
|
| 33 |
+
|
| 34 |
+
### BF16
|
| 35 |
+
|
| 36 |
+
Dumped tensors:
|
| 37 |
+
|
| 38 |
+
`rope.freq_bf16`:
|
| 39 |
+
- shape: `[1, 4096, 64]`, 4096 denotes maximum sequence length, 64 is half of the head dimension (128/2=64)
|
| 40 |
+
- each element is $m \theta_i$ where $m$ is the position id, $i$ is the head dimension index.
|
| 41 |
+
|
| 42 |
+
`rope.cos_bf16`: `cos_bf16 = cos(freq_bf16)`
|
| 43 |
+
|
| 44 |
+
`rope.sin_bf16`: `sin_bf16 = sin(freq_bf16)`
|
| 45 |
+
|
| 46 |
+
### FP32 for reference
|
| 47 |
+
|
| 48 |
+
HuggingFace forces the RoPE constants to be in FP32 format because both BF16 and FP16 introduce errors in the output cos and sin values. For example, BF16 cannot represent the exact value of 4088, 4089, ..., 4095 (all these values are rounded to 4096). [This issue](https://github.com/huggingface/transformers/pull/29285) discusses this problem.
|
| 49 |
+
|
| 50 |
+
Therefore, we also provide the FP32 version of the RoPE constants for reference. This assumes all the constants are computed in FP32 format.
|
| 51 |
+
|
| 52 |
+
- `rope.freq_fp32`
|
| 53 |
+
- `rope.cos_fp32`
|
| 54 |
+
- `rope.sin_fp32`
|
| 55 |
+
|
| 56 |
+
## Softmax
|
| 57 |
+
|
| 58 |
+
`softmax_v` is a function that computes the `matmul(softmax(qk_T), v)` operation in the attention layer. To match the HW behaviour, the operation can be broken down into the following steps:
|
| 59 |
+
|
| 60 |
+
```python
|
| 61 |
+
def softmaxed_v(qk_T, v):
|
| 62 |
+
"""
|
| 63 |
+
qk_T.shape: [bs, num_heads, seq_q_len, seq_kv_len]
|
| 64 |
+
v.shape: [bs, num_heads, seq_kv_len, head_dim]
|
| 65 |
+
"""
|
| 66 |
+
|
| 67 |
+
# elementwise exp
|
| 68 |
+
exp_bf16 = row_exp(qk_T, dim=-1) # shape: [bs, num_heads, seq_q_len, seq_kv_len]
|
| 69 |
+
# sum exp along the head dimension
|
| 70 |
+
exp_sum = reduce_sum(exp_bf16, dim=-1) # shape: [bs, num_heads, seq_q_len, 1]
|
| 71 |
+
# quantize to mxint8 for vector-matrix multiplication
|
| 72 |
+
exp_mxint8 = quantize(exp_bf16)
|
| 73 |
+
# vector-matrix multiplication between two mxint8 tensors
|
| 74 |
+
scaled_v = mxint8_gevm(exp_mxint8, v) # shape: [bs, num_heads, seq_q_len, head_dim]
|
| 75 |
+
# inverse of the sum
|
| 76 |
+
inv = 1.0 / exp_sum # shape: [bs, num_heads, seq_q_len, 1]
|
| 77 |
+
# adjust scaled_v by the inverse
|
| 78 |
+
out = scaled_v * inv # shape: [bs, num_heads, seq_q_len, head_dim]
|
| 79 |
+
return out
|
| 80 |
+
```
|
| 81 |
+
|
| 82 |
+
| Notation | Description |
|
| 83 |
+
| --- | --- |
|
| 84 |
+
| `bs` | batch size, 1 in this case |
|
| 85 |
+
| `num_heads` | number of attention heads, 32 for llama-2-7b |
|
| 86 |
+
| `seq_q_len` | query sequence length, 1 in this case |
|
| 87 |
+
| `seq_kv_len` | key-value sequence length, which increments by 1 for each decoding step |
|
| 88 |
+
| `head_dim` | head dimension, 128 for llama-2-7b |
|
| 89 |
+
|
| 90 |
+
The folder name takes the form `kv-size-<seq_kv_len>_seed-<seed>`.
|
| 91 |
+
For example, `kv-size-4_seed-0` means the key-value sequence length is 4 and the random seed is 0.
|
| 92 |
+
|
| 93 |
+
The following tensors are dumped:
|
| 94 |
+
|
| 95 |
+
| File Name | Description |
|
| 96 |
+
| --- | --- |
|
| 97 |
+
| `softmaxed_v.qk_T` | BF16 `qk_T` tensor |
|
| 98 |
+
| `softmaxed_v.exp-bf16` | BF16 `exp_bf16` tensor |
|
| 99 |
+
| `softmaxed_v.exp_sum` | BF16 `exp_sum` tensor |
|
| 100 |
+
| `softmaxed_v.v` | MXINT8 `v` tensor |
|
| 101 |
+
| `softmaxed_v.exp_mxint8` | MXINT8 `exp_mxint8` tensor |
|
| 102 |
+
| `softmaxed_v.scaled_v` | BF16 `scaled_v` tensor |
|
| 103 |
+
| `softmaxed_v.inv` | BF16 `inv` tensor |
|
| 104 |
+
| `softmaxed_v.out` | BF16 `out` tensor |
|