(self)
| 923 | ) |
| 924 | |
| 925 | def test_drop_output(self): |
| 926 | num_iters = 3 |
| 927 | child_name_prefix = "foo" |
| 928 | |
| 929 | # By default, nothing is dropped. |
| 930 | with self._dummy_context() as ctx: |
| 931 | self._invoke(num_iters=num_iters, xs={}, child_name_prefix=child_name_prefix) |
| 932 | self.assertNestedEqual( |
| 933 | { |
| 934 | "output": jnp.array([[0, 0], [1, 1], [2, 2]]), |
| 935 | "with_carry": { |
| 936 | "output": jnp.array([[0, 0], [2, 2], [4, 4]]), |
| 937 | "with_state": {"output": jnp.array([[0, 10], [2, 12], [4, 14]])}, |
| 938 | }, |
| 939 | }, |
| 940 | ctx.output_collection.module_outputs[child_name_prefix]["nested"], |
| 941 | ) |
| 942 | # Summary values are put in `{child_name_prefix}{i}`. |
| 943 | self.assertNestedEqual( |
| 944 | { |
| 945 | f"{child_name_prefix}{i}": {"carry": WeightedSummary(i, 1)} |
| 946 | for i in range(num_iters) |
| 947 | }, |
| 948 | ctx.output_collection.summaries, |
| 949 | ) |
| 950 | |
| 951 | # Invoke with an output dropper that drops everything. |
| 952 | with self._dummy_context() as ctx: |
| 953 | self._invoke( |
| 954 | num_iters=num_iters, |
| 955 | xs={}, |
| 956 | drop_output=lambda _: True, |
| 957 | child_name_prefix=child_name_prefix, |
| 958 | ) |
| 959 | self.assertNestedEqual({}, ctx.output_collection.module_outputs) |
| 960 | |
| 961 | # Invoke with an output dropper that matches specific paths. |
| 962 | with self._dummy_context() as ctx: |
| 963 | self._invoke( |
| 964 | num_iters=num_iters, |
| 965 | xs={}, |
| 966 | drop_output=lambda path: match_regex_rules(path, rules=[(".*/with_carry.*", True)]), |
| 967 | child_name_prefix=child_name_prefix, |
| 968 | ) |
| 969 | self.assertNestedEqual( |
| 970 | { |
| 971 | "output": jnp.array([[0, 0], [1, 1], [2, 2]]), |
| 972 | }, |
| 973 | ctx.output_collection.module_outputs[child_name_prefix]["nested"], |
| 974 | ) |
| 975 | |
| 976 | # Invoke with an output dropper that matches specific paths. |
| 977 | with self._dummy_context() as ctx: |
| 978 | self._invoke( |
| 979 | num_iters=num_iters, |
| 980 | xs={}, |
| 981 | drop_output=lambda path: match_regex_rules(path, rules=[(".*/with_state.*", True)]), |
| 982 | child_name_prefix=child_name_prefix, |
nothing calls this directly
no test coverage detected