|
| 1 | +# coding=utf-8 |
| 2 | +# Copyright 2022 The Fiddle-Config Authors. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +"""Tests for no_custom_objects.""" |
| 17 | + |
| 18 | +import dataclasses |
| 19 | +import typing |
| 20 | + |
| 21 | +from absl.testing import absltest |
| 22 | +import fiddle as fdl |
| 23 | +from fiddle import daglish |
| 24 | +from fiddle._src.testing.example import fake_encoder_decoder |
| 25 | +from fiddle._src.validation import no_custom_objects |
| 26 | + |
| 27 | + |
| 28 | +def foo(x): |
| 29 | + return x |
| 30 | + |
| 31 | + |
| 32 | +class MyNamedtuple(typing.NamedTuple): |
| 33 | + a: int |
| 34 | + b: str |
| 35 | + |
| 36 | + |
| 37 | +@dataclasses.dataclass(frozen=True) |
| 38 | +class MyDataclass: |
| 39 | + a: int |
| 40 | + b: str |
| 41 | + |
| 42 | + |
| 43 | +class NoCustomObjectsTest(absltest.TestCase): |
| 44 | + |
| 45 | + def test_empty_history(self): |
| 46 | + self.assertEqual( |
| 47 | + no_custom_objects._concise_history(entries=[]), "(no history)" |
| 48 | + ) |
| 49 | + |
| 50 | + def test_concise_history(self): |
| 51 | + config = fake_encoder_decoder.fixture.as_buildable() |
| 52 | + config.encoder.attention.dtype = "float64" |
| 53 | + with self.subTest("overridden"): |
| 54 | + history = no_custom_objects._concise_history( |
| 55 | + entries=config.encoder.attention.__argument_history__["dtype"] |
| 56 | + ) |
| 57 | + self.assertRegex(history, r"Set in .+:\d+:test_concise_history") |
| 58 | + with self.subTest("not_overridden"): |
| 59 | + history = no_custom_objects._concise_history( |
| 60 | + entries=config.encoder.__argument_history__["attention"] |
| 61 | + ) |
| 62 | + self.assertRegex(history, r"Set in .+fake_encoder.+:\d+:fixture") |
| 63 | + with self.subTest("deleted"): |
| 64 | + del config.encoder.attention.dtype |
| 65 | + history = no_custom_objects._concise_history( |
| 66 | + entries=config.encoder.attention.__argument_history__["dtype"] |
| 67 | + ) |
| 68 | + self.assertRegex(history, r"Deleted in .+:\d+:test_concise_history") |
| 69 | + |
| 70 | + def test_get_history_from_state(self): |
| 71 | + config = fdl.Config(foo, {"a": {"b": 1}}) |
| 72 | + traversal = daglish.MemoizedTraversal(NotImplemented, config) # pytype: disable=wrong-arg-types |
| 73 | + state = traversal.initial_state() |
| 74 | + state = daglish.State( |
| 75 | + state.traversal, |
| 76 | + (*state.current_path, daglish.Attr("x")), |
| 77 | + config.x, |
| 78 | + state, |
| 79 | + ) |
| 80 | + state = daglish.State( |
| 81 | + state.traversal, |
| 82 | + (*state.current_path, daglish.Key("a")), |
| 83 | + config.x["a"], |
| 84 | + state, |
| 85 | + ) |
| 86 | + history1 = no_custom_objects._get_history_from_state(state=state) |
| 87 | + state = daglish.State( |
| 88 | + state.traversal, |
| 89 | + (*state.current_path, daglish.Key("b")), |
| 90 | + config.x["a"]["b"], |
| 91 | + state, |
| 92 | + ) |
| 93 | + history2 = no_custom_objects._get_history_from_state(state=state) |
| 94 | + self.assertNotEmpty(history2) |
| 95 | + self.assertIs( |
| 96 | + history1, |
| 97 | + history2, |
| 98 | + msg=( |
| 99 | + "Since dictionaries do not have state, _get_history_from_state" |
| 100 | + " should traverse to an ancestor to find the history." |
| 101 | + ), |
| 102 | + ) |
| 103 | + |
| 104 | + def test_get_config_errors_empty(self): |
| 105 | + config = fake_encoder_decoder.fixture.as_buildable() |
| 106 | + self.assertEmpty(no_custom_objects.get_config_errors(config=config)) |
| 107 | + |
| 108 | + def test_get_config_errors_namedtuple(self): |
| 109 | + config = fake_encoder_decoder.fixture.as_buildable() |
| 110 | + config.encoder.attention = MyNamedtuple(1, "a") |
| 111 | + errors = no_custom_objects.get_config_errors(config=config) |
| 112 | + self.assertLen(errors, 1) |
| 113 | + self.assertRegex( |
| 114 | + errors[0], |
| 115 | + r"Found.*namedtuple.*at \.encoder\.attention.*Set" |
| 116 | + r" in.*:\d+:test_get_config_errors_namedtuple", |
| 117 | + ) |
| 118 | + |
| 119 | + def test_get_config_errors_dataclass(self): |
| 120 | + config = fake_encoder_decoder.fixture.as_buildable() |
| 121 | + config.encoder.attention = MyDataclass(1, "a") |
| 122 | + errors = no_custom_objects.get_config_errors(config=config) |
| 123 | + self.assertLen(errors, 1) |
| 124 | + self.assertRegex( |
| 125 | + errors[0], |
| 126 | + r"Found.*dataclass.*at \.encoder\.attention.*Set" |
| 127 | + r" in.*:\d+:test_get_config_errors_dataclass", |
| 128 | + ) |
| 129 | + |
| 130 | + def test_get_config_errors_not_empty(self): |
| 131 | + config = fake_encoder_decoder.fixture.as_buildable() |
| 132 | + config.encoder.attention = object() |
| 133 | + errors = no_custom_objects.get_config_errors(config=config) |
| 134 | + self.assertLen(errors, 1) |
| 135 | + self.assertRegex( |
| 136 | + errors[0], |
| 137 | + r"Found.*object.*at \.encoder\.attention.*Set" |
| 138 | + r" in.*:\d+:test_get_config_errors_not_empty", |
| 139 | + ) |
| 140 | + |
| 141 | + def test_check_no_custom_objects_okay(self): |
| 142 | + config = fake_encoder_decoder.fixture.as_buildable() |
| 143 | + no_custom_objects.check_no_custom_objects(config) |
| 144 | + |
| 145 | + def test_check_no_custom_objects_error(self): |
| 146 | + config = fake_encoder_decoder.fixture.as_buildable() |
| 147 | + config.encoder.attention = object() |
| 148 | + config.decoder.self_attention = object() |
| 149 | + with self.assertRaisesRegex( |
| 150 | + ValueError, |
| 151 | + r"Custom objects were found.*Custom objects:\n Found.*object.*at" |
| 152 | + r" \.encoder\.attention, Set in" |
| 153 | + r" .*:\d+:test_check_no_custom_objects_error\n Found.*object.*at" |
| 154 | + r" \.decoder\.self_attention, Set in" |
| 155 | + r" .*:\d+:test_check_no_custom_objects_error", |
| 156 | + ): |
| 157 | + no_custom_objects.check_no_custom_objects(config=config) |
| 158 | + |
| 159 | + def test_no_history_custom_objects_error(self): |
| 160 | + config = { |
| 161 | + "encoder_attention": object(), |
| 162 | + "decoder_self_attention": object(), |
| 163 | + } |
| 164 | + with self.assertRaisesRegex( |
| 165 | + ValueError, |
| 166 | + r"Custom objects were found.*Custom objects:" |
| 167 | + r"\n Found.*object.*at \['encoder_attention'\],.*no history.*" |
| 168 | + r"\n Found.*object.*at \['decoder_self_attention'\],.*no history.*", |
| 169 | + ): |
| 170 | + no_custom_objects.check_no_custom_objects(config=config) |
| 171 | + |
| 172 | + |
| 173 | +if __name__ == "__main__": |
| 174 | + absltest.main() |
0 commit comments