-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Haifeng Jin <haifeng-jin@users.noreply.github.com>
- Loading branch information
1 parent
4fcd567
commit 9986ffa
Showing
5 changed files
with
66 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import torch | ||
|
||
from keras_core import ops | ||
from keras_core import optimizers | ||
from keras_core.backend.torch.optimizers import torch_parallel_optimizer | ||
|
||
|
||
class Adadelta( | ||
torch_parallel_optimizer.TorchParallelOptimizer, optimizers.Adadelta | ||
): | ||
def _parallel_update_step( | ||
self, | ||
grads, | ||
variables, | ||
learning_rate, | ||
): | ||
keras_variables = variables | ||
variables = [v.value for v in variables] | ||
|
||
dtype = variables[0].dtype | ||
lr = ops.cast(learning_rate, dtype) | ||
rho = self.rho | ||
|
||
accumulated_grads = [ | ||
self._accumulated_grads[self._get_variable_index(variable)].value | ||
for variable in keras_variables | ||
] | ||
accumulated_delta_vars = [ | ||
self._accumulated_delta_vars[ | ||
self._get_variable_index(variable) | ||
].value | ||
for variable in keras_variables | ||
] | ||
torch._foreach_mul_(accumulated_grads, rho) | ||
torch._foreach_add_( | ||
accumulated_grads, torch._foreach_mul(grads, grads), alpha=1 - rho | ||
) | ||
|
||
def rms(x): | ||
return torch._foreach_sqrt(torch._foreach_add(x, self.epsilon)) | ||
|
||
delta_vars = torch._foreach_mul( | ||
torch._foreach_div( | ||
torch._foreach_mul(rms(accumulated_delta_vars), grads), | ||
rms(accumulated_grads), | ||
), | ||
-1, | ||
) | ||
torch._foreach_mul_(accumulated_delta_vars, rho) | ||
torch._foreach_add_( | ||
accumulated_delta_vars, | ||
torch._foreach_mul(delta_vars, delta_vars), | ||
alpha=1 - rho, | ||
) | ||
|
||
torch._foreach_add_(variables, delta_vars, alpha=lr) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters