mirror of
https://github.com/vllm-project/vllm.git
synced 2025-10-20 23:03:52 +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>
589 lines
20 KiB
Python
589 lines
20 KiB
Python
# SPDX-License-Identifier: Apache-2.0
|
|
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
|
from abc import ABC, abstractmethod
|
|
from statistics import mean
|
|
from typing import Any, NamedTuple, Optional, Union
|
|
|
|
import numpy as np # type: ignore
|
|
import pandas as pd # type: ignore
|
|
from bench_utils import (
|
|
TEXT_SEPARATOR,
|
|
Color,
|
|
logger,
|
|
)
|
|
from transformers import AutoTokenizer # type: ignore
|
|
|
|
# Conversation ID is a string (e.g: "UzTK34D")
|
|
ConvId = str
|
|
|
|
# A list of dicts (dicts with keys "id" and "messages")
|
|
ShareGptConversations = list[dict[str, Any]]
|
|
|
|
# A list of dicts (dicts with keys "role" and "content")
|
|
MessagesList = list[dict[str, str]]
|
|
|
|
# Map conversation ID to conversation messages
|
|
ConversationsMap = list[ConvId, MessagesList]
|
|
|
|
|
|
class Distribution(ABC):
|
|
@abstractmethod
|
|
def sample(self, size: int = 1) -> np.ndarray:
|
|
pass
|
|
|
|
|
|
class UniformDistribution(Distribution):
|
|
def __init__(
|
|
self,
|
|
min_val: Union[int, float],
|
|
max_val: Union[int, float],
|
|
is_integer: bool = True,
|
|
) -> None:
|
|
self.min_val = min_val
|
|
self.max_val = max_val
|
|
self.is_integer = is_integer
|
|
|
|
def sample(self, size: int = 1) -> np.ndarray:
|
|
if self.is_integer:
|
|
return np.random.randint(
|
|
int(self.min_val), int(self.max_val + 1), size=size
|
|
)
|
|
else:
|
|
return np.random.uniform(self.min_val, self.max_val, size=size)
|
|
|
|
def __repr__(self) -> str:
|
|
return f"UniformDistribution[{self.min_val}, {self.max_val}]"
|
|
|
|
|
|
class ConstantDistribution(Distribution):
|
|
def __init__(self, value: Union[int, float]) -> None:
|
|
self.value = value
|
|
self.max_val = value
|
|
|
|
def sample(self, size: int = 1) -> np.ndarray:
|
|
return np.full(shape=size, fill_value=self.value)
|
|
|
|
def __repr__(self) -> str:
|
|
return f"Constant[{self.value}]"
|
|
|
|
|
|
class ZipfDistribution(Distribution):
|
|
def __init__(self, alpha: float, max_val: Optional[int] = None) -> None:
|
|
self.alpha = alpha
|
|
self.max_val = max_val
|
|
|
|
def sample(self, size: int = 1) -> np.ndarray:
|
|
samples = np.random.zipf(self.alpha, size=size)
|
|
if self.max_val:
|
|
samples = np.minimum(samples, self.max_val)
|
|
return samples
|
|
|
|
def __repr__(self) -> str:
|
|
return f"ZipfDistribution[{self.alpha}]"
|
|
|
|
|
|
class PoissonDistribution(Distribution):
|
|
def __init__(self, alpha: float, max_val: Optional[int] = None) -> None:
|
|
self.alpha = alpha
|
|
self.max_val = max_val
|
|
|
|
def sample(self, size: int = 1) -> np.ndarray:
|
|
samples = np.random.poisson(self.alpha, size=size)
|
|
if self.max_val:
|
|
samples = np.minimum(samples, self.max_val)
|
|
return samples
|
|
|
|
def __repr__(self) -> str:
|
|
return f"PoissonDistribution[{self.alpha}]"
|
|
|
|
|
|
class LognormalDistribution(Distribution):
|
|
def __init__(
|
|
self,
|
|
mean: Optional[float] = None,
|
|
sigma: Optional[float] = None,
|
|
average: Optional[int] = None,
|
|
median_ratio: Optional[float] = None,
|
|
max_val: Optional[int] = None,
|
|
) -> None:
|
|
self.average = average
|
|
self.median_ratio = median_ratio
|
|
self.max_val = max_val
|
|
|
|
if average is not None:
|
|
if average < 1:
|
|
raise ValueError("Lognormal average must be positive")
|
|
|
|
if mean or sigma:
|
|
raise ValueError(
|
|
"When using lognormal average, you can't provide mean/sigma"
|
|
)
|
|
|
|
if self.median_ratio is None:
|
|
# Default value that provides relatively wide range of values
|
|
self.median_ratio = 0.85
|
|
|
|
# Calculate mean/sigma of np.random.lognormal based on the average
|
|
mean, sigma = self._generate_lognormal_by_median(
|
|
target_average=self.average, median_ratio=self.median_ratio
|
|
)
|
|
else:
|
|
if mean is None or sigma is None:
|
|
raise ValueError(
|
|
"Must provide both mean and sigma if average is not used"
|
|
)
|
|
|
|
if mean <= 0 or sigma < 0:
|
|
raise ValueError(
|
|
"Lognormal mean must be positive and sigma must be non-negative"
|
|
)
|
|
|
|
# Mean and standard deviation of the underlying normal distribution
|
|
# Based on numpy.random.lognormal
|
|
self.mean = mean
|
|
self.sigma = sigma
|
|
|
|
@staticmethod
|
|
def _generate_lognormal_by_median(
|
|
target_average: int, median_ratio: float
|
|
) -> tuple[float, float]:
|
|
"""
|
|
Compute (mu, sigma) for a lognormal distribution given:
|
|
- a target average (mean of the distribution)
|
|
- a ratio of median / mean (controls skewness), assume mean > median
|
|
|
|
Background:
|
|
If Z ~ Normal(mu, sigma^2), then X = exp(Z) ~ LogNormal(mu, sigma).
|
|
* mean(X) = exp(mu + sigma^2 / 2)
|
|
* median(X) = exp(mu)
|
|
|
|
So:
|
|
median / mean = exp(mu) / exp(mu + sigma^2 / 2)
|
|
= exp(-sigma^2 / 2)
|
|
|
|
Rearranging:
|
|
sigma^2 = 2 * ln(mean / median)
|
|
mu = ln(median)
|
|
|
|
This gives a unique (mu, sigma) for any valid mean and median.
|
|
"""
|
|
# Check input validity: median must be smaller than mean
|
|
if median_ratio <= 0 or median_ratio >= 1:
|
|
raise ValueError("median_ratio must be in range (0, 1)")
|
|
|
|
target_median = target_average * median_ratio
|
|
|
|
# Solve sigma^2 = 2 * ln(mean / median)
|
|
sigma = np.sqrt(2 * np.log(target_average / target_median))
|
|
mu = np.log(target_median)
|
|
|
|
return mu, sigma
|
|
|
|
def sample(self, size: int = 1) -> np.ndarray:
|
|
samples = np.random.lognormal(mean=self.mean, sigma=self.sigma, size=size)
|
|
|
|
if self.average is not None:
|
|
# Scale to average
|
|
samples *= self.average / samples.mean()
|
|
|
|
if self.max_val:
|
|
samples = np.minimum(samples, self.max_val)
|
|
|
|
return np.round(samples).astype(int)
|
|
|
|
def __repr__(self) -> str:
|
|
if self.average:
|
|
return (
|
|
f"LognormalDistribution[{self.average}, "
|
|
f"{self.median_ratio}, {self.max_val}]"
|
|
)
|
|
return f"LognormalDistribution[{self.mean}, {self.sigma}, {self.max_val}]"
|
|
|
|
|
|
class GenConvArgs(NamedTuple):
|
|
num_conversations: int
|
|
text_files: list[str]
|
|
input_num_turns: Distribution
|
|
input_common_prefix_num_tokens: Distribution
|
|
input_prefix_num_tokens: Distribution
|
|
input_num_tokens: Distribution
|
|
output_num_tokens: Distribution
|
|
print_stats: bool
|
|
|
|
|
|
def verify_field_exists(
|
|
conf: dict, field_name: str, section: str, subsection: str
|
|
) -> None:
|
|
if field_name not in conf:
|
|
raise ValueError(
|
|
f"Missing field '{field_name}' in {section=} and {subsection=}"
|
|
)
|
|
|
|
|
|
def get_random_distribution(
|
|
conf: dict, section: str, subsection: str, optional: bool = False
|
|
) -> Distribution:
|
|
# section can be "prompt_input" or "prompt_output" (both required)
|
|
conf = conf[section]
|
|
|
|
if optional and subsection not in conf:
|
|
# Optional subsection, if not found assume the value is always 0
|
|
return ConstantDistribution(0)
|
|
|
|
# subsection can be "num_turns", "num_tokens" or "prefix_num_tokens"
|
|
if subsection not in conf:
|
|
raise ValueError(f"Missing subsection {subsection} in section {section}")
|
|
|
|
conf = conf[subsection]
|
|
|
|
distribution = conf.get("distribution")
|
|
if distribution is None:
|
|
raise ValueError(
|
|
f"Missing field 'distribution' in {section=} and {subsection=}"
|
|
)
|
|
|
|
if distribution == "constant":
|
|
verify_field_exists(conf, "value", section, subsection)
|
|
return ConstantDistribution(conf["value"])
|
|
|
|
elif distribution == "zipf":
|
|
verify_field_exists(conf, "alpha", section, subsection)
|
|
max_val = conf.get("max", None)
|
|
return ZipfDistribution(conf["alpha"], max_val=max_val)
|
|
|
|
elif distribution == "poisson":
|
|
verify_field_exists(conf, "alpha", section, subsection)
|
|
max_val = conf.get("max", None)
|
|
return PoissonDistribution(conf["alpha"], max_val=max_val)
|
|
|
|
elif distribution == "lognormal":
|
|
max_val = conf.get("max", None)
|
|
|
|
if "average" in conf:
|
|
# Infer lognormal mean/sigma (numpy) from input average
|
|
median_ratio = conf.get("median_ratio", None)
|
|
return LognormalDistribution(
|
|
average=conf["average"], median_ratio=median_ratio, max_val=max_val
|
|
)
|
|
|
|
# Use mean/sigma directly (for full control over the distribution)
|
|
verify_field_exists(conf, "mean", section, subsection)
|
|
verify_field_exists(conf, "sigma", section, subsection)
|
|
return LognormalDistribution(
|
|
mean=conf["mean"], sigma=conf["sigma"], max_val=max_val
|
|
)
|
|
|
|
elif distribution == "uniform":
|
|
verify_field_exists(conf, "min", section, subsection)
|
|
verify_field_exists(conf, "max", section, subsection)
|
|
|
|
min_value = conf["min"]
|
|
max_value = conf["max"]
|
|
|
|
assert min_value > 0
|
|
assert min_value <= max_value
|
|
|
|
is_integer = isinstance(min_value, int) and isinstance(max_value, int)
|
|
return UniformDistribution(min_value, max_value, is_integer)
|
|
else:
|
|
raise ValueError(f"Unknown distribution: {distribution}")
|
|
|
|
|
|
def parse_input_json_file(conf: dict) -> GenConvArgs:
|
|
# Validate the input file
|
|
assert isinstance(conf, dict)
|
|
required_fields = [
|
|
"filetype",
|
|
"num_conversations",
|
|
"text_files",
|
|
"prompt_input",
|
|
"prompt_output",
|
|
]
|
|
for field in required_fields:
|
|
assert field in conf, f"Missing field {field} in input {conf}"
|
|
|
|
assert conf["filetype"] == "generate_conversations"
|
|
|
|
assert conf["num_conversations"] > 0, "num_conversations should be larger than zero"
|
|
|
|
text_files = conf["text_files"]
|
|
|
|
assert isinstance(text_files, list), "Field 'text_files' should be a list"
|
|
assert len(text_files) > 0, (
|
|
"Field 'text_files' should be a list with at least one file"
|
|
)
|
|
|
|
# Parse the parameters for the prompt input/output workload
|
|
input_num_turns = get_random_distribution(conf, "prompt_input", "num_turns")
|
|
input_num_tokens = get_random_distribution(conf, "prompt_input", "num_tokens")
|
|
input_common_prefix_num_tokens = get_random_distribution(
|
|
conf, "prompt_input", "common_prefix_num_tokens", optional=True
|
|
)
|
|
input_prefix_num_tokens = get_random_distribution(
|
|
conf, "prompt_input", "prefix_num_tokens"
|
|
)
|
|
output_num_tokens = get_random_distribution(conf, "prompt_output", "num_tokens")
|
|
|
|
print_stats: bool = conf.get("print_stats", False)
|
|
assert isinstance(print_stats, bool), (
|
|
"Field 'print_stats' should be either 'true' or 'false'"
|
|
)
|
|
|
|
args = GenConvArgs(
|
|
num_conversations=conf["num_conversations"],
|
|
text_files=text_files,
|
|
input_num_turns=input_num_turns,
|
|
input_common_prefix_num_tokens=input_common_prefix_num_tokens,
|
|
input_prefix_num_tokens=input_prefix_num_tokens,
|
|
input_num_tokens=input_num_tokens,
|
|
output_num_tokens=output_num_tokens,
|
|
print_stats=print_stats,
|
|
)
|
|
return args
|
|
|
|
|
|
def print_conv_stats(conversations: ConversationsMap, tokenizer: AutoTokenizer) -> None:
|
|
# Collect statistics
|
|
conv_stats: list[dict[Any, Any]] = []
|
|
req_stats: list[int] = []
|
|
|
|
print("\nCollecting statistics...")
|
|
for messages in conversations.values():
|
|
# messages is a list of dicts
|
|
user_tokens: list[int] = []
|
|
assistant_tokens: list[int] = []
|
|
request_tokens: list[int] = []
|
|
|
|
req_tokens = 0
|
|
for m in messages:
|
|
content = m["content"]
|
|
num_tokens = len(tokenizer(content).input_ids)
|
|
|
|
if m["role"] == "user":
|
|
user_tokens.append(num_tokens)
|
|
# New user prompt including all chat history
|
|
req_tokens += num_tokens
|
|
request_tokens.append(req_tokens)
|
|
|
|
elif m["role"] == "assistant":
|
|
assistant_tokens.append(num_tokens)
|
|
# Update assistant answer
|
|
# (will be part of chat history for the next user prompt)
|
|
req_tokens += num_tokens
|
|
|
|
item_stats = {
|
|
"conversation_turns": len(messages),
|
|
"user_tokens": mean(user_tokens),
|
|
"assistant_tokens": mean(assistant_tokens),
|
|
}
|
|
|
|
conv_stats.append(item_stats)
|
|
req_stats.extend(request_tokens)
|
|
|
|
# Print statistics
|
|
percentiles = [0.25, 0.5, 0.75, 0.9, 0.99]
|
|
|
|
print(TEXT_SEPARATOR)
|
|
print(f"{Color.YELLOW}Conversations statistics:{Color.RESET}")
|
|
print(TEXT_SEPARATOR)
|
|
df = pd.DataFrame(conv_stats)
|
|
print(df.describe(percentiles=percentiles).transpose())
|
|
print(TEXT_SEPARATOR)
|
|
print(f"{Color.YELLOW}Request statistics:{Color.RESET}")
|
|
print(TEXT_SEPARATOR)
|
|
df = pd.DataFrame(req_stats, columns=["request_tokens"])
|
|
print(df.describe(percentiles=percentiles).transpose())
|
|
print(TEXT_SEPARATOR)
|
|
|
|
|
|
def generate_conversations(
|
|
args: GenConvArgs, tokenizer: AutoTokenizer
|
|
) -> ConversationsMap:
|
|
# Text for all user prompts
|
|
# (text from the input text files will be appended to this line)
|
|
base_prompt_text = "Please rewrite the following text and add more content: "
|
|
base_prompt_token_count = len(
|
|
tokenizer.encode(base_prompt_text, add_special_tokens=False)
|
|
)
|
|
|
|
logger.info(f"{Color.PURPLE}Generating conversations...{Color.RESET}")
|
|
logger.info(args)
|
|
|
|
list_of_tokens = []
|
|
|
|
for filename in args.text_files:
|
|
# Load text file that will be used to generate prompts
|
|
with open(filename) as file:
|
|
data = file.read()
|
|
tokens_in_file = tokenizer.encode(data, add_special_tokens=False)
|
|
list_of_tokens.extend(tokens_in_file)
|
|
|
|
conversations: ConversationsMap = {}
|
|
conv_id = 0
|
|
|
|
# Generate number of turns for every conversation
|
|
turn_count: np.ndarray = args.input_num_turns.sample(args.num_conversations)
|
|
|
|
# Turn count should be at least 2 (one user prompt and one assistant answer)
|
|
turn_count = np.maximum(turn_count, 2)
|
|
|
|
# Round up to an even number (every user prompt should have an answer)
|
|
turn_count = turn_count + (turn_count % 2)
|
|
|
|
# Generate number of prefix tokens for every conversation
|
|
conv_prefix_tokens: np.ndarray = args.input_prefix_num_tokens.sample(
|
|
args.num_conversations
|
|
)
|
|
|
|
# Used to reduce shared text between conversations
|
|
# (jump/skip over text sections between conversations)
|
|
base_offset = 0
|
|
|
|
# Common prefix size for all conversations (only 1 sample required)
|
|
common_prefix_text = ""
|
|
common_prefix_tokens: int = args.input_common_prefix_num_tokens.sample(1)[0]
|
|
if common_prefix_tokens > 0:
|
|
# Using "." at the end to separate sentences
|
|
common_prefix_text = (
|
|
tokenizer.decode(list_of_tokens[: common_prefix_tokens - 2]) + "."
|
|
)
|
|
base_offset += common_prefix_tokens
|
|
|
|
for conv_id in range(args.num_conversations):
|
|
# Generate a single conversation
|
|
messages: MessagesList = []
|
|
|
|
nturns = turn_count[conv_id]
|
|
|
|
# User prompt token count per turn (with lower limit)
|
|
input_token_count: np.ndarray = args.input_num_tokens.sample(nturns)
|
|
input_token_count = np.maximum(input_token_count, base_prompt_token_count)
|
|
|
|
# Assistant answer token count per turn (with lower limit)
|
|
output_token_count: np.ndarray = args.output_num_tokens.sample(nturns)
|
|
output_token_count = np.maximum(output_token_count, 1)
|
|
|
|
user_turn = True
|
|
for turn_id in range(nturns):
|
|
if user_turn:
|
|
role = "user"
|
|
num_tokens = input_token_count[turn_id]
|
|
|
|
# Generate the user prompt,
|
|
# use a unique prefix (the conv_id) for each conversation
|
|
# (to avoid shared prefix between conversations)
|
|
content = f"{conv_id} is a nice number... "
|
|
|
|
if len(common_prefix_text) > 0 and turn_id == 0:
|
|
content = common_prefix_text + content
|
|
|
|
# Update the number of tokens left for the content
|
|
num_tokens -= len(tokenizer.encode(content, add_special_tokens=False))
|
|
|
|
if turn_id == 0:
|
|
prefix_num_tokens = conv_prefix_tokens[conv_id]
|
|
if prefix_num_tokens > 0:
|
|
# Add prefix text (context) to the first turn
|
|
start_offset = base_offset
|
|
end_offset = start_offset + prefix_num_tokens
|
|
assert len(list_of_tokens) > end_offset, (
|
|
"Not enough input text to generate "
|
|
f"{prefix_num_tokens} tokens for the "
|
|
f"prefix text ({start_offset=}, {end_offset=})"
|
|
)
|
|
|
|
content += f"{conv_id}, " + tokenizer.decode(
|
|
list_of_tokens[start_offset:end_offset]
|
|
)
|
|
base_offset += prefix_num_tokens
|
|
|
|
# Add the actual user prompt/question after the prefix text
|
|
content += base_prompt_text
|
|
num_tokens -= base_prompt_token_count
|
|
|
|
if num_tokens > 0:
|
|
# Add text from the input file (to reach the desired token count)
|
|
start_offset = base_offset + turn_id * input_token_count.max()
|
|
end_offset = start_offset + num_tokens
|
|
assert len(list_of_tokens) > end_offset, (
|
|
f"Not enough input text to generate {num_tokens} tokens "
|
|
f"for the prompt ({start_offset=}, {end_offset=})"
|
|
)
|
|
|
|
# Convert tokens back to text
|
|
content += tokenizer.decode(list_of_tokens[start_offset:end_offset])
|
|
else:
|
|
role = "assistant"
|
|
# This content will not be used as input to the LLM server
|
|
# (actual answers will be used instead).
|
|
# Content is only required to determine the min_tokens/max_tokens
|
|
# (inputs to the LLM server).
|
|
num_tokens = output_token_count[turn_id]
|
|
assert len(list_of_tokens) > num_tokens, (
|
|
f"Not enough input text to generate {num_tokens} "
|
|
"tokens for assistant content"
|
|
)
|
|
content = tokenizer.decode(list_of_tokens[:num_tokens])
|
|
|
|
# Append the user/assistant message to the list of messages
|
|
messages.append({"role": role, "content": content})
|
|
user_turn = not user_turn
|
|
|
|
# Add the new conversation
|
|
conversations[f"CONV_ID_{conv_id}"] = messages
|
|
|
|
# Increase base offset for the next conversation
|
|
base_offset += nturns
|
|
|
|
if args.print_stats:
|
|
print_conv_stats(conversations, tokenizer)
|
|
|
|
return conversations
|
|
|
|
|
|
def conversations_list_to_dict(input_list: ShareGptConversations) -> ConversationsMap:
|
|
conversations: ConversationsMap = {}
|
|
|
|
for item in input_list:
|
|
conv_id: str = item["id"]
|
|
assert isinstance(conv_id, str)
|
|
|
|
assert conv_id not in conversations, (
|
|
f"Conversation ID {conv_id} found more than once in the input"
|
|
)
|
|
|
|
messages: MessagesList = item["messages"]
|
|
assert isinstance(messages, list), (
|
|
f"Conversation messages should be a list (ID: {conv_id})"
|
|
)
|
|
assert len(messages) > 0, f"Conversation with no messages (ID: {conv_id})"
|
|
|
|
conversations[conv_id] = messages
|
|
|
|
logger.info(f"Using {len(conversations)} unique conversations (IDs)")
|
|
assert len(conversations) == len(input_list)
|
|
|
|
# Print statistics about the selected conversations
|
|
stats: list[dict[str, Any]] = []
|
|
for conv_data in conversations.values():
|
|
stats.append({"num_turns": len(conv_data)})
|
|
|
|
print(TEXT_SEPARATOR)
|
|
print(f"{Color.YELLOW}Conversations statistics:{Color.RESET}")
|
|
print(TEXT_SEPARATOR)
|
|
percentiles = [0.25, 0.5, 0.75, 0.9, 0.99, 0.999, 0.9999]
|
|
conv_stats = pd.DataFrame(stats).describe(percentiles=percentiles)
|
|
print(conv_stats.transpose())
|
|
print(TEXT_SEPARATOR)
|
|
|
|
return conversations
|
|
|
|
|
|
def conversations_dict_to_list(input_dict: ConversationsMap) -> ShareGptConversations:
|
|
output: ShareGptConversations = []
|
|
for conv_id, conv_data in input_dict.items():
|
|
new_item = {"id": conv_id, "messages": conv_data}
|
|
output.append(new_item)
|
|
|
|
return output
|