# ******************************************************************************# Copyright 2023 Brainchip Holdings Ltd.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.# ******************************************************************************"""Functions to set/get Akida target version"""__all__=["AkidaVersion","get_akida_version","set_akida_version"]importosfromenumimportEnumfromcontextlibimportcontextmanager
[docs]defget_akida_version():"""Get the target akida version for model conversion. Returns: AkidaVersion: the target akida version, by default ``AkidaVersion.v2`` """ak_str_version=os.environ.get(TARGET_AKIDA_VERSION,"v2")ak_version=getattr(AkidaVersion,ak_str_version,None)ifak_versionisNone:raiseValueError(f"{TARGET_AKIDA_VERSION}={ak_str_version} must be one of {AkidaVersion._member_names_}")returnak_version
[docs]@contextmanagerdefset_akida_version(version):"""Select the target akida version for model conversion. Args: version (AkidaVersion): the target Akida version. """assertisinstance(version,AkidaVersion),"Version must be an AkidaVersion"_prev_state=os.environ.get(TARGET_AKIDA_VERSION,None)try:os.environ[TARGET_AKIDA_VERSION]=version.valueyieldfinally:# Recover default valueif_prev_stateisnotNone:os.environ[TARGET_AKIDA_VERSION]=_prev_stateelse:os.environ.pop(TARGET_AKIDA_VERSION)