Skip to content
Open
Show file tree
Hide file tree
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
22 changes: 18 additions & 4 deletions packages/paddlefleet_ops/src/paddlefleet_ops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@ def _sonic_moe_requirement(
_FLASH_MASK_AVAILABLE = False
_CUDNN_FRONTEND_AVAILABLE = False


def _ecosystem_module_exists(lib_name: str) -> bool:
return (ops_dir / lib_name).exists() or (
ops_dir / f"{lib_name}.py"
).exists()


if paddle.is_compiled_with_cuda():
if paddle.cuda.get_device_capability()[0] >= 9:
_DEEP_GEMM_AVAILABLE = True
Expand All @@ -185,7 +192,7 @@ def _sonic_moe_requirement(
and _cuda_version >= (12, 9)
):
_SONIC_MOE_AVAILABLE = True
if sys.version_info >= (3, 12):
if sys.version_info >= (3, 12) and _ecosystem_module_exists("cudnn"):
_CUDNN_FRONTEND_AVAILABLE = True

if paddle.is_compiled_with_xpu():
Expand Down Expand Up @@ -347,9 +354,16 @@ def _safe_load_ecosystem_lib(
paddle.enable_compat(scope={"cudnn"}, silent=True)
_safe_load_ecosystem_lib("cudnn", ops_dir, globals())
else:
warning, error = _cutedsl_requirement(
"paddlefleet_ops.cudnn", hint=CUDNN_FRONTEND_HINT
)
if sys.version_info < (3, 12):
warning, error = _cutedsl_requirement(
"paddlefleet_ops.cudnn", hint=CUDNN_FRONTEND_HINT
)
else:
warning, error = _build_notice(
"paddlefleet_ops.cudnn",
"cudnn frontend Python module is not installed.",
hint_for_error=CUDNN_FRONTEND_HINT,
)
logger.warning(warning)
blocked_import_messages["paddlefleet_ops.cudnn"] = error

Expand Down
2 changes: 1 addition & 1 deletion packages/paddlefleet_ops/third_party/sonic-moe
27 changes: 20 additions & 7 deletions src/paddlefleet/transformer/moe/moe_expert.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import paddle
import paddle.nn.functional as F
import paddlefleet_ops
from paddle.distributed.flex_checkpoint.dcp.sharded_weight import (
build_sharded_state_dict,
shard_weight,
Expand All @@ -38,11 +39,15 @@
k_grouped_bf16_gemm_tn_contiguous_aligned,
)

try:
if paddlefleet_ops.is_deep_gemm_available():
from paddlefleet_ops import deep_gemm as paddlefleet_deep_gemm
from paddlefleet_ops.sonicmoe.quack_utils import quantize_native_fp8_weights
except (ImportError, RuntimeError):
pass

if paddlefleet_ops.is_sonic_moe_available():
from paddlefleet_ops.sonicmoe.functional import clear_all_fp8_weight_caches
from paddlefleet_ops.sonicmoe.quack_utils import (
install_native_fp8_weight_cache,
quantize_native_fp8_weights,
)


class BMMFunction(paddle.autograd.PyLayer):
Expand Down Expand Up @@ -327,6 +332,10 @@ class SonicMoEExpert(GroupedMLPExpert):
_GROUPED_LAYOUT = "grouped"
_SONIC_LAYOUT = "sonic"

@staticmethod
def clear_fp8_weight_cache():
clear_all_fp8_weight_caches()

@staticmethod
def _grouped_w1_to_sonic(weight):
gate, up = paddle.chunk(weight, 2, axis=-1)
Expand Down Expand Up @@ -423,10 +432,14 @@ def step(self):
def quant_weight(self):
self.convert_weights_to_sonic_layout()

payload = quantize_native_fp8_weights(
self.weight1.permute([1, 2, 0]),
self.weight2.permute([1, 2, 0]),
w1_sonic = self.weight1.permute([1, 2, 0])
w2_sonic = self.weight2.permute([1, 2, 0])
payload = quantize_native_fp8_weights(w1_sonic, w2_sonic, iso32=False)
install_native_fp8_weight_cache(
w1_sonic, w2_sonic, payload, iso32=False
)
self._native_fp8_weight_payload = payload

assert payload["format"] == "1x32", (
f"quant strategy {payload.get('format')} is not supported."
)
Expand Down
Loading