<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Runtime error using MLFlow and Spark on databricks in Machine Learning</title>
    <link>https://community.databricks.com/t5/machine-learning/runtime-error-using-mlflow-and-spark-on-databricks/m-p/14388#M774</link>
    <description>&lt;P&gt;Here is some model I created:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;class SomeModel(mlflow.pyfunc.PythonModel):
    def predict(self, context, input):
        # do fancy ML stuff
        # log results
        pandas_df = pd.DataFrame(...insert predictions here...)
        spark_df = spark.createDataFrame(pandas_df)
        spark_df.write.saveAsTable('tablename', mode='append')&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm trying to log my model in this manner by calling it later in my code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;with mlflow.start_run(run_name="SomeModel_run"):
    model = SomeModel()
    mlflow.pyfunc.log_model("somemodel", python_model=model)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Unfortunately it gives me this Error Message:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;RuntimeError: It appears that you are attempting to reference SparkContext from a broadcast variable, action, or transformation. SparkContext can only be used on the driver, not in code that it run on workers. For more information, see SPARK-5063.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The error is caused because of the line&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;mlflow.pyfunc.log_model("somemodel", python_model=model)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If I comment it out my model will make its predictions and log the results in my table.&lt;/P&gt;&lt;P&gt;Alternatively, removing the lines in my predict function where I call spark to create a dataframe and save the table, I am able to log my model.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;How do I go about resolving this issue? I need my model to not only write to the table but also be logged&lt;/P&gt;</description>
    <pubDate>Thu, 07 Jul 2022 15:49:48 GMT</pubDate>
    <dc:creator>ptawil</dc:creator>
    <dc:date>2022-07-07T15:49:48Z</dc:date>
    <item>
      <title>Runtime error using MLFlow and Spark on databricks</title>
      <link>https://community.databricks.com/t5/machine-learning/runtime-error-using-mlflow-and-spark-on-databricks/m-p/14388#M774</link>
      <description>&lt;P&gt;Here is some model I created:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;class SomeModel(mlflow.pyfunc.PythonModel):
    def predict(self, context, input):
        # do fancy ML stuff
        # log results
        pandas_df = pd.DataFrame(...insert predictions here...)
        spark_df = spark.createDataFrame(pandas_df)
        spark_df.write.saveAsTable('tablename', mode='append')&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm trying to log my model in this manner by calling it later in my code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;with mlflow.start_run(run_name="SomeModel_run"):
    model = SomeModel()
    mlflow.pyfunc.log_model("somemodel", python_model=model)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Unfortunately it gives me this Error Message:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;RuntimeError: It appears that you are attempting to reference SparkContext from a broadcast variable, action, or transformation. SparkContext can only be used on the driver, not in code that it run on workers. For more information, see SPARK-5063.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The error is caused because of the line&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;mlflow.pyfunc.log_model("somemodel", python_model=model)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If I comment it out my model will make its predictions and log the results in my table.&lt;/P&gt;&lt;P&gt;Alternatively, removing the lines in my predict function where I call spark to create a dataframe and save the table, I am able to log my model.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;How do I go about resolving this issue? I need my model to not only write to the table but also be logged&lt;/P&gt;</description>
      <pubDate>Thu, 07 Jul 2022 15:49:48 GMT</pubDate>
      <guid>https://community.databricks.com/t5/machine-learning/runtime-error-using-mlflow-and-spark-on-databricks/m-p/14388#M774</guid>
      <dc:creator>ptawil</dc:creator>
      <dc:date>2022-07-07T15:49:48Z</dc:date>
    </item>
    <item>
      <title>Re: Runtime error using MLFlow and Spark on databricks</title>
      <link>https://community.databricks.com/t5/machine-learning/runtime-error-using-mlflow-and-spark-on-databricks/m-p/14389#M775</link>
      <description>&lt;P&gt;this sis something new we have to explore this , do you have any docs that you re following here &lt;/P&gt;</description>
      <pubDate>Sun, 18 Dec 2022 07:26:34 GMT</pubDate>
      <guid>https://community.databricks.com/t5/machine-learning/runtime-error-using-mlflow-and-spark-on-databricks/m-p/14389#M775</guid>
      <dc:creator>Aviral-Bhardwaj</dc:creator>
      <dc:date>2022-12-18T07:26:34Z</dc:date>
    </item>
    <item>
      <title>Re: Runtime error using MLFlow and Spark on databricks</title>
      <link>https://community.databricks.com/t5/machine-learning/runtime-error-using-mlflow-and-spark-on-databricks/m-p/14390#M776</link>
      <description>&lt;P&gt;Any updates on this? I am running into the same issue&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;@Patrick Tawil​&amp;nbsp;were you able to solve this problem? If so, do you mind sharing?&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jun 2023 15:08:01 GMT</pubDate>
      <guid>https://community.databricks.com/t5/machine-learning/runtime-error-using-mlflow-and-spark-on-databricks/m-p/14390#M776</guid>
      <dc:creator>Nikhil3107</dc:creator>
      <dc:date>2023-06-07T15:08:01Z</dc:date>
    </item>
  </channel>
</rss>

