test_grad_fn_and_next_functions.py 2.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
# 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.
"""
Test the tensor attribute grad_fn and the properties of the reverse node grad_node, such as next_function
"""

import unittest

import paddle
21
from paddle import nn
22 23 24 25


class Testmodel(nn.Layer):
    def __init__(self):
26
        super().__init__()
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

    def forward(self, x):
        y = x**2
        y = x + y
        return y


class TestAnonmousSurvey(unittest.TestCase):
    """
    Test the tensor attribute grad_fn and the properties of the reverse node grad_node, such as next_function

    """

    def init_graph(self):
        """define reversed graph

        func_name [str]: represents the name of the operator node
        next_funcs [dict]: represents the operator node
        """
        self.grad_fn_1 = {"func_name": "GradNodeAccumulation", "next_funcs": {}}
        self.grad_fn_2 = {
            "func_name": "PowGradNode",
            "next_funcs": {"GradNodeAccumulation": self.grad_fn_1},
        }
        self.grad_fn_3 = {
            "func_name": "AddGradNode",
            "next_funcs": {
                "GradNodeAccumulation": self.grad_fn_1,
                "PowGradNode": self.grad_fn_2,
            },
        }
        self.output_grad_fn = {"grad_fn": self.grad_fn_3}

    def init_data(self):
        """define output of model

        the final output will be saved self.output
        """
        model = Testmodel()
        x = paddle.randn([1, 3, 24, 24])
        x.stop_gradient = False
        self.output = model(x)

    def setUp(self):
        self.init_graph()
        self.init_data()

    def test_grad_fn_and_next_funs(self):
        self.check_func(self.output.grad_fn, self.output_grad_fn["grad_fn"])

77
    def check_func(self, grad_fn, grad_fn_json) -> None:
78 79 80 81 82 83 84 85 86 87
        """
        Check each node, grad_fn is tensor attribute. grad_fn_json is structure of next_node.

        Args:
            grad_fn (grad_fn): grad_fn of node
            grad_fn_json (dict): grad_node_json of node
        """
        self.assertEqual(grad_fn.name(), grad_fn_json["func_name"])


88 89
if __name__ == "__main__":
    unittest.main()