Skip to content

Commit

Permalink
lintfixbot: Auto-commit fixed lint errors in codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
ivy-branch committed Jan 27, 2023
1 parent 089bb5d commit 4d0ead8
Show file tree
Hide file tree
Showing 13 changed files with 121 additions and 96 deletions.
33 changes: 18 additions & 15 deletions duplicate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,34 @@
import glob


def get_all_functions_from_directory(root_dir, startswith='test'):
def get_all_functions_from_directory(root_dir, startswith="test"):
if not os.path.exists(root_dir):
print('Invalid directory')
print("Invalid directory")
exit(1)
functions_names = []
for filename in glob.iglob(root_dir + '/**/*.py', recursive=True):
if len(filename) >= 2 and filename[:2] == './':
for filename in glob.iglob(root_dir + "/**/*.py", recursive=True):
if len(filename) >= 2 and filename[:2] == "./":
filename = filename[2:]
filename = filename.replace('.py', '')
filename = filename.replace('/', '.')
filename = filename.replace(".py", "")
filename = filename.replace("/", ".")
module = importlib.import_module(filename)
module_functions_names = \
[obj for obj in dir(module) if obj.startswith(startswith)]
module_functions_names = [
obj for obj in dir(module) if obj.startswith(startswith)
]
functions_names.extend(module_functions_names)
return functions_names


def check_duplicate():
fn_test_core = \
get_all_functions_from_directory('ivy_tests/test_ivy/test_functional/test_core')
fn_test_nn = \
get_all_functions_from_directory('ivy_tests/test_ivy/test_functional/test_nn')
fn_test_experimental = \
get_all_functions_from_directory(
'ivy_tests/test_ivy/test_functional/test_experimental')
fn_test_core = get_all_functions_from_directory(
"ivy_tests/test_ivy/test_functional/test_core"
)
fn_test_nn = get_all_functions_from_directory(
"ivy_tests/test_ivy/test_functional/test_nn"
)
fn_test_experimental = get_all_functions_from_directory(
"ivy_tests/test_ivy/test_functional/test_experimental"
)
fn_ivy_test = set(fn_test_core).union(set(fn_test_nn))
common_list = fn_ivy_test.intersection(set(fn_test_experimental))
return common_list
Expand Down
17 changes: 7 additions & 10 deletions ivy/functional/backends/jax/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,17 @@ def _transpose_padding_helper(k, s, padding, dilation, diff=0):


def _get_tranpose_padding(
x_shape, filter_shape, strides, padding, dims, dilations, output_shape
x_shape, filter_shape, strides, padding, dims, dilations, output_shape
):
new_shape = [
_deconv_length(
x_shape[i], strides[i], filter_shape[i], padding, dilations[i]
)
_deconv_length(x_shape[i], strides[i], filter_shape[i], padding, dilations[i])
for i in range(dims)
]
if output_shape is None:
output_shape = [x_shape[0], *new_shape, filter_shape[-1]]
elif len(output_shape) == dims:
output_shape = [x_shape[0]] + list(output_shape) + [filter_shape[-1]]
shape_diff = [
-(output_shape[1 + i] - new_shape[i])
for i in range(dims)
]
shape_diff = [-(output_shape[1 + i] - new_shape[i]) for i in range(dims)]
pad_list = [
_transpose_padding_helper(
filter_shape[i], strides[i], padding, dilations[i], shape_diff[i]
Expand Down Expand Up @@ -302,7 +297,9 @@ def conv_general_dilated(
new_pad[i] = _handle_padding(
x_shape[i], strides[i], filter_shape[i], padding
)
padding = [(new_pad[i] // 2, new_pad[i] - new_pad[i] // 2) for i in range(dims)]
padding = [
(new_pad[i] // 2, new_pad[i] - new_pad[i] // 2) for i in range(dims)
]
df = _get_x_data_format(dims, data_format)
res = jlax.conv_general_dilated(
x,
Expand Down Expand Up @@ -345,7 +342,7 @@ def conv_general_transpose(
filter_df = _get_filter_dataformat(dims)
if data_format == "channel_first":
x = jnp.transpose(x, (0, *range(2, dims + 2), 1))
x_shape = list(x.shape[1: dims + 1])
x_shape = list(x.shape[1 : dims + 1])
padding = _get_tranpose_padding(
x_shape, filters.shape, strides, padding, dims, dilations, output_shape
)
Expand Down
57 changes: 33 additions & 24 deletions ivy/functional/backends/numpy/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,15 @@ def _dilate_pad_conv(x, filters, strides, padding, dims, dilations):
return x, filters


def _dilate_pad_conv_tranpose(x, filters, strides, padding, dims, dilations, output_shape):
def _dilate_pad_conv_tranpose(
x, filters, strides, padding, dims, dilations, output_shape
):
strides = [strides] * dims if isinstance(strides, int) else strides
dilations = [dilations] * dims if isinstance(dilations, int) else dilations
if output_shape is None:
new_shape = [
_deconv_length(
x.shape[i + 1], strides[i], filters.shape[i], padding,
dilations[i]
x.shape[i + 1], strides[i], filters.shape[i], padding, dilations[i]
)
for i in range(dims)
]
Expand All @@ -66,7 +67,7 @@ def _dilate_pad_conv_tranpose(x, filters, strides, padding, dims, dilations, out
output_shape = [x.shape[0]] + list(output_shape) + [filters.shape[-1]]
for i in reversed(range(dims)):
if strides[i] > 1:
x = _add_dilations(x, strides[i], axis=i+1)
x = _add_dilations(x, strides[i], axis=i + 1)
if dilations[i] > 1:
filters = _add_dilations(filters, dilations[i], axis=i)
pad_specific = [
Expand All @@ -81,18 +82,12 @@ def _dilate_pad_conv_tranpose(x, filters, strides, padding, dims, dilations, out
)
for i in range(dims)
]
pad_top = [
filters.shape[i] - 1 - (pad_specific[i] // 2)
for i in range(dims)
]
pad_top = [filters.shape[i] - 1 - (pad_specific[i] // 2) for i in range(dims)]
pad_bot = [
filters.shape[i] - 1 - (pad_specific[i] - pad_specific[i] // 2)
for i in range(dims)
]
pad_list = [
(pad_top[i], pad_bot[i] + extra_pad[i])
for i in range(dims)
]
pad_list = [(pad_top[i], pad_bot[i] + extra_pad[i]) for i in range(dims)]
x = np.pad(
x,
[
Expand Down Expand Up @@ -169,11 +164,13 @@ def conv1d_transpose(
) -> np.ndarray:
if data_format == "NCW":
x = np.transpose(x, (0, 2, 1))
x, filters = _dilate_pad_conv_tranpose(x, filters, strides, padding, 1, dilations, output_shape)
x = np.flip(x, (1, ))
x, filters = _dilate_pad_conv_tranpose(
x, filters, strides, padding, 1, dilations, output_shape
)
x = np.flip(x, (1,))
res = np.flip(
conv1d(x, filters, 1, "VALID", data_format="NWC", dilations=1),
(1, ),
(1,),
)
if data_format == "NCW":
res = np.transpose(res, (0, 2, 1))
Expand All @@ -191,8 +188,8 @@ def conv2d(
dilations: Optional[Union[int, Tuple[int, int]]] = 1,
out: Optional[np.ndarray] = None,
) -> np.ndarray:
strides = [strides]*2 if isinstance(strides, int) else strides
dilations = [dilations]*2 if isinstance(dilations, int) else dilations
strides = [strides] * 2 if isinstance(strides, int) else strides
dilations = [dilations] * 2 if isinstance(dilations, int) else dilations
if data_format == "NCHW":
x = np.transpose(x, (0, 2, 3, 1))

Expand Down Expand Up @@ -247,7 +244,9 @@ def conv2d_transpose(
):
if data_format == "NCHW":
x = np.transpose(x, (0, 2, 3, 1))
x, filters = _dilate_pad_conv_tranpose(x, filters, strides, padding, 2, dilations, output_shape)
x, filters = _dilate_pad_conv_tranpose(
x, filters, strides, padding, 2, dilations, output_shape
)
x = np.flip(x, (1, 2))
res = np.flip(
conv2d(x, filters, 1, "VALID", data_format="NHWC", dilations=1),
Expand Down Expand Up @@ -288,8 +287,14 @@ def depthwise_conv2d(
out_height = np.ceil(float(x.shape[2]) / float(strides[0]))
out_width = np.ceil(float(x.shape[3]) / float(strides[1]))
else:
out_height = np.ceil(float(x.shape[2] - filter_h + padding[0][0] + padding[0][1] + 1) / float(strides[0]))
out_width = np.ceil(float(x.shape[3] - filter_w + padding[1][0] + padding[1][1] + 1) / float(strides[1]))
out_height = np.ceil(
float(x.shape[2] - filter_h + padding[0][0] + padding[0][1] + 1)
/ float(strides[0])
)
out_width = np.ceil(
float(x.shape[3] - filter_w + padding[1][0] + padding[1][1] + 1)
/ float(strides[1])
)
if data_format == "NHWC":
outputs = np.empty([x.shape[1], int(out_height), int(out_width), 0], x.dtype)
else:
Expand Down Expand Up @@ -317,8 +322,8 @@ def conv3d(
dilations: Optional[Union[int, Tuple[int, int, int]]] = 1,
out: Optional[np.ndarray] = None,
) -> np.ndarray:
strides = [strides]*3 if isinstance(strides, int) else strides
dilations = [dilations]*3 if isinstance(dilations, int) else dilations
strides = [strides] * 3 if isinstance(strides, int) else strides
dilations = [dilations] * 3 if isinstance(dilations, int) else dilations
if data_format == "NCDHW":
x = np.transpose(x, (0, 2, 3, 4, 1))

Expand Down Expand Up @@ -376,7 +381,9 @@ def conv3d_transpose(
):
if data_format == "NCDHW":
x = np.transpose(x, (0, 2, 3, 4, 1))
x, filters = _dilate_pad_conv_tranpose(x, filters, strides, padding, 3, dilations, output_shape)
x, filters = _dilate_pad_conv_tranpose(
x, filters, strides, padding, 3, dilations, output_shape
)
x = np.flip(x, (1, 2, 3))
res = np.flip(
conv3d(x, filters, 1, "VALID", data_format="NDHWC", dilations=1),
Expand Down Expand Up @@ -484,7 +491,9 @@ def conv_general_transpose(
if data_format == "channel_first":
x = np.transpose(x, (0, *range(2, dims + 2), 1))

x, filters = _dilate_pad_conv_tranpose(x, filters, strides, padding, dims, dilations, output_shape)
x, filters = _dilate_pad_conv_tranpose(
x, filters, strides, padding, dims, dilations, output_shape
)

x = np.flip(x, (*range(1, dims + 1),))
res = np.concatenate(
Expand Down
22 changes: 14 additions & 8 deletions ivy/functional/backends/tensorflow/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
import ivy
from ivy.func_wrapper import with_unsupported_dtypes
from . import backend_version
from ivy.functional.ivy.layers import _deconv_length, _get_x_data_format, \
_handle_padding
from ivy.functional.ivy.layers import (
_deconv_length,
_get_x_data_format,
_handle_padding,
)


def _pad_before_conv(x, filters, strides, padding, dims, dilations):
Expand All @@ -24,10 +27,12 @@ def _pad_before_conv(x, filters, strides, padding, dims, dilations):
for i in range(dims)
]
new_pad = [
_handle_padding(x.shape[1+i], strides[i], filter_shape[i], padding)
_handle_padding(x.shape[1 + i], strides[i], filter_shape[i], padding)
for i in range(dims)
]
pad_list = [(new_pad[i] // 2, new_pad[i] - new_pad[i] // 2) for i in range(dims)]
pad_list = [
(new_pad[i] // 2, new_pad[i] - new_pad[i] // 2) for i in range(dims)
]
else:
pad_list = padding
return tf.pad(
Expand All @@ -41,7 +46,9 @@ def _pad_before_conv(x, filters, strides, padding, dims, dilations):
)


def _output_shape(x_shape, filter_shape, output_shape, strides, padding, dims, dilations):
def _output_shape(
x_shape, filter_shape, output_shape, strides, padding, dims, dilations
):
dilations = [dilations] * dims if isinstance(dilations, int) else dilations
strides = [strides] * dims if isinstance(strides, int) else strides
if output_shape is None:
Expand Down Expand Up @@ -283,7 +290,7 @@ def conv_general_dilated(
x_dilations = [x_dilations] * dims if isinstance(x_dilations, int) else x_dilations
for i in range(dims):
if x_dilations[i] > 1:
h = x.shape[1+i]
h = x.shape[1 + i]
new_height = h + (h - 1) * (x_dilations[i] - 1)
h = tf.eye(new_height, dtype=x.dtype)[:: x_dilations[i]]
x = tf.experimental.numpy.swapaxes(x, 1 + i, -1)
Expand All @@ -295,8 +302,7 @@ def conv_general_dilated(
if dims == 3:
strides = [1] + ([strides] * 3 if isinstance(strides, int) else strides) + [1]
dilations = (
[1] + ([dilations] * 3 if isinstance(dilations, int) else dilations) +
[1]
[1] + ([dilations] * 3 if isinstance(dilations, int) else dilations) + [1]
)
if dims == 1:
res = tf.concat(
Expand Down
13 changes: 9 additions & 4 deletions ivy/functional/backends/torch/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def _pad_before_conv(x, filters, strides, padding, dims, dilations):
for i in range(dims)
]
pad_specific = [
_handle_padding(x.shape[2+i], strides[i], filter_shape[i], padding)
_handle_padding(x.shape[2 + i], strides[i], filter_shape[i], padding)
for i in range(dims - 1, -1, -1)
]
pad_list_top = [pad_specific[i] // 2 for i in range(dims)]
Expand All @@ -34,7 +34,9 @@ def _pad_before_conv(x, filters, strides, padding, dims, dilations):
return torch.nn.functional.pad(x, pad_list)


def _pad_before_conv_tranpose(x, filters, strides, padding, dims, dilations, output_shape, filter_shape):
def _pad_before_conv_tranpose(
x, filters, strides, padding, dims, dilations, output_shape, filter_shape
):
if output_shape is None:
out_shape = [
_deconv_length(
Expand Down Expand Up @@ -63,7 +65,10 @@ def _pad_before_conv_tranpose(x, filters, strides, padding, dims, dilations, out
not_valid_pad[i] = True
padding_list = [pad_specific[i] // 2 for i in range(dims)]
out_shape = [
(x.shape[i + 2] - 1) * strides[i] - 2 * padding_list[i] + dilations[i] * (filters.shape[i + 2] - 1) + 1
(x.shape[i + 2] - 1) * strides[i]
- 2 * padding_list[i]
+ dilations[i] * (filters.shape[i + 2] - 1)
+ 1
for i in range(dims)
]
output_padding = [max(output_shape[i + 1] - out_shape[i], 0) for i in range(dims)]
Expand Down Expand Up @@ -361,7 +366,7 @@ def conv_general_dilated(
x_dilations = [x_dilations] * dims if isinstance(x_dilations, int) else x_dilations
for i in range(dims):
if x_dilations[i] > 1:
h = x.shape[2+i]
h = x.shape[2 + i]
new_height = h + (h - 1) * (x_dilations[i] - 1)
h = torch.eye(new_height, dtype=x.dtype)[:: x_dilations[i]]
x = torch.swapaxes(x, 2 + i, -1)
Expand Down
8 changes: 4 additions & 4 deletions ivy/functional/frontends/jax/lax/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def conv_transpose(
if preferred_element_type:
lhs = ivy.astype(lhs, preferred_element_type)
rhs = ivy.astype(rhs, preferred_element_type)
if dimension_numbers[1][-1] == 'O':
if dimension_numbers[1][-1] == "O":
rhs = ivy.swapaxes(rhs, -1, -2)
else:
rhs = ivy.swapaxes(rhs, 0, 1)
Expand All @@ -158,7 +158,7 @@ def conv_transpose(
_format_rhs(rhs, dimension_numbers),
strides,
padding,
dims=len(lhs.shape)-2,
dims=len(lhs.shape) - 2,
data_format=_get_general_df(dimension_numbers[0]),
dilations=1 if rhs_dilation is None else rhs_dilation,
)
Expand All @@ -176,7 +176,7 @@ def conv_general_dilated(
feature_group_count=1,
batch_group_count=1,
precision=None,
preferred_element_type=None
preferred_element_type=None,
):
# TODO: add support for batch_group_count
if preferred_element_type:
Expand All @@ -187,7 +187,7 @@ def conv_general_dilated(
_format_rhs(rhs, dimension_numbers),
window_strides,
padding,
dims=len(lhs.shape)-2,
dims=len(lhs.shape) - 2,
data_format=_get_general_df(dimension_numbers[0]),
x_dilations=1 if lhs_dilation is None else lhs_dilation,
dilations=1 if rhs_dilation is None else rhs_dilation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,5 @@ def sort_complex(a):

# All backends need to be implemented for this function.
@to_ivy_arrays_and_back
def lexsort(
keys,
/,
*,
axis=-1
):
def lexsort(keys, /, *, axis=-1):
return ivy.lexsort(keys, axis=axis)
4 changes: 3 additions & 1 deletion ivy/functional/frontends/tensorflow/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ def uniform(shape, minval=0, maxval=None, dtype=ivy.float32, seed=None, name=Non
)


@with_unsupported_dtypes({"2.9.0 and below": ("int8", "int16", "int32", "int64", "unsigned")}, "tensorflow")
@with_unsupported_dtypes(
{"2.9.0 and below": ("int8", "int16", "int32", "int64", "unsigned")}, "tensorflow"
)
@to_ivy_arrays_and_back
def normal(shape, mean=0.0, stddev=1.0, dtype=ivy.float32, seed=None, name=None):
return ivy.random_normal(mean=mean, std=stddev, shape=shape, dtype=dtype, seed=seed)
Loading

0 comments on commit 4d0ead8

Please sign in to comment.