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

Fix the distribution logic for the JAX trainer. #832

Merged
merged 3 commits into from
Sep 3, 2023

Conversation

qlzh727
Copy link
Member

@qlzh727 qlzh727 commented Sep 1, 2023

  1. The key fix here is too unwrap the jax.array from the trainable_variable/optimizer_variable before feed them to jax.jit train/eval function. Somehow JAX wasn't able to infer the sharding spec from the kerasVariable, which cause it to run with fully replicated weights.

  2. Lift the data distribution logic out and put it outside of the jitted function. I noticed this issue when I debug the sharding setting.

  3. Enforce the sharding constrain for the training state before the end of the jitted function. This will make sure the training state output have the same sharding step as the input. Otherwise it will cause jax to recompile the training function due to the sharding change.

  4. I don't have a way to unit test this, since we can't easily step into a jitted function. The early test result from OPT2 model shows a great reduction of per-device memory usage, which means the distribution is actually working now.

1. eval() should also do a build for uninitialized metrics.
2. model.metrics should take care of uncompiled model.
Copy link
Member

@fchollet fchollet left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

metrics.extend(self._metrics[:])
if self._compile_metrics is not None:
if getattr(self, "_compile_metrics", None) is not None:
Copy link
Member

Choose a reason for hiding this comment

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

Why this change?

  • The loss tracker should already be part of metrics
  • metrics should definitely be called after __init__ so _compile_metrics should exist (if not that seems like a bug)

Copy link
Member Author

Choose a reason for hiding this comment

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

I was hitting some test error for this. The loss_tracker is only inited after model.compile(), same for _compile_metrics .

This was causing issue when I try to access model.metric_variables for a uncompiled model, eg during model.predict().

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

I think you should be able to fix it by adding a if self.compiled: clause in _record_training_state_sharding_spec/etc to only look at optimizer variables and metric variables if the model is compiled, and not before.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, I think in this case, the if self.compiled should be added here, since _loss_tracker and _compile_metrics will only be populated after model.compile. Also the fix has to happen here, otherwise for a unacompiled model/layer, the model.metrics will raise an error which is a bad UX.

Copy link
Member

@fchollet fchollet left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@fchollet
Copy link
Member

fchollet commented Sep 3, 2023

As far as I can tell the test failure isn't related. I'll look into it.

@fchollet fchollet merged commit 2173cbb into keras-team:main Sep 3, 2023
5 of 6 checks passed
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