cancel
Showing results for 
Search instead for 
Did you mean: 
Community Platform Discussions
Connect with fellow community members to discuss general topics related to the Databricks platform, industry trends, and best practices. Share experiences, ask questions, and foster collaboration within the community.
cancel
Showing results for 
Search instead for 
Did you mean: 

Performance issue while calling mlflow endpoint

sanjay
Valued Contributor II

Hi,

I have pyspark dataframe and pyspark udf which calls mlflow model for each row but its performance is too slow.

Here is sample code

def myfunc(input_text):
   restult = mlflowmodel.predict(input_text)
   return result

myfuncUDF = udf(myfunc,StringType())

df = spark.sql("select * from test")
df=df.withColumn("test_result",myfuncUDF("input_text"))

Please suggest how to improve the performance.

Regards,
Sanjay

9 REPLIES 9

sanjay
Valued Contributor II

Thank you Kaniz for the suggestions. This is really helpful. I even tried using applyInPandas. Not sure if this is better than spark UDF. If not can you help me in converting this function to pandas udf or any other optimized function.

 

def myfunc(input_text):
   restult = mlflowmodel.predict(input_text)
   return result
def myfuncUDF(pdf):
  pdf['test_result']=pdf["input_text"].apply(myfunc)
  return pdf
 
df = spark.sql("select * from test")
df = df.groupBy("id").applyInPandas(myfuncUDF)
 
Regards,
Sanjay

sanjay
Valued Contributor II

Thank you @Retired_mod, its really helpful and did worked. Another quick question, I have to pass 2 parameters as input to myfunc. Please help how to pass multiple parameters. 

def myfunc(input_text, param2):
   # Assuming mlflowmodel is defined elsewhere
   result = mlflowmodel.predict(input_text, param2)
   return result

# Create a Pandas UDF
@pandas_udf(StringType())
def myfunc_udf(input_text_series: pd.Series, param2_series: pd.Series) -> pd.Series:
   return input_text_series.apply(myfunc) ??

 

sanjay
Valued Contributor II

Hi Kaniz,

I started getting following error after using myfunc_udf with 2 parameters.

pythonException: 'ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

Regards,

Sanjay

sanjay
Valued Contributor II

Hi @Retired_mod Appreciate if you can help in resolving this issue.

Regards,

Sanjay

Isabeente
New Contributor II

I need to send two arguments to myfunc, thus I have another brief question. I need some guidance on how to pass in many parameters.

BR_DatabricksAI
Contributor

Hello Sanjay, 

Could you please share your code snippet as per latest changes?

sanjay
Valued Contributor II
Hi,
 
Here is code snippet as per latest changes.
 
def myfunc(t1,t2)
  return 'test'
 
@pandas_udf(psf.StringType())
def myfunc_udf(input_text_series: pd.Series, param2_series: pd.Series) -> pd.Series:
  def apply_myfunc(input_text, param2)
    return myfunc(input_text, param2)
  return input_text_series.apply(apply_myfunc, param2_series)
 
df.withColumn("result", myfunc_udf("input_text1", "input_text2"))
 
But I am getting error while running this

pythonException: 'ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

BR_DatabricksAI
Contributor

Hello Sanjay,

The above code don't have the df defined. Can you share your df.show() output. 

Isabeente
New Contributor II

So good

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group