Skip to content
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
14 changes: 11 additions & 3 deletions evo_prot_grad/common/sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ def _product_of_experts(self, inputs: List[str]) -> Tuple[List[torch.Tensor], to
scores += [expert.temperature * score]
# sum scores over experts
return ohs, torch.stack(scores, dim=0).sum(dim=0)


def _compute_gradients(self, ohs: List[torch.Tensor], PoE: torch.Tensor) -> torch.Tensor:
"""Compute the gradients of the product of experts
Expand All @@ -160,8 +159,17 @@ def _compute_gradients(self, ohs: List[torch.Tensor], PoE: torch.Tensor) -> torc
# This checks whether the gradient sequence length
# is exactly two more than the input sequence length.
if oh_grad.shape[1] == self.chains_oh.shape[1] + 2:
oh_grad = oh_grad[:,1:-1]
summed_grads += [ oh_grad @ expert.expert_to_canonical_order ]
oh_grad = oh_grad[:, 1:-1]

# some tokenizers add an <end> token to the protein
# sequence like ProtT5 and Ankh, check and remove here
# if necessary.
# This checks whether the gradient sequence length
# is exactly one more than the input sequence length.
elif oh_grad.shape[1] == self.chains_oh.shape[1] + 1:
oh_grad = oh_grad[:, :-1]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that we can't guarantee that the last element of the gradient sequence is what should removed (why not the first element?)


summed_grads += [oh_grad @ expert.expert_to_canonical_order]
# sum over experts
return torch.stack(summed_grads, dim=0).sum(dim=0)

Expand Down