|
| 1 | +# Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, |
| 10 | +# software distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +""" |
| 16 | +Helper functions for base Modifier and Manger utilities |
| 17 | +""" |
| 18 | + |
| 19 | + |
| 20 | +import re |
| 21 | +from typing import Any, Dict, Tuple, Union |
| 22 | + |
| 23 | +import yaml |
| 24 | + |
| 25 | +from sparseml.utils import UnknownVariableException, restricted_eval |
| 26 | + |
| 27 | + |
| 28 | +__all__ = [ |
| 29 | + "load_recipe_yaml_str_no_classes", |
| 30 | + "rewrite_recipe_yaml_string_with_classes", |
| 31 | + "evaluate_recipe_yaml_str_equations", |
| 32 | +] |
| 33 | + |
| 34 | + |
| 35 | +def load_recipe_yaml_str_no_classes(recipe_yaml_str: str) -> str: |
| 36 | + """ |
| 37 | + :param recipe_yaml_str: YAML string of a SparseML recipe |
| 38 | + :return: recipe loaded into YAML with all objects replaced |
| 39 | + as a dictionary of their parameters |
| 40 | + """ |
| 41 | + pattern = re.compile(r"!(?P<class_name>(?!.*\.)[a-zA-Z_][a-zA-Z^._0-9]+)") |
| 42 | + classless_yaml_str = pattern.sub(r"OBJECT.\g<class_name>:", recipe_yaml_str) |
| 43 | + return yaml.safe_load(classless_yaml_str) |
| 44 | + |
| 45 | + |
| 46 | +def rewrite_recipe_yaml_string_with_classes(recipe_contianer: Any) -> str: |
| 47 | + """ |
| 48 | + :param recipe_contianer: recipe loaded as yaml with load_recipe_yaml_str_no_classes |
| 49 | + :return: recipe serialized into YAML with original class values re-added |
| 50 | + """ |
| 51 | + updated_yaml_str = yaml.dump(recipe_contianer) |
| 52 | + |
| 53 | + # convert object dicts back to object declarations and return |
| 54 | + pattern = re.compile(r"OBJECT\.(?P<class_name>(?!.*\.)[a-zA-Z_][a-zA-Z^._0-9]+):") |
| 55 | + return pattern.sub(r"!\g<class_name>", updated_yaml_str) |
| 56 | + |
| 57 | + |
| 58 | +def evaluate_recipe_yaml_str_equations(recipe_yaml_str: str) -> str: |
| 59 | + """ |
| 60 | + :param recipe_yaml_str: YAML string of a SparseML recipe |
| 61 | + :return: the YAML string with any expressions based on valid |
| 62 | + metadata and recipe variables and operations |
| 63 | + """ |
| 64 | + container = load_recipe_yaml_str_no_classes(recipe_yaml_str) |
| 65 | + if not isinstance(container, dict): |
| 66 | + # yaml string does not create a dict, return original string |
| 67 | + return recipe_yaml_str |
| 68 | + |
| 69 | + # validate and load remaining variables |
| 70 | + container, variables = _evaluate_recipe_variables(container) |
| 71 | + |
| 72 | + # update values nested in modifier lists based on the variables |
| 73 | + for key, val in container.items(): |
| 74 | + if "modifiers" not in key: |
| 75 | + continue |
| 76 | + container[key] = _maybe_evaluate_yaml_object(val, variables) |
| 77 | + |
| 78 | + return rewrite_recipe_yaml_string_with_classes(container) |
| 79 | + |
| 80 | + |
| 81 | +def is_eval_string(val: str) -> bool: |
| 82 | + return val.startswith("eval(") and val.endswith(")") |
| 83 | + |
| 84 | + |
| 85 | +def _maybe_evaluate_recipe_equation( |
| 86 | + val: str, |
| 87 | + variables: Dict[str, Union[int, float]], |
| 88 | +) -> Union[str, float, int]: |
| 89 | + if is_eval_string(val): |
| 90 | + is_eval_str = True |
| 91 | + val = val[5:-1] |
| 92 | + else: |
| 93 | + return val |
| 94 | + |
| 95 | + evaluated_val = restricted_eval(val, variables) |
| 96 | + |
| 97 | + if is_eval_str and not isinstance(evaluated_val, (int, float)): |
| 98 | + raise RuntimeError( |
| 99 | + "eval expressions in recipes must evaluate to a float or int" |
| 100 | + ) |
| 101 | + |
| 102 | + return evaluated_val |
| 103 | + |
| 104 | + |
| 105 | +def _evaluate_recipe_variables( |
| 106 | + recipe_dict: Dict[str, Any], |
| 107 | +) -> Tuple[Dict[str, Any], Dict[str, Union[int, float]]]: |
| 108 | + valid_variables = {} |
| 109 | + prev_num_variables = -1 |
| 110 | + |
| 111 | + while prev_num_variables != len(valid_variables): |
| 112 | + prev_num_variables = len(valid_variables) |
| 113 | + |
| 114 | + for name, val in recipe_dict.items(): |
| 115 | + if name in valid_variables: |
| 116 | + continue |
| 117 | + |
| 118 | + if isinstance(val, (int, float)): |
| 119 | + valid_variables[name] = val |
| 120 | + |
| 121 | + if not isinstance(val, str): |
| 122 | + # only parse string values |
| 123 | + continue |
| 124 | + |
| 125 | + try: |
| 126 | + val = _maybe_evaluate_recipe_equation(val, valid_variables) |
| 127 | + except UnknownVariableException: |
| 128 | + # dependant variables maybe not evaluated yet |
| 129 | + continue |
| 130 | + |
| 131 | + if isinstance(val, (int, float)): |
| 132 | + # update variable value and add to valid vars |
| 133 | + recipe_dict[name] = val |
| 134 | + valid_variables[name] = val |
| 135 | + |
| 136 | + # check that all eval statements have been evaluated |
| 137 | + for name, val in recipe_dict.items(): |
| 138 | + if isinstance(val, str) and is_eval_string(val): |
| 139 | + raise RuntimeError( |
| 140 | + f"Unable to evaluate expression: {val}. Check if any dependent " |
| 141 | + "variables form a cycle or are not defined" |
| 142 | + ) |
| 143 | + |
| 144 | + return recipe_dict, valid_variables |
| 145 | + |
| 146 | + |
| 147 | +def _maybe_evaluate_yaml_object( |
| 148 | + obj: Any, variables: Dict[str, Union[int, float]] |
| 149 | +) -> Any: |
| 150 | + |
| 151 | + if isinstance(obj, str): |
| 152 | + return _maybe_evaluate_recipe_equation(obj, variables) |
| 153 | + elif isinstance(obj, list): |
| 154 | + return [_maybe_evaluate_yaml_object(val, variables) for val in obj] |
| 155 | + elif isinstance(obj, dict): |
| 156 | + return { |
| 157 | + key: _maybe_evaluate_yaml_object(val, variables) for key, val in obj.items() |
| 158 | + } |
| 159 | + else: |
| 160 | + return obj |
| 161 | + |
| 162 | + |
| 163 | +def _maybe_parse_number(val: str) -> Union[str, float, int]: |
| 164 | + try: |
| 165 | + return int(val) |
| 166 | + except Exception: |
| 167 | + try: |
| 168 | + return float(val) |
| 169 | + except Exception: |
| 170 | + return val |
0 commit comments