Skip to content

Activate venv at server start and remove the check from functions #1276

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 0 additions & 2 deletions pgml-extension/src/bindings/langchain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ use crate::create_pymodule;
create_pymodule!("/src/bindings/langchain/langchain.py");

pub fn chunk(splitter: &str, text: &str, kwargs: &serde_json::Value) -> Result<Vec<String>> {
crate::bindings::python::activate()?;

let kwargs = serde_json::to_string(kwargs).unwrap();

Python::with_gil(|py| -> Result<Vec<String>> {
Expand Down
4 changes: 0 additions & 4 deletions pgml-extension/src/bindings/python/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ pub fn activate() -> Result<bool> {
}

pub fn pip_freeze() -> Result<TableIterator<'static, (name!(package, String),)>> {
activate()?;
let packages = Python::with_gil(|py| -> Result<Vec<String>> {
let freeze = get_module!(PY_MODULE).getattr(py, "freeze")?;
let result = freeze.call0(py)?;
Expand All @@ -42,7 +41,6 @@ pub fn pip_freeze() -> Result<TableIterator<'static, (name!(package, String),)>>
}

pub fn validate_dependencies() -> Result<bool> {
activate()?;
Python::with_gil(|py| {
let sys = PyModule::import(py, "sys").unwrap();
let version: String = sys.getattr("version").unwrap().extract().unwrap();
Expand All @@ -68,7 +66,6 @@ pub fn validate_dependencies() -> Result<bool> {
}

pub fn version() -> Result<String> {
activate()?;
Python::with_gil(|py| {
let sys = PyModule::import(py, "sys").unwrap();
let version: String = sys.getattr("version").unwrap().extract().unwrap();
Expand All @@ -77,7 +74,6 @@ pub fn version() -> Result<String> {
}

pub fn package_version(name: &str) -> Result<String> {
activate()?;
Python::with_gil(|py| {
let package = py.import(name)?;
Ok(package.getattr("__version__")?.extract()?)
Expand Down
10 changes: 0 additions & 10 deletions pgml-extension/src/bindings/transformers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ pub fn get_model_from(task: &Value) -> Result<String> {
}

pub fn embed(transformer: &str, inputs: Vec<&str>, kwargs: &serde_json::Value) -> Result<Vec<Vec<f32>>> {
crate::bindings::python::activate()?;

let kwargs = serde_json::to_string(kwargs)?;
Python::with_gil(|py| -> Result<Vec<Vec<f32>>> {
let embed: Py<PyAny> = get_module!(PY_MODULE).getattr(py, "embed").format_traceback(py)?;
Expand All @@ -58,8 +56,6 @@ pub fn embed(transformer: &str, inputs: Vec<&str>, kwargs: &serde_json::Value) -
}

pub fn tune(task: &Task, dataset: TextDataset, hyperparams: &JsonB, path: &Path) -> Result<HashMap<String, f64>> {
crate::bindings::python::activate()?;

let task = task.to_string();
let hyperparams = serde_json::to_string(&hyperparams.0)?;

Expand All @@ -86,8 +82,6 @@ pub fn tune(task: &Task, dataset: TextDataset, hyperparams: &JsonB, path: &Path)
}

pub fn generate(model_id: i64, inputs: Vec<&str>, config: JsonB) -> Result<Vec<String>> {
crate::bindings::python::activate()?;

Python::with_gil(|py| -> Result<Vec<String>> {
let generate = get_module!(PY_MODULE).getattr(py, "generate").format_traceback(py)?;
let config = serde_json::to_string(&config.0)?;
Expand Down Expand Up @@ -162,8 +156,6 @@ pub fn load_dataset(
limit: Option<usize>,
kwargs: &serde_json::Value,
) -> Result<usize> {
crate::bindings::python::activate()?;

let kwargs = serde_json::to_string(kwargs)?;

let dataset = Python::with_gil(|py| -> Result<String> {
Expand Down Expand Up @@ -313,8 +305,6 @@ pub fn load_dataset(
}

pub fn clear_gpu_cache(memory_usage: Option<f32>) -> Result<bool> {
crate::bindings::python::activate().unwrap();

Python::with_gil(|py| -> Result<bool> {
let clear_gpu_cache: Py<PyAny> = get_module!(PY_MODULE)
.getattr(py, "clear_gpu_cache")
Expand Down
2 changes: 0 additions & 2 deletions pgml-extension/src/bindings/transformers/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ pub fn transform<T: serde::Serialize>(
args: &serde_json::Value,
inputs: T,
) -> Result<serde_json::Value> {
crate::bindings::python::activate()?;
whitelist::verify_task(task)?;

let task = serde_json::to_string(task)?;
Expand Down Expand Up @@ -74,7 +73,6 @@ pub fn transform_stream<T: serde::Serialize>(
args: &serde_json::Value,
input: T,
) -> Result<Py<PyAny>> {
crate::bindings::python::activate()?;
whitelist::verify_task(task)?;

let task = serde_json::to_string(task)?;
Expand Down
1 change: 1 addition & 0 deletions pgml-extension/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ extension_sql_file!("../sql/schema.sql", name = "schema");
#[cfg(not(feature = "use_as_lib"))]
#[pg_guard]
pub extern "C" fn _PG_init() {
bindings::python::activate().expect("Error setting python venv");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works? I ask because each connection forks from the postmaster. It gets the venv set correctly? :O

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works for me locally. I can test from multiple different connections.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can confirm tested from three different connections and it worked

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, that was easy.

orm::project::init();
}

Expand Down
9 changes: 1 addition & 8 deletions pgml-extension/src/orm/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,6 @@ impl Model {
},
};

if runtime == Runtime::python {
let _ = crate::bindings::python::activate();
}

let dataset = snapshot.tabular_dataset();
let status = Status::in_progress;
// Create the model record.
Expand Down Expand Up @@ -334,10 +330,7 @@ impl Model {
}

#[cfg(feature = "python")]
Runtime::python => {
let _ = crate::bindings::python::activate();
crate::bindings::sklearn::Estimator::from_bytes(&data)?
}
Runtime::python => crate::bindings::sklearn::Estimator::from_bytes(&data)?,

#[cfg(not(feature = "python"))]
Runtime::python => {
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy