Skip to content
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

rewrite pybind interface for Array2 and Fsa #69

Merged
merged 1 commit into from
Jul 23, 2020

Conversation

qindazhu
Copy link
Collaborator

Guys, please have a review. This PR is relatively important, if the whole structure/style is fine, I will follow it to wrap other algorithms to python (most of them will be straightforward).

Note as in Python, we cannot pass primitive type (int, float, etc.) to function as reference/pointer, I get Array1 back to support those arc_map (see example in arc_sort)

@csukuangfj
Copy link
Collaborator

csukuangfj commented Jul 22, 2020

You can leave the interfaces as they are.

To wrap them to python, you only need to add an indirection.

Take

void ArcSorter::GetOutput(Fsa *fsa_out, int32_t *arc_map /*= nullptr*/) const

as an example, you can use

pyclass.def("get_output", [](const ArcSorter& self, Fsa* fsa_out, std::vector<int32_t>* arc_map = nullptr) {
        self.GetOutput(fsa_out, arc_map ? arc_map->data() : nullptr);
}, py::arg("fsa_out"), py::arg("arc_map") = nullptr
);

I assume that you have made std::vector<int32_t> an opaque type and arc_map is saved in std::vector<int32_t>.

@qindazhu
Copy link
Collaborator Author

I make Array1 share memory with torch.tensor as we do in Array2, so does vector<int32_t> here share memory with tensor? how do you do that with above code?

@csukuangfj
Copy link
Collaborator

I make Array1 share memory with torch.tensor as we do in Array2, so does vector<int32_t> here share memory with tensor? how do you do that with above code?

You can still leave the interfaces as they are. Just replace std::vector<int32_t> above with Array1.

@csukuangfj
Copy link
Collaborator

csukuangfj commented Jul 22, 2020

Alternatively, you can replace std::vector<int32_t> with a py::capsule and construct an Array1 from the py::capsule inside the lambda function. In this case, only DLPackArray1 is needed.

@qindazhu
Copy link
Collaborator Author

Oh, you're right, as we could just change the interface in Python code.

You can still leave the interfaces as they are. Just replace std::vector<int32_t> above with Array1.

I would like to choose this one as constructing Array1 outside of the function is clearer and consistent with what we do for Fsa.

@csukuangfj
Copy link
Collaborator

I would like to choose this one as constructing Array1 outside of the function is clearer and consistent with what we do for Fsa.

Anyway, there is no need to change k2/csrc/*.cc files.

@qindazhu qindazhu force-pushed the haowen-pybind branch 2 times, most recently from 4fa6ba9 to 6628c33 Compare July 22, 2020 12:20
@qindazhu
Copy link
Collaborator Author

Updated, thanks @csukuangfj for the suggestions.

k2/python/csrc/array.cc Outdated Show resolved Hide resolved
@csukuangfj
Copy link
Collaborator

+1

@danpovey
Copy link
Collaborator

danpovey commented Jul 22, 2020 via email

@qindazhu
Copy link
Collaborator Author

Merging..

@qindazhu qindazhu merged commit bf72293 into k2-fsa:master Jul 23, 2020
@qindazhu qindazhu deleted the haowen-pybind branch July 23, 2020 01:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants