@@ -30,7 +30,6 @@ limitations under the License. */
3030
3131#include " paddle/fluid/imperative/all_reduce.h"
3232#include " paddle/fluid/imperative/amp_auto_cast.h"
33- #include " paddle/fluid/imperative/backward_strategy.h"
3433#include " paddle/fluid/imperative/basic_engine.h"
3534#include " paddle/fluid/imperative/data_loader.h"
3635#include " paddle/fluid/imperative/layer.h"
@@ -507,50 +506,6 @@ void BindImperative(py::module *m_ptr) {
507506 []() { memory::allocation::MemoryMapFdSet::Instance ().Clear (); });
508507#endif
509508
510- py::class_<imperative::detail::BackwardStrategy> backward_strategy (
511- m, " BackwardStrategy" , R"DOC(
512-
513- BackwardStrategy is a descriptor of how to run the backward process.
514-
515- **Note**:
516- **This API is only available in** `Dygraph <../../user_guides/howto/dygraph/DyGraph.html>`_ **Mode**
517-
518- Attribute:
519- **sort_sum_gradient**:
520-
521- If framework will sum the gradient by the reverse order of trace. eg. x_var ( :ref:`api_guide_Variable` ) will be the input of multiple OP such as :ref:`api_fluid_layers_scale` , this attr will decide if framework will sum gradient of `x_var` by the reverse order.
522-
523- By Default: False
524-
525- Examples:
526- .. code-block:: python
527-
528- import numpy as np
529- import paddle.fluid as fluid
530-
531- x = np.ones([2, 2], np.float32)
532- with fluid.dygraph.guard():
533- x_var = fluid.dygraph.to_variable(x)
534- sums_inputs = []
535- # x_var will be multi-scales' input here
536- for _ in range(10):
537- sums_inputs.append(fluid.layers.scale(x_var))
538- ret2 = fluid.layers.sums(sums_inputs)
539- loss2 = fluid.layers.reduce_sum(ret2)
540- backward_strategy = fluid.dygraph.BackwardStrategy()
541- backward_strategy.sort_sum_gradient = True
542- loss2.backward(backward_strategy)
543- )DOC" );
544- backward_strategy.def (py::init ())
545- .def_property (" sort_sum_gradient" ,
546- [](const imperative::detail::BackwardStrategy &self) {
547- return self.sorted_sum_gradient_ ;
548- },
549- [](imperative::detail::BackwardStrategy &self,
550- bool sorted_sum_gradient) {
551- self.sorted_sum_gradient_ = sorted_sum_gradient;
552- });
553-
554509 m.def (" start_imperative_gperf_profiler" ,
555510 []() { imperative::StartProfile (); });
556511
@@ -745,21 +700,18 @@ void BindImperative(py::module *m_ptr) {
745700 inputs2.append(tmp)
746701 ret2 = fluid.layers.sums(inputs2)
747702 loss2 = fluid.layers.reduce_sum(ret2)
748- backward_strategy = fluid.dygraph.BackwardStrategy()
749- backward_strategy.sort_sum_gradient = True
750- loss2.backward(backward_strategy)
703+ loss2.backward()
751704 print(loss2.gradient())
752705 loss2.clear_gradient()
753706 print("After clear {}".format(loss2.gradient()))
754707 )DOC" )
755708 .def (" _run_backward" ,
756- [](imperative::VarBase &self,
757- const imperative::detail::BackwardStrategy &bckst,
758- const imperative::Tracer &tracer, bool retain_graph) {
709+ [](imperative::VarBase &self, const imperative::Tracer &tracer,
710+ bool retain_graph) {
759711 // TODO(jiabin): when we impl more backward execution we can
760712 // select them
761713 auto *engine = tracer.GetEngine ();
762- engine->Init (&self, bckst, retain_graph);
714+ engine->Init (&self, retain_graph);
763715 VLOG (3 ) << " Start backward" ;
764716 engine->Execute ();
765717 VLOG (3 ) << " Finish backward" ;
@@ -1024,13 +976,11 @@ void BindImperative(py::module *m_ptr) {
1024976 &output_targets,
1025977 const std::vector<std::shared_ptr<imperative::VarBase>> &output_grads,
1026978 const std::vector<std::shared_ptr<imperative::VarBase>> &no_grad_vars,
1027- const platform::Place &place,
1028- const imperative::detail::BackwardStrategy &strategy,
1029- bool create_graph, bool retain_graph, bool allow_unused,
1030- bool only_inputs) {
979+ const platform::Place &place, bool create_graph, bool retain_graph,
980+ bool allow_unused, bool only_inputs) {
1031981 imperative::PartialGradEngine engine (
1032982 input_targets, output_targets, output_grads, no_grad_vars, place,
1033- strategy, create_graph, retain_graph, allow_unused, only_inputs);
983+ create_graph, retain_graph, allow_unused, only_inputs);
1034984 engine.Execute ();
1035985 return engine.GetResult ();
1036986 },
0 commit comments