<commit_before># -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
# --------------------------------------------------------------------------

import sys
import warnings

import torch
import torch.utils.checkpoint
from packaging import version
from torch.onnx import symbolic_helper

from onnxruntime.capi._pybind_state import register_torch_autograd_function

from . import _logger
from ._fallback import ORTModuleONNXModelException, wrap_exception

# Some autograd.Function's shouldn't be exported as PythonOp.
# If CheckpointFunction is exported as PythonOp, the checkpointed computation
# may be computed by Pytorch, not ORT. This situation is especially important
# for big models such as GPT-2. Exporting CheckpointFunction as PythonOp means
# every transformer would be computed by Pytorch and ORT doesn't contribute
# at all.
BANNED_AUTOGRAD_FUNCTION_NAMES = frozenset([torch.utils.checkpoint.CheckpointFunction.__name__])

# Mapping from pytorch scalar type to onnx scalar type.
_CAST_PYTORCH_TO_ONNX = {
    "Byte": torch.onnx.TensorProtoDataType.UINT8,
    "Char": torch.onnx.TensorProtoDataType.INT8,
    "Double": torch.onnx.TensorProtoDataType.DOUBLE,
    "Float": torch.onnx.TensorProtoDataType.FLOAT,
    "Half": torch.onnx.TensorProtoDataType.FLOAT16,
    "Int": torch.onnx.TensorProtoDataType.INT32,
    "Long": torch.onnx.TensorProtoDataType.INT64,
    "Short": torch.onnx.TensorProtoDataType.INT16,
    "Bool": torch.onnx.TensorProtoDataType.BOOL,
    "ComplexFloat": torch.onnx.TensorProtoDataType.COMPLEX64,
    "ComplexDouble": torch.onnx.TensorProtoDataType.COMPLEX128,
    "BFloat16": torch.onnx.TensorProtoDataType.BFLOAT16,
    "Undefined": torch.onnx.TensorProtoDataType.UNDEFINED,
}


def _pytorch_type_to_onnx(scalar_type: str) -> torch.onnx.TensorProtoDataType:
    try:
        return torch.onnx.JitScalarType.from_name(scalar_type).onnx_type()
    except AttributeError:
        return _CAST_PYTORCH_TO_ONNX[scalar_type]

# For pointer needed for PythonOp execution, we firstly append it into a global store to hold a
# reference (in case it is released after module exported).
NONTENSOR_OBJECT_POINTER_STORE = {}


def _clear_nontensor_object_references():
    NONTENSOR_OBJECT_POINTER_STORE.clear()<commit_msg>python lint (#12825)<commit_after># -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
# --------------------------------------------------------------------------

import sys
import warnings

import torch
import torch.utils.checkpoint
from packaging import version
from torch.onnx import symbolic_helper

from onnxruntime.capi._pybind_state import register_torch_autograd_function

from . import _logger
from ._fallback import ORTModuleONNXModelException, wrap_exception

# Some autograd.Function's shouldn't be exported as PythonOp.
# If CheckpointFunction is exported as PythonOp, the checkpointed computation
# may be computed by Pytorch, not ORT. This situation is especially important
# for big models such as GPT-2. Exporting CheckpointFunction as PythonOp means
# every transformer would be computed by Pytorch and ORT doesn't contribute
# at all.
BANNED_AUTOGRAD_FUNCTION_NAMES = frozenset([torch.utils.checkpoint.CheckpointFunction.__name__])

# Mapping from pytorch scalar type to onnx scalar type.
_CAST_PYTORCH_TO_ONNX = {
    "Byte": torch.onnx.TensorProtoDataType.UINT8,
    "Char": torch.onnx.TensorProtoDataType.INT8,
    "Double": torch.onnx.TensorProtoDataType.DOUBLE,
    "Float": torch.onnx.TensorProtoDataType.FLOAT,
    "Half": torch.onnx.TensorProtoDataType.FLOAT16,
    "Int": torch.onnx.TensorProtoDataType.INT32,
    "Long": torch.onnx.TensorProtoDataType.INT64,
    "Short": torch.onnx.TensorProtoDataType.INT16,
    "Bool": torch.onnx.TensorProtoDataType.BOOL,
    "ComplexFloat": torch.onnx.TensorProtoDataType.COMPLEX64,
    "ComplexDouble": torch.onnx.TensorProtoDataType.COMPLEX128,
    "BFloat16": torch.onnx.TensorProtoDataType.BFLOAT16,
    "Undefined": torch.onnx.TensorProtoDataType.UNDEFINED,
}


def _pytorch_type_to_onnx(scalar_type: str) -> torch.onnx.TensorProtoDataType:
    try:
        return torch.onnx.JitScalarType.from_name(scalar_type).onnx_type()
    except AttributeError:
        return _CAST_PYTORCH_TO_ONNX[scalar_type]


# For pointer needed for PythonOp execution, we firstly append it into a global store to hold a
# reference (in case it is released after module exported).
NONTENSOR_OBJECT_POINTER_STORE = {}


def _clear_nontensor_object_references():
    NONTENSOR_OBJECT_POINTER_STORE.clear()
{'fraction_non_alphanumeric': 0.13739974672857747, 'fraction_numerical': 0.009497678345293373, 'mean_word_length': 3.7201195219123506, 'pattern_counts': {'":': 26, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '834577', 'n_tokens_mistral': 1432, 'n_tokens_neox': 1405, 'n_words': 345}