opt.rs 5.54 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
// 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.

use std::fmt;

18
19
use crate::ENDPOINT_SCHEME;

20
#[derive(PartialEq)]
21
22
23
24
25
26
pub enum Input {
    /// Run an OpenAI compatible HTTP server
    Http,

    /// Read prompt from stdin
    Text,
27
28
29

    /// Pull requests from a namespace/component/endpoint path.
    Endpoint(String),
30
31
32
33
34

    /// Start the engine but don't provide any way to talk to it.
    /// For multi-node sglang, where the engine connects directly
    /// to the co-ordinator via torch distributed / nccl.
    None,
35
36
37
38
39
40
41
42
43
}

impl TryFrom<&str> for Input {
    type Error = anyhow::Error;

    fn try_from(s: &str) -> anyhow::Result<Self> {
        match s {
            "http" => Ok(Input::Http),
            "text" => Ok(Input::Text),
44
            "none" => Ok(Input::None),
45
46
47
48
            endpoint_path if endpoint_path.starts_with(ENDPOINT_SCHEME) => {
                let path = endpoint_path.strip_prefix(ENDPOINT_SCHEME).unwrap();
                Ok(Input::Endpoint(path.to_string()))
            }
49
50
51
52
53
54
55
56
57
58
            e => Err(anyhow::anyhow!("Invalid in= option '{e}'")),
        }
    }
}

impl fmt::Display for Input {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let s = match self {
            Input::Http => "http",
            Input::Text => "text",
59
            Input::Endpoint(path) => path,
60
            Input::None => "none",
61
62
63
64
65
66
67
68
        };
        write!(f, "{s}")
    }
}

pub enum Output {
    /// Accept un-preprocessed requests, echo the prompt back as the response
    EchoFull,
69

70
71
72
    /// Accept preprocessed requests, echo the tokens back as the response
    EchoCore,

73
74
75
    /// Publish requests to a namespace/component/endpoint path.
    Endpoint(String),

76
77
78
    #[cfg(feature = "mistralrs")]
    /// Run inference on a model in a GGUF file using mistralrs w/ candle
    MistralRs,
79
80
81
82

    #[cfg(feature = "sglang")]
    /// Run inference using sglang
    SgLang,
83
84
85
86

    #[cfg(feature = "llamacpp")]
    /// Run inference using llama.cpp
    LlamaCpp,
Graham King's avatar
Graham King committed
87
88
89
90

    #[cfg(feature = "vllm")]
    /// Run inference using vllm's engine
    Vllm,
Graham King's avatar
Graham King committed
91
92
93
94

    #[cfg(feature = "trtllm")]
    /// Run inference using trtllm
    TrtLLM,
95

96
97
    /// Run inference using a user supplied python file that accepts and returns
    /// strings. It does it's own pre-processing.
98
99
    #[cfg(feature = "python")]
    PythonStr(String),
100
101
102
103
104

    /// Run inference using a user supplied python file that accepts and returns
    /// tokens. We do the pre-processing.
    #[cfg(feature = "python")]
    PythonTok(String),
105
106
107
108
109
110
111
}

impl TryFrom<&str> for Output {
    type Error = anyhow::Error;

    fn try_from(s: &str) -> anyhow::Result<Self> {
        match s {
112
113
114
            #[cfg(feature = "mistralrs")]
            "mistralrs" => Ok(Output::MistralRs),

115
116
117
            #[cfg(feature = "sglang")]
            "sglang" => Ok(Output::SgLang),

118
119
120
            #[cfg(feature = "llamacpp")]
            "llamacpp" | "llama_cpp" => Ok(Output::LlamaCpp),

Graham King's avatar
Graham King committed
121
122
123
            #[cfg(feature = "vllm")]
            "vllm" => Ok(Output::Vllm),

Graham King's avatar
Graham King committed
124
125
126
            #[cfg(feature = "trtllm")]
            "trtllm" => Ok(Output::TrtLLM),

127
            "echo_full" => Ok(Output::EchoFull),
128
            "echo_core" => Ok(Output::EchoCore),
129
130
131
132
133
134

            endpoint_path if endpoint_path.starts_with(ENDPOINT_SCHEME) => {
                let path = endpoint_path.strip_prefix(ENDPOINT_SCHEME).unwrap();
                Ok(Output::Endpoint(path.to_string()))
            }

135
136
137
138
139
140
141
142
            #[cfg(feature = "python")]
            python_str_gen if python_str_gen.starts_with(crate::PYTHON_STR_SCHEME) => {
                let path = python_str_gen
                    .strip_prefix(crate::PYTHON_STR_SCHEME)
                    .unwrap();
                Ok(Output::PythonStr(path.to_string()))
            }

143
144
145
146
147
148
149
150
            #[cfg(feature = "python")]
            python_tok_gen if python_tok_gen.starts_with(crate::PYTHON_TOK_SCHEME) => {
                let path = python_tok_gen
                    .strip_prefix(crate::PYTHON_TOK_SCHEME)
                    .unwrap();
                Ok(Output::PythonTok(path.to_string()))
            }

151
152
153
154
155
156
157
158
            e => Err(anyhow::anyhow!("Invalid out= option '{e}'")),
        }
    }
}

impl fmt::Display for Output {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let s = match self {
159
160
161
            #[cfg(feature = "mistralrs")]
            Output::MistralRs => "mistralrs",

162
163
164
            #[cfg(feature = "sglang")]
            Output::SgLang => "sglang",

165
166
167
            #[cfg(feature = "llamacpp")]
            Output::LlamaCpp => "llamacpp",

Graham King's avatar
Graham King committed
168
169
170
            #[cfg(feature = "vllm")]
            Output::Vllm => "vllm",

Graham King's avatar
Graham King committed
171
172
173
            #[cfg(feature = "trtllm")]
            Output::TrtLLM => "trtllm",

174
            Output::EchoFull => "echo_full",
175
            Output::EchoCore => "echo_core",
176
177

            Output::Endpoint(path) => path,
178
179
180

            #[cfg(feature = "python")]
            Output::PythonStr(path) => path,
181
182
183

            #[cfg(feature = "python")]
            Output::PythonTok(path) => path,
184
185
186
187
        };
        write!(f, "{s}")
    }
}