Compare commits
67 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2e0b6e7757 | ||
|
|
941767127c | ||
|
|
74d8d77626 | ||
|
|
fd4ea8ef5c | ||
|
|
1066cbd152 | ||
|
|
6ef00b03a2 | ||
|
|
9140561059 | ||
|
|
77af974b40 | ||
|
|
4934d49274 | ||
|
|
358c328d69 | ||
|
|
4aaafdd289 | ||
|
|
66b108d142 | ||
|
|
e0ff920001 | ||
|
|
face83c7ec | ||
|
|
1db83e31a2 | ||
|
|
a1b9cb2a34 | ||
|
|
3a4fd5ca59 | ||
|
|
c17daa9f89 | ||
|
|
bd29cf3d3a | ||
|
|
31bff69151 | ||
|
|
ba4f826738 | ||
|
|
de60a3fb93 | ||
|
|
21d5daa4ac | ||
|
|
290e015c6c | ||
|
|
1b7c791d60 | ||
|
|
bbe4466fd9 | ||
|
|
08133c4d1a | ||
|
|
76a7983b23 | ||
|
|
8041b7305e | ||
|
|
3ec8c25cd0 | ||
|
|
671af2b1c0 | ||
|
|
6f41f0e377 | ||
|
|
2c9b638065 | ||
|
|
a7347d9a6d | ||
|
|
f8c688d746 | ||
|
|
c9fadda543 | ||
|
|
30fb0956df | ||
|
|
3a765bd5e1 | ||
|
|
26c52a5ea6 | ||
|
|
c3372e87be | ||
|
|
b0a1d667b0 | ||
|
|
e1d5402238 | ||
|
|
3d1cfbfc74 | ||
|
|
37ca558103 | ||
|
|
eed74a558f | ||
|
|
2acd76f346 | ||
|
|
b81a6a6bb3 | ||
|
|
0fbfc4b81b | ||
|
|
c06170cc8e | ||
|
|
614856da25 | ||
|
|
05bdf4eaf3 | ||
|
|
6774bd50b0 | ||
|
|
31c1f3255e | ||
|
|
21d93c140d | ||
|
|
f1c8520146 | ||
|
|
096827c284 | ||
|
|
6565d9e33e | ||
|
|
f375ec8440 | ||
|
|
518369d78c | ||
|
|
30bad5c492 | ||
|
|
3fefe271ec | ||
|
|
6428f1d051 | ||
|
|
7e1b21daac | ||
|
|
cb3f30c600 | ||
|
|
f3e024bece | ||
|
|
31d2ab4aff | ||
|
|
eb17212858 |
2
.github/workflows/publish.yml
vendored
2
.github/workflows/publish.yml
vendored
@@ -49,7 +49,7 @@ jobs:
|
|||||||
matrix:
|
matrix:
|
||||||
os: ['ubuntu-20.04']
|
os: ['ubuntu-20.04']
|
||||||
python-version: ['3.8', '3.9', '3.10', '3.11']
|
python-version: ['3.8', '3.9', '3.10', '3.11']
|
||||||
pytorch-version: ['2.1.0']
|
pytorch-version: ['2.1.2'] # Must be the most recent version that meets requirements.txt.
|
||||||
cuda-version: ['11.8', '12.1']
|
cuda-version: ['11.8', '12.1']
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ ENTRYPOINT ["python3", "-m", "vllm.entrypoints.api_server"]
|
|||||||
FROM vllm-base AS vllm-openai
|
FROM vllm-base AS vllm-openai
|
||||||
# install additional dependencies for openai api server
|
# install additional dependencies for openai api server
|
||||||
RUN --mount=type=cache,target=/root/.cache/pip \
|
RUN --mount=type=cache,target=/root/.cache/pip \
|
||||||
pip install accelerate fschat
|
pip install accelerate
|
||||||
|
|
||||||
COPY --from=build /workspace/vllm/*.so /workspace/vllm/
|
COPY --from=build /workspace/vllm/*.so /workspace/vllm/
|
||||||
COPY vllm vllm
|
COPY vllm vllm
|
||||||
|
|||||||
@@ -47,12 +47,12 @@ RUN mkdir libs \
|
|||||||
COPY ./ /app/vllm
|
COPY ./ /app/vllm
|
||||||
|
|
||||||
RUN python3 -m pip install --upgrade pip
|
RUN python3 -m pip install --upgrade pip
|
||||||
RUN pip install xformers==0.0.22.post7 --no-deps
|
RUN pip install xformers==0.0.23 --no-deps
|
||||||
|
|
||||||
RUN cd /app \
|
RUN cd /app \
|
||||||
&& cd vllm \
|
&& cd vllm \
|
||||||
&& pip install -U -r requirements-rocm.txt \
|
&& pip install -U -r requirements-rocm.txt \
|
||||||
&& bash patch_xformers-0.0.22.post7.rocm.sh \
|
&& bash patch_xformers.rocm.sh \
|
||||||
&& python3 setup.py install \
|
&& python3 setup.py install \
|
||||||
&& cd ..
|
&& cd ..
|
||||||
|
|
||||||
|
|||||||
13
README.md
13
README.md
@@ -27,7 +27,7 @@ Easy, fast, and cheap LLM serving for everyone
|
|||||||
- [2023/06] We officially released vLLM! FastChat-vLLM integration has powered [LMSYS Vicuna and Chatbot Arena](https://chat.lmsys.org) since mid-April. Check out our [blog post](https://vllm.ai).
|
- [2023/06] We officially released vLLM! FastChat-vLLM integration has powered [LMSYS Vicuna and Chatbot Arena](https://chat.lmsys.org) since mid-April. Check out our [blog post](https://vllm.ai).
|
||||||
|
|
||||||
---
|
---
|
||||||
|
## About
|
||||||
vLLM is a fast and easy-to-use library for LLM inference and serving.
|
vLLM is a fast and easy-to-use library for LLM inference and serving.
|
||||||
|
|
||||||
vLLM is fast with:
|
vLLM is fast with:
|
||||||
@@ -35,6 +35,8 @@ vLLM is fast with:
|
|||||||
- State-of-the-art serving throughput
|
- State-of-the-art serving throughput
|
||||||
- Efficient management of attention key and value memory with **PagedAttention**
|
- Efficient management of attention key and value memory with **PagedAttention**
|
||||||
- Continuous batching of incoming requests
|
- Continuous batching of incoming requests
|
||||||
|
- Fast model execution with CUDA/HIP graph
|
||||||
|
- Quantization: [GPTQ](https://arxiv.org/abs/2210.17323), [AWQ](https://arxiv.org/abs/2306.00978), [SqueezeLLM](https://arxiv.org/abs/2306.07629)
|
||||||
- Optimized CUDA kernels
|
- Optimized CUDA kernels
|
||||||
|
|
||||||
vLLM is flexible and easy to use with:
|
vLLM is flexible and easy to use with:
|
||||||
@@ -44,7 +46,7 @@ vLLM is flexible and easy to use with:
|
|||||||
- Tensor parallelism support for distributed inference
|
- Tensor parallelism support for distributed inference
|
||||||
- Streaming outputs
|
- Streaming outputs
|
||||||
- OpenAI-compatible API server
|
- OpenAI-compatible API server
|
||||||
- Support NVIDIA CUDA and AMD ROCm.
|
- Support NVIDIA GPUs and AMD GPUs
|
||||||
|
|
||||||
vLLM seamlessly supports many Hugging Face models, including the following architectures:
|
vLLM seamlessly supports many Hugging Face models, including the following architectures:
|
||||||
|
|
||||||
@@ -52,6 +54,7 @@ vLLM seamlessly supports many Hugging Face models, including the following archi
|
|||||||
- Baichuan & Baichuan2 (`baichuan-inc/Baichuan2-13B-Chat`, `baichuan-inc/Baichuan-7B`, etc.)
|
- Baichuan & Baichuan2 (`baichuan-inc/Baichuan2-13B-Chat`, `baichuan-inc/Baichuan-7B`, etc.)
|
||||||
- BLOOM (`bigscience/bloom`, `bigscience/bloomz`, etc.)
|
- BLOOM (`bigscience/bloom`, `bigscience/bloomz`, etc.)
|
||||||
- ChatGLM (`THUDM/chatglm2-6b`, `THUDM/chatglm3-6b`, etc.)
|
- ChatGLM (`THUDM/chatglm2-6b`, `THUDM/chatglm3-6b`, etc.)
|
||||||
|
- DeciLM (`Deci/DeciLM-7B`, `Deci/DeciLM-7B-instruct`, etc.)
|
||||||
- Falcon (`tiiuae/falcon-7b`, `tiiuae/falcon-40b`, `tiiuae/falcon-rw-7b`, etc.)
|
- Falcon (`tiiuae/falcon-7b`, `tiiuae/falcon-40b`, `tiiuae/falcon-rw-7b`, etc.)
|
||||||
- GPT-2 (`gpt2`, `gpt2-xl`, etc.)
|
- GPT-2 (`gpt2`, `gpt2-xl`, etc.)
|
||||||
- GPT BigCode (`bigcode/starcoder`, `bigcode/gpt_bigcode-santacoder`, etc.)
|
- GPT BigCode (`bigcode/starcoder`, `bigcode/gpt_bigcode-santacoder`, etc.)
|
||||||
@@ -63,7 +66,7 @@ vLLM seamlessly supports many Hugging Face models, including the following archi
|
|||||||
- Mixtral (`mistralai/Mixtral-8x7B-v0.1`, `mistralai/Mixtral-8x7B-Instruct-v0.1`, etc.)
|
- Mixtral (`mistralai/Mixtral-8x7B-v0.1`, `mistralai/Mixtral-8x7B-Instruct-v0.1`, etc.)
|
||||||
- MPT (`mosaicml/mpt-7b`, `mosaicml/mpt-30b`, etc.)
|
- MPT (`mosaicml/mpt-7b`, `mosaicml/mpt-30b`, etc.)
|
||||||
- OPT (`facebook/opt-66b`, `facebook/opt-iml-max-30b`, etc.)
|
- OPT (`facebook/opt-66b`, `facebook/opt-iml-max-30b`, etc.)
|
||||||
- Phi-1.5 (`microsoft/phi-1_5`, etc.)
|
- Phi (`microsoft/phi-1_5`, `microsoft/phi-2`, etc.)
|
||||||
- Qwen (`Qwen/Qwen-7B`, `Qwen/Qwen-7B-Chat`, etc.)
|
- Qwen (`Qwen/Qwen-7B`, `Qwen/Qwen-7B-Chat`, etc.)
|
||||||
- Yi (`01-ai/Yi-6B`, `01-ai/Yi-34B`, etc.)
|
- Yi (`01-ai/Yi-6B`, `01-ai/Yi-34B`, etc.)
|
||||||
|
|
||||||
@@ -72,10 +75,6 @@ Install vLLM with pip or [from source](https://vllm.readthedocs.io/en/latest/get
|
|||||||
```bash
|
```bash
|
||||||
pip install vllm
|
pip install vllm
|
||||||
```
|
```
|
||||||
**NOTE:** The Mixtral model additionally requires `megablocks` which can be installed with pip or [from source](https://github.com/stanford-futuredata/megablocks) on **Python 3.10**:
|
|
||||||
```bash
|
|
||||||
pip install megablocks
|
|
||||||
```
|
|
||||||
|
|
||||||
## Getting Started
|
## Getting Started
|
||||||
|
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ def main(args: argparse.Namespace):
|
|||||||
tensor_parallel_size=args.tensor_parallel_size,
|
tensor_parallel_size=args.tensor_parallel_size,
|
||||||
trust_remote_code=args.trust_remote_code,
|
trust_remote_code=args.trust_remote_code,
|
||||||
dtype=args.dtype,
|
dtype=args.dtype,
|
||||||
|
enforce_eager=args.enforce_eager,
|
||||||
)
|
)
|
||||||
|
|
||||||
sampling_params = SamplingParams(
|
sampling_params = SamplingParams(
|
||||||
@@ -84,7 +85,7 @@ if __name__ == '__main__':
|
|||||||
parser.add_argument('--tokenizer', type=str, default=None)
|
parser.add_argument('--tokenizer', type=str, default=None)
|
||||||
parser.add_argument('--quantization',
|
parser.add_argument('--quantization',
|
||||||
'-q',
|
'-q',
|
||||||
choices=['awq', 'squeezellm', None],
|
choices=['awq', 'gptq', 'squeezellm', None],
|
||||||
default=None)
|
default=None)
|
||||||
parser.add_argument('--tensor-parallel-size', '-tp', type=int, default=1)
|
parser.add_argument('--tensor-parallel-size', '-tp', type=int, default=1)
|
||||||
parser.add_argument('--input-len', type=int, default=32)
|
parser.add_argument('--input-len', type=int, default=32)
|
||||||
@@ -111,6 +112,9 @@ if __name__ == '__main__':
|
|||||||
'The "auto" option will use FP16 precision '
|
'The "auto" option will use FP16 precision '
|
||||||
'for FP32 and FP16 models, and BF16 precision '
|
'for FP32 and FP16 models, and BF16 precision '
|
||||||
'for BF16 models.')
|
'for BF16 models.')
|
||||||
|
parser.add_argument('--enforce-eager',
|
||||||
|
action='store_true',
|
||||||
|
help='enforce eager mode and disable CUDA graph')
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--profile',
|
'--profile',
|
||||||
action='store_true',
|
action='store_true',
|
||||||
|
|||||||
@@ -69,7 +69,8 @@ def run_vllm(
|
|||||||
use_beam_search: bool,
|
use_beam_search: bool,
|
||||||
trust_remote_code: bool,
|
trust_remote_code: bool,
|
||||||
dtype: str,
|
dtype: str,
|
||||||
max_model_len: Optional[int] = None,
|
max_model_len: Optional[int],
|
||||||
|
enforce_eager: bool,
|
||||||
) -> float:
|
) -> float:
|
||||||
from vllm import LLM, SamplingParams
|
from vllm import LLM, SamplingParams
|
||||||
llm = LLM(
|
llm = LLM(
|
||||||
@@ -81,6 +82,7 @@ def run_vllm(
|
|||||||
trust_remote_code=trust_remote_code,
|
trust_remote_code=trust_remote_code,
|
||||||
dtype=dtype,
|
dtype=dtype,
|
||||||
max_model_len=max_model_len,
|
max_model_len=max_model_len,
|
||||||
|
enforce_eager=enforce_eager,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Add the requests to the engine.
|
# Add the requests to the engine.
|
||||||
@@ -204,7 +206,7 @@ def main(args: argparse.Namespace):
|
|||||||
args.quantization, args.tensor_parallel_size,
|
args.quantization, args.tensor_parallel_size,
|
||||||
args.seed, args.n, args.use_beam_search,
|
args.seed, args.n, args.use_beam_search,
|
||||||
args.trust_remote_code, args.dtype,
|
args.trust_remote_code, args.dtype,
|
||||||
args.max_model_len)
|
args.max_model_len, args.enforce_eager)
|
||||||
elif args.backend == "hf":
|
elif args.backend == "hf":
|
||||||
assert args.tensor_parallel_size == 1
|
assert args.tensor_parallel_size == 1
|
||||||
elapsed_time = run_hf(requests, args.model, tokenizer, args.n,
|
elapsed_time = run_hf(requests, args.model, tokenizer, args.n,
|
||||||
@@ -244,7 +246,7 @@ if __name__ == "__main__":
|
|||||||
parser.add_argument("--tokenizer", type=str, default=None)
|
parser.add_argument("--tokenizer", type=str, default=None)
|
||||||
parser.add_argument('--quantization',
|
parser.add_argument('--quantization',
|
||||||
'-q',
|
'-q',
|
||||||
choices=['awq', 'squeezellm', None],
|
choices=['awq', 'gptq', 'squeezellm', None],
|
||||||
default=None)
|
default=None)
|
||||||
parser.add_argument("--tensor-parallel-size", "-tp", type=int, default=1)
|
parser.add_argument("--tensor-parallel-size", "-tp", type=int, default=1)
|
||||||
parser.add_argument("--n",
|
parser.add_argument("--n",
|
||||||
@@ -279,6 +281,9 @@ if __name__ == "__main__":
|
|||||||
'The "auto" option will use FP16 precision '
|
'The "auto" option will use FP16 precision '
|
||||||
'for FP32 and FP16 models, and BF16 precision '
|
'for FP32 and FP16 models, and BF16 precision '
|
||||||
'for BF16 models.')
|
'for BF16 models.')
|
||||||
|
parser.add_argument("--enforce-eager",
|
||||||
|
action="store_true",
|
||||||
|
help="enforce eager execution")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
if args.tokenizer is None:
|
if args.tokenizer is None:
|
||||||
args.tokenizer = args.model
|
args.tokenizer = args.model
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#include <torch/extension.h>
|
|
||||||
#include <ATen/cuda/CUDAContext.h>
|
#include <ATen/cuda/CUDAContext.h>
|
||||||
|
#include <torch/extension.h>
|
||||||
|
#include <c10/cuda/CUDAGuard.h>
|
||||||
|
|
||||||
#include "cuda_compat.h"
|
#include "cuda_compat.h"
|
||||||
#include "dispatch_utils.h"
|
#include "dispatch_utils.h"
|
||||||
@@ -36,6 +37,7 @@ void silu_and_mul(
|
|||||||
|
|
||||||
dim3 grid(num_tokens);
|
dim3 grid(num_tokens);
|
||||||
dim3 block(std::min(d, 1024));
|
dim3 block(std::min(d, 1024));
|
||||||
|
const at::cuda::OptionalCUDAGuard device_guard(device_of(input));
|
||||||
const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
|
const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
|
||||||
VLLM_DISPATCH_FLOATING_TYPES(
|
VLLM_DISPATCH_FLOATING_TYPES(
|
||||||
input.scalar_type(),
|
input.scalar_type(),
|
||||||
@@ -71,6 +73,7 @@ __global__ void activation_kernel(
|
|||||||
int64_t num_tokens = input.numel() / d; \
|
int64_t num_tokens = input.numel() / d; \
|
||||||
dim3 grid(num_tokens); \
|
dim3 grid(num_tokens); \
|
||||||
dim3 block(std::min(d, 1024)); \
|
dim3 block(std::min(d, 1024)); \
|
||||||
|
const at::cuda::OptionalCUDAGuard device_guard(device_of(input)); \
|
||||||
const cudaStream_t stream = at::cuda::getCurrentCUDAStream(); \
|
const cudaStream_t stream = at::cuda::getCurrentCUDAStream(); \
|
||||||
VLLM_DISPATCH_FLOATING_TYPES( \
|
VLLM_DISPATCH_FLOATING_TYPES( \
|
||||||
input.scalar_type(), \
|
input.scalar_type(), \
|
||||||
|
|||||||
@@ -21,6 +21,7 @@
|
|||||||
|
|
||||||
#include <torch/extension.h>
|
#include <torch/extension.h>
|
||||||
#include <ATen/cuda/CUDAContext.h>
|
#include <ATen/cuda/CUDAContext.h>
|
||||||
|
#include <c10/cuda/CUDAGuard.h>
|
||||||
|
|
||||||
#include "attention_dtypes.h"
|
#include "attention_dtypes.h"
|
||||||
#include "attention_utils.cuh"
|
#include "attention_utils.cuh"
|
||||||
@@ -616,6 +617,7 @@ void paged_attention_v1_launcher(
|
|||||||
|
|
||||||
dim3 grid(num_heads, num_seqs, 1);
|
dim3 grid(num_heads, num_seqs, 1);
|
||||||
dim3 block(NUM_THREADS);
|
dim3 block(NUM_THREADS);
|
||||||
|
const at::cuda::OptionalCUDAGuard device_guard(device_of(query));
|
||||||
const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
|
const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
|
||||||
switch (head_size) {
|
switch (head_size) {
|
||||||
// NOTE(woosuk): To reduce the compilation time, we only compile for the
|
// NOTE(woosuk): To reduce the compilation time, we only compile for the
|
||||||
@@ -784,6 +786,7 @@ void paged_attention_v2_launcher(
|
|||||||
int reduce_shared_mem_size = 2 * max_num_partitions * sizeof(float);
|
int reduce_shared_mem_size = 2 * max_num_partitions * sizeof(float);
|
||||||
|
|
||||||
dim3 block(NUM_THREADS);
|
dim3 block(NUM_THREADS);
|
||||||
|
const at::cuda::OptionalCUDAGuard device_guard(device_of(query));
|
||||||
const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
|
const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
|
||||||
switch (head_size) {
|
switch (head_size) {
|
||||||
// NOTE(woosuk): To reduce the compilation time, we only compile for the
|
// NOTE(woosuk): To reduce the compilation time, we only compile for the
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
#include <torch/extension.h>
|
#include <torch/extension.h>
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#include <torch/extension.h>
|
#include <torch/extension.h>
|
||||||
#include <ATen/cuda/CUDAContext.h>
|
#include <ATen/cuda/CUDAContext.h>
|
||||||
|
#include <c10/cuda/CUDAGuard.h>
|
||||||
|
|
||||||
#include "cuda_compat.h"
|
#include "cuda_compat.h"
|
||||||
#include "dispatch_utils.h"
|
#include "dispatch_utils.h"
|
||||||
@@ -33,6 +34,7 @@ void swap_blocks(
|
|||||||
char *dst_ptr = static_cast<char*>(dst.data_ptr());
|
char *dst_ptr = static_cast<char*>(dst.data_ptr());
|
||||||
|
|
||||||
const int64_t block_size_in_bytes = src.element_size() * src[0].numel();
|
const int64_t block_size_in_bytes = src.element_size() * src[0].numel();
|
||||||
|
const at::cuda::OptionalCUDAGuard device_guard(src_device);
|
||||||
const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
|
const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
|
||||||
// NOTE(woosuk): This can be slow if the number of blocks is large.
|
// NOTE(woosuk): This can be slow if the number of blocks is large.
|
||||||
for (const auto& pair : block_mapping) {
|
for (const auto& pair : block_mapping) {
|
||||||
@@ -127,6 +129,7 @@ void copy_blocks(
|
|||||||
const int numel_per_block = key_caches[0][0].numel();
|
const int numel_per_block = key_caches[0][0].numel();
|
||||||
dim3 grid(num_layers, num_pairs);
|
dim3 grid(num_layers, num_pairs);
|
||||||
dim3 block(std::min(1024, numel_per_block));
|
dim3 block(std::min(1024, numel_per_block));
|
||||||
|
const at::cuda::OptionalCUDAGuard device_guard(cache_device);
|
||||||
const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
|
const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
|
||||||
VLLM_DISPATCH_FLOATING_TYPES(
|
VLLM_DISPATCH_FLOATING_TYPES(
|
||||||
key_caches[0].scalar_type(), "copy_blocks_kernel", ([&] {
|
key_caches[0].scalar_type(), "copy_blocks_kernel", ([&] {
|
||||||
@@ -207,6 +210,7 @@ void reshape_and_cache(
|
|||||||
|
|
||||||
dim3 grid(num_tokens);
|
dim3 grid(num_tokens);
|
||||||
dim3 block(std::min(num_heads * head_size, 512));
|
dim3 block(std::min(num_heads * head_size, 512));
|
||||||
|
const at::cuda::OptionalCUDAGuard device_guard(device_of(key));
|
||||||
const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
|
const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
|
||||||
VLLM_DISPATCH_FLOATING_TYPES(
|
VLLM_DISPATCH_FLOATING_TYPES(
|
||||||
key.scalar_type(),
|
key.scalar_type(),
|
||||||
@@ -367,6 +371,7 @@ void gather_cached_kv(
|
|||||||
|
|
||||||
dim3 grid(num_tokens);
|
dim3 grid(num_tokens);
|
||||||
dim3 block(std::min(num_heads * head_size, 512));
|
dim3 block(std::min(num_heads * head_size, 512));
|
||||||
|
const at::cuda::OptionalCUDAGuard device_guard(device_of(key));
|
||||||
const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
|
const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
|
||||||
VLLM_DISPATCH_FLOATING_TYPES(
|
VLLM_DISPATCH_FLOATING_TYPES(
|
||||||
key.scalar_type(),
|
key.scalar_type(),
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
#include <torch/extension.h>
|
#include <torch/extension.h>
|
||||||
|
|
||||||
int get_device_attribute(
|
int get_device_attribute(
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
* Adapted from
|
* Adapted from
|
||||||
* https://github.com/pytorch/pytorch/blob/v2.0.1/aten/src/ATen/Dispatch.h
|
* https://github.com/pytorch/pytorch/blob/v2.0.1/aten/src/ATen/Dispatch.h
|
||||||
*/
|
*/
|
||||||
|
#pragma once
|
||||||
|
|
||||||
#include <torch/extension.h>
|
#include <torch/extension.h>
|
||||||
|
|
||||||
#define VLLM_DISPATCH_CASE_FLOATING_TYPES(...) \
|
#define VLLM_DISPATCH_CASE_FLOATING_TYPES(...) \
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#include <torch/extension.h>
|
#include <torch/extension.h>
|
||||||
#include <ATen/cuda/CUDAContext.h>
|
#include <ATen/cuda/CUDAContext.h>
|
||||||
|
#include <c10/cuda/CUDAGuard.h>
|
||||||
|
|
||||||
#include "dispatch_utils.h"
|
#include "dispatch_utils.h"
|
||||||
#include "reduction_utils.cuh"
|
#include "reduction_utils.cuh"
|
||||||
@@ -76,6 +77,7 @@ void rms_norm(
|
|||||||
|
|
||||||
dim3 grid(num_tokens);
|
dim3 grid(num_tokens);
|
||||||
dim3 block(std::min(hidden_size, 1024));
|
dim3 block(std::min(hidden_size, 1024));
|
||||||
|
const at::cuda::OptionalCUDAGuard device_guard(device_of(input));
|
||||||
const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
|
const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
|
||||||
VLLM_DISPATCH_FLOATING_TYPES(
|
VLLM_DISPATCH_FLOATING_TYPES(
|
||||||
input.scalar_type(),
|
input.scalar_type(),
|
||||||
@@ -101,6 +103,7 @@ void fused_add_rms_norm(
|
|||||||
|
|
||||||
dim3 grid(num_tokens);
|
dim3 grid(num_tokens);
|
||||||
dim3 block(std::min(hidden_size, 1024));
|
dim3 block(std::min(hidden_size, 1024));
|
||||||
|
const at::cuda::OptionalCUDAGuard device_guard(device_of(input));
|
||||||
const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
|
const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
|
||||||
VLLM_DISPATCH_FLOATING_TYPES(
|
VLLM_DISPATCH_FLOATING_TYPES(
|
||||||
input.scalar_type(),
|
input.scalar_type(),
|
||||||
|
|||||||
14
csrc/ops.h
14
csrc/ops.h
@@ -1,3 +1,5 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
#include <torch/extension.h>
|
#include <torch/extension.h>
|
||||||
|
|
||||||
void paged_attention_v1(
|
void paged_attention_v1(
|
||||||
@@ -75,3 +77,15 @@ void squeezellm_gemm(
|
|||||||
torch::Tensor mat,
|
torch::Tensor mat,
|
||||||
torch::Tensor mul,
|
torch::Tensor mul,
|
||||||
torch::Tensor lookup_table);
|
torch::Tensor lookup_table);
|
||||||
|
|
||||||
|
torch::Tensor gptq_gemm(
|
||||||
|
torch::Tensor a,
|
||||||
|
torch::Tensor b_q_weight,
|
||||||
|
torch::Tensor b_gptq_qzeros,
|
||||||
|
torch::Tensor b_gptq_scales,
|
||||||
|
torch::Tensor b_g_idx,
|
||||||
|
bool use_exllama);
|
||||||
|
|
||||||
|
void gptq_shuffle(
|
||||||
|
torch::Tensor q_weight,
|
||||||
|
torch::Tensor q_perm);
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#include <torch/extension.h>
|
#include <torch/extension.h>
|
||||||
#include <ATen/cuda/CUDAContext.h>
|
#include <ATen/cuda/CUDAContext.h>
|
||||||
|
#include <c10/cuda/CUDAGuard.h>
|
||||||
|
|
||||||
#include "cuda_compat.h"
|
#include "cuda_compat.h"
|
||||||
#include "dispatch_utils.h"
|
#include "dispatch_utils.h"
|
||||||
@@ -43,8 +44,8 @@ __global__ void rotary_embedding_kernel(
|
|||||||
scalar_t* __restrict__ key, // [batch_size, seq_len, num_kv_heads, head_size] or [num_tokens, num_kv_heads, head_size]
|
scalar_t* __restrict__ key, // [batch_size, seq_len, num_kv_heads, head_size] or [num_tokens, num_kv_heads, head_size]
|
||||||
const scalar_t* __restrict__ cos_sin_cache, // [max_position, 2, rot_dim // 2]
|
const scalar_t* __restrict__ cos_sin_cache, // [max_position, 2, rot_dim // 2]
|
||||||
const int rot_dim,
|
const int rot_dim,
|
||||||
const int query_stride,
|
const int64_t query_stride,
|
||||||
const int key_stride,
|
const int64_t key_stride,
|
||||||
const int num_heads,
|
const int num_heads,
|
||||||
const int num_kv_heads,
|
const int num_kv_heads,
|
||||||
const int head_size) {
|
const int head_size) {
|
||||||
@@ -60,7 +61,7 @@ __global__ void rotary_embedding_kernel(
|
|||||||
const int nq = num_heads * embed_dim;
|
const int nq = num_heads * embed_dim;
|
||||||
for (int i = threadIdx.x; i < nq; i += blockDim.x) {
|
for (int i = threadIdx.x; i < nq; i += blockDim.x) {
|
||||||
const int head_idx = i / embed_dim;
|
const int head_idx = i / embed_dim;
|
||||||
const int token_head = token_idx * query_stride + head_idx * head_size;
|
const int64_t token_head = token_idx * query_stride + head_idx * head_size;
|
||||||
const int rot_offset = i % embed_dim;
|
const int rot_offset = i % embed_dim;
|
||||||
apply_rotary_embedding<scalar_t, IS_NEOX>(query + token_head, cos_ptr,
|
apply_rotary_embedding<scalar_t, IS_NEOX>(query + token_head, cos_ptr,
|
||||||
sin_ptr, rot_offset, embed_dim);
|
sin_ptr, rot_offset, embed_dim);
|
||||||
@@ -69,7 +70,7 @@ __global__ void rotary_embedding_kernel(
|
|||||||
const int nk = num_kv_heads * embed_dim;
|
const int nk = num_kv_heads * embed_dim;
|
||||||
for (int i = threadIdx.x; i < nk; i += blockDim.x) {
|
for (int i = threadIdx.x; i < nk; i += blockDim.x) {
|
||||||
const int head_idx = i / embed_dim;
|
const int head_idx = i / embed_dim;
|
||||||
const int token_head = token_idx * key_stride + head_idx * head_size;
|
const int64_t token_head = token_idx * key_stride + head_idx * head_size;
|
||||||
const int rot_offset = i % embed_dim;
|
const int rot_offset = i % embed_dim;
|
||||||
apply_rotary_embedding<scalar_t, IS_NEOX>(key + token_head, cos_ptr,
|
apply_rotary_embedding<scalar_t, IS_NEOX>(key + token_head, cos_ptr,
|
||||||
sin_ptr, rot_offset, embed_dim);
|
sin_ptr, rot_offset, embed_dim);
|
||||||
@@ -89,11 +90,12 @@ void rotary_embedding(
|
|||||||
int rot_dim = cos_sin_cache.size(1);
|
int rot_dim = cos_sin_cache.size(1);
|
||||||
int num_heads = query.size(-1) / head_size;
|
int num_heads = query.size(-1) / head_size;
|
||||||
int num_kv_heads = key.size(-1) / head_size;
|
int num_kv_heads = key.size(-1) / head_size;
|
||||||
int query_stride = query.stride(-2);
|
int64_t query_stride = query.stride(-2);
|
||||||
int key_stride = key.stride(-2);
|
int64_t key_stride = key.stride(-2);
|
||||||
|
|
||||||
dim3 grid(num_tokens);
|
dim3 grid(num_tokens);
|
||||||
dim3 block(std::min(num_heads * rot_dim / 2, 512));
|
dim3 block(std::min(num_heads * rot_dim / 2, 512));
|
||||||
|
const at::cuda::OptionalCUDAGuard device_guard(device_of(query));
|
||||||
const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
|
const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
|
||||||
VLLM_DISPATCH_FLOATING_TYPES(
|
VLLM_DISPATCH_FLOATING_TYPES(
|
||||||
query.scalar_type(),
|
query.scalar_type(),
|
||||||
|
|||||||
@@ -52,8 +52,8 @@ PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
|
|||||||
// Quantization ops
|
// Quantization ops
|
||||||
ops.def("awq_gemm", &awq_gemm, "Quantized GEMM for AWQ");
|
ops.def("awq_gemm", &awq_gemm, "Quantized GEMM for AWQ");
|
||||||
#endif
|
#endif
|
||||||
|
ops.def("gptq_gemm", &gptq_gemm, "Quantized GEMM for GPTQ");
|
||||||
|
ops.def("gptq_shuffle", &gptq_shuffle, "Post processing for GPTQ");
|
||||||
ops.def("squeezellm_gemm", &squeezellm_gemm, "Quantized GEMM for SqueezeLLM");
|
ops.def("squeezellm_gemm", &squeezellm_gemm, "Quantized GEMM for SqueezeLLM");
|
||||||
|
|
||||||
// Cache ops
|
// Cache ops
|
||||||
|
|||||||
64
csrc/quantization/gptq/compat.cuh
Normal file
64
csrc/quantization/gptq/compat.cuh
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
/*
|
||||||
|
Copied from https://github.com/turboderp/exllamav2
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _compat_cuh
|
||||||
|
#define _compat_cuh
|
||||||
|
|
||||||
|
namespace vllm {
|
||||||
|
namespace gptq {
|
||||||
|
// atomicAdd for half types, to support CC < 7.x
|
||||||
|
|
||||||
|
__device__ __forceinline__ void atomicAdd_half(half* address, half val)
|
||||||
|
{
|
||||||
|
unsigned int * address_as_ui = (unsigned int *) ((char *)address - ((size_t)address & 2));
|
||||||
|
unsigned int old = *address_as_ui;
|
||||||
|
unsigned int assumed;
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
assumed = old;
|
||||||
|
__half_raw hsum;
|
||||||
|
hsum.x = (size_t)address & 2 ? (old >> 16) : (old & 0xffff);
|
||||||
|
half tmpres = __hadd(hsum, val);
|
||||||
|
hsum = __half_raw(tmpres);
|
||||||
|
old = (size_t)address & 2 ? (old & 0xffff) | (hsum.x << 16) : (old & 0xffff0000) | hsum.x;
|
||||||
|
old = atomicCAS(address_as_ui, assumed, old);
|
||||||
|
}
|
||||||
|
while (assumed != old);
|
||||||
|
}
|
||||||
|
|
||||||
|
// atomicAdd for half2 types
|
||||||
|
|
||||||
|
__device__ __forceinline__ void atomicAdd_half2(half2* address, half2 val)
|
||||||
|
{
|
||||||
|
unsigned int* address_as_ui = (unsigned int*)address;
|
||||||
|
unsigned int old = *address_as_ui;
|
||||||
|
unsigned int assumed;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
assumed = old;
|
||||||
|
half2 old_val = *((half2*)&old);
|
||||||
|
half2 new_val = __hadd2(old_val, val);
|
||||||
|
old = atomicCAS(address_as_ui, assumed, *((unsigned int*)&new_val));
|
||||||
|
}
|
||||||
|
while (assumed != old);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
#if defined(__CUDA_ARCH__) || defined(USE_ROCM)
|
||||||
|
#if __CUDA_ARCH__ < 700 || defined(USE_ROCM)
|
||||||
|
|
||||||
|
__device__ __forceinline__ void atomicAdd(half* address, half val) { atomicAdd_half(address, val); }
|
||||||
|
|
||||||
|
#if __CUDA_ARCH__ < 600 || defined(USE_ROCM)
|
||||||
|
__device__ __forceinline__ void atomicAdd(half2* address, half2 val) { atomicAdd_half2(address, val); }
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
} // namespace gptq
|
||||||
|
} // namespace vllm
|
||||||
|
#endif
|
||||||
151
csrc/quantization/gptq/matrix_view.cuh
Normal file
151
csrc/quantization/gptq/matrix_view.cuh
Normal file
@@ -0,0 +1,151 @@
|
|||||||
|
/*
|
||||||
|
Adapted from https://github.com/turboderp/exllamav2 and https://github.com/turboderp/exllama
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _matrix_view_cuh
|
||||||
|
#define _matrix_view_cuh
|
||||||
|
|
||||||
|
#include <cuda_runtime.h>
|
||||||
|
#include <cuda_fp16.h>
|
||||||
|
|
||||||
|
#include "qdq_util.cuh"
|
||||||
|
|
||||||
|
namespace vllm {
|
||||||
|
namespace gptq {
|
||||||
|
|
||||||
|
class MatrixView_half
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
const half* data;
|
||||||
|
const int height;
|
||||||
|
const int width;
|
||||||
|
|
||||||
|
__device__ __forceinline__ MatrixView_half(const half* data, const int height, const int width)
|
||||||
|
: data(data), height(height), width(width)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
__device__ __forceinline__ half item(int row, int column) const { return data[row * width + column]; }
|
||||||
|
__device__ __forceinline__ half2 item_half2(int row, int column) const { return ((half2*)data)[(row * width + column) / 2]; }
|
||||||
|
__device__ __forceinline__ half2 item_half2half2(int row, int column) const { return __half2half2(data[row * width + column]); }
|
||||||
|
__device__ __forceinline__ const half* item_ptr(int row, int column) const { return &data[row * width + column]; }
|
||||||
|
|
||||||
|
__device__ __forceinline__ void item4(half (&items)[4], int row, int column) const
|
||||||
|
{
|
||||||
|
half2* ptr = (half2*) item_ptr(row, column);
|
||||||
|
half2 i01 = ptr[0];
|
||||||
|
half2 i23 = ptr[1];
|
||||||
|
items[0] = __low2half(i01);
|
||||||
|
items[1] = __high2half(i01);
|
||||||
|
items[2] = __low2half(i23);
|
||||||
|
items[3] = __high2half(i23);
|
||||||
|
}
|
||||||
|
__device__ __forceinline__ void item4_f(float (&items)[4], int row, int column) const
|
||||||
|
{
|
||||||
|
half2* ptr = (half2*)item_ptr(row, column);
|
||||||
|
half2 i01 = ptr[0];
|
||||||
|
half2 i23 = ptr[1];
|
||||||
|
items[0] = __half2float(__low2half(i01));
|
||||||
|
items[1] = __half2float(__high2half(i01));
|
||||||
|
items[2] = __half2float(__low2half(i23));
|
||||||
|
items[3] = __half2float(__high2half(i23));
|
||||||
|
}
|
||||||
|
|
||||||
|
__device__ __forceinline__ void item4_h2(half2 (&items)[4], int row, int column) const
|
||||||
|
{
|
||||||
|
half2* ptr = (half2*)item_ptr(row, column);
|
||||||
|
half2 i01 = ptr[0];
|
||||||
|
half2 i23 = ptr[1];
|
||||||
|
items[0] = __half2half2(__low2half(i01));
|
||||||
|
items[1] = __half2half2(__high2half(i01));
|
||||||
|
items[2] = __half2half2(__low2half(i23));
|
||||||
|
items[3] = __half2half2(__high2half(i23));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class MatrixView_half_rw
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
half* data;
|
||||||
|
const int height;
|
||||||
|
const int width;
|
||||||
|
|
||||||
|
__device__ __forceinline__ MatrixView_half_rw(half* data, const int height, const int width)
|
||||||
|
: data(data), height(height), width(width)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
__device__ __forceinline__ half item(int row, int column) const { return data[row * width + column]; }
|
||||||
|
__device__ __forceinline__ half2 item_half2(int row, int column) const { return ((half2*)data)[(row * width + column) / 2]; }
|
||||||
|
__device__ __forceinline__ half* item_ptr(int row, int column) { return &data[row * width + column]; }
|
||||||
|
__device__ __forceinline__ void set(int row, int column, half value) { data[row * width + column] = value; }
|
||||||
|
__device__ __forceinline__ void set_half2(int row, int column, half2 value) { ((half2*)data)[(row * width + column) / 2] = value; }
|
||||||
|
|
||||||
|
__device__ __forceinline__ void set4(int row, int column, half v0, half v1, half v2, half v3)
|
||||||
|
{
|
||||||
|
half2 v01 = __halves2half2(v0, v1);
|
||||||
|
half2 v23 = __halves2half2(v2, v3);
|
||||||
|
half2* ptr = (half2*) item_ptr(row, column);
|
||||||
|
ptr[0] = v01;
|
||||||
|
ptr[1] = v23;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class MatrixView_q4_row
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
const uint32_t* data;
|
||||||
|
const int height;
|
||||||
|
const int width;
|
||||||
|
|
||||||
|
__device__ __forceinline__ MatrixView_q4_row(const uint32_t* data, const int height, const int width)
|
||||||
|
: data(data), height(height), width(width)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
__device__ __forceinline__ int item(int row, int column) const
|
||||||
|
{
|
||||||
|
int shift = (column & 0x07) * 4;
|
||||||
|
return (data[row * width / 8 + column / 8] >> shift) & 0x0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
__device__ __forceinline__ void item2(int (&items)[2], int row, int column) const
|
||||||
|
{
|
||||||
|
int shift = (column & 0x07) * 4;
|
||||||
|
uint32_t d = data[row * width / 8 + column / 8] >> shift;
|
||||||
|
items[0] = d & 0x0f;
|
||||||
|
items[1] = (d >> 4) & 0x0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
__device__ __forceinline__ void item4(int (&items)[4], int row, int column) const
|
||||||
|
{
|
||||||
|
int shift = (column & 0x07) * 4;
|
||||||
|
uint32_t d = data[row * width / 8 + column / 8] >> shift;
|
||||||
|
items[0] = d & 0x0f;
|
||||||
|
items[1] = (d >> 4) & 0x0f;
|
||||||
|
items[2] = (d >> 8) & 0x0f;
|
||||||
|
items[3] = (d >> 12) & 0x0f;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class MatrixView_q4_column
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
const uint32_t* data;
|
||||||
|
const int height;
|
||||||
|
const int width;
|
||||||
|
|
||||||
|
__device__ __forceinline__ MatrixView_q4_column(const uint32_t* data, const int height, const int width)
|
||||||
|
: data(data), height(height), width(width)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
__device__ __forceinline__ int item(int row, int column) const
|
||||||
|
{
|
||||||
|
int shift = (row & 0x07) * 4;
|
||||||
|
return (data[row / 8 * width + column] >> shift) & 0x0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
__device__ __forceinline__ uint32_t item_uint32_t(int row, int column) { return data[row / 8 * width + column]; }
|
||||||
|
__device__ __forceinline__ const uint32_t* item_uint32_ptr(int row, int column) { return &data[row / 8 * width + column]; }
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace gptq
|
||||||
|
} // namespace vllm
|
||||||
|
#endif
|
||||||
875
csrc/quantization/gptq/q_gemm.cu
Normal file
875
csrc/quantization/gptq/q_gemm.cu
Normal file
@@ -0,0 +1,875 @@
|
|||||||
|
/*
|
||||||
|
Adapted from https://github.com/turboderp/exllamav2 and https://github.com/qwopqwop200/GPTQ-for-LLaMa
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <cstdio>
|
||||||
|
|
||||||
|
#include <torch/extension.h>
|
||||||
|
#include <c10/cuda/CUDAGuard.h>
|
||||||
|
#include <ATen/cuda/CUDAContext.h>
|
||||||
|
#include <cuda_runtime.h>
|
||||||
|
#include <cuda_fp16.h>
|
||||||
|
|
||||||
|
#include "compat.cuh"
|
||||||
|
#include "matrix_view.cuh"
|
||||||
|
#include "qdq_4.cuh"
|
||||||
|
|
||||||
|
namespace vllm {
|
||||||
|
namespace gptq {
|
||||||
|
|
||||||
|
#define BLOCK_KN_SIZE 128
|
||||||
|
#define BLOCK_M_SIZE_MAX 8
|
||||||
|
#define MAX_GROUPS_IN_BLOCK (BLOCK_KN_SIZE / 32)
|
||||||
|
#define MAX_Q_GEMM_ROWS 50
|
||||||
|
#define MAX_ALT_GEMM_ROWS 8
|
||||||
|
#define THREADS_X 32
|
||||||
|
#define THREADS_Y 32
|
||||||
|
#define DIVIDE(x, size) (((x) + (size) - 1) / (size))
|
||||||
|
|
||||||
|
#if defined(USE_ROCM)
|
||||||
|
#include <hipblas/hipblas.h>
|
||||||
|
__host__ __forceinline__ hipblasStatus_t __compat_hipblasHgemm(hipblasHandle_t handle,
|
||||||
|
hipblasOperation_t transA,
|
||||||
|
hipblasOperation_t transB,
|
||||||
|
int m,
|
||||||
|
int n,
|
||||||
|
int k,
|
||||||
|
const half* alpha,
|
||||||
|
const half* AP,
|
||||||
|
int lda,
|
||||||
|
const half* BP,
|
||||||
|
int ldb,
|
||||||
|
const half* beta,
|
||||||
|
half* CP,
|
||||||
|
int ldc) {
|
||||||
|
return hipblasHgemm(handle, transA, transB, m, n, k,
|
||||||
|
reinterpret_cast<const hipblasHalf *>(alpha),
|
||||||
|
reinterpret_cast<const hipblasHalf *>(AP), lda,
|
||||||
|
reinterpret_cast<const hipblasHalf *>(BP), ldb,
|
||||||
|
reinterpret_cast<const hipblasHalf *>(beta),
|
||||||
|
reinterpret_cast<hipblasHalf *>(CP), ldc);
|
||||||
|
}
|
||||||
|
#define hipblasHgemm __compat_hipblasHgemm
|
||||||
|
|
||||||
|
// Previous version of PyTorch were converting to rocBLAS instead of hipBLAS.
|
||||||
|
#define rocblas_operation_none HIPBLAS_OP_N
|
||||||
|
#define rocblas_hgemm __compat_hipblasHgemm
|
||||||
|
#endif
|
||||||
|
|
||||||
|
__forceinline__ __device__ half2 dot22_8(half2(&dq)[4], const half* a_ptr, const half2 g_result)
|
||||||
|
{
|
||||||
|
half2 result = {};
|
||||||
|
const half2* a2_ptr = (const half2*)a_ptr;
|
||||||
|
#pragma unroll
|
||||||
|
for (int i = 0; i < 4; i++) result = __hfma2(dq[i], *a2_ptr++, result);
|
||||||
|
return __hadd2(result, g_result);
|
||||||
|
}
|
||||||
|
|
||||||
|
__forceinline__ __device__ float dot22_8_f(half2(&dq)[4], const half* a_ptr)
|
||||||
|
{
|
||||||
|
half2 result = {};
|
||||||
|
const half2* a2_ptr = (const half2*)a_ptr;
|
||||||
|
#pragma unroll
|
||||||
|
for (int i = 0; i < 4; i++) result = __hfma2(dq[i], *a2_ptr++, result);
|
||||||
|
return __half2float(__low2half(result)) + __half2float(__high2half(result));
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef void (*fp_gemm_half_q_half_gptq_kernel)
|
||||||
|
(
|
||||||
|
const half*,
|
||||||
|
const uint32_t*,
|
||||||
|
const uint32_t*,
|
||||||
|
const half*,
|
||||||
|
half*,
|
||||||
|
const int,
|
||||||
|
const int,
|
||||||
|
const int,
|
||||||
|
const int,
|
||||||
|
const int*
|
||||||
|
);
|
||||||
|
|
||||||
|
template <bool first_block, int m_count>
|
||||||
|
__global__ void gemm_half_q_half_gptq_kernel
|
||||||
|
(
|
||||||
|
const half* __restrict__ a,
|
||||||
|
const uint32_t* __restrict__ b_q_weight,
|
||||||
|
const uint32_t* __restrict__ b_gptq_qzeros,
|
||||||
|
const half* __restrict__ b_gptq_scales,
|
||||||
|
half* __restrict__ c,
|
||||||
|
const int size_m,
|
||||||
|
const int size_n,
|
||||||
|
const int size_k,
|
||||||
|
const int groups,
|
||||||
|
const int* __restrict__ b_q_perm
|
||||||
|
)
|
||||||
|
{
|
||||||
|
MatrixView_half a_(a, size_m, size_k);
|
||||||
|
MatrixView_half_rw c_(c, size_m, size_n);
|
||||||
|
MatrixView_q4_row b_gptq_qzeros_(b_gptq_qzeros, groups, size_n);
|
||||||
|
MatrixView_half b_gptq_scales_(b_gptq_scales, groups, size_n);
|
||||||
|
|
||||||
|
int t = threadIdx.x;
|
||||||
|
|
||||||
|
// Block
|
||||||
|
int offset_n = blockIdx.x * BLOCK_KN_SIZE * 4;
|
||||||
|
int offset_m = blockIdx.y * m_count;
|
||||||
|
int offset_k = blockIdx.z * BLOCK_KN_SIZE;
|
||||||
|
|
||||||
|
int end_n = min(offset_n + BLOCK_KN_SIZE * 4, size_n);
|
||||||
|
int end_m = min(offset_m + m_count, size_m);
|
||||||
|
int end_k = min(offset_k + BLOCK_KN_SIZE, size_k);
|
||||||
|
|
||||||
|
int n = offset_n + t * 4;
|
||||||
|
|
||||||
|
// Preload block_a
|
||||||
|
__shared__ half block_a[m_count][BLOCK_KN_SIZE];
|
||||||
|
|
||||||
|
if (offset_k + t < end_k)
|
||||||
|
{
|
||||||
|
for (int m = 0; m < m_count; ++m)
|
||||||
|
{
|
||||||
|
const half* a_ptr = a_.item_ptr(offset_m + m, 0);
|
||||||
|
half* block_a_ptr = block_a[m];
|
||||||
|
|
||||||
|
half a0;
|
||||||
|
if (b_q_perm) a0 = a_ptr[b_q_perm[offset_k + t]];
|
||||||
|
else a0 = a_ptr[offset_k + t];
|
||||||
|
block_a_ptr[t] = a0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Zero output
|
||||||
|
if (n >= size_n) return;
|
||||||
|
|
||||||
|
if (blockIdx.z == 0)
|
||||||
|
{
|
||||||
|
for (int m = 0; m < m_count; m++)
|
||||||
|
*((uint64_t*)c_.item_ptr(offset_m + m, n)) = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
__syncthreads();
|
||||||
|
|
||||||
|
// Find initial group
|
||||||
|
int groupsize = size_k / groups;
|
||||||
|
int group = offset_k / groupsize;
|
||||||
|
int nextgroup = offset_k + groupsize;
|
||||||
|
|
||||||
|
// a, b offset
|
||||||
|
int qk = offset_k / (32 / 4);
|
||||||
|
|
||||||
|
const uint32_t* b_ptr = b_q_weight + qk * size_n + n;
|
||||||
|
const half* a_ptr = &block_a[0][0];
|
||||||
|
int a_stride = BLOCK_KN_SIZE;
|
||||||
|
|
||||||
|
// Initial group
|
||||||
|
int zeros[4];
|
||||||
|
float scales[4];
|
||||||
|
half2 z1z16[4][2];
|
||||||
|
half2 y1y16[4][2];
|
||||||
|
b_gptq_qzeros_.item4(zeros, group, n);
|
||||||
|
b_gptq_scales_.item4_f(scales, group, n);
|
||||||
|
dequant_4bit_8_prep_zero(zeros[0] + 1, z1z16[0], y1y16[0]);
|
||||||
|
dequant_4bit_8_prep_zero(zeros[1] + 1, z1z16[1], y1y16[1]);
|
||||||
|
dequant_4bit_8_prep_zero(zeros[2] + 1, z1z16[2], y1y16[2]);
|
||||||
|
dequant_4bit_8_prep_zero(zeros[3] + 1, z1z16[3], y1y16[3]);
|
||||||
|
|
||||||
|
// Column result
|
||||||
|
float block_c[m_count][4] = {};
|
||||||
|
|
||||||
|
// Dequantize and multiply
|
||||||
|
int k = offset_k;
|
||||||
|
while (k < end_k)
|
||||||
|
{
|
||||||
|
if (k == nextgroup)
|
||||||
|
{
|
||||||
|
group++;
|
||||||
|
nextgroup += groupsize;
|
||||||
|
b_gptq_qzeros_.item4(zeros, group, n);
|
||||||
|
b_gptq_scales_.item4_f(scales, group, n);
|
||||||
|
dequant_4bit_8_prep_zero(zeros[0] + 1, z1z16[0], y1y16[0]);
|
||||||
|
dequant_4bit_8_prep_zero(zeros[1] + 1, z1z16[1], y1y16[1]);
|
||||||
|
dequant_4bit_8_prep_zero(zeros[2] + 1, z1z16[2], y1y16[2]);
|
||||||
|
dequant_4bit_8_prep_zero(zeros[3] + 1, z1z16[3], y1y16[3]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#pragma unroll
|
||||||
|
for (int j = 0; j < 4; j++)
|
||||||
|
{
|
||||||
|
const int4* b_ptr4 = (int4*) b_ptr;
|
||||||
|
int4 load_int4 = *b_ptr4;
|
||||||
|
|
||||||
|
half2 dq[4][4];
|
||||||
|
dequant_4bit_8_gptq(load_int4.x, dq[0], z1z16[0], y1y16[0], size_n, false);
|
||||||
|
dequant_4bit_8_gptq(load_int4.y, dq[1], z1z16[1], y1y16[1], size_n, false);
|
||||||
|
dequant_4bit_8_gptq(load_int4.z, dq[2], z1z16[2], y1y16[2], size_n, false);
|
||||||
|
dequant_4bit_8_gptq(load_int4.w, dq[3], z1z16[3], y1y16[3], size_n, false);
|
||||||
|
|
||||||
|
#pragma unroll
|
||||||
|
for (int m = 0; m < m_count; m++)
|
||||||
|
{
|
||||||
|
block_c[m][0] = fma(dot22_8_f(dq[0], a_ptr + m * a_stride), scales[0], block_c[m][0]);
|
||||||
|
block_c[m][1] = fma(dot22_8_f(dq[1], a_ptr + m * a_stride), scales[1], block_c[m][1]);
|
||||||
|
block_c[m][2] = fma(dot22_8_f(dq[2], a_ptr + m * a_stride), scales[2], block_c[m][2]);
|
||||||
|
block_c[m][3] = fma(dot22_8_f(dq[3], a_ptr + m * a_stride), scales[3], block_c[m][3]);
|
||||||
|
}
|
||||||
|
|
||||||
|
b_ptr += size_n;
|
||||||
|
a_ptr += 8;
|
||||||
|
}
|
||||||
|
|
||||||
|
k += 32;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int m = 0; m < m_count; m++)
|
||||||
|
{
|
||||||
|
half2 *out = (half2*) c_.item_ptr(offset_m + m, n);
|
||||||
|
half2 result01 = __halves2half2(__float2half_rn(block_c[m][0]), __float2half_rn(block_c[m][1]));
|
||||||
|
half2 result23 = __halves2half2(__float2half_rn(block_c[m][2]), __float2half_rn(block_c[m][3]));
|
||||||
|
atomicAdd(out , result01);
|
||||||
|
atomicAdd(out + 1, result23);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fp_gemm_half_q_half_gptq_kernel pick_gemm_half_q_half_gptq_kernel(bool first_block, const int m_count)
|
||||||
|
{
|
||||||
|
#if BLOCK_M_SIZE_MAX >= 1
|
||||||
|
if (m_count == 1) return gemm_half_q_half_gptq_kernel<true, 1>;
|
||||||
|
#endif
|
||||||
|
#if BLOCK_M_SIZE_MAX >= 2
|
||||||
|
if (m_count == 2) return gemm_half_q_half_gptq_kernel<true, 2>;
|
||||||
|
#endif
|
||||||
|
#if BLOCK_M_SIZE_MAX >= 3
|
||||||
|
if (m_count == 3) return gemm_half_q_half_gptq_kernel<true, 3>;
|
||||||
|
#endif
|
||||||
|
#if BLOCK_M_SIZE_MAX >= 4
|
||||||
|
if (m_count == 4) return gemm_half_q_half_gptq_kernel<true, 4>;
|
||||||
|
#endif
|
||||||
|
#if BLOCK_M_SIZE_MAX >= 5
|
||||||
|
if (m_count == 5) return gemm_half_q_half_gptq_kernel<true, 5>;
|
||||||
|
#endif
|
||||||
|
#if BLOCK_M_SIZE_MAX >= 6
|
||||||
|
if (m_count == 6) return gemm_half_q_half_gptq_kernel<true, 6>;
|
||||||
|
#endif
|
||||||
|
#if BLOCK_M_SIZE_MAX >= 7
|
||||||
|
if (m_count == 7) return gemm_half_q_half_gptq_kernel<true, 7>;
|
||||||
|
#endif
|
||||||
|
#if BLOCK_M_SIZE_MAX >= 8
|
||||||
|
if (m_count == 8) return gemm_half_q_half_gptq_kernel<true, 8>;
|
||||||
|
#endif
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void gemm_half_q_half_cuda_part
|
||||||
|
(
|
||||||
|
const half* a,
|
||||||
|
const uint32_t* b_q_weight,
|
||||||
|
const uint32_t* b_gptq_qzeros,
|
||||||
|
const half* b_gptq_scales,
|
||||||
|
const int* b_q_perm,
|
||||||
|
half* c,
|
||||||
|
int size_m,
|
||||||
|
int size_n,
|
||||||
|
int size_k,
|
||||||
|
int m_count,
|
||||||
|
int groups
|
||||||
|
)
|
||||||
|
{
|
||||||
|
dim3 blockDim, gridDim;
|
||||||
|
blockDim.x = BLOCK_KN_SIZE;
|
||||||
|
blockDim.y = 1;
|
||||||
|
blockDim.z = 1;
|
||||||
|
gridDim.x = DIVIDE(size_n, BLOCK_KN_SIZE * 4);
|
||||||
|
gridDim.y = DIVIDE(size_m, m_count);
|
||||||
|
gridDim.z = DIVIDE(size_k, BLOCK_KN_SIZE);
|
||||||
|
|
||||||
|
fp_gemm_half_q_half_gptq_kernel kernel = pick_gemm_half_q_half_gptq_kernel(true, m_count);
|
||||||
|
|
||||||
|
const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
|
||||||
|
kernel<<<gridDim, blockDim, 0, stream>>>
|
||||||
|
(
|
||||||
|
a,
|
||||||
|
b_q_weight,
|
||||||
|
b_gptq_qzeros,
|
||||||
|
b_gptq_scales,
|
||||||
|
c,
|
||||||
|
size_m,
|
||||||
|
size_n,
|
||||||
|
size_k,
|
||||||
|
groups,
|
||||||
|
b_q_perm
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
__global__ void reconstruct_exllama_kernel
|
||||||
|
(
|
||||||
|
const uint32_t* __restrict__ b_q_weight,
|
||||||
|
const int* __restrict__ b_q_perm,
|
||||||
|
const uint32_t* __restrict__ b_gptq_qzeros,
|
||||||
|
const half* __restrict__ b_gptq_scales,
|
||||||
|
const int size_k,
|
||||||
|
const int size_n,
|
||||||
|
const int groups,
|
||||||
|
half* __restrict__ b
|
||||||
|
)
|
||||||
|
{
|
||||||
|
MatrixView_half_rw b_(b, size_k, size_n);
|
||||||
|
MatrixView_q4_row b_gptq_qzeros_(b_gptq_qzeros, groups, size_n);
|
||||||
|
MatrixView_half b_gptq_scales_(b_gptq_scales, groups, size_n);
|
||||||
|
|
||||||
|
int offset_k = BLOCK_KN_SIZE * blockIdx.y;
|
||||||
|
int offset_n = BLOCK_KN_SIZE * blockIdx.x * 4;
|
||||||
|
|
||||||
|
int end_k = min(offset_k + BLOCK_KN_SIZE, size_k);
|
||||||
|
|
||||||
|
// Preload remapping table
|
||||||
|
__shared__ int perm[BLOCK_KN_SIZE];
|
||||||
|
int t = threadIdx.x;
|
||||||
|
|
||||||
|
if (b_q_perm)
|
||||||
|
{
|
||||||
|
if (offset_k + t < size_k)
|
||||||
|
perm[t] = b_q_perm[offset_k + t];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Column
|
||||||
|
int n = offset_n + t * 4;
|
||||||
|
if (n >= size_n) return;
|
||||||
|
|
||||||
|
// Find initial group
|
||||||
|
int groupsize = size_k / groups;
|
||||||
|
int group = offset_k / groupsize;
|
||||||
|
int nextgroup = offset_k + groupsize;
|
||||||
|
|
||||||
|
// b offset
|
||||||
|
int qk = offset_k / (32 / 4);
|
||||||
|
|
||||||
|
const uint32_t* b_ptr = b_q_weight + qk * size_n + n;
|
||||||
|
|
||||||
|
// Initial zeros/scale
|
||||||
|
int zeros[4];
|
||||||
|
half2 scales[4];
|
||||||
|
half2 z1z16[4][2];
|
||||||
|
half2 y1y16[4][2];
|
||||||
|
b_gptq_qzeros_.item4(zeros, group, n);
|
||||||
|
b_gptq_scales_.item4_h2(scales, group, n);
|
||||||
|
dequant_4bit_8_prep_zero(zeros[0] + 1, z1z16[0], y1y16[0]);
|
||||||
|
dequant_4bit_8_prep_zero(zeros[1] + 1, z1z16[1], y1y16[1]);
|
||||||
|
dequant_4bit_8_prep_zero(zeros[2] + 1, z1z16[2], y1y16[2]);
|
||||||
|
dequant_4bit_8_prep_zero(zeros[3] + 1, z1z16[3], y1y16[3]);
|
||||||
|
|
||||||
|
__syncthreads();
|
||||||
|
|
||||||
|
int k = offset_k;
|
||||||
|
int lk = 0;
|
||||||
|
|
||||||
|
while (k < end_k)
|
||||||
|
{
|
||||||
|
if (k == nextgroup)
|
||||||
|
{
|
||||||
|
group++;
|
||||||
|
nextgroup += groupsize;
|
||||||
|
b_gptq_qzeros_.item4(zeros, group, n);
|
||||||
|
b_gptq_scales_.item4_h2(scales, group, n);
|
||||||
|
dequant_4bit_8_prep_zero(zeros[0] + 1, z1z16[0], y1y16[0]);
|
||||||
|
dequant_4bit_8_prep_zero(zeros[1] + 1, z1z16[1], y1y16[1]);
|
||||||
|
dequant_4bit_8_prep_zero(zeros[2] + 1, z1z16[2], y1y16[2]);
|
||||||
|
dequant_4bit_8_prep_zero(zeros[3] + 1, z1z16[3], y1y16[3]);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int p = 0; p < 4; p++)
|
||||||
|
{
|
||||||
|
half2 dq[4][4];
|
||||||
|
const int4* b_ptr4 = (int4*) b_ptr;
|
||||||
|
int4 load_int4 = *b_ptr4;
|
||||||
|
|
||||||
|
dequant_4bit_8_gptq(load_int4.x, dq[0], z1z16[0], y1y16[0], size_n, false);
|
||||||
|
dequant_4bit_8_gptq(load_int4.y, dq[1], z1z16[1], y1y16[1], size_n, false);
|
||||||
|
dequant_4bit_8_gptq(load_int4.z, dq[2], z1z16[2], y1y16[2], size_n, false);
|
||||||
|
dequant_4bit_8_gptq(load_int4.w, dq[3], z1z16[3], y1y16[3], size_n, false);
|
||||||
|
|
||||||
|
b_ptr += size_n;
|
||||||
|
//half* dqh = (half*)dq;
|
||||||
|
if (b_q_perm)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < 4; j++)
|
||||||
|
{
|
||||||
|
for (int v = 0; v < 4; v++) dq[v][j] = __hmul2(scales[v], dq[v][j]);
|
||||||
|
b_.set4(perm[lk++], n, __low2half(dq[0][j]), __low2half(dq[1][j]), __low2half(dq[2][j]), __low2half(dq[3][j]));
|
||||||
|
b_.set4(perm[lk++], n, __high2half(dq[0][j]), __high2half(dq[1][j]), __high2half(dq[2][j]), __high2half(dq[3][j]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for (int j = 0; j < 4; j++)
|
||||||
|
{
|
||||||
|
for (int v = 0; v < 4; v++) dq[v][j] = __hmul2(scales[v], dq[v][j]);
|
||||||
|
b_.set4(offset_k + lk++, n, __low2half(dq[0][j]), __low2half(dq[1][j]), __low2half(dq[2][j]), __low2half(dq[3][j]));
|
||||||
|
b_.set4(offset_k + lk++, n, __high2half(dq[0][j]), __high2half(dq[1][j]), __high2half(dq[2][j]), __high2half(dq[3][j]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
k += 32;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void reconstruct_exllama
|
||||||
|
(
|
||||||
|
const uint32_t* b_q_weight,
|
||||||
|
const uint32_t* b_gptq_qzeros,
|
||||||
|
const half* b_gptq_scales,
|
||||||
|
const int* b_q_perm,
|
||||||
|
half* out,
|
||||||
|
int height,
|
||||||
|
int width,
|
||||||
|
int groups
|
||||||
|
)
|
||||||
|
{
|
||||||
|
dim3 blockDim, gridDim;
|
||||||
|
blockDim.x = BLOCK_KN_SIZE;
|
||||||
|
blockDim.y = 1;
|
||||||
|
gridDim.y = DIVIDE(height, BLOCK_KN_SIZE);
|
||||||
|
gridDim.x = DIVIDE(width, BLOCK_KN_SIZE);
|
||||||
|
|
||||||
|
const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
|
||||||
|
reconstruct_exllama_kernel<<<gridDim, blockDim, 0, stream>>>
|
||||||
|
(
|
||||||
|
b_q_weight,
|
||||||
|
b_q_perm,
|
||||||
|
b_gptq_qzeros,
|
||||||
|
b_gptq_scales,
|
||||||
|
height,
|
||||||
|
width,
|
||||||
|
groups,
|
||||||
|
out
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
__global__ void gemm_half_q_half_alt_kernel(
|
||||||
|
const half2* __restrict__ vec,
|
||||||
|
const uint32_t* __restrict__ mat,
|
||||||
|
half* __restrict__ mul,
|
||||||
|
const half* __restrict__ scales,
|
||||||
|
const uint32_t* __restrict__ zeros,
|
||||||
|
const int* __restrict__ g_idx,
|
||||||
|
int batch,
|
||||||
|
int height,
|
||||||
|
int width
|
||||||
|
)
|
||||||
|
{
|
||||||
|
int zero_width = width / 8;
|
||||||
|
int vec_height = height * 4;
|
||||||
|
const int blockwidth2 = BLOCK_KN_SIZE / 2;
|
||||||
|
int b = blockIdx.y * BLOCK_M_SIZE_MAX;
|
||||||
|
int b_end = min(BLOCK_M_SIZE_MAX, batch - b);
|
||||||
|
int h = BLOCK_KN_SIZE * blockIdx.z / 8;
|
||||||
|
int h_end = min(BLOCK_KN_SIZE / 8, height - h) * 4;
|
||||||
|
int w = BLOCK_KN_SIZE * blockIdx.x + threadIdx.x;
|
||||||
|
|
||||||
|
__shared__ half2 blockvec[BLOCK_M_SIZE_MAX][blockwidth2];
|
||||||
|
if (threadIdx.x < h_end) {
|
||||||
|
for (int m = 0; m < b_end; ++m) {
|
||||||
|
blockvec[m][threadIdx.x] =
|
||||||
|
vec[(m + b) * vec_height + blockIdx.z * BLOCK_KN_SIZE / 2 +
|
||||||
|
threadIdx.x];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
__shared__ half2 deq2[256][8];
|
||||||
|
int val = threadIdx.x / 8;
|
||||||
|
int off = threadIdx.x % 8;
|
||||||
|
for (; val < 256; val += BLOCK_KN_SIZE / 8) {
|
||||||
|
deq2[val][off] = __halves2half2(
|
||||||
|
__int2half_rn(val & 0xF), __int2half_rn(val >> 4)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (blockIdx.z == 0)
|
||||||
|
{
|
||||||
|
for (int m = 0; m < b_end; m++)
|
||||||
|
mul[(b + m) * width + w] = __int2half_rn(0);
|
||||||
|
}
|
||||||
|
__syncthreads();
|
||||||
|
|
||||||
|
int i = width * h + w;
|
||||||
|
int g_h = h * 8;
|
||||||
|
int k = 0;
|
||||||
|
int z_w = w / 8;
|
||||||
|
int z_mod = (w % 8) * 4;
|
||||||
|
half2 res2;
|
||||||
|
half res[BLOCK_M_SIZE_MAX] = {};
|
||||||
|
|
||||||
|
unsigned int tmp;
|
||||||
|
while (k < h_end) {
|
||||||
|
tmp = mat[i];
|
||||||
|
half2 scales_tmp[4];
|
||||||
|
half2 zeros_tmp[4];
|
||||||
|
for (int tmp_k = 0; tmp_k < 4; tmp_k++) {
|
||||||
|
int g = g_idx[g_h + (k + tmp_k) * 2];
|
||||||
|
int g2 = g_idx[g_h + (k + tmp_k) * 2 + 1];
|
||||||
|
half scale_f = scales[g * width + w];
|
||||||
|
half scale_f2 = scales[g2 * width + w];
|
||||||
|
half2 scale = __halves2half2(scale_f, scale_f2);
|
||||||
|
half2 zero = __halves2half2(
|
||||||
|
__hmul(scale_f, __int2half_rn(-((zeros[g * zero_width + z_w] >> z_mod) & 0xF) - 1)),
|
||||||
|
__hmul(scale_f2, __int2half_rn(-((zeros[g2 * zero_width + z_w] >> z_mod) & 0xF) - 1))
|
||||||
|
);
|
||||||
|
scales_tmp[tmp_k] = scale;
|
||||||
|
zeros_tmp[tmp_k] = zero;
|
||||||
|
}
|
||||||
|
for (int m = 0; m < b_end; m++) {
|
||||||
|
#ifndef USE_ROCM
|
||||||
|
res2 = {};
|
||||||
|
#else
|
||||||
|
res2.x = __half_as_ushort(__float2half(0));
|
||||||
|
res2.y = __half_as_ushort(__float2half(0));
|
||||||
|
#endif
|
||||||
|
res2 = __hfma2(__hfma2(deq2[(tmp >> 0) & 0xff][off], scales_tmp[0], zeros_tmp[0]), blockvec[m][k + 0], res2);
|
||||||
|
res2 = __hfma2(__hfma2(deq2[(tmp >> 8) & 0xff][off], scales_tmp[1], zeros_tmp[1]), blockvec[m][k + 1], res2);
|
||||||
|
res2 = __hfma2(__hfma2(deq2[(tmp >> 16) & 0xff][off], scales_tmp[2], zeros_tmp[2]), blockvec[m][k + 2], res2);
|
||||||
|
res2 = __hfma2(__hfma2(deq2[(tmp >> 24) & 0xff][off], scales_tmp[3], zeros_tmp[3]), blockvec[m][k + 3], res2);
|
||||||
|
#ifndef USE_ROCM
|
||||||
|
res[m] = __hadd(res[m], __hadd(res2.x, res2.y));
|
||||||
|
#else
|
||||||
|
res[m] = __hadd(res[m], __hadd(__ushort_as_half(res2.x), __ushort_as_half(res2.y)));
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
i += width;
|
||||||
|
k += 4;
|
||||||
|
}
|
||||||
|
for (int m = 0; m < b_end; m++) {
|
||||||
|
atomicAdd(&mul[(b + m) * width + w], res[m]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void gemm_half_q_half_alt
|
||||||
|
(
|
||||||
|
const half* a,
|
||||||
|
const uint32_t* b_q_weight,
|
||||||
|
const uint32_t* b_gptq_qzeros,
|
||||||
|
const half* b_gptq_scales,
|
||||||
|
const int* b_g_idx,
|
||||||
|
half* c,
|
||||||
|
int size_m,
|
||||||
|
int size_n,
|
||||||
|
int size_k
|
||||||
|
)
|
||||||
|
{
|
||||||
|
dim3 blockDim, gridDim;
|
||||||
|
blockDim.x = BLOCK_KN_SIZE;
|
||||||
|
blockDim.y = 1;
|
||||||
|
blockDim.z = 1;
|
||||||
|
gridDim.x = DIVIDE(size_n, BLOCK_KN_SIZE);
|
||||||
|
gridDim.y = DIVIDE(size_m, BLOCK_M_SIZE_MAX);
|
||||||
|
gridDim.z = DIVIDE(size_k, BLOCK_KN_SIZE);
|
||||||
|
|
||||||
|
const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
|
||||||
|
gemm_half_q_half_alt_kernel<<<gridDim, blockDim, 0, stream>>>
|
||||||
|
(
|
||||||
|
(const half2*) a,
|
||||||
|
b_q_weight,
|
||||||
|
c,
|
||||||
|
b_gptq_scales,
|
||||||
|
b_gptq_qzeros,
|
||||||
|
b_g_idx,
|
||||||
|
size_m,
|
||||||
|
size_k / 8,
|
||||||
|
size_n
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
__global__ void reconstruct_gptq_kernel
|
||||||
|
(
|
||||||
|
const uint32_t* __restrict__ w,
|
||||||
|
const half* __restrict__ w_scales,
|
||||||
|
const uint32_t* __restrict__ w_zeros,
|
||||||
|
const int* __restrict__ g_idx,
|
||||||
|
const int height,
|
||||||
|
const int width,
|
||||||
|
const int group,
|
||||||
|
half* __restrict__ out
|
||||||
|
)
|
||||||
|
{
|
||||||
|
// Start of block
|
||||||
|
|
||||||
|
int column = BLOCK_KN_SIZE * blockIdx.x + threadIdx.x;
|
||||||
|
int row = blockIdx.y * 8;
|
||||||
|
if (column >= width) return;
|
||||||
|
|
||||||
|
// Views
|
||||||
|
|
||||||
|
MatrixView_q4_column w_(w, height, width);
|
||||||
|
MatrixView_half_rw out_(out, height, width);
|
||||||
|
MatrixView_half w_scales_(w_scales, group, width);
|
||||||
|
MatrixView_q4_row w_zeros_(w_zeros, group, width);
|
||||||
|
|
||||||
|
uint32_t w_read = w_.item_uint32_t(row, column);
|
||||||
|
half* out_ptr = out_.item_ptr(row, column);
|
||||||
|
|
||||||
|
#pragma unroll
|
||||||
|
for (int s = 0; s < 32; s += 4)
|
||||||
|
{
|
||||||
|
int group = g_idx[row + s / 4];
|
||||||
|
half w_scale = w_scales_.item(group, column);
|
||||||
|
uint32_t w_zero = w_zeros_.item(group, column) + 1;
|
||||||
|
half w_item = __hmul(__int2half_rn((int)((w_read >> s) & 0x0f) - w_zero), w_scale);
|
||||||
|
*out_ptr = w_item; out_ptr += out_.width;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void reconstruct_gptq
|
||||||
|
(
|
||||||
|
const uint32_t* b_q_weight,
|
||||||
|
const uint32_t* b_gptq_qzeros,
|
||||||
|
const half* b_gptq_scales,
|
||||||
|
const int* b_g_idx,
|
||||||
|
half* out,
|
||||||
|
int height,
|
||||||
|
int width,
|
||||||
|
int groups
|
||||||
|
)
|
||||||
|
{
|
||||||
|
dim3 blockDim, gridDim;
|
||||||
|
blockDim.x = BLOCK_KN_SIZE;
|
||||||
|
blockDim.y = 1;
|
||||||
|
gridDim.y = DIVIDE(height, 8);
|
||||||
|
gridDim.x = DIVIDE(width, BLOCK_KN_SIZE);
|
||||||
|
const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
|
||||||
|
reconstruct_gptq_kernel<<<gridDim, blockDim, 0, stream>>>
|
||||||
|
(
|
||||||
|
b_q_weight,
|
||||||
|
b_gptq_scales,
|
||||||
|
b_gptq_qzeros,
|
||||||
|
b_g_idx,
|
||||||
|
height,
|
||||||
|
width,
|
||||||
|
groups,
|
||||||
|
out
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void gemm_half_q_half_cuda
|
||||||
|
(
|
||||||
|
cublasHandle_t cublas_handle,
|
||||||
|
const half* a,
|
||||||
|
const uint32_t* b_q_weight,
|
||||||
|
const uint32_t* b_gptq_qzeros,
|
||||||
|
const half* b_gptq_scales,
|
||||||
|
const int* b_g_idx,
|
||||||
|
half* c,
|
||||||
|
half* temp_dq,
|
||||||
|
int size_m,
|
||||||
|
int size_n,
|
||||||
|
int size_k,
|
||||||
|
int groups,
|
||||||
|
bool use_exllama
|
||||||
|
)
|
||||||
|
{
|
||||||
|
if ((use_exllama && size_m > MAX_Q_GEMM_ROWS) || (!use_exllama && size_m > MAX_ALT_GEMM_ROWS)) {
|
||||||
|
// Reconstruct FP16 matrix, then cuBLAS
|
||||||
|
if (use_exllama) {
|
||||||
|
reconstruct_exllama(b_q_weight, b_gptq_qzeros, b_gptq_scales, b_g_idx, temp_dq,
|
||||||
|
size_k, size_n, groups);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
reconstruct_gptq(b_q_weight, b_gptq_qzeros, b_gptq_scales, b_g_idx,
|
||||||
|
temp_dq, size_k, size_n, groups);
|
||||||
|
}
|
||||||
|
|
||||||
|
const half alpha = __float2half(1.0f);
|
||||||
|
const half beta = __float2half(0.0f);
|
||||||
|
cublasHgemm(cublas_handle,
|
||||||
|
CUBLAS_OP_N,
|
||||||
|
CUBLAS_OP_N,
|
||||||
|
size_n, size_m, size_k,
|
||||||
|
&alpha, temp_dq, size_n,
|
||||||
|
a, size_k,
|
||||||
|
&beta, c, size_n);
|
||||||
|
}
|
||||||
|
else if (use_exllama)
|
||||||
|
{
|
||||||
|
// Quantized matmul
|
||||||
|
int max_chunks = size_m / BLOCK_M_SIZE_MAX;
|
||||||
|
int last_chunk = max_chunks * BLOCK_M_SIZE_MAX;
|
||||||
|
int last_chunk_size = size_m - last_chunk;
|
||||||
|
|
||||||
|
if (max_chunks)
|
||||||
|
{
|
||||||
|
gemm_half_q_half_cuda_part(a, b_q_weight, b_gptq_qzeros, b_gptq_scales, b_g_idx,
|
||||||
|
c, last_chunk, size_n, size_k, BLOCK_M_SIZE_MAX,
|
||||||
|
groups);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (last_chunk_size)
|
||||||
|
{
|
||||||
|
gemm_half_q_half_cuda_part(a + last_chunk * size_k, b_q_weight, b_gptq_qzeros,
|
||||||
|
b_gptq_scales, b_g_idx, c + last_chunk * size_n,
|
||||||
|
last_chunk_size, size_n, size_k, last_chunk_size,
|
||||||
|
groups);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
gemm_half_q_half_alt(a, b_q_weight, b_gptq_qzeros, b_gptq_scales, b_g_idx,
|
||||||
|
c, size_m, size_n, size_k);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
__global__ void shuffle_kernel
|
||||||
|
(
|
||||||
|
uint32_t* __restrict__ b_q_weight,
|
||||||
|
const int size_k,
|
||||||
|
const int size_n
|
||||||
|
)
|
||||||
|
{
|
||||||
|
int n = blockIdx.x * THREADS_X + threadIdx.x;
|
||||||
|
if (n >= size_n) return;
|
||||||
|
int k = 0;
|
||||||
|
uint32_t* b_ptr = b_q_weight + n;
|
||||||
|
while (k < size_k) { shuffle_4bit_8 (b_ptr, size_n); b_ptr += 1 * size_n; k += 8; }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
__global__ void make_sequential_kernel
|
||||||
|
(
|
||||||
|
const uint32_t* __restrict__ w,
|
||||||
|
uint32_t* __restrict__ w_new,
|
||||||
|
const int* __restrict__ q_perm,
|
||||||
|
const int w_height,
|
||||||
|
const int w_width
|
||||||
|
)
|
||||||
|
{
|
||||||
|
const uint64_t* w2 = (uint64_t*) w;
|
||||||
|
uint64_t* w_new2 = (uint64_t*) w_new;
|
||||||
|
int w2_stride = w_width >> 1;
|
||||||
|
int w2_column = THREADS_X * blockIdx.x + threadIdx.x;
|
||||||
|
if (w2_column >= w2_stride) return;
|
||||||
|
int w_new2_row = blockIdx.y;
|
||||||
|
int q_perm_idx = w_new2_row << 3;
|
||||||
|
uint64_t dst = 0;
|
||||||
|
|
||||||
|
#pragma unroll
|
||||||
|
for (int i = 0; i < 8; i++)
|
||||||
|
{
|
||||||
|
int source_row = q_perm[q_perm_idx++];
|
||||||
|
|
||||||
|
int w2_row = source_row >> 3;
|
||||||
|
int w2_subrow = source_row & 0x07;
|
||||||
|
int w2_row_shift = w2_subrow << 2;
|
||||||
|
int wnew2_row_shift = i << 2;
|
||||||
|
|
||||||
|
uint64_t src = w2[w2_row * w2_stride + w2_column];
|
||||||
|
src >>= w2_row_shift;
|
||||||
|
src &= 0x0000000f0000000f;
|
||||||
|
src <<= wnew2_row_shift;
|
||||||
|
dst |= src;
|
||||||
|
}
|
||||||
|
w_new2[w_new2_row * w2_stride + w2_column] = dst;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void shuffle_exllama_weight
|
||||||
|
(
|
||||||
|
uint32_t* q_weight,
|
||||||
|
int* q_perm,
|
||||||
|
int height,
|
||||||
|
int width
|
||||||
|
)
|
||||||
|
{
|
||||||
|
if (q_perm)
|
||||||
|
{
|
||||||
|
uint32_t* new_qweight = NULL;
|
||||||
|
cudaMalloc(&new_qweight, height / 8 * width * sizeof(uint32_t));
|
||||||
|
|
||||||
|
dim3 blockDim, gridDim;
|
||||||
|
blockDim.x = THREADS_X;
|
||||||
|
blockDim.y = 1;
|
||||||
|
gridDim.x = DIVIDE(width, THREADS_X);
|
||||||
|
gridDim.y = height / 8;
|
||||||
|
|
||||||
|
const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
|
||||||
|
make_sequential_kernel<<<gridDim, blockDim, 0, stream>>>
|
||||||
|
(
|
||||||
|
q_weight,
|
||||||
|
new_qweight,
|
||||||
|
q_perm,
|
||||||
|
height / 8,
|
||||||
|
width
|
||||||
|
);
|
||||||
|
// Replace qweights
|
||||||
|
cudaMemcpyAsync(q_weight, new_qweight, height / 8 * width * sizeof(uint32_t), cudaMemcpyDeviceToDevice);
|
||||||
|
// Cleanup
|
||||||
|
cudaDeviceSynchronize();
|
||||||
|
cudaFree(new_qweight);
|
||||||
|
}
|
||||||
|
dim3 blockDim, gridDim;
|
||||||
|
blockDim.x = THREADS_X;
|
||||||
|
blockDim.y = 1;
|
||||||
|
gridDim.x = DIVIDE(width, THREADS_X);
|
||||||
|
gridDim.y = 1;
|
||||||
|
const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
|
||||||
|
shuffle_kernel<<<gridDim, blockDim, 0, stream>>>(q_weight, height, width);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace gptq
|
||||||
|
} // namespace vllm
|
||||||
|
|
||||||
|
torch::Tensor gptq_gemm
|
||||||
|
(
|
||||||
|
torch::Tensor a,
|
||||||
|
torch::Tensor b_q_weight,
|
||||||
|
torch::Tensor b_gptq_qzeros,
|
||||||
|
torch::Tensor b_gptq_scales,
|
||||||
|
torch::Tensor b_g_idx,
|
||||||
|
bool use_exllama
|
||||||
|
)
|
||||||
|
{
|
||||||
|
const at::cuda::OptionalCUDAGuard device_guard(device_of(a));
|
||||||
|
auto options = torch::TensorOptions().dtype(a.dtype()).device(a.device());
|
||||||
|
at::Tensor c = torch::empty({a.size(0), b_q_weight.size(1)}, options);
|
||||||
|
at::Tensor temp_dq = torch::empty({b_q_weight.size(0) * 8, b_q_weight.size(1)}, options);
|
||||||
|
|
||||||
|
vllm::gptq::gemm_half_q_half_cuda
|
||||||
|
(
|
||||||
|
at::cuda::getCurrentCUDABlasHandle(),
|
||||||
|
(const half*) a.data_ptr(),
|
||||||
|
(const uint32_t*) b_q_weight.data_ptr(),
|
||||||
|
(const uint32_t*)b_gptq_qzeros.data_ptr(),
|
||||||
|
(const half*) b_gptq_scales.data_ptr(),
|
||||||
|
b_g_idx.device().is_meta() ? NULL : (const int*) b_g_idx.data_ptr(),
|
||||||
|
(half*) c.data_ptr(),
|
||||||
|
(half*) temp_dq.data_ptr(),
|
||||||
|
c.size(0), // m
|
||||||
|
c.size(1), // n
|
||||||
|
a.size(1), // k
|
||||||
|
b_gptq_qzeros.size(0), // group number
|
||||||
|
use_exllama
|
||||||
|
);
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
void gptq_shuffle
|
||||||
|
(
|
||||||
|
torch::Tensor q_weight,
|
||||||
|
torch::Tensor q_perm
|
||||||
|
)
|
||||||
|
{
|
||||||
|
const at::cuda::OptionalCUDAGuard device_guard(device_of(q_weight));
|
||||||
|
vllm::gptq::shuffle_exllama_weight(
|
||||||
|
(uint32_t*) q_weight.data_ptr(),
|
||||||
|
q_perm.device().is_meta() ? NULL : (int*) q_perm.data_ptr(),
|
||||||
|
q_weight.size(0) * 8,
|
||||||
|
q_weight.size(1)
|
||||||
|
);
|
||||||
|
}
|
||||||
235
csrc/quantization/gptq/qdq_4.cuh
Normal file
235
csrc/quantization/gptq/qdq_4.cuh
Normal file
@@ -0,0 +1,235 @@
|
|||||||
|
/*
|
||||||
|
Copied from https://github.com/turboderp/exllamav2
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _qdq_4_cuh
|
||||||
|
#define _qdq_4_cuh
|
||||||
|
|
||||||
|
#include "qdq_util.cuh"
|
||||||
|
|
||||||
|
namespace vllm {
|
||||||
|
namespace gptq {
|
||||||
|
// Permutation:
|
||||||
|
//
|
||||||
|
// 77775555 33331111 66664444 22220000
|
||||||
|
|
||||||
|
__forceinline__ __device__ void shuffle_4bit_8
|
||||||
|
(
|
||||||
|
uint32_t* q,
|
||||||
|
int stride
|
||||||
|
)
|
||||||
|
{
|
||||||
|
uint32_t qa = q[0];
|
||||||
|
uint32_t qb = 0;
|
||||||
|
|
||||||
|
#pragma unroll
|
||||||
|
for (int i = 0; i < 4; i++)
|
||||||
|
{
|
||||||
|
uint32_t qa0 = qa & 0x0f;
|
||||||
|
uint32_t qa1 = (qa & 0xf0) >> 4;
|
||||||
|
qa >>= 8;
|
||||||
|
qb |= (qa1 << (i * 4 + 16));
|
||||||
|
qb |= (qa0 << (i * 4));
|
||||||
|
}
|
||||||
|
q[0] = qb;
|
||||||
|
}
|
||||||
|
|
||||||
|
__forceinline__ __device__ void dequant_4bit_8
|
||||||
|
(
|
||||||
|
const uint32_t q_0,
|
||||||
|
half2 (&dq)[4],
|
||||||
|
int stride
|
||||||
|
)
|
||||||
|
{
|
||||||
|
const uint32_t c0 = 0x64006400;
|
||||||
|
const half y16_ = __float2half_rn(1.0f / 16.0f);
|
||||||
|
const half2 y16 = __halves2half2(y16_, y16_);
|
||||||
|
const half z1_ = __float2half_rn(-1024.0f - 8.0f);
|
||||||
|
const half z16_ = __float2half_rn(-1024.0f / 16.0f - 8.0f);
|
||||||
|
const half2 z1 = __halves2half2(z1_, z1_);
|
||||||
|
const half2 z16 = __halves2half2(z16_, z16_);
|
||||||
|
|
||||||
|
uint32_t qa = q_0;
|
||||||
|
half2_uint32 q0((qa & 0x000f000f) | c0); // half2(q[ 0], q[ 1]) + 1024
|
||||||
|
half2_uint32 q1((qa & 0x00f000f0) | c0); // half2(q[ 2], q[ 3]) * 16 + 1024
|
||||||
|
qa >>= 8;
|
||||||
|
half2_uint32 q2((qa & 0x000f000f) | c0); // half2(q[ 4], q[ 5]) + 1024
|
||||||
|
half2_uint32 q3((qa & 0x00f000f0) | c0); // half2(q[ 6], q[ 7]) * 16 + 1024
|
||||||
|
|
||||||
|
dq[0] = __hadd2(q0.as_half2, z1);
|
||||||
|
dq[1] = __hfma2(q1.as_half2, y16, z16);
|
||||||
|
dq[2] = __hadd2(q2.as_half2, z1);
|
||||||
|
dq[3] = __hfma2(q3.as_half2, y16, z16);
|
||||||
|
}
|
||||||
|
|
||||||
|
__forceinline__ __device__ void dequant_4bit_8_prep_zero_scale
|
||||||
|
(
|
||||||
|
const uint32_t zero,
|
||||||
|
const half scale,
|
||||||
|
half2 (&z1z16)[2],
|
||||||
|
half2 (&y1y16)[2]
|
||||||
|
)
|
||||||
|
{
|
||||||
|
half_uint16 z1(0xe400 | zero); // half(-1024.0f - zero);
|
||||||
|
half z16 = __hsub(__int2half_rn(-64), __int2half_rn(zero));
|
||||||
|
|
||||||
|
half2 scale2 = __half2half2(scale);
|
||||||
|
|
||||||
|
z1z16[0] = __hmul2(scale2, __half2half2(z1.as_half));
|
||||||
|
z1z16[1] = __hmul2(scale2, __half2half2(z16));
|
||||||
|
|
||||||
|
const half y1 = __float2half_rn(1.0f);
|
||||||
|
const half y16 = __float2half_rn(1.0f / 16.0f);
|
||||||
|
|
||||||
|
y1y16[0] = __hmul2(scale2, __half2half2(y1));
|
||||||
|
y1y16[1] = __hmul2(scale2, __half2half2(y16));
|
||||||
|
}
|
||||||
|
|
||||||
|
__forceinline__ __device__ void dequant_4bit_8_prep_zero
|
||||||
|
(
|
||||||
|
const uint32_t zero,
|
||||||
|
half2(&z1z16)[2],
|
||||||
|
half2(&y1y16)[2]
|
||||||
|
)
|
||||||
|
{
|
||||||
|
half_uint16 z1(0xe400 | zero); // half(-1024.0f - zero);
|
||||||
|
half z16 = __hsub(__int2half_rn(-64), __int2half_rn(zero));
|
||||||
|
|
||||||
|
z1z16[0] = __half2half2(z1.as_half);
|
||||||
|
z1z16[1] = __half2half2(z16);
|
||||||
|
|
||||||
|
const half y1 = __float2half_rn(1.0f);
|
||||||
|
const half y16 = __float2half_rn(1.0f / 16.0f);
|
||||||
|
|
||||||
|
y1y16[0] = __half2half2(y1);
|
||||||
|
y1y16[1] = __half2half2(y16);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
__forceinline__ __device__ void dequant_4bit_8_gptq
|
||||||
|
(
|
||||||
|
const uint32_t q_0,
|
||||||
|
half2 (&dq)[4],
|
||||||
|
half2 (&z1z16)[2],
|
||||||
|
half2 (&y1y16)[2],
|
||||||
|
int stride,
|
||||||
|
bool scaled
|
||||||
|
)
|
||||||
|
{
|
||||||
|
const uint32_t c0 = 0x64006400;
|
||||||
|
|
||||||
|
uint32_t qa = q_0;
|
||||||
|
half2_uint32 q0((qa & 0x000f000f) | c0); // half2( q[0] + 1024, q[1] + 1024 )
|
||||||
|
half2_uint32 q1((qa & 0x00f000f0) | c0); // half2( q[2] * 16 + 1024, q[3] * 16 + 1024 )
|
||||||
|
qa >>= 8;
|
||||||
|
half2_uint32 q2((qa & 0x000f000f) | c0); // half2( q[4] + 1024, q[5] + 1024 )
|
||||||
|
half2_uint32 q3((qa & 0x00f000f0) | c0); // half2( q[6] * 16 + 1024, q[7] * 16 + 1024 )
|
||||||
|
|
||||||
|
if (scaled)
|
||||||
|
{
|
||||||
|
dq[0] = __hfma2(q0.as_half2, y1y16[0], z1z16[0]); // half2( q[0] * s - z * s, q[1] * s - z * s)
|
||||||
|
dq[1] = __hfma2(q1.as_half2, y1y16[1], z1z16[1]); // half2( q[2] * s - z * s, q[3] * s - z * s)
|
||||||
|
dq[2] = __hfma2(q2.as_half2, y1y16[0], z1z16[0]);
|
||||||
|
dq[3] = __hfma2(q3.as_half2, y1y16[1], z1z16[1]);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
dq[0] = __hadd2(q0.as_half2, z1z16[0]); // half2( q[0] - z, q[1] - z )
|
||||||
|
dq[1] = __hfma2(q1.as_half2, y1y16[1], z1z16[1]); // half2( q[2] - z, q[3] - z )
|
||||||
|
dq[2] = __hadd2(q2.as_half2, z1z16[0]); // half2( q[4] - z, q[5] - z )
|
||||||
|
dq[3] = __hfma2(q3.as_half2, y1y16[1], z1z16[1]); // half2( q[6] - z, q[7] - z )
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} // namespace gptq
|
||||||
|
} // namespace vllm
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
namespace vllm {
|
||||||
|
namespace gptq {
|
||||||
|
__forceinline__ __device__ void shuffle_4bit_8
|
||||||
|
(
|
||||||
|
uint32_t* q,
|
||||||
|
int stride
|
||||||
|
)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
__forceinline__ __device__ void dequant_4bit_8
|
||||||
|
(
|
||||||
|
const uint32_t q_0,
|
||||||
|
half2 (&dq)[4],
|
||||||
|
int stride
|
||||||
|
)
|
||||||
|
{
|
||||||
|
half dqh[8];
|
||||||
|
for (int i = 0; i < 8; i++) dqh[i] = dq_ns(exb(q_0, i * 4, 0x0f), 8);
|
||||||
|
|
||||||
|
for (int i = 0; i < 4; i++) dq[i] = __halves2half2(dqh[i * 2], dqh[i * 2 + 1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
__forceinline__ __device__ void dequant_4bit_8_prep_zero_scale
|
||||||
|
(
|
||||||
|
const uint32_t zero,
|
||||||
|
const half scale,
|
||||||
|
half2 (&z1)[2],
|
||||||
|
half2 (&y1)[2]
|
||||||
|
)
|
||||||
|
{
|
||||||
|
half z = __int2half_rn(-((int)zero));
|
||||||
|
z = __hmul(z, scale);
|
||||||
|
z1[0] = __half2half2(z);
|
||||||
|
y1[0] = __half2half2(scale);
|
||||||
|
}
|
||||||
|
|
||||||
|
__forceinline__ __device__ void dequant_4bit_8_prep_zero
|
||||||
|
(
|
||||||
|
const uint32_t zero,
|
||||||
|
half2(&z1)[2],
|
||||||
|
half2(&y1)[2]
|
||||||
|
)
|
||||||
|
{
|
||||||
|
half z = __int2half_rn(-((int)zero));
|
||||||
|
z1[0] = __half2half2(z);
|
||||||
|
}
|
||||||
|
|
||||||
|
__forceinline__ __device__ void dequant_4bit_8_gptq
|
||||||
|
(
|
||||||
|
const uint32_t q_0,
|
||||||
|
half2 (&dq)[4],
|
||||||
|
half2 (&z1)[2],
|
||||||
|
half2 (&y1)[2],
|
||||||
|
int stride,
|
||||||
|
bool scaled
|
||||||
|
)
|
||||||
|
{
|
||||||
|
half2 dqh2[8];
|
||||||
|
|
||||||
|
uint32_t qa = q_0;
|
||||||
|
for (int i = 0; i < 4; i++)
|
||||||
|
{
|
||||||
|
half d0 = __int2half_rn(qa & 0x0f); qa >>= 4;
|
||||||
|
half d1 = __int2half_rn(qa & 0x0f); qa >>= 4;
|
||||||
|
dqh2[i] = __halves2half2(d0, d1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (scaled)
|
||||||
|
{
|
||||||
|
dq[0] = __hfma2(dqh2[0], y1[0], z1[0]);
|
||||||
|
dq[1] = __hfma2(dqh2[1], y1[0], z1[0]);
|
||||||
|
dq[2] = __hfma2(dqh2[2], y1[0], z1[0]);
|
||||||
|
dq[3] = __hfma2(dqh2[3], y1[0], z1[0]);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
dq[0] = __hadd2(dqh2[0], z1[0]);
|
||||||
|
dq[1] = __hadd2(dqh2[1], z1[0]);
|
||||||
|
dq[2] = __hadd2(dqh2[2], z1[0]);
|
||||||
|
dq[3] = __hadd2(dqh2[3], z1[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace gptq
|
||||||
|
} // namespace vllm
|
||||||
|
|
||||||
|
#endif
|
||||||
60
csrc/quantization/gptq/qdq_util.cuh
Normal file
60
csrc/quantization/gptq/qdq_util.cuh
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
/*
|
||||||
|
Copied from https://github.com/turboderp/exllamav2
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _qdq_util_cuh
|
||||||
|
#define _qdq_util_cuh
|
||||||
|
|
||||||
|
namespace vllm {
|
||||||
|
namespace gptq {
|
||||||
|
|
||||||
|
union half2_uint32
|
||||||
|
{
|
||||||
|
uint32_t as_uint32;
|
||||||
|
half2 as_half2;
|
||||||
|
__device__ half2_uint32(uint32_t val) : as_uint32(val) {}
|
||||||
|
__device__ half2_uint32(half2 val) : as_half2(val) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
union half_uint16
|
||||||
|
{
|
||||||
|
uint16_t as_uint16;
|
||||||
|
half as_half;
|
||||||
|
__device__ half_uint16(uint16_t val) : as_uint16(val) {}
|
||||||
|
__device__ half_uint16(half val) : as_half(val) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Max_scale premultiplied by 1/256
|
||||||
|
|
||||||
|
__forceinline__ __device__ half dq_scale(const int qs, const half max_scale)
|
||||||
|
{
|
||||||
|
int qs_i = qs + 1;
|
||||||
|
half qs_h = __int2half_rn(qs_i * qs_i);
|
||||||
|
qs_h = __hmul(qs_h, max_scale);
|
||||||
|
return qs_h;
|
||||||
|
}
|
||||||
|
|
||||||
|
__forceinline__ __device__ half dq(const int q, const int qzero, const half scale)
|
||||||
|
{
|
||||||
|
return __hmul(__int2half_rn(q - qzero), scale);
|
||||||
|
}
|
||||||
|
|
||||||
|
__forceinline__ __device__ half dq_ns(const int q, const int qzero)
|
||||||
|
{
|
||||||
|
//return __hsub(__int2half_rn(q), __int2half_rn(qzero));
|
||||||
|
return __int2half_rn(q - qzero);
|
||||||
|
}
|
||||||
|
|
||||||
|
__forceinline__ __device__ int exb(const uint32_t q, const int shift, const int mask)
|
||||||
|
{
|
||||||
|
return (int)((q >> shift) & mask);
|
||||||
|
}
|
||||||
|
|
||||||
|
__forceinline__ __device__ int exb(const uint32_t q1, const uint32_t q0, const int shift, const int mask)
|
||||||
|
{
|
||||||
|
return (int)(__funnelshift_rc(q0, q1, shift) & mask);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace gptq
|
||||||
|
} // namespace vllm
|
||||||
|
#endif
|
||||||
@@ -7,6 +7,7 @@
|
|||||||
// half-tensor
|
// half-tensor
|
||||||
#include <c10/cuda/CUDAStream.h>
|
#include <c10/cuda/CUDAStream.h>
|
||||||
#include <ATen/cuda/CUDATensorMethods.cuh>
|
#include <ATen/cuda/CUDATensorMethods.cuh>
|
||||||
|
#include <c10/cuda/CUDAGuard.h>
|
||||||
|
|
||||||
#define BLOCKWIDTH 128
|
#define BLOCKWIDTH 128
|
||||||
#define BLOCKHEIGHT4 16
|
#define BLOCKHEIGHT4 16
|
||||||
@@ -200,7 +201,9 @@ void squeezellm_gemm(
|
|||||||
);
|
);
|
||||||
dim3 threads(BLOCKWIDTH);
|
dim3 threads(BLOCKWIDTH);
|
||||||
|
|
||||||
vllm::squeezellm::NUQ4MatMulKernel<<<blocks, threads>>>(
|
const at::cuda::OptionalCUDAGuard device_guard(device_of(vec));
|
||||||
|
const cudaStream_t stream = at::cuda::getCurrentCUDAStream();
|
||||||
|
vllm::squeezellm::NUQ4MatMulKernel<<<blocks, threads, 0, stream>>>(
|
||||||
#ifndef USE_ROCM
|
#ifndef USE_ROCM
|
||||||
(half2*) vec.data<at::Half>(),
|
(half2*) vec.data<at::Half>(),
|
||||||
#else
|
#else
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
Installation with ROCm
|
Installation with ROCm
|
||||||
======================
|
======================
|
||||||
|
|
||||||
vLLM 0.2.x onwards supports model inferencing and serving on AMD GPUs with ROCm.
|
vLLM 0.2.4 onwards supports model inferencing and serving on AMD GPUs with ROCm.
|
||||||
At the moment AWQ quantization is not supported in ROCm, but SqueezeLLM quantization has been ported.
|
At the moment AWQ quantization is not supported in ROCm, but SqueezeLLM quantization has been ported.
|
||||||
Data types currently supported in ROCm are FP16 and BF16.
|
Data types currently supported in ROCm are FP16 and BF16.
|
||||||
|
|
||||||
@@ -29,7 +29,7 @@ Installation options:
|
|||||||
|
|
||||||
.. code-block:: console
|
.. code-block:: console
|
||||||
|
|
||||||
$ docker pull embeddedllminfo/vllm-rocm:vllm-v0.2.3
|
$ docker pull embeddedllminfo/vllm-rocm:vllm-v0.2.4
|
||||||
$ docker run -it \
|
$ docker run -it \
|
||||||
--network=host \
|
--network=host \
|
||||||
--group-add=video \
|
--group-add=video \
|
||||||
@@ -70,12 +70,12 @@ You can build and install vLLM from source:
|
|||||||
- ROCm's Flash-attention-2 (v2.0.4) does not support sliding windows attention.
|
- ROCm's Flash-attention-2 (v2.0.4) does not support sliding windows attention.
|
||||||
- You might need to downgrade the "ninja" version to 1.10 it is not used when compiling flash-attention-2 (e.g. `pip install ninja==1.10.2.4`)
|
- You might need to downgrade the "ninja" version to 1.10 it is not used when compiling flash-attention-2 (e.g. `pip install ninja==1.10.2.4`)
|
||||||
|
|
||||||
2. Setup `xformers==0.0.22.post7` without dependencies, and apply patches to adapt for ROCm flash attention
|
2. Setup `xformers==0.0.23` without dependencies, and apply patches to adapt for ROCm flash attention
|
||||||
|
|
||||||
.. code-block:: console
|
.. code-block:: console
|
||||||
|
|
||||||
$ pip install xformers==0.0.22.post7 --no-deps
|
$ pip install xformers==0.0.23 --no-deps
|
||||||
$ bash patch_xformers-0.0.22.post7.rocm.sh
|
$ bash patch_xformers.rocm.sh
|
||||||
|
|
||||||
3. Build vLLM.
|
3. Build vLLM.
|
||||||
|
|
||||||
@@ -116,6 +116,7 @@ Alternatively, if you plan to install vLLM-ROCm on a local machine or start from
|
|||||||
|
|
||||||
- `ROCm <https://rocm.docs.amd.com/en/latest/deploy/linux/index.html>`_
|
- `ROCm <https://rocm.docs.amd.com/en/latest/deploy/linux/index.html>`_
|
||||||
- `Pytorch <https://pytorch.org/>`_
|
- `Pytorch <https://pytorch.org/>`_
|
||||||
|
- `hipBLAS <https://rocm.docs.amd.com/projects/hipBLAS/en/latest/install.html>`_
|
||||||
|
|
||||||
1. Install `flash attention for ROCm <https://github.com/ROCmSoftwarePlatform/flash-attention/tree/flash_attention_for_rocm>`_
|
1. Install `flash attention for ROCm <https://github.com/ROCmSoftwarePlatform/flash-attention/tree/flash_attention_for_rocm>`_
|
||||||
|
|
||||||
@@ -127,12 +128,12 @@ Alternatively, if you plan to install vLLM-ROCm on a local machine or start from
|
|||||||
- ROCm's Flash-attention-2 (v2.0.4) does not support sliding windows attention.
|
- ROCm's Flash-attention-2 (v2.0.4) does not support sliding windows attention.
|
||||||
- You might need to downgrade the "ninja" version to 1.10 it is not used when compiling flash-attention-2 (e.g. `pip install ninja==1.10.2.4`)
|
- You might need to downgrade the "ninja" version to 1.10 it is not used when compiling flash-attention-2 (e.g. `pip install ninja==1.10.2.4`)
|
||||||
|
|
||||||
2. Setup `xformers==0.0.22.post7` without dependencies, and apply patches to adapt for ROCm flash attention
|
2. Setup `xformers==0.0.23` without dependencies, and apply patches to adapt for ROCm flash attention
|
||||||
|
|
||||||
.. code-block:: console
|
.. code-block:: console
|
||||||
|
|
||||||
$ pip install xformers==0.0.22.post7 --no-deps
|
$ pip install xformers==0.0.23 --no-deps
|
||||||
$ bash patch_xformers-0.0.22.post7.rocm.sh
|
$ bash patch_xformers.rocm.sh
|
||||||
|
|
||||||
3. Build vLLM.
|
3. Build vLLM.
|
||||||
|
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ You can install vLLM using pip:
|
|||||||
.. code-block:: console
|
.. code-block:: console
|
||||||
|
|
||||||
$ # (Optional) Create a new conda environment.
|
$ # (Optional) Create a new conda environment.
|
||||||
$ conda create -n myenv python=3.8 -y
|
$ conda create -n myenv python=3.9 -y
|
||||||
$ conda activate myenv
|
$ conda activate myenv
|
||||||
|
|
||||||
$ # Install vLLM with CUDA 12.1.
|
$ # Install vLLM with CUDA 12.1.
|
||||||
@@ -34,13 +34,18 @@ You can install vLLM using pip:
|
|||||||
.. code-block:: console
|
.. code-block:: console
|
||||||
|
|
||||||
$ # Install vLLM with CUDA 11.8.
|
$ # Install vLLM with CUDA 11.8.
|
||||||
$ # Replace `cp310` with your Python version (e.g., `cp38`, `cp39`, `cp311`).
|
$ export VLLM_VERSION=0.2.4
|
||||||
$ pip install https://github.com/vllm-project/vllm/releases/download/v0.2.2/vllm-0.2.2+cu118-cp310-cp310-manylinux1_x86_64.whl
|
$ export PYTHON_VERSION=39
|
||||||
|
$ pip install https://github.com/vllm-project/vllm/releases/download/v${VLLM_VERSION}/vllm-${VLLM_VERSION}+cu118-cp${PYTHON_VERSION}-cp${PYTHON_VERSION}-manylinux1_x86_64.whl
|
||||||
|
|
||||||
$ # Re-install PyTorch with CUDA 11.8.
|
$ # Re-install PyTorch with CUDA 11.8.
|
||||||
$ pip uninstall torch -y
|
$ pip uninstall torch -y
|
||||||
$ pip install torch --upgrade --index-url https://download.pytorch.org/whl/cu118
|
$ pip install torch --upgrade --index-url https://download.pytorch.org/whl/cu118
|
||||||
|
|
||||||
|
$ # Re-install xFormers with CUDA 11.8.
|
||||||
|
$ pip uninstall xformers -y
|
||||||
|
$ pip install --upgrade xformers --index-url https://download.pytorch.org/whl/cu118
|
||||||
|
|
||||||
|
|
||||||
.. _build_from_source:
|
.. _build_from_source:
|
||||||
|
|
||||||
|
|||||||
@@ -30,6 +30,8 @@ vLLM is fast with:
|
|||||||
* State-of-the-art serving throughput
|
* State-of-the-art serving throughput
|
||||||
* Efficient management of attention key and value memory with **PagedAttention**
|
* Efficient management of attention key and value memory with **PagedAttention**
|
||||||
* Continuous batching of incoming requests
|
* Continuous batching of incoming requests
|
||||||
|
* Fast model execution with CUDA/HIP graph
|
||||||
|
* Quantization: `GPTQ <https://arxiv.org/abs/2210.17323>`_, `AWQ <https://arxiv.org/abs/2306.00978>`_, `SqueezeLLM <https://arxiv.org/abs/2306.07629>`_
|
||||||
* Optimized CUDA kernels
|
* Optimized CUDA kernels
|
||||||
|
|
||||||
vLLM is flexible and easy to use with:
|
vLLM is flexible and easy to use with:
|
||||||
@@ -39,7 +41,7 @@ vLLM is flexible and easy to use with:
|
|||||||
* Tensor parallelism support for distributed inference
|
* Tensor parallelism support for distributed inference
|
||||||
* Streaming outputs
|
* Streaming outputs
|
||||||
* OpenAI-compatible API server
|
* OpenAI-compatible API server
|
||||||
* Support NVIDIA CUDA and AMD ROCm.
|
* Support NVIDIA GPUs and AMD GPUs
|
||||||
|
|
||||||
For more information, check out the following:
|
For more information, check out the following:
|
||||||
|
|
||||||
|
|||||||
@@ -58,11 +58,10 @@ Next, you need to rewrite the :code:`forward` methods of your model by following
|
|||||||
+ positions: torch.Tensor,
|
+ positions: torch.Tensor,
|
||||||
+ kv_caches: List[KVCache],
|
+ kv_caches: List[KVCache],
|
||||||
+ input_metadata: InputMetadata,
|
+ input_metadata: InputMetadata,
|
||||||
+ cache_events: Optional[List[torch.cuda.Event]],
|
+) -> Optional[SamplerOutput]:
|
||||||
+) -> SamplerOutput:
|
|
||||||
|
|
||||||
3. Update the code by considering that :code:`input_ids` and :code:`positions` are now flattened tensors.
|
1. Update the code by considering that :code:`input_ids` and :code:`positions` are now flattened tensors.
|
||||||
4. Replace the attention operation with either :code:`PagedAttention`, :code:`PagedAttentionWithRoPE`, or :code:`PagedAttentionWithALiBi` depending on the model's architecture.
|
2. Replace the attention operation with either :code:`PagedAttention`, :code:`PagedAttentionWithRoPE`, or :code:`PagedAttentionWithALiBi` depending on the model's architecture.
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
Currently, vLLM supports the basic multi-head attention mechanism and its variant with rotary positional embeddings.
|
Currently, vLLM supports the basic multi-head attention mechanism and its variant with rotary positional embeddings.
|
||||||
|
|||||||
@@ -89,9 +89,11 @@ Below, you can find an explanation of every engine argument for vLLM:
|
|||||||
|
|
||||||
CPU swap space size (GiB) per GPU.
|
CPU swap space size (GiB) per GPU.
|
||||||
|
|
||||||
.. option:: --gpu-memory-utilization <percentage>
|
.. option:: --gpu-memory-utilization <fraction>
|
||||||
|
|
||||||
The percentage of GPU memory to be used for the model executor.
|
The fraction of GPU memory to be used for the model executor, which can range from 0 to 1.
|
||||||
|
For example, a value of 0.5 would imply 50% GPU memory utilization.
|
||||||
|
If unspecified, will use the default value of 0.9.
|
||||||
|
|
||||||
.. option:: --max-num-batched-tokens <tokens>
|
.. option:: --max-num-batched-tokens <tokens>
|
||||||
|
|
||||||
|
|||||||
@@ -23,6 +23,9 @@ Alongside each architecture, we include some popular models that use it.
|
|||||||
* - :code:`ChatGLMModel`
|
* - :code:`ChatGLMModel`
|
||||||
- ChatGLM
|
- ChatGLM
|
||||||
- :code:`THUDM/chatglm2-6b`, :code:`THUDM/chatglm3-6b`, etc.
|
- :code:`THUDM/chatglm2-6b`, :code:`THUDM/chatglm3-6b`, etc.
|
||||||
|
* - :code:`DeciLMForCausalLM`
|
||||||
|
- DeciLM
|
||||||
|
- :code:`Deci/DeciLM-7B`, :code:`Deci/DeciLM-7B-instruct`, etc.
|
||||||
* - :code:`BloomForCausalLM`
|
* - :code:`BloomForCausalLM`
|
||||||
- BLOOM, BLOOMZ, BLOOMChat
|
- BLOOM, BLOOMZ, BLOOMChat
|
||||||
- :code:`bigscience/bloom`, :code:`bigscience/bloomz`, etc.
|
- :code:`bigscience/bloom`, :code:`bigscience/bloomz`, etc.
|
||||||
@@ -60,8 +63,8 @@ Alongside each architecture, we include some popular models that use it.
|
|||||||
- OPT, OPT-IML
|
- OPT, OPT-IML
|
||||||
- :code:`facebook/opt-66b`, :code:`facebook/opt-iml-max-30b`, etc.
|
- :code:`facebook/opt-66b`, :code:`facebook/opt-iml-max-30b`, etc.
|
||||||
* - :code:`PhiForCausalLM`
|
* - :code:`PhiForCausalLM`
|
||||||
- Phi-1.5
|
- Phi
|
||||||
- :code:`microsoft/phi-1_5`, etc.
|
- :code:`microsoft/phi-1_5`, :code:`microsoft/phi-2`, etc.
|
||||||
* - :code:`QWenLMHeadModel`
|
* - :code:`QWenLMHeadModel`
|
||||||
- Qwen
|
- Qwen
|
||||||
- :code:`Qwen/Qwen-7B`, :code:`Qwen/Qwen-7B-Chat`, etc.
|
- :code:`Qwen/Qwen-7B`, :code:`Qwen/Qwen-7B-Chat`, etc.
|
||||||
@@ -73,6 +76,9 @@ If your model uses one of the above model architectures, you can seamlessly run
|
|||||||
Otherwise, please refer to :ref:`Adding a New Model <adding_a_new_model>` for instructions on how to implement support for your model.
|
Otherwise, please refer to :ref:`Adding a New Model <adding_a_new_model>` for instructions on how to implement support for your model.
|
||||||
Alternatively, you can raise an issue on our `GitHub <https://github.com/vllm-project/vllm/issues>`_ project.
|
Alternatively, you can raise an issue on our `GitHub <https://github.com/vllm-project/vllm/issues>`_ project.
|
||||||
|
|
||||||
|
.. note::
|
||||||
|
Currently, the ROCm version of vLLM supports Mistral and Mixtral only for context lengths up to 4096.
|
||||||
|
|
||||||
.. tip::
|
.. tip::
|
||||||
The easiest way to check if your model is supported is to run the program below:
|
The easiest way to check if your model is supported is to run the program below:
|
||||||
|
|
||||||
@@ -84,12 +90,17 @@ Alternatively, you can raise an issue on our `GitHub <https://github.com/vllm-pr
|
|||||||
output = llm.generate("Hello, my name is")
|
output = llm.generate("Hello, my name is")
|
||||||
print(output)
|
print(output)
|
||||||
|
|
||||||
To use model from www.modelscope.cn
|
If vLLM successfully generates text, it indicates that your model is supported.
|
||||||
|
|
||||||
|
.. tip::
|
||||||
|
To use models from `ModelScope <https://www.modelscope.cn>`_ instead of HuggingFace Hub, set an environment variable:
|
||||||
|
|
||||||
.. code-block:: shell
|
.. code-block:: shell
|
||||||
|
|
||||||
$ export VLLM_USE_MODELSCOPE=True
|
$ export VLLM_USE_MODELSCOPE=True
|
||||||
|
|
||||||
|
And use with :code:`trust_remote_code=True`.
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
from vllm import LLM
|
from vllm import LLM
|
||||||
@@ -97,5 +108,3 @@ Alternatively, you can raise an issue on our `GitHub <https://github.com/vllm-pr
|
|||||||
llm = LLM(model=..., revision=..., trust_remote_code=True) # Name or path of your model
|
llm = LLM(model=..., revision=..., trust_remote_code=True) # Name or path of your model
|
||||||
output = llm.generate("Hello, my name is")
|
output = llm.generate("Hello, my name is")
|
||||||
print(output)
|
print(output)
|
||||||
|
|
||||||
If vLLM successfully generates text, it indicates that your model is supported.
|
|
||||||
|
|||||||
@@ -28,4 +28,4 @@ To run inference on a single or multiple GPUs, use ``VLLM`` class from ``langcha
|
|||||||
|
|
||||||
print(llm("What is the capital of France ?"))
|
print(llm("What is the capital of France ?"))
|
||||||
|
|
||||||
Please refer to this `Tutorial <https://github.com/langchain-ai/langchain/blob/master/docs/extras/integrations/llms/vllm.ipynb>`_ for more details.
|
Please refer to this `Tutorial <https://github.com/langchain-ai/langchain/blob/master/docs/docs/integrations/llms/vllm.ipynb>`_ for more details.
|
||||||
@@ -47,6 +47,6 @@ if __name__ == "__main__":
|
|||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
demo = build_demo()
|
demo = build_demo()
|
||||||
demo.queue(concurrency_count=100).launch(server_name=args.host,
|
demo.queue().launch(server_name=args.host,
|
||||||
server_port=args.port,
|
server_port=args.port,
|
||||||
share=True)
|
share=True)
|
||||||
|
|||||||
@@ -1,21 +1,32 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
set -e
|
||||||
|
|
||||||
|
XFORMERS_VERSION="0.0.23"
|
||||||
|
|
||||||
|
export XFORMERS_INSTALLED_VERSION=$(python -c 'import xformers; print(xformers.__version__)')
|
||||||
|
|
||||||
|
if [ "$XFORMERS_INSTALLED_VERSION" != "$XFORMERS_VERSION" ]; then
|
||||||
|
echo "ERROR: xformers version must be ${XFORMERS_VERSION}. ${XFORMERS_INSTALLED_VERSION} is installed"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
export XFORMERS_FMHA_FLASH_PATH=$(python -c 'from xformers import ops as xops; print(xops.fmha.flash.__file__)')
|
export XFORMERS_FMHA_FLASH_PATH=$(python -c 'from xformers import ops as xops; print(xops.fmha.flash.__file__)')
|
||||||
export XFORMERS_FMHA_COMMON_PATH=$(python -c 'from xformers import ops as xops; print(xops.fmha.common.__file__)')
|
export XFORMERS_FMHA_COMMON_PATH=$(python -c 'from xformers import ops as xops; print(xops.fmha.common.__file__)')
|
||||||
|
|
||||||
echo $XFORMERS_FMHA_FLASH_PATH
|
echo "XFORMERS_FMHA_FLASH_PATH = ${XFORMERS_FMHA_FLASH_PATH}"
|
||||||
echo $XFORMERS_FMHA_COMMON_PATH
|
echo "XFORMERS_FMHA_COMMON_PATH = ${XFORMERS_FMHA_COMMON_PATH}"
|
||||||
|
|
||||||
if ! patch -R -p0 -s -f --dry-run $XFORMERS_FMHA_FLASH_PATH "./rocm_patch/flashpy_xformers-0.0.22.post7.rocm.patch"; then
|
if ! patch -R -p0 -s -f --dry-run $XFORMERS_FMHA_FLASH_PATH "./rocm_patch/flashpy_xformers-${XFORMERS_VERSION}.rocm.patch"; then
|
||||||
echo "Applying patch to ${XFORMERS_FMHA_FLASH_PATH}"
|
echo "Applying patch to ${XFORMERS_FMHA_FLASH_PATH}"
|
||||||
patch -p0 $XFORMERS_FMHA_FLASH_PATH "./rocm_patch/flashpy_xformers-0.0.22.post7.rocm.patch"
|
patch -p0 $XFORMERS_FMHA_FLASH_PATH "./rocm_patch/flashpy_xformers-${XFORMERS_VERSION}.rocm.patch"
|
||||||
echo "Successfully patch ${XFORMERS_FMHA_FLASH_PATH}"
|
echo "Successfully patch ${XFORMERS_FMHA_FLASH_PATH}"
|
||||||
else
|
else
|
||||||
echo "${XFORMERS_FMHA_FLASH_PATH} was patched before"
|
echo "${XFORMERS_FMHA_FLASH_PATH} was patched before"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if ! patch -R -p0 -s -f --dry-run $XFORMERS_FMHA_COMMON_PATH "./rocm_patch/commonpy_xformers-0.0.22.post7.rocm.patch"; then
|
if ! patch -R -p0 -s -f --dry-run $XFORMERS_FMHA_COMMON_PATH "./rocm_patch/commonpy_xformers-${XFORMERS_VERSION}.rocm.patch"; then
|
||||||
echo "Applying patch to ${XFORMERS_FMHA_COMMON_PATH}"
|
echo "Applying patch to ${XFORMERS_FMHA_COMMON_PATH}"
|
||||||
patch -p0 $XFORMERS_FMHA_COMMON_PATH "./rocm_patch/commonpy_xformers-0.0.22.post7.rocm.patch"
|
patch -p0 $XFORMERS_FMHA_COMMON_PATH "./rocm_patch/commonpy_xformers-${XFORMERS_VERSION}.rocm.patch"
|
||||||
echo "Successfully patch ${XFORMERS_FMHA_COMMON_PATH}"
|
echo "Successfully patch ${XFORMERS_FMHA_COMMON_PATH}"
|
||||||
else
|
else
|
||||||
echo "${XFORMERS_FMHA_COMMON_PATH} was patched before"
|
echo "${XFORMERS_FMHA_COMMON_PATH} was patched before"
|
||||||
@@ -4,7 +4,7 @@ requires = [
|
|||||||
"ninja",
|
"ninja",
|
||||||
"packaging",
|
"packaging",
|
||||||
"setuptools >= 49.4.0",
|
"setuptools >= 49.4.0",
|
||||||
"torch >= 2.1.0",
|
"torch == 2.1.2",
|
||||||
"wheel",
|
"wheel",
|
||||||
]
|
]
|
||||||
build-backend = "setuptools.build_meta"
|
build-backend = "setuptools.build_meta"
|
||||||
|
|||||||
@@ -2,5 +2,5 @@
|
|||||||
ninja
|
ninja
|
||||||
packaging
|
packaging
|
||||||
setuptools>=49.4.0
|
setuptools>=49.4.0
|
||||||
torch>=2.1.0
|
torch==2.1.2
|
||||||
wheel
|
wheel
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
# formatting
|
# formatting
|
||||||
yapf==0.32.0
|
yapf==0.32.0
|
||||||
|
toml==0.10.2
|
||||||
ruff==0.1.5
|
ruff==0.1.5
|
||||||
|
|
||||||
# type checking
|
# type checking
|
||||||
|
|||||||
@@ -3,14 +3,10 @@ typing-extensions>=4.8.0
|
|||||||
starlette
|
starlette
|
||||||
psutil
|
psutil
|
||||||
ray >= 2.5.1
|
ray >= 2.5.1
|
||||||
pandas # Required for Ray data.
|
|
||||||
pyarrow # Required for Ray data.
|
|
||||||
sentencepiece # Required for LLaMA tokenizer.
|
sentencepiece # Required for LLaMA tokenizer.
|
||||||
numpy
|
numpy
|
||||||
tokenizers>=0.15.0
|
tokenizers>=0.15.0
|
||||||
huggingface_hub<0.18,>=0.16.4
|
transformers >= 4.36.0 # Required for Mixtral.
|
||||||
einops # Required for phi-1_5
|
|
||||||
transformers >= 4.34.0 # Required for Mistral.
|
|
||||||
fastapi
|
fastapi
|
||||||
uvicorn[standard]
|
uvicorn[standard]
|
||||||
pydantic == 1.10.13 # Required for OpenAI server.
|
pydantic == 1.10.13 # Required for OpenAI server.
|
||||||
|
|||||||
@@ -1,14 +1,11 @@
|
|||||||
ninja # For faster builds.
|
ninja # For faster builds.
|
||||||
psutil
|
psutil
|
||||||
ray >= 2.5.1
|
ray >= 2.5.1
|
||||||
pandas # Required for Ray data.
|
|
||||||
pyarrow # Required for Ray data.
|
|
||||||
sentencepiece # Required for LLaMA tokenizer.
|
sentencepiece # Required for LLaMA tokenizer.
|
||||||
numpy
|
numpy
|
||||||
einops # Required for phi-1_5
|
torch == 2.1.2
|
||||||
torch >= 2.1.0
|
transformers >= 4.36.0 # Required for Mixtral.
|
||||||
transformers >= 4.34.0 # Required for Mistral.
|
xformers == 0.0.23.post1 # Required for CUDA 12.1.
|
||||||
xformers >= 0.0.22.post7 # Required for CUDA 12.1.
|
|
||||||
fastapi
|
fastapi
|
||||||
uvicorn[standard]
|
uvicorn[standard]
|
||||||
pydantic == 1.10.13 # Required for OpenAI server.
|
pydantic == 1.10.13 # Required for OpenAI server.
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
--- /opt/conda/envs/py_3.10/lib/python3.10/site-packages/xformers/ops/fmha/flash.py 2023-11-29 03:17:03.930103539 +0000
|
--- flash_ori.py 2023-12-13 05:43:31.530752623 +0000
|
||||||
+++ flash.py 2023-11-28 16:14:25.206128903 +0000
|
+++ flash_patch.py 2023-12-13 06:00:45.962403104 +0000
|
||||||
@@ -31,39 +31,39 @@
|
@@ -36,44 +36,44 @@
|
||||||
|
|
||||||
FLASH_VERSION = "0.0.0"
|
FLASH_VERSION = "0.0.0"
|
||||||
try:
|
try:
|
||||||
@@ -15,9 +15,12 @@
|
|||||||
- from flash_attn.flash_attn_interface import flash_attn_cuda as _C_flashattention
|
- from flash_attn.flash_attn_interface import flash_attn_cuda as _C_flashattention
|
||||||
-
|
-
|
||||||
- FLASH_VERSION = flash_attn.__version__
|
- FLASH_VERSION = flash_attn.__version__
|
||||||
- flash_ver_parsed = tuple(int(s) for s in FLASH_VERSION.split(".")[:2])
|
- flash_ver_parsed = tuple(int(s) for s in FLASH_VERSION.split(".")[:3])
|
||||||
- if flash_ver_parsed < (2, 3):
|
- if (
|
||||||
- raise ImportError("Requires 2.3 for sliding window support")
|
- flash_ver_parsed != (2, 3, 6)
|
||||||
|
- and os.environ.get("XFORMERS_IGNORE_FLASH_VERSION_CHECK", "0") != "1"
|
||||||
|
- ):
|
||||||
|
- raise ImportError("Requires Flash attention 2.3.6 for varlen_fwd api")
|
||||||
+ #try:
|
+ #try:
|
||||||
+ # from ... import _C_flashattention # type: ignore[attr-defined]
|
+ # from ... import _C_flashattention # type: ignore[attr-defined]
|
||||||
+ # from ..._cpp_lib import _build_metadata
|
+ # from ..._cpp_lib import _build_metadata
|
||||||
@@ -29,35 +32,41 @@
|
|||||||
+ from flash_attn.flash_attn_interface import flash_attn_cuda as _C_flashattention
|
+ from flash_attn.flash_attn_interface import flash_attn_cuda as _C_flashattention
|
||||||
+
|
+
|
||||||
+ FLASH_VERSION = flash_attn.__version__
|
+ FLASH_VERSION = flash_attn.__version__
|
||||||
+ # flash_ver_parsed = tuple(int(s) for s in FLASH_VERSION.split(".")[:2])
|
+ # flash_ver_parsed = tuple(int(s) for s in FLASH_VERSION.split(".")[:3])
|
||||||
+ # if flash_ver_parsed < (2, 3):
|
+ # if (
|
||||||
+ # raise ImportError("Requires 2.3 for sliding window support")
|
+ # flash_ver_parsed != (2, 3, 6)
|
||||||
|
+ # and os.environ.get("XFORMERS_IGNORE_FLASH_VERSION_CHECK", "0") != "1"
|
||||||
|
+ # ):
|
||||||
|
+ # raise ImportError("Requires Flash attention 2.3.6 for varlen_fwd api")
|
||||||
|
|
||||||
# create library so that flash-attn goes through the PyTorch Dispatcher
|
# create library so that flash-attn goes through the PyTorch Dispatcher
|
||||||
- _flash_lib = torch.library.Library("xformers_flash", "DEF")
|
- _flash_lib = torch.library.Library("xformers_flash", "DEF")
|
||||||
+ #_flash_lib = torch.library.Library("xformers_flash", "DEF")
|
-
|
||||||
|
|
||||||
- _flash_lib.define(
|
- _flash_lib.define(
|
||||||
- "flash_fwd(Tensor query, Tensor key, Tensor value, "
|
- "flash_fwd(Tensor query, Tensor key, Tensor value, "
|
||||||
- "Tensor? cu_seqlens_q, Tensor? cu_seqlens_k, "
|
- "Tensor? cu_seqlens_q, Tensor? cu_seqlens_k, Tensor? seqused_k, "
|
||||||
- "int max_seqlen_q, int max_seqlen_k, "
|
- "int max_seqlen_q, int max_seqlen_k, "
|
||||||
- "float p, float softmax_scale, "
|
- "float p, float softmax_scale, "
|
||||||
- "bool is_causal, int window_size, bool return_softmax) -> (Tensor, Tensor, Tensor)"
|
- "bool is_causal, int window_left, "
|
||||||
|
- "int window_right, bool return_softmax) -> (Tensor, Tensor, Tensor)"
|
||||||
- )
|
- )
|
||||||
-
|
+ #_flash_lib = torch.library.Library("xformers_flash", "DEF")
|
||||||
|
|
||||||
- _flash_lib.define(
|
- _flash_lib.define(
|
||||||
- "flash_bwd(Tensor dout, Tensor query, Tensor key, Tensor value, "
|
- "flash_bwd(Tensor dout, Tensor query, Tensor key, Tensor value, "
|
||||||
- "Tensor out, Tensor softmax_lse_, Tensor dq, Tensor dk, Tensor dv, "
|
- "Tensor out, Tensor softmax_lse_, Tensor dq, Tensor dk, Tensor dv, "
|
||||||
- "Tensor cu_seqlens_q, Tensor cu_seqlens_k, "
|
- "Tensor cu_seqlens_q, Tensor cu_seqlens_k, "
|
||||||
- "int max_seqlen_q, int max_seqlen_k, "
|
- "int max_seqlen_q, int max_seqlen_k, "
|
||||||
- "float p, float softmax_scale, bool is_causal, int window_size, Tensor rng_state) -> (Tensor, Tensor, Tensor)"
|
- "float p, float softmax_scale, bool is_causal, "
|
||||||
|
- "int window_left, int window_right, Tensor rng_state) -> (Tensor, Tensor, Tensor)"
|
||||||
- )
|
- )
|
||||||
+ #_flash_lib.define(
|
+ #_flash_lib.define(
|
||||||
+ # "flash_fwd(Tensor query, Tensor key, Tensor value, "
|
+ # "flash_fwd(Tensor query, Tensor key, Tensor value, "
|
||||||
+ # "Tensor? cu_seqlens_q, Tensor? cu_seqlens_k, "
|
+ # "Tensor? cu_seqlens_q, Tensor? cu_seqlens_k, Tensor? seqused_k, "
|
||||||
+ # "int max_seqlen_q, int max_seqlen_k, "
|
+ # "int max_seqlen_q, int max_seqlen_k, "
|
||||||
+ # "float p, float softmax_scale, "
|
+ # "float p, float softmax_scale, "
|
||||||
+ # "bool is_causal, int window_size, bool return_softmax) -> (Tensor, Tensor, Tensor)"
|
+ # "bool is_causal, int window_left, "
|
||||||
|
+ # "int window_right, bool return_softmax) -> (Tensor, Tensor, Tensor)"
|
||||||
+ #)
|
+ #)
|
||||||
+
|
+
|
||||||
+ #_flash_lib.define(
|
+ #_flash_lib.define(
|
||||||
@@ -65,52 +74,61 @@
|
|||||||
+ # "Tensor out, Tensor softmax_lse_, Tensor dq, Tensor dk, Tensor dv, "
|
+ # "Tensor out, Tensor softmax_lse_, Tensor dq, Tensor dk, Tensor dv, "
|
||||||
+ # "Tensor cu_seqlens_q, Tensor cu_seqlens_k, "
|
+ # "Tensor cu_seqlens_q, Tensor cu_seqlens_k, "
|
||||||
+ # "int max_seqlen_q, int max_seqlen_k, "
|
+ # "int max_seqlen_q, int max_seqlen_k, "
|
||||||
+ # "float p, float softmax_scale, bool is_causal, int window_size, Tensor rng_state) -> (Tensor, Tensor, Tensor)"
|
+ # "float p, float softmax_scale, bool is_causal, "
|
||||||
|
+ # "int window_left, int window_right, Tensor rng_state) -> (Tensor, Tensor, Tensor)"
|
||||||
+ #)
|
+ #)
|
||||||
|
|
||||||
def _flash_fwd(
|
def _flash_fwd(
|
||||||
query,
|
query,
|
||||||
@@ -98,8 +98,8 @@
|
@@ -111,8 +111,8 @@
|
||||||
p,
|
p,
|
||||||
softmax_scale,
|
softmax_scale,
|
||||||
is_causal,
|
is_causal,
|
||||||
- window_size - 1, # window_size_left
|
- window_left, # window_size_left
|
||||||
- -1, # window_size_right
|
- window_right, # window_size_right
|
||||||
+ # window_size - 1, # window_size_left
|
+ # window_left, # window_size_left
|
||||||
+ # -1, # window_size_right
|
+ # window_right, # window_size_right
|
||||||
return_softmax,
|
return_softmax,
|
||||||
None, # rng
|
None, # rng
|
||||||
)
|
)
|
||||||
@@ -127,8 +127,8 @@
|
@@ -134,15 +134,15 @@
|
||||||
|
out,
|
||||||
|
cu_seq_lens_q,
|
||||||
|
cu_seq_lens_k,
|
||||||
|
- seqused_k,
|
||||||
|
+ # seqused_k,
|
||||||
|
max_seq_len_q,
|
||||||
|
max_seq_len_k,
|
||||||
|
p,
|
||||||
softmax_scale,
|
softmax_scale,
|
||||||
False,
|
False,
|
||||||
is_causal,
|
is_causal,
|
||||||
- window_size - 1, # window_size_left
|
- window_left,
|
||||||
- -1, # window_size_right
|
- window_right,
|
||||||
+ # window_size - 1, # window_size_left
|
+ # window_left,
|
||||||
+ # -1, # window_size_right
|
+ # window_right,
|
||||||
return_softmax,
|
return_softmax,
|
||||||
None,
|
None,
|
||||||
)
|
)
|
||||||
@@ -169,8 +169,8 @@
|
@@ -184,8 +184,8 @@
|
||||||
p,
|
p,
|
||||||
softmax_scale,
|
softmax_scale,
|
||||||
is_causal,
|
is_causal,
|
||||||
- window_size - 1, # window_size_left
|
- window_left,
|
||||||
- -1, # window_size_right
|
- window_right,
|
||||||
+ # window_size - 1, # window_size_left
|
+ # window_left,
|
||||||
+ # -1, # window_size_right
|
+ # window_right,
|
||||||
None,
|
None,
|
||||||
rng_state,
|
rng_state,
|
||||||
)
|
)
|
||||||
@@ -193,15 +193,15 @@
|
@@ -208,15 +208,15 @@
|
||||||
softmax_scale,
|
softmax_scale,
|
||||||
False, # zero_tensors
|
False, # zero_tensors
|
||||||
is_causal,
|
is_causal,
|
||||||
- window_size - 1, # window_size_left
|
- window_left,
|
||||||
- -1, # window_size_right
|
- window_right,
|
||||||
+ # window_size - 1, # window_size_left
|
+ # window_left,
|
||||||
+ # -1, # window_size_right
|
+ # window_right,
|
||||||
None,
|
None,
|
||||||
rng_state,
|
rng_state,
|
||||||
)
|
)
|
||||||
@@ -123,7 +141,7 @@
|
|||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@@ -348,7 +348,7 @@
|
@@ -400,7 +400,7 @@
|
||||||
implementation.
|
implementation.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
1
setup.py
1
setup.py
@@ -219,6 +219,7 @@ vllm_extension_sources = [
|
|||||||
"csrc/activation_kernels.cu",
|
"csrc/activation_kernels.cu",
|
||||||
"csrc/layernorm_kernels.cu",
|
"csrc/layernorm_kernels.cu",
|
||||||
"csrc/quantization/squeezellm/quant_cuda_kernel.cu",
|
"csrc/quantization/squeezellm/quant_cuda_kernel.cu",
|
||||||
|
"csrc/quantization/gptq/q_gemm.cu",
|
||||||
"csrc/cuda_utils_kernels.cu",
|
"csrc/cuda_utils_kernels.cu",
|
||||||
"csrc/pybind.cpp",
|
"csrc/pybind.cpp",
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -8,11 +8,11 @@ import pytest
|
|||||||
import requests
|
import requests
|
||||||
|
|
||||||
|
|
||||||
def _query_server(prompt: str) -> dict:
|
def _query_server(prompt: str, max_tokens: int = 5) -> dict:
|
||||||
response = requests.post("http://localhost:8000/generate",
|
response = requests.post("http://localhost:8000/generate",
|
||||||
json={
|
json={
|
||||||
"prompt": prompt,
|
"prompt": prompt,
|
||||||
"max_tokens": 100,
|
"max_tokens": max_tokens,
|
||||||
"temperature": 0,
|
"temperature": 0,
|
||||||
"ignore_eos": True
|
"ignore_eos": True
|
||||||
})
|
})
|
||||||
@@ -20,6 +20,10 @@ def _query_server(prompt: str) -> dict:
|
|||||||
return response.json()
|
return response.json()
|
||||||
|
|
||||||
|
|
||||||
|
def _query_server_long(prompt: str) -> dict:
|
||||||
|
return _query_server(prompt, max_tokens=500)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def api_server():
|
def api_server():
|
||||||
script_path = Path(__file__).parent.joinpath(
|
script_path = Path(__file__).parent.joinpath(
|
||||||
@@ -44,13 +48,14 @@ def test_api_server(api_server):
|
|||||||
"""
|
"""
|
||||||
with Pool(32) as pool:
|
with Pool(32) as pool:
|
||||||
# Wait until the server is ready
|
# Wait until the server is ready
|
||||||
prompts = ["Hello world"] * 1
|
prompts = ["warm up"] * 1
|
||||||
result = None
|
result = None
|
||||||
while not result:
|
while not result:
|
||||||
try:
|
try:
|
||||||
for _ in pool.map(_query_server, prompts):
|
for r in pool.map(_query_server, prompts):
|
||||||
|
result = r
|
||||||
break
|
break
|
||||||
except Exception:
|
except requests.exceptions.ConnectionError:
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
# Actual tests start here
|
# Actual tests start here
|
||||||
@@ -63,12 +68,14 @@ def test_api_server(api_server):
|
|||||||
assert num_aborted_requests == 0
|
assert num_aborted_requests == 0
|
||||||
|
|
||||||
# Try with 100 prompts
|
# Try with 100 prompts
|
||||||
prompts = ["Hello world"] * 100
|
prompts = ["test prompt"] * 100
|
||||||
for result in pool.map(_query_server, prompts):
|
for result in pool.map(_query_server, prompts):
|
||||||
assert result
|
assert result
|
||||||
|
|
||||||
|
with Pool(32) as pool:
|
||||||
# Cancel requests
|
# Cancel requests
|
||||||
pool.map_async(_query_server, prompts)
|
prompts = ["canceled requests"] * 100
|
||||||
|
pool.map_async(_query_server_long, prompts)
|
||||||
time.sleep(0.01)
|
time.sleep(0.01)
|
||||||
pool.terminate()
|
pool.terminate()
|
||||||
pool.join()
|
pool.join()
|
||||||
@@ -81,6 +88,6 @@ def test_api_server(api_server):
|
|||||||
# check that server still runs after cancellations
|
# check that server still runs after cancellations
|
||||||
with Pool(32) as pool:
|
with Pool(32) as pool:
|
||||||
# Try with 100 prompts
|
# Try with 100 prompts
|
||||||
prompts = ["Hello world"] * 100
|
prompts = ["test prompt after canceled"] * 100
|
||||||
for result in pool.map(_query_server, prompts):
|
for result in pool.map(_query_server, prompts):
|
||||||
assert result
|
assert result
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import os
|
||||||
from typing import List, Optional, Tuple
|
from typing import List, Optional, Tuple
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
@@ -7,21 +8,33 @@ from transformers import AutoModelForCausalLM
|
|||||||
from vllm import LLM, SamplingParams
|
from vllm import LLM, SamplingParams
|
||||||
from vllm.transformers_utils.tokenizer import get_tokenizer
|
from vllm.transformers_utils.tokenizer import get_tokenizer
|
||||||
|
|
||||||
_TEST_PROMPTS = [
|
_TEST_DIR = os.path.dirname(__file__)
|
||||||
"vLLM is a high-throughput and memory-efficient inference and serving engine for LLMs.",
|
_TEST_PROMPTS = [os.path.join(_TEST_DIR, "prompts", "example.txt")]
|
||||||
"Briefly describe the major milestones in the development of artificial intelligence from 1950 to 2020.",
|
_LONG_PROMPTS = [os.path.join(_TEST_DIR, "prompts", "summary.txt")]
|
||||||
"Compare and contrast artificial intelligence with human intelligence in terms of processing information.",
|
|
||||||
"Describe the basic components of a neural network and how it can be trained.",
|
|
||||||
"Write a short story about a robot that dreams for the first time.",
|
def _read_prompts(filename: str) -> str:
|
||||||
"Analyze the impact of the COVID-19 pandemic on global economic structures and future business models.",
|
prompts = []
|
||||||
"Explain the cultural significance of the Mona Lisa painting, and how its perception might vary in Western versus Eastern societies.",
|
with open(filename, "r") as f:
|
||||||
"Translate the following English sentence into Japanese, French, and Swahili: 'The early bird catches the worm.'",
|
prompt = f.readline()
|
||||||
]
|
prompts.append(prompt)
|
||||||
|
return prompts
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def example_prompts() -> List[str]:
|
def example_prompts() -> List[str]:
|
||||||
return _TEST_PROMPTS
|
prompts = []
|
||||||
|
for filename in _TEST_PROMPTS:
|
||||||
|
prompts += _read_prompts(filename)
|
||||||
|
return prompts
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def example_long_prompts() -> List[str]:
|
||||||
|
prompts = []
|
||||||
|
for filename in _LONG_PROMPTS:
|
||||||
|
prompts += _read_prompts(filename)
|
||||||
|
return prompts
|
||||||
|
|
||||||
|
|
||||||
_STR_DTYPE_TO_TORCH_DTYPE = {
|
_STR_DTYPE_TO_TORCH_DTYPE = {
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import pytest
|
|||||||
import torch
|
import torch
|
||||||
|
|
||||||
from vllm.config import ParallelConfig
|
from vllm.config import ParallelConfig
|
||||||
from vllm.engine.ray_utils import get_open_port
|
from vllm.utils import get_open_port
|
||||||
from vllm.model_executor.parallel_utils.communication_op import (
|
from vllm.model_executor.parallel_utils.communication_op import (
|
||||||
tensor_model_parallel_all_reduce,
|
tensor_model_parallel_all_reduce,
|
||||||
tensor_model_parallel_all_gather,
|
tensor_model_parallel_all_gather,
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ def create_kv_caches(
|
|||||||
head_size: int,
|
head_size: int,
|
||||||
dtype: torch.dtype,
|
dtype: torch.dtype,
|
||||||
seed: int,
|
seed: int,
|
||||||
|
device: str,
|
||||||
) -> Tuple[List[torch.Tensor], List[torch.Tensor]]:
|
) -> Tuple[List[torch.Tensor], List[torch.Tensor]]:
|
||||||
torch.random.manual_seed(seed)
|
torch.random.manual_seed(seed)
|
||||||
torch.cuda.manual_seed(seed)
|
torch.cuda.manual_seed(seed)
|
||||||
@@ -23,7 +24,7 @@ def create_kv_caches(
|
|||||||
for _ in range(num_layers):
|
for _ in range(num_layers):
|
||||||
key_cache = torch.empty(size=key_cache_shape,
|
key_cache = torch.empty(size=key_cache_shape,
|
||||||
dtype=dtype,
|
dtype=dtype,
|
||||||
device='cuda')
|
device=device)
|
||||||
key_cache.uniform_(-scale, scale)
|
key_cache.uniform_(-scale, scale)
|
||||||
key_caches.append(key_cache)
|
key_caches.append(key_cache)
|
||||||
|
|
||||||
@@ -32,7 +33,7 @@ def create_kv_caches(
|
|||||||
for _ in range(num_layers):
|
for _ in range(num_layers):
|
||||||
value_cache = torch.empty(size=value_cache_shape,
|
value_cache = torch.empty(size=value_cache_shape,
|
||||||
dtype=dtype,
|
dtype=dtype,
|
||||||
device='cuda')
|
device=device)
|
||||||
value_cache.uniform_(-scale, scale)
|
value_cache.uniform_(-scale, scale)
|
||||||
value_caches.append(value_cache)
|
value_caches.append(value_cache)
|
||||||
return key_caches, value_caches
|
return key_caches, value_caches
|
||||||
|
|||||||
@@ -7,22 +7,26 @@ DTYPES = [torch.half, torch.bfloat16, torch.float]
|
|||||||
NUM_TOKENS = [7, 83, 2048] # Arbitrary values for testing
|
NUM_TOKENS = [7, 83, 2048] # Arbitrary values for testing
|
||||||
D = [512, 4096, 5120, 13824] # Arbitrary values for testing
|
D = [512, 4096, 5120, 13824] # Arbitrary values for testing
|
||||||
SEEDS = [0]
|
SEEDS = [0]
|
||||||
|
DEVICES = [i for i in range(1 if torch.cuda.device_count() == 1 else 2)]
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("num_tokens", NUM_TOKENS)
|
@pytest.mark.parametrize("num_tokens", NUM_TOKENS)
|
||||||
@pytest.mark.parametrize("d", D)
|
@pytest.mark.parametrize("d", D)
|
||||||
@pytest.mark.parametrize("dtype", DTYPES)
|
@pytest.mark.parametrize("dtype", DTYPES)
|
||||||
@pytest.mark.parametrize("seed", SEEDS)
|
@pytest.mark.parametrize("seed", SEEDS)
|
||||||
|
@pytest.mark.parametrize("device", DEVICES)
|
||||||
@torch.inference_mode()
|
@torch.inference_mode()
|
||||||
def test_silu_and_mul(
|
def test_silu_and_mul(
|
||||||
num_tokens: int,
|
num_tokens: int,
|
||||||
d: int,
|
d: int,
|
||||||
dtype: torch.dtype,
|
dtype: torch.dtype,
|
||||||
seed: int,
|
seed: int,
|
||||||
|
device: int,
|
||||||
) -> None:
|
) -> None:
|
||||||
torch.random.manual_seed(seed)
|
torch.random.manual_seed(seed)
|
||||||
torch.cuda.manual_seed(seed)
|
torch.cuda.manual_seed(seed)
|
||||||
x = torch.randn(num_tokens, 2 * d, dtype=dtype, device="cuda")
|
gpu_id = f"cuda:{device}"
|
||||||
|
x = torch.randn(num_tokens, 2 * d, dtype=dtype, device=gpu_id)
|
||||||
layer = SiluAndMul()
|
layer = SiluAndMul()
|
||||||
out = layer(x)
|
out = layer(x)
|
||||||
ref_out = layer._forward(x)
|
ref_out = layer._forward(x)
|
||||||
@@ -33,16 +37,19 @@ def test_silu_and_mul(
|
|||||||
@pytest.mark.parametrize("d", D)
|
@pytest.mark.parametrize("d", D)
|
||||||
@pytest.mark.parametrize("dtype", DTYPES)
|
@pytest.mark.parametrize("dtype", DTYPES)
|
||||||
@pytest.mark.parametrize("seed", SEEDS)
|
@pytest.mark.parametrize("seed", SEEDS)
|
||||||
|
@pytest.mark.parametrize("device", DEVICES)
|
||||||
@torch.inference_mode()
|
@torch.inference_mode()
|
||||||
def test_gelu_new(
|
def test_gelu_new(
|
||||||
num_tokens: int,
|
num_tokens: int,
|
||||||
d: int,
|
d: int,
|
||||||
dtype: torch.dtype,
|
dtype: torch.dtype,
|
||||||
seed: int,
|
seed: int,
|
||||||
|
device: int,
|
||||||
) -> None:
|
) -> None:
|
||||||
torch.random.manual_seed(seed)
|
torch.random.manual_seed(seed)
|
||||||
torch.cuda.manual_seed(seed)
|
torch.cuda.manual_seed(seed)
|
||||||
x = torch.randn(num_tokens, d, dtype=dtype, device="cuda")
|
gpu_id = f"cuda:{device}"
|
||||||
|
x = torch.randn(num_tokens, d, dtype=dtype, device=gpu_id)
|
||||||
layer = NewGELU()
|
layer = NewGELU()
|
||||||
out = layer(x)
|
out = layer(x)
|
||||||
ref_out = layer._forward(x)
|
ref_out = layer._forward(x)
|
||||||
@@ -53,15 +60,18 @@ def test_gelu_new(
|
|||||||
@pytest.mark.parametrize("d", D)
|
@pytest.mark.parametrize("d", D)
|
||||||
@pytest.mark.parametrize("dtype", DTYPES)
|
@pytest.mark.parametrize("dtype", DTYPES)
|
||||||
@pytest.mark.parametrize("seed", SEEDS)
|
@pytest.mark.parametrize("seed", SEEDS)
|
||||||
|
@pytest.mark.parametrize("device", DEVICES)
|
||||||
def test_gelu_fast(
|
def test_gelu_fast(
|
||||||
num_tokens: int,
|
num_tokens: int,
|
||||||
d: int,
|
d: int,
|
||||||
dtype: torch.dtype,
|
dtype: torch.dtype,
|
||||||
seed: int,
|
seed: int,
|
||||||
|
device: int,
|
||||||
) -> None:
|
) -> None:
|
||||||
torch.random.manual_seed(seed)
|
torch.random.manual_seed(seed)
|
||||||
torch.cuda.manual_seed(seed)
|
torch.cuda.manual_seed(seed)
|
||||||
x = torch.randn(num_tokens, d, dtype=dtype, device="cuda")
|
gpu_id = f"cuda:{device}"
|
||||||
|
x = torch.randn(num_tokens, d, dtype=dtype, device=gpu_id)
|
||||||
layer = FastGELU()
|
layer = FastGELU()
|
||||||
out = layer(x)
|
out = layer(x)
|
||||||
ref_out = layer._forward(x)
|
ref_out = layer._forward(x)
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ HEAD_SIZES = [64, 80, 96, 112, 128, 256]
|
|||||||
BLOCK_SIZES = [16, 32]
|
BLOCK_SIZES = [16, 32]
|
||||||
USE_ALIBI = [False, True]
|
USE_ALIBI = [False, True]
|
||||||
SEEDS = [0]
|
SEEDS = [0]
|
||||||
|
DEVICES = [i for i in range(1 if torch.cuda.device_count() == 1 else 2)]
|
||||||
|
|
||||||
|
|
||||||
def ref_masked_attention(
|
def ref_masked_attention(
|
||||||
@@ -87,7 +88,7 @@ def ref_single_query_cached_kv_attention(
|
|||||||
alibi_bias = None
|
alibi_bias = None
|
||||||
if alibi_slopes is not None:
|
if alibi_slopes is not None:
|
||||||
# Create the ALiBi bias used in the paged attention kernel.
|
# Create the ALiBi bias used in the paged attention kernel.
|
||||||
position_ids = torch.arange(context_len, device="cuda").int()
|
position_ids = torch.arange(context_len, device=query.device).int()
|
||||||
alibi_bias = (position_ids - context_len + 1).float()
|
alibi_bias = (position_ids - context_len + 1).float()
|
||||||
alibi_bias = alibi_slopes.view(-1, 1, 1) * alibi_bias.view(
|
alibi_bias = alibi_slopes.view(-1, 1, 1) * alibi_bias.view(
|
||||||
1, 1, -1)
|
1, 1, -1)
|
||||||
@@ -105,6 +106,7 @@ def ref_single_query_cached_kv_attention(
|
|||||||
@pytest.mark.parametrize("block_size", BLOCK_SIZES)
|
@pytest.mark.parametrize("block_size", BLOCK_SIZES)
|
||||||
@pytest.mark.parametrize("dtype", DTYPES)
|
@pytest.mark.parametrize("dtype", DTYPES)
|
||||||
@pytest.mark.parametrize("seed", SEEDS)
|
@pytest.mark.parametrize("seed", SEEDS)
|
||||||
|
@pytest.mark.parametrize("device", DEVICES)
|
||||||
def test_paged_attention(
|
def test_paged_attention(
|
||||||
kv_cache_factory,
|
kv_cache_factory,
|
||||||
version: str,
|
version: str,
|
||||||
@@ -115,18 +117,19 @@ def test_paged_attention(
|
|||||||
block_size: int,
|
block_size: int,
|
||||||
dtype: torch.dtype,
|
dtype: torch.dtype,
|
||||||
seed: int,
|
seed: int,
|
||||||
|
device: int,
|
||||||
) -> None:
|
) -> None:
|
||||||
random.seed(seed)
|
random.seed(seed)
|
||||||
torch.random.manual_seed(seed)
|
torch.random.manual_seed(seed)
|
||||||
torch.cuda.manual_seed(seed)
|
torch.cuda.manual_seed(seed)
|
||||||
|
gpu_id = f"cuda:{device}"
|
||||||
scale = float(1.0 / (head_size**0.5))
|
scale = float(1.0 / (head_size**0.5))
|
||||||
num_query_heads, num_kv_heads = num_heads
|
num_query_heads, num_kv_heads = num_heads
|
||||||
query = torch.empty(num_seqs,
|
query = torch.empty(num_seqs,
|
||||||
num_query_heads,
|
num_query_heads,
|
||||||
head_size,
|
head_size,
|
||||||
dtype=dtype,
|
dtype=dtype,
|
||||||
device="cuda")
|
device=gpu_id)
|
||||||
query.uniform_(-scale, scale)
|
query.uniform_(-scale, scale)
|
||||||
|
|
||||||
assert num_query_heads % num_kv_heads == 0
|
assert num_query_heads % num_kv_heads == 0
|
||||||
@@ -135,12 +138,12 @@ def test_paged_attention(
|
|||||||
if use_alibi:
|
if use_alibi:
|
||||||
alibi_slopes = torch.randn(num_query_heads,
|
alibi_slopes = torch.randn(num_query_heads,
|
||||||
dtype=torch.float,
|
dtype=torch.float,
|
||||||
device="cuda")
|
device=gpu_id)
|
||||||
|
|
||||||
context_lens = [random.randint(1, MAX_SEQ_LEN) for _ in range(num_seqs)]
|
context_lens = [random.randint(1, MAX_SEQ_LEN) for _ in range(num_seqs)]
|
||||||
context_lens[-1] = MAX_SEQ_LEN
|
context_lens[-1] = MAX_SEQ_LEN
|
||||||
max_context_len = max(context_lens)
|
max_context_len = max(context_lens)
|
||||||
context_lens = torch.tensor(context_lens, dtype=torch.int, device="cuda")
|
context_lens = torch.tensor(context_lens, dtype=torch.int, device=gpu_id)
|
||||||
|
|
||||||
# Create the block tables.
|
# Create the block tables.
|
||||||
max_num_blocks_per_seq = (max_context_len + block_size - 1) // block_size
|
max_num_blocks_per_seq = (max_context_len + block_size - 1) // block_size
|
||||||
@@ -151,12 +154,12 @@ def test_paged_attention(
|
|||||||
for _ in range(max_num_blocks_per_seq)
|
for _ in range(max_num_blocks_per_seq)
|
||||||
]
|
]
|
||||||
block_tables.append(block_table)
|
block_tables.append(block_table)
|
||||||
block_tables = torch.tensor(block_tables, dtype=torch.int, device="cuda")
|
block_tables = torch.tensor(block_tables, dtype=torch.int, device=gpu_id)
|
||||||
|
|
||||||
# Create the KV caches.
|
# Create the KV caches.
|
||||||
key_caches, value_caches = kv_cache_factory(NUM_BLOCKS, block_size, 1,
|
key_caches, value_caches = kv_cache_factory(NUM_BLOCKS, block_size, 1,
|
||||||
num_kv_heads, head_size, dtype,
|
num_kv_heads, head_size, dtype,
|
||||||
seed)
|
seed, gpu_id)
|
||||||
key_cache, value_cache = key_caches[0], value_caches[0]
|
key_cache, value_cache = key_caches[0], value_caches[0]
|
||||||
|
|
||||||
# Call the paged attention kernel.
|
# Call the paged attention kernel.
|
||||||
@@ -249,7 +252,7 @@ def ref_multi_query_kv_attention(
|
|||||||
attn_mask = torch.triu(torch.ones(seq_len, seq_len, dtype=dtype),
|
attn_mask = torch.triu(torch.ones(seq_len, seq_len, dtype=dtype),
|
||||||
diagonal=1)
|
diagonal=1)
|
||||||
attn_mask = attn_mask * torch.finfo(dtype).min
|
attn_mask = attn_mask * torch.finfo(dtype).min
|
||||||
attn_mask = attn_mask.to(dtype=dtype, device="cuda")
|
attn_mask = attn_mask.to(dtype=dtype, device=query.device)
|
||||||
|
|
||||||
ref_output = ref_masked_attention(
|
ref_output = ref_masked_attention(
|
||||||
query[start_idx:end_idx],
|
query[start_idx:end_idx],
|
||||||
@@ -269,6 +272,7 @@ def ref_multi_query_kv_attention(
|
|||||||
@pytest.mark.parametrize("head_size", HEAD_SIZES)
|
@pytest.mark.parametrize("head_size", HEAD_SIZES)
|
||||||
@pytest.mark.parametrize("dtype", DTYPES)
|
@pytest.mark.parametrize("dtype", DTYPES)
|
||||||
@pytest.mark.parametrize("seed", SEEDS)
|
@pytest.mark.parametrize("seed", SEEDS)
|
||||||
|
@pytest.mark.parametrize("device", DEVICES)
|
||||||
@torch.inference_mode()
|
@torch.inference_mode()
|
||||||
def test_multi_query_kv_attention(
|
def test_multi_query_kv_attention(
|
||||||
num_seqs: int,
|
num_seqs: int,
|
||||||
@@ -276,11 +280,12 @@ def test_multi_query_kv_attention(
|
|||||||
head_size: int,
|
head_size: int,
|
||||||
dtype: torch.dtype,
|
dtype: torch.dtype,
|
||||||
seed: int,
|
seed: int,
|
||||||
|
device: int,
|
||||||
) -> None:
|
) -> None:
|
||||||
random.seed(seed)
|
random.seed(seed)
|
||||||
torch.random.manual_seed(seed)
|
torch.random.manual_seed(seed)
|
||||||
torch.cuda.manual_seed(seed)
|
torch.cuda.manual_seed(seed)
|
||||||
|
gpu_id = f"cuda:{device}"
|
||||||
# MAX_SEQ_LEN sometimes causes OOM in the reference implementation.
|
# MAX_SEQ_LEN sometimes causes OOM in the reference implementation.
|
||||||
# As the xformers library is already tested with its own tests, we can use
|
# As the xformers library is already tested with its own tests, we can use
|
||||||
# a smaller MAX_SEQ_LEN here.
|
# a smaller MAX_SEQ_LEN here.
|
||||||
@@ -294,7 +299,7 @@ def test_multi_query_kv_attention(
|
|||||||
num_query_heads + 2 * num_kv_heads,
|
num_query_heads + 2 * num_kv_heads,
|
||||||
head_size,
|
head_size,
|
||||||
dtype=dtype,
|
dtype=dtype,
|
||||||
device="cuda")
|
device=gpu_id)
|
||||||
qkv.uniform_(-scale, scale)
|
qkv.uniform_(-scale, scale)
|
||||||
query, key, value = qkv.split(
|
query, key, value = qkv.split(
|
||||||
[num_query_heads, num_kv_heads, num_kv_heads], dim=1)
|
[num_query_heads, num_kv_heads, num_kv_heads], dim=1)
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ BLOCK_SIZES = [8, 16, 32]
|
|||||||
NUM_BLOCKS = [1024, 36000] # Arbitrary values for testing
|
NUM_BLOCKS = [1024, 36000] # Arbitrary values for testing
|
||||||
NUM_MAPPINGS = [256] # Arbitrary values for testing
|
NUM_MAPPINGS = [256] # Arbitrary values for testing
|
||||||
SEEDS = [0]
|
SEEDS = [0]
|
||||||
|
DEVICES = [i for i in range(1 if torch.cuda.device_count() == 1 else 2)]
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("num_mappings", NUM_MAPPINGS)
|
@pytest.mark.parametrize("num_mappings", NUM_MAPPINGS)
|
||||||
@@ -24,6 +25,7 @@ SEEDS = [0]
|
|||||||
@pytest.mark.parametrize("num_blocks", NUM_BLOCKS)
|
@pytest.mark.parametrize("num_blocks", NUM_BLOCKS)
|
||||||
@pytest.mark.parametrize("dtype", DTYPES)
|
@pytest.mark.parametrize("dtype", DTYPES)
|
||||||
@pytest.mark.parametrize("seed", SEEDS)
|
@pytest.mark.parametrize("seed", SEEDS)
|
||||||
|
@pytest.mark.parametrize("device", DEVICES)
|
||||||
@torch.inference_mode()
|
@torch.inference_mode()
|
||||||
def test_copy_blocks(
|
def test_copy_blocks(
|
||||||
kv_cache_factory,
|
kv_cache_factory,
|
||||||
@@ -35,11 +37,12 @@ def test_copy_blocks(
|
|||||||
num_blocks: int,
|
num_blocks: int,
|
||||||
dtype: torch.dtype,
|
dtype: torch.dtype,
|
||||||
seed: int,
|
seed: int,
|
||||||
|
device: int,
|
||||||
) -> None:
|
) -> None:
|
||||||
random.seed(seed)
|
random.seed(seed)
|
||||||
torch.random.manual_seed(seed)
|
torch.random.manual_seed(seed)
|
||||||
torch.cuda.manual_seed(seed)
|
torch.cuda.manual_seed(seed)
|
||||||
|
gpu_id = f"cuda:{device}"
|
||||||
# Generate random block mappings where each source block is mapped to two
|
# Generate random block mappings where each source block is mapped to two
|
||||||
# destination blocks.
|
# destination blocks.
|
||||||
assert 2 * num_mappings <= num_blocks
|
assert 2 * num_mappings <= num_blocks
|
||||||
@@ -56,7 +59,7 @@ def test_copy_blocks(
|
|||||||
# Create the KV caches.
|
# Create the KV caches.
|
||||||
key_caches, value_caches = kv_cache_factory(num_blocks, block_size,
|
key_caches, value_caches = kv_cache_factory(num_blocks, block_size,
|
||||||
num_layers, num_heads,
|
num_layers, num_heads,
|
||||||
head_size, dtype, seed)
|
head_size, dtype, seed, gpu_id)
|
||||||
|
|
||||||
# Clone the KV caches.
|
# Clone the KV caches.
|
||||||
cloned_key_caches = [key_cache.clone() for key_cache in key_caches]
|
cloned_key_caches = [key_cache.clone() for key_cache in key_caches]
|
||||||
@@ -88,6 +91,7 @@ def test_copy_blocks(
|
|||||||
@pytest.mark.parametrize("num_blocks", NUM_BLOCKS)
|
@pytest.mark.parametrize("num_blocks", NUM_BLOCKS)
|
||||||
@pytest.mark.parametrize("dtype", DTYPES)
|
@pytest.mark.parametrize("dtype", DTYPES)
|
||||||
@pytest.mark.parametrize("seed", SEEDS)
|
@pytest.mark.parametrize("seed", SEEDS)
|
||||||
|
@pytest.mark.parametrize("device", DEVICES)
|
||||||
@torch.inference_mode()
|
@torch.inference_mode()
|
||||||
def test_reshape_and_cache(
|
def test_reshape_and_cache(
|
||||||
kv_cache_factory,
|
kv_cache_factory,
|
||||||
@@ -98,28 +102,29 @@ def test_reshape_and_cache(
|
|||||||
num_blocks: int,
|
num_blocks: int,
|
||||||
dtype: torch.dtype,
|
dtype: torch.dtype,
|
||||||
seed: int,
|
seed: int,
|
||||||
|
device: int,
|
||||||
) -> None:
|
) -> None:
|
||||||
random.seed(seed)
|
random.seed(seed)
|
||||||
torch.random.manual_seed(seed)
|
torch.random.manual_seed(seed)
|
||||||
torch.cuda.manual_seed(seed)
|
torch.cuda.manual_seed(seed)
|
||||||
|
gpu_id = f"cuda:{device}"
|
||||||
# Create a random slot mapping.
|
# Create a random slot mapping.
|
||||||
num_slots = block_size * num_blocks
|
num_slots = block_size * num_blocks
|
||||||
slot_mapping = random.sample(range(num_slots), num_tokens)
|
slot_mapping = random.sample(range(num_slots), num_tokens)
|
||||||
slot_mapping = torch.tensor(slot_mapping, dtype=torch.long, device="cuda")
|
slot_mapping = torch.tensor(slot_mapping, dtype=torch.long, device=gpu_id)
|
||||||
|
|
||||||
qkv = torch.randn(num_tokens,
|
qkv = torch.randn(num_tokens,
|
||||||
3,
|
3,
|
||||||
num_heads,
|
num_heads,
|
||||||
head_size,
|
head_size,
|
||||||
dtype=dtype,
|
dtype=dtype,
|
||||||
device="cuda")
|
device=gpu_id)
|
||||||
_, key, value = qkv.unbind(dim=1)
|
_, key, value = qkv.unbind(dim=1)
|
||||||
|
|
||||||
# Create the KV caches.
|
# Create the KV caches.
|
||||||
key_caches, value_caches = kv_cache_factory(num_blocks, block_size, 1,
|
key_caches, value_caches = kv_cache_factory(num_blocks, block_size, 1,
|
||||||
num_heads, head_size, dtype,
|
num_heads, head_size, dtype,
|
||||||
seed)
|
seed, gpu_id)
|
||||||
key_cache, value_cache = key_caches[0], value_caches[0]
|
key_cache, value_cache = key_caches[0], value_caches[0]
|
||||||
|
|
||||||
# Clone the KV caches.
|
# Clone the KV caches.
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ NUM_TOKENS = [7, 83, 4096] # Arbitrary values for testing
|
|||||||
HIDDEN_SIZES = [768, 5120, 8192] # Arbitrary values for testing
|
HIDDEN_SIZES = [768, 5120, 8192] # Arbitrary values for testing
|
||||||
ADD_RESIDUAL = [False, True]
|
ADD_RESIDUAL = [False, True]
|
||||||
SEEDS = [0]
|
SEEDS = [0]
|
||||||
|
DEVICES = [i for i in range(1 if torch.cuda.device_count() == 1 else 2)]
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("num_tokens", NUM_TOKENS)
|
@pytest.mark.parametrize("num_tokens", NUM_TOKENS)
|
||||||
@@ -15,6 +16,7 @@ SEEDS = [0]
|
|||||||
@pytest.mark.parametrize("add_residual", ADD_RESIDUAL)
|
@pytest.mark.parametrize("add_residual", ADD_RESIDUAL)
|
||||||
@pytest.mark.parametrize("dtype", DTYPES)
|
@pytest.mark.parametrize("dtype", DTYPES)
|
||||||
@pytest.mark.parametrize("seed", SEEDS)
|
@pytest.mark.parametrize("seed", SEEDS)
|
||||||
|
@pytest.mark.parametrize("device", DEVICES)
|
||||||
@torch.inference_mode()
|
@torch.inference_mode()
|
||||||
def test_rms_norm(
|
def test_rms_norm(
|
||||||
num_tokens: int,
|
num_tokens: int,
|
||||||
@@ -22,14 +24,15 @@ def test_rms_norm(
|
|||||||
add_residual: bool,
|
add_residual: bool,
|
||||||
dtype: torch.dtype,
|
dtype: torch.dtype,
|
||||||
seed: int,
|
seed: int,
|
||||||
|
device: int,
|
||||||
) -> None:
|
) -> None:
|
||||||
torch.random.manual_seed(seed)
|
torch.random.manual_seed(seed)
|
||||||
torch.cuda.manual_seed(seed)
|
torch.cuda.manual_seed(seed)
|
||||||
|
gpu_id = f"cuda:{device}"
|
||||||
layer = RMSNorm(hidden_size).to(dtype).cuda()
|
layer = RMSNorm(hidden_size).to(dtype=dtype, device=gpu_id)
|
||||||
layer.weight.data.normal_(mean=1.0, std=0.1)
|
layer.weight.data.normal_(mean=1.0, std=0.1)
|
||||||
scale = 1 / (2 * hidden_size)
|
scale = 1 / (2 * hidden_size)
|
||||||
x = torch.randn(num_tokens, hidden_size, dtype=dtype, device="cuda")
|
x = torch.randn(num_tokens, hidden_size, dtype=dtype, device=gpu_id)
|
||||||
x *= scale
|
x *= scale
|
||||||
residual = torch.randn_like(x) * scale if add_residual else None
|
residual = torch.randn_like(x) * scale if add_residual else None
|
||||||
|
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ NUM_HEADS = [7, 17] # Arbitrary values for testing
|
|||||||
BATCH_SIZES = [1, 5] # Arbitrary values for testing
|
BATCH_SIZES = [1, 5] # Arbitrary values for testing
|
||||||
SEQ_LENS = [11, 8192] # Arbitrary values for testing
|
SEQ_LENS = [11, 8192] # Arbitrary values for testing
|
||||||
SEEDS = [0]
|
SEEDS = [0]
|
||||||
|
DEVICES = [i for i in range(1 if torch.cuda.device_count() == 1 else 2)]
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("is_neox_style", IS_NEOX_STYLE)
|
@pytest.mark.parametrize("is_neox_style", IS_NEOX_STYLE)
|
||||||
@@ -23,6 +24,7 @@ SEEDS = [0]
|
|||||||
@pytest.mark.parametrize("rotary_dim", ROTARY_DIMS)
|
@pytest.mark.parametrize("rotary_dim", ROTARY_DIMS)
|
||||||
@pytest.mark.parametrize("dtype", DTYPES)
|
@pytest.mark.parametrize("dtype", DTYPES)
|
||||||
@pytest.mark.parametrize("seed", SEEDS)
|
@pytest.mark.parametrize("seed", SEEDS)
|
||||||
|
@pytest.mark.parametrize("device", DEVICES)
|
||||||
@torch.inference_mode()
|
@torch.inference_mode()
|
||||||
def test_rotary_embedding(
|
def test_rotary_embedding(
|
||||||
is_neox_style: bool,
|
is_neox_style: bool,
|
||||||
@@ -33,6 +35,7 @@ def test_rotary_embedding(
|
|||||||
rotary_dim: Optional[int],
|
rotary_dim: Optional[int],
|
||||||
dtype: torch.dtype,
|
dtype: torch.dtype,
|
||||||
seed: int,
|
seed: int,
|
||||||
|
device: int,
|
||||||
max_position: int = 8192,
|
max_position: int = 8192,
|
||||||
base: int = 10000,
|
base: int = 10000,
|
||||||
) -> None:
|
) -> None:
|
||||||
@@ -40,20 +43,20 @@ def test_rotary_embedding(
|
|||||||
rotary_dim = head_size
|
rotary_dim = head_size
|
||||||
torch.random.manual_seed(seed)
|
torch.random.manual_seed(seed)
|
||||||
torch.cuda.manual_seed(seed)
|
torch.cuda.manual_seed(seed)
|
||||||
|
gpu_id = f"cuda:{device}"
|
||||||
if rotary_dim is None:
|
if rotary_dim is None:
|
||||||
rotary_dim = head_size
|
rotary_dim = head_size
|
||||||
rope = get_rope(head_size, rotary_dim, max_position, base, is_neox_style)
|
rope = get_rope(head_size, rotary_dim, max_position, base, is_neox_style)
|
||||||
rope = rope.to(dtype).cuda()
|
rope = rope.to(dtype=dtype, device=gpu_id)
|
||||||
|
|
||||||
positions = torch.randint(0,
|
positions = torch.randint(0,
|
||||||
max_position, (batch_size, seq_len),
|
max_position, (batch_size, seq_len),
|
||||||
device="cuda")
|
device=gpu_id)
|
||||||
query = torch.randn(batch_size,
|
query = torch.randn(batch_size,
|
||||||
seq_len,
|
seq_len,
|
||||||
num_heads * head_size,
|
num_heads * head_size,
|
||||||
dtype=dtype,
|
dtype=dtype,
|
||||||
device="cuda")
|
device=gpu_id)
|
||||||
key = torch.randn_like(query)
|
key = torch.randn_like(query)
|
||||||
|
|
||||||
# NOTE(woosuk): The reference implementation should be executed first
|
# NOTE(woosuk): The reference implementation should be executed first
|
||||||
|
|||||||
37
tests/models/test_mistral.py
Normal file
37
tests/models/test_mistral.py
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
"""Compare the outputs of HF and vLLM for Mistral models using greedy sampling.
|
||||||
|
|
||||||
|
Run `pytest tests/models/test_mistral.py --forked`.
|
||||||
|
"""
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
MODELS = [
|
||||||
|
"mistralai/Mistral-7B-Instruct-v0.1",
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("model", MODELS)
|
||||||
|
@pytest.mark.parametrize("dtype", ["bfloat16"])
|
||||||
|
@pytest.mark.parametrize("max_tokens", [128])
|
||||||
|
def test_models(
|
||||||
|
hf_runner,
|
||||||
|
vllm_runner,
|
||||||
|
example_long_prompts,
|
||||||
|
model: str,
|
||||||
|
dtype: str,
|
||||||
|
max_tokens: int,
|
||||||
|
) -> None:
|
||||||
|
hf_model = hf_runner(model, dtype=dtype)
|
||||||
|
hf_outputs = hf_model.generate_greedy(example_long_prompts, max_tokens)
|
||||||
|
del hf_model
|
||||||
|
|
||||||
|
vllm_model = vllm_runner(model, dtype=dtype)
|
||||||
|
vllm_outputs = vllm_model.generate_greedy(example_long_prompts, max_tokens)
|
||||||
|
del vllm_model
|
||||||
|
|
||||||
|
for i in range(len(example_long_prompts)):
|
||||||
|
hf_output_ids, hf_output_str = hf_outputs[i]
|
||||||
|
vllm_output_ids, vllm_output_str = vllm_outputs[i]
|
||||||
|
assert hf_output_str == vllm_output_str, (
|
||||||
|
f"Test{i}:\nHF: {hf_output_str!r}\nvLLM: {vllm_output_str!r}")
|
||||||
|
assert hf_output_ids == vllm_output_ids, (
|
||||||
|
f"Test{i}:\nHF: {hf_output_ids}\nvLLM: {vllm_output_ids}")
|
||||||
@@ -8,6 +8,7 @@ MODELS = [
|
|||||||
"facebook/opt-125m",
|
"facebook/opt-125m",
|
||||||
"meta-llama/Llama-2-7b-hf",
|
"meta-llama/Llama-2-7b-hf",
|
||||||
"mistralai/Mistral-7B-v0.1",
|
"mistralai/Mistral-7B-v0.1",
|
||||||
|
"Deci/DeciLM-7b",
|
||||||
"tiiuae/falcon-7b",
|
"tiiuae/falcon-7b",
|
||||||
"gpt2",
|
"gpt2",
|
||||||
"bigcode/tiny_starcoder_py",
|
"bigcode/tiny_starcoder_py",
|
||||||
@@ -15,12 +16,12 @@ MODELS = [
|
|||||||
"EleutherAI/pythia-70m",
|
"EleutherAI/pythia-70m",
|
||||||
"bigscience/bloom-560m",
|
"bigscience/bloom-560m",
|
||||||
"mosaicml/mpt-7b",
|
"mosaicml/mpt-7b",
|
||||||
"microsoft/phi-1_5",
|
"microsoft/phi-2",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("model", MODELS)
|
@pytest.mark.parametrize("model", MODELS)
|
||||||
@pytest.mark.parametrize("dtype", ["half"])
|
@pytest.mark.parametrize("dtype", ["float"])
|
||||||
@pytest.mark.parametrize("max_tokens", [128])
|
@pytest.mark.parametrize("max_tokens", [128])
|
||||||
def test_models(
|
def test_models(
|
||||||
hf_runner,
|
hf_runner,
|
||||||
|
|||||||
8
tests/prompts/example.txt
Normal file
8
tests/prompts/example.txt
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
vLLM is a high-throughput and memory-efficient inference and serving engine for LLMs.
|
||||||
|
Briefly describe the major milestones in the development of artificial intelligence from 1950 to 2020.
|
||||||
|
Compare and contrast artificial intelligence with human intelligence in terms of processing information.
|
||||||
|
Describe the basic components of a neural network and how it can be trained.
|
||||||
|
Write a short story about a robot that dreams for the first time.
|
||||||
|
Analyze the impact of the COVID-19 pandemic on global economic structures and future business models.
|
||||||
|
Explain the cultural significance of the Mona Lisa painting, and how its perception might vary in Western versus Eastern societies.
|
||||||
|
Translate the following English sentence into Japanese, French, and Swahili: 'The early bird catches the worm.'
|
||||||
1
tests/prompts/summary.txt
Normal file
1
tests/prompts/summary.txt
Normal file
File diff suppressed because one or more lines are too long
@@ -33,8 +33,9 @@ def test_prepare_prompt():
|
|||||||
expected_selected_token_indices.append(selected_token_start_idx +
|
expected_selected_token_indices.append(selected_token_start_idx +
|
||||||
prompt_len - 1)
|
prompt_len - 1)
|
||||||
selected_token_start_idx += max_seq_len
|
selected_token_start_idx += max_seq_len
|
||||||
input_tokens, input_positions, _ = model_runner._prepare_prompt(
|
input_tokens, input_positions, _, return_prompt_lens = (
|
||||||
seq_group_metadata_list)
|
model_runner._prepare_prompt(seq_group_metadata_list))
|
||||||
|
assert return_prompt_lens == prompt_lens
|
||||||
sampling_metadata = model_runner._prepare_sample(seq_group_metadata_list,
|
sampling_metadata = model_runner._prepare_sample(seq_group_metadata_list,
|
||||||
prompt_lens)
|
prompt_lens)
|
||||||
assert input_tokens.shape == (batch_size, max_seq_len)
|
assert input_tokens.shape == (batch_size, max_seq_len)
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ from vllm.entrypoints.llm import LLM
|
|||||||
from vllm.outputs import CompletionOutput, RequestOutput
|
from vllm.outputs import CompletionOutput, RequestOutput
|
||||||
from vllm.sampling_params import SamplingParams
|
from vllm.sampling_params import SamplingParams
|
||||||
|
|
||||||
__version__ = "0.2.4"
|
__version__ = "0.2.7"
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"LLM",
|
"LLM",
|
||||||
|
|||||||
@@ -49,6 +49,12 @@ class ModelConfig:
|
|||||||
output). If None, will be derived from the model.
|
output). If None, will be derived from the model.
|
||||||
quantization: Quantization method that was used to quantize the model
|
quantization: Quantization method that was used to quantize the model
|
||||||
weights. If None, we assume the model weights are not quantized.
|
weights. If None, we assume the model weights are not quantized.
|
||||||
|
enforce_eager: Whether to enforce eager execution. If True, we will
|
||||||
|
disable CUDA graph and always execute the model in eager mode.
|
||||||
|
If False, we will use CUDA graph and eager execution in hybrid.
|
||||||
|
max_context_len_to_capture: Maximum context len covered by CUDA graphs.
|
||||||
|
When a sequence has context length larger than this, we fall back
|
||||||
|
to eager mode.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
@@ -65,6 +71,8 @@ class ModelConfig:
|
|||||||
tokenizer_revision: Optional[str] = None,
|
tokenizer_revision: Optional[str] = None,
|
||||||
max_model_len: Optional[int] = None,
|
max_model_len: Optional[int] = None,
|
||||||
quantization: Optional[str] = None,
|
quantization: Optional[str] = None,
|
||||||
|
enforce_eager: bool = False,
|
||||||
|
max_context_len_to_capture: Optional[int] = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
self.model = model
|
self.model = model
|
||||||
self.tokenizer = tokenizer
|
self.tokenizer = tokenizer
|
||||||
@@ -76,6 +84,8 @@ class ModelConfig:
|
|||||||
self.revision = revision
|
self.revision = revision
|
||||||
self.tokenizer_revision = tokenizer_revision
|
self.tokenizer_revision = tokenizer_revision
|
||||||
self.quantization = quantization
|
self.quantization = quantization
|
||||||
|
self.enforce_eager = enforce_eager
|
||||||
|
self.max_context_len_to_capture = max_context_len_to_capture
|
||||||
|
|
||||||
if os.environ.get("VLLM_USE_MODELSCOPE", "False").lower() == "true":
|
if os.environ.get("VLLM_USE_MODELSCOPE", "False").lower() == "true":
|
||||||
# download model from ModelScope hub,
|
# download model from ModelScope hub,
|
||||||
@@ -95,19 +105,19 @@ class ModelConfig:
|
|||||||
self._verify_load_format()
|
self._verify_load_format()
|
||||||
self._verify_tokenizer_mode()
|
self._verify_tokenizer_mode()
|
||||||
self._verify_quantization()
|
self._verify_quantization()
|
||||||
|
self._verify_cuda_graph()
|
||||||
|
|
||||||
def _verify_load_format(self) -> None:
|
def _verify_load_format(self) -> None:
|
||||||
load_format = self.load_format.lower()
|
load_format = self.load_format.lower()
|
||||||
supported_load_format = [
|
supported_load_format = [
|
||||||
"auto", "pt", "safetensors", "npcache", "dummy"
|
"auto", "pt", "safetensors", "npcache", "dummy"
|
||||||
]
|
]
|
||||||
rocm_not_supported_load_format = ["safetensors"]
|
rocm_not_supported_load_format = []
|
||||||
if load_format not in supported_load_format:
|
if load_format not in supported_load_format:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f"Unknown load format: {self.load_format}. Must be one of "
|
f"Unknown load format: {self.load_format}. Must be one of "
|
||||||
"'auto', 'pt', 'safetensors', 'npcache', or 'dummy'.")
|
"'auto', 'pt', 'safetensors', 'npcache', or 'dummy'.")
|
||||||
if is_hip():
|
if is_hip() and load_format in rocm_not_supported_load_format:
|
||||||
if load_format in ["safetensors"]:
|
|
||||||
rocm_supported_load_format = [
|
rocm_supported_load_format = [
|
||||||
f for f in supported_load_format
|
f for f in supported_load_format
|
||||||
if (f not in rocm_not_supported_load_format)
|
if (f not in rocm_not_supported_load_format)
|
||||||
@@ -116,19 +126,13 @@ class ModelConfig:
|
|||||||
f"load format \'{load_format}\' is not supported in ROCm. "
|
f"load format \'{load_format}\' is not supported in ROCm. "
|
||||||
f"Supported load format are "
|
f"Supported load format are "
|
||||||
f"{rocm_supported_load_format}")
|
f"{rocm_supported_load_format}")
|
||||||
# Force ROCm to load from pt weights if nothing specific is set
|
|
||||||
if load_format == "auto":
|
|
||||||
load_format = "pt"
|
|
||||||
|
|
||||||
# FIXME(woosuk): This is a temporary hack. Support safetensor weights.
|
# TODO: Remove this check once HF updates the pt weights of Mixtral.
|
||||||
architectures = getattr(self.hf_config, "architectures", [])
|
architectures = getattr(self.hf_config, "architectures", [])
|
||||||
if "MixtralForCausalLM" in architectures and load_format != "pt":
|
if "MixtralForCausalLM" in architectures and load_format == "pt":
|
||||||
logger.info(
|
raise ValueError(
|
||||||
"Currently, only 'pt' format is supported for Mixtral. "
|
"Currently, the 'pt' format is not supported for Mixtral. "
|
||||||
"Changing the format to 'pt'. This may re-download the "
|
"Please use the 'safetensors' format instead. ")
|
||||||
"weights if you have downloaded the safetensor weights.")
|
|
||||||
load_format = "pt"
|
|
||||||
|
|
||||||
self.load_format = load_format
|
self.load_format = load_format
|
||||||
|
|
||||||
def _verify_tokenizer_mode(self) -> None:
|
def _verify_tokenizer_mode(self) -> None:
|
||||||
@@ -140,7 +144,7 @@ class ModelConfig:
|
|||||||
self.tokenizer_mode = tokenizer_mode
|
self.tokenizer_mode = tokenizer_mode
|
||||||
|
|
||||||
def _verify_quantization(self) -> None:
|
def _verify_quantization(self) -> None:
|
||||||
supported_quantization = ["awq", "squeezellm"]
|
supported_quantization = ["awq", "gptq", "squeezellm"]
|
||||||
rocm_not_supported_quantization = ["awq"]
|
rocm_not_supported_quantization = ["awq"]
|
||||||
if self.quantization is not None:
|
if self.quantization is not None:
|
||||||
self.quantization = self.quantization.lower()
|
self.quantization = self.quantization.lower()
|
||||||
@@ -172,6 +176,12 @@ class ModelConfig:
|
|||||||
"optimized yet. The speed can be slower than "
|
"optimized yet. The speed can be slower than "
|
||||||
"non-quantized models.")
|
"non-quantized models.")
|
||||||
|
|
||||||
|
def _verify_cuda_graph(self) -> None:
|
||||||
|
if self.max_context_len_to_capture is None:
|
||||||
|
self.max_context_len_to_capture = self.max_model_len
|
||||||
|
self.max_context_len_to_capture = min(self.max_context_len_to_capture,
|
||||||
|
self.max_model_len)
|
||||||
|
|
||||||
def verify_with_parallel_config(
|
def verify_with_parallel_config(
|
||||||
self,
|
self,
|
||||||
parallel_config: "ParallelConfig",
|
parallel_config: "ParallelConfig",
|
||||||
|
|||||||
@@ -103,7 +103,7 @@ class BlockSpaceManager:
|
|||||||
def can_allocate(self, seq_group: SequenceGroup) -> AllocStatus:
|
def can_allocate(self, seq_group: SequenceGroup) -> AllocStatus:
|
||||||
# FIXME(woosuk): Here we assume that all sequences in the group share
|
# FIXME(woosuk): Here we assume that all sequences in the group share
|
||||||
# the same prompt. This may not be true for preempted sequences.
|
# the same prompt. This may not be true for preempted sequences.
|
||||||
seq = seq_group.get_seqs()[0]
|
seq = seq_group.get_seqs(status=SequenceStatus.WAITING)[0]
|
||||||
num_required_blocks = len(seq.logical_token_blocks)
|
num_required_blocks = len(seq.logical_token_blocks)
|
||||||
if self.block_sliding_window is not None:
|
if self.block_sliding_window is not None:
|
||||||
num_required_blocks = min(num_required_blocks,
|
num_required_blocks = min(num_required_blocks,
|
||||||
@@ -122,7 +122,7 @@ class BlockSpaceManager:
|
|||||||
def allocate(self, seq_group: SequenceGroup) -> None:
|
def allocate(self, seq_group: SequenceGroup) -> None:
|
||||||
# NOTE: Here we assume that all sequences in the group have the same
|
# NOTE: Here we assume that all sequences in the group have the same
|
||||||
# prompt.
|
# prompt.
|
||||||
seq = seq_group.get_seqs()[0]
|
seq = seq_group.get_seqs(status=SequenceStatus.WAITING)[0]
|
||||||
|
|
||||||
# Allocate new physical token blocks that will store the prompt tokens.
|
# Allocate new physical token blocks that will store the prompt tokens.
|
||||||
block_table: BlockTable = []
|
block_table: BlockTable = []
|
||||||
@@ -137,7 +137,7 @@ class BlockSpaceManager:
|
|||||||
block_table.append(block)
|
block_table.append(block)
|
||||||
|
|
||||||
# Assign the block table for each sequence.
|
# Assign the block table for each sequence.
|
||||||
for seq in seq_group.get_seqs():
|
for seq in seq_group.get_seqs(status=SequenceStatus.WAITING):
|
||||||
self.block_tables[seq.seq_id] = block_table.copy()
|
self.block_tables[seq.seq_id] = block_table.copy()
|
||||||
|
|
||||||
def can_append_slot(self, seq_group: SequenceGroup) -> bool:
|
def can_append_slot(self, seq_group: SequenceGroup) -> bool:
|
||||||
|
|||||||
@@ -139,15 +139,17 @@ class Scheduler:
|
|||||||
while self.waiting:
|
while self.waiting:
|
||||||
seq_group = self.waiting[0]
|
seq_group = self.waiting[0]
|
||||||
|
|
||||||
assert seq_group.num_seqs() == 1, (
|
waiting_seqs = seq_group.get_seqs(
|
||||||
|
status=SequenceStatus.WAITING)
|
||||||
|
assert len(waiting_seqs) == 1, (
|
||||||
"Waiting sequence group should have only one prompt "
|
"Waiting sequence group should have only one prompt "
|
||||||
"sequence.")
|
"sequence.")
|
||||||
num_prompt_tokens = seq_group.get_seqs()[0].get_len()
|
num_prompt_tokens = waiting_seqs[0].get_len()
|
||||||
if num_prompt_tokens > self.prompt_limit:
|
if num_prompt_tokens > self.prompt_limit:
|
||||||
logger.warning(
|
logger.warning(
|
||||||
f"Input prompt ({num_prompt_tokens} tokens) is too long"
|
f"Input prompt ({num_prompt_tokens} tokens) is too long"
|
||||||
f" and exceeds limit of {self.prompt_limit}")
|
f" and exceeds limit of {self.prompt_limit}")
|
||||||
for seq in seq_group.get_seqs():
|
for seq in waiting_seqs:
|
||||||
seq.status = SequenceStatus.FINISHED_IGNORED
|
seq.status = SequenceStatus.FINISHED_IGNORED
|
||||||
ignored_seq_groups.append(seq_group)
|
ignored_seq_groups.append(seq_group)
|
||||||
self.waiting.pop(0)
|
self.waiting.pop(0)
|
||||||
@@ -161,7 +163,7 @@ class Scheduler:
|
|||||||
logger.warning(
|
logger.warning(
|
||||||
f"Input prompt ({num_prompt_tokens} tokens) is too long"
|
f"Input prompt ({num_prompt_tokens} tokens) is too long"
|
||||||
f" and exceeds the capacity of block_manager")
|
f" and exceeds the capacity of block_manager")
|
||||||
for seq in seq_group.get_seqs():
|
for seq in waiting_seqs:
|
||||||
seq.status = SequenceStatus.FINISHED_IGNORED
|
seq.status = SequenceStatus.FINISHED_IGNORED
|
||||||
ignored_seq_groups.append(seq_group)
|
ignored_seq_groups.append(seq_group)
|
||||||
self.waiting.pop(0)
|
self.waiting.pop(0)
|
||||||
@@ -317,7 +319,7 @@ class Scheduler:
|
|||||||
|
|
||||||
def _allocate(self, seq_group: SequenceGroup) -> None:
|
def _allocate(self, seq_group: SequenceGroup) -> None:
|
||||||
self.block_manager.allocate(seq_group)
|
self.block_manager.allocate(seq_group)
|
||||||
for seq in seq_group.get_seqs():
|
for seq in seq_group.get_seqs(status=SequenceStatus.WAITING):
|
||||||
seq.status = SequenceStatus.RUNNING
|
seq.status = SequenceStatus.RUNNING
|
||||||
|
|
||||||
def _append_slot(
|
def _append_slot(
|
||||||
|
|||||||
@@ -33,6 +33,8 @@ class EngineArgs:
|
|||||||
revision: Optional[str] = None
|
revision: Optional[str] = None
|
||||||
tokenizer_revision: Optional[str] = None
|
tokenizer_revision: Optional[str] = None
|
||||||
quantization: Optional[str] = None
|
quantization: Optional[str] = None
|
||||||
|
enforce_eager: bool = False
|
||||||
|
max_context_len_to_capture: int = 8192
|
||||||
|
|
||||||
def __post_init__(self):
|
def __post_init__(self):
|
||||||
if self.tokenizer is None:
|
if self.tokenizer is None:
|
||||||
@@ -154,11 +156,13 @@ class EngineArgs:
|
|||||||
type=int,
|
type=int,
|
||||||
default=EngineArgs.swap_space,
|
default=EngineArgs.swap_space,
|
||||||
help='CPU swap space size (GiB) per GPU')
|
help='CPU swap space size (GiB) per GPU')
|
||||||
parser.add_argument('--gpu-memory-utilization',
|
parser.add_argument(
|
||||||
|
'--gpu-memory-utilization',
|
||||||
type=float,
|
type=float,
|
||||||
default=EngineArgs.gpu_memory_utilization,
|
default=EngineArgs.gpu_memory_utilization,
|
||||||
help='the percentage of GPU memory to be used for'
|
help='the fraction of GPU memory to be used for '
|
||||||
'the model executor')
|
'the model executor, which can range from 0 to 1.'
|
||||||
|
'If unspecified, will use the default value of 0.9.')
|
||||||
parser.add_argument('--max-num-batched-tokens',
|
parser.add_argument('--max-num-batched-tokens',
|
||||||
type=int,
|
type=int,
|
||||||
default=EngineArgs.max_num_batched_tokens,
|
default=EngineArgs.max_num_batched_tokens,
|
||||||
@@ -179,9 +183,25 @@ class EngineArgs:
|
|||||||
parser.add_argument('--quantization',
|
parser.add_argument('--quantization',
|
||||||
'-q',
|
'-q',
|
||||||
type=str,
|
type=str,
|
||||||
choices=['awq', 'squeezellm', None],
|
choices=['awq', 'gptq', 'squeezellm', None],
|
||||||
default=None,
|
default=None,
|
||||||
help='Method used to quantize the weights')
|
help='Method used to quantize the weights. If '
|
||||||
|
'None, we first check the `quantization_config` '
|
||||||
|
'attribute in the model config file. If that is '
|
||||||
|
'None, we assume the model weights are not '
|
||||||
|
'quantized and use `dtype` to determine the data '
|
||||||
|
'type of the weights.')
|
||||||
|
parser.add_argument('--enforce-eager',
|
||||||
|
action='store_true',
|
||||||
|
help='Always use eager-mode PyTorch. If False, '
|
||||||
|
'will use eager mode and CUDA graph in hybrid '
|
||||||
|
'for maximal performance and flexibility.')
|
||||||
|
parser.add_argument('--max-context-len-to-capture',
|
||||||
|
type=int,
|
||||||
|
default=EngineArgs.max_context_len_to_capture,
|
||||||
|
help='maximum context length covered by CUDA '
|
||||||
|
'graphs. When a sequence has context length '
|
||||||
|
'larger than this, we fall back to eager mode.')
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@@ -200,7 +220,8 @@ class EngineArgs:
|
|||||||
self.download_dir, self.load_format,
|
self.download_dir, self.load_format,
|
||||||
self.dtype, self.seed, self.revision,
|
self.dtype, self.seed, self.revision,
|
||||||
self.tokenizer_revision, self.max_model_len,
|
self.tokenizer_revision, self.max_model_len,
|
||||||
self.quantization)
|
self.quantization, self.enforce_eager,
|
||||||
|
self.max_context_len_to_capture)
|
||||||
cache_config = CacheConfig(self.block_size,
|
cache_config = CacheConfig(self.block_size,
|
||||||
self.gpu_memory_utilization,
|
self.gpu_memory_utilization,
|
||||||
self.swap_space,
|
self.swap_space,
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import asyncio
|
|||||||
import time
|
import time
|
||||||
from functools import partial
|
from functools import partial
|
||||||
from typing import (Any, Dict, Iterable, List, Optional, Set, Tuple, Type,
|
from typing import (Any, Dict, Iterable, List, Optional, Set, Tuple, Type,
|
||||||
Union)
|
Union, AsyncIterator)
|
||||||
|
|
||||||
from vllm.config import ModelConfig
|
from vllm.config import ModelConfig
|
||||||
from vllm.engine.arg_utils import AsyncEngineArgs
|
from vllm.engine.arg_utils import AsyncEngineArgs
|
||||||
@@ -183,50 +183,54 @@ class _AsyncLLMEngine(LLMEngine):
|
|||||||
and updates the scheduler with the model outputs. Finally, it decodes
|
and updates the scheduler with the model outputs. Finally, it decodes
|
||||||
the sequences and returns the newly generated results.
|
the sequences and returns the newly generated results.
|
||||||
"""
|
"""
|
||||||
seq_group_metadata_list, scheduler_outputs, ignored = self._schedule()
|
seq_group_metadata_list, scheduler_outputs = self.scheduler.schedule()
|
||||||
if scheduler_outputs.is_empty():
|
|
||||||
return ignored
|
|
||||||
|
|
||||||
|
if not scheduler_outputs.is_empty():
|
||||||
# Execute the model.
|
# Execute the model.
|
||||||
output = await self._run_workers_async(
|
all_outputs = await self._run_workers_async(
|
||||||
"execute_model",
|
"execute_model",
|
||||||
seq_group_metadata_list=seq_group_metadata_list,
|
driver_kwargs={
|
||||||
blocks_to_swap_in=scheduler_outputs.blocks_to_swap_in,
|
"seq_group_metadata_list": seq_group_metadata_list,
|
||||||
blocks_to_swap_out=scheduler_outputs.blocks_to_swap_out,
|
"blocks_to_swap_in": scheduler_outputs.blocks_to_swap_in,
|
||||||
blocks_to_copy=scheduler_outputs.blocks_to_copy,
|
"blocks_to_swap_out": scheduler_outputs.blocks_to_swap_out,
|
||||||
)
|
"blocks_to_copy": scheduler_outputs.blocks_to_copy,
|
||||||
|
})
|
||||||
|
|
||||||
return self._process_model_outputs(output, scheduler_outputs) + ignored
|
# Only the driver worker returns the sampling results.
|
||||||
|
output = all_outputs[0]
|
||||||
|
else:
|
||||||
|
output = []
|
||||||
|
|
||||||
|
return self._process_model_outputs(output, scheduler_outputs)
|
||||||
|
|
||||||
async def _run_workers_async(
|
async def _run_workers_async(
|
||||||
self,
|
self,
|
||||||
method: str,
|
method: str,
|
||||||
*args,
|
*args,
|
||||||
get_all_outputs: bool = False,
|
driver_args: Optional[List[Any]] = None,
|
||||||
|
driver_kwargs: Optional[Dict[str, Any]] = None,
|
||||||
**kwargs,
|
**kwargs,
|
||||||
) -> Any:
|
) -> Any:
|
||||||
"""Runs the given method on all workers."""
|
"""Runs the given method on all workers."""
|
||||||
coros = []
|
coros = []
|
||||||
for worker in self.workers:
|
|
||||||
if self.parallel_config.worker_use_ray:
|
if driver_args is None:
|
||||||
coros.append(
|
driver_args = args
|
||||||
worker.execute_method.remote(method, *args, **kwargs))
|
if driver_kwargs is None:
|
||||||
else:
|
driver_kwargs = kwargs
|
||||||
executor = getattr(worker, method)
|
|
||||||
|
# Run the driver worker asynchronously.
|
||||||
|
driver_executor = getattr(self.driver_worker, method)
|
||||||
coros.append(asyncio.get_event_loop().run_in_executor(
|
coros.append(asyncio.get_event_loop().run_in_executor(
|
||||||
None, partial(executor, *args, **kwargs)))
|
None, partial(driver_executor, *driver_args, **driver_kwargs)))
|
||||||
|
|
||||||
|
# Run the ray workers asynchronously.
|
||||||
|
for worker in self.workers:
|
||||||
|
coros.append(worker.execute_method.remote(method, *args, **kwargs))
|
||||||
|
|
||||||
all_outputs = await asyncio.gather(*coros)
|
all_outputs = await asyncio.gather(*coros)
|
||||||
|
|
||||||
if get_all_outputs:
|
|
||||||
return all_outputs
|
return all_outputs
|
||||||
|
|
||||||
# Make sure all workers have the same results.
|
|
||||||
output = all_outputs[0]
|
|
||||||
for other_output in all_outputs[1:]:
|
|
||||||
assert output == other_output
|
|
||||||
return output
|
|
||||||
|
|
||||||
|
|
||||||
class AsyncLLMEngine:
|
class AsyncLLMEngine:
|
||||||
"""An asynchronous wrapper for LLMEngine.
|
"""An asynchronous wrapper for LLMEngine.
|
||||||
@@ -405,7 +409,8 @@ class AsyncLLMEngine:
|
|||||||
prompt: Optional[str],
|
prompt: Optional[str],
|
||||||
sampling_params: SamplingParams,
|
sampling_params: SamplingParams,
|
||||||
request_id: str,
|
request_id: str,
|
||||||
prompt_token_ids: Optional[List[int]] = None) -> RequestOutput:
|
prompt_token_ids: Optional[List[int]] = None
|
||||||
|
) -> AsyncIterator[RequestOutput]:
|
||||||
"""Generate outputs for a request.
|
"""Generate outputs for a request.
|
||||||
|
|
||||||
Generate outputs for a request. This method is a coroutine. It adds the
|
Generate outputs for a request. This method is a coroutine. It adds the
|
||||||
@@ -489,13 +494,12 @@ class AsyncLLMEngine:
|
|||||||
engine_configs = engine_args.create_engine_configs()
|
engine_configs = engine_args.create_engine_configs()
|
||||||
parallel_config = engine_configs[2]
|
parallel_config = engine_configs[2]
|
||||||
# Initialize the cluster.
|
# Initialize the cluster.
|
||||||
distributed_init_method, placement_group = initialize_cluster(
|
placement_group = initialize_cluster(parallel_config,
|
||||||
parallel_config, engine_args.engine_use_ray)
|
engine_args.engine_use_ray)
|
||||||
# Create the async LLM engine.
|
# Create the async LLM engine.
|
||||||
engine = cls(parallel_config.worker_use_ray,
|
engine = cls(parallel_config.worker_use_ray,
|
||||||
engine_args.engine_use_ray,
|
engine_args.engine_use_ray,
|
||||||
*engine_configs,
|
*engine_configs,
|
||||||
distributed_init_method,
|
|
||||||
placement_group,
|
placement_group,
|
||||||
log_requests=not engine_args.disable_log_requests,
|
log_requests=not engine_args.disable_log_requests,
|
||||||
log_stats=not engine_args.disable_log_stats,
|
log_stats=not engine_args.disable_log_stats,
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
import copy
|
import copy
|
||||||
|
from collections import defaultdict
|
||||||
|
import os
|
||||||
import time
|
import time
|
||||||
from functools import partial
|
from typing import (TYPE_CHECKING, Any, Dict, Iterable, List, Optional, Tuple,
|
||||||
from typing import TYPE_CHECKING, Any, Iterable, List, Optional, Tuple, Union
|
Union)
|
||||||
|
|
||||||
from vllm.config import (CacheConfig, ModelConfig, ParallelConfig,
|
from vllm.config import (CacheConfig, ModelConfig, ParallelConfig,
|
||||||
SchedulerConfig)
|
SchedulerConfig)
|
||||||
@@ -13,14 +15,12 @@ from vllm.logger import init_logger
|
|||||||
from vllm.outputs import RequestOutput
|
from vllm.outputs import RequestOutput
|
||||||
from vllm.sampling_params import SamplingParams
|
from vllm.sampling_params import SamplingParams
|
||||||
from vllm.sequence import (SamplerOutput, Sequence, SequenceGroup,
|
from vllm.sequence import (SamplerOutput, Sequence, SequenceGroup,
|
||||||
SequenceGroupMetadata, SequenceGroupOutput,
|
SequenceGroupOutput, SequenceOutput, SequenceStatus)
|
||||||
SequenceOutput, SequenceStatus)
|
|
||||||
from vllm.transformers_utils.tokenizer import (detokenize_incrementally,
|
from vllm.transformers_utils.tokenizer import (detokenize_incrementally,
|
||||||
get_tokenizer)
|
get_tokenizer)
|
||||||
from vllm.utils import Counter
|
from vllm.utils import Counter, set_cuda_visible_devices, get_ip, get_open_port
|
||||||
|
|
||||||
if ray:
|
if ray:
|
||||||
from ray.air.util.torch_dist import init_torch_dist_process_group
|
|
||||||
from ray.util.scheduling_strategies import PlacementGroupSchedulingStrategy
|
from ray.util.scheduling_strategies import PlacementGroupSchedulingStrategy
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
@@ -53,8 +53,6 @@ class LLMEngine:
|
|||||||
management.
|
management.
|
||||||
parallel_config: The configuration related to distributed execution.
|
parallel_config: The configuration related to distributed execution.
|
||||||
scheduler_config: The configuration related to the request scheduler.
|
scheduler_config: The configuration related to the request scheduler.
|
||||||
distributed_init_method: The initialization method for distributed
|
|
||||||
execution. See `torch.distributed.init_process_group` for details.
|
|
||||||
placement_group: Ray placement group for distributed execution.
|
placement_group: Ray placement group for distributed execution.
|
||||||
Required for distributed execution.
|
Required for distributed execution.
|
||||||
log_stats: Whether to log statistics.
|
log_stats: Whether to log statistics.
|
||||||
@@ -66,7 +64,6 @@ class LLMEngine:
|
|||||||
cache_config: CacheConfig,
|
cache_config: CacheConfig,
|
||||||
parallel_config: ParallelConfig,
|
parallel_config: ParallelConfig,
|
||||||
scheduler_config: SchedulerConfig,
|
scheduler_config: SchedulerConfig,
|
||||||
distributed_init_method: str,
|
|
||||||
placement_group: Optional["PlacementGroup"],
|
placement_group: Optional["PlacementGroup"],
|
||||||
log_stats: bool,
|
log_stats: bool,
|
||||||
) -> None:
|
) -> None:
|
||||||
@@ -84,6 +81,7 @@ class LLMEngine:
|
|||||||
f"load_format={model_config.load_format}, "
|
f"load_format={model_config.load_format}, "
|
||||||
f"tensor_parallel_size={parallel_config.tensor_parallel_size}, "
|
f"tensor_parallel_size={parallel_config.tensor_parallel_size}, "
|
||||||
f"quantization={model_config.quantization}, "
|
f"quantization={model_config.quantization}, "
|
||||||
|
f"enforce_eager={model_config.enforce_eager}, "
|
||||||
f"seed={model_config.seed})")
|
f"seed={model_config.seed})")
|
||||||
# TODO(woosuk): Print more configs in debug mode.
|
# TODO(woosuk): Print more configs in debug mode.
|
||||||
|
|
||||||
@@ -104,9 +102,13 @@ class LLMEngine:
|
|||||||
|
|
||||||
# Create the parallel GPU workers.
|
# Create the parallel GPU workers.
|
||||||
if self.parallel_config.worker_use_ray:
|
if self.parallel_config.worker_use_ray:
|
||||||
|
# Disable Ray usage stats collection.
|
||||||
|
ray_usage = os.environ.get("RAY_USAGE_STATS_ENABLED", "0")
|
||||||
|
if ray_usage != "1":
|
||||||
|
os.environ["RAY_USAGE_STATS_ENABLED"] = "0"
|
||||||
self._init_workers_ray(placement_group)
|
self._init_workers_ray(placement_group)
|
||||||
else:
|
else:
|
||||||
self._init_workers(distributed_init_method)
|
self._init_workers()
|
||||||
|
|
||||||
# Profile the memory usage and initialize the cache.
|
# Profile the memory usage and initialize the cache.
|
||||||
self._init_cache()
|
self._init_cache()
|
||||||
@@ -121,7 +123,7 @@ class LLMEngine:
|
|||||||
# List of (timestamp, num_tokens)
|
# List of (timestamp, num_tokens)
|
||||||
self.num_generation_tokens: List[Tuple[float, int]] = []
|
self.num_generation_tokens: List[Tuple[float, int]] = []
|
||||||
|
|
||||||
def _init_workers(self, distributed_init_method: str):
|
def _init_workers(self):
|
||||||
# Lazy import the Worker to avoid importing torch.cuda/xformers
|
# Lazy import the Worker to avoid importing torch.cuda/xformers
|
||||||
# before CUDA_VISIBLE_DEVICES is set in the Worker
|
# before CUDA_VISIBLE_DEVICES is set in the Worker
|
||||||
from vllm.worker.worker import Worker
|
from vllm.worker.worker import Worker
|
||||||
@@ -130,70 +132,122 @@ class LLMEngine:
|
|||||||
"Ray is required if parallel_config.world_size > 1.")
|
"Ray is required if parallel_config.world_size > 1.")
|
||||||
|
|
||||||
self.workers: List[Worker] = []
|
self.workers: List[Worker] = []
|
||||||
worker = Worker(
|
distributed_init_method = f"tcp://{get_ip()}:{get_open_port()}"
|
||||||
|
self.driver_worker = Worker(
|
||||||
self.model_config,
|
self.model_config,
|
||||||
self.parallel_config,
|
self.parallel_config,
|
||||||
self.scheduler_config,
|
self.scheduler_config,
|
||||||
0,
|
local_rank=0,
|
||||||
distributed_init_method,
|
rank=0,
|
||||||
)
|
distributed_init_method=distributed_init_method,
|
||||||
self.workers.append(worker)
|
is_driver_worker=True,
|
||||||
self._run_workers(
|
|
||||||
"init_model",
|
|
||||||
get_all_outputs=True,
|
|
||||||
)
|
|
||||||
self._run_workers(
|
|
||||||
"load_model",
|
|
||||||
get_all_outputs=True,
|
|
||||||
max_concurrent_workers=self.parallel_config.
|
|
||||||
max_parallel_loading_workers,
|
|
||||||
)
|
)
|
||||||
|
self._run_workers("init_model")
|
||||||
|
self._run_workers("load_model")
|
||||||
|
|
||||||
def _init_workers_ray(self, placement_group: "PlacementGroup",
|
def _init_workers_ray(self, placement_group: "PlacementGroup",
|
||||||
**ray_remote_kwargs):
|
**ray_remote_kwargs):
|
||||||
# Lazy import the Worker to avoid importing torch.cuda/xformers
|
|
||||||
# before CUDA_VISIBLE_DEVICES is set in the Worker
|
|
||||||
from vllm.worker.worker import Worker
|
|
||||||
|
|
||||||
self.workers: List[Worker] = []
|
|
||||||
for bundle in placement_group.bundle_specs:
|
|
||||||
if not bundle.get("GPU", 0):
|
|
||||||
continue
|
|
||||||
if self.parallel_config.tensor_parallel_size == 1:
|
if self.parallel_config.tensor_parallel_size == 1:
|
||||||
num_gpus = self.cache_config.gpu_memory_utilization
|
num_gpus = self.cache_config.gpu_memory_utilization
|
||||||
else:
|
else:
|
||||||
num_gpus = 1
|
num_gpus = 1
|
||||||
|
|
||||||
|
self.driver_dummy_worker: RayWorkerVllm = None
|
||||||
|
self.workers: List[RayWorkerVllm] = []
|
||||||
|
|
||||||
|
driver_ip = get_ip()
|
||||||
|
for bundle_id, bundle in enumerate(placement_group.bundle_specs):
|
||||||
|
if not bundle.get("GPU", 0):
|
||||||
|
continue
|
||||||
|
scheduling_strategy = PlacementGroupSchedulingStrategy(
|
||||||
|
placement_group=placement_group,
|
||||||
|
placement_group_capture_child_tasks=True,
|
||||||
|
placement_group_bundle_index=bundle_id,
|
||||||
|
)
|
||||||
worker = ray.remote(
|
worker = ray.remote(
|
||||||
num_cpus=0,
|
num_cpus=0,
|
||||||
num_gpus=num_gpus,
|
num_gpus=num_gpus,
|
||||||
scheduling_strategy=PlacementGroupSchedulingStrategy(
|
scheduling_strategy=scheduling_strategy,
|
||||||
placement_group=placement_group,
|
|
||||||
placement_group_capture_child_tasks=True),
|
|
||||||
**ray_remote_kwargs,
|
**ray_remote_kwargs,
|
||||||
)(RayWorkerVllm).remote(self.model_config.trust_remote_code)
|
)(RayWorkerVllm).remote(self.model_config.trust_remote_code)
|
||||||
|
|
||||||
|
worker_ip = ray.get(worker.get_node_ip.remote())
|
||||||
|
if worker_ip == driver_ip and self.driver_dummy_worker is None:
|
||||||
|
# If the worker is on the same node as the driver, we use it
|
||||||
|
# as the resource holder for the driver process.
|
||||||
|
self.driver_dummy_worker = worker
|
||||||
|
else:
|
||||||
self.workers.append(worker)
|
self.workers.append(worker)
|
||||||
|
|
||||||
|
if self.driver_dummy_worker is None:
|
||||||
|
raise ValueError(
|
||||||
|
"Ray does not allocate any GPUs on the driver node. Consider "
|
||||||
|
"adjusting the Ray placement group or running the driver on a "
|
||||||
|
"GPU node.")
|
||||||
|
|
||||||
|
driver_node_id, driver_gpu_ids = ray.get(
|
||||||
|
self.driver_dummy_worker.get_node_and_gpu_ids.remote())
|
||||||
|
worker_node_and_gpu_ids = ray.get(
|
||||||
|
[worker.get_node_and_gpu_ids.remote() for worker in self.workers])
|
||||||
|
|
||||||
|
node_workers = defaultdict(list)
|
||||||
|
node_gpus = defaultdict(list)
|
||||||
|
|
||||||
|
node_workers[driver_node_id].append(0)
|
||||||
|
node_gpus[driver_node_id].extend(driver_gpu_ids)
|
||||||
|
for i, (node_id, gpu_ids) in enumerate(worker_node_and_gpu_ids,
|
||||||
|
start=1):
|
||||||
|
node_workers[node_id].append(i)
|
||||||
|
node_gpus[node_id].extend(gpu_ids)
|
||||||
|
for node_id, gpu_ids in node_gpus.items():
|
||||||
|
node_gpus[node_id] = sorted(gpu_ids)
|
||||||
|
|
||||||
|
# Set CUDA_VISIBLE_DEVICES for the driver.
|
||||||
|
set_cuda_visible_devices(node_gpus[driver_node_id])
|
||||||
|
for worker, (node_id, _) in zip(self.workers, worker_node_and_gpu_ids):
|
||||||
|
worker.set_cuda_visible_devices.remote(node_gpus[node_id])
|
||||||
|
|
||||||
|
distributed_init_method = f"tcp://{driver_ip}:{get_open_port()}"
|
||||||
|
|
||||||
|
# Lazy import the Worker to avoid importing torch.cuda/xformers
|
||||||
|
# before CUDA_VISIBLE_DEVICES is set in the Worker
|
||||||
|
from vllm.worker.worker import Worker
|
||||||
|
|
||||||
# Initialize torch distributed process group for the workers.
|
# Initialize torch distributed process group for the workers.
|
||||||
init_torch_dist_process_group(self.workers, backend="nccl")
|
|
||||||
model_config = copy.deepcopy(self.model_config)
|
model_config = copy.deepcopy(self.model_config)
|
||||||
parallel_config = copy.deepcopy(self.parallel_config)
|
parallel_config = copy.deepcopy(self.parallel_config)
|
||||||
scheduler_config = copy.deepcopy(self.scheduler_config)
|
scheduler_config = copy.deepcopy(self.scheduler_config)
|
||||||
self._run_workers("init_worker",
|
|
||||||
get_all_outputs=True,
|
for rank, (worker, (node_id,
|
||||||
worker_init_fn=lambda: Worker(
|
_)) in enumerate(zip(self.workers,
|
||||||
|
worker_node_and_gpu_ids),
|
||||||
|
start=1):
|
||||||
|
local_rank = node_workers[node_id].index(rank)
|
||||||
|
worker.init_worker.remote(
|
||||||
|
lambda rank=rank, local_rank=local_rank: Worker(
|
||||||
model_config,
|
model_config,
|
||||||
parallel_config,
|
parallel_config,
|
||||||
scheduler_config,
|
scheduler_config,
|
||||||
None,
|
local_rank,
|
||||||
None,
|
rank,
|
||||||
|
distributed_init_method,
|
||||||
))
|
))
|
||||||
self._run_workers(
|
|
||||||
"init_model",
|
driver_rank = 0
|
||||||
get_all_outputs=True,
|
driver_local_rank = node_workers[driver_node_id].index(driver_rank)
|
||||||
|
self.driver_worker = Worker(
|
||||||
|
model_config,
|
||||||
|
parallel_config,
|
||||||
|
scheduler_config,
|
||||||
|
driver_local_rank,
|
||||||
|
driver_rank,
|
||||||
|
distributed_init_method,
|
||||||
|
is_driver_worker=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
self._run_workers("init_model")
|
||||||
self._run_workers(
|
self._run_workers(
|
||||||
"load_model",
|
"load_model",
|
||||||
get_all_outputs=True,
|
|
||||||
max_concurrent_workers=self.parallel_config.
|
max_concurrent_workers=self.parallel_config.
|
||||||
max_parallel_loading_workers,
|
max_parallel_loading_workers,
|
||||||
)
|
)
|
||||||
@@ -207,7 +261,6 @@ class LLMEngine:
|
|||||||
# Get the maximum number of blocks that can be allocated on GPU and CPU.
|
# Get the maximum number of blocks that can be allocated on GPU and CPU.
|
||||||
num_blocks = self._run_workers(
|
num_blocks = self._run_workers(
|
||||||
"profile_num_available_blocks",
|
"profile_num_available_blocks",
|
||||||
get_all_outputs=True,
|
|
||||||
block_size=self.cache_config.block_size,
|
block_size=self.cache_config.block_size,
|
||||||
gpu_memory_utilization=self.cache_config.gpu_memory_utilization,
|
gpu_memory_utilization=self.cache_config.gpu_memory_utilization,
|
||||||
cpu_swap_space=self.cache_config.swap_space_bytes,
|
cpu_swap_space=self.cache_config.swap_space_bytes,
|
||||||
@@ -226,12 +279,23 @@ class LLMEngine:
|
|||||||
raise ValueError("No available memory for the cache blocks. "
|
raise ValueError("No available memory for the cache blocks. "
|
||||||
"Try increasing `gpu_memory_utilization` when "
|
"Try increasing `gpu_memory_utilization` when "
|
||||||
"initializing the engine.")
|
"initializing the engine.")
|
||||||
|
max_seq_len = self.cache_config.block_size * num_gpu_blocks
|
||||||
|
if self.model_config.max_model_len > max_seq_len:
|
||||||
|
raise ValueError(
|
||||||
|
f"The model's max seq len ({self.model_config.max_model_len}) "
|
||||||
|
"is larger than the maximum number of tokens that can be "
|
||||||
|
f"stored in KV cache ({max_seq_len}). Try increasing "
|
||||||
|
"`gpu_memory_utilization` or decreasing `max_model_len` when "
|
||||||
|
"initializing the engine.")
|
||||||
|
|
||||||
self.cache_config.num_gpu_blocks = num_gpu_blocks
|
self.cache_config.num_gpu_blocks = num_gpu_blocks
|
||||||
self.cache_config.num_cpu_blocks = num_cpu_blocks
|
self.cache_config.num_cpu_blocks = num_cpu_blocks
|
||||||
|
|
||||||
# Initialize the cache.
|
# Initialize the cache.
|
||||||
self._run_workers("init_cache_engine", cache_config=self.cache_config)
|
self._run_workers("init_cache_engine", cache_config=self.cache_config)
|
||||||
|
# Warm up the model. This includes capturing the model into CUDA graph
|
||||||
|
# if enforce_eager is False.
|
||||||
|
self._run_workers("warm_up_model")
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_engine_args(cls, engine_args: EngineArgs) -> "LLMEngine":
|
def from_engine_args(cls, engine_args: EngineArgs) -> "LLMEngine":
|
||||||
@@ -240,11 +304,9 @@ class LLMEngine:
|
|||||||
engine_configs = engine_args.create_engine_configs()
|
engine_configs = engine_args.create_engine_configs()
|
||||||
parallel_config = engine_configs[2]
|
parallel_config = engine_configs[2]
|
||||||
# Initialize the cluster.
|
# Initialize the cluster.
|
||||||
distributed_init_method, placement_group = initialize_cluster(
|
placement_group = initialize_cluster(parallel_config)
|
||||||
parallel_config)
|
|
||||||
# Create the LLM engine.
|
# Create the LLM engine.
|
||||||
engine = cls(*engine_configs,
|
engine = cls(*engine_configs,
|
||||||
distributed_init_method,
|
|
||||||
placement_group,
|
placement_group,
|
||||||
log_stats=not engine_args.disable_log_stats)
|
log_stats=not engine_args.disable_log_stats)
|
||||||
return engine
|
return engine
|
||||||
@@ -311,16 +373,6 @@ class LLMEngine:
|
|||||||
"""Returns True if there are unfinished requests."""
|
"""Returns True if there are unfinished requests."""
|
||||||
return self.scheduler.has_unfinished_seqs()
|
return self.scheduler.has_unfinished_seqs()
|
||||||
|
|
||||||
def _schedule(
|
|
||||||
self
|
|
||||||
) -> Tuple[List[SequenceGroupMetadata], SchedulerOutputs,
|
|
||||||
List[RequestOutput]]:
|
|
||||||
seq_group_metadata_list, scheduler_outputs = self.scheduler.schedule()
|
|
||||||
return seq_group_metadata_list, scheduler_outputs, [
|
|
||||||
RequestOutput.from_seq_group(seq_group)
|
|
||||||
for seq_group in scheduler_outputs.ignored_seq_groups
|
|
||||||
]
|
|
||||||
|
|
||||||
def _check_beam_search_early_stopping(
|
def _check_beam_search_early_stopping(
|
||||||
self,
|
self,
|
||||||
early_stopping: Union[bool, str],
|
early_stopping: Union[bool, str],
|
||||||
@@ -569,18 +621,23 @@ class LLMEngine:
|
|||||||
and updates the scheduler with the model outputs. Finally, it decodes
|
and updates the scheduler with the model outputs. Finally, it decodes
|
||||||
the sequences and returns the newly generated results.
|
the sequences and returns the newly generated results.
|
||||||
"""
|
"""
|
||||||
seq_group_metadata_list, scheduler_outputs, ignored = self._schedule()
|
seq_group_metadata_list, scheduler_outputs = self.scheduler.schedule()
|
||||||
if scheduler_outputs.is_empty():
|
|
||||||
return ignored
|
|
||||||
|
|
||||||
|
if not scheduler_outputs.is_empty():
|
||||||
# Execute the model.
|
# Execute the model.
|
||||||
output = self._run_workers(
|
all_outputs = self._run_workers(
|
||||||
"execute_model",
|
"execute_model",
|
||||||
seq_group_metadata_list=seq_group_metadata_list,
|
driver_kwargs={
|
||||||
blocks_to_swap_in=scheduler_outputs.blocks_to_swap_in,
|
"seq_group_metadata_list": seq_group_metadata_list,
|
||||||
blocks_to_swap_out=scheduler_outputs.blocks_to_swap_out,
|
"blocks_to_swap_in": scheduler_outputs.blocks_to_swap_in,
|
||||||
blocks_to_copy=scheduler_outputs.blocks_to_copy,
|
"blocks_to_swap_out": scheduler_outputs.blocks_to_swap_out,
|
||||||
)
|
"blocks_to_copy": scheduler_outputs.blocks_to_copy,
|
||||||
|
})
|
||||||
|
|
||||||
|
# Only the driver worker returns the sampling results.
|
||||||
|
output = all_outputs[0]
|
||||||
|
else:
|
||||||
|
output = []
|
||||||
|
|
||||||
return self._process_model_outputs(output, scheduler_outputs)
|
return self._process_model_outputs(output, scheduler_outputs)
|
||||||
|
|
||||||
@@ -682,6 +739,7 @@ class LLMEngine:
|
|||||||
"""Stop the finished sequences."""
|
"""Stop the finished sequences."""
|
||||||
for stop_str in sampling_params.stop:
|
for stop_str in sampling_params.stop:
|
||||||
if seq.output_text.endswith(stop_str):
|
if seq.output_text.endswith(stop_str):
|
||||||
|
if not sampling_params.include_stop_str_in_output:
|
||||||
# Truncate the output text so that the stop string is
|
# Truncate the output text so that the stop string is
|
||||||
# not included in the output.
|
# not included in the output.
|
||||||
seq.output_text = seq.output_text[:-len(stop_str)]
|
seq.output_text = seq.output_text[:-len(stop_str)]
|
||||||
@@ -707,53 +765,38 @@ class LLMEngine:
|
|||||||
seq.status = SequenceStatus.FINISHED_STOPPED
|
seq.status = SequenceStatus.FINISHED_STOPPED
|
||||||
return
|
return
|
||||||
|
|
||||||
def _run_workers_in_batch(
|
|
||||||
self,
|
|
||||||
workers,
|
|
||||||
method: str,
|
|
||||||
*args,
|
|
||||||
**kwargs,
|
|
||||||
):
|
|
||||||
all_outputs = []
|
|
||||||
for worker in workers:
|
|
||||||
if self.parallel_config.worker_use_ray:
|
|
||||||
executor = partial(worker.execute_method.remote, method)
|
|
||||||
else:
|
|
||||||
executor = getattr(worker, method)
|
|
||||||
|
|
||||||
output = executor(*args, **kwargs)
|
|
||||||
all_outputs.append(output)
|
|
||||||
if self.parallel_config.worker_use_ray:
|
|
||||||
all_outputs = ray.get(all_outputs)
|
|
||||||
return all_outputs
|
|
||||||
|
|
||||||
def _run_workers(
|
def _run_workers(
|
||||||
self,
|
self,
|
||||||
method: str,
|
method: str,
|
||||||
*args,
|
*args,
|
||||||
get_all_outputs: bool = False,
|
driver_args: Optional[List[Any]] = None,
|
||||||
|
driver_kwargs: Optional[Dict[str, Any]] = None,
|
||||||
max_concurrent_workers: Optional[int] = None,
|
max_concurrent_workers: Optional[int] = None,
|
||||||
**kwargs,
|
**kwargs,
|
||||||
) -> Any:
|
) -> Any:
|
||||||
"""Runs the given method on all workers."""
|
"""Runs the given method on all workers."""
|
||||||
all_outputs = []
|
|
||||||
if max_concurrent_workers:
|
if max_concurrent_workers:
|
||||||
work_groups = [
|
raise NotImplementedError(
|
||||||
self.workers[i:i + max_concurrent_workers]
|
"max_concurrent_workers is not supported yet.")
|
||||||
for i in range(0, len(self.workers), max_concurrent_workers)
|
|
||||||
|
# Start the ray workers first.
|
||||||
|
ray_worker_outputs = [
|
||||||
|
worker.execute_method.remote(method, *args, **kwargs)
|
||||||
|
for worker in self.workers
|
||||||
]
|
]
|
||||||
else:
|
|
||||||
work_groups = [self.workers]
|
|
||||||
|
|
||||||
for workers in work_groups:
|
if driver_args is None:
|
||||||
all_outputs.extend(
|
driver_args = args
|
||||||
self._run_workers_in_batch(workers, method, *args, **kwargs))
|
if driver_kwargs is None:
|
||||||
|
driver_kwargs = kwargs
|
||||||
|
|
||||||
if get_all_outputs:
|
# Start the driver worker after all the ray workers.
|
||||||
return all_outputs
|
driver_worker_output = getattr(self.driver_worker,
|
||||||
|
method)(*driver_args, **driver_kwargs)
|
||||||
|
|
||||||
# Make sure all workers have the same results.
|
# Get the results of the ray workers.
|
||||||
output = all_outputs[0]
|
if self.workers:
|
||||||
for other_output in all_outputs[1:]:
|
ray_worker_outputs = ray.get(ray_worker_outputs)
|
||||||
assert output == other_output
|
|
||||||
return output
|
return [driver_worker_output] + ray_worker_outputs
|
||||||
|
|||||||
@@ -1,17 +1,15 @@
|
|||||||
import socket
|
from typing import Optional, List, Tuple, TYPE_CHECKING
|
||||||
from typing import Optional, Tuple, TYPE_CHECKING
|
|
||||||
|
|
||||||
from vllm.config import ParallelConfig
|
from vllm.config import ParallelConfig
|
||||||
from vllm.logger import init_logger
|
from vllm.logger import init_logger
|
||||||
from vllm.utils import is_hip
|
from vllm.utils import is_hip, set_cuda_visible_devices, get_ip
|
||||||
|
|
||||||
logger = init_logger(__name__)
|
logger = init_logger(__name__)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import ray
|
import ray
|
||||||
from ray.air.util.torch_dist import TorchDistributedWorker
|
|
||||||
|
|
||||||
class RayWorkerVllm(TorchDistributedWorker):
|
class RayWorkerVllm:
|
||||||
"""Ray wrapper for vllm.worker.Worker, allowing Worker to be
|
"""Ray wrapper for vllm.worker.Worker, allowing Worker to be
|
||||||
lazliy initialized after Ray sets CUDA_VISIBLE_DEVICES."""
|
lazliy initialized after Ray sets CUDA_VISIBLE_DEVICES."""
|
||||||
|
|
||||||
@@ -31,24 +29,28 @@ try:
|
|||||||
executor = getattr(self, method)
|
executor = getattr(self, method)
|
||||||
return executor(*args, **kwargs)
|
return executor(*args, **kwargs)
|
||||||
|
|
||||||
|
def get_node_ip(self) -> str:
|
||||||
|
return get_ip()
|
||||||
|
|
||||||
|
def get_node_and_gpu_ids(self) -> Tuple[str, List[int]]:
|
||||||
|
node_id = ray.get_runtime_context().get_node_id()
|
||||||
|
gpu_ids = ray.get_gpu_ids()
|
||||||
|
return node_id, gpu_ids
|
||||||
|
|
||||||
|
def set_cuda_visible_devices(self, device_ids) -> None:
|
||||||
|
set_cuda_visible_devices(device_ids)
|
||||||
|
|
||||||
except ImportError as e:
|
except ImportError as e:
|
||||||
logger.warning(f"Failed to import Ray with {e!r}. "
|
logger.warning(f"Failed to import Ray with {e!r}. "
|
||||||
"For distributed inference, please install Ray with "
|
"For distributed inference, please install Ray with "
|
||||||
"`pip install ray pandas pyarrow`.")
|
"`pip install ray pandas pyarrow`.")
|
||||||
ray = None
|
ray = None
|
||||||
TorchDistributedWorker = None
|
|
||||||
RayWorkerVllm = None
|
RayWorkerVllm = None
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from ray.util.placement_group import PlacementGroup
|
from ray.util.placement_group import PlacementGroup
|
||||||
|
|
||||||
|
|
||||||
def get_open_port():
|
|
||||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
|
||||||
s.bind(("", 0))
|
|
||||||
return s.getsockname()[1]
|
|
||||||
|
|
||||||
|
|
||||||
def initialize_cluster(
|
def initialize_cluster(
|
||||||
parallel_config: ParallelConfig,
|
parallel_config: ParallelConfig,
|
||||||
engine_use_ray: bool = False,
|
engine_use_ray: bool = False,
|
||||||
@@ -82,13 +84,11 @@ def initialize_cluster(
|
|||||||
ray.init(address=ray_address, ignore_reinit_error=True)
|
ray.init(address=ray_address, ignore_reinit_error=True)
|
||||||
|
|
||||||
if not parallel_config.worker_use_ray:
|
if not parallel_config.worker_use_ray:
|
||||||
# Initialize cluster locally.
|
assert parallel_config.world_size == 1, (
|
||||||
port = get_open_port()
|
"Ray is required if parallel_config.world_size > 1.")
|
||||||
# We need to setup the distributed init method to make sure
|
return None
|
||||||
# the distributed megatron code (e.g., get world size) works correctly.
|
|
||||||
distributed_init_method = f"tcp://localhost:{port}"
|
|
||||||
return distributed_init_method, None
|
|
||||||
|
|
||||||
|
# Create placement group for worker processes
|
||||||
current_placement_group = ray.util.get_current_placement_group()
|
current_placement_group = ray.util.get_current_placement_group()
|
||||||
if current_placement_group:
|
if current_placement_group:
|
||||||
# We are in a placement group
|
# We are in a placement group
|
||||||
@@ -113,12 +113,12 @@ def initialize_cluster(
|
|||||||
"The number of required GPUs exceeds the total number of "
|
"The number of required GPUs exceeds the total number of "
|
||||||
"available GPUs in the cluster.")
|
"available GPUs in the cluster.")
|
||||||
# Create a new placement group
|
# Create a new placement group
|
||||||
current_placement_group = ray.util.placement_group([{
|
placement_group_specs = ([{"GPU": 1}] * parallel_config.world_size)
|
||||||
"GPU": 1
|
current_placement_group = ray.util.placement_group(
|
||||||
}] * parallel_config.world_size)
|
placement_group_specs)
|
||||||
# Wait until PG is ready - this will block until all
|
# Wait until PG is ready - this will block until all
|
||||||
# requested resources are available, and will timeout
|
# requested resources are available, and will timeout
|
||||||
# if they cannot be provisioned.
|
# if they cannot be provisioned.
|
||||||
ray.get(current_placement_group.ready(), timeout=1800)
|
ray.get(current_placement_group.ready(), timeout=1800)
|
||||||
|
|
||||||
return None, current_placement_group
|
return current_placement_group
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ from vllm.sampling_params import SamplingParams
|
|||||||
from vllm.utils import random_uuid
|
from vllm.utils import random_uuid
|
||||||
|
|
||||||
TIMEOUT_KEEP_ALIVE = 5 # seconds.
|
TIMEOUT_KEEP_ALIVE = 5 # seconds.
|
||||||
TIMEOUT_TO_PREVENT_DEADLOCK = 1 # seconds.
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
engine = None
|
engine = None
|
||||||
|
|
||||||
@@ -73,6 +72,8 @@ if __name__ == "__main__":
|
|||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument("--host", type=str, default=None)
|
parser.add_argument("--host", type=str, default=None)
|
||||||
parser.add_argument("--port", type=int, default=8000)
|
parser.add_argument("--port", type=int, default=8000)
|
||||||
|
parser.add_argument("--ssl-keyfile", type=str, default=None)
|
||||||
|
parser.add_argument("--ssl-certfile", type=str, default=None)
|
||||||
parser = AsyncEngineArgs.add_cli_args(parser)
|
parser = AsyncEngineArgs.add_cli_args(parser)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
@@ -83,4 +84,6 @@ if __name__ == "__main__":
|
|||||||
host=args.host,
|
host=args.host,
|
||||||
port=args.port,
|
port=args.port,
|
||||||
log_level="debug",
|
log_level="debug",
|
||||||
timeout_keep_alive=TIMEOUT_KEEP_ALIVE)
|
timeout_keep_alive=TIMEOUT_KEEP_ALIVE,
|
||||||
|
ssl_keyfile=args.ssl_keyfile,
|
||||||
|
ssl_certfile=args.ssl_certfile)
|
||||||
|
|||||||
@@ -38,8 +38,10 @@ class LLM:
|
|||||||
However, if the `torch_dtype` in the config is `float32`, we will
|
However, if the `torch_dtype` in the config is `float32`, we will
|
||||||
use `float16` instead.
|
use `float16` instead.
|
||||||
quantization: The method used to quantize the model weights. Currently,
|
quantization: The method used to quantize the model weights. Currently,
|
||||||
we support "awq". If None, we assume the model weights are not
|
we support "awq", "gptq" and "squeezellm". If None, we first check
|
||||||
quantized and use `dtype` to determine the data type of the weights.
|
the `quantization_config` attribute in the model config file. If
|
||||||
|
that is None, we assume the model weights are not quantized and use
|
||||||
|
`dtype` to determine the data type of the weights.
|
||||||
revision: The specific model version to use. It can be a branch name,
|
revision: The specific model version to use. It can be a branch name,
|
||||||
a tag name, or a commit id.
|
a tag name, or a commit id.
|
||||||
tokenizer_revision: The specific tokenizer version to use. It can be a
|
tokenizer_revision: The specific tokenizer version to use. It can be a
|
||||||
@@ -55,6 +57,12 @@ class LLM:
|
|||||||
when their `best_of` sampling parameters are larger than 1. If all
|
when their `best_of` sampling parameters are larger than 1. If all
|
||||||
requests will have `best_of=1`, you can safely set this to 0.
|
requests will have `best_of=1`, you can safely set this to 0.
|
||||||
Otherwise, too small values may cause out-of-memory (OOM) errors.
|
Otherwise, too small values may cause out-of-memory (OOM) errors.
|
||||||
|
enforce_eager: Whether to enforce eager execution. If True, we will
|
||||||
|
disable CUDA graph and always execute the model in eager mode.
|
||||||
|
If False, we will use CUDA graph and eager execution in hybrid.
|
||||||
|
max_context_len_to_capture: Maximum context len covered by CUDA graphs.
|
||||||
|
When a sequence has context length larger than this, we fall back
|
||||||
|
to eager mode.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
@@ -71,6 +79,8 @@ class LLM:
|
|||||||
seed: int = 0,
|
seed: int = 0,
|
||||||
gpu_memory_utilization: float = 0.9,
|
gpu_memory_utilization: float = 0.9,
|
||||||
swap_space: int = 4,
|
swap_space: int = 4,
|
||||||
|
enforce_eager: bool = False,
|
||||||
|
max_context_len_to_capture: int = 8192,
|
||||||
**kwargs,
|
**kwargs,
|
||||||
) -> None:
|
) -> None:
|
||||||
if "disable_log_stats" not in kwargs:
|
if "disable_log_stats" not in kwargs:
|
||||||
@@ -88,6 +98,8 @@ class LLM:
|
|||||||
seed=seed,
|
seed=seed,
|
||||||
gpu_memory_utilization=gpu_memory_utilization,
|
gpu_memory_utilization=gpu_memory_utilization,
|
||||||
swap_space=swap_space,
|
swap_space=swap_space,
|
||||||
|
enforce_eager=enforce_eager,
|
||||||
|
max_context_len_to_capture=max_context_len_to_capture,
|
||||||
**kwargs,
|
**kwargs,
|
||||||
)
|
)
|
||||||
self.llm_engine = LLMEngine.from_engine_args(engine_args)
|
self.llm_engine = LLMEngine.from_engine_args(engine_args)
|
||||||
|
|||||||
@@ -80,6 +80,14 @@ def parse_args():
|
|||||||
default="assistant",
|
default="assistant",
|
||||||
help="The role name to return if "
|
help="The role name to return if "
|
||||||
"`request.add_generation_prompt=true`.")
|
"`request.add_generation_prompt=true`.")
|
||||||
|
parser.add_argument("--ssl-keyfile",
|
||||||
|
type=str,
|
||||||
|
default=None,
|
||||||
|
help="The file path to the SSL key file")
|
||||||
|
parser.add_argument("--ssl-certfile",
|
||||||
|
type=str,
|
||||||
|
default=None,
|
||||||
|
help="The file path to the SSL cert file")
|
||||||
|
|
||||||
parser = AsyncEngineArgs.add_cli_args(parser)
|
parser = AsyncEngineArgs.add_cli_args(parser)
|
||||||
return parser.parse_args()
|
return parser.parse_args()
|
||||||
@@ -744,4 +752,6 @@ if __name__ == "__main__":
|
|||||||
host=args.host,
|
host=args.host,
|
||||||
port=args.port,
|
port=args.port,
|
||||||
log_level="info",
|
log_level="info",
|
||||||
timeout_keep_alive=TIMEOUT_KEEP_ALIVE)
|
timeout_keep_alive=TIMEOUT_KEEP_ALIVE,
|
||||||
|
ssl_keyfile=args.ssl_keyfile,
|
||||||
|
ssl_certfile=args.ssl_certfile)
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
from typing import List, Optional
|
from typing import Optional
|
||||||
|
|
||||||
import torch
|
import torch
|
||||||
|
|
||||||
@@ -16,27 +16,29 @@ class InputMetadata:
|
|||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
prompt_lens: List[int],
|
is_prompt: bool,
|
||||||
slot_mapping: torch.Tensor,
|
slot_mapping: torch.Tensor,
|
||||||
max_context_len: Optional[int],
|
max_context_len: Optional[int],
|
||||||
context_lens: Optional[torch.Tensor],
|
context_lens: Optional[torch.Tensor],
|
||||||
block_tables: Optional[torch.Tensor],
|
block_tables: Optional[torch.Tensor],
|
||||||
|
use_cuda_graph: bool,
|
||||||
) -> None:
|
) -> None:
|
||||||
self.prompt_lens = prompt_lens
|
self.is_prompt = is_prompt
|
||||||
self.max_context_len = max_context_len
|
self.max_context_len = max_context_len
|
||||||
self.slot_mapping = slot_mapping
|
self.slot_mapping = slot_mapping
|
||||||
self.context_lens = context_lens
|
self.context_lens = context_lens
|
||||||
self.block_tables = block_tables
|
self.block_tables = block_tables
|
||||||
|
self.use_cuda_graph = use_cuda_graph
|
||||||
|
|
||||||
self.is_prompt = len(prompt_lens) > 0
|
|
||||||
# Set during the execution of the first attention op.
|
# Set during the execution of the first attention op.
|
||||||
# FIXME(woosuk): This is a hack.
|
# FIXME(woosuk): This is a hack.
|
||||||
self.attn_bias = None
|
self.attn_bias = None
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return ("InputMetadata("
|
return ("InputMetadata("
|
||||||
f"prompt_lens={self.prompt_lens}, "
|
f"is_prompt={self.is_prompt}, "
|
||||||
f"max_context_len={self.max_context_len}, "
|
f"max_context_len={self.max_context_len}, "
|
||||||
f"slot_mapping={self.slot_mapping}, "
|
f"slot_mapping={self.slot_mapping}, "
|
||||||
f"context_lens={self.context_lens}, "
|
f"context_lens={self.context_lens}, "
|
||||||
f"block_tables={self.block_tables})")
|
f"block_tables={self.block_tables}, "
|
||||||
|
f"use_cuda_graph={self.use_cuda_graph})")
|
||||||
|
|||||||
@@ -24,13 +24,10 @@ class PagedAttention(nn.Module):
|
|||||||
can either contain prompt tokens or generation tokens.
|
can either contain prompt tokens or generation tokens.
|
||||||
The class does the following:
|
The class does the following:
|
||||||
|
|
||||||
1. Wait for the cache operations (e.g., swap, copy) to finish. The cache
|
1. Reshape and store the input key and value tensors in the KV cache.
|
||||||
operations are issued by the cache engine before executing the forward
|
2. Perform (multi-head/multi-query/grouped-query) attention using either
|
||||||
pass of the model, and they are executed asynchronously.
|
|
||||||
2. Reshape and store the input key and value tensors in the KV cache.
|
|
||||||
3. Perform (multi-head/multi-query/grouped-query) attention using either
|
|
||||||
xformers or the PagedAttention custom op.
|
xformers or the PagedAttention custom op.
|
||||||
4. Return the output tensor.
|
3. Return the output tensor.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
@@ -67,7 +64,6 @@ class PagedAttention(nn.Module):
|
|||||||
key_cache: Optional[torch.Tensor],
|
key_cache: Optional[torch.Tensor],
|
||||||
value_cache: Optional[torch.Tensor],
|
value_cache: Optional[torch.Tensor],
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_event: Optional[torch.cuda.Event],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
"""PagedAttention forward pass.
|
"""PagedAttention forward pass.
|
||||||
|
|
||||||
@@ -80,7 +76,6 @@ class PagedAttention(nn.Module):
|
|||||||
value_cache: shape = [num_blocks, num_kv_heads, head_size,
|
value_cache: shape = [num_blocks, num_kv_heads, head_size,
|
||||||
block_size]
|
block_size]
|
||||||
input_metadata: metadata for the inputs.
|
input_metadata: metadata for the inputs.
|
||||||
cache_event: event to wait for the cache operations to finish.
|
|
||||||
Returns:
|
Returns:
|
||||||
shape = [batch_size, seq_len, num_heads * head_size]
|
shape = [batch_size, seq_len, num_heads * head_size]
|
||||||
"""
|
"""
|
||||||
@@ -89,10 +84,6 @@ class PagedAttention(nn.Module):
|
|||||||
query = query.view(-1, self.num_heads, self.head_size)
|
query = query.view(-1, self.num_heads, self.head_size)
|
||||||
key = key.view(-1, self.num_kv_heads, self.head_size)
|
key = key.view(-1, self.num_kv_heads, self.head_size)
|
||||||
value = value.view(-1, self.num_kv_heads, self.head_size)
|
value = value.view(-1, self.num_kv_heads, self.head_size)
|
||||||
slot_mapping = input_metadata.slot_mapping.flatten()
|
|
||||||
|
|
||||||
if cache_event is not None:
|
|
||||||
cache_event.wait()
|
|
||||||
|
|
||||||
# Reshape the keys and values and store them in the cache.
|
# Reshape the keys and values and store them in the cache.
|
||||||
# If key_cache and value_cache are not provided, the new key and value
|
# If key_cache and value_cache are not provided, the new key and value
|
||||||
@@ -104,7 +95,7 @@ class PagedAttention(nn.Module):
|
|||||||
value,
|
value,
|
||||||
key_cache,
|
key_cache,
|
||||||
value_cache,
|
value_cache,
|
||||||
slot_mapping,
|
input_metadata.slot_mapping.flatten(),
|
||||||
)
|
)
|
||||||
|
|
||||||
if input_metadata.is_prompt:
|
if input_metadata.is_prompt:
|
||||||
@@ -138,7 +129,8 @@ class PagedAttention(nn.Module):
|
|||||||
input_metadata.attn_bias = attn_bias
|
input_metadata.attn_bias = attn_bias
|
||||||
else:
|
else:
|
||||||
input_metadata.attn_bias = _make_alibi_bias(
|
input_metadata.attn_bias = _make_alibi_bias(
|
||||||
self.alibi_slopes, batch_size, seq_len, query.dtype)
|
self.alibi_slopes, self.num_kv_heads, batch_size,
|
||||||
|
seq_len, query.dtype)
|
||||||
|
|
||||||
# TODO(woosuk): Too many view operations. Let's try to reduce them
|
# TODO(woosuk): Too many view operations. Let's try to reduce them
|
||||||
# in the future for code readability.
|
# in the future for code readability.
|
||||||
@@ -164,6 +156,7 @@ class PagedAttention(nn.Module):
|
|||||||
output = out.view_as(query)
|
output = out.view_as(query)
|
||||||
else:
|
else:
|
||||||
# Decoding run.
|
# Decoding run.
|
||||||
|
if key_cache is not None and value_cache is not None:
|
||||||
output = _paged_attention(
|
output = _paged_attention(
|
||||||
query,
|
query,
|
||||||
key_cache,
|
key_cache,
|
||||||
@@ -173,6 +166,10 @@ class PagedAttention(nn.Module):
|
|||||||
self.scale,
|
self.scale,
|
||||||
self.alibi_slopes,
|
self.alibi_slopes,
|
||||||
)
|
)
|
||||||
|
else:
|
||||||
|
# This happens during the initial memory profiling run for
|
||||||
|
# CUDA graphs.
|
||||||
|
output = torch.zeros_like(query)
|
||||||
|
|
||||||
# Reshape the output tensor.
|
# Reshape the output tensor.
|
||||||
return output.view(batch_size, seq_len, hidden_size)
|
return output.view(batch_size, seq_len, hidden_size)
|
||||||
@@ -180,31 +177,34 @@ class PagedAttention(nn.Module):
|
|||||||
|
|
||||||
def _make_alibi_bias(
|
def _make_alibi_bias(
|
||||||
alibi_slopes: torch.Tensor,
|
alibi_slopes: torch.Tensor,
|
||||||
|
num_kv_heads: int,
|
||||||
batch_size: int,
|
batch_size: int,
|
||||||
seq_len: int,
|
seq_len: int,
|
||||||
dtype: torch.dtype,
|
dtype: torch.dtype,
|
||||||
) -> LowerTriangularMaskWithTensorBias:
|
) -> LowerTriangularMaskWithTensorBias:
|
||||||
bias = torch.arange(seq_len, dtype=dtype)
|
bias = torch.arange(seq_len, dtype=dtype, device="cuda")
|
||||||
# NOTE(zhuohan): HF uses
|
# NOTE(zhuohan): HF uses
|
||||||
# `bias = bias[None, :].repeat(prompt_len, 1)`
|
# `bias = bias[None, :].repeat(prompt_len, 1)`
|
||||||
# here. We find that both biases give the same results, but
|
# here. We find that both biases give the same results, but
|
||||||
# the bias below more accurately follows the original ALiBi
|
# the bias below more accurately follows the original ALiBi
|
||||||
# paper.
|
# paper.
|
||||||
bias = bias[None, :] - bias[:, None]
|
bias = bias[None, :] - bias[:, None]
|
||||||
bias = bias.to(alibi_slopes.device)
|
|
||||||
|
|
||||||
# When using custom attention bias, xformers requires the bias to
|
# When using custom attention bias, xformers requires the bias to
|
||||||
# be sliced from a tensor whose length is a multiple of 8.
|
# be sliced from a tensor whose length is a multiple of 8.
|
||||||
padded_len = (seq_len + 7) // 8 * 8
|
padded_len = (seq_len + 7) // 8 * 8
|
||||||
|
num_heads = alibi_slopes.shape[0]
|
||||||
bias = torch.empty(
|
bias = torch.empty(
|
||||||
batch_size,
|
batch_size,
|
||||||
alibi_slopes.shape[0],
|
num_heads,
|
||||||
seq_len,
|
seq_len,
|
||||||
padded_len,
|
padded_len,
|
||||||
device=alibi_slopes.device,
|
device=alibi_slopes.device,
|
||||||
dtype=dtype,
|
dtype=dtype,
|
||||||
)[:, :, :, :seq_len].copy_(bias)
|
)[:, :, :, :seq_len].copy_(bias)
|
||||||
bias.mul_(alibi_slopes[:, None, None])
|
bias.mul_(alibi_slopes[:, None, None])
|
||||||
|
if num_heads != num_kv_heads:
|
||||||
|
bias = bias.unflatten(1, (num_kv_heads, num_heads // num_kv_heads))
|
||||||
attn_bias = LowerTriangularMaskWithTensorBias(bias)
|
attn_bias = LowerTriangularMaskWithTensorBias(bias)
|
||||||
return attn_bias
|
return attn_bias
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
from typing import Dict, List, Optional
|
from typing import Any, Dict, List, Optional
|
||||||
|
|
||||||
import torch
|
import torch
|
||||||
import torch.nn.functional as F
|
import torch.nn.functional as F
|
||||||
@@ -21,8 +21,10 @@ class LinearMethodBase(ABC):
|
|||||||
"""Base class for different (maybe quantized) linear methods."""
|
"""Base class for different (maybe quantized) linear methods."""
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def create_weights(self, input_size: int, output_size: int,
|
def create_weights(self, input_size_per_partition: int,
|
||||||
params_dtype: torch.dtype) -> Dict[str, torch.Tensor]:
|
output_size_per_partition: int, input_size: int,
|
||||||
|
output_size: int,
|
||||||
|
params_dtype: torch.dtype) -> Dict[str, Any]:
|
||||||
"""Create weights for a linear layer."""
|
"""Create weights for a linear layer."""
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
@@ -46,10 +48,12 @@ class UnquantizedLinearMethod(LinearMethodBase):
|
|||||||
def __init__(self, separate_bias_add: bool = False):
|
def __init__(self, separate_bias_add: bool = False):
|
||||||
self.separate_bias_add = separate_bias_add
|
self.separate_bias_add = separate_bias_add
|
||||||
|
|
||||||
def create_weights(self, input_size: int, output_size: int,
|
def create_weights(self, input_size_per_partition: int,
|
||||||
params_dtype: torch.dtype) -> Dict[str, torch.Tensor]:
|
output_size_per_partition: int, input_size: int,
|
||||||
weight = Parameter(torch.empty(output_size,
|
output_size: int,
|
||||||
input_size,
|
params_dtype: torch.dtype) -> Dict[str, Any]:
|
||||||
|
weight = Parameter(torch.empty(output_size_per_partition,
|
||||||
|
input_size_per_partition,
|
||||||
device=torch.cuda.current_device(),
|
device=torch.cuda.current_device(),
|
||||||
dtype=params_dtype),
|
dtype=params_dtype),
|
||||||
requires_grad=False)
|
requires_grad=False)
|
||||||
@@ -102,8 +106,10 @@ class ReplicatedLinear(torch.nn.Module):
|
|||||||
linear_method = UnquantizedLinearMethod()
|
linear_method = UnquantizedLinearMethod()
|
||||||
self.linear_method = linear_method
|
self.linear_method = linear_method
|
||||||
self.linear_weights = self.linear_method.create_weights(
|
self.linear_weights = self.linear_method.create_weights(
|
||||||
self.input_size, self.output_size, self.params_dtype)
|
self.input_size, self.output_size, self.input_size,
|
||||||
|
self.output_size, self.params_dtype)
|
||||||
for name, weight in self.linear_weights.items():
|
for name, weight in self.linear_weights.items():
|
||||||
|
if isinstance(weight, torch.Tensor):
|
||||||
self.register_parameter(name, weight)
|
self.register_parameter(name, weight)
|
||||||
if bias:
|
if bias:
|
||||||
self.bias = Parameter(
|
self.bias = Parameter(
|
||||||
@@ -168,8 +174,10 @@ class ColumnParallelLinear(torch.nn.Module):
|
|||||||
linear_method = UnquantizedLinearMethod()
|
linear_method = UnquantizedLinearMethod()
|
||||||
self.linear_method = linear_method
|
self.linear_method = linear_method
|
||||||
self.linear_weights = self.linear_method.create_weights(
|
self.linear_weights = self.linear_method.create_weights(
|
||||||
self.input_size, self.output_size_per_partition, self.params_dtype)
|
self.input_size, self.output_size_per_partition, self.input_size,
|
||||||
|
self.output_size, self.params_dtype)
|
||||||
for name, weight in self.linear_weights.items():
|
for name, weight in self.linear_weights.items():
|
||||||
|
if isinstance(weight, torch.Tensor):
|
||||||
self.register_parameter(name, weight)
|
self.register_parameter(name, weight)
|
||||||
set_weight_attrs(weight, {"weight_loader": self.weight_loader})
|
set_weight_attrs(weight, {"weight_loader": self.weight_loader})
|
||||||
if bias:
|
if bias:
|
||||||
@@ -295,6 +303,8 @@ class MergedColumnParallelLinear(ColumnParallelLinear):
|
|||||||
loaded_weight = loaded_weight.narrow(output_dim, start_idx,
|
loaded_weight = loaded_weight.narrow(output_dim, start_idx,
|
||||||
shard_size)
|
shard_size)
|
||||||
else:
|
else:
|
||||||
|
ignore_warning = getattr(param, "ignore_warning", False)
|
||||||
|
if not ignore_warning:
|
||||||
logger.warning(
|
logger.warning(
|
||||||
"Loading a weight without `output_dim` attribute in "
|
"Loading a weight without `output_dim` attribute in "
|
||||||
"MergedColumnParallelLinear, assume the weight is "
|
"MergedColumnParallelLinear, assume the weight is "
|
||||||
@@ -418,6 +428,8 @@ class QKVParallelLinear(ColumnParallelLinear):
|
|||||||
loaded_weight = loaded_weight.narrow(output_dim, start_idx,
|
loaded_weight = loaded_weight.narrow(output_dim, start_idx,
|
||||||
shard_size)
|
shard_size)
|
||||||
else:
|
else:
|
||||||
|
ignore_warning = getattr(param, "ignore_warning", False)
|
||||||
|
if not ignore_warning:
|
||||||
logger.warning(
|
logger.warning(
|
||||||
"Loading a weight without `output_dim` attribute in "
|
"Loading a weight without `output_dim` attribute in "
|
||||||
"QKVParallelLinear, assume the weight is the same "
|
"QKVParallelLinear, assume the weight is the same "
|
||||||
@@ -481,8 +493,10 @@ class RowParallelLinear(torch.nn.Module):
|
|||||||
linear_method = UnquantizedLinearMethod()
|
linear_method = UnquantizedLinearMethod()
|
||||||
self.linear_method = linear_method
|
self.linear_method = linear_method
|
||||||
self.linear_weights = self.linear_method.create_weights(
|
self.linear_weights = self.linear_method.create_weights(
|
||||||
self.input_size_per_partition, self.output_size, self.params_dtype)
|
self.input_size_per_partition, self.output_size, self.input_size,
|
||||||
|
self.output_size, self.params_dtype)
|
||||||
for name, weight in self.linear_weights.items():
|
for name, weight in self.linear_weights.items():
|
||||||
|
if isinstance(weight, torch.Tensor):
|
||||||
self.register_parameter(name, weight)
|
self.register_parameter(name, weight)
|
||||||
set_weight_attrs(weight, {"weight_loader": self.weight_loader})
|
set_weight_attrs(weight, {"weight_loader": self.weight_loader})
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
from typing import Type
|
from typing import Type
|
||||||
|
|
||||||
from vllm.model_executor.layers.quantization.awq import AWQConfig
|
|
||||||
from vllm.model_executor.layers.quantization.squeezellm import SqueezeLLMConfig
|
|
||||||
from vllm.model_executor.layers.quantization.base_config import QuantizationConfig
|
from vllm.model_executor.layers.quantization.base_config import QuantizationConfig
|
||||||
|
from vllm.model_executor.layers.quantization.awq import AWQConfig
|
||||||
|
from vllm.model_executor.layers.quantization.gptq import GPTQConfig
|
||||||
|
from vllm.model_executor.layers.quantization.squeezellm import SqueezeLLMConfig
|
||||||
|
|
||||||
_QUANTIZATION_CONFIG_REGISTRY = {
|
_QUANTIZATION_CONFIG_REGISTRY = {
|
||||||
"awq": AWQConfig,
|
"awq": AWQConfig,
|
||||||
|
"gptq": GPTQConfig,
|
||||||
"squeezellm": SqueezeLLMConfig,
|
"squeezellm": SqueezeLLMConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -77,14 +77,16 @@ class AWQLinearMethod(LinearMethodBase):
|
|||||||
def __init__(self, quant_config: AWQConfig):
|
def __init__(self, quant_config: AWQConfig):
|
||||||
self.quant_config = quant_config
|
self.quant_config = quant_config
|
||||||
|
|
||||||
def create_weights(self, input_size: int, output_size: int,
|
def create_weights(self, input_size_per_partition: int,
|
||||||
params_dtype: torch.dtype) -> Dict[str, torch.Tensor]:
|
output_size_per_partition: int, input_size: int,
|
||||||
if input_size % self.quant_config.group_size != 0:
|
output_size: int,
|
||||||
|
params_dtype: torch.dtype) -> Dict[str, Any]:
|
||||||
|
if input_size_per_partition % self.quant_config.group_size != 0:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
"The input size is not aligned with the quantized "
|
"The input size is not aligned with the quantized "
|
||||||
"weight shape. This can be caused by too large "
|
"weight shape. This can be caused by too large "
|
||||||
"tensor parallel size.")
|
"tensor parallel size.")
|
||||||
if output_size % self.quant_config.pack_factor != 0:
|
if output_size_per_partition % self.quant_config.pack_factor != 0:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
"The output size is not aligned with the quantized "
|
"The output size is not aligned with the quantized "
|
||||||
"weight shape. This can be caused by too large "
|
"weight shape. This can be caused by too large "
|
||||||
@@ -92,8 +94,8 @@ class AWQLinearMethod(LinearMethodBase):
|
|||||||
|
|
||||||
qweight = Parameter(
|
qweight = Parameter(
|
||||||
torch.empty(
|
torch.empty(
|
||||||
input_size,
|
input_size_per_partition,
|
||||||
output_size // self.quant_config.pack_factor,
|
output_size_per_partition // self.quant_config.pack_factor,
|
||||||
device="cuda",
|
device="cuda",
|
||||||
dtype=torch.int32,
|
dtype=torch.int32,
|
||||||
),
|
),
|
||||||
@@ -108,8 +110,8 @@ class AWQLinearMethod(LinearMethodBase):
|
|||||||
})
|
})
|
||||||
qzeros = Parameter(
|
qzeros = Parameter(
|
||||||
torch.empty(
|
torch.empty(
|
||||||
input_size // self.quant_config.group_size,
|
input_size_per_partition // self.quant_config.group_size,
|
||||||
output_size // self.quant_config.pack_factor,
|
output_size_per_partition // self.quant_config.pack_factor,
|
||||||
device="cuda",
|
device="cuda",
|
||||||
dtype=torch.int32,
|
dtype=torch.int32,
|
||||||
),
|
),
|
||||||
@@ -124,8 +126,8 @@ class AWQLinearMethod(LinearMethodBase):
|
|||||||
})
|
})
|
||||||
scales = Parameter(
|
scales = Parameter(
|
||||||
torch.empty(
|
torch.empty(
|
||||||
input_size // self.quant_config.group_size,
|
input_size_per_partition // self.quant_config.group_size,
|
||||||
output_size,
|
output_size_per_partition,
|
||||||
device="cuda",
|
device="cuda",
|
||||||
dtype=params_dtype,
|
dtype=params_dtype,
|
||||||
),
|
),
|
||||||
@@ -142,7 +144,7 @@ class AWQLinearMethod(LinearMethodBase):
|
|||||||
}
|
}
|
||||||
|
|
||||||
def apply_weights(self,
|
def apply_weights(self,
|
||||||
weights: Dict[str, torch.Tensor],
|
weights: Dict[str, Any],
|
||||||
x: torch.Tensor,
|
x: torch.Tensor,
|
||||||
bias: Optional[torch.Tensor] = None) -> torch.Tensor:
|
bias: Optional[torch.Tensor] = None) -> torch.Tensor:
|
||||||
qweight = weights["qweight"]
|
qweight = weights["qweight"]
|
||||||
|
|||||||
215
vllm/model_executor/layers/quantization/gptq.py
Normal file
215
vllm/model_executor/layers/quantization/gptq.py
Normal file
@@ -0,0 +1,215 @@
|
|||||||
|
import enum
|
||||||
|
from enum import Enum
|
||||||
|
from typing import Any, Dict, List, Optional
|
||||||
|
|
||||||
|
import torch
|
||||||
|
from torch.nn.parameter import Parameter
|
||||||
|
|
||||||
|
from vllm._C import ops
|
||||||
|
from vllm.model_executor.layers.linear import (LinearMethodBase,
|
||||||
|
set_weight_attrs)
|
||||||
|
from vllm.model_executor.layers.quantization.base_config import (
|
||||||
|
QuantizationConfig)
|
||||||
|
|
||||||
|
|
||||||
|
class GPTQConfig(QuantizationConfig):
|
||||||
|
"""Config class for GPTQ.
|
||||||
|
|
||||||
|
Reference: https://arxiv.org/abs/2210.17323
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
weight_bits: int,
|
||||||
|
group_size: int,
|
||||||
|
desc_act: bool,
|
||||||
|
) -> None:
|
||||||
|
self.weight_bits = weight_bits
|
||||||
|
self.group_size = group_size
|
||||||
|
self.desc_act = desc_act
|
||||||
|
self.pack_factor = 32 // self.weight_bits
|
||||||
|
# exllama kernel v1 only supports 4 bit
|
||||||
|
if self.weight_bits != 4:
|
||||||
|
raise ValueError(
|
||||||
|
"Currently, only 4-bit weight quantization is supported for "
|
||||||
|
f"GPTQ, but got {self.weight_bits} bits.")
|
||||||
|
|
||||||
|
def __repr__(self) -> str:
|
||||||
|
return (f"GPTQConfig(weight_bits={self.weight_bits}, "
|
||||||
|
f"group_size={self.group_size}, "
|
||||||
|
f"desc_act={self.desc_act})")
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_name(cls) -> str:
|
||||||
|
return "gptq"
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_supported_act_dtypes(cls) -> List[torch.dtype]:
|
||||||
|
return [torch.half]
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
# Need to figure it out
|
||||||
|
def get_min_capability(cls) -> int:
|
||||||
|
return 60
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_config_filenames(cls) -> List[str]:
|
||||||
|
return ["quantize_config.json"]
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_config(cls, config: Dict[str, Any]) -> "GPTQConfig":
|
||||||
|
weight_bits = cls.get_from_keys(config, ["bits"])
|
||||||
|
group_size = cls.get_from_keys(config, ["group_size"])
|
||||||
|
desc_act = cls.get_from_keys(config, ["desc_act"])
|
||||||
|
return cls(weight_bits, group_size, desc_act)
|
||||||
|
|
||||||
|
def get_linear_method(self) -> "GPTQLinearMethod":
|
||||||
|
return GPTQLinearMethod(self)
|
||||||
|
|
||||||
|
def get_scaled_act_names(self) -> List[str]:
|
||||||
|
return []
|
||||||
|
|
||||||
|
|
||||||
|
class ExllamaState(Enum):
|
||||||
|
|
||||||
|
UNUSED = enum.auto()
|
||||||
|
UNINITIALIZED = enum.auto()
|
||||||
|
READY = enum.auto()
|
||||||
|
|
||||||
|
|
||||||
|
class GPTQLinearMethod(LinearMethodBase):
|
||||||
|
"""Linear method for GPTQ.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
quant_config: The GPTQ quantization config.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, quant_config: GPTQConfig):
|
||||||
|
self.quant_config = quant_config
|
||||||
|
|
||||||
|
def create_weights(
|
||||||
|
self,
|
||||||
|
input_size_per_partition: int,
|
||||||
|
output_size_per_partition: int,
|
||||||
|
input_size: int,
|
||||||
|
output_size: int,
|
||||||
|
params_dtype: torch.dtype,
|
||||||
|
) -> Dict[str, Any]:
|
||||||
|
del output_size # Unused.
|
||||||
|
if input_size_per_partition % self.quant_config.group_size != 0:
|
||||||
|
raise ValueError(
|
||||||
|
"The input size is not aligned with the quantized "
|
||||||
|
"weight shape. This can be caused by too large "
|
||||||
|
"tensor parallel size.")
|
||||||
|
if output_size_per_partition % self.quant_config.pack_factor != 0:
|
||||||
|
raise ValueError(
|
||||||
|
"The output size is not aligned with the quantized "
|
||||||
|
"weight shape. This can be caused by too large "
|
||||||
|
"tensor parallel size.")
|
||||||
|
|
||||||
|
if self.quant_config.group_size != -1:
|
||||||
|
group_size = self.quant_config.group_size
|
||||||
|
else:
|
||||||
|
group_size = input_size
|
||||||
|
exllama_state = ExllamaState.UNINITIALIZED
|
||||||
|
scale_and_zero_size = input_size // group_size
|
||||||
|
scale_and_zero_input_dim = None
|
||||||
|
if input_size != input_size_per_partition and self.quant_config.group_size != -1:
|
||||||
|
# For act-order models, we cannot use Exllama for row parallel layer
|
||||||
|
if self.quant_config.desc_act:
|
||||||
|
exllama_state = ExllamaState.UNUSED
|
||||||
|
else:
|
||||||
|
# we need to partition qzeros and scales for exllama kernel
|
||||||
|
scale_and_zero_size = input_size_per_partition // group_size
|
||||||
|
scale_and_zero_input_dim = 0
|
||||||
|
|
||||||
|
qweight = Parameter(
|
||||||
|
torch.empty(
|
||||||
|
input_size_per_partition // self.quant_config.pack_factor,
|
||||||
|
output_size_per_partition,
|
||||||
|
device="cuda",
|
||||||
|
dtype=torch.int32,
|
||||||
|
),
|
||||||
|
requires_grad=False,
|
||||||
|
)
|
||||||
|
set_weight_attrs(
|
||||||
|
qweight, {
|
||||||
|
"input_dim": 0,
|
||||||
|
"output_dim": 1,
|
||||||
|
"packed_dim": 0,
|
||||||
|
"pack_factor": self.quant_config.pack_factor,
|
||||||
|
})
|
||||||
|
g_idx = Parameter(
|
||||||
|
torch.tensor(
|
||||||
|
[
|
||||||
|
i // self.quant_config.group_size
|
||||||
|
for i in range(input_size_per_partition)
|
||||||
|
],
|
||||||
|
device="cuda",
|
||||||
|
dtype=torch.int32,
|
||||||
|
),
|
||||||
|
requires_grad=False,
|
||||||
|
)
|
||||||
|
# Ignore warning from fused linear layers such as QKVParallelLinear.
|
||||||
|
set_weight_attrs(g_idx, {"input_dim": 0, "ignore_warning": True})
|
||||||
|
qzeros = Parameter(
|
||||||
|
torch.empty(
|
||||||
|
scale_and_zero_size,
|
||||||
|
output_size_per_partition // self.quant_config.pack_factor,
|
||||||
|
device="cuda",
|
||||||
|
dtype=torch.int32,
|
||||||
|
),
|
||||||
|
requires_grad=False,
|
||||||
|
)
|
||||||
|
set_weight_attrs(
|
||||||
|
qzeros, {
|
||||||
|
"input_dim": scale_and_zero_input_dim,
|
||||||
|
"output_dim": 1,
|
||||||
|
"packed_dim": 1,
|
||||||
|
"pack_factor": self.quant_config.pack_factor,
|
||||||
|
})
|
||||||
|
scales = Parameter(
|
||||||
|
torch.empty(
|
||||||
|
scale_and_zero_size,
|
||||||
|
output_size_per_partition,
|
||||||
|
device="cuda",
|
||||||
|
dtype=params_dtype,
|
||||||
|
),
|
||||||
|
requires_grad=False,
|
||||||
|
)
|
||||||
|
set_weight_attrs(scales, {
|
||||||
|
"input_dim": scale_and_zero_input_dim,
|
||||||
|
"output_dim": 1,
|
||||||
|
})
|
||||||
|
return {
|
||||||
|
"qweight": qweight,
|
||||||
|
"g_idx": g_idx,
|
||||||
|
"qzeros": qzeros,
|
||||||
|
"scales": scales,
|
||||||
|
"exllama_state": exllama_state,
|
||||||
|
}
|
||||||
|
|
||||||
|
def apply_weights(self,
|
||||||
|
weights: Dict[str, Any],
|
||||||
|
x: torch.Tensor,
|
||||||
|
bias: Optional[torch.Tensor] = None) -> torch.Tensor:
|
||||||
|
qweight = weights["qweight"]
|
||||||
|
out_shape = x.shape[:-1] + (qweight.shape[-1], )
|
||||||
|
reshaped_x = x.reshape(-1, x.shape[-1])
|
||||||
|
# exllama needs to shuffle the weight after the weight is loaded
|
||||||
|
# here we do the shuffle on first forward pass
|
||||||
|
if weights["exllama_state"] == ExllamaState.UNINITIALIZED:
|
||||||
|
if self.quant_config.desc_act:
|
||||||
|
weights["g_idx"] = torch.argsort(weights["g_idx"]).to(
|
||||||
|
torch.int)
|
||||||
|
else:
|
||||||
|
weights["g_idx"] = torch.empty((1, 1), device="meta")
|
||||||
|
weights["exllama_state"] = ExllamaState.READY
|
||||||
|
ops.gptq_shuffle(weights["qweight"], weights["g_idx"])
|
||||||
|
output = ops.gptq_gemm(reshaped_x, weights["qweight"],
|
||||||
|
weights["qzeros"], weights["scales"],
|
||||||
|
weights["g_idx"],
|
||||||
|
weights["exllama_state"] == ExllamaState.READY)
|
||||||
|
if bias is not None:
|
||||||
|
output = output + bias
|
||||||
|
return output.reshape(out_shape)
|
||||||
@@ -67,17 +67,19 @@ class SqueezeLLMLinearMethod(LinearMethodBase):
|
|||||||
def __init__(self, quant_config: SqueezeLLMConfig):
|
def __init__(self, quant_config: SqueezeLLMConfig):
|
||||||
self.quant_config = quant_config
|
self.quant_config = quant_config
|
||||||
|
|
||||||
def create_weights(self, input_size: int, output_size: int,
|
def create_weights(self, input_size_per_partition: int,
|
||||||
params_dtype: torch.dtype) -> Dict[str, torch.Tensor]:
|
output_size_per_partition: int, input_size: int,
|
||||||
if input_size % self.quant_config.pack_factor != 0:
|
output_size: int,
|
||||||
|
params_dtype: torch.dtype) -> Dict[str, Any]:
|
||||||
|
if input_size_per_partition % self.quant_config.pack_factor != 0:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
"The input size is not aligned with the quantized "
|
"The input size is not aligned with the quantized "
|
||||||
"weight shape. This can be caused by too large "
|
"weight shape. This can be caused by too large "
|
||||||
"tensor parallel size.")
|
"tensor parallel size.")
|
||||||
qweight = Parameter(
|
qweight = Parameter(
|
||||||
torch.empty(
|
torch.empty(
|
||||||
input_size // self.quant_config.pack_factor,
|
input_size_per_partition // self.quant_config.pack_factor,
|
||||||
output_size,
|
output_size_per_partition,
|
||||||
device="cuda",
|
device="cuda",
|
||||||
dtype=torch.int32,
|
dtype=torch.int32,
|
||||||
),
|
),
|
||||||
@@ -108,7 +110,7 @@ class SqueezeLLMLinearMethod(LinearMethodBase):
|
|||||||
}
|
}
|
||||||
|
|
||||||
def apply_weights(self,
|
def apply_weights(self,
|
||||||
weights: Dict[str, torch.Tensor],
|
weights: Dict[str, Any],
|
||||||
x: torch.Tensor,
|
x: torch.Tensor,
|
||||||
bias: Optional[torch.Tensor] = None) -> torch.Tensor:
|
bias: Optional[torch.Tensor] = None) -> torch.Tensor:
|
||||||
qweight = weights["qweight"]
|
qweight = weights["qweight"]
|
||||||
|
|||||||
@@ -5,14 +5,12 @@ import torch
|
|||||||
import torch.nn as nn
|
import torch.nn as nn
|
||||||
|
|
||||||
from vllm.model_executor.parallel_utils.communication_op import (
|
from vllm.model_executor.parallel_utils.communication_op import (
|
||||||
tensor_model_parallel_all_gather)
|
tensor_model_parallel_gather)
|
||||||
from vllm.model_executor.sampling_metadata import SamplingMetadata
|
from vllm.model_executor.sampling_metadata import SamplingMetadata, SamplingTensors
|
||||||
from vllm.sampling_params import SamplingParams, SamplingType
|
from vllm.sampling_params import SamplingParams, SamplingType
|
||||||
from vllm.sequence import (PromptLogprobs, SampleLogprobs, SamplerOutput,
|
from vllm.sequence import (PromptLogprobs, SampleLogprobs, SamplerOutput,
|
||||||
SequenceData, SequenceGroupOutput, SequenceOutput)
|
SequenceData, SequenceGroupOutput, SequenceOutput)
|
||||||
|
|
||||||
_SAMPLING_EPS = 1e-5
|
|
||||||
|
|
||||||
|
|
||||||
class Sampler(nn.Module):
|
class Sampler(nn.Module):
|
||||||
"""Samples the next tokens from the model's outputs.
|
"""Samples the next tokens from the model's outputs.
|
||||||
@@ -39,7 +37,7 @@ class Sampler(nn.Module):
|
|||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
sampling_metadata: SamplingMetadata,
|
sampling_metadata: SamplingMetadata,
|
||||||
embedding_bias: Optional[torch.Tensor] = None,
|
embedding_bias: Optional[torch.Tensor] = None,
|
||||||
) -> SamplerOutput:
|
) -> Optional[SamplerOutput]:
|
||||||
# Get the hidden states that we use for sampling.
|
# Get the hidden states that we use for sampling.
|
||||||
hidden_states = _prune_hidden_states(hidden_states, sampling_metadata)
|
hidden_states = _prune_hidden_states(hidden_states, sampling_metadata)
|
||||||
|
|
||||||
@@ -47,40 +45,42 @@ class Sampler(nn.Module):
|
|||||||
logits = _get_logits(hidden_states, embedding, embedding_bias,
|
logits = _get_logits(hidden_states, embedding, embedding_bias,
|
||||||
self.vocab_size)
|
self.vocab_size)
|
||||||
|
|
||||||
|
# Only perform sampling in the driver worker.
|
||||||
|
# Note: `_get_logits` is still distributed across TP workers because
|
||||||
|
# the `embedding` weight is distributed across TP workers.
|
||||||
|
# TODO(zhuohan): Change the get_logits part to a separate stage.
|
||||||
|
if not sampling_metadata.perform_sampling:
|
||||||
|
return None
|
||||||
|
|
||||||
|
assert logits is not None
|
||||||
|
_, vocab_size = logits.shape
|
||||||
|
|
||||||
# Apply logits processors (if any).
|
# Apply logits processors (if any).
|
||||||
logits = _apply_logits_processors(logits, sampling_metadata)
|
logits = _apply_logits_processors(logits, sampling_metadata)
|
||||||
|
|
||||||
|
# Prepare sampling tensors with pinned memory to avoid blocking.
|
||||||
|
(sampling_tensors, do_penalties, do_top_p_top_k,
|
||||||
|
do_min_p) = SamplingTensors.from_sampling_metadata(
|
||||||
|
sampling_metadata, vocab_size, logits.device, logits.dtype)
|
||||||
|
|
||||||
# Apply presence and frequency penalties.
|
# Apply presence and frequency penalties.
|
||||||
presence_penalties, frequency_penalties, repetition_penalties = (
|
if do_penalties:
|
||||||
_get_penalties(sampling_metadata))
|
logits = _apply_penalties(logits, sampling_tensors.prompt_tokens,
|
||||||
assert len(presence_penalties) == logits.shape[0]
|
sampling_tensors.output_tokens,
|
||||||
assert len(frequency_penalties) == logits.shape[0]
|
sampling_tensors.presence_penalties,
|
||||||
assert len(repetition_penalties) == logits.shape[0]
|
sampling_tensors.frequency_penalties,
|
||||||
logits = _apply_penalties(logits, sampling_metadata,
|
sampling_tensors.repetition_penalties)
|
||||||
presence_penalties, frequency_penalties,
|
|
||||||
repetition_penalties)
|
|
||||||
|
|
||||||
# Apply temperature scaling.
|
# Apply temperature scaling.
|
||||||
temperatures = _get_temperatures(sampling_metadata)
|
|
||||||
assert len(temperatures) == logits.shape[0]
|
|
||||||
if any(t != 1.0 for t in temperatures):
|
|
||||||
t = torch.tensor(temperatures,
|
|
||||||
dtype=logits.dtype,
|
|
||||||
device=logits.device)
|
|
||||||
# Use in-place division to avoid creating a new tensor.
|
# Use in-place division to avoid creating a new tensor.
|
||||||
logits.div_(t.unsqueeze(dim=1))
|
logits.div_(sampling_tensors.temperatures.unsqueeze_(dim=1))
|
||||||
|
|
||||||
# Apply top-p and top-k truncation.
|
if do_top_p_top_k:
|
||||||
top_ps, top_ks, min_ps = _get_top_p_top_k_min_p(
|
logits = _apply_top_p_top_k(logits, sampling_tensors.top_ps,
|
||||||
sampling_metadata, self.vocab_size)
|
sampling_tensors.top_ks)
|
||||||
assert len(top_ps) == len(top_ks) == logits.shape[0]
|
|
||||||
do_top_p = any(p < 1.0 - _SAMPLING_EPS for p in top_ps)
|
|
||||||
do_top_k = any(k != self.vocab_size for k in top_ks)
|
|
||||||
if do_top_p or do_top_k:
|
|
||||||
logits = _apply_top_p_top_k(logits, top_ps, top_ks)
|
|
||||||
|
|
||||||
do_min_p = any(mp > _SAMPLING_EPS for mp in min_ps)
|
|
||||||
if do_min_p:
|
if do_min_p:
|
||||||
logits = _apply_min_p(logits, min_ps)
|
logits = _apply_min_p(logits, sampling_tensors.min_ps)
|
||||||
|
|
||||||
# We use float32 for probabilities and log probabilities.
|
# We use float32 for probabilities and log probabilities.
|
||||||
# Compute the probabilities.
|
# Compute the probabilities.
|
||||||
@@ -100,13 +100,14 @@ class Sampler(nn.Module):
|
|||||||
|
|
||||||
def _get_logits(hidden_states: torch.Tensor, embedding: torch.Tensor,
|
def _get_logits(hidden_states: torch.Tensor, embedding: torch.Tensor,
|
||||||
embedding_bias: Optional[torch.Tensor],
|
embedding_bias: Optional[torch.Tensor],
|
||||||
vocab_size: int) -> torch.Tensor:
|
vocab_size: int) -> Optional[torch.Tensor]:
|
||||||
# Get the logits for the next tokens.
|
# Get the logits for the next tokens.
|
||||||
logits = torch.matmul(hidden_states, embedding.t())
|
logits = torch.matmul(hidden_states, embedding.t())
|
||||||
if embedding_bias is not None:
|
if embedding_bias is not None:
|
||||||
logits += embedding_bias
|
logits += embedding_bias
|
||||||
logits = tensor_model_parallel_all_gather(logits)
|
logits = tensor_model_parallel_gather(logits)
|
||||||
# Remove paddings in vocab (if any).
|
# Remove paddings in vocab (if any).
|
||||||
|
if logits is not None:
|
||||||
logits = logits[:, :vocab_size]
|
logits = logits[:, :vocab_size]
|
||||||
return logits
|
return logits
|
||||||
|
|
||||||
@@ -120,73 +121,17 @@ def _prune_hidden_states(
|
|||||||
sampling_metadata.selected_token_indices)
|
sampling_metadata.selected_token_indices)
|
||||||
|
|
||||||
|
|
||||||
def _get_penalties(
|
|
||||||
sampling_metadata: SamplingMetadata
|
|
||||||
) -> Tuple[List[float], List[float], List[float]]:
|
|
||||||
# Collect the presence and frequency penalties.
|
|
||||||
presence_penalties: List[float] = []
|
|
||||||
frequency_penalties: List[float] = []
|
|
||||||
repetition_penalties: List[float] = []
|
|
||||||
for i, seq_group in enumerate(sampling_metadata.seq_groups):
|
|
||||||
seq_ids, sampling_params = seq_group
|
|
||||||
p = sampling_params.presence_penalty
|
|
||||||
f = sampling_params.frequency_penalty
|
|
||||||
r = sampling_params.repetition_penalty
|
|
||||||
if (i < sampling_metadata.num_prompts
|
|
||||||
and sampling_params.prompt_logprobs is not None):
|
|
||||||
# NOTE: We do not apply presence and frequency penalties for the
|
|
||||||
# prompt token positions where we don't sample new tokens.
|
|
||||||
prompt_len = sampling_metadata.prompt_lens[i]
|
|
||||||
presence_penalties += [0] * (prompt_len - 1)
|
|
||||||
frequency_penalties += [0] * (prompt_len - 1)
|
|
||||||
repetition_penalties += [1] * (prompt_len - 1)
|
|
||||||
presence_penalties += [p] * len(seq_ids)
|
|
||||||
frequency_penalties += [f] * len(seq_ids)
|
|
||||||
repetition_penalties += [r] * len(seq_ids)
|
|
||||||
return presence_penalties, frequency_penalties, repetition_penalties
|
|
||||||
|
|
||||||
|
|
||||||
def _get_prompt_and_output_tokens(
|
|
||||||
sampling_metadata: SamplingMetadata,
|
|
||||||
) -> Tuple[List[List[int]], List[List[int]]]:
|
|
||||||
prompt_tokens: List[List[int]] = []
|
|
||||||
output_tokens: List[List[int]] = []
|
|
||||||
for i, seq_group in enumerate(sampling_metadata.seq_groups):
|
|
||||||
seq_ids, sampling_params = seq_group
|
|
||||||
if (i < sampling_metadata.num_prompts
|
|
||||||
and sampling_params.prompt_logprobs is not None):
|
|
||||||
# NOTE: prompt token positions do not need output tokens to
|
|
||||||
# compute penalties.
|
|
||||||
prompt_len = sampling_metadata.prompt_lens[i]
|
|
||||||
prompt_tokens.extend([] for _ in range(prompt_len - 1))
|
|
||||||
output_tokens.extend([] for _ in range(prompt_len - 1))
|
|
||||||
for seq_id in seq_ids:
|
|
||||||
seq_data = sampling_metadata.seq_data[seq_id]
|
|
||||||
prompt_tokens.append(seq_data.prompt_token_ids)
|
|
||||||
output_tokens.append(seq_data.output_token_ids)
|
|
||||||
return prompt_tokens, output_tokens
|
|
||||||
|
|
||||||
|
|
||||||
def _get_bin_counts_and_mask(
|
def _get_bin_counts_and_mask(
|
||||||
logits: torch.Tensor,
|
tokens: torch.Tensor,
|
||||||
tokens: List[List[int]],
|
|
||||||
vocab_size: int,
|
vocab_size: int,
|
||||||
num_seqs: int,
|
num_seqs: int,
|
||||||
) -> Tuple[torch.Tensor, torch.Tensor]:
|
) -> Tuple[torch.Tensor, torch.Tensor]:
|
||||||
max_len = max(len(tokens) for tokens in tokens)
|
|
||||||
padded_tokens = [
|
|
||||||
tokens + [vocab_size] * (max_len - len(tokens)) for tokens in tokens
|
|
||||||
]
|
|
||||||
tokens_tensor = torch.tensor(padded_tokens,
|
|
||||||
dtype=torch.long,
|
|
||||||
device=logits.device)
|
|
||||||
|
|
||||||
# Compute the bin counts for the tokens.
|
# Compute the bin counts for the tokens.
|
||||||
# vocab_size + 1 for padding.
|
# vocab_size + 1 for padding.
|
||||||
bin_counts = torch.zeros((num_seqs, vocab_size + 1),
|
bin_counts = torch.zeros((num_seqs, vocab_size + 1),
|
||||||
dtype=torch.long,
|
dtype=torch.long,
|
||||||
device=logits.device)
|
device=tokens.device)
|
||||||
bin_counts.scatter_add_(1, tokens_tensor, torch.ones_like(tokens_tensor))
|
bin_counts.scatter_add_(1, tokens, torch.ones_like(tokens))
|
||||||
bin_counts = bin_counts[:, :vocab_size]
|
bin_counts = bin_counts[:, :vocab_size]
|
||||||
mask = bin_counts > 0
|
mask = bin_counts > 0
|
||||||
|
|
||||||
@@ -217,45 +162,16 @@ def _apply_logits_processors(
|
|||||||
return logits
|
return logits
|
||||||
|
|
||||||
|
|
||||||
def _apply_penalties(
|
def _apply_penalties(logits: torch.Tensor, prompt_tokens_tensor: torch.Tensor,
|
||||||
logits: torch.Tensor,
|
output_tokens_tensor: torch.Tensor,
|
||||||
sampling_metadata: SamplingMetadata,
|
presence_penalties: torch.Tensor,
|
||||||
presence_penalties: List[float],
|
frequency_penalties: torch.Tensor,
|
||||||
frequency_penalties: List[float],
|
repetition_penalties: torch.Tensor) -> torch.Tensor:
|
||||||
repetition_penalties: List[float],
|
|
||||||
) -> torch.Tensor:
|
|
||||||
num_seqs, vocab_size = logits.shape
|
num_seqs, vocab_size = logits.shape
|
||||||
for i in range(num_seqs):
|
_, prompt_mask = _get_bin_counts_and_mask(prompt_tokens_tensor, vocab_size,
|
||||||
p = presence_penalties[i]
|
num_seqs)
|
||||||
f = frequency_penalties[i]
|
|
||||||
r = repetition_penalties[i]
|
|
||||||
if abs(p) < _SAMPLING_EPS and abs(f) < _SAMPLING_EPS and abs(
|
|
||||||
r - 1.0) < _SAMPLING_EPS:
|
|
||||||
continue
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
# Return early if all sequences have zero penalties.
|
|
||||||
return logits
|
|
||||||
|
|
||||||
prompt_tokens, output_tokens = (
|
|
||||||
_get_prompt_and_output_tokens(sampling_metadata))
|
|
||||||
assert len(prompt_tokens) == logits.shape[0]
|
|
||||||
assert len(output_tokens) == logits.shape[0]
|
|
||||||
|
|
||||||
prompt_bin_counts, prompt_mask = _get_bin_counts_and_mask(
|
|
||||||
logits, prompt_tokens, vocab_size, num_seqs)
|
|
||||||
output_bin_counts, output_mask = _get_bin_counts_and_mask(
|
output_bin_counts, output_mask = _get_bin_counts_and_mask(
|
||||||
logits, output_tokens, vocab_size, num_seqs)
|
output_tokens_tensor, vocab_size, num_seqs)
|
||||||
|
|
||||||
repetition_penalties = torch.tensor(repetition_penalties,
|
|
||||||
dtype=logits.dtype,
|
|
||||||
device=logits.device)
|
|
||||||
frequency_penalties = torch.tensor(frequency_penalties,
|
|
||||||
dtype=logits.dtype,
|
|
||||||
device=logits.device)
|
|
||||||
presence_penalties = torch.tensor(presence_penalties,
|
|
||||||
dtype=logits.dtype,
|
|
||||||
device=logits.device)
|
|
||||||
|
|
||||||
repetition_penalties = repetition_penalties[:, None].repeat(1, vocab_size)
|
repetition_penalties = repetition_penalties[:, None].repeat(1, vocab_size)
|
||||||
repetition_penalties[~(prompt_mask | output_mask)] = 1.0
|
repetition_penalties[~(prompt_mask | output_mask)] = 1.0
|
||||||
@@ -264,109 +180,65 @@ def _apply_penalties(
|
|||||||
|
|
||||||
# We follow the definition in OpenAI API.
|
# We follow the definition in OpenAI API.
|
||||||
# Refer to https://platform.openai.com/docs/api-reference/parameter-details
|
# Refer to https://platform.openai.com/docs/api-reference/parameter-details
|
||||||
logits -= frequency_penalties.unsqueeze(dim=1) * output_bin_counts
|
logits -= frequency_penalties.unsqueeze_(dim=1) * output_bin_counts
|
||||||
logits -= presence_penalties.unsqueeze(dim=1) * output_mask
|
logits -= presence_penalties.unsqueeze_(dim=1) * output_mask
|
||||||
return logits
|
return logits
|
||||||
|
|
||||||
|
|
||||||
def _get_temperatures(sampling_metadata: SamplingMetadata) -> List[float]:
|
|
||||||
# Collect the temperatures for the logits.
|
|
||||||
temperatures: List[float] = []
|
|
||||||
for i, seq_group in enumerate(sampling_metadata.seq_groups):
|
|
||||||
seq_ids, sampling_params = seq_group
|
|
||||||
temperature = sampling_params.temperature
|
|
||||||
if temperature < _SAMPLING_EPS:
|
|
||||||
# NOTE: Zero temperature means deterministic sampling
|
|
||||||
# (i.e., greedy sampling or beam search).
|
|
||||||
# Set the temperature to 1 to avoid division by zero.
|
|
||||||
temperature = 1.0
|
|
||||||
if (i < sampling_metadata.num_prompts
|
|
||||||
and sampling_params.prompt_logprobs is not None):
|
|
||||||
prompt_len = sampling_metadata.prompt_lens[i]
|
|
||||||
temperatures += [temperature] * (prompt_len - 1)
|
|
||||||
temperatures += [temperature] * len(seq_ids)
|
|
||||||
return temperatures
|
|
||||||
|
|
||||||
|
|
||||||
def _get_top_p_top_k_min_p(
|
|
||||||
sampling_metadata: SamplingMetadata,
|
|
||||||
vocab_size: int,
|
|
||||||
) -> Tuple[List[float], List[int], List[float]]:
|
|
||||||
top_ps: List[float] = []
|
|
||||||
top_ks: List[int] = []
|
|
||||||
min_ps: List[float] = []
|
|
||||||
for i, seq_group in enumerate(sampling_metadata.seq_groups):
|
|
||||||
seq_ids, sampling_params = seq_group
|
|
||||||
top_p = sampling_params.top_p
|
|
||||||
min_p = sampling_params.min_p
|
|
||||||
# k should not be greater than the vocab size.
|
|
||||||
top_k = min(sampling_params.top_k, vocab_size)
|
|
||||||
# k=-1 means no truncation.
|
|
||||||
top_k = vocab_size if top_k == -1 else top_k
|
|
||||||
if (i < sampling_metadata.num_prompts
|
|
||||||
and sampling_params.prompt_logprobs is not None):
|
|
||||||
prompt_len = sampling_metadata.prompt_lens[i]
|
|
||||||
top_ps += [top_p] * (prompt_len - 1)
|
|
||||||
top_ks += [top_k] * (prompt_len - 1)
|
|
||||||
min_ps += [min_p] * (prompt_len - 1)
|
|
||||||
top_ps += [top_p] * len(seq_ids)
|
|
||||||
top_ks += [top_k] * len(seq_ids)
|
|
||||||
min_ps += [min_p] * len(seq_ids)
|
|
||||||
return top_ps, top_ks, min_ps
|
|
||||||
|
|
||||||
|
|
||||||
def _apply_top_p_top_k(
|
def _apply_top_p_top_k(
|
||||||
logits: torch.Tensor,
|
logits: torch.Tensor,
|
||||||
top_ps: List[float],
|
p: torch.Tensor,
|
||||||
top_ks: List[int],
|
k: torch.Tensor,
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
p = torch.tensor(top_ps, dtype=logits.dtype, device=logits.device)
|
|
||||||
k = torch.tensor(top_ks, dtype=torch.int, device=logits.device)
|
|
||||||
logits_sort, logits_idx = logits.sort(dim=-1, descending=True)
|
logits_sort, logits_idx = logits.sort(dim=-1, descending=True)
|
||||||
|
|
||||||
# Apply top-p.
|
# Apply top-p.
|
||||||
probs_sort = logits_sort.softmax(dim=-1)
|
probs_sort = logits_sort.softmax(dim=-1)
|
||||||
probs_sum = probs_sort.cumsum(dim=-1)
|
probs_sum = probs_sort.cumsum(dim=-1).sub_(probs_sort)
|
||||||
top_p_mask = (probs_sum - probs_sort) > p.unsqueeze(dim=1)
|
top_p_mask = probs_sum > p.unsqueeze_(dim=1)
|
||||||
logits_sort[top_p_mask] = -float("inf")
|
|
||||||
|
|
||||||
# Apply top-k.
|
# Apply top-k.
|
||||||
# Create a mask for the top-k elements.
|
# Create a mask for the top-k elements.
|
||||||
top_k_mask = torch.arange(logits_idx.shape[-1], device=logits_idx.device)
|
top_k_mask = torch.arange(logits_idx.shape[-1], device=logits_idx.device)
|
||||||
top_k_mask = top_k_mask.expand(logits_idx.shape[0], -1)
|
top_k_mask = top_k_mask.expand(logits_idx.shape[0], -1)
|
||||||
top_k_mask = top_k_mask >= k.unsqueeze(dim=1)
|
top_k_mask = top_k_mask >= k.unsqueeze_(dim=1)
|
||||||
logits_sort[top_k_mask] = -float("inf")
|
|
||||||
|
# Final mask.
|
||||||
|
mask = (top_p_mask | top_k_mask)
|
||||||
|
logits_sort.masked_fill_(mask, -float("inf"))
|
||||||
|
|
||||||
# Re-sort the probabilities.
|
# Re-sort the probabilities.
|
||||||
logits = torch.gather(logits_sort,
|
src = torch.arange(logits_idx.shape[-1],
|
||||||
dim=-1,
|
device=logits_idx.device).expand_as(logits_idx)
|
||||||
index=torch.argsort(logits_idx, dim=-1))
|
logits_idx_inv = torch.empty_like(logits_idx).scatter_(dim=-1,
|
||||||
|
index=logits_idx,
|
||||||
|
src=src)
|
||||||
|
logits = torch.gather(logits_sort, dim=-1, index=logits_idx_inv)
|
||||||
return logits
|
return logits
|
||||||
|
|
||||||
|
|
||||||
def _apply_min_p(
|
def _apply_min_p(
|
||||||
logits: torch.Tensor,
|
logits: torch.Tensor,
|
||||||
min_ps: List[float],
|
min_p: torch.Tensor,
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
"""
|
"""
|
||||||
Adapted from
|
Adapted from
|
||||||
https://github.com/oobabooga/text-generation-webui/blob/3146124ec01f02c8fb1650a6517cf1b60b537aaf/modules/sampler_hijack.py#L16C17-L16C17
|
https://github.com/oobabooga/text-generation-webui/blob/3146124ec01f02c8fb1650a6517cf1b60b537aaf/modules/sampler_hijack.py#L16C17-L16C17
|
||||||
"""
|
"""
|
||||||
min_p = torch.tensor(min_ps, dtype=logits.dtype, device=logits.device)
|
|
||||||
probs = torch.softmax(logits, dim=-1)
|
probs = torch.softmax(logits, dim=-1)
|
||||||
top_probs, _ = probs.max(dim=-1, keepdim=True)
|
top_probs, _ = probs.max(dim=-1, keepdim=True)
|
||||||
scaled_min_p = min_p.unsqueeze(dim=1) * top_probs
|
scaled_min_p = min_p.unsqueeze_(dim=1) * top_probs
|
||||||
tokens_to_remove = probs < scaled_min_p
|
tokens_to_remove = probs < scaled_min_p
|
||||||
logits = logits.masked_fill(tokens_to_remove, -float("inf"))
|
logits = logits.masked_fill_(tokens_to_remove, -float("inf"))
|
||||||
|
|
||||||
return logits
|
return logits
|
||||||
|
|
||||||
|
|
||||||
def _greedy_sample(
|
def _greedy_sample(
|
||||||
selected_seq_groups: List[Tuple[List[int], SamplingParams]],
|
selected_seq_groups: List[Tuple[List[int], SamplingParams]],
|
||||||
logprobs: torch.Tensor,
|
samples: torch.Tensor,
|
||||||
) -> List[Tuple[List[int], List[int]]]:
|
) -> List[Tuple[List[int], List[int]]]:
|
||||||
samples = torch.argmax(logprobs, dim=-1).cpu()
|
samples = samples.tolist()
|
||||||
sample_idx = 0
|
sample_idx = 0
|
||||||
results = []
|
results = []
|
||||||
for seq_group in selected_seq_groups:
|
for seq_group in selected_seq_groups:
|
||||||
@@ -375,27 +247,19 @@ def _greedy_sample(
|
|||||||
assert num_parent_seqs == 1, (
|
assert num_parent_seqs == 1, (
|
||||||
"Greedy sampling should have only one seq.")
|
"Greedy sampling should have only one seq.")
|
||||||
parent_ids = list(range(num_parent_seqs))
|
parent_ids = list(range(num_parent_seqs))
|
||||||
next_token_ids = [samples[sample_idx].item()]
|
next_token_ids = [samples[sample_idx]]
|
||||||
results.append((next_token_ids, parent_ids))
|
results.append((next_token_ids, parent_ids))
|
||||||
sample_idx += num_parent_seqs
|
sample_idx += num_parent_seqs
|
||||||
assert sample_idx == logprobs.size(0)
|
|
||||||
return results
|
return results
|
||||||
|
|
||||||
|
|
||||||
def _random_sample(
|
def _random_sample(
|
||||||
selected_seq_groups: List[Tuple[List[int], SamplingParams]],
|
selected_seq_groups: List[Tuple[List[int], SamplingParams]],
|
||||||
is_prompts: List[bool],
|
is_prompts: List[bool],
|
||||||
probs: torch.Tensor,
|
random_samples: torch.Tensor,
|
||||||
) -> List[Tuple[List[int], List[int]]]:
|
) -> List[Tuple[List[int], List[int]]]:
|
||||||
# Find the maximum best_of value of the prompt phase requests.
|
# Find the maximum best_of value of the prompt phase requests.
|
||||||
max_best_of = 1
|
random_samples = random_samples.cpu()
|
||||||
for seq_group, is_prompt in zip(selected_seq_groups, is_prompts):
|
|
||||||
if is_prompt:
|
|
||||||
seq_ids, sampling_params = seq_group
|
|
||||||
max_best_of = max(max_best_of, sampling_params.best_of)
|
|
||||||
random_samples = torch.multinomial(probs,
|
|
||||||
num_samples=max_best_of,
|
|
||||||
replacement=True).cpu()
|
|
||||||
sample_idx = 0
|
sample_idx = 0
|
||||||
results = []
|
results = []
|
||||||
for seq_group, is_prompt in zip(selected_seq_groups, is_prompts):
|
for seq_group, is_prompt in zip(selected_seq_groups, is_prompts):
|
||||||
@@ -403,8 +267,6 @@ def _random_sample(
|
|||||||
num_parent_seqs = len(seq_ids)
|
num_parent_seqs = len(seq_ids)
|
||||||
if is_prompt:
|
if is_prompt:
|
||||||
# Prompt phase.
|
# Prompt phase.
|
||||||
assert num_parent_seqs == 1, (
|
|
||||||
"Prompt input should have only one seq.")
|
|
||||||
parent_ids = [0] * sampling_params.best_of
|
parent_ids = [0] * sampling_params.best_of
|
||||||
next_token_ids = random_samples[
|
next_token_ids = random_samples[
|
||||||
sample_idx, :sampling_params.best_of].tolist()
|
sample_idx, :sampling_params.best_of].tolist()
|
||||||
@@ -415,7 +277,6 @@ def _random_sample(
|
|||||||
num_parent_seqs, 0].tolist()
|
num_parent_seqs, 0].tolist()
|
||||||
results.append((next_token_ids, parent_ids))
|
results.append((next_token_ids, parent_ids))
|
||||||
sample_idx += num_parent_seqs
|
sample_idx += num_parent_seqs
|
||||||
assert sample_idx == probs.size(0)
|
|
||||||
return results
|
return results
|
||||||
|
|
||||||
|
|
||||||
@@ -472,6 +333,28 @@ def _beam_search_sample(
|
|||||||
return results
|
return results
|
||||||
|
|
||||||
|
|
||||||
|
# torch.multinomial forces a GPU<->CPU sync.
|
||||||
|
# Therefore, we use an optimized implementation instead.
|
||||||
|
# Note that we always sample with replacement.
|
||||||
|
# probs will be modified in place, but this is fine, as we pass
|
||||||
|
# in a copy already.
|
||||||
|
def _multinomial(
|
||||||
|
probs: torch.Tensor,
|
||||||
|
num_samples: int,
|
||||||
|
):
|
||||||
|
if num_samples > 1:
|
||||||
|
# This is equivalent to torch.repeat_interleaved (which also
|
||||||
|
# forces a GPU<->CPU sync).
|
||||||
|
# This allows us to do sampling with replacement by creating
|
||||||
|
# num_samples copies of each row in the tensor, and then
|
||||||
|
# batch sampling the resulting tensor.
|
||||||
|
probs = probs[:, None, :].expand(probs.shape[0], num_samples,
|
||||||
|
probs.shape[1]).contiguous().view(
|
||||||
|
-1, probs.shape[1])
|
||||||
|
q = torch.empty_like(probs).exponential_(1)
|
||||||
|
return probs.div_(q).argmax(dim=1).view(-1, num_samples)
|
||||||
|
|
||||||
|
|
||||||
def _sample(
|
def _sample(
|
||||||
probs: torch.Tensor,
|
probs: torch.Tensor,
|
||||||
logprobs: torch.Tensor,
|
logprobs: torch.Tensor,
|
||||||
@@ -485,28 +368,51 @@ def _sample(
|
|||||||
categorized_seq_group_ids[sampling_type].append(i)
|
categorized_seq_group_ids[sampling_type].append(i)
|
||||||
|
|
||||||
sample_results_dict: Dict[int, Tuple[List[int], List[int]]] = {}
|
sample_results_dict: Dict[int, Tuple[List[int], List[int]]] = {}
|
||||||
|
sample_metadata = {}
|
||||||
|
|
||||||
|
# Counterintiutively, having two loops here is actually faster.
|
||||||
|
# The first loop can run without waiting on GPU<->CPU sync.
|
||||||
for sampling_type in SamplingType:
|
for sampling_type in SamplingType:
|
||||||
seq_group_ids = categorized_seq_group_ids[sampling_type]
|
|
||||||
seq_groups = [sampling_metadata.seq_groups[i] for i in seq_group_ids]
|
|
||||||
is_prompts = [i < sampling_metadata.num_prompts for i in seq_group_ids]
|
|
||||||
sample_indices = categorized_sample_indices[sampling_type]
|
sample_indices = categorized_sample_indices[sampling_type]
|
||||||
num_tokens = len(sample_indices)
|
num_tokens = len(sample_indices)
|
||||||
if num_tokens == 0:
|
if num_tokens == 0:
|
||||||
continue
|
continue
|
||||||
|
seq_group_ids = categorized_seq_group_ids[sampling_type]
|
||||||
|
seq_groups = [sampling_metadata.seq_groups[i] for i in seq_group_ids]
|
||||||
|
is_prompts = [i < sampling_metadata.num_prompts for i in seq_group_ids]
|
||||||
|
sample_metadata[sampling_type] = (seq_group_ids, seq_groups,
|
||||||
|
is_prompts, sample_indices)
|
||||||
if sampling_type == SamplingType.GREEDY:
|
if sampling_type == SamplingType.GREEDY:
|
||||||
category_logprobs = logprobs[sample_indices]
|
greedy_samples = torch.argmax(logprobs[sample_indices], dim=-1)
|
||||||
sample_results = _greedy_sample(seq_groups, category_logprobs)
|
|
||||||
elif sampling_type == SamplingType.RANDOM:
|
elif sampling_type == SamplingType.RANDOM:
|
||||||
category_probs = probs[sample_indices]
|
max_best_of = 1
|
||||||
sample_results = _random_sample(seq_groups, is_prompts,
|
for seq_group, is_prompt in zip(seq_groups, is_prompts):
|
||||||
category_probs)
|
if is_prompt:
|
||||||
|
_, sampling_params = seq_group
|
||||||
|
max_best_of = max(max_best_of, sampling_params.best_of)
|
||||||
|
multinomial_samples = _multinomial(probs[sample_indices],
|
||||||
|
max_best_of)
|
||||||
elif sampling_type == SamplingType.BEAM:
|
elif sampling_type == SamplingType.BEAM:
|
||||||
category_logprobs = logprobs[sample_indices]
|
beam_search_logprobs = logprobs[sample_indices]
|
||||||
sample_results = _beam_search_sample(seq_groups, is_prompts,
|
|
||||||
sampling_metadata.seq_data,
|
|
||||||
category_logprobs)
|
|
||||||
else:
|
else:
|
||||||
raise ValueError(f"Unsupported sampling type: {sampling_type}")
|
raise ValueError(f"Unsupported sampling type: {sampling_type}")
|
||||||
|
|
||||||
|
# GPU<->CPU sync happens in the loop below.
|
||||||
|
|
||||||
|
for sampling_type in SamplingType:
|
||||||
|
if sampling_type not in sample_metadata:
|
||||||
|
continue
|
||||||
|
seq_group_ids, seq_groups, is_prompts, sample_indices = sample_metadata[
|
||||||
|
sampling_type]
|
||||||
|
if sampling_type == SamplingType.GREEDY:
|
||||||
|
sample_results = _greedy_sample(seq_groups, greedy_samples)
|
||||||
|
elif sampling_type == SamplingType.RANDOM:
|
||||||
|
sample_results = _random_sample(seq_groups, is_prompts,
|
||||||
|
multinomial_samples)
|
||||||
|
elif sampling_type == SamplingType.BEAM:
|
||||||
|
sample_results = _beam_search_sample(seq_groups, is_prompts,
|
||||||
|
sampling_metadata.seq_data,
|
||||||
|
beam_search_logprobs)
|
||||||
sample_results_dict.update(zip(seq_group_ids, sample_results))
|
sample_results_dict.update(zip(seq_group_ids, sample_results))
|
||||||
|
|
||||||
sample_results = [
|
sample_results = [
|
||||||
@@ -557,7 +463,7 @@ def _get_logprobs(
|
|||||||
batched_logprobs_query_result = logprobs[[
|
batched_logprobs_query_result = logprobs[[
|
||||||
batched_logprobs_query_seq_indices,
|
batched_logprobs_query_seq_indices,
|
||||||
batched_logprobs_query_token_indices
|
batched_logprobs_query_token_indices
|
||||||
]].cpu()
|
]]
|
||||||
|
|
||||||
# Batched query for logprobs of topk tokens
|
# Batched query for logprobs of topk tokens
|
||||||
if largest_num_logprobs > 0:
|
if largest_num_logprobs > 0:
|
||||||
@@ -569,6 +475,8 @@ def _get_logprobs(
|
|||||||
else:
|
else:
|
||||||
top_logprobs, top_token_ids = None, None
|
top_logprobs, top_token_ids = None, None
|
||||||
|
|
||||||
|
batched_logprobs_query_result = batched_logprobs_query_result.cpu()
|
||||||
|
|
||||||
# Gather results
|
# Gather results
|
||||||
result_prompt_logprobs: List[Optional[PromptLogprobs]] = []
|
result_prompt_logprobs: List[Optional[PromptLogprobs]] = []
|
||||||
result_sample_logprobs: List[SampleLogprobs] = []
|
result_sample_logprobs: List[SampleLogprobs] = []
|
||||||
|
|||||||
@@ -7,54 +7,9 @@ import torch.nn as nn
|
|||||||
from transformers import PretrainedConfig
|
from transformers import PretrainedConfig
|
||||||
|
|
||||||
from vllm.config import ModelConfig
|
from vllm.config import ModelConfig
|
||||||
from vllm.model_executor.models import *
|
from vllm.model_executor.models import ModelRegistry
|
||||||
from vllm.model_executor.weight_utils import (get_quant_config,
|
from vllm.model_executor.weight_utils import (get_quant_config,
|
||||||
initialize_dummy_weights)
|
initialize_dummy_weights)
|
||||||
from vllm.utils import is_hip
|
|
||||||
from vllm.logger import init_logger
|
|
||||||
|
|
||||||
logger = init_logger(__name__)
|
|
||||||
|
|
||||||
# TODO(woosuk): Lazy-load the model classes.
|
|
||||||
_MODEL_REGISTRY = {
|
|
||||||
"AquilaModel": AquilaForCausalLM,
|
|
||||||
"AquilaForCausalLM": AquilaForCausalLM, # AquilaChat2
|
|
||||||
"BaiChuanForCausalLM": BaiChuanForCausalLM, # baichuan-7b
|
|
||||||
"BaichuanForCausalLM": BaichuanForCausalLM, # baichuan-13b
|
|
||||||
"BloomForCausalLM": BloomForCausalLM,
|
|
||||||
"ChatGLMModel": ChatGLMForCausalLM,
|
|
||||||
"ChatGLMForConditionalGeneration": ChatGLMForCausalLM,
|
|
||||||
"FalconForCausalLM": FalconForCausalLM,
|
|
||||||
"GPT2LMHeadModel": GPT2LMHeadModel,
|
|
||||||
"GPTBigCodeForCausalLM": GPTBigCodeForCausalLM,
|
|
||||||
"GPTJForCausalLM": GPTJForCausalLM,
|
|
||||||
"GPTNeoXForCausalLM": GPTNeoXForCausalLM,
|
|
||||||
"InternLMForCausalLM": InternLMForCausalLM,
|
|
||||||
"LlamaForCausalLM": LlamaForCausalLM,
|
|
||||||
"LLaMAForCausalLM": LlamaForCausalLM, # For decapoda-research/llama-*
|
|
||||||
"MistralForCausalLM": MistralForCausalLM,
|
|
||||||
"MixtralForCausalLM": MixtralForCausalLM,
|
|
||||||
# transformers's mpt class has lower case
|
|
||||||
"MptForCausalLM": MPTForCausalLM,
|
|
||||||
"MPTForCausalLM": MPTForCausalLM,
|
|
||||||
"OPTForCausalLM": OPTForCausalLM,
|
|
||||||
"PhiForCausalLM": PhiForCausalLM,
|
|
||||||
"QWenLMHeadModel": QWenLMHeadModel,
|
|
||||||
"RWForCausalLM": FalconForCausalLM,
|
|
||||||
"YiForCausalLM": YiForCausalLM,
|
|
||||||
}
|
|
||||||
|
|
||||||
# Models to be disabled in ROCm
|
|
||||||
_ROCM_UNSUPPORTED_MODELS = []
|
|
||||||
if is_hip():
|
|
||||||
for rocm_model in _ROCM_UNSUPPORTED_MODELS:
|
|
||||||
del _MODEL_REGISTRY[rocm_model]
|
|
||||||
|
|
||||||
# Models partially supported in ROCm
|
|
||||||
_ROCM_PARTIALLY_SUPPORTED_MODELS = {
|
|
||||||
"MistralForCausalLM":
|
|
||||||
"Sliding window attention is not supported in ROCm's flash attention",
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
@@ -69,19 +24,12 @@ def _set_default_torch_dtype(dtype: torch.dtype):
|
|||||||
def _get_model_architecture(config: PretrainedConfig) -> Type[nn.Module]:
|
def _get_model_architecture(config: PretrainedConfig) -> Type[nn.Module]:
|
||||||
architectures = getattr(config, "architectures", [])
|
architectures = getattr(config, "architectures", [])
|
||||||
for arch in architectures:
|
for arch in architectures:
|
||||||
if arch in _MODEL_REGISTRY:
|
model_cls = ModelRegistry.load_model_cls(arch)
|
||||||
if is_hip() and arch in _ROCM_PARTIALLY_SUPPORTED_MODELS:
|
if model_cls is not None:
|
||||||
logger.warning(
|
return model_cls
|
||||||
f"{arch} is not fully supported in ROCm. Reason: "
|
|
||||||
f"{_ROCM_PARTIALLY_SUPPORTED_MODELS[arch]}")
|
|
||||||
return _MODEL_REGISTRY[arch]
|
|
||||||
elif arch in _ROCM_UNSUPPORTED_MODELS:
|
|
||||||
raise ValueError(
|
|
||||||
f"Model architecture {arch} is not supported by ROCm for now. \n"
|
|
||||||
f"Supported architectures {list(_MODEL_REGISTRY.keys())}")
|
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f"Model architectures {architectures} are not supported for now. "
|
f"Model architectures {architectures} are not supported for now. "
|
||||||
f"Supported architectures: {list(_MODEL_REGISTRY.keys())}")
|
f"Supported architectures: {ModelRegistry.get_supported_archs()}")
|
||||||
|
|
||||||
|
|
||||||
def get_model(model_config: ModelConfig) -> nn.Module:
|
def get_model(model_config: ModelConfig) -> nn.Module:
|
||||||
|
|||||||
@@ -1,41 +1,83 @@
|
|||||||
from vllm.model_executor.models.aquila import AquilaForCausalLM
|
import importlib
|
||||||
from vllm.model_executor.models.baichuan import (BaiChuanForCausalLM,
|
from typing import List, Optional, Type
|
||||||
BaichuanForCausalLM)
|
|
||||||
from vllm.model_executor.models.bloom import BloomForCausalLM
|
import torch.nn as nn
|
||||||
from vllm.model_executor.models.falcon import FalconForCausalLM
|
|
||||||
from vllm.model_executor.models.gpt2 import GPT2LMHeadModel
|
from vllm.logger import init_logger
|
||||||
from vllm.model_executor.models.gpt_bigcode import GPTBigCodeForCausalLM
|
from vllm.utils import is_hip
|
||||||
from vllm.model_executor.models.gpt_j import GPTJForCausalLM
|
|
||||||
from vllm.model_executor.models.gpt_neox import GPTNeoXForCausalLM
|
logger = init_logger(__name__)
|
||||||
from vllm.model_executor.models.internlm import InternLMForCausalLM
|
|
||||||
from vllm.model_executor.models.llama import LlamaForCausalLM
|
# Architecture -> (module, class).
|
||||||
from vllm.model_executor.models.mistral import MistralForCausalLM
|
_MODELS = {
|
||||||
from vllm.model_executor.models.mixtral import MixtralForCausalLM
|
"AquilaModel": ("aquila", "AquilaForCausalLM"),
|
||||||
from vllm.model_executor.models.mpt import MPTForCausalLM
|
"AquilaForCausalLM": ("aquila", "AquilaForCausalLM"), # AquilaChat2
|
||||||
from vllm.model_executor.models.opt import OPTForCausalLM
|
"BaiChuanForCausalLM": ("baichuan", "BaiChuanForCausalLM"), # baichuan-7b
|
||||||
from vllm.model_executor.models.phi_1_5 import PhiForCausalLM
|
"BaichuanForCausalLM": ("baichuan", "BaichuanForCausalLM"), # baichuan-13b
|
||||||
from vllm.model_executor.models.qwen import QWenLMHeadModel
|
"BloomForCausalLM": ("bloom", "BloomForCausalLM"),
|
||||||
from vllm.model_executor.models.chatglm import ChatGLMForCausalLM
|
"ChatGLMModel": ("chatglm", "ChatGLMForCausalLM"),
|
||||||
from vllm.model_executor.models.yi import YiForCausalLM
|
"ChatGLMForConditionalGeneration": ("chatglm", "ChatGLMForCausalLM"),
|
||||||
|
"DeciLMForCausalLM": ("decilm", "DeciLMForCausalLM"),
|
||||||
|
"FalconForCausalLM": ("falcon", "FalconForCausalLM"),
|
||||||
|
"GPT2LMHeadModel": ("gpt2", "GPT2LMHeadModel"),
|
||||||
|
"GPTBigCodeForCausalLM": ("gpt_bigcode", "GPTBigCodeForCausalLM"),
|
||||||
|
"GPTJForCausalLM": ("gpt_j", "GPTJForCausalLM"),
|
||||||
|
"GPTNeoXForCausalLM": ("gpt_neox", "GPTNeoXForCausalLM"),
|
||||||
|
"InternLMForCausalLM": ("internlm", "InternLMForCausalLM"),
|
||||||
|
"LlamaForCausalLM": ("llama", "LlamaForCausalLM"),
|
||||||
|
# For decapoda-research/llama-*
|
||||||
|
"LLaMAForCausalLM": ("llama", "LlamaForCausalLM"),
|
||||||
|
"MistralForCausalLM": ("mistral", "MistralForCausalLM"),
|
||||||
|
"MixtralForCausalLM": ("mixtral", "MixtralForCausalLM"),
|
||||||
|
# transformers's mpt class has lower case
|
||||||
|
"MptForCausalLM": ("mpt", "MPTForCausalLM"),
|
||||||
|
"MPTForCausalLM": ("mpt", "MPTForCausalLM"),
|
||||||
|
"OPTForCausalLM": ("opt", "OPTForCausalLM"),
|
||||||
|
"PhiForCausalLM": ("phi_1_5", "PhiForCausalLM"),
|
||||||
|
"QWenLMHeadModel": ("qwen", "QWenLMHeadModel"),
|
||||||
|
"RWForCausalLM": ("falcon", "FalconForCausalLM"),
|
||||||
|
"YiForCausalLM": ("yi", "YiForCausalLM"),
|
||||||
|
}
|
||||||
|
|
||||||
|
# Models not supported by ROCm.
|
||||||
|
_ROCM_UNSUPPORTED_MODELS = []
|
||||||
|
|
||||||
|
# Models partially supported by ROCm.
|
||||||
|
# Architecture -> Reason.
|
||||||
|
_ROCM_PARTIALLY_SUPPORTED_MODELS = {
|
||||||
|
"MistralForCausalLM":
|
||||||
|
"Sliding window attention is not yet supported in ROCm's flash attention",
|
||||||
|
"MixtralForCausalLM":
|
||||||
|
"Sliding window attention is not yet supported in ROCm's flash attention",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class ModelRegistry:
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def load_model_cls(model_arch: str) -> Optional[Type[nn.Module]]:
|
||||||
|
if model_arch not in _MODELS:
|
||||||
|
return None
|
||||||
|
if is_hip():
|
||||||
|
if model_arch in _ROCM_UNSUPPORTED_MODELS:
|
||||||
|
raise ValueError(
|
||||||
|
f"Model architecture {model_arch} is not supported by "
|
||||||
|
"ROCm for now.")
|
||||||
|
if model_arch in _ROCM_PARTIALLY_SUPPORTED_MODELS:
|
||||||
|
logger.warning(
|
||||||
|
f"Model architecture {model_arch} is partially supported "
|
||||||
|
"by ROCm: " + _ROCM_PARTIALLY_SUPPORTED_MODELS[model_arch])
|
||||||
|
|
||||||
|
module_name, model_cls_name = _MODELS[model_arch]
|
||||||
|
module = importlib.import_module(
|
||||||
|
f"vllm.model_executor.models.{module_name}")
|
||||||
|
return getattr(module, model_cls_name, None)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_supported_archs() -> List[str]:
|
||||||
|
return list(_MODELS.keys())
|
||||||
|
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"AquilaForCausalLM",
|
"ModelRegistry",
|
||||||
"BaiChuanForCausalLM",
|
|
||||||
"BaichuanForCausalLM",
|
|
||||||
"BloomForCausalLM",
|
|
||||||
"ChatGLMForCausalLM",
|
|
||||||
"FalconForCausalLM",
|
|
||||||
"GPT2LMHeadModel",
|
|
||||||
"GPTBigCodeForCausalLM",
|
|
||||||
"GPTJForCausalLM",
|
|
||||||
"GPTNeoXForCausalLM",
|
|
||||||
"InternLMForCausalLM",
|
|
||||||
"LlamaForCausalLM",
|
|
||||||
"MPTForCausalLM",
|
|
||||||
"OPTForCausalLM",
|
|
||||||
"PhiForCausalLM",
|
|
||||||
"QWenLMHeadModel",
|
|
||||||
"MistralForCausalLM",
|
|
||||||
"MixtralForCausalLM",
|
|
||||||
"YiForCausalLM",
|
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -158,14 +158,12 @@ class AquilaAttention(nn.Module):
|
|||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
kv_cache: KVCache,
|
kv_cache: KVCache,
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_event: Optional[torch.cuda.Event],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
qkv, _ = self.qkv_proj(hidden_states)
|
qkv, _ = self.qkv_proj(hidden_states)
|
||||||
q, k, v = qkv.split([self.q_size, self.kv_size, self.kv_size], dim=-1)
|
q, k, v = qkv.split([self.q_size, self.kv_size, self.kv_size], dim=-1)
|
||||||
q, k = self.rotary_emb(positions, q, k)
|
q, k = self.rotary_emb(positions, q, k)
|
||||||
k_cache, v_cache = kv_cache
|
k_cache, v_cache = kv_cache
|
||||||
attn_output = self.attn(q, k, v, k_cache, v_cache, input_metadata,
|
attn_output = self.attn(q, k, v, k_cache, v_cache, input_metadata)
|
||||||
cache_event)
|
|
||||||
output, _ = self.o_proj(attn_output)
|
output, _ = self.o_proj(attn_output)
|
||||||
return output
|
return output
|
||||||
|
|
||||||
@@ -209,7 +207,6 @@ class AquilaDecoderLayer(nn.Module):
|
|||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
kv_cache: KVCache,
|
kv_cache: KVCache,
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_event: Optional[torch.cuda.Event],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
# Self Attention
|
# Self Attention
|
||||||
residual = hidden_states
|
residual = hidden_states
|
||||||
@@ -219,7 +216,6 @@ class AquilaDecoderLayer(nn.Module):
|
|||||||
hidden_states=hidden_states,
|
hidden_states=hidden_states,
|
||||||
kv_cache=kv_cache,
|
kv_cache=kv_cache,
|
||||||
input_metadata=input_metadata,
|
input_metadata=input_metadata,
|
||||||
cache_event=cache_event,
|
|
||||||
)
|
)
|
||||||
hidden_states = residual + hidden_states
|
hidden_states = residual + hidden_states
|
||||||
|
|
||||||
@@ -258,18 +254,15 @@ class AquilaModel(nn.Module):
|
|||||||
positions: torch.Tensor,
|
positions: torch.Tensor,
|
||||||
kv_caches: List[KVCache],
|
kv_caches: List[KVCache],
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_events: Optional[List[torch.cuda.Event]],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
hidden_states = self.embed_tokens(input_ids)
|
hidden_states = self.embed_tokens(input_ids)
|
||||||
for i in range(len(self.layers)):
|
for i in range(len(self.layers)):
|
||||||
cache_event = None if cache_events is None else cache_events[i]
|
|
||||||
layer = self.layers[i]
|
layer = self.layers[i]
|
||||||
hidden_states = layer(
|
hidden_states = layer(
|
||||||
positions,
|
positions,
|
||||||
hidden_states,
|
hidden_states,
|
||||||
kv_caches[i],
|
kv_caches[i],
|
||||||
input_metadata,
|
input_metadata,
|
||||||
cache_event,
|
|
||||||
)
|
)
|
||||||
hidden_states = self.norm(hidden_states)
|
hidden_states = self.norm(hidden_states)
|
||||||
|
|
||||||
@@ -296,17 +289,16 @@ class AquilaForCausalLM(nn.Module):
|
|||||||
positions: torch.Tensor,
|
positions: torch.Tensor,
|
||||||
kv_caches: List[KVCache],
|
kv_caches: List[KVCache],
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_events: Optional[List[torch.cuda.Event]],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
hidden_states = self.model(input_ids, positions, kv_caches,
|
hidden_states = self.model(input_ids, positions, kv_caches,
|
||||||
input_metadata, cache_events)
|
input_metadata)
|
||||||
return hidden_states
|
return hidden_states
|
||||||
|
|
||||||
def sample(
|
def sample(
|
||||||
self,
|
self,
|
||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
sampling_metadata: SamplingMetadata,
|
sampling_metadata: SamplingMetadata,
|
||||||
) -> SamplerOutput:
|
) -> Optional[SamplerOutput]:
|
||||||
next_tokens = self.sampler(self.lm_head.weight, hidden_states,
|
next_tokens = self.sampler(self.lm_head.weight, hidden_states,
|
||||||
sampling_metadata)
|
sampling_metadata)
|
||||||
return next_tokens
|
return next_tokens
|
||||||
@@ -332,11 +324,18 @@ class AquilaForCausalLM(nn.Module):
|
|||||||
for (param_name, weight_name, shard_id) in stacked_params_mapping:
|
for (param_name, weight_name, shard_id) in stacked_params_mapping:
|
||||||
if weight_name not in name:
|
if weight_name not in name:
|
||||||
continue
|
continue
|
||||||
param = params_dict[name.replace(weight_name, param_name)]
|
name = name.replace(weight_name, param_name)
|
||||||
|
# Skip loading extra bias for GPTQ models.
|
||||||
|
if name.endswith(".bias") and name not in params_dict:
|
||||||
|
continue
|
||||||
|
param = params_dict[name]
|
||||||
weight_loader = param.weight_loader
|
weight_loader = param.weight_loader
|
||||||
weight_loader(param, loaded_weight, shard_id)
|
weight_loader(param, loaded_weight, shard_id)
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
|
# Skip loading extra bias for GPTQ models.
|
||||||
|
if name.endswith(".bias") and name not in params_dict:
|
||||||
|
continue
|
||||||
param = params_dict[name]
|
param = params_dict[name]
|
||||||
weight_loader = getattr(param, "weight_loader",
|
weight_loader = getattr(param, "weight_loader",
|
||||||
default_weight_loader)
|
default_weight_loader)
|
||||||
|
|||||||
@@ -172,15 +172,13 @@ class BaiChuanAttention(nn.Module):
|
|||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
kv_cache: KVCache,
|
kv_cache: KVCache,
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_event: Optional[torch.cuda.Event],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
qkv, _ = self.W_pack(hidden_states)
|
qkv, _ = self.W_pack(hidden_states)
|
||||||
q, k, v = qkv.chunk(chunks=3, dim=-1)
|
q, k, v = qkv.chunk(chunks=3, dim=-1)
|
||||||
if self.postion_embedding != "ALIBI":
|
if self.postion_embedding != "ALIBI":
|
||||||
q, k = self.rotary_emb(positions, q, k)
|
q, k = self.rotary_emb(positions, q, k)
|
||||||
k_cache, v_cache = kv_cache
|
k_cache, v_cache = kv_cache
|
||||||
attn_output = self.attn(q, k, v, k_cache, v_cache, input_metadata,
|
attn_output = self.attn(q, k, v, k_cache, v_cache, input_metadata)
|
||||||
cache_event)
|
|
||||||
output, _ = self.o_proj(attn_output)
|
output, _ = self.o_proj(attn_output)
|
||||||
return output
|
return output
|
||||||
|
|
||||||
@@ -221,7 +219,6 @@ class BaiChuanDecoderLayer(nn.Module):
|
|||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
kv_cache: KVCache,
|
kv_cache: KVCache,
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_event: Optional[torch.cuda.Event],
|
|
||||||
residual: Optional[torch.Tensor],
|
residual: Optional[torch.Tensor],
|
||||||
) -> Tuple[torch.Tensor, torch.Tensor]:
|
) -> Tuple[torch.Tensor, torch.Tensor]:
|
||||||
# Self Attention
|
# Self Attention
|
||||||
@@ -236,7 +233,6 @@ class BaiChuanDecoderLayer(nn.Module):
|
|||||||
hidden_states=hidden_states,
|
hidden_states=hidden_states,
|
||||||
kv_cache=kv_cache,
|
kv_cache=kv_cache,
|
||||||
input_metadata=input_metadata,
|
input_metadata=input_metadata,
|
||||||
cache_event=cache_event,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Fully Connected
|
# Fully Connected
|
||||||
@@ -273,19 +269,16 @@ class BaiChuanModel(nn.Module):
|
|||||||
positions: torch.Tensor,
|
positions: torch.Tensor,
|
||||||
kv_caches: List[KVCache],
|
kv_caches: List[KVCache],
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_events: Optional[List[torch.cuda.Event]],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
hidden_states = self.embed_tokens(input_ids)
|
hidden_states = self.embed_tokens(input_ids)
|
||||||
residual = None
|
residual = None
|
||||||
for i in range(len(self.layers)):
|
for i in range(len(self.layers)):
|
||||||
cache_event = None if cache_events is None else cache_events[i]
|
|
||||||
layer = self.layers[i]
|
layer = self.layers[i]
|
||||||
hidden_states, residual = layer(
|
hidden_states, residual = layer(
|
||||||
positions,
|
positions,
|
||||||
hidden_states,
|
hidden_states,
|
||||||
kv_caches[i],
|
kv_caches[i],
|
||||||
input_metadata,
|
input_metadata,
|
||||||
cache_event,
|
|
||||||
residual,
|
residual,
|
||||||
)
|
)
|
||||||
hidden_states, _ = self.norm(hidden_states, residual)
|
hidden_states, _ = self.norm(hidden_states, residual)
|
||||||
@@ -311,17 +304,16 @@ class BaiChuanBaseForCausalLM(nn.Module):
|
|||||||
positions: torch.Tensor,
|
positions: torch.Tensor,
|
||||||
kv_caches: List[KVCache],
|
kv_caches: List[KVCache],
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_events: Optional[List[torch.cuda.Event]],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
hidden_states = self.model(input_ids, positions, kv_caches,
|
hidden_states = self.model(input_ids, positions, kv_caches,
|
||||||
input_metadata, cache_events)
|
input_metadata)
|
||||||
return hidden_states
|
return hidden_states
|
||||||
|
|
||||||
def sample(
|
def sample(
|
||||||
self,
|
self,
|
||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
sampling_metadata: SamplingMetadata,
|
sampling_metadata: SamplingMetadata,
|
||||||
) -> SamplerOutput:
|
) -> Optional[SamplerOutput]:
|
||||||
next_tokens = self.sampler(self.lm_head.weight, hidden_states,
|
next_tokens = self.sampler(self.lm_head.weight, hidden_states,
|
||||||
sampling_metadata)
|
sampling_metadata)
|
||||||
return next_tokens
|
return next_tokens
|
||||||
@@ -355,11 +347,18 @@ class BaiChuanBaseForCausalLM(nn.Module):
|
|||||||
for (param_name, weight_name, shard_id) in stacked_params_mapping:
|
for (param_name, weight_name, shard_id) in stacked_params_mapping:
|
||||||
if weight_name not in name:
|
if weight_name not in name:
|
||||||
continue
|
continue
|
||||||
param = params_dict[name.replace(weight_name, param_name)]
|
name = name.replace(weight_name, param_name)
|
||||||
|
# Skip loading extra bias for GPTQ models.
|
||||||
|
if name.endswith(".bias") and name not in params_dict:
|
||||||
|
continue
|
||||||
|
param = params_dict[name]
|
||||||
weight_loader = param.weight_loader
|
weight_loader = param.weight_loader
|
||||||
weight_loader(param, loaded_weight, shard_id)
|
weight_loader(param, loaded_weight, shard_id)
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
|
# Skip loading extra bias for GPTQ models.
|
||||||
|
if name.endswith(".bias") and name not in params_dict:
|
||||||
|
continue
|
||||||
param = params_dict[name]
|
param = params_dict[name]
|
||||||
weight_loader = getattr(param, "weight_loader",
|
weight_loader = getattr(param, "weight_loader",
|
||||||
default_weight_loader)
|
default_weight_loader)
|
||||||
|
|||||||
@@ -118,14 +118,12 @@ class BloomAttention(nn.Module):
|
|||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
kv_cache: KVCache,
|
kv_cache: KVCache,
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_event: Optional[torch.cuda.Event],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
del position_ids # Unused.
|
del position_ids # Unused.
|
||||||
qkv, _ = self.query_key_value(hidden_states)
|
qkv, _ = self.query_key_value(hidden_states)
|
||||||
q, k, v = qkv.chunk(chunks=3, dim=-1)
|
q, k, v = qkv.chunk(chunks=3, dim=-1)
|
||||||
k_cache, v_cache = kv_cache
|
k_cache, v_cache = kv_cache
|
||||||
attn_output = self.attn(q, k, v, k_cache, v_cache, input_metadata,
|
attn_output = self.attn(q, k, v, k_cache, v_cache, input_metadata)
|
||||||
cache_event)
|
|
||||||
output, _ = self.dense(attn_output)
|
output, _ = self.dense(attn_output)
|
||||||
return output
|
return output
|
||||||
|
|
||||||
@@ -184,7 +182,6 @@ class BloomBlock(nn.Module):
|
|||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
kv_cache: KVCache,
|
kv_cache: KVCache,
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_event: Optional[torch.cuda.Event],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
# Layer norm at the beginning of the transformer layer.
|
# Layer norm at the beginning of the transformer layer.
|
||||||
layernorm_output = self.input_layernorm(hidden_states)
|
layernorm_output = self.input_layernorm(hidden_states)
|
||||||
@@ -201,7 +198,6 @@ class BloomBlock(nn.Module):
|
|||||||
hidden_states=layernorm_output,
|
hidden_states=layernorm_output,
|
||||||
kv_cache=kv_cache,
|
kv_cache=kv_cache,
|
||||||
input_metadata=input_metadata,
|
input_metadata=input_metadata,
|
||||||
cache_event=cache_event,
|
|
||||||
)
|
)
|
||||||
attention_output = attention_output + residual
|
attention_output = attention_output + residual
|
||||||
layernorm_output = self.post_attention_layernorm(attention_output)
|
layernorm_output = self.post_attention_layernorm(attention_output)
|
||||||
@@ -250,19 +246,16 @@ class BloomModel(nn.Module):
|
|||||||
position_ids: torch.Tensor,
|
position_ids: torch.Tensor,
|
||||||
kv_caches: List[KVCache],
|
kv_caches: List[KVCache],
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_events: Optional[List[torch.cuda.Event]],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
hidden_states = self.word_embeddings(input_ids)
|
hidden_states = self.word_embeddings(input_ids)
|
||||||
hidden_states = self.word_embeddings_layernorm(hidden_states)
|
hidden_states = self.word_embeddings_layernorm(hidden_states)
|
||||||
for i in range(len(self.h)):
|
for i in range(len(self.h)):
|
||||||
cache_event = None if cache_events is None else cache_events[i]
|
|
||||||
layer = self.h[i]
|
layer = self.h[i]
|
||||||
hidden_states = layer(
|
hidden_states = layer(
|
||||||
position_ids,
|
position_ids,
|
||||||
hidden_states,
|
hidden_states,
|
||||||
kv_caches[i],
|
kv_caches[i],
|
||||||
input_metadata,
|
input_metadata,
|
||||||
cache_event,
|
|
||||||
)
|
)
|
||||||
hidden_states = self.ln_f(hidden_states)
|
hidden_states = self.ln_f(hidden_states)
|
||||||
return hidden_states
|
return hidden_states
|
||||||
@@ -288,17 +281,16 @@ class BloomForCausalLM(nn.Module):
|
|||||||
positions: torch.Tensor,
|
positions: torch.Tensor,
|
||||||
kv_caches: List[KVCache],
|
kv_caches: List[KVCache],
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_events: Optional[List[torch.cuda.Event]],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
hidden_states = self.transformer(input_ids, positions, kv_caches,
|
hidden_states = self.transformer(input_ids, positions, kv_caches,
|
||||||
input_metadata, cache_events)
|
input_metadata)
|
||||||
return hidden_states
|
return hidden_states
|
||||||
|
|
||||||
def sample(
|
def sample(
|
||||||
self,
|
self,
|
||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
sampling_metadata: SamplingMetadata,
|
sampling_metadata: SamplingMetadata,
|
||||||
) -> SamplerOutput:
|
) -> Optional[SamplerOutput]:
|
||||||
next_tokens = self.sampler(self.lm_head_weight, hidden_states,
|
next_tokens = self.sampler(self.lm_head_weight, hidden_states,
|
||||||
sampling_metadata)
|
sampling_metadata)
|
||||||
return next_tokens
|
return next_tokens
|
||||||
|
|||||||
@@ -100,7 +100,6 @@ class GLMAttention(nn.Module):
|
|||||||
position_ids: torch.Tensor,
|
position_ids: torch.Tensor,
|
||||||
kv_cache: KVCache,
|
kv_cache: KVCache,
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_event: Optional[torch.cuda.Event],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
qkv, _ = self.query_key_value(hidden_states)
|
qkv, _ = self.query_key_value(hidden_states)
|
||||||
q, k, v = qkv.split([self.q_size, self.kv_size, self.kv_size], dim=-1)
|
q, k, v = qkv.split([self.q_size, self.kv_size, self.kv_size], dim=-1)
|
||||||
@@ -113,7 +112,6 @@ class GLMAttention(nn.Module):
|
|||||||
key_cache,
|
key_cache,
|
||||||
value_cache,
|
value_cache,
|
||||||
input_metadata,
|
input_metadata,
|
||||||
cache_event,
|
|
||||||
)
|
)
|
||||||
attn_output, _ = self.dense(context_layer)
|
attn_output, _ = self.dense(context_layer)
|
||||||
return attn_output
|
return attn_output
|
||||||
@@ -203,7 +201,6 @@ class GLMBlock(nn.Module):
|
|||||||
position_ids: torch.Tensor,
|
position_ids: torch.Tensor,
|
||||||
kv_cache: KVCache,
|
kv_cache: KVCache,
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_event: Optional[torch.cuda.Event],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
# hidden_states: [num_tokens, h]
|
# hidden_states: [num_tokens, h]
|
||||||
# Layer norm at the beginning of the transformer layer.
|
# Layer norm at the beginning of the transformer layer.
|
||||||
@@ -214,7 +211,6 @@ class GLMBlock(nn.Module):
|
|||||||
position_ids=position_ids,
|
position_ids=position_ids,
|
||||||
kv_cache=kv_cache,
|
kv_cache=kv_cache,
|
||||||
input_metadata=input_metadata,
|
input_metadata=input_metadata,
|
||||||
cache_event=cache_event,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Residual connection.
|
# Residual connection.
|
||||||
@@ -269,17 +265,14 @@ class GLMTransformer(nn.Module):
|
|||||||
position_ids: torch.Tensor,
|
position_ids: torch.Tensor,
|
||||||
kv_caches: List[KVCache],
|
kv_caches: List[KVCache],
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_events: Optional[List[torch.cuda.Event]],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
for i in range(self.num_layers):
|
for i in range(self.num_layers):
|
||||||
cache_event = None if cache_events is None else cache_events[i]
|
|
||||||
layer = self.layers[i]
|
layer = self.layers[i]
|
||||||
hidden_states = layer(
|
hidden_states = layer(
|
||||||
hidden_states=hidden_states,
|
hidden_states=hidden_states,
|
||||||
position_ids=position_ids,
|
position_ids=position_ids,
|
||||||
kv_cache=kv_caches[i],
|
kv_cache=kv_caches[i],
|
||||||
input_metadata=input_metadata,
|
input_metadata=input_metadata,
|
||||||
cache_event=cache_event,
|
|
||||||
)
|
)
|
||||||
# Final layer norm.
|
# Final layer norm.
|
||||||
if self.post_layer_norm:
|
if self.post_layer_norm:
|
||||||
@@ -314,8 +307,7 @@ class ChatGLMModel(nn.Module):
|
|||||||
position_ids: torch.Tensor,
|
position_ids: torch.Tensor,
|
||||||
kv_caches: List[KVCache],
|
kv_caches: List[KVCache],
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_events: Optional[List[torch.cuda.Event]],
|
) -> torch.Tensor:
|
||||||
):
|
|
||||||
inputs_embeds = self.embedding(input_ids)
|
inputs_embeds = self.embedding(input_ids)
|
||||||
|
|
||||||
# Run encoder.
|
# Run encoder.
|
||||||
@@ -324,9 +316,7 @@ class ChatGLMModel(nn.Module):
|
|||||||
position_ids=position_ids,
|
position_ids=position_ids,
|
||||||
kv_caches=kv_caches,
|
kv_caches=kv_caches,
|
||||||
input_metadata=input_metadata,
|
input_metadata=input_metadata,
|
||||||
cache_events=cache_events,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
return hidden_states
|
return hidden_states
|
||||||
|
|
||||||
|
|
||||||
@@ -350,17 +340,16 @@ class ChatGLMForCausalLM(nn.Module):
|
|||||||
positions: torch.Tensor,
|
positions: torch.Tensor,
|
||||||
kv_caches: List[KVCache],
|
kv_caches: List[KVCache],
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_events: Optional[List[torch.cuda.Event]],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
hidden_states = self.transformer(input_ids, positions, kv_caches,
|
hidden_states = self.transformer(input_ids, positions, kv_caches,
|
||||||
input_metadata, cache_events)
|
input_metadata)
|
||||||
return hidden_states
|
return hidden_states
|
||||||
|
|
||||||
def sample(
|
def sample(
|
||||||
self,
|
self,
|
||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
sampling_metadata: SamplingMetadata,
|
sampling_metadata: SamplingMetadata,
|
||||||
) -> SamplerOutput:
|
) -> Optional[SamplerOutput]:
|
||||||
next_tokens = self.sampler(self.lm_head_weight, hidden_states,
|
next_tokens = self.sampler(self.lm_head_weight, hidden_states,
|
||||||
sampling_metadata)
|
sampling_metadata)
|
||||||
return next_tokens
|
return next_tokens
|
||||||
@@ -377,6 +366,9 @@ class ChatGLMForCausalLM(nn.Module):
|
|||||||
continue
|
continue
|
||||||
if "word_embeddings" in name:
|
if "word_embeddings" in name:
|
||||||
name = name.replace(".word_embeddings", "")
|
name = name.replace(".word_embeddings", "")
|
||||||
|
# Skip loading extra bias for GPTQ models.
|
||||||
|
if name.endswith(".bias") and name not in params_dict:
|
||||||
|
continue
|
||||||
param = params_dict[name]
|
param = params_dict[name]
|
||||||
weight_loader = getattr(param, "weight_loader",
|
weight_loader = getattr(param, "weight_loader",
|
||||||
default_weight_loader)
|
default_weight_loader)
|
||||||
|
|||||||
123
vllm/model_executor/models/decilm.py
Normal file
123
vllm/model_executor/models/decilm.py
Normal file
@@ -0,0 +1,123 @@
|
|||||||
|
# coding=utf-8
|
||||||
|
# Adapted from
|
||||||
|
# https://github.com/huggingface/transformers/blob/v4.28.0/src/transformers/models/llama/modeling_llama.py
|
||||||
|
# Copyright 2023 DeciAI Research Team. All rights reserved.
|
||||||
|
# Copyright 2023 The vLLM team.
|
||||||
|
# Copyright 2022 EleutherAI and the HuggingFace Inc. team. All rights reserved.
|
||||||
|
#
|
||||||
|
# This code is based on MistralAI GPT-NeoX library and the GPT-NeoX
|
||||||
|
# and OPT implementations in this library. It has been modified from its
|
||||||
|
# original forms to accommodate minor architectural differences compared
|
||||||
|
# to GPT-NeoX and OPT used by the Meta AI team that trained the model.
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
"""Inference-only DeciLM model compatible with HuggingFace weights."""
|
||||||
|
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
import torch
|
||||||
|
from transformers import PretrainedConfig
|
||||||
|
|
||||||
|
from vllm.model_executor.layers.linear import LinearMethodBase
|
||||||
|
from vllm.model_executor.models.llama import LlamaForCausalLM
|
||||||
|
from vllm.model_executor.weight_utils import (default_weight_loader,
|
||||||
|
hf_model_weights_iterator)
|
||||||
|
|
||||||
|
|
||||||
|
class DeciLMForCausalLM(LlamaForCausalLM):
|
||||||
|
"""
|
||||||
|
Implementation for https://huggingface.co/Deci/DeciLM-7b-instruct.
|
||||||
|
Based on the llama executor.
|
||||||
|
|
||||||
|
The main difference is that DeciLM uses Variable Grouped Query Attention.
|
||||||
|
The constant number of GQA heads in the decoder is overriden with a value
|
||||||
|
per layer.
|
||||||
|
|
||||||
|
Usually, in the HuggingFace implementation, instead of
|
||||||
|
"config.num_key_value_heads", we use
|
||||||
|
"config.num_key_value_heads_per_layer[i]" which varies.
|
||||||
|
|
||||||
|
Currently, PagedAttention does not work well with variable GQA, so we
|
||||||
|
normalize the weights upon loading, and use uniform GQA with the max value
|
||||||
|
instead.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
config: Optional[PretrainedConfig] = None,
|
||||||
|
linear_method: Optional[LinearMethodBase] = None,
|
||||||
|
) -> None:
|
||||||
|
config.num_key_value_heads = max(config.num_key_value_heads_per_layer)
|
||||||
|
delattr(config, "num_key_value_heads_per_layer")
|
||||||
|
super().__init__(config=config, linear_method=linear_method)
|
||||||
|
|
||||||
|
def load_weights(self,
|
||||||
|
model_name_or_path: str,
|
||||||
|
cache_dir: Optional[str] = None,
|
||||||
|
load_format: str = "auto",
|
||||||
|
revision: Optional[str] = None):
|
||||||
|
stacked_params_mapping = [
|
||||||
|
# (param_name, shard_name, shard_id)
|
||||||
|
("qkv_proj", "q_proj", "q"),
|
||||||
|
("qkv_proj", "k_proj", "k"),
|
||||||
|
("qkv_proj", "v_proj", "v"),
|
||||||
|
("gate_up_proj", "gate_proj", 0),
|
||||||
|
("gate_up_proj", "up_proj", 1),
|
||||||
|
]
|
||||||
|
params_dict = dict(self.named_parameters())
|
||||||
|
for name, loaded_weight in hf_model_weights_iterator(
|
||||||
|
model_name_or_path, cache_dir, load_format, revision):
|
||||||
|
if "rotary_emb.inv_freq" in name:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if "k_proj" in name or "v_proj" in name:
|
||||||
|
loaded_weight = self._degroup_weight(loaded_weight)
|
||||||
|
|
||||||
|
for (param_name, weight_name, shard_id) in stacked_params_mapping:
|
||||||
|
if weight_name not in name:
|
||||||
|
continue
|
||||||
|
name = name.replace(weight_name, param_name)
|
||||||
|
# Skip loading extra bias for GPTQ models.
|
||||||
|
if name.endswith(".bias") and name not in params_dict:
|
||||||
|
continue
|
||||||
|
param = params_dict[name]
|
||||||
|
weight_loader = param.weight_loader
|
||||||
|
weight_loader(param, loaded_weight, shard_id)
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
# Skip loading extra bias for GPTQ models.
|
||||||
|
if name.endswith(".bias") and name not in params_dict:
|
||||||
|
continue
|
||||||
|
param = params_dict[name]
|
||||||
|
weight_loader = getattr(param, "weight_loader",
|
||||||
|
default_weight_loader)
|
||||||
|
weight_loader(param, loaded_weight)
|
||||||
|
|
||||||
|
def _degroup_weight(self, loaded_weight: torch.Tensor) -> torch.Tensor:
|
||||||
|
hidden_size = self.config.hidden_size
|
||||||
|
head_size = self.config.hidden_size // self.config.num_attention_heads
|
||||||
|
target_num_kv_heads = self.config.num_key_value_heads
|
||||||
|
num_kv_heads = loaded_weight.shape[0] // head_size
|
||||||
|
n_repeats = target_num_kv_heads / num_kv_heads
|
||||||
|
assert n_repeats == int(n_repeats)
|
||||||
|
|
||||||
|
n_repeats = int(n_repeats)
|
||||||
|
loaded_weight = loaded_weight.view(num_kv_heads, head_size,
|
||||||
|
hidden_size)
|
||||||
|
loaded_weight = torch.repeat_interleave(loaded_weight,
|
||||||
|
repeats=n_repeats,
|
||||||
|
dim=0)
|
||||||
|
loaded_weight = loaded_weight.reshape(target_num_kv_heads * head_size,
|
||||||
|
hidden_size)
|
||||||
|
|
||||||
|
return loaded_weight
|
||||||
@@ -178,7 +178,6 @@ class FalconAttention(nn.Module):
|
|||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
kv_cache: KVCache,
|
kv_cache: KVCache,
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_event: Optional[torch.cuda.Event],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
qkv, bias = self.query_key_value(hidden_states)
|
qkv, bias = self.query_key_value(hidden_states)
|
||||||
if bias is not None:
|
if bias is not None:
|
||||||
@@ -187,8 +186,7 @@ class FalconAttention(nn.Module):
|
|||||||
if self.use_rotary:
|
if self.use_rotary:
|
||||||
q, k = self.rotary_emb(positions, q, k)
|
q, k = self.rotary_emb(positions, q, k)
|
||||||
k_cache, v_cache = kv_cache
|
k_cache, v_cache = kv_cache
|
||||||
attn_output = self.attn(q, k, v, k_cache, v_cache, input_metadata,
|
attn_output = self.attn(q, k, v, k_cache, v_cache, input_metadata)
|
||||||
cache_event)
|
|
||||||
attn_output, bias = self.dense(attn_output)
|
attn_output, bias = self.dense(attn_output)
|
||||||
return attn_output, bias
|
return attn_output, bias
|
||||||
|
|
||||||
@@ -266,8 +264,7 @@ class FalconDecoderLayer(nn.Module):
|
|||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
kv_cache: KVCache,
|
kv_cache: KVCache,
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_event: Optional[torch.cuda.Event],
|
) -> torch.Tensor:
|
||||||
):
|
|
||||||
residual = hidden_states
|
residual = hidden_states
|
||||||
|
|
||||||
if self.config.new_decoder_architecture:
|
if self.config.new_decoder_architecture:
|
||||||
@@ -282,7 +279,6 @@ class FalconDecoderLayer(nn.Module):
|
|||||||
hidden_states=attention_layernorm_out,
|
hidden_states=attention_layernorm_out,
|
||||||
kv_cache=kv_cache,
|
kv_cache=kv_cache,
|
||||||
input_metadata=input_metadata,
|
input_metadata=input_metadata,
|
||||||
cache_event=cache_event,
|
|
||||||
)
|
)
|
||||||
if self.reduce_row_parallel_results and attention_bias is not None:
|
if self.reduce_row_parallel_results and attention_bias is not None:
|
||||||
attention_output += attention_bias
|
attention_output += attention_bias
|
||||||
@@ -311,7 +307,6 @@ class FalconDecoderLayer(nn.Module):
|
|||||||
mlp_output += mlp_bias
|
mlp_output += mlp_bias
|
||||||
|
|
||||||
output = mlp_output + residual
|
output = mlp_output + residual
|
||||||
|
|
||||||
return output
|
return output
|
||||||
|
|
||||||
|
|
||||||
@@ -349,18 +344,15 @@ class FalconModel(nn.Module):
|
|||||||
positions: torch.Tensor,
|
positions: torch.Tensor,
|
||||||
kv_caches: List[KVCache],
|
kv_caches: List[KVCache],
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_events: Optional[List[torch.cuda.Event]],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
hidden_states = self.word_embeddings(input_ids)
|
hidden_states = self.word_embeddings(input_ids)
|
||||||
for i in range(len(self.h)):
|
for i in range(len(self.h)):
|
||||||
cache_event = None if cache_events is None else cache_events[i]
|
|
||||||
layer = self.h[i]
|
layer = self.h[i]
|
||||||
hidden_states = layer(
|
hidden_states = layer(
|
||||||
positions,
|
positions,
|
||||||
hidden_states,
|
hidden_states,
|
||||||
kv_caches[i],
|
kv_caches[i],
|
||||||
input_metadata,
|
input_metadata,
|
||||||
cache_event,
|
|
||||||
)
|
)
|
||||||
hidden_states = self.ln_f(hidden_states)
|
hidden_states = self.ln_f(hidden_states)
|
||||||
return hidden_states
|
return hidden_states
|
||||||
@@ -389,14 +381,12 @@ class FalconForCausalLM(nn.Module):
|
|||||||
positions: torch.Tensor,
|
positions: torch.Tensor,
|
||||||
kv_caches: List[KVCache],
|
kv_caches: List[KVCache],
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_events: Optional[List[torch.cuda.Event]],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
hidden_states = self.transformer(
|
hidden_states = self.transformer(
|
||||||
input_ids,
|
input_ids,
|
||||||
positions,
|
positions,
|
||||||
kv_caches,
|
kv_caches,
|
||||||
input_metadata,
|
input_metadata,
|
||||||
cache_events,
|
|
||||||
)
|
)
|
||||||
return hidden_states
|
return hidden_states
|
||||||
|
|
||||||
@@ -404,7 +394,7 @@ class FalconForCausalLM(nn.Module):
|
|||||||
self,
|
self,
|
||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
sampling_metadata: SamplingMetadata,
|
sampling_metadata: SamplingMetadata,
|
||||||
) -> SamplerOutput:
|
) -> Optional[SamplerOutput]:
|
||||||
next_tokens = self.sampler(self.lm_head.weight, hidden_states,
|
next_tokens = self.sampler(self.lm_head.weight, hidden_states,
|
||||||
sampling_metadata)
|
sampling_metadata)
|
||||||
return next_tokens
|
return next_tokens
|
||||||
@@ -425,16 +415,21 @@ class FalconForCausalLM(nn.Module):
|
|||||||
params_dict = dict(self.named_parameters())
|
params_dict = dict(self.named_parameters())
|
||||||
for name, loaded_weight in hf_model_weights_iterator(
|
for name, loaded_weight in hf_model_weights_iterator(
|
||||||
model_name_or_path, cache_dir, load_format, revision):
|
model_name_or_path, cache_dir, load_format, revision):
|
||||||
|
# Skip loading extra bias for GPTQ models.
|
||||||
|
if name.endswith(".bias") and name not in params_dict:
|
||||||
|
continue
|
||||||
param = params_dict[name]
|
param = params_dict[name]
|
||||||
if "query_key_value" in name:
|
if "query_key_value" in name:
|
||||||
output_dim = getattr(param, "output_dim", None)
|
output_dim = getattr(param, "output_dim", None)
|
||||||
loaded_weight_shape = loaded_weight.shape
|
loaded_weight_shape = loaded_weight.shape
|
||||||
|
if output_dim is not None:
|
||||||
loaded_weight = loaded_weight.view(
|
loaded_weight = loaded_weight.view(
|
||||||
loaded_weight_shape[:output_dim] +
|
loaded_weight_shape[:output_dim] +
|
||||||
(total_num_kv_heads, num_query_heads_per_kv_head + 2, -1) +
|
(total_num_kv_heads, num_query_heads_per_kv_head + 2,
|
||||||
loaded_weight_shape[output_dim + 1:])
|
-1) + loaded_weight_shape[output_dim + 1:])
|
||||||
wq = loaded_weight.narrow(
|
wq = loaded_weight.narrow(
|
||||||
output_dim + 1, 0, num_query_heads_per_kv_head).reshape(
|
output_dim + 1, 0,
|
||||||
|
num_query_heads_per_kv_head).reshape(
|
||||||
*loaded_weight_shape[:output_dim], -1,
|
*loaded_weight_shape[:output_dim], -1,
|
||||||
*loaded_weight_shape[output_dim + 1:])
|
*loaded_weight_shape[output_dim + 1:])
|
||||||
wk = loaded_weight.narrow(
|
wk = loaded_weight.narrow(
|
||||||
|
|||||||
@@ -82,13 +82,12 @@ class GPT2Attention(nn.Module):
|
|||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
kv_cache: KVCache,
|
kv_cache: KVCache,
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_event: Optional[torch.cuda.Event],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
qkv, _ = self.c_attn(hidden_states)
|
qkv, _ = self.c_attn(hidden_states)
|
||||||
q, k, v = qkv.chunk(chunks=3, dim=-1)
|
q, k, v = qkv.chunk(chunks=3, dim=-1)
|
||||||
key_cache, value_cache = kv_cache
|
key_cache, value_cache = kv_cache
|
||||||
attn_output = self.attn(q, k, v, key_cache, value_cache,
|
attn_output = self.attn(q, k, v, key_cache, value_cache,
|
||||||
input_metadata, cache_event)
|
input_metadata)
|
||||||
attn_output, _ = self.c_proj(attn_output)
|
attn_output, _ = self.c_proj(attn_output)
|
||||||
return attn_output
|
return attn_output
|
||||||
|
|
||||||
@@ -148,7 +147,6 @@ class GPT2Block(nn.Module):
|
|||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
kv_cache: KVCache,
|
kv_cache: KVCache,
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_event: Optional[torch.cuda.Event],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
residual = hidden_states
|
residual = hidden_states
|
||||||
hidden_states = self.ln_1(hidden_states)
|
hidden_states = self.ln_1(hidden_states)
|
||||||
@@ -156,7 +154,6 @@ class GPT2Block(nn.Module):
|
|||||||
hidden_states=hidden_states,
|
hidden_states=hidden_states,
|
||||||
kv_cache=kv_cache,
|
kv_cache=kv_cache,
|
||||||
input_metadata=input_metadata,
|
input_metadata=input_metadata,
|
||||||
cache_event=cache_event,
|
|
||||||
)
|
)
|
||||||
# residual connection
|
# residual connection
|
||||||
hidden_states = attn_output + residual
|
hidden_states = attn_output + residual
|
||||||
@@ -196,17 +193,14 @@ class GPT2Model(nn.Module):
|
|||||||
position_ids: torch.Tensor,
|
position_ids: torch.Tensor,
|
||||||
kv_caches: List[KVCache],
|
kv_caches: List[KVCache],
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_events: Optional[List[torch.cuda.Event]],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
inputs_embeds = self.wte(input_ids)
|
inputs_embeds = self.wte(input_ids)
|
||||||
position_embeds = self.wpe(position_ids)
|
position_embeds = self.wpe(position_ids)
|
||||||
hidden_states = inputs_embeds + position_embeds
|
hidden_states = inputs_embeds + position_embeds
|
||||||
|
|
||||||
for i in range(len(self.h)):
|
for i in range(len(self.h)):
|
||||||
cache_event = None if cache_events is None else cache_events[i]
|
|
||||||
layer = self.h[i]
|
layer = self.h[i]
|
||||||
hidden_states = layer(hidden_states, kv_caches[i], input_metadata,
|
hidden_states = layer(hidden_states, kv_caches[i], input_metadata)
|
||||||
cache_event)
|
|
||||||
|
|
||||||
hidden_states = self.ln_f(hidden_states)
|
hidden_states = self.ln_f(hidden_states)
|
||||||
return hidden_states
|
return hidden_states
|
||||||
@@ -232,17 +226,16 @@ class GPT2LMHeadModel(nn.Module):
|
|||||||
positions: torch.Tensor,
|
positions: torch.Tensor,
|
||||||
kv_caches: List[KVCache],
|
kv_caches: List[KVCache],
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_events: Optional[List[torch.cuda.Event]],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
hidden_states = self.transformer(input_ids, positions, kv_caches,
|
hidden_states = self.transformer(input_ids, positions, kv_caches,
|
||||||
input_metadata, cache_events)
|
input_metadata)
|
||||||
return hidden_states
|
return hidden_states
|
||||||
|
|
||||||
def sample(
|
def sample(
|
||||||
self,
|
self,
|
||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
sampling_metadata: SamplingMetadata,
|
sampling_metadata: SamplingMetadata,
|
||||||
) -> SamplerOutput:
|
) -> Optional[SamplerOutput]:
|
||||||
next_tokens = self.sampler(self.lm_head_weight, hidden_states,
|
next_tokens = self.sampler(self.lm_head_weight, hidden_states,
|
||||||
sampling_metadata)
|
sampling_metadata)
|
||||||
return next_tokens
|
return next_tokens
|
||||||
@@ -275,7 +268,6 @@ class GPT2LMHeadModel(nn.Module):
|
|||||||
if not name.endswith(".weight"):
|
if not name.endswith(".weight"):
|
||||||
continue
|
continue
|
||||||
loaded_weight = loaded_weight.t()
|
loaded_weight = loaded_weight.t()
|
||||||
|
|
||||||
weight_loader = getattr(param, "weight_loader",
|
weight_loader = getattr(param, "weight_loader",
|
||||||
default_weight_loader)
|
default_weight_loader)
|
||||||
weight_loader(param, loaded_weight)
|
weight_loader(param, loaded_weight)
|
||||||
|
|||||||
@@ -95,7 +95,6 @@ class GPTBigCodeAttention(nn.Module):
|
|||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
kv_cache: KVCache,
|
kv_cache: KVCache,
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_event: Optional[torch.cuda.Event],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
qkv, _ = self.c_attn(hidden_states)
|
qkv, _ = self.c_attn(hidden_states)
|
||||||
q, k, v = qkv.split(
|
q, k, v = qkv.split(
|
||||||
@@ -107,7 +106,7 @@ class GPTBigCodeAttention(nn.Module):
|
|||||||
)
|
)
|
||||||
key_cache, value_cache = kv_cache
|
key_cache, value_cache = kv_cache
|
||||||
attn_output = self.attn(q, k, v, key_cache, value_cache,
|
attn_output = self.attn(q, k, v, key_cache, value_cache,
|
||||||
input_metadata, cache_event)
|
input_metadata)
|
||||||
attn_output, _ = self.c_proj(attn_output)
|
attn_output, _ = self.c_proj(attn_output)
|
||||||
return attn_output
|
return attn_output
|
||||||
|
|
||||||
@@ -167,7 +166,6 @@ class GPTBigCodeBlock(nn.Module):
|
|||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
kv_cache: KVCache,
|
kv_cache: KVCache,
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_event: Optional[torch.cuda.Event],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
residual = hidden_states
|
residual = hidden_states
|
||||||
hidden_states = self.ln_1(hidden_states)
|
hidden_states = self.ln_1(hidden_states)
|
||||||
@@ -175,7 +173,6 @@ class GPTBigCodeBlock(nn.Module):
|
|||||||
hidden_states=hidden_states,
|
hidden_states=hidden_states,
|
||||||
kv_cache=kv_cache,
|
kv_cache=kv_cache,
|
||||||
input_metadata=input_metadata,
|
input_metadata=input_metadata,
|
||||||
cache_event=cache_event,
|
|
||||||
)
|
)
|
||||||
# residual connection
|
# residual connection
|
||||||
hidden_states = attn_output + residual
|
hidden_states = attn_output + residual
|
||||||
@@ -215,17 +212,14 @@ class GPTBigCodeModel(nn.Module):
|
|||||||
position_ids: torch.Tensor,
|
position_ids: torch.Tensor,
|
||||||
kv_caches: List[KVCache],
|
kv_caches: List[KVCache],
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_events: Optional[List[torch.cuda.Event]],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
inputs_embeds = self.wte(input_ids)
|
inputs_embeds = self.wte(input_ids)
|
||||||
position_embeds = self.wpe(position_ids)
|
position_embeds = self.wpe(position_ids)
|
||||||
hidden_states = inputs_embeds + position_embeds
|
hidden_states = inputs_embeds + position_embeds
|
||||||
|
|
||||||
for i in range(len(self.h)):
|
for i in range(len(self.h)):
|
||||||
cache_event = None if cache_events is None else cache_events[i]
|
|
||||||
layer = self.h[i]
|
layer = self.h[i]
|
||||||
hidden_states = layer(hidden_states, kv_caches[i], input_metadata,
|
hidden_states = layer(hidden_states, kv_caches[i], input_metadata)
|
||||||
cache_event)
|
|
||||||
|
|
||||||
hidden_states = self.ln_f(hidden_states)
|
hidden_states = self.ln_f(hidden_states)
|
||||||
return hidden_states
|
return hidden_states
|
||||||
@@ -251,17 +245,16 @@ class GPTBigCodeForCausalLM(nn.Module):
|
|||||||
positions: torch.Tensor,
|
positions: torch.Tensor,
|
||||||
kv_caches: List[KVCache],
|
kv_caches: List[KVCache],
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_events: Optional[List[torch.cuda.Event]],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
hidden_states = self.transformer(input_ids, positions, kv_caches,
|
hidden_states = self.transformer(input_ids, positions, kv_caches,
|
||||||
input_metadata, cache_events)
|
input_metadata)
|
||||||
return hidden_states
|
return hidden_states
|
||||||
|
|
||||||
def sample(
|
def sample(
|
||||||
self,
|
self,
|
||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
sampling_metadata: SamplingMetadata,
|
sampling_metadata: SamplingMetadata,
|
||||||
) -> SamplerOutput:
|
) -> Optional[SamplerOutput]:
|
||||||
next_tokens = self.sampler(self.lm_head_weight, hidden_states,
|
next_tokens = self.sampler(self.lm_head_weight, hidden_states,
|
||||||
sampling_metadata)
|
sampling_metadata)
|
||||||
return next_tokens
|
return next_tokens
|
||||||
|
|||||||
@@ -94,14 +94,12 @@ class GPTJAttention(nn.Module):
|
|||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
kv_cache: KVCache,
|
kv_cache: KVCache,
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_event: Optional[torch.cuda.Event],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
qkv, _ = self.qkv_proj(hidden_states)
|
qkv, _ = self.qkv_proj(hidden_states)
|
||||||
q, k, v = qkv.chunk(chunks=3, dim=-1)
|
q, k, v = qkv.chunk(chunks=3, dim=-1)
|
||||||
q, k = self.rotary_emb(position_ids, q, k)
|
q, k = self.rotary_emb(position_ids, q, k)
|
||||||
k_cache, v_cache = kv_cache
|
k_cache, v_cache = kv_cache
|
||||||
attn_output = self.attn(q, k, v, k_cache, v_cache, input_metadata,
|
attn_output = self.attn(q, k, v, k_cache, v_cache, input_metadata)
|
||||||
cache_event)
|
|
||||||
attn_output, _ = self.out_proj(attn_output)
|
attn_output, _ = self.out_proj(attn_output)
|
||||||
return attn_output
|
return attn_output
|
||||||
|
|
||||||
@@ -156,7 +154,6 @@ class GPTJBlock(nn.Module):
|
|||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
kv_cache: KVCache,
|
kv_cache: KVCache,
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_event: Optional[torch.cuda.Event],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
residual = hidden_states
|
residual = hidden_states
|
||||||
hidden_states = self.ln_1(hidden_states)
|
hidden_states = self.ln_1(hidden_states)
|
||||||
@@ -165,7 +162,6 @@ class GPTJBlock(nn.Module):
|
|||||||
hidden_states=hidden_states,
|
hidden_states=hidden_states,
|
||||||
kv_cache=kv_cache,
|
kv_cache=kv_cache,
|
||||||
input_metadata=input_metadata,
|
input_metadata=input_metadata,
|
||||||
cache_event=cache_event,
|
|
||||||
)
|
)
|
||||||
mlp_output = self.mlp(hidden_states)
|
mlp_output = self.mlp(hidden_states)
|
||||||
hidden_states = attn_output + mlp_output + residual
|
hidden_states = attn_output + mlp_output + residual
|
||||||
@@ -196,18 +192,15 @@ class GPTJModel(nn.Module):
|
|||||||
position_ids: torch.Tensor,
|
position_ids: torch.Tensor,
|
||||||
kv_caches: List[KVCache],
|
kv_caches: List[KVCache],
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_events: Optional[List[torch.cuda.Event]],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
hidden_states = self.wte(input_ids)
|
hidden_states = self.wte(input_ids)
|
||||||
for i in range(len(self.h)):
|
for i in range(len(self.h)):
|
||||||
cache_event = None if cache_events is None else cache_events[i]
|
|
||||||
layer = self.h[i]
|
layer = self.h[i]
|
||||||
hidden_states = layer(
|
hidden_states = layer(
|
||||||
position_ids,
|
position_ids,
|
||||||
hidden_states,
|
hidden_states,
|
||||||
kv_caches[i],
|
kv_caches[i],
|
||||||
input_metadata,
|
input_metadata,
|
||||||
cache_event,
|
|
||||||
)
|
)
|
||||||
hidden_states = self.ln_f(hidden_states)
|
hidden_states = self.ln_f(hidden_states)
|
||||||
return hidden_states
|
return hidden_states
|
||||||
@@ -238,17 +231,16 @@ class GPTJForCausalLM(nn.Module):
|
|||||||
positions: torch.Tensor,
|
positions: torch.Tensor,
|
||||||
kv_caches: List[KVCache],
|
kv_caches: List[KVCache],
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_events: Optional[List[torch.cuda.Event]],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
hidden_states = self.transformer(input_ids, positions, kv_caches,
|
hidden_states = self.transformer(input_ids, positions, kv_caches,
|
||||||
input_metadata, cache_events)
|
input_metadata)
|
||||||
return hidden_states
|
return hidden_states
|
||||||
|
|
||||||
def sample(
|
def sample(
|
||||||
self,
|
self,
|
||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
sampling_metadata: SamplingMetadata,
|
sampling_metadata: SamplingMetadata,
|
||||||
) -> SamplerOutput:
|
) -> Optional[SamplerOutput]:
|
||||||
next_tokens = self.sampler(self.lm_head.weight, hidden_states,
|
next_tokens = self.sampler(self.lm_head.weight, hidden_states,
|
||||||
sampling_metadata, self.lm_head.bias)
|
sampling_metadata, self.lm_head.bias)
|
||||||
return next_tokens
|
return next_tokens
|
||||||
@@ -274,11 +266,18 @@ class GPTJForCausalLM(nn.Module):
|
|||||||
for (param_name, weight_name, shard_id) in stacked_params_mapping:
|
for (param_name, weight_name, shard_id) in stacked_params_mapping:
|
||||||
if weight_name not in name:
|
if weight_name not in name:
|
||||||
continue
|
continue
|
||||||
param = params_dict[name.replace(weight_name, param_name)]
|
name = name.replace(weight_name, param_name)
|
||||||
|
# Skip loading extra bias for GPTQ models.
|
||||||
|
if name.endswith(".bias") and name not in params_dict:
|
||||||
|
continue
|
||||||
|
param = params_dict[name]
|
||||||
weight_loader = param.weight_loader
|
weight_loader = param.weight_loader
|
||||||
weight_loader(param, loaded_weight, shard_id)
|
weight_loader(param, loaded_weight, shard_id)
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
|
# Skip loading extra bias for GPTQ models.
|
||||||
|
if name.endswith(".bias") and name not in params_dict:
|
||||||
|
continue
|
||||||
param = params_dict[name]
|
param = params_dict[name]
|
||||||
weight_loader = getattr(param, "weight_loader",
|
weight_loader = getattr(param, "weight_loader",
|
||||||
default_weight_loader)
|
default_weight_loader)
|
||||||
|
|||||||
@@ -54,6 +54,7 @@ class GPTNeoXAttention(nn.Module):
|
|||||||
self.total_num_heads = config.num_attention_heads
|
self.total_num_heads = config.num_attention_heads
|
||||||
self.hidden_size = config.hidden_size
|
self.hidden_size = config.hidden_size
|
||||||
self.head_size = self.hidden_size // self.total_num_heads
|
self.head_size = self.hidden_size // self.total_num_heads
|
||||||
|
self.bias = getattr(config, "attention_bias", True)
|
||||||
|
|
||||||
tensor_model_parallel_world_size = (
|
tensor_model_parallel_world_size = (
|
||||||
get_tensor_model_parallel_world_size())
|
get_tensor_model_parallel_world_size())
|
||||||
@@ -65,14 +66,15 @@ class GPTNeoXAttention(nn.Module):
|
|||||||
config.hidden_size,
|
config.hidden_size,
|
||||||
self.head_size,
|
self.head_size,
|
||||||
self.total_num_heads,
|
self.total_num_heads,
|
||||||
|
bias=self.bias,
|
||||||
linear_method=linear_method,
|
linear_method=linear_method,
|
||||||
)
|
)
|
||||||
self.dense = RowParallelLinear(
|
self.dense = RowParallelLinear(
|
||||||
config.hidden_size,
|
config.hidden_size,
|
||||||
config.hidden_size,
|
config.hidden_size,
|
||||||
|
bias=self.bias,
|
||||||
linear_method=linear_method,
|
linear_method=linear_method,
|
||||||
)
|
)
|
||||||
|
|
||||||
scaling = self.head_size**-0.5
|
scaling = self.head_size**-0.5
|
||||||
rotary_dim = int(self.head_size * config.rotary_pct)
|
rotary_dim = int(self.head_size * config.rotary_pct)
|
||||||
assert rotary_dim % 2 == 0
|
assert rotary_dim % 2 == 0
|
||||||
@@ -93,14 +95,12 @@ class GPTNeoXAttention(nn.Module):
|
|||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
kv_cache: KVCache,
|
kv_cache: KVCache,
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_event: Optional[torch.cuda.Event],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
qkv, _ = self.query_key_value(hidden_states)
|
qkv, _ = self.query_key_value(hidden_states)
|
||||||
q, k, v = qkv.chunk(chunks=3, dim=-1)
|
q, k, v = qkv.chunk(chunks=3, dim=-1)
|
||||||
q, k = self.rotary_emb(position_ids, q, k)
|
q, k = self.rotary_emb(position_ids, q, k)
|
||||||
k_cache, v_cache = kv_cache
|
k_cache, v_cache = kv_cache
|
||||||
attn_output = self.attn(q, k, v, k_cache, v_cache, input_metadata,
|
attn_output = self.attn(q, k, v, k_cache, v_cache, input_metadata)
|
||||||
cache_event)
|
|
||||||
output, _ = self.dense(attn_output)
|
output, _ = self.dense(attn_output)
|
||||||
return output
|
return output
|
||||||
|
|
||||||
@@ -156,7 +156,6 @@ class GPTNeoXLayer(nn.Module):
|
|||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
kv_cache: KVCache,
|
kv_cache: KVCache,
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_event: Optional[torch.cuda.Event],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
attn_input = self.input_layernorm(hidden_states)
|
attn_input = self.input_layernorm(hidden_states)
|
||||||
attn_output = self.attention(
|
attn_output = self.attention(
|
||||||
@@ -164,7 +163,6 @@ class GPTNeoXLayer(nn.Module):
|
|||||||
hidden_states=attn_input,
|
hidden_states=attn_input,
|
||||||
kv_cache=kv_cache,
|
kv_cache=kv_cache,
|
||||||
input_metadata=input_metadata,
|
input_metadata=input_metadata,
|
||||||
cache_event=cache_event,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if self.use_parallel_residual:
|
if self.use_parallel_residual:
|
||||||
@@ -211,18 +209,15 @@ class GPTNeoXModel(nn.Module):
|
|||||||
position_ids: torch.Tensor,
|
position_ids: torch.Tensor,
|
||||||
kv_caches: List[KVCache],
|
kv_caches: List[KVCache],
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_events: Optional[List[torch.cuda.Event]],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
hidden_states = self.embed_in(input_ids)
|
hidden_states = self.embed_in(input_ids)
|
||||||
for i in range(len(self.layers)):
|
for i in range(len(self.layers)):
|
||||||
cache_event = None if cache_events is None else cache_events[i]
|
|
||||||
layer = self.layers[i]
|
layer = self.layers[i]
|
||||||
hidden_states = layer(
|
hidden_states = layer(
|
||||||
position_ids,
|
position_ids,
|
||||||
hidden_states,
|
hidden_states,
|
||||||
kv_caches[i],
|
kv_caches[i],
|
||||||
input_metadata,
|
input_metadata,
|
||||||
cache_event,
|
|
||||||
)
|
)
|
||||||
hidden_states = self.final_layer_norm(hidden_states)
|
hidden_states = self.final_layer_norm(hidden_states)
|
||||||
return hidden_states
|
return hidden_states
|
||||||
@@ -251,17 +246,16 @@ class GPTNeoXForCausalLM(nn.Module):
|
|||||||
positions: torch.Tensor,
|
positions: torch.Tensor,
|
||||||
kv_caches: List[KVCache],
|
kv_caches: List[KVCache],
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_events: Optional[List[torch.cuda.Event]],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
hidden_states = self.gpt_neox(input_ids, positions, kv_caches,
|
hidden_states = self.gpt_neox(input_ids, positions, kv_caches,
|
||||||
input_metadata, cache_events)
|
input_metadata)
|
||||||
return hidden_states
|
return hidden_states
|
||||||
|
|
||||||
def sample(
|
def sample(
|
||||||
self,
|
self,
|
||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
sampling_metadata: SamplingMetadata,
|
sampling_metadata: SamplingMetadata,
|
||||||
) -> SamplerOutput:
|
) -> Optional[SamplerOutput]:
|
||||||
next_tokens = self.sampler(self.embed_out.weight, hidden_states,
|
next_tokens = self.sampler(self.embed_out.weight, hidden_states,
|
||||||
sampling_metadata)
|
sampling_metadata)
|
||||||
return next_tokens
|
return next_tokens
|
||||||
|
|||||||
@@ -110,14 +110,12 @@ class InternLMAttention(nn.Module):
|
|||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
kv_cache: KVCache,
|
kv_cache: KVCache,
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_event: Optional[torch.cuda.Event],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
qkv, _ = self.qkv_proj(hidden_states)
|
qkv, _ = self.qkv_proj(hidden_states)
|
||||||
q, k, v = qkv.chunk(chunks=3, dim=-1)
|
q, k, v = qkv.chunk(chunks=3, dim=-1)
|
||||||
q, k = self.rotary_emb(positions, q, k)
|
q, k = self.rotary_emb(positions, q, k)
|
||||||
k_cache, v_cache = kv_cache
|
k_cache, v_cache = kv_cache
|
||||||
attn_output = self.attn(q, k, v, k_cache, v_cache, input_metadata,
|
attn_output = self.attn(q, k, v, k_cache, v_cache, input_metadata)
|
||||||
cache_event)
|
|
||||||
output, _ = self.o_proj(attn_output)
|
output, _ = self.o_proj(attn_output)
|
||||||
return output
|
return output
|
||||||
|
|
||||||
@@ -160,7 +158,6 @@ class InternLMDecoderLayer(nn.Module):
|
|||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
kv_cache: KVCache,
|
kv_cache: KVCache,
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_event: Optional[torch.cuda.Event],
|
|
||||||
residual: Optional[torch.Tensor],
|
residual: Optional[torch.Tensor],
|
||||||
) -> Tuple[torch.Tensor, torch.Tensor]:
|
) -> Tuple[torch.Tensor, torch.Tensor]:
|
||||||
# Self Attention
|
# Self Attention
|
||||||
@@ -175,7 +172,6 @@ class InternLMDecoderLayer(nn.Module):
|
|||||||
hidden_states=hidden_states,
|
hidden_states=hidden_states,
|
||||||
kv_cache=kv_cache,
|
kv_cache=kv_cache,
|
||||||
input_metadata=input_metadata,
|
input_metadata=input_metadata,
|
||||||
cache_event=cache_event,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Fully Connected
|
# Fully Connected
|
||||||
@@ -214,19 +210,16 @@ class InternLMModel(nn.Module):
|
|||||||
positions: torch.Tensor,
|
positions: torch.Tensor,
|
||||||
kv_caches: List[KVCache],
|
kv_caches: List[KVCache],
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_events: Optional[List[torch.cuda.Event]],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
hidden_states = self.embed_tokens(input_ids)
|
hidden_states = self.embed_tokens(input_ids)
|
||||||
residual = None
|
residual = None
|
||||||
for i in range(len(self.layers)):
|
for i in range(len(self.layers)):
|
||||||
cache_event = None if cache_events is None else cache_events[i]
|
|
||||||
layer = self.layers[i]
|
layer = self.layers[i]
|
||||||
hidden_states, residual = layer(
|
hidden_states, residual = layer(
|
||||||
positions,
|
positions,
|
||||||
hidden_states,
|
hidden_states,
|
||||||
kv_caches[i],
|
kv_caches[i],
|
||||||
input_metadata,
|
input_metadata,
|
||||||
cache_event,
|
|
||||||
residual,
|
residual,
|
||||||
)
|
)
|
||||||
hidden_states, _ = self.norm(hidden_states, residual)
|
hidden_states, _ = self.norm(hidden_states, residual)
|
||||||
@@ -253,17 +246,16 @@ class InternLMForCausalLM(nn.Module):
|
|||||||
positions: torch.Tensor,
|
positions: torch.Tensor,
|
||||||
kv_caches: List[KVCache],
|
kv_caches: List[KVCache],
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_events: Optional[List[torch.cuda.Event]],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
hidden_states = self.model(input_ids, positions, kv_caches,
|
hidden_states = self.model(input_ids, positions, kv_caches,
|
||||||
input_metadata, cache_events)
|
input_metadata)
|
||||||
return hidden_states
|
return hidden_states
|
||||||
|
|
||||||
def sample(
|
def sample(
|
||||||
self,
|
self,
|
||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
sampling_metadata: SamplingMetadata,
|
sampling_metadata: SamplingMetadata,
|
||||||
) -> SamplerOutput:
|
) -> Optional[SamplerOutput]:
|
||||||
next_tokens = self.sampler(self.lm_head.weight, hidden_states,
|
next_tokens = self.sampler(self.lm_head.weight, hidden_states,
|
||||||
sampling_metadata)
|
sampling_metadata)
|
||||||
return next_tokens
|
return next_tokens
|
||||||
@@ -289,11 +281,18 @@ class InternLMForCausalLM(nn.Module):
|
|||||||
for (param_name, weight_name, shard_id) in stacked_params_mapping:
|
for (param_name, weight_name, shard_id) in stacked_params_mapping:
|
||||||
if weight_name not in name:
|
if weight_name not in name:
|
||||||
continue
|
continue
|
||||||
param = params_dict[name.replace(weight_name, param_name)]
|
name = name.replace(weight_name, param_name)
|
||||||
|
# Skip loading extra bias for GPTQ models.
|
||||||
|
if name.endswith(".bias") and name not in params_dict:
|
||||||
|
continue
|
||||||
|
param = params_dict[name]
|
||||||
weight_loader = param.weight_loader
|
weight_loader = param.weight_loader
|
||||||
weight_loader(param, loaded_weight, shard_id)
|
weight_loader(param, loaded_weight, shard_id)
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
|
# Skip loading extra bias for GPTQ models.
|
||||||
|
if name.endswith(".bias") and name not in params_dict:
|
||||||
|
continue
|
||||||
param = params_dict[name]
|
param = params_dict[name]
|
||||||
weight_loader = getattr(param, "weight_loader",
|
weight_loader = getattr(param, "weight_loader",
|
||||||
default_weight_loader)
|
default_weight_loader)
|
||||||
|
|||||||
@@ -147,14 +147,12 @@ class LlamaAttention(nn.Module):
|
|||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
kv_cache: KVCache,
|
kv_cache: KVCache,
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_event: Optional[torch.cuda.Event],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
qkv, _ = self.qkv_proj(hidden_states)
|
qkv, _ = self.qkv_proj(hidden_states)
|
||||||
q, k, v = qkv.split([self.q_size, self.kv_size, self.kv_size], dim=-1)
|
q, k, v = qkv.split([self.q_size, self.kv_size, self.kv_size], dim=-1)
|
||||||
q, k = self.rotary_emb(positions, q, k)
|
q, k = self.rotary_emb(positions, q, k)
|
||||||
k_cache, v_cache = kv_cache
|
k_cache, v_cache = kv_cache
|
||||||
attn_output = self.attn(q, k, v, k_cache, v_cache, input_metadata,
|
attn_output = self.attn(q, k, v, k_cache, v_cache, input_metadata)
|
||||||
cache_event)
|
|
||||||
output, _ = self.o_proj(attn_output)
|
output, _ = self.o_proj(attn_output)
|
||||||
return output
|
return output
|
||||||
|
|
||||||
@@ -198,7 +196,6 @@ class LlamaDecoderLayer(nn.Module):
|
|||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
kv_cache: KVCache,
|
kv_cache: KVCache,
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_event: Optional[torch.cuda.Event],
|
|
||||||
residual: Optional[torch.Tensor],
|
residual: Optional[torch.Tensor],
|
||||||
) -> Tuple[torch.Tensor, torch.Tensor]:
|
) -> Tuple[torch.Tensor, torch.Tensor]:
|
||||||
# Self Attention
|
# Self Attention
|
||||||
@@ -213,7 +210,6 @@ class LlamaDecoderLayer(nn.Module):
|
|||||||
hidden_states=hidden_states,
|
hidden_states=hidden_states,
|
||||||
kv_cache=kv_cache,
|
kv_cache=kv_cache,
|
||||||
input_metadata=input_metadata,
|
input_metadata=input_metadata,
|
||||||
cache_event=cache_event,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Fully Connected
|
# Fully Connected
|
||||||
@@ -250,19 +246,16 @@ class LlamaModel(nn.Module):
|
|||||||
positions: torch.Tensor,
|
positions: torch.Tensor,
|
||||||
kv_caches: List[KVCache],
|
kv_caches: List[KVCache],
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_events: Optional[List[torch.cuda.Event]],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
hidden_states = self.embed_tokens(input_ids)
|
hidden_states = self.embed_tokens(input_ids)
|
||||||
residual = None
|
residual = None
|
||||||
for i in range(len(self.layers)):
|
for i in range(len(self.layers)):
|
||||||
cache_event = None if cache_events is None else cache_events[i]
|
|
||||||
layer = self.layers[i]
|
layer = self.layers[i]
|
||||||
hidden_states, residual = layer(
|
hidden_states, residual = layer(
|
||||||
positions,
|
positions,
|
||||||
hidden_states,
|
hidden_states,
|
||||||
kv_caches[i],
|
kv_caches[i],
|
||||||
input_metadata,
|
input_metadata,
|
||||||
cache_event,
|
|
||||||
residual,
|
residual,
|
||||||
)
|
)
|
||||||
hidden_states, _ = self.norm(hidden_states, residual)
|
hidden_states, _ = self.norm(hidden_states, residual)
|
||||||
@@ -289,17 +282,16 @@ class LlamaForCausalLM(nn.Module):
|
|||||||
positions: torch.Tensor,
|
positions: torch.Tensor,
|
||||||
kv_caches: List[KVCache],
|
kv_caches: List[KVCache],
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_events: Optional[List[torch.cuda.Event]],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
hidden_states = self.model(input_ids, positions, kv_caches,
|
hidden_states = self.model(input_ids, positions, kv_caches,
|
||||||
input_metadata, cache_events)
|
input_metadata)
|
||||||
return hidden_states
|
return hidden_states
|
||||||
|
|
||||||
def sample(
|
def sample(
|
||||||
self,
|
self,
|
||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
sampling_metadata: SamplingMetadata,
|
sampling_metadata: SamplingMetadata,
|
||||||
) -> SamplerOutput:
|
) -> Optional[SamplerOutput]:
|
||||||
next_tokens = self.sampler(self.lm_head.weight, hidden_states,
|
next_tokens = self.sampler(self.lm_head.weight, hidden_states,
|
||||||
sampling_metadata)
|
sampling_metadata)
|
||||||
return next_tokens
|
return next_tokens
|
||||||
@@ -330,11 +322,18 @@ class LlamaForCausalLM(nn.Module):
|
|||||||
for (param_name, weight_name, shard_id) in stacked_params_mapping:
|
for (param_name, weight_name, shard_id) in stacked_params_mapping:
|
||||||
if weight_name not in name:
|
if weight_name not in name:
|
||||||
continue
|
continue
|
||||||
param = params_dict[name.replace(weight_name, param_name)]
|
name = name.replace(weight_name, param_name)
|
||||||
|
# Skip loading extra bias for GPTQ models.
|
||||||
|
if name.endswith(".bias") and name not in params_dict:
|
||||||
|
continue
|
||||||
|
param = params_dict[name]
|
||||||
weight_loader = param.weight_loader
|
weight_loader = param.weight_loader
|
||||||
weight_loader(param, loaded_weight, shard_id)
|
weight_loader(param, loaded_weight, shard_id)
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
|
# Skip loading extra bias for GPTQ models.
|
||||||
|
if name.endswith(".bias") and name not in params_dict:
|
||||||
|
continue
|
||||||
param = params_dict[name]
|
param = params_dict[name]
|
||||||
weight_loader = getattr(param, "weight_loader",
|
weight_loader = getattr(param, "weight_loader",
|
||||||
default_weight_loader)
|
default_weight_loader)
|
||||||
|
|||||||
@@ -145,14 +145,12 @@ class MistralAttention(nn.Module):
|
|||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
kv_cache: KVCache,
|
kv_cache: KVCache,
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_event: Optional[torch.cuda.Event],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
qkv, _ = self.qkv_proj(hidden_states)
|
qkv, _ = self.qkv_proj(hidden_states)
|
||||||
q, k, v = qkv.split([self.q_size, self.kv_size, self.kv_size], dim=-1)
|
q, k, v = qkv.split([self.q_size, self.kv_size, self.kv_size], dim=-1)
|
||||||
q, k = self.rotary_emb(positions, q, k)
|
q, k = self.rotary_emb(positions, q, k)
|
||||||
k_cache, v_cache = kv_cache
|
k_cache, v_cache = kv_cache
|
||||||
attn_output = self.attn(q, k, v, k_cache, v_cache, input_metadata,
|
attn_output = self.attn(q, k, v, k_cache, v_cache, input_metadata)
|
||||||
cache_event)
|
|
||||||
output, _ = self.o_proj(attn_output)
|
output, _ = self.o_proj(attn_output)
|
||||||
return output
|
return output
|
||||||
|
|
||||||
@@ -193,7 +191,6 @@ class MistralDecoderLayer(nn.Module):
|
|||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
kv_cache: KVCache,
|
kv_cache: KVCache,
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_event: Optional[torch.cuda.Event],
|
|
||||||
residual: Optional[torch.Tensor],
|
residual: Optional[torch.Tensor],
|
||||||
) -> Tuple[torch.Tensor, torch.Tensor]:
|
) -> Tuple[torch.Tensor, torch.Tensor]:
|
||||||
# Self Attention
|
# Self Attention
|
||||||
@@ -208,7 +205,6 @@ class MistralDecoderLayer(nn.Module):
|
|||||||
hidden_states=hidden_states,
|
hidden_states=hidden_states,
|
||||||
kv_cache=kv_cache,
|
kv_cache=kv_cache,
|
||||||
input_metadata=input_metadata,
|
input_metadata=input_metadata,
|
||||||
cache_event=cache_event,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Fully Connected
|
# Fully Connected
|
||||||
@@ -246,19 +242,16 @@ class MistralModel(nn.Module):
|
|||||||
positions: torch.Tensor,
|
positions: torch.Tensor,
|
||||||
kv_caches: List[KVCache],
|
kv_caches: List[KVCache],
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_events: Optional[List[torch.cuda.Event]],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
hidden_states = self.embed_tokens(input_ids)
|
hidden_states = self.embed_tokens(input_ids)
|
||||||
residual = None
|
residual = None
|
||||||
for i in range(len(self.layers)):
|
for i in range(len(self.layers)):
|
||||||
cache_event = None if cache_events is None else cache_events[i]
|
|
||||||
layer = self.layers[i]
|
layer = self.layers[i]
|
||||||
hidden_states, residual = layer(
|
hidden_states, residual = layer(
|
||||||
positions,
|
positions,
|
||||||
hidden_states,
|
hidden_states,
|
||||||
kv_caches[i],
|
kv_caches[i],
|
||||||
input_metadata,
|
input_metadata,
|
||||||
cache_event,
|
|
||||||
residual,
|
residual,
|
||||||
)
|
)
|
||||||
hidden_states, _ = self.norm(hidden_states, residual)
|
hidden_states, _ = self.norm(hidden_states, residual)
|
||||||
@@ -285,17 +278,16 @@ class MistralForCausalLM(nn.Module):
|
|||||||
positions: torch.Tensor,
|
positions: torch.Tensor,
|
||||||
kv_caches: List[KVCache],
|
kv_caches: List[KVCache],
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_events: Optional[List[torch.cuda.Event]],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
hidden_states = self.model(input_ids, positions, kv_caches,
|
hidden_states = self.model(input_ids, positions, kv_caches,
|
||||||
input_metadata, cache_events)
|
input_metadata)
|
||||||
return hidden_states
|
return hidden_states
|
||||||
|
|
||||||
def sample(
|
def sample(
|
||||||
self,
|
self,
|
||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
sampling_metadata: SamplingMetadata,
|
sampling_metadata: SamplingMetadata,
|
||||||
) -> SamplerOutput:
|
) -> Optional[SamplerOutput]:
|
||||||
next_tokens = self.sampler(self.lm_head.weight, hidden_states,
|
next_tokens = self.sampler(self.lm_head.weight, hidden_states,
|
||||||
sampling_metadata)
|
sampling_metadata)
|
||||||
return next_tokens
|
return next_tokens
|
||||||
@@ -321,11 +313,18 @@ class MistralForCausalLM(nn.Module):
|
|||||||
for (param_name, weight_name, shard_id) in stacked_params_mapping:
|
for (param_name, weight_name, shard_id) in stacked_params_mapping:
|
||||||
if weight_name not in name:
|
if weight_name not in name:
|
||||||
continue
|
continue
|
||||||
param = params_dict[name.replace(weight_name, param_name)]
|
name = name.replace(weight_name, param_name)
|
||||||
|
# Skip loading extra bias for GPTQ models.
|
||||||
|
if name.endswith(".bias") and name not in params_dict:
|
||||||
|
continue
|
||||||
|
param = params_dict[name]
|
||||||
weight_loader = param.weight_loader
|
weight_loader = param.weight_loader
|
||||||
weight_loader(param, loaded_weight, shard_id)
|
weight_loader(param, loaded_weight, shard_id)
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
|
# Skip loading extra bias for GPTQ models.
|
||||||
|
if name.endswith(".bias") and name not in params_dict:
|
||||||
|
continue
|
||||||
param = params_dict[name]
|
param = params_dict[name]
|
||||||
weight_loader = getattr(param, "weight_loader",
|
weight_loader = getattr(param, "weight_loader",
|
||||||
default_weight_loader)
|
default_weight_loader)
|
||||||
|
|||||||
@@ -29,25 +29,13 @@ import torch
|
|||||||
import torch.nn.functional as F
|
import torch.nn.functional as F
|
||||||
|
|
||||||
from torch import nn
|
from torch import nn
|
||||||
from transformers import MistralConfig
|
from transformers import MixtralConfig
|
||||||
|
|
||||||
try:
|
|
||||||
import megablocks.ops as ops
|
|
||||||
except ImportError:
|
|
||||||
print(
|
|
||||||
"MegaBlocks not found. Please install it by `pip install megablocks`. "
|
|
||||||
"Note that MegaBlocks depends on mosaicml-turbo, which only supports "
|
|
||||||
"Python 3.10 for now.")
|
|
||||||
try:
|
|
||||||
import stk
|
|
||||||
except ImportError:
|
|
||||||
print(
|
|
||||||
"STK not found: please see https://github.com/stanford-futuredata/stk")
|
|
||||||
|
|
||||||
from vllm.model_executor.input_metadata import InputMetadata
|
from vllm.model_executor.input_metadata import InputMetadata
|
||||||
from vllm.model_executor.layers.attention import PagedAttention
|
from vllm.model_executor.layers.attention import PagedAttention
|
||||||
from vllm.model_executor.layers.layernorm import RMSNorm
|
from vllm.model_executor.layers.layernorm import RMSNorm
|
||||||
from vllm.model_executor.layers.linear import (LinearMethodBase,
|
from vllm.model_executor.layers.linear import (LinearMethodBase,
|
||||||
|
ReplicatedLinear,
|
||||||
QKVParallelLinear,
|
QKVParallelLinear,
|
||||||
RowParallelLinear)
|
RowParallelLinear)
|
||||||
from vllm.model_executor.layers.rotary_embedding import get_rope
|
from vllm.model_executor.layers.rotary_embedding import get_rope
|
||||||
@@ -61,14 +49,115 @@ from vllm.model_executor.parallel_utils.parallel_state import (
|
|||||||
from vllm.model_executor.sampling_metadata import SamplingMetadata
|
from vllm.model_executor.sampling_metadata import SamplingMetadata
|
||||||
from vllm.model_executor.weight_utils import (default_weight_loader,
|
from vllm.model_executor.weight_utils import (default_weight_loader,
|
||||||
hf_model_weights_iterator)
|
hf_model_weights_iterator)
|
||||||
from vllm.model_executor.utils import set_weight_attrs
|
|
||||||
from vllm.sequence import SamplerOutput
|
from vllm.sequence import SamplerOutput
|
||||||
|
|
||||||
KVCache = Tuple[torch.Tensor, torch.Tensor]
|
KVCache = Tuple[torch.Tensor, torch.Tensor]
|
||||||
|
|
||||||
|
|
||||||
def promote_scalar(x: torch.Tensor) -> torch.Tensor:
|
class MixtralMLP(nn.Module):
|
||||||
return x.view(1) if len(x.size()) == 0 else x
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
num_experts: int,
|
||||||
|
hidden_size: int,
|
||||||
|
intermediate_size: int,
|
||||||
|
linear_method: Optional[LinearMethodBase] = None,
|
||||||
|
) -> None:
|
||||||
|
super().__init__()
|
||||||
|
self.num_experts = num_experts
|
||||||
|
self.ffn_dim = intermediate_size
|
||||||
|
self.hidden_dim = hidden_size
|
||||||
|
|
||||||
|
self.w1 = ReplicatedLinear(self.hidden_dim,
|
||||||
|
self.ffn_dim,
|
||||||
|
bias=False,
|
||||||
|
linear_method=linear_method)
|
||||||
|
self.w2 = ReplicatedLinear(self.ffn_dim,
|
||||||
|
self.hidden_dim,
|
||||||
|
bias=False,
|
||||||
|
linear_method=linear_method)
|
||||||
|
self.w3 = ReplicatedLinear(self.hidden_dim,
|
||||||
|
self.ffn_dim,
|
||||||
|
bias=False,
|
||||||
|
linear_method=linear_method)
|
||||||
|
|
||||||
|
# TODO: Use vllm's SiluAndMul
|
||||||
|
self.act_fn = nn.SiLU()
|
||||||
|
|
||||||
|
def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
|
||||||
|
w1_out, _ = self.w1(hidden_states)
|
||||||
|
w1_out = self.act_fn(w1_out)
|
||||||
|
w3_out, _ = self.w3(hidden_states)
|
||||||
|
current_hidden_states = w1_out * w3_out
|
||||||
|
current_hidden_states, _ = self.w2(current_hidden_states)
|
||||||
|
return current_hidden_states
|
||||||
|
|
||||||
|
|
||||||
|
class MixtralMoE(nn.Module):
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
config: MixtralConfig,
|
||||||
|
linear_method: Optional[LinearMethodBase] = None,
|
||||||
|
):
|
||||||
|
super().__init__()
|
||||||
|
self.config = config
|
||||||
|
self.rank = get_tensor_model_parallel_rank()
|
||||||
|
self.tp_size = get_tensor_model_parallel_world_size()
|
||||||
|
self.num_total_experts = config.num_local_experts
|
||||||
|
self.top_k = config.num_experts_per_tok
|
||||||
|
if self.tp_size > self.num_total_experts:
|
||||||
|
raise ValueError(
|
||||||
|
f"Tensor parallel size {self.tp_size} is greater than "
|
||||||
|
f"the number of experts {self.num_total_experts}.")
|
||||||
|
# Split experts equally between ranks
|
||||||
|
self.expert_indicies = np.array_split(range(
|
||||||
|
self.num_total_experts), self.tp_size)[self.rank].tolist()
|
||||||
|
if not self.expert_indicies:
|
||||||
|
raise ValueError(
|
||||||
|
f"Rank {self.rank} has no experts assigned to it.")
|
||||||
|
|
||||||
|
self.experts = nn.ModuleList([
|
||||||
|
MixtralMLP(self.num_total_experts,
|
||||||
|
config.hidden_size,
|
||||||
|
config.intermediate_size,
|
||||||
|
linear_method=linear_method)
|
||||||
|
if idx in self.expert_indicies else None
|
||||||
|
for idx in range(self.num_total_experts)
|
||||||
|
])
|
||||||
|
self.gate = ReplicatedLinear(config.hidden_size,
|
||||||
|
self.num_total_experts,
|
||||||
|
bias=False,
|
||||||
|
linear_method=None)
|
||||||
|
|
||||||
|
def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
|
||||||
|
batch_size, sequence_length, hidden_dim = hidden_states.shape
|
||||||
|
hidden_states = hidden_states.view(-1, hidden_dim)
|
||||||
|
# router_logits: (batch * sequence_length, n_experts)
|
||||||
|
router_logits, _ = self.gate(hidden_states)
|
||||||
|
|
||||||
|
routing_weights = F.softmax(router_logits, dim=1, dtype=torch.float)
|
||||||
|
routing_weights, selected_experts = torch.topk(routing_weights,
|
||||||
|
self.top_k,
|
||||||
|
dim=-1)
|
||||||
|
routing_weights /= routing_weights.sum(dim=-1, keepdim=True)
|
||||||
|
|
||||||
|
final_hidden_states = None
|
||||||
|
for expert_idx in self.expert_indicies:
|
||||||
|
expert_layer = self.experts[expert_idx]
|
||||||
|
expert_mask = (selected_experts == expert_idx)
|
||||||
|
expert_weights = (routing_weights * expert_mask).sum(dim=-1,
|
||||||
|
keepdim=True)
|
||||||
|
|
||||||
|
current_hidden_states = expert_layer(hidden_states).mul_(
|
||||||
|
expert_weights)
|
||||||
|
if final_hidden_states is None:
|
||||||
|
final_hidden_states = current_hidden_states
|
||||||
|
else:
|
||||||
|
final_hidden_states.add_(current_hidden_states)
|
||||||
|
|
||||||
|
return tensor_model_parallel_all_reduce(final_hidden_states).view(
|
||||||
|
batch_size, sequence_length, hidden_dim)
|
||||||
|
|
||||||
|
|
||||||
class MixtralAttention(nn.Module):
|
class MixtralAttention(nn.Module):
|
||||||
@@ -79,6 +168,7 @@ class MixtralAttention(nn.Module):
|
|||||||
num_kv_heads: int,
|
num_kv_heads: int,
|
||||||
max_position: int = 4096 * 32,
|
max_position: int = 4096 * 32,
|
||||||
rope_theta: float = 10000,
|
rope_theta: float = 10000,
|
||||||
|
linear_method: Optional[LinearMethodBase] = None,
|
||||||
sliding_window: Optional[int] = None) -> None:
|
sliding_window: Optional[int] = None) -> None:
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.hidden_size = hidden_size
|
self.hidden_size = hidden_size
|
||||||
@@ -103,24 +193,26 @@ class MixtralAttention(nn.Module):
|
|||||||
self.rope_theta = rope_theta
|
self.rope_theta = rope_theta
|
||||||
self.sliding_window = sliding_window
|
self.sliding_window = sliding_window
|
||||||
|
|
||||||
self.wqkv = QKVParallelLinear(
|
self.qkv_proj = QKVParallelLinear(
|
||||||
hidden_size,
|
hidden_size,
|
||||||
self.head_dim,
|
self.head_dim,
|
||||||
self.total_num_heads,
|
self.total_num_heads,
|
||||||
self.total_num_kv_heads,
|
self.total_num_kv_heads,
|
||||||
bias=False,
|
bias=False,
|
||||||
|
linear_method=linear_method,
|
||||||
)
|
)
|
||||||
self.wo = RowParallelLinear(
|
self.o_proj = RowParallelLinear(
|
||||||
self.total_num_heads * self.head_dim,
|
self.total_num_heads * self.head_dim,
|
||||||
hidden_size,
|
hidden_size,
|
||||||
bias=False,
|
bias=False,
|
||||||
|
linear_method=linear_method,
|
||||||
)
|
)
|
||||||
self.rotary_emb = get_rope(
|
self.rotary_emb = get_rope(
|
||||||
self.head_dim,
|
self.head_dim,
|
||||||
rotary_dim=self.head_dim,
|
rotary_dim=self.head_dim,
|
||||||
max_position=max_position,
|
max_position=max_position,
|
||||||
base=int(self.rope_theta),
|
base=int(self.rope_theta),
|
||||||
is_neox_style=False, # weights not in HF format
|
is_neox_style=True,
|
||||||
)
|
)
|
||||||
self.attn = PagedAttention(
|
self.attn = PagedAttention(
|
||||||
self.num_heads,
|
self.num_heads,
|
||||||
@@ -136,336 +228,91 @@ class MixtralAttention(nn.Module):
|
|||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
kv_cache: KVCache,
|
kv_cache: KVCache,
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_event: Optional[torch.cuda.Event],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
qkv, _ = self.wqkv(hidden_states)
|
qkv, _ = self.qkv_proj(hidden_states)
|
||||||
q, k, v = qkv.split([self.q_size, self.kv_size, self.kv_size], dim=-1)
|
q, k, v = qkv.split([self.q_size, self.kv_size, self.kv_size], dim=-1)
|
||||||
q, k = self.rotary_emb(positions, q, k)
|
q, k = self.rotary_emb(positions, q, k)
|
||||||
k_cache, v_cache = kv_cache
|
k_cache, v_cache = kv_cache
|
||||||
attn_output = self.attn(q, k, v, k_cache, v_cache, input_metadata,
|
attn_output = self.attn(q, k, v, k_cache, v_cache, input_metadata)
|
||||||
cache_event)
|
output, _ = self.o_proj(attn_output)
|
||||||
output, _ = self.wo(attn_output)
|
|
||||||
return output
|
return output
|
||||||
|
|
||||||
|
|
||||||
class BlockSparseMoE(nn.Module):
|
|
||||||
"""
|
|
||||||
Built on the paper and library Megablocks as described in
|
|
||||||
https://arxiv.org/abs/2211.15841. This implementation is
|
|
||||||
strictly equivalent to standard MoE with full capacity (no
|
|
||||||
dropped tokens). It's faster since it formulates MoE operations
|
|
||||||
in terms of block-sparse operations to accomodate imbalanced
|
|
||||||
assignments of tokens to experts, whereas standard MoE either
|
|
||||||
(1) drop tokens at the cost of reduced performance or (2) set
|
|
||||||
capacity factor to number of experts and thus waste computation
|
|
||||||
and memory on padding.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, hidden_dim: int, ffn_dim: int, num_experts: int,
|
|
||||||
top_k: int):
|
|
||||||
super().__init__()
|
|
||||||
self.hidden_dim = hidden_dim
|
|
||||||
self.ffn_dim = ffn_dim
|
|
||||||
self.num_experts = num_experts
|
|
||||||
self.top_k = top_k
|
|
||||||
|
|
||||||
# gating
|
|
||||||
self.gate = nn.Linear(self.hidden_dim,
|
|
||||||
self.num_experts,
|
|
||||||
bias=False,
|
|
||||||
device=torch.cuda.current_device())
|
|
||||||
|
|
||||||
tp_size = get_tensor_model_parallel_world_size()
|
|
||||||
assert self.ffn_dim % tp_size == 0
|
|
||||||
self.ffn_dim_per_partition = self.ffn_dim // tp_size
|
|
||||||
# merged expert weights, all of size (ffn_dim * n_experts, model_dim)
|
|
||||||
self.w1 = nn.Parameter(
|
|
||||||
torch.empty(self.ffn_dim_per_partition * self.num_experts,
|
|
||||||
self.hidden_dim,
|
|
||||||
device=torch.cuda.current_device()))
|
|
||||||
set_weight_attrs(self.w1, {"weight_loader": self.moe_weight_loader})
|
|
||||||
self.w2 = nn.Parameter(
|
|
||||||
torch.empty(self.ffn_dim_per_partition * self.num_experts,
|
|
||||||
self.hidden_dim,
|
|
||||||
device=torch.cuda.current_device()))
|
|
||||||
set_weight_attrs(self.w2, {"weight_loader": self.moe_weight_loader})
|
|
||||||
self.w3 = nn.Parameter(
|
|
||||||
torch.empty(self.ffn_dim_per_partition * self.num_experts,
|
|
||||||
self.hidden_dim,
|
|
||||||
device=torch.cuda.current_device()))
|
|
||||||
set_weight_attrs(self.w3, {"weight_loader": self.moe_weight_loader})
|
|
||||||
|
|
||||||
# Calculate the number of bits needed to represent the expert indices
|
|
||||||
# so that we can pass it to radix sort.
|
|
||||||
self.sort_end_bit = max(int(np.ceil(np.log2(self.num_experts))), 1)
|
|
||||||
self.blocking = 128
|
|
||||||
self.quantize_scatter_num_bits = -1
|
|
||||||
|
|
||||||
# Calculate the number of bits needed to represent the column indices
|
|
||||||
# in the intermediate sparse matrix.
|
|
||||||
max_column_index = (self.ffn_dim * self.num_experts) // self.blocking
|
|
||||||
self.transpose_sort_end_bit = max(
|
|
||||||
int(np.ceil(np.log2(max_column_index))), 1)
|
|
||||||
|
|
||||||
def moe_weight_loader(self, param: nn.Parameter,
|
|
||||||
loaded_weight: torch.Tensor) -> None:
|
|
||||||
"""
|
|
||||||
Load the weights for the MoE linear layer.
|
|
||||||
"""
|
|
||||||
tp_rank = get_tensor_model_parallel_rank()
|
|
||||||
shard_size = self.ffn_dim_per_partition
|
|
||||||
loaded_weight = loaded_weight.view(self.num_experts, self.ffn_dim, -1)
|
|
||||||
loaded_weight = loaded_weight[:, shard_size * tp_rank:shard_size *
|
|
||||||
(tp_rank + 1)]
|
|
||||||
loaded_weight = loaded_weight.reshape_as(param)
|
|
||||||
param.data.copy_(loaded_weight)
|
|
||||||
|
|
||||||
def sparse_transpose(
|
|
||||||
self, size: int, row_indices,
|
|
||||||
column_indices) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
|
|
||||||
block_columns = size[1] // self.blocking
|
|
||||||
|
|
||||||
# Sort row indices by column indices to get the transposed matrix's
|
|
||||||
# column indices.
|
|
||||||
#
|
|
||||||
# NOTE: Our sort operation uses the same width indices as the input
|
|
||||||
# values. To avoid overflow when we have large activation matrices
|
|
||||||
# we cast to 32-bit before sorting.
|
|
||||||
_, gather_indices = ops.sort(column_indices.int(),
|
|
||||||
self.transpose_sort_end_bit)
|
|
||||||
|
|
||||||
# There are a constant number of blocks in every row of the sparse
|
|
||||||
# matrix. A blocks offset is:
|
|
||||||
#
|
|
||||||
# row_index * blocks_per_row + column_index % blocks_per_row
|
|
||||||
#
|
|
||||||
# Once we have the block offsets ordered for transposition we can
|
|
||||||
# divide by blocks_per_row to get the transposed column indices.
|
|
||||||
column_indices_t = row_indices.gather(0, gather_indices.long())
|
|
||||||
block_offsets_t = gather_indices.int()
|
|
||||||
|
|
||||||
zero = torch.zeros((1, ), dtype=torch.int32, device=row_indices.device)
|
|
||||||
nnz_per_column = ops.histogram(column_indices, block_columns)
|
|
||||||
nnz_per_column = ops.inclusive_cumsum(nnz_per_column, 0)
|
|
||||||
offsets_t = torch.cat([zero, nnz_per_column])
|
|
||||||
return column_indices_t, offsets_t, block_offsets_t
|
|
||||||
|
|
||||||
def topology(self, x: torch.Tensor,
|
|
||||||
padded_bins: torch.Tensor) -> "stk.Matrix":
|
|
||||||
padded_tokens, _ = x.size()
|
|
||||||
assert padded_tokens % self.blocking == 0
|
|
||||||
assert self.ffn_dim_per_partition % self.blocking == 0
|
|
||||||
|
|
||||||
# Offsets for the sparse matrix. All rows have the
|
|
||||||
# same number of nonzero blocks dictated by the
|
|
||||||
# dimensionality of a single expert.
|
|
||||||
block_rows = padded_tokens // self.blocking
|
|
||||||
blocks_per_row = self.ffn_dim_per_partition // self.blocking
|
|
||||||
offsets = torch.arange(
|
|
||||||
0,
|
|
||||||
block_rows * blocks_per_row + 1,
|
|
||||||
blocks_per_row,
|
|
||||||
dtype=torch.int32,
|
|
||||||
device=x.device,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Indices for the sparse matrix. The indices for
|
|
||||||
# the intermediate matrix are dynamic depending
|
|
||||||
# on the mapping of tokens to experts.
|
|
||||||
column_indices = ops.topology(padded_bins, self.blocking, block_rows,
|
|
||||||
blocks_per_row)
|
|
||||||
|
|
||||||
# TODO(tgale): This is unused. Remove the need for this in stk.
|
|
||||||
# For now, use meta init to save the device memory.
|
|
||||||
data = torch.empty(
|
|
||||||
column_indices.numel(),
|
|
||||||
self.blocking,
|
|
||||||
self.blocking,
|
|
||||||
dtype=x.dtype,
|
|
||||||
device="meta",
|
|
||||||
)
|
|
||||||
shape = (padded_tokens, self.ffn_dim_per_partition * self.num_experts)
|
|
||||||
row_indices = stk.ops.row_indices(shape, data, offsets, column_indices)
|
|
||||||
column_indices_t, offsets_t, block_offsets_t = self.sparse_transpose(
|
|
||||||
shape, row_indices, column_indices)
|
|
||||||
return stk.Matrix(
|
|
||||||
shape,
|
|
||||||
data,
|
|
||||||
row_indices,
|
|
||||||
column_indices,
|
|
||||||
offsets,
|
|
||||||
column_indices_t,
|
|
||||||
offsets_t,
|
|
||||||
block_offsets_t,
|
|
||||||
)
|
|
||||||
|
|
||||||
def indices_and_padded_bins(
|
|
||||||
self, selected_experts: torch.Tensor
|
|
||||||
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor,
|
|
||||||
torch.Tensor]:
|
|
||||||
# Sort the expert ids to produce the scatter/gather
|
|
||||||
# indices for the permutation.
|
|
||||||
selected_experts = selected_experts.int()
|
|
||||||
bin_ids, indices = ops.sort(selected_experts, self.sort_end_bit)
|
|
||||||
|
|
||||||
# Histogram the expert ids to identify the number of
|
|
||||||
# tokens routed to each expert.
|
|
||||||
tokens_per_expert = ops.histogram(selected_experts, self.num_experts)
|
|
||||||
|
|
||||||
# Round the token counts up to the block size used in
|
|
||||||
# the matrix muliplications. Caculate the starting
|
|
||||||
# position of each bin.
|
|
||||||
padded_tokens_per_expert = ops.round_up(tokens_per_expert,
|
|
||||||
self.blocking)
|
|
||||||
padded_bins = ops.inclusive_cumsum(padded_tokens_per_expert, 0)
|
|
||||||
padded_bins = promote_scalar(padded_bins)
|
|
||||||
|
|
||||||
# Calculate the bin bounds for the sorted tokens.
|
|
||||||
bins = ops.inclusive_cumsum(tokens_per_expert, 0)
|
|
||||||
bins = promote_scalar(bins)
|
|
||||||
return indices, bin_ids, bins, padded_bins, tokens_per_expert
|
|
||||||
|
|
||||||
@torch.inference_mode()
|
|
||||||
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
||||||
"""
|
|
||||||
x: (sequence_length, model_dim)
|
|
||||||
gate_logits: (sequence_length, n_experts)
|
|
||||||
"""
|
|
||||||
# optional reshape
|
|
||||||
input_shape = x.shape
|
|
||||||
x = x.view(-1, input_shape[-1])
|
|
||||||
|
|
||||||
# gate_logits: (sequence_length, n_experts)
|
|
||||||
gate_logits = self.gate(x)
|
|
||||||
# all_probs: (sequence_length, n_experts) and upcast for softmax
|
|
||||||
all_probs = F.softmax(gate_logits, dim=1, dtype=torch.float)
|
|
||||||
# weights, selected_experts: (sequence_length, top-k)
|
|
||||||
weights, selected_experts = torch.topk(all_probs, self.top_k, dim=-1)
|
|
||||||
weights /= weights.sum(dim=-1, keepdim=True)
|
|
||||||
weights = weights.flatten().to(x.dtype)
|
|
||||||
selected_experts = selected_experts.flatten()
|
|
||||||
|
|
||||||
(indices, bin_ids, bins, padded_bins,
|
|
||||||
_) = self.indices_and_padded_bins(selected_experts)
|
|
||||||
|
|
||||||
# Permute tokens and pad to prepare expert computation
|
|
||||||
# (top_k * sequence_length + padding, model_dim)
|
|
||||||
x = ops.padded_gather(x, indices, bin_ids, bins, padded_bins,
|
|
||||||
self.top_k)
|
|
||||||
|
|
||||||
# Create the sparse matrix topology
|
|
||||||
with torch.no_grad():
|
|
||||||
topo = self.topology(x, padded_bins)
|
|
||||||
|
|
||||||
# Perform the expert computation
|
|
||||||
# First Dense x Dense -> Sparse for w1 and w3,
|
|
||||||
# (top_k * sequence_length + padding, ffn_dim * n_experts)
|
|
||||||
x = stk.Matrix(
|
|
||||||
topo.size(),
|
|
||||||
F.silu(stk.ops.sdd(x, self.w1.t(), topo).data) *
|
|
||||||
stk.ops.sdd(x, self.w3.t(), topo).data,
|
|
||||||
topo.row_indices,
|
|
||||||
topo.column_indices,
|
|
||||||
topo.offsets,
|
|
||||||
topo.column_indices_t,
|
|
||||||
topo.offsets_t,
|
|
||||||
topo.block_offsets_t,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Then Sparse x Dense -> Dense for w2
|
|
||||||
# (top_k * sequence_length + padding, model_dim)
|
|
||||||
x = stk.ops.dsd(x, self.w2)
|
|
||||||
|
|
||||||
x = tensor_model_parallel_all_reduce(x)
|
|
||||||
|
|
||||||
# Permute back and remove padding
|
|
||||||
# (top_k * sequence_length, model_dim)
|
|
||||||
x = ops.padded_scatter(
|
|
||||||
x,
|
|
||||||
indices,
|
|
||||||
bin_ids,
|
|
||||||
weights,
|
|
||||||
bins,
|
|
||||||
padded_bins,
|
|
||||||
self.top_k,
|
|
||||||
self.quantize_scatter_num_bits,
|
|
||||||
)
|
|
||||||
return x.view(*input_shape)
|
|
||||||
|
|
||||||
|
|
||||||
class MixtralDecoderLayer(nn.Module):
|
class MixtralDecoderLayer(nn.Module):
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
config: MistralConfig,
|
config: MixtralConfig,
|
||||||
|
linear_method: Optional[LinearMethodBase] = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.hidden_size = config.hidden_size
|
self.hidden_size = config.hidden_size
|
||||||
# Requires transformers > 4.32.0
|
# Requires transformers > 4.32.0
|
||||||
rope_theta = getattr(config, "rope_theta", 10000)
|
rope_theta = getattr(config, "rope_theta", 10000)
|
||||||
self.attention = MixtralAttention(
|
self.self_attn = MixtralAttention(
|
||||||
hidden_size=self.hidden_size,
|
hidden_size=self.hidden_size,
|
||||||
num_heads=config.num_attention_heads,
|
num_heads=config.num_attention_heads,
|
||||||
max_position=config.max_position_embeddings,
|
max_position=config.max_position_embeddings,
|
||||||
num_kv_heads=config.num_key_value_heads,
|
num_kv_heads=config.num_key_value_heads,
|
||||||
rope_theta=rope_theta,
|
rope_theta=rope_theta,
|
||||||
sliding_window=config.sliding_window)
|
sliding_window=config.sliding_window,
|
||||||
self.block_sparse_moe = BlockSparseMoE(
|
linear_method=linear_method)
|
||||||
hidden_dim=self.hidden_size,
|
self.block_sparse_moe = MixtralMoE(config=config,
|
||||||
ffn_dim=config.intermediate_size,
|
linear_method=linear_method)
|
||||||
num_experts=config.num_local_experts,
|
self.input_layernorm = RMSNorm(config.hidden_size,
|
||||||
top_k=config.num_experts_per_tok,
|
eps=config.rms_norm_eps)
|
||||||
)
|
self.post_attention_layernorm = RMSNorm(config.hidden_size,
|
||||||
self.attention_norm = RMSNorm(config.hidden_size,
|
|
||||||
eps=config.rms_norm_eps)
|
eps=config.rms_norm_eps)
|
||||||
self.ffn_norm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
|
||||||
|
|
||||||
def forward(
|
def forward(
|
||||||
self,
|
self,
|
||||||
positions: torch.Tensor,
|
positions: torch.Tensor,
|
||||||
x: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
kv_cache: KVCache,
|
kv_cache: KVCache,
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_event: Optional[torch.cuda.Event],
|
residual: Optional[torch.Tensor],
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
r = self.attention(
|
# Self Attention
|
||||||
|
if residual is None:
|
||||||
|
residual = hidden_states
|
||||||
|
hidden_states = self.input_layernorm(hidden_states)
|
||||||
|
else:
|
||||||
|
hidden_states, residual = self.input_layernorm(
|
||||||
|
hidden_states, residual)
|
||||||
|
hidden_states = self.self_attn(
|
||||||
positions=positions,
|
positions=positions,
|
||||||
hidden_states=self.attention_norm(x),
|
hidden_states=hidden_states,
|
||||||
kv_cache=kv_cache,
|
kv_cache=kv_cache,
|
||||||
input_metadata=input_metadata,
|
input_metadata=input_metadata,
|
||||||
cache_event=cache_event,
|
|
||||||
)
|
)
|
||||||
h = x + r
|
|
||||||
r = self.block_sparse_moe(self.ffn_norm(h))
|
# Fully Connected
|
||||||
out = h + r
|
hidden_states, residual = self.post_attention_layernorm(
|
||||||
return out
|
hidden_states, residual)
|
||||||
|
hidden_states = self.block_sparse_moe(hidden_states)
|
||||||
|
return hidden_states, residual
|
||||||
|
|
||||||
|
|
||||||
class MixtralForCausalLM(nn.Module):
|
class MixtralModel(nn.Module):
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
config: MistralConfig,
|
config: MixtralConfig,
|
||||||
linear_method: Optional[LinearMethodBase] = None,
|
linear_method: Optional[LinearMethodBase] = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.config = config
|
|
||||||
assert linear_method is None
|
|
||||||
self.padding_idx = config.pad_token_id
|
self.padding_idx = config.pad_token_id
|
||||||
self.vocab_size = config.vocab_size
|
self.vocab_size = config.vocab_size
|
||||||
self.tok_embeddings = VocabParallelEmbedding(
|
|
||||||
|
self.embed_tokens = VocabParallelEmbedding(
|
||||||
config.vocab_size,
|
config.vocab_size,
|
||||||
config.hidden_size,
|
config.hidden_size,
|
||||||
)
|
)
|
||||||
|
|
||||||
self.norm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
|
||||||
self.output = ParallelLMHead(config.vocab_size, config.hidden_size)
|
|
||||||
self.sampler = Sampler(config.vocab_size)
|
|
||||||
|
|
||||||
self.layers = nn.ModuleList([
|
self.layers = nn.ModuleList([
|
||||||
MixtralDecoderLayer(config)
|
MixtralDecoderLayer(config, linear_method=linear_method)
|
||||||
for _ in range(config.num_hidden_layers)
|
for _ in range(config.num_hidden_layers)
|
||||||
])
|
])
|
||||||
|
self.norm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
||||||
|
|
||||||
def forward(
|
def forward(
|
||||||
self,
|
self,
|
||||||
@@ -473,30 +320,49 @@ class MixtralForCausalLM(nn.Module):
|
|||||||
positions: torch.Tensor,
|
positions: torch.Tensor,
|
||||||
kv_caches: List[KVCache],
|
kv_caches: List[KVCache],
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_events: Optional[List[torch.cuda.Event]],
|
) -> torch.Tensor:
|
||||||
) -> SamplerOutput:
|
hidden_states = self.embed_tokens(input_ids)
|
||||||
hidden_states = self.tok_embeddings(input_ids)
|
residual = None
|
||||||
|
|
||||||
# forward
|
|
||||||
for i in range(len(self.layers)):
|
for i in range(len(self.layers)):
|
||||||
cache_event = None if cache_events is None else cache_events[i]
|
|
||||||
layer = self.layers[i]
|
layer = self.layers[i]
|
||||||
hidden_states = layer(
|
hidden_states, residual = layer(positions, hidden_states,
|
||||||
positions,
|
kv_caches[i], input_metadata,
|
||||||
hidden_states,
|
residual)
|
||||||
kv_caches[i],
|
hidden_states, _ = self.norm(hidden_states, residual)
|
||||||
input_metadata,
|
return hidden_states
|
||||||
cache_event,
|
|
||||||
)
|
|
||||||
hidden_states = self.norm(hidden_states)
|
class MixtralForCausalLM(nn.Module):
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
config: MixtralConfig,
|
||||||
|
linear_method: Optional[LinearMethodBase] = None,
|
||||||
|
) -> None:
|
||||||
|
super().__init__()
|
||||||
|
self.config = config
|
||||||
|
self.linear_method = linear_method
|
||||||
|
self.model = MixtralModel(config, linear_method)
|
||||||
|
self.lm_head = ParallelLMHead(config.vocab_size, config.hidden_size)
|
||||||
|
self.sampler = Sampler(config.vocab_size)
|
||||||
|
|
||||||
|
def forward(
|
||||||
|
self,
|
||||||
|
input_ids: torch.Tensor,
|
||||||
|
positions: torch.Tensor,
|
||||||
|
kv_caches: List[KVCache],
|
||||||
|
input_metadata: InputMetadata,
|
||||||
|
) -> torch.Tensor:
|
||||||
|
hidden_states = self.model(input_ids, positions, kv_caches,
|
||||||
|
input_metadata)
|
||||||
return hidden_states
|
return hidden_states
|
||||||
|
|
||||||
def sample(
|
def sample(
|
||||||
self,
|
self,
|
||||||
hidden_states: Optional[torch.Tensor],
|
hidden_states: Optional[torch.Tensor],
|
||||||
sampling_metadata: SamplingMetadata,
|
sampling_metadata: SamplingMetadata,
|
||||||
) -> SamplerOutput:
|
) -> Optional[SamplerOutput]:
|
||||||
next_tokens = self.sampler(self.output.weight, hidden_states,
|
next_tokens = self.sampler(self.lm_head.weight, hidden_states,
|
||||||
sampling_metadata)
|
sampling_metadata)
|
||||||
return next_tokens
|
return next_tokens
|
||||||
|
|
||||||
@@ -507,23 +373,39 @@ class MixtralForCausalLM(nn.Module):
|
|||||||
revision: Optional[str] = None):
|
revision: Optional[str] = None):
|
||||||
stacked_params_mapping = [
|
stacked_params_mapping = [
|
||||||
# (param_name, shard_name, shard_id)
|
# (param_name, shard_name, shard_id)
|
||||||
("wqkv", "wq", "q"),
|
("qkv_proj", "q_proj", "q"),
|
||||||
("wqkv", "wk", "k"),
|
("qkv_proj", "k_proj", "k"),
|
||||||
("wqkv", "wv", "v"),
|
("qkv_proj", "v_proj", "v"),
|
||||||
]
|
]
|
||||||
|
|
||||||
params_dict = dict(self.named_parameters())
|
params_dict = dict(self.named_parameters())
|
||||||
for name, loaded_weight in hf_model_weights_iterator(
|
for name, loaded_weight in hf_model_weights_iterator(
|
||||||
model_name_or_path, cache_dir, load_format, revision):
|
model_name_or_path,
|
||||||
|
cache_dir,
|
||||||
|
load_format,
|
||||||
|
revision,
|
||||||
|
fall_back_to_pt=False):
|
||||||
if "rotary_emb.inv_freq" in name:
|
if "rotary_emb.inv_freq" in name:
|
||||||
continue
|
continue
|
||||||
for (param_name, weight_name, shard_id) in stacked_params_mapping:
|
for (param_name, weight_name, shard_id) in stacked_params_mapping:
|
||||||
if weight_name not in name:
|
if weight_name not in name:
|
||||||
continue
|
continue
|
||||||
param = params_dict[name.replace(weight_name, param_name)]
|
name = name.replace(weight_name, param_name)
|
||||||
|
# Skip loading extra bias for GPTQ models.
|
||||||
|
if name.endswith(".bias") and name not in params_dict:
|
||||||
|
continue
|
||||||
|
param = params_dict[name]
|
||||||
weight_loader = param.weight_loader
|
weight_loader = param.weight_loader
|
||||||
weight_loader(param, loaded_weight, shard_id)
|
weight_loader(param, loaded_weight, shard_id)
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
|
# Skip loading extra bias for GPTQ models.
|
||||||
|
if name.endswith(".bias") and name not in params_dict:
|
||||||
|
continue
|
||||||
|
# Skip experts that are not assigned to this worker.
|
||||||
|
if ("block_sparse_moe.experts." in name
|
||||||
|
and name not in params_dict):
|
||||||
|
continue
|
||||||
param = params_dict[name]
|
param = params_dict[name]
|
||||||
weight_loader = getattr(param, "weight_loader",
|
weight_loader = getattr(param, "weight_loader",
|
||||||
default_weight_loader)
|
default_weight_loader)
|
||||||
|
|||||||
@@ -50,9 +50,14 @@ class MPTAttention(nn.Module):
|
|||||||
super().__init__()
|
super().__init__()
|
||||||
self.d_model = config.d_model
|
self.d_model = config.d_model
|
||||||
self.total_num_heads = config.n_heads
|
self.total_num_heads = config.n_heads
|
||||||
|
self.head_dim = self.d_model // self.total_num_heads
|
||||||
self.clip_qkv = config.attn_config["clip_qkv"]
|
self.clip_qkv = config.attn_config["clip_qkv"]
|
||||||
self.qk_ln = config.attn_config["qk_ln"]
|
self.qk_ln = config.attn_config["qk_ln"]
|
||||||
self.alibi_bias_max = config.attn_config["alibi_bias_max"]
|
self.alibi_bias_max = config.attn_config["alibi_bias_max"]
|
||||||
|
if "kv_n_heads" in config.attn_config:
|
||||||
|
self.total_num_kv_heads = config.attn_config['kv_n_heads']
|
||||||
|
else:
|
||||||
|
self.total_num_kv_heads = self.total_num_heads
|
||||||
assert not config.attn_config["prefix_lm"]
|
assert not config.attn_config["prefix_lm"]
|
||||||
assert config.attn_config["alibi"]
|
assert config.attn_config["alibi"]
|
||||||
|
|
||||||
@@ -61,6 +66,7 @@ class MPTAttention(nn.Module):
|
|||||||
self.d_model,
|
self.d_model,
|
||||||
self.d_model // self.total_num_heads,
|
self.d_model // self.total_num_heads,
|
||||||
self.total_num_heads,
|
self.total_num_heads,
|
||||||
|
self.total_num_kv_heads,
|
||||||
bias=not config.no_bias,
|
bias=not config.no_bias,
|
||||||
linear_method=linear_method,
|
linear_method=linear_method,
|
||||||
)
|
)
|
||||||
@@ -78,6 +84,17 @@ class MPTAttention(nn.Module):
|
|||||||
assert self.total_num_heads % tp_world_size == 0
|
assert self.total_num_heads % tp_world_size == 0
|
||||||
self.num_heads = self.total_num_heads // tp_world_size
|
self.num_heads = self.total_num_heads // tp_world_size
|
||||||
|
|
||||||
|
if self.total_num_kv_heads >= tp_world_size:
|
||||||
|
# Number of KV heads is greater than TP size, so we partition
|
||||||
|
# the KV heads across multiple tensor parallel GPUs.
|
||||||
|
assert self.total_num_kv_heads % tp_world_size == 0
|
||||||
|
else:
|
||||||
|
# Number of KV heads is less than TP size, so we replicate
|
||||||
|
# the KV heads across multiple tensor parallel GPUs.
|
||||||
|
assert tp_world_size % self.total_num_kv_heads == 0
|
||||||
|
self.num_kv_heads = max(1, self.total_num_kv_heads // tp_world_size)
|
||||||
|
self.q_size = self.num_heads * self.head_dim
|
||||||
|
self.kv_size = self.num_kv_heads * self.head_dim
|
||||||
# Create the alibi slopes and slice them.
|
# Create the alibi slopes and slice them.
|
||||||
tp_rank = get_tensor_model_parallel_rank()
|
tp_rank = get_tensor_model_parallel_rank()
|
||||||
head_start = tp_rank * self.num_heads
|
head_start = tp_rank * self.num_heads
|
||||||
@@ -91,7 +108,8 @@ class MPTAttention(nn.Module):
|
|||||||
self.attn = PagedAttention(self.num_heads,
|
self.attn = PagedAttention(self.num_heads,
|
||||||
self.head_dim,
|
self.head_dim,
|
||||||
scaling,
|
scaling,
|
||||||
alibi_slopes=alibi_slopes)
|
alibi_slopes=alibi_slopes,
|
||||||
|
num_kv_heads=self.num_kv_heads)
|
||||||
|
|
||||||
def forward(
|
def forward(
|
||||||
self,
|
self,
|
||||||
@@ -99,19 +117,17 @@ class MPTAttention(nn.Module):
|
|||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
kv_cache: KVCache,
|
kv_cache: KVCache,
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_event: Optional[torch.cuda.Event],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
del position_ids # unused.
|
del position_ids # unused.
|
||||||
qkv, _ = self.Wqkv(hidden_states)
|
qkv, _ = self.Wqkv(hidden_states)
|
||||||
if self.clip_qkv is not None:
|
if self.clip_qkv is not None:
|
||||||
qkv.clamp_(min=-self.clip_qkv, max=self.clip_qkv)
|
qkv.clamp_(min=-self.clip_qkv, max=self.clip_qkv)
|
||||||
q, k, v = qkv.chunk(chunks=3, dim=-1)
|
q, k, v = qkv.split([self.q_size, self.kv_size, self.kv_size], dim=-1)
|
||||||
if self.qk_ln:
|
if self.qk_ln:
|
||||||
q = self.q_ln(q)
|
q = self.q_ln(q)
|
||||||
k = self.k_ln(k)
|
k = self.k_ln(k)
|
||||||
k_cache, v_cache = kv_cache
|
k_cache, v_cache = kv_cache
|
||||||
attn_output = self.attn(q, k, v, k_cache, v_cache, input_metadata,
|
attn_output = self.attn(q, k, v, k_cache, v_cache, input_metadata)
|
||||||
cache_event)
|
|
||||||
output, _ = self.out_proj(attn_output)
|
output, _ = self.out_proj(attn_output)
|
||||||
return output
|
return output
|
||||||
|
|
||||||
@@ -169,7 +185,6 @@ class MPTBlock(nn.Module):
|
|||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
kv_cache: KVCache,
|
kv_cache: KVCache,
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_event: Optional[torch.cuda.Event],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
x = self.norm_1(hidden_states)
|
x = self.norm_1(hidden_states)
|
||||||
x = self.attn(
|
x = self.attn(
|
||||||
@@ -177,7 +192,6 @@ class MPTBlock(nn.Module):
|
|||||||
hidden_states=x,
|
hidden_states=x,
|
||||||
kv_cache=kv_cache,
|
kv_cache=kv_cache,
|
||||||
input_metadata=input_metadata,
|
input_metadata=input_metadata,
|
||||||
cache_event=cache_event,
|
|
||||||
)
|
)
|
||||||
hidden_states = hidden_states + x
|
hidden_states = hidden_states + x
|
||||||
x = self.norm_2(hidden_states)
|
x = self.norm_2(hidden_states)
|
||||||
@@ -217,18 +231,15 @@ class MPTModel(nn.Module):
|
|||||||
position_ids: torch.Tensor,
|
position_ids: torch.Tensor,
|
||||||
kv_caches: List[KVCache],
|
kv_caches: List[KVCache],
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_events: Optional[List[torch.cuda.Event]],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
hidden_states = self.wte(input_ids)
|
hidden_states = self.wte(input_ids)
|
||||||
for i in range(len(self.blocks)):
|
for i in range(len(self.blocks)):
|
||||||
cache_event = None if cache_events is None else cache_events[i]
|
|
||||||
block = self.blocks[i]
|
block = self.blocks[i]
|
||||||
hidden_states = block(
|
hidden_states = block(
|
||||||
position_ids,
|
position_ids,
|
||||||
hidden_states,
|
hidden_states,
|
||||||
kv_caches[i],
|
kv_caches[i],
|
||||||
input_metadata,
|
input_metadata,
|
||||||
cache_event,
|
|
||||||
)
|
)
|
||||||
hidden_states = self.norm_f(hidden_states)
|
hidden_states = self.norm_f(hidden_states)
|
||||||
return hidden_states
|
return hidden_states
|
||||||
@@ -256,17 +267,16 @@ class MPTForCausalLM(nn.Module):
|
|||||||
positions: torch.Tensor,
|
positions: torch.Tensor,
|
||||||
kv_caches: List[KVCache],
|
kv_caches: List[KVCache],
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_events: Optional[List[torch.cuda.Event]],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
hidden_states = self.transformer(input_ids, positions, kv_caches,
|
hidden_states = self.transformer(input_ids, positions, kv_caches,
|
||||||
input_metadata, cache_events)
|
input_metadata)
|
||||||
return hidden_states
|
return hidden_states
|
||||||
|
|
||||||
def sample(
|
def sample(
|
||||||
self,
|
self,
|
||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
sampling_metadata: SamplingMetadata,
|
sampling_metadata: SamplingMetadata,
|
||||||
) -> SamplerOutput:
|
) -> Optional[SamplerOutput]:
|
||||||
next_tokens = self.sampler(self.lm_head_weight, hidden_states,
|
next_tokens = self.sampler(self.lm_head_weight, hidden_states,
|
||||||
sampling_metadata)
|
sampling_metadata)
|
||||||
return next_tokens
|
return next_tokens
|
||||||
@@ -279,6 +289,9 @@ class MPTForCausalLM(nn.Module):
|
|||||||
params_dict = dict(self.named_parameters(remove_duplicate=False))
|
params_dict = dict(self.named_parameters(remove_duplicate=False))
|
||||||
for name, loaded_weight in hf_model_weights_iterator(
|
for name, loaded_weight in hf_model_weights_iterator(
|
||||||
model_name_or_path, cache_dir, load_format, revision):
|
model_name_or_path, cache_dir, load_format, revision):
|
||||||
|
# Skip loading extra bias for GPTQ models.
|
||||||
|
if name.endswith(".bias") and name not in params_dict:
|
||||||
|
continue
|
||||||
param = params_dict[name]
|
param = params_dict[name]
|
||||||
weight_loader = getattr(param, "weight_loader",
|
weight_loader = getattr(param, "weight_loader",
|
||||||
default_weight_loader)
|
default_weight_loader)
|
||||||
|
|||||||
@@ -98,13 +98,12 @@ class OPTAttention(nn.Module):
|
|||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
kv_cache: KVCache,
|
kv_cache: KVCache,
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_event: Optional[torch.cuda.Event],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
qkv, _ = self.qkv_proj(hidden_states)
|
qkv, _ = self.qkv_proj(hidden_states)
|
||||||
q, k, v = qkv.chunk(chunks=3, dim=-1)
|
q, k, v = qkv.chunk(chunks=3, dim=-1)
|
||||||
key_cache, value_cache = kv_cache
|
key_cache, value_cache = kv_cache
|
||||||
attn_output = self.attn(q, k, v, key_cache, value_cache,
|
attn_output = self.attn(q, k, v, key_cache, value_cache,
|
||||||
input_metadata, cache_event)
|
input_metadata)
|
||||||
output, _ = self.out_proj(attn_output)
|
output, _ = self.out_proj(attn_output)
|
||||||
return output
|
return output
|
||||||
|
|
||||||
@@ -154,7 +153,6 @@ class OPTDecoderLayer(nn.Module):
|
|||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
kv_cache: KVCache,
|
kv_cache: KVCache,
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_event: Optional[torch.cuda.Event],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
# Self Attention
|
# Self Attention
|
||||||
residual = hidden_states
|
residual = hidden_states
|
||||||
@@ -163,8 +161,7 @@ class OPTDecoderLayer(nn.Module):
|
|||||||
hidden_states = self.self_attn_layer_norm(hidden_states)
|
hidden_states = self.self_attn_layer_norm(hidden_states)
|
||||||
hidden_states = self.self_attn(hidden_states=hidden_states,
|
hidden_states = self.self_attn(hidden_states=hidden_states,
|
||||||
kv_cache=kv_cache,
|
kv_cache=kv_cache,
|
||||||
input_metadata=input_metadata,
|
input_metadata=input_metadata)
|
||||||
cache_event=cache_event)
|
|
||||||
hidden_states = residual + hidden_states
|
hidden_states = residual + hidden_states
|
||||||
# 350m applies layer norm AFTER attention
|
# 350m applies layer norm AFTER attention
|
||||||
if not self.do_layer_norm_before:
|
if not self.do_layer_norm_before:
|
||||||
@@ -245,7 +242,6 @@ class OPTDecoder(nn.Module):
|
|||||||
positions: torch.Tensor,
|
positions: torch.Tensor,
|
||||||
kv_caches: List[KVCache],
|
kv_caches: List[KVCache],
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_events: Optional[List[torch.cuda.Event]],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
inputs_embeds = self.embed_tokens(input_ids)
|
inputs_embeds = self.embed_tokens(input_ids)
|
||||||
pos_embeds = self.embed_positions(positions)
|
pos_embeds = self.embed_positions(positions)
|
||||||
@@ -254,10 +250,8 @@ class OPTDecoder(nn.Module):
|
|||||||
hidden_states = inputs_embeds + pos_embeds
|
hidden_states = inputs_embeds + pos_embeds
|
||||||
|
|
||||||
for i in range(len(self.layers)):
|
for i in range(len(self.layers)):
|
||||||
cache_event = None if cache_events is None else cache_events[i]
|
|
||||||
layer = self.layers[i]
|
layer = self.layers[i]
|
||||||
hidden_states = layer(hidden_states, kv_caches[i], input_metadata,
|
hidden_states = layer(hidden_states, kv_caches[i], input_metadata)
|
||||||
cache_event)
|
|
||||||
|
|
||||||
if self.final_layer_norm is not None:
|
if self.final_layer_norm is not None:
|
||||||
hidden_states = self.final_layer_norm(hidden_states)
|
hidden_states = self.final_layer_norm(hidden_states)
|
||||||
@@ -282,10 +276,8 @@ class OPTModel(nn.Module):
|
|||||||
positions: torch.Tensor,
|
positions: torch.Tensor,
|
||||||
kv_caches: List[KVCache],
|
kv_caches: List[KVCache],
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_events: Optional[List[torch.cuda.Event]],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
return self.decoder(input_ids, positions, kv_caches, input_metadata,
|
return self.decoder(input_ids, positions, kv_caches, input_metadata)
|
||||||
cache_events)
|
|
||||||
|
|
||||||
|
|
||||||
class OPTForCausalLM(nn.Module):
|
class OPTForCausalLM(nn.Module):
|
||||||
@@ -308,17 +300,16 @@ class OPTForCausalLM(nn.Module):
|
|||||||
positions: torch.Tensor,
|
positions: torch.Tensor,
|
||||||
kv_caches: List[KVCache],
|
kv_caches: List[KVCache],
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_events: Optional[List[torch.cuda.Event]],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
hidden_states = self.model(input_ids, positions, kv_caches,
|
hidden_states = self.model(input_ids, positions, kv_caches,
|
||||||
input_metadata, cache_events)
|
input_metadata)
|
||||||
return hidden_states
|
return hidden_states
|
||||||
|
|
||||||
def sample(
|
def sample(
|
||||||
self,
|
self,
|
||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
sampling_metadata: SamplingMetadata,
|
sampling_metadata: SamplingMetadata,
|
||||||
) -> SamplerOutput:
|
) -> Optional[SamplerOutput]:
|
||||||
next_tokens = self.sampler(self.lm_head_weight, hidden_states,
|
next_tokens = self.sampler(self.lm_head_weight, hidden_states,
|
||||||
sampling_metadata)
|
sampling_metadata)
|
||||||
return next_tokens
|
return next_tokens
|
||||||
@@ -345,11 +336,18 @@ class OPTForCausalLM(nn.Module):
|
|||||||
for (param_name, weight_name, shard_id) in stacked_params_mapping:
|
for (param_name, weight_name, shard_id) in stacked_params_mapping:
|
||||||
if weight_name not in name:
|
if weight_name not in name:
|
||||||
continue
|
continue
|
||||||
param = params_dict[name.replace(weight_name, param_name)]
|
name = name.replace(weight_name, param_name)
|
||||||
|
# Skip loading extra bias for GPTQ models.
|
||||||
|
if name.endswith(".bias") and name not in params_dict:
|
||||||
|
continue
|
||||||
|
param = params_dict[name]
|
||||||
weight_loader = param.weight_loader
|
weight_loader = param.weight_loader
|
||||||
weight_loader(param, loaded_weight, shard_id)
|
weight_loader(param, loaded_weight, shard_id)
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
|
# Skip loading extra bias for GPTQ models.
|
||||||
|
if name.endswith(".bias") and name not in params_dict:
|
||||||
|
continue
|
||||||
param = params_dict[name]
|
param = params_dict[name]
|
||||||
weight_loader = getattr(param, "weight_loader",
|
weight_loader = getattr(param, "weight_loader",
|
||||||
default_weight_loader)
|
default_weight_loader)
|
||||||
|
|||||||
@@ -135,14 +135,12 @@ class PhiAttention(nn.Module):
|
|||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
kv_cache: KVCache,
|
kv_cache: KVCache,
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_event: Optional[torch.cuda.Event],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
qkv, _ = self.Wqkv(hidden_states)
|
qkv, _ = self.Wqkv(hidden_states)
|
||||||
q, k, v = qkv.chunk(chunks=3, dim=-1)
|
q, k, v = qkv.chunk(chunks=3, dim=-1)
|
||||||
q, k = self.rotary_emb(position_ids, q, k)
|
q, k = self.rotary_emb(position_ids, q, k)
|
||||||
k_cache, v_cache = kv_cache
|
k_cache, v_cache = kv_cache
|
||||||
attn_output = self.attn(q, k, v, k_cache, v_cache, input_metadata,
|
attn_output = self.attn(q, k, v, k_cache, v_cache, input_metadata)
|
||||||
cache_event)
|
|
||||||
output, _ = self.out_proj(attn_output)
|
output, _ = self.out_proj(attn_output)
|
||||||
return output
|
return output
|
||||||
|
|
||||||
@@ -195,7 +193,6 @@ class PhiLayer(nn.Module):
|
|||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
kv_cache: KVCache,
|
kv_cache: KVCache,
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_event: Optional[torch.cuda.Event],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
residual = hidden_states
|
residual = hidden_states
|
||||||
hidden_states = self.ln(hidden_states)
|
hidden_states = self.ln(hidden_states)
|
||||||
@@ -204,7 +201,6 @@ class PhiLayer(nn.Module):
|
|||||||
hidden_states=hidden_states,
|
hidden_states=hidden_states,
|
||||||
kv_cache=kv_cache,
|
kv_cache=kv_cache,
|
||||||
input_metadata=input_metadata,
|
input_metadata=input_metadata,
|
||||||
cache_event=cache_event,
|
|
||||||
)
|
)
|
||||||
feed_forward_hidden_states = self.mlp(hidden_states)
|
feed_forward_hidden_states = self.mlp(hidden_states)
|
||||||
hidden_states = attn_outputs + feed_forward_hidden_states + residual
|
hidden_states = attn_outputs + feed_forward_hidden_states + residual
|
||||||
@@ -231,18 +227,15 @@ class PhiModel(nn.Module):
|
|||||||
positions: torch.Tensor,
|
positions: torch.Tensor,
|
||||||
kv_caches: List[KVCache],
|
kv_caches: List[KVCache],
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_events: Optional[List[torch.cuda.Event]],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
hidden_states = self.embd(input_ids)
|
hidden_states = self.embd(input_ids)
|
||||||
for i in range(self.config.num_hidden_layers):
|
for i in range(self.config.num_hidden_layers):
|
||||||
cache_event = None if cache_events is None else cache_events[i]
|
|
||||||
layer = self.h[i]
|
layer = self.h[i]
|
||||||
hidden_states = layer(
|
hidden_states = layer(
|
||||||
positions,
|
positions,
|
||||||
hidden_states,
|
hidden_states,
|
||||||
kv_caches[i],
|
kv_caches[i],
|
||||||
input_metadata,
|
input_metadata,
|
||||||
cache_event,
|
|
||||||
)
|
)
|
||||||
return hidden_states
|
return hidden_states
|
||||||
|
|
||||||
@@ -277,10 +270,9 @@ class PhiForCausalLM(nn.Module):
|
|||||||
positions: torch.Tensor,
|
positions: torch.Tensor,
|
||||||
kv_caches: List[KVCache],
|
kv_caches: List[KVCache],
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_events: Optional[List[torch.cuda.Event]],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
hidden_states = self.transformer(input_ids, positions, kv_caches,
|
hidden_states = self.transformer(input_ids, positions, kv_caches,
|
||||||
input_metadata, cache_events)
|
input_metadata)
|
||||||
hidden_states = self.lm_head.ln(hidden_states)
|
hidden_states = self.lm_head.ln(hidden_states)
|
||||||
return hidden_states
|
return hidden_states
|
||||||
|
|
||||||
@@ -288,7 +280,7 @@ class PhiForCausalLM(nn.Module):
|
|||||||
self,
|
self,
|
||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
sampling_metadata: SamplingMetadata,
|
sampling_metadata: SamplingMetadata,
|
||||||
) -> SamplerOutput:
|
) -> Optional[SamplerOutput]:
|
||||||
head = self.lm_head.linear
|
head = self.lm_head.linear
|
||||||
next_tokens = self.sampler(head.weight, hidden_states,
|
next_tokens = self.sampler(head.weight, hidden_states,
|
||||||
sampling_metadata, head.bias)
|
sampling_metadata, head.bias)
|
||||||
@@ -305,6 +297,9 @@ class PhiForCausalLM(nn.Module):
|
|||||||
if "rotary_emb.inv_freq" in name:
|
if "rotary_emb.inv_freq" in name:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
# Skip loading extra bias for GPTQ models.
|
||||||
|
if name.endswith(".bias") and name not in params_dict:
|
||||||
|
continue
|
||||||
# pylint: disable=E1136
|
# pylint: disable=E1136
|
||||||
param = params_dict[name]
|
param = params_dict[name]
|
||||||
weight_loader = getattr(param, "weight_loader",
|
weight_loader = getattr(param, "weight_loader",
|
||||||
|
|||||||
@@ -82,7 +82,6 @@ class QWenAttention(nn.Module):
|
|||||||
self.num_heads = (self.total_num_heads //
|
self.num_heads = (self.total_num_heads //
|
||||||
tensor_model_parallel_world_size)
|
tensor_model_parallel_world_size)
|
||||||
self.head_dim = hidden_size // self.total_num_heads
|
self.head_dim = hidden_size // self.total_num_heads
|
||||||
|
|
||||||
self.c_attn = QKVParallelLinear(
|
self.c_attn = QKVParallelLinear(
|
||||||
hidden_size,
|
hidden_size,
|
||||||
self.head_dim,
|
self.head_dim,
|
||||||
@@ -113,14 +112,12 @@ class QWenAttention(nn.Module):
|
|||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
kv_cache: KVCache,
|
kv_cache: KVCache,
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_event: Optional[torch.cuda.Event],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
qkv, _ = self.c_attn(hidden_states)
|
qkv, _ = self.c_attn(hidden_states)
|
||||||
q, k, v = qkv.chunk(chunks=3, dim=-1)
|
q, k, v = qkv.chunk(chunks=3, dim=-1)
|
||||||
q, k = self.rotary_emb(positions, q, k)
|
q, k = self.rotary_emb(positions, q, k)
|
||||||
k_cache, v_cache = kv_cache
|
k_cache, v_cache = kv_cache
|
||||||
attn_output = self.attn(q, k, v, k_cache, v_cache, input_metadata,
|
attn_output = self.attn(q, k, v, k_cache, v_cache, input_metadata)
|
||||||
cache_event)
|
|
||||||
|
|
||||||
output, _ = self.c_proj(attn_output)
|
output, _ = self.c_proj(attn_output)
|
||||||
return output
|
return output
|
||||||
@@ -157,7 +154,6 @@ class QWenBlock(nn.Module):
|
|||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
kv_cache: KVCache,
|
kv_cache: KVCache,
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_event: Optional[torch.cuda.Event],
|
|
||||||
residual: Optional[torch.Tensor],
|
residual: Optional[torch.Tensor],
|
||||||
) -> Tuple[torch.Tensor, torch.Tensor]:
|
) -> Tuple[torch.Tensor, torch.Tensor]:
|
||||||
# Self Attention
|
# Self Attention
|
||||||
@@ -171,7 +167,6 @@ class QWenBlock(nn.Module):
|
|||||||
hidden_states=hidden_states,
|
hidden_states=hidden_states,
|
||||||
kv_cache=kv_cache,
|
kv_cache=kv_cache,
|
||||||
input_metadata=input_metadata,
|
input_metadata=input_metadata,
|
||||||
cache_event=cache_event,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Fully Connected
|
# Fully Connected
|
||||||
@@ -207,19 +202,16 @@ class QWenModel(nn.Module):
|
|||||||
positions: torch.Tensor,
|
positions: torch.Tensor,
|
||||||
kv_caches: List[KVCache],
|
kv_caches: List[KVCache],
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_events: Optional[List[torch.cuda.Event]],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
hidden_states = self.wte(input_ids)
|
hidden_states = self.wte(input_ids)
|
||||||
residual = None
|
residual = None
|
||||||
for i in range(len(self.h)):
|
for i in range(len(self.h)):
|
||||||
cache_event = None if cache_events is None else cache_events[i]
|
|
||||||
layer = self.h[i]
|
layer = self.h[i]
|
||||||
hidden_states, residual = layer(
|
hidden_states, residual = layer(
|
||||||
positions,
|
positions,
|
||||||
hidden_states,
|
hidden_states,
|
||||||
kv_caches[i],
|
kv_caches[i],
|
||||||
input_metadata,
|
input_metadata,
|
||||||
cache_event,
|
|
||||||
residual,
|
residual,
|
||||||
)
|
)
|
||||||
hidden_states, _ = self.ln_f(hidden_states, residual)
|
hidden_states, _ = self.ln_f(hidden_states, residual)
|
||||||
@@ -246,17 +238,16 @@ class QWenLMHeadModel(nn.Module):
|
|||||||
positions: torch.Tensor,
|
positions: torch.Tensor,
|
||||||
kv_caches: List[KVCache],
|
kv_caches: List[KVCache],
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_events: Optional[List[torch.cuda.Event]],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
hidden_states = self.transformer(input_ids, positions, kv_caches,
|
hidden_states = self.transformer(input_ids, positions, kv_caches,
|
||||||
input_metadata, cache_events)
|
input_metadata)
|
||||||
return hidden_states
|
return hidden_states
|
||||||
|
|
||||||
def sample(
|
def sample(
|
||||||
self,
|
self,
|
||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
sampling_metadata: SamplingMetadata,
|
sampling_metadata: SamplingMetadata,
|
||||||
) -> SamplerOutput:
|
) -> Optional[SamplerOutput]:
|
||||||
next_tokens = self.sampler(self.lm_head.weight, hidden_states,
|
next_tokens = self.sampler(self.lm_head.weight, hidden_states,
|
||||||
sampling_metadata)
|
sampling_metadata)
|
||||||
return next_tokens
|
return next_tokens
|
||||||
@@ -279,11 +270,18 @@ class QWenLMHeadModel(nn.Module):
|
|||||||
for (param_name, weight_name, shard_id) in stacked_params_mapping:
|
for (param_name, weight_name, shard_id) in stacked_params_mapping:
|
||||||
if weight_name not in name:
|
if weight_name not in name:
|
||||||
continue
|
continue
|
||||||
param = params_dict[name.replace(weight_name, param_name)]
|
name = name.replace(weight_name, param_name)
|
||||||
|
# Skip loading extra bias for GPTQ models.
|
||||||
|
if name.endswith(".bias") and name not in params_dict:
|
||||||
|
continue
|
||||||
|
param = params_dict[name]
|
||||||
weight_loader = param.weight_loader
|
weight_loader = param.weight_loader
|
||||||
weight_loader(param, loaded_weight, shard_id)
|
weight_loader(param, loaded_weight, shard_id)
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
|
# Skip loading extra bias for GPTQ models.
|
||||||
|
if name.endswith(".bias") and name not in params_dict:
|
||||||
|
continue
|
||||||
param = params_dict[name]
|
param = params_dict[name]
|
||||||
weight_loader = getattr(param, "weight_loader",
|
weight_loader = getattr(param, "weight_loader",
|
||||||
default_weight_loader)
|
default_weight_loader)
|
||||||
|
|||||||
@@ -146,14 +146,12 @@ class YiAttention(nn.Module):
|
|||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
kv_cache: KVCache,
|
kv_cache: KVCache,
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_event: Optional[torch.cuda.Event],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
qkv, _ = self.qkv_proj(hidden_states)
|
qkv, _ = self.qkv_proj(hidden_states)
|
||||||
q, k, v = qkv.split([self.q_size, self.kv_size, self.kv_size], dim=-1)
|
q, k, v = qkv.split([self.q_size, self.kv_size, self.kv_size], dim=-1)
|
||||||
q, k = self.rotary_emb(positions, q, k)
|
q, k = self.rotary_emb(positions, q, k)
|
||||||
k_cache, v_cache = kv_cache
|
k_cache, v_cache = kv_cache
|
||||||
attn_output = self.attn(q, k, v, k_cache, v_cache, input_metadata,
|
attn_output = self.attn(q, k, v, k_cache, v_cache, input_metadata)
|
||||||
cache_event)
|
|
||||||
output, _ = self.o_proj(attn_output)
|
output, _ = self.o_proj(attn_output)
|
||||||
return output
|
return output
|
||||||
|
|
||||||
@@ -195,7 +193,6 @@ class YiDecoderLayer(nn.Module):
|
|||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
kv_cache: KVCache,
|
kv_cache: KVCache,
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_event: Optional[torch.cuda.Event],
|
|
||||||
residual: Optional[torch.Tensor],
|
residual: Optional[torch.Tensor],
|
||||||
) -> Tuple[torch.Tensor, torch.Tensor]:
|
) -> Tuple[torch.Tensor, torch.Tensor]:
|
||||||
# Self Attention
|
# Self Attention
|
||||||
@@ -209,7 +206,6 @@ class YiDecoderLayer(nn.Module):
|
|||||||
hidden_states=hidden_states,
|
hidden_states=hidden_states,
|
||||||
kv_cache=kv_cache,
|
kv_cache=kv_cache,
|
||||||
input_metadata=input_metadata,
|
input_metadata=input_metadata,
|
||||||
cache_event=cache_event,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Fully Connected
|
# Fully Connected
|
||||||
@@ -245,19 +241,16 @@ class YiModel(nn.Module):
|
|||||||
positions: torch.Tensor,
|
positions: torch.Tensor,
|
||||||
kv_caches: List[KVCache],
|
kv_caches: List[KVCache],
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_events: Optional[List[torch.cuda.Event]],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
hidden_states = self.embed_tokens(input_ids)
|
hidden_states = self.embed_tokens(input_ids)
|
||||||
residual = None
|
residual = None
|
||||||
for i in range(len(self.layers)):
|
for i in range(len(self.layers)):
|
||||||
cache_event = None if cache_events is None else cache_events[i]
|
|
||||||
layer = self.layers[i]
|
layer = self.layers[i]
|
||||||
hidden_states, residual = layer(
|
hidden_states, residual = layer(
|
||||||
positions,
|
positions,
|
||||||
hidden_states,
|
hidden_states,
|
||||||
kv_caches[i],
|
kv_caches[i],
|
||||||
input_metadata,
|
input_metadata,
|
||||||
cache_event,
|
|
||||||
residual,
|
residual,
|
||||||
)
|
)
|
||||||
hidden_states, _ = self.norm(hidden_states, residual)
|
hidden_states, _ = self.norm(hidden_states, residual)
|
||||||
@@ -284,17 +277,16 @@ class YiForCausalLM(nn.Module):
|
|||||||
positions: torch.Tensor,
|
positions: torch.Tensor,
|
||||||
kv_caches: List[KVCache],
|
kv_caches: List[KVCache],
|
||||||
input_metadata: InputMetadata,
|
input_metadata: InputMetadata,
|
||||||
cache_events: Optional[List[torch.cuda.Event]],
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
hidden_states = self.model(input_ids, positions, kv_caches,
|
hidden_states = self.model(input_ids, positions, kv_caches,
|
||||||
input_metadata, cache_events)
|
input_metadata)
|
||||||
return hidden_states
|
return hidden_states
|
||||||
|
|
||||||
def sample(
|
def sample(
|
||||||
self,
|
self,
|
||||||
hidden_states: torch.Tensor,
|
hidden_states: torch.Tensor,
|
||||||
sampling_metadata: SamplingMetadata,
|
sampling_metadata: SamplingMetadata,
|
||||||
) -> SamplerOutput:
|
) -> Optional[SamplerOutput]:
|
||||||
next_tokens = self.sampler(self.lm_head.weight, hidden_states,
|
next_tokens = self.sampler(self.lm_head.weight, hidden_states,
|
||||||
sampling_metadata)
|
sampling_metadata)
|
||||||
return next_tokens
|
return next_tokens
|
||||||
@@ -320,11 +312,18 @@ class YiForCausalLM(nn.Module):
|
|||||||
for (param_name, weight_name, shard_id) in stacked_params_mapping:
|
for (param_name, weight_name, shard_id) in stacked_params_mapping:
|
||||||
if weight_name not in name:
|
if weight_name not in name:
|
||||||
continue
|
continue
|
||||||
param = params_dict[name.replace(weight_name, param_name)]
|
name = name.replace(weight_name, param_name)
|
||||||
|
# Skip loading extra bias for GPTQ models.
|
||||||
|
if name.endswith(".bias") and name not in params_dict:
|
||||||
|
continue
|
||||||
|
param = params_dict[name]
|
||||||
weight_loader = param.weight_loader
|
weight_loader = param.weight_loader
|
||||||
weight_loader(param, loaded_weight, shard_id)
|
weight_loader(param, loaded_weight, shard_id)
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
|
# Skip loading extra bias for GPTQ models.
|
||||||
|
if name.endswith(".bias") and name not in params_dict:
|
||||||
|
continue
|
||||||
param = params_dict[name]
|
param = params_dict[name]
|
||||||
weight_loader = getattr(param, "weight_loader",
|
weight_loader = getattr(param, "weight_loader",
|
||||||
default_weight_loader)
|
default_weight_loader)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import torch
|
import torch
|
||||||
|
|
||||||
from vllm.model_executor.parallel_utils.parallel_state import (
|
from vllm.model_executor.parallel_utils.parallel_state import (
|
||||||
|
get_tensor_model_parallel_rank,
|
||||||
get_tensor_model_parallel_world_size,
|
get_tensor_model_parallel_world_size,
|
||||||
get_tensor_model_parallel_group,
|
get_tensor_model_parallel_group,
|
||||||
)
|
)
|
||||||
@@ -45,3 +46,61 @@ def tensor_model_parallel_all_gather(input_, dim=-1):
|
|||||||
(world_size * input_size[dim], ) +
|
(world_size * input_size[dim], ) +
|
||||||
input_size[dim + 1:])
|
input_size[dim + 1:])
|
||||||
return output_tensor
|
return output_tensor
|
||||||
|
|
||||||
|
|
||||||
|
def tensor_model_parallel_gather(input_, dst=0, dim=-1):
|
||||||
|
"""Gather the input tensor across model parallel group.
|
||||||
|
|
||||||
|
NOTE: We assume that the input tensor is on the same device across
|
||||||
|
all the ranks.
|
||||||
|
"""
|
||||||
|
world_size = get_tensor_model_parallel_world_size()
|
||||||
|
# Bypass the function if we are using only 1 GPU.
|
||||||
|
if world_size == 1:
|
||||||
|
return input_
|
||||||
|
assert -input_.dim() <= dim < input_.dim(), (
|
||||||
|
f"Invalid dim ({dim}) for input tensor with shape {input_.size()}")
|
||||||
|
if dim < 0:
|
||||||
|
# Convert negative dim to positive.
|
||||||
|
dim += input_.dim()
|
||||||
|
# Allocate output tensor.
|
||||||
|
if get_tensor_model_parallel_rank() == dst:
|
||||||
|
gather_list = [torch.empty_like(input_) for _ in range(world_size)]
|
||||||
|
else:
|
||||||
|
gather_list = None
|
||||||
|
# Gather.
|
||||||
|
torch.distributed.gather(input_,
|
||||||
|
gather_list,
|
||||||
|
dst=dst,
|
||||||
|
group=get_tensor_model_parallel_group())
|
||||||
|
if get_tensor_model_parallel_rank() == dst:
|
||||||
|
output_tensor = torch.cat(gather_list, dim=dim)
|
||||||
|
else:
|
||||||
|
output_tensor = None
|
||||||
|
return output_tensor
|
||||||
|
|
||||||
|
|
||||||
|
def broadcast(input_, src=0):
|
||||||
|
"""Broadcast the input tensor."""
|
||||||
|
world_size = torch.distributed.get_world_size()
|
||||||
|
assert 0 <= src < world_size, f"Invalid src rank ({src})"
|
||||||
|
|
||||||
|
# Bypass the function if we are using only 1 GPU.
|
||||||
|
if world_size == 1:
|
||||||
|
return input_
|
||||||
|
# Broadcast.
|
||||||
|
torch.distributed.broadcast(input_, src=src)
|
||||||
|
return input_
|
||||||
|
|
||||||
|
|
||||||
|
def broadcast_object_list(obj_list, src=0):
|
||||||
|
"""Broadcast the input object list."""
|
||||||
|
world_size = torch.distributed.get_world_size()
|
||||||
|
assert 0 <= src < world_size, f"Invalid src rank ({src})"
|
||||||
|
|
||||||
|
# Bypass the function if we are using only 1 GPU.
|
||||||
|
if world_size == 1:
|
||||||
|
return obj_list
|
||||||
|
# Broadcast.
|
||||||
|
torch.distributed.broadcast_object_list(obj_list, src=src)
|
||||||
|
return obj_list
|
||||||
|
|||||||
@@ -1,9 +1,13 @@
|
|||||||
from typing import Dict, List, Tuple
|
from dataclasses import dataclass
|
||||||
|
from typing import Dict, List, Optional, Tuple
|
||||||
|
|
||||||
import torch
|
import torch
|
||||||
|
|
||||||
from vllm.sampling_params import SamplingParams, SamplingType
|
from vllm.sampling_params import SamplingParams, SamplingType
|
||||||
from vllm.sequence import SequenceData
|
from vllm.sequence import SequenceData
|
||||||
|
from vllm.utils import in_wsl
|
||||||
|
|
||||||
|
_SAMPLING_EPS = 1e-5
|
||||||
|
|
||||||
|
|
||||||
class SamplingMetadata:
|
class SamplingMetadata:
|
||||||
@@ -14,24 +18,29 @@ class SamplingMetadata:
|
|||||||
seq_data: Seq_id -> SequenceData.
|
seq_data: Seq_id -> SequenceData.
|
||||||
prompt_lens: Lengths of prompts.
|
prompt_lens: Lengths of prompts.
|
||||||
selected_token_indices: Token indices selected for sampling.
|
selected_token_indices: Token indices selected for sampling.
|
||||||
categorized_sample_indices: SamplingType -> token indicies to sample.
|
categorized_sample_indices: SamplingType -> token indices to sample.
|
||||||
|
perform_sampling: Whether to perform sampling. This option is used to
|
||||||
|
make the sampling only happens in the driver worker, and disable
|
||||||
|
sampling in other worker processes.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
seq_groups: List[Tuple[List[int], SamplingParams]],
|
seq_groups: Optional[List[Tuple[List[int], SamplingParams]]],
|
||||||
seq_data: Dict[int, SequenceData],
|
seq_data: Optional[Dict[int, SequenceData]],
|
||||||
prompt_lens: List[int],
|
prompt_lens: Optional[List[int]],
|
||||||
selected_token_indices: torch.Tensor,
|
selected_token_indices: torch.Tensor,
|
||||||
categorized_sample_indices: Dict[SamplingType, torch.Tensor],
|
categorized_sample_indices: Optional[Dict[SamplingType, torch.Tensor]],
|
||||||
|
perform_sampling: bool = True,
|
||||||
) -> None:
|
) -> None:
|
||||||
self.seq_groups = seq_groups
|
self.seq_groups = seq_groups
|
||||||
self.seq_data = seq_data
|
self.seq_data = seq_data
|
||||||
self.prompt_lens = prompt_lens
|
self.prompt_lens = prompt_lens
|
||||||
self.selected_token_indices = selected_token_indices
|
self.selected_token_indices = selected_token_indices
|
||||||
self.categorized_sample_indices = categorized_sample_indices
|
self.categorized_sample_indices = categorized_sample_indices
|
||||||
|
self.perform_sampling = perform_sampling
|
||||||
|
|
||||||
self.num_prompts = len(prompt_lens)
|
self.num_prompts = len(prompt_lens) if prompt_lens is not None else 0
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return (
|
return (
|
||||||
@@ -40,4 +49,188 @@ class SamplingMetadata:
|
|||||||
f"seq_data={self.seq_data}, "
|
f"seq_data={self.seq_data}, "
|
||||||
f"prompt_lens={self.prompt_lens}, "
|
f"prompt_lens={self.prompt_lens}, "
|
||||||
f"selected_token_indices={self.selected_token_indices}, "
|
f"selected_token_indices={self.selected_token_indices}, "
|
||||||
f"categorized_sample_indices={self.categorized_sample_indices})")
|
f"categorized_sample_indices={self.categorized_sample_indices}), "
|
||||||
|
f"perform_sampling={self.perform_sampling})")
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class SamplingTensors:
|
||||||
|
"""Tensors for sampling."""
|
||||||
|
|
||||||
|
temperatures: torch.Tensor
|
||||||
|
top_ps: torch.Tensor
|
||||||
|
top_ks: torch.Tensor
|
||||||
|
min_ps: torch.Tensor
|
||||||
|
presence_penalties: torch.Tensor
|
||||||
|
frequency_penalties: torch.Tensor
|
||||||
|
repetition_penalties: torch.Tensor
|
||||||
|
prompt_tokens: torch.Tensor
|
||||||
|
output_tokens: torch.Tensor
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_sampling_metadata(
|
||||||
|
cls, sampling_metadata: "SamplingMetadata", vocab_size: int,
|
||||||
|
device: torch.device,
|
||||||
|
dtype: torch.dtype) -> Tuple["SamplingTensors", bool, bool, bool]:
|
||||||
|
prompt_tokens: List[List[int]] = []
|
||||||
|
output_tokens: List[List[int]] = []
|
||||||
|
top_ks: List[int] = []
|
||||||
|
temperatures: List[float] = []
|
||||||
|
top_ps: List[float] = []
|
||||||
|
min_ps: List[float] = []
|
||||||
|
presence_penalties: List[float] = []
|
||||||
|
frequency_penalties: List[float] = []
|
||||||
|
repetition_penalties: List[float] = []
|
||||||
|
do_penalties = False
|
||||||
|
do_top_p_top_k = False
|
||||||
|
do_min_p = False
|
||||||
|
for i, seq_group in enumerate(sampling_metadata.seq_groups):
|
||||||
|
seq_ids, sampling_params = seq_group
|
||||||
|
temperature = sampling_params.temperature
|
||||||
|
p = sampling_params.presence_penalty
|
||||||
|
f = sampling_params.frequency_penalty
|
||||||
|
r = sampling_params.repetition_penalty
|
||||||
|
top_p = sampling_params.top_p
|
||||||
|
min_p = sampling_params.min_p
|
||||||
|
# k should not be greater than the vocab size.
|
||||||
|
top_k = min(sampling_params.top_k, vocab_size)
|
||||||
|
top_k = vocab_size if top_k == -1 else top_k
|
||||||
|
if temperature < _SAMPLING_EPS:
|
||||||
|
# NOTE: Zero temperature means deterministic sampling
|
||||||
|
# (i.e., greedy sampling or beam search).
|
||||||
|
# Set the temperature to 1 to avoid division by zero.
|
||||||
|
temperature = 1.0
|
||||||
|
if not do_top_p_top_k and (top_p < 1.0 - _SAMPLING_EPS
|
||||||
|
or top_k != vocab_size):
|
||||||
|
do_top_p_top_k = True
|
||||||
|
if not do_min_p and min_p > _SAMPLING_EPS:
|
||||||
|
do_min_p = True
|
||||||
|
if not do_penalties and (abs(p) >= _SAMPLING_EPS
|
||||||
|
or abs(f) >= _SAMPLING_EPS
|
||||||
|
or abs(r - 1.0) >= _SAMPLING_EPS):
|
||||||
|
do_penalties = True
|
||||||
|
if (i < sampling_metadata.num_prompts
|
||||||
|
and sampling_params.prompt_logprobs is not None):
|
||||||
|
# For tokens in the prompt that we only need to get their logprobs
|
||||||
|
prompt_len = sampling_metadata.prompt_lens[i]
|
||||||
|
temperatures += [temperature] * (prompt_len - 1)
|
||||||
|
top_ps += [top_p] * (prompt_len - 1)
|
||||||
|
top_ks += [top_k] * (prompt_len - 1)
|
||||||
|
min_ps += [min_p] * (prompt_len - 1)
|
||||||
|
presence_penalties += [0] * (prompt_len - 1)
|
||||||
|
frequency_penalties += [0] * (prompt_len - 1)
|
||||||
|
repetition_penalties += [1] * (prompt_len - 1)
|
||||||
|
prompt_tokens.extend([] for _ in range(prompt_len - 1))
|
||||||
|
output_tokens.extend([] for _ in range(prompt_len - 1))
|
||||||
|
for seq_id in seq_ids:
|
||||||
|
seq_data = sampling_metadata.seq_data[seq_id]
|
||||||
|
prompt_tokens.append(seq_data.prompt_token_ids)
|
||||||
|
output_tokens.append(seq_data.output_token_ids)
|
||||||
|
temperatures += [temperature] * len(seq_ids)
|
||||||
|
top_ps += [top_p] * len(seq_ids)
|
||||||
|
top_ks += [top_k] * len(seq_ids)
|
||||||
|
min_ps += [min_p] * len(seq_ids)
|
||||||
|
presence_penalties += [p] * len(seq_ids)
|
||||||
|
frequency_penalties += [f] * len(seq_ids)
|
||||||
|
repetition_penalties += [r] * len(seq_ids)
|
||||||
|
|
||||||
|
sampling_tensors = SamplingTensors.from_lists(
|
||||||
|
temperatures, top_ps, top_ks, min_ps, presence_penalties,
|
||||||
|
frequency_penalties, repetition_penalties, prompt_tokens,
|
||||||
|
output_tokens, vocab_size, device, dtype)
|
||||||
|
return (sampling_tensors, do_penalties, do_top_p_top_k, do_min_p)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_lists(cls, temperatures: List[float], top_ps: List[float],
|
||||||
|
top_ks: List[int], min_ps: List[float],
|
||||||
|
presence_penalties: List[float],
|
||||||
|
frequency_penalties: List[float],
|
||||||
|
repetition_penalties: List[float],
|
||||||
|
prompt_tokens: List[List[int]],
|
||||||
|
output_tokens: List[List[int]], vocab_size: int,
|
||||||
|
device: torch.device,
|
||||||
|
dtype: torch.dtype) -> "SamplingTensors":
|
||||||
|
# Note that the performance will be very bad without
|
||||||
|
# pinned memory.
|
||||||
|
pin_memory = not in_wsl()
|
||||||
|
prompt_max_len = max(len(tokens) for tokens in prompt_tokens)
|
||||||
|
prompt_padded_tokens = [
|
||||||
|
tokens + [vocab_size] * (prompt_max_len - len(tokens))
|
||||||
|
for tokens in prompt_tokens
|
||||||
|
]
|
||||||
|
output_max_len = max(len(tokens) for tokens in output_tokens)
|
||||||
|
output_padded_tokens = [
|
||||||
|
tokens + [vocab_size] * (output_max_len - len(tokens))
|
||||||
|
for tokens in output_tokens
|
||||||
|
]
|
||||||
|
|
||||||
|
temperatures_t = torch.tensor(
|
||||||
|
temperatures,
|
||||||
|
device="cpu",
|
||||||
|
dtype=dtype,
|
||||||
|
pin_memory=pin_memory,
|
||||||
|
)
|
||||||
|
top_ps_t = torch.tensor(
|
||||||
|
top_ps,
|
||||||
|
device="cpu",
|
||||||
|
dtype=dtype,
|
||||||
|
pin_memory=pin_memory,
|
||||||
|
)
|
||||||
|
min_ps_t = torch.tensor(
|
||||||
|
min_ps,
|
||||||
|
device="cpu",
|
||||||
|
dtype=dtype,
|
||||||
|
pin_memory=pin_memory,
|
||||||
|
)
|
||||||
|
presence_penalties_t = torch.tensor(
|
||||||
|
presence_penalties,
|
||||||
|
device="cpu",
|
||||||
|
dtype=dtype,
|
||||||
|
pin_memory=pin_memory,
|
||||||
|
)
|
||||||
|
frequency_penalties_t = torch.tensor(
|
||||||
|
frequency_penalties,
|
||||||
|
device="cpu",
|
||||||
|
dtype=dtype,
|
||||||
|
pin_memory=pin_memory,
|
||||||
|
)
|
||||||
|
repetition_penalties_t = torch.tensor(
|
||||||
|
repetition_penalties,
|
||||||
|
device="cpu",
|
||||||
|
dtype=dtype,
|
||||||
|
pin_memory=pin_memory,
|
||||||
|
)
|
||||||
|
top_ks_t = torch.tensor(
|
||||||
|
top_ks,
|
||||||
|
device="cpu",
|
||||||
|
dtype=torch.int,
|
||||||
|
pin_memory=pin_memory,
|
||||||
|
)
|
||||||
|
prompt_tensor = torch.tensor(
|
||||||
|
prompt_padded_tokens,
|
||||||
|
device="cpu",
|
||||||
|
dtype=torch.long,
|
||||||
|
pin_memory=pin_memory,
|
||||||
|
)
|
||||||
|
output_tensor = torch.tensor(
|
||||||
|
output_padded_tokens,
|
||||||
|
device="cpu",
|
||||||
|
dtype=torch.long,
|
||||||
|
pin_memory=pin_memory,
|
||||||
|
)
|
||||||
|
# Because the memory is pinned, we can do non-blocking
|
||||||
|
# transfer to device.
|
||||||
|
return cls(
|
||||||
|
temperatures=temperatures_t.to(device=device, non_blocking=True),
|
||||||
|
top_ps=top_ps_t.to(device=device, non_blocking=True),
|
||||||
|
top_ks=top_ks_t.to(device=device, non_blocking=True),
|
||||||
|
min_ps=min_ps_t.to(device=device, non_blocking=True),
|
||||||
|
presence_penalties=presence_penalties_t.to(device=device,
|
||||||
|
non_blocking=True),
|
||||||
|
frequency_penalties=frequency_penalties_t.to(device=device,
|
||||||
|
non_blocking=True),
|
||||||
|
repetition_penalties=repetition_penalties_t.to(device=device,
|
||||||
|
non_blocking=True),
|
||||||
|
prompt_tokens=prompt_tensor.to(device=device, non_blocking=True),
|
||||||
|
output_tokens=output_tensor.to(device=device, non_blocking=True),
|
||||||
|
)
|
||||||
|
|||||||
@@ -125,15 +125,29 @@ def get_quant_config(
|
|||||||
def prepare_hf_model_weights(
|
def prepare_hf_model_weights(
|
||||||
model_name_or_path: str,
|
model_name_or_path: str,
|
||||||
cache_dir: Optional[str] = None,
|
cache_dir: Optional[str] = None,
|
||||||
use_safetensors: bool = False,
|
load_format: str = "auto",
|
||||||
fall_back_to_pt: bool = True,
|
fall_back_to_pt: bool = True,
|
||||||
revision: Optional[str] = None,
|
revision: Optional[str] = None,
|
||||||
) -> Tuple[str, List[str], bool]:
|
) -> Tuple[str, List[str], bool]:
|
||||||
# Download model weights from huggingface.
|
# Download model weights from huggingface.
|
||||||
is_local = os.path.isdir(model_name_or_path)
|
is_local = os.path.isdir(model_name_or_path)
|
||||||
|
use_safetensors = False
|
||||||
# Some quantized models use .pt files for storing the weights.
|
# Some quantized models use .pt files for storing the weights.
|
||||||
allow_patterns = ["*.safetensors"
|
if load_format == "auto":
|
||||||
] if use_safetensors else ["*.bin", "*.pt"]
|
allow_patterns = ["*.safetensors", "*.bin"]
|
||||||
|
elif load_format == "safetensors":
|
||||||
|
use_safetensors = True
|
||||||
|
allow_patterns = ["*.safetensors"]
|
||||||
|
elif load_format == "pt":
|
||||||
|
allow_patterns = ["*.pt"]
|
||||||
|
elif load_format == "npcache":
|
||||||
|
allow_patterns = ["*.bin"]
|
||||||
|
else:
|
||||||
|
raise ValueError(f"Unknown load_format: {load_format}")
|
||||||
|
|
||||||
|
if fall_back_to_pt:
|
||||||
|
allow_patterns += ["*.pt"]
|
||||||
|
|
||||||
if not is_local:
|
if not is_local:
|
||||||
# Use file lock to prevent multiple processes from
|
# Use file lock to prevent multiple processes from
|
||||||
# downloading the same model weights at the same time.
|
# downloading the same model weights at the same time.
|
||||||
@@ -148,6 +162,10 @@ def prepare_hf_model_weights(
|
|||||||
hf_weights_files: List[str] = []
|
hf_weights_files: List[str] = []
|
||||||
for pattern in allow_patterns:
|
for pattern in allow_patterns:
|
||||||
hf_weights_files += glob.glob(os.path.join(hf_folder, pattern))
|
hf_weights_files += glob.glob(os.path.join(hf_folder, pattern))
|
||||||
|
if len(hf_weights_files) > 0:
|
||||||
|
if pattern == "*.safetensors":
|
||||||
|
use_safetensors = True
|
||||||
|
break
|
||||||
if not use_safetensors:
|
if not use_safetensors:
|
||||||
# Exclude files that are not needed for inference.
|
# Exclude files that are not needed for inference.
|
||||||
# https://github.com/huggingface/transformers/blob/v4.34.0/src/transformers/trainer.py#L227-L233
|
# https://github.com/huggingface/transformers/blob/v4.34.0/src/transformers/trainer.py#L227-L233
|
||||||
@@ -163,13 +181,6 @@ def prepare_hf_model_weights(
|
|||||||
if not any(f.endswith(x) for x in blacklist)
|
if not any(f.endswith(x) for x in blacklist)
|
||||||
]
|
]
|
||||||
|
|
||||||
if len(hf_weights_files) == 0 and use_safetensors and fall_back_to_pt:
|
|
||||||
return prepare_hf_model_weights(model_name_or_path,
|
|
||||||
cache_dir=cache_dir,
|
|
||||||
use_safetensors=False,
|
|
||||||
fall_back_to_pt=False,
|
|
||||||
revision=revision)
|
|
||||||
|
|
||||||
if len(hf_weights_files) == 0:
|
if len(hf_weights_files) == 0:
|
||||||
raise RuntimeError(
|
raise RuntimeError(
|
||||||
f"Cannot find any model weights with `{model_name_or_path}`")
|
f"Cannot find any model weights with `{model_name_or_path}`")
|
||||||
@@ -182,30 +193,16 @@ def hf_model_weights_iterator(
|
|||||||
cache_dir: Optional[str] = None,
|
cache_dir: Optional[str] = None,
|
||||||
load_format: str = "auto",
|
load_format: str = "auto",
|
||||||
revision: Optional[str] = None,
|
revision: Optional[str] = None,
|
||||||
|
fall_back_to_pt: Optional[bool] = True,
|
||||||
) -> Iterator[Tuple[str, torch.Tensor]]:
|
) -> Iterator[Tuple[str, torch.Tensor]]:
|
||||||
use_safetensors = False
|
|
||||||
use_np_cache = False
|
|
||||||
fall_back_to_pt = False
|
|
||||||
if load_format == "auto":
|
|
||||||
use_safetensors = True
|
|
||||||
fall_back_to_pt = True
|
|
||||||
elif load_format == "safetensors":
|
|
||||||
use_safetensors = True
|
|
||||||
elif load_format == "pt":
|
|
||||||
pass
|
|
||||||
elif load_format == "npcache":
|
|
||||||
use_np_cache = True
|
|
||||||
else:
|
|
||||||
raise ValueError(f"Unknown load_format: {load_format}")
|
|
||||||
|
|
||||||
hf_folder, hf_weights_files, use_safetensors = prepare_hf_model_weights(
|
hf_folder, hf_weights_files, use_safetensors = prepare_hf_model_weights(
|
||||||
model_name_or_path,
|
model_name_or_path,
|
||||||
cache_dir=cache_dir,
|
cache_dir=cache_dir,
|
||||||
use_safetensors=use_safetensors,
|
load_format=load_format,
|
||||||
fall_back_to_pt=fall_back_to_pt,
|
fall_back_to_pt=fall_back_to_pt,
|
||||||
revision=revision)
|
revision=revision)
|
||||||
|
|
||||||
if use_np_cache:
|
if load_format == "npcache":
|
||||||
# Currently np_cache only support *.bin checkpoints
|
# Currently np_cache only support *.bin checkpoints
|
||||||
assert use_safetensors is False
|
assert use_safetensors is False
|
||||||
|
|
||||||
@@ -287,4 +284,5 @@ def initialize_dummy_weights(
|
|||||||
values between -1e-3 and 1e-3 works well for most models.
|
values between -1e-3 and 1e-3 works well for most models.
|
||||||
"""
|
"""
|
||||||
for param in model.state_dict().values():
|
for param in model.state_dict().values():
|
||||||
|
if torch.is_floating_point(param):
|
||||||
param.data.uniform_(low, high)
|
param.data.uniform_(low, high)
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
from enum import IntEnum
|
from enum import IntEnum
|
||||||
from functools import cached_property
|
from functools import cached_property
|
||||||
from typing import Callable, List, Optional, Union
|
from typing import Callable, List, Optional, Union
|
||||||
|
|
||||||
import torch
|
import torch
|
||||||
|
|
||||||
_SAMPLING_EPS = 1e-5
|
_SAMPLING_EPS = 1e-5
|
||||||
@@ -70,6 +71,8 @@ class SamplingParams:
|
|||||||
stop_token_ids: List of tokens that stop the generation when they are
|
stop_token_ids: List of tokens that stop the generation when they are
|
||||||
generated. The returned output will contain the stop tokens unless
|
generated. The returned output will contain the stop tokens unless
|
||||||
the stop tokens are special tokens.
|
the stop tokens are special tokens.
|
||||||
|
include_stop_str_in_output: Whether to include the stop strings in output
|
||||||
|
text. Defaults to False.
|
||||||
ignore_eos: Whether to ignore the EOS token and continue generating
|
ignore_eos: Whether to ignore the EOS token and continue generating
|
||||||
tokens after the EOS token is generated.
|
tokens after the EOS token is generated.
|
||||||
max_tokens: Maximum number of tokens to generate per output sequence.
|
max_tokens: Maximum number of tokens to generate per output sequence.
|
||||||
@@ -97,12 +100,13 @@ class SamplingParams:
|
|||||||
temperature: float = 1.0,
|
temperature: float = 1.0,
|
||||||
top_p: float = 1.0,
|
top_p: float = 1.0,
|
||||||
top_k: int = -1,
|
top_k: int = -1,
|
||||||
min_p: int = 0.0,
|
min_p: float = 0.0,
|
||||||
use_beam_search: bool = False,
|
use_beam_search: bool = False,
|
||||||
length_penalty: float = 1.0,
|
length_penalty: float = 1.0,
|
||||||
early_stopping: Union[bool, str] = False,
|
early_stopping: Union[bool, str] = False,
|
||||||
stop: Optional[Union[str, List[str]]] = None,
|
stop: Optional[Union[str, List[str]]] = None,
|
||||||
stop_token_ids: Optional[List[int]] = None,
|
stop_token_ids: Optional[List[int]] = None,
|
||||||
|
include_stop_str_in_output: bool = False,
|
||||||
ignore_eos: bool = False,
|
ignore_eos: bool = False,
|
||||||
max_tokens: int = 16,
|
max_tokens: int = 16,
|
||||||
logprobs: Optional[int] = None,
|
logprobs: Optional[int] = None,
|
||||||
@@ -140,6 +144,7 @@ class SamplingParams:
|
|||||||
self.skip_special_tokens = skip_special_tokens
|
self.skip_special_tokens = skip_special_tokens
|
||||||
self.spaces_between_special_tokens = spaces_between_special_tokens
|
self.spaces_between_special_tokens = spaces_between_special_tokens
|
||||||
self.logits_processors = logits_processors
|
self.logits_processors = logits_processors
|
||||||
|
self.include_stop_str_in_output = include_stop_str_in_output
|
||||||
self._verify_args()
|
self._verify_args()
|
||||||
if self.use_beam_search:
|
if self.use_beam_search:
|
||||||
self._verify_beam_search()
|
self._verify_beam_search()
|
||||||
@@ -227,7 +232,8 @@ class SamplingParams:
|
|||||||
return SamplingType.RANDOM
|
return SamplingType.RANDOM
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return (f"SamplingParams(n={self.n}, "
|
return (
|
||||||
|
f"SamplingParams(n={self.n}, "
|
||||||
f"best_of={self.best_of}, "
|
f"best_of={self.best_of}, "
|
||||||
f"presence_penalty={self.presence_penalty}, "
|
f"presence_penalty={self.presence_penalty}, "
|
||||||
f"frequency_penalty={self.frequency_penalty}, "
|
f"frequency_penalty={self.frequency_penalty}, "
|
||||||
@@ -241,6 +247,7 @@ class SamplingParams:
|
|||||||
f"early_stopping={self.early_stopping}, "
|
f"early_stopping={self.early_stopping}, "
|
||||||
f"stop={self.stop}, "
|
f"stop={self.stop}, "
|
||||||
f"stop_token_ids={self.stop_token_ids}, "
|
f"stop_token_ids={self.stop_token_ids}, "
|
||||||
|
f"include_stop_str_in_output={self.include_stop_str_in_output}, "
|
||||||
f"ignore_eos={self.ignore_eos}, "
|
f"ignore_eos={self.ignore_eos}, "
|
||||||
f"max_tokens={self.max_tokens}, "
|
f"max_tokens={self.max_tokens}, "
|
||||||
f"logprobs={self.logprobs}, "
|
f"logprobs={self.logprobs}, "
|
||||||
|
|||||||
@@ -8,9 +8,6 @@ from vllm.transformers_utils.tokenizers import *
|
|||||||
|
|
||||||
logger = init_logger(__name__)
|
logger = init_logger(__name__)
|
||||||
|
|
||||||
# A fast LLaMA tokenizer with the pre-processed `tokenizer.json` file.
|
|
||||||
_FAST_LLAMA_TOKENIZER = "hf-internal-testing/llama-tokenizer"
|
|
||||||
|
|
||||||
|
|
||||||
def get_tokenizer(
|
def get_tokenizer(
|
||||||
tokenizer_name: str,
|
tokenizer_name: str,
|
||||||
@@ -27,13 +24,6 @@ def get_tokenizer(
|
|||||||
"Cannot use the fast tokenizer in slow tokenizer mode.")
|
"Cannot use the fast tokenizer in slow tokenizer mode.")
|
||||||
kwargs["use_fast"] = False
|
kwargs["use_fast"] = False
|
||||||
|
|
||||||
if ("llama" in tokenizer_name.lower() and kwargs.get("use_fast", True)
|
|
||||||
and tokenizer_name != _FAST_LLAMA_TOKENIZER):
|
|
||||||
logger.info(
|
|
||||||
"For some LLaMA V1 models, initializing the fast tokenizer may "
|
|
||||||
"take a long time. To reduce the initialization time, consider "
|
|
||||||
f"using '{_FAST_LLAMA_TOKENIZER}' instead of the original "
|
|
||||||
"tokenizer.")
|
|
||||||
try:
|
try:
|
||||||
tokenizer = AutoTokenizer.from_pretrained(
|
tokenizer = AutoTokenizer.from_pretrained(
|
||||||
tokenizer_name,
|
tokenizer_name,
|
||||||
@@ -41,13 +31,6 @@ def get_tokenizer(
|
|||||||
trust_remote_code=trust_remote_code,
|
trust_remote_code=trust_remote_code,
|
||||||
tokenizer_revision=tokenizer_revision,
|
tokenizer_revision=tokenizer_revision,
|
||||||
**kwargs)
|
**kwargs)
|
||||||
except TypeError as e:
|
|
||||||
# The LLaMA tokenizer causes a protobuf error in some environments.
|
|
||||||
err_msg = (
|
|
||||||
"Failed to load the tokenizer. If you are using a LLaMA V1 model "
|
|
||||||
f"consider using '{_FAST_LLAMA_TOKENIZER}' instead of the "
|
|
||||||
"original tokenizer.")
|
|
||||||
raise RuntimeError(err_msg) from e
|
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
# If the error pertains to the tokenizer class not existing or not
|
# If the error pertains to the tokenizer class not existing or not
|
||||||
# currently being imported, suggest using the --trust-remote-code flag.
|
# currently being imported, suggest using the --trust-remote-code flag.
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
import enum
|
import enum
|
||||||
|
import os
|
||||||
|
import socket
|
||||||
import uuid
|
import uuid
|
||||||
from platform import uname
|
from platform import uname
|
||||||
|
from typing import List
|
||||||
|
|
||||||
import psutil
|
import psutil
|
||||||
import torch
|
import torch
|
||||||
@@ -40,11 +43,6 @@ def get_max_shared_memory_bytes(gpu: int = 0) -> int:
|
|||||||
return int(max_shared_mem)
|
return int(max_shared_mem)
|
||||||
|
|
||||||
|
|
||||||
def get_gpu_memory(gpu: int = 0) -> int:
|
|
||||||
"""Returns the total memory of the GPU in bytes."""
|
|
||||||
return torch.cuda.get_device_properties(gpu).total_memory
|
|
||||||
|
|
||||||
|
|
||||||
def get_cpu_memory() -> int:
|
def get_cpu_memory() -> int:
|
||||||
"""Returns the total CPU memory of the node in bytes."""
|
"""Returns the total CPU memory of the node in bytes."""
|
||||||
return psutil.virtual_memory().total
|
return psutil.virtual_memory().total
|
||||||
@@ -57,3 +55,17 @@ def random_uuid() -> str:
|
|||||||
def in_wsl() -> bool:
|
def in_wsl() -> bool:
|
||||||
# Reference: https://github.com/microsoft/WSL/issues/4071
|
# Reference: https://github.com/microsoft/WSL/issues/4071
|
||||||
return "microsoft" in " ".join(uname()).lower()
|
return "microsoft" in " ".join(uname()).lower()
|
||||||
|
|
||||||
|
|
||||||
|
def get_ip() -> str:
|
||||||
|
return socket.gethostbyname(socket.gethostname())
|
||||||
|
|
||||||
|
|
||||||
|
def get_open_port() -> int:
|
||||||
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
||||||
|
s.bind(("", 0))
|
||||||
|
return s.getsockname()[1]
|
||||||
|
|
||||||
|
|
||||||
|
def set_cuda_visible_devices(device_ids: List[int]) -> None:
|
||||||
|
os.environ["CUDA_VISIBLE_DEVICES"] = ",".join(map(str, device_ids))
|
||||||
|
|||||||
@@ -1,16 +1,26 @@
|
|||||||
from typing import Dict, List, Optional, Tuple
|
import time
|
||||||
|
from typing import Dict, List, Optional, Tuple, Union
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
import torch
|
import torch
|
||||||
|
import torch.nn as nn
|
||||||
|
|
||||||
from vllm.config import ModelConfig, ParallelConfig, SchedulerConfig
|
from vllm.config import ModelConfig, ParallelConfig, SchedulerConfig
|
||||||
from vllm.logger import init_logger
|
from vllm.logger import init_logger
|
||||||
from vllm.model_executor import get_model, InputMetadata, SamplingMetadata
|
from vllm.model_executor import get_model, InputMetadata, SamplingMetadata
|
||||||
|
from vllm.model_executor.parallel_utils.communication_op import (
|
||||||
|
broadcast, broadcast_object_list)
|
||||||
from vllm.sampling_params import SamplingParams, SamplingType
|
from vllm.sampling_params import SamplingParams, SamplingType
|
||||||
from vllm.sequence import SamplerOutput, SequenceData, SequenceGroupMetadata
|
from vllm.sequence import SamplerOutput, SequenceData, SequenceGroupMetadata
|
||||||
|
from vllm.utils import in_wsl
|
||||||
|
|
||||||
logger = init_logger(__name__)
|
logger = init_logger(__name__)
|
||||||
|
|
||||||
|
KVCache = Tuple[torch.Tensor, torch.Tensor]
|
||||||
_PAD_SLOT_ID = -1
|
_PAD_SLOT_ID = -1
|
||||||
|
# Capture graphs for batch size 1, 2, 4, 8, 16, 24, 32, 40, ..., 256.
|
||||||
|
# NOTE: _get_graph_batch_size needs to be updated if this list is changed.
|
||||||
|
_BATCH_SIZES_TO_CAPTURE = [1, 2, 4] + [8 * i for i in range(1, 33)]
|
||||||
|
|
||||||
|
|
||||||
class ModelRunner:
|
class ModelRunner:
|
||||||
@@ -20,10 +30,12 @@ class ModelRunner:
|
|||||||
model_config: ModelConfig,
|
model_config: ModelConfig,
|
||||||
parallel_config: ParallelConfig,
|
parallel_config: ParallelConfig,
|
||||||
scheduler_config: SchedulerConfig,
|
scheduler_config: SchedulerConfig,
|
||||||
|
is_driver_worker: bool = False,
|
||||||
):
|
):
|
||||||
self.model_config = model_config
|
self.model_config = model_config
|
||||||
self.parallel_config = parallel_config
|
self.parallel_config = parallel_config
|
||||||
self.scheduler_config = scheduler_config
|
self.scheduler_config = scheduler_config
|
||||||
|
self.is_driver_worker = is_driver_worker
|
||||||
|
|
||||||
# model_config can be None in tests/samplers/test_sampler.py.
|
# model_config can be None in tests/samplers/test_sampler.py.
|
||||||
# FIXME(woosuk): This is a hack to make the tests work. Refactor this.
|
# FIXME(woosuk): This is a hack to make the tests work. Refactor this.
|
||||||
@@ -32,16 +44,37 @@ class ModelRunner:
|
|||||||
self.model = None
|
self.model = None
|
||||||
self.block_size = None # Set after initial profiling.
|
self.block_size = None # Set after initial profiling.
|
||||||
|
|
||||||
|
self.graph_runners: Dict[int, CUDAGraphRunner] = {}
|
||||||
|
self.graph_memory_pool = None # Set during graph capture.
|
||||||
|
|
||||||
|
self.max_context_len_to_capture = (
|
||||||
|
self.model_config.max_context_len_to_capture
|
||||||
|
if self.model_config is not None else 0)
|
||||||
|
# When using CUDA graph, the input block tables must be padded to
|
||||||
|
# max_context_len_to_capture. However, creating the block table in
|
||||||
|
# Python can be expensive. To optimize this, we cache the block table
|
||||||
|
# in numpy and only copy the actual input content at every iteration.
|
||||||
|
# The shape of the cached block table will be
|
||||||
|
# (max batch size to capture, max context len to capture / block size).
|
||||||
|
self.graph_block_tables = None # Set after initial profiling.
|
||||||
|
# cache in_wsl result
|
||||||
|
self.in_wsl = in_wsl()
|
||||||
|
|
||||||
def load_model(self) -> None:
|
def load_model(self) -> None:
|
||||||
self.model = get_model(self.model_config)
|
self.model = get_model(self.model_config)
|
||||||
|
|
||||||
def set_block_size(self, block_size: int) -> None:
|
def set_block_size(self, block_size: int) -> None:
|
||||||
self.block_size = block_size
|
self.block_size = block_size
|
||||||
|
|
||||||
|
max_num_blocks = (self.max_context_len_to_capture + block_size -
|
||||||
|
1) // block_size
|
||||||
|
self.graph_block_tables = np.zeros(
|
||||||
|
(max(_BATCH_SIZES_TO_CAPTURE), max_num_blocks), dtype=np.int32)
|
||||||
|
|
||||||
def _prepare_prompt(
|
def _prepare_prompt(
|
||||||
self,
|
self,
|
||||||
seq_group_metadata_list: List[SequenceGroupMetadata],
|
seq_group_metadata_list: List[SequenceGroupMetadata],
|
||||||
) -> Tuple[torch.Tensor, torch.Tensor, InputMetadata]:
|
) -> Tuple[torch.Tensor, torch.Tensor, InputMetadata, List[int]]:
|
||||||
assert len(seq_group_metadata_list) > 0
|
assert len(seq_group_metadata_list) > 0
|
||||||
input_tokens: List[List[int]] = []
|
input_tokens: List[List[int]] = []
|
||||||
input_positions: List[List[int]] = []
|
input_positions: List[List[int]] = []
|
||||||
@@ -106,13 +139,14 @@ class ModelRunner:
|
|||||||
dtype=torch.long)
|
dtype=torch.long)
|
||||||
|
|
||||||
input_metadata = InputMetadata(
|
input_metadata = InputMetadata(
|
||||||
prompt_lens=prompt_lens,
|
is_prompt=True,
|
||||||
slot_mapping=slot_mapping,
|
slot_mapping=slot_mapping,
|
||||||
max_context_len=None,
|
max_context_len=None,
|
||||||
context_lens=None,
|
context_lens=None,
|
||||||
block_tables=None,
|
block_tables=None,
|
||||||
|
use_cuda_graph=False,
|
||||||
)
|
)
|
||||||
return input_tokens, input_positions, input_metadata
|
return input_tokens, input_positions, input_metadata, prompt_lens
|
||||||
|
|
||||||
def _prepare_decode(
|
def _prepare_decode(
|
||||||
self,
|
self,
|
||||||
@@ -134,14 +168,14 @@ class ModelRunner:
|
|||||||
generation_token = seq_data.get_last_token_id()
|
generation_token = seq_data.get_last_token_id()
|
||||||
input_tokens.append([generation_token])
|
input_tokens.append([generation_token])
|
||||||
|
|
||||||
context_len = seq_data.get_len()
|
seq_len = seq_data.get_len()
|
||||||
if self.sliding_window is not None:
|
position = seq_len - 1
|
||||||
context_len = min(context_len, self.sliding_window)
|
|
||||||
context_lens.append(context_len)
|
|
||||||
|
|
||||||
position = context_len - 1
|
|
||||||
input_positions.append([position])
|
input_positions.append([position])
|
||||||
|
|
||||||
|
context_len = seq_len if self.sliding_window is None else min(
|
||||||
|
seq_len, self.sliding_window)
|
||||||
|
context_lens.append(context_len)
|
||||||
|
|
||||||
block_table = seq_group_metadata.block_tables[seq_id]
|
block_table = seq_group_metadata.block_tables[seq_id]
|
||||||
block_number = block_table[position // self.block_size]
|
block_number = block_table[position // self.block_size]
|
||||||
block_offset = position % self.block_size
|
block_offset = position % self.block_size
|
||||||
@@ -154,34 +188,68 @@ class ModelRunner:
|
|||||||
block_table = block_table[-sliding_window_blocks:]
|
block_table = block_table[-sliding_window_blocks:]
|
||||||
block_tables.append(block_table)
|
block_tables.append(block_table)
|
||||||
|
|
||||||
|
batch_size = len(input_tokens)
|
||||||
|
max_context_len = max(context_lens)
|
||||||
|
use_captured_graph = (
|
||||||
|
not self.model_config.enforce_eager
|
||||||
|
and batch_size <= _BATCH_SIZES_TO_CAPTURE[-1]
|
||||||
|
and max_context_len <= self.max_context_len_to_capture)
|
||||||
|
if use_captured_graph:
|
||||||
|
# Pad the input tokens, positions, and slot mapping to match the
|
||||||
|
# batch size of the captured graph.
|
||||||
|
graph_batch_size = _get_graph_batch_size(batch_size)
|
||||||
|
assert graph_batch_size >= batch_size
|
||||||
|
for _ in range(graph_batch_size - batch_size):
|
||||||
|
input_tokens.append([])
|
||||||
|
input_positions.append([])
|
||||||
|
slot_mapping.append([])
|
||||||
|
context_lens.append(1)
|
||||||
|
block_tables.append([])
|
||||||
|
batch_size = graph_batch_size
|
||||||
|
|
||||||
input_tokens = _make_tensor_with_pad(input_tokens,
|
input_tokens = _make_tensor_with_pad(input_tokens,
|
||||||
max_len=1,
|
max_len=1,
|
||||||
pad=0,
|
pad=0,
|
||||||
dtype=torch.long)
|
dtype=torch.long,
|
||||||
|
device="cuda")
|
||||||
input_positions = _make_tensor_with_pad(input_positions,
|
input_positions = _make_tensor_with_pad(input_positions,
|
||||||
max_len=1,
|
max_len=1,
|
||||||
pad=0,
|
pad=0,
|
||||||
dtype=torch.long)
|
dtype=torch.long,
|
||||||
|
device="cuda")
|
||||||
slot_mapping = _make_tensor_with_pad(slot_mapping,
|
slot_mapping = _make_tensor_with_pad(slot_mapping,
|
||||||
max_len=1,
|
max_len=1,
|
||||||
pad=_PAD_SLOT_ID,
|
pad=_PAD_SLOT_ID,
|
||||||
dtype=torch.long)
|
dtype=torch.long,
|
||||||
max_context_len = max(context_lens)
|
device="cuda")
|
||||||
context_lens = torch.tensor(context_lens,
|
context_lens = torch.tensor(context_lens,
|
||||||
dtype=torch.int,
|
dtype=torch.int,
|
||||||
device="cuda")
|
device="cuda")
|
||||||
max_block_table_len = max([len(t) for t in block_tables])
|
|
||||||
block_tables = _make_tensor_with_pad(block_tables,
|
if use_captured_graph:
|
||||||
max_len=max_block_table_len,
|
# The shape of graph_block_tables is
|
||||||
|
# [max batch size, max context len // block size].
|
||||||
|
input_block_tables = self.graph_block_tables[:batch_size]
|
||||||
|
for i, block_table in enumerate(block_tables):
|
||||||
|
if block_table:
|
||||||
|
input_block_tables[i, :len(block_table)] = block_table
|
||||||
|
block_tables = torch.tensor(input_block_tables, device="cuda")
|
||||||
|
else:
|
||||||
|
block_tables = _make_tensor_with_pad(
|
||||||
|
block_tables,
|
||||||
|
max_len=max_context_len,
|
||||||
pad=0,
|
pad=0,
|
||||||
dtype=torch.int)
|
dtype=torch.int,
|
||||||
|
device="cuda",
|
||||||
|
)
|
||||||
|
|
||||||
input_metadata = InputMetadata(
|
input_metadata = InputMetadata(
|
||||||
prompt_lens=[],
|
is_prompt=False,
|
||||||
slot_mapping=slot_mapping,
|
slot_mapping=slot_mapping,
|
||||||
max_context_len=max_context_len,
|
max_context_len=max_context_len,
|
||||||
context_lens=context_lens,
|
context_lens=context_lens,
|
||||||
block_tables=block_tables,
|
block_tables=block_tables,
|
||||||
|
use_cuda_graph=use_captured_graph,
|
||||||
)
|
)
|
||||||
return input_tokens, input_positions, input_metadata
|
return input_tokens, input_positions, input_metadata
|
||||||
|
|
||||||
@@ -234,11 +302,11 @@ class ModelRunner:
|
|||||||
categorized_sample_indices_start_idx + num_seqs))
|
categorized_sample_indices_start_idx + num_seqs))
|
||||||
categorized_sample_indices_start_idx += num_seqs
|
categorized_sample_indices_start_idx += num_seqs
|
||||||
|
|
||||||
selected_token_indices = torch.tensor(selected_token_indices,
|
selected_token_indices = _async_h2d(selected_token_indices,
|
||||||
dtype=torch.long,
|
dtype=torch.long,
|
||||||
device="cuda")
|
pin_memory=not self.in_wsl)
|
||||||
categorized_sample_indices = {
|
categorized_sample_indices = {
|
||||||
t: torch.tensor(seq_ids, dtype=torch.int, device="cuda")
|
t: _async_h2d(seq_ids, dtype=torch.int, pin_memory=not self.in_wsl)
|
||||||
for t, seq_ids in categorized_sample_indices.items()
|
for t, seq_ids in categorized_sample_indices.items()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -255,33 +323,138 @@ class ModelRunner:
|
|||||||
)
|
)
|
||||||
return sampling_metadata
|
return sampling_metadata
|
||||||
|
|
||||||
|
def prepare_input_tensors(
|
||||||
|
self,
|
||||||
|
seq_group_metadata_list: Optional[List[SequenceGroupMetadata]],
|
||||||
|
) -> Tuple[torch.Tensor, torch.Tensor, InputMetadata, SamplingMetadata]:
|
||||||
|
if self.is_driver_worker:
|
||||||
|
# NOTE: We assume that all sequences in the group are all prompts or
|
||||||
|
# all decodes.
|
||||||
|
is_prompt = seq_group_metadata_list[0].is_prompt
|
||||||
|
# Prepare input tensors.
|
||||||
|
if is_prompt:
|
||||||
|
(input_tokens, input_positions, input_metadata,
|
||||||
|
prompt_lens) = self._prepare_prompt(seq_group_metadata_list)
|
||||||
|
else:
|
||||||
|
(input_tokens, input_positions, input_metadata
|
||||||
|
) = self._prepare_decode(seq_group_metadata_list)
|
||||||
|
prompt_lens = []
|
||||||
|
sampling_metadata = self._prepare_sample(seq_group_metadata_list,
|
||||||
|
prompt_lens)
|
||||||
|
|
||||||
|
def get_size_or_none(x: Optional[torch.Tensor]):
|
||||||
|
return x.size() if x is not None else None
|
||||||
|
|
||||||
|
# Broadcast the input data. For input tensors, we first broadcast
|
||||||
|
# its shape and then broadcast the tensor to avoid high
|
||||||
|
# serialization cost.
|
||||||
|
py_data = {
|
||||||
|
"input_tokens_size":
|
||||||
|
input_tokens.size(),
|
||||||
|
"input_positions_size":
|
||||||
|
input_positions.size(),
|
||||||
|
"is_prompt":
|
||||||
|
input_metadata.is_prompt,
|
||||||
|
"slot_mapping_size":
|
||||||
|
get_size_or_none(input_metadata.slot_mapping),
|
||||||
|
"max_context_len":
|
||||||
|
input_metadata.max_context_len,
|
||||||
|
"context_lens_size":
|
||||||
|
get_size_or_none(input_metadata.context_lens),
|
||||||
|
"block_tables_size":
|
||||||
|
get_size_or_none(input_metadata.block_tables),
|
||||||
|
"use_cuda_graph":
|
||||||
|
input_metadata.use_cuda_graph,
|
||||||
|
"selected_token_indices_size":
|
||||||
|
sampling_metadata.selected_token_indices.size(),
|
||||||
|
}
|
||||||
|
broadcast_object_list([py_data], src=0)
|
||||||
|
# TODO(zhuohan): Combine the broadcasts or set async_op=True.
|
||||||
|
broadcast(input_tokens, src=0)
|
||||||
|
broadcast(input_positions, src=0)
|
||||||
|
if input_metadata.slot_mapping is not None:
|
||||||
|
broadcast(input_metadata.slot_mapping, src=0)
|
||||||
|
if input_metadata.context_lens is not None:
|
||||||
|
broadcast(input_metadata.context_lens, src=0)
|
||||||
|
if input_metadata.block_tables is not None:
|
||||||
|
broadcast(input_metadata.block_tables, src=0)
|
||||||
|
broadcast(sampling_metadata.selected_token_indices, src=0)
|
||||||
|
else:
|
||||||
|
receving_list = [None]
|
||||||
|
broadcast_object_list(receving_list, src=0)
|
||||||
|
py_data = receving_list[0]
|
||||||
|
input_tokens = torch.empty(*py_data["input_tokens_size"],
|
||||||
|
dtype=torch.long,
|
||||||
|
device="cuda")
|
||||||
|
broadcast(input_tokens, src=0)
|
||||||
|
input_positions = torch.empty(*py_data["input_positions_size"],
|
||||||
|
dtype=torch.long,
|
||||||
|
device="cuda")
|
||||||
|
broadcast(input_positions, src=0)
|
||||||
|
if py_data["slot_mapping_size"] is not None:
|
||||||
|
slot_mapping = torch.empty(*py_data["slot_mapping_size"],
|
||||||
|
dtype=torch.long,
|
||||||
|
device="cuda")
|
||||||
|
broadcast(slot_mapping, src=0)
|
||||||
|
else:
|
||||||
|
slot_mapping = None
|
||||||
|
if py_data["context_lens_size"] is not None:
|
||||||
|
context_lens = torch.empty(*py_data["context_lens_size"],
|
||||||
|
dtype=torch.int,
|
||||||
|
device="cuda")
|
||||||
|
broadcast(context_lens, src=0)
|
||||||
|
else:
|
||||||
|
context_lens = None
|
||||||
|
if py_data["block_tables_size"] is not None:
|
||||||
|
block_tables = torch.empty(*py_data["block_tables_size"],
|
||||||
|
dtype=torch.int,
|
||||||
|
device="cuda")
|
||||||
|
broadcast(block_tables, src=0)
|
||||||
|
else:
|
||||||
|
block_tables = None
|
||||||
|
selected_token_indices = torch.empty(
|
||||||
|
*py_data["selected_token_indices_size"],
|
||||||
|
dtype=torch.long,
|
||||||
|
device="cuda")
|
||||||
|
broadcast(selected_token_indices, src=0)
|
||||||
|
input_metadata = InputMetadata(
|
||||||
|
is_prompt=py_data["is_prompt"],
|
||||||
|
slot_mapping=slot_mapping,
|
||||||
|
max_context_len=py_data["max_context_len"],
|
||||||
|
context_lens=context_lens,
|
||||||
|
block_tables=block_tables,
|
||||||
|
use_cuda_graph=py_data["use_cuda_graph"],
|
||||||
|
)
|
||||||
|
sampling_metadata = SamplingMetadata(
|
||||||
|
seq_groups=None,
|
||||||
|
seq_data=None,
|
||||||
|
prompt_lens=None,
|
||||||
|
selected_token_indices=selected_token_indices,
|
||||||
|
categorized_sample_indices=None,
|
||||||
|
perform_sampling=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
return input_tokens, input_positions, input_metadata, sampling_metadata
|
||||||
|
|
||||||
@torch.inference_mode()
|
@torch.inference_mode()
|
||||||
def execute_model(
|
def execute_model(
|
||||||
self,
|
self,
|
||||||
seq_group_metadata_list: List[SequenceGroupMetadata],
|
seq_group_metadata_list: Optional[List[SequenceGroupMetadata]],
|
||||||
kv_caches: List[Tuple[torch.Tensor, torch.Tensor]],
|
kv_caches: List[Tuple[torch.Tensor, torch.Tensor]],
|
||||||
cache_events: Optional[List[torch.cuda.Event]] = None,
|
) -> Optional[SamplerOutput]:
|
||||||
) -> SamplerOutput:
|
input_tokens, input_positions, input_metadata, sampling_metadata = (
|
||||||
# NOTE: We assume that all sequences in the group are all prompts or
|
self.prepare_input_tensors(seq_group_metadata_list))
|
||||||
# all decodes.
|
|
||||||
# Prepare input tensors.
|
|
||||||
is_prompt = seq_group_metadata_list[0].is_prompt
|
|
||||||
if is_prompt:
|
|
||||||
inputs = self._prepare_prompt(seq_group_metadata_list)
|
|
||||||
input_tokens, input_positions, input_metadata = inputs
|
|
||||||
else:
|
|
||||||
inputs = self._prepare_decode(seq_group_metadata_list)
|
|
||||||
input_tokens, input_positions, input_metadata = inputs
|
|
||||||
sampling_metadata = self._prepare_sample(seq_group_metadata_list,
|
|
||||||
input_metadata.prompt_lens)
|
|
||||||
|
|
||||||
# Execute the model.
|
# Execute the model.
|
||||||
hidden_states = self.model(
|
if input_metadata.use_cuda_graph:
|
||||||
|
graph_batch_size = input_tokens.shape[0]
|
||||||
|
model_executable = self.graph_runners[graph_batch_size]
|
||||||
|
else:
|
||||||
|
model_executable = self.model
|
||||||
|
hidden_states = model_executable(
|
||||||
input_ids=input_tokens,
|
input_ids=input_tokens,
|
||||||
positions=input_positions,
|
positions=input_positions,
|
||||||
kv_caches=kv_caches,
|
kv_caches=kv_caches,
|
||||||
input_metadata=input_metadata,
|
input_metadata=input_metadata,
|
||||||
cache_events=cache_events,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Sample the next token.
|
# Sample the next token.
|
||||||
@@ -319,8 +492,141 @@ class ModelRunner:
|
|||||||
num_layers = self.model_config.get_num_layers(self.parallel_config)
|
num_layers = self.model_config.get_num_layers(self.parallel_config)
|
||||||
kv_caches = [(None, None)] * num_layers
|
kv_caches = [(None, None)] * num_layers
|
||||||
self.execute_model(seqs, kv_caches)
|
self.execute_model(seqs, kv_caches)
|
||||||
|
torch.cuda.synchronize()
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@torch.inference_mode()
|
||||||
|
def capture_model(self, kv_caches: List[KVCache]) -> None:
|
||||||
|
assert not self.model_config.enforce_eager
|
||||||
|
logger.info("Capturing the model for CUDA graphs. This may lead to "
|
||||||
|
"unexpected consequences if the model is not static. To "
|
||||||
|
"run the model in eager mode, set 'enforce_eager=True' or "
|
||||||
|
"use '--enforce-eager' in the CLI.")
|
||||||
|
logger.info("CUDA graphs can take additional 1~3 GiB memory per GPU. "
|
||||||
|
"If you are running out of memory, consider decreasing "
|
||||||
|
"`gpu_memory_utilization` or enforcing eager mode.")
|
||||||
|
start_time = time.perf_counter()
|
||||||
|
|
||||||
|
# Prepare dummy inputs. These will be reused for all batch sizes.
|
||||||
|
max_batch_size = max(_BATCH_SIZES_TO_CAPTURE)
|
||||||
|
input_tokens = torch.zeros(max_batch_size, 1, dtype=torch.long).cuda()
|
||||||
|
input_positions = torch.zeros(max_batch_size, 1,
|
||||||
|
dtype=torch.long).cuda()
|
||||||
|
slot_mapping = torch.empty(max_batch_size, 1, dtype=torch.long).cuda()
|
||||||
|
slot_mapping.fill_(_PAD_SLOT_ID)
|
||||||
|
context_lens = torch.ones(max_batch_size, dtype=torch.int32).cuda()
|
||||||
|
block_tables = torch.from_numpy(self.graph_block_tables).cuda()
|
||||||
|
|
||||||
|
# NOTE: Capturing the largest batch size first may help reduce the
|
||||||
|
# memory usage of CUDA graph.
|
||||||
|
for batch_size in reversed(_BATCH_SIZES_TO_CAPTURE):
|
||||||
|
# Create dummy input_metadata.
|
||||||
|
input_metadata = InputMetadata(
|
||||||
|
is_prompt=False,
|
||||||
|
slot_mapping=slot_mapping[:batch_size],
|
||||||
|
max_context_len=self.max_context_len_to_capture,
|
||||||
|
context_lens=context_lens[:batch_size],
|
||||||
|
block_tables=block_tables[:batch_size],
|
||||||
|
use_cuda_graph=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
graph_runner = CUDAGraphRunner(self.model)
|
||||||
|
graph_runner.capture(
|
||||||
|
input_tokens[:batch_size],
|
||||||
|
input_positions[:batch_size],
|
||||||
|
kv_caches,
|
||||||
|
input_metadata,
|
||||||
|
memory_pool=self.graph_memory_pool,
|
||||||
|
)
|
||||||
|
self.graph_memory_pool = graph_runner.graph.pool()
|
||||||
|
self.graph_runners[batch_size] = graph_runner
|
||||||
|
|
||||||
|
end_time = time.perf_counter()
|
||||||
|
elapsed_time = end_time - start_time
|
||||||
|
# This usually takes < 10 seconds.
|
||||||
|
logger.info(f"Graph capturing finished in {elapsed_time:.0f} secs.")
|
||||||
|
|
||||||
|
|
||||||
|
class CUDAGraphRunner:
|
||||||
|
|
||||||
|
def __init__(self, model: nn.Module):
|
||||||
|
self.model = model
|
||||||
|
self.graph = None
|
||||||
|
self.input_buffers: Dict[str, torch.Tensor] = {}
|
||||||
|
self.output_buffers: Dict[str, torch.Tensor] = {}
|
||||||
|
|
||||||
|
def capture(
|
||||||
|
self,
|
||||||
|
input_ids: torch.Tensor,
|
||||||
|
positions: torch.Tensor,
|
||||||
|
kv_caches: List[KVCache],
|
||||||
|
input_metadata: InputMetadata,
|
||||||
|
memory_pool,
|
||||||
|
) -> None:
|
||||||
|
assert self.graph is None
|
||||||
|
# Run the model once without capturing the graph.
|
||||||
|
# This is to make sure that the captured graph does not include the
|
||||||
|
# kernel launches for initial benchmarking (e.g., Triton autotune).
|
||||||
|
self.model(
|
||||||
|
input_ids,
|
||||||
|
positions,
|
||||||
|
kv_caches,
|
||||||
|
input_metadata,
|
||||||
|
)
|
||||||
|
torch.cuda.synchronize()
|
||||||
|
|
||||||
|
# Capture the graph.
|
||||||
|
self.graph = torch.cuda.CUDAGraph()
|
||||||
|
with torch.cuda.graph(self.graph, pool=memory_pool):
|
||||||
|
hidden_states = self.model(
|
||||||
|
input_ids,
|
||||||
|
positions,
|
||||||
|
kv_caches,
|
||||||
|
input_metadata,
|
||||||
|
)
|
||||||
|
torch.cuda.synchronize()
|
||||||
|
|
||||||
|
# Save the input and output buffers.
|
||||||
|
self.input_buffers = {
|
||||||
|
"input_ids": input_ids,
|
||||||
|
"positions": positions,
|
||||||
|
"kv_caches": kv_caches,
|
||||||
|
"slot_mapping": input_metadata.slot_mapping,
|
||||||
|
"context_lens": input_metadata.context_lens,
|
||||||
|
"block_tables": input_metadata.block_tables,
|
||||||
|
}
|
||||||
|
self.output_buffers = {"hidden_states": hidden_states}
|
||||||
|
return
|
||||||
|
|
||||||
|
def forward(
|
||||||
|
self,
|
||||||
|
input_ids: torch.Tensor,
|
||||||
|
positions: torch.Tensor,
|
||||||
|
kv_caches: List[Tuple[torch.Tensor, torch.Tensor]],
|
||||||
|
input_metadata: InputMetadata,
|
||||||
|
) -> torch.Tensor:
|
||||||
|
# KV caches are fixed tensors, so we don't need to copy them.
|
||||||
|
del kv_caches
|
||||||
|
|
||||||
|
# Copy the input tensors to the input buffers.
|
||||||
|
self.input_buffers["input_ids"].copy_(input_ids, non_blocking=True)
|
||||||
|
self.input_buffers["positions"].copy_(positions, non_blocking=True)
|
||||||
|
self.input_buffers["slot_mapping"].copy_(input_metadata.slot_mapping,
|
||||||
|
non_blocking=True)
|
||||||
|
self.input_buffers["context_lens"].copy_(input_metadata.context_lens,
|
||||||
|
non_blocking=True)
|
||||||
|
self.input_buffers["block_tables"].copy_(input_metadata.block_tables,
|
||||||
|
non_blocking=True)
|
||||||
|
|
||||||
|
# Run the graph.
|
||||||
|
self.graph.replay()
|
||||||
|
|
||||||
|
# Return the output tensor.
|
||||||
|
return self.output_buffers["hidden_states"]
|
||||||
|
|
||||||
|
def __call__(self, *args, **kwargs):
|
||||||
|
return self.forward(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
def _pad_to_max(x: List[int], max_len: int, pad: int) -> List[int]:
|
def _pad_to_max(x: List[int], max_len: int, pad: int) -> List[int]:
|
||||||
assert len(x) <= max_len
|
assert len(x) <= max_len
|
||||||
@@ -332,6 +638,25 @@ def _make_tensor_with_pad(
|
|||||||
max_len: int,
|
max_len: int,
|
||||||
pad: int,
|
pad: int,
|
||||||
dtype: torch.dtype,
|
dtype: torch.dtype,
|
||||||
|
device: Union[str, torch.device] = "cuda",
|
||||||
|
pin_memory: bool = False,
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
padded_x = [_pad_to_max(x_i, max_len, pad) for x_i in x]
|
padded_x = [_pad_to_max(x_i, max_len, pad) for x_i in x]
|
||||||
return torch.tensor(padded_x, dtype=dtype, device="cuda")
|
return torch.tensor(padded_x,
|
||||||
|
dtype=dtype,
|
||||||
|
device=device,
|
||||||
|
pin_memory=pin_memory and str(device) == "cpu")
|
||||||
|
|
||||||
|
|
||||||
|
def _get_graph_batch_size(batch_size: int) -> int:
|
||||||
|
if batch_size <= 2:
|
||||||
|
return batch_size
|
||||||
|
elif batch_size <= 4:
|
||||||
|
return 4
|
||||||
|
else:
|
||||||
|
return (batch_size + 7) // 8 * 8
|
||||||
|
|
||||||
|
|
||||||
|
def _async_h2d(data: list, dtype, pin_memory):
|
||||||
|
t = torch.tensor(data, dtype=dtype, pin_memory=pin_memory)
|
||||||
|
return t.to(device="cuda", non_blocking=True)
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user