I do have the same error message, but for a slightly different problem. In the end, did you fix it?
In my case, I would like opt.step() to use complete different gradient values.
‘new_gradients’ is a dictionary for which the keys() have the same name as that the model parameters. For example,
block.fc1.weight
block.fc1.bias
etc…
Thus, we have that
	f = model(x)

	loss = criterion(f, y)
	
	for (key, w) in model.named_parameters():
		w.register_hook(lambda grad: new_gradients[key])

	optimizer.zero_grad()
	loss.backward()

There is an error at the very last line,
RuntimeError: hook ‘lambda’ has changed the size of value
Help would be appreciated.