6f5dfc09fd20992ee49ded85fca2b5ebadfc147e
ComfyUI Quantization Node Pack
A custom node pack for ComfyUI that provides tools for quantizing model weights to lower precision formats like FP16, BF16, or true FP8 types.
Overview
This node pack provides tools for quantizing models directly within ComfyUI. It includes:
- Model To State Dict: Extracts the state dictionary from a model object and attempts to normalize keys.
- Quantize Model to FP8 Format: Converts model weights directly to
float8_e4m3fnorfloat8_e5m2format (requires CUDA). - Quantize Model Scaled: Applies simulated FP8 scaling (per-tensor or per-channel) and then casts the model to
float16,bfloat16, or keeps the original format. - Save As SafeTensor: Saves the processed state dictionary to a
.safetensorsfile at a specified path.
Installation
- Clone or download this repository into your ComfyUI's
custom_nodesdirectory.- Example using git:
cd ComfyUI/custom_nodes git clone [https://github.com/YourUsername/YourRepoName.git](https://github.com/YourUsername/YourRepoName.git) ComfyUI_QuantNodes # Replace with your actual repo URL and desired folder name - Alternatively, download the ZIP and extract it into
ComfyUI/custom_nodes/ComfyUI_QuantNodes.
- Example using git:
- Install dependencies (optional, check if already installed):
pip install -r ComfyUI/custom_nodes/ComfyUI_QuantNodes/requirements.txt - Restart ComfyUI.
Usage
Model To State Dict
- Category:
Model Quantization/Utils - Function: Extracts state dict from a MODEL object, stripping common prefixes.
- Inputs:
model: The inputMODELobject.
- Outputs:
model_state_dict: The extracted state dictionary.
Quantize Model to FP8 Format
- Category:
Model Quantization/FP8 Direct - Function: Converts model weights directly to a specific FP8 format. Requires CUDA.
- Inputs:
model_state_dict: The state dictionary to quantize.fp8_format: The target FP8 format (float8_e5m2orfloat8_e4m3fn).
- Outputs:
quantized_model_state_dict: The state dictionary with FP8 tensors.
Quantize Model Scaled
- Category:
Model Quantization - Function: Applies simulated FP8 value scaling and then casts to FP16, BF16, or keeps the original dtype. Useful for size reduction with good compatibility.
- Inputs:
model_state_dict: The state dictionary to quantize.scaling_strategy: How to simulate scaling (per_tensororper_channel).processing_device: Where to perform calculations (Auto,CPU,GPU).output_dtype: Final data type (Original,float16,bfloat16). Defaults tofloat16.
- Outputs:
quantized_model_state_dict: The processed state dictionary.
Save As SafeTensor
- Category:
Model Quantization/Save - Function: Saves the processed state dictionary to a
.safetensorsfile. - Inputs:
quantized_model_state_dict: The state dictionary to save.absolute_save_path: The full path (including filename) where the model will be saved.
- Outputs: None (Output node).
Example Workflow
- Load a model using a standard loader (e.g.,
Load Checkpoint). - Connect the
MODELoutput to theModel To State Dictnode. - Connect the
model_state_dictoutput fromModel To State DicttoQuantize Model Scaled. - In
Quantize Model Scaled, select your desiredscaling_strategyand setoutput_dtypetofloat16(for size reduction). - Connect the
quantized_model_state_dictoutput fromQuantize Model Scaledto theSave Model as SafeTensornode. - Specify the
absolute_save_pathin theSave Model as SafeTensornode. - Queue the prompt.
- Restart ComfyUI or refresh loaders to find the saved model.
Requirements
- PyTorch (usually included with ComfyUI)
safetensorstqdm- CUDA-enabled GPU is required for the
Quantize Model scalednode.
License
MIT (Or your chosen license)
Languages
Python
99.5%
JavaScript
0.5%