|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +import os |
| 8 | +import unittest |
| 9 | + |
| 10 | +import torch |
| 11 | +from pytorch3d.implicitron.tools.config import expand_args_fields, get_default_args |
| 12 | + |
| 13 | +from ..impl.optimizer_factory import ImplicitronOptimizerFactory |
| 14 | + |
| 15 | +internal = os.environ.get("FB_TEST", False) |
| 16 | + |
| 17 | + |
| 18 | +class TestOptimizerFactory(unittest.TestCase): |
| 19 | + def setUp(self) -> None: |
| 20 | + torch.manual_seed(42) |
| 21 | + expand_args_fields(ImplicitronOptimizerFactory) |
| 22 | + |
| 23 | + def _get_param_groups(self, model): |
| 24 | + default_cfg = get_default_args(ImplicitronOptimizerFactory) |
| 25 | + factory = ImplicitronOptimizerFactory(default_cfg) |
| 26 | + return factory._get_param_groups(model) |
| 27 | + |
| 28 | + def _assert_allin(self, a, param_groups, key): |
| 29 | + with self.subTest(f"Testing key {key}"): |
| 30 | + b = param_groups[key] |
| 31 | + for el in a: |
| 32 | + if el not in b: |
| 33 | + raise ValueError( |
| 34 | + f"Element {el}\n\n from:\n\n {a}\n\n not in:\n\n {b}\n\n." |
| 35 | + + f" Full param groups = \n\n{param_groups}" |
| 36 | + ) |
| 37 | + for el in b: |
| 38 | + if el not in a: |
| 39 | + raise ValueError( |
| 40 | + f"Element {el}\n\n from:\n\n {b}\n\n not in:\n\n {a}\n\n." |
| 41 | + + f" Full param groups = \n\n{param_groups}" |
| 42 | + ) |
| 43 | + |
| 44 | + def test_default_param_group_assignment(self): |
| 45 | + pa, pb, pc = [torch.nn.Parameter(data=torch.tensor(i * 1.0)) for i in range(3)] |
| 46 | + na, nb = Node(params=[pa]), Node(params=[pb]) |
| 47 | + root = Node(children=[na, nb], params=[pc]) |
| 48 | + param_groups = self._get_param_groups(root) |
| 49 | + self._assert_allin([pa, pb, pc], param_groups, "default") |
| 50 | + |
| 51 | + def test_member_overrides_default_param_group_assignment(self): |
| 52 | + pa, pb, pc = [torch.nn.Parameter(data=torch.tensor(i * 1.0)) for i in range(3)] |
| 53 | + na, nb = Node(params=[pa]), Node(params=[pb]) |
| 54 | + root = Node(children=[na, nb], params=[pc], param_groups={"m1": "pb"}) |
| 55 | + param_groups = self._get_param_groups(root) |
| 56 | + self._assert_allin([pa, pc], param_groups, "default") |
| 57 | + self._assert_allin([pb], param_groups, "pb") |
| 58 | + |
| 59 | + def test_self_overrides_member_param_group_assignment(self): |
| 60 | + pa, pb, pc = [torch.nn.Parameter(data=torch.tensor(i * 1.0)) for i in range(3)] |
| 61 | + na, nb = Node(params=[pa]), Node(params=[pb], param_groups={"self": "pb_self"}) |
| 62 | + root = Node(children=[na, nb], params=[pc], param_groups={"m1": "pb_member"}) |
| 63 | + param_groups = self._get_param_groups(root) |
| 64 | + self._assert_allin([pa, pc], param_groups, "default") |
| 65 | + self._assert_allin([pb], param_groups, "pb_self") |
| 66 | + assert len(param_groups["pb_member"]) == 0, param_groups |
| 67 | + |
| 68 | + def test_param_overrides_self_param_group_assignment(self): |
| 69 | + pa, pb, pc = [torch.nn.Parameter(data=torch.tensor(i * 1.0)) for i in range(3)] |
| 70 | + na, nb = Node(params=[pa]), Node( |
| 71 | + params=[pb], param_groups={"self": "pb_self", "p1": "pb_param"} |
| 72 | + ) |
| 73 | + root = Node(children=[na, nb], params=[pc], param_groups={"m1": "pb_member"}) |
| 74 | + param_groups = self._get_param_groups(root) |
| 75 | + self._assert_allin([pa, pc], param_groups, "default") |
| 76 | + self._assert_allin([pb], param_groups, "pb_self") |
| 77 | + assert len(param_groups["pb_member"]) == 0, param_groups |
| 78 | + |
| 79 | + def test_no_param_groups_defined(self): |
| 80 | + pa, pb, pc = [torch.nn.Parameter(data=torch.tensor(i * 1.0)) for i in range(3)] |
| 81 | + na, nb = Node(params=[pa]), Node(params=[pb]) |
| 82 | + root = Node(children=[na, nb], params=[pc]) |
| 83 | + param_groups = self._get_param_groups(root) |
| 84 | + self._assert_allin([pa, pb, pc], param_groups, "default") |
| 85 | + |
| 86 | + def test_tree_param_groups_defined(self): |
| 87 | + """ |
| 88 | + Test generic tree assignment. |
| 89 | +
|
| 90 | + A0 |
| 91 | + |--------------------------- |
| 92 | + | | | |
| 93 | + Bb M J- |
| 94 | + |----- |------- |
| 95 | + | | | | |
| 96 | + C Ddg K Ll |
| 97 | + |-------------- |
| 98 | + | | | | |
| 99 | + E4 Ff G H- |
| 100 | +
|
| 101 | + All nodes have one parameter. Character next to the capital |
| 102 | + letter means they have added something to their `parameter_groups`: |
| 103 | + - small letter same as capital means self is set to that letter |
| 104 | + - small letter different then capital means that member is set |
| 105 | + (the one that is named like that) |
| 106 | + - number means parameter's parameter_group is set like that |
| 107 | + - "-" means it does not have `parameter_groups` member |
| 108 | + """ |
| 109 | + p = [torch.nn.Parameter(data=torch.tensor(i * 1.0)) for i in range(12)] |
| 110 | + L = Node(params=[p[11]], param_groups={"self": "l"}) |
| 111 | + K = Node(params=[p[10]], param_groups={}) |
| 112 | + J = Node(params=[p[9]], param_groups=None, children=[K, L]) |
| 113 | + M = Node(params=[p[8]], param_groups={}) |
| 114 | + |
| 115 | + E = Node(params=[p[4]], param_groups={"p0": "4"}) |
| 116 | + F = Node(params=[p[5]], param_groups={"self": "f"}) |
| 117 | + G = Node(params=[p[6]], param_groups={}) |
| 118 | + H = Node(params=[p[7]], param_groups=None) |
| 119 | + |
| 120 | + D = Node( |
| 121 | + params=[p[3]], param_groups={"self": "d", "m2": "g"}, children=[E, F, G, H] |
| 122 | + ) |
| 123 | + C = Node(params=[p[2]], param_groups={}) |
| 124 | + |
| 125 | + B = Node(params=[p[1]], param_groups={"self": "b"}, children=[C, D]) |
| 126 | + |
| 127 | + A = Node(params=[p[0]], param_groups={"p0": "0"}, children=[B, M, J]) |
| 128 | + |
| 129 | + param_groups = self._get_param_groups(A) |
| 130 | + |
| 131 | + # if parts of the group belong to two different categories assert is repeated |
| 132 | + # parameter level |
| 133 | + self._assert_allin([p[0]], param_groups, "0") |
| 134 | + self._assert_allin([p[4]], param_groups, "4") |
| 135 | + # self level |
| 136 | + self._assert_allin([p[5]], param_groups, "f") |
| 137 | + self._assert_allin([p[11]], param_groups, "l") |
| 138 | + self._assert_allin([p[2], p[1]], param_groups, "b") |
| 139 | + self._assert_allin([p[7], p[3]], param_groups, "d") |
| 140 | + # member level |
| 141 | + self._assert_allin([p[6]], param_groups, "g") |
| 142 | + # inherit level |
| 143 | + self._assert_allin([p[7], p[3]], param_groups, "d") |
| 144 | + self._assert_allin([p[2], p[1]], param_groups, "b") |
| 145 | + # default level |
| 146 | + self._assert_allin([p[8], p[9], p[10]], param_groups, "default") |
| 147 | + |
| 148 | + |
| 149 | +class Node(torch.nn.Module): |
| 150 | + def __init__(self, children=(), params=(), param_groups=None): |
| 151 | + super().__init__() |
| 152 | + for i, child in enumerate(children): |
| 153 | + self.add_module("m" + str(i), child) |
| 154 | + for i, param in enumerate(params): |
| 155 | + setattr(self, "p" + str(i), param) |
| 156 | + if param_groups is not None: |
| 157 | + self.param_groups = param_groups |
| 158 | + |
| 159 | + def __str__(self): |
| 160 | + return ( |
| 161 | + "modules:\n" + str(self._modules) + "\nparameters\n" + str(self._parameters) |
| 162 | + ) |
0 commit comments