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

fixing broken device swap #15

Merged
merged 1 commit into from
Apr 5, 2024
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: 1 addition & 2 deletions crystal_diffusion/models/score_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,7 @@ def _forward_unchecked(self, batch: Dict[AnyStr, torch.Tensor]) -> torch.Tensor:
computed_scores : the scores computed by the model.
"""
positions = batch[self.position_key] # shape [batch_size, number_of_atoms, spatial_dimension]
times = batch[self.timestep_key] # shape [batch_size, 1]

times = batch[self.timestep_key].to(positions.device) # shape [batch_size, 1]
input = torch.cat([self.flatten(positions), times], dim=1)

output = self.mlp_layers(input).reshape(positions.shape)
Expand Down
2 changes: 1 addition & 1 deletion crystal_diffusion/samplers/noisy_position_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,6 @@ def get_noisy_position_sample(real_relative_positions: torch.Tensor, sigmas: tor
"sigmas array is expected to be of the same shape as the real_relative_positions array"

z_scores = NoisyPositionSampler._get_gaussian_noise(real_relative_positions.shape)
noise = sigmas * z_scores
noise = (sigmas * z_scores).to(real_relative_positions.device)
noisy_relative_positions = map_positions_to_unit_cell(real_relative_positions + noise)
return noisy_relative_positions
13 changes: 8 additions & 5 deletions crystal_diffusion/score/wrapped_gaussian_score.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
import numpy as np
import torch

SIGMA_THRESHOLD = 1.0 / np.sqrt(2.0 * np.pi)
U_THRESHOLD = 0.5
SIGMA_THRESHOLD = torch.Tensor([1.0 / np.sqrt(2.0 * np.pi)])
U_THRESHOLD = torch.Tensor([0.5])


def get_sigma_normalized_score_brute_force(u: float, sigma: float, kmax: Optional[int] = None) -> float:
Expand Down Expand Up @@ -124,7 +124,8 @@ def get_sigma_normalized_score(
for mask_calculator, score_calculator in zip(mask_calculators, score_calculators):
mask = mask_calculator(list_u, list_sigma)
if mask.any():
flat_view[mask] = score_calculator(list_u[mask], list_sigma[mask], list_k)
device = flat_view.device
flat_view[mask] = score_calculator(list_u[mask], list_sigma.to(device)[mask], list_k.to(device))

return sigma_normalized_scores

Expand All @@ -139,7 +140,8 @@ def _get_small_sigma_small_u_mask(list_u: torch.Tensor, list_sigma: torch.Tensor
Returns:
mask_1a : an array of booleans of shape [Nu]
"""
return torch.logical_and(list_sigma <= SIGMA_THRESHOLD, list_u < U_THRESHOLD)
device = list_u.device
return torch.logical_and(list_sigma.to(device) <= SIGMA_THRESHOLD.to(device), list_u < U_THRESHOLD.to(device))


def _get_small_sigma_large_u_mask(list_u: torch.Tensor, list_sigma: torch.Tensor) -> torch.Tensor:
Expand All @@ -152,7 +154,8 @@ def _get_small_sigma_large_u_mask(list_u: torch.Tensor, list_sigma: torch.Tensor
Returns:
mask_1b : an array of booleans of shape [Nu]
"""
return torch.logical_and(list_sigma <= SIGMA_THRESHOLD, list_u >= U_THRESHOLD)
device = list_u.device
return torch.logical_and(list_sigma.to(device) <= SIGMA_THRESHOLD.to(device), list_u >= U_THRESHOLD.to(device))


def _get_large_sigma_mask(list_u: torch.Tensor, list_sigma: torch.Tensor) -> torch.Tensor:
Expand Down
Loading