This is effectively equivalent with what I suggested. In your 2nd code snippet, input is a tensor, which do not require gradient. Hence no error.
In fact, all your code can just be simplified to either use default ReLU or just directly call .clamp. What’s the purpose of writing this function?