-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
133 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,116 @@ | ||
## Scatter mean function (courtesy of ChatGPT) | ||
## Adapted from https://github.com/mir-group/pytorch_runstats | ||
|
||
import torch | ||
"""basic scatter operations from torch_scatter | ||
Using code from https://github.com/rusty1s/pytorch_scatter, but cut down to avoid a dependency. | ||
def scatter_mean(input, index=None, dim=None): | ||
if index is not None: | ||
# Case 1: Index is specified | ||
output_size = index.max().tolist() + 1 | ||
output = torch.zeros(output_size, input.size(1), device=input.device) | ||
n = torch.zeros(output_size, device=input.device) | ||
PyTorch plans to move these features into the main repo, but until then, | ||
to make installation simpler, we need this pure python set of wrappers | ||
that don't require installing PyTorch C++ extensions. | ||
See https://github.com/pytorch/pytorch/issues/63780. | ||
""" | ||
|
||
for i in range(input.size(0)): | ||
idx = index[i] | ||
n[idx] += 1 | ||
output[idx] += (input[i] - output[idx]) / n[idx] | ||
from typing import Optional | ||
|
||
return output | ||
import torch | ||
|
||
elif dim is not None: | ||
# Case 2: Index is skipped, output_dim is specified | ||
output = torch.zeros(len(dim), input.size(1), device=input.device) | ||
|
||
start_idx = 0 | ||
for i, dim in enumerate(dim): | ||
end_idx = start_idx + dim | ||
if dim > 0: | ||
segment_sum = input[start_idx:end_idx].sum(dim=0) | ||
output[i] = segment_sum / dim | ||
start_idx = end_idx | ||
def _broadcast(src: torch.Tensor, other: torch.Tensor, dim: int): | ||
if dim < 0: | ||
dim = other.dim() + dim | ||
if src.dim() == 1: | ||
for _ in range(0, dim): | ||
src = src.unsqueeze(0) | ||
for _ in range(src.dim(), other.dim()): | ||
src = src.unsqueeze(-1) | ||
src = src.expand_as(other) | ||
return src | ||
|
||
return output | ||
|
||
def scatter( | ||
src: torch.Tensor, | ||
index: torch.Tensor, | ||
dim: int = -1, | ||
out: Optional[torch.Tensor] = None, | ||
dim_size: Optional[int] = None, | ||
reduce: str = "sum", | ||
) -> torch.Tensor: | ||
assert reduce == "sum" # for now, TODO | ||
index = _broadcast(index, src, dim) | ||
if out is None: | ||
size = list(src.size()) | ||
if dim_size is not None: | ||
size[dim] = dim_size | ||
elif index.numel() == 0: | ||
size[dim] = 0 | ||
else: | ||
size[dim] = int(index.max()) + 1 | ||
out = torch.zeros(size, dtype=src.dtype, device=src.device) | ||
return out.scatter_add_(dim, index, src) | ||
else: | ||
raise ValueError("Either 'index' or 'dim' must be specified.") | ||
return out.scatter_add_(dim, index, src) | ||
|
||
|
||
def scatter_std( | ||
src: torch.Tensor, | ||
index: torch.Tensor, | ||
dim: int = -1, | ||
out: Optional[torch.Tensor] = None, | ||
dim_size: Optional[int] = None, | ||
unbiased: bool = True, | ||
) -> torch.Tensor: | ||
|
||
if out is not None: | ||
dim_size = out.size(dim) | ||
|
||
if dim < 0: | ||
dim = src.dim() + dim | ||
|
||
count_dim = dim | ||
if index.dim() <= dim: | ||
count_dim = index.dim() - 1 | ||
|
||
# # Example usage for Case 1 (index specified): | ||
# input1 = torch.randn(3000, 144) | ||
# index1 = torch.randint(0, 1000, (3000,)) | ||
# output1 = scatter_mean(input1, index=index1) | ||
# print("Output shape (Case 1):", output1.shape) | ||
ones = torch.ones(index.size(), dtype=src.dtype, device=src.device) | ||
count = scatter(ones, index, count_dim, dim_size=dim_size) | ||
|
||
# # Example usage for Case 2 (index skipped, output_dim specified): | ||
# input2 = torch.randn(3000, 144) | ||
# output_dim = [3000] | ||
# output2 = scatter_mean(input2, dim=output_dim) | ||
# print("Output shape (Case 2):", output2.shape) | ||
index = _broadcast(index, src, dim) | ||
tmp = scatter(src, index, dim, dim_size=dim_size) | ||
count = _broadcast(count, tmp, dim).clamp(1) | ||
mean = tmp.div(count) | ||
|
||
# # Example usage for Case 3 (both spe): | ||
# input = torch.randn(3000, 144) | ||
# index = torch.randint(0, 1000, (3000,)) | ||
# output_dim = [3000] | ||
var = src - mean.gather(dim, index) | ||
var = var * var | ||
out = scatter(var, index, dim, out, dim_size) | ||
|
||
# output = scatter_mean(input, index, output_dim) | ||
# print(output.size()) # Should print torch.Size([1000, 144]) | ||
if unbiased: | ||
count = count.sub(1).clamp_(1) | ||
out = out.div(count + 1e-6).sqrt() | ||
|
||
return out | ||
|
||
|
||
def scatter_mean( | ||
src: torch.Tensor, | ||
index: torch.Tensor, | ||
dim: int = -1, | ||
out: Optional[torch.Tensor] = None, | ||
dim_size: Optional[int] = None, | ||
) -> torch.Tensor: | ||
out = scatter(src, index, dim, out, dim_size) | ||
dim_size = out.size(dim) | ||
|
||
index_dim = dim | ||
if index_dim < 0: | ||
index_dim = index_dim + src.dim() | ||
if index.dim() <= index_dim: | ||
index_dim = index.dim() - 1 | ||
|
||
ones = torch.ones(index.size(), dtype=src.dtype, device=src.device) | ||
count = scatter(ones, index, index_dim, None, dim_size) | ||
count[count < 1] = 1 | ||
count = _broadcast(count, out, dim) | ||
if out.is_floating_point(): | ||
out.true_divide_(count) | ||
else: | ||
out.div_(count, rounding_mode="floor") | ||
return out |