Skip to content

Commit

Permalink
Using an enum for flash backens (paged/flashdecoding/flashinfer) (#2385)
Browse files Browse the repository at this point in the history
* Using an enum for flash backens (paged/flashdecoding/flashinfer)

* Early exit on server too.

* Clippy.

* Fix clippy and fmt.
  • Loading branch information
Narsil authored Aug 9, 2024
1 parent 6e127dc commit 7a48a84
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 38 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ server/exllama_kernels/exllama_kernels/exllama_ext_hip.cpp

data/
load_tests/*.json
server/fbgemmm
16 changes: 11 additions & 5 deletions backends/v3/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use nohash_hasher::IntMap;
use std::sync::Arc;
use text_generation_router::infer::{Backend, GeneratedText, InferError, InferStreamResponse};
use text_generation_router::validation::ValidGenerateRequest;
use text_generation_router::{FinishReason, PrefillToken, Token};
use text_generation_router::{Attention, FinishReason, PrefillToken, Token};
use tokio::sync::mpsc::error::SendError;
use tokio::sync::{mpsc, Notify};
use tokio::time::Instant;
Expand Down Expand Up @@ -35,12 +35,18 @@ impl BackendV3 {
window_size: Option<u32>,
speculate: u32,
) -> Self {
let flashdecoding = if let Ok(flashdecoding) = std::env::var("FLASH_DECODING") {
matches!(flashdecoding.to_lowercase().as_str(), "1" | "true")
let attention = if let Ok(attention) = std::env::var("ATTENTION") {
attention
.parse()
.unwrap_or_else(|_| panic!("Invalid attention was specified :`{attention}`"))
} else {
false
Attention::Paged
};
let block_size = if attention == Attention::FlashDecoding {
256
} else {
16
};
let block_size = if flashdecoding { 256 } else { 16 };

let queue = Queue::new(
requires_padding,
Expand Down
2 changes: 1 addition & 1 deletion docs/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -2080,4 +2080,4 @@
"description": "Hugging Face Text Generation Inference API"
}
]
}
}
4 changes: 2 additions & 2 deletions docs/source/conceptual/quantization.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ We recommend using the official quantization scripts for creating your quants:
2. [GPTQ/ Marlin](https://github.com/AutoGPTQ/AutoGPTQ/blob/main/examples/quantization/basic_usage.py)
3. [EXL2](https://github.com/turboderp/exllamav2/blob/master/doc/convert.md)

For on-the-fly quantization you simply need to pass one of the supported quantization types and TGI takes care of the rest.
For on-the-fly quantization you simply need to pass one of the supported quantization types and TGI takes care of the rest.

## Quantization with bitsandbytes, EETQ & fp8

Expand Down Expand Up @@ -69,4 +69,4 @@ text-generation-launcher --model-id /data/falcon-40b-gptq/ --sharded true --num-
You can learn more about the quantization options by running `text-generation-server quantize --help`.

If you wish to do more with GPTQ models (e.g. train an adapter on top), you can read about transformers GPTQ integration [here](https://huggingface.co/blog/gptq-integration).
You can learn more about GPTQ from the [paper](https://arxiv.org/pdf/2210.17323.pdf).
You can learn more about GPTQ from the [paper](https://arxiv.org/pdf/2210.17323.pdf).
2 changes: 1 addition & 1 deletion launcher/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1461,7 +1461,7 @@ fn main() -> Result<(), LauncherError> {

if config.model_type == Some("gemma2".to_string()) {
tracing::info!("Forcing flash decoding because of softcap usage");
std::env::set_var("FLASH_DECODING", "1");
std::env::set_var("ATTENTION", "flashdecoding");
}
let config: Config = config.into();

Expand Down
18 changes: 12 additions & 6 deletions router/src/infer/v2/scheduler.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/// Batching and inference logic
use crate::infer::v2::queue::{Entry, Queue};
use crate::infer::{
Backend, GenerateStreamResponse, GeneratedText, InferError, InferStreamResponse,
Attention, Backend, GenerateStreamResponse, GeneratedText, InferError, InferStreamResponse,
};
use crate::validation::ValidGenerateRequest;
use crate::{FinishReason, PrefillToken, Token};
use crate::{Attention, FinishReason, PrefillToken, Token};
use nohash_hasher::IntMap;
use std::sync::{
atomic::{AtomicBool, Ordering},
Expand Down Expand Up @@ -40,12 +40,18 @@ impl BackendV2 {
generation_health: Arc<AtomicBool>,
) -> Self {
// Infer shared state
let flashdecoding = if let Ok(flashdecoding) = std::env::var("FLASH_DECODING") {
matches!(flashdecoding.to_lowercase().as_str(), "1" | "true")
let attention = if let Ok(attention) = std::env::var("ATTENTION") {
attention
.parse()
.expect(&format!("Invalid attention was specified :`{attention}`"))
} else {
false
Attention::Paged
};
let block_size = if attention == Attention::FlashDecoding {
256
} else {
16
};
let block_size = if flashdecoding { 256 } else { 16 };
let queue = Queue::new(requires_padding, block_size, window_size, speculate);
let batching_task_notifier = Arc::new(Notify::new());

Expand Down
29 changes: 29 additions & 0 deletions router/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,35 @@ use tracing::warn;
use utoipa::ToSchema;
use validation::Validation;

#[derive(PartialEq)]
pub enum Attention {
Paged,
FlashDecoding,
FlashInfer,
}

#[derive(Debug)]
pub struct ParseError;

impl std::fmt::Display for ParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Cannot parse attention value")
}
}
impl std::error::Error for ParseError {}

impl std::str::FromStr for Attention {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"paged" => Ok(Attention::Paged),
"flashdecoding" => Ok(Attention::FlashDecoding),
"flashinfer" => Ok(Attention::FlashInfer),
_ => Err(ParseError),
}
}
}

#[derive(Clone, Deserialize, ToSchema)]
pub(crate) struct VertexInstance {
#[schema(example = "What is Deep Learning?")]
Expand Down
4 changes: 2 additions & 2 deletions server/text_generation_server/layers/attention/common.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from dataclasses import dataclass
from text_generation_server.models.globals import FLASH_DECODING, FLASH_INFER
from text_generation_server.models.globals import ATTENTION
import torch
from typing import Optional


if FLASH_DECODING or FLASH_INFER:
if ATTENTION in {"flashinfer", "flashdecoding"}:

@dataclass
class Seqlen:
Expand Down
11 changes: 5 additions & 6 deletions server/text_generation_server/layers/attention/cuda.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import torch
from text_generation_server.utils.import_utils import SYSTEM
from text_generation_server.models.globals import (
FLASH_DECODING,
ATTENTION,
BLOCK_SIZE,
FLASH_INFER,
)
from text_generation_server.layers.attention import Seqlen
from typing import Optional
Expand All @@ -27,7 +26,7 @@ def reshape_and_cache(
value_cache: torch.Tensor,
slots: torch.Tensor,
):
if FLASH_DECODING or FLASH_INFER:
if ATTENTION in {"flashdecoding", "flashinfer"}:
shape = key_cache.shape
key_cache.view(-1, shape[-2], shape[-1])[slots] = key
value_cache.view(-1, shape[-2], shape[-1])[slots] = value
Expand Down Expand Up @@ -76,7 +75,7 @@ def paged_attention(
# V1 to avoid the overhead of reduction. Also, if the number of
# sequences or heads is large, we use V1 since there is enough work
# to parallelize.
if FLASH_INFER:
if ATTENTION == "flashinfer":
from text_generation_server.layers.attention.flash_infer import decode_state

return decode_state.get().forward(
Expand All @@ -85,7 +84,7 @@ def paged_attention(
logits_soft_cap=softcap,
sm_scale=softmax_scale,
)
elif FLASH_DECODING:
elif ATTENTION == "flashdecoding":
max_q = 1
max_k = max_s
import flash_attn_2_cuda
Expand Down Expand Up @@ -219,7 +218,7 @@ def paged_attention(

SUPPORTS_WINDOWING = V2

if FLASH_INFER:
if ATTENTION == "flashinfer":

def attention(
q,
Expand Down
4 changes: 2 additions & 2 deletions server/text_generation_server/layers/attention/rocm.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import torch
from text_generation_server.utils.import_utils import SYSTEM
from text_generation_server.models.globals import FLASH_DECODING
from text_generation_server.models.globals import ATTENTION
from text_generation_server.layers.attention import Seqlen
from text_generation_server.utils.log import log_master
from loguru import logger
Expand All @@ -28,7 +28,7 @@ def reshape_and_cache(
value_cache: torch.Tensor,
slots: torch.Tensor,
):
if FLASH_DECODING:
if ATTENTION == "flashdecoding":
shape = key_cache.shape
key_cache.view(-1, shape[-2], shape[-1])[slots] = key
value_cache.view(-1, shape[-2], shape[-1])[slots] = value
Expand Down
11 changes: 5 additions & 6 deletions server/text_generation_server/models/flash_causal_lm.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@
from text_generation_server.pb import generate_pb2
from text_generation_server.models.globals import (
MEM_POOL,
FLASH_DECODING,
FLASH_INFER,
ATTENTION,
BLOCK_SIZE,
CUDA_GRAPHS,
get_adapter_to_index,
Expand Down Expand Up @@ -938,7 +937,7 @@ def __init__(
self.cuda_graphs = {}
self.kv_cache = []

if FLASH_INFER:
if ATTENTION == "flashinfer":
from text_generation_server.layers.attention.flash_infer import (
create_prefill_state,
create_decode_state,
Expand Down Expand Up @@ -990,7 +989,7 @@ def init_kv_cache(
else:
x = BLOCK_SIZE // element_size

if FLASH_DECODING or FLASH_INFER:
if ATTENTION in {"flashdecoding", "flashinfer"}:
self.kv_cache = [
(
torch.empty(
Expand Down Expand Up @@ -1062,7 +1061,7 @@ def cuda_graph_warmup(self, bs: int, max_s: int, max_bt: int):
graph = torch.cuda.CUDAGraph()
self.cuda_graphs[bs]["graph"] = graph

if FLASH_INFER:
if ATTENTION == "flashinfer":
from text_generation_server.layers.attention.flash_infer import (
create_decode_state_cuda_graphs,
)
Expand Down Expand Up @@ -1766,7 +1765,7 @@ def _forward_context(
input_lengths: torch.Tensor,
state: Optional[Any] = None,
) -> ContextManager:
if not FLASH_INFER:
if ATTENTION != "flashinfer":
return nullcontext()

from text_generation_server.layers.attention.flash_infer import (
Expand Down
14 changes: 7 additions & 7 deletions server/text_generation_server/models/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@

from text_generation_server.utils.log import log_master

FLASH_INFER = os.getenv("FLASH_INFER") in {"1", "true", "True"}
if FLASH_INFER:
log_master(logger.info, "Using FLASH_INFER")
ATTENTION = os.getenv("ATTENTION", "paged")
_expected = {"paged", "flashdecoding", "flashinfer"}
assert (
ATTENTION in _expected
), f"Attention is not valid {ATTENTION}, expected {_expected}"
log_master(logger.info, f"Using Attention = {ATTENTION}")

MEM_POOL = torch.cuda.graph_pool_handle() if torch.cuda.is_available() else None
# This is overridden by the cli
FLASH_DECODING = os.getenv("FLASH_DECODING") in {"1", "true", "True"}
BLOCK_SIZE: int = 256 if FLASH_DECODING else 16
if FLASH_DECODING:
log_master(logger.info, "Using FLASH_DECODING")
BLOCK_SIZE: int = 256 if ATTENTION == "flashdecoding" else 16


cuda_graphs = os.getenv("CUDA_GRAPHS")
Expand Down

0 comments on commit 7a48a84

Please sign in to comment.