def _cast_to(_input: Union[Var, np.ndarray], dtype_str: str, node_name: str) -> Var:
    """Create a Var to cast from input to target dtype."""
    valid_dtypes = SSAOpRegistry._get_core_op_cls("cast").supported_dtypes()
    if dtype_str in valid_dtypes:
        res = mb.cast(x=_input, dtype=dtype_str)
    else:
        np_dtype = types.nptype_from_builtin(types.string_to_builtin(dtype_str))
        if np.issubdtype(np_dtype, np.integer):
            target_dtype = "int32"
        elif np.issubdtype(np_dtype, np.floating):
            target_dtype = "fp32"
        else:
            raise ValueError(f"Unsupported `to` op ({node_name}) with target dtype {np_dtype}")
        logger.warning(
            f"The {dtype_str} is not supported by cast op. Will do best-effort cast to {target_dtype}"
        )
        res = mb.cast(x=_input, dtype=target_dtype)
    return res
Create a Var to cast from input to target dtype.
coremltools/converters/mil/frontend/torch/ops.py