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
2 changes: 1 addition & 1 deletion src/llmcompressor/args/dataset_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"""

from dataclasses import dataclass, field
from typing import Callable
from collections.abc import Callable

from datasets import Dataset, DatasetDict
from torch.utils.data import DataLoader
Expand Down
8 changes: 4 additions & 4 deletions src/llmcompressor/core/events/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ class Event:
:type global_batch: int
"""

type_: Optional[EventType] = None
steps_per_epoch: Optional[int] = None
batches_per_step: Optional[int] = None
type_: EventType | None = None
steps_per_epoch: int | None = None
batches_per_step: int | None = None
invocations_per_step: int = 1
Comment on lines +87 to 90
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

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

from typing import Optional appears unused after converting annotations to X | None (it’s only referenced in docstrings now). This will fail ruff check with F401; please remove the unused Optional import.

Copilot uses AI. Check for mistakes.
global_step: int = 0
global_batch: int = 0
Expand Down Expand Up @@ -206,7 +206,7 @@ def current_index(self, value: float):
)

def should_update(
self, start: Optional[float], end: Optional[float], update: Optional[float]
self, start: float | None, end: float | None, update: float | None
) -> bool:
"""
Determines if the event should trigger an update.
Expand Down
4 changes: 3 additions & 1 deletion src/llmcompressor/core/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"""

from dataclasses import dataclass
from typing import Any, Callable
from typing import Any

from collections.abc import Callable

Comment on lines +10 to 13
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

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

Import order likely violates ruff/isort: from collections.abc import Callable should generally be sorted before from typing import Any within the stdlib section. Running ruff check --fix should reorder this block.

Suggested change
from typing import Any
from collections.abc import Callable
from collections.abc import Callable
from typing import Any

Copilot uses AI. Check for mistakes.
from loguru import logger

Expand Down
8 changes: 5 additions & 3 deletions src/llmcompressor/core/session_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

import threading
from contextlib import contextmanager
from typing import TYPE_CHECKING, Any, Generator, Optional
from typing import TYPE_CHECKING, Any, Optional

from collections.abc import Generator

Comment on lines +10 to 13
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

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

Import order likely violates ruff/isort: from collections.abc import Generator should be grouped with other stdlib imports and sorted before the typing import. Running ruff check --fix should normalize this.

Copilot uses AI. Check for mistakes.
from loguru import logger

Expand Down Expand Up @@ -91,7 +93,7 @@ def event(cls, event_type: EventType, **kwargs) -> ModifiedState:
return active_session().event(event_type, **kwargs)

@classmethod
def batch_start(cls, batch_data: Optional[Any] = None, **kwargs) -> ModifiedState:
def batch_start(cls, batch_data: Any | None = None, **kwargs) -> ModifiedState:
"""
Invoke a batch start event for the active session

Expand All @@ -102,7 +104,7 @@ def batch_start(cls, batch_data: Optional[Any] = None, **kwargs) -> ModifiedStat
return cls.event(EventType.BATCH_START, batch_data=batch_data, **kwargs)

@classmethod
def loss_calculated(cls, loss: Optional[Any] = None, **kwargs) -> ModifiedState:
def loss_calculated(cls, loss: Any | None = None, **kwargs) -> ModifiedState:
"""
Invoke a loss calculated event for the active session

Expand Down
6 changes: 4 additions & 2 deletions src/llmcompressor/datasets/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import math
import re
from collections.abc import Iterator, Sized
from typing import Any, Callable, Optional
from typing import Any, Optional

from collections.abc import Callable

Comment on lines 12 to 16
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

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

The stdlib imports here are split and out of isort order (Iterator, Sized imported from collections.abc, then typing, then Callable from collections.abc). This will likely fail ruff check (I001). Consider consolidating the collections.abc imports and letting ruff check --fix sort the stdlib section.

Copilot uses AI. Check for mistakes.
import torch
from datasets import Dataset
Expand Down Expand Up @@ -334,7 +336,7 @@ class LengthAwareSampler(Sampler[int]):
def __init__(
self,
data_source: Dataset,
num_samples: Optional[int] = None,
num_samples: int | None = None,
batch_size: int = 1,
Comment on lines 336 to 340
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

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

Optional is now unused in this module after switching to int | None (and similar) annotations, and from typing import Any, Optional will fail ruff check with F401. Please remove the unused Optional import.

Copilot uses AI. Check for mistakes.
) -> None:
self.data_source = data_source
Expand Down
6 changes: 4 additions & 2 deletions src/llmcompressor/entrypoints/model_free/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import shutil
from concurrent.futures import ThreadPoolExecutor, as_completed
from pathlib import Path
from typing import Iterable, Optional
from typing import Optional
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

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

This import block will likely fail ruff check:

  • Optional is unused after switching to torch.device | str | None (F401).
  • isort typically expects from collections.abc import Iterable to be sorted with the other stdlib imports and usually before typing.
    Please remove the unused import and let ruff check --fix normalize ordering.
Suggested change
from typing import Optional

Copilot uses AI. Check for mistakes.

from collections.abc import Iterable

import torch
import tqdm
Expand Down Expand Up @@ -40,7 +42,7 @@ def model_free_ptq(
scheme: QuantizationScheme | str,
ignore: Iterable[str] = tuple(),
max_workers: int = 1,
device: Optional[torch.device | str] = None,
device: torch.device | str | None = None,
):
"""
Quantize a model without the need for a model definition. This function operates on
Expand Down
8 changes: 5 additions & 3 deletions src/llmcompressor/entrypoints/model_free/helpers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import os
import re
from collections import defaultdict
from typing import Mapping, TypeVar
from typing import TypeVar

from collections.abc import Mapping

Comment on lines +4 to 7
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

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

Import order likely violates ruff/isort: from collections.abc import Mapping should generally be grouped/sorted with other stdlib imports and come before from typing import TypeVar. Running ruff check --fix should reorder these automatically.

Suggested change
from typing import TypeVar
from collections.abc import Mapping
from collections.abc import Mapping
from typing import TypeVar

Copilot uses AI. Check for mistakes.
import torch
from compressed_tensors.utils.match import _match_name
Expand Down Expand Up @@ -95,11 +97,11 @@ def natural_key(s: str) -> list[str | int]:
)

# once we have a full set, yield and reset
if all((matches[target] is not None for target in targets)):
if all(matches[target] is not None for target in targets):
matched_sets.append(matches)
matches = dict.fromkeys(targets, None)

unmatched_set = matches if any((v is not None for v in matches.values())) else None
unmatched_set = matches if any(v is not None for v in matches.values()) else None

if return_unmatched:
return matched_sets, unmatched_set
Expand Down
2 changes: 1 addition & 1 deletion src/llmcompressor/entrypoints/model_free/process.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
from collections import defaultdict
from collections.abc import Iterator, Mapping
from typing import Iterable
from collections.abc import Iterable

Comment on lines 3 to 5
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

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

There are two separate imports from collections.abc (Iterator, Mapping and Iterable). ruff isort rules typically require these to be combined into a single sorted import (otherwise I001). Please consolidate them (or run ruff check --fix).

Copilot uses AI. Check for mistakes.
import torch
from compressed_tensors.quantization import QuantizationScheme
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def reindex_fused_weights(
shutil.copyfile(resolved_path, save_path)

# read index file
with open(index_file, "r") as file:
with open(index_file) as file:
index_file_data = json.load(file)

weight_map: dict[str, str] = index_file_data["weight_map"]
Expand Down
2 changes: 1 addition & 1 deletion src/llmcompressor/entrypoints/model_free/save_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def update_config(
# write results to config.json file
config_file_path = find_config_path(save_directory)
if config_file_path is not None:
with open(config_file_path, "r") as file:
with open(config_file_path) as file:
config_data = json.load(file)

config_data[QUANTIZATION_CONFIG_NAME] = qconfig_data
Expand Down
2 changes: 1 addition & 1 deletion src/llmcompressor/entrypoints/model_free/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def validate_safetensors_index(model_files: dict[str, str], scheme: Quantization
return

if is_microscale_scheme(scheme):
with open(index_file_path, "r") as file:
with open(index_file_path) as file:
weight_map: dict[str, str] = json.load(file)["weight_map"]

file_map = invert_mapping(weight_map)
Expand Down
4 changes: 3 additions & 1 deletion src/llmcompressor/entrypoints/oneshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import os
from datetime import datetime
from pathlib import Path
from typing import TYPE_CHECKING, Callable
from typing import TYPE_CHECKING

from collections.abc import Callable

Comment on lines +15 to 18
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

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

Import order likely violates ruff/isort: from collections.abc import Callable should be sorted before from typing import TYPE_CHECKING within the stdlib section. Running ruff check --fix should correct the ordering.

Suggested change
from typing import TYPE_CHECKING
from collections.abc import Callable
from collections.abc import Callable
from typing import TYPE_CHECKING

Copilot uses AI. Check for mistakes.
from loguru import logger
from torch.utils.data import DataLoader
Expand Down
10 changes: 5 additions & 5 deletions src/llmcompressor/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@
class LoggerConfig:
disabled: bool = False
clear_loggers: bool = True
console_log_level: Optional[str] = "INFO"
log_file: Optional[str] = None
log_file_level: Optional[str] = None
console_log_level: str | None = "INFO"
log_file: str | None = None
log_file_level: str | None = None
metrics_disabled: bool = False


def configure_logger(config: Optional[LoggerConfig] = None) -> None:
def configure_logger(config: LoggerConfig | None = None) -> None:
"""
Comment on lines +57 to 64
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

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

The from typing import Any, Dict, Optional import now has unused names (Dict, Optional) after switching to dict[...] and | None. This will fail ruff check with F401; please remove the unused imports (or run ruff check --fix).

Copilot uses AI. Check for mistakes.
Configure the logger for LLM Compressor.

Expand Down Expand Up @@ -122,7 +122,7 @@ def configure_logger(config: Optional[LoggerConfig] = None) -> None:
logger.level("METRIC", no=38, color="<yellow>", icon="📈")


def support_log_once(record: Dict[str, Any]) -> bool:
def support_log_once(record: dict[str, Any]) -> bool:
"""
Support logging only once using `.bind(log_once=True)`

Expand Down
2 changes: 1 addition & 1 deletion src/llmcompressor/modeling/fuse.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Iterable
from collections.abc import Iterable

import torch
from compressed_tensors import (
Expand Down
10 changes: 5 additions & 5 deletions src/llmcompressor/modeling/gpt_oss.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ def copy_from_fused_weights(
def forward(
self,
hidden_states: torch.Tensor, # [B, T, H]
router_indices: Optional[
router_indices: None | (
torch.Tensor
] = None, # [B, T, top_k] or [tokens, top_k]
routing_weights: Optional[torch.Tensor] = None, # [B, T, E] or [tokens, E]
) = None, # [B, T, top_k] or [tokens, top_k]
routing_weights: torch.Tensor | None = None, # [B, T, E] or [tokens, E]
) -> torch.Tensor:
Comment on lines 109 to 116
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

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

from typing import List, Optional is now unused (the file uses list[...] and | None), which will cause ruff check to fail with F401. Please remove the unused typing imports (or run ruff check --fix).

Copilot uses AI. Check for mistakes.
"""
Implements the MoE computation using the router outputs.
Expand Down Expand Up @@ -192,11 +192,11 @@ def set_module_by_path(root: nn.Module, dotpath: str, new_module: nn.Module) ->
setattr(parent, parts[-1], new_module)


def find_experts(model: nn.Module) -> List[ExpertMeta]:
def find_experts(model: nn.Module) -> list[ExpertMeta]:
"""
Locate GPT-OSS MoE expert modules under model.model.layers[*].mlp.experts.
"""
metas: List[ExpertMeta] = []
metas: list[ExpertMeta] = []
for li, layer in enumerate(model.model.layers):
experts = layer.mlp.experts
device = next(experts.parameters(), torch.zeros(())).device
Expand Down
2 changes: 1 addition & 1 deletion src/llmcompressor/modeling/llama4.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def __init__(
self.shared_expert = original.shared_expert
self.calibrate_all_experts = calibrate_all_experts

def forward(self, hidden_states: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
def forward(self, hidden_states: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:
hidden_states = hidden_states.reshape(-1, self.hidden_dim)
router_scores, router_logits = self.router(hidden_states)
out = self.shared_expert(hidden_states)
Expand Down
1 change: 0 additions & 1 deletion src/llmcompressor/modeling/qwen3_moe.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# coding=utf-8
# Copyright 2025 The Qwen team, Alibaba Group and the HuggingFace Inc. team.
# All rights reserved.
#
Expand Down
12 changes: 6 additions & 6 deletions src/llmcompressor/modifiers/autoround/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,17 +147,17 @@ class AutoRoundModifier(Modifier, QuantizationMixin):
Defaults to None.
"""

sequential_targets: Union[str, List[str], None] = None
sequential_targets: str | list[str] | None = None
# AutoRound modifier arguments
iters: int = 200
enable_torch_compile: bool = True
batch_size: int = 8
lr: Optional[float] = None
device_ids: Optional[str] = None
lr: float | None = None
device_ids: str | None = None

# private variables
_all_module_input: Dict[str, List[Tuple]] = PrivateAttr(default_factory=dict)
_q_input: Optional[torch.Tensor] = PrivateAttr(default=None)
_all_module_input: dict[str, list[tuple]] = PrivateAttr(default_factory=dict)
_q_input: torch.Tensor | None = PrivateAttr(default=None)
Comment on lines +150 to +160
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

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

from typing import Dict, List, Optional, Tuple, Union appears unused after updating this module’s annotations to built-in generics and | None. This will fail ruff check with F401; please remove the unused typing imports (or run ruff check --fix).

Copilot uses AI. Check for mistakes.

def on_initialize(self, state: State, **kwargs) -> bool:
"""
Expand Down Expand Up @@ -338,7 +338,7 @@ def on_finalize(self, state: State, **kwargs) -> bool:

return True

def get_unquantized_layer_names(self, wrapped_model: torch.nn.Module) -> List[str]:
def get_unquantized_layer_names(self, wrapped_model: torch.nn.Module) -> list[str]:
unquantized_layers = []

for name, module in wrapped_model.named_modules():
Expand Down
8 changes: 5 additions & 3 deletions src/llmcompressor/modifiers/awq/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import inspect
from itertools import product
from typing import Iterator, Literal
from typing import Literal

from collections.abc import Iterator

Comment on lines +3 to 6
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

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

Import order likely violates ruff/isort: from collections.abc import Iterator should be sorted before from typing import Literal within the stdlib section. Running ruff check --fix should normalize this ordering.

Suggested change
from typing import Literal
from collections.abc import Iterator
from collections.abc import Iterator
from typing import Literal

Copilot uses AI. Check for mistakes.
import torch
from compressed_tensors.quantization import (
Expand Down Expand Up @@ -335,12 +337,12 @@ def _set_resolved_mappings(self, model: Module) -> None:
resolved_mappings: list[ResolvedMapping] = []
module_to_name = get_module_to_name_dict(model)
# Get names of modules targeted for quantization (excludes ignored)
targeted_names = set(
targeted_names = {
name
for name, _ in match_named_modules(
model, self.resolved_targets, self.ignore
)
)
}
for mapping in self.mappings:
# we deliberately don't use the ignore list when matching mappings,
# so that we can handle layers that need smoothing but not quantization
Expand Down
14 changes: 7 additions & 7 deletions src/llmcompressor/modifiers/gptq/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,17 @@ class GPTQModifier(Modifier, QuantizationMixin):
"""

# gptq modifier arguments
sequential_targets: Union[str, List[str], None] = None
sequential_targets: str | list[str] | None = None
block_size: int = 128
dampening_frac: Optional[float] = 0.01
dampening_frac: float | None = 0.01
# TODO: this does not serialize / will be incorrectly written
actorder: Optional[Union[ActivationOrdering, Sentinel]] = Sentinel("static")
actorder: ActivationOrdering | Sentinel | None = Sentinel("static")
offload_hessians: bool = False
Comment on lines +121 to 126
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

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

from typing import Dict, List, Optional, Tuple, Union appears unused after migrating this module’s annotations to built-in generics/| unions (these names no longer show up in type positions). This will fail ruff check with F401; please remove the unused typing imports (or run ruff check --fix).

Copilot uses AI. Check for mistakes.

# private variables
_module_names: Dict[torch.nn.Module, str] = PrivateAttr(default_factory=dict)
_hessians: Dict[torch.nn.Module, torch.Tensor] = PrivateAttr(default_factory=dict)
_num_samples: Dict[torch.nn.Module, torch.Tensor] = PrivateAttr(
_module_names: dict[torch.nn.Module, str] = PrivateAttr(default_factory=dict)
_hessians: dict[torch.nn.Module, torch.Tensor] = PrivateAttr(default_factory=dict)
_num_samples: dict[torch.nn.Module, torch.Tensor] = PrivateAttr(
default_factory=dict
)

Expand Down Expand Up @@ -235,7 +235,7 @@ def on_event(self, state: State, event: Event, **kwargs):
def calibrate_module(
self,
module: torch.nn.Module,
args: Tuple[torch.Tensor, ...],
args: tuple[torch.Tensor, ...],
_output: torch.Tensor,
):
"""
Expand Down
4 changes: 2 additions & 2 deletions src/llmcompressor/modifiers/pruning/constant/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@


class ConstantPruningModifier(Modifier, LayerParamMasking):
targets: Union[str, List[str]]
parameterized_layers_: Dict[str, ModelParameterizedLayer] = None
targets: str | list[str]
parameterized_layers_: dict[str, ModelParameterizedLayer] = None
_epsilon: float = 10e-9
_save_masks: bool = False
_use_hooks: bool = False
Expand Down
6 changes: 4 additions & 2 deletions src/llmcompressor/modifiers/pruning/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import math
import re
from dataclasses import dataclass
from typing import Any, Callable, Dict
from typing import Any, Dict

from collections.abc import Callable

Comment on lines +12 to 15
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

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

Import order likely violates ruff/isort: from collections.abc import Callable should be sorted before the typing import within the stdlib section. Running ruff check --fix should reorder these imports automatically.

Suggested change
from typing import Any, Dict
from collections.abc import Callable
from collections.abc import Callable
from typing import Any, Dict

Copilot uses AI. Check for mistakes.
from llmcompressor.core import Event, State

Expand All @@ -34,7 +36,7 @@ class PruningCreateSettings:
update: float
init_sparsity: float
final_sparsity: float
args: Dict[str, Any]
args: dict[str, Any]


SchedulerCalculationType = Callable[[Event, State], float]
Expand Down
6 changes: 3 additions & 3 deletions src/llmcompressor/modifiers/pruning/magnitude/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@


class MagnitudePruningModifier(Modifier, LayerParamMasking):
targets: Union[str, List[str]]
targets: str | list[str]
init_sparsity: float
final_sparsity: float
update_scheduler: str = "cubic"
scheduler_args: Dict[str, Any] = {}
scheduler_args: dict[str, Any] = {}
mask_structure: str = "unstructured"
Comment on lines 24 to 30
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

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

The from typing import Any, Dict, List, Union import is now partially unused (Dict, List, Union no longer appear in this module after the |/built-in generic updates) and will fail ruff check with F401. Please trim the typing import to only what’s still used.

Copilot uses AI. Check for mistakes.
leave_enabled: bool = False
apply_globally: bool = False

parameterized_layers_: Dict[str, ModelParameterizedLayer] = None
parameterized_layers_: dict[str, ModelParameterizedLayer] = None
_save_masks: bool = False
_use_hooks: bool = False
scheduler_function_: SchedulerCalculationType = None
Expand Down
Loading
Loading