bentoml / BentoML

The easiest way to serve AI apps and models - Build reliable Inference APIs, LLM apps, Multi-model chains, RAG service, and much more!
https://bentoml.com
Apache License 2.0
6.99k stars 777 forks source link

Failure of bento serve in production with aiohttp error #2270

Closed andreea-anghel closed 2 years ago

andreea-anghel commented 2 years ago

Describe the bug

The sklearn example available here https://docs.bentoml.org/en/latest/quickstart.html#installation fails at inference time with aiohttp error (unexpected loop argument). The bento service is deployed in production mode bentoml serve iris_classifier:latest --production. When deployed in development mode, the inference works as expected.

To Reproduce

Steps to reproduce the issue:

  1. Install BentoML: pip install bentoml --pre
  2. Train and save bento sklearn model:
    
    import bentoml

from sklearn import svm from sklearn import datasets

Load predefined training set to build an example model

iris = datasets.load_iris() X, y = iris.data, iris.target

Model Training

clf = svm.SVC(gamma='scale') clf.fit(X, y)

Call to bentoml..save(, model)

In order to save to BentoML's standard format in a local model store

bentoml.sklearn.save("iris_clf", clf)

3. Create BentoML service

bento.py

import bentoml import bentoml.sklearn import numpy as np

from bentoml.io import NumpyNdarray

Load the runner for the latest ScikitLearn model we just saved

iris_clf_runner = bentoml.sklearn.load_runner("iris_clf:latest")

Create the iris_classifier service with the ScikitLearn runner

Multiple runners may be specified if needed in the runners array

When packaged as a bento, the runners here will included

svc = bentoml.Service("iris_classifier", runners=[iris_clf_runner])

Create API function with pre- and post- processing logic with your new "svc" annotation

@svc.api(input=NumpyNdarray(), output=NumpyNdarray()) def predict(input_ndarray: np.ndarray) -> np.ndarray:

Define pre-processing logic

result = iris_clf_runner.run(input_ndarray)
# Define post-processing logic
return result

4. Create BentoML configuration file

bentofile.yaml

service: "bento.py:svc" # A convention for locating your service: : include:

Expected behavior

The response should be the classification result, namely 1.

Screenshots/Logs

The error generated by the server is the following:

Exception on /predict [POST]
Traceback (most recent call last):
  File "/home/aan/anaconda3/envs/coursera-env/lib/python3.9/site-packages/bentoml/_internal/server/service_app.py", line 356, in api_func
    output: t.Any = await run_in_threadpool(api.func, input_data)
  File "/home/aan/anaconda3/envs/coursera-env/lib/python3.9/site-packages/starlette/concurrency.py", line 45, in run_in_threadpool
    return await anyio.to_thread.run_sync(func, *args)
  File "/home/aan/anaconda3/envs/coursera-env/lib/python3.9/site-packages/anyio/to_thread.py", line 28, in run_sync
    return await get_asynclib().run_sync_in_worker_thread(func, *args, cancellable=cancellable,
  File "/home/aan/anaconda3/envs/coursera-env/lib/python3.9/site-packages/anyio/_backends/_asyncio.py", line 818, in run_sync_in_worker_thread
    return await future
  File "/home/aan/anaconda3/envs/coursera-env/lib/python3.9/site-packages/anyio/_backends/_asyncio.py", line 754, in run
    result = context.run(func, *args)
  File "/home/aan/bentoml/bentos/iris_classifier/g6trvxmntk6hwfg5/src/bento.py", line 20, in predict
    result = iris_clf_runner.run(input_ndarray)
  File "/home/aan/anaconda3/envs/coursera-env/lib/python3.9/site-packages/bentoml/_internal/runner/runner.py", line 141, in run
    return self._impl.run(*args, **kwargs)
  File "/home/aan/anaconda3/envs/coursera-env/lib/python3.9/site-packages/bentoml/_internal/runner/remote.py", line 111, in run
    return anyio.from_thread.run(self.async_run, *args, **kwargs)
  File "/home/aan/anaconda3/envs/coursera-env/lib/python3.9/site-packages/anyio/from_thread.py", line 35, in run
    return asynclib.run_async_from_thread(func, *args)
  File "/home/aan/anaconda3/envs/coursera-env/lib/python3.9/site-packages/anyio/_backends/_asyncio.py", line 847, in run_async_from_thread
    return f.result()
  File "/home/aan/anaconda3/envs/coursera-env/lib/python3.9/concurrent/futures/_base.py", line 445, in result
    return self.__get_result()
  File "/home/aan/anaconda3/envs/coursera-env/lib/python3.9/concurrent/futures/_base.py", line 390, in __get_result
    raise self._exception
  File "/home/aan/anaconda3/envs/coursera-env/lib/python3.9/site-packages/bentoml/_internal/runner/remote.py", line 102, in async_run
    return await self._async_req(url, *args, **kwargs)
  File "/home/aan/anaconda3/envs/coursera-env/lib/python3.9/site-packages/bentoml/_internal/runner/remote.py", line 81, in _async_req
    client = self._get_client()
  File "/home/aan/anaconda3/envs/coursera-env/lib/python3.9/site-packages/simple_di/__init__.py", line 124, in _
    return func(*_inject_args(bind.args), **_inject_kwargs(bind.kwargs))
  File "/home/aan/anaconda3/envs/coursera-env/lib/python3.9/site-packages/bentoml/_internal/runner/remote.py", line 67, in _get_client
    connector=self._get_conn(),
  File "/home/aan/anaconda3/envs/coursera-env/lib/python3.9/site-packages/simple_di/__init__.py", line 124, in _
    return func(*_inject_args(bind.args), **_inject_kwargs(bind.kwargs))
  File "/home/aan/anaconda3/envs/coursera-env/lib/python3.9/site-packages/bentoml/_internal/runner/remote.py", line 44, in _get_conn
    self._conn = aiohttp.UnixConnector(path=uds, loop=self._loop)
TypeError: __init__() got an unexpected keyword argument 'loop'

Environment:

Additional context

alexdivet commented 2 years ago

resolved here #2203

andreea-anghel commented 2 years ago

It works. Thanks @alexdivet