@ZhuXingJune
I did not use clamp and wrote a piece of code for myself. But, you can check whether it works or not by calculating the norm of the gradient before and after calling that code:
float modelImpl::get_grad_norm(int grad_norm_type) {
    torch::Tensor tmp = torch::zeros({1});

    for (auto &p : layers->named_parameters()) {
        auto z = p.value(); // note that z is a Tensor, same as &p : layers->parameters
        tmp += torch::pow(torch::norm(z.grad(), grad_norm_type), grad_norm_type);
    }

    auto total_norm = torch::pow(tmp, 1 / (grad_norm_type + 1e-6));
    return *total_norm.to(cpu_device).data<float>();
}