Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shard input_mask for Llama #905

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
14 changes: 8 additions & 6 deletions sharktank/sharktank/examples/export_paged_llm_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,22 +220,24 @@ def _(model, tokens, seq_lens, seq_block_ids, cs):
cache_tensors = cs

attention_mask = None
sl = tokens.shape[1]
input_mask = model.input_mask(seq_lens, sl)
if args.use_attention_mask:
sl = tokens.shape[1]
input_mask = model.input_mask(seq_lens, sl)
attention_mask = model.attention_mask(input_mask)

if llama_config.tensor_parallelism_size != 1:
shard_count = llama_config.tensor_parallelism_size

tokens = ops.replicate(tokens, count=shard_count)
input_mask = ops.replicate(input_mask, count=shard_count)
if attention_mask:
attention_mask = ops.replicate(attention_mask, count=shard_count)
seq_block_ids = ops.replicate(seq_block_ids, count=shard_count)
cache_tensors = repack_cache(cs, cache_shard_dim)

logits = model.prefill(
tokens,
input_mask=input_mask,
attention_mask=attention_mask,
seq_block_ids=seq_block_ids,
cache_state=cache_tensors,
Expand Down Expand Up @@ -306,15 +308,14 @@ def _(
seq_block_ids,
cache_state,
):
input_mask = model.input_mask(
seq_lens, seq_block_ids.shape[1] * model.cache.block_seq_stride
)
attention_mask = model.decode_attention_mask(input_mask)
input_mask = model.input_mask(seq_lens, 1)
attention_mask = model.decode_attention_mask(input_mask, seq_block_ids)

if llama_config.tensor_parallelism_size != 1:
shard_count = llama_config.tensor_parallelism_size

tokens = ops.replicate(tokens, count=shard_count)
input_mask = ops.replicate(input_mask, count=shard_count)
attention_mask = ops.replicate(attention_mask, count=shard_count)
start_positions = ops.replicate(start_positions, count=shard_count)
seq_block_ids = ops.replicate(seq_block_ids, count=shard_count)
Expand All @@ -323,6 +324,7 @@ def _(

logits = model.decode(
tokens,
input_mask=input_mask,
attention_mask=attention_mask,
start_positions=start_positions,
seq_block_ids=seq_block_ids,
Expand Down
11 changes: 9 additions & 2 deletions sharktank/sharktank/layers/causal_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,17 @@ def input_mask(
mask = range_vector >= matrix
return mask

def decode_attention_mask(self, boolean_input_mask: torch.Tensor):
def decode_attention_mask(
self, boolean_input_mask: torch.Tensor, seq_block_ids: torch.Tensor
):
dtype = self.attention_dtype
# Calculate total sequence length from blocks
total_seq_len = seq_block_ids.shape[1] * self.config.block_seq_stride
# Expand boolean mask to [batch_size, total_seq_len]
expanded_mask = boolean_input_mask.expand(-1, total_seq_len)
# Convert to numeric mask
numeric_mask = torch.where(
boolean_input_mask, self._maximally_negative_value(dtype), 0
expanded_mask, self._maximally_negative_value(dtype), 0
).to(dtype)
return numeric_mask.unsqueeze(1).unsqueeze(1).to(self.device)

Expand Down
10 changes: 10 additions & 0 deletions sharktank/sharktank/models/llama/llama.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,20 +129,25 @@ def prefill(
# [bs, batch_seq_len]
tokens: Union[torch.Tensor, ReplicatedTensor],
*,
# [bs, batch_seq_len]
input_mask: Union[torch.Tensor, ReplicatedTensor],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This input needs a check such that it should be mutually exclusive with the attention_mask arg.

# [1, 1, batch_seq_len, batch_seq_len]
attention_mask: Optional[Union[torch.Tensor, ReplicatedTensor]],
# [bs, batch_seq_len // block_seq_stride]
seq_block_ids: Union[torch.Tensor, ReplicatedTensor],
cache_state: list[Union[torch.Tensor, SplitPrimitiveTensor]],
):
self._assert_device(tokens)
self._assert_device(input_mask)
self._assert_device(attention_mask, dtype=self.activation_dtype)
self._assert_device(seq_block_ids)
self._assert_device(*cache_state, dtype=self.activation_dtype)

h = self.token_embedding(tokens)
self.trace_tensor("llama.token_embedding", h)

h *= input_mask.unsqueeze(-1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that if we assume this should somehow substitute the attention mask the math checks out.


# Iterate over attention blocks.
for block_idx, block in enumerate(self.attn_blocks):
if block_idx == 0:
Expand All @@ -166,6 +171,8 @@ def decode(
# [bs, 1]
tokens: Union[torch.Tensor, ReplicatedTensor],
*,
# [bs, 1]
input_mask: Union[torch.Tensor, ReplicatedTensor],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This input needs a check such that it should be mutually exclusive with the attention_mask arg.

# [bs, 1, 1, batch_seq_len]
attention_mask: Union[torch.Tensor, ReplicatedTensor],
# [bs] of starting positions
Expand All @@ -188,6 +195,7 @@ def decode(
== seq_block_ids.shape[1] * self.config.block_seq_stride
)
self._assert_device(tokens)
self._assert_device(input_mask)
self._assert_device(attention_mask, dtype=self.activation_dtype)
self._assert_device(start_positions)
self._assert_device(*cache_state, dtype=self.activation_dtype)
Expand All @@ -202,6 +210,8 @@ def decode(
h = self.token_embedding(tokens)
self.trace_tensor("llama.token_embedding", h)

h *= input_mask.unsqueeze(-1)

# Iterate over attention blocks.
for block_idx, block in enumerate(self.attn_blocks):
if block_idx == 0:
Expand Down
Loading