mirror of
https://git.datalinker.icu/vllm-project/vllm.git
synced 2026-01-20 22:14:43 +08:00
Signed-off-by: rongfu.leng <rongfu.leng@daocloud.io> Signed-off-by: Jinzhen Lin <linjinzhen@hotmail.com> Signed-off-by: Huzaifa Sidhpurwala <huzaifas@redhat.com> Signed-off-by: Varun Sundar Rabindranath <vsundarr@redhat.com> Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com> Signed-off-by: Jee Jee Li <pandaleefree@gmail.com> Signed-off-by: mgoin <mgoin64@gmail.com> Signed-off-by: Animesh Jain <anijain@umich.edu> Signed-off-by: Rui Qiao <ruisearch42@gmail.com> Signed-off-by: Xiongfei Wei <isaacwxf23@gmail.com> Signed-off-by: Nick Hill <nhill@redhat.com> Signed-off-by: yewentao256 <zhyanwentao@126.com> Signed-off-by: kf <kuanfu.liu@embeddedllm.com> Signed-off-by: vllmellm <vllm.ellm@embeddedllm.com> Signed-off-by: NickLucche <nlucches@redhat.com> Signed-off-by: Dipika Sikka <dipikasikka1@gmail.com> Signed-off-by: Sage Moore <sage@neuralmagic.com> Signed-off-by: tjtanaavllm <tunjian.tan@amd.com> Signed-off-by: Yong Hoon Shin <yhshin@meta.com> Signed-off-by: Chih-Chieh-Yang <7364402+cyang49@users.noreply.github.com> Signed-off-by: Roger Wang <hey@rogerw.me> Signed-off-by: Vadim Gimpelson <vadim.gimpelson@centml.ai> Signed-off-by: Isotr0py <2037008807@qq.com> Signed-off-by: zRzRzRzRzRzRzR <2448370773@qq.com> Signed-off-by: Chih-Chieh Yang <7364402+cyang49@users.noreply.github.com> Signed-off-by: DarkLight1337 <tlleungac@connect.ust.hk> Signed-off-by: Thomas Parnell <tpa@zurich.ibm.com> Signed-off-by: yan <yan.ma@intel.com> Signed-off-by: Yan Ma <yan.ma@intel.com> Signed-off-by: Xiao Liu <xiszishu@gmail.com> Signed-off-by: jiahanc <173873397+jiahanc@users.noreply.github.com> Signed-off-by: Isotr0py <mozf@mail2.sysu.edu.cn> Signed-off-by: Ye (Charlotte) Qi <yeq@meta.com> Signed-off-by: LopezCastroRoberto <roberto.lopez.castro@udc.es> Signed-off-by: Andy Xie <andy.xning@gmail.com> Signed-off-by: Haibin Lin <haibin.lin@bytedance.com> Signed-off-by: David Ben-David <davidb@pliops.com> Signed-off-by: Woosuk Kwon <woosuk.kwon@berkeley.edu> Signed-off-by: jiang1.li <jiang1.li@intel.com> Signed-off-by: Seiji Eicher <seiji@anyscale.com> Signed-off-by: zitian.zhao <zitian.zhao@tencentmusic.com> Signed-off-by: 22quinn <33176974+22quinn@users.noreply.github.com> Signed-off-by: Abirdcfly <fp544037857@gmail.com> Signed-off-by: Giancarlo Delfin <gdelfin@meta.com> Signed-off-by: Tyler Michael Smith <tyler@neuralmagic.com> Signed-off-by: huangweixiao <huangweixiao@msh.team> Signed-off-by: alyosha-swamy <raghav@arcee.ai> Signed-off-by: Eric Hanley <ericehanley@google.com> Signed-off-by: Abatom <abzhonghua@gmail.com> Signed-off-by: CLFutureX <775523362@qq.com> Signed-off-by: Linkun Chen <github@lkchen.net> Signed-off-by: tjtanaa <tunjian.tan@embeddedllm.com> Signed-off-by: Gregory Shtrasberg <Gregory.Shtrasberg@amd.com> Signed-off-by: tlipoca9 <tlipoca9@gmail.com> Signed-off-by: elvischenv <219235043+elvischenv@users.noreply.github.com> Signed-off-by: zitian zhao <zitian.zhao@tencentmusic.com> Signed-off-by: mgoin <michael@neuralmagic.com> Signed-off-by: wang.yuqi <noooop@126.com> Signed-off-by: Benji Beck <benjibeck@meta.com> Signed-off-by: Siyuan Liu <lsiyuan@google.com> Signed-off-by: Benjamin Chislett <benjamin.chislett@centml.ai> Signed-off-by: isotr0py <2037008807@qq.com> Signed-off-by: Chen Zhang <zhangch99@outlook.com> Signed-off-by: simon-mo <xmo@berkeley.edu> Signed-off-by: LucasWilkinson <lwilkinson@neuralmagic.com> Signed-off-by: Zhang Jason <ning.zhang2@amd.com> Signed-off-by: Yongye Zhu <zyy1102000@gmail.com> Signed-off-by: asafg <asafg@ai21.com> Signed-off-by: Siyuan Fu <siyuanf@nvidia.com> Signed-off-by: Lain <fusiyuan2000@hotmail.com> Signed-off-by: Max de Bayser <mbayser@br.ibm.com> Signed-off-by: Lucas Wilkinson <lwilkins@redhat.com> Signed-off-by: Kunshang Ji <kunshang.ji@intel.com> Signed-off-by: Tao He <linzhu.ht@alibaba-inc.com> Signed-off-by: Michael Goin <mgoin64@gmail.com> Signed-off-by: QscQ <qscqesze@gmail.com> Signed-off-by: qingjun <qingjun@minimaxi.com> Signed-off-by: Syed Muhammad Bin Asif <syedmba7@connect.hku.hk> Signed-off-by: Lionel Villard <villard@us.ibm.com> Signed-off-by: ycyaw66 <497410282@qq.com> Signed-off-by: David Chen <530634352@qq.com> Signed-off-by: Linkun <github@lkchen.net> Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> Signed-off-by: Ming Yang <minos.future@gmail.com> Signed-off-by: Adrian Garcia <adrian.garcia@inceptionai.ai> Signed-off-by: shaojunqi <shaojunqi.sjq@alibaba-inc.com> Signed-off-by: Ricardo Decal <rdecal@anyscale.com> Signed-off-by: Andrew Chan <andrewkchan.akc@gmail.com> Signed-off-by: Felix Marty <Felix.Marty@amd.com> Signed-off-by: Andrew Sansom <andrew@protopia.ai> Signed-off-by: Zhiyu Cheng <zhiyuc@nvidia.com> Signed-off-by: Shu Wang <shuw@nvidia.com> Signed-off-by: Po-Han Huang <pohanh@nvidia.com> Signed-off-by: Shu Wang. <shuw@nvidia.com> Signed-off-by: XIn Li <xinli@nvidia.com> Signed-off-by: Junhao Li <junhao@ubicloud.com> Signed-off-by: chaunceyjiang <chaunceyjiang@gmail.com> Signed-off-by: iAmir97 <Amir.balwel@embeddedllm.com> Signed-off-by: iAmir97 <71513472+iAmir97@users.noreply.github.com> Signed-off-by: <zyy1102000@gmail.com> Signed-off-by: Guy Stone <guys@spotify.com> Signed-off-by: <yyweiss@gmail.com> Signed-off-by: yyw <yyweiss@gmail.com> Signed-off-by: Russell Bryant <rbryant@redhat.com> Signed-off-by: Pradyun Ramadorai <pradyunr@amazon.com> Signed-off-by: Pradyun92 <142861237+Pradyun92@users.noreply.github.com> Signed-off-by: Jinzhen Lin <jinzhen.ljz@antgroup.com> Co-authored-by: rongfu.leng <rongfu.leng@daocloud.io> Co-authored-by: Huzaifa Sidhpurwala <huzaifas@redhat.com> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: Russell Bryant <rbryant@redhat.com> Co-authored-by: Varun Sundar Rabindranath <varunsundar08@gmail.com> Co-authored-by: Varun Sundar Rabindranath <vsundarr@redhat.com> Co-authored-by: Harry Mellor <19981378+hmellor@users.noreply.github.com> Co-authored-by: Jee Jee Li <pandaleefree@gmail.com> Co-authored-by: Michael Goin <mgoin64@gmail.com> Co-authored-by: Animesh Jain <jainanimesh2305@yahoo.com> Co-authored-by: Rui Qiao <161574667+ruisearch42@users.noreply.github.com> Co-authored-by: XiongfeiWei <isaacwxf23@gmail.com> Co-authored-by: Nick Hill <nhill@redhat.com> Co-authored-by: Wentao Ye <44945378+yewentao256@users.noreply.github.com> Co-authored-by: JartX <sagformas@gmail.com> Co-authored-by: fhl2000 <63384265+fhl2000@users.noreply.github.com> Co-authored-by: vllmellm <vllm.ellm@embeddedllm.com> Co-authored-by: kf <kuanfu.liu@embeddedllm.com> Co-authored-by: Nicolò Lucchesi <nlucches@redhat.com> Co-authored-by: Dipika Sikka <dipikasikka1@gmail.com> Co-authored-by: Sage Moore <sage@neuralmagic.com> Co-authored-by: tjtanaavllm <tunjian.tan@amd.com> Co-authored-by: Yong Hoon Shin <48474650+sarckk@users.noreply.github.com> Co-authored-by: Chih-Chieh Yang <7364402+cyang49@users.noreply.github.com> Co-authored-by: Roger Wang <hey@rogerw.me> Co-authored-by: Vadim Gimpelson <156319763+vadiklyutiy@users.noreply.github.com> Co-authored-by: Yuxuan Zhang <2448370773@qq.com> Co-authored-by: Isotr0py <2037008807@qq.com> Co-authored-by: Cyrus Leung <tlleungac@connect.ust.hk> Co-authored-by: Thomas Parnell <tpa@zurich.ibm.com> Co-authored-by: Yan Ma <yan.ma@intel.com> Co-authored-by: Xiao <xiszishu@gmail.com> Co-authored-by: jiahanc <173873397+jiahanc@users.noreply.github.com> Co-authored-by: Isotr0py <mozf@mail2.sysu.edu.cn> Co-authored-by: Ye (Charlotte) Qi <yeq@meta.com> Co-authored-by: Roberto L. Castro <38211239+LopezCastroRoberto@users.noreply.github.com> Co-authored-by: Ning Xie <andy.xning@gmail.com> Co-authored-by: H <linhaibin.eric@gmail.com> Co-authored-by: David Ben-David <sdavidbd@gmail.com> Co-authored-by: David Ben-David <davidb@pliops.com> Co-authored-by: Woosuk Kwon <woosuk.kwon@berkeley.edu> Co-authored-by: Li, Jiang <jiang1.li@intel.com> Co-authored-by: TankNee <nee@tanknee.cn> Co-authored-by: Cyrus Leung <cyrus.tl.leung@gmail.com> Co-authored-by: Seiji Eicher <58963096+eicherseiji@users.noreply.github.com> Co-authored-by: ZiTian.Zhao <zitian.zhao@tencentmusic.com> Co-authored-by: 22quinn <33176974+22quinn@users.noreply.github.com> Co-authored-by: Abirdcfly <fp544037857@gmail.com> Co-authored-by: Giancarlo Delfin <32987265+TheEpicDolphin@users.noreply.github.com> Co-authored-by: Chenxi Yang <cxyang@cs.utexas.edu> Co-authored-by: Chenxi Yang <cxyang@meta.com> Co-authored-by: Tyler Michael Smith <tyler@neuralmagic.com> Co-authored-by: Weixiao Huang <hwx.simle@gmail.com> Co-authored-by: Raghav Ravishankar <113712354+alyosha-swamy@users.noreply.github.com> Co-authored-by: ericehanley <ericehanley@google.com> Co-authored-by: Zhonghua Deng <abzhonghua@gmail.com> Co-authored-by: Po-Han Huang (NVIDIA) <53919306+nvpohanh@users.noreply.github.com> Co-authored-by: PiteXChen <44110731+CLFutureX@users.noreply.github.com> Co-authored-by: lkchen <github@lkchen.net> Co-authored-by: TJian <tunjian.tan@embeddedllm.com> Co-authored-by: Gregory Shtrasberg <156009573+gshtras@users.noreply.github.com> Co-authored-by: tlipoca9 <160737620+tlipoca9@users.noreply.github.com> Co-authored-by: elvischenv <219235043+elvischenv@users.noreply.github.com> Co-authored-by: wang.yuqi <noooop@126.com> Co-authored-by: Benji Beck <benjibeck@meta.com> Co-authored-by: youkaichao <youkaichao@gmail.com> Co-authored-by: Siyuan Liu <lsiyuan@google.com> Co-authored-by: Benjamin Chislett <chislett.ben@gmail.com> Co-authored-by: LiuXiaoxuanPKU <lilyliupku@gmail.com> Co-authored-by: simon-mo <xmo@berkeley.edu> Co-authored-by: Chen Zhang <zhangch99@outlook.com> Co-authored-by: Hongxia Yang <62075498+hongxiayang@users.noreply.github.com> Co-authored-by: Minseok Lee <47620120+minseokl@users.noreply.github.com> Co-authored-by: Yongye Zhu <zyy1102000@gmail.com> Co-authored-by: Lucas Wilkinson <LucasWilkinson@users.noreply.github.com> Co-authored-by: Zhang Jason <ning.zhang2@amd.com> Co-authored-by: Asaf Joseph Gardin <39553475+Josephasafg@users.noreply.github.com> Co-authored-by: asafg <asafg@ai21.com> Co-authored-by: Lain <siyuanf@nvidia.com> Co-authored-by: tc-mb <157115220+tc-mb@users.noreply.github.com> Co-authored-by: imning3 <hbning@pku.edu.cn> Co-authored-by: Maximilien de Bayser <mbayser@br.ibm.com> Co-authored-by: Kunshang Ji <kunshang.ji@intel.com> Co-authored-by: Tao He <linzhu.ht@alibaba-inc.com> Co-authored-by: qscqesze <qingjun@minimaxi.com> Co-authored-by: Syed Muhammad Bin Asif <92625830+syedmba@users.noreply.github.com> Co-authored-by: Lionel Villard <villard@us.ibm.com> Co-authored-by: WeiQing Chen <40507679+david6666666@users.noreply.github.com> Co-authored-by: ycyaw66 <497410282@qq.com> Co-authored-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> Co-authored-by: Ming Yang <minos.future@gmail.com> Co-authored-by: Adrián García García <adrigarvk8@gmail.com> Co-authored-by: Michael Goin <mgoin@redhat.com> Co-authored-by: JaceyShao <65159281+JaceyShao@users.noreply.github.com> Co-authored-by: shaojunqi <shaojunqi.sjq@alibaba-inc.com> Co-authored-by: Ricardo Decal <crypdick@users.noreply.github.com> Co-authored-by: Andrew Chan <andrewkchan.akc@gmail.com> Co-authored-by: fxmarty-amd <felmarty@amd.com> Co-authored-by: Andrew Sansom <andrew@protopia.ai> Co-authored-by: Zhiyu <zhiyuc@nvidia.com> Co-authored-by: Shu Wang <shuw@nvidia.com> Co-authored-by: XIn Li <xinli@nvidia.com> Co-authored-by: Junhao Li <streaver91@gmail.com> Co-authored-by: Chauncey <chaunceyjiang@gmail.com> Co-authored-by: iAmir97 <71513472+iAmir97@users.noreply.github.com> Co-authored-by: iAmir97 <Amir.balwel@embeddedllm.com> Co-authored-by: Hong Hanh <hanh.usth@gmail.com> Co-authored-by: Daniel Serebrenik <74646983+pliops-daniels@users.noreply.github.com> Co-authored-by: yewentao256 <zhyanwentao@126.com> Co-authored-by: Guy Stone <guys@spotify.com> Co-authored-by: yyweiss <70619747+yyweiss@users.noreply.github.com> Co-authored-by: Pradyun92 <142861237+Pradyun92@users.noreply.github.com> Co-authored-by: Pradyun Ramadorai <pradyunr@amazon.com> Co-authored-by: Nicolò Lucchesi <nicolo.lucchesi@gmail.com>
350 lines
12 KiB
Python
350 lines
12 KiB
Python
# SPDX-License-Identifier: Apache-2.0
|
|
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
|
|
|
import functools
|
|
import struct
|
|
from dataclasses import dataclass
|
|
from enum import Enum
|
|
from typing import Optional, Union
|
|
|
|
_SCALAR_TYPES_ID_MAP = {}
|
|
|
|
|
|
# Mirrors enum in `core/scalar_type.hpp`
|
|
class NanRepr(Enum):
|
|
NONE = 0 # nans are not supported
|
|
IEEE_754 = 1 # nans are: Exp all 1s, mantissa not all 0s
|
|
EXTD_RANGE_MAX_MIN = 2 # nans are: Exp all 1s, mantissa all 1s
|
|
|
|
|
|
# This ScalarType class is a parallel implementation of the C++ ScalarType
|
|
# class found in csrc/core/scalar_type.hpp. These two classes should be kept
|
|
# in sync until the inductor fully supports custom C++ classes.
|
|
@dataclass(frozen=True)
|
|
class ScalarType:
|
|
"""
|
|
ScalarType can represent a wide range of floating point and integer
|
|
types, in particular it can be used to represent sub-byte data types
|
|
(something that torch.dtype currently does not support). It is also
|
|
capable of representing types with a bias, i.e.:
|
|
`stored_value = value + bias`,
|
|
this is useful for quantized types (e.g. standard GPTQ 4bit uses a bias
|
|
of 8). The implementation for this class can be found in
|
|
csrc/core/scalar_type.hpp, these type signatures should be kept in sync
|
|
with that file.
|
|
"""
|
|
|
|
exponent: int
|
|
"""
|
|
Number of bits in the exponent if this is a floating point type
|
|
(zero if this an integer type)
|
|
"""
|
|
|
|
mantissa: int
|
|
"""
|
|
Number of bits in the mantissa if this is a floating point type,
|
|
or the number bits representing an integer excluding the sign bit if
|
|
this an integer type.
|
|
"""
|
|
|
|
signed: bool
|
|
"If the type is signed (i.e. has a sign bit)"
|
|
|
|
bias: int
|
|
"""
|
|
bias used to encode the values in this scalar type
|
|
(value = stored_value - bias, default 0) for example if we store the
|
|
type as an unsigned integer with a bias of 128 then the value 0 will be
|
|
stored as 128 and -1 will be stored as 127 and 1 will be stored as 129.
|
|
"""
|
|
|
|
_finite_values_only: bool = False
|
|
"""
|
|
Private: if infs are supported, used `has_infs()` instead.
|
|
"""
|
|
|
|
nan_repr: NanRepr = NanRepr.IEEE_754
|
|
"""
|
|
How NaNs are represent in this scalar type, returns NanRepr value.
|
|
(not applicable for integer types)
|
|
"""
|
|
|
|
def _floating_point_max_int(self) -> int:
|
|
assert (
|
|
self.mantissa <= 52 and self.exponent <= 11
|
|
), f"Cannot represent max/min as a double for type {self.__str__()}"
|
|
|
|
max_mantissa = (1 << self.mantissa) - 1
|
|
if self.nan_repr == NanRepr.EXTD_RANGE_MAX_MIN:
|
|
max_mantissa = max_mantissa - 1
|
|
|
|
max_exponent = (1 << self.exponent) - 2
|
|
if (self.nan_repr == NanRepr.EXTD_RANGE_MAX_MIN
|
|
or self.nan_repr == NanRepr.NONE):
|
|
assert (
|
|
self.exponent < 11
|
|
), f"Cannot represent max/min as a double for type {self.__str__()}"
|
|
max_exponent = max_exponent + 1
|
|
|
|
# adjust the exponent to match that of a double
|
|
# for now we assume the exponent bias is the standard 2^(e-1) -1, (where
|
|
# e is the exponent bits), there is some precedent for non-standard
|
|
# biases, example `float8_e4m3b11fnuz` here:
|
|
# https://github.com/jax-ml/ml_dtypes but to avoid premature over
|
|
# complication we are just assuming the standard exponent bias until
|
|
# there is a need to support non-standard biases
|
|
exponent_bias = (1 << (self.exponent - 1)) - 1
|
|
exponent_bias_double = (1 << 10) - 1 # double e = 11
|
|
|
|
max_exponent_double = (max_exponent - exponent_bias +
|
|
exponent_bias_double)
|
|
|
|
# shift the mantissa and exponent into the proper positions for an
|
|
# IEEE double and bitwise-or them together.
|
|
return (max_mantissa <<
|
|
(52 - self.mantissa)) | (max_exponent_double << 52)
|
|
|
|
def _floating_point_max(self) -> float:
|
|
double_raw = self._floating_point_max_int()
|
|
return struct.unpack('!d', struct.pack('!Q', double_raw))[0]
|
|
|
|
def _raw_max(self) -> Union[int, float]:
|
|
if self.is_floating_point():
|
|
return self._floating_point_max()
|
|
else:
|
|
assert (self.size_bits < 64 or self.size_bits == 64
|
|
and self.is_signed()), "Cannot represent max as an int"
|
|
return (1 << self.mantissa) - 1
|
|
|
|
def _raw_min(self) -> Union[int, float]:
|
|
if self.is_floating_point():
|
|
assert self.is_signed(
|
|
), "We currently assume all floating point types are signed"
|
|
sign_bit_double = 1 << 63
|
|
|
|
max_raw = self._floating_point_max_int()
|
|
min_raw = max_raw | sign_bit_double
|
|
return struct.unpack('!d', struct.pack('!Q', min_raw))[0]
|
|
else:
|
|
assert (not self.is_signed() or self.size_bits
|
|
<= 64), "Cannot represent min as a int64_t"
|
|
|
|
if self.is_signed():
|
|
return -(1 << (self.size_bits - 1))
|
|
else:
|
|
return 0
|
|
|
|
@functools.cached_property
|
|
def id(self) -> int:
|
|
"""
|
|
Convert the ScalarType to an int which can be passed to pytorch custom
|
|
ops. This layout of the int must be kept in sync with the C++
|
|
ScalarType's from_id method.
|
|
"""
|
|
val = 0
|
|
offset = 0
|
|
|
|
def or_and_advance(member, bit_width):
|
|
nonlocal val
|
|
nonlocal offset
|
|
bit_mask = (1 << bit_width) - 1
|
|
val = val | (int(member) & bit_mask) << offset
|
|
offset = offset + bit_width
|
|
|
|
or_and_advance(self.exponent, 8)
|
|
or_and_advance(self.mantissa, 8)
|
|
or_and_advance(self.signed, 1)
|
|
or_and_advance(self.bias, 32)
|
|
or_and_advance(self._finite_values_only, 1)
|
|
or_and_advance(self.nan_repr.value, 8)
|
|
|
|
assert offset <= 64, \
|
|
f"ScalarType fields too big {offset} to fit into an int64"
|
|
|
|
_SCALAR_TYPES_ID_MAP[val] = self
|
|
|
|
return val
|
|
|
|
@property
|
|
def size_bits(self) -> int:
|
|
return self.exponent + self.mantissa + int(self.signed)
|
|
|
|
def min(self) -> Union[int, float]:
|
|
"""
|
|
Min representable value for this scalar type.
|
|
(accounting for bias if there is one)
|
|
"""
|
|
return self._raw_min() - self.bias
|
|
|
|
def max(self) -> Union[int, float]:
|
|
"""
|
|
Max representable value for this scalar type.
|
|
(accounting for bias if there is one)
|
|
"""
|
|
return self._raw_max() - self.bias
|
|
|
|
def is_signed(self) -> bool:
|
|
"""
|
|
If the type is signed (i.e. has a sign bit), same as `signed`
|
|
added for consistency with:
|
|
https://pytorch.org/docs/stable/generated/torch.Tensor.is_signed.html
|
|
"""
|
|
return self.signed
|
|
|
|
def is_floating_point(self) -> bool:
|
|
"If the type is a floating point type"
|
|
return self.exponent != 0
|
|
|
|
def is_integer(self) -> bool:
|
|
"If the type is an integer type"
|
|
return self.exponent == 0
|
|
|
|
def has_bias(self) -> bool:
|
|
"If the type has a non-zero bias"
|
|
return self.bias != 0
|
|
|
|
def has_infs(self) -> bool:
|
|
"If the type is floating point and supports infinity"
|
|
return not self._finite_values_only
|
|
|
|
def has_nans(self) -> bool:
|
|
return self.nan_repr != NanRepr.NONE.value
|
|
|
|
def is_ieee_754(self) -> bool:
|
|
"""
|
|
If the type is a floating point type that follows IEEE 754
|
|
conventions
|
|
"""
|
|
return self.nan_repr == NanRepr.IEEE_754.value and \
|
|
not self._finite_values_only
|
|
|
|
def __str__(self) -> str:
|
|
"""
|
|
naming generally follows: https://github.com/jax-ml/ml_dtypes
|
|
for floating point types (leading f) the scheme is:
|
|
`float<size_bits>_e<exponent_bits>m<mantissa_bits>[flags]`
|
|
flags:
|
|
- no-flags: means it follows IEEE 754 conventions
|
|
- f: means finite values only (no infinities)
|
|
- n: means nans are supported (non-standard encoding)
|
|
for integer types the scheme is:
|
|
`[u]int<size_bits>[b<bias>]`
|
|
- if bias is not present it means its zero
|
|
"""
|
|
if self.is_floating_point():
|
|
ret = "float" + str(self.size_bits) + "_e" + str(
|
|
self.exponent) + "m" + str(self.mantissa)
|
|
|
|
if not self.is_ieee_754():
|
|
if self._finite_values_only:
|
|
ret = ret + "f"
|
|
if self.nan_repr != NanRepr.NONE:
|
|
ret = ret + "n"
|
|
|
|
return ret
|
|
else:
|
|
ret = ("int" if self.is_signed() else "uint") + str(self.size_bits)
|
|
if self.has_bias():
|
|
ret = ret + "b" + str(self.bias)
|
|
return ret
|
|
|
|
def __repr__(self) -> str:
|
|
return "ScalarType." + self.__str__()
|
|
|
|
# __len__ needs to be defined (and has to throw TypeError) for pytorch's
|
|
# opcheck to work.
|
|
def __len__(self) -> int:
|
|
raise TypeError
|
|
|
|
#
|
|
# Convenience Constructors
|
|
#
|
|
|
|
@classmethod
|
|
def int_(cls, size_bits: int, bias: Optional[int]) -> 'ScalarType':
|
|
"Create a signed integer scalar type (size_bits includes sign-bit)."
|
|
ret = cls(0, size_bits - 1, True, bias if bias else 0)
|
|
ret.id # noqa B018: make sure the id is cached
|
|
return ret
|
|
|
|
@classmethod
|
|
def uint(cls, size_bits: int, bias: Optional[int]) -> 'ScalarType':
|
|
"""Create a unsigned integer scalar type."""
|
|
ret = cls(0, size_bits, False, bias if bias else 0)
|
|
ret.id # noqa B018: make sure the id is cached
|
|
return ret
|
|
|
|
@classmethod
|
|
def float_IEEE754(cls, exponent: int, mantissa: int) -> 'ScalarType':
|
|
"""
|
|
Create a standard floating point type
|
|
(i.e. follows IEEE 754 conventions).
|
|
"""
|
|
assert (mantissa > 0 and exponent > 0)
|
|
ret = cls(exponent, mantissa, True, 0)
|
|
ret.id # noqa B018: make sure the id is cached
|
|
return ret
|
|
|
|
@classmethod
|
|
def float_(cls, exponent: int, mantissa: int, finite_values_only: bool,
|
|
nan_repr: NanRepr) -> 'ScalarType':
|
|
"""
|
|
Create a non-standard floating point type
|
|
(i.e. does not follow IEEE 754 conventions).
|
|
"""
|
|
assert (mantissa > 0 and exponent > 0)
|
|
assert (nan_repr != NanRepr.IEEE_754), (
|
|
"use `float_IEEE754` constructor for floating point types that "
|
|
"follow IEEE 754 conventions")
|
|
ret = cls(exponent, mantissa, True, 0, finite_values_only, nan_repr)
|
|
ret.id # noqa B018: make sure the id is cached
|
|
return ret
|
|
|
|
@classmethod
|
|
def from_id(cls, scalar_type_id: int):
|
|
if scalar_type_id not in _SCALAR_TYPES_ID_MAP:
|
|
raise ValueError(
|
|
f"scalar_type_id {scalar_type_id} doesn't exists.")
|
|
return _SCALAR_TYPES_ID_MAP[scalar_type_id]
|
|
|
|
|
|
# naming generally follows: https://github.com/jax-ml/ml_dtypes
|
|
# for floating point types (leading f) the scheme is:
|
|
# `float<size_bits>_e<exponent_bits>m<mantissa_bits>[flags]`
|
|
# flags:
|
|
# - no-flags: means it follows IEEE 754 conventions
|
|
# - f: means finite values only (no infinities)
|
|
# - n: means nans are supported (non-standard encoding)
|
|
# for integer types the scheme is:
|
|
# `[u]int<size_bits>[b<bias>]`
|
|
# - if bias is not present it means its zero
|
|
|
|
|
|
class scalar_types:
|
|
int4 = ScalarType.int_(4, None)
|
|
uint4 = ScalarType.uint(4, None)
|
|
int8 = ScalarType.int_(8, None)
|
|
uint8 = ScalarType.uint(8, None)
|
|
float8_e4m3fn = ScalarType.float_(4, 3, True, NanRepr.EXTD_RANGE_MAX_MIN)
|
|
float8_e5m2 = ScalarType.float_IEEE754(5, 2)
|
|
float8_e8m0fnu = ScalarType(8, 0, False, 0, True,
|
|
NanRepr.EXTD_RANGE_MAX_MIN)
|
|
float16_e8m7 = ScalarType.float_IEEE754(8, 7)
|
|
float16_e5m10 = ScalarType.float_IEEE754(5, 10)
|
|
|
|
# fp6, https://github.com/usyd-fsalab/fp6_llm/tree/main
|
|
float6_e3m2f = ScalarType.float_(3, 2, True, NanRepr.NONE)
|
|
|
|
# fp4, https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf
|
|
float4_e2m1f = ScalarType.float_(2, 1, True, NanRepr.NONE)
|
|
|
|
# "gptq" types
|
|
uint2b2 = ScalarType.uint(2, 2)
|
|
uint3b4 = ScalarType.uint(3, 4)
|
|
uint4b8 = ScalarType.uint(4, 8)
|
|
uint8b128 = ScalarType.uint(8, 128)
|
|
|
|
# colloquial names
|
|
bfloat16 = float16_e8m7
|
|
float16 = float16_e5m10
|