Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

vllm support#1063

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
kczimm wants to merge9 commits intomaster
base:master
Choose a base branch
Loading
fromkczimm-vllm-support
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletionspgml-extension/requirements.txt
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -26,3 +26,4 @@ xgboost==2.0.0
langchain==0.0.287
einops==0.6.1
pynvml==11.5.0
vllm==0.2.0
20 changes: 19 additions & 1 deletionpgml-extension/src/api.rs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,6 +4,7 @@ use std::str::FromStr;
use ndarray::Zip;
use pgrx::iter::{SetOfIterator, TableIterator};
use pgrx::*;
use serde_json::Value;

#[cfg(feature = "python")]
use serde_json::json;
Expand DownExpand Up@@ -610,7 +611,7 @@ pub fn transform_json(
inputs: default!(Vec<&str>, "ARRAY[]::TEXT[]"),
cache: default!(bool, false),
) -> JsonB {
matchcrate::bindings::transformers::transform(&task.0,&args.0, inputs) {
match transform(task.0, args.0, inputs) {
Ok(output) => JsonB(output),
Err(e) => error!("{e}"),
}
Expand All@@ -632,6 +633,23 @@ pub fn transform_string(
}
}

fn transform(mut task: Value, args: Value, inputs: Vec<&str>) -> anyhow::Result<Value> {
// use vLLM if model present in task and backend is set to vllm
let use_vllm = task.as_object_mut().is_some_and(|obj| {
obj.contains_key("model") && matches!(obj.get("backend"), Some(Value::String(backend)) if backend.to_string().to_ascii_lowercase() == "vllm")
});

if use_vllm {
Ok(crate::bindings::vllm::vllm_inference(&task, &inputs)?)
} else {
if let Some(map) = task.as_object_mut() {
// pop backend keyword, if present
let _ = map.remove("backend");
}
crate::bindings::transformers::transform(&task, &args, inputs)
}
}

#[cfg(feature = "python")]
#[pg_extern(immutable, parallel_safe, name = "generate")]
fn generate(project_name: &str, inputs: &str, config: default!(JsonB, "'{}'")) -> String {
Expand Down
2 changes: 2 additions & 0 deletionspgml-extension/src/bindings/mod.rs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -46,6 +46,8 @@ pub mod python;
pub mod sklearn;
#[cfg(feature = "python")]
pub mod transformers;
#[cfg(feature = "python")]
pub mod vllm;
pub mod xgboost;

pub type Fit = fn(dataset: &Dataset, hyperparams: &Hyperparams) -> Result<Box<dyn Bindings>>;
Expand Down
86 changes: 86 additions & 0 deletionspgml-extension/src/bindings/vllm/inference.rs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
use parking_lot::Mutex;
use pyo3::prelude::*;
use serde_json::{json, Value};

use super::LLM;

/// Cache a single model per client process. vLLM does not allow multiple, simultaneous models to be loaded.
/// See GH issue, https://github.com/vllm-project/vllm/issues/565
static MODEL: Mutex<Option<LLM>> = Mutex::new(None);

pub fn vllm_inference(task: &Value, inputs: &[&str]) -> PyResult<Value> {
crate::bindings::python::activate().expect("python venv activate");
let mut model = MODEL.lock();

let llm = match get_model_name(&model, task) {
ModelName::Same => model.as_mut().expect("ModelName::Same as_mut"),
ModelName::Different(name) => {
if let Some(llm) = model.take() {
// delete old model, exists
destroy_model_parallel(llm)?;
}
// make new model
let llm = LLM::new(&name)?;
model.insert(llm)
}
};

let outputs = llm
.generate(&inputs, None)?
.iter()
.map(|o| {
o.outputs()
.expect("RequestOutput::outputs()")
.iter()
.map(|o| o.text().expect("CompletionOutput::text()"))
.collect::<Vec<_>>()
})
.collect::<Vec<Vec<_>>>();

Ok(json!(outputs))
}

/// Determine if the "model" specified in the task is the same model as the one cached.
///
/// # Panic
/// This function panics if:
/// - `task` is not an object
/// - "model" key is missing from `task` object
/// - "model" value is not a str
fn get_model_name<M>(model: &M, task: &Value) -> ModelName
where
M: std::ops::Deref<Target = Option<LLM>>,
{
let name = task.as_object()
.expect("`task` is an object")
.get("model")
.expect("model key is present")
.as_str()
.expect("model value is a str");

if matches!(model.as_ref(), Some(llm) if llm.model() == name) {
ModelName::Same
} else {
ModelName::Different(name.to_string())
}
}

enum ModelName {
Same,
Different(String),
}

// See https://github.com/vllm-project/vllm/issues/565#issuecomment-1725174811
fn destroy_model_parallel(llm: LLM) -> PyResult<()> {
Python::with_gil(|py| {
PyModule::import(py, "vllm")?
.getattr("model_executor")?
.getattr("parallel_utils")?
.getattr("parallel_state")?
.getattr("destroy_model_parallel")?
.call0()?;
drop(llm);
PyModule::import(py, "gc")?.getattr("collect")?.call0()?;
Ok(())
})
}
Loading

[8]ページ先頭

©2009-2025 Movatter.jp