Skip to content

Commit 3ece187

Browse files
cf-icehzgzhmyname
authored andcommitted
feat: add Granite causal language model support
1 parent 50b1f22 commit 3ece187

8 files changed

Lines changed: 1042 additions & 0 deletions

File tree

paddleformers/transformers/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@
171171
"paddleocr_vl.processor": ["PaddleOCRVLProcessor"],
172172
"gpt_oss.configuration": ["GptOssConfig"],
173173
"gpt_oss.modeling": ["GptOssModel", "GptOssForCausalLM", "GptOssForCausalLMPipe"],
174+
"granite.configuration": ["GraniteConfig"],
175+
"granite.modeling": ["GraniteModel", "GraniteForCausalLM"],
176+
"granite.tokenizer": ["GraniteTokenizer"],
174177
"kimi_k25.vision_processor": ["KimiK25VisionProcessor"],
175178
"kimi_k25.processor": ["KimiK25Processor"],
176179
"kimi_k25.tokenizer": ["TikTokenTokenizer"],
@@ -296,6 +299,7 @@
296299
"Qwen3NextPretrainingCriterion",
297300
],
298301
"llama": [],
302+
"granite": [],
299303
"qwen2": [],
300304
"glm_ocr": [],
301305
"qwen3": [],
@@ -417,6 +421,7 @@
417421
from .kimi_k2 import *
418422
from .paddleocr_vl import *
419423
from .llama import *
424+
from .granite import *
420425
from .optimization import *
421426
from .qwen2 import *
422427
from .qwen2_5_vl import *

paddleformers/transformers/auto/configuration.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
("ernie4_5_moe_vl", "Ernie4_5_VLConfig"),
4242
("paddleocr_vl", "PaddleOCRVLConfig"),
4343
("llama", "LlamaConfig"),
44+
("granitemoehybrid", "GraniteConfig"),
4445
("kimi_k2", "KimiK2Config"),
4546
("qwen2", "Qwen2Config"),
4647
("qwen2_5_vl", "Qwen2_5_VLConfig"),
@@ -82,6 +83,7 @@
8283
("ernie4_5_moe_vl", "Ernie4_5_VLMoeForConditionalGeneration"),
8384
("paddleocr_vl", "PaddleOCRVLForConditionalGeneration"),
8485
("llama", "Llama"),
86+
("granitemoehybrid", "Granite"),
8587
("qwen2", "Qwen2"),
8688
("qwen2_5_vl", "Qwen2_5_VL"),
8789
("qwen2_5_vl_text", "Qwen2_5_VL"),
@@ -112,6 +114,7 @@
112114
("qwen2_5_vl_text", "qwen2_5_vl"),
113115
("qwen3_vl_text", "qwen3_vl"),
114116
("qwen3_vl_moe_text", "qwen3_vl_moe"),
117+
("granitemoehybrid", "granite"),
115118
("phi4flash", "phi4"),
116119
]
117120
)

paddleformers/transformers/auto/modeling.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
("Ernie4_5_VLMoe", "ernie4_5_moe_vl"),
6262
("PaddleOCRVL", "paddleocr_vl"),
6363
("Llama", "llama"),
64+
("Granite", "granite"),
6465
("KimiK2", "kimi_k2"),
6566
("Qwen2", "qwen2"),
6667
("Qwen2_5_VL", "qwen2_5_vl"),
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright (c) 2026 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
"""Granite model package."""
15+
16+
import sys
17+
from typing import TYPE_CHECKING
18+
19+
from ...utils.lazy_import import _LazyModule
20+
21+
import_structure = {
22+
"tokenizer": ["GraniteTokenizer"],
23+
"configuration": ["GraniteConfig"],
24+
"modeling": [
25+
"GraniteDecoderLayer",
26+
"GraniteModel",
27+
"GraniteForCausalLM",
28+
],
29+
}
30+
31+
if TYPE_CHECKING:
32+
from .configuration import *
33+
from .modeling import *
34+
from .tokenizer import *
35+
else:
36+
sys.modules[__name__] = _LazyModule(
37+
__name__,
38+
globals()["__file__"],
39+
import_structure,
40+
module_spec=__spec__,
41+
)
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
# Copyright (c) 2026 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from ..configuration_utils import PretrainedConfig
16+
from ..modeling_rope_utils import rope_config_validation, standardize_rope_params
17+
18+
19+
class GraniteConfig(PretrainedConfig):
20+
model_type = "granitemoehybrid"
21+
tokenizer_class = "GPT2Tokenizer"
22+
23+
def __init__(
24+
self,
25+
vocab_size=100352,
26+
hidden_size=1024,
27+
intermediate_size=2048,
28+
shared_intermediate_size=2048,
29+
max_position_embeddings=32768,
30+
num_hidden_layers=28,
31+
num_attention_heads=16,
32+
num_key_value_heads=4,
33+
initializer_range=0.1,
34+
rms_norm_eps=1e-5,
35+
use_cache=True,
36+
pad_token_id=100256,
37+
bos_token_id=100257,
38+
eos_token_id=100257,
39+
hidden_act="silu",
40+
attention_bias=False,
41+
attention_dropout=0.0,
42+
mlp_bias=False,
43+
head_dim=None,
44+
tie_word_embeddings=True,
45+
# MuP scaling factors
46+
embedding_multiplier=1.0,
47+
attention_multiplier=1.0,
48+
residual_multiplier=1.0,
49+
logits_scaling=1.0,
50+
# MoE (unused for 350m-base, all zero)
51+
num_local_experts=0,
52+
num_experts_per_tok=0,
53+
output_router_logits=False,
54+
router_aux_loss_coef=0.01,
55+
# Mamba (unused for 350m-base, all attention layers)
56+
mamba_n_heads=128,
57+
mamba_n_groups=1,
58+
mamba_d_state=256,
59+
mamba_d_head="auto",
60+
mamba_d_conv=4,
61+
mamba_expand=2,
62+
mamba_chunk_size=256,
63+
mamba_conv_bias=True,
64+
mamba_proj_bias=False,
65+
layer_types=None,
66+
position_embedding_type="rope",
67+
rope_theta=10000000.0,
68+
rope_scaling=None,
69+
**kwargs,
70+
):
71+
self.vocab_size = vocab_size
72+
self.max_position_embeddings = max_position_embeddings
73+
self.hidden_size = hidden_size
74+
self.intermediate_size = intermediate_size
75+
self.shared_intermediate_size = shared_intermediate_size
76+
self.num_hidden_layers = num_hidden_layers
77+
self.num_attention_heads = num_attention_heads
78+
79+
if num_key_value_heads is None:
80+
num_key_value_heads = num_attention_heads
81+
self.num_key_value_heads = num_key_value_heads
82+
83+
self.hidden_act = hidden_act
84+
self.initializer_range = initializer_range
85+
self.rms_norm_eps = rms_norm_eps
86+
self.use_cache = use_cache
87+
self.attention_bias = attention_bias
88+
self.attention_dropout = attention_dropout
89+
self.mlp_bias = mlp_bias
90+
self.head_dim = head_dim if head_dim is not None else self.hidden_size // self.num_attention_heads
91+
92+
# MuP scaling factors
93+
self.embedding_multiplier = embedding_multiplier
94+
self.attention_multiplier = attention_multiplier
95+
self.residual_multiplier = residual_multiplier
96+
self.logits_scaling = logits_scaling
97+
98+
# MoE params
99+
self.num_local_experts = num_local_experts
100+
self.num_experts_per_tok = num_experts_per_tok
101+
self.output_router_logits = output_router_logits
102+
self.router_aux_loss_coef = router_aux_loss_coef
103+
104+
# Mamba params (for architecture compatibility, unused in 350m-base)
105+
self.mamba_n_heads = mamba_n_heads
106+
self.mamba_n_groups = mamba_n_groups
107+
self.mamba_d_state = mamba_d_state
108+
self.mamba_d_head = mamba_d_head
109+
self.mamba_d_conv = mamba_d_conv
110+
self.mamba_expand = mamba_expand
111+
self.mamba_chunk_size = mamba_chunk_size
112+
self.mamba_conv_bias = mamba_conv_bias
113+
self.mamba_proj_bias = mamba_proj_bias
114+
115+
# Layer types
116+
if layer_types is None:
117+
layer_types = ["attention"] * num_hidden_layers
118+
if any(layer_type != "attention" for layer_type in layer_types):
119+
raise ValueError("GraniteForCausalLM supports attention-only Granite configurations.")
120+
if num_local_experts or num_experts_per_tok:
121+
raise ValueError("GraniteForCausalLM does not support MoE configurations.")
122+
self.layer_types = layer_types
123+
self.position_embedding_type = position_embedding_type
124+
125+
# RoPE
126+
self.rope_theta = rope_theta
127+
self.rope_scaling = rope_scaling
128+
if self.rope_scaling is not None and "type" in self.rope_scaling:
129+
self.rope_scaling["rope_type"] = self.rope_scaling["type"]
130+
self.rope_parameters = self.rope_scaling if self.rope_scaling is not None else {"rope_type": "default", "rope_theta": rope_theta}
131+
standardize_rope_params(self, rope_theta=self.rope_theta)
132+
rope_config_validation(self)
133+
134+
super().__init__(
135+
pad_token_id=pad_token_id,
136+
bos_token_id=bos_token_id,
137+
eos_token_id=eos_token_id,
138+
tie_word_embeddings=tie_word_embeddings,
139+
**kwargs,
140+
)

0 commit comments

Comments
 (0)