fastvideo.v1.utils
#
Module Contents#
Classes#
ArgumentParser that allows both underscore and dash in names. |
|
SortedHelpFormatter that sorts arguments by their option strings. |
|
Functions#
align height, width according to alignment |
|
replace |
|
Convert a dictionary of mask indices to a 3D list of tensors. |
|
We either use the library file specified by the |
|
Get the current compute dtype from mixed precision policy. |
|
Get the current mixed precision state. |
|
Historical comments: |
|
Check if the model path is a Hugging Face Hub model ID and download it if needed. |
|
Check if the model path is a Hugging Face Hub model ID and download it if needed. |
|
Download and extract just the model_index.json for a Hugging Face model. |
|
Resolve an object by its fully qualified name. |
|
Run a method of an object with the given arguments and keyword arguments. If the method is string, it will be converted to a method using getattr. If the method is serialized bytes and will be deserialized using cloudpickle. If the method is a callable, it will be called directly. |
|
Set mixed precision policy globally. |
|
Verify that the model directory contains a valid diffusers configuration. |
|
A replacement for |
Data#
API#
- class fastvideo.v1.utils.FlexibleArgumentParser(*args, **kwargs)[source]#
Bases:
argparse.ArgumentParser
ArgumentParser that allows both underscore and dash in names.
Initialization
- parse_args(args=None, namespace=None) argparse.Namespace [source]#
- class fastvideo.v1.utils.MixedPrecisionState[source]#
- compute_dtype: Optional[torch.dtype][source]#
None
- master_dtype: Optional[torch.dtype][source]#
None
- output_dtype: Optional[torch.dtype][source]#
None
- param_dtype: Optional[torch.dtype][source]#
None
- reduce_dtype: Optional[torch.dtype][source]#
None
- class fastvideo.v1.utils.SortedHelpFormatter(prog, indent_increment=2, max_help_position=24, width=None)[source]#
Bases:
argparse.HelpFormatter
SortedHelpFormatter that sorts arguments by their option strings.
Initialization
- class fastvideo.v1.utils.StoreBoolean(option_strings, dest, default=False, required=False, help=None)[source]#
Bases:
argparse.Action
- class fastvideo.v1.utils.TypeBasedDispatcher(mapping: List[Tuple[Type, Callable]])[source]#
Initialization
- fastvideo.v1.utils.align_to(value: int, alignment: int) int [source]#
align height, width according to alignment
- fastvideo.v1.utils.current_stream() torch.cuda.Stream [source]#
replace
torch.cuda.current_stream()
withfastvideo.v1.utils.current_stream()
. it turns out thattorch.cuda.current_stream()
is quite expensive, as it will construct a new stream object at each call. here we patchtorch.cuda.set_stream
to keep track of the current stream directly, so that we can avoid callingtorch.cuda.current_stream()
.the underlying hypothesis is that we do not call
torch._C._cuda_setStream
from C/C++ code.
- fastvideo.v1.utils.dict_to_3d_list(mask_strategy: Optional[Dict[str, Any]] = None, t_max: Optional[int] = None, l_max: Optional[int] = None, h_max: Optional[int] = None) List[List[List[Optional[torch.Tensor]]]] [source]#
Convert a dictionary of mask indices to a 3D list of tensors.
- Parameters:
mask_strategy – keys are “t_l_h”, values are torch.Tensor masks.
t_max – if provided (all three), force the output shape to (t_max, l_max, h_max). If all three are None, infer shape from the data.
l_max – if provided (all three), force the output shape to (t_max, l_max, h_max). If all three are None, infer shape from the data.
h_max – if provided (all three), force the output shape to (t_max, l_max, h_max). If all three are None, infer shape from the data.
- fastvideo.v1.utils.find_nccl_library() str [source]#
We either use the library file specified by the
VLLM_NCCL_SO_PATH
environment variable, or we find the library file brought by PyTorch. After importingtorch
,libnccl.so.2
orlibrccl.so.1
can be found byctypes
automatically.
- fastvideo.v1.utils.get_compute_dtype() torch.dtype [source]#
Get the current compute dtype from mixed precision policy.
- Returns:
The compute dtype to use, defaults to get_default_dtype() if no policy set
- Return type:
- fastvideo.v1.utils.get_mixed_precision_state() fastvideo.v1.utils.MixedPrecisionState [source]#
Get the current mixed precision state.
- fastvideo.v1.utils.import_pynvml()[source]#
Historical comments:
libnvml.so is the library behind nvidia-smi, and pynvml is a Python wrapper around it. We use it to get GPU status without initializing CUDA context in the current process. Historically, there are two packages that provide pynvml:
nvidia-ml-py
(https://pypi.org/project/nvidia-ml-py/): The official wrapper. It is a dependency of FastVideo, and is installed when users install FastVideo. It provides a Python module namedpynvml
.pynvml
(https://pypi.org/project/pynvml/): An unofficial wrapper. Prior to version 12.0, it also provides a Python modulepynvml
, and therefore conflicts with the official one which is a standalone Python file. This causes errors when both of them are installed. Starting from version 12.0, it migrates to a new module namedpynvml_utils
to avoid the conflict. It is so confusing that many packages in the community use the unofficial one by mistake, and we have to handle this case. For example,nvcr.io/nvidia/pytorch:24.12-py3
uses the unofficial one, and it will cause errors, see the issue https://github.com/vllm-project/vllm/issues/12847 for example. After all the troubles, we decide to copy the officialpynvml
module to our codebase, and use it directly.
- fastvideo.v1.utils.maybe_download_lora(model_name_or_path: str, local_dir: Optional[str] = None, download: bool = True) str [source]#
Check if the model path is a Hugging Face Hub model ID and download it if needed.
- Parameters:
model_name_or_path – Local path or Hugging Face Hub model ID
local_dir – Local directory to save the model
download – Whether to download the model from Hugging Face Hub
- Returns:
Local path to the model
- fastvideo.v1.utils.maybe_download_model(model_name_or_path: str, local_dir: Optional[str] = None, download: bool = True) str [source]#
Check if the model path is a Hugging Face Hub model ID and download it if needed.
- Parameters:
model_name_or_path – Local path or Hugging Face Hub model ID
local_dir – Local directory to save the model
download – Whether to download the model from Hugging Face Hub
- Returns:
Local path to the model
- fastvideo.v1.utils.maybe_download_model_index(model_name_or_path: str) Dict[str, Any] [source]#
Download and extract just the model_index.json for a Hugging Face model.
- Parameters:
model_name_or_path – Path or HF Hub model ID
- Returns:
The parsed model_index.json as a dictionary
- fastvideo.v1.utils.resolve_obj_by_qualname(qualname: str) Any [source]#
Resolve an object by its fully qualified name.
- fastvideo.v1.utils.run_method(obj: Any, method: Union[str, bytes, Callable], args: tuple[Any], kwargs: dict[str, Any]) Any [source]#
Run a method of an object with the given arguments and keyword arguments. If the method is string, it will be converted to a method using getattr. If the method is serialized bytes and will be deserialized using cloudpickle. If the method is a callable, it will be called directly.
- fastvideo.v1.utils.set_mixed_precision_policy(master_dtype: torch.dtype, param_dtype: torch.dtype, reduce_dtype: torch.dtype, output_dtype: Optional[torch.dtype] = None)[source]#
Set mixed precision policy globally.
- Parameters:
param_dtype – Parameter dtype used for training
reduce_dtype – Reduction dtype used for gradients
output_dtype – Optional output dtype
- fastvideo.v1.utils.verify_model_config_and_directory(model_path: str) Dict[str, Any] [source]#
Verify that the model directory contains a valid diffusers configuration.
- Parameters:
model_path – Path to the model directory
- Returns:
The loaded model configuration as a dictionary
- fastvideo.v1.utils.warn_for_unimplemented_methods(cls: Type[fastvideo.v1.utils.T]) Type[fastvideo.v1.utils.T] [source]#
A replacement for
abc.ABC
. When we useabc.ABC
, subclasses will fail to instantiate if they do not implement all abstract methods. Here, we only requireraise NotImplementedError
in the base class, and log a warning if the method is not implemented in the subclass.