coremltools icon indicating copy to clipboard operation
coremltools copied to clipboard

PyPI sdist missing C++ source — cannot build native libraries from source

Open mikedep333 opened this issue 2 months ago • 4 comments

🐞Describing the bug

The PyPI source distribution (sdist) does not include the C++ source directories needed to build the native libraries (libmilstoragepython, libmodelpackage, kmeans1d/_core). Building from the sdist produces a wheel without these libraries, so model conversion crashes at runtime.

The Git repository contains everything needed — CMakeLists.txt, deps/, mlmodel/, cmake/, modelpackage/, milstoragepython/ — but these are excluded from the sdist tarball. The setup.py packages the resulting .so files via package_data, but since the CMake build never runs during pip install, they don't exist.

Stack Trace

WARNING: No module named 'coremltools.libmilstoragepython' WARNING: No module named 'coremltools.libcoremlpython'

RuntimeError: BlobWriter not loaded

To Reproduce

pip install coremltools --no-binary coremltools python -c " import coremltools as ct import torch

model = torch.nn.Linear(10, 5) traced = torch.jit.trace(model, torch.randn(1, 10)) ct.convert(traced) # RuntimeError: BlobWriter not loaded "

System environment (please complete the following information):

  • coremltools version: 8.3 and 9.0 (likely all versions)
  • OS (e.g. MacOS version or Linux type): Linux x86_64 (RHEL 9), but affects any platform when building from sdist
  • Any other relevant version information (e.g. PyTorch or TensorFlow version): Python 3.12, PyTorch 2.6.0

Additional context

Building from the Git repository with CMake works perfectly — all three native libraries compile and model conversion succeeds. The published wheels on PyPI (both macOS and manylinux) include the native libraries and work fine — the issue is only with the sdist.

The fix would be to include the C++ source directories in MANIFEST.in (or equivalent) and wire the CMake build into the PEP 517 build backend so pip install from the sdist produces a working package.

mikedep333 avatar Sep 04 '26 19:09 mikedep333

This is basically the same issue as #2350. However this issue is stated much more succinctly. I'll dup that issue to this one.

TobyRoseman avatar Sep 04 '26 23:09 TobyRoseman

I will take this issue. Please assign it to me.

The problem is that the PyPI sdist is missing necessary C++ source directories. This prevents building native libraries during installation. I will first check the MANIFEST.in file to ensure it includes the required directories like milstoragepython/ and modelpackage/. Then, I will modify the build process in setup.py to integrate the CMake build with the PEP 517 backend. This should allow pip install to compile the libraries correctly from the sdist.

modelpath-dev avatar Sep 09 '26 07:09 modelpath-dev

Hi Toby,

Mike and I are on the same team. Mike has pointed out issues with coremltools' build system in this ticket and #2848.

You basically have two options to solve the issue:

  • make your MANIFEST.in, setup.py, and CMake build system even more complicated
  • ditch setuptools and move to a modern PEP 517 build backend with native CMake support

Full disclosure: I have to admit that I'm no longer a fan of setuptools. I have been doing Python development for nearly 25 years. About 20 years ago setuptools was a real improvement over distutils. These days it's an antiquated build system for Python and ill-suited for CMake-based projects. coremltools has been around for a decade. It made sense to use setuptools. Even ten years ago, setuptools was the only viable and good option for building Python packages.

Nowadays there are several modern build backends tailored for Python projects with C/C++ code. The most prominent ones are meson-python for Meson based projects (NumPy, SciPy, matplotlib) and scikit-build-core (Torch, lightgbm). Torch switched to scikit-build-core a few months ago. coremltools uses CMake, so scikit-build-core would make sense.

Fun fact: The documentation of vendored pybind11https://github.com/apple/coremltools/blob/9.0/deps/pybind11/docs/compiling.rst recommends scikit_build_core for CMake projects and calls setuptools a historically popular option.

In case I got you interested, here is a PoC in my fork. It's mostly AI generated. The branch has three commits:

  1. Replaces setup.py with pyproject.toml and scikit-build-core (my CMake is a bit rusty and I'm not sure if all changes to CMakeLists.txt make sense)
  2. Updates scripts and documentation
  3. Replaces version.py with setuptools-scm

I can open a PR if you are interested.

tiran avatar Sep 09 '26 08:09 tiran

@modelpath-dev I cannot assign the issue to you, but thank you very much!

Your solution would meet our needs. See @tiran 's comment above, and let us know if we can be of help.

mikedep333 avatar Sep 09 '26 15:09 mikedep333