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
19 changes: 1 addition & 18 deletions repeng/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,24 +166,7 @@ def forward(self, *args, **kwargs):

norm_pre = torch.norm(modified, dim=-1, keepdim=True)

# we should ignore the padding tokens when doing the activation addition
# mask has ones for non padding tokens and zeros at padding tokens.
# only tested this on left padding
if "position_ids" in kwargs:
pos = kwargs["position_ids"]
zero_indices = (pos == 0).cumsum(1).argmax(1, keepdim=True)
col_indices = torch.arange(pos.size(1), device=pos.device).unsqueeze(0)
target_shape = modified.shape
mask = (
(col_indices >= zero_indices)
.float()
.reshape(target_shape[0], target_shape[1], 1)
)
mask = mask.to(modified.dtype).to(modified.device)
else:
mask = 1.0

modified = self.params.operator(modified, control * mask)
modified = self.params.operator(modified, control)

if self.params.normalize:
norm_post = torch.norm(modified, dim=-1, keepdim=True)
Expand Down