Skip to content
Merged
Changes from 6 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
48 changes: 28 additions & 20 deletions vllm/worker/hpu_model_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def find_rope_layer(parent, path):

class HpuModelAdapter(torch.nn.Module):

def __init__(self, model, vllm_config, layer_names):
def __init__(self, model, vllm_config, layer_names, is_causal=True):
super().__init__()
self.model = model
self.prefill_use_fusedsdpa = "fsdpa" in enabled_flags()
Expand All @@ -253,9 +253,7 @@ def __init__(self, model, vllm_config, layer_names):
self.dtype = vllm_config.model_config.dtype
self.layer_names = layer_names
self.is_pooler = hasattr(self.model, "_pooler")
self.is_causal = True
if self.is_pooler:
self.set_causal_option(self.model)
self.is_causal = is_causal
self.use_merged_prefill = VLLM_MERGED_PREFILL

def _set_attn_bias(self, attn_metadata, batch_size, seq_len, device,
Expand Down Expand Up @@ -436,18 +434,6 @@ def sample(self, *args, **kwargs):
def generate_proposals(self, *args, **kwargs):
return self.model.generate_proposals(*args, **kwargs)

def set_causal_option(self, module):
if isinstance(module, HPUAttentionImpl) and hasattr(
module, 'attn_type'):
self.is_causal = not (
module.attn_type == AttentionType.ENCODER
or module.attn_type == AttentionType.ENCODER_ONLY
or module.attn_type == AttentionType.ENCODER_DECODER)
return
else:
for child_name, child_module in module.named_children():
self.set_causal_option(child_module)

# sampler property will be used by spec_decode_worker
# don't rename
@property
Expand Down Expand Up @@ -832,12 +818,15 @@ def load_model(self) -> None:
hidden_layer_markstep_interval)
path_to_rope = get_path_to_rope(self.model)
torch.hpu.synchronize()

self.is_causal = True
if self.is_pooler:
self.set_causal_option(self.model)
with HabanaMemoryProfiler() as m_wrap:
self.model = self._maybe_wrap_in_hpu_graph(
self.model,
vllm_config=self.vllm_config,
layer_names=path_to_rope)
layer_names=path_to_rope,
is_causal=self.is_causal)
msg = f"Wrapping in HPU Graph took {m_wrap.get_summary_string()}"
logger.info(msg)
with HabanaMemoryProfiler() as m_wrap:
Expand Down Expand Up @@ -1027,17 +1016,36 @@ def make_attn_bias(self, seq_lens, max_prompt_len, dtype):
pad=-1,
dtype=torch.long,
flat=self.use_merged_prefill)

q_seq_idx_t = seq_idx_t.unsqueeze(-1)
kv_seq_idx_t = seq_idx_t.unsqueeze(-2)
q_seq_pos_t = seq_pos_t.unsqueeze(-1)
kv_seq_pos_t = seq_pos_t.unsqueeze(-2)
seq_idx_t = q_seq_idx_t != kv_seq_idx_t
seq_pos_t = kv_seq_pos_t > q_seq_pos_t
attn_mask = seq_idx_t | seq_pos_t
attn_mask = (seq_idx_t | seq_pos_t) if self.is_causal else seq_idx_t
if self.is_pooler:
mask_v = torch.where(q_seq_pos_t < 0, True, False)
attn_mask = attn_mask | mask_v
off_value = -3E38 #small number, avoid nan and overflow
else:
off_value = -math.inf
attn_bias = torch.zeros_like(attn_mask, dtype=dtype)
attn_bias.masked_fill_(attn_mask, -math.inf)
attn_bias.masked_fill_(attn_mask, off_value)
return attn_bias.unsqueeze(1)

def set_causal_option(self, module):
if isinstance(module, HPUAttentionImpl) and hasattr(
module, 'attn_type'):
self.is_causal = not (
module.attn_type == AttentionType.ENCODER
or module.attn_type == AttentionType.ENCODER_ONLY
or module.attn_type == AttentionType.ENCODER_DECODER)
return
else:
for child_name, child_module in module.named_children():
self.set_causal_option(child_module)

def move_to_device(self, tensor):
return tensor if tensor is None else tensor.to(self.device,
non_blocking=True)
Expand Down