Skip to content

Commit cd8995c

Browse files
authored
fix: Support importing multiple Bentos with shared models (#5543)
Fixes #5538 Signed-off-by: Frost Ming <me@frostming.com>
1 parent cc4dbfb commit cd8995c

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/bentoml/_internal/bento/bento.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from ..configuration import BENTOML_VERSION
2828
from ..configuration.containers import BentoMLContainer
2929
from ..models import ModelStore
30+
from ..models import copy_model
3031
from ..runner import Runner
3132
from ..store import Store
3233
from ..store import StoreItem
@@ -587,7 +588,16 @@ def save(
587588
if self._model_store is not None:
588589
# Move models to the global model store, if any
589590
for model in self._model_store.list():
590-
model.save(model_store)
591+
copied = copy_model(
592+
model.tag,
593+
src_model_store=self._model_store,
594+
target_model_store=model_store,
595+
)
596+
if not copied:
597+
logger.warning(
598+
"Model %s already exists in the global model store, skipping copy.",
599+
model.tag,
600+
)
591601

592602
def ignore_models(src_path: str, names: list[str]) -> t.Iterable[str]:
593603
if Path(src_path) == self._path and "models" in names:

src/bentoml/_internal/models/model.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -700,18 +700,20 @@ def copy_model(
700700
*,
701701
src_model_store: ModelStore,
702702
target_model_store: ModelStore,
703-
):
703+
) -> bool:
704704
"""copy a model from src model store to target modelstore, and do nothing if the
705-
model tag already exist in target model store
705+
model tag already exist in target model store.
706+
Returns True if the model is copied, False if the model already exists in target model store.
706707
"""
707708
try:
708709
target_model_store.get(model_tag) # if model tag already found in target
709-
return
710+
return False
710711
except NotFound:
711712
pass
712713

713714
model = src_model_store.get(model_tag)
714715
model.save(target_model_store)
716+
return True
715717

716718

717719
def _ModelInfo_dumper(dumper: yaml.Dumper, info: ModelInfo) -> yaml.Node:

0 commit comments

Comments
 (0)