mirror of
https://git.datalinker.icu/vllm-project/vllm.git
synced 2026-04-18 06:27:04 +08:00
Signed-off-by: Nick Hill <nhill@redhat.com> Signed-off-by: Lucas Kabela <lucaskabela@meta.com> Signed-off-by: Max de Bayser <mbayser@br.ibm.com> Signed-off-by: Andrew Sansom <andrew@protopia.ai> Signed-off-by: Boyuan Feng <boyuan@meta.com> Signed-off-by: Boyuan Feng <fby.1994@gmail.com> Signed-off-by: boyuanfeng <boyuan@meta.com> Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com> Signed-off-by: JartX <sagformas@epdcenter.es> Signed-off-by: Chendi Xue <Chendi.Xue@intel.com> Signed-off-by: chaunceyjiang <chaunceyjiang@gmail.com> Signed-off-by: DarkLight1337 <tlleungac@connect.ust.hk> Signed-off-by: Chen Zhang <zhangch99@outlook.com> Signed-off-by: Roger Wang <hey@rogerw.io> Signed-off-by: mgoin <mgoin64@gmail.com> Signed-off-by: wwl2755 <wangwenlong2755@gmail.com> Signed-off-by: Manoel Marques <manoel.marques@ibm.com> Signed-off-by: Manoel Marques <manoelmrqs@gmail.com> Signed-off-by: Isotr0py <mozf@mail2.sysu.edu.cn> Signed-off-by: pengdrumli <pengdrumli@tencent.com> Signed-off-by: windsonsea <haifeng.yao@daocloud.io> Signed-off-by: Woosuk Kwon <woosuk@thinkingmachines.ai> Signed-off-by: Woosuk Kwon <woosuk.kwon@berkeley.edu> Signed-off-by: Huamin Li <3ericli@gmail.com> Signed-off-by: simondanielsson <simon.danielsson99@hotmail.com> Signed-off-by: Rahul Tuli <rtuli@redhat.com> Signed-off-by: Yang <lymailforjob@gmail.com> Signed-off-by: Debolina Roy <debroy@redhat.com> Signed-off-by: David Chen <530634352@qq.com> Signed-off-by: wangzi <3220100013@zju.edu.cn> Signed-off-by: Eldar Kurtic <8884008+eldarkurtic@users.noreply.github.com> Signed-off-by: NickLucche <nlucches@redhat.com> Signed-off-by: Yizhou Liu <liu_yizhou@outlook.com> Signed-off-by: Sara Kokkila Schumacher <saraks@ibm.com> Signed-off-by: Csrayz <jover@cmbchina.com> Signed-off-by: ivyilike <pww123@cmbchina.com> Signed-off-by: Burkhard Ringlein <ngl@zurich.ibm.com> Signed-off-by: Bowen Wang <abmfy@icloud.com> Signed-off-by: qqma <qqma@amazon.com> Signed-off-by: ElizaWszola <ewszola@redhat.com> Signed-off-by: Lu Fang <fanglu@fb.com> Signed-off-by: Zhuohan Li <zhuohan123@gmail.com> Signed-off-by: Luka Govedič <lgovedic@redhat.com> Signed-off-by: luka <lgovedic@redhat.com> Signed-off-by: Luka Govedič <ProExpertProg@users.noreply.github.com> Signed-off-by: Or Ozeri <oro@il.ibm.com> Signed-off-by: Johnny Yang <johnnyyang@google.com> Signed-off-by: Alec Solder <alecs@fb.com> Signed-off-by: Alec S <10566873+alecsolder@users.noreply.github.com> Signed-off-by: Russell Bryant <rbryant@redhat.com> Signed-off-by: Matthew Bonanni <mbonanni@redhat.com> Signed-off-by: Alexander Matveev <amatveev@redhat.com> Signed-off-by: yewentao256 <zhyanwentao@126.com> Signed-off-by: liuye.hj <liuye.hj@alibaba-inc.com> Signed-off-by: Kunshang Ji <kunshang.ji@intel.com> Signed-off-by: Lucia Fang <116399278+luccafong@users.noreply.github.com> Signed-off-by: Michael Goin <mgoin64@gmail.com> Signed-off-by: Varun Sundar Rabindranath <vsundarr@redhat.com> Signed-off-by: Ming Yang <minos.future@gmail.com> Signed-off-by: Zhikaiiii <1658973216@qq.com> Signed-off-by: Andreas Hartel <andreas.hartel@aleph-alpha.com> Signed-off-by: Jee Jee Li <pandaleefree@gmail.com> Signed-off-by: vllmellm <vllm.ellm@embeddedllm.com> Signed-off-by: wuxibin <wuxibin@bytedance.com> Signed-off-by: youkaichao <youkaichao@gmail.com> Signed-off-by: Peter Pan <Peter.Pan@daocloud.io> Signed-off-by: Peter Pan <peter.pan@daocloud.io> Signed-off-by: Nicolò Lucchesi<nicolo.lucchesi@gmail.com> Signed-off-by: Thomas Parnell <tpa@zurich.ibm.com> Signed-off-by: Sage Moore <sage@neuralmagic.com> Signed-off-by: Lucas Wilkinson <lwilkins@redhat.com> Signed-off-by: Lucas Wilkinson <LucasWilkinson@users.noreply.github.com> Signed-off-by: Tyler Michael Smith <tyler@neuralmagic.com> Signed-off-by: Bill Nell <bnell@redhat.com> Signed-off-by: Shreeasish Kumar <shreeasish@rivosinc.com> Signed-off-by: Weida Hong <wdhongtw@google.com> Signed-off-by: Ekagra Ranjan <3116519+ekagra-ranjan@users.noreply.github.com> Signed-off-by: Hashem Hashemi <hashem.hashemi@amd.com> Signed-off-by: Hashem Hashemi <159079214+amd-hhashemi@users.noreply.github.com> Signed-off-by: Amir Samani <asamani@nvidia.com> Signed-off-by: ElizaWszola <elizaw.9289@gmail.com> Signed-off-by: jiahanc <173873397+jiahanc@users.noreply.github.com> Signed-off-by: ilmarkov <markovilya197@gmail.com> Signed-off-by: Gregory Shtrasberg <Gregory.Shtrasberg@amd.com> Signed-off-by: Jialin Ouyang <Jialin.Ouyang@gmail.com> Signed-off-by: rouchenzi <ruochenwen@gmail.com> Signed-off-by: rouchenzi <40842833+rouchenzi@users.noreply.github.com> Signed-off-by: Andrew Xia <axia@meta.com> Signed-off-by: Kourosh Hakhamaneshi <kourosh@anyscale.com> Signed-off-by: Corey Lowman <clowman1993@gmail.com> Signed-off-by: jpvillam <jpvillam@amd.com> Signed-off-by: dougbtv <dosmith@redhat.com> Signed-off-by: Chenxi Yang <cxyang@fb.com> Signed-off-by: Wentao Ye <44945378+yewentao256@users.noreply.github.com> Signed-off-by: ahao-anyscale <ahao@anyscale.com> Signed-off-by: Yan Lu <luyan@nvidia.com> Signed-off-by: baxingpiaochong <771405853@qq.com> Signed-off-by: Kyle Sayers <kylesayrs@gmail.com> Signed-off-by: Nikhil Gupta <nikhil.gupta2@arm.com> Signed-off-by: Yong Hoon Shin <yhshin@meta.com> Signed-off-by: Benjamin Chislett <benjamin.chislett@centml.ai> Signed-off-by: Benjamin Chislett <bchislett@nvidia.com> Signed-off-by: Ben Browning <bbrownin@redhat.com> Signed-off-by: Chengji Yao <chengjiyao@google.com> Signed-off-by: jiang1.li <jiang1.li@intel.com> Signed-off-by: Jackmin801 <ongjackm@gmail.com> Signed-off-by: Jonas M. Kübler <44084297+jmkuebler@users.noreply.github.com> Signed-off-by: taohui <taohui3@gmail.com> Signed-off-by: rongfu.leng <rongfu.leng@daocloud.io> Signed-off-by: Shu Wang <shuw@nvidia.com> Signed-off-by: Shu Wang. <shuw@nvidia.com> Signed-off-by: Tyler Michael Smith <tlrmchlsmth@gmail.com> Signed-off-by: Duncan Moss <djm.moss@gmail.com> Signed-off-by: Shiyan Deng <dsy842974287@meta.com> Signed-off-by: Wei Wei <wwei6@meta.com> Signed-off-by: Saman Keon <samanamp@outlook.com> Signed-off-by: yangxurui <yangxurui@meituan.com> Signed-off-by: nicole-lihui <nicole.li@daocloud.io> Signed-off-by: courage17340 <courage17340@163.com> Signed-off-by: Jacob Kahn <jacobkahn1@gmail.com> Signed-off-by: Fadi Arafeh <fadi.arafeh@arm.com> Signed-off-by: Agata Dobrzyniewicz <adobrzyniewicz@habana.ai> Signed-off-by: zxw <1020938856@qq.com> Signed-off-by: wang.yuqi <noooop@126.com> Signed-off-by: Cyrus Leung <cyrus.tl.leung@gmail.com> Signed-off-by: chenlang <chen.lang5@zte.com.cn> Signed-off-by: Jonas Kuebler <kuebj@amazon.com> Signed-off-by: AlonKejzman <alonkeizman@gmail.com> Signed-off-by: Tao Hui <taohui3@gmail.com> Signed-off-by: Matthew Bonanni <mbonanni001@gmail.com> Signed-off-by: Tomer Asida <57313761+tomeras91@users.noreply.github.com> Signed-off-by: Aleksandr Malyshev <maleksan@amd.com> Signed-off-by: Eugene Khvedchenia <ekhvedchenia@nvidia.com> Signed-off-by: Eugene Khvedchenya <ekhvedchenya@gmail.com> Signed-off-by: yiting.jiang <yiting.jiang@daocloud.io> Signed-off-by: xaguilar <Xavier.AguilarFruto@amd.com> Signed-off-by: Iceber Gu <caiwei95@hotmail.com> Signed-off-by: Tao He <linzhu.ht@alibaba-inc.com> Signed-off-by: Icey <1790571317@qq.com> Signed-off-by: 许文卿 <xwq391974@alibaba-inc.com> Signed-off-by: Chih-Chieh-Yang <7364402+cyang49@users.noreply.github.com> Co-authored-by: Nick Hill <nhill@redhat.com> Co-authored-by: Lucas Kabela <lucasakabela@gmail.com> Co-authored-by: Maximilien de Bayser <mbayser@br.ibm.com> Co-authored-by: Andrew Sansom <andrew@protopia.ai> Co-authored-by: Boyuan Feng <boyuan@meta.com> Co-authored-by: Luka Govedič <ProExpertProg@users.noreply.github.com> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: Harry Mellor <19981378+hmellor@users.noreply.github.com> Co-authored-by: JartX <sagformas@epdcenter.es> Co-authored-by: Chendi.Xue <chendi.xue@intel.com> Co-authored-by: Chauncey <chaunceyjiang@gmail.com> Co-authored-by: xin.li <xin.li@daocloud.io> Co-authored-by: Cyrus Leung <tlleungac@connect.ust.hk> Co-authored-by: Chen Zhang <zhangch99@outlook.com> Co-authored-by: Roger Wang <hey@rogerw.io> Co-authored-by: Michael Goin <mgoin64@gmail.com> Co-authored-by: Wenlong Wang <wangwenlong2755@gmail.com> Co-authored-by: Manoel Marques <manoelmrqs@gmail.com> Co-authored-by: Isotr0py <mozf@mail2.sysu.edu.cn> Co-authored-by: lirong <56789630+lirong-lirong@users.noreply.github.com> Co-authored-by: Michael Yao <haifeng.yao@daocloud.io> Co-authored-by: Woosuk Kwon <woosuk.kwon@berkeley.edu> Co-authored-by: Huamin Li <3ericli@gmail.com> Co-authored-by: Lu Fang <30275821+houseroad@users.noreply.github.com> Co-authored-by: Simon Danielsson <70206058+simondanielsson@users.noreply.github.com> Co-authored-by: Rahul Tuli <rtuli@redhat.com> Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Yang Liu <127183760+KKSK-DON@users.noreply.github.com> Co-authored-by: Deboleina <debroy@redhat.com> Co-authored-by: yinz-aizip <yinz@aizip.ai> Co-authored-by: WeiQing Chen <40507679+david6666666@users.noreply.github.com> Co-authored-by: wangzi <3220100013@zju.edu.cn> Co-authored-by: Eldar Kurtić <8884008+eldarkurtic@users.noreply.github.com> Co-authored-by: Nicolò Lucchesi <nlucches@redhat.com> Co-authored-by: Ye (Charlotte) Qi <yeq@meta.com> Co-authored-by: Yizhou <136800916+yiz-liu@users.noreply.github.com> Co-authored-by: Sara-KS <50249410+Sara-KS@users.noreply.github.com> Co-authored-by: Csrayz <jover@cmbchina.com> Co-authored-by: ivyilike <pww123@cmbchina.com> Co-authored-by: Burkhard Ringlein <ngl@zurich.ibm.com> Co-authored-by: Bowen Wang <abmfy@icloud.com> Co-authored-by: Tyler Michael Smith <tyler@neuralmagic.com> Co-authored-by: Daisy-Ma-coder <daisy.ma.0117@gmail.com> Co-authored-by: qqma <qqma@amazon.com> Co-authored-by: ElizaWszola <ewszola@redhat.com> Co-authored-by: Lucia Fang <116399278+luccafong@users.noreply.github.com> Co-authored-by: Zhuohan Li <zhuohan123@gmail.com> Co-authored-by: Simon Mo <simon.mo@hey.com> Co-authored-by: Or Ozeri <oro@il.ibm.com> Co-authored-by: Johnny Yang <24908445+jcyang43@users.noreply.github.com> Co-authored-by: Chengji Yao <chengjiyao@google.com> Co-authored-by: Alec S <10566873+alecsolder@users.noreply.github.com> Co-authored-by: Alec Solder <alecs@fb.com> Co-authored-by: Russell Bryant <rbryant@redhat.com> Co-authored-by: Matthew Bonanni <mbonanni@redhat.com> Co-authored-by: Robert Shaw <114415538+robertgshaw2-redhat@users.noreply.github.com> Co-authored-by: Chris Bamford <chrisbam4d@gmail.com> Co-authored-by: Alexander Matveev <59768536+alexm-redhat@users.noreply.github.com> Co-authored-by: Wentao Ye <44945378+yewentao256@users.noreply.github.com> Co-authored-by: JJJYmmm <92386084+JJJYmmm@users.noreply.github.com> Co-authored-by: liuye.hj <liuye.hj@alibaba-inc.com> Co-authored-by: Kunshang Ji <kunshang.ji@intel.com> Co-authored-by: Lucia (Lu) Fang <fanglu@meta.com> Co-authored-by: Varun Sundar Rabindranath <varunsundar08@gmail.com> Co-authored-by: Varun Sundar Rabindranath <vsundarr@redhat.com> Co-authored-by: Ming Yang <yming@meta.com> Co-authored-by: Zhikaiiii <55917203+Zhikaiiii@users.noreply.github.com> Co-authored-by: Andreas Hartel <andreas@hartel.me> Co-authored-by: Jee Jee Li <pandaleefree@gmail.com> Co-authored-by: vllmellm <vllm.ellm@embeddedllm.com> Co-authored-by: Joel <wuxibin89@163.com> Co-authored-by: youkaichao <youkaichao@gmail.com> Co-authored-by: Mark McLoughlin <markmc@redhat.com> Co-authored-by: Peter Pan <peter.pan@daocloud.io> Co-authored-by: Nicolò Lucchesi <nicolo.lucchesi@gmail.com> Co-authored-by: Fanli Lin <fanli.lin@intel.com> Co-authored-by: Thomas Parnell <tpa@zurich.ibm.com> Co-authored-by: Lucas Wilkinson <LucasWilkinson@users.noreply.github.com> Co-authored-by: Sage Moore <sage@neuralmagic.com> Co-authored-by: yewentao256 <zhyanwentao@126.com> Co-authored-by: bnellnm <49004751+bnellnm@users.noreply.github.com> Co-authored-by: rivos-shreeasish <shreeasish@rivosinc.com> Co-authored-by: Chih-Chieh Yang <chih.chieh.yang@ibm.com> Co-authored-by: Weida Hong <wdhongtw@gmail.com> Co-authored-by: Ekagra Ranjan <3116519+ekagra-ranjan@users.noreply.github.com> Co-authored-by: Hashem Hashemi <159079214+amd-hhashemi@users.noreply.github.com> Co-authored-by: Amir Samani <samani@ualberta.ca> Co-authored-by: Luka Govedič <lgovedic@redhat.com> Co-authored-by: jiahanc <173873397+jiahanc@users.noreply.github.com> Co-authored-by: Ilya Markov <markovilya197@gmail.com> Co-authored-by: Gregory Shtrasberg <156009573+gshtras@users.noreply.github.com> Co-authored-by: Jialin Ouyang <Jialin.Ouyang@gmail.com> Co-authored-by: rouchenzi <40842833+rouchenzi@users.noreply.github.com> Co-authored-by: Andrew Xia <axia@meta.com> Co-authored-by: kourosh hakhamaneshi <31483498+kouroshHakha@users.noreply.github.com> Co-authored-by: Corey Lowman <clowman1993@gmail.com> Co-authored-by: Juan Villamizar <100237675+jpvillam-amd@users.noreply.github.com> Co-authored-by: jpvillam <jpvillam@amd.com> Co-authored-by: Doug Smith <dosmith@redhat.com> Co-authored-by: Chenxi Yang <cxyang@cs.utexas.edu> Co-authored-by: Chenxi Yang <cxyang@fb.com> Co-authored-by: ahao-anyscale <ahao@anyscale.com> Co-authored-by: 0xNullPath <luyanfcp@foxmail.com> Co-authored-by: baxingpiaochong <771405853@qq.com> Co-authored-by: Benjamin Chislett <bchislett@nvidia.com> Co-authored-by: Kyle Sayers <kylesayrs@gmail.com> Co-authored-by: Nikhil Gupta <nikhil.gupta2@arm.com> Co-authored-by: Yong Hoon Shin <48474650+sarckk@users.noreply.github.com> Co-authored-by: lhsjohn <huashuoli@tencent.com> Co-authored-by: Ben Browning <bbrownin@redhat.com> Co-authored-by: Li, Jiang <jiang1.li@intel.com> Co-authored-by: Jackmin801 <56836461+Jackmin801@users.noreply.github.com> Co-authored-by: Jonas M. Kübler <44084297+jmkuebler@users.noreply.github.com> Co-authored-by: Tao Hui <taohui3@gmail.com> Co-authored-by: rongfu.leng <rongfu.leng@daocloud.io> Co-authored-by: Shu Wang <shuw@nvidia.com> Co-authored-by: Tyler Michael Smith <tlrmchlsmth@gmail.com> Co-authored-by: Duncan Moss <djm.moss@gmail.com> Co-authored-by: Shiyan Deng <dsy842974287@meta.com> Co-authored-by: Wei Wei <wwei6@meta.com> Co-authored-by: Saman A. Pour <samanamp@outlook.com> Co-authored-by: XuruiYang <530534756@qq.com> Co-authored-by: yangxurui <yangxurui@meituan.com> Co-authored-by: Nicole LiHui 🥜 <nicolelihui@outlook.com> Co-authored-by: courage17340 <courage17340@users.noreply.github.com> Co-authored-by: Jacob Kahn <jacobkahn1@gmail.com> Co-authored-by: Nicole LiHui 🥜 <nicole.li@daocloud.io> Co-authored-by: Fadi Arafeh <115173828+fadara01@users.noreply.github.com> Co-authored-by: Agata Dobrzyniewicz <160237065+adobrzyn@users.noreply.github.com> Co-authored-by: yyzxw <34639446+yyzxw@users.noreply.github.com> Co-authored-by: wang.yuqi <noooop@126.com> Co-authored-by: Cyrus Leung <cyrus.tl.leung@gmail.com> Co-authored-by: chenlang <chen.lang5@zte.com.cn> Co-authored-by: chenlang <10346245@zte.com.cn> Co-authored-by: AlonKejzman <alonkeizman@gmail.com> Co-authored-by: tomeras91 <57313761+tomeras91@users.noreply.github.com> Co-authored-by: Aleksandr Malyshev <164964928+maleksan85@users.noreply.github.com> Co-authored-by: Aleksandr Malyshev <maleksan@amd.com> Co-authored-by: Doug Lehr <douglehr@amd.com> Co-authored-by: Eugene Khvedchenya <ekhvedchenya@gmail.com> Co-authored-by: yitingdc <59356937+yitingdc@users.noreply.github.com> Co-authored-by: xaguilar-amd <xavier.aguilarfruto@amd.com> Co-authored-by: Iceber Gu <caiwei95@hotmail.com> Co-authored-by: Tao He <linzhu.ht@alibaba-inc.com> Co-authored-by: Icey <1790571317@qq.com> Co-authored-by: Xu Wenqing <121550081+Xu-Wenqing@users.noreply.github.com> Co-authored-by: Chih-Chieh Yang <7364402+cyang49@users.noreply.github.com> Co-authored-by: RishiAstra <40644327+RishiAstra@users.noreply.github.com>
511 lines
19 KiB
Python
511 lines
19 KiB
Python
# SPDX-License-Identifier: Apache-2.0
|
|
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
|
import copy
|
|
import dataclasses
|
|
from math import prod
|
|
from typing import Optional
|
|
|
|
import pytest
|
|
import torch
|
|
|
|
from vllm import _custom_ops as ops
|
|
from vllm.config import ParallelConfig, VllmConfig, set_current_vllm_config
|
|
from vllm.model_executor.layers.fused_moe.config import (
|
|
FUSED_MOE_UNQUANTIZED_CONFIG, fp8_w8a8_moe_quant_config)
|
|
from vllm.model_executor.layers.fused_moe.cutlass_moe import (
|
|
cutlass_moe_fp8, run_cutlass_moe_fp8)
|
|
from vllm.model_executor.layers.fused_moe.fused_moe import (fused_experts,
|
|
fused_topk)
|
|
from vllm.model_executor.layers.fused_moe.utils import (
|
|
moe_kernel_quantize_input)
|
|
from vllm.platforms import current_platform
|
|
|
|
NUM_EXPERTS = [40, 64]
|
|
TOP_KS = [6, 8]
|
|
|
|
MNK_FACTORS = [
|
|
(2, 1024, 1024),
|
|
(2, 1024, 1536),
|
|
(2, 3072, 1024),
|
|
(2, 3072, 1536),
|
|
(7, 3072, 1536),
|
|
(64, 1024, 1024),
|
|
(64, 1024, 1536),
|
|
(64, 3072, 1024),
|
|
(64, 3072, 1536),
|
|
(224, 1024, 1024),
|
|
(224, 1024, 1536),
|
|
(224, 3072, 1024),
|
|
(224, 3072, 1536),
|
|
(32768, 1024, 1024),
|
|
# These sizes trigger wrong answers.
|
|
#(7232, 2048, 5120),
|
|
#(40000, 2048, 5120),
|
|
]
|
|
|
|
vllm_config = VllmConfig(parallel_config=ParallelConfig(
|
|
pipeline_parallel_size=1))
|
|
vllm_config.scheduler_config.max_num_seqs = 128
|
|
vllm_config.scheduler_config.max_model_len = 8192
|
|
|
|
|
|
@dataclasses.dataclass
|
|
class MOETensors:
|
|
a: torch.Tensor
|
|
w1: torch.Tensor
|
|
w2: torch.Tensor
|
|
ab_strides1: torch.Tensor
|
|
c_strides1: torch.Tensor
|
|
ab_strides2: torch.Tensor
|
|
c_strides2: torch.Tensor
|
|
|
|
@staticmethod
|
|
def make_moe_tensors(m: int, k: int, n: int, e: int,
|
|
dtype: torch.dtype) -> "MOETensors":
|
|
a = torch.randn((m, k), device="cuda", dtype=dtype) / 10
|
|
w1 = torch.randn((e, 2 * n, k), device="cuda", dtype=dtype) / 10
|
|
w2 = torch.randn((e, k, n), device="cuda", dtype=dtype) / 10
|
|
ab_strides1 = torch.full((e, ), k, device="cuda", dtype=torch.int64)
|
|
c_strides1 = torch.full((e, ), 2 * n, device="cuda", dtype=torch.int64)
|
|
ab_strides2 = torch.full((e, ), n, device="cuda", dtype=torch.int64)
|
|
c_strides2 = torch.full((e, ), k, device="cuda", dtype=torch.int64)
|
|
return MOETensors(a=a,
|
|
w1=w1,
|
|
w2=w2,
|
|
ab_strides1=ab_strides1,
|
|
c_strides1=c_strides1,
|
|
ab_strides2=ab_strides2,
|
|
c_strides2=c_strides2)
|
|
|
|
|
|
@dataclasses.dataclass
|
|
class MOETensors8Bit(MOETensors):
|
|
# quantized
|
|
a_q: Optional[torch.Tensor] = None # a -> a_q
|
|
w1_q: Optional[torch.Tensor] = None # w1 -> w1_q
|
|
w2_q: Optional[torch.Tensor] = None # w2 -> w2_q
|
|
a_scale: Optional[torch.Tensor] = None
|
|
w1_scale: Optional[torch.Tensor] = None
|
|
w2_scale: Optional[torch.Tensor] = None
|
|
# dequantized
|
|
a_d: Optional[torch.Tensor] = None # a -> a_q -> a_d
|
|
w1_d: Optional[torch.Tensor] = None # w1 -> w1_q -> w1_d
|
|
w2_d: Optional[torch.Tensor] = None # w2 -> w2_q -> w2_d
|
|
|
|
@staticmethod
|
|
def make_moe_tensors_8bit(m: int, k: int, n: int, e: int,
|
|
per_act_token: bool,
|
|
per_out_channel: bool) -> "MOETensors8Bit":
|
|
dtype = torch.half
|
|
q_dtype = torch.float8_e4m3fn
|
|
|
|
moe_tensors_fp16 = MOETensors.make_moe_tensors(m, k, n, e, dtype)
|
|
|
|
# a -> a_q, w1 -> w1_q, w2 -> w2_q
|
|
n_b_scales = 2 * n if per_out_channel else 1
|
|
k_b_scales = k if per_out_channel else 1
|
|
# Get the right scale for tests.
|
|
a_q, a_scale = ops.scaled_fp8_quant(
|
|
moe_tensors_fp16.a, None, use_per_token_if_dynamic=per_act_token)
|
|
|
|
w1_q = torch.empty((e, 2 * n, k), device="cuda", dtype=q_dtype)
|
|
w2_q = torch.empty((e, k, n), device="cuda", dtype=q_dtype)
|
|
|
|
w1_scale = torch.empty((e, n_b_scales, 1),
|
|
device="cuda",
|
|
dtype=torch.float32)
|
|
w2_scale = torch.empty((e, k_b_scales, 1),
|
|
device="cuda",
|
|
dtype=torch.float32)
|
|
for expert in range(e):
|
|
w1_q[expert], w1_scale[expert] = ops.scaled_fp8_quant(
|
|
moe_tensors_fp16.w1[expert],
|
|
use_per_token_if_dynamic=per_out_channel)
|
|
w2_q[expert], w2_scale[expert] = ops.scaled_fp8_quant(
|
|
moe_tensors_fp16.w2[expert],
|
|
use_per_token_if_dynamic=per_out_channel)
|
|
|
|
# a_q -> a_d, w1_q -> w1_d, w2_q -> w2_d
|
|
a_d = a_q.float().mul(a_scale).to(dtype)
|
|
w1_d = torch.empty_like(moe_tensors_fp16.w1)
|
|
w2_d = torch.empty_like(moe_tensors_fp16.w2)
|
|
for expert in range(e):
|
|
w1_d[expert] = (w1_q[expert].float() * w1_scale[expert]).half()
|
|
w2_d[expert] = (w2_q[expert].float() * w2_scale[expert]).half()
|
|
|
|
return MOETensors8Bit(a=moe_tensors_fp16.a,
|
|
w1=moe_tensors_fp16.w1,
|
|
w2=moe_tensors_fp16.w2,
|
|
ab_strides1=moe_tensors_fp16.ab_strides1,
|
|
c_strides1=moe_tensors_fp16.c_strides1,
|
|
ab_strides2=moe_tensors_fp16.ab_strides2,
|
|
c_strides2=moe_tensors_fp16.c_strides2,
|
|
a_q=a_q,
|
|
w1_q=w1_q,
|
|
w2_q=w2_q,
|
|
a_scale=a_scale,
|
|
w1_scale=w1_scale,
|
|
w2_scale=w2_scale,
|
|
a_d=a_d,
|
|
w1_d=w1_d,
|
|
w2_d=w2_d)
|
|
|
|
|
|
def run_with_expert_maps(num_experts: int, num_local_experts: int,
|
|
**cutlass_moe_kwargs):
|
|
|
|
def slice_experts():
|
|
slice_params = [
|
|
"w1_q", "w2_q", "ab_strides1", "ab_strides2", "c_strides1",
|
|
"c_strides2"
|
|
]
|
|
full_tensors = {
|
|
k: v
|
|
for k, v in cutlass_moe_kwargs.items()
|
|
if k in slice_params and k in cutlass_moe_kwargs
|
|
}
|
|
|
|
quant_config = cutlass_moe_kwargs["quant_config"]
|
|
|
|
for i in range(0, num_experts, num_local_experts):
|
|
s, e = i, i + num_local_experts
|
|
|
|
# make expert map
|
|
expert_map = [-1] * num_experts
|
|
expert_map[s:e] = list(range(num_local_experts))
|
|
expert_map = torch.tensor(expert_map,
|
|
dtype=torch.int32,
|
|
device="cuda")
|
|
|
|
# update cutlass moe arg with expert_map
|
|
cutlass_moe_kwargs["expert_map"] = expert_map
|
|
# update cutlass moe arg tensors
|
|
for k, t in full_tensors.items():
|
|
cutlass_moe_kwargs[k] = t[s:e]
|
|
|
|
new_quant_config = copy.deepcopy(quant_config)
|
|
new_quant_config._w1.scale = quant_config.w1_scale[s:e]
|
|
new_quant_config._w2.scale = quant_config.w2_scale[s:e]
|
|
|
|
cutlass_moe_kwargs["quant_config"] = new_quant_config
|
|
|
|
yield cutlass_moe_kwargs
|
|
|
|
out_tensor = torch.zeros_like(cutlass_moe_kwargs["a"])
|
|
for kwargs in slice_experts():
|
|
out_tensor = out_tensor + cutlass_moe_fp8(**kwargs)
|
|
|
|
return out_tensor
|
|
|
|
|
|
def run_8_bit(moe_tensors: MOETensors8Bit,
|
|
topk_weights: torch.Tensor,
|
|
topk_ids: torch.Tensor,
|
|
per_act_token: bool,
|
|
per_out_ch: bool,
|
|
num_local_experts: Optional[int] = None) -> torch.Tensor:
|
|
assert not any([
|
|
t is None for t in [
|
|
moe_tensors.w1_q, moe_tensors.w2_q, moe_tensors.w1_scale,
|
|
moe_tensors.w2_scale, moe_tensors.a_scale
|
|
]
|
|
])
|
|
|
|
quant_config = fp8_w8a8_moe_quant_config(
|
|
w1_scale=moe_tensors.w1_scale,
|
|
w2_scale=moe_tensors.w2_scale,
|
|
per_act_token_quant=per_act_token,
|
|
per_out_ch_quant=per_out_ch,
|
|
# Set to moe_tensors.a_scale iff static scales + per tensor.
|
|
# This is not currently being tested.
|
|
a1_scale=None,
|
|
)
|
|
|
|
kwargs = {
|
|
'a': moe_tensors.a,
|
|
'w1_q': moe_tensors.w1_q, # type: ignore[union-attr]
|
|
'w2_q': moe_tensors.w2_q, # type: ignore[union-attr]
|
|
'topk_weights': topk_weights,
|
|
'topk_ids': topk_ids,
|
|
'ab_strides1': moe_tensors.ab_strides1,
|
|
'ab_strides2': moe_tensors.ab_strides2,
|
|
'c_strides1': moe_tensors.c_strides1,
|
|
'c_strides2': moe_tensors.c_strides2,
|
|
'quant_config': quant_config,
|
|
}
|
|
|
|
num_experts = moe_tensors.w1.size(0)
|
|
with_ep = num_local_experts is not None or num_local_experts == num_experts
|
|
if not with_ep:
|
|
return cutlass_moe_fp8(**kwargs)
|
|
|
|
assert num_local_experts is not None
|
|
return run_with_expert_maps(
|
|
num_experts,
|
|
num_local_experts, # type: ignore[arg-type]
|
|
**kwargs)
|
|
|
|
|
|
@pytest.mark.parametrize("m,n,k", MNK_FACTORS)
|
|
@pytest.mark.parametrize("e", NUM_EXPERTS)
|
|
@pytest.mark.parametrize("topk", TOP_KS)
|
|
@pytest.mark.parametrize("per_act_token", [True, False])
|
|
@pytest.mark.parametrize("per_out_ch", [True, False])
|
|
@pytest.mark.skipif(
|
|
(lambda x: x is None or not ops.cutlass_group_gemm_supported(x.to_int()))(
|
|
current_platform.get_device_capability()),
|
|
reason="Grouped gemm is not supported on this GPU type.")
|
|
def test_cutlass_moe_8_bit_no_graph(
|
|
m: int,
|
|
n: int,
|
|
k: int,
|
|
e: int,
|
|
topk: int,
|
|
per_act_token: bool,
|
|
per_out_ch: bool,
|
|
monkeypatch,
|
|
ep_size: Optional[int] = None,
|
|
):
|
|
current_platform.seed_everything(7)
|
|
monkeypatch.setenv("VLLM_FUSED_MOE_CHUNK_SIZE", "8192")
|
|
with set_current_vllm_config(vllm_config):
|
|
mt = MOETensors8Bit.make_moe_tensors_8bit(m, k, n, e, per_act_token,
|
|
per_out_ch)
|
|
|
|
score = torch.randn((m, e), device="cuda", dtype=torch.half)
|
|
topk_weights, topk_ids, _ = fused_topk(mt.a,
|
|
score,
|
|
topk,
|
|
renormalize=False)
|
|
|
|
# Note that we are using the dequantized versions of the tensors.
|
|
# Using a, w1 and w2 directly results in minor output differences.
|
|
|
|
quant_config = FUSED_MOE_UNQUANTIZED_CONFIG
|
|
triton_output = fused_experts(mt.a_d,
|
|
mt.w1_d,
|
|
mt.w2_d,
|
|
topk_weights,
|
|
topk_ids,
|
|
quant_config=quant_config)
|
|
|
|
if ep_size is not None:
|
|
assert e % ep_size == 0, "Cannot distribute experts evenly"
|
|
number_local_experts = e // ep_size
|
|
else:
|
|
number_local_experts = None
|
|
|
|
cutlass_output = run_8_bit(mt, topk_weights, topk_ids, per_act_token,
|
|
per_out_ch, number_local_experts)
|
|
|
|
# Note 5.5 only needed for larger problem sizes, 5 works ok for
|
|
# the rest.
|
|
torch.testing.assert_close(triton_output,
|
|
cutlass_output,
|
|
atol=5.5e-2,
|
|
rtol=1e-2)
|
|
|
|
|
|
@pytest.mark.parametrize("m,n,k", MNK_FACTORS)
|
|
@pytest.mark.parametrize("e", NUM_EXPERTS)
|
|
@pytest.mark.parametrize("topk", TOP_KS)
|
|
@pytest.mark.parametrize("per_act_token", [True, False])
|
|
@pytest.mark.parametrize("per_out_ch", [True, False])
|
|
@pytest.mark.skipif(
|
|
(lambda x: x is None or not ops.cutlass_group_gemm_supported(x.to_int()))(
|
|
current_platform.get_device_capability()),
|
|
reason="Grouped gemm is not supported on this GPU type.")
|
|
def test_cutlass_moe_8_bit_cuda_graph(
|
|
m: int,
|
|
n: int,
|
|
k: int,
|
|
e: int,
|
|
topk: int,
|
|
per_act_token: bool,
|
|
per_out_ch: bool,
|
|
monkeypatch,
|
|
):
|
|
current_platform.seed_everything(7)
|
|
monkeypatch.setenv("VLLM_FUSED_MOE_CHUNK_SIZE", "8192")
|
|
with set_current_vllm_config(vllm_config):
|
|
dtype = torch.half
|
|
|
|
mt = MOETensors8Bit.make_moe_tensors_8bit(m, k, n, e, per_act_token,
|
|
per_out_ch)
|
|
|
|
score = torch.randn((m, e), device="cuda", dtype=dtype)
|
|
topk_weights, topk_ids, _ = fused_topk(mt.a,
|
|
score,
|
|
topk,
|
|
renormalize=False)
|
|
|
|
# Note that we are using the dequantized versions of the tensors.
|
|
# Using a, w1 and w2 directly results in minor output differences.
|
|
quant_config = FUSED_MOE_UNQUANTIZED_CONFIG
|
|
triton_output = fused_experts(mt.a_d,
|
|
mt.w1_d,
|
|
mt.w2_d,
|
|
topk_weights,
|
|
topk_ids,
|
|
quant_config=quant_config)
|
|
|
|
stream = torch.cuda.Stream()
|
|
graph = torch.cuda.CUDAGraph()
|
|
with torch.cuda.graph(graph, stream=stream):
|
|
cutlass_output = run_8_bit(mt, topk_weights, topk_ids,
|
|
per_act_token, per_out_ch)
|
|
|
|
torch.cuda.synchronize()
|
|
graph.replay()
|
|
torch.cuda.synchronize()
|
|
|
|
torch.testing.assert_close(triton_output,
|
|
cutlass_output,
|
|
atol=9e-2,
|
|
rtol=1e-2)
|
|
|
|
|
|
@pytest.mark.parametrize("m", [64])
|
|
@pytest.mark.parametrize("n", [1024])
|
|
@pytest.mark.parametrize("k", [4096])
|
|
@pytest.mark.parametrize("e", [16])
|
|
@pytest.mark.parametrize("topk", [1, 8])
|
|
@pytest.mark.parametrize("per_act_token", [True])
|
|
@pytest.mark.parametrize("per_out_channel", [True])
|
|
@pytest.mark.parametrize("ep_size", [1, 2, 4, 8, 16])
|
|
@pytest.mark.skipif(
|
|
(lambda x: x is None or not ops.cutlass_group_gemm_supported(x.to_int()))(
|
|
current_platform.get_device_capability()),
|
|
reason="Grouped gemm is not supported on this GPU type.")
|
|
def test_cutlass_moe_8_bit_EP(
|
|
m: int,
|
|
n: int,
|
|
k: int,
|
|
e: int,
|
|
topk: int,
|
|
per_act_token: bool,
|
|
per_out_channel: bool,
|
|
ep_size: int,
|
|
monkeypatch,
|
|
):
|
|
test_cutlass_moe_8_bit_no_graph(m, n, k, e, topk, per_act_token,
|
|
per_out_channel, monkeypatch, ep_size)
|
|
|
|
|
|
LARGE_MNK_FACTORS = [
|
|
(1, 8192, 5120, 31),
|
|
(32768, 1024, 1024, 16),
|
|
(65536, 512, 1024, 16),
|
|
]
|
|
|
|
|
|
@pytest.mark.parametrize("m,n,k,topk", LARGE_MNK_FACTORS)
|
|
@pytest.mark.parametrize("e", [128])
|
|
@pytest.mark.parametrize("per_act_token", [False])
|
|
@pytest.mark.parametrize("per_out_channel", [True])
|
|
@pytest.mark.parametrize("ep_size", [8])
|
|
@pytest.mark.skipif(
|
|
(lambda x: x is None or not ops.cutlass_group_gemm_supported(x.to_int()))(
|
|
current_platform.get_device_capability()),
|
|
reason="Grouped gemm is not supported on this GPU type.")
|
|
def test_cutlass_moe_8_bit_EP_large(
|
|
m: int,
|
|
n: int,
|
|
k: int,
|
|
e: int,
|
|
topk: int,
|
|
per_act_token: bool,
|
|
per_out_channel: bool,
|
|
ep_size: int,
|
|
monkeypatch,
|
|
):
|
|
test_cutlass_moe_8_bit_no_graph(m, n, k, e, topk, per_act_token,
|
|
per_out_channel, monkeypatch, ep_size)
|
|
|
|
|
|
@pytest.mark.parametrize("m,n,k,topk", [(1, 8192, 5120, 31)])
|
|
@pytest.mark.parametrize("e", [128])
|
|
@pytest.mark.parametrize("per_act_token", [False])
|
|
@pytest.mark.parametrize("per_out_channel", [True])
|
|
@pytest.mark.parametrize("ep_size", [8])
|
|
@pytest.mark.skipif(
|
|
(lambda x: x is None or not ops.cutlass_group_gemm_supported(x.to_int()))(
|
|
current_platform.get_device_capability()),
|
|
reason="Grouped gemm is not supported on this GPU type.")
|
|
def test_run_cutlass_moe_fp8(
|
|
m: int,
|
|
n: int,
|
|
k: int,
|
|
e: int,
|
|
topk: int,
|
|
per_act_token: bool,
|
|
per_out_channel: bool,
|
|
ep_size: int,
|
|
):
|
|
current_platform.seed_everything(7)
|
|
with set_current_vllm_config(vllm_config):
|
|
mt = MOETensors8Bit.make_moe_tensors_8bit(m, k, n, e, per_act_token,
|
|
per_out_channel)
|
|
|
|
score = torch.randn((m, e), device="cuda", dtype=torch.half)
|
|
topk_weights, topk_ids, _ = fused_topk(mt.a,
|
|
score,
|
|
topk,
|
|
renormalize=False)
|
|
# we want to make sure there is at least one token that's generated in
|
|
# this expert shard and at least one token that's NOT generated in this
|
|
# expert shard
|
|
topk_ids[0][0] = -1
|
|
topk_ids[0][1] = 1
|
|
|
|
workspace13_shape = (m * topk, max(2 * n, k))
|
|
workspace2_shape = (m * topk, max(n, k))
|
|
output_shape = (m, k)
|
|
|
|
workspace13 = torch.empty(prod(workspace13_shape),
|
|
device="cuda",
|
|
dtype=mt.a.dtype)
|
|
workspace2 = torch.empty(prod(workspace2_shape),
|
|
device="cuda",
|
|
dtype=mt.a.dtype)
|
|
|
|
num_local_experts = e // ep_size
|
|
start, end = 0, num_local_experts
|
|
expert_map = [-1] * e
|
|
expert_map[start:end] = list(range(num_local_experts))
|
|
expert_map = torch.tensor(expert_map, dtype=torch.int32, device="cuda")
|
|
|
|
ab_strides1 = torch.full((e, ), k, device="cuda", dtype=torch.int64)
|
|
ab_strides2 = torch.full((e, ), n, device="cuda", dtype=torch.int64)
|
|
c_strides1 = torch.full((e, ), 2 * n, device="cuda", dtype=torch.int64)
|
|
c_strides2 = torch.full((e, ), k, device="cuda", dtype=torch.int64)
|
|
|
|
activation = lambda o, i: torch.ops._C.silu_and_mul(o, i)
|
|
a1q, a1q_scale = moe_kernel_quantize_input(mt.a, mt.a_scale,
|
|
torch.float8_e4m3fn,
|
|
per_act_token)
|
|
global_num_experts = -1 if mt.w1_q is None else mt.w1_q.size(0)
|
|
func = lambda output: run_cutlass_moe_fp8(
|
|
output, a1q, mt.w1_q, mt.w2_q, topk_ids, activation,
|
|
global_num_experts, expert_map, mt.w1_scale, mt.w2_scale,
|
|
a1q_scale, None, ab_strides1, ab_strides2, c_strides1, c_strides2,
|
|
workspace13, workspace2, None, mt.a.dtype, per_act_token,
|
|
per_out_channel, False, topk_weights)
|
|
|
|
workspace13.random_()
|
|
output_random_workspace = torch.empty(output_shape,
|
|
device="cuda",
|
|
dtype=mt.a.dtype)
|
|
func(output_random_workspace)
|
|
|
|
workspace13.fill_(0)
|
|
output_zero_workspace = torch.zeros(output_shape,
|
|
device="cuda",
|
|
dtype=mt.a.dtype)
|
|
func(output_zero_workspace)
|
|
|
|
torch.testing.assert_close(output_random_workspace,
|
|
output_zero_workspace,
|
|
atol=5e-3,
|
|
rtol=1e-3)
|