BLACK FRIDAY SALE – Unlock Up to 25% OFF on All Exam Prep! - Ends In 00:00:00 Coupon code: SAVE25
Welcome to Pass4Success

- Free Preparation Discussions

Databricks Certified Associate Developer for Apache Spark 3.5 Exam - Topic 7 Question 8 Discussion

Posted by Rikki on Nov 22, 2025 at 07:04 PM

An MLOps engineer is building a Pandas UDF that applies a language model that translates English strings into Spanish. The initial code is loading the model on every call to the UDF, which is hurting the performance of the data pipeline.

The initial code is:

def in_spanish_inner(df: pd.Series) -> pd.Series:

model = get_translation_model(target_lang='es')

return df.apply(model)

in_spanish = sf.pandas_udf(in_spanish_inner, StringType())

How can the MLOps engineer change this code to reduce how many times the language model is loaded?

Suggested Answer: D

The provided code defines a Pandas UDF of type Series-to-Series, where a new instance of the language model is created on each call, which happens per batch. This is inefficient and results in significant overhead due to repeated model initialization.

To reduce the frequency of model loading, the engineer should convert the UDF to an iterator-based Pandas UDF (Iterator[pd.Series] -> Iterator[pd.Series]). This allows the model to be loaded once per executor and reused across multiple batches, rather than once per call.

From the official Databricks documentation:

''Iterator of Series to Iterator of Series UDFs are useful when the UDF initialization is expensive... For example, loading a ML model once per executor rather than once per row/batch.''

--- Databricks Official Docs: Pandas UDFs

Correct implementation looks like:

python

CopyEdit

@pandas_udf('string')

def translate_udf(batch_iter: Iterator[pd.Series]) -> Iterator[pd.Series]:

model = get_translation_model(target_lang='es')

for batch in batch_iter:

yield batch.apply(model)

This refactor ensures the get_translation_model() is invoked once per executor process, not per batch, significantly improving pipeline performance.


Get Premium Databricks Certified Associate Developer for Apache Spark 3.5 Questions as Interactive Web-Based Practice Test or PDF Real Exam Format Detailed Explanations Instant Access Get Your Practice Test
29 comments · newest first
HE
Henriette 8 months ago
Overall, I feel like reducing model loads is key. Any option that does that is worth considering!
FR
Francesco 8 months ago
D sounds interesting too. Iterators could help manage memory better.
DA
Davida 8 months ago
I’m leaning towards B. Scalar UDFs seem more efficient for single values.
FR
Frederica 8 months ago
Yeah, but option C could also work. It might optimize the pipeline better.
RO
Rosalyn 8 months ago
This question is tricky! I feel like it tests our understanding of UDFs.
NA
Nakita 9 months ago
Wait, why load the model every time? That’s wild!
AN
Annice 9 months ago
D) seems like overkill for this problem.
JI
Jillian 9 months ago
C) sounds interesting, but will it really help?
MA
Mariann 10 months ago
I think B) is the way to go!
BO
Bonita 10 months ago
A) Convert to a PySpark UDF for better performance.
DU
Dudley 10 months ago
Ah, the joys of optimizing data pipelines! I hope the MLOps engineer has a good sense of humor.
AB
Abraham 10 months ago
D) looks interesting, but I'm not sure if it's the most straightforward solution here.
TA
Tarra 10 months ago
Hmm, I wonder if the model can be cached somehow to improve performance even further.
RI
Rikki 10 months ago
I’m a bit confused about the difference between Series-Scalar and Iterator UDFs. I think one of them might help with the model loading issue, but I can't recall which.
AL
Alpha 11 months ago
I practiced a similar question where we had to optimize UDFs, and I feel like using mapInPandas might be the right approach here.
GI
Gilma 11 months ago
The key here is to find a way to load the model only once, rather than on every call. I think options B or D are the most promising approaches to achieve that.
CY
Cyndy 11 months ago
Option C, running the function in a mapInPandas() call, could also be a good solution. That way the model is only loaded once per partition, which could improve performance.
ED
Edward 11 months ago
B) seems like the best option to reduce the number of times the language model is loaded.
GE
Gertude 11 months ago
I think converting the Pandas UDF to a PySpark UDF could help, but I need to double-check if that actually reduces model loading times.
DE
Derick 12 months ago
I remember we discussed how loading models repeatedly can slow down performance, but I'm not sure which option would best address that.
RO
Rolf 12 months ago
I'm leaning towards option D - converting to an Iterator[Series]-Iterator[Series] UDF. That might allow for even more optimization by loading the model once per batch of data.
MA
Magda 1 year ago
I think option A is the best. PySpark UDFs load models once, right?
EF
Effie 1 year ago
I'm a bit confused by the different UDF types mentioned. I'll need to review the differences between Series-Series, Series-Scalar, and Iterator[Series]-Iterator[Series] UDFs to decide the best approach here.
MA
Maryln 1 year ago
This looks like a performance optimization question. I'd start by considering option B - converting the Pandas UDF to a Series-Scalar UDF. That way, the model can be loaded once and reused for each row.
RE
Refugia 7 months ago
↳ replying to Maryln
I think B is the most straightforward solution here.
JA
Jani 7 months ago
↳ replying to Maryln
C could work, but it might not be as efficient as B.
BI
Billye 7 months ago
↳ replying to Maryln
What about option C? Could that work too?
ER
Ernie 7 months ago
↳ replying to Maryln
Definitely! Loading the model once is key for performance.
CA
Catherin 8 months ago
↳ replying to Maryln
I agree, option B seems like the best choice. Reusing the model will save time.
0/2000 characters

Save Cancel