Elior Cohen
12/25/2024, 8:18 AMbentoml.Service class - so my code at the moment looks like
svc = MyCustomService(...)
@svc.api(input=..., output..., doc=...)
async def classify(...)
return ...
I wonder, now with the class based services, I guess that I can still wrap the methods with the save decorator - but I'm wondering how would I connect it to the class itself?
For exmaple, from your XGBoost docs:
@bentoml.service(
resources={"cpu": "2"},
traffic={"timeout": 10},
)
class CancerClassifier:
# Retrieve the latest version of the model from the BentoML Model Store
bento_model = bentoml.models.get("cancer:latest")
def __init__(self):
self.model = bentoml.xgboost.load_model(self.bento_model)
# Check resource availability
if os.getenv("CUDA_VISIBLE_DEVICES") not in (None, "", "-1"):
self.model.set_param({"predictor": "gpu_predictor", "gpu_id": 0}) # type: ignore (incomplete XGBoost types)
else:
nthreads = os.getenv("OMP_NUM_THREADS")
if nthreads:
nthreads = max(int(nthreads), 1)
else:
nthreads = 1
self.model.set_param(
{"predictor": "cpu_predictor", "nthread": nthreads}
)
@bentoml.api
def predict(self, data: np.ndarray) -> np.ndarray:
return self.model.predict(xgb.DMatrix(data))
How would I replicate this with a custom service?Elior Cohen
12/25/2024, 9:07 AMdef openapi_spec() to adhere to API standards of my orgJian Shen Yap
12/26/2024, 1:23 PMElior Cohen
12/26/2024, 1:24 PMElior Cohen
12/26/2024, 1:24 PMElior Cohen
12/26/2024, 1:24 PMElior Cohen
12/26/2024, 1:24 PMbentoml.Service class is being created internally by the @benotml.service dcoratorElior Cohen
12/26/2024, 1:25 PMbentoml.ServiceElior Cohen
12/26/2024, 1:25 PMclass MyCustomService(benotml.Service) with the new bentoml interfaceElior Cohen
12/26/2024, 1:27 PMclass MyCustomService(bentoml.Service)), of how to implement the service.py shown in my first comment while using the custom service - that will be enoughJian Shen Yap
12/26/2024, 1:42 PM@bentoml.service
class CustomService:
...
class ChildService(CustomService):
...Elior Cohen
12/29/2024, 6:17 AM@bentoml.service(
resources={"cpu": "2"},
traffic={"timeout": 10},
)
class CancerClassifier:
# Retrieve the latest version of the model from the BentoML Model Store
bento_model = bentoml.models.get("cancer:latest")
def __init__(self):
self.model = bentoml.xgboost.load_model(self.bento_model)
# Check resource availability
if os.getenv("CUDA_VISIBLE_DEVICES") not in (None, "", "-1"):
self.model.set_param({"predictor": "gpu_predictor", "gpu_id": 0}) # type: ignore (incomplete XGBoost types)
else:
nthreads = os.getenv("OMP_NUM_THREADS")
if nthreads:
nthreads = max(int(nthreads), 1)
else:
nthreads = 1
self.model.set_param(
{"predictor": "cpu_predictor", "nthread": nthreads}
)
@bentoml.api
def predict(self, data: np.ndarray) -> np.ndarray:
return self.model.predict(xgb.DMatrix(data))
There are two usages here of the service the @bentoml.service and the @bentoml.api - how do I apply the child service I created to the class and method decorators?Elior Cohen
01/05/2025, 7:24 AMJian Shen Yap
01/05/2025, 1:17 PMclass ChildClassifier(CancerClassifier):
...
works for you?Elior Cohen
01/06/2025, 6:35 AMresources and traffic as in the XGBoost example (the class level decorator)
2. How do I decorate the predict function which now has @bentoml.api - what should I put instead of it? Because internally @bentoml.api creates a different Service and we want to use the new one (ChildClassifier)Elior Cohen
01/06/2025, 1:55 PMElior Cohen
01/06/2025, 1:55 PM