Skip to content

Lazy-fit model_mu in R-learner so it's only trained when return_components is used#936

Open
aman-coder03 wants to merge 2 commits into
uber:masterfrom
aman-coder03:feature/rlearner-lazy-model-fit
Open

Lazy-fit model_mu in R-learner so it's only trained when return_components is used#936
aman-coder03 wants to merge 2 commits into
uber:masterfrom
aman-coder03:feature/rlearner-lazy-model-fit

Conversation

@aman-coder03

Copy link
Copy Markdown
Contributor

Proposed changes

follow up to #923 model_mu (the nuisance outcome model) was being fit on the full training data inside fit() every time, even when nobody ever calls predict(..., return_components=True) ,that's wasted work for the common case

this defers the fit: fit() now just caches the training data, and model_mu only gets fit the first time predict(return_components=True) is actually called, then cached so it's not re-fit on later calls

also fixes a related bug this uncovered: the bootstrap loops in fit_predict(return_ci=True) and estimate_ate(bootstrap_ci=True) save/restore model_mu and models_tau around resampling, but weren't saving/restoring the new cached training data used for the lazy fit. without the fix, calling predict(return_components=True) after a bootstrap run would silently fit model_mu on stale bootstrap-resampled data instead of the real training set

applies to BaseRLearner, BaseRClassifier, and XGBRRegressor

Types of changes

What types of changes does your code introduce to CausalML?
Put an x in the boxes that apply

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation Update (if none of the other choices apply)

Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code.

  • I have read the CONTRIBUTING doc
  • I have signed the CLA
  • Lint and unit tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)
  • Any dependent changes have been merged and published in downstream modules

Further comments

If this is a relatively large or complex change, kick off the discussion by explaining why you chose the solution you did and what alternatives you considered, etc. This PR template is adopted from appium.

@jeongyoonlee jeongyoonlee left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Deferring the model_mu fit requires pinning the full training set on the instance (self._mu_fit_X, self._mu_fit_y), which inflates a fitted R-learner from 2.7 KiB to 4.8 MiB at n=20k/p=30 (pickle). With save()/load() added in #929/#932, every saved R-learner now carries its training data — a steep cost to avoid one model_mu.fit(X, y), which is cheap for the default learners. Either keep the eager fit, or exclude the cached X/y from serialization so on-disk size doesn't regress.

The new tests cover only BaseRLearner; BaseRClassifier and XGBRRegressor get the same lazy machinery but no test — please add at least the classifier path.

Note this overlaps #935, which also rewrites BaseRLearner.fit() (and keeps the eager model_mu fit this PR removes) — they'll need to be sequenced.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants