Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Bentoml BentoML Framework FastAI

From Leeroopedia
Knowledge Sources
Domains ML Framework, FastAI, Deep Learning, Model Serialization
Last Updated 2026-02-13 15:00 GMT

Overview

Provides the BentoML framework integration for fast.ai, enabling saving, loading, and serving fastai.learner.Learner models through the BentoML model store.

Description

This module implements the BentoML framework adapter for fast.ai v2+. It requires both torch and fastai packages (with fastai.basics to enforce v2 minimum). The module registers the PyTorchTensorContainer as a side effect import.

save_model() validates that the provided object is a Learner (not a raw nn.Module), records framework versions for fastai, fastcore, and torch, and serializes the learner using learner.export() with cloudpickle as the pickle module. Batchable signatures are explicitly disallowed for fastai models, raising BentoMLException if any signature has batchable=True. The default signature is predict (non-batchable).

load_model() loads the learner from the stored pickle file using fastai.learner.load_learner() with cpu=True to ensure portability. If the model uses mixed precision, it is automatically converted to FP32.

get() retrieves the BentoML Model metadata for a given tag, validating the module name.

get_runnable() creates a FastAIRunnable class that supports CPU only (GPU is not supported for fastai inference). The runnable sets the model to eval mode (disabling dropout and batchnorm), enters a torch.inference_mode() (or torch.no_grad() for PyTorch < 1.9) context, and maps each signature method to a runnable method. A warning is logged recommending users access the underlying PyTorch model directly for performance-critical use cases.

Usage

Use this module to save fast.ai Learner models to the BentoML model store and serve them via BentoML services. Access via bentoml.fastai. For performance-sensitive inference, consider extracting the underlying PyTorch model via learner.model and using bentoml.pytorch instead.

Code Reference

Source Location

Signature

def get(tag_like: str | Tag) -> bentoml.Model: ...

def load_model(bento_model: str | Tag | bentoml.Model) -> learner.Learner: ...

def save_model(
    name: Tag | str,
    learner_: learner.Learner,
    *,
    signatures: ModelSignaturesType | None = None,
    labels: dict[str, str] | None = None,
    custom_objects: dict[str, t.Any] | None = None,
    external_modules: t.List[ModuleType] | None = None,
    metadata: dict[str, t.Any] | None = None,
) -> bentoml.Model: ...

def get_runnable(bento_model: bentoml.Model) -> t.Type[bentoml.legacy.Runnable]: ...

Import

import bentoml

# Via the public API
bento_model = bentoml.fastai.save_model("fai_learner", learner)
loaded = bentoml.fastai.load_model("fai_learner:latest")

# Direct import
from bentoml._internal.frameworks.fastai import save_model, load_model, get, get_runnable

I/O Contract

Inputs

Name Type Required Description
name Tag or str Yes (save_model) Name or tag for the model in the store
learner_ fastai.learner.Learner Yes (save_model) The fast.ai Learner instance to save; must not be a raw nn.Module
tag_like str or Tag Yes (get) Tag to retrieve from the model store
bento_model str, Tag, or bentoml.Model Yes (load_model) Model identifier or object to load
signatures ModelSignaturesType or None No Method signatures; defaults to {"predict": {"batchable": False}}. Batchable signatures are not allowed
labels dict[str, str] or None No User-defined labels for model management
custom_objects dict[str, Any] or None No Additional objects to save with the model
external_modules List[ModuleType] or None No Additional Python modules to bundle
metadata dict[str, Any] or None No Custom metadata for the model

Outputs

Name Type Description
bentoml.Model (save_model) bentoml.Model The saved model reference in the BentoML store
learner.Learner (load_model) fastai.learner.Learner The deserialized fast.ai learner loaded on CPU
bentoml.Model (get) bentoml.Model BentoML Model metadata object
FastAIRunnable (get_runnable) type[Runnable] A CPU-only Runnable class for serving the learner

Usage Examples

import bentoml
from fastai.text.data import URLs, untar_data, TextDataLoaders
from fastai.text.models import AWD_LSTM
from fastai.text.learner import text_classifier_learner
from fastai.metrics import accuracy

# Train a model
dls = TextDataLoaders.from_folder(untar_data(URLs.IMDB), valid="test")
learner = text_classifier_learner(dls, AWD_LSTM, drop_mult=0.5, metrics=accuracy)
learner.fine_tune(4, 1e-2)

# Save to BentoML
tag = bentoml.fastai.save_model("fai_learner", learner)
print(f"Saved: {tag}")

# Load the model back
loaded_learner = bentoml.fastai.load_model("fai_learner:latest")
result = loaded_learner.predict("I love that movie!")

# For performance-critical inference, extract the PyTorch model:
# torch_model = loaded_learner.model
# bentoml.pytorch.save_model("fai_torch", torch_model)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment