未验证 提交 2f24b2d8 编写于 作者: W WangZhen 提交者: GitHub

[Dy2St]Support call backward() without params in dy2st (#49812)

* Support call backward() without params in dy2st
上级 989e39a5
...@@ -95,7 +95,8 @@ class SelectOutputInferShape : public framework::InferShapeBase { ...@@ -95,7 +95,8 @@ class SelectOutputInferShape : public framework::InferShapeBase {
void operator()(framework::InferShapeContext *context) const override { void operator()(framework::InferShapeContext *context) const override {
OP_INOUT_CHECK(context->HasInput("X"), "Input", "X", "SelectOutput"); OP_INOUT_CHECK(context->HasInput("X"), "Input", "X", "SelectOutput");
OP_INOUT_CHECK(context->HasInput("Mask"), "Input", "Mask", "SelectOutput"); OP_INOUT_CHECK(context->HasInput("Mask"), "Input", "Mask", "SelectOutput");
OP_INOUT_CHECK(context->HasOutputs("Out"), "Output", "Out", "SelectOutput"); OP_INOUT_CHECK(
context->HasOutputs("Out", true), "Output", "Out", "SelectOutput");
} }
}; };
......
...@@ -26,9 +26,9 @@ static PyObject *eager_api_run_program(PyObject *self, ...@@ -26,9 +26,9 @@ static PyObject *eager_api_run_program(PyObject *self,
PyObject *kwargs) { PyObject *kwargs) {
PyThreadState *tstate = nullptr; PyThreadState *tstate = nullptr;
try { try {
auto X = GetTensorListFromArgs("run_program", "X", args, 0, false); auto X = GetTensorListFromArgs("run_program", "X", args, 0, true);
auto Params = GetTensorListFromArgs("run_program", "Params", args, 1, true); auto Params = GetTensorListFromArgs("run_program", "Params", args, 1, true);
auto Out = GetTensorPtrListFromArgs("run_program", "Out", args, 2, false); auto Out = GetTensorPtrListFromArgs("run_program", "Out", args, 2, true);
auto OutScope = auto OutScope =
GetScopePtrListFromArgs("run_program", "OutScope", args, 3, false); GetScopePtrListFromArgs("run_program", "OutScope", args, 3, false);
auto DOut = GetTensorPtrListFromArgs("run_program", "DOut", args, 4, true); auto DOut = GetTensorPtrListFromArgs("run_program", "DOut", args, 4, true);
......
# Copyright (c) 2023 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.
import unittest
import numpy as np
import paddle
class Net(paddle.nn.Layer):
def __init__(self):
super(Net, self).__init__()
@paddle.jit.to_static
def forward(self, x):
out = x + 1
return out
class TestBackwardWithoutParams(unittest.TestCase):
def test_run(self):
net = Net()
x = paddle.ones([2, 2])
x.stop_gradient = False
out = net(x)
loss = paddle.mean(out)
loss.backward()
np.testing.assert_equal(x.grad.numpy(), np.full(x.shape, 0.25))
if __name__ == '__main__':
unittest.main()
...@@ -377,6 +377,7 @@ class TestTransform(TestTransformBase): ...@@ -377,6 +377,7 @@ class TestTransform(TestTransformBase):
if not isinstance(dy_outs, (tuple, list)): if not isinstance(dy_outs, (tuple, list)):
dy_outs = (dy_outs,) dy_outs = (dy_outs,)
self.dygraph_func.eval()
st_outs = self.get_static_output() st_outs = self.get_static_output()
if not isinstance(st_outs, (tuple, list)): if not isinstance(st_outs, (tuple, list)):
st_outs = (st_outs,) st_outs = (st_outs,)
......
...@@ -206,10 +206,6 @@ class PartialProgramLayer: ...@@ -206,10 +206,6 @@ class PartialProgramLayer:
else: else:
return core.Scope() return core.Scope()
@LazyInitialized
def __fake_vars(self):
return _create_fake_var()
@LazyInitialized @LazyInitialized
def _double_grads(self): def _double_grads(self):
return self._get_double_grads(self._origin_main_program) return self._get_double_grads(self._origin_main_program)
...@@ -604,7 +600,7 @@ class PartialProgramLayer: ...@@ -604,7 +600,7 @@ class PartialProgramLayer:
if isinstance(out, framework.Variable): if isinstance(out, framework.Variable):
targets.append(program.global_block().var(out.name)) targets.append(program.global_block().var(out.name))
if targets and self._params: if targets:
backward.gradients(targets=targets, inputs=[]) backward.gradients(targets=targets, inputs=[])
start_idx = len(main_program.block(0).ops) + 2 * len( start_idx = len(main_program.block(0).ops) + 2 * len(
...@@ -1123,12 +1119,7 @@ class PartialProgramLayer: ...@@ -1123,12 +1119,7 @@ class PartialProgramLayer:
) )
def _valid_vars(self, vars): def _valid_vars(self, vars):
""" return vars if vars else None
Note: run_program_op.InferShape requires `X`/'Out' not be null.
But it's common in dy2static, fake varBase is created to handle the
problem.
"""
return vars if vars else self.__fake_vars
def _create_fake_var(): def _create_fake_var():
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册