You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Apple M1/M2 Docker容器中SentenceTransformer段错误解决问询

Apple M1/M2设备Docker容器中SentenceTransformer调用段错误解决方案

经多台机器测试,此为Apple M1/M2专属问题。在Apple Silicon M2设备的Docker容器中运行Flask应用,调用SentenceTransformer的model.encode方法时触发Segmentation Fault(段错误),导致应用崩溃。

复现代码

from sentence_transformers import SentenceTransformer
model = SentenceTransformer('all-MiniLM-L6-v2')

# 待编码的句子
sentences = ['This framework generates embeddings for each input sentence',
    'Sentences are passed as a list of string.', 
    'The quick brown fox jumps over the lazy dog.']

# 调用model.encode编码句子
sentence_embeddings = model.encode(sentences)

错误回溯信息

Fatal Python error: Segmentation fault

Thread 0x0000ffff640ff1a0 (most recent call first):
  File "/usr/local/lib/python3.11/threading.py", line 331 in wait
  File "/usr/local/lib/python3.11/threading.py", line 629 in wait
  File "/usr/local/lib/python3.11/site-packages/tqdm/_monitor.py", line 60 in run
  File "/usr/local/lib/python3.11/threading.py", line 1045 in _bootstrap_inner
  File "/usr/local/lib/python3.11/threading.py", line 1002 in _bootstrap

Current thread 0x0000ffffa6814020 (most recent call first):
  File "/usr/local/lib/python3.11/site-packages/transformers/activations.py", line 78 in forward
  File "/usr/local/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1527 in _call_impl
  File "/usr/local/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1518 in _wrapped_call_impl
  File "/usr/local/lib/python3.11/site-packages/transformers/models/bert/modeling_bert.py", line 452 in forward
  File "/usr/local/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1527 in _call_impl
  File "/usr/local/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1518 in _wrapped_call_impl
  File "/usr/local/lib/python3.11/site-packages/transformers/models/bert/modeling_bert.py", line 551 in feed_forward_chunk
  File "/usr/local/lib/python3.11/site-packages/transformers/pytorch_utils.py", line 240 in apply_chunking_to_forward
  File "/usr/local/lib/python3.11/site-packages/transformers/models/bert/modeling_bert.py", line 539 in forward
  File "/usr/local/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1527 in _call_impl
  File "/usr/local/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1518 in _wrapped_call_impl
  File "/usr/local/lib/python3.11/site-packages/transformers/models/bert/modeling_bert.py", line 612 in forward
  File "/usr/local/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1527 in _call_impl
  File "/usr/local/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1518 in _wrapped_call_impl
  File "/usr/local/lib/python3.11/site-packages/transformers/models/bert/modeling_bert.py", line 1022 in forward
  File "/usr/local/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1527 in _call_impl
  File "/usr/local/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1518 in _wrapped_call_impl
  File "/usr/local/lib/python3.11/site-packages/sentence_transformers/models/Transformer.py", line 66 in forward
  File "/usr/local/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1527 in _call_impl
  File "/usr/local/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1518 in _wrapped_call_impl
  File "/usr/local/lib/python3.11/site-packages/torch/nn/modules/container.py", line 215 in forward
  File "/usr/local/lib/python3.11/site-packages/sentence_transformers/SentenceTransformer.py", line 165 in encode
  File "<stdin>", line 1 in <module>

Extension modules: numpy.core._multiarray_umath, numpy.core._multiarray_tests, numpy.linalg._umath_linalg, numpy.fft._pocketfft_internal, numpy.random._common, numpy.random.bit_generator, numpy.random._bounded_integers, numpy.random._mt19937, numpy.random.mtrand, numpy.random._philox, numpy.random._pcg64, numpy.random._sfc64, numpy.random._generator, torch._C, torch._C._fft, torch._C._linalg, torch._C._nested, torch._C._nn, torch._C._sparse, torch._C._special, scipy._lib._ccallback_c, scipy.sparse._sparsetools, _csparsetools, scipy.sparse._csparsetools, scipy.sparse.linalg._isolve._iterative, scipy.linalg._fblas, scipy.linalg._flapack, scipy.linalg.cython_lapack, scipy.linalg._cythonized_array_utils, scipy.linalg._solve_toeplitz, scipy.linalg._decomp_lu_cython, scipy.linalg._matfuncs_sqrtm_triu, scipy.linalg.cython_blas, scipy.linalg._matfuncs_expm, scipy.linalg._decomp_update, scipy.linalg._flinalg, scipy.sparse.linalg._dsolve._superlu, scipy.sparse.linalg._eigen.arpack._arpack, scipy.sparse.csgraph._tools, scipy.sparse.csgraph._shortest_path, scipy.sparse.csgraph._traversal, scipy.sparse.csgraph._min_spanning_tree, scipy.sparse.csgraph._flow, scipy.sparse.csgraph._matching, scipy.sparse.csgraph._reordering, scipy.spatial._ckdtree, scipy._lib.messagestream, scipy.spatial._qhull, scipy.spatial._voronoi, scipy.spatial._distance_wrap, scipy.spatial._hausdorff, scipy.special._ufuncs_cxx, scipy.special._ufuncs, scipy.special._specfun, scipy.special._comb, scipy.special._ellip_harm_2, scipy.spatial.transform._rotation, scipy.ndimage._nd_image, _ni_label, scipy.ndimage._ni_label, scipy.optimize._minpack2, scipy.optimize._group_columns, scipy.optimize._trlib._trlib, scipy.optimize._lbfgsb, _moduleTNC, scipy.optimize._moduleTNC, scipy.optimize._cobyla, scipy.optimize._slsqp, scipy.optimize._minpack, scipy.optimize._lsq.givens_elimination, scipy.optimize._zeros, scipy.optimize.__nnls, scipy.optimize._highs.cython.src._highs_wrapper, scipy.optimize._highs._highs_wrapper, scipy.optimize._highs.cython.src._highs_constants, scipy.optimize._highs._highs_constants, scipy.linalg._interpolative, scipy.optimize._bglu_dense, scipy.optimize._lsap, scipy.optimize._direct, scipy.integrate._odepack, scipy.integrate._quadpack, scipy.integrate._vode, scipy.integrate._dop, scipy.integrate._lsoda, scipy.special.cython_special, scipy.stats._stats, scipy.stats.beta_ufunc, scipy.stats._boost.beta_ufunc, scipy.stats.binom_ufunc, scipy.stats._boost.binom_ufunc, scipy.stats.nbinom_ufunc, scipy.stats._boost.nbinom_ufunc, scipy.stats.hypergeom_ufunc, scipy.stats._boost.hypergeom_ufunc, scipy.stats.ncf_ufunc, scipy.stats._boost.ncf_ufunc, scipy.stats.ncx2_ufunc, scipy.stats._boost.ncx2_ufunc, scipy.stats.nct_ufunc, scipy.stats._boost.nct_ufunc, scipy.stats.skewnorm_ufunc, scipy.stats._boost.skewnorm_ufunc, scipy.stats.invgauss_ufunc, scipy.stats._boost.invgauss_ufunc, scipy.interpolate._fitpack, scipy.interpolate.dfitpack, scipy.interpolate._bspl, scipy.interpolate._ppoly, scipy.interpolate.interpnd, scipy.interpolate._rbfinterp_pythran, scipy.interpolate._rgi_cython, scipy.stats._biasedurn, scipy.stats._levy_stable.levyst, scipy.stats._stats_pythran, scipy._lib._uarray._uarray, scipy.stats._statlib, scipy.stats._sobol, scipy.stats._qmc_cy, scipy.stats._mvn, scipy.stats._rcont.rcont, regex._regex, sklearn.__check_build._check_build, sklearn.utils._isfinite, sklearn.utils.murmurhash, sklearn.utils._openmp_helpers, sklearn.utils._logistic_sigmoid, sklearn.utils.sparsefuncs_fast, sklearn.preprocessing._csr_polynomial_expansion, sklearn.preprocessing._target_encoder_fast, sklearn.utils._vector_sentinel, sklearn.feature_extraction._hashing_fast, sklearn.utils._random, sklearn.utils._seq_dataset, sklearn.metrics.cluster._expected_mutual_info_fast, sklearn.metrics._dist_metrics, sklearn.metrics._pairwise_distances_reduction._datasets_pair, sklearn.utils._cython_blas, sklearn.metrics._pairwise_distances_reduction._base, sklearn.metrics._pairwise_distances_reduction._middle_term_computer, sklearn.utils._heap, sklearn.utils._sorting, sklearn.metrics._pairwise_distances_reduction._argkmin, sklearn.metrics._pairwise_distances_reduction._argkmin_classmode, sklearn.metrics._pairwise_distances_reduction._radius_neighbors, sklearn.metrics._pairwise_fast, sklearn.linear_model._cd_fast, sklearn._loss._loss, sklearn.utils.arrayfuncs, sklearn.svm._liblinear, sklearn.svm._libsvm, sklearn.svm._libsvm_sparse, sklearn.utils._weight_vector, sklearn.linear_model._sgd_fast, sklearn.linear_model._sag_fast, scipy.io.matlab._mio_utils, scipy.io.matlab._streams, scipy.io.matlab._mio5_utils, sklearn.datasets._svmlight_format_fast, charset_normalizer.md, yaml._yaml, sentencepiece._sentencepiece, PIL._imaging (total: 163)
Segmentation fault

已排查情况

  • Docker容器已分配充足内存
  • 已更新torch、transformers、sentence-transformers等依赖库
  • 相同代码在Docker环境外可正常运行

当前pip包版本列表

Package               Version
--------------------- ---------
blinker               1.6.3
certifi               2023.7.22
charset-normalizer    3.3.0
click                 8.1.7
filelock              3.12.4
Flask                 3.0.0
fsspec                2023.9.2
huggingface-hub       0.17.3
idna                  3.4
itsdangerous          2.1.2
Jinja2                3.1.2
joblib                1.3.2
MarkupSafe            2.1.3
mpmath                1.3.0
networkx              3.1
nltk                  3.8.1
numpy                 1.26.0
packaging             23.2
Pillow                10.0.1
pip                   23.2.1
PyYAML                6.0.1
regex                 2023.10.3
requests              2.31.0
safetensors           0.4.0
scikit-learn          1.3.1
scipy                 1.11.3
sentence-transformers 2.2.2
sentencepiece         0.1.99
setuptools            65.5.1
sympy                 1.12
threadpoolctl         3.2.0
tokenizers            0.14.1
torch                 2.1.0
torchvision           0.16.0
tqdm                  4.66.1
transformers          4.34.0
typing_extensions     4.8.0
urllib3               2.0.6
Werkzeug              3.0.0
wheel                 0.41.2

Dockerfile核心内容

FROM python:3.11

RUN pip install --upgrade pip
RUN pip install Flask==3.0.0 sentence-transformers==2.2.2

解决方案

1. 使用Apple Silicon专属Python镜像

默认python:3.11为x86架构镜像,在M1/M2上依赖Rosetta转译易引发兼容性问题,改用arm64原生镜像:

FROM python:3.11-slim-bookworm

2. 安装Apple Silicon优化的PyTorch

替换Dockerfile中的依赖安装命令,安装适配arm64的PyTorch版本:

RUN pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu

3. 强制单线程运行

段错误可能与多线程兼容性有关,调用model.encode时指定单线程参数:

sentence_embeddings = model.encode(sentences, show_progress_bar=False, device='cpu', batch_size=1)

或容器启动时设置环境变量:

export OMP_NUM_THREADS=1

4. 降级依赖版本

部分新版本依赖在arm64 Docker环境存在兼容性问题,尝试降级核心库:

RUN pip install Flask==3.0.0 torch==2.0.1 transformers==4.33.3 sentence-transformers==2.2.2

5. 优化Rosetta转译设置

若必须使用x86镜像,在Docker Desktop中启用"Use Rosetta for x86/amd64 emulation on Apple Silicon",并调整容器资源:

docker run -e OMP_NUM_THREADS=1 --cpus 4 --memory 8g your-image

内容的提问来源于stack exchange,提问作者Pavol Travnik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 21:55:54