Source code for aimet_torch.model_validator.model_validator
# -*- mode: python -*-# =============================================================================# @@-COPYRIGHT-START-@@## Copyright (c) 2021, Qualcomm Innovation Center, Inc. All rights reserved.## Redistribution and use in source and binary forms, with or without# modification, are permitted provided that the following conditions are met:## 1. Redistributions of source code must retain the above copyright notice,# this list of conditions and the following disclaimer.## 2. Redistributions in binary form must reproduce the above copyright notice,# this list of conditions and the following disclaimer in the documentation# and/or other materials provided with the distribution.## 3. Neither the name of the copyright holder nor the names of its contributors# may be used to endorse or promote products derived from this software# without specific prior written permission.## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE# POSSIBILITY OF SUCH DAMAGE.## SPDX-License-Identifier: BSD-3-Clause## @@-COPYRIGHT-END-@@# =============================================================================""" Utility for validating pytorch models prior to using AIMET features """fromtypingimportTuple,Union,Callableimporttorchfromaimet_common.utilsimportAimetLoggerimportaimet_torch.model_validator.validation_checksasval_checkslogger=AimetLogger.get_area_logger(AimetLogger.LogAreas.Utils)
[docs]classModelValidator:""" ModelValidator object for validating that AIMET features can be applied on the Pytorch model. """_validation_checks=[val_checks.validate_for_reused_modules,val_checks.validate_for_missing_modules]
[docs]@staticmethoddefadd_check(validation_check:Callable):""" Add a validation check function to be used for validating the model. Validation check functions must take the model, model inputs, and kwargs as inputs. The validation check must output True if the model passes the check, and False otherwise. :param validation_check: Validation check function for validating the model. """ModelValidator._validation_checks.append(validation_check)
[docs]@staticmethoddefvalidate_model(model:torch.nn.Module,model_input:Union[torch.Tensor,Tuple],**kwargs)->bool:""" Validate the pytorch model by running all validation check functions and returning True if all pass, False otherwise. Keyword arguments can be used to pass specific arguments to particular validation checkers. Currently supported keyword arguments: layers_to_exclude: List of torch.nn.Modules to be excluded in the check for missing modules. These layers and all of their sublayers will not be flagged if they do not have a corresponding Pytorch module. :param model: Pytorch model to validate :param model_input: Dummy input to the model :return True if pytorch model is valid, False otherwise """is_valid_model=Truefailed_val_checks=set()forval_checkinModelValidator._validation_checks:logger.info('Running validator check %s',val_check)val_check_result=val_check(model,model_input,**kwargs)ifnotval_check_result:failed_val_checks.add(val_check)is_valid_model=is_valid_modelandval_check_resultifnotis_valid_model:logger.info('The following validator checks failed:')forval_checkinfailed_val_checks:logger.info('\t%s',val_check)returnis_valid_modellogger.info('All validation checks passed.')returnis_valid_model