zodiac.providers.constants
1# SPDX-License-Identifier: MPL-2.0 AND LicenseRef-Commons-Clause-License-Condition-1.0 2# <!-- // /* d a r k s h a p e s */ --> 3 4 5# pylint:disable=no-name-in-module 6 7import os 8from enum import Enum 9from typing import Annotated, Callable, List, Optional, Union 10 11from nnll.configure.init_gpu import first_available 12from nnll.mir.json_cache import TEMPLATE_PATH_NAMED, VERSIONS_PATH_NAMED, JSONCache 13from nnll.mir.maid import MIRDatabase 14from nnll.monitor.file import dbuq 15from pydantic import BaseModel, Field 16from transformers.pipelines import PIPELINE_REGISTRY 17 18MIR_DB = MIRDatabase() 19CUETYPE_PATH_NAMED = os.path.join(os.path.dirname(__file__), "cuetype.json") 20CUETYPE_CONFIG = JSONCache(CUETYPE_PATH_NAMED) 21TEMPLATE_CONFIG = JSONCache(TEMPLATE_PATH_NAMED) 22VERSIONS_DATA = JSONCache(VERSIONS_PATH_NAMED) 23VERSIONS_DATA._load_cache() 24VERSIONS_CONFIG = VERSIONS_DATA._cache 25 26 27def check_host(api_name: str, api_url: str) -> bool: 28 """Perform network test to ensure a host server is running\n 29 :param api_name: Type of host API 30 :param api_url: The (default) configuration data for that API 31 :return: Whether the server is up or not 32 """ 33 34 from json.decoder import JSONDecodeError 35 36 import httpcore 37 import httpx 38 import requests 39 from openai import APIConnectionError, APIStatusError, APITimeoutError # , JSONDecodeError, 40 from urllib3.exceptions import MaxRetryError, NewConnectionError 41 42 if api_name == "LM_STUDIO": 43 from lmstudio import APIConnectionError, APIStatusError, APITimeoutError, JSONDecodeError 44 try: 45 dbuq(api_url) 46 request = requests.get(api_url, timeout=(1, 1)) 47 if request is not None: 48 dbuq(vars(request)) 49 if hasattr(request, "status_code"): 50 status = request.status_code 51 dbuq(status) 52 if (hasattr(request, "ok") and request.ok) or (hasattr(request, "reason") and request.reason == "OK"): 53 dbuq(f"Available {api_name}") 54 return True 55 elif hasattr(request, "json"): 56 status = request.json() 57 if status.get("result") == "OK": 58 dbuq(f"Available {api_name}") 59 return True 60 request.raise_for_status() 61 requests.HTTPError() 62 except ( 63 APIConnectionError, 64 APITimeoutError, 65 APIStatusError, 66 requests.exceptions.InvalidURL, 67 requests.exceptions.ConnectionError, 68 requests.adapters.ConnectionError, 69 requests.HTTPError, 70 httpcore.ConnectError, 71 httpx.ConnectError, 72 ConnectionRefusedError, 73 MaxRetryError, 74 NewConnectionError, 75 TimeoutError, 76 JSONDecodeError, 77 OSError, 78 RuntimeError, 79 ConnectionError, 80 ) as error_log: 81 dbuq(error_log) 82 return False 83 84 85@CUETYPE_CONFIG.decorator 86def has_api(api_name: str, data: dict = None) -> bool: 87 """Check available modules, try to import dynamically.\n 88 True for successful import, else False\n 89 90 :param api_name: Constant name for API 91 :param _data: filled by config decorator, ignore, defaults to None 92 :return: Package availability or `check_host` for servers; boolean result 93 :rtype: bool 94 """ 95 from importlib import import_module 96 from json.decoder import JSONDecodeError 97 98 hosted_apis = ["OLLAMA", "LM_STUDIO", "LLAMAFILE", "VLLM"] # , "CORTEX" ] #became jan 99 try: 100 api_data = data.get(api_name, {"module": api_name.lower()}) # pylint: disable=unsubscriptable-object 101 except JSONDecodeError as error_log: 102 dbuq(error_log) 103 return False 104 try: 105 module = import_module(api_data.get("module")) 106 if module: 107 if api_name not in hosted_apis: 108 return True 109 else: 110 dbuq(api_data.get("api_url")) 111 url = api_data.get("api_url") 112 if url: 113 return check_host(api_name, url) 114 except (UnboundLocalError, ImportError, ModuleNotFoundError, JSONDecodeError) as error_log: 115 dbuq(error_log) 116 dbuq("|Ignorable| Source unavailable:", f"{api_name}") 117 return False 118 119 120show_all_docstring = ":param _show_all(): Show all POSSIBLE API types of a given class" 121show_available_docstring = ":param _show_available(): Show all AVAILABLE API types of a given class" 122check_type_docstring = ":param _check_type: Check for a SINGLE API availability" 123 124base_enum_docstring = f"""{show_all_docstring}{show_available_docstring}{check_type_docstring}""" 125 126 127class BaseEnum(Enum): 128 f"""{base_enum_docstring}""" 129 130 @classmethod 131 def show_all(cls) -> List: 132 """Show all POSSIBLE API types of a given class""" 133 return [x for x, y in cls.__members__.items()] 134 135 @classmethod 136 def show_available(cls) -> bool: 137 """Show all AVAILABLE API types of a given class""" 138 return [library.value[1] for library in list(cls) if library.value[0] is True] 139 140 @classmethod 141 def check_type(cls, type_name: str) -> bool: 142 """Check for a SINGLE API availability""" 143 type_name = type_name.upper() 144 return has_api(type_name) 145 146 147class CueType(BaseEnum): 148 f"""Model Provider constants\n 149 Caches and servers\n 150 <NAME: (Availability, IMPORT_NAME)>{base_enum_docstring} 151 {base_enum_docstring}""" 152 153 # Dfferentiation of boolean conditions 154 # GIVEN : The state of all provider modules & servers are marked at launch 155 156 HUB: tuple = (has_api("HUB"), "HUB") 157 KAGGLE: tuple = (has_api("KAGGLE"), "KAGGLE") 158 LLAMAFILE: tuple = (has_api("LLAMAFILE"), "LLAMAFILE") 159 LM_STUDIO: tuple = (has_api("LM_STUDIO"), "LM_STUDIO") 160 MLX_AUDIO: tuple = (has_api("MLX_AUDIO"), "MLX_AUDIO") 161 OLLAMA: tuple = (has_api("OLLAMA"), "OLLAMA") 162 VLLM: tuple = (has_api("VLLM"), "VLLM") 163 164 165example_str = ("function_name", "import.function_name") 166 167 168class PkgType(BaseEnum): 169 """Package dependency constants 170 Collected info from hub model tags and dependencies 171 <NAME: (Availability, IMPORT_NAME, [Github repositories*] 172 *if applicable, otherwise IMPORT_NAME is pip package 173 NOTE: NAME is colloquial and does not always match IMPORT_NAME>""" 174 175 AUDIOGEN: tuple = (has_api("AUDIOCRAFT"), "AUDIOCRAFT", ["exdysa/facebookresearch-audiocraft-revamp"]) # this fork supports mps 176 BAGEL: tuple = (has_api("BAGEL"), "BAGEL", ["bytedance-seed/BAGEL"]) 177 BITNET: tuple = (has_api("BITNET"), "BITNET", ["microsoft/BitNet"]) 178 BITSANDBYTES: tuple = (has_api("BITSANDBYTES"), "BITSANDBYTES", []) # bitsandbytes-foundation/bitsandbytes 179 DFLOAT11: tuple = (has_api("DFLOAT11"), "DFLOAT11", ["LeanModels/DFloat11"]) 180 DIFFUSERS: tuple = (has_api("DIFFUSERS"), "DIFFUSERS", []) 181 EXLLAMAV2: tuple = (has_api("EXLLAMAV2"), "EXLLAMAV2", []) # turboderp-org/exllamav2 182 F_LITE: tuple = (has_api("F_LITE"), "F_LITE", ["fal-ai/f-lite"]) 183 HIDIFFUSION: tuple = (has_api("HIDIFFUSION"), "HIDIFFUSION", ["megvii-research/HiDiffusion"]) 184 IMAGE_GEN_AUX: tuple = (has_api("IMAGE_GEN_AUX"), "IMAGE_GEN_AUX", ["huggingface/image_gen_aux"]) 185 JAX: tuple = (has_api("JAX"), "JAX", []) 186 KERAS: tuple = (has_api("KERAS"), "KERAS", []) 187 LLAMA: tuple = (has_api("LLAMA_CPP"), "LLAMA_CPP", []) 188 LUMINA_MGPT: tuple = (has_api("INFERENCE_SOLVER"), "INFERENCE_SOLVER", ["Alpha-VLLM/Lumina-mGPT"]) 189 LUMINA_MGPT2: tuple = (has_api("INFERENCE_SOLVER"), "INFERENCE_SOLVER", ["Alpha-VLLM/Lumina-mGPT-2.0"]) 190 MFLUX: tuple = (has_api("MFLUX"), "MFLUX", []) # "filipstrand/mflux" 191 MLX_AUDIO: tuple = (CueType.check_type("MLX_AUDIO"), "MLX_AUDIO", []) # Blaizzy/mlx-audio 192 MLX_CHROMA: tuple = (has_api("CHROMA"), "CHROMA", ["exdysa/jack813-mlx-chroma"]) 193 MLX_LM: tuple = (has_api("MLX_LM"), "MLX_LM", []) # "ml-explore/mlx-lm" 194 MLX_VLM: tuple = (has_api("MLX_VLM"), "MLX_VLM", []) # Blaizzy/mlx-vlm 195 MLX: tuple = (has_api("MLX_LM"), "MLX", []) 196 ONNX: tuple = (has_api("ONNX"), "ONNX", ["ONNX"]) 197 ORPHEUS_TTS: tuple = (has_api("ORPHEUS_TTS"), "ORPHEUS_TTS", ["canopyai/Orpheus-TTS"]) 198 OUTETTS: tuple = (has_api("OUTETTS"), "OUTETTS", ["edwko/OuteTTS"]) 199 PARLER_TTS: tuple = (has_api("PARLER_TTS"), "PARLER_TTS", ["huggingface/parler-tts"]) 200 PLEIAS: tuple = (has_api("PLEIAS"), "PLEIAS", ["exdysa/Pleias-Pleias-RAG-Library"]) # bypasses vllm for macos to avoid requiring gcc/AVIX 201 SENTENCE_TRANSFORMERS: tuple = (has_api("SENTENCE_TRANSFORMERS"), "SENTENCE_TRANSFORMERS", []) # UKPLab/sentence-transformers 202 SHOW_O: tuple = (has_api("SHOW_O"), "SHOW_O", ["showlab/show-o"]) 203 SPANDREL_EXTRA_ARCHES: tuple = (has_api("SPANDREL_EXTRA_ARCHES"), "SPANDREL_EXTRA_ARCHES", []) 204 SPANDREL: tuple = (has_api("SPANDREL"), "SPANDREL", []) 205 SVDQUANT: tuple = (has_api("NUNCHAKU"), "NUNCHAKU", ["mit-han-lab/nunchaku"]) 206 TENSORFLOW: tuple = (has_api("TENSORFLOW"), "TENSORFLOW", []) 207 TORCH: tuple = (has_api("TORCH"), "TORCH", []) # Possible that torch is NOT needed (mlx_lm, or some other unforeseen future ) 208 TORCHAUDIO: tuple = (has_api("TORCHAUDIO"), "TORCHAUDIO", []) 209 TORCHVISION: tuple = (has_api("TORCHVISION"), "TORCHVISION", []) 210 TRANSFORMERS: tuple = (has_api("TRANSFORMERS"), "TRANSFORMERS", []) 211 VLLM: tuple = (CueType.check_type("VLLM"), "VLLM", []) 212 213 214class ChipType(Enum): 215 f"""Device constants\n 216 CUDA, MPS, XPU, MTIA [Supported PkgTypes]\n 217 {base_enum_docstring}""" 218 219 def __call__(cls): 220 cls.initialie_device() 221 222 @classmethod 223 def initialize_device(cls) -> None: 224 chip_types = [ 225 ( 226 "CUDA", 227 [ 228 PkgType.BAGEL, 229 PkgType.BITSANDBYTES, 230 PkgType.DFLOAT11, 231 PkgType.EXLLAMAV2, 232 PkgType.F_LITE, 233 PkgType.LUMINA_MGPT, 234 PkgType.ORPHEUS_TTS, 235 PkgType.OUTETTS, 236 PkgType.VLLM, 237 ], 238 ), 239 ("MPS", [PkgType.MFLUX, PkgType.MLX_AUDIO, PkgType.MLX_LM, PkgType.BAGEL]), 240 ("XPU", []), 241 ("MTIA", []), 242 ] 243 cls._device = first_available(assign=True, init=True, clean=True) # pylint:disable=no-member, protected-access 244 if hasattr(cls._device, "type"): 245 gpu = cls._device.type 246 else: 247 gpu = "" 248 for name, pkg_type in chip_types: 249 setattr(cls, name, (name.lower() in gpu, name, pkg_type)) 250 setattr( 251 cls, 252 "CPU", 253 ( 254 True, 255 "CPU", 256 [ 257 PkgType.AUDIOGEN, 258 PkgType.PARLER_TTS, 259 PkgType.LLAMA, 260 PkgType.HIDIFFUSION, 261 PkgType.SENTENCE_TRANSFORMERS, 262 PkgType.DIFFUSERS, 263 PkgType.TRANSFORMERS, 264 PkgType.TORCH, 265 ], 266 ), 267 ) 268 269 @classmethod 270 def _show_all(cls) -> List[str]: 271 """Show all POSSIBLE processor types""" 272 atypes = [atype for atype in cls.__dict__ if "_" not in atype] 273 return atypes 274 275 @classmethod 276 def _show_ready(cls, api_name: Optional[str] = None) -> Union[List[str], bool]: 277 """Show all READY devices.\n 278 If api_name is provided, checks if the specific API is ready. 279 :param api_name: Boolean check for the specific API by name, defaults to None 280 :return: `bool` or list of ready devices 281 """ 282 atypes = cls._show_all() 283 if api_name: 284 return api_name.upper() in [x for x in atypes if getattr(cls, x)[0]] 285 return [getattr(cls, x) for x in atypes if getattr(cls, x)[0] is True] 286 287 @classmethod 288 def _show_pkgs(cls) -> Union[List[PkgType], str]: 289 """Return compatible PkgTypes for all available chipsets\n 290 If no chipsets are detected, returns onlyCPU compatibility options\n 291 :return: `PkgType`s for the available processors including CPU 292 """ 293 pkg_names = getattr(cls, "CPU")[-1] 294 atypes = cls._show_ready() 295 available = [pkg[1] for pkg in atypes if pkg[0] is True] 296 priority = next(iter(available), "CPU") if available else "CPU" 297 if priority not in [pkg[1] for pkg in cls._show_all() if not pkg[2]] and priority != "CPU": 298 pkg_names = atypes[0][2] + pkg_names 299 return pkg_names 300 301 302ChipType.initialize_device() 303 304 305# class PipeType(Enum): 306# MFLUX: tuple = (ChipType._show_ready("mps"), PkgType.check_type("MFLUX"), {"mir_tag": "flux"}) # pylint:disable=protected-access 307# MFLUX: tuple = ("MPS" in ChipType._show_ready("mps"), PkgType.MFLUX, {"mir_tag": "flux"}) # pylint:disable=protected-access 308 309 310# Experimental way to abstract/declarify complex task names 311class GenTypeC(BaseModel): 312 """ 313 Generative inference types in ***C***-dimensional order\n 314 ***Comprehensiveness***, sorted from 'most involved' to 'least involved'\n 315 The terms define 'artistic' and ambiguous operations\n 316 317 :param clone: Copying identity, voice, exact mirror 318 :param sync: Tone, tempo, color, quality, genre, scale, mood 319 :param translate: A range of comprehensible approximations\n 320 """ 321 322 clone: Annotated[Callable | None, Field(default=None)] 323 sync: Annotated[Callable | None, Field(default=None)] 324 translate: Annotated[Callable | None, Field(default=None)] 325 326 327class GenTypeCText(BaseModel): 328 """ 329 Generative inference types in ***C***-dimensional order for text operations\n 330 ***Comprehensiveness***, sorted from 'most involved' to 'least involved'\n 331 The terms define 'concrete' and more rigid operations\n 332 333 :param research: Quoting, paraphrasing, and deriving from sources 334 :param chain_of_thought: A performance of processing step-by-step (similar to `reasoning`) 335 :param question_answer: Basic, straightforward responses\n 336 """ 337 338 research: Annotated[Optional[Callable | None], Field(default=None, examples=example_str)] 339 chain_of_thought: Annotated[Optional[Callable | None], Field(default=None, examples=example_str)] 340 question_answer: Annotated[Optional[Callable | None], Field(default=None, examples=example_str)] 341 342 343class GenTypeE(BaseModel): 344 """ 345 Generative inference operation types in ***E***-dimensional order \n 346 ***Equivalence***, lists sorted from 'highly-similar' to 'loosely correlated.'"\n 347 :param universal: Affecting all conversions 348 :param text: Text-only conversions\n 349 350 ***multimedia generation*** 351 ``` 352 Y-axis: Detail (Most involved to least involved) 353 │ 354 │ clone 355 │ sync 356 │ translate 357 │ 358 +───────────────────────────────────────> X-axis: Equivalence (Loosely correlated to highly similar) 359 ``` 360 ***text generation*** 361 ``` 362 Y-axis: Detail (Most involved to least involved) 363 │ 364 │ research 365 │ chain-of-thought 366 │ question/answer 367 │ 368 +───────────────────────────────────────> X-axis: Equivalence (Loosely correlated to highly similar) 369 ``` 370 371 This is essentially the translation operation of C types, and the mapping of them to E \n 372 373 An abstract generalization of the set of all multimodal generative synthesis processes\n 374 The sum of each coordinate pair reflects effective compute use\n 375 In this way, both C types and their similarity are translatable, but not 1:1 identical\n 376 Text is allowed to perform all 6 core operations. Other media perform only 3.\n 377 """ 378 379 # note: `sync` may have better terms, such as 'harmonize' or 'attune'. `sync` was chosen because it is shorter 380 381 universal: GenTypeC = GenTypeC(clone=None, sync=None, translate=None) 382 text: GenTypeCText = GenTypeCText(research=None, chain_of_thought=None, question_answer=None) 383 384 385# Here, a case could be made that tasks could be determined by filters, rather than graphing 386# This is valid but, it offers no guarantees for difficult logic conditions that can be easily verified by graph algorithms 387# Using graphs also allows us to offload the logic elsewhere 388 389VALID_CONVERSIONS = ["text", "image", "music", "speech", "audio", "video", "3d", "vector_graphic", "upscale_image"] 390VALID_JUNCTIONS = [""] 391 392# note : decide on a way to keep paired tuples and sets together inside config dict 393 394tasks = PIPELINE_REGISTRY.get_supported_tasks() + ["translation_XX_to_YY"] 395 396VALID_TASKS = { # normalized to lowercase 397 CueType.VLLM: { 398 ("text", "text"): ["text"], 399 ("image", "text"): ["vision"], 400 }, 401 CueType.OLLAMA: { 402 ("text", "text"): ["mllama"], 403 ("image", "text"): ["llava", "vllm"], 404 }, 405 CueType.LLAMAFILE: { 406 ("text", "text"): ["text"], 407 }, 408 CueType.LM_STUDIO: { 409 # ("image", "text"): [("vision", True)], 410 ("text", "text"): ["llm"], 411 }, 412 CueType.HUB: { 413 ("image", "image"): [ 414 "image-to-image", 415 "inpaint", 416 "inpainting", 417 "depth-to-image", 418 "i2i", 419 "image-to-image", 420 "any-to-any", 421 ], 422 ("text", "image"): ["kolors", "kolorspipeline", "image-generation", "any-to-any", "text-to-image", "chromapipeline"], 423 ("image", "text"): [ 424 "image-classification", 425 "image-to-text", 426 "image-text-to-text", 427 "visual-question-answering", 428 "image-captioning", 429 "image-segmentation", 430 "depth-estimation", 431 "image-feature-extraction", 432 "mask-generation", 433 "object-detection", 434 "visual-question-answering", 435 "keypoint-detection", 436 "vllm", 437 "vqa", 438 "vision", 439 "zero-shot-object-detection", 440 "zero-shot-image-classification", 441 "timm", 442 "zero-shot image classification", 443 "any-to-any", 444 "vidore", 445 ], 446 ("image", "video"): ["image-to-video", "i2v", "reference-to-video", "refernce-to-video"], 447 ("video", "text"): ["video-classification"], 448 ("text", "video"): ["video generation", "t2v", "text-to-video", "HunyuanVideoPipeline"], 449 ("text", "text"): [ 450 "any-to-any", 451 "named entity recognition", 452 "entity typing", 453 "relation classification", 454 "question answering", 455 "fill-mask", 456 "chat", 457 "conversational", 458 "text-generation", 459 "causal-lm", 460 "text2text-generation", 461 "document-question-answering", 462 "feature-extraction", 463 "question-answering", 464 "sentiment-analysis", 465 "summarization", 466 "table-question-answering", 467 "text-classification", 468 "token-classification", 469 "translation", 470 "zero-shot-classification", 471 "translation_xx_to_yy", 472 "t2t", 473 "chatglm", 474 "exbert", 475 ], 476 ("text", "audio"): ["text-to-audio", "t2a", "any-to-any", "text-to-audio", " musicldmpipeline", "musicgen", "audiocraft", "audiogen", "AudioLDMPipeline", "AudioLDM2Pipeline"], 477 ("audio", "text"): ["zero-shot-audio-classification", "audio-classification", "a2t", "audio-text-to-text", "any-to-any"], 478 ("text", "speech"): [ 479 "text-to-speech", 480 "tts", 481 "any-to-any", 482 "annotation", 483 ], 484 ("speech", "text"): [ 485 "speech-to-text", 486 "speech", 487 "speech-translation", 488 "speech-summarization", 489 "automatic-speech-recognition", 490 "dictation", 491 "stt", 492 "any-to-any", 493 "hf-asr-leaderboard", 494 ], 495 }, 496 CueType.KAGGLE: { 497 ("text", "text"): ["text"], 498 }, 499 # CueType.CORTEX: { 500 # ("text", "text"): ["text"], 501 # }, 502}
28def check_host(api_name: str, api_url: str) -> bool: 29 """Perform network test to ensure a host server is running\n 30 :param api_name: Type of host API 31 :param api_url: The (default) configuration data for that API 32 :return: Whether the server is up or not 33 """ 34 35 from json.decoder import JSONDecodeError 36 37 import httpcore 38 import httpx 39 import requests 40 from openai import APIConnectionError, APIStatusError, APITimeoutError # , JSONDecodeError, 41 from urllib3.exceptions import MaxRetryError, NewConnectionError 42 43 if api_name == "LM_STUDIO": 44 from lmstudio import APIConnectionError, APIStatusError, APITimeoutError, JSONDecodeError 45 try: 46 dbuq(api_url) 47 request = requests.get(api_url, timeout=(1, 1)) 48 if request is not None: 49 dbuq(vars(request)) 50 if hasattr(request, "status_code"): 51 status = request.status_code 52 dbuq(status) 53 if (hasattr(request, "ok") and request.ok) or (hasattr(request, "reason") and request.reason == "OK"): 54 dbuq(f"Available {api_name}") 55 return True 56 elif hasattr(request, "json"): 57 status = request.json() 58 if status.get("result") == "OK": 59 dbuq(f"Available {api_name}") 60 return True 61 request.raise_for_status() 62 requests.HTTPError() 63 except ( 64 APIConnectionError, 65 APITimeoutError, 66 APIStatusError, 67 requests.exceptions.InvalidURL, 68 requests.exceptions.ConnectionError, 69 requests.adapters.ConnectionError, 70 requests.HTTPError, 71 httpcore.ConnectError, 72 httpx.ConnectError, 73 ConnectionRefusedError, 74 MaxRetryError, 75 NewConnectionError, 76 TimeoutError, 77 JSONDecodeError, 78 OSError, 79 RuntimeError, 80 ConnectionError, 81 ) as error_log: 82 dbuq(error_log) 83 return False
Perform network test to ensure a host server is running
Parameters
- api_name: Type of host API
- api_url: The (default) configuration data for that API
Returns
Whether the server is up or not
86@CUETYPE_CONFIG.decorator 87def has_api(api_name: str, data: dict = None) -> bool: 88 """Check available modules, try to import dynamically.\n 89 True for successful import, else False\n 90 91 :param api_name: Constant name for API 92 :param _data: filled by config decorator, ignore, defaults to None 93 :return: Package availability or `check_host` for servers; boolean result 94 :rtype: bool 95 """ 96 from importlib import import_module 97 from json.decoder import JSONDecodeError 98 99 hosted_apis = ["OLLAMA", "LM_STUDIO", "LLAMAFILE", "VLLM"] # , "CORTEX" ] #became jan 100 try: 101 api_data = data.get(api_name, {"module": api_name.lower()}) # pylint: disable=unsubscriptable-object 102 except JSONDecodeError as error_log: 103 dbuq(error_log) 104 return False 105 try: 106 module = import_module(api_data.get("module")) 107 if module: 108 if api_name not in hosted_apis: 109 return True 110 else: 111 dbuq(api_data.get("api_url")) 112 url = api_data.get("api_url") 113 if url: 114 return check_host(api_name, url) 115 except (UnboundLocalError, ImportError, ModuleNotFoundError, JSONDecodeError) as error_log: 116 dbuq(error_log) 117 dbuq("|Ignorable| Source unavailable:", f"{api_name}") 118 return False
Check available modules, try to import dynamically.
True for successful import, else False
Parameters
- api_name: Constant name for API
- _data: filled by config decorator, ignore, defaults to None
Returns
Package availability or
check_hostfor servers; boolean result
128class BaseEnum(Enum): 129 f"""{base_enum_docstring}""" 130 131 @classmethod 132 def show_all(cls) -> List: 133 """Show all POSSIBLE API types of a given class""" 134 return [x for x, y in cls.__members__.items()] 135 136 @classmethod 137 def show_available(cls) -> bool: 138 """Show all AVAILABLE API types of a given class""" 139 return [library.value[1] for library in list(cls) if library.value[0] is True] 140 141 @classmethod 142 def check_type(cls, type_name: str) -> bool: 143 """Check for a SINGLE API availability""" 144 type_name = type_name.upper() 145 return has_api(type_name)
131 @classmethod 132 def show_all(cls) -> List: 133 """Show all POSSIBLE API types of a given class""" 134 return [x for x, y in cls.__members__.items()]
Show all POSSIBLE API types of a given class
148class CueType(BaseEnum): 149 f"""Model Provider constants\n 150 Caches and servers\n 151 <NAME: (Availability, IMPORT_NAME)>{base_enum_docstring} 152 {base_enum_docstring}""" 153 154 # Dfferentiation of boolean conditions 155 # GIVEN : The state of all provider modules & servers are marked at launch 156 157 HUB: tuple = (has_api("HUB"), "HUB") 158 KAGGLE: tuple = (has_api("KAGGLE"), "KAGGLE") 159 LLAMAFILE: tuple = (has_api("LLAMAFILE"), "LLAMAFILE") 160 LM_STUDIO: tuple = (has_api("LM_STUDIO"), "LM_STUDIO") 161 MLX_AUDIO: tuple = (has_api("MLX_AUDIO"), "MLX_AUDIO") 162 OLLAMA: tuple = (has_api("OLLAMA"), "OLLAMA") 163 VLLM: tuple = (has_api("VLLM"), "VLLM")
Inherited Members
169class PkgType(BaseEnum): 170 """Package dependency constants 171 Collected info from hub model tags and dependencies 172 <NAME: (Availability, IMPORT_NAME, [Github repositories*] 173 *if applicable, otherwise IMPORT_NAME is pip package 174 NOTE: NAME is colloquial and does not always match IMPORT_NAME>""" 175 176 AUDIOGEN: tuple = (has_api("AUDIOCRAFT"), "AUDIOCRAFT", ["exdysa/facebookresearch-audiocraft-revamp"]) # this fork supports mps 177 BAGEL: tuple = (has_api("BAGEL"), "BAGEL", ["bytedance-seed/BAGEL"]) 178 BITNET: tuple = (has_api("BITNET"), "BITNET", ["microsoft/BitNet"]) 179 BITSANDBYTES: tuple = (has_api("BITSANDBYTES"), "BITSANDBYTES", []) # bitsandbytes-foundation/bitsandbytes 180 DFLOAT11: tuple = (has_api("DFLOAT11"), "DFLOAT11", ["LeanModels/DFloat11"]) 181 DIFFUSERS: tuple = (has_api("DIFFUSERS"), "DIFFUSERS", []) 182 EXLLAMAV2: tuple = (has_api("EXLLAMAV2"), "EXLLAMAV2", []) # turboderp-org/exllamav2 183 F_LITE: tuple = (has_api("F_LITE"), "F_LITE", ["fal-ai/f-lite"]) 184 HIDIFFUSION: tuple = (has_api("HIDIFFUSION"), "HIDIFFUSION", ["megvii-research/HiDiffusion"]) 185 IMAGE_GEN_AUX: tuple = (has_api("IMAGE_GEN_AUX"), "IMAGE_GEN_AUX", ["huggingface/image_gen_aux"]) 186 JAX: tuple = (has_api("JAX"), "JAX", []) 187 KERAS: tuple = (has_api("KERAS"), "KERAS", []) 188 LLAMA: tuple = (has_api("LLAMA_CPP"), "LLAMA_CPP", []) 189 LUMINA_MGPT: tuple = (has_api("INFERENCE_SOLVER"), "INFERENCE_SOLVER", ["Alpha-VLLM/Lumina-mGPT"]) 190 LUMINA_MGPT2: tuple = (has_api("INFERENCE_SOLVER"), "INFERENCE_SOLVER", ["Alpha-VLLM/Lumina-mGPT-2.0"]) 191 MFLUX: tuple = (has_api("MFLUX"), "MFLUX", []) # "filipstrand/mflux" 192 MLX_AUDIO: tuple = (CueType.check_type("MLX_AUDIO"), "MLX_AUDIO", []) # Blaizzy/mlx-audio 193 MLX_CHROMA: tuple = (has_api("CHROMA"), "CHROMA", ["exdysa/jack813-mlx-chroma"]) 194 MLX_LM: tuple = (has_api("MLX_LM"), "MLX_LM", []) # "ml-explore/mlx-lm" 195 MLX_VLM: tuple = (has_api("MLX_VLM"), "MLX_VLM", []) # Blaizzy/mlx-vlm 196 MLX: tuple = (has_api("MLX_LM"), "MLX", []) 197 ONNX: tuple = (has_api("ONNX"), "ONNX", ["ONNX"]) 198 ORPHEUS_TTS: tuple = (has_api("ORPHEUS_TTS"), "ORPHEUS_TTS", ["canopyai/Orpheus-TTS"]) 199 OUTETTS: tuple = (has_api("OUTETTS"), "OUTETTS", ["edwko/OuteTTS"]) 200 PARLER_TTS: tuple = (has_api("PARLER_TTS"), "PARLER_TTS", ["huggingface/parler-tts"]) 201 PLEIAS: tuple = (has_api("PLEIAS"), "PLEIAS", ["exdysa/Pleias-Pleias-RAG-Library"]) # bypasses vllm for macos to avoid requiring gcc/AVIX 202 SENTENCE_TRANSFORMERS: tuple = (has_api("SENTENCE_TRANSFORMERS"), "SENTENCE_TRANSFORMERS", []) # UKPLab/sentence-transformers 203 SHOW_O: tuple = (has_api("SHOW_O"), "SHOW_O", ["showlab/show-o"]) 204 SPANDREL_EXTRA_ARCHES: tuple = (has_api("SPANDREL_EXTRA_ARCHES"), "SPANDREL_EXTRA_ARCHES", []) 205 SPANDREL: tuple = (has_api("SPANDREL"), "SPANDREL", []) 206 SVDQUANT: tuple = (has_api("NUNCHAKU"), "NUNCHAKU", ["mit-han-lab/nunchaku"]) 207 TENSORFLOW: tuple = (has_api("TENSORFLOW"), "TENSORFLOW", []) 208 TORCH: tuple = (has_api("TORCH"), "TORCH", []) # Possible that torch is NOT needed (mlx_lm, or some other unforeseen future ) 209 TORCHAUDIO: tuple = (has_api("TORCHAUDIO"), "TORCHAUDIO", []) 210 TORCHVISION: tuple = (has_api("TORCHVISION"), "TORCHVISION", []) 211 TRANSFORMERS: tuple = (has_api("TRANSFORMERS"), "TRANSFORMERS", []) 212 VLLM: tuple = (CueType.check_type("VLLM"), "VLLM", [])
Package dependency constants
Collected info from hub model tags and dependencies
Inherited Members
215class ChipType(Enum): 216 f"""Device constants\n 217 CUDA, MPS, XPU, MTIA [Supported PkgTypes]\n 218 {base_enum_docstring}""" 219 220 def __call__(cls): 221 cls.initialie_device() 222 223 @classmethod 224 def initialize_device(cls) -> None: 225 chip_types = [ 226 ( 227 "CUDA", 228 [ 229 PkgType.BAGEL, 230 PkgType.BITSANDBYTES, 231 PkgType.DFLOAT11, 232 PkgType.EXLLAMAV2, 233 PkgType.F_LITE, 234 PkgType.LUMINA_MGPT, 235 PkgType.ORPHEUS_TTS, 236 PkgType.OUTETTS, 237 PkgType.VLLM, 238 ], 239 ), 240 ("MPS", [PkgType.MFLUX, PkgType.MLX_AUDIO, PkgType.MLX_LM, PkgType.BAGEL]), 241 ("XPU", []), 242 ("MTIA", []), 243 ] 244 cls._device = first_available(assign=True, init=True, clean=True) # pylint:disable=no-member, protected-access 245 if hasattr(cls._device, "type"): 246 gpu = cls._device.type 247 else: 248 gpu = "" 249 for name, pkg_type in chip_types: 250 setattr(cls, name, (name.lower() in gpu, name, pkg_type)) 251 setattr( 252 cls, 253 "CPU", 254 ( 255 True, 256 "CPU", 257 [ 258 PkgType.AUDIOGEN, 259 PkgType.PARLER_TTS, 260 PkgType.LLAMA, 261 PkgType.HIDIFFUSION, 262 PkgType.SENTENCE_TRANSFORMERS, 263 PkgType.DIFFUSERS, 264 PkgType.TRANSFORMERS, 265 PkgType.TORCH, 266 ], 267 ), 268 ) 269 270 @classmethod 271 def _show_all(cls) -> List[str]: 272 """Show all POSSIBLE processor types""" 273 atypes = [atype for atype in cls.__dict__ if "_" not in atype] 274 return atypes 275 276 @classmethod 277 def _show_ready(cls, api_name: Optional[str] = None) -> Union[List[str], bool]: 278 """Show all READY devices.\n 279 If api_name is provided, checks if the specific API is ready. 280 :param api_name: Boolean check for the specific API by name, defaults to None 281 :return: `bool` or list of ready devices 282 """ 283 atypes = cls._show_all() 284 if api_name: 285 return api_name.upper() in [x for x in atypes if getattr(cls, x)[0]] 286 return [getattr(cls, x) for x in atypes if getattr(cls, x)[0] is True] 287 288 @classmethod 289 def _show_pkgs(cls) -> Union[List[PkgType], str]: 290 """Return compatible PkgTypes for all available chipsets\n 291 If no chipsets are detected, returns onlyCPU compatibility options\n 292 :return: `PkgType`s for the available processors including CPU 293 """ 294 pkg_names = getattr(cls, "CPU")[-1] 295 atypes = cls._show_ready() 296 available = [pkg[1] for pkg in atypes if pkg[0] is True] 297 priority = next(iter(available), "CPU") if available else "CPU" 298 if priority not in [pkg[1] for pkg in cls._show_all() if not pkg[2]] and priority != "CPU": 299 pkg_names = atypes[0][2] + pkg_names 300 return pkg_names
223 @classmethod 224 def initialize_device(cls) -> None: 225 chip_types = [ 226 ( 227 "CUDA", 228 [ 229 PkgType.BAGEL, 230 PkgType.BITSANDBYTES, 231 PkgType.DFLOAT11, 232 PkgType.EXLLAMAV2, 233 PkgType.F_LITE, 234 PkgType.LUMINA_MGPT, 235 PkgType.ORPHEUS_TTS, 236 PkgType.OUTETTS, 237 PkgType.VLLM, 238 ], 239 ), 240 ("MPS", [PkgType.MFLUX, PkgType.MLX_AUDIO, PkgType.MLX_LM, PkgType.BAGEL]), 241 ("XPU", []), 242 ("MTIA", []), 243 ] 244 cls._device = first_available(assign=True, init=True, clean=True) # pylint:disable=no-member, protected-access 245 if hasattr(cls._device, "type"): 246 gpu = cls._device.type 247 else: 248 gpu = "" 249 for name, pkg_type in chip_types: 250 setattr(cls, name, (name.lower() in gpu, name, pkg_type)) 251 setattr( 252 cls, 253 "CPU", 254 ( 255 True, 256 "CPU", 257 [ 258 PkgType.AUDIOGEN, 259 PkgType.PARLER_TTS, 260 PkgType.LLAMA, 261 PkgType.HIDIFFUSION, 262 PkgType.SENTENCE_TRANSFORMERS, 263 PkgType.DIFFUSERS, 264 PkgType.TRANSFORMERS, 265 PkgType.TORCH, 266 ], 267 ), 268 )
312class GenTypeC(BaseModel): 313 """ 314 Generative inference types in ***C***-dimensional order\n 315 ***Comprehensiveness***, sorted from 'most involved' to 'least involved'\n 316 The terms define 'artistic' and ambiguous operations\n 317 318 :param clone: Copying identity, voice, exact mirror 319 :param sync: Tone, tempo, color, quality, genre, scale, mood 320 :param translate: A range of comprehensible approximations\n 321 """ 322 323 clone: Annotated[Callable | None, Field(default=None)] 324 sync: Annotated[Callable | None, Field(default=None)] 325 translate: Annotated[Callable | None, Field(default=None)]
Generative inference types in C-dimensional order
Comprehensiveness, sorted from 'most involved' to 'least involved'
The terms define 'artistic' and ambiguous operations
Parameters
- clone: Copying identity, voice, exact mirror
- sync: Tone, tempo, color, quality, genre, scale, mood
- translate: A range of comprehensible approximations
328class GenTypeCText(BaseModel): 329 """ 330 Generative inference types in ***C***-dimensional order for text operations\n 331 ***Comprehensiveness***, sorted from 'most involved' to 'least involved'\n 332 The terms define 'concrete' and more rigid operations\n 333 334 :param research: Quoting, paraphrasing, and deriving from sources 335 :param chain_of_thought: A performance of processing step-by-step (similar to `reasoning`) 336 :param question_answer: Basic, straightforward responses\n 337 """ 338 339 research: Annotated[Optional[Callable | None], Field(default=None, examples=example_str)] 340 chain_of_thought: Annotated[Optional[Callable | None], Field(default=None, examples=example_str)] 341 question_answer: Annotated[Optional[Callable | None], Field(default=None, examples=example_str)]
Generative inference types in C-dimensional order for text operations
Comprehensiveness, sorted from 'most involved' to 'least involved'
The terms define 'concrete' and more rigid operations
Parameters
- research: Quoting, paraphrasing, and deriving from sources
- chain_of_thought: A performance of processing step-by-step (similar to
reasoning) - question_answer: Basic, straightforward responses
344class GenTypeE(BaseModel): 345 """ 346 Generative inference operation types in ***E***-dimensional order \n 347 ***Equivalence***, lists sorted from 'highly-similar' to 'loosely correlated.'"\n 348 :param universal: Affecting all conversions 349 :param text: Text-only conversions\n 350 351 ***multimedia generation*** 352 ``` 353 Y-axis: Detail (Most involved to least involved) 354 │ 355 │ clone 356 │ sync 357 │ translate 358 │ 359 +───────────────────────────────────────> X-axis: Equivalence (Loosely correlated to highly similar) 360 ``` 361 ***text generation*** 362 ``` 363 Y-axis: Detail (Most involved to least involved) 364 │ 365 │ research 366 │ chain-of-thought 367 │ question/answer 368 │ 369 +───────────────────────────────────────> X-axis: Equivalence (Loosely correlated to highly similar) 370 ``` 371 372 This is essentially the translation operation of C types, and the mapping of them to E \n 373 374 An abstract generalization of the set of all multimodal generative synthesis processes\n 375 The sum of each coordinate pair reflects effective compute use\n 376 In this way, both C types and their similarity are translatable, but not 1:1 identical\n 377 Text is allowed to perform all 6 core operations. Other media perform only 3.\n 378 """ 379 380 # note: `sync` may have better terms, such as 'harmonize' or 'attune'. `sync` was chosen because it is shorter 381 382 universal: GenTypeC = GenTypeC(clone=None, sync=None, translate=None) 383 text: GenTypeCText = GenTypeCText(research=None, chain_of_thought=None, question_answer=None)
Generative inference operation types in E-dimensional order
Equivalence, lists sorted from 'highly-similar' to 'loosely correlated.'"
Parameters
- universal: Affecting all conversions
- text: Text-only conversions
multimedia generation
Y-axis: Detail (Most involved to least involved)
│
│ clone
│ sync
│ translate
│
+───────────────────────────────────────> X-axis: Equivalence (Loosely correlated to highly similar)
text generation
Y-axis: Detail (Most involved to least involved)
│
│ research
│ chain-of-thought
│ question/answer
│
+───────────────────────────────────────> X-axis: Equivalence (Loosely correlated to highly similar)
This is essentially the translation operation of C types, and the mapping of them to E
An abstract generalization of the set of all multimodal generative synthesis processes
The sum of each coordinate pair reflects effective compute use
In this way, both C types and their similarity are translatable, but not 1:1 identical
Text is allowed to perform all 6 core operations. Other media perform only 3.