Skip to main content

Run batch inference with a registered model

Score a table of data with a model that's registered in Unity Catalog, and write the predictions to a Delta table. A batch inference job loads the model by alias, such as Champion, instead of by version number. When you reassign the alias to a new model version, the job uses the new version on its next run, without code changes.

Requirements​

You need the following before you run batch inference:

  • A model registered in Unity Catalog. See Register a model in Unity Catalog.

  • A model version with the Champion alias. The examples on Register a model in Unity Catalog assign the Challenger alias. After you validate that version, assign Champion to it. You must own the registered model, and have the USE SCHEMA and USE CATALOG privileges on its catalog and schema, to set an alias:

    Python
    from mlflow import MlflowClient

    MlflowClient().set_registered_model_alias("<catalog>.<schema>.<model>", "Champion", <version>)

    Or replace @Champion in the following examples with @Challenger.

  • The EXECUTE privilege on the registered model, and the USE CATALOG and USE SCHEMA privileges on its catalog and schema.

  • A table of input data with the columns that the model expects. This data can come from feature tables. See Build feature tables for machine learning.

Choose an inference method​

Match the method to the size and type of your data:

Method

Use for

Example

pandas on one node

Small batches and validation runs in a notebook

Score data with pandas

Spark user-defined function (UDF)

Large tables, distributed across a cluster, in batch or streaming jobs

Score data with a Spark UDF

pandas UDF for images

Computer vision models that score images stored in cloud object storage

Reference solution for image applications

Method

Use for

Example

pandas on one node

Small batches and validation runs in a notebook

Score data with pandas

Spark user-defined function (UDF)

Large tables, distributed across a cluster, in batch or streaming jobs

Score data with a Spark UDF

pandas UDF for images

Computer vision models that score images stored in cloud object storage

Reference solution for image applications

Score data with pandas​

Load the model as a generic Python function, and call predict():

Python
import mlflow

mlflow.set_registry_uri("databricks-uc")

model = mlflow.pyfunc.load_model("models:/<catalog>.<schema>.<model>@Champion")

input_df = spark.table("<catalog>.<schema>.<input-table>").toPandas()
input_df["prediction"] = model.predict(input_df)

To load a specific version instead, use models:/<catalog>.<schema>.<model>/<version>. See Load model version by alias for inference workloads.

Score data with a Spark UDF​

Load the model as a Spark user-defined function (UDF), so that scoring runs in parallel across the cluster, and write the predictions to a Delta table:

Python
import mlflow

mlflow.set_registry_uri("databricks-uc")

model_udf = mlflow.pyfunc.spark_udf(spark, "models:/<catalog>.<schema>.<model>@Champion")

input_data = spark.table("<catalog>.<schema>.<input-table>")
predictions = input_data.withColumn("prediction", model_udf())

predictions.write.mode("overwrite").saveAsTable("<catalog>.<schema>.<predictions-table>")

To restore the model's library dependencies in the UDF, pass env_manager="virtualenv" to spark_udf. See Log, load, and register MLflow models.

To run the scoring on a schedule, add the notebook to a job. See Orchestrate workloads with Lakeflow Jobs.

Next steps​