Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Makes prediction work on GPUs #149

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Conversation

dirkgr
Copy link

@dirkgr dirkgr commented Apr 9, 2019

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
Copy link
Author

dirkgr commented Apr 9, 2019

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
Copy link
Author

dirkgr commented Apr 9, 2019

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

@@ -20,6 +20,13 @@ import array
from libc.stdint cimport uint16_t, uint32_t, uint64_t, uintptr_t, int32_t

import numpy
try:
import cupy
to_numpy = cupy.asnumpy
Copy link
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
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
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
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

@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

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

@jqueguiner
Copy link

jqueguiner commented Jun 21, 2019

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

@stale
Copy link

stale bot commented Aug 31, 2019

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
Copy link
Author

dirkgr commented Sep 14, 2019

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
Copy link
Collaborator

svlandeg commented Oct 15, 2019

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
Copy link

stale bot commented Dec 14, 2019

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

Any updates on this?

@m1
Copy link

m1 commented Sep 2, 2020

Would love some updates on this!

@svlandeg
Copy link
Collaborator

svlandeg commented Sep 7, 2020

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

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
Copy link

c0stya commented Feb 2, 2021

Any progress on spaCy v.3 integration?

@svlandeg
Copy link
Collaborator

svlandeg commented Feb 2, 2021

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

@svlandeg Friendly ping :)

@svlandeg
Copy link
Collaborator

svlandeg commented Feb 8, 2022

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

@LifeIsStrange
Copy link

@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.

None yet

10 participants