BentoML icon indicating copy to clipboard operation
BentoML copied to clipboard

bug: Cannot specify Numpy DType and Shape with Pydantic as deletion contains a bug.

Open michelkok opened this issue 9 months ago • 0 comments

Describe the bug

When specifying a Numpy array with Shape and DType, we get an error concerning the deletion of remaining_annotations. When looking at the responsible code (here) this makes sense. Using del in the list you are looping over will change it while looping over it. So, in this case, it will remove Shape first, and then with i==1 it will try to remove DType as well. However, since Shape was already removed, DType is now at index 0, and not at index i, which is why the code fails.

When does it happen

from typing import Annotated

import bentoml
import numpy as np
from bentoml.validators import DType, Shape

# Other code 

    @bentoml.api
    def predict_all(
        self,
        spectrum: Annotated[np.ndarray, Shape((246,)), DType("float32")]
        = Field(description="A 1x4 tensor with float32 dtype")
    ) -> np.ndarray:
        return np.mean([model.predict(spectrum) for model in self.models])
    

Solution

    # Still remaining after numpy annotations are processed.
    remaining = []
    for annotation in (remaining_annotations):
        if isinstance(annotation, Shape):
            shape = annotation.dimensions
        elif isinstance(annotation, DType):
            dtype = annotation.dtype
        else:
            remaining.append(annotation)

    return source, [TensorSchema("numpy-array", dtype, shape), *remaining]

Shall I make a PR?

To reproduce

No response

Expected behavior

No response

Environment

Environment variable

BENTOML_DEBUG=''
BENTOML_QUIET=''
BENTOML_BUNDLE_LOCAL_BUILD=''
BENTOML_DO_NOT_TRACK=''
BENTOML_CONFIG=''
BENTOML_CONFIG_OPTIONS=''
BENTOML_PORT=''
BENTOML_HOST=''
BENTOML_API_WORKERS=''

System information

bentoml: 1.2.4 python: 3.12.3 platform: Linux-5.15.146.1-microsoft-standard-WSL2-x86_64-with-glibc2.31 uid_gid: 0:0

pip_packages
aiohttp==3.9.3
aiosignal==1.3.1
alembic==1.13.1
annotated-types==0.6.0
antlr4-python3-runtime==4.9.3
anyio==4.3.0
appdirs==1.4.4
asgiref==3.7.2
attrs==23.2.0
bentoml==1.2.4
build==1.1.1
CacheControl==0.13.1
cattrs==23.1.2
certifi==2024.2.2
cffi==1.16.0
cfgv==3.4.0
charset-normalizer==3.3.2
circus==0.18.0
clearml==1.14.4
cleo==2.1.0
click==8.1.7
click-option-group==0.5.6
cloudpickle==3.0.0
colorlog==6.8.2
contextlib2==21.6.0
contourpy==1.2.1
crashtest==0.4.1
cryptography==42.0.7
cycler==0.12.1
dataclasses-json==0.6.4
deepmerge==1.1.1
Deprecated==1.2.14
distlib==0.3.8
dulwich==0.21.7
fastjsonschema==2.19.1
filelock==3.13.1
fonttools==4.51.0
frozenlist==1.4.1
fs==2.4.16
furl==2.1.3
greenlet==3.0.3
h11==0.14.0
httpcore==1.0.4
httpx==0.27.0
hydra-core==1.3.2
identify==2.5.35
idna==3.6
importlib-metadata==6.11.0
inflection==0.5.1
iniconfig==2.0.0
installer==0.7.0
jaraco.classes==3.4.0
jeepney==0.8.0
Jinja2==3.1.3
joblib==1.3.2
jsonschema==4.21.1
jsonschema-specifications==2023.12.1
keyring==24.3.1
kiwisolver==1.4.5
loguru==0.7.2
Mako==1.3.2
markdown-it-py==3.0.0
MarkupSafe==2.1.5
marshmallow==3.21.0
matplotlib==3.8.4
mdurl==0.1.2
more-itertools==10.2.0
msgpack==1.0.8
multidict==6.0.5
mypy-extensions==1.0.0
nodeenv==1.8.0
numpy==1.26.4
nvidia-ml-py==11.525.150
omegaconf==2.3.0
opentelemetry-api==1.20.0
opentelemetry-instrumentation==0.41b0
opentelemetry-instrumentation-aiohttp-client==0.41b0
opentelemetry-instrumentation-asgi==0.41b0
opentelemetry-sdk==1.20.0
opentelemetry-semantic-conventions==0.41b0
opentelemetry-util-http==0.41b0
optuna==3.5.0
optuna-fast-fanova==0.0.4
orderedmultidict==1.0.1
packaging==23.2
pandas==2.2.2
pathlib2==2.3.7.post1
pathspec==0.12.1
pexpect==4.9.0
pillow==10.2.0
pip-requirements-parser==32.0.1
pip-tools==7.4.0
pkginfo==1.10.0
platformdirs==4.2.0
plotly==5.19.0
pluggy==1.5.0
poetry==1.7.1
poetry-core==1.8.1
poetry-plugin-export==1.6.0
pre-commit==3.6.2
prometheus_client==0.20.0
psutil==5.9.8
ptyprocess==0.7.0
py4j==0.10.9.7
pycparser==2.22
pydantic==2.6.3
pydantic_core==2.16.3
Pygments==2.17.2
PyJWT==2.8.0
pyparsing==3.1.2
pyproject_hooks==1.0.0
pytest==8.2.0
python-dateutil==2.9.0.post0
python-json-logger==2.0.7
python-multipart==0.0.9
pytz==2024.1
PyYAML==6.0.1
pyzmq==25.1.2
rapidfuzz==3.9.0
referencing==0.33.0
requests==2.31.0
requests-toolbelt==1.0.0
rich==13.7.1
rpds-py==0.18.0
ruff==0.2.2
schema==0.7.5
scikit-learn==1.4.1.post1
scipy==1.12.0
seaborn==0.13.2
SecretStorage==3.3.3
setuptools==69.1.1
shellingham==1.5.4
simple-di==0.1.5
six==1.16.0
sniffio==1.3.1
SQLAlchemy==2.0.27
starlette==0.37.1
tenacity==8.2.3
threadpoolctl==3.3.0
tomlkit==0.12.5
tornado==6.4
tqdm==4.66.2
trove-classifiers==2024.4.10
typing-inspect==0.9.0
typing_extensions==4.10.0
tzdata==2024.1
urllib3==2.2.1
uvicorn==0.27.1
virtualenv==20.25.1
watchfiles==0.21.0
wheel==0.42.0
wrapt==1.16.0
xgboost==1.7.6
yarl==1.9.4
zipp==3.17.0

michelkok avatar May 16 '24 13:05 michelkok