Skip to content

Commit

Permalink
Merge pull request #159 from manopapad/more-test-gens
Browse files Browse the repository at this point in the history
Small additions to test input generators
  • Loading branch information
manopapad authored Dec 16, 2021
2 parents ace0d87 + c383e6b commit 3129e84
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 25 deletions.
4 changes: 2 additions & 2 deletions tests/intra_array_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
#

import numpy as np
from test_tools.generators import seq_array
from test_tools.generators import mk_0to1_array

import cunumeric as num
from legate.core import LEGATE_MAX_DIM


def random_array(lib, ndim):
return seq_array(lib, ndim * (5,))
return mk_0to1_array(lib, ndim * (5,))


def nd_view_of_1d(lib, ndim):
Expand Down
4 changes: 2 additions & 2 deletions tests/singleton_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
#

import numpy as np
from test_tools.generators import scalar_gen, seq_array
from test_tools.generators import mk_0to1_array, scalar_gen

import cunumeric as num
from legate.core import LEGATE_MAX_DIM


def nonscalar_gen(lib):
for ndim in range(1, LEGATE_MAX_DIM): # off-by-one is by design
yield seq_array(lib, ndim * (5,))
yield mk_0to1_array(lib, ndim * (5,))


def tuple_set(tup, idx, val):
Expand Down
43 changes: 22 additions & 21 deletions tests/test_tools/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,34 @@ def scalar_gen(lib, val):
yield lib.full(ndim * (5,), val)[ndim * (slice(1, 2),)]


def seq_array(lib, shape):
def mk_0to1_array(lib, shape):
"""
Constructs an array of the required shape, containing (in C order)
sequential values uniformly spaced in the range (0,1].
sequential real values uniformly spaced in the range (0,1].
"""
size = np.prod(shape)
if size == 1:
# Avoid zeros, since those are more likely to cause arithmetic issues
# or produce degenerate outputs.
return lib.full(shape, 0.5)
return mk_seq_array(lib, shape) / size

Such arrays should (hopefully) make good candidates for test inputs,
because they:

- are fast to construct
- do not contain duplicate values
- contain small values, unlikely to cause arithmetic blowup
- don't require RNG to construct, which is awkward to use in a determinstic
way in a distributed setting
- don't contain zeros, which are more likely to cause arithmetic issues or
produce degenerate outputs
def mk_seq_array(lib, shape):
"""
Constructs an array of the required shape, containing (in C order)
sequential integer values starting from 1.
"""
arr = lib.full(shape, 0.5)
arr = lib.zeros(shape, dtype=int)
size = np.prod(shape)
if size > 1:
# Don't return the reshaped array directly, instead use it to update
# the contents of an existing array of the same shape, thus producing a
# Store without transformations, that has been tiled in the natural way
arr[:] = lib.arange(1, size + 1).reshape(shape) / size
# Don't return the reshaped array directly, instead use it to update
# the contents of an existing array of the same shape, thus producing a
# Store without transformations, that has been tiled in the natural way
arr[:] = lib.arange(1, size + 1).reshape(shape)
return arr


def broadcasts_to(lib, tgt_shape):
def broadcasts_to(lib, tgt_shape, mk_array=mk_0to1_array):
"""
Generates a collection of arrays that will broadcast to the given shape.
"""
Expand All @@ -74,10 +75,10 @@ def broadcasts_to(lib, tgt_shape):
src_shape = tuple(
d if keep else 1 for (d, keep) in zip(tgt_shape, mask)
)
yield seq_array(lib, src_shape)
yield mk_array(lib, src_shape)


def permutes_to(lib, tgt_shape):
def permutes_to(lib, tgt_shape, mk_array=mk_0to1_array):
"""
Generates all the possible ways that an array can be transposed to meet a
given shape.
Expand All @@ -94,4 +95,4 @@ def permutes_to(lib, tgt_shape):
for (i, j) in enumerate(axes):
src_shape[j] = tgt_shape[i]
src_shape = tuple(src_shape)
yield seq_array(lib, src_shape).transpose(axes)
yield mk_array(lib, src_shape).transpose(axes)

0 comments on commit 3129e84

Please sign in to comment.