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

Don't duplicate frozen parameters during predict() #20851

Merged
merged 1 commit into from
Feb 4, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions keras/src/backend/jax/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,15 +308,15 @@ def predict_step(state, data):
return outputs, (state[0], non_trainable_variables)

if not self.run_eagerly and self.jit_compile:
predict_step = jax.jit(predict_step)
predict_step = jax.jit(predict_step, donate_argnums=0)

_step_function = self._make_function(
predict_step, concatenate_outputs=True
)

def step_function(state, iterator):
outputs, state = _step_function(state, iterator)
return outputs, state[1]
return outputs, state

self.predict_function = step_function

Expand Down Expand Up @@ -671,14 +671,20 @@ def append_to_outputs(batch_outputs, outputs):
state = self._get_jax_state(
trainable_variables=True,
non_trainable_variables=True,
purge_model_variables=True,
)
self._purge_model_variables(non_trainable_variables=True)
self._jax_state_synced = False
else:
state = (state[0], non_trainable_variables)
batch_outputs, non_trainable_variables = self.predict_function(
state, iterator
)
batch_outputs, state = self.predict_function(state, iterator)
(
trainable_variables,
non_trainable_variables,
) = state
self._jax_state = {
"trainable_variables": trainable_variables,
# I wouldn't recommend modifying non-trainable model state
# during predict(), but it's allowed.
"non_trainable_variables": non_trainable_variables,
}
outputs = append_to_outputs(batch_outputs, outputs)

# Dispatch callbacks. This takes care of async dispatch.
Expand All @@ -687,11 +693,6 @@ def append_to_outputs(batch_outputs, outputs):
if self.stop_predicting:
break

self._jax_state = {
# I wouldn't recommend modifying non-trainable model state
# during predict(), but it's allowed.
"non_trainable_variables": non_trainable_variables,
}
self.jax_state_sync()
callbacks.on_predict_end()
self._jax_state = None
Expand Down Expand Up @@ -819,10 +820,10 @@ def predict_on_batch(self, x):
def data():
yield (x,)

batch_outputs, non_trainable_variables = self.predict_function(
state, data()
)
batch_outputs, state = self.predict_function(state, data())
trainable_variables, non_trainable_variables = state
self._jax_state = {
"trainable_variables": trainable_variables,
"non_trainable_variables": non_trainable_variables,
}
self.jax_state_sync()
Expand Down Expand Up @@ -929,7 +930,7 @@ def _purge_model_variables(
):
"""Remove all the model variable for memory saving.

During JAX training, since the training function are stateless, we have
During JAX training, since the training function is stateless, we have
to pass in and get the model weights over and over, during which the
copy of the weights that attached to the Variable are still and
occupying extra memory. We remove those variable to save memory (for
Expand Down