Skip to content

Commit 409a9d8

Browse files
authored
fix: better way to set service name (#5383)
Signed-off-by: Frost Ming <me@frostming.com>
1 parent 913549c commit 409a9d8

File tree

2 files changed

+9
-29
lines changed

2 files changed

+9
-29
lines changed

src/_bentoml_sdk/service/config.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,6 @@ class ServiceConfig(TypedDict, total=False):
246246
service level (per replica) config
247247
"""
248248

249-
name: str
250249
traffic: TrafficSchema
251250
backlog: Annotated[int, Ge(64)]
252251
max_runner_connections: Posint

src/_bentoml_sdk/service/factory.py

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class _DummyService:
8080
class Service(t.Generic[T]):
8181
"""A Bentoml service that can be served by BentoML server."""
8282

83+
name: str
8384
config: Config = attrs.field(factory=Config)
8485
inner: type[T] = _DummyService
8586
image: t.Optional[Image] = None
@@ -88,7 +89,7 @@ class Service(t.Generic[T]):
8889
models: list[Model[t.Any]] = attrs.field(factory=list)
8990
cmd: t.Optional[t.List[str]] = None
9091
bento: t.Optional[Bento] = attrs.field(init=False, default=None)
91-
apis: dict[str, APIMethod[..., t.Any]] = attrs.field(factory=dict, init=False)
92+
apis: dict[str, APIMethod[..., t.Any]] = attrs.field(factory=dict)
9293
dependencies: dict[str, Dependency[t.Any]] = attrs.field(factory=dict, init=False)
9394
mount_apps: list[tuple[ext.ASGIApp, str, str]] = attrs.field(
9495
factory=list, init=False
@@ -277,11 +278,6 @@ def extract_routes_from_asgi_app(app: t.Any, current_prefix: str) -> None:
277278
}
278279
)
279280

280-
@property
281-
def name(self) -> str:
282-
name = self.config.get("name") or self.inner.__name__
283-
return name
284-
285281
@property
286282
def import_string(self) -> str:
287283
if self._import_str is None:
@@ -490,9 +486,8 @@ def service(inner: type[T], /) -> Service[T]: ...
490486

491487
@t.overload
492488
def service(
493-
inner: None = ...,
494-
/,
495489
*,
490+
name: str | None = None,
496491
image: Image | None = None,
497492
envs: list[dict[str, str]] | None = None,
498493
labels: dict[str, str] | None = None,
@@ -506,6 +501,7 @@ def service(
506501
inner: type[T] | None = None,
507502
/,
508503
*,
504+
name: str | None = None,
509505
image: Image | None = None,
510506
envs: list[dict[str, str]] | None = None,
511507
labels: dict[str, str] | None = None,
@@ -529,6 +525,7 @@ def decorator(inner: type[T]) -> Service[T]:
529525
if isinstance(inner, Service):
530526
raise TypeError("service() decorator can only be applied once")
531527
return service_class(
528+
name=name or inner.__name__,
532529
config=config,
533530
inner=inner,
534531
image=image,
@@ -550,7 +547,6 @@ class RunnerHandle(runner.runnable_class):
550547
def __init__(self) -> None:
551548
super().__init__(**runner.runnable_init_params)
552549

553-
RunnerHandle.__name__ = runner.name
554550
apis: dict[str, APIMethod[..., t.Any]] = {}
555551
assert runner.runnable_class.bentoml_runnable_methods__ is not None
556552
for method in runner.runner_methods:
@@ -572,31 +568,16 @@ def __init__(self) -> None:
572568
gpus: list[int] | str | int = resource_config["nvidia.com/gpu"]
573569
if isinstance(gpus, str):
574570
gpus = int(gpus)
575-
if runner.workers_per_resource > 1:
576-
config["workers"] = {}
577-
workers_per_resource = int(runner.workers_per_resource)
578-
if isinstance(gpus, int):
579-
gpus = list(range(gpus))
580-
for i in gpus:
581-
config["workers"].extend([{"gpus": i}] * workers_per_resource)
582-
else:
583-
resources_per_worker = int(1 / runner.workers_per_resource)
584-
if isinstance(gpus, int):
585-
config["workers"] = [
586-
{"gpus": resources_per_worker}
587-
for _ in range(gpus // resources_per_worker)
588-
]
589-
else:
590-
config["workers"] = [
591-
{"gpus": gpus[i : i + resources_per_worker]}
592-
for i in range(0, len(gpus), resources_per_worker)
593-
]
571+
elif isinstance(gpus, list):
572+
gpus = len(gpus)
573+
config["workers"] = int(gpus * runner.workers_per_resource)
594574
elif "cpus" in resource_config:
595575
config["workers"] = (
596576
math.ceil(resource_config["cpus"]) * runner.workers_per_resource
597577
)
598578
config.update(kwargs)
599579
return Service(
580+
name=runner.name,
600581
config=config,
601582
inner=RunnerHandle,
602583
models=[BentoModel(m.tag) for m in runner.models],

0 commit comments

Comments
 (0)