This implement will have different output compared with tf.sequence_mask when the input is 2d-dimension.
e.g When lengths is array([[1],[3], [2],[4]]).
So I improved this implement as following:
    def sequence_mask(self, lengths, maxlen=None, dtype=torch.bool):
        if maxlen is None:
            maxlen = lengths.max()
        row_vector = torch.arange(0, maxlen, 1)
        matrix = torch.unsqueeze(lengths, dim=-1)
        mask = row_vector < matrix

        mask.type(dtype)
        return mask

Hope this can help those who want to use tf.sequence_mask in PyTorch also.