From 59248032dc2bf93273243cccaf70dcd81c0e45df Mon Sep 17 00:00:00 2001 From: ViperEkura <3081035982@qq.com> Date: Sat, 25 Jul 2026 20:47:38 +0800 Subject: [PATCH] chore: fix ruff lint warnings and signal handling edge cases - Fix pre-existing ruff lint warnings (F401, F541, F841, E741) - Exclude .md/.json/.yml from ruff format check - Unblock SIGTERM/SIGINT via pthread_sigmask in early signal handler - Do not restore SIG_DFL on unregister to prevent pending signal kills --- astrai/parallel/signal_handler.py | 13 +++++++++++-- astrai/trainer/strategy.py | 2 +- pyproject.toml | 3 ++- scripts/eval/analyze_weights.py | 6 +++--- scripts/eval/evaluate_mmlu.py | 1 - scripts/eval/evaluate_ppl.py | 4 ++-- tests/data/test_dataset.py | 2 +- 7 files changed, 20 insertions(+), 11 deletions(-) diff --git a/astrai/parallel/signal_handler.py b/astrai/parallel/signal_handler.py index 5e6a480..4c477f3 100644 --- a/astrai/parallel/signal_handler.py +++ b/astrai/parallel/signal_handler.py @@ -24,6 +24,17 @@ def _early_handler(signum: int, frame): def install_early_signal_handlers(): for sig in (signal.SIGTERM, signal.SIGINT): signal.signal(sig, _early_handler) + _unblock_signals() + + +def _unblock_signals(): + try: + mask = signal.pthread_sigmask(signal.SIG_BLOCK, set()) + blocked = {signal.SIGTERM, signal.SIGINT} & mask + if blocked: + signal.pthread_sigmask(signal.SIG_UNBLOCK, blocked) + except (AttributeError, OSError): + pass def register_signal_handlers(context): @@ -40,5 +51,3 @@ def unregister_signal_handlers(): global _active_context _active_context = None _early_stop.clear() - signal.signal(signal.SIGTERM, signal.SIG_DFL) - signal.signal(signal.SIGINT, signal.SIG_DFL) diff --git a/astrai/trainer/strategy.py b/astrai/trainer/strategy.py index fdb8347..35cab8c 100644 --- a/astrai/trainer/strategy.py +++ b/astrai/trainer/strategy.py @@ -1,7 +1,7 @@ """Training strategy implementations with factory pattern.""" from abc import ABC, abstractmethod -from typing import Callable, Dict, Optional, Union +from typing import Callable, Dict, Union import torch import torch.nn as nn diff --git a/pyproject.toml b/pyproject.toml index ac630df..929f696 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -49,4 +49,5 @@ target-version = "py312" quote-style = "double" indent-style = "space" skip-magic-trailing-comma = false -line-ending = "auto" \ No newline at end of file +line-ending = "auto" +exclude = ["*.md", "*.json", "*.yml", "*.yaml"] \ No newline at end of file diff --git a/scripts/eval/analyze_weights.py b/scripts/eval/analyze_weights.py index 5000300..85c83af 100644 --- a/scripts/eval/analyze_weights.py +++ b/scripts/eval/analyze_weights.py @@ -143,7 +143,7 @@ def print_layer_grid(results: dict[str, dict]): widths = [6] + [10] * len(comps) metric = "er_99_norm" - print(f"\n--- Per-Layer Effective Rank (99% energy) ---") + print("\n--- Per-Layer Effective Rank (99% energy) ---") print(format_header(["Layer"] + comps, widths)) print("-" * sum(widths)) @@ -173,7 +173,7 @@ def print_layer_grid(results: dict[str, dict]): def print_weight_stats(results: dict[str, dict]): groups = group_by_component(results) widths = [20, 12, 12, 12, 12] - print(f"\n--- Weight Value Statistics ---") + print("\n--- Weight Value Statistics ---") print(format_header(["Component", "Mean", "Std", "Min", "Max"], widths)) print("-" * sum(widths)) @@ -265,7 +265,7 @@ def main(): ) print(f"{'=' * 70}") - print(f"Loading weights...") + print("Loading weights...") sd = safetensors.torch.load_file(str(weights_path)) print(f" {len(sd)} keys loaded") diff --git a/scripts/eval/evaluate_mmlu.py b/scripts/eval/evaluate_mmlu.py index 9263c7d..fdff769 100644 --- a/scripts/eval/evaluate_mmlu.py +++ b/scripts/eval/evaluate_mmlu.py @@ -215,7 +215,6 @@ def _permute_choices(item: dict, rng: random.Random) -> tuple[dict, str]: positional bias (e.g. always picking B). """ letters = ("A", "B", "C", "D") - contents = [item[k] for k in letters] perm = list(letters) rng.shuffle(perm) permuted = {"question": item["question"]} diff --git a/scripts/eval/evaluate_ppl.py b/scripts/eval/evaluate_ppl.py index 9c3e83d..171f0d5 100644 --- a/scripts/eval/evaluate_ppl.py +++ b/scripts/eval/evaluate_ppl.py @@ -148,7 +148,7 @@ class LossAccumulator: self.total += sum(losses) self.count += len(losses) if self.stream: - clamped = [min(max(l, 0.0), self._HIST_MAX) for l in losses] + clamped = [min(max(v, 0.0), self._HIST_MAX) for v in losses] idx = torch.tensor(clamped) / self._HIST_MAX * (self._HIST_BINS - 1) self.hist += torch.bincount( idx.long().clamp(0, self._HIST_BINS - 1), @@ -315,7 +315,7 @@ def print_stats(label: str, stats: Dict): ) by_type = stats.get("by_token_type", {}) if by_type: - print(f"\n by token type:") + print("\n by token type:") print(f" {'type':<12} {'count':>8} {'mean_loss':>10} {'ppl':>8}") print(f" {'-' * 12} {'-' * 8} {'-' * 10} {'-' * 8}") for ttype, s in by_type.items(): diff --git a/tests/data/test_dataset.py b/tests/data/test_dataset.py index 93899b6..d04b904 100644 --- a/tests/data/test_dataset.py +++ b/tests/data/test_dataset.py @@ -824,7 +824,7 @@ def test_grpo_builder_preserves_response_boundaries(base_test_env): from tests.data.conftest import make_grpo_no_template_config tokenizer = base_test_env["tokenizer"] - tokenizer_path = _save_test_tokenizer(base_test_env["test_dir"], tokenizer) + _save_test_tokenizer(base_test_env["test_dir"], tokenizer) builder = SectionedMaskBuilder() config = make_grpo_no_template_config()