You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Best way to distribute a Python package which relies on the C api? #655
I'm wondering if you have any suggestions or best practices for distributing a Python package that uses classes and methods from the low-level API (things in the _tskit module and/or raw C functions)? Right now I'm using Cython, and bundling the necessary includes and source files (tskit.h, tables.[c,h], etc.) directly in my package. This works fine; the source code is extremely light-weight. But it means I must pin to a specific version of _tskit, or possibly run into binary incompatibility errors. (In fact this already happened to me once; a small change in one of the header files between versions caused all sorts of data alignment errors and segfaults until I figured out what was going on.) Perhaps you'd consider adding something like np.get_includes() and including the headers with each release?
You could include tskit as a git submodule, and only update that as necessary. With a submodule, you'd avoid issues with a different tskit being installed elsewhere on the system.
Great to hear you're doing this sort of thing @terhorst, and I'm very happy to support embedding the C API in python packages! We're very open to adding any new infrastructure that would make life easier for downstream developers like you. However, ABI compatibility is hard, and until we hit C API 1.0, I don't think we can make any promises on that count.
The general philosophy we've taken to the C code is to assume that people are going to embed tskit rather than link to it. It sounds like you're embedding the C code (which is great). However, I'm not clear how you're interacting with _tskit - would you mind sketching out how you're setting things up?
User provides a (Python) TreeSequence to my method.
I do some preprocessing via the Pythonic interface, things like computing windowed summary statistics, GKNNs, etc.
Having done this, I want to iterate over the TreeSequence as fast as possible in C. In particular, I want to use the VariantGenerator class, and it is much easier to initialize this in Python using a Cython extension type, i.e.
ctypedef class _tskit.VariantGenerator [object VariantGenerator]: ...
# followed by
cdef VariantGenerator vargen = _tskit.VariantGenerator(ts.get_ll_tree_sequence(), ...)
than it is to do it in C.
Of course, step (1) is optional -- the user could simply provide a filename instead, though this approach seems more Pythonic. However, (2) and particularly (3) get tricky. If the user has a different version of tskit installed than the one against which I compiled, the program will potentially crash.
For now, the solution of just bundling all the headers and pinning to a specific version is fine. It just means that the user will have to create a separate venv for our package, or settle for a pinned version of tskit that does not automatically update.
We'll have a pre-print up in a couple of weeks at which point I can share the code and be more precise. (And I'm fine with a #wontfix resolution on this if the use case is far out of the mainstream.)
This is very tricky @terhorst, and I'm afraid we won't be able to support using the low-level Python C API like this. Trying to maintain ABI compatibility is just too hard, and it would constrain our development too much if we had to worry about maintaining it right now. Also, the low-level Python interface is undocumented and will be changing arbitrarily in the future - to be able to maintain a stable high-level interface and develop the library, we do need to be able to change things under the hood.
Having said all that, I'm sure there'll be a way to achieve what you want to do in a stable and forward compatible way. I'm happy to have a look when you open source the code, and see what we can do. What you want to do should be possible (take a tree sequence as input in Python and process it using the C API), so we'll figure out a supportable way to do it and document it.
So, maybe you could ping us again here in a few weeks once your code is ready to view? Good luck with getting the preprint finished, I'm excited to see what you've been up to!
On Mon, Jun 1, 2020 at 11:47 AM Jerome Kelleher ***@***.***> wrote:
This is very tricky @terhorst <https://github.com/terhorst>, and I'm
afraid we won't be able to support using the low-level Python C API like
this. Trying to maintain ABI compatibility is just too hard, and it would
constrain our development too much if we had to worry about maintaining it
right now. Also, the low-level Python interface is undocumented and will be
changing arbitrarily in the future - to be able to maintain a stable
high-level interface and develop the library, we do need to be able to
change things under the hood.
Having said all that, I'm sure there'll be a way to achieve what you want
to do in a stable and forward compatible way. I'm happy to have a look when
you open source the code, and see what we can do. What you want to do
*should* be possible (take a tree sequence as input in Python and process
it using the C API), so we'll figure out a supportable way to do it and
document it.
So, maybe you could ping us again here in a few weeks once your code is
ready to view? Good luck with getting the preprint finished, I'm excited to
see what you've been up to!
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#655 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAEOHCD435ELATVFVFOYNLRUPESTANCNFSM4NPF2ARQ>
.
We use this to iterate over variants in Cython instead of in a Python for loop. To make this work, we currently have to distribute the tskit headers and pin to a specific version. I think an alternative solution would be to distribute the headers alongside the low-level _tskit module. That way, provided our module was compiled on installation, everything should work regardless of the installed tskit version. This shouldn't require you to maintain API compatibility between versions since, if there is an API change in tskit, the worst that would happen is the compile fails (and I have to update our code).
One potential issue that could arise if if tskit is upgraded after the fact. If the memory layout of any of the structs changed, our software would probably crash. It seems like this would be a problem for other projects as well, namely Numpy. Perhaps their API is so mature that the situation does not arise in practice. In any case, this would manifest as a bug in our code not yours, so no added work for you.
This is very interesting @terhorst, and it's really great to see you using the C APIs. The way we've been focusing on doing this for msprime and tsinfer is for each Python module to compile in its own local version of the tskit C APIs. Sure, it's a bit wasteful but tskit's a pretty small library so it doesn't amount to that much extra size in the binaries. That way, there's never any API or ABI worries when you're dealing with other versions of tskit.
The tricky bit, then, is transferring the tree sequence data between the two versions of tskit. For example, in msprime we have tsk_table_collection_t which contains data generated by the simulation in C, and we want to pass that back to Python so that we can create the corresponding tskit.TreeSequence Python object (backed by tskit's compiled version of the C tskit API, which doesn't have to be the same as msprime's). The way we do this is using a LightweightTableCollection Python/C class, which just wraps a tskit table_collection_t and provides methods to move data in and out using a canonical dictionary form. At the moment, we're copying the code for this LightweightTableCollection directly into the low-level C modules for msprime and tsinfer, but we are hoping we can come up with a better solution than this. Some kind of tskit-CPython shim which could be included in projects which do this sort of thing, and take away this hassle and nasty code duplication.
Would something like this work for you? I did look into using the CPython capsules API quite extensively when figuring out how to split tskit out of msprime, and I decided that just recompiling the code into each module was the only way to really make things solid and to avoid API/ABI issues.
We can certainly look into shipping some headers along with the low-level module as well, if the above proposal doesn't fit your needs. But I really would like to have a general and well-documented solution which people could use and be assured was robust to changes in tskit versions over time, and I think the approach above is a good candidate for that.
Oh, interesting, it never occurred to me that msprime and tskit could be running different versions of the C API. Your solution makes sense. I'll look into LightWeightTableCollection. Thanks for all the help!
Activity
molpopgen commentedon May 31, 2020
You could include tskit as a git submodule, and only update that as necessary. With a submodule, you'd avoid issues with a different tskit being installed elsewhere on the system.
jeromekelleher commentedon Jun 1, 2020
Great to hear you're doing this sort of thing @terhorst, and I'm very happy to support embedding the C API in python packages! We're very open to adding any new infrastructure that would make life easier for downstream developers like you. However, ABI compatibility is hard, and until we hit C API 1.0, I don't think we can make any promises on that count.
The general philosophy we've taken to the C code is to assume that people are going to embed tskit rather than link to it. It sounds like you're embedding the C code (which is great). However, I'm not clear how you're interacting with _tskit - would you mind sketching out how you're setting things up?
terhorst commentedon Jun 1, 2020
Thanks for the responses. Sure, my use case is:
TreeSequenceto my method.Of course, step (1) is optional -- the user could simply provide a filename instead, though this approach seems more Pythonic. However, (2) and particularly (3) get tricky. If the user has a different version of
tskitinstalled than the one against which I compiled, the program will potentially crash.For now, the solution of just bundling all the headers and pinning to a specific version is fine. It just means that the user will have to create a separate venv for our package, or settle for a pinned version of
tskitthat does not automatically update.We'll have a pre-print up in a couple of weeks at which point I can share the code and be more precise. (And I'm fine with a #wontfix resolution on this if the use case is far out of the mainstream.)
jeromekelleher commentedon Jun 1, 2020
This is very tricky @terhorst, and I'm afraid we won't be able to support using the low-level Python C API like this. Trying to maintain ABI compatibility is just too hard, and it would constrain our development too much if we had to worry about maintaining it right now. Also, the low-level Python interface is undocumented and will be changing arbitrarily in the future - to be able to maintain a stable high-level interface and develop the library, we do need to be able to change things under the hood.
Having said all that, I'm sure there'll be a way to achieve what you want to do in a stable and forward compatible way. I'm happy to have a look when you open source the code, and see what we can do. What you want to do should be possible (take a tree sequence as input in Python and process it using the C API), so we'll figure out a supportable way to do it and document it.
So, maybe you could ping us again here in a few weeks once your code is ready to view? Good luck with getting the preprint finished, I'm excited to see what you've been up to!
terhorst commentedon Jun 1, 2020
terhorst commentedon Sep 24, 2020
Surprise surprise, "a couple of weeks" turned into "three and a half months" :-) The gist of what we are doing is here:
https://github.com/terhorst/xsmc/blob/7b6f35d1b0713d86d11e23b5d71e6d401e480f9c/xsmc/_tskit.pxd#L62-L65
We use this to iterate over variants in Cython instead of in a Python for loop. To make this work, we currently have to distribute the tskit headers and pin to a specific version. I think an alternative solution would be to distribute the headers alongside the low-level
_tskitmodule. That way, provided our module was compiled on installation, everything should work regardless of the installed tskit version. This shouldn't require you to maintain API compatibility between versions since, if there is an API change in tskit, the worst that would happen is the compile fails (and I have to update our code).One potential issue that could arise if if tskit is upgraded after the fact. If the memory layout of any of the structs changed, our software would probably crash. It seems like this would be a problem for other projects as well, namely Numpy. Perhaps their API is so mature that the situation does not arise in practice. In any case, this would manifest as a bug in our code not yours, so no added work for you.
jeromekelleher commentedon Sep 25, 2020
This is very interesting @terhorst, and it's really great to see you using the C APIs. The way we've been focusing on doing this for msprime and tsinfer is for each Python module to compile in its own local version of the tskit C APIs. Sure, it's a bit wasteful but tskit's a pretty small library so it doesn't amount to that much extra size in the binaries. That way, there's never any API or ABI worries when you're dealing with other versions of tskit.
The tricky bit, then, is transferring the tree sequence data between the two versions of tskit. For example, in msprime we have tsk_table_collection_t which contains data generated by the simulation in C, and we want to pass that back to Python so that we can create the corresponding
tskit.TreeSequencePython object (backed by tskit's compiled version of the C tskit API, which doesn't have to be the same as msprime's). The way we do this is using a LightweightTableCollection Python/C class, which just wraps a tskit table_collection_t and provides methods to move data in and out using a canonical dictionary form. At the moment, we're copying the code for this LightweightTableCollection directly into the low-level C modules for msprime and tsinfer, but we are hoping we can come up with a better solution than this. Some kind of tskit-CPython shim which could be included in projects which do this sort of thing, and take away this hassle and nasty code duplication.Would something like this work for you? I did look into using the CPython capsules API quite extensively when figuring out how to split tskit out of msprime, and I decided that just recompiling the code into each module was the only way to really make things solid and to avoid API/ABI issues.
We can certainly look into shipping some headers along with the low-level module as well, if the above proposal doesn't fit your needs. But I really would like to have a general and well-documented solution which people could use and be assured was robust to changes in tskit versions over time, and I think the approach above is a good candidate for that.
terhorst commentedon Sep 25, 2020
Oh, interesting, it never occurred to me that msprime and tskit could be running different versions of the C API. Your solution makes sense. I'll look into LightWeightTableCollection. Thanks for all the help!
rewrite to be version agnostic to tskit (cf tskit-dev/tskit#655)