Make the onnx submodule init lazy
diff --git a/src/transformers/onnx/__init__.py b/src/transformers/onnx/__init__.py
index a80567e202b0e..7419e8c21b516 100644
--- a/src/transformers/onnx/__init__.py
+++ b/src/transformers/onnx/__init__.py
@@ -13,6 +13,24 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-from .config import EXTERNAL_DATA_FORMAT_SIZE_LIMIT, OnnxConfig, OnnxConfigWithPast, PatchingSpec
-from .convert import export, validate_model_outputs
-from .utils import ParameterFormat, compute_serialized_parameters_size
+from typing import TYPE_CHECKING
+
+from ..file_utils import _LazyModule
+
+
+_import_structure = {
+    "config": ["EXTERNAL_DATA_FORMAT_SIZE_LIMIT", "OnnxConfig", "OnnxConfigWithPast", "PatchingSpec"],
+    "convert": ["export", "validate_model_outputs"],
+    "utils": ["ParameterFormat", "compute_serialized_parameters_size"],
+}
+
+
+if TYPE_CHECKING:
+    from .config import EXTERNAL_DATA_FORMAT_SIZE_LIMIT, OnnxConfig, OnnxConfigWithPast, PatchingSpec
+    from .convert import export, validate_model_outputs
+    from .utils import ParameterFormat, compute_serialized_parameters_size
+
+else:
+    import sys
+
+    sys.modules[__name__] = _LazyModule(__name__, globals()["__file__"], _import_structure)

# What does this PR do?

The `onnx` submodule does not use a lazy init like the other ones. This results in importing a given model/tokenzier/config, which imports OnnxConfig, initializing the onnx submodule completely, and in turn importing `PreTrainedModel` and `TFPreTrainedModel` (so PyTorch and TensorFlow).

This PR solves this issue by making the init lazy like all the others.