validation.rs 2.74 KB
Newer Older
Olivier Dehaene's avatar
Olivier Dehaene committed
1
use crate::server::GenerateRequest;
Olivier Dehaene's avatar
Olivier Dehaene committed
2
3
use axum::http::StatusCode;
use thiserror::Error;
Olivier Dehaene's avatar
Olivier Dehaene committed
4
5
6
use tokenizers::tokenizer::Tokenizer;
use tokio::sync::{mpsc, oneshot};

Olivier Dehaene's avatar
Olivier Dehaene committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#[derive(Error, Debug)]
pub enum ValidationError {
    #[error("Temperature must be strictly positive")]
    Temperature,
    #[error("Top p must be <= 0.0 or > 1.0")]
    TopP,
    #[error("Top k must be strictly positive")]
    TopK,
    #[error("Max New Tokens must be < 512")]
    MaxNewTokens,
    #[error("Inputs must have less than 512 tokens. Given: {0}")]
    InputLength(usize),
}

impl From<ValidationError> for (StatusCode, String) {
    fn from(err: ValidationError) -> Self {
        (StatusCode::BAD_REQUEST, err.to_string())
    }
}
Olivier Dehaene's avatar
Olivier Dehaene committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

type ValidationRequest = (
    GenerateRequest,
    oneshot::Sender<Result<(usize, GenerateRequest), ValidationError>>,
);

#[derive(Debug, Clone)]
pub(crate) struct Validation {
    sender: mpsc::Sender<ValidationRequest>,
}

impl Validation {
    pub(crate) fn new(tokenizer: Tokenizer) -> Self {
        let (validation_sender, validation_receiver) = mpsc::channel(128);

        tokio::spawn(validation_task(tokenizer, validation_receiver));

        Self {
            sender: validation_sender,
        }
    }

    pub(crate) async fn validate(
        &self,
        request: GenerateRequest,
    ) -> Result<(usize, GenerateRequest), ValidationError> {
        let (sender, receiver) = oneshot::channel();
        self.sender.send((request, sender)).await.unwrap();
        receiver.await.unwrap()
    }
}

async fn validation_task(tokenizer: Tokenizer, mut receiver: mpsc::Receiver<ValidationRequest>) {
    while let Some((request, response_tx)) = receiver.recv().await {
        if request.parameters.temperature < 0.0 {
Olivier Dehaene's avatar
Olivier Dehaene committed
61
62
63
            response_tx
                .send(Err(ValidationError::Temperature))
                .unwrap_or(());
Olivier Dehaene's avatar
Olivier Dehaene committed
64
65
66
            continue;
        }
        if request.parameters.top_p <= 0.0 || request.parameters.top_p > 1.0 {
Olivier Dehaene's avatar
Olivier Dehaene committed
67
68
69
70
71
            response_tx.send(Err(ValidationError::TopP)).unwrap_or(());
            continue;
        }
        if request.parameters.top_k < 0 {
            response_tx.send(Err(ValidationError::TopK)).unwrap_or(());
Olivier Dehaene's avatar
Olivier Dehaene committed
72
73
74
            continue;
        }
        if request.parameters.max_new_tokens > 512 {
Olivier Dehaene's avatar
Olivier Dehaene committed
75
76
77
            response_tx
                .send(Err(ValidationError::MaxNewTokens))
                .unwrap_or(());
Olivier Dehaene's avatar
Olivier Dehaene committed
78
79
80
81
82
83
84
            continue;
        }

        let inputs = tokenizer.encode(request.inputs.clone(), false).unwrap();
        let input_length = inputs.len();

        if input_length > 512 {
Olivier Dehaene's avatar
Olivier Dehaene committed
85
86
87
            response_tx
                .send(Err(ValidationError::InputLength(input_length)))
                .unwrap_or(());
Olivier Dehaene's avatar
Olivier Dehaene committed
88
89
90
91
92
93
            continue;
        }

        response_tx.send(Ok((input_length, request))).unwrap_or(());
    }
}