(batch, in_dim, out_dim)
return MaskedBatch(batch.data, mask, dims)
if in_dim is indexed by i and out_dim by j, masks ret[i,j] where i > j
if in_dim is indexed by i and out_dim by j, masks ret[i,j] where i > j
if in_dim is indexed by i and out_dim by j masks ret [ i j ] where i > j
def causal_mask(batch, in_dim, out_dim):
    '''if in_dim is indexed by i and out_dim by j, masks ret[i,j] where i > j'''
    if not isinstance(batch, MaskedBatch):
        # TODO or we could just promote to MaskedBatch /shrug
        if in_dim == 1 and out_dim == 2:
            return batch - batch.new(
                *batch.size()[1:]).fill_(1e10).tril(-1).unsqueeze(0)
        elif in_dim == 2 and out_dim == 1:
            return batch - batch.new(
                *batch.size()[1:]).fill_(1e10).triu(1).unsqueeze(0)
        else:
            raise NotImplementedError("unsupported arguments for causal_mask")
    if in_dim == 1 and out_dim == 2:
        mask = batch.mask * batch.mask.new(
            *batch.data.size()[1:]).fill_(1).triu(0).unsqueeze(0)
    elif in_dim == 2 and out_dim == 1:
        mask = batch.mask * batch.mask.new(
            *batch.data.size()[1:]).fill_(1).tril(0).unsqueeze(0)
    else:
        raise NotImplementedError("unsupported arguments for causal_mask")
    dims = tuple(True if d + 1 in (in_dim, out_dim) else b
                 for d, b in enumerate(batch.dims))
    return MaskedBatch(batch.data, mask, dims)
def causal_mask ( batch , in_dim , out_dim ) : if not isinstance ( batch , MaskedBatch ) : # TODO or we could just promote to MaskedBatch /shrug if in_dim == 1 and out_dim == 2 : return batch - batch . new ( * batch . size ( ) [ 1 : ] ) . fill_ ( 1e10 ) . tril ( - 1 ) . unsqueeze ( 0 ) elif in_dim == 2 and out_dim == 1 : return batch - batch . new ( * batch . size ( ) [ 1 : ] ) . fill_ ( 1e10 ) . triu ( 1 ) . unsqueeze ( 0 ) else : raise NotImplementedError ( "unsupported arguments for causal_mask" ) if in_dim == 1 and out_dim == 2 : mask = batch . mask * batch . mask . new ( * batch . data . size ( ) [ 1 : ] ) . fill_ ( 1 ) . triu ( 0 ) . unsqueeze ( 0 ) elif in_dim == 2 and out_dim == 1 : mask = batch . mask * batch . mask . new ( * batch . data . size ( ) [ 1 : ] ) . fill_ ( 1 ) . tril ( 0 ) . unsqueeze ( 0 ) else : raise NotImplementedError ( "unsupported arguments for causal_mask" ) dims = tuple ( True if d + 1 in ( in_dim , out_dim ) else b for d , b in enumerate ( batch . dims ) ) return MaskedBatch ( batch . data , mask , dims )