Montey/php-edge
0
1# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.2# SPDX-License-Identifier: MIT3#4# Permission is hereby granted, free of charge, to any person obtaining a5# copy of this software and associated documentation files (the "Software"),6# to deal in the Software without restriction, including without limitation7# the rights to use, copy, modify, merge, publish, distribute, sublicense,8# and/or sell copies of the Software, and to permit persons to whom the9# Software is furnished to do so, subject to the following conditions:10#11# The above copyright notice and this permission notice shall be included in12# all copies or substantial portions of the Software.13#14# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER20# DEALINGS IN THE SOFTWARE.21 22from torch import nn23 24 25class CachedModule(nn.Module):26 def __init__(self, block, select_cache_step_func) -> None:27 super().__init__()28 self.block = block29 self.select_cache_step_func = select_cache_step_func30 self.cur_step = 031 self.cached_results = None32 self.enabled = True33 34 def __getattr__(self, name):35 try:36 return super().__getattr__(name)37 except AttributeError:38 return getattr(self.block, name)39 40 def if_cache(self):41 return self.select_cache_step_func(self.cur_step) and self.enabled42 43 def enable_cache(self):44 self.enabled = True45 46 def disable_cache(self):47 self.enabled = False48 self.cur_step = 049 50 def forward(self, *args, **kwargs):51 if not self.if_cache():52 self.cached_results = self.block(*args, **kwargs)53 if self.enabled:54 self.cur_step += 155 return self.cached_results56 