Skip to content
Closed
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
4 changes: 2 additions & 2 deletions .lintrunner.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ init_command = [
is_formatter = true

[[linter]]
code = 'BLACK-ISORT'
code = 'RUFF-FORMAT'
include_patterns = [
'**/*.py'
]
Expand All @@ -74,7 +74,7 @@ command = [
'-m',
'lintrunner_adapters',
'run',
'black_isort_linter',
'ruff_format_linter',
'--',
'@{{PATHSFILE}}'
]
Expand Down
18 changes: 0 additions & 18 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,11 @@ repos:
args: [--markdown-linebreak-ext=md]
- id: check-yaml
- id: requirements-txt-fixer

- repo: https://github.com/psf/black
rev: 24.1.0
hooks:
- id: black
name: Format code
exclude: "hadamard_utils.py"
- repo: https://github.com/pycqa/isort
rev: 5.11.5
hooks:
- id: isort
name: Format imports
- repo: https://github.com/MarcoGorelli/absolufy-imports
rev: v0.3.1
hooks:
- id: absolufy-imports
exclude: examples/
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.6.0
hooks:
- id: ruff
args: [ --fix ]
- repo: local
hooks:
- id: format-json
Expand Down
6 changes: 3 additions & 3 deletions docs/source/exts/auto_config_doc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ def run(self):
(class_name,) = self.arguments
package_config = OlivePackageConfig.load_default_config()
auto_config_class = import_class(class_name, package_config)
assert issubclass(auto_config_class, AutoConfigClass) or issubclass(
auto_config_class, Pass
), f"{class_name} is not a subclass of AutoConfigClass or Pass"
assert issubclass(auto_config_class, AutoConfigClass) or issubclass(auto_config_class, Pass), (
f"{class_name} is not a subclass of AutoConfigClass or Pass"
)

node = nodes.section()
node.document = self.state.document
Expand Down
2 changes: 1 addition & 1 deletion docs/source/exts/gallery_directive.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def run(self) -> Optional[List[nodes.Node]]:

# Parse the template with Sphinx Design to create an output container
# Prep the options for the template grid
class_ = "gallery-directive" + f' {self.options.get("class-container", "")}'
class_ = "gallery-directive" + f" {self.options.get('class-container', '')}"
options = {"gutter": 2, "class-container": class_}
options_str = "\n".join(f":{k}: {v}" for k, v in options.items())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,13 @@ def predict(
sentence = sentence[: sentence.index(ai_token)].strip()
break
sentence = sentence.strip()
a, b = [[y[0], convert_to_markdown(y[1])] for y in history] + [[text, convert_to_markdown(sentence)]], [
*history,
[text, sentence],
]
a, b = (
[[y[0], convert_to_markdown(y[1])] for y in history] + [[text, convert_to_markdown(sentence)]],
[
*history,
[text, sentence],
],
)
yield a, b, "Generating..."

if shared_state.interrupted:
Expand Down
1 change: 0 additions & 1 deletion examples/inception/prepare_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@


def resolve_windows_config():

with Path("inception_config.json").open() as f:
snpe_windows_config = json.load(f)

Expand Down
4 changes: 2 additions & 2 deletions examples/llama2/tensor_parallel_generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
torch.cuda.empty_cache()

model_id = "meta-llama/Llama-2-7b-hf"
model_path = "models/tensor_parallel/tensor_parallel-conversion-transformers_optimization_fp16/gpu-cuda_model/model_{:02d}".format( # noqa: E501
rank
model_path = (
f"models/tensor_parallel/tensor_parallel-conversion-transformers_optimization_fp16/gpu-cuda_model/model_{rank:02d}"
)
prompt = "What is an apple?"
# prompt = "Is it normal to have a dark ring around the iris of my eye?"
Expand Down
11 changes: 7 additions & 4 deletions examples/open_llama/user_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,13 @@ def __iter__(self):
inp = trainenc["input_ids"][i:j].unsqueeze(0)
mask = torch.ones(inp.shape)
if self.sess is None:
yield {
"input_ids": inp.detach().cpu().numpy().astype("int64"),
"attention_mask": mask.detach().cpu().numpy().astype("int64"),
}, 0
yield (
{
"input_ids": inp.detach().cpu().numpy().astype("int64"),
"attention_mask": mask.detach().cpu().numpy().astype("int64"),
},
0,
)
else:
outputs = self.sess.run(
None,
Expand Down
7 changes: 3 additions & 4 deletions examples/phi2/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
# --------------------------------------------------------------------------
# copied from https://github.com/microsoft/onnxruntime/blob/main/onnxruntime/python/tools/transformers/models/phi2/inference_example.py # noqa: E501
# copied from https://github.com/microsoft/onnxruntime/blob/main/onnxruntime/python/tools/transformers/models/phi2/inference_example.py
import numpy as np
import onnxruntime as ort
import torch
Expand Down Expand Up @@ -40,7 +40,6 @@ def __init__(self, decoder_path):
self.tokenizer = None

def get_initial_inputs_and_outputs(self, encodings_dict):

input_ids = torch.tensor(encodings_dict["input_ids"], device=self.device, dtype=torch.int32)
attention_mask = torch.tensor(encodings_dict["attention_mask"], device=self.device, dtype=torch.int32)
step = torch.tensor([0], device=self.device, dtype=torch.int64)
Expand Down Expand Up @@ -278,7 +277,7 @@ def genai_run(prompts, model_path, max_length=200):
app_started_timestamp = time.time()
model = og.Model(model_path)
model_loaded_timestamp = time.time()
print("Model loaded in {:.2f} seconds".format(model_loaded_timestamp - app_started_timestamp))
print(f"Model loaded in {model_loaded_timestamp - app_started_timestamp:.2f} seconds")
tokenizer = og.Tokenizer(model)

input_tokens = tokenizer.encode_batch(prompts)
Expand All @@ -299,7 +298,7 @@ def genai_run(prompts, model_path, max_length=200):
output_token_count = 0

for i, prompt in enumerate(prompts):
print(f"Prompt #{i+1:02d}: {prompt}")
print(f"Prompt #{i + 1:02d}: {prompt}")
print(tokenizer.decode(generator.get_sequence(i)))

output_token_count = sum(len(generator.get_sequence(i)) for i in range(len(prompts)))
Expand Down
6 changes: 2 additions & 4 deletions examples/phi3/phi3.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ def use_passes(template_json, *passes):


def generate_config(args):

json_file_template = "phi3_template.json"
with open(json_file_template) as f:
template_json = json.load(f)
Expand Down Expand Up @@ -261,14 +260,13 @@ def generate_config(args):


def genai_run(prompt, model_path, max_length):

print("\nModel inference starts...")

print("Loading model...")
app_started_timestamp = time.time()
model = og.Model(model_path)
model_loaded_timestamp = time.time()
print("Model loaded in {:.2f} seconds".format(model_loaded_timestamp - app_started_timestamp))
print(f"Model loaded in {model_loaded_timestamp - app_started_timestamp:.2f} seconds")

print("Creating tokenizer...")
tokenizer = og.Tokenizer(model)
Expand Down Expand Up @@ -320,7 +318,7 @@ def genai_run(prompt, model_path, max_length):
"\n\n"
f"Prompt tokens: {len(input_tokens)}, New tokens: {len(new_tokens)},"
f" Time to first: {(first_token_timestamp - started_timestamp):.2f}s,"
f" New tokens per second: {len(new_tokens)/run_time:.2f} tps"
f" New tokens per second: {len(new_tokens) / run_time:.2f} tps"
)


Expand Down
6 changes: 3 additions & 3 deletions examples/red_pajama/user_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
MIN_TRANSFORMERS_VERSION = "4.30.2"

# check transformers version
assert (
transformers.__version__ >= MIN_TRANSFORMERS_VERSION
), f"Please upgrade transformers to version {MIN_TRANSFORMERS_VERSION} or higher."
assert transformers.__version__ >= MIN_TRANSFORMERS_VERSION, (
f"Please upgrade transformers to version {MIN_TRANSFORMERS_VERSION} or higher."
)
8 changes: 2 additions & 6 deletions examples/resnet/prepare_model_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,7 @@ def prepare_model(num_epochs=1, models_dir="models", data_dir="data"):
loss.backward()
optimizer.step()
if (i + 1) % 100 == 0:
print(
"Epoch [{}/{}], Step [{}/{}] Loss: {:.4f}".format(
epoch + 1, num_epochs, i + 1, total_step, loss.item()
)
)
print(f"Epoch [{epoch + 1}/{num_epochs}], Step [{i + 1}/{total_step}] Loss: {loss.item():.4f}")
# Decay learning rate
if (epoch + 1) % 20 == 0:
curr_lr /= 3
Expand All @@ -129,7 +125,7 @@ def prepare_model(num_epochs=1, models_dir="models", data_dir="data"):
total += labels.size(0)
correct += (predicted == labels).sum().item()

print("Accuracy of the model on the test images: {} %".format(100 * correct / total))
print(f"Accuracy of the model on the test images: {100 * correct / total} %")

# Save the model
model.to("cpu")
Expand Down
6 changes: 3 additions & 3 deletions examples/utils/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,9 +428,9 @@ def get_initial_inputs(
else [apply_template(template, p) for p in prompt]
)
else:
assert isinstance(prompt, str) or (
isinstance(prompt, list) and all(isinstance(p, str) for p in prompt)
), "tuple prompts require a template"
assert isinstance(prompt, str) or (isinstance(prompt, list) and all(isinstance(p, str) for p in prompt)), (
"tuple prompts require a template"
)

encodings_dict = self.tokenizer(prompt, return_tensors="np", padding=True)
input_ids = encodings_dict["input_ids"].astype(self.input_info["input_ids"]["dtype"])
Expand Down
24 changes: 12 additions & 12 deletions examples/utils/kv_cache_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,18 +160,18 @@ def update(self, present_kvs: List["NDArray"]):
assert present_len > 0, "present_kvs cannot be empty"

if self.seen_len == 0:
assert (
present_len <= self.max_cache_len
), "present_kvs is longer than max_cache_len during prompt processing"
assert present_len <= self.max_cache_len, (
"present_kvs is longer than max_cache_len during prompt processing"
)
# prompt processing
for k, v in zip(self.past_names, present_kvs):
self.cache[k][:, :, :present_len] = v
self.seen_len = present_len
return

assert (
present_len == self.max_cache_len + 1
), "present_kvs must be one step longer than max_cache_len in token generation"
assert present_len == self.max_cache_len + 1, (
"present_kvs must be one step longer than max_cache_len in token generation"
)
for k, v in zip(self.past_names, present_kvs):
self.cache[k][:, :, self.seen_len] = v[:, :, -1]
self.seen_len += 1
Expand Down Expand Up @@ -414,9 +414,9 @@ def update(self, present_kvs: List[OrtValue]):
# prompt processing
present_len = present_kvs[0].shape()[2]
assert present_len > 0, "present_kvs cannot be empty"
assert (
present_len <= self.max_cache_len
), "present_kvs is longer than max_cache_len during prompt processing"
assert present_len <= self.max_cache_len, (
"present_kvs is longer than max_cache_len during prompt processing"
)
for k, v in zip(self.past_names, present_kvs):
if self.backend == "torch":
import torch
Expand All @@ -431,9 +431,9 @@ def update(self, present_kvs: List[OrtValue]):

# token generation
for past_k, present_k, v in zip(self.past_names, self.present_names, present_kvs):
assert (
self.output_cache[present_k].data_ptr() == v.data_ptr()
), "out cache ortvalue should be same as present ortvalue"
assert self.output_cache[present_k].data_ptr() == v.data_ptr(), (
"out cache ortvalue should be same as present ortvalue"
)
if self.backend == "torch":
self.cache[past_k][:, :, self.seen_len] = self.output_cache[present_k][:, :, -1]
else:
Expand Down
1 change: 0 additions & 1 deletion examples/vgg/prepare_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@


def resolve_windows_config():

with Path("vgg_config.json").open() as f:
snpe_windows_config = json.load(f)

Expand Down
2 changes: 1 addition & 1 deletion examples/vit/val_tiny_imagenet/val_tiny_imagenet.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def evaluate_onnx_model(session, dataloader):
ground_truth = val_idx_to_name[label[0]]
pred_label = vit_id2label["id2label"][str(top1_pred)]

print(f"Image {i+1}: {img_name[0]}")
print(f"Image {i + 1}: {img_name[0]}")
print(f" Ground Truth: {ground_truth}")
print(f" Top-1 Prediction: {pred_label}")
print(f" Top-5 Predictions: {[vit_id2label['id2label'][str(pred)] for pred in top5_preds]}\n")
Expand Down
6 changes: 3 additions & 3 deletions olive/auto_optimizer/regulate_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ def _regulate_precision(self, pass_config, pass_flows):

is_cuda_ep = self.accelerator_spec.execution_provider != "TensorrtExecutionProvider"
is_trt_ep = self.accelerator_spec.execution_provider == "TensorrtExecutionProvider"
assert (
not is_cuda_ep or not is_trt_ep
), "can not support CUDA/DmlExecutionProvider and TensorrtExecutionProvider at the same time"
assert not is_cuda_ep or not is_trt_ep, (
"can not support CUDA/DmlExecutionProvider and TensorrtExecutionProvider at the same time"
)

customized_fp16 = self._allow_precision("fp16")
cuda_fp16 = customized_fp16 and is_cuda_ep
Expand Down
1 change: 0 additions & 1 deletion olive/cli/auto_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@


class AutoOptCommand(BaseOliveCLICommand):

@staticmethod
def register_subcommand(parser: ArgumentParser):
sub_parser = parser.add_parser(
Expand Down
6 changes: 3 additions & 3 deletions olive/cli/generate_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ def run(self):

def _get_run_config(self, tempdir: str) -> Dict:
input_model_config = get_input_model_config(self.args)
assert (
input_model_config["type"].lower() == "onnxmodel"
), "Only ONNX models are supported in generate-adapter command."
assert input_model_config["type"].lower() == "onnxmodel", (
"Only ONNX models are supported in generate-adapter command."
)

to_replace = [
("input_model", input_model_config),
Expand Down
4 changes: 1 addition & 3 deletions olive/cli/quantize.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@


class QuantizeCommand(BaseOliveCLICommand):

@staticmethod
def register_subcommand(parser: ArgumentParser):
sub_parser = parser.add_parser(
Expand Down Expand Up @@ -130,8 +129,7 @@ def _get_pass_list(self, precision, algo, impl, is_hf_model):

if not pass_list:
raise ValueError(
f"Quantiation for precision {precision}, algorithm {algo} "
f"and implementation {impl} is not supported"
f"Quantiation for precision {precision}, algorithm {algo} and implementation {impl} is not supported"
)
logger.info("pass list: %s", pass_list)
return pass_list
Expand Down
1 change: 0 additions & 1 deletion olive/cli/session_params_tuning.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@


class SessionParamsTuningCommand(BaseOliveCLICommand):

@staticmethod
def register_subcommand(parser: ArgumentParser):
sub_parser = parser.add_parser(
Expand Down
6 changes: 3 additions & 3 deletions olive/data/component/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,9 @@ def __init__(
self.annotations = None
if annotations_file is not None:
self.annotations = np.load(self.data_dir / annotations_file)
assert len(self.annotations) == len(
self.input_files
), "Number of annotations should be equal to number of input files."
assert len(self.annotations) == len(self.input_files), (
"Number of annotations should be equal to number of input files."
)

def __len__(self):
return len(self.input_files)
Expand Down
2 changes: 1 addition & 1 deletion olive/data/component/load_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def huggingface_dataset(
split: Optional[str] = "validation",
data_files: Optional[Union[str, Sequence[str], Mapping[str, Union[str, Sequence[str]]]]] = None,
col_filters: Optional[Mapping[str, Union[str, int, float]]] = None,
**kwargs
**kwargs,
):
"""Create a dataset from huggingface datasets."""
from datasets.utils.logging import disable_progress_bar, set_verbosity_error
Expand Down
2 changes: 1 addition & 1 deletion olive/data/component/pre_process_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def audio_classification_pre_process(
max_samples: Optional[int] = None,
trust_remote_code: Optional[bool] = None,
feature_extractor_args: Optional[Dict[str, Any]] = None,
**kwargs
**kwargs,
):
"""Pre-process data for audio classification task.

Expand Down
Loading