code: def lsq_clamp_round(
      target: torch.Tensor, target_space: QuantizationTargetSpace
  ):
      qmax: int = target_space.qmax
      qmin: int = target_space.qmin
      if target_space._scaler is not None:
          scale = target_space._scaler.expand(target_space.scale, target_space.shape, keepdim=True)  # type: ignore
      else:
          scale = target_space.scale
  
      class LSQClampRound(torch.autograd.Function):
          @staticmethod
          def forward(
              ctx: Any, target: torch.Tensor, scale: torch.Tensor
          ):
              ctx.save_for_backward(target, scale)
              quantize_target = torch.clamp(target / scale, qmin, qmax)
              dequantize_target = torch.round(quantize_target) * scale
              return dequantize_target
  
          @staticmethod
          def backward(ctx: Any, grad_output: Any) -> Any:
              target, scale = ctx.saved_tensors
              grad_scale_factor = (
                  1.0 / ((qmax * target.numel()) ** 0.5)
                  if (qmax * target.numel()) ** 0.5 != 0
                  else 1.0
              )
  
              q_target = target / scale
              # compute index
              ind_neg = (q_target < qmin).float()
              ind_pos = (q_target > qmax).float()
              ind_mid = 1.0 - ind_neg.float() - ind_pos.float()
              # scale gradient
              grad_scale = (
                  (
                      ind_neg * qmin
                      + ind_pos * qmax
                      + ind_mid * (-q_target + torch.round(q_target))
                  )
                  * grad_output
                  * grad_scale_factor
              )
              if target_space._scaler is None:
                  grad_scale = grad_scale.sum().expand(scale.size())
              grad_target = grad_output * ind_mid
  
              return grad_target, grad_scale
  
      return LSQClampRound.apply(target, scale)
  
imports: from typing import Any
  import torch
  from .target_space import PruningTargetSpace, QuantizationTargetSpace, TargetSpace, TargetType
label_desc: Use image node_id 2 for calling a global function with example usage: lsq_clamp_round(target, target_space) and returns: LSQClampRound, dequantize_target, grad_target,  grad_scale