-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Multi-threaded generator GPU usage is very low #3026
Comments
If you're just trying to tokenize, do If you do need to run the models, you could also try using multiprocessing to feed the GPU with more tasks. Here's an example script that reads from stdin, predicts head indices in multiple processes, and prints the results to stdout in order. import sys
from cytoolz import partition_all
from pathlib import Path
import plac
import spacy
from joblib import Parallel, delayed
@plac.annotations(
model=("Model name (needs tagger)", "positional", None, str),
n_jobs=("Number of workers", "option", "n", int),
batch_size=("Batch-size for each process", "option", "b", int),
)
def main(model, n_jobs=5, batch_size=2000):
texts = (ujson.loads(line)['text'] for line in sys.stdin)
# Split into outer-batches
partitions = partition_all(batch_size, texts)
for partition in partition_all(batch_size*10, texts):
# Parallelise within the outer-batch, and collate the results
executor = Parallel(n_jobs=n_jobs)
do = delayed(predict_heads)
tasks = (do(model, subpart) for subpart in partition_all(batch_size//10, partition))
results = executor(tasks)
# Print when complete
for result_group in results:
for record in result_group:
print(record)
def predict_heads(model_name, texts):
nlp = spacy.load(model_name)
nlp.disable_pipes('tagger', 'ner')
output = []
for doc in nlp.pipe(texts):
heads = [token.head.i - token.i for token in doc]
tokens = [token.text for token in doc]
output.append(ujson.dumps({'text': doc.text, 'heads': heads, 'tokens': tokens}))
return output
if __name__ == '__main__':
import socket
try:
BrokenPipeError
except NameError:
BrokenPipeError = socket.error
try:
plac.call(main)
except BrokenPipeError:
import os, sys
# Python flushes standard streams on exit; redirect remaining output
# to devnull to avoid another BrokenPipeError at shutdown
devnull = os.open(os.devnull, os.O_WRONLY)
os.dup2(devnull, sys.stdout.fileno())
sys.exit(1) # Python exits with error code 1 on EPIPE |
Thank you @honnibal I was hoping to bypass the CPU restriction of #1508 by parallelizing It seems like Is the I am actually extracting tokens & POS tags at the sentence level. I disabled 'ner' & 'parser' to get some speedup.
|
@harsham05 The In v2.1.x ( In summary, for now:
Closing now to merge discussion with #2075 |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
How to reproduce the behaviour
I am trying to tokenize documents using the multi threaded generator, but I am not seeing any speedup on the GPU compared to the CPU.
The GPU utilization seems to be only 6-7 % when monitoring
watch -d -n 0.5 nvidia-smi
I have installed spacy using
pip install -U spacy[cuda92]
Your Environment
The text was updated successfully, but these errors were encountered: