Improve model card: Add `library_name` metadata and comprehensive sample usage
Browse filesThis PR enhances the model card by:
- Adding `library_name: transformers` to the metadata, enabling the "how to use" button and better discoverability on the Hugging Face Hub.
- Expanding the "How to use" section with detailed code examples for text generation and demonstrating how to obtain expert routing probabilities, which is a key feature of this Mixture-of-Experts (MoE) model and aligns with the paper's focus on interpretability.
- Adding a brief introduction to summarize the paper's core contributions and the model's purpose.
README.md
CHANGED
|
@@ -1,18 +1,85 @@
|
|
| 1 |
---
|
| 2 |
-
license: apache-2.0
|
| 3 |
language:
|
| 4 |
- en
|
| 5 |
- zh
|
|
|
|
| 6 |
pipeline_tag: text-generation
|
|
|
|
| 7 |
---
|
| 8 |
|
| 9 |
# BlockFFN-XLarge
|
| 10 |
|
| 11 |
This is the original 1.2B BlockFFN checkpoint used in the paper *BlockFFN: Towards End-Side Acceleration-Friendly Mixture-of-Experts with Chunk-Level Activation Sparsity* for acceleration tests.
|
| 12 |
-
You can load and use this model simply by using `AutoTokenizer` and `AutoModelForCausalLM`.
|
| 13 |
|
| 14 |
Links: [[Paper](https://arxiv.org/pdf/2507.08771)] [[Codes](https://github.com/thunlp/BlockFFN)]
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
### Citation
|
| 17 |
|
| 18 |
If you find our work useful for your research, please kindly cite our paper as follows:
|
|
@@ -25,4 +92,4 @@ If you find our work useful for your research, please kindly cite our paper as f
|
|
| 25 |
year={2025},
|
| 26 |
url={https://arxiv.org/pdf/2507.08771},
|
| 27 |
}
|
| 28 |
-
```
|
|
|
|
| 1 |
---
|
|
|
|
| 2 |
language:
|
| 3 |
- en
|
| 4 |
- zh
|
| 5 |
+
license: apache-2.0
|
| 6 |
pipeline_tag: text-generation
|
| 7 |
+
library_name: transformers
|
| 8 |
---
|
| 9 |
|
| 10 |
# BlockFFN-XLarge
|
| 11 |
|
| 12 |
This is the original 1.2B BlockFFN checkpoint used in the paper *BlockFFN: Towards End-Side Acceleration-Friendly Mixture-of-Experts with Chunk-Level Activation Sparsity* for acceleration tests.
|
|
|
|
| 13 |
|
| 14 |
Links: [[Paper](https://arxiv.org/pdf/2507.08771)] [[Codes](https://github.com/thunlp/BlockFFN)]
|
| 15 |
|
| 16 |
+
### Introduction
|
| 17 |
+
|
| 18 |
+
**BlockFFN** presents a novel Mixture-of-Experts (MoE) architecture designed to enhance activation sparsity at both token and chunk levels, making LLMs more acceleration-friendly, especially for end-side devices. This approach integrates a new router for differentiable and flexible routing and is optimized with CLS-aware training objectives. The model achieves superior performance and significant speedup on end-side devices.
|
| 19 |
+
|
| 20 |
+
### How to Use
|
| 21 |
+
|
| 22 |
+
You can explore the core implementation of **BlockFFN** in the [GitHub repository](https://github.com/thunlp/BlockFFN). You can load and use this model simply by using `AutoTokenizer` and `AutoModelForCausalLM`.
|
| 23 |
+
|
| 24 |
+
#### Text Generation
|
| 25 |
+
|
| 26 |
+
```python
|
| 27 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
| 28 |
+
import torch
|
| 29 |
+
|
| 30 |
+
model_name = "SparseLLM/BlockFFN-XLarge" # Or other BlockFFN models like SparseLLM/BlockFFN-XLarge-sft
|
| 31 |
+
|
| 32 |
+
pipe = pipeline(
|
| 33 |
+
"text-generation",
|
| 34 |
+
model_name,
|
| 35 |
+
tokenizer=AutoTokenizer.from_pretrained(model_name),
|
| 36 |
+
torch_dtype=torch.bfloat16,
|
| 37 |
+
device_map="auto",
|
| 38 |
+
trust_remote_code=True,
|
| 39 |
+
)
|
| 40 |
+
print(pipe("The key to life is", max_new_tokens=20, do_sample=True)[0]["generated_text"])
|
| 41 |
+
```
|
| 42 |
+
|
| 43 |
+
#### Get Expert Routing Probabilities
|
| 44 |
+
|
| 45 |
+
Based on expert routing probabilities, **BlockFFN** enables mechanistic interpretability by understanding which sparse features are activated to which token. Following the standard MoE approach, you can obtain expert routing probabilities for all layers by setting `output_router_probs=True`. The example below demonstrates how to compute and analyze the expert activation patterns:
|
| 46 |
+
|
| 47 |
+
```python
|
| 48 |
+
import torch
|
| 49 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 50 |
+
|
| 51 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 52 |
+
"SparseLLM/BlockFFN-XLarge",
|
| 53 |
+
torch_dtype=torch.bfloat16,
|
| 54 |
+
device_map="auto",
|
| 55 |
+
trust_remote_code=True,
|
| 56 |
+
)
|
| 57 |
+
tokenizer = AutoTokenizer.from_pretrained("SparseLLM/BlockFFN-XLarge")
|
| 58 |
+
|
| 59 |
+
inputs = tokenizer("City and County of San Francisco", return_tensors="pt")
|
| 60 |
+
outputs = model(**inputs.to(model.device), output_router_probs=True)
|
| 61 |
+
|
| 62 |
+
# Get full expert routing probabilities: [batch_size, seq_len, moe_heads, moe_experts**2]
|
| 63 |
+
# Note: The output format for router_probs might vary based on the specific BlockFFN implementation details.
|
| 64 |
+
# This example assumes a common structure for illustration.
|
| 65 |
+
if hasattr(outputs, 'router_probs') and outputs.router_probs is not None:
|
| 66 |
+
for layer_idx, layer_router_probs in enumerate(outputs.router_probs):
|
| 67 |
+
print(f"Layer {layer_idx} Router Probs Shape: {layer_router_probs.shape}")
|
| 68 |
+
# Example: Analyze first token's expert activation in the first layer
|
| 69 |
+
if layer_router_probs.shape[1] > 0: # Check if there are tokens
|
| 70 |
+
first_token_probs = layer_router_probs[0, 0] # batch_idx, token_idx
|
| 71 |
+
# Assuming first_token_probs is [num_heads, num_experts]
|
| 72 |
+
# Sum across heads to get overall expert importance
|
| 73 |
+
expert_activations = first_token_probs.sum(dim=0)
|
| 74 |
+
activated_experts = (expert_activations > 1e-2).nonzero(as_tuple=True)[0]
|
| 75 |
+
decoded_token = tokenizer.decode(inputs.input_ids[0, 0])
|
| 76 |
+
print(f"Token: '{decoded_token}' (Layer {layer_idx}) Activated Experts Count: {len(activated_experts)}")
|
| 77 |
+
# print(f"Activated Expert Indices: {activated_experts.tolist()}")
|
| 78 |
+
else:
|
| 79 |
+
print("Model output does not contain 'router_probs'.")
|
| 80 |
+
|
| 81 |
+
```
|
| 82 |
+
|
| 83 |
### Citation
|
| 84 |
|
| 85 |
If you find our work useful for your research, please kindly cite our paper as follows:
|
|
|
|
| 92 |
year={2025},
|
| 93 |
url={https://arxiv.org/pdf/2507.08771},
|
| 94 |
}
|
| 95 |
+
```
|