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

Support list and tuple for args. #32344

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion python/paddle/distributed/collective.py
Original file line number Diff line number Diff line change
Expand Up @@ -871,10 +871,12 @@ def split(x,
import paddle
from paddle.distributed import init_parallel_env

# required: gpu

paddle.set_device('gpu:%d'%paddle.distributed.ParallelEnv().dev_id)
init_parallel_env()
data = paddle.randint(0, 8, shape=[10,4])
emb_out = padle.distributed.split(
emb_out = paddle.distributed.split(
data,
(8, 8),
operation="embedding",
Expand Down
55 changes: 55 additions & 0 deletions python/paddle/fluid/tests/unittests/test_layer_norm_op_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,60 @@ def compute_v2(x_np):
self.assertTrue(np.allclose(y1, y2))


class TestLayerNormFunction(unittest.TestCase):
def test_dygraph(self):
places = [fluid.CPUPlace()]
if core.is_compiled_with_cuda() and core.op_support_gpu("layer_norm"):
places.append(fluid.CUDAPlace(0))
for p in places:
shape = [4, 10, 4, 4]

def compute_v0(x):
with fluid.dygraph.guard(p):
ln = fluid.dygraph.LayerNorm(shape[1:])
y = ln(fluid.dygraph.to_variable(x))
return y.numpy()

def compute_v1(x):
with fluid.dygraph.guard(p):
x = fluid.dygraph.to_variable(x)
y = paddle.nn.functional.layer_norm(x, shape[1:])
return y.numpy()

def compute_v2(x):
with fluid.dygraph.guard(p):
x = fluid.dygraph.to_variable(x)
y = paddle.nn.functional.layer_norm(x, tuple(shape[1:]))
return y.numpy()

def compute_v3(x):
with fluid.dygraph.guard(p):
ln = fluid.dygraph.LayerNorm(shape[-1])
y = ln(fluid.dygraph.to_variable(x))
return y.numpy()

def compute_v4(x):
with fluid.dygraph.guard(p):
x = fluid.dygraph.to_variable(x)
y = paddle.nn.functional.layer_norm(x, shape[-1])
return y.numpy()

x = np.random.randn(*shape).astype("float32")
y0 = compute_v0(x)
y1 = compute_v1(x)
y2 = compute_v2(x)
self.assertTrue(np.allclose(y0, y1))
self.assertTrue(np.allclose(y0, y2))
y3 = compute_v3(x)
y4 = compute_v4(x)
self.assertTrue(np.allclose(y3, y4))

self.assertRaises(
ValueError,
paddle.nn.functional.layer_norm,
x=x,
normalized_shape=1.0)


if __name__ == '__main__':
unittest.main()
10 changes: 10 additions & 0 deletions python/paddle/nn/functional/norm.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
from ...fluid.param_attr import ParamAttr
from ...fluid import core, dygraph_utils

import numbers

__all__ = [
'batch_norm',
# 'data_norm',
Expand Down Expand Up @@ -286,6 +288,14 @@ def layer_norm(x,
"""
input_shape = list(x.shape)
input_ndim = len(input_shape)
if isinstance(normalized_shape, numbers.Integral):
normalized_shape = [normalized_shape]
elif isinstance(normalized_shape, tuple):
normalized_shape = list(normalized_shape)
elif not isinstance(normalized_shape, list):
raise ValueError(
"`normalized_shape` should be int, list of ints or tuple of ints.")

normalized_ndim = len(normalized_shape)
begin_norm_axis = input_ndim - normalized_ndim
if input_ndim < normalized_ndim or input_shape[
Expand Down
3 changes: 2 additions & 1 deletion python/paddle/reader/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,8 @@ def _impl():
sys.stderr.write("import ujson error: " + str(e) + " use json\n")
import json

assert type(readers) is list and len(readers) > 0
assert isinstance(readers, (list, tuple)) and len(readers) > 0, (
"`readers` must be list or tuple.")

def _read_into_queue(reader, queue):
try:
Expand Down
9 changes: 6 additions & 3 deletions python/paddle/vision/datasets/folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ def has_valid_extension(filename, extensions):

Args:
filename (str): path to a file
extensions (tuple of str): extensions to consider (lowercase)
extensions (list[str]|tuple[str]): extensions to consider

Returns:
bool: True if the filename ends with one of given extensions
"""
assert isinstance(extensions,
(list, tuple)), ("`extensions` must be list or tuple.")
extensions = tuple([x.lower() for x in extensions])
return filename.lower().endswith(extensions)


Expand Down Expand Up @@ -73,7 +76,7 @@ class DatasetFolder(Dataset):
Args:
root (string): Root directory path.
loader (callable|optional): A function to load a sample given its path.
extensions (tuple[str]|optional): A list of allowed extensions.
extensions (list[str]|tuple[str]|optional): A list of allowed extensions.
both extensions and is_valid_file should not be passed.
transform (callable|optional): A function/transform that takes in
a sample and returns a transformed version.
Expand Down Expand Up @@ -226,7 +229,7 @@ class ImageFolder(Dataset):
Args:
root (string): Root directory path.
loader (callable, optional): A function to load a sample given its path.
extensions (tuple[string], optional): A list of allowed extensions.
extensions (list[str]|tuple[str], optional): A list of allowed extensions.
both extensions and is_valid_file should not be passed.
transform (callable, optional): A function/transform that takes in
a sample and returns a transformed version.
Expand Down
2 changes: 1 addition & 1 deletion python/paddle/vision/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ def yolo_box(x,
import paddle
import numpy as np

x = np.random.random([2, 14, 8, 8]).astype('float32')
x = np.random.random([2, 14, 8, 8]).astype('float32')
img_size = np.ones((2, 2)).astype('int32')

x = paddle.to_tensor(x)
Expand Down