2023-04-26 12:23:54 -06:00
|
|
|
mod health;
|
2023-02-02 06:59:27 -07:00
|
|
|
/// Text Generation Inference Webserver
|
2023-02-02 07:02:04 -07:00
|
|
|
mod infer;
|
2023-02-02 06:59:27 -07:00
|
|
|
mod queue;
|
2022-10-17 10:27:33 -06:00
|
|
|
pub mod server;
|
2022-10-18 07:19:03 -06:00
|
|
|
mod validation;
|
2022-10-17 06:59:00 -06:00
|
|
|
|
2023-01-31 09:04:00 -07:00
|
|
|
use infer::Infer;
|
2023-02-02 06:59:27 -07:00
|
|
|
use queue::{Entry, Queue};
|
2022-10-18 07:19:03 -06:00
|
|
|
use serde::{Deserialize, Serialize};
|
2023-02-03 04:43:37 -07:00
|
|
|
use utoipa::ToSchema;
|
2022-10-17 06:59:00 -06:00
|
|
|
use validation::Validation;
|
2022-10-18 07:19:03 -06:00
|
|
|
|
2023-04-18 08:16:06 -06:00
|
|
|
/// Hub type
|
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
2023-04-21 07:36:29 -06:00
|
|
|
pub struct HubModelInfo {
|
2023-04-18 08:16:06 -06:00
|
|
|
#[serde(rename(deserialize = "id"))]
|
|
|
|
pub model_id: String,
|
|
|
|
pub sha: Option<String>,
|
|
|
|
pub pipeline_tag: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Serialize, ToSchema)]
|
|
|
|
pub struct Info {
|
2023-04-25 05:11:18 -06:00
|
|
|
/// Model info
|
2023-04-18 08:16:06 -06:00
|
|
|
#[schema(example = "bigscience/blomm-560m")]
|
|
|
|
pub model_id: String,
|
|
|
|
#[schema(nullable = true, example = "e985a63cdc139290c5f700ff1929f0b5942cced2")]
|
|
|
|
pub model_sha: Option<String>,
|
2023-04-21 07:36:29 -06:00
|
|
|
#[schema(example = "torch.float16")]
|
|
|
|
pub model_dtype: String,
|
|
|
|
#[schema(example = "cuda")]
|
|
|
|
pub model_device_type: String,
|
2023-04-18 08:16:06 -06:00
|
|
|
#[schema(nullable = true, example = "text-generation")]
|
|
|
|
pub model_pipeline_tag: Option<String>,
|
2023-04-25 05:11:18 -06:00
|
|
|
/// Router Parameters
|
|
|
|
#[schema(example = "128")]
|
|
|
|
pub max_concurrent_requests: usize,
|
|
|
|
#[schema(example = "2")]
|
|
|
|
pub max_best_of: usize,
|
|
|
|
#[schema(example = "4")]
|
|
|
|
pub max_stop_sequences: usize,
|
|
|
|
#[schema(example = "1024")]
|
|
|
|
pub max_input_length: usize,
|
|
|
|
#[schema(example = "2048")]
|
|
|
|
pub max_total_tokens: usize,
|
|
|
|
#[schema(example = "1.2")]
|
|
|
|
pub waiting_served_ratio: f32,
|
|
|
|
#[schema(example = "32000")]
|
|
|
|
pub max_batch_total_tokens: u32,
|
|
|
|
#[schema(example = "20")]
|
|
|
|
pub max_waiting_tokens: usize,
|
|
|
|
#[schema(example = "2")]
|
|
|
|
pub validation_workers: usize,
|
|
|
|
/// Router Info
|
2023-04-18 08:16:06 -06:00
|
|
|
#[schema(example = "0.5.0")]
|
|
|
|
pub version: &'static str,
|
|
|
|
#[schema(nullable = true, example = "null")]
|
|
|
|
pub sha: Option<&'static str>,
|
2023-05-02 07:43:19 -06:00
|
|
|
#[schema(nullable = true, example = "null")]
|
|
|
|
pub docker_label: Option<&'static str>,
|
2023-04-18 08:16:06 -06:00
|
|
|
}
|
|
|
|
|
2023-02-03 04:43:37 -07:00
|
|
|
#[derive(Clone, Debug, Deserialize, ToSchema)]
|
2022-10-18 07:19:03 -06:00
|
|
|
pub(crate) struct GenerateParameters {
|
2023-03-09 07:30:54 -07:00
|
|
|
#[serde(default)]
|
|
|
|
#[schema(exclusive_minimum = 0, nullable = true, default = "null", example = 1)]
|
|
|
|
pub best_of: Option<usize>,
|
2023-02-03 04:43:37 -07:00
|
|
|
#[serde(default)]
|
|
|
|
#[schema(
|
|
|
|
exclusive_minimum = 0.0,
|
|
|
|
nullable = true,
|
|
|
|
default = "null",
|
|
|
|
example = 0.5
|
|
|
|
)]
|
|
|
|
pub temperature: Option<f32>,
|
|
|
|
#[serde(default)]
|
|
|
|
#[schema(
|
|
|
|
exclusive_minimum = 0.0,
|
|
|
|
nullable = true,
|
|
|
|
default = "null",
|
|
|
|
example = 1.03
|
|
|
|
)]
|
|
|
|
pub repetition_penalty: Option<f32>,
|
|
|
|
#[serde(default)]
|
|
|
|
#[schema(exclusive_minimum = 0, nullable = true, default = "null", example = 10)]
|
|
|
|
pub top_k: Option<i32>,
|
|
|
|
#[serde(default)]
|
|
|
|
#[schema(
|
|
|
|
exclusive_minimum = 0.0,
|
|
|
|
maximum = 1.0,
|
|
|
|
nullable = true,
|
|
|
|
default = "null",
|
|
|
|
example = 0.95
|
|
|
|
)]
|
|
|
|
pub top_p: Option<f32>,
|
2023-02-28 02:19:32 -07:00
|
|
|
#[serde(default)]
|
2023-03-09 03:33:57 -07:00
|
|
|
#[schema(
|
|
|
|
exclusive_minimum = 0.0,
|
|
|
|
maximum = 1.0,
|
|
|
|
nullable = true,
|
|
|
|
default = "null",
|
|
|
|
example = 0.95
|
|
|
|
)]
|
|
|
|
pub typical_p: Option<f32>,
|
|
|
|
#[serde(default)]
|
2023-02-03 04:43:37 -07:00
|
|
|
#[schema(default = "false", example = true)]
|
2022-10-18 07:19:03 -06:00
|
|
|
pub do_sample: bool,
|
|
|
|
#[serde(default = "default_max_new_tokens")]
|
2023-02-03 04:43:37 -07:00
|
|
|
#[schema(exclusive_minimum = 0, exclusive_maximum = 512, default = "20")]
|
2022-10-18 07:19:03 -06:00
|
|
|
pub max_new_tokens: u32,
|
2022-12-15 09:03:56 -07:00
|
|
|
#[serde(default)]
|
2023-03-09 07:30:54 -07:00
|
|
|
#[schema(nullable = true, default = "null", example = false)]
|
2023-02-28 02:19:32 -07:00
|
|
|
pub return_full_text: Option<bool>,
|
|
|
|
#[serde(default)]
|
2023-02-27 06:56:58 -07:00
|
|
|
#[schema(inline, max_items = 4, example = json ! (["photographer"]))]
|
2022-12-12 10:25:22 -07:00
|
|
|
pub stop: Vec<String>,
|
2022-12-15 09:03:56 -07:00
|
|
|
#[serde(default)]
|
2023-03-09 07:30:54 -07:00
|
|
|
#[schema(nullable = true, default = "null", example = "null")]
|
2023-03-09 05:10:30 -07:00
|
|
|
pub truncate: Option<usize>,
|
|
|
|
#[serde(default)]
|
2023-03-02 04:30:41 -07:00
|
|
|
#[schema(default = "false", example = true)]
|
|
|
|
pub watermark: bool,
|
|
|
|
#[serde(default)]
|
2023-02-03 04:43:37 -07:00
|
|
|
#[schema(default = "true")]
|
2022-12-15 09:03:56 -07:00
|
|
|
pub details: bool,
|
2023-01-30 07:36:16 -07:00
|
|
|
#[serde(default)]
|
2023-03-09 07:30:54 -07:00
|
|
|
#[schema(
|
|
|
|
exclusive_minimum = 0,
|
|
|
|
nullable = true,
|
|
|
|
default = "null",
|
|
|
|
example = "null"
|
|
|
|
)]
|
2023-01-30 07:36:16 -07:00
|
|
|
pub seed: Option<u64>,
|
2022-10-18 07:19:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn default_max_new_tokens() -> u32 {
|
|
|
|
20
|
|
|
|
}
|
|
|
|
|
|
|
|
fn default_parameters() -> GenerateParameters {
|
|
|
|
GenerateParameters {
|
2023-03-09 07:30:54 -07:00
|
|
|
best_of: None,
|
2023-02-03 04:43:37 -07:00
|
|
|
temperature: None,
|
|
|
|
repetition_penalty: None,
|
|
|
|
top_k: None,
|
|
|
|
top_p: None,
|
2023-03-09 03:33:57 -07:00
|
|
|
typical_p: None,
|
2023-02-28 02:19:32 -07:00
|
|
|
do_sample: false,
|
2022-10-18 07:19:03 -06:00
|
|
|
max_new_tokens: default_max_new_tokens(),
|
2023-02-28 02:19:32 -07:00
|
|
|
return_full_text: None,
|
2023-03-02 04:30:41 -07:00
|
|
|
stop: Vec::new(),
|
2023-03-09 05:10:30 -07:00
|
|
|
truncate: None,
|
2023-03-02 04:30:41 -07:00
|
|
|
watermark: false,
|
2022-12-15 09:03:56 -07:00
|
|
|
details: false,
|
2023-01-30 07:36:16 -07:00
|
|
|
seed: None,
|
2022-10-18 07:19:03 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-03 04:43:37 -07:00
|
|
|
#[derive(Clone, Debug, Deserialize, ToSchema)]
|
2022-10-18 07:19:03 -06:00
|
|
|
pub(crate) struct GenerateRequest {
|
2023-02-03 04:43:37 -07:00
|
|
|
#[schema(example = "My name is Olivier and I")]
|
2022-10-18 07:19:03 -06:00
|
|
|
pub inputs: String,
|
|
|
|
#[serde(default = "default_parameters")]
|
|
|
|
pub parameters: GenerateParameters,
|
|
|
|
}
|
|
|
|
|
2023-02-27 06:56:58 -07:00
|
|
|
#[derive(Clone, Debug, Deserialize, ToSchema)]
|
|
|
|
pub(crate) struct CompatGenerateRequest {
|
|
|
|
#[schema(example = "My name is Olivier and I")]
|
|
|
|
pub inputs: String,
|
|
|
|
#[serde(default = "default_parameters")]
|
|
|
|
pub parameters: GenerateParameters,
|
|
|
|
#[serde(default)]
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub stream: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<CompatGenerateRequest> for GenerateRequest {
|
|
|
|
fn from(req: CompatGenerateRequest) -> Self {
|
|
|
|
Self {
|
|
|
|
inputs: req.inputs,
|
|
|
|
parameters: req.parameters,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-24 07:55:57 -07:00
|
|
|
#[derive(Debug, Serialize, ToSchema)]
|
|
|
|
pub struct PrefillToken {
|
|
|
|
#[schema(example = 0)]
|
|
|
|
id: u32,
|
|
|
|
#[schema(example = "test")]
|
|
|
|
text: String,
|
2023-02-27 06:56:58 -07:00
|
|
|
#[schema(nullable = true, example = - 0.34)]
|
2023-02-24 07:55:57 -07:00
|
|
|
logprob: f32,
|
|
|
|
}
|
|
|
|
|
2023-02-03 04:43:37 -07:00
|
|
|
#[derive(Debug, Serialize, ToSchema)]
|
|
|
|
pub struct Token {
|
|
|
|
#[schema(example = 0)]
|
|
|
|
id: u32,
|
|
|
|
#[schema(example = "test")]
|
|
|
|
text: String,
|
2023-02-27 06:56:58 -07:00
|
|
|
#[schema(nullable = true, example = - 0.34)]
|
2023-02-03 04:43:37 -07:00
|
|
|
logprob: f32,
|
2023-02-24 07:55:57 -07:00
|
|
|
#[schema(example = "false")]
|
|
|
|
special: bool,
|
2023-02-03 04:43:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, ToSchema)]
|
|
|
|
#[serde(rename_all(serialize = "snake_case"))]
|
|
|
|
pub(crate) enum FinishReason {
|
|
|
|
#[schema(rename = "length")]
|
|
|
|
Length,
|
|
|
|
#[serde(rename = "eos_token")]
|
|
|
|
#[schema(rename = "eos_token")]
|
|
|
|
EndOfSequenceToken,
|
|
|
|
#[schema(rename = "stop_sequence")]
|
|
|
|
StopSequence,
|
|
|
|
}
|
2023-01-31 09:04:00 -07:00
|
|
|
|
2023-03-09 07:30:54 -07:00
|
|
|
#[derive(Serialize, ToSchema)]
|
|
|
|
pub(crate) struct BestOfSequence {
|
|
|
|
#[schema(example = "test")]
|
|
|
|
pub generated_text: String,
|
|
|
|
#[schema(example = "length")]
|
|
|
|
pub finish_reason: FinishReason,
|
|
|
|
#[schema(example = 1)]
|
|
|
|
pub generated_tokens: u32,
|
|
|
|
#[schema(nullable = true, example = 42)]
|
|
|
|
pub seed: Option<u64>,
|
|
|
|
pub prefill: Vec<PrefillToken>,
|
|
|
|
pub tokens: Vec<Token>,
|
|
|
|
}
|
|
|
|
|
2023-02-03 04:43:37 -07:00
|
|
|
#[derive(Serialize, ToSchema)]
|
2022-12-15 09:03:56 -07:00
|
|
|
pub(crate) struct Details {
|
2023-02-03 04:43:37 -07:00
|
|
|
#[schema(example = "length")]
|
|
|
|
pub finish_reason: FinishReason,
|
|
|
|
#[schema(example = 1)]
|
2022-12-15 09:03:56 -07:00
|
|
|
pub generated_tokens: u32,
|
2023-03-09 07:30:54 -07:00
|
|
|
#[schema(nullable = true, example = 42)]
|
2023-01-30 07:36:16 -07:00
|
|
|
pub seed: Option<u64>,
|
2023-03-07 10:52:22 -07:00
|
|
|
pub prefill: Vec<PrefillToken>,
|
|
|
|
pub tokens: Vec<Token>,
|
2023-03-09 07:30:54 -07:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub best_of_sequences: Option<Vec<BestOfSequence>>,
|
2022-12-15 09:03:56 -07:00
|
|
|
}
|
|
|
|
|
2023-02-03 04:43:37 -07:00
|
|
|
#[derive(Serialize, ToSchema)]
|
2023-01-31 09:04:00 -07:00
|
|
|
pub(crate) struct GenerateResponse {
|
2023-02-03 04:43:37 -07:00
|
|
|
#[schema(example = "test")]
|
2022-10-18 07:19:03 -06:00
|
|
|
pub generated_text: String,
|
2022-12-15 09:03:56 -07:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub details: Option<Details>,
|
2022-10-18 07:19:03 -06:00
|
|
|
}
|
2022-10-27 06:25:29 -06:00
|
|
|
|
2023-02-03 04:43:37 -07:00
|
|
|
#[derive(Serialize, ToSchema)]
|
|
|
|
pub(crate) struct StreamDetails {
|
|
|
|
#[schema(example = "length")]
|
|
|
|
pub finish_reason: FinishReason,
|
|
|
|
#[schema(example = 1)]
|
|
|
|
pub generated_tokens: u32,
|
2023-03-09 07:30:54 -07:00
|
|
|
#[schema(nullable = true, example = 42)]
|
2023-02-03 04:43:37 -07:00
|
|
|
pub seed: Option<u64>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, ToSchema)]
|
2023-01-31 09:04:00 -07:00
|
|
|
pub(crate) struct StreamResponse {
|
|
|
|
pub token: Token,
|
2023-02-03 04:43:37 -07:00
|
|
|
#[schema(nullable = true, default = "null", example = "test")]
|
2023-01-31 09:04:00 -07:00
|
|
|
pub generated_text: Option<String>,
|
2023-02-03 04:43:37 -07:00
|
|
|
#[schema(nullable = true, default = "null")]
|
|
|
|
pub details: Option<StreamDetails>,
|
2023-01-31 09:04:00 -07:00
|
|
|
}
|
|
|
|
|
2023-02-03 04:43:37 -07:00
|
|
|
#[derive(Serialize, ToSchema)]
|
2022-10-27 06:25:29 -06:00
|
|
|
pub(crate) struct ErrorResponse {
|
|
|
|
pub error: String,
|
2023-03-07 10:52:22 -07:00
|
|
|
pub error_type: String,
|
2022-10-27 06:25:29 -06:00
|
|
|
}
|
2023-04-26 08:14:40 -06:00
|
|
|
|
|
|
|
#[cfg(test)]
|
2023-04-26 12:23:54 -06:00
|
|
|
mod tests {
|
2023-04-26 08:14:40 -06:00
|
|
|
use std::io::Write;
|
|
|
|
use tokenizers::Tokenizer;
|
|
|
|
|
2023-04-26 12:23:54 -06:00
|
|
|
pub(crate) async fn get_tokenizer() -> Tokenizer {
|
|
|
|
if !std::path::Path::new("tokenizer.json").exists() {
|
|
|
|
let content = reqwest::get("https://huggingface.co/gpt2/raw/main/tokenizer.json")
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.bytes()
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let mut file = std::fs::File::create("tokenizer.json").unwrap();
|
2023-04-26 08:14:40 -06:00
|
|
|
file.write_all(&content).unwrap();
|
|
|
|
}
|
|
|
|
Tokenizer::from_file("tokenizer.json").unwrap()
|
|
|
|
}
|
|
|
|
}
|