Added Pt Arange and Pt Linspace nodes.
This commit is contained in:
@@ -1,4 +1,39 @@
|
||||
from typing import Dict, Any
|
||||
from typing import Optional, Tuple, Union
|
||||
|
||||
def str_to_number(s: str) -> Union[int, float]:
|
||||
"""
|
||||
Converts a string representation of a number to an integer or float.
|
||||
|
||||
Args:
|
||||
s (str): The string to convert.
|
||||
|
||||
Returns:
|
||||
Union[int, float]: The converted number, either as an `int` if no decimal point is present or as a `float` otherwise.
|
||||
|
||||
Raises:
|
||||
ValueError: If the input string is empty.
|
||||
"""
|
||||
if s:
|
||||
if "." in s:
|
||||
return float(s)
|
||||
return int(s)
|
||||
raise ValueError("Input string cannot be empty.")
|
||||
|
||||
|
||||
def str_to_number_with_default(s: str, n: Union[int, float]) -> Union[int, float]:
|
||||
"""
|
||||
Converts a string representation of a number to an integer or float, with a fallback default value.
|
||||
|
||||
Args:
|
||||
s (str): The string to convert.
|
||||
n (Union[int, float]): The default value to return if `s` is empty.
|
||||
|
||||
Returns:
|
||||
Union[int, float]: The converted number if `s` is non-empty, otherwise `n`.
|
||||
"""
|
||||
return str_to_number(s) if s else n
|
||||
|
||||
|
||||
def python_dict_first_value_len(d: Dict[Any, Any]) -> int:
|
||||
r"""
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
import os
|
||||
import sys
|
||||
from typing import Any, Dict, Tuple
|
||||
import torch
|
||||
from .utils import DTYPE_MAPPING
|
||||
|
||||
PROJECT_ROOT = os.path.realpath(os.path.join(os.path.dirname(__file__), "..", ".."))
|
||||
MODULE_ROOT = os.path.join(PROJECT_ROOT, "modules")
|
||||
sys.path.append(MODULE_ROOT)
|
||||
from cda.utils import str_to_number, str_to_number_with_default
|
||||
|
||||
|
||||
class PtArange:
|
||||
"""
|
||||
Creates a PyTorch tensor using `torch.arange` with the specified start, end, and step values.
|
||||
|
||||
The values are parsed from string inputs and converted to numeric types. This allows users to specify
|
||||
tensor range values in text fields. The function also ensures proper dtype selection.
|
||||
|
||||
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": {
|
||||
"start": ("STRING", {"default":"", "multiline": False}),
|
||||
"end": ("STRING", {"default":"", "multiline": False}),
|
||||
"step": ("STRING", {"default":"", "multiline": False}),
|
||||
"data_type": (("float32", "float16", "bfloat16", "float64", "uint8", "int8", "int16", "int32", "int64"),)
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES: tuple = ("TENSOR",)
|
||||
FUNCTION: str = "f"
|
||||
CATEGORY: str = "Data Analysis"
|
||||
|
||||
|
||||
def f(self, start: str, end: str, step: str, data_type: str) -> Tuple[torch.Tensor]:
|
||||
"""
|
||||
Creates a PyTorch tensor using `torch.arange` with the given start, stop, and step values.
|
||||
|
||||
Args:
|
||||
start (str): The starting value of the sequence. If empty, defaults to 0.
|
||||
end (str): The end value of the sequence (exclusive).
|
||||
step (str): The step size for generating the sequence. If empty, defaults to 1.
|
||||
data_type (str): The desired PyTorch dtype for the resulting tensor.
|
||||
|
||||
Returns:
|
||||
Tuple[torch.Tensor]: A tuple containing the generated PyTorch tensor.
|
||||
"""
|
||||
start_value = str_to_number_with_default(start, 0)
|
||||
end_value = str_to_number(end)
|
||||
step_value = str_to_number_with_default(step, 1)
|
||||
tens = torch.arange(start_value, end_value, step_value, dtype=DTYPE_MAPPING[data_type])
|
||||
return (tens,)
|
||||
@@ -0,0 +1,62 @@
|
||||
import os
|
||||
import sys
|
||||
from typing import Any, Dict, Tuple
|
||||
import torch
|
||||
from .utils import DTYPE_MAPPING
|
||||
|
||||
PROJECT_ROOT = os.path.realpath(os.path.join(os.path.dirname(__file__), "..", ".."))
|
||||
MODULE_ROOT = os.path.join(PROJECT_ROOT, "modules")
|
||||
sys.path.append(MODULE_ROOT)
|
||||
from cda.utils import str_to_number, str_to_number_with_default
|
||||
|
||||
|
||||
class PtLinspace:
|
||||
"""
|
||||
Creates a PyTorch tensor using `torch.arange` with the specified start, end, and steps values.
|
||||
|
||||
The start and end values are parsed from string inputs and converted to a float.
|
||||
The steps value is converted to an int.
|
||||
|
||||
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": {
|
||||
"start": ("STRING", {"default":"", "multiline": False}),
|
||||
"end": ("STRING", {"default":"", "multiline": False}),
|
||||
"steps": ("STRING", {"default":"", "multiline": False}),
|
||||
"data_type": (("float32", "float16", "bfloat16", "float64", "uint8", "int8", "int16", "int32", "int64"),)
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES: tuple = ("TENSOR",)
|
||||
FUNCTION: str = "f"
|
||||
CATEGORY: str = "Data Analysis"
|
||||
|
||||
|
||||
def f(self, start: str, end: str, steps: str, data_type: str) -> Tuple[torch.Tensor]:
|
||||
"""
|
||||
Creates a PyTorch tensor using `torch.arange` with the given start, end, and step values.
|
||||
|
||||
Args:
|
||||
start (str): The starting value of the sequence.
|
||||
end (str): The end value of the sequence (inclusive).
|
||||
steps (str): The number of steps
|
||||
data_type (str): The desired PyTorch dtype for the resulting tensor.
|
||||
|
||||
Returns:
|
||||
Tuple[torch.Tensor]: A tuple containing the generated PyTorch tensor.
|
||||
"""
|
||||
start_value = float(start)
|
||||
end_value = float(end)
|
||||
steps_value = int(steps)
|
||||
tens = torch.linspace(start_value, end_value, steps_value, dtype=DTYPE_MAPPING[data_type])
|
||||
return (tens,)
|
||||
Reference in New Issue
Block a user