Is it sound to use a slice of numpy::PyReadonlyArray inside pyo3::allow_threads()?
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 25/100
Direzione di ricerca
Inizia con l’ema binding e gli entry point core_ema mostrati nell’issue, quindi leggi la documentazione sul prestito di PyReadonlyArray e il comportamento di pyo3::Python::allow_threads. Il lavoro è completato quando è disponibile una conclusione di sicurezza documentata e supportata dal progetto, oppure una modifica o un workaround chiaramente circoscritto, con ogni esempio o test pertinente identificato dai maintainer.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Greetings,
My goal is to implement an efficient (and safe) way to wrap a Rust function, using numpy::PyReadonlyArray, with zero-copy and GIL release during computation.
My question is general, but here is a concrete example from my EMA function binding:
use crate::indicators::ema::core_ema;
use numpy::{PyArray1, PyArrayMethods};
use pyo3::pyfunction;
#[pyfunction(signature = (data, window_size = 14, alpha = None))]
pub(crate) fn ema<'py>(
py: pyo3::Python<'py>,
data: numpy::PyReadonlyArray1<'py, f64>,
window_size: usize,
alpha: Option<f64>,
) -> pyo3::PyResult<pyo3::Py<numpy::PyArray1<f64>>> {
let slice = data.as_slice()?;
let py_array_out = PyArray1::<f64>::zeros(py, [len], false);
let py_array_ptr = unsafe { py_array_out.as_slice_mut()? };
py.allow_threads(|| core_ema(slice, window_size, alpha.into(), py_array_ptr))
.map_err(|e| pyo3::exceptions::PyValueError::new_err(format!("{:?}", e)))?;
Ok(py_array_out.into())
}
I want to release the GIL to enable multithreading. I wonder how to do it safely. Or if it's possible.
The documentation of numpy::PyReadonlyArray says:
An instance of this type ensures that there are no instances of PyReadwriteArray, i.e. that only shared references into the interior of the array can be created safely
Additionnally, in the numpy module borrow documentation, it says:
The aim of this module is to ensure that safe Rust code is unable to violate these requirements on its own. We cannot prevent unchecked code - this includes unsafe Rust, Python or other native code like C or Fortran - from violating them. Therefore the responsibility to avoid this lies with the author of that code instead of the compiler
So, in my understanding, I get a reference to input data which is owned by Python and can, in theory, be changed from the outside (e.g. by another Python thread). So in my understanding, the answer should be 'no' it is not sound. But I'm not sure if there is a way, or a workaround or a good practice or if I just need to avoid using allow_thread with PyReadonlyArray1.
Thanks a lot for your time.
- Lingua principale
- Rust
- Stelle
- 1.4k
- Fork
- 141
- Merge medio
- 16m
- PR unite (30g)
- 3
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di PyO3/rust-numpy
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
PyO3/rust-numpy#565 · 1 commento ·
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 58/100
PyO3/rust-numpy#563 ·
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 56/100
PyO3/rust-numpy#557 ·
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 45/100
PyO3/rust-numpy#547 · 2 commenti ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 20/100
PyO3/rust-numpy#535 ·
Tutte le issue di PyO3/rust-numpy
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
TheLarkInn/aipm#2413 ·
-
documentation
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 90/100
alexgorbatchev/simple-ptt#15 ·
-
tooling
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
-
todo:ticket
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
taikoxyz/taiko-mono#22168 · 1 commento ·