Skip to content

Commit

Permalink
更新 pyo3 到 0.18
Browse files Browse the repository at this point in the history
  • Loading branch information
AlongWY committed Feb 7, 2023
1 parent acd3ed1 commit 25d353a
Show file tree
Hide file tree
Showing 9 changed files with 13 additions and 28 deletions.
2 changes: 1 addition & 1 deletion python/extension/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ rayon = { version = "1.5" }
rayon-cond = { version = "0.2" }
anyhow = { version = "1.0" }
serde = { version = "1.0", features = ["derive"] }
pyo3 = { version = "0.17", features = ["extension-module", "anyhow", "serde"] }
pyo3 = { version = "0.18", features = ["extension-module", "anyhow", "serde"] }
mimalloc = { version = "0.1", default-features = false, optional = true }

[dependencies.ltp]
Expand Down
1 change: 0 additions & 1 deletion python/extension/src/hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ impl PyHook {
}

/// add words to the hook, the freq can be zero
#[args(freq = "None")]
#[pyo3(text_signature = "(self, word, freq = None)")]
pub fn add_word(&mut self, word: &str, freq: Option<usize>) -> usize {
self.hook.add_word(word, freq)
Expand Down
18 changes: 6 additions & 12 deletions python/extension/src/perceptron/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ pub enum ModelType {
#[pymethods]
impl ModelType {
#[new]
#[args(model_type = "None")]
pub fn new(model_type: Option<&str>) -> PyResult<Self> {
Ok(match model_type {
Some("cws") => ModelType::CWS,
Expand Down Expand Up @@ -80,15 +79,13 @@ pub struct PyModel {
#[pymethods]
impl PyModel {
#[new]
#[args(model_type = "ModelType::Auto")]
pub fn new(path: &str, model_type: ModelType) -> PyResult<Self> {
Self::load(path, model_type)
}

/// Load Model from a path
#[staticmethod]
#[pyo3(text_signature = "(path, model_type=ModelType.Auto)")]
#[args(model_type = "ModelType::Auto")]
pub fn load(path: &str, model_type: ModelType) -> PyResult<Self> {
let file = std::fs::File::open(path)?;
let format = if path.ends_with(".json") {
Expand Down Expand Up @@ -139,15 +136,15 @@ impl PyModel {
EnumModel::CWS(model) => Ok(crate::perceptron::specialization::PyCWSModel {
model: model.clone(),
}
.into_py(py)),
.into_py(py)),
EnumModel::POS(model) => Ok(crate::perceptron::specialization::PyPOSModel {
model: model.clone(),
}
.into_py(py)),
.into_py(py)),
EnumModel::NER(model) => Ok(crate::perceptron::specialization::PyNERModel {
model: model.clone(),
}
.into_py(py)),
.into_py(py)),
}
}

Expand All @@ -168,7 +165,6 @@ impl PyModel {
Ok(())
}

#[args(args = "*", parallelism = true)]
pub fn __call__(&self, py: Python, args: &PyTuple, parallelism: bool) -> PyResult<PyObject> {
let first = args.get_item(0)?;
let is_single = match &self.model {
Expand Down Expand Up @@ -210,7 +206,6 @@ impl PyModel {

/// Predict a sentence
#[pyo3(text_signature = "(self, *args)")]
#[args(args = "*")]
pub fn predict(&self, py: Python, args: &PyTuple) -> PyResult<PyObject> {
Ok(match &self.model {
EnumModel::CWS(model) => {
Expand All @@ -222,7 +217,7 @@ impl PyModel {
.into_iter()
.map(|s| PyString::new(py, s)),
)
.into()
.into()
}
EnumModel::POS(model) => {
let words: Vec<&str> = args.get_item(0)?.extract()?;
Expand All @@ -233,7 +228,7 @@ impl PyModel {
.into_iter()
.map(|s| PyString::new(py, s)),
)
.into()
.into()
}
EnumModel::NER(model) => {
let words: Vec<&str> = args.get_item(0)?.extract()?;
Expand All @@ -245,14 +240,13 @@ impl PyModel {
.into_iter()
.map(|s| PyString::new(py, s)),
)
.into()
.into()
}
})
}

/// Predict batched sentences
#[pyo3(text_signature = "(self, *args, parallelism = True)")]
#[args(args = "*", parallelism = true)]
pub fn batch_predict(
&self,
py: Python,
Expand Down
4 changes: 1 addition & 3 deletions python/extension/src/perceptron/specialization/cws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ impl PyCWSModel {
Ok(())
}

#[args(args = "*", parallelism = true)]
pub fn __call__(&self, py: Python, args: &PyTuple, parallelism: bool) -> PyResult<PyObject> {
let first = args.get_item(0)?;
let is_single = match first.get_type().name()? {
Expand Down Expand Up @@ -153,11 +152,10 @@ impl PyCWSModel {
.into_iter()
.map(|s| PyString::new(py, s)),
)
.into())
.into())
}

/// Predict batched sentences
#[args(parallelism = true)]
#[pyo3(text_signature = "(self, batch_text, parallelism=True)")]
pub fn batch_predict(
&self,
Expand Down
4 changes: 1 addition & 3 deletions python/extension/src/perceptron/specialization/ner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ impl PyNERModel {
Ok(Self::inner_load(path)?)
}

#[args(args = "*", parallelism = true)]
pub fn __call__(&self, py: Python, args: &PyTuple, parallelism: bool) -> PyResult<PyObject> {
let first = args.get_item(0)?;
let is_single = match first.get_type().name()? {
Expand Down Expand Up @@ -71,11 +70,10 @@ impl PyNERModel {
.into_iter()
.map(|s| PyString::new(py, s)),
)
.into())
.into())
}

/// Predict batched sentences
#[args(parallelism = true)]
#[pyo3(text_signature = "(self, batch_words, batch_pos , parallelism=True)")]
pub fn batch_predict(
&self,
Expand Down
4 changes: 1 addition & 3 deletions python/extension/src/perceptron/specialization/pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ impl PyPOSModel {
Ok(Self::inner_load(path)?)
}

#[args(args = "*", parallelism = true)]
pub fn __call__(&self, py: Python, args: &PyTuple, parallelism: bool) -> PyResult<PyObject> {
let first = args.get_item(0)?;
let is_single = match first.get_type().name()? {
Expand Down Expand Up @@ -62,11 +61,10 @@ impl PyPOSModel {
.into_iter()
.map(|s| PyString::new(py, s)),
)
.into())
.into())
}

/// Predict batched sentences
#[args(parallelism = true)]
#[pyo3(text_signature = "(self, batch_words, parallelism=True)")]
pub fn batch_predict(
&self,
Expand Down
1 change: 0 additions & 1 deletion python/extension/src/perceptron/trainer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ pub struct PyTrainer {
#[pymethods]
impl PyTrainer {
#[new]
#[args(model_type = "ModelType::Auto", labels = "None")]
pub fn new(model_type: ModelType, labels: Option<Vec<String>>) -> PyResult<Self> {
let trainer = match (model_type, labels) {
(ModelType::CWS, _) => EnumTrainer::CWS(Default::default()),
Expand Down
1 change: 0 additions & 1 deletion python/extension/src/stnsplit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ impl StnSplit {
}

/// batch split to sentences
#[args(threads = "8")]
#[pyo3(text_signature = "(self, batch_text, threads=8)")]
pub fn batch_split(
&self,
Expand Down
6 changes: 3 additions & 3 deletions rust/ltp-cffi/examples/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ int main() {
const char *pos_model_path = "data/legacy-models/pos_model.bin";
const char *ner_model_path = "data/legacy-models/ner_model.bin";
Model *cws_model = NULL;
cws_model = model_load(cws_model_path, strlen(cws_model_path));
cws_model = model_load(cws_model_path);
Model *pos_model = NULL;
pos_model = model_load(pos_model_path, strlen(pos_model_path));
pos_model = model_load(pos_model_path);
Model *ner_model = NULL;
ner_model = model_load(ner_model_path, strlen(ner_model_path));
ner_model = model_load(ner_model_path);

const char *sentence = "他叫汤姆去拿外衣";
size_t word_length[MAX_WORD_LEN] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
Expand Down

0 comments on commit 25d353a

Please sign in to comment.