def _add_const(self, name: str, val: Union[torch.Tensor, torch._C.ScriptObject]) -> None:
        """Create a const op and add it to the graph."""
        if isinstance(val, torch.Tensor) and self._has_compression_info(name):
            try:
                compression_op = self._construct_compression_op(val.detach().numpy(), name)
                self.context.add(compression_op)
                return
            except NotImplementedError as e:
                logger.warning(
                    "Failed to create a compression op based on the compression info "
                    f"carried by {name} in the torch model. Ignored the compression info "
                    f"and constructed a normal const. Detailed error message:\n{e}"
                )

        if isinstance(val, torch._C.ScriptObject):
            logger.info(f"Encountered constant {name} of type _torch._C.ScriptObject")
            return
        elif isinstance(val, torch.Tensor) and val.is_quantized:
            const = _dequantized_weight(val.cpu(), name)
            self.context.add(const)
            return
        elif not isinstance(val, torch.Tensor):
            raise ValueError(f"unsupported class for {name} in PyTorch graph: {type(val)}")
        val = val.detach().cpu().numpy()
        # TODO (rdar://107718371): support uint8 activation quantization in torchscript
        # Some torchscript models store indices with uint8, which are unrelated to quantization and
        # need to be cast to int32 since many non-quantized Core ML ops do not support int8.
        # We need a way to distinguish whether an uint8 is quantization (so should be kept)
        # or not (so should be cast to int32).
        if self.context.frontend == TorchFrontend.TORCHSCRIPT and val.dtype == np.uint8:
            val = val.astype(np.int32)
        const = mb.const(val=val, name=name)
        self.context.add(const)
Create a const op and add it to the graph.
coremltools/converters/mil/frontend/torch/converter.py