Skip to content

Commit

Permalink
Fix AttributeError: module 'numpy' has no attribute 'bool'. (apache#2…
Browse files Browse the repository at this point in the history
  • Loading branch information
mdengler committed Oct 25, 2023
1 parent b84609d commit 96bb6f7
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ npx.load('my_arrays')
# np.int64 # Signed 64-bit integer types
# np.float32 # Standard double-precision floating point
# np.complex # Complex numbers represented by 128 floats
# np.bool # Boolean type storing TRUE and FALSE values
# bool # Boolean type storing TRUE and FALSE values (formerly `np.bool`)
# np.object # Python object type
# np.string_ # Fixed-length string type
# np.unicode_ # Fixed-length unicode type
Expand Down
4 changes: 2 additions & 2 deletions python/mxnet/ndarray/ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -3078,7 +3078,7 @@ def indexing_key_expand_implicit_axes(key, shape):
(slice(None, 2, None), None, 0, slice(None, None, None))
>>> bool_array = np.array([[True, False, True, False],
[False, True, False, True],
[True, False, True, False]], dtype=np.bool)
[True, False, True, False]], dtype=bool)
>>> indexing_key_expand_implicit_axes(np.s_[bool_array, None, 0:2], shape)
(array([0, 0, 1, 1, 2, 2], dtype=int64), array([0, 2, 1, 3, 0, 2], dtype=int64), None, slice(None, 2, None))
"""
Expand Down Expand Up @@ -3109,7 +3109,7 @@ def indexing_key_expand_implicit_axes(key, shape):
)
ell_idx = i
else:
# convert primitive type boolean value to mx.np.bool type
# convert primitive type boolean value to bool (formerly mx.np.bool) type
# otherwise will be treated as 1/0
if isinstance(idx, bool):
idx = array(idx, dtype=np.bool_)
Expand Down
10 changes: 5 additions & 5 deletions python/mxnet/ndarray/numpy/_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ def full(shape, fill_value, dtype=None, order='C', device=None, out=None): # py
device = str(device)
if isinstance(fill_value, bool):
fill_value = int(fill_value)
dtype = _np.bool if dtype is None else dtype
dtype = bool if dtype is None else dtype
elif isinstance(fill_value, numeric_types):
if dtype is None or dtype is float:
dtype = dtype_from_number(fill_value)
Expand Down Expand Up @@ -526,7 +526,7 @@ def empty_like(prototype, dtype=None, order='C', subok=False, shape=None): # pyl
dtype_list = {_np.float16: 'float16', _np.float32: 'float32', _np.float64: 'float64',
float: 'float64', _np.int8: 'int8', _np.int16: 'int16', _np.int32: 'int32',
_np.int64: 'int64', int:'int64', _np.uint8: 'uint8', _np.uint16: 'uint16',
_np.uint32: 'uint32', _np.uint64: 'uint64', _np.bool: 'bool',
_np.uint32: 'uint32', _np.uint64: 'uint64', bool: 'bool',
_np.bool_: 'bool_', bool: 'bool', None: 'None'}
if order != 'C':
raise NotImplementedError("Only support C-order at this moment")
Expand Down Expand Up @@ -8969,7 +8969,7 @@ def isposinf(x, out=None, **kwargs):
>>> np.isposinf(np.array([-np.inf, 0., np.inf]))
array([False, False, True])
>>> x = np.array([-np.inf, 0., np.inf])
>>> y = np.array([True, True, True], dtype=np.bool)
>>> y = np.array([True, True, True], dtype=bool)
>>> np.isposinf(x, y)
array([False, False, True])
>>> y
Expand Down Expand Up @@ -9015,7 +9015,7 @@ def isneginf(x, out=None, **kwargs):
>>> np.isneginf(np.array([-np.inf, 0., np.inf]))
array([ True, False, False])
>>> x = np.array([-np.inf, 0., np.inf])
>>> y = np.array([True, True, True], dtype=np.bool)
>>> y = np.array([True, True, True], dtype=bool)
>>> np.isneginf(x, y)
array([ True, False, False])
>>> y
Expand Down Expand Up @@ -9070,7 +9070,7 @@ def isfinite(x, out=None, **kwargs):
>>> np.isfinite(np.array([np.log(-1.),1.,np.log(0)]))
array([False, True, False])
>>> x = np.array([-np.inf, 0., np.inf])
>>> y = np.array([True, True, True], dtype=np.bool)
>>> y = np.array([True, True, True], dtype=bool)
>>> np.isfinite(x, y)
array([False, True, False])
>>> y
Expand Down
14 changes: 7 additions & 7 deletions python/mxnet/numpy/multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def _as_mx_np_array(object, device=None, zero_copy=False):
return from_numpy(object, zero_copy and object.flags['C_CONTIGUOUS'])
elif isinstance(object, (integer_types, numeric_types)):
return object
elif isinstance(object, (_np.bool_, _np.bool)):
elif isinstance(object, (_np.bool_, bool)):
return array(object, dtype=_np.bool_, device=device)
elif isinstance(object, (list, tuple)):
tmp = [_as_mx_np_array(arr, device=device, zero_copy=zero_copy) for arr in object]
Expand Down Expand Up @@ -782,7 +782,7 @@ def __getitem__(self, key):
ndim = self.ndim # pylint: disable=redefined-outer-name
shape = self.shape # pylint: disable=redefined-outer-name
if isinstance(key, bool): # otherwise will be treated as 0 and 1
key = array(key, dtype=_np.bool, device=self.device)
key = array(key, dtype=bool, device=self.device)
if isinstance(key, list):
try:
new_key = _np.array(key)
Expand Down Expand Up @@ -966,13 +966,13 @@ def __setitem__(self, key, value):
if isinstance(value, NDArray) and not isinstance(value, ndarray):
raise TypeError('Cannot assign mx.nd.NDArray to mxnet.numpy.ndarray')
if isinstance(key, bool): # otherwise will be treated as 0 and 1
key = array(key, dtype=_np.bool)
key = array(key, dtype=bool)

# Handle single boolean assign of matching dimensionality and size first for higher speed
# If the boolean array is mixed with other idices, it is instead expanded into (multiple)
# integer array indices and will be handled by advanced assign.
# Come before the check self.dim == 0 as it also handle the 0-dim case.
if isinstance(key, ndarray) and key.dtype == _np.bool:
if isinstance(key, ndarray) and key.dtype == bool:
return self._set_np_boolean_indexing(key, value)

# handle basic and advanced indexing
Expand Down Expand Up @@ -12123,7 +12123,7 @@ def isposinf(x, out=None, **kwargs):
>>> np.isposinf(np.array([-np.inf, 0., np.inf]))
array([False, False, True])
>>> x = np.array([-np.inf, 0., np.inf])
>>> y = np.array([True, True, True], dtype=np.bool)
>>> y = np.array([True, True, True], dtype=bool)
>>> np.isposinf(x, y)
array([False, False, True])
>>> y
Expand Down Expand Up @@ -12169,7 +12169,7 @@ def isneginf(x, out=None, **kwargs):
>>> np.isneginf(np.array([-np.inf, 0., np.inf]))
array([ True, False, False])
>>> x = np.array([-np.inf, 0., np.inf])
>>> y = np.array([True, True, True], dtype=np.bool)
>>> y = np.array([True, True, True], dtype=bool)
>>> np.isneginf(x, y)
array([ True, False, False])
>>> y
Expand Down Expand Up @@ -12224,7 +12224,7 @@ def isfinite(x, out=None, **kwargs):
>>> np.isfinite(np.array([np.log(-1.),1.,np.log(0)]))
array([False, True, False])
>>> x = np.array([-np.inf, 0., np.inf])
>>> y = np.array([True, True, True], dtype=np.bool)
>>> y = np.array([True, True, True], dtype=bool)
>>> np.isfinite(x, y)
array([False, True, False])
>>> y
Expand Down
2 changes: 1 addition & 1 deletion python/mxnet/numpy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
int8 = onp.dtype(onp.int8)
int64 = onp.dtype(onp.int64)
bool_ = onp.dtype(onp.bool_)
bool = onp.dtype(onp.bool)
bool = onp.dtype(bool)
int16 = onp.dtype(onp.int16)
uint16 = onp.dtype(onp.uint16)
uint32 = onp.dtype(onp.uint32)
Expand Down
2 changes: 1 addition & 1 deletion python/mxnet/symbol/numpy/_symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -1334,7 +1334,7 @@ def full(shape, fill_value, dtype=None, order='C', ctx=None, out=None): # pylin
return ret
if isinstance(fill_value, bool):
fill_value = int(fill_value)
dtype = _np.bool if dtype is None else dtype
dtype = bool if dtype is None else dtype
return _npi.full(shape=shape, value=fill_value, ctx=ctx, dtype=dtype, out=out)


Expand Down
4 changes: 2 additions & 2 deletions python/mxnet/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def default_rtols():
return {np.dtype(np.float16): 1e-2,
np.dtype(np.float32): 1e-4,
np.dtype(np.float64): 1e-5,
np.dtype(np.bool): 0,
np.dtype(bool): 0,
np.dtype(np.int8): 0,
np.dtype(np.uint8): 0,
np.dtype(np.int32): 0,
Expand All @@ -90,7 +90,7 @@ def default_atols():
return {np.dtype(np.float16): 1e-1,
np.dtype(np.float32): 1e-3,
np.dtype(np.float64): 1e-20,
np.dtype(np.bool): 0,
np.dtype(bool): 0,
np.dtype(np.int8): 0,
np.dtype(np.uint8): 0,
np.dtype(np.int32): 0,
Expand Down
2 changes: 1 addition & 1 deletion tests/python/dnnl/subgraphs/test_matmul_subgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def forward(self, x, mask):
value = mx.np.reshape(value, (-2, -2, self._num_heads, -1))
scores = mx.npx.batch_dot(mx.np.swapaxes(query, 1, 2), mx.np.swapaxes(key, 1, 2),
transpose_b=True)
mask = mx.np.expand_dims(mask, axis=1).astype(np.bool)
mask = mx.np.expand_dims(mask, axis=1).astype(bool)
attn_weights = mx.npx.masked_softmax(scores, mask=mask, axis=-1, temperature=self._scale)
attn_weights = mx.npx.dropout(attn_weights, p=0.1)
context_vec = mx.npx.batch_dot(attn_weights,
Expand Down
2 changes: 1 addition & 1 deletion tests/python/gpu/test_fusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ def forward(self, valid_length):

foo = Foo()
foo.hybridize(static_alloc=True)
out = foo(mx.np.ones((10,), ctx=mx.gpu(), dtype=np.bool))
out = foo(mx.np.ones((10,), ctx=mx.gpu(), dtype=bool))
mx.npx.waitall()

@use_np
Expand Down
34 changes: 17 additions & 17 deletions tests/python/unittest/test_numpy_interoperability.py
Original file line number Diff line number Diff line change
Expand Up @@ -1497,8 +1497,8 @@ def _add_workload_gcd():


def _add_workload_bitwise_or():
OpArgMngr.add_workload('bitwise_or', np.array([False, False, True, True], dtype=np.bool),
np.array([False, True, False, True], dtype=np.bool))
OpArgMngr.add_workload('bitwise_or', np.array([False, False, True, True], dtype=bool),
np.array([False, True, False, True], dtype=bool))
for dtype in [np.int8, np.int32, np.int64]:
zeros = np.array([0], dtype=dtype)
ones = np.array([-1], dtype=dtype)
Expand All @@ -1509,8 +1509,8 @@ def _add_workload_bitwise_or():


def _add_workload_bitwise_and():
OpArgMngr.add_workload('bitwise_and', np.array([False, False, True, True], dtype=np.bool),
np.array([False, True, False, True], dtype=np.bool))
OpArgMngr.add_workload('bitwise_and', np.array([False, False, True, True], dtype=bool),
np.array([False, True, False, True], dtype=bool))
for dtype in [np.int8, np.int32, np.int64]:
zeros = np.array([0], dtype=dtype)
ones = np.array([-1], dtype=dtype)
Expand All @@ -1521,8 +1521,8 @@ def _add_workload_bitwise_and():


def _add_workload_bitwise_xor():
OpArgMngr.add_workload('bitwise_xor', np.array([False, False, True, True], dtype=np.bool),
np.array([False, True, False, True], dtype=np.bool))
OpArgMngr.add_workload('bitwise_xor', np.array([False, False, True, True], dtype=bool),
np.array([False, True, False, True], dtype=bool))
for dtype in [np.int8, np.int32, np.int64]:
zeros = np.array([0], dtype=dtype)
ones = np.array([-1], dtype=dtype)
Expand Down Expand Up @@ -1834,11 +1834,11 @@ def _add_workload_floor(array_pool):
def _add_workload_logical_not(array_pool):
OpArgMngr.add_workload('logical_not', np.ones(10, dtype=np.int32))
OpArgMngr.add_workload('logical_not', array_pool['4x1'])
OpArgMngr.add_workload('logical_not', np.array([True, False, True, False], dtype=np.bool))
OpArgMngr.add_workload('logical_not', np.array([True, False, True, False], dtype=bool))


def _add_workload_bitwise_not():
OpArgMngr.add_workload('bitwise_not', np.array([True, False, True, False], dtype=np.bool))
OpArgMngr.add_workload('bitwise_not', np.array([True, False, True, False], dtype=bool))
for dtype in [np.int8, np.int32, np.int64]:
zeros = np.array([0], dtype=dtype)
ones = np.array([-1], dtype=dtype)
Expand All @@ -1847,7 +1847,7 @@ def _add_workload_bitwise_not():


def _add_workload_invert():
OpArgMngr.add_workload('invert', np.array([True, False, True, False], dtype=np.bool))
OpArgMngr.add_workload('invert', np.array([True, False, True, False], dtype=bool))
for dtype in [np.int8, np.int32, np.int64]:
zeros = np.array([0], dtype=dtype)
ones = np.array([-1], dtype=dtype)
Expand Down Expand Up @@ -2045,19 +2045,19 @@ def _add_workload_less_equal(array_pool):

def _add_workload_logical_and(array_pool):
OpArgMngr.add_workload('logical_and', np.array([0, 1, 2, 4, 2], dtype=np.float32), np.array([-2, 5, 1, 4, 3], dtype=np.float32))
OpArgMngr.add_workload('logical_and', np.array([False, False, True, True], dtype=np.bool),
np.array([False, True, False, True], dtype=np.bool))
OpArgMngr.add_workload('logical_and', np.array([False, False, True, True], dtype=bool),
np.array([False, True, False, True], dtype=bool))

def _add_workload_logical_or(array_pool):
OpArgMngr.add_workload('logical_or', np.array([0, 1, 2, 4, 2], dtype=np.bool), np.array([-2, 5, 1, 4, 3], dtype=np.bool))
OpArgMngr.add_workload('logical_or', np.array([False, False, True, True], dtype=np.bool),
np.array([False, True, False, True], dtype=np.bool))
OpArgMngr.add_workload('logical_or', np.array([0, 1, 2, 4, 2], dtype=bool), np.array([-2, 5, 1, 4, 3], dtype=bool))
OpArgMngr.add_workload('logical_or', np.array([False, False, True, True], dtype=bool),
np.array([False, True, False, True], dtype=bool))


def _add_workload_logical_xor(array_pool):
OpArgMngr.add_workload('logical_xor', np.array([0, 1, 2, 4, 2], dtype=np.float32), np.array([-2, 5, 1, 4, 3], dtype=np.float32))
OpArgMngr.add_workload('logical_xor', np.array([False, False, True, True], dtype=np.bool),
np.array([False, True, False, True], dtype=np.bool))
OpArgMngr.add_workload('logical_xor', np.array([False, False, True, True], dtype=bool),
np.array([False, True, False, True], dtype=bool))


def _add_workload_where():
Expand Down Expand Up @@ -2910,7 +2910,7 @@ def _add_workload_select():
condlist = np.array([[ True, True, True, False, False,
False, False, False, False, False],
[ False, False, False, False, False,
False, True, True, True, True]], dtype=np.bool)
False, True, True, True, True]], dtype=bool)
choicelist = np.array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81]])
OpArgMngr.add_workload('select', condlist, choicelist)
Expand Down
18 changes: 9 additions & 9 deletions tests/python/unittest/test_numpy_ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_np_empty():
(np.float32, np.float32),
(np.float64, np.float64),
(np.bool_, np.bool_),
(np.bool, np.bool_),
(bool, np.bool_),
('int8', np.int8),
('int32', np.int32),
('float16', np.float16),
Expand Down Expand Up @@ -80,7 +80,7 @@ def test_np_empty():

@use_np
def test_np_array_creation():
dtypes = [_np.int8, _np.int32, _np.float16, _np.float32, _np.float64, _np.bool, _np.bool_,
dtypes = [_np.int8, _np.int32, _np.float16, _np.float32, _np.float64, bool, _np.bool_,
'int8', 'int32', 'float16', 'float32', 'float64', 'bool', None]
objects = [
[],
Expand Down Expand Up @@ -154,7 +154,7 @@ def check_zero_array_creation(shape, dtype):
assert type(y[1]) == np.ndarray

for shape in shapes:
for dtype in [_np.bool, bool, _np.bool, 'bool']:
for dtype in [bool, 'bool']:
check_zero_array_creation(shape, dtype)


Expand Down Expand Up @@ -207,7 +207,7 @@ def check_ones_array_creation(shape, dtype):
assert type(y[1]) == np.ndarray

for shape in shapes:
for dtype in [_np.bool, bool, _np.bool, 'bool']:
for dtype in [bool, 'bool']:
check_ones_array_creation(shape, dtype)


Expand Down Expand Up @@ -575,7 +575,7 @@ def check_astype_equal(itype, otype, copy, expect_zero_copy=False, hybridize=Fal
assert id(mx_ret) == id(mx_data)
assert id(np_ret) == id(np_data)

dtypes = [np.int8, np.uint8, np.int32, np.float16, np.float32, np.float64, np.bool, np.bool_,
dtypes = [np.int8, np.uint8, np.int32, np.float16, np.float32, np.float64, bool, np.bool_,
'int8', 'uint8', 'int32', 'float16', 'float32', 'float64', 'bool']

for itype, otype in itertools.product(dtypes, dtypes):
Expand Down Expand Up @@ -1218,7 +1218,7 @@ def test_boolean_index_tuple():
[[4, 5],
[6, 7]]], dtype=np.int32)
b = np.array([[False,True],
[True,False]],dtype=np.bool)
[True,False]],dtype=bool)
_np_a = a.asnumpy()
_np_b = b.asnumpy()
assert same(a[:, b].asnumpy(), _np_a[:, _np_b])
Expand Down Expand Up @@ -1252,7 +1252,7 @@ def test_boolean_index_assign():
# test boolean indexing assign
shape = (3, 2, 3)
mx_data = np.random.uniform(size=shape)
mx_mask = np.array([[False,True], [True,False], [True,False]],dtype=np.bool)
mx_mask = np.array([[False,True], [True,False], [True,False]],dtype=bool)
np_data = mx_data.asnumpy()
np_mask = mx_mask.asnumpy()

Expand All @@ -1271,7 +1271,7 @@ def test_boolean_index_assign():
mx_data[mx_mask, :] = 3
assert_almost_equal(mx_data.asnumpy(), np_data, rtol=1e-3, atol=1e-5, use_broadcast=False)

mx_mask = np.array([[False,True, True],[False, True,False]],dtype=np.bool)
mx_mask = np.array([[False,True, True],[False, True,False]],dtype=bool)
np_mask = mx_mask.asnumpy()

np_data[0, np_mask] = 5
Expand Down Expand Up @@ -1310,7 +1310,7 @@ def test_boolean_index_autograd():

@use_np
def test_np_get_dtype():
dtypes = [_np.int8, _np.int32, _np.float16, _np.float32, _np.float64, _np.bool, _np.bool_,
dtypes = [_np.int8, _np.int32, _np.float16, _np.float32, _np.float64, bool, _np.bool_,
'int8', 'int32', 'float16', 'float32', 'float64', 'bool', None]
objects = [
[],
Expand Down
Loading

0 comments on commit 96bb6f7

Please sign in to comment.