Added various PyTorch wrapper nodes to generate tensors.
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
from typing import Any, Dict
|
||||
import torch
|
||||
import ast
|
||||
from .utils import DTYPE_MAPPING
|
||||
|
||||
class PtFull:
|
||||
"""
|
||||
Creates a PyTorch tensor filled with a specified value using the size entered in the text field.
|
||||
For example, if you want to create a 4D tensor of batch size=2, channel=3, height=96, width=32 filled with 7, enter:
|
||||
[2,3,96,32] for size and 7 for value.
|
||||
|
||||
category: PyTorch wrapper - Tensor creation
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls) -> Dict[str, Any]:
|
||||
"""
|
||||
Defines the input types for the function.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: A dictionary specifying required input types.
|
||||
"""
|
||||
return {
|
||||
"required": {
|
||||
"value": ("STRING", {"multiline": False}),
|
||||
"size": ("STRING", {"multiline": True}),
|
||||
"data_type": (("float32", "float16", "bfloat16", "float64", "uint8", "int8", "int16", "int32", "int64", "bool"),)
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES: tuple = ("TENSOR",)
|
||||
FUNCTION: str = "f"
|
||||
CATEGORY: str = "Data Analysis"
|
||||
|
||||
def f(self, value: str, size: str, data_type: str) -> tuple:
|
||||
"""
|
||||
Creates a PyTorch tensor filled with a specified value.
|
||||
|
||||
Args:
|
||||
value (str): String representing the value to fill the tensor.
|
||||
size (str): String representation of the tensor size.
|
||||
data_type (str): Desired data type of the tensor.
|
||||
|
||||
Returns:
|
||||
tuple: A tuple containing the PyTorch tensor.
|
||||
"""
|
||||
# Convert string to Python list
|
||||
list_size = ast.literal_eval(size)
|
||||
|
||||
# Convert the value to the appropriate type
|
||||
dtype = DTYPE_MAPPING[data_type]
|
||||
|
||||
if dtype in {torch.float32, torch.float16, torch.bfloat16, torch.float64}:
|
||||
fill_value = float(value)
|
||||
elif dtype in {torch.uint8, torch.int8, torch.int16, torch.int32, torch.int64}:
|
||||
fill_value = int(value)
|
||||
elif dtype == torch.bool:
|
||||
fill_value = value.lower() in {"true", "1"}
|
||||
else:
|
||||
raise ValueError(f"Unsupported data type: {data_type}")
|
||||
|
||||
# Convert list to PyTorch Size
|
||||
sz = torch.Size(list_size)
|
||||
tens = torch.full(sz, fill_value, dtype=dtype)
|
||||
|
||||
return (tens,)
|
||||
@@ -0,0 +1,53 @@
|
||||
from typing import Any, Dict
|
||||
import torch
|
||||
import ast
|
||||
from .utils import DTYPE_MAPPING
|
||||
|
||||
class PtOnes:
|
||||
"""
|
||||
Creates a PyTorch tensor of ones using the size entered in the text field.
|
||||
For example, if you want to create a 4D tensor of batch size=2, channel=3, height=96, width=32, enter:
|
||||
[2,3,96,32]
|
||||
|
||||
category: PyTorch wrapper - Tensor creation
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls) -> Dict[str, Any]:
|
||||
"""
|
||||
Defines the input types for the function.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: A dictionary specifying required input types.
|
||||
"""
|
||||
return {
|
||||
"required": {
|
||||
"size": ("STRING", {"multiline": True}),
|
||||
"data_type": (("float32", "float16", "bfloat16", "float64", "uint8", "int8", "int16", "int32", "int64", "bool"),)
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES: tuple = ("TENSOR",)
|
||||
FUNCTION: str = "f"
|
||||
CATEGORY: str = "Data Analysis"
|
||||
|
||||
def f(self, size: str, data_type: str) -> tuple:
|
||||
"""
|
||||
Creates a PyTorch tensor of ones using the size entered in the text field.
|
||||
|
||||
Args:
|
||||
size (str): String value in the text field.
|
||||
data_type (str): Desired data type of the tensor.
|
||||
|
||||
Returns:
|
||||
tuple: A tuple containing the PyTorch tensor.
|
||||
"""
|
||||
# Convert string to Python list
|
||||
list_size = ast.literal_eval(size)
|
||||
|
||||
# Convert list to PyTorch Size
|
||||
sz = torch.Size(list_size)
|
||||
|
||||
tens = torch.ones(sz, dtype=DTYPE_MAPPING[data_type])
|
||||
|
||||
return (tens,)
|
||||
@@ -0,0 +1,60 @@
|
||||
from typing import Any, Dict
|
||||
import torch
|
||||
import ast
|
||||
|
||||
|
||||
class PtPermute:
|
||||
"""
|
||||
Permutes the dimensions of a PyTorch tensor according to the specified order.
|
||||
For example, if a tensor has shape (2, 3, 96, 32) and `new_axes="[0, 3, 1, 2]"`,
|
||||
the output will have shape (2, 32, 3, 96).
|
||||
|
||||
Specify the new axis order in the `new_axes` field using brackets, as shown below:
|
||||
```
|
||||
[0, 3, 1, 2]
|
||||
```
|
||||
category: PyTorch wrapper - Transform
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls) -> Dict[str, Any]:
|
||||
"""
|
||||
Defines the input types for the function.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: A dictionary specifying required input types.
|
||||
"""
|
||||
return {
|
||||
"required": {
|
||||
"tens": ("TENSOR", {}),
|
||||
"new_axes": ("STRING", {"multiline": True})
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES: tuple = ("TENSOR",)
|
||||
FUNCTION: str = "f"
|
||||
CATEGORY: str = "Data Analysis"
|
||||
|
||||
def f(self, tens: torch.Tensor, new_axes: str) -> tuple:
|
||||
"""
|
||||
Permutes the dimensions of the input tensor based on the specified order.
|
||||
|
||||
Args:
|
||||
tens (torch.Tensor): The input tensor.
|
||||
new_axes (str): A string representation of the desired dimension order,
|
||||
e.g., "[0, 3, 1, 2]" or "(0, 3, 1, 2)".
|
||||
|
||||
Returns:
|
||||
tuple: A tuple containing the permuted PyTorch tensor.
|
||||
"""
|
||||
# Convert string to a tuple of integers
|
||||
permute_order = tuple(ast.literal_eval(new_axes))
|
||||
|
||||
# Ensure the parsed order is valid
|
||||
if not isinstance(permute_order, tuple) or not all(isinstance(i, int) for i in permute_order):
|
||||
raise ValueError(f"Invalid format for new_axes: {new_axes}. Must be a tuple or list of integers.")
|
||||
|
||||
# Apply permutation
|
||||
tens = tens.permute(*permute_order)
|
||||
|
||||
return (tens,)
|
||||
@@ -0,0 +1,57 @@
|
||||
from typing import Any, Dict
|
||||
import torch
|
||||
import ast
|
||||
from .utils import DTYPE_MAPPING
|
||||
|
||||
class PtRand:
|
||||
"""
|
||||
Creates a PyTorch tensor with values sampled from a uniform distribution
|
||||
using the size entered in the text field.
|
||||
For example, if you want to create a 4D tensor of batch size=2, channel=3, height=96, width=32, enter:
|
||||
[2,3,96,32]
|
||||
|
||||
category: PyTorch wrapper - Tensor creation
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls) -> Dict[str, Any]:
|
||||
"""
|
||||
Defines the input types for the function.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: A dictionary specifying required input types.
|
||||
"""
|
||||
return {
|
||||
"required": {
|
||||
"size": ("STRING", {"multiline": True}),
|
||||
"data_type": (("float32", "float16", "bfloat16", "float64"),)
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES: tuple = ("TENSOR",)
|
||||
FUNCTION: str = "f"
|
||||
CATEGORY: str = "Data Analysis"
|
||||
|
||||
@classmethod
|
||||
def IS_CHANGED(cls, **kw):
|
||||
return float("NaN")
|
||||
|
||||
def f(self, size: str, data_type: str) -> tuple:
|
||||
"""
|
||||
Creates a PyTorch tensor with values sampled from a uniform distribution.
|
||||
|
||||
Args:
|
||||
size (str): String value in the text field.
|
||||
data_type (str): Desired floating-point data type of the tensor.
|
||||
|
||||
Returns:
|
||||
tuple: A tuple containing the PyTorch tensor.
|
||||
"""
|
||||
# Convert string to Python list
|
||||
list_size = ast.literal_eval(size)
|
||||
|
||||
# Convert list to PyTorch Size
|
||||
sz = torch.Size(list_size)
|
||||
tens = torch.rand(sz, dtype=DTYPE_MAPPING[data_type])
|
||||
|
||||
return (tens,)
|
||||
@@ -0,0 +1,72 @@
|
||||
import time
|
||||
from typing import Any, Dict
|
||||
import torch
|
||||
import ast
|
||||
import copy
|
||||
from .utils import DTYPE_MAPPING
|
||||
|
||||
class PtRandInt:
|
||||
"""
|
||||
Creates a PyTorch tensor filled with random integers within a specified range using the size entered in the text field.
|
||||
For example, if you want to create a 4D tensor of batch size=2, channel=3, height=96, width=32 with random integers in the range [0, 10),
|
||||
enter [2,3,96,32] for size, 0 for min_value, and 10 for max_value.
|
||||
|
||||
category: PyTorch wrapper - Tensor creation
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls) -> Dict[str, Any]:
|
||||
"""
|
||||
Defines the input types for the function.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: A dictionary specifying required input types.
|
||||
"""
|
||||
return {
|
||||
"required": {
|
||||
"min_value": ("INT", {"default": 0, "min": -2**31, "max": 2**31}),
|
||||
"max_value": ("INT", {"default": 1, "min": -2**31, "max": 2**31}),
|
||||
"size": ("STRING", {"multiline": True}),
|
||||
"data_type": (("uint8", "int8", "int16", "int32", "int64"),)
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES: tuple = ("TENSOR",)
|
||||
FUNCTION: str = "f"
|
||||
CATEGORY: str = "Data Analysis"
|
||||
|
||||
@classmethod
|
||||
def IS_CHANGED(cls, **kw):
|
||||
return float("NaN")
|
||||
|
||||
def __init__(self):
|
||||
# Initialize a separate generator with a different seed
|
||||
self.gen = torch.Generator()
|
||||
self.gen.manual_seed(int(time.time() * 1000000) % (2**32)) # Unique seed
|
||||
|
||||
def f(self, min_value: int, max_value: int, size: str, data_type: str) -> tuple:
|
||||
"""
|
||||
Creates a PyTorch tensor filled with random integers in the range [min_value, max_value).
|
||||
|
||||
Args:
|
||||
min_value (int): Minimum value (inclusive).
|
||||
max_value (int): Maximum value (exclusive).
|
||||
size (str): String representation of the tensor size.
|
||||
data_type (str): Desired integer data type of the tensor.
|
||||
|
||||
Returns:
|
||||
tuple: A tuple containing the PyTorch tensor.
|
||||
"""
|
||||
# Convert string to Python list
|
||||
list_size = ast.literal_eval(size)
|
||||
|
||||
# Convert list to PyTorch Size
|
||||
sz = torch.Size(list_size)
|
||||
|
||||
# Ensure max_value is greater than min_value
|
||||
if max_value <= min_value:
|
||||
raise ValueError("max_value must be greater than min_value.")
|
||||
|
||||
# Generate random integers within the specified range
|
||||
tens = torch.randint(min_value, max_value, sz, dtype=DTYPE_MAPPING[data_type], generator=self.gen)
|
||||
return (tens,)
|
||||
@@ -0,0 +1,57 @@
|
||||
from typing import Any, Dict
|
||||
import torch
|
||||
import ast
|
||||
from .utils import DTYPE_MAPPING
|
||||
|
||||
class PtRandn:
|
||||
"""
|
||||
Creates a PyTorch tensor with values sampled from a standard normal distribution (mean=0, std=1)
|
||||
using the size entered in the text field.
|
||||
For example, if you want to create a 4D tensor of batch size=2, channel=3, height=96, width=32, enter:
|
||||
[2,3,96,32]
|
||||
|
||||
category: PyTorch wrapper - Tensor creation
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls) -> Dict[str, Any]:
|
||||
"""
|
||||
Defines the input types for the function.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: A dictionary specifying required input types.
|
||||
"""
|
||||
return {
|
||||
"required": {
|
||||
"size": ("STRING", {"multiline": True}),
|
||||
"data_type": (("float32", "float16", "bfloat16", "float64"),)
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES: tuple = ("TENSOR",)
|
||||
FUNCTION: str = "f"
|
||||
CATEGORY: str = "Data Analysis"
|
||||
|
||||
@classmethod
|
||||
def IS_CHANGED(cls, **kw):
|
||||
return float("NaN")
|
||||
|
||||
def f(self, size: str, data_type: str) -> tuple:
|
||||
"""
|
||||
Creates a PyTorch tensor with values sampled from a standard normal distribution.
|
||||
|
||||
Args:
|
||||
size (str): String value in the text field.
|
||||
data_type (str): Desired floating-point data type of the tensor.
|
||||
|
||||
Returns:
|
||||
tuple: A tuple containing the PyTorch tensor.
|
||||
"""
|
||||
# Convert string to Python list
|
||||
list_size = ast.literal_eval(size)
|
||||
|
||||
# Convert list to PyTorch Size
|
||||
sz = torch.Size(list_size)
|
||||
tens = torch.randn(sz, dtype=DTYPE_MAPPING[data_type])
|
||||
|
||||
return (tens,)
|
||||
@@ -0,0 +1,45 @@
|
||||
from typing import Any, Dict
|
||||
import torch
|
||||
from .utils import DTYPE_MAPPING
|
||||
|
||||
class PtSqueeze:
|
||||
"""
|
||||
Removes a dimension at the specified position in the input tensor if it is of size 1.
|
||||
|
||||
Specifying a tuple of dimensions is not supported yet.
|
||||
|
||||
category: PyTorch wrapper - Transform
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls) -> Dict[str, Any]:
|
||||
"""
|
||||
Defines the input types for the function.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: A dictionary specifying required input types.
|
||||
"""
|
||||
return {
|
||||
"required": {
|
||||
"tens": ("TENSOR", {}),
|
||||
"dim": ("INT", {"default": -1, "min": -1, "max": 10})
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES: tuple = ("TENSOR",)
|
||||
FUNCTION: str = "f"
|
||||
CATEGORY: str = "Data Analysis"
|
||||
|
||||
def f(self, tens: torch.Tensor, dim: int) -> tuple:
|
||||
"""
|
||||
Removes a dimension at the specified position in the input tensor if it is of size 1.
|
||||
|
||||
Args:
|
||||
tens (torch.Tensor): Input tensor.
|
||||
dim (int): Dimension to remove if it is of size 1.
|
||||
|
||||
Returns:
|
||||
tuple: A tuple containing the transformed PyTorch tensor.
|
||||
"""
|
||||
tens = torch.squeeze(tens, dim)
|
||||
return (tens,)
|
||||
@@ -0,0 +1,44 @@
|
||||
from typing import Any, Dict
|
||||
import torch
|
||||
|
||||
|
||||
class PtUnsqueeze:
|
||||
"""
|
||||
Adds a singleton dimension at the specified position in the input tensor.
|
||||
For example, if a tensor has shape (3, 4) and `dim=1`, the output will have shape (3, 1, 4).
|
||||
|
||||
category: PyTorch wrapper - Transform
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls) -> Dict[str, Any]:
|
||||
"""
|
||||
Defines the input types for the function.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: A dictionary specifying required input types.
|
||||
"""
|
||||
return {
|
||||
"required": {
|
||||
"tens": ("TENSOR", {}),
|
||||
"dim": ("INT", {"default": -1, "min": -10, "max": 10})
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES: tuple = ("TENSOR",)
|
||||
FUNCTION: str = "f"
|
||||
CATEGORY: str = "Data Analysis"
|
||||
|
||||
def f(self, tens: torch.Tensor, dim: int) -> tuple:
|
||||
"""
|
||||
Adds a singleton dimension at the specified position.
|
||||
|
||||
Args:
|
||||
tens (torch.Tensor): Input tensor.
|
||||
dim (int): Dimension at which to insert the new singleton dimension.
|
||||
|
||||
Returns:
|
||||
tuple: A tuple containing the transformed PyTorch tensor.
|
||||
"""
|
||||
tens = torch.unsqueeze(tens, dim)
|
||||
return (tens,)
|
||||
@@ -0,0 +1,62 @@
|
||||
from typing import Any, Dict
|
||||
import torch
|
||||
import ast
|
||||
|
||||
|
||||
class PtView:
|
||||
"""
|
||||
Reshapes a PyTorch tensor into a specified shape using `torch.view()`.
|
||||
The total number of elements must remain unchanged.
|
||||
|
||||
For example, if a tensor has shape (2, 3, 4), and `new_shape="[6, 4]"`,
|
||||
the output will have shape (6, 4).
|
||||
|
||||
Use -1 to automatically infer a dimension, e.g.:
|
||||
```
|
||||
[2, -1]
|
||||
```
|
||||
category: PyTorch wrapper - Transform
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls) -> Dict[str, Any]:
|
||||
"""
|
||||
Defines the input types for the function.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: A dictionary specifying required input types.
|
||||
"""
|
||||
return {
|
||||
"required": {
|
||||
"tens": ("TENSOR", {}),
|
||||
"new_shape": ("STRING", {"multiline": True})
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES: tuple = ("TENSOR",)
|
||||
FUNCTION: str = "f"
|
||||
CATEGORY: str = "Data Analysis"
|
||||
|
||||
def f(self, tens: torch.Tensor, new_shape: str) -> tuple:
|
||||
"""
|
||||
Reshapes the input tensor based on the specified shape.
|
||||
|
||||
Args:
|
||||
tens (torch.Tensor): The input tensor.
|
||||
new_shape (str): A string representation of the desired shape,
|
||||
e.g., "[2, -1]" or "(6, 4)".
|
||||
|
||||
Returns:
|
||||
tuple: A tuple containing the reshaped PyTorch tensor.
|
||||
"""
|
||||
# Convert string to a tuple of integers
|
||||
shape_order = tuple(ast.literal_eval(new_shape))
|
||||
|
||||
# Ensure the parsed shape is valid
|
||||
if not isinstance(shape_order, tuple) or not all(isinstance(i, int) for i in shape_order):
|
||||
raise ValueError(f"Invalid format for new_shape: {new_shape}. Must be a tuple or list of integers.")
|
||||
|
||||
# Apply view operation
|
||||
tens = tens.view(*shape_order)
|
||||
|
||||
return (tens,)
|
||||
@@ -0,0 +1,52 @@
|
||||
from typing import Any, Dict
|
||||
import torch
|
||||
import ast
|
||||
from .utils import DTYPE_MAPPING
|
||||
|
||||
class PtZeros:
|
||||
"""
|
||||
Creates a PyTorch tensor of zeros using the size entered in the text field.
|
||||
For example, if you want to create a 4D tensor of batch size=2, channel=3, height=96, width=32, enter:
|
||||
[2,3,96,32]
|
||||
|
||||
category: PyTorch wrapper - Tensor creation
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls) -> Dict[str, Any]:
|
||||
"""
|
||||
Defines the input types for the function.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: A dictionary specifying required input types.
|
||||
"""
|
||||
return {
|
||||
"required": {
|
||||
"size": ("STRING", {"multiline": True}),
|
||||
"data_type": (("float32", "float16", "bfloat16", "float64", "uint8", "int8", "int16", "int32", "int64", "bool"),)
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES: tuple = ("TENSOR",)
|
||||
FUNCTION: str = "f"
|
||||
CATEGORY: str = "Data Analysis"
|
||||
|
||||
def f(self, size: str, data_type: str) -> tuple:
|
||||
"""
|
||||
Creates a PyTorch tensor of zeros using the size entered in the text field.
|
||||
|
||||
Args:
|
||||
size (str): String value in the text field.
|
||||
data_type (str): Desired data type of the tensor.
|
||||
|
||||
Returns:
|
||||
tuple: A tuple containing the PyTorch tensor.
|
||||
"""
|
||||
# Convert string to Python list
|
||||
list_size = ast.literal_eval(size)
|
||||
|
||||
# Convert list to PyTorch Size
|
||||
sz = torch.Size(list_size)
|
||||
tens = torch.zeros(sz, dtype=DTYPE_MAPPING[data_type])
|
||||
|
||||
return (tens,)
|
||||
@@ -0,0 +1,14 @@
|
||||
import torch
|
||||
|
||||
DTYPE_MAPPING = {
|
||||
"float32": torch.float32,
|
||||
"float16": torch.float16,
|
||||
"bfloat16": torch.bfloat16,
|
||||
"float64": torch.float64,
|
||||
"uint8": torch.uint8,
|
||||
"int8": torch.int8,
|
||||
"int16": torch.int16,
|
||||
"int32": torch.int32,
|
||||
"int64": torch.int64,
|
||||
"bool": torch.bool
|
||||
}
|
||||
Reference in New Issue
Block a user