|
| 1 | +# Copyright 2025 Rebellions Inc. 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 |
| 16 | + |
| 17 | +import torch |
| 18 | +from torch import Tensor |
| 19 | + |
| 20 | + |
| 21 | +@torch.library.custom_op( |
| 22 | + "rbln_custom_ops::custom_moe_glu", |
| 23 | + mutates_args=(), |
| 24 | +) |
| 25 | +def custom_moe_glu( |
| 26 | + hidden_states: Tensor, |
| 27 | + gate_proj_weight: Tensor, |
| 28 | + up_proj_weight: Tensor, |
| 29 | + down_proj_weight: Tensor, |
| 30 | + masked_routing_weight: Tensor, |
| 31 | + expert_select_count: Tensor, |
| 32 | + gate_proj_bias: Optional[Tensor] = None, |
| 33 | + up_proj_bias: Optional[Tensor] = None, |
| 34 | + down_proj_bias: Optional[Tensor] = None, |
| 35 | +) -> Tensor: |
| 36 | + """ |
| 37 | + Customized MoE GLU operation. |
| 38 | + Expected tensor shapes: |
| 39 | + - hidden_states: [batch*seq_len, hidden_size] |
| 40 | + - gate_proj_weight: [num_experts, hidden_size, intermediate_size] |
| 41 | + - up_proj_weight: [num_experts, hidden_size, intermediate_size] |
| 42 | + - down_proj_weight: [num_experts, intermediate_size, hidden_size] |
| 43 | + - masked_routing_weight: [batch * seq_len, num_experts] |
| 44 | + - gate_proj_bias: [num_experts, intermediate_size] |
| 45 | + - up_proj_bias: [num_experts, intermediate_size] |
| 46 | + - down_proj_bias: [num_experts, hidden_size] |
| 47 | + Returns: |
| 48 | + Tensor: [batch * seq_len, hidden_size] |
| 49 | + """ |
| 50 | + |
| 51 | + out = torch.zeros_like(hidden_states) |
| 52 | + expert_cnt = gate_proj_weight.shape[0] |
| 53 | + for i in range(expert_cnt): |
| 54 | + gate_proj = torch.nn.functional.linear(hidden_states, gate_proj_weight[i]) |
| 55 | + up_proj = torch.nn.functional.linear(hidden_states, up_proj_weight[i]) |
| 56 | + mul = torch.nn.functional.silu(gate_proj) * up_proj |
| 57 | + down_proj = torch.nn.functional.linear(mul, down_proj_weight[i]) |
| 58 | + out += down_proj * masked_routing_weight[:, i : i + 1] |
| 59 | + |
| 60 | + return out |
| 61 | + |
| 62 | + |
| 63 | +@custom_moe_glu.register_fake |
| 64 | +def custom_moe_glu_fake( |
| 65 | + hidden_states: Tensor, |
| 66 | + gate_proj_weight: Tensor, |
| 67 | + up_proj_weight: Tensor, |
| 68 | + down_proj_weight: Tensor, |
| 69 | + masked_routing_weight: Tensor, |
| 70 | + expert_select_count: Tensor, |
| 71 | + gate_proj_bias: Optional[Tensor] = None, |
| 72 | + up_proj_bias: Optional[Tensor] = None, |
| 73 | + down_proj_bias: Optional[Tensor] = None, |
| 74 | +) -> Tensor: |
| 75 | + return torch.empty_like(hidden_states) |
0 commit comments