0

I've observed that nlp.pipe is 30-40% slower on my almost brand new M1 Pro Macbook than on my old Macbook Pro from 2017. Most other functions are faster on the M1 by a similar margin, so this is not the performance I would expect.

For a benchmark, I'm running the following code (with scispacy):

import os
from pathlib import Path
import re
import spacy
import time


with open(Path(os.path.expanduser('~')) / 'Downloads' / 'icd10cm-table and index-April 1 2023' / 'icd10cm-tabular-April 1 2023.xml') as f:
    content = f.read()
matches = re.findall(r'<desc>([^<]*)</desc>', content)

nlp = spacy.load('en_core_sci_lg')
start_time = time.time()
for x in nlp.pipe(matches):
    pass
print('%s seconds elapsed' % (time.time() - start_time))

My M1 Mac takes over 75 seconds to complete the task, while my 2017 Intel Mac can do it in 46 seconds.

I don't know whether spacy uses numpy, but I installed a fast version of numpy using ABarrier's answer to this question. That made numpy faster, but made no difference for spacy. I'm assuming that somewhere there is an unoptimized binary being used, but I don't know how to figure out what it is.


Instructions to replicate my benchmark:

You can get the file I'm using here (cdc.gov); it's a table of ICD-10 concepts in XML format. If you don't have scispacy or don't have the en_core_sci_lg model, create an environment and run

pip install scispacy
pip install https://s3-us-west-2.amazonaws.com/ai2-s2-scispacy/releases/v0.5.1/en_core_sci_lg-0.5.1.tar.gz
Zorgoth
  • 499
  • 3
  • 9

1 Answers1

2

Install the package thinc-apple-ops:

pip install thinc-apple-ops

Or through the spacy extra:

pip install 'spacy[apple]'

(The unoptimized package is blis. We'd like to be able to switch to a newer version of BLIS with M1 support, but there are still some open bugs.)

aab
  • 10,858
  • 22
  • 38
  • This worked to make spaCy fast (13 seconds from 75!), but it also force-overwrites numpy, which is a shame because my `conda install -c conda-forge numpy "libblas=*=*accelerate` version of numpy is twice as fast. I can overwrite that numpy by running the command again, and it doesn't prevent spaCy from working, at least on my trivial benchmark, but the fast numpy performance doesn't come back. Do you have any idea how I could have fast numpy and fast spaCy coexisting in the same environment? – Zorgoth Mar 13 '23 at 19:41
  • 1
    If you have the dev requirements installed, you should be able compile it against a custom numpy install by using something like `pip install thinc-apple-ops --no-build-isolation --no-binary thinc-apple-ops`. – aab Mar 14 '23 at 07:41