Skip to content

Commit

Permalink
fix by code review
Browse files Browse the repository at this point in the history
  • Loading branch information
2742195759 committed Apr 28, 2022
1 parent 920ec40 commit 835bb56
Show file tree
Hide file tree
Showing 4 changed files with 510 additions and 59 deletions.
2 changes: 1 addition & 1 deletion paddle/phi/kernels/impl/einsum_impl.h
Expand Up @@ -357,7 +357,7 @@ DenseTensor PerformReduction(const Context& dev_ctx,

inline bool is_no_need_transpose(const std::vector<int>& axis) {
for (size_t i = 0; i < axis.size(); ++i) {
if (i != size_t(axis[i])) return false;
if (i != static_cast<size_t>(axis[i])) return false;
}
return true;
}
Expand Down
50 changes: 16 additions & 34 deletions python/paddle/fluid/tests/unittests/test_einsum.py
Expand Up @@ -19,19 +19,6 @@
from paddle.fluid import core


def error_trans(func, *args, **kargs):
"""
transport C++ exception into Python exception.
because einsum_v2 raise different exception with einsum_v1.
"""
try:
out = func(*args, **kargs)
except ValueError as e:
if "Same label have different shapes" in str(e):
raise AssertionError("Invalid operands: label i "
"corresponds to non-broadcastable dimensions.")


class TestErrors(unittest.TestCase):
def setUp(self):
pass
Expand Down Expand Up @@ -95,7 +82,7 @@ def test_param_errors(self):
with self.assertRaisesRegex(AssertionError, (
"Invalid operands: label i "
"corresponds to non-broadcastable dimensions.")):
error_trans(paddle.einsum, 'ij...,ji...', a, a)
paddle.einsum('ij...,ji...', a, a)


class TestEinsum(unittest.TestCase):
Expand Down Expand Up @@ -383,21 +370,18 @@ def test_sums(self):
self.check_output("...,...", a, a)
self.check_output("i,i", a, a)

# TODO(@xiongkun): explict broadcast in EinsumOp is not supported, it's not recommend to use einsum like this.
#p = np.ones((10, 2)).astype('float')
#q = np.ones((1, 2)).astype('float')
#self.check_output('ij,ij->j', p, q)
p = np.ones((10, 2)).astype('float')
q = np.ones((1, 2)).astype('float')
self.check_output('ij,ij->j', p, q)

# TODO(@xiongkun): explict-label-broadcast in EinsumOp is not supported, it's not recommend to use einsum like this.
#x = np.array([2., 3.]).astype('float')
#y = np.array([4.]).astype('float')
#self.check_output("i, i", x, y)
x = np.array([2., 3.]).astype('float')
y = np.array([4.]).astype('float')
self.check_output("i, i", x, y)

# TODO(@xiongkun): explict-label-broadcast in EinsumOp is not supported, it's not recommend to use einsum like this.
#p = np.ones((1, 5)) / 2
#q = np.ones((5, 5)) / 2
#self.check_output("...ij,...jk->...ik", p, p)
#self.check_output("...ij,...jk->...ik", p, q)
p = np.ones((1, 5)) / 2
q = np.ones((5, 5)) / 2
self.check_output("...ij,...jk->...ik", p, p)
self.check_output("...ij,...jk->...ik", p, q)

x = np.eye(2).astype('float')
y = np.ones(2).astype('float')
Expand All @@ -406,13 +390,11 @@ def test_sums(self):
self.check_output("ij,i->", x, y)

def test_large_nops(self):
pass
# TODO(@xiongkun): explict broadcast in EinsumOp is not supported, it's not recommend to use einsum like this.
#a = np.arange(4 * 3 * 1 * 4).reshape(4, 3, 1, 4).astype('float')
#self.check_output('a...b,b...c,c...d', a, a, a)
#self.check_output('a...b,b...c,c...a', a, a, a)
#self.check_output('a...b,b...c,c...a', a, a, a)
#self.check_output('...ab,...ba,...ab,...ab', a, a, a, a)
a = np.arange(4 * 3 * 1 * 4).reshape(4, 3, 1, 4).astype('float')
self.check_output('a...b,b...c,c...d', a, a, a)
self.check_output('a...b,b...c,c...a', a, a, a)
self.check_output('a...b,b...c,c...a', a, a, a)
self.check_output('...ab,...ba,...ab,...ab', a, a, a, a)

def test_static_graph(self):
paddle.enable_static()
Expand Down

1 comment on commit 835bb56

@paddle-bot-old
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Congratulation! Your pull request passed all required CI. You could ask reviewer(s) to approve and merge. 🎉

Please sign in to comment.