afe.common_utils ================ .. py:module:: afe.common_utils .. autoapi-nested-parse:: This file contains functions that can be used in all AFE source code Classes ------- .. autoapisummary:: afe.common_utils.EnumHelper afe.common_utils.Singleton afe.common_utils.ARMRuntime Functions --------- .. autoapisummary:: afe.common_utils.get_index_from_node_name afe.common_utils.unroll_tuple_range afe.common_utils.parse_indices afe.common_utils.generate_node_name_patterns afe.common_utils.search_matched_node_names afe.common_utils.get_afe_git_commit_id afe.common_utils.get_filetype_from_directory afe.common_utils.is_installed afe.common_utils.get_arm_runtime Module Contents --------------- .. py:class:: EnumHelper Overload Enum's _missing_ method to print out more informative error message. .. py:method:: values() -> List[str] :classmethod: List out the supported values. .. py:class:: Singleton .. py:function:: get_index_from_node_name(name: str, splitter='_') -> Optional[int] Use the postfix after the last splitter of the given name. If the postfix is an integer, return it as the index. Parameters ---------- :param name: str. Name of the node. :param splitter: str. Used to locate the postfix. Return ------ :return: Optional[int]. Return None if there is no index in the given node name else return the index in int. .. py:function:: unroll_tuple_range(tuple_range: Tuple[int, int]) -> List[int] Unroll a tuple of two integers to a range of integers where the lower bound is the first integer of the tuple and the upper bound is the second integer of the tuple. Parameters ---------- :param tuple_range: Tuple[int, int]. Tuple of two integers used to generate a list of continuous integers. Return ------ :return: List[int]. List of continuous integers .. py:function:: parse_indices(indices_list: List[Union[int, Tuple[int, int]]]) -> List[int] Used to parse a list of indices. Each element in the list can be: 1. A single integer 2. A tuple contains 2 integers where the left one represent the lower bound and the right one represents the upper bound. Both lower bound and upper bound are inclusive. Example ------- .. code-block:: python indices_list = [2, (11, 14), 7] decoded_indices_list = parse_indices(indices_list) # decoded_indices_list = [2, 7, 11, 12, 13, 14] Parameters ---------- :param indices_list: List[int, Tuple[int, int]]. List of indices contains element in either an integer or a Tuple of two integers. Return ------ :return: List[int]. List of indices. Each element is a integer .. py:function:: generate_node_name_patterns(patterns: List[Union[str, int, Tuple[int, int]]]) -> List[str] Given patterns in List[Union[str, int, Tuple[int, int]]], generate node name patterns using the different types of pattern as below: 1. str: Append to the output list. Support wildcard. 2. int: Append the *_{number} to the output list. 3. Tuple[int, int]: Unroll the tuple of two integers to a range of integers where the lower bound is the first integer of the tuple and the upper bound is the second integer of the tuple. Each integer will be converted to *_{number} string and append to the output list. The output list contains a list of str. Please check the example below Example ------- .. code-block:: python patterns = [2, (11, 14), "*_conv*", "3", "10"] str_patterns = generate_str_patterns(patterns) # str_patterns = ["*_2", "*_11", "*_12", "*_13", "*_14", "*_conv*", "*_3", "*_10"] Parameters ---------- :param patterns: List[str, int, Tuple[int, int]] Return ----- :return: List[str]. List of str patterns. .. py:function:: search_matched_node_names(node_names: List[str], patterns: List[Union[str, int, Tuple[int, int]]], excluded_patterns: Optional[List[Union[str, int, Tuple[int, int]]]] = None) -> Set[str] Given a list of node names and targeted patterns in List[Union[str, int, Tuple[int, int]]] format. Generate a node name set using the different types of pattern as below: 1. str: Support wildcard. 2. int: Find node with name contains *_{number}. 3. Tuple[int, int]: Unroll the tuple of two integers to a range of integers where the lower bound is the first integer of the tuple and the upper bound is the second integer of the tuple. Each integer will be converted to *_{number} string that will be used as 2 above Return a set of matched node names Example ------- The example will search nodes with indices equal to [2, 3, 10, 11, 12, 13, 14] and all the node contains "conv" in the node name. Because the excluded_patterns is assigned so the nodes with "conv2d_transpose" in the node name will be excluded. .. code-block:: python patterns = [2, (11, 14), "*conv*", "3", "10"] excluded_patterns = ["*conv2d_transpose*"] matched_node_name_set = search_matched_node_names(net, patterns, excluded_patterns) Parameters ---------- :param node_names: List[str]. List of node names and will be applied by the pattern matching using the patterns input argument. :param patterns: List[Union[str, int, Tuple[int, int]]]. List of patterns. :param excluded_patterns: Optional[List[Union[str, int, Tuple[int, int]]]]. Default is None. List of patterns that will be excluded from the pattern matching. Node names that contains these patterns will be excluded from the matched patterns. Return ------ :return: Set[str]. Set of node names that contain the given patterns but not the excluded patterns .. py:function:: get_afe_git_commit_id() -> Optional[str] Get the last AFE git commit ID if running out of a git repository. Return None if the ID can't be found. If running an installed package, no attempt is made to get the commit ID. :return: commit ID as string, if it can't get ID returns None. .. py:function:: get_filetype_from_directory(dir_path: str, filetype: str) -> str Looks through the files inside a directory and returns the file path with the matching filetype. Raises an error if multiple files match the filetype EG: in './example_dir' find '.json' file path .. py:function:: is_installed() -> bool Return true if this file appears to be running from an installed package (not a source directory). .. py:class:: ARMRuntime Host and port of TVM RPC server where ARM code will be run in tests .. py:attribute:: hostname :type: str .. py:attribute:: port :type: int .. py:function:: get_arm_runtime() -> Optional[ARMRuntime] Return the ARM target information if ARM target support is found on the system, or None otherwise.