-
Notifications
You must be signed in to change notification settings - Fork 39
feat: add Python implementation of accelerated OP #67
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
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0c783b6
chore: update build requirements
XuehaiPan 26dc334
feat: make CXX/CUDA extenstion optional
XuehaiPan 4fa9b86
feat: build pure-Python wheel
XuehaiPan efb7436
feat(workflows): enable pure-Python tests
XuehaiPan ecd4a43
docs(CHANGELOG): update CHANGELOG.md
XuehaiPan 2a08697
chore: update setup.py
XuehaiPan 18a0084
chore: use inplace operators
XuehaiPan 8653f7b
chore(workflows): update action versions
XuehaiPan 1840b64
chore: use torch.Tensor.copy_
XuehaiPan 75e8957
chore: split tests
XuehaiPan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: make CXX/CUDA extenstion optional
- Loading branch information
commit 26dc3349c68c4cb9f70253b7de8834c011be7906
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,15 @@ | ||
# Copyright 2022 MetaOPT Team. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ============================================================================== | ||
"""The Python implementation of accelerated ops.""" |
This file contains hidden or 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,114 @@ | ||
# Copyright 2022 MetaOPT Team. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ============================================================================== | ||
"""The Python implementation of accelerated AdamOp.""" | ||
|
||
# pylint: disable=invalid-name,too-many-arguments,unused-argument | ||
|
||
from typing import Tuple | ||
|
||
import torch | ||
|
||
|
||
def forward_( | ||
updates: torch.Tensor, | ||
mu: torch.Tensor, | ||
nu: torch.Tensor, | ||
b1: float, | ||
b2: float, | ||
eps: float, | ||
eps_root: float, | ||
count: int, | ||
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: | ||
"""Adam forward inplace.""" | ||
inv_one_minus_pow_b1 = 1.0 / (1.0 - pow(b1, count)) | ||
inv_one_minus_pow_b2 = 1.0 / (1.0 - pow(b2, count)) | ||
|
||
mu = mu.mul_(b1).add_(updates, alpha=1.0 - b1) | ||
nu = nu.mul_(b2).add_(updates.square(), alpha=1.0 - b2) | ||
updates.data.copy_( | ||
mu.mul(inv_one_minus_pow_b1).div_( | ||
nu.mul(inv_one_minus_pow_b2).add_(eps_root).sqrt_().add_(eps) | ||
) | ||
) | ||
return updates, mu, nu | ||
|
||
|
||
def forward_mu(updates: torch.Tensor, mu: torch.Tensor, b1: float) -> torch.Tensor: | ||
"""Adam forward mu.""" | ||
return mu.mul(b1).add_(updates, alpha=1.0 - b1) | ||
|
||
|
||
def forward_nu(updates: torch.Tensor, nu: torch.Tensor, b2: float) -> torch.Tensor: | ||
"""Adam forward nu.""" | ||
return nu.mul(b2).add_(updates.square(), alpha=1.0 - b2) | ||
|
||
|
||
def forward_updates( | ||
new_mu: torch.Tensor, | ||
new_nu: torch.Tensor, | ||
b1: float, | ||
b2: float, | ||
eps: float, | ||
eps_root: float, | ||
count: int, | ||
) -> torch.Tensor: | ||
"""Adam forward updates.""" | ||
inv_one_minus_pow_b1 = 1.0 / (1.0 - pow(b1, count)) | ||
inv_one_minus_pow_b2 = 1.0 / (1.0 - pow(b2, count)) | ||
return new_mu.mul(inv_one_minus_pow_b1).div_( | ||
new_nu.mul(inv_one_minus_pow_b2).add_(eps_root).sqrt_().add_(eps) | ||
) | ||
|
||
|
||
def backward_mu( | ||
dmu: torch.Tensor, updates: torch.Tensor, mu: torch.Tensor, b1: float | ||
) -> Tuple[torch.Tensor, torch.Tensor]: | ||
"""Adam backward mu.""" | ||
dupdates = dmu.mul(1.0 - b1) | ||
dmu = dmu.mul(b1) | ||
return dupdates, dmu | ||
|
||
|
||
def backward_nu( | ||
dnu: torch.Tensor, updates: torch.Tensor, nu: torch.Tensor, b2: float | ||
) -> Tuple[torch.Tensor, torch.Tensor]: | ||
"""Adam backward nu.""" | ||
dupdates = updates.mul(dnu).mul_(2.0 * (1.0 - b2)) | ||
dnu = dnu.mul(b2) | ||
return dupdates, dnu | ||
|
||
|
||
def backward_updates( | ||
dupdates: torch.Tensor, | ||
updates: torch.Tensor, | ||
new_mu: torch.Tensor, | ||
new_nu: torch.Tensor, | ||
b1: float, | ||
b2: float, | ||
count: int, | ||
) -> Tuple[torch.Tensor, torch.Tensor]: | ||
"""Adam backward updates.""" | ||
one_minus_pow_b1 = 1.0 - pow(b1, count) | ||
inv_one_minus_pow_b2 = 1.0 / (1.0 - pow(b2, count)) | ||
|
||
updates_div_new_mu = updates.div(new_mu) | ||
denominator = updates_div_new_mu.mul(one_minus_pow_b1) | ||
dnew_mu_out = dupdates.mul(updates_div_new_mu) | ||
XuehaiPan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
dnew_nu_out = dupdates.mul(updates).mul_(denominator.square()).mul_(-0.5 * inv_one_minus_pow_b2) | ||
XuehaiPan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
mask = new_mu == 0 | ||
dnew_mu_out[mask] = 0 | ||
dnew_nu_out[mask] = 0 | ||
return dnew_mu_out, dnew_nu_out |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1.0 -> double or float or?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will be cast to the same floating point type as
dmu.dtype
, usuallytorch.float32
.