Closed shinyano closed 1 month ago
Thanks for your issue! cc @qubvel in case you have bandwidth :)
Hi @shinyano, thanks for opening the issue!
I'm not sure about replacing torch.tensor()
with torch.from_numpy()
because I suspect there might be not only np.arrays but for example scalar values. However, we can resolve it by checking if the value is an array.
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?
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 š¤
@shinyano great! ping me for review when it's ready š¤
@qubvel I have created a PR, Would you like to review it?
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:https://github.com/huggingface/transformers/blob/5c1027bf09717f664b579e01cbb8ec3ef5aeb140/src/transformers/feature_extraction_utils.py#L139-L150
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.