MCPcopy Create free account
hub / github.com/PaddlePaddle/Paddle / TEST

Function TEST

test/cpp/eager/task_tests/backward_test.cc:36–85  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

34namespace egr {
35
36TEST(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
87TEST(Backward, SingleNodeCustomGrad) {
88 // Prepare Device Contexts

Callers

nothing calls this directly

Calls 13

InitEnvFunction · 0.85
make_ddimFunction · 0.85
CreateTensorWithValueFunction · 0.85
BackwardFunction · 0.85
SetAttributes_scaleMethod · 0.80
SetGradOutMetaMethod · 0.80
CPUPlaceClass · 0.50
TensorClass · 0.50
SetGradNodeMethod · 0.45
SetStopGradientMethod · 0.45

Tested by

no test coverage detected