Databricks Certified Generative AI Engineer Associate free question 8 / 10 · Domain 4: Assembling and Deploying Applications
A team writes a chain with preprocessing and postprocessing as a subclass of MLflow PythonModel and logs it as a custom pyfunc model. Which pair of functions does the page on deploying custom Python code to Model Serving name as required when packaging arbitrary Python code with MLflow?
- Two functions: __init__ and predict
- Two functions: predict and format_outputs
- Two functions: load_context and predict
- Three functions: load_context, predict and format_inputs
Show the answer and explanation
Answer: C
Why this is correct
The page says that when you package arbitrary Python code as an MLflow custom Python model, there are two required functions: load_context and predict. load_context holds anything that needs to be loaded just one time for the model to operate, such as model weights and a tokenizer. The documentation gives the reason that this minimizes the number of artifacts loaded during predict, which speeds up inference. predict holds all the logic that runs every time an input request is made, and preprocessing and postprocessing can be written as helpers called from inside it.
Why the other options are wrong
"Two functions: __init__ and predict" … The documentation does not list __init__ among the required functions. The place it names for anything that needs to be loaded only once is load_context, and its example loads the weights and tokenizer from context.artifacts there.
"Two functions: predict and format_outputs" … format_outputs is a helper function that the example author added to shape the output, not a function MLflow requires. The two required functions are load_context and predict, and postprocessing is called from within predict.
"Three functions: load_context, predict and format_inputs" … format_inputs is also a helper that the example adds to shape the input. The example's predict calls it first, but the functions listed as required are only load_context and predict.
Key point
In the documentation's example, load_context reads the model weights and the tokenizer from context.artifacts, and predict calls format_inputs, the model's predict and format_outputs in that order. Loading the weights inside predict would run the load on every request, which goes against the reason the documentation gives for leaving one-time loading to load_context.