<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def clip_gradients(batch_result, model, max_grad_norm):
 """ Clip gradients to a given maximum length """

    if max_grad_norm is not None:
        grad_norm = torch.nn.utils.clip_grad_norm_(
            filter(lambda p: p.requires_grad, model.parameters()),
            max_norm=max_grad_norm
        )
    else:
        grad_norm = 0.0

    batch_result['grad_norm'] = grad_norm