LightGBM: [ci] aarch64 python-package job failing: "cannot allocate memory in static TLS block"

Description

For the last few days, I’ve observed the aarch64 CI job (which we run on an x86_64 box, using QEMU for emulation), failing with errors like the following during test collection:

___________ ERROR collecting tests/python_package_test/test_basic.py ___________
ImportError while importing test module '/LightGBM/tests/python_package_test/test_basic.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/root/miniforge/envs/test-env/lib/python3.12/importlib/__init__.py:90: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/python_package_test/test_basic.py:12: in <module>
    from sklearn.datasets import dump_svmlight_file, load_svmlight_file, make_blobs
/root/miniforge/envs/test-env/lib/python3.12/site-packages/sklearn/__init__.py:97: in <module>
    from .utils._show_versions import show_versions
/root/miniforge/envs/test-env/lib/python3.12/site-packages/sklearn/utils/_show_versions.py:15: in <module>
    from ._openmp_helpers import _openmp_parallelism_enabled
E   ImportError: /root/miniforge/envs/test-env/lib/python3.12/site-packages/sklearn/utils/../../../../libgomp.so.1: cannot allocate memory in static TLS block

Reproducible example

This is happening across several different PRs, with changesets that are very unlikely to be causing this, suggesting it’s some other change in the environment. For example:

Environment info

N/A

Additional Comments

“TLS” in this error refers to “thread-local storage”.

There is lots of prior discussion on similar issues:

All of those are about using libgomp on aarch64.

From https://bugzilla.redhat.com/show_bug.cgi?id=1722181:

The GNU TLS2 model which I’m afraid aarch64 uses unfortunately eats from the same TLS preallocated pool as libraries that require static TLS like libgomp, where it is performance critical to have it as static TLS.

On https://github.com/opencv/opencv/issues/14884, there’s some discussion about this specifically being caused by bundled libgomp in multiple Python packages, and there are suggestions that importing those libraries earlier (and therefore loading their libgomp earlier) can resolve this.

These also have some helpful information:

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16

Commits related to this issue

Most upvoted comments

I noticed that most of those are pyarrow and its dependencies, which helped me to narrow this down even further from https://github.com/microsoft/LightGBM/issues/6509#issuecomment-2212334291.

Importing just pyarrow following by scikit-learn (which causes libgomp to be loaded) is sufficient to reproduce the error (in the environment described in https://github.com/microsoft/LightGBM/issues/6509#issuecomment-2212334291):

# no error
python -c "import pyarrow"

# no error
python -c "import sklearn"

# error: cannot allocate memory in static TLS block
python -c "import pyarrow; import sklearn"

Just switching the order is sufficient to avoid the error, at least in this case.

# no error
python -c "import sklearn; import pyarrow"

That matches findings from many of the threads linked in the description of this issue. For example, from https://github.com/pytorch/pytorch/issues/2575.

glibc has a table called the DTV. There is a slot for every dlopen’d library with TLS. Its use is not important for this discussion.

The DTV is resizable. However, in older versions of glibc, adding a library with static TLS will not resize the DTV, but do a conservative check that amounts to “have a I loaded more than 14 libraries with TLS”…

… changing import order can fix things, because if you change it in a way that loads all your “static TLS” libraries first, then future “dynamic TLS” libraries will resize the DTV like normal

  • It seems this issue was fixed by a glibc patch in 2014, which eliminates this check and lazily updates the DTV.*

It’s late in my timezone so I’m going to stop here for tonight. I’ll do some more testing soon. In short, I think that to get around this we should:

A couple of thoughts here:

  1. Are you installing scikit-learn from PyPI (a wheel?) If so, is it an option to install that from conda? The conda package should not bundle libgomp, and so should not have this problem.
  2. I wonder if scikit-learn has thought about name-mangling their copy of libgomp to avoid these issues. That would come at the cost of multiple copies of libgomp being loaded in one process, which may explode resource contention.

Probably no great answer right now, but all the more incentive to work on https://discuss.python.org/t/implementation-variants-rehashing-and-refocusing/54884!