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

fix: use torch.from_numpy() instead of torch.Tensor() to keep data type #2951

Merged
merged 3 commits into from Aug 31, 2022
Merged

fix: use torch.from_numpy() instead of torch.Tensor() to keep data type #2951

merged 3 commits into from Aug 31, 2022

Conversation

lsh918
Copy link
Contributor

@lsh918 lsh918 commented Aug 30, 2022

When running pytorch runner with async_run method in production mode(not in dev mode), tensors with other type than Float32 type is converted into Float32 type.(Ex. LongTensor is converted to FloatTensor) And I think this bug is originated by this line.

I made small test to check this issue. (numpy == 1.22.3 / torch == 1.12.1)

import torch
import numpy as np

arr = np.array([1, 2, 3, 4], dtype=np.int64)
t1 = torch.from_numpy(arr)
t2 = torch.Tensor(arr)

assert t1.dtype == torch.int64
assert t2.dtype == torch.float32

Thus I think changing torch.Tensor(ret) to torch.from_numpy(ret) should resolve this bug. Please tell me if it was a intended behavior.(i.e converting every tensor into Float32 tensor). Thank you!

@lsh918 lsh918 requested a review from a team as a code owner August 30, 2022 07:51
@lsh918 lsh918 requested review from ssheng and removed request for a team August 30, 2022 07:51
@parano parano requested a review from larme August 30, 2022 08:08
@codecov
Copy link

codecov bot commented Aug 30, 2022

Codecov Report

Merging #2951 (53ab5f0) into main (83d8a30) will decrease coverage by 1.62%.
The diff coverage is 0.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #2951      +/-   ##
==========================================
- Coverage   70.60%   68.97%   -1.63%     
==========================================
  Files         104      122      +18     
  Lines        9538    10135     +597     
==========================================
+ Hits         6734     6991     +257     
- Misses       2804     3144     +340     
Impacted Files Coverage Δ
bentoml/_internal/frameworks/common/pytorch.py 72.63% <0.00%> (+72.63%) ⬆️
bentoml/picklable_model.py 0.00% <0.00%> (ø)
bentoml/serve.py 0.00% <0.00%> (ø)
bentoml/pytorch.py 0.00% <0.00%> (ø)
bentoml/bentos.py 49.33% <0.00%> (ø)
bentoml/sklearn.py 0.00% <0.00%> (ø)
bentoml/start.py 0.00% <0.00%> (ø)
bentoml/torchscript.py 55.55% <0.00%> (ø)
bentoml/keras.py 0.00% <0.00%> (ø)
bentoml/tensorflow.py 0.00% <0.00%> (ø)
... and 15 more

@larme
Copy link
Member

larme commented Aug 31, 2022

LGTM, Thanks @lsh918 !

@larme larme merged commit ef67576 into bentoml:main Aug 31, 2022
@lsh918 lsh918 deleted the fix-pytorch-tensor-conversion branch August 31, 2022 02:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants