Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added the tensor operations for slicing, mutations, joinings and indexing #26558

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion determine_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pydriller import Repository
import os # noqa
import bz2
import _pickle as cPickle
import pickle as cPickle
import sys
from run_tests_CLI.get_all_tests import get_all_tests

Expand Down
26 changes: 26 additions & 0 deletions ivy/data_classes/array/experimental/manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1080,3 +1080,29 @@ def fill_diagonal(
ivy.fill_diag also applies to this method with minimal changes.
"""
return ivy.fill_diagonal(self._data, v, wrap=wrap)


def tensor_ops(tensor, operation_type, *args):
if operation_type == "index":
if len(args) != 1:
raise ValueError
index = args[0]
return tensor[index]
elif operation_type == "slice":
if len(args) != 2:
raise ValueError
start, end = args
return tensor[start:end]
elif operation_type == "join":
if len(args) != 1:
raise ValueError
other_tensor = args[0]
return ivy.cat((tensor, other_tensor), dim=0)
elif operation_type == "mutate":
if len(args) != 1:
raise ValueError
factor = args[0]
tensor.mul_(factor)
return tensor
else:
raise ValueError
27 changes: 27 additions & 0 deletions ivy/data_classes/container/experimental/manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2996,3 +2996,30 @@ def fill_diagonal(
v,
wrap=wrap,
)


@staticmethod
def tensor_ops(tensor, operation_type, *args):
if operation_type == "index":
if len(args) != 1:
raise ValueError
index = args[0]
return tensor[index]
elif operation_type == "slice":
if len(args) != 2:
raise ValueError
start, end = args
return tensor[start:end]
elif operation_type == "join":
if len(args) != 1:
raise ValueError
other_tensor = args[0]
return ivy.cat((tensor, other_tensor), dim=0)
elif operation_type == "mutate":
if len(args) != 1:
raise ValueError
factor = args[0]
tensor.mul_(factor)
return tensor
else:
raise ValueError
26 changes: 26 additions & 0 deletions ivy/functional/backends/jax/experimental/manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,3 +416,29 @@ def fill_diagonal(
a = a.at[:end:step].set(jnp.array(v).astype(a.dtype))
a = jnp.reshape(a, shape)
return a


def tensor_ops(tensor, operation_type, *args):
if operation_type == "index":
if len(args) != 1:
raise ValueError
index = args[0]
return tensor[index]
elif operation_type == "slice":
if len(args) != 2:
raise ValueError
start, end = args
return tensor[start:end]
elif operation_type == "join":
if len(args) != 1:
raise ValueError
other_tensor = args[0]
return ivy.cat((tensor, other_tensor), dim=0)
elif operation_type == "mutate":
if len(args) != 1:
raise ValueError
factor = args[0]
tensor.mul_(factor)
return tensor
else:
raise ValueError
26 changes: 26 additions & 0 deletions ivy/functional/backends/numpy/experimental/manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,3 +505,29 @@ def fill_diagonal(
) -> np.ndarray:
np.fill_diagonal(a, v, wrap=wrap)
return a


def tensor_ops(tensor, operation_type, *args):
if operation_type == "index":
if len(args) != 1:
raise ValueError
index = args[0]
return tensor[index]
elif operation_type == "slice":
if len(args) != 2:
raise ValueError
start, end = args
return tensor[start:end]
elif operation_type == "join":
if len(args) != 1:
raise ValueError
other_tensor = args[0]
return ivy.cat((tensor, other_tensor), dim=0)
elif operation_type == "mutate":
if len(args) != 1:
raise ValueError
factor = args[0]
tensor.mul_(factor)
return tensor
else:
raise ValueError
26 changes: 26 additions & 0 deletions ivy/functional/backends/tensorflow/experimental/manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,3 +367,29 @@ def fill_diagonal(
a = tf.tensor_scatter_nd_update(a, indices, ups)
a = tf.reshape(a, shape)
return a


def tensor_ops(tensor, operation_type, *args):
if operation_type == "index":
if len(args) != 1:
raise ValueError
index = args[0]
return tensor[index]
elif operation_type == "slice":
if len(args) != 2:
raise ValueError
start, end = args
return tensor[start:end]
elif operation_type == "join":
if len(args) != 1:
raise ValueError
other_tensor = args[0]
return ivy.cat((tensor, other_tensor), dim=0)
elif operation_type == "mutate":
if len(args) != 1:
raise ValueError
factor = args[0]
tensor.mul_(factor)
return tensor
else:
raise ValueError
26 changes: 26 additions & 0 deletions ivy/functional/backends/torch/experimental/manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,3 +395,29 @@ def fill_diagonal(
a = torch.where(w, v, a)
a = torch.reshape(a, shape)
return a


def tensor_ops(tensor, operation_type, *args):
if operation_type == "index":
if len(args) != 1:
raise ValueError
index = args[0]
return tensor[index]
elif operation_type == "slice":
if len(args) != 2:
raise ValueError
start, end = args
return tensor[start:end]
elif operation_type == "join":
if len(args) != 1:
raise ValueError
other_tensor = args[0]
return ivy.cat((tensor, other_tensor), dim=0)
elif operation_type == "mutate":
if len(args) != 1:
raise ValueError
factor = args[0]
tensor.mul_(factor)
return tensor
else:
raise ValueError
16 changes: 16 additions & 0 deletions ivy/functional/frontends/paddle/tensor/linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,19 @@ def bincount(x, weights=None, minlength=0, name=None):
def dist(x, y, p=2):
ret = ivy.vector_norm(ivy.subtract(x, y), ord=p)
return ivy.reshape(ret, (1,))


@with_supported_dtypes({"2.4.1 and above": ("int64",)}, "paddle")
@to_ivy_arrays_and_back
def linear_algebra_histogram(d, num_bins):
min_value = ivy.min(d)
max_value = ivy.max(d)
bin_width = (max_value - min_value) / num_bins
bin_edges = ivy.arange(min_value, max_value + bin_width, bin_width)

# Count the number of values in each bin.
bin_counts = ivy.zeros(num_bins)
for value in d:
bin_index = ivy.searchsorted(bin_edges, value)
bin_counts[bin_index] += 1
return ivy.bin_counts
Original file line number Diff line number Diff line change
Expand Up @@ -366,3 +366,30 @@ def select(input, dim, index):
slices = [slice(None)] * num_dims
slices[dim] = index
return input[tuple(slices)]


@to_ivy_arrays_and_back
def tensor_ops(tensor, operation_type, *args):
if operation_type == "index":
if len(args) != 1:
raise ValueError
index = args[0]
return tensor[index]
elif operation_type == "slice":
if len(args) != 2:
raise ValueError
start, end = args
return tensor[start:end]
elif operation_type == "join":
if len(args) != 1:
raise ValueError
other_tensor = args[0]
return ivy.cat((tensor, other_tensor), dim=0)
elif operation_type == "mutate":
if len(args) != 1:
raise ValueError
factor = args[0]
tensor.mul_(factor)
return tensor
else:
raise ValueError
26 changes: 26 additions & 0 deletions ivy/functional/ivy/experimental/manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2152,3 +2152,29 @@ def fill_diagonal(
Array with the diagonal filled.
"""
return ivy.current_backend(a).fill_diag(a, v, wrap=wrap)


def tensor_ops(tensor, operation_type, *args):
if operation_type == "index":
if len(args) != 1:
raise ValueError
index = args[0]
return tensor[index]
elif operation_type == "slice":
if len(args) != 2:
raise ValueError
start, end = args
return tensor[start:end]
elif operation_type == "join":
if len(args) != 1:
raise ValueError
other_tensor = args[0]
return ivy.cat((tensor, other_tensor), dim=0)
elif operation_type == "mutate":
if len(args) != 1:
raise ValueError
factor = args[0]
tensor.mul_(factor)
return tensor
else:
raise ValueError
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
fn_tree="numpy.fft.ifft",
dtype_and_x=x_and_ifft(),
)
def test_numpy_iftt(dtype_and_x, backend_fw, frontend, test_flags, fn_tree, on_device):
def test_numpy_ifft(dtype_and_x, backend_fw, frontend, test_flags, fn_tree, on_device):
input_dtype, x, dim, norm, n = dtype_and_x
helpers.test_frontend_function(
input_dtypes=input_dtype,
Expand All @@ -39,7 +39,7 @@ def test_numpy_iftt(dtype_and_x, backend_fw, frontend, test_flags, fn_tree, on_d
available_dtypes=helpers.get_dtypes("float"), shape=(4,), array_api_dtypes=True
),
)
def test_numpy_ifttshift(
def test_numpy_ifftshift(
dtype_and_x, backend_fw, frontend, test_flags, fn_tree, on_device
):
input_dtype, arr = dtype_and_x
Expand Down Expand Up @@ -92,9 +92,7 @@ def test_numpy_fft(
available_dtypes=helpers.get_dtypes("float"), shape=(4,), array_api_dtypes=True
),
)
def test_numpy_fttshift(
dtype_and_x, backend_fw, frontend, test_flags, fn_tree, on_device
):
def fttshift(dtype_and_x, backend_fw, frontend, test_flags, fn_tree, on_device):
input_dtype, arr = dtype_and_x
helpers.test_frontend_function(
input_dtypes=input_dtype,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1593,3 +1593,29 @@ def test_torch_select(
dim=axis,
index=idx,
)


def tensor_ops(tensor, operation_type, *args):
if operation_type == "index":
if len(args) != 1:
raise ValueError
index = args[0]
return tensor[index]
elif operation_type == "slice":
if len(args) != 2:
raise ValueError
start, end = args
return tensor[start:end]
elif operation_type == "join":
if len(args) != 1:
raise ValueError
other_tensor = args[0]
return tensor.cat((tensor, other_tensor), dim=0)
elif operation_type == "mutate":
if len(args) != 1:
raise ValueError
factor = args[0]
tensor.mul_(factor)
return tensor
else:
raise ValueError
Loading