Based on the provided code it seems grad(mean_error, dummy_w) might be freeing the intermediate tensors, which are still needed in (error + penalty_value).backward().
retain_graph=True will make sure to keep these intermediates alive.