hello, I'm kind of confused. I assumed the reason ...
# ask-for-help
m
hello, I'm kind of confused. I assumed the reason for saving the model to BentoML and then pushing it is that I can load it from my services with the tag, but it seems that's always returning
None
. Do I need to bundle the models in my bento? My workflow is that I have a script for saving the models. I run that and push them to BentoML Cloud. If I do bentoml
serve
, it works locally, but when I push the service, importing the model with
BentoModel("tag")
fails, returning None.
j
Hi, can you share the service code that you are using? if you are refenence
BentoModel
as a class attribute, the models will be automatically referenced / and bunlded into your bento
m
Copy code
import os
import sys
import numpy as np
import torch
import trimesh
import bentoml

from pathlib import Path
from typing import Annotated
from pydantic import Field
from bentoml.validators import ContentType

TRIPOSG_CODE_DIR = "./triposg"
sys.path.append(TRIPOSG_CODE_DIR)
sys.path.append(os.path.join(TRIPOSG_CODE_DIR, "scripts"))

from triposg.scripts.briarmbg import BriaRMBG
from triposg.scripts.image_process import prepare_image
from triposg.pipelines.pipeline_triposg import TripoSGPipeline

WHITE = np.array([1.0, 1.0, 1.0])

image = (
    bentoml.images.Image(python_version="3.10")
    .requirements_file("./requirements.txt")
    .system_packages(
        "ninja-build",
        "libffi-dev",
        "libgl1-mesa-glx",
        "libgles2-mesa-dev",
        "mesa-utils",
        "libglib2.0-dev",
    )
)

RMBG_PRETRAINED_MODEL = "checkpoints/RMBG-1.4"
TRIPOSG_PRETRAINED_MODEL = "checkpoints/TripoSG"

@bentoml.service(image=image, resources={"gpu": 1})
class Mesh:
    device = "cuda"
    dtype = torch.float16

    rmbg_net = BriaRMBG.from_pretrained(RMBG_PRETRAINED_MODEL).to(device)
    triposg_pipe = TripoSGPipeline.from_pretrained(TRIPOSG_PRETRAINED_MODEL).to(device, dtype)

    def __init__(self):
        self.rmbg_net.eval()

    @bentoml.task
    @torch.no_grad()
    def generate(
        self,
        context: bentoml.Context,
        image_path: Annotated[Path, ContentType("image/*")],
        seed: int = Field(default=1234, description="Seed for reproducibility"),
        num_inference_steps: int = Field(
            default=50, description="Number of inference steps"
        ),
        guidance_scale: float = Field(
            default=7.5, description="Guidance scale for the model"
        ),
    ) -> Annotated[Path, ContentType("application/octet-stream")]:

        image = prepare_image(image_path, bg_color=WHITE, rmbg_net=self.rmbg_net)

        generated = self.triposg_pipe(
            image=image,
            generator=torch.Generator(device=self.device).manual_seed(seed),
            num_inference_steps=num_inference_steps,
            guidance_scale=guidance_scale,
        )
        vertices, faces = generated.samples[0]
        mesh = trimesh.Trimesh(vertices.astype(np.float32), np.ascontiguousarray(faces))

        mesh_path = Path(context.temp_dir) / "mesh.glb"
        mesh.export(mesh_path)

        torch.cuda.empty_cache()

        return mesh_path
this is the version i have working, in this case i have copied the models to the root folder manually
Copy code
import os
import sys
from bentoml.models import BentoModel
import numpy as np
import torch
import trimesh
import bentoml

from pathlib import Path
from typing import Annotated
from pydantic import Field
from bentoml.validators import ContentType

TRIPOSG_CODE_DIR = "./triposg"
sys.path.append(TRIPOSG_CODE_DIR)
sys.path.append(os.path.join(TRIPOSG_CODE_DIR, "scripts"))

from triposg.scripts.briarmbg import BriaRMBG
from triposg.scripts.image_process import prepare_image
from triposg.pipelines.pipeline_triposg import TripoSGPipeline

WHITE = np.array([1.0, 1.0, 1.0])

image = (
    bentoml.images.Image(python_version="3.10")
    .requirements_file("./requirements.txt")
    .system_packages(
        "ninja-build",
        "libffi-dev",
        "libgl1-mesa-glx",
        "libgles2-mesa-dev",
        "mesa-utils",
        "libglib2.0-dev",
    )
)

@bentoml.service(image=image, resources={"gpu": 1})
class Mesh:
    device = "cuda"
    dtype = torch.float16

    rmbg_net = BriaRMBG.from_pretrained(
        BentoModel("briarmbg:latest").resolve().path
    ).to(device)
    triposg_pipe = TripoSGPipeline.from_pretrained(
        BentoModel("triposg:latest").resolve().path
    ).to(device, dtype)

    def __init__(self):
        self.rmbg_net.eval()

    @bentoml.task
    @torch.no_grad()
    def generate(
        self,
        context: bentoml.Context,
        image_path: Annotated[Path, ContentType("image/*")],
        seed: int = Field(default=1234, description="Seed for reproducibility"),
        num_inference_steps: int = Field(
            default=50, description="Number of inference steps"
        ),
        guidance_scale: float = Field(
            default=7.5, description="Guidance scale for the model"
        ),
    ) -> Annotated[Path, ContentType("application/octet-stream")]:

        image = prepare_image(image_path, bg_color=WHITE, rmbg_net=self.rmbg_net)

        generated = self.triposg_pipe(
            image=image,
            generator=torch.Generator(device=self.device).manual_seed(seed),
            num_inference_steps=num_inference_steps,
            guidance_scale=guidance_scale,
        )
        vertices, faces = generated.samples[0]
        mesh = trimesh.Trimesh(vertices.astype(np.float32), np.ascontiguousarray(faces))

        mesh_path = Path(context.temp_dir) / "mesh.glb"
        mesh.export(mesh_path)

        torch.cuda.empty_cache()

        return mesh_path
this is the one that's not working
Copy code
2025-05-21T07:00:28Z
[Service: Mesh][Replica: jwhsz]
$PORT is set! Overriding $BENTOML_PORT with $PORT (3000)
2025-05-21T07:00:30Z
[Service: Mesh][Replica: jwhsz]
DeprecationWarning: The parameter '--production' is deprecated and will be 
2025-05-21T07:00:30Z
[Service: Mesh][Replica: jwhsz]
removed in the future. (Current behaviour: This is enabled by default. To run in
2025-05-21T07:00:30Z
[Service: Mesh][Replica: jwhsz]
development mode, use '--development'.)
2025-05-21T07:00:39Z
[Service: Mesh][Replica: jwhsz]
2025-05-21T07:00:38+0000 [INFO] [cli] Adding system packages using distro debian's package manager
2025-05-21T07:00:39Z
[Service: Mesh][Replica: jwhsz]
Error: [serve] `serve` failed: No cloud context default found
j
Hello. you will need to use
BentoModel("briarmbg:latest")
as a class attribute i.e.
Copy code
class Mesh:
    model = BentoModel("briarmbg:latest")

    def __init__(self):
         rmbg_net = BriaRMBG.from_pretrained(model.path).to(device)
🙏 1
m
🤦 shows i'm python noob, thank you @Jian Shen Yap
j
Hey no problem, this is a BentoML specific feature where we will scan all the class attributes to check what model to be included into the bento
👍 1