Error: Tensor for 'out' is on CPU, Tensor for argument #1 'self' is on CPU, but expected them to be on GPU (while checking arguments for addmm)

The nn.Linear layer is created on the CPU, so you would need to transfer it to the GPU as well:

layer = nn.Linear(784, 10).to(dev)
lin = layer(flatten)