Adam Liter
05/02/2024, 5:56 PMShape validation to work with numpy? I’m trying this example, but with an np.ndarray instead of torch.Tensor, and I’m getting an error:
IndexError: list assignment index out of range
Works fine if I remove the Shape restriction inside Annotated.Adam Liter
05/02/2024, 11:47 PMfrom typing import Annotated
from pydantic import Field
import bentoml
from bentoml.validators import Shape, DType
import numpy as np
@bentoml.service
class ModelService:
def __init__(self):
self.model = ""
@bentoml.api
def predict(
self,
input_arr: Annotated[
np.ndarray,
# Shape((-1, 2)), # <-- this doesn't work
DType("float32")
] = Field(description="A (-1, 2)-shaped np.ndarray")
) -> np.ndarray:
return input_arrAdam Liter
05/02/2024, 11:55 PMtorch.Tensor? I’m using bentoml==1.2.12.Adam Liter
05/03/2024, 12:15 AMdiff --git a/src/_bentoml_sdk/_pydantic.py b/src/_bentoml_sdk/_pydantic.py
index ae4e49d5..987b0358 100644
--- a/src/_bentoml_sdk/_pydantic.py
+++ b/src/_bentoml_sdk/_pydantic.py
@@ -53,13 +53,13 @@ def numpy_prepare_pydantic_annotations(
_, remaining_annotations = _known_annotated_metadata.collect_known_metadata(
annotations
)
- for i, annotation in enumerate(remaining_annotations[:]):
+ for annotation in remaining_annotations[:]:
if isinstance(annotation, Shape):
shape = annotation.dimensions
- del remaining_annotations[i]
+ remaining_annotations.remove(annotation)
elif isinstance(annotation, DType):
dtype = annotation.dtype
- del remaining_annotations[i]
+ remaining_annotations.remove(annotation)
return source, [TensorSchema("numpy-array", dtype, shape), *remaining_annotations]
@@ -80,13 +80,13 @@ def torch_prepare_pydantic_annotations(
_, remaining_annotations = _known_annotated_metadata.collect_known_metadata(
annotations
)
- for i, annotation in enumerate(remaining_annotations[:]):
+ for annotation in remaining_annotations[:]:
if isinstance(annotation, Shape):
shape = annotation.dimensions
- del remaining_annotations[i]
+ remaining_annotations.remove(annotation)
elif isinstance(annotation, DType):
dtype = annotation.dtype
- del remaining_annotations[i]
+ remaining_annotations.remove(annotation)
return source, [TensorSchema("torch-tensor", dtype, shape), *remaining_annotations]
@@ -106,13 +106,13 @@ def tf_prepare_pydantic_annotations(
_, remaining_annotations = _known_annotated_metadata.collect_known_metadata(
annotations
)
- for i, annotation in enumerate(remaining_annotations[:]):
+ for annotation in remaining_annotations[:]:
if isinstance(annotation, Shape):
shape = annotation.dimensions
- del remaining_annotations[i]
+ remaining_annotations.remove(annotation)
elif isinstance(annotation, DType):
dtype = annotation.dtype
- del remaining_annotations[i]
+ remaining_annotations.remove(annotation)
return source, [TensorSchema("tf-tensor", dtype, shape), *remaining_annotations]
@@ -169,10 +169,10 @@ def pathlib_prepare_pydantic_annotations(
annotations
)
content_type: str | None = None
- for i, annotation in enumerate(remaining_annotations[:]):
+ for annotation in remaining_annotations[:]:
if isinstance(annotation, ContentType):
content_type = annotation.content_type
- del remaining_annotations[i]
+ remaining_annotations.annotation(remove)
return source, [FileSchema(content_type=content_type), *remaining_annotations]Adam Liter
05/03/2024, 12:21 AMSean
05/03/2024, 12:31 AMSean
05/03/2024, 1:24 AMAdam Liter
05/03/2024, 9:29 PMruff and verified it’s working. Also fixed a related typo in the docs. Happy to update the PR further though with any additional feedback you might have.Sean
05/04/2024, 12:25 AMSean
05/07/2024, 3:23 AMv1.2.13. 🎉Adam Liter
05/07/2024, 4:27 AM