Skip to content

fix: model.fit metric not collected issue. #1085

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

Merged
merged 8 commits into from
Oct 15, 2024
Merged

Conversation

Genesis929
Copy link
Collaborator

Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:

  • Make sure to open an issue as a bug/issue before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea
  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary)

Fixes #<issue_number_goes_here> 🦕

@product-auto-label product-auto-label bot added size: xs Pull request size is extra small. api: bigquery Issues related to the googleapis/python-bigquery-dataframes API. labels Oct 15, 2024
@Genesis929 Genesis929 marked this pull request as ready for review October 15, 2024 17:15
@Genesis929 Genesis929 requested review from a team as code owners October 15, 2024 17:15
@Genesis929 Genesis929 added the owlbot:run Add this label to trigger the Owlbot post processor. label Oct 15, 2024
@gcf-owl-bot gcf-owl-bot bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Oct 15, 2024
@Genesis929 Genesis929 requested review from chelsea-lin and shobsi and removed request for chelsea-lin October 15, 2024 18:04
@@ -273,6 +273,8 @@ def _create_model_ref(
def _create_model_with_sql(self, session: bigframes.Session, sql: str) -> BqmlModel:
# fit the model, synchronously
_, job = session._start_query_ml_ddl(sql)
if session._metrics is not None:
session._metrics.count_job_stats(job)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we not do this inside _start_query_ml_ddl? That way all ML DDLs will be accounted in the session metrics

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Updated, and added an assertion in test for register.

@@ -31,8 +31,14 @@ def test_linear_regression_configure_fit_score(penguins_df_default_index, datase
]
]
y_train = df[["body_mass_g"]]

start_execution_count = df._block._expr.session._metrics.execution_count

model.fit(X_train, y_train)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we have the opportunity to make this work work all ML APIs - fit, score, predict, transform, .... We should write such tests for all APIs by writing the metrics collection logic in more central place in the code.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

score, predict works, seems internally is calling other functions.

Copy link
Contributor

@shobsi shobsi Oct 15, 2024

Choose a reason for hiding this comment

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

Good to know, thanks for checking! We could just pick one or two of the existing tests that does fit, score, predict, register and transform and assert everywhere that we are counting stats for those operations.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Test Added.

@product-auto-label product-auto-label bot added size: s Pull request size is small. and removed size: xs Pull request size is extra small. labels Oct 15, 2024
@Genesis929 Genesis929 requested a review from shobsi October 15, 2024 20:23
@@ -1373,7 +1373,14 @@ def _start_query_ml_ddl(
# https://cloud.google.com/bigquery/docs/customer-managed-encryption#encrypt-model
job_config.destination_encryption_configuration = None

return bf_io_bigquery.start_query_with_client(self.bqclient, sql, job_config)
results_iterator, query_job = bf_io_bigquery.start_query_with_client(
Copy link
Contributor

Choose a reason for hiding this comment

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

I see start_query_with_client already has the stats update

if metrics is not None:
metrics.count_job_stats(query_job)

so feels like we could be adding double counting

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's not double counting, but yes here we can send the metric into the start_query_with_client, thanks for point that out.

model.fit(X_train, y_train)

end_execution_count = df._block._expr.session._metrics.execution_count
assert end_execution_count - start_execution_count == 2
Copy link
Contributor

Choose a reason for hiding this comment

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

Worth adding a comment why fit does 2 queries

@Genesis929 Genesis929 requested a review from shobsi October 15, 2024 21:51
@Genesis929 Genesis929 merged commit 06cec00 into main Oct 15, 2024
20 of 23 checks passed
@Genesis929 Genesis929 deleted the model_fit_metric_fix branch October 15, 2024 23:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: bigquery Issues related to the googleapis/python-bigquery-dataframes API. size: s Pull request size is small.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants