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

Bug/median #624

Merged
merged 5 commits into from
Jul 10, 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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
- [#614](https://github.com/helmholtz-analytics/heat/pull/614) New feature: printing of DNDarrays and ``__repr__`` and ``__str__`` functions
- [#615](https://github.com/helmholtz-analytics/heat/pull/615) New feature: `skew()`
- [#615](https://github.com/helmholtz-analytics/heat/pull/615) New feature: `kurtosis()`
- [#618](https://github.com/helmholtz-analytics/heat/pull/618) Printing of unbalnced DNDarrays added
- [#618](https://github.com/helmholtz-analytics/heat/pull/618) Printing of unbalanced DNDarrays added
- [#624](https://github.com/helmholtz-analytics/heat/pull/624) Bugfix: distributed median() indexing and casting

# v0.4.0

Expand Down
20 changes: 12 additions & 8 deletions heat/core/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1323,17 +1323,17 @@ def local_percentile(data, axis, indices):

# sanitize q
if isinstance(q, list) or isinstance(q, tuple):
t_perc_dtype = torch.promote_types(type(q[0]), t_x.dtype)
t_perc_dtype = torch.promote_types(type(q[0]), torch.float32)
t_q = torch.tensor(q, dtype=t_perc_dtype, device=t_x.device)
elif np.isscalar(q):
t_perc_dtype = torch.promote_types(type(q), t_x.dtype)
t_perc_dtype = torch.promote_types(type(q), torch.float32)
t_q = torch.tensor([q], dtype=t_perc_dtype, device=t_x.device)
elif isinstance(q, dndarray.DNDarray):
if x.comm.is_distributed() and q.split is not None:
# q needs to be local
q.resplit_(axis=None)
t_q = q._DNDarray__array
t_perc_dtype = torch.promote_types(t_q.dtype, t_x.dtype)
t_perc_dtype = torch.promote_types(t_q.dtype, torch.float32)
else:
raise TypeError("DNDarray, list or tuple supported, but q was {}".format(type(q)))

Expand Down Expand Up @@ -1407,8 +1407,8 @@ def local_percentile(data, axis, indices):

if split == axis:
# map percentile location: which q on what rank
t_indices_map = torch.ones((size, nperc), dtype=t_q.dtype, device=t_q.device) * -1.0
t_local_indices = torch.ones((1, nperc), dtype=t_q.dtype, device=t_q.device) * -1.0
t_indices_map = torch.ones((size, nperc), dtype=t_indices.dtype, device=t_q.device) * -1
t_local_indices = torch.ones((1, nperc), dtype=t_indices.dtype, device=t_q.device) * -1
offset, _, chunk = x.comm.chunk(gshape, split)
chunk_start = chunk[split].start
chunk_stop = chunk[split].stop
Expand All @@ -1429,15 +1429,19 @@ def local_percentile(data, axis, indices):
data.get_halo(1)
t_data = data.array_with_halos
# fill out percentile
t_ind_on_rank -= offset
t_map_sum = t_indices_map.sum(axis=1)
perc_ranks = torch.where(t_map_sum > -1 * nperc)[0].tolist()
for r in perc_ranks:
for r_id, r in enumerate(perc_ranks):
# chunk of the global percentile that will be populated by rank r
_, _, perc_chunk = x.comm.chunk(output_shape, join, rank=r, w_size=len(perc_ranks))
_, _, perc_chunk = x.comm.chunk(output_shape, join, rank=r_id, w_size=len(perc_ranks))
perc_slice = perc_slice[:join] + (perc_chunk[join],) + perc_slice[join + 1 :]
local_p = factories.zeros(percentile[perc_slice].shape, dtype=perc_dtype, comm=x.comm)
if rank == r:
local_p = factories.array(local_percentile(t_data, axis, t_ind_on_rank - offset))
if rank > 0:
# correct indices for halo
t_ind_on_rank += 1
local_p = factories.array(local_percentile(t_data, axis, t_ind_on_rank))
x.comm.Bcast(local_p, root=r)
percentile[perc_slice] = local_p
else:
Expand Down
8 changes: 7 additions & 1 deletion heat/core/tests/test_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,13 @@ def test_percentile(self):
q = 100
p_np = np.percentile(x_np, q, axis=0)
p_ht = ht.percentile(x_ht, q, axis=0)
self.assertAlmostEqual(p_ht.numpy().all(), p_np.all())
self.assertTrue((p_ht.numpy() == p_np).all())

# test median (q = 50)
q = 50
p_np = np.percentile(x_np, q, axis=0)
p_ht = ht.median(x_ht_split0, axis=0)
self.assertTrue((p_ht.numpy() == p_np).all())

# test list q and writing to output buffer
q = [0.1, 2.3, 15.9, 50.0, 84.1, 97.7, 99.9]
Expand Down