Files
TashaSkyUp-EternalKernelPyt…/hf_diffusers_nodes.py
T
Tasha Upchurch d2d16cadfe Enhanced PyTorch Functionalities and Server Endpoints; Introduced Torchvision and Hugging Face Diffusers Integration
This commit introduces significant enhancements and new features across multiple files in the project. Major changes include:

1. `pytorch_nodes.py`: Expanded PyTorch node functionalities with additional imports, device compatibility checks, optimizer state updates, and training enhancements. Introduced new methods like `optimizer_to` for transferring optimizer state to a specified device, `reset_optimizer_weights` to clear optimizer states, and `make_image_tensor` for converting outputs to image tensors. Also, improved the training process with device compatibility checks, optimizer updates, and the ability to save and load models during training for efficiency.

2. `server_endpoints.py`: Modified server endpoints for improved git integration and user management. Adjustments include dynamic handling of different git repositories, refined branch and user management in GitIO class, and the integration of user-specific git settings in user management functions.

3. `config.yaml`: Added new configurations for temporary directory and git repositories, providing more flexibility and control over file management and version control integration.

4. `torchvision_nodes.py`: Introduced a new file to integrate torchvision functionalities. It includes classes for transform composition, application of torchvision transforms, and handling of torchvision-specific data structures.

5. `hf_diffusers_nodes.py`: Integrated Hugging Face's diffusers library with new nodes for loading and utilizing DDPMPipelines, handling tensor to image conversions, and managing datasets from the Hugging Face ecosystem.

6. `image.py`, `video.py`, `functional.py`: Refactored and enhanced existing functionalities. Included minor tweaks and bug fixes to improve the overall efficiency and robustness of image and video processing capabilities.

7. `requirements.txt`: Updated dependencies, replacing `fastnoisesimd` with `diffusers` to align with the newly integrated Hugging Face diffusers functionalities.

8. `config.py`: Improved the configuration file loading mechanism with dynamic path handling and the ability to replace placeholders with actual paths, enhancing the flexibility of configuration management.

This comprehensive update significantly enhances the project's capabilities in machine learning, especially in PyTorch and torchvision, and improves server endpoint functionalities for better user and version control management.
2023-12-27 13:22:48 -05:00

147 lines
3.9 KiB
Python

import torch
import folder_paths
import torchvision
import torch.nn as nn
import functools
import torch
import json
try:
from .config import config_settings
except ImportError as e:
from config import config_settings
NODE_CLASS_MAPPINGS = {}
NODE_DISPLAY_NAME_MAPPINGS = {}
def ETK_HF_Diffusers_base(cls):
cls.CATEGORY = "ETK/HF/Diffusers"
# Add spaces to the camel case class name
pretty_name = cls.__name__
for i in range(1, len(pretty_name)):
if pretty_name[i].isupper():
pretty_name = pretty_name[:i] + " " + pretty_name[i:]
cls.DISPLAY_NAME = pretty_name
NODE_CLASS_MAPPINGS[cls.__name__] = cls
NODE_DISPLAY_NAME_MAPPINGS[cls.DISPLAY_NAME] = pretty_name
# Wrap the function defined in the FUNCTION attribute
func_name = getattr(cls, "FUNCTION", None)
if func_name and hasattr(cls, func_name):
original_func = getattr(cls, func_name)
@functools.wraps(original_func)
def wrapped_func(*args, **kwargs):
with torch.inference_mode(False):
return original_func(*args, **kwargs)
setattr(cls, func_name, wrapped_func)
return cls
@ETK_HF_Diffusers_base
class DDPMPipline:
"""A node that loads the specified DDPMPipeline, supports only from_pretrained for now"""
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"model_name": ("STRING", {"default": "johnowhitaker/ddpm-butterflies-32px"}),
"device": (["cpu", "cuda"], {"default": "cuda"}),
},
}
RETURN_TYPES = ("DDPM_PL",)
FUNCTION = "load_from_pretrained"
def load_from_pretrained(self, model_name, device):
from diffusers import DDPMPipeline
ret = DDPMPipeline.from_pretrained(model_name, device=device)
return (ret,)
@ETK_HF_Diffusers_base
class SampleDDPMPipline:
"""A node that samples from the specified DDPMPipeline"""
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"model": ("DDPM_PL", {}),
"batch_size": ("INT", {"default": 1}),
"num_timesteps": ("INT", {"default": 1000}),
},
}
RETURN_TYPES = ("TENSOR",)
FUNCTION = "sample"
def sample(
self,
model,
batch_size,
num_timesteps,
):
ret = model(
batch_size=batch_size,
generator=None,
num_inference_steps=num_timesteps,
output_type="tensor",
)
ret = torch.tensor(ret.images)
return (ret,)
@ETK_HF_Diffusers_base
class TensorToImage:
"""A node that converts a tensor to an image"""
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"tensor": ("TORCH_TENSOR", {}),
"incoming_format": (["BHWC", "BCHW","HWC"], {"default": "BHWC"}
),
},
}
RETURN_TYPES = ("IMAGE",)
FUNCTION = "tensor_to_image"
def tensor_to_image(self, tensor, incoming_format):
# outgoing format is BHWC torch tensor float 32 0-1
if incoming_format == "BCHW":
tensor = tensor.permute(0, 2, 3, 1)
elif incoming_format == "HWC":
tensor = tensor.unsqueeze(0)
tensor = tensor.clamp(0, 1)
return (tensor,)
@ETK_HF_Diffusers_base
class LoadHFDataset:
"""A node that loads the specified HFDataset"""
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"dataset_name": ("STRING", {"default": "huggan/smithsonian_butterflies_subset"}),
},
}
RETURN_TYPES = ("HF_DATASET",)
FUNCTION = "load_from_hf"
def load_from_hf(self, dataset_name):
from datasets import load_dataset
ret = load_dataset(dataset_name,
split="train",
)
return (ret,)