Added various activation function nodes and chained model node.

This commit is contained in:
HowToSD
2025-03-14 19:12:34 -07:00
parent 8fbecdfba9
commit d5a14828df
163 changed files with 1378 additions and 128 deletions
@@ -0,0 +1,48 @@
from typing import Any, Dict, Tuple, Callable
import torch
class PtApplyFunction:
"""
Applies a function to the input tensor.
### Fields:
- tens: Tensor to which the function be applied.
- closure: Differentiable function to apply to the tensor.
category: PyTorch wrapper - Training
"""
@classmethod
def INPUT_TYPES(cls) -> Dict[str, Any]:
"""
Defines the expected input types.
Returns:
Dict[str, Any]: A dictionary specifying required and optional input types.
"""
return {
"required": {
"tens": ("TENSOR", {}),
"closure": ("PTCALLABLE", {})
}
}
RETURN_TYPES: Tuple[str, ...] = ("TENSOR",)
FUNCTION: str = "f"
CATEGORY: str = "Training"
def f(self,
tens: torch.Tensor,
closure: Callable) -> Tuple[torch.Tensor]:
"""
Applies a function to the input tensor.
Args:
tens (torch.Tensor): Tensor to which the function be applied.
closure (Callable): Differentiable function to apply to the tensor.
Returns:
Tuple[nn.Module]: A tuple containing the instantiated model.
"""
return (closure(tens),)
+38
View File
@@ -0,0 +1,38 @@
from typing import Any, Dict, Callable, Tuple
import torch.nn.functional
class PtfGELU:
"""
Ptf GELU:
A PyTorch wrapper for the GELU activation function.
This class provides a callable that applies the GELU activation function
from `torch.nn.functional`.
category: PyTorch wrapper - Callable
"""
@classmethod
def INPUT_TYPES(cls) -> Dict[str, Any]:
"""
Defines the required input types.
Returns:
Dict[str, Any]: An empty dictionary since this function requires no inputs.
"""
return {}
RETURN_TYPES: Tuple[str, ...] = ("PTCALLABLE",)
FUNCTION: str = "f"
CATEGORY: str = "Data Analysis"
def f(self) -> Tuple[Callable]:
"""
Returns the PyTorch GELU function as a callable.
Returns:
Tuple[Callable]: A tuple containing `torch.nn.functional.silu`.
"""
return (torch.nn.functional.gelu,)
+47
View File
@@ -0,0 +1,47 @@
from typing import Any, Dict, Callable, Tuple
import torch.nn.functional
import functools
class PtfLeakyReLU:
"""
Ptf LeakyReLU:
A PyTorch wrapper for the LeakyReLU activation function.
This class provides a callable that applies the LeakyReLU activation function
from `torch.nn.functional`.
category: PyTorch wrapper - Callable
"""
@classmethod
def INPUT_TYPES(cls) -> Dict[str, Any]:
"""
Defines the required input types.
Returns:
Dict[str, Any]: An empty dictionary since this function requires no inputs.
"""
return {
"required": {
"negative_slope": ("FLOAT", {"default": 0.01, "min": 1e-6, "max": 1e4, "step": 1e-5}),
}
}
RETURN_TYPES: Tuple[str, ...] = ("PTCALLABLE",)
FUNCTION: str = "f"
CATEGORY: str = "Data Analysis"
def f(self, negative_slope: float) -> Tuple[Callable]:
"""
Returns the PyTorch LeakyReLU function as a callable.
Args:
negative_slope (float): The multiplier for the negative input.
Returns:
Tuple[Callable]: A tuple containing `torch.nn.functional.leaky_relu`.
"""
leaky_relu_last_axis = functools.partial(torch.nn.functional.leaky_relu, negative_slope=negative_slope)
return (leaky_relu_last_axis,)
@@ -0,0 +1,39 @@
from typing import Any, Dict, Callable, Tuple
import torch
import torch.nn.functional
import functools
log_softmax_last_axis = functools.partial(torch.nn.functional.log_softmax, dim=-1)
class PtfLogSoftmax:
"""
A PyTorch wrapper for the log_softmax activation function.
This class provides a callable that applies the log_softmax activation function
from `torch.nn.functional`.
category: PyTorch wrapper - Callable
"""
@classmethod
def INPUT_TYPES(cls) -> Dict[str, Any]:
"""
Defines the required input types.
Returns:
Dict[str, Any]: An empty dictionary since this function requires no inputs.
"""
return {}
RETURN_TYPES: Tuple[str, ...] = ("PTCALLABLE",)
FUNCTION: str = "f"
CATEGORY: str = "Data Analysis"
def f(self) -> Tuple[Callable]:
"""
Returns the PyTorch log_softmax function as a callable.
Returns:
Tuple[Callable]: A tuple containing `logsoftmax_last_axis`.
"""
return (log_softmax_last_axis,)
+38
View File
@@ -0,0 +1,38 @@
from typing import Any, Dict, Callable, Tuple
import torch.nn.functional
class PtfReLU:
"""
Ptf ReLU:
A PyTorch wrapper for the ReLU activation function.
This class provides a callable that applies the ReLU activation function
from `torch.nn.functional`.
category: PyTorch wrapper - Callable
"""
@classmethod
def INPUT_TYPES(cls) -> Dict[str, Any]:
"""
Defines the required input types.
Returns:
Dict[str, Any]: An empty dictionary since this function requires no inputs.
"""
return {}
RETURN_TYPES: Tuple[str, ...] = ("PTCALLABLE",)
FUNCTION: str = "f"
CATEGORY: str = "Data Analysis"
def f(self) -> Tuple[Callable]:
"""
Returns the PyTorch ReLU function as a callable.
Returns:
Tuple[Callable]: A tuple containing `torch.nn.functional.relu`.
"""
return (torch.nn.functional.relu,)
+36
View File
@@ -0,0 +1,36 @@
from typing import Any, Dict, Callable, Tuple
import torch.nn.functional
class PtfSigmoid:
"""
A PyTorch wrapper for the sigmoid activation function.
This class provides a callable that applies the sigmoid activation function
from `torch.nn.functional`.
category: PyTorch wrapper - Callable
"""
@classmethod
def INPUT_TYPES(cls) -> Dict[str, Any]:
"""
Defines the required input types.
Returns:
Dict[str, Any]: An empty dictionary since this function requires no inputs.
"""
return {}
RETURN_TYPES: Tuple[str, ...] = ("PTCALLABLE",)
FUNCTION: str = "f"
CATEGORY: str = "Data Analysis"
def f(self) -> Tuple[Callable]:
"""
Returns the PyTorch sigmoid function as a callable.
Returns:
Tuple[Callable]: A tuple containing `torch.nn.functional.relu`.
"""
return (torch.nn.functional.sigmoid,)
+38
View File
@@ -0,0 +1,38 @@
from typing import Any, Dict, Callable, Tuple
import torch.nn.functional
class PtfSiLU:
"""
Ptf SiLU:
A PyTorch wrapper for the SiLU activation function.
This class provides a callable that applies the SiLU activation function
from `torch.nn.functional`.
category: PyTorch wrapper - Callable
"""
@classmethod
def INPUT_TYPES(cls) -> Dict[str, Any]:
"""
Defines the required input types.
Returns:
Dict[str, Any]: An empty dictionary since this function requires no inputs.
"""
return {}
RETURN_TYPES: Tuple[str, ...] = ("PTCALLABLE",)
FUNCTION: str = "f"
CATEGORY: str = "Data Analysis"
def f(self) -> Tuple[Callable]:
"""
Returns the PyTorch SiLU function as a callable.
Returns:
Tuple[Callable]: A tuple containing `torch.nn.functional.silu`.
"""
return (torch.nn.functional.silu,)
+41
View File
@@ -0,0 +1,41 @@
from typing import Any, Dict, Callable, Tuple
import torch.nn.functional
import functools
softmax_last_axis = functools.partial(torch.nn.functional.softmax, dim=-1)
class PtfSoftmax:
"""
A PyTorch wrapper for the softmax activation function.
This class provides a callable that applies the softmax activation function
from `torch.nn.functional`.
Internally, this node calls torch.nn.functional.softmax(dim=-1).
See https://github.com/pytorch/pytorch/issues/1020 to find out more about the meaning of dim.
category: PyTorch wrapper - Callable
"""
@classmethod
def INPUT_TYPES(cls) -> Dict[str, Any]:
"""
Defines the required input types.
Returns:
Dict[str, Any]: An empty dictionary since this function requires no inputs.
"""
return {}
RETURN_TYPES: Tuple[str, ...] = ("PTCALLABLE",)
FUNCTION: str = "f"
CATEGORY: str = "Data Analysis"
def f(self) -> Tuple[Callable]:
"""
Returns the PyTorch softmax function as a callable.
Returns:
Tuple[Callable]: A tuple containing `torch.nn.functional.relu`.
"""
return (softmax_last_axis,)
+38
View File
@@ -0,0 +1,38 @@
from typing import Any, Dict, Callable, Tuple
import torch.nn.functional
class PtfSoftplus:
"""
Ptf Softplus:
A PyTorch wrapper for the Softplus activation function.
This class provides a callable that applies the Softplus activation function
from `torch.nn.functional`.
category: PyTorch wrapper - Callable
"""
@classmethod
def INPUT_TYPES(cls) -> Dict[str, Any]:
"""
Defines the required input types.
Returns:
Dict[str, Any]: An empty dictionary since this function requires no inputs.
"""
return {}
RETURN_TYPES: Tuple[str, ...] = ("PTCALLABLE",)
FUNCTION: str = "f"
CATEGORY: str = "Data Analysis"
def f(self) -> Tuple[Callable]:
"""
Returns the PyTorch Softplus function as a callable.
Returns:
Tuple[Callable]: A tuple containing `torch.nn.functional.softplus`.
"""
return (torch.nn.functional.softplus,)
+36
View File
@@ -0,0 +1,36 @@
from typing import Any, Dict, Callable, Tuple
import torch.nn.functional
class PtfTanh:
"""
A PyTorch wrapper for the tanh activation function.
This class provides a callable that applies the tanh activation function
from `torch.nn.functional`.
category: PyTorch wrapper - Callable
"""
@classmethod
def INPUT_TYPES(cls) -> Dict[str, Any]:
"""
Defines the required input types.
Returns:
Dict[str, Any]: An empty dictionary since this function requires no inputs.
"""
return {}
RETURN_TYPES: Tuple[str, ...] = ("PTCALLABLE",)
FUNCTION: str = "f"
CATEGORY: str = "Data Analysis"
def f(self) -> Tuple[Callable]:
"""
Returns the PyTorch tanh function as a callable.
Returns:
Tuple[Callable]: A tuple containing `torch.nn.functional.relu`.
"""
return (torch.nn.functional.tanh,)
@@ -0,0 +1,108 @@
from typing import Any, Dict, Tuple, Callable, Optional
import torch
import torch.nn as nn
class PtnChainedModelDef(nn.Module):
"""
A chained model that sequentially applies `model_a` and `model_b`,
optionally followed by a closure function.
### Fields:
- `model_a`: First model in the chain.
- `model_b`: Second model in the chain.
- `closure`: Optional differentiable function to be called at the end of forward.
Example:
>>> model_a = nn.Linear(10, 20)
>>> model_b = nn.Linear(20, 5)
>>> closure = torch.nn.ReLU()
>>> model = PtnChainedModelDef(model_a, model_b, closure)
>>> x = torch.randn(1, 10)
>>> output = model(x)
pragma: skip_doc
"""
def __init__(self,
model_a: nn.Module,
model_b: nn.Module,
closure: Optional[Callable] = None) -> None:
super().__init__()
self.model_a = model_a
self.model_b = model_b
self.closure = closure
def forward(self, inputs: torch.Tensor) -> torch.Tensor:
"""
Forward pass through the chained models.
Args:
inputs (torch.Tensor): Input tensor.
Returns:
torch.Tensor: Output tensor after passing through `model_a`, `model_b`, and optionally `closure`.
"""
x = self.model_a(inputs)
x = self.model_b(x) # Fixed the incorrect use of `inputs` instead of `x`
if self.closure:
x = self.closure(x)
return x
class PtnChainedModel:
"""
Constructs a chained PyTorch model.
### Fields:
- model_a: First model in the chain.
- model_b: Second model in the chain.
- closure: Optional differentiable function to be called at the end of forward.
category: PyTorch wrapper - Training
"""
@classmethod
def INPUT_TYPES(cls) -> Dict[str, Any]:
"""
Defines the expected input types.
Returns:
Dict[str, Any]: A dictionary specifying required and optional input types.
"""
return {
"required": {
"model_a": ("PTMODEL", {}),
"model_b": ("PTMODEL", {})
},
"optional": {
"closure": ("PTCALLABLE", {})
}
}
RETURN_TYPES: Tuple[str, ...] = ("PTMODEL",)
FUNCTION: str = "f"
CATEGORY: str = "Training"
@classmethod
def IS_CHANGED(cls, **kw):
return float("NaN")
def f(self,
model_a: nn.Module,
model_b: nn.Module,
closure: Optional[Callable] = None) -> Tuple[nn.Module]:
"""
Constructs a chained model with optional closure.
Args:
model_a (nn.Module): First model in the chain.
model_b (nn.Module): Second model in the chain.
closure (Callable, optional): A function applied after `model_b`.
Returns:
Tuple[nn.Module]: A tuple containing the instantiated model.
"""
with torch.inference_mode(False):
model = PtnChainedModelDef(model_a, model_b, closure)
return (model,)