# Copyright (c) 2026 LightSeek Foundation # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. """Utilities for selecting and loading models.""" import contextlib from collections.abc import Iterator import torch from torch import nn from tokenspeed.runtime.configs.model_config import ModelConfig from tokenspeed.runtime.utils import get_colorful_logger logger = get_colorful_logger(__name__) @contextlib.contextmanager def set_default_torch_dtype(dtype: torch.dtype) -> Iterator[None]: """Sets the default torch dtype to the given dtype.""" old_dtype = torch.get_default_dtype() torch.set_default_dtype(dtype) yield torch.set_default_dtype(old_dtype) def get_model_architecture(model_config: ModelConfig) -> tuple[type[nn.Module], str]: from tokenspeed.runtime.models.registry import ModelRegistry architectures = getattr(model_config.hf_config, "architectures", []) # Mixtral only supports the quantization backends listed here in the # current model registry and loader stack. mixtral_supported = ["fp8", "compressed-tensors"] if ( model_config.quantization is not None and model_config.quantization not in mixtral_supported and "MixtralForCausalLM" in architectures ): architectures = ["QuantMixtralForCausalLM"] return ModelRegistry.resolve_model_cls(architectures)