Skip to content
This repository has been archived by the owner on Nov 22, 2022. It is now read-only.

Commit

Permalink
implement onnxable lasttimestep pooling (#268)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #268

add special pooling of the last hidden state when exporting so ATen and extra operators is not introduced

Reviewed By: houseroad

Differential Revision: D13954997

fbshipit-source-id: a33820a75d7b604ef6eb402f2e6898c0ec0890ed
  • Loading branch information
gardenia22 authored and facebook-github-bot committed Feb 6, 2019
1 parent febf424 commit 5df1377
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pytext/models/representations/pooling.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ def __init__(self, config: Module.Config, n_input: int) -> None:
super().__init__(config)

def forward(self, inputs: torch.Tensor, seq_lengths: torch.Tensor) -> torch.Tensor:
# inputs: (bsz, max_len, dim)
# seq_lengths: (bsz,)

if torch._C._get_tracing_state():
# if it is exporting, the batch size = 1, so we return the last hidden state
# by returning the last dimension to avoid introducing extra operators
assert inputs.shape[0] == 1
return inputs[:, -1, :]
bsz, _, dim = inputs.shape
idx = seq_lengths.unsqueeze(1).expand(bsz, dim).unsqueeze(1)
return inputs.gather(1, idx - 1).squeeze(1)

0 comments on commit 5df1377

Please sign in to comment.