添加KV Cache支持:实现prefill/decode阶段分离
- MultiHeadAttention类添加KV cache机制 - TransformerBlock支持use_cache参数 - SimpleTransformer新增prefill和decode方法 - 添加InferenceEngine推理引擎类 - 更新example.py演示推理过程 - 更新README.md文档
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
# Simple Transformer
|
||||
|
||||
A minimal Transformer implementation using NumPy.
|
||||
A minimal Transformer implementation using NumPy with **KV Cache** support for efficient inference.
|
||||
|
||||
## Files
|
||||
|
||||
- `transformer.py` - Core Transformer model implementation
|
||||
- `example.py` - Usage example
|
||||
- `transformer.py` - Core Transformer model implementation with KV Cache
|
||||
- `example.py` - Usage examples (training & inference)
|
||||
|
||||
## Usage
|
||||
|
||||
@@ -21,6 +21,21 @@ The implementation includes:
|
||||
- **Feed-Forward Network**: Two-layer fully connected network with ReLU activation
|
||||
- **Layer Normalization**: Applied after each sub-layer
|
||||
- **Positional Encoding**: Sinusoidal position embeddings
|
||||
- **KV Cache**: Efficient inference with prefill/decode separation
|
||||
|
||||
## Inference: Prefill vs Decode
|
||||
|
||||
### Prefill Stage
|
||||
- Process the complete input prompt in parallel
|
||||
- Initialize KV cache for all layers
|
||||
- Generate the first token
|
||||
- Computation: O(n²)
|
||||
|
||||
### Decode Stage
|
||||
- Process one token at a time
|
||||
- Reuse cached Key and Value tensors
|
||||
- Only compute new Query
|
||||
- Computation: O(n) per step
|
||||
|
||||
## Model Parameters
|
||||
|
||||
@@ -32,4 +47,9 @@ Default configuration:
|
||||
- Feed-forward dimension: 2048
|
||||
- Max sequence length: 100
|
||||
|
||||
Total parameters: ~19.4M
|
||||
Total parameters: ~19.4M
|
||||
|
||||
## Classes
|
||||
|
||||
- `SimpleTransformer` - Transformer model with prefill/decode methods
|
||||
- `InferenceEngine` - Manages inference process with KV cache
|
||||
Reference in New Issue
Block a user