Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Target py310 and modernize codebase with ruff #23401

Merged
merged 7 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions csharp/tools/MauiModelTester/create_test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import shutil
import sys
from pathlib import Path
from typing import Dict, List, Optional

import numpy as np

Expand Down Expand Up @@ -84,7 +83,7 @@ def parse_args():
return args


def create_existing_data_map(pb_files: List[Path]):
def create_existing_data_map(pb_files: list[Path]):
import onnx_test_data_utils as data_utils

data_map = {}
Expand All @@ -98,9 +97,9 @@ def create_existing_data_map(pb_files: List[Path]):

def add_model_and_test_data_to_app(
model_path: Path,
symbolic_dims: Optional[Dict[str, int]] = None,
input_map: Optional[Dict[str, np.ndarray]] = None,
output_map: Optional[Dict[str, np.ndarray]] = None,
symbolic_dims: dict[str, int] | None = None,
input_map: dict[str, np.ndarray] | None = None,
output_map: dict[str, np.ndarray] | None = None,
):
import ort_test_dir_utils as utils

Expand Down
2 changes: 0 additions & 2 deletions onnxruntime/python/backend/backend_rep.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
Implements ONNX's backend API.
"""

from typing import Any, Tuple # noqa: F401

from onnx.backend.base import BackendRep

from onnxruntime import RunOptions
Expand Down
5 changes: 3 additions & 2 deletions onnxruntime/python/onnxruntime_inference_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import os
import typing
import warnings
from typing import Any, Sequence
from collections.abc import Sequence
from typing import Any

from onnxruntime.capi import _pybind_state as C

Expand Down Expand Up @@ -143,7 +144,7 @@ def set_provider_options(name, options):
if not all([isinstance(options_for_provider, dict) for options_for_provider in provider_options]):
raise ValueError("'provider_options' values must be dicts.")

for name, options in zip(providers, provider_options):
for name, options in zip(providers, provider_options, strict=False):
set_provider_options(name, options)

else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import os
import sys
from dataclasses import dataclass
from typing import List, Optional, Union

import onnx
from onnx import TensorProto, helper
Expand Down Expand Up @@ -65,7 +64,7 @@ class IOInfo:
index: int
name: str
elem_type: TensorProto.DataType
shape: Optional[List[Union[int, str]]]
shape: list[int | str] | None


def str_is_int(string: str) -> bool:
Expand All @@ -76,7 +75,7 @@ def str_is_int(string: str) -> bool:
return False


def parse_shape(shape_str: str) -> Optional[List[Union[int, str]]]:
def parse_shape(shape_str: str) -> list[int | str] | None:
try:
shape = [int(s) if str_is_int(s) else s for s in shape_str.split(",")]
except ValueError:
Expand Down Expand Up @@ -204,7 +203,7 @@ def parse_arguments() -> argparse.Namespace:
return parser.parse_args()


def get_attributes(attr_data_info: List[List[str]]):
def get_attributes(attr_data_info: list[list[str]]):
if not attr_data_info:
return {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
import sys
from abc import abstractmethod
from argparse import Action, ArgumentParser
from collections.abc import Callable
from contextlib import contextmanager
from dataclasses import dataclass
from fnmatch import fnmatch
from functools import wraps
from typing import Callable

build_dir = os.environ.get("KERNEL_EXPLORER_BUILD_DIR", None)
if build_dir is None:
Expand Down
10 changes: 5 additions & 5 deletions onnxruntime/python/tools/offline_tuning.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
import sys
from collections import OrderedDict
from pprint import pprint
from typing import Any, Dict, List
from typing import Any

import onnx

TuningResults = Dict[str, Any]
TuningResults = dict[str, Any]

_TUNING_RESULTS_KEY = "tuning_results"

Expand All @@ -32,7 +32,7 @@ def extract(model: onnx.ModelProto):
return json.loads(tuning_results_prop.value)


def embed(model: onnx.ModelProto, tuning_results: List[TuningResults], overwrite=False):
def embed(model: onnx.ModelProto, tuning_results: list[TuningResults], overwrite=False):
idx = _find_tuning_results_in_props(model.metadata_props)
assert overwrite or idx <= 0, "the supplied onnx file already have tuning results embedded!"

Expand All @@ -47,7 +47,7 @@ def embed(model: onnx.ModelProto, tuning_results: List[TuningResults], overwrite

class Merger:
class EpAndValidators:
def __init__(self, ep: str, validators: Dict[str, str]):
def __init__(self, ep: str, validators: dict[str, str]):
self.ep = ep
self.validators = copy.deepcopy(validators)
self.key = (ep, tuple(sorted(validators.items())))
Expand All @@ -61,7 +61,7 @@ def __eq__(self, other):
def __init__(self):
self.ev_to_results = OrderedDict()

def merge(self, tuning_results: List[TuningResults]):
def merge(self, tuning_results: list[TuningResults]):
for trs in tuning_results:
self._merge_one(trs)

Expand Down
4 changes: 2 additions & 2 deletions onnxruntime/python/tools/quantization/base_quantizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# license information.
# --------------------------------------------------------------------------
import logging
from typing import Any, Dict
from typing import Any

import numpy as np
import onnx
Expand Down Expand Up @@ -36,7 +36,7 @@


class QuantizationParams:
def __init__(self, **data: Dict[str, Any]):
def __init__(self, **data: dict[str, Any]):
self.data = {}
for k, v in data.items():
if not isinstance(k, str):
Expand Down
44 changes: 24 additions & 20 deletions onnxruntime/python/tools/quantization/calibrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import itertools
import os
import uuid
from collections.abc import Sequence
from enum import Enum
from pathlib import Path
from typing import Dict, Optional, Sequence, Tuple, Union

import numpy as np
import onnx
Expand Down Expand Up @@ -39,7 +39,7 @@ def rel_entr(pk: np.ndarray, qk: np.ndarray) -> np.ndarray:
def entropy(
pk: np.ndarray,
qk: np.ndarray,
base: Optional[float] = None,
base: float | None = None,
axis: int = 0,
) -> np.ndarray:
"""
Expand Down Expand Up @@ -100,7 +100,7 @@ def to_dict(self):


class TensorsData:
def __init__(self, calibration_method, data: Dict[str, Union[TensorData, Tuple]]):
def __init__(self, calibration_method, data: dict[str, TensorData | tuple]):
self.calibration_method = calibration_method
self.data = {}
for k, v in data.items():
Expand Down Expand Up @@ -187,8 +187,8 @@ def set_range(self, start_index: int, end_index: int):
class CalibraterBase:
def __init__(
self,
model_path: Union[str, Path],
op_types_to_calibrate: Optional[Sequence[str]] = None,
model_path: str | Path,
op_types_to_calibrate: Sequence[str] | None = None,
augmented_model_path="augmented_model.onnx",
symmetric=False,
use_external_data_format=False,
Expand Down Expand Up @@ -297,8 +297,8 @@ def compute_data(self) -> TensorsData:
class MinMaxCalibrater(CalibraterBase):
def __init__(
self,
model_path: Union[str, Path],
op_types_to_calibrate: Optional[Sequence[str]] = None,
model_path: str | Path,
op_types_to_calibrate: Sequence[str] | None = None,
augmented_model_path="augmented_model.onnx",
symmetric=False,
use_external_data_format=False,
Expand Down Expand Up @@ -476,7 +476,8 @@ def compute_data(self) -> TensorsData:

output_names = [self.infer_session.get_outputs()[i].name for i in range(len(self.intermediate_outputs[0]))]
output_dicts_list = [
dict(zip(output_names, intermediate_output)) for intermediate_output in self.intermediate_outputs
dict(zip(output_names, intermediate_output, strict=False))
for intermediate_output in self.intermediate_outputs
]

merged_output_dict = {}
Expand Down Expand Up @@ -507,7 +508,9 @@ def compute_data(self) -> TensorsData:
else:
pairs.append(tuple([min_value_array, max_value_array]))

new_calibrate_tensors_range = TensorsData(CalibrationMethod.MinMax, dict(zip(calibrate_tensor_names, pairs)))
new_calibrate_tensors_range = TensorsData(
CalibrationMethod.MinMax, dict(zip(calibrate_tensor_names, pairs, strict=False))
)
if self.calibrate_tensors_range:
self.calibrate_tensors_range = self.merge_range(self.calibrate_tensors_range, new_calibrate_tensors_range)
else:
Expand All @@ -519,8 +522,8 @@ def compute_data(self) -> TensorsData:
class HistogramCalibrater(CalibraterBase):
def __init__(
self,
model_path: Union[str, Path],
op_types_to_calibrate: Optional[Sequence[str]] = None,
model_path: str | Path,
op_types_to_calibrate: Sequence[str] | None = None,
augmented_model_path="augmented_model.onnx",
use_external_data_format=False,
method="percentile",
Expand Down Expand Up @@ -608,7 +611,8 @@ def collect_data(self, data_reader: CalibrationDataReader):
raise ValueError("No data is collected.")

output_dicts_list = [
dict(zip(output_names, intermediate_output)) for intermediate_output in self.intermediate_outputs
dict(zip(output_names, intermediate_output, strict=False))
for intermediate_output in self.intermediate_outputs
]

merged_dict = {}
Expand Down Expand Up @@ -653,8 +657,8 @@ def compute_data(self) -> TensorsData:
class EntropyCalibrater(HistogramCalibrater):
def __init__(
self,
model_path: Union[str, Path],
op_types_to_calibrate: Optional[Sequence[str]] = None,
model_path: str | Path,
op_types_to_calibrate: Sequence[str] | None = None,
augmented_model_path="augmented_model.onnx",
use_external_data_format=False,
method="entropy",
Expand Down Expand Up @@ -687,8 +691,8 @@ def __init__(
class PercentileCalibrater(HistogramCalibrater):
def __init__(
self,
model_path: Union[str, Path],
op_types_to_calibrate: Optional[Sequence[str]] = None,
model_path: str | Path,
op_types_to_calibrate: Sequence[str] | None = None,
augmented_model_path="augmented_model.onnx",
use_external_data_format=False,
method="percentile",
Expand Down Expand Up @@ -721,8 +725,8 @@ def __init__(
class DistributionCalibrater(HistogramCalibrater):
def __init__(
self,
model_path: Union[str, Path],
op_types_to_calibrate: Optional[Sequence[str]] = None,
model_path: str | Path,
op_types_to_calibrate: Sequence[str] | None = None,
augmented_model_path="augmented_model.onnx",
use_external_data_format=False,
method="distribution",
Expand Down Expand Up @@ -1168,8 +1172,8 @@ def get_entropy_threshold(self, histogram, num_quantized_bins):


def create_calibrator(
model: Union[str, Path],
op_types_to_calibrate: Optional[Sequence[str]] = None,
model: str | Path,
op_types_to_calibrate: Sequence[str] | None = None,
augmented_model_path="augmented_model.onnx",
calibrate_method=CalibrationMethod.MinMax,
use_external_data_format=False,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import argparse
import logging
import os
from typing import List, Tuple

import numpy as np
import numpy.typing as npt
Expand Down Expand Up @@ -44,7 +43,7 @@ def __init__(self, model: ModelProto, quant_type: int, block_size: int, nodes_to
self.nodes_to_exclude = set(nodes_to_exclude)

@staticmethod
def __get_initializer(name, graph_path: List[GraphProto]) -> Tuple[TensorProto, GraphProto]:
def __get_initializer(name, graph_path: list[GraphProto]) -> tuple[TensorProto, GraphProto]:
for gid in range(len(graph_path) - 1, -1, -1):
graph = graph_path[gid]
for tensor in graph.initializer:
Expand Down Expand Up @@ -74,7 +73,7 @@ def bnb4_block_quant(self, fpweight: npt.ArrayLike) -> np.ndarray:

return (packed, absmax)

def _bnb4_matmul_node_weight(self, node: NodeProto, graph_stack: List[GraphProto]) -> NodeProto:
def _bnb4_matmul_node_weight(self, node: NodeProto, graph_stack: list[GraphProto]) -> NodeProto:
"""If the node is MatMul with fp32 const weight, quantize the weight with int4, and return the new node"""

if node.op_type != "MatMul":
Expand Down Expand Up @@ -129,7 +128,7 @@ def _bnb4_matmul_node_weight(self, node: NodeProto, graph_stack: List[GraphProto

return matmul_bnb4_node

def _process_subgraph(self, graph_stack: List[GraphProto]):
def _process_subgraph(self, graph_stack: list[GraphProto]):
new_nodes = []
graph = graph_stack[-1]

Expand Down
Loading
Loading