def turn_last_true_mask_to_false(mask, true_mask_cnt=None):
    """Turn the last true value to false for each row in a mask matrix."""
    # mask: shape(batch_size, seq_len)
    if true_mask_cnt is None:
        true_mask_cnt = torch.sum(mask, dim=1).unsqueeze(1)
    turn_position_indices = mask.cumsum(dim=1) == true_mask_cnt
    converted_mask = mask.clone()
    converted_mask[turn_position_indices] = False
    return converted_mask
Turn the last true value to false for each row in a mask matrix.
smoe/utils/operations/operation_tensor.py