What is the benefit of using register_buffer
compared to declaring a normal tensor in __init__
?
class MyModule(nn.Module):
def __init__(self, child):
self.child = torch.as_tensor(child).int()
# vs
self.register_buffer('child', torch.from_numpy(np.array(child, dtype=np.int32)))
- The buffer is serialized along with the module, but if we initialize it in
__init__
then that would not matter. - Also buffer will be pushed to cuda if the model is pushed to cuda, but we could ourselves check the device and push accordingly
So what is the special case that only buffer can do?
It would be nice to have a .register_buffer
that does not serialize the tensor, just push to cuda with model.cuda