This repository has been archived by the owner on Dec 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support masking of unconnected nodes (LAM) (#171)
Co-authored-by: Harrison Cook <[email protected]>
- Loading branch information
Showing
8 changed files
with
147 additions
and
23 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
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 |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# (C) Copyright 2024 Anemoi contributors. | ||
# | ||
# This software is licensed under the terms of the Apache Licence Version 2.0 | ||
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. | ||
# | ||
# In applying this licence, ECMWF does not waive the privileges and immunities | ||
# granted to it by virtue of its status as an intergovernmental organisation | ||
# nor does it submit to any jurisdiction. | ||
|
||
from __future__ import annotations | ||
|
||
import logging | ||
from abc import ABC | ||
from abc import abstractmethod | ||
from collections.abc import Sequence | ||
from typing import TYPE_CHECKING | ||
from typing import Union | ||
|
||
if TYPE_CHECKING: | ||
from torch_geometric.data import HeteroData | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
ArrayIndex = Union[slice, int, Sequence[int]] | ||
|
||
|
||
class BaseGridIndices(ABC): | ||
"""Base class for custom grid indices.""" | ||
|
||
def __init__(self, nodes_name: str, reader_group_size: int) -> None: | ||
self.nodes_name = nodes_name | ||
self.reader_group_size = reader_group_size | ||
|
||
def setup(self, graph: HeteroData) -> None: | ||
self.grid_size = self.compute_grid_size(graph) | ||
|
||
def split_seq_in_shards(self, reader_group_rank: int) -> tuple[int, int]: | ||
"""Get the indices to split a sequence into equal size shards.""" | ||
grid_shard_size = self.grid_size // self.reader_group_size | ||
grid_start = reader_group_rank * grid_shard_size | ||
if reader_group_rank == self.reader_group_size - 1: | ||
grid_end = self.grid_size | ||
else: | ||
grid_end = (reader_group_rank + 1) * grid_shard_size | ||
|
||
return slice(grid_start, grid_end) | ||
|
||
@abstractmethod | ||
def compute_grid_size(self, graph: HeteroData) -> int: ... | ||
|
||
@abstractmethod | ||
def get_shard_indices(self, reader_group_rank: int) -> ArrayIndex: ... | ||
|
||
|
||
class FullGrid(BaseGridIndices): | ||
"""The full grid is loaded.""" | ||
|
||
def compute_grid_size(self, graph: HeteroData) -> int: | ||
return graph[self.nodes_name].num_nodes | ||
|
||
def get_shard_indices(self, reader_group_rank: int) -> ArrayIndex: | ||
return self.split_seq_in_shards(reader_group_rank) | ||
|
||
|
||
class MaskedGrid(BaseGridIndices): | ||
"""Grid is masked based on a node attribute.""" | ||
|
||
def __init__(self, nodes_name: str, reader_group_size: int, node_attribute_name: str): | ||
super().__init__(nodes_name, reader_group_size) | ||
self.node_attribute_name = node_attribute_name | ||
|
||
def setup(self, graph: HeteroData) -> None: | ||
LOGGER.info( | ||
"The graph attribute %s of the %s nodes will be used to masking the spatial dimension.", | ||
self.node_attribute_name, | ||
self.nodes_name, | ||
) | ||
self.spatial_indices = graph[self.nodes_name][self.node_attribute_name].squeeze().tolist() | ||
super().setup(graph) | ||
|
||
def compute_grid_size(self, _graph: HeteroData) -> int: | ||
return len(self.spatial_indices) | ||
|
||
def get_shard_indices(self, reader_group_rank: int) -> ArrayIndex: | ||
sequence_indices = self.split_seq_in_shards(reader_group_rank) | ||
return self.spatial_indices[sequence_indices] |
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