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 15
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
28 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,6 +68,7 @@ def __init__( | |
num_heads: int, | ||
activation: str, | ||
window_size: int, | ||
grid_lat_coslon_sinlon: Tensor = None, | ||
dropout_p: float = 0.0, | ||
): | ||
super().__init__() | ||
|
@@ -80,6 +81,11 @@ def __init__( | |
|
||
self.layer_norm1 = nn.LayerNorm(num_channels) | ||
|
||
self.grid_lat_coslon_sinlon = grid_lat_coslon_sinlon | ||
if self.grid_lat_coslon_sinlon is not None: | ||
self.grid_lat_coslon_sinlon = self.grid_lat_coslon_sinlon | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
sahahner
Author
Member
|
||
self.pos_embedder = nn.Linear(3, num_channels) # assuming that we have 3 position features, lat and cos / sin of lon | ||
|
||
self.attention = MultiHeadSelfAttention( | ||
num_heads=num_heads, | ||
embed_dim=num_channels, | ||
|
@@ -99,6 +105,11 @@ def __init__( | |
def forward( | ||
self, x: Tensor, shapes: list, batch_size: int, model_comm_group: Optional[ProcessGroup] = None | ||
) -> Tensor: | ||
if self.grid_lat_coslon_sinlon is not None: | ||
pos_embedding = self.pos_embedder(self.grid_lat_coslon_sinlon.to(x.device)) | ||
pos_embedding = pos_embedding.repeat(batch_size, 1) | ||
x = x + pos_embedding | ||
|
||
# Need to be out of place for gradient propagation | ||
x = x + self.attention(self.layer_norm1(x), shapes, batch_size, model_comm_group=model_comm_group) | ||
x = x + self.mlp(self.layer_norm2(x)) | ||
|
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
I guess here you can leave out the part
if grid_lat_coslon_sinlon is not None:
self.grid_lat_coslon_sinlon = grid_lat_coslon_sinlon
because you have already assigned it above.
I think it would be good to make it a buffer so that it is moved to the GPU together with the model. Currently you copy it from CPU to GPU in each forward
if self.grid_lat_coslon_sinlon is not None:
pos_embedding = self.pos_embedder(self.grid_lat_coslon_sinlon.to(x.device))
pos_embedding = pos_embedding.repeat(batch_size, 1)