26/42
Part 4 of 6 · Shapes and the maskLearn
A plain attribute stays behind when the model moves
Here's what goes wrong with a plain attribute instead.
class Plain(nn.Module):
def __init__(self, block_size=8):
super().__init__()
self.proj = nn.Linear(4, 4)
ones = torch.ones(block_size, block_size, dtype=torch.bool)
self.mask = torch.tril(ones)
for model in (Masked(), Plain()):
model.to("meta")
print(type(model).__name__, model.proj.weight.device, model.mask.device)
Masked meta meta
Plain meta cpu
meta is a stand-in device that holds shapes but no numbers. It shows the move on any machine, with no GPU. The buffer moved with the layer. The plain attribute stayed on the CPU.
On a GPU that's a crash. The first forward pass fails with expected self and mask to be on the same device, because the scores are on the GPU and the mask isn't.