mirror of
https://github.com/FlashML-org/FreeToken.git
synced 2026-11-26 05:15:46 +08:00
56 lines
1.4 KiB
Python
56 lines
1.4 KiB
Python
from __future__ import annotations
|
|
|
|
from abc import ABC, abstractmethod
|
|
from typing import TYPE_CHECKING
|
|
|
|
from freetoken.layers import (
|
|
BaseOP,
|
|
LinearColParallelMerged,
|
|
LinearRowParallel,
|
|
gelu_and_mul,
|
|
gelu_tanh_and_mul,
|
|
silu_and_mul,
|
|
)
|
|
from freetoken.utils import nvtx_annotate
|
|
|
|
if TYPE_CHECKING:
|
|
import torch
|
|
|
|
from .config import ModelConfig
|
|
|
|
|
|
class BaseLLMModel(ABC, BaseOP):
|
|
@abstractmethod
|
|
def forward(self) -> torch.Tensor: ...
|
|
|
|
|
|
class GatedMLP(BaseOP):
|
|
def __init__(self, config: ModelConfig):
|
|
self.gate_up_proj = LinearColParallelMerged(
|
|
config.hidden_size,
|
|
[config.intermediate_size, config.intermediate_size],
|
|
has_bias=False,
|
|
)
|
|
|
|
fn_map = {"silu": silu_and_mul, "gelu": gelu_and_mul, "gelu_tanh": gelu_tanh_and_mul}
|
|
act_fn = fn_map.get(config.hidden_act, None)
|
|
if act_fn is None:
|
|
raise ValueError(f"Unsupported activation function: {config.hidden_act}")
|
|
self.act_fn = act_fn
|
|
self.down_proj = LinearRowParallel(
|
|
config.intermediate_size,
|
|
config.hidden_size,
|
|
has_bias=False,
|
|
)
|
|
|
|
@nvtx_annotate("MLP")
|
|
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
gate_up = self.gate_up_proj.forward(x)
|
|
del x
|
|
y = self.act_fn(gate_up)
|
|
del gate_up
|
|
return self.down_proj.forward(y)
|
|
|
|
|
|
__all__ = ["BaseLLMModel", "GatedMLP"]
|