| 34 | namespace egr { |
| 35 | |
| 36 | TEST(Backward, SingleNodeEmptyGrad) { |
| 37 | // Prepare Device Contexts |
| 38 | eager_test::InitEnv(phi::CPUPlace()); |
| 39 | |
| 40 | // Prepare Inputs |
| 41 | phi::DDim ddim = common::make_ddim({4, 16, 16, 32}); |
| 42 | |
| 43 | // Create Target Tensor |
| 44 | paddle::Tensor target_tensor = |
| 45 | eager_test::CreateTensorWithValue(ddim, |
| 46 | phi::CPUPlace(), |
| 47 | phi::DataType::FLOAT32, |
| 48 | phi::DataLayout::NCHW, |
| 49 | 1.0 /*value*/, |
| 50 | false /*is_leaf*/); |
| 51 | |
| 52 | paddle::Tensor leaf_tensor; |
| 53 | { |
| 54 | // Create Scale Node |
| 55 | auto node0_ptr = std::make_shared<GradNodeScale>(1, 1); |
| 56 | node0_ptr->SetAttributes_scale(5.0 /*scale*/); |
| 57 | |
| 58 | // Set grad in/out meta |
| 59 | node0_ptr->SetDefaultGradInOutMeta(); |
| 60 | AutogradMeta* auto_grad_meta = EagerUtils::autograd_meta(&target_tensor); |
| 61 | auto_grad_meta->SetGradNode( |
| 62 | std::dynamic_pointer_cast<GradNodeBase>(node0_ptr)); |
| 63 | auto_grad_meta->SetSingleOutRankWithSlot(0, 0); |
| 64 | auto_grad_meta->SetStopGradient(false); |
| 65 | |
| 66 | AutogradMeta* auto_grad_meta1 = EagerUtils::autograd_meta(&leaf_tensor); |
| 67 | |
| 68 | // Connect Tensor and AccumulationNode via AutoGradMeta |
| 69 | auto acc_node_ptr = |
| 70 | std::make_shared<egr::GradNodeAccumulation>(leaf_tensor); |
| 71 | |
| 72 | auto_grad_meta1->SetGradNode( |
| 73 | std::dynamic_pointer_cast<GradNodeBase>(acc_node_ptr)); |
| 74 | auto_grad_meta1->SetSingleOutRankWithSlot(0, 0); |
| 75 | auto_grad_meta1->SetStopGradient(false); |
| 76 | |
| 77 | node0_ptr->SetGradOutMeta({leaf_tensor}, 0); |
| 78 | } |
| 79 | std::vector<paddle::Tensor> outs = {target_tensor}; |
| 80 | // Run Backward |
| 81 | Backward(outs, {}); |
| 82 | |
| 83 | // Check Output Value |
| 84 | eager_test::CompareGradTensorWithValue<float>(leaf_tensor, 5.0); |
| 85 | } |
| 86 | |
| 87 | TEST(Backward, SingleNodeCustomGrad) { |
| 88 | // Prepare Device Contexts |
nothing calls this directly
no test coverage detected