-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy path_edistance.py
More file actions
1159 lines (985 loc) · 38.7 KB
/
_edistance.py
File metadata and controls
1159 lines (985 loc) · 38.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from collections.abc import Sequence
import cupy as cp
import numpy as np
import pandas as pd
from rapids_singlecell._cuda import _edistance_cuda as _ed
from rapids_singlecell._utils import (
_calculate_blocks_per_pair,
_create_category_index_mapping,
_split_pairs,
)
from rapids_singlecell.squidpy_gpu._utils import _assert_categorical_obs
from ._base_metric import BaseMetric, parse_device_ids
if TYPE_CHECKING:
from anndata import AnnData
class EDistanceMetric(BaseMetric):
"""
GPU-accelerated Energy Distance metric.
Energy distance is a statistical distance between probability distributions
that generalizes the Euclidean distance to distributions. It is particularly
useful for comparing groups of cells in high-dimensional spaces.
Parameters
----------
layer_key
Key in adata.layers for cell data. Mutually exclusive with obsm_key.
obsm_key
Key in adata.obsm for embeddings (default: 'X_pca')
References
----------
Székely, G. J., & Rizzo, M. L. (2013).
Energy statistics: A class of statistics based on distances.
Journal of Statistical Planning and Inference, 143(8), 1249-1272.
"""
supports_multi_gpu: bool = True
def __init__(
self,
layer_key: str | None = None,
obsm_key: str | None = "X_pca",
):
"""Initialize energy distance metric."""
if layer_key is not None and obsm_key is not None:
raise ValueError(
"Cannot use 'layer_key' and 'obsm_key' at the same time. "
"Please provide only one of the two keys."
)
super().__init__(obsm_key=obsm_key)
self.layer_key = layer_key
def pairwise(
self,
adata: AnnData,
groupby: str,
*,
groups: Sequence[str] | None = None,
bootstrap: bool = False,
n_bootstrap: int = 100,
random_state: int = 0,
multi_gpu: bool | list[int] | str | None = None,
) -> pd.DataFrame | tuple[pd.DataFrame, pd.DataFrame]:
"""
Compute pairwise energy distances between all cell groups.
Returns a DataFrame containing pairwise distances. When bootstrap=True,
returns a tuple of (distances, distances_var) DataFrames.
The distances DataFrame contains:
distances[a,b] = 2*d[a,b] - d[a] - d[b]
When bootstrap=True, distances_var contains:
distances_var[a,b] = 4*d_var[a,b] + d_var[a] + d_var[b]
Parameters
----------
adata
Annotated data matrix
groupby
Key in adata.obs for grouping
groups
Specific groups to compute (if None, use all)
bootstrap
Whether to compute bootstrap variance estimates
n_bootstrap
Number of bootstrap iterations (if bootstrap=True)
random_state
Random seed for reproducibility
multi_gpu
GPU selection:
- None: Use all GPUs if metric supports it, else GPU 0 (default)
- True: Use all available GPUs
- False: Use only GPU 0
- list[int]: Use specific GPU IDs (e.g., [0, 2])
- str: Comma-separated GPU IDs (e.g., "0,2")
Returns
-------
result
DataFrame with pairwise distances. If bootstrap=True, returns
tuple of (distances, distances_var) DataFrames.
"""
_assert_categorical_obs(adata, key=groupby)
embedding = self._get_embedding(adata)
original_groups = adata.obs[groupby]
group_map = {v: i for i, v in enumerate(original_groups.cat.categories.values)}
group_labels = cp.array([group_map[c] for c in original_groups], dtype=cp.int32)
# Use harmony's category mapping
k = len(group_map)
cat_offsets, cell_indices = _create_category_index_mapping(group_labels, k)
all_groups = list(original_groups.cat.categories.values)
groups_list = all_groups if groups is None else groups
if not bootstrap:
return self._prepare_edistance_df(
embedding=embedding,
cat_offsets=cat_offsets,
cell_indices=cell_indices,
k=k,
all_groups=all_groups,
groups_list=groups_list,
groupby=groupby,
multi_gpu=multi_gpu,
)
return self._prepare_edistance_df_bootstrap(
embedding=embedding,
cat_offsets=cat_offsets,
cell_indices=cell_indices,
k=k,
all_groups=all_groups,
groups_list=groups_list,
groupby=groupby,
n_bootstrap=n_bootstrap,
random_state=random_state,
multi_gpu=multi_gpu,
)
def onesided_distances(
self,
adata: AnnData,
groupby: str,
selected_group: str | Sequence[str],
*,
groups: Sequence[str] | None = None,
bootstrap: bool = False,
n_bootstrap: int = 100,
random_state: int = 0,
multi_gpu: bool | list[int] | str | None = None,
) -> pd.DataFrame | tuple[pd.DataFrame, pd.DataFrame]:
"""
Compute energy distances from selected reference group(s) to all other groups.
Parameters
----------
adata
Annotated data matrix
groupby
Key in adata.obs for grouping cells
selected_group
Reference group(s) to compute distances from. Can be a single
group name or a sequence of group names for multiple controls.
groups
Specific groups to compute distances to (if None, use all)
bootstrap
Whether to compute bootstrap variance estimates
n_bootstrap
Number of bootstrap iterations (if bootstrap=True)
random_state
Random seed for reproducibility
multi_gpu
GPU selection:
- None: Use all GPUs if metric supports it, else GPU 0 (default)
- True: Use all available GPUs
- False: Use only GPU 0
- list[int]: Use specific GPU IDs (e.g., [0, 2])
- str: Comma-separated GPU IDs (e.g., "0,2")
Returns
-------
distances
DataFrame with groups as index and selected_group(s) as columns.
If bootstrap=True, returns tuple of (distances, distances_var).
"""
_assert_categorical_obs(adata, key=groupby)
# Normalize selected_group to a list
if isinstance(selected_group, str):
selected_groups = [selected_group]
else:
selected_groups = list(selected_group)
embedding = self._get_embedding(adata)
original_groups = adata.obs[groupby]
group_map = {v: i for i, v in enumerate(original_groups.cat.categories.values)}
for sg in selected_groups:
if sg not in group_map:
raise ValueError(
f"Selected group '{sg}' not found in groupby '{groupby}'"
)
group_labels = cp.array([group_map[c] for c in original_groups], dtype=cp.int32)
k = len(group_map)
cat_offsets, cell_indices = _create_category_index_mapping(group_labels, k)
all_groups = list(original_groups.cat.categories.values)
groups_list = all_groups if groups is None else list(groups)
selected_indices = [group_map[sg] for sg in selected_groups]
device_ids = parse_device_ids(multi_gpu=multi_gpu)
if bootstrap:
onesided_means, onesided_vars = self._onesided_means_bootstrap(
embedding=embedding,
cat_offsets=cat_offsets,
cell_indices=cell_indices,
k=k,
selected_indices=selected_indices,
n_bootstrap=n_bootstrap,
random_state=random_state,
device_ids=device_ids,
)
diag_means = cp.diag(onesided_means)
diag_vars = cp.diag(onesided_vars)
# Compute energy distances for each control:
# e[s,b] = 2*d[s,b] - d[s,s] - d[b,b]
ed_cols = {}
var_cols = {}
for sg, si in zip(selected_groups, selected_indices):
ed_row = 2 * onesided_means[si, :] - diag_means[si] - diag_means
ed_row[si] = 0.0
ed_cols[sg] = ed_row.get()
var_row = 4 * onesided_vars[si, :] + diag_vars[si] + diag_vars
var_row[si] = 0.0
var_cols[sg] = var_row.get()
distances = pd.DataFrame(ed_cols, index=all_groups)
distances.index.name = groupby
distances.columns.name = "selected_group"
variances = pd.DataFrame(var_cols, index=all_groups)
variances.index.name = groupby
variances.columns.name = "selected_group"
if groups_list != all_groups:
distances = distances.loc[groups_list]
variances = variances.loc[groups_list]
return distances, variances
# Non-bootstrap path
onesided_means = self._onesided_means(
embedding,
cat_offsets,
cell_indices,
k,
selected_indices=selected_indices,
device_ids=device_ids,
)
# Compute energy distances for each control:
# e[s,b] = 2*d[s,b] - d[s,s] - d[b,b]
diag = cp.diag(onesided_means)
ed_cols = {}
for sg, si in zip(selected_groups, selected_indices):
ed_row = 2 * onesided_means[si, :] - diag[si] - diag
ed_row[si] = 0.0
ed_cols[sg] = ed_row.get()
df = pd.DataFrame(ed_cols, index=all_groups)
df.index.name = groupby
df.columns.name = "selected_group"
if groups_list != all_groups:
df = df.loc[groups_list]
return df
def bootstrap(
self,
adata: AnnData,
groupby: str,
group_a: str,
group_b: str,
*,
n_bootstrap: int = 100,
random_state: int = 0,
multi_gpu: bool | list[int] | str | None = None,
) -> tuple[float, float]:
"""
Compute bootstrap mean and variance for energy distance between two groups.
Parameters
----------
adata
Annotated data matrix
groupby
Key in adata.obs for grouping cells
group_a
First group name
group_b
Second group name
n_bootstrap
Number of bootstrap iterations
random_state
Random seed for reproducibility
multi_gpu
GPU selection:
- None: Use all GPUs if metric supports it, else GPU 0 (default)
- True: Use all available GPUs
- False: Use only GPU 0
- list[int]: Use specific GPU IDs (e.g., [0, 2])
- str: Comma-separated GPU IDs (e.g., "0,2")
Returns
-------
mean
Bootstrap mean distance
variance
Bootstrap variance
"""
# Compute pairwise with bootstrap
df, df_var = self.pairwise(
adata=adata,
groupby=groupby,
groups=[group_a, group_b],
bootstrap=True,
n_bootstrap=n_bootstrap,
random_state=random_state,
multi_gpu=multi_gpu,
)
mean = df.loc[group_a, group_b]
variance = df_var.loc[group_a, group_b]
return float(mean), float(variance)
# Helper methods
def _get_embedding(self, adata: AnnData) -> cp.ndarray:
"""Get embedding from adata using layer_key or obsm_key.
Preserves the input dtype (float32 or float64) for precision control.
"""
if self.layer_key:
data = adata.layers[self.layer_key]
else:
data = adata.obsm[self.obsm_key]
# Convert to cupy array if needed, preserving dtype
if isinstance(data, cp.ndarray):
return data
return cp.asarray(data)
def compute_distance(
self,
X: np.ndarray | cp.ndarray,
Y: np.ndarray | cp.ndarray,
) -> float:
"""
Compute energy distance between two arrays directly.
Parameters
----------
X
First array of shape (n_samples_x, n_features)
Y
Second array of shape (n_samples_y, n_features)
Returns
-------
float
Energy distance between X and Y
"""
# Convert to cupy arrays, preserving dtype
X_gpu = cp.asarray(X)
Y_gpu = cp.asarray(Y)
if len(X_gpu) == 0 or len(Y_gpu) == 0:
raise ValueError("Neither X nor Y can be empty.")
# Compute mean pairwise distances
d_xy = self._mean_pairwise_distance(X_gpu, Y_gpu)
d_xx = self._mean_pairwise_distance_within(X_gpu)
d_yy = self._mean_pairwise_distance_within(Y_gpu)
# Energy distance formula
return float(2 * d_xy - d_xx - d_yy)
def bootstrap_arrays(
self,
X: np.ndarray | cp.ndarray,
Y: np.ndarray | cp.ndarray,
*,
n_bootstrap: int = 100,
random_state: int = 0,
) -> tuple[float, float]:
"""
Compute bootstrap mean and variance for energy distance between arrays.
Parameters
----------
X
First array of shape (n_samples_x, n_features)
Y
Second array of shape (n_samples_y, n_features)
n_bootstrap
Number of bootstrap iterations
random_state
Random seed for reproducibility
Returns
-------
mean
Bootstrap mean distance
variance
Bootstrap variance
"""
# Convert to cupy arrays, preserving dtype
X_gpu = cp.asarray(X)
Y_gpu = cp.asarray(Y)
if len(X_gpu) == 0 or len(Y_gpu) == 0:
raise ValueError("Neither X nor Y can be empty.")
rng = np.random.default_rng(random_state)
distances = []
for _ in range(n_bootstrap):
# Bootstrap sample indices
X_idx = rng.choice(len(X_gpu), size=len(X_gpu), replace=True)
Y_idx = rng.choice(len(Y_gpu), size=len(Y_gpu), replace=True)
X_boot = X_gpu[X_idx]
Y_boot = Y_gpu[Y_idx]
# Compute distance for this bootstrap sample
d_xy = self._mean_pairwise_distance(X_boot, Y_boot)
d_xx = self._mean_pairwise_distance_within(X_boot)
d_yy = self._mean_pairwise_distance_within(Y_boot)
distance = 2 * d_xy - d_xx - d_yy
distances.append(float(distance))
return float(np.mean(distances)), float(np.var(distances))
def _mean_pairwise_distance(
self,
X: cp.ndarray,
Y: cp.ndarray,
) -> float:
"""Compute mean pairwise Euclidean distance between X and Y."""
# Use cdist-like computation on GPU
# X: (n, d), Y: (m, d) -> dist[i,j] = ||X[i] - Y[j]||
# Compute squared distances using broadcasting
# ||x - y||^2 = ||x||^2 + ||y||^2 - 2*x.y
X_sq = cp.sum(X * X, axis=1, keepdims=True) # (n, 1)
Y_sq = cp.sum(Y * Y, axis=1, keepdims=True) # (m, 1)
XY = X @ Y.T # (n, m)
dist_sq = X_sq + Y_sq.T - 2 * XY
dist_sq = cp.maximum(dist_sq, 0) # Numerical stability
distances = cp.sqrt(dist_sq)
return float(cp.mean(distances))
def _mean_pairwise_distance_within(self, X: cp.ndarray) -> float:
"""Compute mean pairwise Euclidean distance within X (upper triangle only)."""
n = len(X)
if n < 2:
return 0.0
# Compute all pairwise distances
X_sq = cp.sum(X * X, axis=1, keepdims=True)
dist_sq = X_sq + X_sq.T - 2 * (X @ X.T)
dist_sq = cp.maximum(dist_sq, 0)
distances = cp.sqrt(dist_sq)
# Get upper triangle (excluding diagonal)
triu_indices = cp.triu_indices(n, k=1)
upper_distances = distances[triu_indices]
return float(cp.mean(upper_distances))
# Internal methods from original _edistance.py
def _pairwise_means(
self,
embedding: cp.ndarray,
cat_offsets: cp.ndarray,
cell_indices: cp.ndarray,
k: int,
device_ids: list[int],
) -> cp.ndarray:
"""Compute between-group mean distances for all group pairs.
Splits pairs across specified GPUs and aggregates results on GPU 0.
Parameters
----------
embedding
Cell embeddings on GPU 0
cat_offsets
Category offsets on GPU 0
cell_indices
Cell indices on GPU 0
k
Number of groups
device_ids
List of GPU device IDs to use
Returns
-------
cp.ndarray
Matrix of mean pairwise distances (k x k)
"""
n_devices = len(device_ids)
_, n_features = embedding.shape
# Get group sizes to filter out single-cell diagonal pairs
group_sizes = cp.diff(cat_offsets).astype(cp.int64)
# Build upper triangular indices, excluding diagonal for single-cell groups
triu_indices = cp.triu_indices(k)
pair_left = triu_indices[0].astype(cp.int32)
pair_right = triu_indices[1].astype(cp.int32)
# Filter out diagonal pairs where group has < 2 cells
is_diagonal = pair_left == pair_right
has_pairs = group_sizes[pair_left] >= 2
keep_mask = ~is_diagonal | has_pairs
pair_left = pair_left[keep_mask]
pair_right = pair_right[keep_mask]
num_pairs = len(pair_left)
if num_pairs == 0:
# No pairs to compute
norm_matrix = self._compute_norm_matrix(group_sizes, embedding.dtype)
return cp.zeros((k, k), dtype=embedding.dtype) / norm_matrix
# Split pairs across devices with load balancing
pair_chunks = _split_pairs(pair_left, pair_right, n_devices, group_sizes)
# Phase 1: Create streams and start async data transfer to all devices
streams = {}
device_data = []
for i, device_id in enumerate(device_ids):
chunk_left, chunk_right = pair_chunks[i]
if len(chunk_left) == 0:
device_data.append(None)
continue
with cp.cuda.Device(device_id):
# Create non-blocking stream for this device
streams[device_id] = cp.cuda.Stream(non_blocking=True)
with streams[device_id]:
# Replicate data to this device (async on stream)
if device_id == 0:
dev_emb = embedding
dev_off = cat_offsets
dev_idx = cell_indices
else:
dev_emb = cp.asarray(embedding)
dev_off = cp.asarray(cat_offsets)
dev_idx = cp.asarray(cell_indices)
# Copy pair indices to this device
dev_pair_left = cp.asarray(chunk_left)
dev_pair_right = cp.asarray(chunk_right)
# Initialize local accumulator
dev_sums = cp.zeros((k, k), dtype=embedding.dtype)
device_data.append(
{
"emb": dev_emb,
"off": dev_off,
"idx": dev_idx,
"pair_left": dev_pair_left,
"pair_right": dev_pair_right,
"sums": dev_sums,
"n_pairs": len(dev_pair_left),
"device_id": device_id,
}
)
# Phase 2: Synchronize data transfers, then launch kernels
for data in device_data:
if data is None:
continue
device_id = data["device_id"]
with cp.cuda.Device(device_id):
# Wait for data transfer to complete on this device
streams[device_id].synchronize()
# Launch kernel (on default stream, async)
is_double = embedding.dtype == np.float64
config = _ed.get_kernel_config(n_features, is_double)
if config is None:
raise RuntimeError(
"Insufficient shared memory for edistance kernel"
)
cell_tile, feat_tile, block_size, shared_mem = config
blocks_per_pair = _calculate_blocks_per_pair(data["n_pairs"])
_ed.compute_distances(
data["emb"],
data["off"],
data["idx"],
data["pair_left"],
data["pair_right"],
data["sums"],
data["n_pairs"],
k,
n_features,
blocks_per_pair,
cell_tile,
feat_tile,
block_size,
shared_mem,
cp.cuda.get_current_stream().ptr,
)
# Phase 3: Synchronize all devices (wait for kernels to complete)
for data in device_data:
if data is not None:
with cp.cuda.Device(data["device_id"]):
cp.cuda.Stream.null.synchronize()
# Phase 4: Aggregate on GPU 0
with cp.cuda.Device(0):
pairwise_sums = cp.zeros((k, k), dtype=embedding.dtype)
for data in device_data:
if data is not None:
dev0_sums = cp.asarray(data["sums"])
pairwise_sums += dev0_sums
# Normalize sums to means
norm_matrix = self._compute_norm_matrix(group_sizes, embedding.dtype)
return pairwise_sums / norm_matrix
def _compute_norm_matrix(
self, group_sizes: cp.ndarray, dtype: np.dtype
) -> cp.ndarray:
"""Compute normalization matrix for pairwise means.
Parameters
----------
group_sizes
Array of group sizes
dtype
Data type for output matrix
Returns
-------
cp.ndarray
Normalization matrix (k x k)
"""
diag_counts = group_sizes * (group_sizes - 1) // 2
# Handle single-cell groups: replace 0 with 1 to avoid division by zero
diag_counts = cp.maximum(diag_counts, 1)
cross_counts = cp.outer(group_sizes, group_sizes)
norm_matrix = cross_counts.astype(dtype)
cp.fill_diagonal(norm_matrix, diag_counts.astype(dtype))
return norm_matrix
def _onesided_means(
self,
embedding: cp.ndarray,
cat_offsets: cp.ndarray,
cell_indices: cp.ndarray,
k: int,
*,
selected_indices: list[int],
device_ids: list[int],
) -> cp.ndarray:
"""Compute mean distances from selected group(s) to all groups.
Splits pairs across specified GPUs and aggregates results on GPU 0.
Computes:
- d[s, i] for each s in selected_indices, for all i (cross-distances)
- d[i, i] for non-selected groups (self-distances for energy distance)
Parameters
----------
embedding
Cell embeddings on GPU 0
cat_offsets
Category offsets on GPU 0
cell_indices
Cell indices on GPU 0
k
Number of groups
selected_indices
Indices of the selected (control) groups
device_ids
List of GPU device IDs to use
Returns
-------
cp.ndarray
Matrix of mean onesided distances (k x k)
"""
n_devices = len(device_ids)
_, n_features = embedding.shape
# Get group sizes
group_sizes = cp.diff(cat_offsets).astype(cp.int64)
# Build pairs for onesided computation.
# The kernel symmetrizes: for pair (a,b) it writes to both
# sums[a,b] and sums[b,a]. So we must avoid having both (i,j)
# and (j,i) in the pair list to prevent double-counting.
selected_set = set(selected_indices)
# Collect unique pairs as a set of (min, max) tuples
pair_set: set[tuple[int, int]] = set()
# Cross pairs: (s, i) for each selected s and all i
for si in selected_indices:
for i in range(k):
pair_set.add((min(si, i), max(si, i)))
# Diagonal pairs for non-selected groups with >= 2 cells
for i in range(k):
if i not in selected_set and int(group_sizes[i]) >= 2:
pair_set.add((i, i))
pairs = sorted(pair_set)
pair_left = cp.array([p[0] for p in pairs], dtype=cp.int32)
pair_right = cp.array([p[1] for p in pairs], dtype=cp.int32)
num_pairs = len(pair_left)
if num_pairs == 0:
norm_matrix = self._compute_norm_matrix(group_sizes, embedding.dtype)
return cp.zeros((k, k), dtype=embedding.dtype) / norm_matrix
# Split pairs across devices with load balancing
pair_chunks = _split_pairs(pair_left, pair_right, n_devices, group_sizes)
# Phase 1: Create streams and start async data transfer to all devices
streams = {}
device_data = []
for i, device_id in enumerate(device_ids):
chunk_left, chunk_right = pair_chunks[i]
if len(chunk_left) == 0:
device_data.append(None)
continue
with cp.cuda.Device(device_id):
# Create non-blocking stream for this device
streams[device_id] = cp.cuda.Stream(non_blocking=True)
with streams[device_id]:
# Replicate data to this device (async on stream)
if device_id == device_ids[0]:
dev_emb = embedding
dev_off = cat_offsets
dev_idx = cell_indices
else:
dev_emb = cp.asarray(embedding)
dev_off = cp.asarray(cat_offsets)
dev_idx = cp.asarray(cell_indices)
dev_pair_left = cp.asarray(chunk_left)
dev_pair_right = cp.asarray(chunk_right)
dev_sums = cp.zeros((k, k), dtype=embedding.dtype)
device_data.append(
{
"emb": dev_emb,
"off": dev_off,
"idx": dev_idx,
"pair_left": dev_pair_left,
"pair_right": dev_pair_right,
"sums": dev_sums,
"n_pairs": len(dev_pair_left),
"device_id": device_id,
}
)
# Phase 2: Synchronize data transfers, then launch kernels
for data in device_data:
if data is None:
continue
device_id = data["device_id"]
with cp.cuda.Device(device_id):
# Wait for data transfer to complete on this device
streams[device_id].synchronize()
# Launch kernel (on default stream, async)
is_double = embedding.dtype == np.float64
config = _ed.get_kernel_config(n_features, is_double)
if config is None:
raise RuntimeError(
"Insufficient shared memory for edistance kernel"
)
cell_tile, feat_tile, block_size, shared_mem = config
blocks_per_pair = _calculate_blocks_per_pair(data["n_pairs"])
_ed.compute_distances(
data["emb"],
data["off"],
data["idx"],
data["pair_left"],
data["pair_right"],
data["sums"],
data["n_pairs"],
k,
n_features,
blocks_per_pair,
cell_tile,
feat_tile,
block_size,
shared_mem,
cp.cuda.get_current_stream().ptr,
)
# Phase 3: Synchronize all devices (wait for kernels to complete)
for data in device_data:
if data is not None:
with cp.cuda.Device(data["device_id"]):
cp.cuda.Stream.null.synchronize()
# Phase 4: Aggregate on GPU 0
with cp.cuda.Device(device_ids[0]):
onesided_sums = cp.zeros((k, k), dtype=embedding.dtype)
for data in device_data:
if data is not None:
dev0_sums = cp.asarray(data["sums"])
onesided_sums += dev0_sums
norm_matrix = self._compute_norm_matrix(group_sizes, embedding.dtype)
return onesided_sums / norm_matrix
def _pairwise_means_bootstrap(
self,
embedding: cp.ndarray,
*,
cat_offsets: cp.ndarray,
cell_indices: cp.ndarray,
k: int,
n_bootstrap: int,
random_state: int,
device_ids: list[int],
) -> tuple[cp.ndarray, cp.ndarray]:
"""Compute bootstrap statistics for pairwise distances.
Each bootstrap iteration uses all GPUs for its pairwise computation.
Parameters
----------
embedding
Cell embeddings on GPU 0
cat_offsets
Category offsets on GPU 0
cell_indices
Cell indices on GPU 0
k
Number of groups
n_bootstrap
Number of bootstrap iterations
random_state
Random seed for reproducibility
device_ids
List of GPU device IDs to use
Returns
-------
tuple
(means, variances) matrices (k x k each)
"""
# Get group sizes for bootstrap sampling (on GPU 0)
group_sizes = cp.diff(cat_offsets)
# Run bootstrap iterations - each uses all GPUs for pairwise computation
all_results = []
for i in range(n_bootstrap):
# Generate bootstrap sample on GPU 0
boot_cat_offsets, boot_cell_indices = self._bootstrap_sample_cells(
cat_offsets=cat_offsets,
cell_indices=cell_indices,
group_sizes_gpu=group_sizes,
seed=random_state + i,
)
# Compute pairwise means using all GPUs
pairwise_means = self._pairwise_means(
embedding=embedding,
cat_offsets=boot_cat_offsets,
cell_indices=boot_cell_indices,
k=k,
device_ids=device_ids,
)
all_results.append(pairwise_means.get())
# Compute statistics on first GPU
with cp.cuda.Device(device_ids[0]):
bootstrap_stack = cp.array(all_results) # [n_bootstrap, k, k]
means = cp.mean(bootstrap_stack, axis=0)
variances = cp.var(bootstrap_stack, axis=0)
return means, variances
def _onesided_means_bootstrap(
self,
embedding: cp.ndarray,
*,
cat_offsets: cp.ndarray,
cell_indices: cp.ndarray,
k: int,
selected_indices: list[int],
n_bootstrap: int,
random_state: int,
device_ids: list[int],
) -> tuple[cp.ndarray, cp.ndarray]:
"""Compute bootstrap statistics for onesided distances.
Each bootstrap iteration uses all GPUs for its onesided computation.
Parameters
----------
embedding
Cell embeddings on GPU 0
cat_offsets
Category offsets on GPU 0
cell_indices
Cell indices on GPU 0
k
Number of groups
selected_indices
Indices of the selected (control) groups
n_bootstrap
Number of bootstrap iterations
random_state
Random seed for reproducibility
device_ids
List of GPU device IDs to use
Returns
-------
tuple
(means, variances) matrices (k x k each)
"""
# Get group sizes for bootstrap sampling (on GPU 0)
group_sizes = cp.diff(cat_offsets)
# Run bootstrap iterations - each uses all GPUs for onesided computation
all_results = []
for i in range(n_bootstrap):
# Generate bootstrap sample on GPU 0
boot_cat_offsets, boot_cell_indices = self._bootstrap_sample_cells(
cat_offsets=cat_offsets,
cell_indices=cell_indices,
group_sizes_gpu=group_sizes,
seed=random_state + i,
)
# Compute onesided means using all GPUs
onesided_means = self._onesided_means(
embedding=embedding,
cat_offsets=boot_cat_offsets,
cell_indices=boot_cell_indices,
k=k,
selected_indices=selected_indices,
device_ids=device_ids,
)
all_results.append(onesided_means.get())
# Compute statistics on first GPU
with cp.cuda.Device(device_ids[0]):
bootstrap_stack = cp.array(all_results)