Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions QEfficient/blocking/attention_blocking.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ def generic_blocked_attention_interface(
prefill_only: bool = False,
**kwargs,
):
prefill_only = prefill_only or blocking_config.mode.is_prefill
strategy = _STRATEGIES[
BlockingMode.get_final_mode(blocking_config, prefill_only=prefill_only, is_mla=is_mla, mla_kwargs=mla_kwargs)
]
Expand Down
5 changes: 4 additions & 1 deletion QEfficient/transformers/models/modeling_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -1668,7 +1668,10 @@ def export(
"continuous_batching": self.continuous_batching,
"comp_ctx_lengths": self.comp_ctx_lengths_decode,
}
if getattr(self.model.config, "model_type", None) == "qwen3_vl_moe":
if (
getattr(self.model.config, "model_type", None) == "qwen3_vl_moe"
or getattr(self.model.config, "model_type", None) == "qwen3_5_moe"
):
_blocking_cfg = self.lang_model.hash_params.get("blocking_kwargs", None)
batch_fold = (
not prefill_only and _blocking_cfg is not None and _blocking_cfg.mode == BlockingMode.KV_BATCH_FOLD
Expand Down
79 changes: 63 additions & 16 deletions QEfficient/transformers/models/qwen3_5_moe/modeling_qwen3_5_moe.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,55 @@ def read_only_blockedKV(self, start_index: int, end_index: int, layer_idx: int,
raise ValueError(f"Layer {layer_idx} is not a full_attention layer")
return layer.read_only_blockedKV(start_index, end_index, cache_kwargs)

def read_only_blocked_K(self, start_index: int, end_index: int, layer_idx: int, cache_kwargs: dict):
layer = self.kv_layers[layer_idx]
if layer is None:
raise ValueError(f"Layer {layer_idx} is not a full_attention layer")
return layer.read_only_blocked_K(start_index, end_index, cache_kwargs)

def read_only_blocked_V(self, start_index: int, end_index: int, layer_idx: int, cache_kwargs: dict):
layer = self.kv_layers[layer_idx]
if layer is None:
raise ValueError(f"Layer {layer_idx} is not a full_attention layer")
return layer.read_only_blocked_V(start_index, end_index, cache_kwargs)

def read_only_blocked_K_batch(
self, start_index: int, end_index: int, layer_idx: int, cache_kwargs: dict, folded_cache=None
):
layer = self.kv_layers[layer_idx]
if layer is None:
raise ValueError(f"Layer {layer_idx} is not a full_attention layer")
return layer.read_only_blocked_K_batch(start_index, end_index, cache_kwargs, folded_cache=folded_cache)

def read_only_blocked_V_batch(
self, start_index: int, end_index: int, layer_idx: int, cache_kwargs: dict, folded_cache=None
):
layer = self.kv_layers[layer_idx]
if layer is None:
raise ValueError(f"Layer {layer_idx} is not a full_attention layer")
return layer.read_only_blocked_V_batch(start_index, end_index, cache_kwargs, folded_cache=folded_cache)

def write_only(self, key_states: torch.Tensor, value_states: torch.Tensor, layer_idx: int, cache_kwargs: dict):
layer = self.kv_layers[layer_idx]
if layer is None:
raise ValueError(f"Layer {layer_idx} is not a full_attention layer")
return layer.write_only(key_states, value_states, cache_kwargs)

def get_batch_folded_kv(self, layer_idx):
"""Return retained K/V cache for one layer in batch-folded compute layout."""
layer = self.kv_layers[layer_idx]
if layer is None:
raise ValueError(f"Layer {layer_idx} is not a full_attention layer")
return layer.get_batch_folded_kv()

def write_only_batch(
self, key_states: torch.Tensor, value_states: torch.Tensor, layer_idx: int, cache_kwargs: dict
):
layer = self.kv_layers[layer_idx]
if layer is None:
raise ValueError(f"Layer {layer_idx} is not a full_attention layer")
return layer.write_only_batch(key_states, value_states, cache_kwargs)

def has_previous_state(self, layer_idx=None) -> bool:
if layer_idx is not None:
if layer_idx < 0 or layer_idx >= len(self.layer_types):
Expand Down Expand Up @@ -2033,19 +2076,24 @@ def _build_lang_spec(seq_len_val, comp_ctx_len=None):
return lang, compiler_options

def get_onnx_dynamic_axes(
self, comp_ctx_lengths: Optional[List[int]] = None, kv_offload: bool = False, continuous_batching: bool = False
self,
comp_ctx_lengths: Optional[List[int]] = None,
kv_offload: bool = False,
continuous_batching: bool = False,
batch_fold: bool = False,
):
num_layers = self.config.text_config.num_hidden_layers
batch_axis_name = "full_batch_size" if continuous_batching else "batch_size"
input_batch_axis_name = "full_batch_size" if continuous_batching and batch_fold else "batch_size"

vision_dynamic_axes = {
"pixel_values": {0: "grid_height", 1: "grid_width"},
"image_grid_thw": {0: "batch_size", 1: "time", 2: "grid_h", 3: "grid_w"},
}

lang_dynamic_axes = {
"input_ids": {0: "batch_size", 1: "seq_len"},
"position_ids": {1: "batch_size", 2: "seq_len"},
"input_ids": {0: input_batch_axis_name, 1: "seq_len"},
"position_ids": {1: input_batch_axis_name, 2: "seq_len"},
"vision_embeds": {0: "vision_batch_size", 1: "vision_size"},
}

Expand All @@ -2058,7 +2106,7 @@ def get_onnx_dynamic_axes(
lang_dynamic_axes[f"recurrent_state.{i}"] = {0: batch_axis_name}

if continuous_batching:
lang_dynamic_axes["batch_index"] = {0: "batch_size"}
lang_dynamic_axes["batch_index"] = {0: input_batch_axis_name}

if comp_ctx_lengths is not None:
lang_dynamic_axes["comp_ctx_lengths"] = {0: "comp_ctx_lengths"}
Expand All @@ -2081,25 +2129,31 @@ def get_dummy_inputs(
continuous_batching: bool = False,
**kwargs,
):
batch_fold = kwargs.pop("batch_fold", False)
bs = kwargs.get("batch_size", constants.ONNX_EXPORT_EXAMPLE_BATCH_SIZE)
fbs = constants.ONNX_EXPORT_EXAMPLE_FBS
if continuous_batching and batch_fold:
bs = fbs

inputs_shapes = {}

dummy_seq_len = 32
inputs_shapes["input_ids"] = (constants.ONNX_EXPORT_EXAMPLE_BATCH_SIZE, dummy_seq_len)
inputs_shapes["input_ids"] = (bs, dummy_seq_len)

inputs_shapes["position_ids"] = (
4,
constants.ONNX_EXPORT_EXAMPLE_BATCH_SIZE,
bs,
dummy_seq_len,
)
inputs_shapes["pixel_values"] = (11008, 1536)
inputs_shapes["image_grid_thw"] = (
constants.ONNX_EXPORT_EXAMPLE_BATCH_SIZE,
bs,
1,
86,
128,
)
inputs_shapes["vision_embeds"] = (
constants.ONNX_EXPORT_EXAMPLE_BATCH_SIZE,
bs,
2752,
self.model.config.text_config.hidden_size,
)
Expand All @@ -2114,19 +2168,12 @@ def get_dummy_inputs(
lang_inputs["input_ids"] = torch.zeros((inputs_shapes["input_ids"]), dtype=torch.int64)
lang_inputs["vision_embeds"] = torch.zeros((inputs_shapes["vision_embeds"]), dtype=float_dtype)
lang_inputs["position_ids"] = (
(
torch.arange(dummy_seq_len, dtype=torch.int64)
.view(1, dummy_seq_len)
.repeat(constants.ONNX_EXPORT_EXAMPLE_BATCH_SIZE, 1)
)
(torch.arange(dummy_seq_len, dtype=torch.int64).view(1, dummy_seq_len).repeat(bs, 1))
.unsqueeze(0)
.repeat(4, 1, 1)
)
lang_inputs["image_idx"] = torch.zeros((inputs_shapes["image_idx"]), dtype=torch.int64)

bs: int = constants.ONNX_EXPORT_EXAMPLE_BATCH_SIZE
fbs: int = constants.ONNX_EXPORT_EXAMPLE_FBS

kv_cache_shape = get_padding_shape_from_config(
config=self.model.config.text_config,
batch_size=fbs if continuous_batching else bs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,6 @@ def forward(
batch_index=batch_index,
position_ids=position_ids[0],
past_seen_tokens=past_seen_tokens,
prefill_only=blocking_config.mode.is_prefill,
)
else:
key_states, value_states, attention_mask, _ = past_key_value_update(
Expand Down
Loading
Loading