Skip to content

feat: add ruff and resolve issue #2262

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

Merged
merged 10 commits into from
Jul 26, 2024
Merged

feat: add ruff and resolve issue #2262

merged 10 commits into from
Jul 26, 2024

Conversation

drbh
Copy link
Collaborator

@drbh drbh commented Jul 19, 2024

This PR adds ruff linting to TGI and adds ruff to the pre-commit hook

@drbh drbh force-pushed the integrate-ruff-linting branch from d12f426 to 790082b Compare July 19, 2024 16:05
@Narsil
Copy link
Collaborator

Narsil commented Jul 23, 2024

I'm fine moving to ruff. But seeing integration tests failure means something very wrong has happened.

@drbh drbh force-pushed the integrate-ruff-linting branch 2 times, most recently from 05be40f to 634e958 Compare July 24, 2024 14:57
@drbh drbh force-pushed the integrate-ruff-linting branch from 25452f6 to e216e53 Compare July 24, 2024 19:33
Copy link
Collaborator

@Narsil Narsil left a comment

Choose a reason for hiding this comment

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

Thanks a lot.

A few places I'm confused by the changes asked by ruff.
We could merge as-is if necessary, mostly nits.


def is_fbgemm_gpu_available():
try:
return importlib.util.find_spec("fbgemm_gpu.experimental.gen_ai") is not None
Copy link
Collaborator

Choose a reason for hiding this comment

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

What's the rationale behind this change ?

I find it must less clear in intent. If ruff complains there must be a good reason behind it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this is a lint from Pyflakes for unused-import https://docs.astral.sh/ruff/rules/unused-import/

@@ -0,0 +1,56 @@
import torch
Copy link
Collaborator

Choose a reason for hiding this comment

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

Where is this file coming from ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this resolve a Pyflakes issue of undefined-name for the torch_snr_error function used in server/text_generation_server/layers/gptq/quantize.py. This file adds torch_snr_error as copied from openppl-public/ppq

@@ -2,12 +2,13 @@
import math
import torch
from torch import nn
from loguru import logger

# Inverse dim formula to find dim based on number of rotations
Copy link
Collaborator

Choose a reason for hiding this comment

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

??

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ooo stray comment. removed, thanks!

@@ -69,12 +69,12 @@ def load(config, prefix: str, weights):

# GPTQ,AWQ,EETQ don't quantize heads (nor embeddings)
if config.quantize in ["gptq", "awq", "eetq", "marlin"]:
quantize = None
pass
Copy link
Collaborator

Choose a reason for hiding this comment

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

This feels wrong. We really want to set quantize=None in each of these I think.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this is a lint for unused-variable as quantize is set but never used in this function.

reverted the change but i'm not sure if this check is needed..

@danieldk is there a case we need the quantize variable?

@@ -523,7 +528,7 @@ def get_model(
dtype=dtype,
trust_remote_code=trust_remote_code,
)
elif model_type == MAMBA:
elif model_type == ModelType.MAMBA:
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd rather keep the global hack personally.

This is just unecessary indirection to me. Pre-existing code is also indirection compared to pure string comparison, but the reason was to make sure we keep documenting supported models correctly.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this is related to the undefined-name lint, since ruff could not determine that the variables exist.

I agree this adds unnecessary complexity, just reverted to prefer the global variables and added # ruff: noqa: F821 to the top of the file to avoid checking this specific lint.

@@ -233,7 +233,7 @@ def filter(self, request_ids: List[int]) -> Optional["CausalLMBatch"]:
]

# Ensure that past_key_values tensors can be updated in-place
if type(self.past_key_values[0]) == tuple:
if isinstance(self.past_key_values[0], tuple):
Copy link
Collaborator

Choose a reason for hiding this comment

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

No.
type(xxX) == tuple was definitely intended here, ti's not a rookie mistake.
It's because xxx can be an instance of tuple, yet not a tuple.

I don't remember specificially the issue at hand, but I'd rather trust our past selves than the linter here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ahhh great catch!!

It seems that a variable can be an instance of a tuple via inheritance which would return True, even when its not a base tuple

example repro below

class CustomTuple(tuple):
    pass

ct = CustomTuple([1, 2, 3])

print(type(ct) == tuple)  # False
print(isinstance(ct, tuple))  # True

reverted isinstance changes and opted to replace the == with is to make the linter happy.

**This should be equivalent, is checks for object identity where == checks for value, and in the case of built-in types this will be the same.

@@ -39,6 +39,12 @@
from transformers.activations import ACT2FN
from transformers.configuration_utils import PretrainedConfig

if SYSTEM == "rocm":
try:
from vllm import _custom_C
Copy link
Collaborator

Choose a reason for hiding this comment

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

Where is this coming from ?

This doesn'tseem to be linked to ruff.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this is related to a undefined-name lint, _custom_C is used in the DeepseekV2MLP.forward call but was not imported/defined

@@ -45,6 +45,15 @@
SpeculativeHead,
)

# copied from https://github.com/huggingface/transformers/blob/cd4584e3c809bb9e1392ccd3fe38b40daba5519a/src/transformers/models/t5/modeling_t5.py#L1316
# Warning message for FutureWarning: head_mask was separated into two input args - head_mask, decoder_head_mask
__HEAD_MASK_WARNING_MSG = """
Copy link
Collaborator

Choose a reason for hiding this comment

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

???? Why do we need this ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this is also a undefined-name lint, __HEAD_MASK_WARNING_MSG is called as a warning in T5ForConditionalGeneration.forward however I'm not sure we need this warning... leaving for now but happy to remove

@drbh drbh requested a review from Narsil July 25, 2024 17:28
@drbh drbh merged commit bab02ff into main Jul 26, 2024
10 checks passed
@drbh drbh deleted the integrate-ruff-linting branch July 26, 2024 14:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants