-
Notifications
You must be signed in to change notification settings - Fork 685
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add addcdiv * fix tensor_functions * fix inplace * add test number * rename consistent to global
- Loading branch information
Showing
13 changed files
with
234 additions
and
12 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 |
---|---|---|
|
@@ -199,6 +199,7 @@ Pointwise Ops | |
arccos | ||
arccosh | ||
add | ||
addcdiv | ||
addcmul | ||
asin | ||
asinh | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
""" | ||
Copyright 2020 The OneFlow Authors. 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. | ||
""" | ||
import oneflow | ||
from oneflow.framework.docstr.utils import add_docstr | ||
|
||
add_docstr( | ||
oneflow.addcdiv, | ||
r""" | ||
addcdiv(input, tensor1, tensor2, *, value=1) -> Tensor | ||
This function is equivalent to PyTorch’s addcdiv function. | ||
The documentation is referenced from: https://pytorch.org/docs/1.10/generated/torch.addcdiv.html. | ||
Performs the element-wise division of :attr:`tensor1` by :attr:`tensor2`, | ||
multiply the result by the scalar :attr:`value` and add it to :attr:`input`. | ||
.. math:: | ||
\text{out}_i = \text{input}_i + \text{value} \times \frac{\text{tensor1}_i}{\text{tensor2}_i} | ||
The shapes of :attr:`input`, :attr:`tensor1`, and :attr:`tensor2` must be | ||
`broadcastable`. | ||
For inputs of type `FloatTensor` or `DoubleTensor`, :attr:`value` must be | ||
a real number, otherwise an integer. | ||
Args: | ||
input (Tensor): the tensor to be added | ||
tensor1 (Tensor): the numerator tensor | ||
tensor2 (Tensor): the denominator tensor | ||
Keyword args: | ||
value (Number, optional): multiplier for :math:`\text{{tensor1}} / \text{{tensor2}}` | ||
Example:: | ||
>>> import oneflow as flow | ||
>>> input = flow.tensor([ 0.3810, 1.2774, -0.2972, -0.3719]) | ||
>>> tensor1 = flow.tensor([0.8032, 0.2930, -0.8113, -0.2308]) | ||
>>> tensor2 = flow.tensor([[0.5], [1]]) | ||
>>> output = flow.addcdiv(input, tensor1, tensor2) | ||
>>> output.shape | ||
oneflow.Size([2, 4]) | ||
""", | ||
) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
""" | ||
Copyright 2020 The OneFlow Authors. 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. | ||
""" | ||
import unittest | ||
from oneflow.test_utils.automated_test_util import * | ||
import oneflow as flow | ||
import oneflow.unittest | ||
|
||
|
||
@flow.unittest.skip_unless_1n1d() | ||
class TestAddcdiv(flow.unittest.TestCase): | ||
@autotest(n=5) | ||
def test_addcdiv(test_case): | ||
device = random_device() | ||
ndim = random(2, 4).to(int).value() | ||
shape = [random(2, 4) for i in range(ndim)] | ||
input = random_tensor(ndim, *shape).to(device) | ||
tensor1 = random_tensor(ndim, *shape).to(device) | ||
tensor2 = random_tensor(ndim, *shape).to(device) | ||
value = random(2, 4).to(int) | ||
output = torch.addcdiv(input, tensor1, tensor2, value=value) | ||
return output | ||
|
||
@autotest(n=5) | ||
def test_tensor_addcdiv(test_case): | ||
device = random_device() | ||
ndim = random(2, 4).to(int).value() | ||
shape = [random(2, 4) for i in range(ndim)] | ||
input = random_tensor(ndim, *shape).to(device) | ||
tensor1 = random_tensor(ndim, *shape).to(device) | ||
tensor2 = random_tensor(ndim, *shape).to(device) | ||
value = random(2, 4).to(int) | ||
output = input.addcdiv(tensor1, tensor2, value=value) | ||
return output | ||
|
||
@autotest(n=5) | ||
def test_tensor_addcdiv_inplace(test_case): | ||
device = random_device() | ||
ndim = random(2, 4).to(int).value() | ||
shape = [random(2, 4) for i in range(ndim)] | ||
input = random_tensor(ndim, *shape).to(device) | ||
input = input + 1.0 | ||
tensor1 = random_tensor(ndim, *shape).to(device) | ||
tensor2 = random_tensor(ndim, *shape).to(device) | ||
value = random(2, 4).to(int) | ||
input.addcdiv_(tensor1, tensor2, value=value) | ||
return input | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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,44 @@ | ||
""" | ||
Copyright 2020 The OneFlow Authors. 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. | ||
""" | ||
import unittest | ||
|
||
import oneflow as flow | ||
import oneflow.unittest | ||
from oneflow.test_utils.automated_test_util import * | ||
|
||
|
||
@autotest(n=1, check_graph=False) | ||
def _test_addcdiv(test_case, ndim, placement, sbp): | ||
shape = [random(2, 4) * 8 for i in range(ndim)] | ||
input = random_tensor(ndim, *shape).to_global(placement=placement, sbp=sbp) | ||
tensor1 = random_tensor(ndim, *shape).to_global(placement=placement, sbp=sbp) | ||
tensor2 = random_tensor(ndim, *shape).to_global(placement=placement, sbp=sbp) | ||
value = random(2, 4).to(int) | ||
output = torch.addcdiv(input, tensor1, tensor2, value=value) | ||
return output | ||
|
||
|
||
class TestModule(flow.unittest.TestCase): | ||
@globaltest | ||
def test_addcdiv(test_case): | ||
ndim = random(2, 4).to(int).value() | ||
for placement in all_placement(): | ||
for sbp in all_sbp(placement, max_dim=ndim): | ||
_test_addcdiv(test_case, ndim, placement, sbp) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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