-
Notifications
You must be signed in to change notification settings - Fork 82
[uc] add activation, norm, pos emb, quant, sdpa operators for uc backend. #342
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
base: master
Are you sure you want to change the base?
Changes from 3 commits
827a5e6
41399f5
9f7258c
41713a3
3f75a52
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,5 @@ | ||||||||||||||||||||||||||||||||
| from mojo_opset.utils.platform import get_impl_by_platform | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| _op_map = get_impl_by_platform() | ||||||||||||||||||||||||||||||||
| globals().update(_op_map) | ||||||||||||||||||||||||||||||||
| __all__ = list(_op_map.keys()) | ||||||||||||||||||||||||||||||||
|
Comment on lines
+1
to
+5
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
|
|
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,70 @@ | ||||||||||||||||||
| from functools import lru_cache | ||||||||||||||||||
|
|
||||||||||||||||||
| import torch | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| _DTYPE_API_SUFFIX = { | ||||||||||||||||||
| torch.float16: "fp16", | ||||||||||||||||||
| torch.bfloat16: "bf16", | ||||||||||||||||||
| torch.float32: "fp32", | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| @lru_cache(maxsize=1) | ||||||||||||||||||
| def _uc_kernels(): | ||||||||||||||||||
| import uc_kernel | ||||||||||||||||||
|
|
||||||||||||||||||
| return uc_kernel.load() | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| def _matrix_shape(tensor: torch.Tensor) -> tuple[int, int]: | ||||||||||||||||||
| if tensor.dim() == 0: | ||||||||||||||||||
| return 1, 1 | ||||||||||||||||||
| if tensor.dim() == 1: | ||||||||||||||||||
| return 1, tensor.numel() | ||||||||||||||||||
| return tensor.numel() // tensor.shape[-1], tensor.shape[-1] | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| def _typed_api(api: str, dtype: torch.dtype) -> str: | ||||||||||||||||||
| suffix = _DTYPE_API_SUFFIX.get(dtype) | ||||||||||||||||||
| if suffix is None: | ||||||||||||||||||
| raise NotImplementedError(f"UC backend {api} does not support dtype {dtype}.") | ||||||||||||||||||
|
|
||||||||||||||||||
| kernels = _uc_kernels() | ||||||||||||||||||
| typed_api = f"{api}_{suffix}" | ||||||||||||||||||
| if typed_api in kernels.keys(): | ||||||||||||||||||
| return typed_api | ||||||||||||||||||
| if dtype == torch.float16 and api in kernels.keys(): | ||||||||||||||||||
| return api | ||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In Python, checking membership directly in a dictionary (e.g.,
Suggested change
|
||||||||||||||||||
| raise NotImplementedError(f"UC backend {api} does not provide a {suffix} kernel artifact.") | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| def run_unary_kernel(api: str, x: torch.Tensor) -> torch.Tensor: | ||||||||||||||||||
| if x.numel() == 0: | ||||||||||||||||||
| return torch.empty_like(x) | ||||||||||||||||||
|
|
||||||||||||||||||
| kernel_input = x.contiguous() | ||||||||||||||||||
| kernel_output = torch.empty_like(kernel_input) | ||||||||||||||||||
| rows, cols = _matrix_shape(kernel_input) | ||||||||||||||||||
| _uc_kernels()[_typed_api(api, kernel_input.dtype)](kernel_input, kernel_output, rows, cols) | ||||||||||||||||||
| return kernel_output.reshape(x.shape) | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| def run_binary_kernel(api: str, lhs: torch.Tensor, rhs: torch.Tensor) -> torch.Tensor: | ||||||||||||||||||
| if lhs.shape != rhs.shape: | ||||||||||||||||||
| raise ValueError(f"UC backend {api} expects matching input shapes, got {lhs.shape} and {rhs.shape}.") | ||||||||||||||||||
| if lhs.dtype != rhs.dtype: | ||||||||||||||||||
| raise ValueError(f"UC backend {api} expects matching input dtypes, got {lhs.dtype} and {rhs.dtype}.") | ||||||||||||||||||
| if lhs.numel() == 0: | ||||||||||||||||||
| return torch.empty_like(lhs) | ||||||||||||||||||
|
|
||||||||||||||||||
| kernel_lhs = lhs.contiguous() | ||||||||||||||||||
| kernel_rhs = rhs.contiguous() | ||||||||||||||||||
| kernel_output = torch.empty_like(kernel_lhs) | ||||||||||||||||||
| rows, cols = _matrix_shape(kernel_lhs) | ||||||||||||||||||
| _uc_kernels()[_typed_api(api, kernel_lhs.dtype)](kernel_lhs, kernel_rhs, kernel_output, rows, cols) | ||||||||||||||||||
| return kernel_output.reshape(lhs.shape) | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| def run_kernel(api: str, dtype: torch.dtype, *args) -> None: | ||||||||||||||||||
| _uc_kernels()[_typed_api(api, dtype)](*args) | ||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import torch | ||
|
|
||
| from mojo_opset.core import MojoGelu | ||
| from mojo_opset.core import MojoSilu | ||
| from mojo_opset.core import MojoSwiGLU | ||
|
|
||
| from ._utils import run_binary_kernel | ||
| from ._utils import run_unary_kernel | ||
|
|
||
|
|
||
| class UCGelu(MojoGelu): | ||
| supported_platforms_list = ["npu"] | ||
|
|
||
| def forward(self, x: torch.Tensor) -> torch.Tensor: | ||
| return run_unary_kernel("mojo_gelu", x) | ||
|
|
||
|
|
||
| class UCSilu(MojoSilu): | ||
| supported_platforms_list = ["npu"] | ||
|
|
||
| def forward(self, x: torch.Tensor) -> torch.Tensor: | ||
| return run_unary_kernel("mojo_silu", x) | ||
|
|
||
|
|
||
| class UCSwiGLU(MojoSwiGLU): | ||
| supported_platforms_list = ["npu"] | ||
|
|
||
| def forward(self, gate_out: torch.Tensor, up_out: torch.Tensor) -> torch.Tensor: | ||
| if self.swiglu_limit > 0: | ||
| return super().forward(gate_out, up_out) | ||
| return run_binary_kernel("mojo_swiglu", gate_out, up_out) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This block of code is completely redundant. Setting
CLOSE_MATMUL_K_SHIFTwhenMOJO_DETERMINISTICis enabled is already handled at lines 11-17. Removing this redundant block keeps the code clean and maintainable.