Spaces:
Running
on
Zero
Running
on
Zero
| # This file includes code derived from the SiT project (https://github.com/willisma/SiT), | |
| # which is licensed under the MIT License. | |
| # | |
| # MIT License | |
| # | |
| # Copyright (c) Meta Platforms, Inc. and affiliates. | |
| # | |
| # Permission is hereby granted, free of charge, to any person obtaining a copy | |
| # of this software and associated documentation files (the "Software"), to deal | |
| # in the Software without restriction, including without limitation the rights | |
| # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| # copies of the Software, and to permit persons to whom the Software is | |
| # furnished to do so, subject to the following conditions: | |
| # | |
| # The above copyright notice and this permission notice shall be included in all | |
| # copies or substantial portions of the Software. | |
| # | |
| # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
| # SOFTWARE. | |
| import torch as th | |
| class EasyDict: | |
| def __init__(self, sub_dict): | |
| for k, v in sub_dict.items(): | |
| setattr(self, k, v) | |
| def __getitem__(self, key): | |
| return getattr(self, key) | |
| def mean_flat(x): | |
| """ | |
| Take the mean over all non-batch dimensions. | |
| """ | |
| return th.mean(x, dim=list(range(1, len(x.size())))) | |
| def log_state(state): | |
| result = [] | |
| sorted_state = dict(sorted(state.items())) | |
| for key, value in sorted_state.items(): | |
| # Check if the value is an instance of a class | |
| if "<object" in str(value) or "object at" in str(value): | |
| result.append(f"{key}: [{value.__class__.__name__}]") | |
| else: | |
| result.append(f"{key}: {value}") | |
| return '\n'.join(result) | |