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
25 changes: 16 additions & 9 deletions inference/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,23 @@ def forward(self, x: torch.Tensor) -> torch.Tensor:
Raises:
ValueError: If `world_size` is not defined.
"""
if world_size > 1:
mask = (x < self.vocab_start_idx) | (x >= self.vocab_end_idx)
x = x - self.vocab_start_idx
x[mask] = 0
y = F.embedding(x, self.weight)
if world_size > 1:
y[mask] = 0
dist.all_reduce(y)
return y
def forward(self, x: torch.Tensor) -> torch.Tensor:
if self.world_size < 1:
raise ValueError("world_size must be >= 1")

if self.world_size == 1:
return F.embedding(x, self.weight)

# For world_size > 1
mask = (x < self.vocab_start_idx) | (x >= self.vocab_end_idx)
x = x - self.vocab_start_idx
x[mask] = 0

y = F.embedding(x, self.weight)
y[mask] = 0

dist.all_reduce(y)
return y

def linear(x: torch.Tensor, weight: torch.Tensor, bias: Optional[torch.Tensor] = None) -> torch.Tensor:
"""
Expand Down