Merge branch 'main' of https://huggingface.co/inclusionAI/Ling-plus
Browse files- LICENCE +21 -0
- README.md +86 -3
- ant-bailing.png +0 -0
- modeling_bailing_moe.py +13 -8
LICENCE
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
MIT License
|
2 |
+
|
3 |
+
Copyright (c) 2025 inclusionAI
|
4 |
+
|
5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6 |
+
of this software and associated documentation files (the "Software"), to deal
|
7 |
+
in the Software without restriction, including without limitation the rights
|
8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9 |
+
copies of the Software, and to permit persons to whom the Software is
|
10 |
+
furnished to do so, subject to the following conditions:
|
11 |
+
|
12 |
+
The above copyright notice and this permission notice shall be included in all
|
13 |
+
copies or substantial portions of the Software.
|
14 |
+
|
15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21 |
+
SOFTWARE.
|
README.md
CHANGED
@@ -1,3 +1,86 @@
|
|
1 |
-
---
|
2 |
-
license: mit
|
3 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
license: mit
|
3 |
+
---
|
4 |
+
|
5 |
+
# Ling
|
6 |
+
|
7 |
+
<p align="center">
|
8 |
+
<img src="https://huggingface.co/inclusionAI/Ling-plus/resolve/main/ant-bailing.png" width="100"/>
|
9 |
+
<p>
|
10 |
+
|
11 |
+
<p align="center">
|
12 |
+
🤗 <a href="https://huggingface.co/inclusionAI">Hugging Face</a>
|
13 |
+
<p>
|
14 |
+
|
15 |
+
## Introduction
|
16 |
+
|
17 |
+
Ling is a MoE LLM provided and open-sourced by InclusionAI. We introduce two different sizes, which are Ling-Lite and Ling-Plus. Ling-Lite has 16.8 billion parameters with 2.75 billion activated parameters, while Ling-Plus has 290 billion parameters with 28.8 billion activated parameters. Both models demonstrate impressive performance compared to existing models in the industry.
|
18 |
+
|
19 |
+
Their structure makes it easy to scale up and down and adapt to different tasks, so users can use these models for a wide range of tasks, from processing natural language to solving complex problems. Furthermore, the open-source nature of Ling promotes collaboration and innovation within the AI community, fostering a diverse range of use cases and enhancements.
|
20 |
+
|
21 |
+
As more developers and researchers engage with the platform, we can expect rapid advancements and improvements, leading to even more sophisticated applications. This collaborative approach accelerates development and ensures that the models remain at the forefront of technology, addressing emerging challenges in various fields.
|
22 |
+
|
23 |
+
## Model Downloads
|
24 |
+
|
25 |
+
You can download the following table to see the various parameters for your use case. If you are located in mainland China, we also provide the model on Modulescope.cn to speed up the download process.
|
26 |
+
|
27 |
+
<div align="center">
|
28 |
+
|
29 |
+
| **Model** | **#Total Params** | **#Activated Params** | **Context Length** | **Download** |
|
30 |
+
| :----------------: | :---------------: | :-------------------: | :----------------: | :----------: |
|
31 |
+
| Ling-plus-base | 290B | 28.8B | 64K | [🤗 HuggingFace](https://huggingface.co/inclusionAI/Ling-plus-base) |
|
32 |
+
| Ling-plus | 290B | 28.8B | 64K | [🤗 HuggingFace](https://huggingface.co/inclusionAI/Ling-plus) |
|
33 |
+
</div>
|
34 |
+
|
35 |
+
## Evaluation
|
36 |
+
|
37 |
+
Detailed evaluation results are reported in our technical report [TBD].
|
38 |
+
|
39 |
+
## Quickstart
|
40 |
+
### 🤗 Hugging Face Transformers
|
41 |
+
|
42 |
+
Here is a code snippet to show you how to use the chat model with `transformers`:
|
43 |
+
|
44 |
+
```python
|
45 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
46 |
+
|
47 |
+
model_name = "inclusionAI/Ling-lite"
|
48 |
+
|
49 |
+
model = AutoModelForCausalLM.from_pretrained(
|
50 |
+
model_name,
|
51 |
+
torch_dtype="auto",
|
52 |
+
device_map="auto"
|
53 |
+
)
|
54 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
55 |
+
|
56 |
+
prompt = "Give me a short introduction to large language models."
|
57 |
+
messages = [
|
58 |
+
{"role": "system", "content": "You are Ling, an assistant created by inclusionAI"},
|
59 |
+
{"role": "user", "content": prompt}
|
60 |
+
]
|
61 |
+
text = tokenizer.apply_chat_template(
|
62 |
+
messages,
|
63 |
+
tokenize=False,
|
64 |
+
add_generation_prompt=True
|
65 |
+
)
|
66 |
+
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
67 |
+
|
68 |
+
generated_ids = model.generate(
|
69 |
+
**model_inputs,
|
70 |
+
max_new_tokens=512
|
71 |
+
)
|
72 |
+
generated_ids = [
|
73 |
+
output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
|
74 |
+
]
|
75 |
+
|
76 |
+
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
77 |
+
```
|
78 |
+
|
79 |
+
## Deployment
|
80 |
+
Please refer to [Github](https://github.com/inclusionAI/Ling/blob/master/README.md)
|
81 |
+
|
82 |
+
## License
|
83 |
+
This code repository is licensed under [the MIT License](https://huggingface.co/inclusionAI/Ling-plus/blob/main/LICENCE).
|
84 |
+
|
85 |
+
## Citation
|
86 |
+
[TBD]
|
ant-bailing.png
ADDED
![]() |
modeling_bailing_moe.py
CHANGED
@@ -117,8 +117,8 @@ class BailingMoeRMSNorm(nn.Module):
|
|
117 |
hidden_states = hidden_states.to(torch.float32)
|
118 |
variance = hidden_states.pow(2).mean(-1, keepdim=True)
|
119 |
hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon)
|
|
|
120 |
|
121 |
-
return (self.weight.float() * hidden_states).to(input_dtype)
|
122 |
|
123 |
ALL_LAYERNORM_LAYERS.append(BailingMoeRMSNorm)
|
124 |
|
@@ -495,7 +495,7 @@ class BailingMoeAttention(nn.Module):
|
|
495 |
key_states = repeat_kv(key_states, self.num_key_value_groups)
|
496 |
value_states = repeat_kv(value_states, self.num_key_value_groups)
|
497 |
|
498 |
-
attn_weights = torch.matmul(query_states / math.sqrt(self.head_dim), key_states.transpose(2, 3))
|
499 |
|
500 |
if attn_weights.size() != (bsz, self.num_heads, q_len, kv_seq_len):
|
501 |
raise ValueError(
|
@@ -825,7 +825,6 @@ class BailingMoeSdpaAttention(BailingMoeAttention):
|
|
825 |
dropout_p=self.attention_dropout if self.training else 0.0,
|
826 |
# The q_len > 1 is necessary to match with AttentionMaskConverter.to_causal_4d that does not create a causal mask in case q_len == 1.
|
827 |
is_causal=self.is_causal and attention_mask is None and q_len > 1,
|
828 |
-
# enable_gqa=True
|
829 |
)
|
830 |
|
831 |
attn_output = attn_output.transpose(1, 2).contiguous()
|
@@ -847,6 +846,7 @@ class BailingMoeDecoderLayer(nn.Module):
|
|
847 |
def __init__(self, config: BailingMoeConfig, layer_idx: int):
|
848 |
super().__init__()
|
849 |
self.hidden_size = config.hidden_size
|
|
|
850 |
self.attention = BAILING_MOE_ATTENTION_CLASSES[config._attn_implementation](config=config, layer_idx=layer_idx)
|
851 |
|
852 |
self.mlp = (
|
@@ -1167,7 +1167,7 @@ class BailingMoeModel(BailingMoePreTrainedModel):
|
|
1167 |
all_router_logits = () if output_router_logits else None
|
1168 |
next_decoder_cache = None
|
1169 |
|
1170 |
-
for
|
1171 |
if output_hidden_states:
|
1172 |
all_hidden_states += (hidden_states,)
|
1173 |
|
@@ -1332,9 +1332,10 @@ class BailingMoeForCausalLM(BailingMoePreTrainedModel):
|
|
1332 |
)
|
1333 |
logits = F.linear(hidden_states, norm_weight, None)
|
1334 |
else:
|
1335 |
-
self.lm_head.weight.data = (
|
1336 |
-
|
1337 |
-
|
|
|
1338 |
logits = F.linear(hidden_states, self.lm_head.weight.data, None)
|
1339 |
self.norm_head = False
|
1340 |
else:
|
@@ -1380,7 +1381,11 @@ class BailingMoeForCausalLM(BailingMoePreTrainedModel):
|
|
1380 |
if isinstance(past_key_values, Cache):
|
1381 |
cache_length = past_key_values.get_seq_length()
|
1382 |
past_length = past_key_values.seen_tokens
|
1383 |
-
max_cache_length =
|
|
|
|
|
|
|
|
|
1384 |
else:
|
1385 |
cache_length = past_length = past_key_values[0][0].shape[2]
|
1386 |
max_cache_length = None
|
|
|
117 |
hidden_states = hidden_states.to(torch.float32)
|
118 |
variance = hidden_states.pow(2).mean(-1, keepdim=True)
|
119 |
hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon)
|
120 |
+
return self.weight * hidden_states.to(input_dtype)
|
121 |
|
|
|
122 |
|
123 |
ALL_LAYERNORM_LAYERS.append(BailingMoeRMSNorm)
|
124 |
|
|
|
495 |
key_states = repeat_kv(key_states, self.num_key_value_groups)
|
496 |
value_states = repeat_kv(value_states, self.num_key_value_groups)
|
497 |
|
498 |
+
attn_weights = torch.matmul(query_states / math.sqrt(self.head_dim), key_states.transpose(2, 3))
|
499 |
|
500 |
if attn_weights.size() != (bsz, self.num_heads, q_len, kv_seq_len):
|
501 |
raise ValueError(
|
|
|
825 |
dropout_p=self.attention_dropout if self.training else 0.0,
|
826 |
# The q_len > 1 is necessary to match with AttentionMaskConverter.to_causal_4d that does not create a causal mask in case q_len == 1.
|
827 |
is_causal=self.is_causal and attention_mask is None and q_len > 1,
|
|
|
828 |
)
|
829 |
|
830 |
attn_output = attn_output.transpose(1, 2).contiguous()
|
|
|
846 |
def __init__(self, config: BailingMoeConfig, layer_idx: int):
|
847 |
super().__init__()
|
848 |
self.hidden_size = config.hidden_size
|
849 |
+
|
850 |
self.attention = BAILING_MOE_ATTENTION_CLASSES[config._attn_implementation](config=config, layer_idx=layer_idx)
|
851 |
|
852 |
self.mlp = (
|
|
|
1167 |
all_router_logits = () if output_router_logits else None
|
1168 |
next_decoder_cache = None
|
1169 |
|
1170 |
+
for decoder_layer in self.layers:
|
1171 |
if output_hidden_states:
|
1172 |
all_hidden_states += (hidden_states,)
|
1173 |
|
|
|
1332 |
)
|
1333 |
logits = F.linear(hidden_states, norm_weight, None)
|
1334 |
else:
|
1335 |
+
self.lm_head.weight.data = (
|
1336 |
+
self.lm_head.weight.data.float()
|
1337 |
+
/ (torch.norm(self.lm_head.weight.data.float(), p=2, dim=0, keepdim=True) + 1e-7)
|
1338 |
+
).to(hidden_states.dtype)
|
1339 |
logits = F.linear(hidden_states, self.lm_head.weight.data, None)
|
1340 |
self.norm_head = False
|
1341 |
else:
|
|
|
1381 |
if isinstance(past_key_values, Cache):
|
1382 |
cache_length = past_key_values.get_seq_length()
|
1383 |
past_length = past_key_values.seen_tokens
|
1384 |
+
max_cache_length = (
|
1385 |
+
past_key_values.get_max_length()
|
1386 |
+
if hasattr(past_key_values, "get_max_length")
|
1387 |
+
else past_key_values.get_max_cache_shape()
|
1388 |
+
)
|
1389 |
else:
|
1390 |
cache_length = past_length = past_key_values[0][0].shape[2]
|
1391 |
max_cache_length = None
|