|
| 1 | +# Copyright 2025 The HuggingFace Team. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from typing import Optional, Tuple, Union |
| 16 | + |
| 17 | +import torch |
| 18 | +from executorch import version as executorch_version |
| 19 | +from packaging import version as pkg_version |
| 20 | + |
| 21 | + |
| 22 | +if pkg_version.parse(executorch_version.__version__) >= pkg_version.parse("0.6.0"): |
| 23 | + from executorch.extension.llm.custom_ops.custom_ops import custom_sdpa # noqa |
| 24 | + |
| 25 | + def custom_sdpa_with_start_pos_forward( |
| 26 | + module: torch.nn.Module, |
| 27 | + query: torch.Tensor, |
| 28 | + key: torch.Tensor, |
| 29 | + value: torch.Tensor, |
| 30 | + attention_mask: Union[torch.Tensor, "BlockMask"], # noqa |
| 31 | + scaling: Optional[float] = None, |
| 32 | + softcap: Optional[float] = None, |
| 33 | + head_mask: Optional[torch.Tensor] = None, |
| 34 | + **kwargs, |
| 35 | + ) -> Tuple[torch.Tensor, None]: |
| 36 | + # This is before the transpose |
| 37 | + max_seq_len = key.shape[2] |
| 38 | + |
| 39 | + # FA2 uses non-transposed inputs |
| 40 | + query = query.transpose(1, 2) |
| 41 | + key = key.transpose(1, 2) |
| 42 | + value = value.transpose(1, 2) |
| 43 | + |
| 44 | + # Convert the hell out of the inputs to fp32 and back |
| 45 | + input_dtype = query.dtype |
| 46 | + query = query.to(torch.float32) |
| 47 | + key = key.to(torch.float32) |
| 48 | + value = value.to(torch.float32) |
| 49 | + |
| 50 | + # Ignore the causal flag from kwargs but use the one in module |
| 51 | + kwargs.pop("is_causal", None) |
| 52 | + |
| 53 | + # Calculate the input pos from attention mask. |
| 54 | + # Branch out for float vs bool mask |
| 55 | + # assert attention_mask.dim() == 2, f"attention_mask must be a 2D matrix." |
| 56 | + attention_mask = attention_mask.reshape(-1, max_seq_len) |
| 57 | + first_row_mask = attention_mask[0, :] |
| 58 | + # [0, 0, 0, 0, -inf, -inf, -inf, -inf], start_pos = 3 |
| 59 | + start_pos = torch.argmin(first_row_mask).item() - 1 |
| 60 | + output = torch.ops.llama.custom_sdpa( |
| 61 | + query, |
| 62 | + key, |
| 63 | + value, |
| 64 | + start_pos=start_pos, |
| 65 | + attn_mask=None, |
| 66 | + drpout_p=0.0, |
| 67 | + is_causal=module.is_causal, |
| 68 | + scale=scaling, |
| 69 | + ) |
| 70 | + return output.to(input_dtype), None |
0 commit comments