Skip to content

Commit 7b9a5cf

Browse files
authored
Remove falcon mamba kernel (#51184)
* clean falcon mamba * comm * Revert the test file * clean * never used it
1 parent ab014e2 commit 7b9a5cf

3 files changed

Lines changed: 20 additions & 110 deletions

File tree

‎src/transformers/models/falcon_mamba/modeling_falcon_mamba.py‎

Lines changed: 7 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -116,30 +116,6 @@ def causal_conv1d_fn(
116116
return out.to(hidden_states.dtype)
117117

118118

119-
@use_kernel_func_from_hub_with_fallback("mamba_inner_fn", "mamba_ssm")
120-
def mamba_inner_fn(
121-
xz: torch.Tensor,
122-
conv1d_weight: torch.Tensor,
123-
conv1d_bias: torch.Tensor | None,
124-
x_proj_weight: torch.Tensor,
125-
delta_proj_weight: torch.Tensor,
126-
out_proj_weight: torch.Tensor,
127-
out_proj_bias: torch.Tensor | None,
128-
A: torch.Tensor,
129-
B: torch.Tensor | None = None,
130-
C: torch.Tensor | None = None,
131-
D: torch.Tensor | None = None,
132-
delta_bias: torch.Tensor | None = None,
133-
delta_softplus: bool = True,
134-
b_rms_weight: torch.Tensor | None = None,
135-
c_rms_weight: torch.Tensor | None = None,
136-
dt_rms_weight: torch.Tensor | None = None,
137-
b_c_dt_rms_eps: float = 1e-6,
138-
**kwargs,
139-
):
140-
return None
141-
142-
143119
@use_kernel_func_from_hub_with_fallback("selective_state_update", "mamba_ssm")
144120
def mamba_selective_state_update(
145121
state: torch.Tensor,
@@ -294,15 +270,16 @@ def combine_fn(left, right):
294270
return scan_output
295271

296272

297-
@use_kernelized_func(
298-
[mamba_inner_fn, mamba_selective_scan, mamba_selective_state_update, causal_conv1d_fn, causal_conv1d_update]
299-
)
273+
@use_kernelized_func([mamba_selective_scan, mamba_selective_state_update, causal_conv1d_fn, causal_conv1d_update])
300274
class FalconMambaMixer(nn.Module):
301275
"""
302276
Compute ∆, A, B, C, and D the state space parameters and compute the `contextualized_states`.
303-
A, D are input independent (see FalconMamba paper [1] Section 3.5.2 "Interpretation of A" for why A isn't selective)
304-
∆, B, C are input-dependent (this is a key difference between FalconMamba and the linear time invariant S4,
305-
and is why FalconMamba is called **selective** state spaces)
277+
A, D are input independent (see Mamba paper [1] Section 3.5.2 "Interpretation of A" for why A isn't selective)
278+
∆, B, C are input-dependent (this is a key difference between Mamba and the linear time invariant S4,
279+
and is why Mamba is called **selective** state spaces)
280+
281+
NOTE: unlike Mamba, there is no fused `mamba_inner_fn` path: it can only normalize dt after `dt_proj`, while
282+
FalconMamba normalizes it before, see https://github.com/huggingface/transformers/pull/51184
306283
"""
307284

308285
def __init__(self, config: FalconMambaConfig, layer_idx: int, initialize_mixer_weights: bool = True):
@@ -401,32 +378,6 @@ def forward(
401378
projected_states = self.in_proj(hidden_states).transpose(1, 2)
402379

403380
A = -torch.exp(self.A_log.float())
404-
if self.training and cache is None:
405-
fused_output = mamba_inner_fn(
406-
projected_states,
407-
self.conv1d.weight,
408-
self.conv1d.bias if self.use_conv_bias else None,
409-
self.x_proj.weight,
410-
self.dt_proj.weight,
411-
self.out_proj.weight,
412-
self.out_proj.bias.float() if self.use_bias else None,
413-
A,
414-
None, # input-dependent B
415-
None, # input-dependent C
416-
self.D.float(),
417-
delta_bias=self.dt_proj.bias.float(),
418-
delta_softplus=True,
419-
# Key difference: norms on B, C, and dt
420-
b_rms_weight=self.b_layernorm.weight,
421-
c_rms_weight=self.c_layernorm.weight,
422-
dt_rms_weight=self.dt_layernorm.weight,
423-
b_c_dt_rms_eps=self.rms_eps,
424-
)
425-
426-
# Only kernels can use this shortcircuit, fallback to normal torch otherwise
427-
if fused_output is not None:
428-
return fused_output
429-
430381
hidden_states_B_C, gate = projected_states.chunk(2, dim=1)
431382

432383
# 2. Convolution sequence transformation

‎src/transformers/models/falcon_mamba/modular_falcon_mamba.py‎

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
from ... import initialization as init
2121
from ...cache_utils import Cache
22+
from ...integrations import use_kernelized_func
2223
from ...utils import auto_docstring, logging
2324
from ...utils.deprecation import deprecate_kwarg
2425
from ..llama.modeling_llama import LlamaRMSNorm
@@ -34,7 +35,6 @@
3435
apply_mask_to_padding_states,
3536
causal_conv1d_fn,
3637
causal_conv1d_update,
37-
mamba_inner_fn,
3838
mamba_selective_scan,
3939
mamba_selective_state_update,
4040
)
@@ -108,7 +108,18 @@ def __init__(self, hidden_size, eps: float = 1e-6):
108108
self.weight = nn.Buffer(torch.ones(hidden_size, requires_grad=False), persistent=False)
109109

110110

111+
@use_kernelized_func([mamba_selective_scan, mamba_selective_state_update, causal_conv1d_fn, causal_conv1d_update])
111112
class FalconMambaMixer(MambaMixer):
113+
"""
114+
Compute ∆, A, B, C, and D the state space parameters and compute the `contextualized_states`.
115+
A, D are input independent (see Mamba paper [1] Section 3.5.2 "Interpretation of A" for why A isn't selective)
116+
∆, B, C are input-dependent (this is a key difference between Mamba and the linear time invariant S4,
117+
and is why Mamba is called **selective** state spaces)
118+
119+
NOTE: unlike Mamba, there is no fused `mamba_inner_fn` path: it can only normalize dt after `dt_proj`, while
120+
FalconMamba normalizes it before, see https://github.com/huggingface/transformers/pull/51184
121+
"""
122+
112123
def __init__(self, config: FalconMambaConfig, layer_idx: int, initialize_mixer_weights: bool = True):
113124
super().__init__(config, layer_idx, initialize_mixer_weights)
114125
# These include dummy weights that are not used (only for imitating the kernels path)
@@ -146,32 +157,6 @@ def forward(
146157
projected_states = self.in_proj(hidden_states).transpose(1, 2)
147158

148159
A = -torch.exp(self.A_log.float())
149-
if self.training and cache is None:
150-
fused_output = mamba_inner_fn(
151-
projected_states,
152-
self.conv1d.weight,
153-
self.conv1d.bias if self.use_conv_bias else None,
154-
self.x_proj.weight,
155-
self.dt_proj.weight,
156-
self.out_proj.weight,
157-
self.out_proj.bias.float() if self.use_bias else None,
158-
A,
159-
None, # input-dependent B
160-
None, # input-dependent C
161-
self.D.float(),
162-
delta_bias=self.dt_proj.bias.float(),
163-
delta_softplus=True,
164-
# Key difference: norms on B, C, and dt
165-
b_rms_weight=self.b_layernorm.weight,
166-
c_rms_weight=self.c_layernorm.weight,
167-
dt_rms_weight=self.dt_layernorm.weight,
168-
b_c_dt_rms_eps=self.rms_eps,
169-
)
170-
171-
# Only kernels can use this shortcircuit, fallback to normal torch otherwise
172-
if fused_output is not None:
173-
return fused_output
174-
175160
hidden_states_B_C, gate = projected_states.chunk(2, dim=1)
176161

177162
# 2. Convolution sequence transformation

‎src/transformers/models/jamba/modeling_jamba.py‎

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -262,30 +262,6 @@ def causal_conv1d_fn(
262262
return out.to(hidden_states.dtype)
263263

264264

265-
@use_kernel_func_from_hub_with_fallback("mamba_inner_fn", "mamba_ssm")
266-
def mamba_inner_fn(
267-
xz: torch.Tensor,
268-
conv1d_weight: torch.Tensor,
269-
conv1d_bias: torch.Tensor | None,
270-
x_proj_weight: torch.Tensor,
271-
delta_proj_weight: torch.Tensor,
272-
out_proj_weight: torch.Tensor,
273-
out_proj_bias: torch.Tensor | None,
274-
A: torch.Tensor,
275-
B: torch.Tensor | None = None,
276-
C: torch.Tensor | None = None,
277-
D: torch.Tensor | None = None,
278-
delta_bias: torch.Tensor | None = None,
279-
delta_softplus: bool = True,
280-
b_rms_weight: torch.Tensor | None = None,
281-
c_rms_weight: torch.Tensor | None = None,
282-
dt_rms_weight: torch.Tensor | None = None,
283-
b_c_dt_rms_eps: float = 1e-6,
284-
**kwargs,
285-
):
286-
return None
287-
288-
289265
@use_kernel_func_from_hub_with_fallback("selective_state_update", "mamba_ssm")
290266
def mamba_selective_state_update(
291267
state: torch.Tensor,
@@ -440,9 +416,7 @@ def combine_fn(left, right):
440416
return scan_output
441417

442418

443-
@use_kernelized_func(
444-
[mamba_inner_fn, mamba_selective_scan, mamba_selective_state_update, causal_conv1d_fn, causal_conv1d_update]
445-
)
419+
@use_kernelized_func([mamba_selective_scan, mamba_selective_state_update, causal_conv1d_fn, causal_conv1d_update])
446420
class JambaMambaMixer(nn.Module):
447421
"""
448422
Compute ∆, A, B, C, and D the state space parameters and compute the `contextualized_states`.

0 commit comments

Comments
 (0)