Skip to content

test sdpa with fp16 #553

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

Merged
merged 3 commits into from
Apr 29, 2024
Merged
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 export_et_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class CustomKVCache(nn.Module):
def __init__(self, max_batch_size, max_seq_length, n_heads, head_dim, dtype):
super().__init__()

dtype = torch.float

# This is flipped around from what is in build.model's KVCache
cache_shape = (max_batch_size, max_seq_length, n_heads, head_dim)
self.register_buffer(
Expand All @@ -21,8 +23,8 @@ def __init__(self, max_batch_size, max_seq_length, n_heads, head_dim, dtype):
def update(self, input_pos, k_val, v_val):
k_out = self.k_cache
v_out = self.v_cache
k_out[:, :, input_pos] = k_val
v_out[:, :, input_pos] = v_val
k_out[:, :, input_pos] = k_val.float()
v_out[:, :, input_pos] = v_val.float()

return k_out, v_out

Expand Down Expand Up @@ -67,15 +69,15 @@ def forward(self, x, freqs_cis, mask, input_pos=None):
# KV cache should always be enabled
assert self.kv_cache is not None
output = torch.ops.llama.sdpa_with_kv_cache(
q,
k,
v,
q.float(),
Copy link
Contributor

Choose a reason for hiding this comment

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

do these need to be .to(dtype=torch.float)?

k.float(),
v.float(),
self.kv_cache.k_cache,
self.kv_cache.v_cache,
input_pos[-1].item(),
seqlen,
)
output = output.view(bsz, seqlen, self.dim)
output = output.view(bsz, seqlen, self.dim).to(dtype=q.dtype)
return self.wo(output)


Expand Down