docstr: Quantize the model during export with graph mode quantization for linformer encoder.
docstr_tok: ["Quantize","the","model","during","export","with","graph","mode","quantization","for","linformer","encoder","."]
code: def graph_mode_quantize(
      self, inputs, data_loader, calibration_num_batches=64
  ):
      """Quantize the model during export with graph mode quantization for linformer encoder."""
      if (
          isinstance(self.right_encoder, RoBERTaEncoder)
          and self.right_encoder.use_linformer_encoder
          and isinstance(self.left_encoder, RoBERTaEncoder)
          and self.left_encoder.use_linformer_encoder
      ):
          trace = self.trace(inputs)
          qconfig = get_default_qconfig("fbgemm")
          qconfig_dict = {"": qconfig}
          prepare_m = prepare_jit(trace, qconfig_dict, inplace=False)
          prepare_m.eval()
          with torch.no_grad():
              for i, (_, batch) in enumerate(data_loader):
                  print("Running calibration with batch {}".format(i))
                  input_data = self.onnx_trace_input(batch)
                  prepare_m(*input_data)
                  if i == calibration_num_batches - 1:
                      break
          trace = convert_jit(prepare_m, inplace=True)
      else:
          super().quantize()
          trace = self.trace(inputs)
  
      return trace
  
imports: from typing import Dict, List, Optional, Tuple
  import torch
  from pytext.common.constants import Stage, TORCH_VERSION
  from pytext.config import ConfigBase
  from pytext.config.component import create_loss
  from pytext.data.roberta_tensorizer import RoBERTaTensorizer
  from pytext.data.tensorizers import FloatListTensorizer, LabelTensorizer, Tensorizer
  from pytext.loss import BinaryCrossEntropyLoss, MultiLabelSoftMarginLoss
  from pytext.models.decoders.mlp_decoder_two_tower import MLPDecoderTwoTower
  from pytext.models.model import BaseModel
  from pytext.models.module import create_module
  from pytext.models.output_layers import ClassificationOutputLayer
  from pytext.models.output_layers.doc_classification_output_layer import BinaryClassificationOutputLayer, MulticlassOutputLayer, MultiLabelOutputLayer
  from pytext.models.roberta import RoBERTaEncoder, RoBERTaEncoderBase
  from pytext.torchscript.module import ScriptPyTextTwoTowerModuleWithDense, ScriptPyTextTwoTowerModuleWithDenseTower
  from pytext.utils.label import get_label_weights
  from pytext.utils.usage import log_class_usage
label_desc: Use image node_id 3 for calling the TwoTowerClassificationModel obj's underlying member method code with example usage: obj.graph_mode_quantize(inputs, data_loader, calibration_num_batches) and returns: trace