From 3223492fd484268c92094620c9c43f3881552f92 Mon Sep 17 00:00:00 2001 From: gouzi <530971494@qq.com> Date: Thu, 18 Apr 2024 22:13:46 +0800 Subject: [PATCH 01/21] add log --- .../eager/to_static/run_program_op_func.h | 20 +++++ .../eager/to_static/run_program_op_node.h | 82 +++++++++++++------ paddle/fluid/framework/op_desc.cc | 4 + paddle/fluid/framework/type_defs.cc | 4 +- paddle/fluid/framework/type_defs.h | 6 +- paddle/fluid/pybind/op_function_common.cc | 28 ++++++- .../jit/dy2static/pir_partial_program.py | 8 +- test/dygraph_to_static/test_no_gradient.py | 5 +- 8 files changed, 120 insertions(+), 37 deletions(-) diff --git a/paddle/fluid/eager/to_static/run_program_op_func.h b/paddle/fluid/eager/to_static/run_program_op_func.h index cdb4de66ae189..76837c1be2163 100644 --- a/paddle/fluid/eager/to_static/run_program_op_func.h +++ b/paddle/fluid/eager/to_static/run_program_op_func.h @@ -317,6 +317,26 @@ inline void pir_run_program_ad_func( // Set Attributes grad_node->SetAttrMap(attrs); + auto* backward_program = + PADDLE_GET_CONST(::pir::Program*, attrs.at("backward_program")); + + auto* forward_program = + PADDLE_GET_CONST(::pir::Program*, attrs.at("forward_program")); + + auto testkey = PADDLE_GET_CONST(std::string, attrs.at("testkey")); + + VLOG(1) << "[pir_run_program_ad_func] testkey: " << testkey; + // TODO(gouzil): 对比一下这里的attrs的地址 + VLOG(1) << "[pir_run_program_ad_func] attrs: " << &attrs; + + VLOG(1) << "[pir_run_program_ad_func] backward_program: " + << backward_program; + VLOG(1) << "[pir_run_program_ad_func] backward_program: " + << backward_program->num_ops(); + VLOG(1) << "[pir_run_program_ad_func] forward_program: " << forward_program; + VLOG(1) << "[pir_run_program_ad_func] forward_program: " + << forward_program->num_ops(); + // Clear unused x vars auto filter_x = pir_filter_unused_input_var_in_backward(x_tmp, "bx", attrs); // Set TensorWrappers diff --git a/paddle/fluid/eager/to_static/run_program_op_node.h b/paddle/fluid/eager/to_static/run_program_op_node.h index fdcad4f439c3b..505015a0056c2 100644 --- a/paddle/fluid/eager/to_static/run_program_op_node.h +++ b/paddle/fluid/eager/to_static/run_program_op_node.h @@ -467,21 +467,18 @@ inline void PirRunProgramAPI( auto param_values = PADDLE_GET_CONST(std::vector<::pir::Value>, attrs.at("fp")); - auto *forward_global_block = - PADDLE_GET_CONST(::pir::Block *, attrs.at("forward_global_block")); - auto *backward_global_block = - PADDLE_GET_CONST(::pir::Block *, attrs.at("backward_global_block")); - auto *forward_program = - forward_global_block->GetParentOp()->GetParentProgram(); + PADDLE_GET_CONST(::pir::Program *, attrs.at("forward_program")); + auto *backward_program = + PADDLE_GET_CONST(::pir::Program *, attrs.at("backward_program")); + + ::pir::Block *forward_global_block = forward_program->block(); if (FLAGS_print_ir) { std::ostringstream print_stream; print_stream << "ForwardProgram is :\n"; forward_program->Print(print_stream); if (!is_test) { - auto *backward_program = - backward_global_block->GetParentOp()->GetParentProgram(); print_stream << "BackwardProgram is:\n"; backward_program->Print(print_stream); } else { @@ -1046,11 +1043,6 @@ inline void PirRunProgramGradAPI( VLOG(4) << "global_inner_scope:" << global_inner_scope; - auto *backward_global_block = - PADDLE_GET_CONST(::pir::Block *, attrs.at("backward_global_block")); - auto *backward_program = - backward_global_block->GetParentOp()->GetParentProgram(); - auto output_grad_values = PADDLE_GET_CONST(std::vector<::pir::Value>, attrs.at("bo_g")); auto forward_input_values = @@ -1068,27 +1060,63 @@ inline void PirRunProgramGradAPI( details::Trans2ContiguousTensorsInplace(out_grad); + auto testkey = PADDLE_GET_CONST(std::string, attrs.at("testkey")); + + std::cout << "backward_program get start" << std::endl; + auto *backward_program = + PADDLE_GET_CONST(::pir::Program *, attrs.at("backward_program")); + + auto *forward_program = + PADDLE_GET_CONST(::pir::Program *, attrs.at("forward_program")); + + VLOG(0) << "[PirRunProgramGradAPI] testkey: " << testkey; + VLOG(0) << "[PirRunProgramGradAPI] backward_program addr: " + << backward_program; + VLOG(0) << "[PirRunProgramGradAPI] forward_program addr: " << forward_program; + VLOG(0) << backward_program->num_ops(); + VLOG(0) << forward_program->num_ops(); + std::cout << "backward_program get end" << std::endl; + + std::cout << "backward_program block get start" << std::endl; + auto pb = backward_program->block(); + VLOG(0) << pb; + VLOG(0) << pb->num_ops(); + VLOG(0) << pb->empty(); + std::cout << "backward_program block get end" << std::endl; + // share x, param, middles, output_grads, out into scope. + VLOG(1) << "out_grad start"; + details::ShareTensorsIntoScopeByValue(backward_program->block(), + out_grad, + output_grad_values, + global_inner_scope); + VLOG(1) << "out_grad end"; details::ShareTensorsIntoScopeByValue( - backward_global_block, out_grad, output_grad_values, global_inner_scope); - details::ShareTensorsIntoScopeByValue( - backward_global_block, x, forward_input_values, global_inner_scope); - details::ShareTensorsIntoScopeByValue(backward_global_block, + backward_program->block(), x, forward_input_values, global_inner_scope); + VLOG(1) << "x end"; + details::ShareTensorsIntoScopeByValue(backward_program->block(), middles, forward_middle_values, global_inner_scope); + VLOG(1) << "middles end"; + details::ShareTensorsIntoScopeByValue(backward_program->block(), + out, + forward_output_values, + global_inner_scope); + VLOG(1) << "out end"; details::ShareTensorsIntoScopeByValue( - backward_global_block, out, forward_output_values, global_inner_scope); - details::ShareTensorsIntoScopeByValue( - backward_global_block, params, parameter_values, global_inner_scope); + backward_program->block(), params, parameter_values, global_inner_scope); + VLOG(1) << "params end"; // Clear out and middles to avoid hold memory until backward finish. out.clear(); middles.clear(); + VLOG(1) << "out and middles clear end"; auto &cache = paddle::framework::InterpreterCoreInfoCache::Instance(); std::shared_ptr interpreter_core = nullptr; + if (!cache.Has(program_id, global_inner_scope, place_hash_key, @@ -1138,10 +1166,10 @@ inline void PirRunProgramGradAPI( // get all eager gc vars std::set skip_eager_delete_vars; auto skip_names = details::GetNameFromValue( - backward_global_block, x_grad_values, false, true); + backward_program->block(), x_grad_values, false, true); skip_eager_delete_vars.insert(skip_names.begin(), skip_names.end()); skip_names = details::GetNameFromValue( - backward_global_block, p_grad_values, false, true); + backward_program->block(), p_grad_values, false, true); skip_eager_delete_vars.insert(skip_names.begin(), skip_names.end()); interpreter_core->SetSkipGcVars(skip_eager_delete_vars); cache.UpdateSkipEagerDeleteVars(program_id, @@ -1174,7 +1202,7 @@ inline void PirRunProgramGradAPI( } } - if (!backward_global_block->empty()) { + if (!backward_program->block()->empty()) { paddle::platform::RecordEvent record_event( "interpreter_core_run", paddle::platform::TracerEventType::UserDefined, @@ -1189,9 +1217,11 @@ inline void PirRunProgramGradAPI( "fetch_and_gc", paddle::platform::TracerEventType::UserDefined, 1); // Step 4. get outputs details::ShareTensorsFromScopeByValue( - backward_global_block, x_grad, x_grad_values, global_inner_scope); - details::ShareTensorsFromScopeByValue( - backward_global_block, params_grad, p_grad_values, global_inner_scope); + backward_program->block(), x_grad, x_grad_values, global_inner_scope); + details::ShareTensorsFromScopeByValue(backward_program->block(), + params_grad, + p_grad_values, + global_inner_scope); VLOG(4) << "after backward gc all vars"; global_inner_scope->SetCanReused(true); details::GcScope(global_inner_scope); diff --git a/paddle/fluid/framework/op_desc.cc b/paddle/fluid/framework/op_desc.cc index 32c520711d978..0187edec5cc10 100644 --- a/paddle/fluid/framework/op_desc.cc +++ b/paddle/fluid/framework/op_desc.cc @@ -26,6 +26,7 @@ limitations under the License. */ #include "paddle/fluid/operators/ops_extra_info.h" #include "paddle/phi/common/complex.h" #include "paddle/pir/include/core/block.h" +#include "paddle/pir/include/core/program.h" #include "paddle/pir/include/core/value.h" #include "paddle/utils/blank.h" @@ -977,6 +978,9 @@ struct SetAttrDescVisitor { void operator()(const std::vector &v) const { // just do nothing. } + void operator()(const ::pir::Program &v) const { + // just do nothing. + } void operator()(const std::vector &v) const { std::vector var_names; for (auto var : v) { diff --git a/paddle/fluid/framework/type_defs.cc b/paddle/fluid/framework/type_defs.cc index d8a6546ea718d..a17ff2d8a81d7 100644 --- a/paddle/fluid/framework/type_defs.cc +++ b/paddle/fluid/framework/type_defs.cc @@ -39,7 +39,9 @@ template class variant, ::pir::Block*, - std::vector<::pir::Value>>; + std::vector<::pir::Value>, + ::pir::Program, + ProgramDesc*>; } // namespace paddle REGISTER_LOG_SIMPLY_STR(paddle::framework::AttributeMap); REGISTER_LOG_SIMPLY_STR(paddle::framework::Attribute); diff --git a/paddle/fluid/framework/type_defs.h b/paddle/fluid/framework/type_defs.h index 61f133ceb082a..56e0eb7644894 100644 --- a/paddle/fluid/framework/type_defs.h +++ b/paddle/fluid/framework/type_defs.h @@ -27,6 +27,7 @@ limitations under the License. */ #include "paddle/phi/common/scalar.h" #include "paddle/phi/core/infermeta_utils.h" #include "paddle/pir/include/core/block.h" +#include "paddle/pir/include/core/program.h" #include "paddle/pir/include/core/value.h" #include "paddle/utils/blank.h" #include "paddle/utils/small_vector.h" @@ -40,6 +41,7 @@ class InferShapeContext; class InferVarTypeContext; class VarDesc; class BlockDesc; +class ProgramDesc; class Variable; class InferNoNeedBufferVarsFN; @@ -67,7 +69,9 @@ using Attribute = paddle::variant, ::pir::Block*, - std::vector<::pir::Value>>; + std::vector<::pir::Value>, + ::pir::Program*, + ProgramDesc*>; using AttributeMap = std::unordered_map; using OpCreator = diff --git a/paddle/fluid/pybind/op_function_common.cc b/paddle/fluid/pybind/op_function_common.cc index f8f1424ded243..9e0d0bb30d19e 100644 --- a/paddle/fluid/pybind/op_function_common.cc +++ b/paddle/fluid/pybind/op_function_common.cc @@ -36,6 +36,7 @@ #include "paddle/phi/common/complex.h" #include "paddle/pir/include/core/block.h" #include "paddle/pir/include/core/op_result.h" +#include "paddle/pir/include/core/region.h" #include "paddle/pir/include/core/value.h" namespace paddle { @@ -858,6 +859,24 @@ void CastPyArg2AttrIRBlock(PyObject* obj, attrs[key] = reinterpret_cast<::pir::Block*&>(vh[0]); } +void CastPyArg2AttrIRProgram(PyObject* obj, + paddle::framework::AttributeMap& attrs, // NOLINT + const std::string& key, + const std::string& op_type, + ssize_t arg_pos) { + VLOG(1) << "After Process pir::Program*"; + ::pybind11::detail::instance* inst = + (::pybind11::detail::instance*)obj; // NOLINT + void** vh = inst->simple_layout ? inst->simple_value_holder + : &inst->nonsimple.values_and_holders[0]; + + ::pir::Program* program = reinterpret_cast<::pir::Program*>(vh[0]); + + attrs[key] = program; + // attrs[key] = reinterpret_cast<::pir::Program*>(vh[0]); + // attrs[key] = vh[0]; +} + void CastPyArg2AttrValues(PyObject* obj, paddle::framework::AttributeMap& attrs, // NOLINT const std::string& key, @@ -998,6 +1017,7 @@ void ConstructAttrMapForRunProgram( attr_end)); PyObject* obj = nullptr; + attrs["testkey"] = std::string("testvalue"); for (ssize_t arg_pos = attr_start; arg_pos < attr_end; arg_pos += 2) { VLOG(1) << "Start Process " << arg_pos; Py_ssize_t key_len = 0; @@ -1020,11 +1040,11 @@ void ConstructAttrMapForRunProgram( if (std::set({"cuda_graph_capture_mode"}).count(key)) { CastPyArg2AttrString(obj, attrs, key, op_type, arg_pos); - } else if (std::set({"global_block", - "forward_global_block", - "backward_global_block"}) - .count(key)) { + } else if (std::set({"global_block"}).count(key)) { CastPyArg2AttrIRBlock(obj, attrs, key, op_type, arg_pos); + } else if (std::set({"forward_program", "backward_program"}) + .count(key)) { + CastPyArg2AttrIRProgram(obj, attrs, key, op_type, arg_pos); } else if (std::set({"is_test", "use_interpretorcore"}) .count(key)) { CastPyArg2AttrBoolean(obj, attrs, key, op_type, arg_pos); diff --git a/python/paddle/jit/dy2static/pir_partial_program.py b/python/paddle/jit/dy2static/pir_partial_program.py index cddac384213ab..9aaa4eb5971d4 100644 --- a/python/paddle/jit/dy2static/pir_partial_program.py +++ b/python/paddle/jit/dy2static/pir_partial_program.py @@ -898,10 +898,10 @@ def _prune_unused_params(self, program): def _prepare_attributes(self): attrs = [ - 'forward_global_block', - self.program.forward_program.global_block(), - 'backward_global_block', - self.program.backward_program.global_block(), + 'forward_program', + self.program.forward_program, + 'backward_program', + self.program.backward_program, 'is_test', not self.training, 'program_id', diff --git a/test/dygraph_to_static/test_no_gradient.py b/test/dygraph_to_static/test_no_gradient.py index 1bd3a02f54ede..b2f1346248f0a 100644 --- a/test/dygraph_to_static/test_no_gradient.py +++ b/test/dygraph_to_static/test_no_gradient.py @@ -12,10 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +breakpoint() # noqa: T100 import unittest import numpy -from dygraph_to_static_utils import Dy2StTestBase +from dygraph_to_static_utils import Dy2StTestBase, test_ast_only, test_pir_only import paddle @@ -33,6 +34,8 @@ def main_func(x, index): class TestNoGradientCase(Dy2StTestBase): + @test_ast_only + @test_pir_only def test_no_gradient(self): paddle.disable_static() x = paddle.randn([10, 3]) From c1398d17916c9f6552acf2d27e88d20b7cdcfb10 Mon Sep 17 00:00:00 2001 From: gouzi <530971494@qq.com> Date: Thu, 18 Apr 2024 22:27:27 +0800 Subject: [PATCH 02/21] add log --- paddle/fluid/framework/type_defs.cc | 2 +- paddle/pir/src/core/program.cc | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/paddle/fluid/framework/type_defs.cc b/paddle/fluid/framework/type_defs.cc index a17ff2d8a81d7..ad4eebf4ed3a8 100644 --- a/paddle/fluid/framework/type_defs.cc +++ b/paddle/fluid/framework/type_defs.cc @@ -40,7 +40,7 @@ template class variant, ::pir::Block*, std::vector<::pir::Value>, - ::pir::Program, + ::pir::Program*, ProgramDesc*>; } // namespace paddle REGISTER_LOG_SIMPLY_STR(paddle::framework::AttributeMap); diff --git a/paddle/pir/src/core/program.cc b/paddle/pir/src/core/program.cc index 19d08f094fd4c..6576cf00d40b8 100644 --- a/paddle/pir/src/core/program.cc +++ b/paddle/pir/src/core/program.cc @@ -23,6 +23,7 @@ Program::Program(IrContext* context) { } Program::~Program() { + VLOG(1) << "[Program] Destroy Program"; if (module_) { module_.Destroy(); } From 2a7f67d21d5c3e311b2ba7764086380ddb38f807 Mon Sep 17 00:00:00 2001 From: gouzi <530971494@qq.com> Date: Fri, 19 Apr 2024 10:29:15 +0800 Subject: [PATCH 03/21] add log --- paddle/fluid/framework/op_desc.cc | 2 +- paddle/fluid/pybind/op_function_common.cc | 11 ++++++++--- paddle/fluid/pybind/pir.cc | 2 +- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/paddle/fluid/framework/op_desc.cc b/paddle/fluid/framework/op_desc.cc index 0187edec5cc10..c131c93909705 100644 --- a/paddle/fluid/framework/op_desc.cc +++ b/paddle/fluid/framework/op_desc.cc @@ -978,7 +978,7 @@ struct SetAttrDescVisitor { void operator()(const std::vector &v) const { // just do nothing. } - void operator()(const ::pir::Program &v) const { + void operator()(const std::shared_ptr &v) const { // just do nothing. } void operator()(const std::vector &v) const { diff --git a/paddle/fluid/pybind/op_function_common.cc b/paddle/fluid/pybind/op_function_common.cc index 9e0d0bb30d19e..6db25f5f9c213 100644 --- a/paddle/fluid/pybind/op_function_common.cc +++ b/paddle/fluid/pybind/op_function_common.cc @@ -870,9 +870,14 @@ void CastPyArg2AttrIRProgram(PyObject* obj, void** vh = inst->simple_layout ? inst->simple_value_holder : &inst->nonsimple.values_and_holders[0]; - ::pir::Program* program = reinterpret_cast<::pir::Program*>(vh[0]); - - attrs[key] = program; + // ::pir::Program* program = reinterpret_cast<::pir::Program*>(vh[0]); + std::shared_ptr<::pir::Program> program = + reinterpret_cast&>(vh[0]); + // TODO(gouzil): 试一下pybind11能不能使用智能指针作为参数 + // pir::IrMapping mapper; + attrs[key] = program.get(); + // attrs[key] = program.get(); + // attrs[key] = program->Clone(mapper); // attrs[key] = reinterpret_cast<::pir::Program*>(vh[0]); // attrs[key] = vh[0]; } diff --git a/paddle/fluid/pybind/pir.cc b/paddle/fluid/pybind/pir.cc index 4176ecf0bbcbb..7b02c0676c9c6 100644 --- a/paddle/fluid/pybind/pir.cc +++ b/paddle/fluid/pybind/pir.cc @@ -255,7 +255,7 @@ void BindProgram(py::module *m) { )DOC"); program .def(py::init([]() { - return std::make_unique(pir::IrContext::Instance()); + return std::make_shared(pir::IrContext::Instance()); })) .def("__str__", [](const std::shared_ptr &self) { From 733ac038c15130259f13d0dbe6d4e68edd6ae447 Mon Sep 17 00:00:00 2001 From: gouzi <530971494@qq.com> Date: Fri, 19 Apr 2024 15:41:44 +0800 Subject: [PATCH 04/21] add handle --- paddle/fluid/pybind/op_function_common.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/paddle/fluid/pybind/op_function_common.cc b/paddle/fluid/pybind/op_function_common.cc index 6db25f5f9c213..91c969d7c1695 100644 --- a/paddle/fluid/pybind/op_function_common.cc +++ b/paddle/fluid/pybind/op_function_common.cc @@ -870,6 +870,7 @@ void CastPyArg2AttrIRProgram(PyObject* obj, void** vh = inst->simple_layout ? inst->simple_value_holder : &inst->nonsimple.values_and_holders[0]; + ::pybind11::handle(obj).inc_ref(); // ::pir::Program* program = reinterpret_cast<::pir::Program*>(vh[0]); std::shared_ptr<::pir::Program> program = reinterpret_cast&>(vh[0]); From 5c90eacfe7e972eb8f859c66ed98fbafdf92c463 Mon Sep 17 00:00:00 2001 From: gouzi <530971494@qq.com> Date: Fri, 19 Apr 2024 17:47:43 +0800 Subject: [PATCH 05/21] remove breakpoint --- test/dygraph_to_static/test_no_gradient.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/dygraph_to_static/test_no_gradient.py b/test/dygraph_to_static/test_no_gradient.py index b2f1346248f0a..2283c08a3d299 100644 --- a/test/dygraph_to_static/test_no_gradient.py +++ b/test/dygraph_to_static/test_no_gradient.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -breakpoint() # noqa: T100 +# breakpoint() import unittest import numpy From 64a56b0209d7bd6f8032e677753e51bd0e053803 Mon Sep 17 00:00:00 2001 From: gouzi <530971494@qq.com> Date: Sat, 20 Apr 2024 23:05:47 +0800 Subject: [PATCH 06/21] use `std::shared_ptr<::pir::Program>>` --- .../eager/to_static/run_program_op_func.h | 8 +++---- .../eager/to_static/run_program_op_node.h | 24 +++++++++++-------- paddle/fluid/framework/type_defs.cc | 3 +-- paddle/fluid/framework/type_defs.h | 3 +-- paddle/fluid/pybind/op_function_common.cc | 23 ++++++------------ 5 files changed, 27 insertions(+), 34 deletions(-) diff --git a/paddle/fluid/eager/to_static/run_program_op_func.h b/paddle/fluid/eager/to_static/run_program_op_func.h index 76837c1be2163..f305bb2a191ae 100644 --- a/paddle/fluid/eager/to_static/run_program_op_func.h +++ b/paddle/fluid/eager/to_static/run_program_op_func.h @@ -317,11 +317,11 @@ inline void pir_run_program_ad_func( // Set Attributes grad_node->SetAttrMap(attrs); - auto* backward_program = - PADDLE_GET_CONST(::pir::Program*, attrs.at("backward_program")); + auto backward_program = PADDLE_GET_CONST(std::shared_ptr<::pir::Program>, + attrs.at("backward_program")); - auto* forward_program = - PADDLE_GET_CONST(::pir::Program*, attrs.at("forward_program")); + auto forward_program = PADDLE_GET_CONST(std::shared_ptr<::pir::Program>, + attrs.at("forward_program")); auto testkey = PADDLE_GET_CONST(std::string, attrs.at("testkey")); diff --git a/paddle/fluid/eager/to_static/run_program_op_node.h b/paddle/fluid/eager/to_static/run_program_op_node.h index 505015a0056c2..9f01e4a8a683a 100644 --- a/paddle/fluid/eager/to_static/run_program_op_node.h +++ b/paddle/fluid/eager/to_static/run_program_op_node.h @@ -467,10 +467,10 @@ inline void PirRunProgramAPI( auto param_values = PADDLE_GET_CONST(std::vector<::pir::Value>, attrs.at("fp")); - auto *forward_program = - PADDLE_GET_CONST(::pir::Program *, attrs.at("forward_program")); - auto *backward_program = - PADDLE_GET_CONST(::pir::Program *, attrs.at("backward_program")); + auto forward_program = PADDLE_GET_CONST(std::shared_ptr<::pir::Program>, + attrs.at("forward_program")); + auto backward_program = PADDLE_GET_CONST(std::shared_ptr<::pir::Program>, + attrs.at("backward_program")); ::pir::Block *forward_global_block = forward_program->block(); @@ -511,7 +511,7 @@ inline void PirRunProgramAPI( forward_global_block, params, param_values, global_inner_scope); // Step 2. create new interpretercore auto passed_kernel_program = - paddle::framework::ApplyIrPass(forward_program, place); + paddle::framework::ApplyIrPass(forward_program.get(), place); if (FLAGS_print_ir) { std::ostringstream print_stream; print_stream << "LoweredProgram( AfterPass ) is :\n"; @@ -1063,11 +1063,11 @@ inline void PirRunProgramGradAPI( auto testkey = PADDLE_GET_CONST(std::string, attrs.at("testkey")); std::cout << "backward_program get start" << std::endl; - auto *backward_program = - PADDLE_GET_CONST(::pir::Program *, attrs.at("backward_program")); + auto backward_program = PADDLE_GET_CONST(std::shared_ptr<::pir::Program>, + attrs.at("backward_program")); - auto *forward_program = - PADDLE_GET_CONST(::pir::Program *, attrs.at("forward_program")); + auto forward_program = PADDLE_GET_CONST(std::shared_ptr<::pir::Program>, + attrs.at("forward_program")); VLOG(0) << "[PirRunProgramGradAPI] testkey: " << testkey; VLOG(0) << "[PirRunProgramGradAPI] backward_program addr: " @@ -1083,6 +1083,10 @@ inline void PirRunProgramGradAPI( VLOG(0) << pb->num_ops(); VLOG(0) << pb->empty(); std::cout << "backward_program block get end" << std::endl; + std::ostringstream print_stream; + print_stream << "backward_program is :\n"; + backward_program->Print(print_stream); + std::cout << print_stream.str() << std::endl; // share x, param, middles, output_grads, out into scope. VLOG(1) << "out_grad start"; @@ -1129,7 +1133,7 @@ inline void PirRunProgramGradAPI( VLOG(2) << "No interpretercore cache, so create a new interpretercore"; // Step 1. share input_vars & parameters into scope auto passed_kernel_program = - paddle::framework::ApplyIrPass(backward_program, place); + paddle::framework::ApplyIrPass(backward_program.get(), place); if (FLAGS_print_ir) { std::ostringstream print_stream; print_stream << "LoweredProgram( AfterPass | Backward ) is :\n"; diff --git a/paddle/fluid/framework/type_defs.cc b/paddle/fluid/framework/type_defs.cc index ad4eebf4ed3a8..6d350f1fe1e6c 100644 --- a/paddle/fluid/framework/type_defs.cc +++ b/paddle/fluid/framework/type_defs.cc @@ -40,8 +40,7 @@ template class variant, ::pir::Block*, std::vector<::pir::Value>, - ::pir::Program*, - ProgramDesc*>; + std::shared_ptr<::pir::Program>>; } // namespace paddle REGISTER_LOG_SIMPLY_STR(paddle::framework::AttributeMap); REGISTER_LOG_SIMPLY_STR(paddle::framework::Attribute); diff --git a/paddle/fluid/framework/type_defs.h b/paddle/fluid/framework/type_defs.h index 56e0eb7644894..d401d82df63c9 100644 --- a/paddle/fluid/framework/type_defs.h +++ b/paddle/fluid/framework/type_defs.h @@ -70,8 +70,7 @@ using Attribute = paddle::variant, ::pir::Block*, std::vector<::pir::Value>, - ::pir::Program*, - ProgramDesc*>; + std::shared_ptr<::pir::Program>>; using AttributeMap = std::unordered_map; using OpCreator = diff --git a/paddle/fluid/pybind/op_function_common.cc b/paddle/fluid/pybind/op_function_common.cc index 91c969d7c1695..f939ccf287ca2 100644 --- a/paddle/fluid/pybind/op_function_common.cc +++ b/paddle/fluid/pybind/op_function_common.cc @@ -865,22 +865,13 @@ void CastPyArg2AttrIRProgram(PyObject* obj, const std::string& op_type, ssize_t arg_pos) { VLOG(1) << "After Process pir::Program*"; - ::pybind11::detail::instance* inst = - (::pybind11::detail::instance*)obj; // NOLINT - void** vh = inst->simple_layout ? inst->simple_value_holder - : &inst->nonsimple.values_and_holders[0]; - - ::pybind11::handle(obj).inc_ref(); - // ::pir::Program* program = reinterpret_cast<::pir::Program*>(vh[0]); - std::shared_ptr<::pir::Program> program = - reinterpret_cast&>(vh[0]); - // TODO(gouzil): 试一下pybind11能不能使用智能指针作为参数 - // pir::IrMapping mapper; - attrs[key] = program.get(); - // attrs[key] = program.get(); - // attrs[key] = program->Clone(mapper); - // attrs[key] = reinterpret_cast<::pir::Program*>(vh[0]); - // attrs[key] = vh[0]; + const std::shared_ptr<::pir::Program> program = + ::py::handle(obj).cast>(); + VLOG(1) << "[CastPyArg2AttrIRProgram] program use count: " + << program.use_count(); + attrs[key] = program; + VLOG(1) << "[CastPyArg2AttrIRProgram] program use count: " + << program.use_count(); } void CastPyArg2AttrValues(PyObject* obj, From 8aba5efadbf508bf1c99ff6db2031c498a5c2b32 Mon Sep 17 00:00:00 2001 From: gouzi <530971494@qq.com> Date: Wed, 24 Apr 2024 19:16:01 +0800 Subject: [PATCH 07/21] copy #58180 --- paddle/fluid/eager/to_static/run_program_op_func.h | 11 +---------- paddle/fluid/eager/to_static/run_program_op_node.h | 4 ++++ 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/paddle/fluid/eager/to_static/run_program_op_func.h b/paddle/fluid/eager/to_static/run_program_op_func.h index f305bb2a191ae..3bde86812d1a0 100644 --- a/paddle/fluid/eager/to_static/run_program_op_func.h +++ b/paddle/fluid/eager/to_static/run_program_op_func.h @@ -346,16 +346,7 @@ inline void pir_run_program_ad_func( grad_node->SetStepScope(step_scope); // just for set useable. - // Set Grad out rank as same as fwd input and set stop gradient to bwd - // NOTE(@xiongkun): Not every tensor in x(list of tensor) is required - // gradient. for example: x[1] is not used for output, the x[1] is ignored. - - std::vector x_require_grad; - for (size_t i = 0; i < x.size(); ++i) { - x_require_grad.push_back(&x[i]); - } - - grad_node->SetGradOutMeta(x_require_grad, /*slot id*/ 0); + grad_node->SetGradOutMeta(x, /*slot id*/ 0); grad_node->SetGradOutMeta(params, /*slot id*/ 1); // TODO(@xiongkun): rewrite by new ir representation. diff --git a/paddle/fluid/eager/to_static/run_program_op_node.h b/paddle/fluid/eager/to_static/run_program_op_node.h index 387a55f77be1b..8e3893324eb2c 100644 --- a/paddle/fluid/eager/to_static/run_program_op_node.h +++ b/paddle/fluid/eager/to_static/run_program_op_node.h @@ -1505,6 +1505,10 @@ class PirGradNodeRunProgram : public egr::GradNodeBase { VLOG(3) << "End Eager Backward Node: PirGradNodeRunProgram"; *executed_ = true; + egr::EagerUtils::FillZeroForEmptyOptionalGradOutput(&x_grad, + this->OutputMeta()[0]); + egr::EagerUtils::FillZeroForEmptyOptionalGradOutput(¶ms_grad, + this->OutputMeta()[1]); return {x_grad, params_grad}; } From 23bbd587de941f26bd659cb9f666f1416d1c04d1 Mon Sep 17 00:00:00 2001 From: gouzi <530971494@qq.com> Date: Wed, 24 Apr 2024 19:34:16 +0800 Subject: [PATCH 08/21] clean log and open test --- .../eager/to_static/run_program_op_func.h | 20 ------------ .../eager/to_static/run_program_op_node.h | 31 +------------------ paddle/fluid/pybind/op_function_common.cc | 4 --- paddle/pir/src/core/program.cc | 1 - test/dygraph_to_static/test_no_gradient.py | 6 ++-- 5 files changed, 3 insertions(+), 59 deletions(-) diff --git a/paddle/fluid/eager/to_static/run_program_op_func.h b/paddle/fluid/eager/to_static/run_program_op_func.h index 3bde86812d1a0..df0dc3946507d 100644 --- a/paddle/fluid/eager/to_static/run_program_op_func.h +++ b/paddle/fluid/eager/to_static/run_program_op_func.h @@ -317,26 +317,6 @@ inline void pir_run_program_ad_func( // Set Attributes grad_node->SetAttrMap(attrs); - auto backward_program = PADDLE_GET_CONST(std::shared_ptr<::pir::Program>, - attrs.at("backward_program")); - - auto forward_program = PADDLE_GET_CONST(std::shared_ptr<::pir::Program>, - attrs.at("forward_program")); - - auto testkey = PADDLE_GET_CONST(std::string, attrs.at("testkey")); - - VLOG(1) << "[pir_run_program_ad_func] testkey: " << testkey; - // TODO(gouzil): 对比一下这里的attrs的地址 - VLOG(1) << "[pir_run_program_ad_func] attrs: " << &attrs; - - VLOG(1) << "[pir_run_program_ad_func] backward_program: " - << backward_program; - VLOG(1) << "[pir_run_program_ad_func] backward_program: " - << backward_program->num_ops(); - VLOG(1) << "[pir_run_program_ad_func] forward_program: " << forward_program; - VLOG(1) << "[pir_run_program_ad_func] forward_program: " - << forward_program->num_ops(); - // Clear unused x vars auto filter_x = pir_filter_unused_input_var_in_backward(x_tmp, "bx", attrs); // Set TensorWrappers diff --git a/paddle/fluid/eager/to_static/run_program_op_node.h b/paddle/fluid/eager/to_static/run_program_op_node.h index 8e3893324eb2c..8c1928c8d8705 100644 --- a/paddle/fluid/eager/to_static/run_program_op_node.h +++ b/paddle/fluid/eager/to_static/run_program_op_node.h @@ -1060,57 +1060,29 @@ inline void PirRunProgramGradAPI( details::Trans2ContiguousTensorsInplace(out_grad); - auto testkey = PADDLE_GET_CONST(std::string, attrs.at("testkey")); - - std::cout << "backward_program get start" << std::endl; auto backward_program = PADDLE_GET_CONST(std::shared_ptr<::pir::Program>, attrs.at("backward_program")); auto forward_program = PADDLE_GET_CONST(std::shared_ptr<::pir::Program>, attrs.at("forward_program")); - VLOG(0) << "[PirRunProgramGradAPI] testkey: " << testkey; - VLOG(0) << "[PirRunProgramGradAPI] backward_program addr: " - << backward_program; - VLOG(0) << "[PirRunProgramGradAPI] forward_program addr: " << forward_program; - VLOG(0) << backward_program->num_ops(); - VLOG(0) << forward_program->num_ops(); - std::cout << "backward_program get end" << std::endl; - - std::cout << "backward_program block get start" << std::endl; - auto pb = backward_program->block(); - VLOG(0) << pb; - VLOG(0) << pb->num_ops(); - VLOG(0) << pb->empty(); - std::cout << "backward_program block get end" << std::endl; - std::ostringstream print_stream; - print_stream << "backward_program is :\n"; - backward_program->Print(print_stream); - std::cout << print_stream.str() << std::endl; - // share x, param, middles, output_grads, out into scope. - VLOG(1) << "out_grad start"; details::ShareTensorsIntoScopeByValue(backward_program->block(), out_grad, output_grad_values, global_inner_scope); - VLOG(1) << "out_grad end"; details::ShareTensorsIntoScopeByValue( backward_program->block(), x, forward_input_values, global_inner_scope); - VLOG(1) << "x end"; details::ShareTensorsIntoScopeByValue(backward_program->block(), middles, forward_middle_values, global_inner_scope); - VLOG(1) << "middles end"; details::ShareTensorsIntoScopeByValue(backward_program->block(), out, forward_output_values, global_inner_scope); - VLOG(1) << "out end"; details::ShareTensorsIntoScopeByValue( backward_program->block(), params, parameter_values, global_inner_scope); - VLOG(1) << "params end"; // Clear out and middles to avoid hold memory until backward finish. out.clear(); @@ -1369,8 +1341,7 @@ class GradNodeRunProgram : public egr::GradNodeBase { if (x[i].is_dense_tensor()) { x_grad->emplace_back(std::make_shared()); } else if (x[i].is_selected_rows()) { - auto selected_row = std::make_shared(); - x_grad->emplace_back(selected_row); + x_grad->emplace_back(std::make_shared()); } x_grad->back().set_name(x_grad_names[i]); } diff --git a/paddle/fluid/pybind/op_function_common.cc b/paddle/fluid/pybind/op_function_common.cc index f939ccf287ca2..2a1a4c84d4225 100644 --- a/paddle/fluid/pybind/op_function_common.cc +++ b/paddle/fluid/pybind/op_function_common.cc @@ -867,11 +867,7 @@ void CastPyArg2AttrIRProgram(PyObject* obj, VLOG(1) << "After Process pir::Program*"; const std::shared_ptr<::pir::Program> program = ::py::handle(obj).cast>(); - VLOG(1) << "[CastPyArg2AttrIRProgram] program use count: " - << program.use_count(); attrs[key] = program; - VLOG(1) << "[CastPyArg2AttrIRProgram] program use count: " - << program.use_count(); } void CastPyArg2AttrValues(PyObject* obj, diff --git a/paddle/pir/src/core/program.cc b/paddle/pir/src/core/program.cc index 6576cf00d40b8..19d08f094fd4c 100644 --- a/paddle/pir/src/core/program.cc +++ b/paddle/pir/src/core/program.cc @@ -23,7 +23,6 @@ Program::Program(IrContext* context) { } Program::~Program() { - VLOG(1) << "[Program] Destroy Program"; if (module_) { module_.Destroy(); } diff --git a/test/dygraph_to_static/test_no_gradient.py b/test/dygraph_to_static/test_no_gradient.py index 2283c08a3d299..84f7b032c2f4a 100644 --- a/test/dygraph_to_static/test_no_gradient.py +++ b/test/dygraph_to_static/test_no_gradient.py @@ -12,11 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -# breakpoint() import unittest import numpy -from dygraph_to_static_utils import Dy2StTestBase, test_ast_only, test_pir_only +from dygraph_to_static_utils import Dy2StTestBase, test_legacy_and_pt_and_pir import paddle @@ -34,8 +33,7 @@ def main_func(x, index): class TestNoGradientCase(Dy2StTestBase): - @test_ast_only - @test_pir_only + @test_legacy_and_pt_and_pir def test_no_gradient(self): paddle.disable_static() x = paddle.randn([10, 3]) From 31be3b994dc8ed27350d8c59e1da114ca57dcc3c Mon Sep 17 00:00:00 2001 From: gouzi <530971494@qq.com> Date: Wed, 24 Apr 2024 19:37:32 +0800 Subject: [PATCH 09/21] Rollback execution sequence --- paddle/fluid/eager/to_static/run_program_op_node.h | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/paddle/fluid/eager/to_static/run_program_op_node.h b/paddle/fluid/eager/to_static/run_program_op_node.h index 8c1928c8d8705..bcbc5fb48dafc 100644 --- a/paddle/fluid/eager/to_static/run_program_op_node.h +++ b/paddle/fluid/eager/to_static/run_program_op_node.h @@ -1043,6 +1043,9 @@ inline void PirRunProgramGradAPI( VLOG(4) << "global_inner_scope:" << global_inner_scope; + auto backward_program = PADDLE_GET_CONST(std::shared_ptr<::pir::Program>, + attrs.at("backward_program")); + auto output_grad_values = PADDLE_GET_CONST(std::vector<::pir::Value>, attrs.at("bo_g")); auto forward_input_values = @@ -1060,12 +1063,6 @@ inline void PirRunProgramGradAPI( details::Trans2ContiguousTensorsInplace(out_grad); - auto backward_program = PADDLE_GET_CONST(std::shared_ptr<::pir::Program>, - attrs.at("backward_program")); - - auto forward_program = PADDLE_GET_CONST(std::shared_ptr<::pir::Program>, - attrs.at("forward_program")); - // share x, param, middles, output_grads, out into scope. details::ShareTensorsIntoScopeByValue(backward_program->block(), out_grad, From e005597da512f8c9aa2c927af92c01879e71c75c Mon Sep 17 00:00:00 2001 From: gouzi <530971494@qq.com> Date: Wed, 24 Apr 2024 19:42:30 +0800 Subject: [PATCH 10/21] rm log --- paddle/fluid/eager/to_static/run_program_op_node.h | 1 - 1 file changed, 1 deletion(-) diff --git a/paddle/fluid/eager/to_static/run_program_op_node.h b/paddle/fluid/eager/to_static/run_program_op_node.h index bcbc5fb48dafc..a8c0413cfb4d9 100644 --- a/paddle/fluid/eager/to_static/run_program_op_node.h +++ b/paddle/fluid/eager/to_static/run_program_op_node.h @@ -1084,7 +1084,6 @@ inline void PirRunProgramGradAPI( // Clear out and middles to avoid hold memory until backward finish. out.clear(); middles.clear(); - VLOG(1) << "out and middles clear end"; auto &cache = paddle::framework::InterpreterCoreInfoCache::Instance(); std::shared_ptr interpreter_core = From 6277bc21ff0b8a43b53b62f81716f043d4ca0b91 Mon Sep 17 00:00:00 2001 From: gouzi <530971494@qq.com> Date: Wed, 24 Apr 2024 19:44:01 +0800 Subject: [PATCH 11/21] rm `class ProgramDesc` --- paddle/fluid/framework/type_defs.h | 1 - 1 file changed, 1 deletion(-) diff --git a/paddle/fluid/framework/type_defs.h b/paddle/fluid/framework/type_defs.h index d401d82df63c9..919da60601555 100644 --- a/paddle/fluid/framework/type_defs.h +++ b/paddle/fluid/framework/type_defs.h @@ -41,7 +41,6 @@ class InferShapeContext; class InferVarTypeContext; class VarDesc; class BlockDesc; -class ProgramDesc; class Variable; class InferNoNeedBufferVarsFN; From 4972090a7e651ef054d40ad2a1f5ba3f1e160e7e Mon Sep 17 00:00:00 2001 From: gouzi <530971494@qq.com> Date: Wed, 24 Apr 2024 19:45:52 +0800 Subject: [PATCH 12/21] rm `test[key]` --- paddle/fluid/pybind/op_function_common.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/paddle/fluid/pybind/op_function_common.cc b/paddle/fluid/pybind/op_function_common.cc index 2a1a4c84d4225..52001185c2cd8 100644 --- a/paddle/fluid/pybind/op_function_common.cc +++ b/paddle/fluid/pybind/op_function_common.cc @@ -1010,7 +1010,6 @@ void ConstructAttrMapForRunProgram( attr_end)); PyObject* obj = nullptr; - attrs["testkey"] = std::string("testvalue"); for (ssize_t arg_pos = attr_start; arg_pos < attr_end; arg_pos += 2) { VLOG(1) << "Start Process " << arg_pos; Py_ssize_t key_len = 0; From 4bcbcc6d2b02dca1a3ca462bb6dabf0820108884 Mon Sep 17 00:00:00 2001 From: gouzi <530971494@qq.com> Date: Wed, 24 Apr 2024 23:54:11 +0800 Subject: [PATCH 13/21] rm include --- paddle/fluid/pybind/op_function_common.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/paddle/fluid/pybind/op_function_common.cc b/paddle/fluid/pybind/op_function_common.cc index 52001185c2cd8..cc484f74ab22f 100644 --- a/paddle/fluid/pybind/op_function_common.cc +++ b/paddle/fluid/pybind/op_function_common.cc @@ -36,7 +36,6 @@ #include "paddle/phi/common/complex.h" #include "paddle/pir/include/core/block.h" #include "paddle/pir/include/core/op_result.h" -#include "paddle/pir/include/core/region.h" #include "paddle/pir/include/core/value.h" namespace paddle { From 414fbda57c8fe694ce3577b3c4ce776c7b90ff98 Mon Sep 17 00:00:00 2001 From: gouzi <530971494@qq.com> Date: Thu, 25 Apr 2024 18:24:39 +0800 Subject: [PATCH 14/21] fix jit.load for pir --- python/paddle/jit/pir_translated_layer.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/python/paddle/jit/pir_translated_layer.py b/python/paddle/jit/pir_translated_layer.py index 1f50677821633..dee752b8ef7cc 100644 --- a/python/paddle/jit/pir_translated_layer.py +++ b/python/paddle/jit/pir_translated_layer.py @@ -343,10 +343,10 @@ def _run_dygraph(instance, input, program_holder): ) attrs = [ - 'forward_global_block', - forward_program.global_block(), - 'backward_global_block', - backward_program.global_block(), + 'forward_program', + forward_program, + 'backward_program', + backward_program, 'is_test', instance._is_test, 'program_id', From 1e68e624935cbf56255480c699be669f46889f24 Mon Sep 17 00:00:00 2001 From: gouzi <530971494@qq.com> Date: Thu, 25 Apr 2024 21:25:41 +0800 Subject: [PATCH 15/21] [ci][test] ignore test --- test/cpp/prim/test_eager_prim.cc | 287 ++++++++++++++++--------------- 1 file changed, 144 insertions(+), 143 deletions(-) diff --git a/test/cpp/prim/test_eager_prim.cc b/test/cpp/prim/test_eager_prim.cc index ac82ecac53081..4a13bf78025f3 100644 --- a/test/cpp/prim/test_eager_prim.cc +++ b/test/cpp/prim/test_eager_prim.cc @@ -17,159 +17,160 @@ #include "glog/logging.h" #include "gtest/gtest.h" #include "paddle/common/flags.h" -#include "paddle/fluid/eager/api/generated/eager_generated/forwards/dygraph_functions.h" -#include "paddle/fluid/eager/api/utils/hook_utils.h" -#include "paddle/fluid/eager/backward.h" +// #include +// "paddle/fluid/eager/api/generated/eager_generated/forwards/dygraph_functions.h" +// #include "paddle/fluid/eager/api/utils/hook_utils.h" +// #include "paddle/fluid/eager/backward.h" #include "paddle/fluid/prim/utils/utils.h" -#include "paddle/phi/core/dense_tensor.h" -#include "paddle/phi/core/kernel_registry.h" -#include "paddle/phi/core/tensor_meta.h" -#include "test/cpp/eager/test_utils.h" -#include "test/cpp/prim/init_env_utils.h" +// #include "paddle/phi/core/dense_tensor.h" +// #include "paddle/phi/core/kernel_registry.h" +// #include "paddle/phi/core/tensor_meta.h" +// #include "test/cpp/eager/test_utils.h" +// #include "test/cpp/prim/init_env_utils.h" COMMON_DECLARE_string(tensor_operants_mode); namespace paddle { namespace prim { -TEST(EagerPrim, TanhBackwardTest) { - // 1. Initialized - eager_test::InitEnv(paddle::platform::CPUPlace()); - FLAGS_tensor_operants_mode = "eager"; - paddle::prim::InitTensorOperants(); - // 2. pre - paddle::framework::DDim ddim = common::make_ddim({4, 16, 16, 32}); - paddle::Tensor tensor0 = - eager_test::CreateTensorWithValue(ddim, - paddle::platform::CPUPlace(), - phi::DataType::FLOAT32, - phi::DataLayout::NCHW, - 5.0 /*value*/, - true /*is_leaf*/); - ::egr::egr_utils_api::RetainGradForTensor(tensor0); - paddle::Tensor tensor1 = - eager_test::CreateTensorWithValue(ddim, - paddle::platform::CPUPlace(), - phi::DataType::FLOAT32, - phi::DataLayout::NCHW, - 5.0 /*value*/, - true /*is_leaf*/); - ::egr::egr_utils_api::RetainGradForTensor(tensor1); - // 3. Run Forward once - paddle::Tensor out0 = tanh_ad_func(tensor0); - std::vector outs0 = {out0}; - // Disable prim - PrimCommonUtils::SetBwdPrimEnabled(false); - ASSERT_FALSE(PrimCommonUtils::IsBwdPrimEnabled()); - // 4. Run Backward - egr::Backward(outs0, {}, false); +// TEST(EagerPrim, TanhBackwardTest) { +// // 1. Initialized +// eager_test::InitEnv(paddle::platform::CPUPlace()); +// FLAGS_tensor_operants_mode = "eager"; +// paddle::prim::InitTensorOperants(); +// // 2. pre +// paddle::framework::DDim ddim = common::make_ddim({4, 16, 16, 32}); +// paddle::Tensor tensor0 = +// eager_test::CreateTensorWithValue(ddim, +// paddle::platform::CPUPlace(), +// phi::DataType::FLOAT32, +// phi::DataLayout::NCHW, +// 5.0 /*value*/, +// true /*is_leaf*/); +// ::egr::egr_utils_api::RetainGradForTensor(tensor0); +// paddle::Tensor tensor1 = +// eager_test::CreateTensorWithValue(ddim, +// paddle::platform::CPUPlace(), +// phi::DataType::FLOAT32, +// phi::DataLayout::NCHW, +// 5.0 /*value*/, +// true /*is_leaf*/); +// ::egr::egr_utils_api::RetainGradForTensor(tensor1); +// // 3. Run Forward once +// paddle::Tensor out0 = tanh_ad_func(tensor0); +// std::vector outs0 = {out0}; +// // Disable prim +// PrimCommonUtils::SetBwdPrimEnabled(false); +// ASSERT_FALSE(PrimCommonUtils::IsBwdPrimEnabled()); +// // 4. Run Backward +// egr::Backward(outs0, {}, false); - paddle::Tensor out1 = tanh_ad_func(tensor1); - std::vector outs1 = {out1}; - // Enable prim - PrimCommonUtils::SetBwdPrimEnabled(true); - ASSERT_TRUE(PrimCommonUtils::IsBwdPrimEnabled()); - // 4. Run Backward - ::egr::Backward(outs1, {}, false); - VLOG(7) - << "Target Grad is: " - << std::static_pointer_cast( - ::egr::EagerUtils::unsafe_autograd_meta(tensor0)->Grad().impl()) - ->data()[0]; - VLOG(7) - << "Result Grad is: " - << std::static_pointer_cast( - ::egr::EagerUtils::unsafe_autograd_meta(tensor1)->Grad().impl()) - ->data()[0]; - // Examine Backward Grad - eager_test::CompareGradTensorWithValue( - tensor1, - std::static_pointer_cast( - ::egr::EagerUtils::unsafe_autograd_meta(tensor0)->Grad().impl()) - ->data()[0]); -} +// paddle::Tensor out1 = tanh_ad_func(tensor1); +// std::vector outs1 = {out1}; +// // Enable prim +// PrimCommonUtils::SetBwdPrimEnabled(true); +// ASSERT_TRUE(PrimCommonUtils::IsBwdPrimEnabled()); +// // 4. Run Backward +// ::egr::Backward(outs1, {}, false); +// VLOG(7) +// << "Target Grad is: " +// << std::static_pointer_cast( +// ::egr::EagerUtils::unsafe_autograd_meta(tensor0)->Grad().impl()) +// ->data()[0]; +// VLOG(7) +// << "Result Grad is: " +// << std::static_pointer_cast( +// ::egr::EagerUtils::unsafe_autograd_meta(tensor1)->Grad().impl()) +// ->data()[0]; +// // Examine Backward Grad +// eager_test::CompareGradTensorWithValue( +// tensor1, +// std::static_pointer_cast( +// ::egr::EagerUtils::unsafe_autograd_meta(tensor0)->Grad().impl()) +// ->data()[0]); +// } -TEST(EagerPrim, LogicalOperantsTest) { - // 1. Initialized - eager_test::InitEnv(paddle::platform::CPUPlace()); - FLAGS_tensor_operants_mode = "eager"; - paddle::prim::InitTensorOperants(); - // 2. pre - paddle::framework::DDim ddim = common::make_ddim({4, 16, 16, 32}); - paddle::Tensor tensor0 = - eager_test::CreateTensorWithValue(ddim, - paddle::platform::CPUPlace(), - phi::DataType::INT32, - phi::DataLayout::NCHW, - 1 /*value*/, - true /*is_leaf*/); - ::egr::egr_utils_api::RetainGradForTensor(tensor0); - paddle::Tensor tensor1 = - eager_test::CreateTensorWithValue(ddim, - paddle::platform::CPUPlace(), - phi::DataType::INT32, - phi::DataLayout::NCHW, - 0 /*value*/, - true /*is_leaf*/); - ::egr::egr_utils_api::RetainGradForTensor(tensor1); - // 3. Run Forward once - paddle::Tensor out0 = tensor0 & tensor1; - paddle::Tensor out1 = bitwise_and_ad_func(tensor0, tensor1); - EXPECT_EQ(out0.data()[0], out1.data()[0]); - out0 = tensor0 | tensor1; - out1 = bitwise_or_ad_func(tensor0, tensor1); - EXPECT_EQ(out0.data()[0], out1.data()[0]); - out0 = tensor0 ^ tensor1; - out1 = bitwise_xor_ad_func(tensor0, tensor1); - EXPECT_EQ(out0.data()[0], out1.data()[0]); - out0 = ~tensor0; - out1 = bitwise_not_ad_func(tensor0); - EXPECT_EQ(out0.data()[0], out1.data()[0]); -} +// TEST(EagerPrim, LogicalOperantsTest) { +// // 1. Initialized +// eager_test::InitEnv(paddle::platform::CPUPlace()); +// FLAGS_tensor_operants_mode = "eager"; +// paddle::prim::InitTensorOperants(); +// // 2. pre +// paddle::framework::DDim ddim = common::make_ddim({4, 16, 16, 32}); +// paddle::Tensor tensor0 = +// eager_test::CreateTensorWithValue(ddim, +// paddle::platform::CPUPlace(), +// phi::DataType::INT32, +// phi::DataLayout::NCHW, +// 1 /*value*/, +// true /*is_leaf*/); +// ::egr::egr_utils_api::RetainGradForTensor(tensor0); +// paddle::Tensor tensor1 = +// eager_test::CreateTensorWithValue(ddim, +// paddle::platform::CPUPlace(), +// phi::DataType::INT32, +// phi::DataLayout::NCHW, +// 0 /*value*/, +// true /*is_leaf*/); +// ::egr::egr_utils_api::RetainGradForTensor(tensor1); +// // 3. Run Forward once +// paddle::Tensor out0 = tensor0 & tensor1; +// paddle::Tensor out1 = bitwise_and_ad_func(tensor0, tensor1); +// EXPECT_EQ(out0.data()[0], out1.data()[0]); +// out0 = tensor0 | tensor1; +// out1 = bitwise_or_ad_func(tensor0, tensor1); +// EXPECT_EQ(out0.data()[0], out1.data()[0]); +// out0 = tensor0 ^ tensor1; +// out1 = bitwise_xor_ad_func(tensor0, tensor1); +// EXPECT_EQ(out0.data()[0], out1.data()[0]); +// out0 = ~tensor0; +// out1 = bitwise_not_ad_func(tensor0); +// EXPECT_EQ(out0.data()[0], out1.data()[0]); +// } -TEST(EagerPrim, CompareOperantsTest) { - // 1. Initialized - eager_test::InitEnv(paddle::platform::CPUPlace()); - FLAGS_tensor_operants_mode = "eager"; - paddle::prim::InitTensorOperants(); - // 2. pre - paddle::framework::DDim ddim = common::make_ddim({4, 16, 16, 32}); - paddle::Tensor tensor0 = - eager_test::CreateTensorWithValue(ddim, - paddle::platform::CPUPlace(), - phi::DataType::INT32, - phi::DataLayout::NCHW, - 1 /*value*/, - true /*is_leaf*/); - ::egr::egr_utils_api::RetainGradForTensor(tensor0); - paddle::Tensor tensor1 = - eager_test::CreateTensorWithValue(ddim, - paddle::platform::CPUPlace(), - phi::DataType::INT32, - phi::DataLayout::NCHW, - 0 /*value*/, - true /*is_leaf*/); - ::egr::egr_utils_api::RetainGradForTensor(tensor1); - // 3. Run Forward once - paddle::Tensor out0 = (tensor0 < tensor1); - paddle::Tensor out1 = less_than_ad_func(tensor0, tensor1); - EXPECT_EQ(out0.data()[0], out1.data()[0]); - out0 = (tensor0 <= tensor1); - out1 = less_equal_ad_func(tensor0, tensor1); - EXPECT_EQ(out0.data()[0], out1.data()[0]); - out0 = (tensor0 == tensor1); - out1 = equal_ad_func(tensor0, tensor1); - EXPECT_EQ(out0.data()[0], out1.data()[0]); - out0 = (tensor0 != tensor1); - out1 = not_equal_ad_func(tensor0, tensor1); - EXPECT_EQ(out0.data()[0], out1.data()[0]); - out0 = (tensor0 > tensor1); - out1 = greater_than_ad_func(tensor0, tensor1); - EXPECT_EQ(out0.data()[0], out1.data()[0]); - out0 = (tensor0 >= tensor1); - out1 = greater_equal_ad_func(tensor0, tensor1); - EXPECT_EQ(out0.data()[0], out1.data()[0]); -} +// TEST(EagerPrim, CompareOperantsTest) { +// // 1. Initialized +// eager_test::InitEnv(paddle::platform::CPUPlace()); +// FLAGS_tensor_operants_mode = "eager"; +// paddle::prim::InitTensorOperants(); +// // 2. pre +// paddle::framework::DDim ddim = common::make_ddim({4, 16, 16, 32}); +// paddle::Tensor tensor0 = +// eager_test::CreateTensorWithValue(ddim, +// paddle::platform::CPUPlace(), +// phi::DataType::INT32, +// phi::DataLayout::NCHW, +// 1 /*value*/, +// true /*is_leaf*/); +// ::egr::egr_utils_api::RetainGradForTensor(tensor0); +// paddle::Tensor tensor1 = +// eager_test::CreateTensorWithValue(ddim, +// paddle::platform::CPUPlace(), +// phi::DataType::INT32, +// phi::DataLayout::NCHW, +// 0 /*value*/, +// true /*is_leaf*/); +// ::egr::egr_utils_api::RetainGradForTensor(tensor1); +// // 3. Run Forward once +// paddle::Tensor out0 = (tensor0 < tensor1); +// paddle::Tensor out1 = less_than_ad_func(tensor0, tensor1); +// EXPECT_EQ(out0.data()[0], out1.data()[0]); +// out0 = (tensor0 <= tensor1); +// out1 = less_equal_ad_func(tensor0, tensor1); +// EXPECT_EQ(out0.data()[0], out1.data()[0]); +// out0 = (tensor0 == tensor1); +// out1 = equal_ad_func(tensor0, tensor1); +// EXPECT_EQ(out0.data()[0], out1.data()[0]); +// out0 = (tensor0 != tensor1); +// out1 = not_equal_ad_func(tensor0, tensor1); +// EXPECT_EQ(out0.data()[0], out1.data()[0]); +// out0 = (tensor0 > tensor1); +// out1 = greater_than_ad_func(tensor0, tensor1); +// EXPECT_EQ(out0.data()[0], out1.data()[0]); +// out0 = (tensor0 >= tensor1); +// out1 = greater_equal_ad_func(tensor0, tensor1); +// EXPECT_EQ(out0.data()[0], out1.data()[0]); +// } TEST(EagerPrim, TestFlags) { PrimCommonUtils::SetBwdPrimEnabled(true); From cb6cdabafa771382c8ab783fd0060ac93b6eceb4 Mon Sep 17 00:00:00 2001 From: gouzi <530971494@qq.com> Date: Thu, 25 Apr 2024 22:28:34 +0800 Subject: [PATCH 16/21] [ci][test] open `CompareOperantsTest` --- test/cpp/prim/test_eager_prim.cc | 95 ++++++++++++++++---------------- 1 file changed, 47 insertions(+), 48 deletions(-) diff --git a/test/cpp/prim/test_eager_prim.cc b/test/cpp/prim/test_eager_prim.cc index 4a13bf78025f3..c7b590379cb61 100644 --- a/test/cpp/prim/test_eager_prim.cc +++ b/test/cpp/prim/test_eager_prim.cc @@ -17,16 +17,15 @@ #include "glog/logging.h" #include "gtest/gtest.h" #include "paddle/common/flags.h" -// #include -// "paddle/fluid/eager/api/generated/eager_generated/forwards/dygraph_functions.h" -// #include "paddle/fluid/eager/api/utils/hook_utils.h" +#include "paddle/fluid/eager/api/generated/eager_generated/forwards/dygraph_functions.h" +#include "paddle/fluid/eager/api/utils/hook_utils.h" // #include "paddle/fluid/eager/backward.h" #include "paddle/fluid/prim/utils/utils.h" // #include "paddle/phi/core/dense_tensor.h" // #include "paddle/phi/core/kernel_registry.h" // #include "paddle/phi/core/tensor_meta.h" -// #include "test/cpp/eager/test_utils.h" -// #include "test/cpp/prim/init_env_utils.h" +#include "test/cpp/eager/test_utils.h" +#include "test/cpp/prim/init_env_utils.h" COMMON_DECLARE_string(tensor_operants_mode); @@ -128,49 +127,49 @@ namespace prim { // EXPECT_EQ(out0.data()[0], out1.data()[0]); // } -// TEST(EagerPrim, CompareOperantsTest) { -// // 1. Initialized -// eager_test::InitEnv(paddle::platform::CPUPlace()); -// FLAGS_tensor_operants_mode = "eager"; -// paddle::prim::InitTensorOperants(); -// // 2. pre -// paddle::framework::DDim ddim = common::make_ddim({4, 16, 16, 32}); -// paddle::Tensor tensor0 = -// eager_test::CreateTensorWithValue(ddim, -// paddle::platform::CPUPlace(), -// phi::DataType::INT32, -// phi::DataLayout::NCHW, -// 1 /*value*/, -// true /*is_leaf*/); -// ::egr::egr_utils_api::RetainGradForTensor(tensor0); -// paddle::Tensor tensor1 = -// eager_test::CreateTensorWithValue(ddim, -// paddle::platform::CPUPlace(), -// phi::DataType::INT32, -// phi::DataLayout::NCHW, -// 0 /*value*/, -// true /*is_leaf*/); -// ::egr::egr_utils_api::RetainGradForTensor(tensor1); -// // 3. Run Forward once -// paddle::Tensor out0 = (tensor0 < tensor1); -// paddle::Tensor out1 = less_than_ad_func(tensor0, tensor1); -// EXPECT_EQ(out0.data()[0], out1.data()[0]); -// out0 = (tensor0 <= tensor1); -// out1 = less_equal_ad_func(tensor0, tensor1); -// EXPECT_EQ(out0.data()[0], out1.data()[0]); -// out0 = (tensor0 == tensor1); -// out1 = equal_ad_func(tensor0, tensor1); -// EXPECT_EQ(out0.data()[0], out1.data()[0]); -// out0 = (tensor0 != tensor1); -// out1 = not_equal_ad_func(tensor0, tensor1); -// EXPECT_EQ(out0.data()[0], out1.data()[0]); -// out0 = (tensor0 > tensor1); -// out1 = greater_than_ad_func(tensor0, tensor1); -// EXPECT_EQ(out0.data()[0], out1.data()[0]); -// out0 = (tensor0 >= tensor1); -// out1 = greater_equal_ad_func(tensor0, tensor1); -// EXPECT_EQ(out0.data()[0], out1.data()[0]); -// } +TEST(EagerPrim, CompareOperantsTest) { + // 1. Initialized + eager_test::InitEnv(paddle::platform::CPUPlace()); + FLAGS_tensor_operants_mode = "eager"; + paddle::prim::InitTensorOperants(); + // 2. pre + paddle::framework::DDim ddim = common::make_ddim({4, 16, 16, 32}); + paddle::Tensor tensor0 = + eager_test::CreateTensorWithValue(ddim, + paddle::platform::CPUPlace(), + phi::DataType::INT32, + phi::DataLayout::NCHW, + 1 /*value*/, + true /*is_leaf*/); + ::egr::egr_utils_api::RetainGradForTensor(tensor0); + paddle::Tensor tensor1 = + eager_test::CreateTensorWithValue(ddim, + paddle::platform::CPUPlace(), + phi::DataType::INT32, + phi::DataLayout::NCHW, + 0 /*value*/, + true /*is_leaf*/); + ::egr::egr_utils_api::RetainGradForTensor(tensor1); + // 3. Run Forward once + paddle::Tensor out0 = (tensor0 < tensor1); + paddle::Tensor out1 = less_than_ad_func(tensor0, tensor1); + EXPECT_EQ(out0.data()[0], out1.data()[0]); + out0 = (tensor0 <= tensor1); + out1 = less_equal_ad_func(tensor0, tensor1); + EXPECT_EQ(out0.data()[0], out1.data()[0]); + out0 = (tensor0 == tensor1); + out1 = equal_ad_func(tensor0, tensor1); + EXPECT_EQ(out0.data()[0], out1.data()[0]); + out0 = (tensor0 != tensor1); + out1 = not_equal_ad_func(tensor0, tensor1); + EXPECT_EQ(out0.data()[0], out1.data()[0]); + out0 = (tensor0 > tensor1); + out1 = greater_than_ad_func(tensor0, tensor1); + EXPECT_EQ(out0.data()[0], out1.data()[0]); + out0 = (tensor0 >= tensor1); + out1 = greater_equal_ad_func(tensor0, tensor1); + EXPECT_EQ(out0.data()[0], out1.data()[0]); +} TEST(EagerPrim, TestFlags) { PrimCommonUtils::SetBwdPrimEnabled(true); From a9946ee67609b6232ac8b68805f972a47b12e629 Mon Sep 17 00:00:00 2001 From: gouzi <530971494@qq.com> Date: Thu, 25 Apr 2024 23:06:17 +0800 Subject: [PATCH 17/21] [ci][test] ignore hook_utils --- test/cpp/prim/test_eager_prim.cc | 116 +++++++++++++++---------------- 1 file changed, 58 insertions(+), 58 deletions(-) diff --git a/test/cpp/prim/test_eager_prim.cc b/test/cpp/prim/test_eager_prim.cc index c7b590379cb61..0b81c69e14417 100644 --- a/test/cpp/prim/test_eager_prim.cc +++ b/test/cpp/prim/test_eager_prim.cc @@ -18,7 +18,7 @@ #include "gtest/gtest.h" #include "paddle/common/flags.h" #include "paddle/fluid/eager/api/generated/eager_generated/forwards/dygraph_functions.h" -#include "paddle/fluid/eager/api/utils/hook_utils.h" +// #include "paddle/fluid/eager/api/utils/hook_utils.h" // #include "paddle/fluid/eager/backward.h" #include "paddle/fluid/prim/utils/utils.h" // #include "paddle/phi/core/dense_tensor.h" @@ -89,7 +89,45 @@ namespace prim { // ->data()[0]); // } -// TEST(EagerPrim, LogicalOperantsTest) { +TEST(EagerPrim, LogicalOperantsTest) { + // 1. Initialized + eager_test::InitEnv(paddle::platform::CPUPlace()); + FLAGS_tensor_operants_mode = "eager"; + paddle::prim::InitTensorOperants(); + // 2. pre + paddle::framework::DDim ddim = common::make_ddim({4, 16, 16, 32}); + paddle::Tensor tensor0 = + eager_test::CreateTensorWithValue(ddim, + paddle::platform::CPUPlace(), + phi::DataType::INT32, + phi::DataLayout::NCHW, + 1 /*value*/, + true /*is_leaf*/); + // ::egr::egr_utils_api::RetainGradForTensor(tensor0); + paddle::Tensor tensor1 = + eager_test::CreateTensorWithValue(ddim, + paddle::platform::CPUPlace(), + phi::DataType::INT32, + phi::DataLayout::NCHW, + 0 /*value*/, + true /*is_leaf*/); + // ::egr::egr_utils_api::RetainGradForTensor(tensor1); + // 3. Run Forward once + paddle::Tensor out0 = tensor0 & tensor1; + paddle::Tensor out1 = bitwise_and_ad_func(tensor0, tensor1); + EXPECT_EQ(out0.data()[0], out1.data()[0]); + out0 = tensor0 | tensor1; + out1 = bitwise_or_ad_func(tensor0, tensor1); + EXPECT_EQ(out0.data()[0], out1.data()[0]); + out0 = tensor0 ^ tensor1; + out1 = bitwise_xor_ad_func(tensor0, tensor1); + EXPECT_EQ(out0.data()[0], out1.data()[0]); + out0 = ~tensor0; + out1 = bitwise_not_ad_func(tensor0); + EXPECT_EQ(out0.data()[0], out1.data()[0]); +} + +// TEST(EagerPrim, CompareOperantsTest) { // // 1. Initialized // eager_test::InitEnv(paddle::platform::CPUPlace()); // FLAGS_tensor_operants_mode = "eager"; @@ -113,64 +151,26 @@ namespace prim { // true /*is_leaf*/); // ::egr::egr_utils_api::RetainGradForTensor(tensor1); // // 3. Run Forward once -// paddle::Tensor out0 = tensor0 & tensor1; -// paddle::Tensor out1 = bitwise_and_ad_func(tensor0, tensor1); -// EXPECT_EQ(out0.data()[0], out1.data()[0]); -// out0 = tensor0 | tensor1; -// out1 = bitwise_or_ad_func(tensor0, tensor1); -// EXPECT_EQ(out0.data()[0], out1.data()[0]); -// out0 = tensor0 ^ tensor1; -// out1 = bitwise_xor_ad_func(tensor0, tensor1); -// EXPECT_EQ(out0.data()[0], out1.data()[0]); -// out0 = ~tensor0; -// out1 = bitwise_not_ad_func(tensor0); -// EXPECT_EQ(out0.data()[0], out1.data()[0]); +// paddle::Tensor out0 = (tensor0 < tensor1); +// paddle::Tensor out1 = less_than_ad_func(tensor0, tensor1); +// EXPECT_EQ(out0.data()[0], out1.data()[0]); +// out0 = (tensor0 <= tensor1); +// out1 = less_equal_ad_func(tensor0, tensor1); +// EXPECT_EQ(out0.data()[0], out1.data()[0]); +// out0 = (tensor0 == tensor1); +// out1 = equal_ad_func(tensor0, tensor1); +// EXPECT_EQ(out0.data()[0], out1.data()[0]); +// out0 = (tensor0 != tensor1); +// out1 = not_equal_ad_func(tensor0, tensor1); +// EXPECT_EQ(out0.data()[0], out1.data()[0]); +// out0 = (tensor0 > tensor1); +// out1 = greater_than_ad_func(tensor0, tensor1); +// EXPECT_EQ(out0.data()[0], out1.data()[0]); +// out0 = (tensor0 >= tensor1); +// out1 = greater_equal_ad_func(tensor0, tensor1); +// EXPECT_EQ(out0.data()[0], out1.data()[0]); // } -TEST(EagerPrim, CompareOperantsTest) { - // 1. Initialized - eager_test::InitEnv(paddle::platform::CPUPlace()); - FLAGS_tensor_operants_mode = "eager"; - paddle::prim::InitTensorOperants(); - // 2. pre - paddle::framework::DDim ddim = common::make_ddim({4, 16, 16, 32}); - paddle::Tensor tensor0 = - eager_test::CreateTensorWithValue(ddim, - paddle::platform::CPUPlace(), - phi::DataType::INT32, - phi::DataLayout::NCHW, - 1 /*value*/, - true /*is_leaf*/); - ::egr::egr_utils_api::RetainGradForTensor(tensor0); - paddle::Tensor tensor1 = - eager_test::CreateTensorWithValue(ddim, - paddle::platform::CPUPlace(), - phi::DataType::INT32, - phi::DataLayout::NCHW, - 0 /*value*/, - true /*is_leaf*/); - ::egr::egr_utils_api::RetainGradForTensor(tensor1); - // 3. Run Forward once - paddle::Tensor out0 = (tensor0 < tensor1); - paddle::Tensor out1 = less_than_ad_func(tensor0, tensor1); - EXPECT_EQ(out0.data()[0], out1.data()[0]); - out0 = (tensor0 <= tensor1); - out1 = less_equal_ad_func(tensor0, tensor1); - EXPECT_EQ(out0.data()[0], out1.data()[0]); - out0 = (tensor0 == tensor1); - out1 = equal_ad_func(tensor0, tensor1); - EXPECT_EQ(out0.data()[0], out1.data()[0]); - out0 = (tensor0 != tensor1); - out1 = not_equal_ad_func(tensor0, tensor1); - EXPECT_EQ(out0.data()[0], out1.data()[0]); - out0 = (tensor0 > tensor1); - out1 = greater_than_ad_func(tensor0, tensor1); - EXPECT_EQ(out0.data()[0], out1.data()[0]); - out0 = (tensor0 >= tensor1); - out1 = greater_equal_ad_func(tensor0, tensor1); - EXPECT_EQ(out0.data()[0], out1.data()[0]); -} - TEST(EagerPrim, TestFlags) { PrimCommonUtils::SetBwdPrimEnabled(true); ASSERT_TRUE(PrimCommonUtils::IsBwdPrimEnabled()); From d83476b3eb6f4091793116a0cfc44813fa77a093 Mon Sep 17 00:00:00 2001 From: gouzi <530971494@qq.com> Date: Fri, 26 Apr 2024 00:12:51 +0800 Subject: [PATCH 18/21] [ci][test] ignore `dygraph_functions` and open `hook_utils` --- test/cpp/prim/test_eager_prim.cc | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/test/cpp/prim/test_eager_prim.cc b/test/cpp/prim/test_eager_prim.cc index 0b81c69e14417..4c2fc582aa822 100644 --- a/test/cpp/prim/test_eager_prim.cc +++ b/test/cpp/prim/test_eager_prim.cc @@ -17,8 +17,9 @@ #include "glog/logging.h" #include "gtest/gtest.h" #include "paddle/common/flags.h" -#include "paddle/fluid/eager/api/generated/eager_generated/forwards/dygraph_functions.h" -// #include "paddle/fluid/eager/api/utils/hook_utils.h" +// #include +// "paddle/fluid/eager/api/generated/eager_generated/forwards/dygraph_functions.h" +#include "paddle/fluid/eager/api/utils/hook_utils.h" // #include "paddle/fluid/eager/backward.h" #include "paddle/fluid/prim/utils/utils.h" // #include "paddle/phi/core/dense_tensor.h" @@ -103,7 +104,7 @@ TEST(EagerPrim, LogicalOperantsTest) { phi::DataLayout::NCHW, 1 /*value*/, true /*is_leaf*/); - // ::egr::egr_utils_api::RetainGradForTensor(tensor0); + ::egr::egr_utils_api::RetainGradForTensor(tensor0); paddle::Tensor tensor1 = eager_test::CreateTensorWithValue(ddim, paddle::platform::CPUPlace(), @@ -111,20 +112,20 @@ TEST(EagerPrim, LogicalOperantsTest) { phi::DataLayout::NCHW, 0 /*value*/, true /*is_leaf*/); - // ::egr::egr_utils_api::RetainGradForTensor(tensor1); + ::egr::egr_utils_api::RetainGradForTensor(tensor1); // 3. Run Forward once paddle::Tensor out0 = tensor0 & tensor1; - paddle::Tensor out1 = bitwise_and_ad_func(tensor0, tensor1); - EXPECT_EQ(out0.data()[0], out1.data()[0]); - out0 = tensor0 | tensor1; - out1 = bitwise_or_ad_func(tensor0, tensor1); - EXPECT_EQ(out0.data()[0], out1.data()[0]); - out0 = tensor0 ^ tensor1; - out1 = bitwise_xor_ad_func(tensor0, tensor1); - EXPECT_EQ(out0.data()[0], out1.data()[0]); - out0 = ~tensor0; - out1 = bitwise_not_ad_func(tensor0); - EXPECT_EQ(out0.data()[0], out1.data()[0]); + // paddle::Tensor out1 = bitwise_and_ad_func(tensor0, tensor1); + // EXPECT_EQ(out0.data()[0], out1.data()[0]); + // out0 = tensor0 | tensor1; + // out1 = bitwise_or_ad_func(tensor0, tensor1); + // EXPECT_EQ(out0.data()[0], out1.data()[0]); + // out0 = tensor0 ^ tensor1; + // out1 = bitwise_xor_ad_func(tensor0, tensor1); + // EXPECT_EQ(out0.data()[0], out1.data()[0]); + // out0 = ~tensor0; + // out1 = bitwise_not_ad_func(tensor0); + // EXPECT_EQ(out0.data()[0], out1.data()[0]); } // TEST(EagerPrim, CompareOperantsTest) { From 462cdf72616f15f3c7c1e7ef17e3a1ae748893c5 Mon Sep 17 00:00:00 2001 From: gouzi <530971494@qq.com> Date: Wed, 1 May 2024 15:18:19 +0800 Subject: [PATCH 19/21] fix test_comp_eager --- test/cpp/prim/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/cpp/prim/CMakeLists.txt b/test/cpp/prim/CMakeLists.txt index 7f5b3af052588..647587b79194a 100644 --- a/test/cpp/prim/CMakeLists.txt +++ b/test/cpp/prim/CMakeLists.txt @@ -23,7 +23,7 @@ if(NOT (NOT WITH_PYTHON AND ON_INFER)) cc_library(init_env_utils SRCS init_env_utils.cc) target_compile_definitions(init_env_utils PUBLIC PADDLE_DLL_EXPORT) - paddle_test(test_comp_eager SRCS test_eager_prim.cc DEPS init_env_utils) + paddle_test(test_comp_eager SRCS test_eager_prim.cc DEPS init_env_utils.cc) endif() # skip win32 since wget is not installed by default on windows machine. From c7e9825618b67747cac48734be8d8107c41acba9 Mon Sep 17 00:00:00 2001 From: gouzi <530971494@qq.com> Date: Wed, 1 May 2024 15:21:35 +0800 Subject: [PATCH 20/21] rollback `test_eager_prim.cc` --- test/cpp/prim/test_eager_prim.cc | 229 +++++++++++++++---------------- 1 file changed, 114 insertions(+), 115 deletions(-) diff --git a/test/cpp/prim/test_eager_prim.cc b/test/cpp/prim/test_eager_prim.cc index 4c2fc582aa822..ac82ecac53081 100644 --- a/test/cpp/prim/test_eager_prim.cc +++ b/test/cpp/prim/test_eager_prim.cc @@ -17,14 +17,13 @@ #include "glog/logging.h" #include "gtest/gtest.h" #include "paddle/common/flags.h" -// #include -// "paddle/fluid/eager/api/generated/eager_generated/forwards/dygraph_functions.h" +#include "paddle/fluid/eager/api/generated/eager_generated/forwards/dygraph_functions.h" #include "paddle/fluid/eager/api/utils/hook_utils.h" -// #include "paddle/fluid/eager/backward.h" +#include "paddle/fluid/eager/backward.h" #include "paddle/fluid/prim/utils/utils.h" -// #include "paddle/phi/core/dense_tensor.h" -// #include "paddle/phi/core/kernel_registry.h" -// #include "paddle/phi/core/tensor_meta.h" +#include "paddle/phi/core/dense_tensor.h" +#include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/core/tensor_meta.h" #include "test/cpp/eager/test_utils.h" #include "test/cpp/prim/init_env_utils.h" @@ -33,62 +32,62 @@ COMMON_DECLARE_string(tensor_operants_mode); namespace paddle { namespace prim { -// TEST(EagerPrim, TanhBackwardTest) { -// // 1. Initialized -// eager_test::InitEnv(paddle::platform::CPUPlace()); -// FLAGS_tensor_operants_mode = "eager"; -// paddle::prim::InitTensorOperants(); -// // 2. pre -// paddle::framework::DDim ddim = common::make_ddim({4, 16, 16, 32}); -// paddle::Tensor tensor0 = -// eager_test::CreateTensorWithValue(ddim, -// paddle::platform::CPUPlace(), -// phi::DataType::FLOAT32, -// phi::DataLayout::NCHW, -// 5.0 /*value*/, -// true /*is_leaf*/); -// ::egr::egr_utils_api::RetainGradForTensor(tensor0); -// paddle::Tensor tensor1 = -// eager_test::CreateTensorWithValue(ddim, -// paddle::platform::CPUPlace(), -// phi::DataType::FLOAT32, -// phi::DataLayout::NCHW, -// 5.0 /*value*/, -// true /*is_leaf*/); -// ::egr::egr_utils_api::RetainGradForTensor(tensor1); -// // 3. Run Forward once -// paddle::Tensor out0 = tanh_ad_func(tensor0); -// std::vector outs0 = {out0}; -// // Disable prim -// PrimCommonUtils::SetBwdPrimEnabled(false); -// ASSERT_FALSE(PrimCommonUtils::IsBwdPrimEnabled()); -// // 4. Run Backward -// egr::Backward(outs0, {}, false); +TEST(EagerPrim, TanhBackwardTest) { + // 1. Initialized + eager_test::InitEnv(paddle::platform::CPUPlace()); + FLAGS_tensor_operants_mode = "eager"; + paddle::prim::InitTensorOperants(); + // 2. pre + paddle::framework::DDim ddim = common::make_ddim({4, 16, 16, 32}); + paddle::Tensor tensor0 = + eager_test::CreateTensorWithValue(ddim, + paddle::platform::CPUPlace(), + phi::DataType::FLOAT32, + phi::DataLayout::NCHW, + 5.0 /*value*/, + true /*is_leaf*/); + ::egr::egr_utils_api::RetainGradForTensor(tensor0); + paddle::Tensor tensor1 = + eager_test::CreateTensorWithValue(ddim, + paddle::platform::CPUPlace(), + phi::DataType::FLOAT32, + phi::DataLayout::NCHW, + 5.0 /*value*/, + true /*is_leaf*/); + ::egr::egr_utils_api::RetainGradForTensor(tensor1); + // 3. Run Forward once + paddle::Tensor out0 = tanh_ad_func(tensor0); + std::vector outs0 = {out0}; + // Disable prim + PrimCommonUtils::SetBwdPrimEnabled(false); + ASSERT_FALSE(PrimCommonUtils::IsBwdPrimEnabled()); + // 4. Run Backward + egr::Backward(outs0, {}, false); -// paddle::Tensor out1 = tanh_ad_func(tensor1); -// std::vector outs1 = {out1}; -// // Enable prim -// PrimCommonUtils::SetBwdPrimEnabled(true); -// ASSERT_TRUE(PrimCommonUtils::IsBwdPrimEnabled()); -// // 4. Run Backward -// ::egr::Backward(outs1, {}, false); -// VLOG(7) -// << "Target Grad is: " -// << std::static_pointer_cast( -// ::egr::EagerUtils::unsafe_autograd_meta(tensor0)->Grad().impl()) -// ->data()[0]; -// VLOG(7) -// << "Result Grad is: " -// << std::static_pointer_cast( -// ::egr::EagerUtils::unsafe_autograd_meta(tensor1)->Grad().impl()) -// ->data()[0]; -// // Examine Backward Grad -// eager_test::CompareGradTensorWithValue( -// tensor1, -// std::static_pointer_cast( -// ::egr::EagerUtils::unsafe_autograd_meta(tensor0)->Grad().impl()) -// ->data()[0]); -// } + paddle::Tensor out1 = tanh_ad_func(tensor1); + std::vector outs1 = {out1}; + // Enable prim + PrimCommonUtils::SetBwdPrimEnabled(true); + ASSERT_TRUE(PrimCommonUtils::IsBwdPrimEnabled()); + // 4. Run Backward + ::egr::Backward(outs1, {}, false); + VLOG(7) + << "Target Grad is: " + << std::static_pointer_cast( + ::egr::EagerUtils::unsafe_autograd_meta(tensor0)->Grad().impl()) + ->data()[0]; + VLOG(7) + << "Result Grad is: " + << std::static_pointer_cast( + ::egr::EagerUtils::unsafe_autograd_meta(tensor1)->Grad().impl()) + ->data()[0]; + // Examine Backward Grad + eager_test::CompareGradTensorWithValue( + tensor1, + std::static_pointer_cast( + ::egr::EagerUtils::unsafe_autograd_meta(tensor0)->Grad().impl()) + ->data()[0]); +} TEST(EagerPrim, LogicalOperantsTest) { // 1. Initialized @@ -115,62 +114,62 @@ TEST(EagerPrim, LogicalOperantsTest) { ::egr::egr_utils_api::RetainGradForTensor(tensor1); // 3. Run Forward once paddle::Tensor out0 = tensor0 & tensor1; - // paddle::Tensor out1 = bitwise_and_ad_func(tensor0, tensor1); - // EXPECT_EQ(out0.data()[0], out1.data()[0]); - // out0 = tensor0 | tensor1; - // out1 = bitwise_or_ad_func(tensor0, tensor1); - // EXPECT_EQ(out0.data()[0], out1.data()[0]); - // out0 = tensor0 ^ tensor1; - // out1 = bitwise_xor_ad_func(tensor0, tensor1); - // EXPECT_EQ(out0.data()[0], out1.data()[0]); - // out0 = ~tensor0; - // out1 = bitwise_not_ad_func(tensor0); - // EXPECT_EQ(out0.data()[0], out1.data()[0]); + paddle::Tensor out1 = bitwise_and_ad_func(tensor0, tensor1); + EXPECT_EQ(out0.data()[0], out1.data()[0]); + out0 = tensor0 | tensor1; + out1 = bitwise_or_ad_func(tensor0, tensor1); + EXPECT_EQ(out0.data()[0], out1.data()[0]); + out0 = tensor0 ^ tensor1; + out1 = bitwise_xor_ad_func(tensor0, tensor1); + EXPECT_EQ(out0.data()[0], out1.data()[0]); + out0 = ~tensor0; + out1 = bitwise_not_ad_func(tensor0); + EXPECT_EQ(out0.data()[0], out1.data()[0]); } -// TEST(EagerPrim, CompareOperantsTest) { -// // 1. Initialized -// eager_test::InitEnv(paddle::platform::CPUPlace()); -// FLAGS_tensor_operants_mode = "eager"; -// paddle::prim::InitTensorOperants(); -// // 2. pre -// paddle::framework::DDim ddim = common::make_ddim({4, 16, 16, 32}); -// paddle::Tensor tensor0 = -// eager_test::CreateTensorWithValue(ddim, -// paddle::platform::CPUPlace(), -// phi::DataType::INT32, -// phi::DataLayout::NCHW, -// 1 /*value*/, -// true /*is_leaf*/); -// ::egr::egr_utils_api::RetainGradForTensor(tensor0); -// paddle::Tensor tensor1 = -// eager_test::CreateTensorWithValue(ddim, -// paddle::platform::CPUPlace(), -// phi::DataType::INT32, -// phi::DataLayout::NCHW, -// 0 /*value*/, -// true /*is_leaf*/); -// ::egr::egr_utils_api::RetainGradForTensor(tensor1); -// // 3. Run Forward once -// paddle::Tensor out0 = (tensor0 < tensor1); -// paddle::Tensor out1 = less_than_ad_func(tensor0, tensor1); -// EXPECT_EQ(out0.data()[0], out1.data()[0]); -// out0 = (tensor0 <= tensor1); -// out1 = less_equal_ad_func(tensor0, tensor1); -// EXPECT_EQ(out0.data()[0], out1.data()[0]); -// out0 = (tensor0 == tensor1); -// out1 = equal_ad_func(tensor0, tensor1); -// EXPECT_EQ(out0.data()[0], out1.data()[0]); -// out0 = (tensor0 != tensor1); -// out1 = not_equal_ad_func(tensor0, tensor1); -// EXPECT_EQ(out0.data()[0], out1.data()[0]); -// out0 = (tensor0 > tensor1); -// out1 = greater_than_ad_func(tensor0, tensor1); -// EXPECT_EQ(out0.data()[0], out1.data()[0]); -// out0 = (tensor0 >= tensor1); -// out1 = greater_equal_ad_func(tensor0, tensor1); -// EXPECT_EQ(out0.data()[0], out1.data()[0]); -// } +TEST(EagerPrim, CompareOperantsTest) { + // 1. Initialized + eager_test::InitEnv(paddle::platform::CPUPlace()); + FLAGS_tensor_operants_mode = "eager"; + paddle::prim::InitTensorOperants(); + // 2. pre + paddle::framework::DDim ddim = common::make_ddim({4, 16, 16, 32}); + paddle::Tensor tensor0 = + eager_test::CreateTensorWithValue(ddim, + paddle::platform::CPUPlace(), + phi::DataType::INT32, + phi::DataLayout::NCHW, + 1 /*value*/, + true /*is_leaf*/); + ::egr::egr_utils_api::RetainGradForTensor(tensor0); + paddle::Tensor tensor1 = + eager_test::CreateTensorWithValue(ddim, + paddle::platform::CPUPlace(), + phi::DataType::INT32, + phi::DataLayout::NCHW, + 0 /*value*/, + true /*is_leaf*/); + ::egr::egr_utils_api::RetainGradForTensor(tensor1); + // 3. Run Forward once + paddle::Tensor out0 = (tensor0 < tensor1); + paddle::Tensor out1 = less_than_ad_func(tensor0, tensor1); + EXPECT_EQ(out0.data()[0], out1.data()[0]); + out0 = (tensor0 <= tensor1); + out1 = less_equal_ad_func(tensor0, tensor1); + EXPECT_EQ(out0.data()[0], out1.data()[0]); + out0 = (tensor0 == tensor1); + out1 = equal_ad_func(tensor0, tensor1); + EXPECT_EQ(out0.data()[0], out1.data()[0]); + out0 = (tensor0 != tensor1); + out1 = not_equal_ad_func(tensor0, tensor1); + EXPECT_EQ(out0.data()[0], out1.data()[0]); + out0 = (tensor0 > tensor1); + out1 = greater_than_ad_func(tensor0, tensor1); + EXPECT_EQ(out0.data()[0], out1.data()[0]); + out0 = (tensor0 >= tensor1); + out1 = greater_equal_ad_func(tensor0, tensor1); + EXPECT_EQ(out0.data()[0], out1.data()[0]); +} TEST(EagerPrim, TestFlags) { PrimCommonUtils::SetBwdPrimEnabled(true); From de20f12ab03a685985e67c0d68f838fa2e636b6b Mon Sep 17 00:00:00 2001 From: gouzi <530971494@qq.com> Date: Wed, 1 May 2024 18:44:28 +0800 Subject: [PATCH 21/21] fix --- test/cpp/prim/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/cpp/prim/CMakeLists.txt b/test/cpp/prim/CMakeLists.txt index 647587b79194a..51b5bb70a6e22 100644 --- a/test/cpp/prim/CMakeLists.txt +++ b/test/cpp/prim/CMakeLists.txt @@ -23,7 +23,7 @@ if(NOT (NOT WITH_PYTHON AND ON_INFER)) cc_library(init_env_utils SRCS init_env_utils.cc) target_compile_definitions(init_env_utils PUBLIC PADDLE_DLL_EXPORT) - paddle_test(test_comp_eager SRCS test_eager_prim.cc DEPS init_env_utils.cc) + paddle_test(test_comp_eager SRCS test_eager_prim.cc init_env_utils.cc) endif() # skip win32 since wget is not installed by default on windows machine.