-
Notifications
You must be signed in to change notification settings - Fork 28.1k
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
Replace torch.tensor()
with torch.from_numpy()
when processing numpy arrays
#33185
Comments
Thanks for your issue! cc @qubvel in case you have bandwidth :) |
Hi @shinyano, thanks for opening the issue! I'm not sure about replacing def as_tensor(value):
if isinstance(value, (list, tuple)) and len(value) > 0:
if isinstance(value[0], np.ndarray):
value = np.array(value)
elif (
isinstance(value[0], (list, tuple))
and len(value[0]) > 0
and isinstance(value[0][0], np.ndarray)
):
value = np.array(value)
# Modified code
if isinstance(value, np.ndarray):
tensor = torch.from_numpy(value)
else:
tensor = torch.tensor(value)
return tensor What do you think? Would you like to submit a PR to fix it? |
The modification is exactly what I wanted :). And I would be glad to submit the PR. |
@shinyano great! ping me for review when it's ready 🤗 |
System Info
transformers
version: 4.43.4Who can help?
@ArthurZucker @gante @Rocketknight1
Information
Tasks
examples
folder (such as GLUE/SQuAD, ...)Reproduction
I'm using Pemja, a C extension using multiple python threads, to execute python scripts from java. I use Blip models in my python script and it hangs when I create a new python thread to execute this script.
This is my minimal script:
The minimal Java code to call the script:
As written in the comment,
inputs = self.processor(images=image, return_tensors="pt")
would stuck. By importing source code, I found that the processor was stuck here at line 149 andvalue
is a numpy array:transformers/src/transformers/feature_extraction_utils.py
Lines 139 to 150 in 5c1027b
In research, I noticed similiar question had happened here: stackoverflow. And
torch.from_numpy()
works fine both in the stackoverflow link and in my case, as shown in the script above.Although this bug won't appear if I call the script directly from python, still It seems that using
torch.tensor()
on numpy arrays is not a reliable practice when multiple processing or threading is involved.So I would suggest that replacing
torch.tensor()
withtorch.from_numpy()
to process numpy arrays is a good idea. Please correct me if it's actually bad.Expected behavior
The script should not stuck and finish its work.
The text was updated successfully, but these errors were encountered: