Skip to content

refactor(framework): pytorch splitting refactor #2317

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

Merged
merged 21 commits into from
Feb 28, 2022

Conversation

larme
Copy link
Member

@larme larme commented Feb 25, 2022

Description

Motivation and Context

How Has This Been Tested?

Checklist:

  • My code follows the bentoml code style, both make format and
    make lint script have passed
    (instructions).
  • My change reduces project test coverage and requires unit tests to be added
  • I have added unit tests covering my code change
  • My change requires a change to the documentation
  • I have updated the documentation accordingly

@pep8speaks
Copy link

pep8speaks commented Feb 25, 2022

Hello @larme, Thanks for updating this PR.

Line 20:1: F401 '.common.pytorch.PyTorchTensorContainer' imported but unused

Line 16:1: F811 redefinition of unused 'torch' from line 4

Line 15:1: F401 '...exceptions.MissingDependencyException' imported but unused

Comment last updated at 2022-02-28 11:25:55 UTC

@codecov
Copy link

codecov bot commented Feb 25, 2022

Codecov Report

Merging #2317 (b1cec3e) into main (1826fd1) will increase coverage by 3.56%.
The diff coverage is 88.33%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #2317      +/-   ##
==========================================
+ Coverage   45.24%   48.81%   +3.56%     
==========================================
  Files         106      134      +28     
  Lines        8281     8420     +139     
==========================================
+ Hits         3747     4110     +363     
+ Misses       4534     4310     -224     
Impacted Files Coverage Δ
bentoml/pytorch.py 0.00% <0.00%> (ø)
bentoml/_internal/frameworks/pytorch.py 90.38% <83.33%> (+25.09%) ⬆️
bentoml/_internal/frameworks/pytorch_lightning.py 93.61% <84.61%> (+21.39%) ⬆️
bentoml/_internal/frameworks/common/pytorch.py 87.61% <87.61%> (ø)
bentoml/_internal/frameworks/torchscript.py 93.18% <93.18%> (ø)
bentoml/__init__.py 100.00% <100.00%> (ø)
bentoml/torchscript.py 100.00% <100.00%> (ø)
bentoml/transformers.py 0.00% <0.00%> (ø)
bentoml/detectron.py 0.00% <0.00%> (ø)
... and 31 more

Copy link
Contributor

@aarnphm aarnphm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are still some small and quick fix needed

@@ -43,8 +33,6 @@
""" # noqa
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this block anymore since you already do an import check under .common.pytorch

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking about just keeping these blocks in case one day we remove torch import from .common.pytorch. What's your opinion? @aarnphm

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm why would we do so? I don't think we would ever do such things right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot 2022-02-26 at 17 43 02

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a change to import torch from .common.pytorch directly @aarnphm

@aarnphm
Copy link
Contributor

aarnphm commented Feb 25, 2022

You also have to add flags for torchscript to codecov.yml

@aarnphm
Copy link
Contributor

aarnphm commented Feb 26, 2022

the PyTorchContainer is failing the test, and for some reason the torchscript integration test is not running ?

@larme
Copy link
Member Author

larme commented Feb 28, 2022

the PyTorchContainer is failing the test, and for some reason the torchscript integration test is not running ?

Let's fix this after batch_axis is redone. Current fix is to disable testing for batch_axis=1

def _mapping(item: t.Union["ext.NpNDArray", torch.Tensor]) -> torch.Tensor:
if LazyType["ext.NpNDArray"]("numpy.ndarray").isinstance(item):
item = torch.Tensor(item, device=self._device_id)
return item
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's already a tensor, we need to call to(device) on it as well, right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

init with self._device_id so no need to call to(device)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aarnphm @bojiang I think you mean in self._configure() we have set default tensor storage type? But if the tensor is deserialized from DataContainer payload which is serialized in another machine and these 2 machine have mismatched device, then the tensor won't be converted to the proper device automatically. Also in my simple benchmark to(device) won't cost a lot if from_device and to_device is the same

Here's my simple demo:

https://gist.github.com/larme/7640f2d97d45c92946f1573b3eaaa0be

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think calling to shouldn't affect perf that much. I think we just need to make sure that device is consistent across, so that #2230 won't happen.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this change will make device more consistent?

Copy link
Contributor

@aarnphm aarnphm Feb 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good then, lets do that

aarnphm
aarnphm previously approved these changes Feb 28, 2022
@aarnphm
Copy link
Contributor

aarnphm commented Feb 28, 2022

paddlepaddle test will be addressed in a separate PR, seems like it just randomly fails all the time when using paddlehub module.

@bojiang bojiang merged commit c864e44 into bentoml:main Feb 28, 2022
aarnphm added a commit to aarnphm/BentoML that referenced this pull request Jul 29, 2022
* refactor(framework): split pytorch impl to pytorch and torchscript

* style: reformat

* style: minor fix

* doc: fix pytorch/torchscript changes

* chore: add torchscript github workflow

* style: minor fix

* Update bentoml/_internal/frameworks/common/pytorch.py

Co-authored-by: Aaron Pham <[email protected]>

* Update bentoml/_internal/frameworks/common/pytorch.py

Co-authored-by: Aaron Pham <[email protected]>

* Update bentoml/_internal/frameworks/common/pytorch.py

Co-authored-by: Aaron Pham <[email protected]>

* Update bentoml/_internal/frameworks/common/pytorch.py

Co-authored-by: Aaron Pham <[email protected]>

* Update bentoml/_internal/frameworks/pytorch.py

Co-authored-by: Aaron Pham <[email protected]>

* Update bentoml/_internal/frameworks/common/pytorch.py

Co-authored-by: Aaron Pham <[email protected]>

* Update bentoml/_internal/frameworks/common/pytorch.py

Co-authored-by: Aaron Pham <[email protected]>

* Update bentoml/_internal/frameworks/common/pytorch.py

Co-authored-by: Aaron Pham <[email protected]>

* style: minor fix

* test: pytorch frameworks test save format

* chore: update codecov.yml for torchscript

* chore: github workflow add torchscript

* test(framework): disable pytorch container batch_axis=1 test

* chore: remove double import

* fix: pytorch tensor to device

Co-authored-by: Aaron Pham <[email protected]>
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.

4 participants