zodiac.streams.class_stream

  1#  # # <!-- // /*  SPDX-License-Identifier: MPL-2.0*/ -->
  2#  # # <!-- // /*  d a r k s h a p e s */ -->
  3
  4from typing import List, Tuple, Callable, Union, Any, Generator
  5from zodiac.providers.registry_entry import RegistryEntry
  6from zodiac.providers.constants import MIR_DB, VERSIONS_CONFIG, ChipType
  7
  8
  9async def ancestor_data(mir_tag_or_registry_entry: RegistryEntry | list, field_name: str = "pkg") -> Generator:
 10    """Trace lineage of a model for the specified field \n
 11    :param registry_entry: RegistryEntry for the model that needs to be traced
 12    :param field_name: The name of the database field containing the data sought
 13    :return: A generator populated with matching data fields"""
 14    mir_db = MIR_DB.database
 15    if isinstance(mir_tag_or_registry_entry, RegistryEntry):
 16        mir_prefix = mir_tag_or_registry_entry.mir[0]
 17    else:
 18        mir_prefix = mir_tag_or_registry_entry[0]
 19    base_fields = [
 20        "diffusers",
 21        "*",
 22    ]  # "prior"
 23    return [mir_db[mir_prefix][x].get(field_name) for x in base_fields if mir_db[mir_prefix].get(x, {}).get(field_name, {})]
 24
 25
 26async def best_package(pkg_data: RegistryEntry | dict[int | str, Any], ready_list: list[tuple[ChipType]] = ChipType._show_ready()) -> tuple[str]:
 27    """Identify the best package based on model data and package sets.\n
 28    :param mir_db_pkg: Dictionary containing package data to match
 29    :param ready_pkg_types: List of priority package processors to evaluate
 30    :return: Tuple containing (class name, package type) if match found, otherwise None"""
 31
 32    if isinstance(pkg_data, RegistryEntry):
 33        pkg_loop: list = await ancestor_data(pkg_data)
 34        # print(pkg_loop)
 35        pkg_loop.insert(0, pkg_data.modules | pkg_loop[0])
 36    else:
 37        pkg_loop = [pkg_data]  # await ancestor_data(pkg_data)  # normalize to list
 38    for processor in ready_list:
 39        for pkg_type in processor[2]:
 40            if pkg_type.value[0]:  # Determine if the package is available
 41                package_name = pkg_type.value[1].lower()
 42                for index, data in next(iter(pkg_loop)).items():
 43                    if package_name in data:
 44                        return (index, data, pkg_type)
 45
 46
 47async def find_package(entry: RegistryEntry = None, mir_entry: list[str] | None = None) -> Tuple[str]:
 48    """Look up class and package in MIR from RegistryEntry.\n
 49    :param entry: A RegistryEntry object containing MIR (Model Identifier Resource) details.
 50    :return: A tuple containing the class name of the package and its type if found; otherwise, None.
 51    :raises: AttributeError: If PkgType or ChipType classes are not properly defined."""
 52    import re
 53
 54    mir_base = entry.mir[0] if not mir_entry else mir_entry[0]
 55    mir_comp = entry.mir[1] if not mir_entry else mir_entry[1]
 56    mir_ids = [mir_comp, "diffusers", "*"]
 57    suffixes = VERSIONS_CONFIG.get("suffixes")
 58    if suffixes:
 59        for compatibility, model_data in MIR_DB.database[mir_base].items():
 60            package_key = model_data.get("pkg")
 61            if package_key and (any(re.match(pattern, compatibility) for pattern in suffixes) or compatibility in mir_ids):  # Fallback to CPU packages if no match found in GPU packages
 62                package_data = await best_package(package_key)
 63                if package_data:
 64                    return package_data
 65    return
 66
 67
 68async def stage_class(class_object: Callable) -> List[Tuple[Union[str, Callable]]]:
 69    """Returns a tuple of data for each sub-class of a module
 70    :param class_obj: The class item to inspect.
 71    ex:('diffusers', 'models.autoencoders.autoencoder_kl', 'AutoencoderKL', <class 'diffusers.models.autoencoders.autoencoder_kl.AutoencoderKL'>),"""
 72    import typing
 73
 74    pipe_args = typing.get_type_hints(class_object.__init__).values()
 75    sub_classes = [
 76        (*pipe.__module__.split(".", 1), pipe.__name__, pipe)  #
 77        for pipe in pipe_args  #
 78        if "builtins" not in pipe.__module__ and "typing" not in pipe.__module__
 79    ]
 80    return sub_classes
 81
 82
 83# async def show_transformers_tasks(class_name: str) -> List[str]:
 84#     """Retrieves a list of task classes associated with a specified transformer class.\n
 85#     :param class_name: The name of the transformer class to inspect.
 86#     :param pkg_type: The dependency for the module
 87#     :return: A list of task classes associated with the specified transformer.
 88#     """
 89#     class_obj: Callable = make_callable(class_name, PkgType.TRANSFORMERS.value[1].lower())
 90#     class_module: Callable = make_callable(*class_obj.__module__.split(".", 1)[-1:], class_obj.__module__.split(".", 1)[0])
 91#     task_classes = getattr(class_module, "__all__")
 92#     return task_classes
 93
 94
 95# from mir.mappers import make_callable
 96# from zodiac.providers.constants import MIR_DB
 97# from zodiac.class_stream import lookup_package
 98# from zodiac.class_stream import trace_class
 99
100# model_data = lookup_package(entry)
101# package_data = [content for content in model_data.get("pkg").values() if next(iter(content)) in ["diffusers", "transformers"]]
102# class_data = trace_class(make_callable(package_data[0], "diffusers"))
103# # [terminal_gen(data[3],getattr(PkgType,data[0].upper())) for data in class_data]
async def ancestor_data( mir_tag_or_registry_entry: zodiac.providers.registry_entry.RegistryEntry | list, field_name: str = 'pkg') -> Generator:
10async def ancestor_data(mir_tag_or_registry_entry: RegistryEntry | list, field_name: str = "pkg") -> Generator:
11    """Trace lineage of a model for the specified field \n
12    :param registry_entry: RegistryEntry for the model that needs to be traced
13    :param field_name: The name of the database field containing the data sought
14    :return: A generator populated with matching data fields"""
15    mir_db = MIR_DB.database
16    if isinstance(mir_tag_or_registry_entry, RegistryEntry):
17        mir_prefix = mir_tag_or_registry_entry.mir[0]
18    else:
19        mir_prefix = mir_tag_or_registry_entry[0]
20    base_fields = [
21        "diffusers",
22        "*",
23    ]  # "prior"
24    return [mir_db[mir_prefix][x].get(field_name) for x in base_fields if mir_db[mir_prefix].get(x, {}).get(field_name, {})]

Trace lineage of a model for the specified field

Parameters
  • registry_entry: RegistryEntry for the model that needs to be traced
  • field_name: The name of the database field containing the data sought
Returns

A generator populated with matching data fields

async def best_package( pkg_data: zodiac.providers.registry_entry.RegistryEntry | dict[int | str, typing.Any], ready_list: list[tuple[zodiac.providers.constants.ChipType]] = [(True, 'MPS', [<PkgType.MFLUX: (True, 'MFLUX', [])>, <PkgType.MLX_AUDIO: (True, 'MLX_AUDIO', [])>, <PkgType.MLX_LM: (True, 'MLX_LM', [])>, <PkgType.BAGEL: (False, 'BAGEL', ['bytedance-seed/BAGEL'])>]), (True, 'CPU', [<PkgType.AUDIOGEN: (False, 'AUDIOCRAFT', ['exdysa/facebookresearch-audiocraft-revamp'])>, <PkgType.PARLER_TTS: (False, 'PARLER_TTS', ['huggingface/parler-tts'])>, <PkgType.LLAMA: (True, 'LLAMA_CPP', [])>, <PkgType.HIDIFFUSION: (False, 'HIDIFFUSION', ['megvii-research/HiDiffusion'])>, <PkgType.SENTENCE_TRANSFORMERS: (False, 'SENTENCE_TRANSFORMERS', [])>, <PkgType.DIFFUSERS: (True, 'DIFFUSERS', [])>, <PkgType.TRANSFORMERS: (True, 'TRANSFORMERS', [])>, <PkgType.TORCH: (True, 'TORCH', [])>])]) -> tuple[str]:
27async def best_package(pkg_data: RegistryEntry | dict[int | str, Any], ready_list: list[tuple[ChipType]] = ChipType._show_ready()) -> tuple[str]:
28    """Identify the best package based on model data and package sets.\n
29    :param mir_db_pkg: Dictionary containing package data to match
30    :param ready_pkg_types: List of priority package processors to evaluate
31    :return: Tuple containing (class name, package type) if match found, otherwise None"""
32
33    if isinstance(pkg_data, RegistryEntry):
34        pkg_loop: list = await ancestor_data(pkg_data)
35        # print(pkg_loop)
36        pkg_loop.insert(0, pkg_data.modules | pkg_loop[0])
37    else:
38        pkg_loop = [pkg_data]  # await ancestor_data(pkg_data)  # normalize to list
39    for processor in ready_list:
40        for pkg_type in processor[2]:
41            if pkg_type.value[0]:  # Determine if the package is available
42                package_name = pkg_type.value[1].lower()
43                for index, data in next(iter(pkg_loop)).items():
44                    if package_name in data:
45                        return (index, data, pkg_type)

Identify the best package based on model data and package sets.

Parameters
  • mir_db_pkg: Dictionary containing package data to match
  • ready_pkg_types: List of priority package processors to evaluate
Returns

Tuple containing (class name, package type) if match found, otherwise None

async def find_package( entry: zodiac.providers.registry_entry.RegistryEntry = None, mir_entry: list[str] | None = None) -> Tuple[str]:
48async def find_package(entry: RegistryEntry = None, mir_entry: list[str] | None = None) -> Tuple[str]:
49    """Look up class and package in MIR from RegistryEntry.\n
50    :param entry: A RegistryEntry object containing MIR (Model Identifier Resource) details.
51    :return: A tuple containing the class name of the package and its type if found; otherwise, None.
52    :raises: AttributeError: If PkgType or ChipType classes are not properly defined."""
53    import re
54
55    mir_base = entry.mir[0] if not mir_entry else mir_entry[0]
56    mir_comp = entry.mir[1] if not mir_entry else mir_entry[1]
57    mir_ids = [mir_comp, "diffusers", "*"]
58    suffixes = VERSIONS_CONFIG.get("suffixes")
59    if suffixes:
60        for compatibility, model_data in MIR_DB.database[mir_base].items():
61            package_key = model_data.get("pkg")
62            if package_key and (any(re.match(pattern, compatibility) for pattern in suffixes) or compatibility in mir_ids):  # Fallback to CPU packages if no match found in GPU packages
63                package_data = await best_package(package_key)
64                if package_data:
65                    return package_data
66    return

Look up class and package in MIR from RegistryEntry.

Parameters
  • entry: A RegistryEntry object containing MIR (Model Identifier Resource) details.
Returns

A tuple containing the class name of the package and its type if found; otherwise, None.

Raises
  • AttributeError: If PkgType or ChipType classes are not properly defined.
async def stage_class(class_object: Callable) -> List[Tuple[Union[str, Callable]]]:
69async def stage_class(class_object: Callable) -> List[Tuple[Union[str, Callable]]]:
70    """Returns a tuple of data for each sub-class of a module
71    :param class_obj: The class item to inspect.
72    ex:('diffusers', 'models.autoencoders.autoencoder_kl', 'AutoencoderKL', <class 'diffusers.models.autoencoders.autoencoder_kl.AutoencoderKL'>),"""
73    import typing
74
75    pipe_args = typing.get_type_hints(class_object.__init__).values()
76    sub_classes = [
77        (*pipe.__module__.split(".", 1), pipe.__name__, pipe)  #
78        for pipe in pipe_args  #
79        if "builtins" not in pipe.__module__ and "typing" not in pipe.__module__
80    ]
81    return sub_classes

Returns a tuple of data for each sub-class of a module

Parameters
  • class_obj: The class item to inspect. ex:('diffusers', 'models.autoencoders.autoencoder_kl', 'AutoencoderKL', ),