opt.rs 5.52 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
21
22
23
24
25
pub enum Input {
    /// Run an OpenAI compatible HTTP server
    Http,

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

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

    /// 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,
34
35
36
37
38
39
40
41
42
}

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),
43
            "none" => Ok(Input::None),
44
45
46
47
            endpoint_path if endpoint_path.starts_with(ENDPOINT_SCHEME) => {
                let path = endpoint_path.strip_prefix(ENDPOINT_SCHEME).unwrap();
                Ok(Input::Endpoint(path.to_string()))
            }
48
49
50
51
52
53
54
55
56
57
            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",
58
            Input::Endpoint(path) => path,
59
            Input::None => "none",
60
61
62
63
64
65
66
67
        };
        write!(f, "{s}")
    }
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

134
135
136
137
138
139
140
141
            #[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()))
            }

142
143
144
145
146
147
148
149
            #[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()))
            }

150
151
152
153
154
155
156
157
            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 {
158
159
160
            #[cfg(feature = "mistralrs")]
            Output::MistralRs => "mistralrs",

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

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

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

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

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

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

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

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