The novel Prune module 29 also works for this.
import torch.nn.utils.prune as prune

# define some linear layer
out_features = 5
in_features = 10
linLayer = torch.nn.Linear(in_features, out_features, bias = False) 

# define your maks
mask = np.random.randint(2,size=(out_features,in_features)) 
# apply the mask
prune.custom_from_mask(
      linLayer, name='weight', mask=torch.tensor(mask))

plt.spy(linLayer.weight.data)

# [...] training
# after training you can remove the mask for inference
prune.remove(linLayer, "weight") ```