-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
[API]Support static branch in paddle.to_tensor #45164
Merged
Merged
Changes from all commits
Commits
Show all changes
48 commits
Select commit
Hold shift + click to select a range
94724ec
fix_shape
feifei-111 23ed6e1
code style
feifei-111 1a02a6e
fix_var_shape
feifei-111 6f65b26
Merge remote-tracking branch 'upstream/develop' into fix_var_shape
feifei-111 3f54345
fix assert
feifei-111 7877146
fix to_tensor badreturn
feifei-111 5505531
added annotate
feifei-111 40e60cb
del no need change
feifei-111 69081cc
fix code style
feifei-111 e089910
fix code style
feifei-111 6969a9e
fix dtype
feifei-111 86fc94c
fix to_tensor
feifei-111 4b7444b
add test
feifei-111 b39cc2a
modify test
feifei-111 f7af87c
fix code style
feifei-111 cf8a14b
fix codestyle
feifei-111 b382071
fix transformer
feifei-111 0dfe2ae
fix test
feifei-111 166441b
code style
feifei-111 e8ced58
resume assign
feifei-111 0af1bd2
fix input dtype not exist
feifei-111 8a7a4c0
fix complex type
feifei-111 9055a64
code style
feifei-111 967eb1c
fix tests
feifei-111 63762dd
fix transformer for variable
feifei-111 3f5e3ed
fix stynax
feifei-111 332766d
cancel to_variable
feifei-111 bb5045f
fix
feifei-111 3408d86
annotation
feifei-111 4641f1f
code style
feifei-111 5bb2a95
style
feifei-111 7f2209f
cs
feifei-111 792643e
fix place
feifei-111 472175c
dtype check
feifei-111 2c9480c
fix place
feifei-111 e41b58b
cast
feifei-111 9c6e928
code style
feifei-111 570a5d4
typewrite err
feifei-111 df3ed2c
fix cudaplace
feifei-111 ab67fe8
add attr check
feifei-111 6bf2990
fix type str
feifei-111 bfee8d6
sue convert dtype
feifei-111 17f17d3
fix stop gradient
feifei-111 c029f7b
bug fix
feifei-111 8f76b90
fix complex type
feifei-111 8b9d353
code style
feifei-111 9206a18
fix device
feifei-111 aa11ad4
1
feifei-111 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
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
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
124 changes: 124 additions & 0 deletions
124
python/paddle/fluid/tests/unittests/dygraph_to_static/test_to_tensor.py
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,124 @@ | ||
# Copyright (c) 2021 PaddlePaddle 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. | ||
|
||
from __future__ import print_function | ||
|
||
import numpy | ||
import paddle | ||
import unittest | ||
import os | ||
import tempfile | ||
import paddle.inference as paddle_infer | ||
from paddle.fluid.framework import program_guard, Program | ||
import numpy as np | ||
from paddle.fluid import core | ||
|
||
|
||
def case0(x): | ||
a = paddle.to_tensor([1.0, 2.0, 3.0], dtype="int64") | ||
|
||
return a | ||
|
||
|
||
def case1(x): | ||
paddle.set_default_dtype("float64") | ||
a = paddle.to_tensor([1.0, 2.0, 3.0], stop_gradient=False) | ||
|
||
return a | ||
|
||
|
||
def case2(x): | ||
if core.is_compiled_with_cuda(): | ||
place = paddle.CUDAPlace(0) | ||
else: | ||
place = paddle.CPUPlace() | ||
a = paddle.to_tensor([1.0, 2.0, 3.0], | ||
place=place, | ||
dtype="int64", | ||
stop_gradient=False) | ||
|
||
return a | ||
|
||
|
||
def case3(x): | ||
paddle.set_default_dtype("float64") | ||
if core.is_compiled_with_cuda(): | ||
place = paddle.CUDAPlace(0) | ||
else: | ||
place = paddle.CPUPlace() | ||
a = paddle.to_tensor([1.0, 2.0, 3.0], place=place) | ||
|
||
return a | ||
|
||
|
||
class TestToTensorReturnVal(unittest.TestCase): | ||
|
||
def test_to_tensor_badreturn(self): | ||
paddle.disable_static() | ||
x = paddle.to_tensor([3]) | ||
|
||
a = paddle.jit.to_static(case0)(x) | ||
b = case0(x) | ||
self.assertTrue(a.dtype == b.dtype) | ||
self.assertTrue(a.stop_gradient == b.stop_gradient) | ||
self.assertTrue(a.place._equals(b.place)) | ||
|
||
a = paddle.jit.to_static(case1)(x) | ||
b = case1(x) | ||
self.assertTrue(a.dtype == b.dtype) | ||
self.assertTrue(a.stop_gradient == b.stop_gradient) | ||
self.assertTrue(a.place._equals(b.place)) | ||
|
||
a = paddle.jit.to_static(case2)(x) | ||
b = case2(x) | ||
self.assertTrue(a.dtype == b.dtype) | ||
self.assertTrue(a.stop_gradient == b.stop_gradient) | ||
self.assertTrue(a.place._equals(b.place)) | ||
|
||
a = paddle.jit.to_static(case3)(x) | ||
b = case3(x) | ||
self.assertTrue(a.dtype == b.dtype) | ||
self.assertTrue(a.stop_gradient == b.stop_gradient) | ||
self.assertTrue(a.place._equals(b.place)) | ||
|
||
|
||
class TestStatic(unittest.TestCase): | ||
|
||
def test_static(self): | ||
paddle.enable_static() | ||
main_prog = Program() | ||
starup_prog = Program() | ||
with program_guard(main_prog, starup_prog): | ||
if core.is_compiled_with_cuda(): | ||
place = paddle.CUDAPlace(0) | ||
else: | ||
place = paddle.CPUPlace() | ||
|
||
x = paddle.to_tensor(paddle.randn([5, 2]), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 单测需要再丰富一下,你这里并没有测试[var, 1, 1]的情况 |
||
dtype='float64', | ||
stop_gradient=False, | ||
place=place) | ||
|
||
out = paddle.static.nn.fc(x, 1) | ||
|
||
sgd = paddle.optimizer.SGD() | ||
sgd.minimize(paddle.mean(out)) | ||
|
||
exe = paddle.static.Executor() | ||
exe.run(starup_prog) | ||
res = exe.run(fetch_list=[x, out]) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
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.
不用加这一行