Skip to content

Commit efd6c61

Browse files
committed
Add prod_sequence to avoid creating vector
1 parent 2e05154 commit efd6c61

10 files changed

Lines changed: 25 additions & 15 deletions

File tree

cupy/core/_routines_indexing.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -687,14 +687,14 @@ cdef _scatter_op_single(
687687

688688
lshape = a_shape[:li]
689689
rshape = a_shape[ri + 1:]
690-
adim = internal.prod(a_shape[li:ri + 1])
690+
adim = internal.prod_sequence(a_shape[li:ri + 1])
691691

692692
indices_shape = indices.shape
693693
v_shape = lshape + indices_shape + rshape
694694
v = _manipulation.broadcast_to(v, v_shape)
695695

696696
cdim = indices.size
697-
rdim = internal.prod(rshape)
697+
rdim = internal.prod_sequence(rshape)
698698
indices = _manipulation._reshape(
699699
indices,
700700
(1,) * len(lshape) + indices_shape + (1,) * len(rshape))

cupy/core/_routines_manipulation.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,7 @@ cdef ndarray _concatenate_single_kernel(
765765

766766
ret = core.ndarray(shape, dtype=dtype)
767767
if same_shape_and_contiguous:
768-
base = internal.prod(shape[axis:]) // len(arrays)
768+
base = internal.prod_sequence(shape[axis:]) // len(arrays)
769769
_concatenate_kernel_same_size(x, base, ret)
770770
return ret
771771

cupy/core/core.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2763,7 +2763,7 @@ cpdef ndarray _convert_object_with_cuda_array_interface(a):
27632763
for sh, st in zip(shape, strides):
27642764
nbytes = max(nbytes, abs(sh * st))
27652765
else:
2766-
nbytes = internal.prod(shape) * dtype.itemsize
2766+
nbytes = internal.prod_sequence(shape) * dtype.itemsize
27672767
mem = memory_module.UnownedMemory(desc['data'][0], nbytes, a)
27682768
memptr = memory.MemoryPointer(mem, 0)
27692769
return ndarray(shape, dtype, memptr, strides)

cupy/core/internal.pxd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ from libc.stdint cimport uint16_t
55

66
cpdef Py_ssize_t prod(const vector.vector[Py_ssize_t]& args)
77

8+
cpdef Py_ssize_t prod_sequence(object args)
9+
810
cpdef tuple get_size(object size)
911

1012
cpdef bint vector_equal(

cupy/core/internal.pyx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ cpdef inline Py_ssize_t prod(const vector.vector[Py_ssize_t]& args):
1919
return n
2020

2121

22+
@cython.profile(False)
23+
cpdef inline Py_ssize_t prod_sequence(object args):
24+
cdef Py_ssize_t i, n = 1
25+
for i in args:
26+
n *= i
27+
return n
28+
29+
2230
@cython.profile(False)
2331
cpdef inline tuple get_size(object size):
2432
if size is None:

cupy/cudnn.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ def get_rnn_lin_layer_matrix_params(
837837
cudnn.destroyFilterDescriptor(mat_desc)
838838
byte_size = _get_byte_size(data_type)
839839
offset = (ptr - w.data.ptr) // byte_size
840-
size = internal.prod(dim)
840+
size = internal.prod_sequence(dim)
841841
mat = w[offset:offset + size]
842842
return mat
843843

@@ -855,7 +855,7 @@ def get_rnn_lin_layer_bias_params(
855855
cudnn.destroyFilterDescriptor(bias_desc)
856856
byte_size = _get_byte_size(data_type)
857857
offset = (ptr - w.data.ptr) // byte_size
858-
size = internal.prod(dim)
858+
size = internal.prod_sequence(dim)
859859
bias = w[offset:offset + size]
860860
return bias
861861

cupy/linalg/einsum.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,8 @@ def _flatten_transpose(a, axeses):
285285
transpose_axes.extend(axes)
286286
shapes.append([a.shape[axis] for axis in axes])
287287
return (
288-
a.transpose(transpose_axes).reshape(
289-
tuple(cupy.core.internal.prod(shape) for shape in shapes)),
288+
a.transpose(transpose_axes).reshape(tuple([
289+
cupy.core.internal.prod_sequence(shape) for shape in shapes])),
290290
shapes
291291
)
292292

cupy/linalg/product.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ def tensordot(a, b, axes=2):
310310

311311
ret_shape = a.shape[sum_ndim:] + b.shape[sum_ndim:]
312312

313-
k = internal.prod(a.shape[:sum_ndim])
313+
k = internal.prod_sequence(a.shape[:sum_ndim])
314314
# Avoid division by zero: core.tensordot_core returns zeros without
315315
# checking n, m consistency, thus allowing 0-length dimensions to work
316316
n = a.size // k if k != 0 else 0

cupy/linalg/solve.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def tensorsolve(a, b, axes=None):
163163
a = a.transpose(allaxes)
164164

165165
oldshape = a.shape[-(a.ndim - b.ndim):]
166-
prod = cupy.internal.prod(oldshape)
166+
prod = cupy.internal.prod_sequence(oldshape)
167167

168168
a = a.reshape(-1, prod)
169169
b = b.ravel()
@@ -458,7 +458,7 @@ def tensorinv(a, ind=2):
458458
raise ValueError('Invalid ind argument')
459459
oldshape = a.shape
460460
invshape = oldshape[ind:] + oldshape[:ind]
461-
prod = cupy.internal.prod(oldshape[ind:])
461+
prod = cupy.internal.prod_sequence(oldshape[ind:])
462462
a = a.reshape(prod, -1)
463463
a_inv = inv(a)
464464
return a_inv.reshape(*invshape)

tests/cupy_tests/core_tests/test_internal.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ def test_two(self):
1818
self.assertEqual(internal.prod([2, 3]), 6)
1919

2020

21-
class TestProdSsizeT(unittest.TestCase):
21+
class TestProdSequence(unittest.TestCase):
2222

2323
def test_empty(self):
24-
self.assertEqual(internal.prod([]), 1)
24+
self.assertEqual(internal.prod_sequence(()), 1)
2525

2626
def test_one(self):
27-
self.assertEqual(internal.prod([2]), 2)
27+
self.assertEqual(internal.prod_sequence((2,)), 2)
2828

2929
def test_two(self):
30-
self.assertEqual(internal.prod([2, 3]), 6)
30+
self.assertEqual(internal.prod_sequence((2, 3)), 6)
3131

3232

3333
class TestGetSize(unittest.TestCase):

0 commit comments

Comments
 (0)