FlagGems is a high-performance general operator library implemented in OpenAI Triton. It aims to provide a suite of kernel functions to accelerate LLM training and inference.
By registering with the ATen backend of PyTorch, FlagGems facilitates a seamless transition, allowing users to switch to the Triton function library without the need to modify their model code. Users can still utilize the ATen backend as usual while experiencing significant performance enhancement. The Triton language offers benefits in readability, user-friendliness and performance comparable to CUDA. This convenience allows developers to engage in the development of FlagGems with minimal learning investment.
In FlagGems, we provide automatic code generation that developers can use to conveniently generate pointwise single operators and pointwise fused operators. Automatic code generation can handle various needs such as normal pointwise computations, non-tensor arguments, and specifying output data types.
Decorating the pointwise operator function with pointwise_dynamic
can save the manual handling of tensor addressing, tensor read/write, parallel tiling, tensor broadcasting, dynamic dimensions, non-contiguous storage, etc. For example, in the following code, developers only need to describe the computational logic to generate flexible and efficient Triton code.
@pointwise_dynamic(promotion_methods=[(0, "COMPLEX_TO_FLOAT")])
@triton.jit
def abs_func(x):
return tl.abs(x)
By default, pointwise_dynamic
treats all parameters as tensors, and by passing a list of boolean values to the parameter is_tensor
, developers can specify which parameters are tensors and which are not. Additionally, developers can pass in dtypes
to indicate the data types of non-tensor parameters, but this is not required. For example, in the following code, the alpha
parameter is defined as a non-tensor floating point number, while the x
and y
parameters are defined as tensors.
@pointwise_dynamic(
is_tensor=[True, True, False],
dtypes=[None, None, float],
promotion_methods=[(0,"DEFAULT")]
)
@triton.jit
def add_func(x, y, alpha):
return x + y * alpha
Furthermore, developers MUST provide promotion_methods to specify how type promotion should be handled for the operation to achieve the correct output type during computation.
@pointwise_dynamic(output_dtypes=[torch.bool])
@triton.jit
def ge(x, y):
return x > y
In promotion_methods
, an int
is used to indicate the position of the parameter requiring type promotion, while a str
denotes the method of type promotion. The str
corresponds to the following enumerated types:
class ELEMENTWISE_TYPE_PROMOTION_KIND(Enum):
DEFAULT = (0,)
NO_OPMATH = (1,)
INT_TO_FLOAT = (2,)
ALWAYS_BOOL = (3,)
COMPLEX_TO_FLOAT = (4,)
BOOL_TO_LONG = (5,)
Examples:
DEFAULT
:addNO_OPMATH
: where, nextafter, catINT_TO_FLOAT
:sinALWAYS_BOOL
:eqCOMPLEX_TO_FLOAT
:absBOOL_TO_LONG
:powgit clone https://github.com/FlagOpen/FlagGems.git
cd FlagGems
pip install .
Enable permanently
import flag_gems
flag_gems.enable()
Enable temporarily
import flag_gems
with flag_gems.use_gems():
pass
Example
import torch
import flag_gems
M, N, K = 1024, 1024, 1024
A = torch.randn((M, K), dtype=torch.float16, device="cuda")
B = torch.randn((K, N), dtype=torch.float16, device="cuda")
with flag_gems.use_gems():
C = torch.mm(A, B)
Test Operator Accuracy
cd tests
pytest test_xx_ops.py
cd tests
pytest test_xx_ops.py --ref cpu
Test Model Accuracy
cd examples
pytest model_xx_test.py
Test Operator Performance
cd benchmark
pytest test_xx_perf.py -s
cd benchmark
pytest test_xx_perf.py -s --mode cpu
Run tests with logging infomation
pytest program.py --log-cli-level debug
Not recommended in performance testing.
Operators will be implemented according to OperatorList.md.
Platform | float16 | float32 | bfloat16 |
---|---|---|---|
Nvidia A100 | ✓ | ✓ | ✓ |
The following chart shows the speedup of FlagGems compared with PyTorch ATen library in eager mode. The speedup is calculated by averaging the speedup on each shape, representing the overall performance of the operator.
If you are interested in contributing to the FlagGems project, please refer to CONTRIBUTING.md. Any contributions would be highly appreciated.
If you have any questions about our project, please submit an issue, or contact us through flaggems@baai.ac.cn.
The FlagGems project is based on Apache 2.0.