Skip to content

Makes prediction work on GPUs#149

Open
dirkgr wants to merge 4 commits into
huggingface:masterfrom
dirkgr:GpuFix
Open

Makes prediction work on GPUs#149
dirkgr wants to merge 4 commits into
huggingface:masterfrom
dirkgr:GpuFix

Conversation

@dirkgr

@dirkgr dirkgr commented Apr 9, 2019

Copy link
Copy Markdown

When you use numpy.zeros to create the embed_arr, you can't later add it to embed_vector, because embed_vector might not be a numpy array. This re-works the code such that the array type of embed_vector is preserved all the way through.

I stole this approach from explosion/spaCy#3362. Thanks, @danielkingai2!

@dirkgr

dirkgr commented Apr 9, 2019

Copy link
Copy Markdown
Author

To help those who Google, the error message you get ends with

  File "neuralcoref.pyx", line 596, in neuralcoref.neuralcoref.NeuralCoref.__call__
  File "neuralcoref.pyx", line 723, in neuralcoref.neuralcoref.NeuralCoref.predict
  File "neuralcoref.pyx", line 913, in neuralcoref.neuralcoref.NeuralCoref.get_mention_embeddings
  File "neuralcoref.pyx", line 904, in neuralcoref.neuralcoref.NeuralCoref.get_average_embedding
  File "cupy/core/core.pyx", line 1238, in cupy.core.core.ndarray.__array_ufunc__
  File "cupy/core/_kernel.pyx", line 816, in cupy.core._kernel.ufunc.__call__
  File "cupy/core/_kernel.pyx", line 99, in cupy.core._kernel._preprocess_args
TypeError: Unsupported type <class 'numpy.ndarray'>

Though it does seem there are other problems too when you run on GPU.

@dirkgr dirkgr changed the title Makes this work on GPUs Makes prediction work on GPUs Apr 9, 2019
@dirkgr

dirkgr commented Apr 9, 2019

Copy link
Copy Markdown
Author

I fixed all the other problems I am aware of at this point. On my machine, it runs about 8x faster on one GPU.

import numpy
try:
import cupy
to_numpy = cupy.asnumpy

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can avoid this dependency check by relying on Thinc for that.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you feel about this?

def to_numpy(a):
    if thinc.neural.util.is_cupy_array(a):
        import cupy
        return cupy.asnumpy(a)
    else:
        return a

That way there isn't a conditional import, but we still have to import cupy. I'm not that familiar with thinc, but the thinc source does not use cupy.asnumpy() anywhere, so there probably isn't a good wrapper.

cdef int n = 0
embed_arr = numpy.zeros(self.static_vectors.shape[1], dtype='float32')
for token in span:
if token.lower not in PUNCTS:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove these?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is all about removing the call to numpy.zeros(). Once I had replaced it with sum(), the code collapsed into just those two lines. Other than the location of the output vector, it should perform exactly the same way.

@HarshTrivedi

Copy link
Copy Markdown

@thomwolf Can you take a look at this PR? I am trying to use neuralcoref on a large corpus but w/o GPU it's taking too much of time.

@HarshTrivedi

Copy link
Copy Markdown

I could get it to work on GPU using this fix, thanks @dirkgr!

@jqueguiner

jqueguiner commented Jun 21, 2019

Copy link
Copy Markdown

Hi guys this is awesome work is it possible to merge the PR ?

@stale

stale Bot commented Aug 31, 2019

Copy link
Copy Markdown

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale Bot added the wontfix label Aug 31, 2019
@stale stale Bot closed this Sep 7, 2019
@dirkgr

dirkgr commented Sep 14, 2019

Copy link
Copy Markdown
Author

Hi guys this is awesome work is it possible to merge the PR ?

Looks like the answer is "no".

@svlandeg svlandeg added gpu and removed wontfix labels Oct 14, 2019
@svlandeg svlandeg reopened this Oct 14, 2019
@svlandeg

svlandeg commented Oct 15, 2019

Copy link
Copy Markdown
Collaborator

Sorry for the late response to this. The PR closed automatically but I think this is valuable work so I reopened. We're probably going to work on a closer integration with spaCy & thinc too.

@stale

stale Bot commented Dec 14, 2019

Copy link
Copy Markdown

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale Bot added the wontfix label Dec 14, 2019
@svlandeg svlandeg removed the wontfix label Dec 17, 2019
@jkhalsa-arabesque

Copy link
Copy Markdown

Any updates on this?

@m1

m1 commented Sep 2, 2020

Copy link
Copy Markdown

Would love some updates on this!

@svlandeg

svlandeg commented Sep 7, 2020

Copy link
Copy Markdown
Collaborator

I want to keep this PR open as the code may be useful for those who want to build from source and try this out.

However moving forward, when spaCy v.3 will be released, we'll update this code significantly to be compatible with Thinc 8. At that point, GPU support will be automatic...

@sreyemnayr

Copy link
Copy Markdown

For those trying to make the pipeline work with GPU support on spaCy > 2.1, here's the additional step for patching prior to installing from source (after activating your venv and pip installing spaCy):

git clone https://github.com/huggingface/neuralcoref.git
cd neuralcoref
git fetch origin pull/149/head:gpufix
git checkout gpufix
pip install -r requirements.txt
pip install -e .

@c0stya

c0stya commented Feb 2, 2021

Copy link
Copy Markdown

Any progress on spaCy v.3 integration?

@svlandeg

svlandeg commented Feb 2, 2021

Copy link
Copy Markdown
Collaborator

You mean the v3 that was released yesterday? ;-)

It's definitely on our roadmap, but it's not the only thing we're working on ;-)

@LifeIsStrange

Copy link
Copy Markdown

@svlandeg Friendly ping :)

@svlandeg

svlandeg commented Feb 8, 2022

Copy link
Copy Markdown
Collaborator

Hi! Please refer to #295 (comment) for more info :-)

@LifeIsStrange

Copy link
Copy Markdown

@svlandeg Thx for the update, I'm curious wether the updated implementation target the latest state of the art (cf #334 ) AKA 81% accuracy on Ontonotes (or at least 79% since the second paper is quite old already)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants