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

Fix gen_attack's error on GPU. #112

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 6 additions & 2 deletions advertorch/attacks/blackbox/gen_attack.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def crossover(p1, p2, probs):
Select from p1 with the probabilties in probs.
"""
u = torch.rand(*p1.shape)
u = u.to(p1.device)
return torch.where(probs[:, :, None] > u, p1, p2)


Expand All @@ -87,6 +88,8 @@ def selection(pop_t, fitness, tau):
# sample parent 1 from pop_t according to probs (multinomial)
# sample parent 2 from pop_t according to probs (multinomial)
u1, u2 = torch.rand(2, n_batch, nb_samples)
u1 = u1.to(pop_t.device)
u2 = u2.to(pop_t.device)

# out of the original N samples, we draw another N samples
# this requires us to compute the following broadcasted comparison
Expand Down Expand Up @@ -119,9 +122,10 @@ def mutation(pop_t, alpha, rho, eps):
"""
# alpha and eps both have shape [B]
perturb_noise = (2 * torch.rand(*pop_t.shape) - 1)
perturb_noise = perturb_noise.to(eps.device)
perturb_noise = perturb_noise * alpha[:, None, None] * eps[:, None, None]

mask = (torch.rand(*pop_t.shape) > rho[:, None, None]).float()
mask = (torch.rand(*pop_t.shape).to(eps.device) > rho[:, None, None]).float()

return pop_t + mask * perturb_noise

Expand Down Expand Up @@ -218,8 +222,8 @@ def gen_attack(
# shape: [B, N, F]
pop_t = 2 * torch.rand(n_batch, nb_samples, n_dim) - 1
# Sample from Uniform(-eps, eps)
pop_t = eps[:, None, None] * pop_t
pop_t = pop_t.to(x.device)
pop_t = eps[:, None, None] * pop_t
else:
pop_t = pop_init.clone()

Expand Down