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

Features/590 nbytes #683

Merged
merged 5 commits into from
Oct 13, 2020
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Pending additions
## New features
- [#683](https://github.com/helmholtz-analytics/heat/pull/683) New properties: nbytes, gnbytes, lnbytes
### Manipulations
### Statistical Functions
### Linear Algebra
Expand Down
37 changes: 37 additions & 0 deletions heat/core/dndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,19 @@ def dtype(self):
def gshape(self):
return self.__gshape

@property
def nbytes(self):
"""
Equivalent to property gnbytes.
Note: Does not include memory consumed by non-element attributes of the DNDarray object.

Returns
-------
global_number_of_bytes : int
number of bytes consumed by the global tensor
"""
return self._DNDarray__array.element_size() * self.size

@property
def numdims(self):
"""
Expand Down Expand Up @@ -147,6 +160,18 @@ def size(self):
"""
return torch.prod(torch.tensor(self.gshape, device=self.device.torch_device)).item()

@property
def gnbytes(self):
"""
Note: Does not include memory consumed by non-element attributes of the DNDarray object.

Returns
-------
global_number_of_bytes : int
number of bytes consumed by the global tensor
"""
return self.nbytes

@property
def gnumel(self):
"""
Expand All @@ -158,6 +183,18 @@ def gnumel(self):
"""
return self.size

@property
def lnbytes(self):
"""
Note: Does not include memory consumed by non-element attributes of the DNDarray object.

Returns
-------
local_number_of_bytes : int
number of bytes consumed by the local tensor
"""
return self._DNDarray__array.element_size() * self._DNDarray__array.nelement()

@property
def lnumel(self):
"""
Expand Down
134 changes: 134 additions & 0 deletions heat/core/tests/test_dndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,61 @@ def test_lloc(self):
self.assertTrue(torch.all(a._DNDarray__array[3:7:2, 2:5:2] == 1))
self.assertEqual(a.lloc[3:7:2, 2:5:2].dtype, torch.float32)

def test_lnbytes(self):
# undistributed case

# integer
x_uint8 = ht.arange(6 * 7 * 8, dtype=ht.uint8).reshape((6, 7, 8))
x_int8 = ht.arange(6 * 7 * 8, dtype=ht.int8).reshape((6, 7, 8))
x_int16 = ht.arange(6 * 7 * 8, dtype=ht.int16).reshape((6, 7, 8))
x_int32 = ht.arange(6 * 7 * 8, dtype=ht.int32).reshape((6, 7, 8))
x_int64 = ht.arange(6 * 7 * 8, dtype=ht.int64).reshape((6, 7, 8))

# float
x_float32 = ht.arange(6 * 7 * 8, dtype=ht.float32).reshape((6, 7, 8))
x_float64 = ht.arange(6 * 7 * 8, dtype=ht.float64).reshape((6, 7, 8))

# bool
x_bool = ht.arange(6 * 7 * 8, dtype=ht.bool).reshape((6, 7, 8))

self.assertEqual(x_uint8.lnbytes, x_uint8.gnbytes)
self.assertEqual(x_int8.lnbytes, x_int8.gnbytes)
self.assertEqual(x_int16.lnbytes, x_int16.gnbytes)
self.assertEqual(x_int32.lnbytes, x_int32.gnbytes)
self.assertEqual(x_int64.lnbytes, x_int64.gnbytes)

self.assertEqual(x_float32.lnbytes, x_float32.gnbytes)
self.assertEqual(x_float64.lnbytes, x_float64.gnbytes)

self.assertEqual(x_bool.lnbytes, x_bool.gnbytes)

# distributed case

# integer
x_uint8_d = ht.arange(6 * 7 * 8, split=0, dtype=ht.uint8)
x_int8_d = ht.arange(6 * 7 * 8, split=0, dtype=ht.int8)
x_int16_d = ht.arange(6 * 7 * 8, split=0, dtype=ht.int16)
x_int32_d = ht.arange(6 * 7 * 8, split=0, dtype=ht.int32)
x_int64_d = ht.arange(6 * 7 * 8, split=0, dtype=ht.int64)

# float
x_float32_d = ht.arange(6 * 7 * 8, split=0, dtype=ht.float32)
x_float64_d = ht.arange(6 * 7 * 8, split=0, dtype=ht.float64)

# bool
x_bool_d = ht.arange(6 * 7 * 8, split=0, dtype=ht.bool)

self.assertEqual(x_uint8_d.lnbytes, x_uint8_d.lnumel * 1)
self.assertEqual(x_int8_d.lnbytes, x_int8_d.lnumel * 1)
self.assertEqual(x_int16_d.lnbytes, x_int16_d.lnumel * 2)
self.assertEqual(x_int32_d.lnbytes, x_int32_d.lnumel * 4)
self.assertEqual(x_int64_d.lnbytes, x_int64_d.lnumel * 8)

self.assertEqual(x_float32_d.lnbytes, x_float32_d.lnumel * 4)
self.assertEqual(x_float64_d.lnbytes, x_float64_d.lnumel * 8)

self.assertEqual(x_bool_d.lnbytes, x_bool_d.lnumel * 1)

def test_lshift(self):
int_tensor = ht.array([[0, 1], [2, 3]])
int_result = ht.array([[0, 4], [8, 12]])
Expand All @@ -493,6 +548,85 @@ def test_lshift(self):
with self.assertRaises(TypeError):
ht.array([True]) << 2

def test_nbytes(self):
# undistributed case

# integer
x_uint8 = ht.arange(6 * 7 * 8, dtype=ht.uint8).reshape((6, 7, 8))
x_int8 = ht.arange(6 * 7 * 8, dtype=ht.int8).reshape((6, 7, 8))
x_int16 = ht.arange(6 * 7 * 8, dtype=ht.int16).reshape((6, 7, 8))
x_int32 = ht.arange(6 * 7 * 8, dtype=ht.int32).reshape((6, 7, 8))
x_int64 = ht.arange(6 * 7 * 8, dtype=ht.int64).reshape((6, 7, 8))

# float
x_float32 = ht.arange(6 * 7 * 8, dtype=ht.float32).reshape((6, 7, 8))
x_float64 = ht.arange(6 * 7 * 8, dtype=ht.float64).reshape((6, 7, 8))

# bool
x_bool = ht.arange(6 * 7 * 8, dtype=ht.bool).reshape((6, 7, 8))

self.assertEqual(x_uint8.nbytes, 336 * 1)
self.assertEqual(x_int8.nbytes, 336 * 1)
self.assertEqual(x_int16.nbytes, 336 * 2)
self.assertEqual(x_int32.nbytes, 336 * 4)
self.assertEqual(x_int64.nbytes, 336 * 8)

self.assertEqual(x_float32.nbytes, 336 * 4)
self.assertEqual(x_float64.nbytes, 336 * 8)

self.assertEqual(x_bool.nbytes, 336 * 1)

# equivalent function gnbytes
self.assertEqual(x_uint8.nbytes, x_uint8.gnbytes)
self.assertEqual(x_int8.nbytes, x_int8.gnbytes)
self.assertEqual(x_int16.nbytes, x_int16.gnbytes)
self.assertEqual(x_int32.nbytes, x_int32.gnbytes)
self.assertEqual(x_int64.nbytes, x_int64.gnbytes)

self.assertEqual(x_float32.nbytes, x_float32.gnbytes)
self.assertEqual(x_float64.nbytes, x_float64.gnbytes)

self.assertEqual(x_bool.nbytes, x_bool.gnbytes)

# distributed case

# integer
x_uint8_d = ht.arange(6 * 7 * 8, split=0, dtype=ht.uint8)
x_int8_d = ht.arange(6 * 7 * 8, split=0, dtype=ht.int8)
x_int16_d = ht.arange(6 * 7 * 8, split=0, dtype=ht.int16)
x_int32_d = ht.arange(6 * 7 * 8, split=0, dtype=ht.int32)
x_int64_d = ht.arange(6 * 7 * 8, split=0, dtype=ht.int64)

# float
x_float32_d = ht.arange(6 * 7 * 8, split=0, dtype=ht.float32)
x_float64_d = ht.arange(6 * 7 * 8, split=0, dtype=ht.float64)

# bool
x_bool_d = ht.arange(6 * 7 * 8, split=0, dtype=ht.bool)

self.assertEqual(x_uint8_d.nbytes, 336 * 1)
self.assertEqual(x_int8_d.nbytes, 336 * 1)
self.assertEqual(x_int16_d.nbytes, 336 * 2)
self.assertEqual(x_int32_d.nbytes, 336 * 4)
self.assertEqual(x_int64_d.nbytes, 336 * 8)

self.assertEqual(x_float32_d.nbytes, 336 * 4)
self.assertEqual(x_float64_d.nbytes, 336 * 8)

self.assertEqual(x_bool_d.nbytes, 336 * 1)

# equivalent function gnbytes
self.assertEqual(x_uint8_d.nbytes, x_uint8_d.gnbytes)
self.assertEqual(x_int8_d.nbytes, x_int8_d.gnbytes)
self.assertEqual(x_int16_d.nbytes, x_int16_d.gnbytes)
self.assertEqual(x_int32_d.nbytes, x_int32_d.gnbytes)
self.assertEqual(x_int64_d.nbytes, x_int64_d.gnbytes)

self.assertEqual(x_float32_d.nbytes, x_float32_d.gnbytes)
self.assertEqual(x_float64_d.nbytes, x_float64_d.gnbytes)

self.assertEqual(x_bool_d.nbytes, x_bool_d.gnbytes)

def test_ndim(self):
a = ht.empty([2, 3, 3, 2])
self.assertEqual(a.ndim, 4)
Expand Down