Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Decimal object to Double cast in ML Flow #6600

Merged
merged 8 commits into from
Sep 2, 2022

Conversation

shitaoli-db
Copy link
Contributor

@shitaoli-db shitaoli-db commented Aug 26, 2022

What changes are proposed in this pull request?

(Please fill in changes proposed in this fix)

How is this patch tested?

Added unit test for Decimal to float object in enforce schema cast.
Also added end to end test with databricks automl model inference, which has double schema in model, the inference was successful

image

image

Does this PR change the documentation?

  • No. You can skip the rest of this section.
  • Yes. Make sure the changed pages / sections render correctly by following the steps below.

Release Notes

Is this a user-facing change?

  • No. You can skip the rest of this section.
  • Yes. Give a description of this change to be included in the release notes for MLflow users.

Model inference now allow user to use Decimal object as input. Note decimal input will only be allowed when schema is double and the data will be casted to double and thus may lost precision.

What component(s), interfaces, languages, and integrations does this PR affect?

Components

  • area/artifacts: Artifact stores and artifact logging
  • area/build: Build and test infrastructure for MLflow
  • area/docs: MLflow documentation pages
  • area/examples: Example code
  • area/model-registry: Model Registry service, APIs, and the fluent client calls for Model Registry
  • area/models: MLmodel format, model serialization/deserialization, flavors
  • area/pipelines: Pipelines, Pipeline APIs, Pipeline configs, Pipeline Templates
  • area/projects: MLproject format, project running backends
  • area/scoring: MLflow Model server, model deployment tools, Spark UDFs
  • area/server-infra: MLflow Tracking server backend
  • area/tracking: Tracking Service, tracking client APIs, autologging

Interface

  • area/uiux: Front-end, user experience, plotting, JavaScript, JavaScript dev server
  • area/docker: Docker use across MLflow's components, such as MLflow Projects and MLflow Models
  • area/sqlalchemy: Use of SQLAlchemy in the Tracking Service or Model Registry
  • area/windows: Windows support

Language

  • language/r: R APIs and clients
  • language/java: Java APIs and clients
  • language/new: Proposals for new client languages

Integrations

  • integrations/azure: Azure and Azure ML integrations
  • integrations/sagemaker: SageMaker integrations
  • integrations/databricks: Databricks integrations

How should the PR be classified in the release notes? Choose one:

  • rn/breaking-change - The PR will be mentioned in the "Breaking Changes" section
  • rn/none - No description will be included. The PR will be mentioned only by the PR number in the "Small Bugfixes and Documentation Updates" section
  • rn/feature - A new user-facing feature worth mentioning in the release notes
  • rn/bug-fix - A user-facing bug fix worth mentioning in the release notes
  • rn/documentation - A user-facing documentation change worth mentioning in the release notes

@github-actions
Copy link

@shitaoli-db Thanks for the contribution! The DCO check failed. Please sign off your commits by following the instructions here: https://github.com/mlflow/mlflow/runs/8028008004. See https://github.com/mlflow/mlflow/blob/master/CONTRIBUTING.rst#sign-your-work for more details.

@github-actions github-actions bot added area/models MLmodel format, model serialization/deserialization, flavors rn/feature Mention under Features in Changelogs. labels Aug 26, 2022
Signed-off-by: Shitao Li <shitao.li@databricks.com>
Signed-off-by: Shitao Li <shitao.li@databricks.com>
@shitaoli-db shitaoli-db changed the title Add support for Decimal object to Double cast in ML Flow [ML-24550] Add support for Decimal object to Double cast in ML Flow Aug 26, 2022
@@ -397,6 +402,15 @@ def _enforce_mlflow_datatype(name, values: pandas.Series, t: DataType):
raise MlflowException(
"Failed to convert column {0} from type {1} to {2}.".format(name, values.dtype, t)
)
if t == DataType.double and values.dtype == decimal.Decimal:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought in our test the values.dtype is object, while type(values[0]) is decimal.Decimal. Can you confirm?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, verified
image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also our unit test and e2e notebook test all passed when object is Decimal

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I didn't know dtype == object and dtype == decimal.Decimal can both be true. LGTM!

@shitaoli-db shitaoli-db changed the title [ML-24550] Add support for Decimal object to Double cast in ML Flow Add support for Decimal object to Double cast in ML Flow Aug 26, 2022
@@ -397,6 +402,15 @@ def _enforce_mlflow_datatype(name, values: pandas.Series, t: DataType):
raise MlflowException(
"Failed to convert column {0} from type {1} to {2}.".format(name, values.dtype, t)
)
if t == DataType.double and values.dtype == decimal.Decimal:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I didn't know dtype == object and dtype == decimal.Decimal can both be true. LGTM!

@@ -397,6 +402,15 @@ def _enforce_mlflow_datatype(name, values: pandas.Series, t: DataType):
raise MlflowException(
"Failed to convert column {0} from type {1} to {2}.".format(name, values.dtype, t)
)
if t == DataType.double and values.dtype == decimal.Decimal:
# NB: Pyspark Decimal columne get converted to decimal.Decimal when converted to pandas
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: columne -> column.
in order to support ... - is part of the comment missing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. Done.

Copy link
Contributor

@tomasatdatabricks tomasatdatabricks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

Can you also update docs please?
Can we make this work only in spark_udf environment or protect it with an argument? E.g. we could use an env variable or extra argument to pyfunc.load_model. Would that be feasible?

Signed-off-by: Shitao Li <shitao.li@databricks.com>
@shitaoli-db
Copy link
Contributor Author

Let me try with an extra argument first. Do you know how did we generate this message?

image

If we use the extra argument, then we have to customize this message in the UI to let customer know that if they want their model with decimal training data work in inference, they have to pass an extra argument.

Looks good.

Can you also update docs please? Can we make this work only in spark_udf environment or protect it with an argument? E.g. we could use an env variable or extra argument to pyfunc.load_model. Would that be feasible?

model_uri: str,
suppress_warnings: bool = False,
dst_path: str = None,
support_decimal: bool = False,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to accomplish the goal of providing conditional decimal enforcement without adding this flag to the mlflow.pyfunc.load_model() API? I'd rather not extend this API for a niche use case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally don't think we need this flag at all. Can we just always enable this conversion? I don't see any real use case of setting that to false (or only add that flag later when we confirm it's a real use case).

@dbczumar @tomasatdatabricks what do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think tom provided 2 approach to me
image

The other option I could think on top of my head is use environment variable

https://github.com/mlflow/mlflow/blob/master/mlflow/environment_variables.py

I am not sure which approach is more preferrable, let's have a quick agreement.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @yxiong here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted back the change, now we always enable the cast.

@shitaoli-db shitaoli-db requested review from tomasatdatabricks and removed request for dbczumar August 31, 2022 17:22
Copy link
Collaborator

@dbczumar dbczumar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@dbczumar dbczumar enabled auto-merge (squash) August 31, 2022 21:58
Signed-off-by: Shitao Li <shitao.li@databricks.com>
auto-merge was automatically disabled August 31, 2022 22:46

Head branch was pushed to by a user without write access

@shitaoli-db shitaoli-db requested review from dbczumar and removed request for tomasatdatabricks August 31, 2022 23:24
@shitaoli-db
Copy link
Contributor Author

autoformat

Signed-off-by: Shitao Li <shitao.li@databricks.com>
…ved the enforce scehma away so we have to do same.

Signed-off-by: Shitao Li <shitao.li@databricks.com>
Signed-off-by: Shitao Li <shitao.li@databricks.com>
@shitaoli-db
Copy link
Contributor Author

automerge

@dbczumar dbczumar enabled auto-merge (squash) September 2, 2022 06:27
Signed-off-by: Shitao Li <shitao.li@databricks.com>
auto-merge was automatically disabled September 2, 2022 17:31

Head branch was pushed to by a user without write access

@dbczumar dbczumar enabled auto-merge (squash) September 2, 2022 21:06
@dbczumar dbczumar merged commit eb3588a into mlflow:master Sep 2, 2022
prithvikannan pushed a commit to prithvikannan/mlflow that referenced this pull request Sep 6, 2022
* Add Decimal to double cast when enforce the schema.

Signed-off-by: Shitao Li <shitao.li@databricks.com>

* Add unit test for casting from Decimal to double.

Signed-off-by: Shitao Li <shitao.li@databricks.com>

* Fix the comment in the python file.

Signed-off-by: Shitao Li <shitao.li@databricks.com>

* Fix lint problem.

Signed-off-by: Shitao Li <shitao.li@databricks.com>

* Move the change to utils since merge conflict. Previous commit had moved the enforce scehma away so we have to do same.

Signed-off-by: Shitao Li <shitao.li@databricks.com>

* Clean up __init__.py since merge resolver did not handle merge conflict.

Signed-off-by: Shitao Li <shitao.li@databricks.com>

* Lint the utils.py.

Signed-off-by: Shitao Li <shitao.li@databricks.com>

Signed-off-by: Shitao Li <shitao.li@databricks.com>
Signed-off-by: Prithvi Kannan <prithvi.kannan@databricks.com>
prithvikannan pushed a commit to prithvikannan/mlflow that referenced this pull request Sep 7, 2022
* Add Decimal to double cast when enforce the schema.

Signed-off-by: Shitao Li <shitao.li@databricks.com>

* Add unit test for casting from Decimal to double.

Signed-off-by: Shitao Li <shitao.li@databricks.com>

* Fix the comment in the python file.

Signed-off-by: Shitao Li <shitao.li@databricks.com>

* Fix lint problem.

Signed-off-by: Shitao Li <shitao.li@databricks.com>

* Move the change to utils since merge conflict. Previous commit had moved the enforce scehma away so we have to do same.

Signed-off-by: Shitao Li <shitao.li@databricks.com>

* Clean up __init__.py since merge resolver did not handle merge conflict.

Signed-off-by: Shitao Li <shitao.li@databricks.com>

* Lint the utils.py.

Signed-off-by: Shitao Li <shitao.li@databricks.com>

Signed-off-by: Shitao Li <shitao.li@databricks.com>
Signed-off-by: Prithvi Kannan <prithvi.kannan@databricks.com>
nnethery pushed a commit to nnethery/mlflow that referenced this pull request Feb 1, 2024
* Add Decimal to double cast when enforce the schema.

Signed-off-by: Shitao Li <shitao.li@databricks.com>

* Add unit test for casting from Decimal to double.

Signed-off-by: Shitao Li <shitao.li@databricks.com>

* Fix the comment in the python file.

Signed-off-by: Shitao Li <shitao.li@databricks.com>

* Fix lint problem.

Signed-off-by: Shitao Li <shitao.li@databricks.com>

* Move the change to utils since merge conflict. Previous commit had moved the enforce scehma away so we have to do same.

Signed-off-by: Shitao Li <shitao.li@databricks.com>

* Clean up __init__.py since merge resolver did not handle merge conflict.

Signed-off-by: Shitao Li <shitao.li@databricks.com>

* Lint the utils.py.

Signed-off-by: Shitao Li <shitao.li@databricks.com>

Signed-off-by: Shitao Li <shitao.li@databricks.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/models MLmodel format, model serialization/deserialization, flavors rn/feature Mention under Features in Changelogs.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants