bentoml / BentoML

The easiest way to serve AI apps and models - Build Model Inference APIs, Job queues, LLM apps, Multi-model pipelines, and more!
https://bentoml.com
Apache License 2.0
7.17k stars 792 forks source link

Relative imports are not recognized properly as local python dependencies #1097

Closed pietermarsman closed 2 years ago

pietermarsman commented 4 years ago

Describe the bug

Local python modules should be copied to the packed bento docker (the thing in ~/bentoml/repository, not sure how to call it). But relative imports are not recognized properly as local python dependencies.

To Reproduce

Have a package structure like this:

src
├── __init__.py
├── bento_service.py
└──  util.py

and import something from the util.py:

from .util import util_function

The util.py file is not copied to the bento docker.

Expected behavior

The number of bento docker files should be exactly the same when using relative and absolute imports.

Screenshots/Logs

Output logs of saving the BentoService with relative imports:

[2020-09-16 11:15:14,795] WARNING - auto_pip_dependencies enabled, it may override package versions specified in `pip_dependencies=['torch', 'imantics', 'shapely']`
[2020-09-16 11:15:15,879] DEBUG - Creating local YataiService instance
[2020-09-16 11:15:15,968] DEBUG - Upgrading tables to the latest revision
[2020-09-16 11:15:15,972] DEBUG - Created temporary directory: /tmp/bentoml-temp-3_1cm4o2
[2020-09-16 11:15:16,980] DEBUG - copy_local_py_modules target_module_name: segmentation_model_inference.bento_service, target_module_file: /home/pieter/projects/segmentation-model-inference/segmentation_model_inference/bento_service.py
[2020-09-16 11:15:16,980] DEBUG - Searching for local dependant modules of segmentation_model_inference.bento_service:/home/pieter/projects/segmentation-model-inference/segmentation_model_inference/bento_service.py
[2020-09-16 11:15:16,981] DEBUG - Find bad module imports that can not be parsed properly: dict_keys(['logging', 're', 'typing', 'numpy', 'torch', 'bentoml', 'bentoml.adapters', 'util', 'bento_artifact', 'detectron2.structures', 'imantics', 'shapely.geometry'])
[2020-09-16 11:15:16,982] DEBUG - Copying user local python dependencies: {'segmentation_model_inference.bento_service': <module 'segmentation_model_inference.bento_service' from '/home/pieter/projects/segmentation-model-inference/segmentation_model_inference/bento_service.py'>}
[2020-09-16 11:15:16,982] DEBUG - Copying local python module '/home/pieter/projects/segmentation-model-inference/segmentation_model_inference/bento_service.py'
[2020-09-16 11:15:16,982] DEBUG - Creating empty __init__.py under folder:'/tmp/bentoml-temp-3_1cm4o2/SegmentationService'
[2020-09-16 11:15:16,982] DEBUG - Creating empty __init__.py under folder:'/tmp/bentoml-temp-3_1cm4o2/SegmentationService/segmentation_model_inference'
[2020-09-16 11:15:16,982] DEBUG - Creating empty __init__.py under folder:'/tmp/bentoml-temp-3_1cm4o2/SegmentationService/artifacts'
[2020-09-16 11:15:16,982] DEBUG - Done copying local python dependant modules
[2020-09-16 11:15:16,983] DEBUG - Using Docker Base Image bentoml/model-server:0.8.6
[2020-09-16 11:15:17,412] INFO - BentoService bundle 'SegmentationService:20200916111515_B707A8' saved to: /home/pieter/bentoml/repository/SegmentationService/20200916111515_B707A8

Output log of saving the BentoService with absolute imports:

[2020-09-16 12:31:48,073] WARNING - auto_pip_dependencies enabled, it may override package versions specified in `pip_dependencies=['torch', 'imantics', 'shapely']`
[2020-09-16 12:31:49,113] DEBUG - Creating local YataiService instance
[2020-09-16 12:31:49,200] DEBUG - Upgrading tables to the latest revision
[2020-09-16 12:31:49,204] DEBUG - Created temporary directory: /tmp/bentoml-temp-7id681ne
[2020-09-16 12:31:50,277] WARNING - unknown package dependency for module: blob_storage
[2020-09-16 12:31:50,277] DEBUG - copy_local_py_modules target_module_name: segmentation_model_inference.bento_service, target_module_file: /home/pieter/projects/segmentation-model-inference/segmentation_model_inference/bento_service.py
[2020-09-16 12:31:50,277] DEBUG - Searching for local dependant modules of segmentation_model_inference.bento_service:/home/pieter/projects/segmentation-model-inference/segmentation_model_inference/bento_service.py
[2020-09-16 12:31:50,284] DEBUG - Find bad module imports that can not be parsed properly: dict_keys(['logging', 're', 'typing', 'numpy', 'torch', 'bentoml', 'bentoml.adapters', 'os', 'hashlib', 'detectron2.layers', 'abc', 'datetime', 'requests', 'google.auth', 'google.auth.credentials', 'google.cloud', 'google.cloud.storage', 'google.oauth2', 'json', 'bentoml.artifact', 'bentoml.exceptions', 'bentoml.service_env', 'detectron2.engine', 'detectron2.checkpoint', 'detectron2.config', 'detectron2.structures', 'imantics', 'shapely.geometry'])
[2020-09-16 12:31:50,284] DEBUG - Copying user local python dependencies: {'segmentation_model_inference': Module('segmentation_model_inference', '/home/pieter/projects/segmentation-model-inference/segmentation_model_inference/__init__.py', ['/home/pieter/projects/segmentation-model-inference/segmentation_model_inference']), 'segmentation_model_inference.util': Module('segmentation_model_inference.util', '/home/pieter/projects/segmentation-model-inference/segmentation_model_inference/util.py'), 'segmentation_model_inference.service': Module('segmentation_model_inference.service', '/home/pieter/projects/segmentation-model-inference/segmentation_model_inference/service/__init__.py', ['/home/pieter/projects/segmentation-model-inference/segmentation_model_inference/service']), 'segmentation_model_inference.service.blob_storage': Module('segmentation_model_inference.service.blob_storage', '/home/pieter/projects/segmentation-model-inference/segmentation_model_inference/service/blob_storage.py'), 'segmentation_model_inference.bento_artifact': Module('segmentation_model_inference.bento_artifact', '/home/pieter/projects/segmentation-model-inference/segmentation_model_inference/bento_artifact.py'), 'segmentation_model_inference.bento_service': <module 'segmentation_model_inference.bento_service' from '/home/pieter/projects/segmentation-model-inference/segmentation_model_inference/bento_service.py'>}
[2020-09-16 12:31:50,285] DEBUG - Copying local python module '/home/pieter/projects/segmentation-model-inference/segmentation_model_inference/__init__.py'
[2020-09-16 12:31:50,285] DEBUG - Copying local python module '/home/pieter/projects/segmentation-model-inference/segmentation_model_inference/util.py'
[2020-09-16 12:31:50,285] DEBUG - Copying local python module '/home/pieter/projects/segmentation-model-inference/segmentation_model_inference/service/__init__.py'
[2020-09-16 12:31:50,285] DEBUG - Copying local python module '/home/pieter/projects/segmentation-model-inference/segmentation_model_inference/service/blob_storage.py'
[2020-09-16 12:31:50,286] DEBUG - Copying local python module '/home/pieter/projects/segmentation-model-inference/segmentation_model_inference/bento_artifact.py'
[2020-09-16 12:31:50,286] DEBUG - Copying local python module '/home/pieter/projects/segmentation-model-inference/segmentation_model_inference/bento_service.py'
[2020-09-16 12:31:50,286] DEBUG - Creating empty __init__.py under folder:'/tmp/bentoml-temp-7id681ne/SegmentationService'
[2020-09-16 12:31:50,286] DEBUG - Creating empty __init__.py under folder:'/tmp/bentoml-temp-7id681ne/SegmentationService/artifacts'
[2020-09-16 12:31:50,286] DEBUG - Done copying local python dependant modules
[2020-09-16 12:31:50,287] DEBUG - Using Docker Base Image bentoml/model-server:0.8.6
[2020-09-16 12:31:50,688] INFO - BentoService bundle 'SegmentationService:20200916123149_F90291' saved to: /home/pieter/bentoml/repository/SegmentationService/20200916123149_F90291

Environment:

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.1 LTS
Release:        20.04
Codename:       focal
$ python --version -v     
Python 3.7.5
$ pip freeze | grep Bento
BentoML==0.8.6

Additional context

I have a custom BentoModelArtifact in _bentoartifact.py that is also not copied properly.

parano commented 4 years ago

Hey @pietermarsman, do you have a __init__.py file under those directories where your python files are located?

pietermarsman commented 4 years ago

Yes I have. Changed the description of the issue to reflect this. Sorry, tried to make it as minimal as possible.

RXminuS commented 4 years ago

Can confirm that I'm having the same behavior. I also have __init__.py files all around. It works however when I don't do relative imports (or run the pack.py script as python -m pack but just python pack.py)

lminer commented 3 years ago

I have the same issue and I'm using absolute, not relative imports. What's the workaround?

parano commented 3 years ago

@lminer absolute path should work as expected, could you start a separate issue and share more details on how to reproduce it?

lminer commented 3 years ago

I fixed it by appending the root directory to my python path in the module where I create the service and pack the model.

import sys
from os import path
ROOT_DIR = path.dirname(path.dirname(path.abspath(__file__)))
sys.path.append(ROOT_DIR)
stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

parano commented 2 years ago

In BentoML, local python modules can be included by specifying the include filed in the bentofile.yaml build file. E.g.:

# bentofile.yaml
service: "iris_classifier:svc"
description: "file: ./readme.md"
labels:
  foo: bar
  team: abc
include:
- "*.py"
- "*.json"
exclude: 
- "*.pyc"

Note that BentoML 1.0 no longer try to automatically capture local python dependencies, instead it asks users to specifically include the python files required for their ML service. This design also ensures an identical import path in serving environment, allowing relative imports to work the same way they are in a development environment.

pietermarsman commented 2 years ago

Thanks!