M1加速深度学习:HanLP正式支持苹果芯GPU

原文链接: https://www.hankcs.com/nlp/hanlp-official-m1-support.html

今天我的MBP M1MAX终于寄到了,于是第一时间为HanLP提供M1的原生CPU+GPU支持。MBP用户从此…

正好M1 Max也到了,跟上大佬的脚步 :grinning:

1 Like

最近PyTorch在预览版中添加了对M1的GPU支持,HanLP也刚发布了适配,此博文已经更新了最新的安装方法,欢迎大家体验。

请问是不是不兼容tensorflow-macos 2.9.2?我之前安装过2.6.0,运行示例是正常的,最近升级了包以后提示:

Failed to load https://file.hankcs.com/hanlp/mtl/close_tok_pos_ner_srl_dep_sdp_con_electra_base_20210111_124519.zip.
If the problem still persists, please submit an issue to https://github.com/hankcs/HanLP/issues
When reporting an issue, make sure to paste the FULL ERROR LOG below.
================================ERROR LOG BEGINS================================
OS: macOS-12.4-arm64-arm-64bit
Python: 3.9.7
PyTorch: 1.11.0
HanLP: 2.1.0-beta.30
Traceback (most recent call last):
  File "/Users/ern/Developer/python/test2.py", line 4, in <module>
    HanLP = hanlp.load(
  File "/Users/ern/Developer/miniforge3/lib/python3.9/site-packages/hanlp/__init__.py", line 43, in load
    return load_from_meta_file(save_dir, 'meta.json', verbose=verbose, **kwargs)
  File "/Users/ern/Developer/miniforge3/lib/python3.9/site-packages/hanlp/utils/component_util.py", line 175, in load_from_meta_file
    raise e from None
  File "/Users/ern/Developer/miniforge3/lib/python3.9/site-packages/hanlp/utils/component_util.py", line 92, in load_from_meta_file
    obj: Component = object_from_classpath(cls)
  File "/Users/ern/Developer/miniforge3/lib/python3.9/site-packages/hanlp_common/reflection.py", line 27, in object_from_classpath
    classpath = str_to_type(classpath)
  File "/Users/ern/Developer/miniforge3/lib/python3.9/site-packages/hanlp_common/reflection.py", line 44, in str_to_type
    cls = getattr(importlib.import_module(module_name), class_name)
  File "/Users/ern/Developer/miniforge3/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/Users/ern/Developer/miniforge3/lib/python3.9/site-packages/hanlp/components/mtl/multi_task_learning.py", line 27, in <module>
    from hanlp.components.mtl.tasks import Task
  File "/Users/ern/Developer/miniforge3/lib/python3.9/site-packages/hanlp/components/mtl/tasks/__init__.py", line 23, in <module>
    from hanlp.transform.transformer_tokenizer import TransformerSequenceTokenizer
  File "/Users/ern/Developer/miniforge3/lib/python3.9/site-packages/hanlp/transform/transformer_tokenizer.py", line 9, in <module>
    from hanlp.layers.transformers.pt_imports import PreTrainedTokenizer, PretrainedConfig, AutoTokenizer_
  File "/Users/ern/Developer/miniforge3/lib/python3.9/site-packages/hanlp/layers/transformers/pt_imports.py", line 13, in <module>
    from transformers import BertTokenizer, BertConfig, PretrainedConfig, \
  File "/Users/ern/Developer/miniforge3/lib/python3.9/site-packages/transformers/__init__.py", line 30, in <module>
    from . import dependency_versions_check
  File "/Users/ern/Developer/miniforge3/lib/python3.9/site-packages/transformers/dependency_versions_check.py", line 17, in <module>
    from .utils.versions import require_version, require_version_core
  File "/Users/ern/Developer/miniforge3/lib/python3.9/site-packages/transformers/utils/__init__.py", line 46, in <module>
    from .hub import (
  File "/Users/ern/Developer/miniforge3/lib/python3.9/site-packages/transformers/utils/hub.py", line 39, in <module>
    from huggingface_hub import HfFolder, Repository, create_repo, list_repo_files, whoami
  File "/Users/ern/Developer/miniforge3/lib/python3.9/site-packages/huggingface_hub/__init__.py", line 70, in <module>
    from .keras_mixin import (
  File "/Users/ern/Developer/miniforge3/lib/python3.9/site-packages/huggingface_hub/keras_mixin.py", line 27, in <module>
    import tensorflow as tf
  File "/Users/ern/Developer/miniforge3/lib/python3.9/site-packages/tensorflow/__init__.py", line 443, in <module>
    _ll.load_library(_plugin_dir)
  File "/Users/ern/Developer/miniforge3/lib/python3.9/site-packages/tensorflow/python/framework/load_library.py", line 151, in load_library
    py_tf.TF_LoadLibrary(lib)
tensorflow.python.framework.errors_impl.NotFoundError: dlopen(/Users/ern/Developer/miniforge3/lib/python3.9/site-packages/tensorflow-plugins/.!62456!libmetal_plugin.dylib, 0x0006): tried: '/Users/ern/Developer/miniforge3/lib/python3.9/site-packages/tensorflow-plugins/.!62456!libmetal_plugin.dylib' ()

[Done] exited with code=1 in 20.884 seconds

运行的是示例:

import hanlp
from hanlp_common.document import Document

HanLP = hanlp.load(
    hanlp.pretrained.mtl.CLOSE_TOK_POS_NER_SRL_DEP_SDP_CON_ELECTRA_BASE_ZH)
print(HanLP.device)

其中“/close_tok_pos_ner_srl_dep_sdp_con_electra_base_20210111_124519”在我的HANLP_HOME里是有的(现在看到是解压缩的形态)。

求助,谢谢

OS:macOS 12.4
M1 Pro的MBP

TF的兼容性很差,所以:

NEVER install thirdparty packages (TensorFlow/fastText etc.) by yourself, as higher or lower versions of thirparty packages have not been tested and might not work properly.

感谢,那我还是重装一下环境吧

按您的帖子重装了TF 2.6.0和后续步骤以后,能正常运行出mps:0的结果,但运行这段示例:

import hanlp
from hanlp_common.document import Document

HanLP = hanlp.load(
    hanlp.pretrained.mtl.CLOSE_TOK_POS_NER_SRL_DEP_SDP_CON_ELECTRA_BASE_ZH)
#print(HanLP.device)
doc: Document = HanLP(['2021年HanLPv2.1为生产环境带来次世代最先进的多语种NLP技术。', '阿婆主来到北京立方庭参观自然语义科技公司。'])
doc.pretty_print()

会提示报错

/AppleInternal/Library/BuildRoots/b6051351-c030-11ec-96e9-3e7866fcf3a1/Library/Caches/com.apple.xbs/Sources/MetalPerformanceShadersGraph/mpsgraph/MetalPerformanceShadersGraph/Core/Files/MPSGraphUtilities.mm:343: failed assertion `unsupported datatype for constant’
[Done] exited with code=null in 4.357 seconds

是还有什么问题吗?谢谢

PyTorch测试版还是不稳定,已更新说明:M1加速深度学习:HanLP正式支持苹果芯GPU-码农场

谢谢,还是等正式版吧

今天无意中看到一个解释,是数据类型的兼容问题。

1 Like

谢谢分享,我好像没有用float16或64,应该还有其他原因。

在 安装tensorflow-macos 步骤时出现这个报错应该怎么办呀

ERROR: Could not find a version that satisfies the requirement tensorflow-macos==2.6.0 (from versions: 2.9.0, 2.9.1, 2.9.2, 2.10.0, 2.11.0)

ERROR: No matching distribution found for tensorflow-macos==2.6.0