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

Switched linters, black -> ruff #334

Merged
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
4 changes: 2 additions & 2 deletions .github/workflows/python-quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ jobs:
run: |
pip install --upgrade pip
pip install .[dev]
- run: black --check bench examples optimum test
- run: ruff check bench examples optimum test
- run: ruff format bench examples optimum test --diff
- run: ruff check --show-fixes bench examples optimum test
ishandeva marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
check_dirs := optimum test bench examples

check:
black --check ${check_dirs}
ruff check ${check_dirs}
ruff check --show-fixes ${check_dirs}
ruff format ${check_dirs} --diff

style:
black ${check_dirs}
ruff check ${check_dirs} --fix
ruff format ${check_dirs}

test:
python -m pytest -sv test
4 changes: 3 additions & 1 deletion examples/nlp/text-generation/quantize_causal_lm_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ def main():
torch_dtype = (
torch.float16
if args.load_dtype == "float16"
else torch.bfloat16 if args.load_dtype == "bfloat16" else torch.float32
else torch.bfloat16
if args.load_dtype == "bfloat16"
else torch.float32
)
model = AutoModelForCausalLM.from_pretrained(args.model, torch_dtype=torch_dtype, low_cpu_mem_usage=True).to(
device
Expand Down
1 change: 0 additions & 1 deletion examples/vision/object-detection/quantize_owl_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@


def detect(model, processor, image, texts):

inputs = processor(text=texts, images=image, return_tensors="pt").to(model.device)

# forward pass
Expand Down
2 changes: 1 addition & 1 deletion external/awq/test_awq_kernels.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
# limitations under the License.
import pytest
import torch

from pack import pack_awq

from optimum.quanto import AffineQuantizer, MaxOptimizer, qint4, ungroup


Expand Down
2 changes: 1 addition & 1 deletion external/awq/test_awq_packing.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
import numpy as np
import pytest
import torch

from pack_intweight import pack_intweight
from packing_utils import pack_awq, reverse_awq_order, unpack_awq

from optimum.quanto import AWQPackedTensor, AWQPacking


Expand Down
2 changes: 1 addition & 1 deletion external/smoothquant/smoothquant.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
from tqdm import tqdm
from transformers import AutoModelForCausalLM, AutoTokenizer
from transformers.models.bloom.modeling_bloom import BloomBlock
from transformers.models.opt.modeling_opt import OPTDecoderLayer
from transformers.models.llama.modeling_llama import LlamaDecoderLayer, LlamaRMSNorm
from transformers.models.mistral.modeling_mistral import MistralDecoderLayer, MistralRMSNorm
from transformers.models.opt.modeling_opt import OPTDecoderLayer


def get_act_scales(model, tokenizer, dataset, num_samples=512, seq_len=512):
Expand Down
1 change: 0 additions & 1 deletion optimum/quanto/library/extensions/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@


class Extension(object):

def __init__(
self,
name: str,
Expand Down
2 changes: 0 additions & 2 deletions optimum/quanto/models/diffusers_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@


class QuantizedDiffusersModel(ModelHubMixin):

BASE_NAME = "quanto"
base_class = None

Expand Down Expand Up @@ -188,5 +187,4 @@ def _save_pretrained(self, save_directory: Path) -> None:


class QuantizedPixArtTransformer2DModel(QuantizedDiffusersModel):

base_class = PixArtTransformer2DModel
2 changes: 0 additions & 2 deletions optimum/quanto/models/transformers_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@


class QuantizedTransformersModel(ModelHubMixin):

BASE_NAME = "quanto"
auto_class = None

Expand Down Expand Up @@ -178,5 +177,4 @@ def _save_pretrained(self, save_directory: Path) -> None:


class QuantizedModelForCausalLM(QuantizedTransformersModel):

auto_class = AutoModelForCausalLM
1 change: 0 additions & 1 deletion optimum/quanto/subpackage/commands/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

@optimum_cli_subcommand()
class QuantoCommand(BaseOptimumCLICommand):

COMMAND = CommandInfo(name="quanto", help="Hugging Face models quantization tools")
SUBCOMMANDS = (
CommandInfo(
Expand Down
1 change: 0 additions & 1 deletion optimum/quanto/tensor/activations/qbytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@


class ActivationQBytesQuantizer(Function):

@staticmethod
def forward(ctx, base: torch.Tensor, qtype: qtype, scale: torch.Tensor) -> torch.Tensor:
if qtype.bits != 8:
Expand Down
1 change: 0 additions & 1 deletion optimum/quanto/tensor/optimizers/absmax_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@


class AbsmaxOptimizer(SymmetricOptimizer):

def optimize(
self, base: torch.Tensor, qtype: qtype, axis: Optional[int] = None
) -> Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]]:
Expand Down
1 change: 0 additions & 1 deletion optimum/quanto/tensor/optimizers/affine_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@


class AffineOptimizer(Optimizer):

def __call__(
self,
base: torch.Tensor,
Expand Down
1 change: 0 additions & 1 deletion optimum/quanto/tensor/optimizers/max_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@


class MaxOptimizer(AffineOptimizer):

def optimize(
self, base: torch.Tensor, qtype: qtype, axis: int
) -> Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]]:
Expand Down
1 change: 0 additions & 1 deletion optimum/quanto/tensor/optimizers/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@


class Optimizer(ABC):

def __call__(
self, base: torch.Tensor, bits: int, axis: int, group_size: Optional[int] = None
) -> Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]]:
Expand Down
1 change: 0 additions & 1 deletion optimum/quanto/tensor/optimizers/symmetric_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@


class SymmetricOptimizer(Optimizer):

def __call__(self, base: torch.Tensor, qtype: qtype, axis: Optional[int] = None) -> torch.Tensor:
if axis not in [None, 0, -1]:
raise ValueError("axis parameter must be None, 0 (first axis) or -1 (last axis)")
Expand Down
1 change: 0 additions & 1 deletion optimum/quanto/tensor/qbits.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ def backward(ctx, gO):


class QBitsTensor(QTensor):

def __init__(self, qtype, axis, group_size, size, stride, data, scale, shift, requires_grad=False):
super().__init__(qtype, axis)
self._data = data
Expand Down
1 change: 0 additions & 1 deletion optimum/quanto/tensor/qbytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ def backward(ctx, gO):


class QBytesTensor(QTensor):

def __init__(self, qtype, axis, size, stride, data, scale, requires_grad=False):
super().__init__(qtype, axis)
self._data = data
Expand Down
1 change: 0 additions & 1 deletion optimum/quanto/tensor/qtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def qfallback(callable, *args, **kwargs):


class QTensor(torch.Tensor):

def __init__(self, qtype, axis):
self._qtype = qtype
self._axis = axis
Expand Down
1 change: 0 additions & 1 deletion optimum/quanto/tensor/weights/marlin/fp8/qbits.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ def __new__(cls, qtype, axis, size, stride, data, scale, requires_grad=False):
)

def __init__(self, qtype, axis, size, stride, data, scale, requires_grad=False):

assert axis == 0
assert data.ndim == 2

Expand Down
1 change: 0 additions & 1 deletion optimum/quanto/tensor/weights/qbits.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@


class WeightsQBitsQuantizer(Function):

@staticmethod
def forward(
ctx,
Expand Down
1 change: 0 additions & 1 deletion optimum/quanto/tensor/weights/qbytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@


class WeightQBytesQuantizer(Function):

@staticmethod
def forward(
ctx, base: torch.Tensor, qtype: qtype, axis: int, scale: torch.Tensor, activation_qtype: qtype, optimized: bool
Expand Down
15 changes: 8 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ dynamic = ['version']
homepage = 'https://github.com/huggingface/optimum-quanto'

[project.optional-dependencies]
dev = ['pytest', 'ruff', 'black']
dev = ['pytest', 'ruff']
examples = [
'torchvision',
'transformers',
Expand All @@ -50,19 +50,20 @@ version = {attr = 'optimum.quanto.__version__'}
requires = ['setuptools>65.5.1', 'setuptools_scm']
build-backend = 'setuptools.build_meta'

[tool.black]
line-length = 119

[tool.ruff]
# Never enforce `E501` (line length violations).
# Configuration for Ruff
line-length = 119 # Same line-length as Black had

# Linting rules:
# Never enforce `E501` (line length violations) and other specific rules.
lint.ignore = ['C901', 'E501', 'E741']
lint.select = ['C', 'E', 'F', 'I', 'W']
line-length = 119

# Ignore import violations in all `__init__.py` files.
[tool.ruff.lint.per-file-ignores]
'__init__.py' = ['E402', 'F401', 'F403', 'F811']

# isort configuration (to sort imports)
[tool.ruff.lint.isort]
lines-after-imports = 2
known-first-party = ['optimum.quanto']
known-first-party = ['optimum.quanto']
2 changes: 1 addition & 1 deletion setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ else
pip install --upgrade --pre torch torchvision torchaudio --index-url https://download.pytorch.org/whl/nightly/cu118
fi
# Build tools
pip install black ruff pytest build
pip install ruff pytest build
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a legacy file I should have removed a long time ago since it is obsoleted by optional targets in pyproject.toml.

Anyway, you also need to edit the python-quality CI workflows under .github.

Copy link
Contributor Author

@ishandeva ishandeva Oct 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyway, you also need to edit the python-quality CI workflows under .github.

Do we want similar verbosity flags on this one as well?
I see that currently it simply calls check :
- run: ruff check bench examples optimum test

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that would be useful thank you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alrighty then, done.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason it might still be relevant is because ppl submitting pull-request might not be familiar with the concept of linting/formatting, but would understand immediately what this is about when looking at the failing CI logs for their pull-request.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see... yes, agreed.

# For examples
pip install accelerate transformers datasets
4 changes: 1 addition & 3 deletions test/library/test_quantize.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,7 @@ def test_affine_quantize(input_shape, dtype, qtype, axis, group_size, shift_mode
"zeropoint": 6e-2,
"float": 5e-2,
},
}[
qtype
][shift_mode]
}[qtype][shift_mode]
if group_size is not None:
qa = ungroup(qa, axis=axis, orig_shape=a.shape)
assert_similar(a, qa, atol=atol)
Expand Down
1 change: 0 additions & 1 deletion test/quantize/test_quantize_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def forward(self, inputs):


class ClassificationModel(torch.nn.Module):

def __init__(self, input_size, output_size, hidden_size, classes):
super().__init__()
self.model = MLP(input_size, output_size, hidden_size)
Expand Down
4 changes: 1 addition & 3 deletions test/tensor/weights/test_weight_qbits_tensor_quantize.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ def test_weight_qbits_tensor_quantize(input_shape, dtype, qtype, axis, group_siz
"zeropoint": 6e-2,
"float": 5e-2,
},
}[
qtype
][shift_mode]
}[qtype][shift_mode]
assert_similar(a, qa, atol=atol)


Expand Down
Loading