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
This commit is contained in:
2026-07-25 21:08:30 +08:00
parent ceadc34ea9
commit 59248032dc
7 changed files with 20 additions and 11 deletions
+11 -2
View File
@@ -24,6 +24,17 @@ def _early_handler(signum: int, frame):
def install_early_signal_handlers(): def install_early_signal_handlers():
for sig in (signal.SIGTERM, signal.SIGINT): for sig in (signal.SIGTERM, signal.SIGINT):
signal.signal(sig, _early_handler) 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): def register_signal_handlers(context):
@@ -40,5 +51,3 @@ def unregister_signal_handlers():
global _active_context global _active_context
_active_context = None _active_context = None
_early_stop.clear() _early_stop.clear()
signal.signal(signal.SIGTERM, signal.SIG_DFL)
signal.signal(signal.SIGINT, signal.SIG_DFL)
+1 -1
View File
@@ -1,7 +1,7 @@
"""Training strategy implementations with factory pattern.""" """Training strategy implementations with factory pattern."""
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from typing import Callable, Dict, Optional, Union from typing import Callable, Dict, Union
import torch import torch
import torch.nn as nn import torch.nn as nn
+2 -1
View File
@@ -49,4 +49,5 @@ target-version = "py312"
quote-style = "double" quote-style = "double"
indent-style = "space" indent-style = "space"
skip-magic-trailing-comma = false skip-magic-trailing-comma = false
line-ending = "auto" line-ending = "auto"
exclude = ["*.md", "*.json", "*.yml", "*.yaml"]
+3 -3
View File
@@ -143,7 +143,7 @@ def print_layer_grid(results: dict[str, dict]):
widths = [6] + [10] * len(comps) widths = [6] + [10] * len(comps)
metric = "er_99_norm" 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(format_header(["Layer"] + comps, widths))
print("-" * sum(widths)) print("-" * sum(widths))
@@ -173,7 +173,7 @@ def print_layer_grid(results: dict[str, dict]):
def print_weight_stats(results: dict[str, dict]): def print_weight_stats(results: dict[str, dict]):
groups = group_by_component(results) groups = group_by_component(results)
widths = [20, 12, 12, 12, 12] 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(format_header(["Component", "Mean", "Std", "Min", "Max"], widths))
print("-" * sum(widths)) print("-" * sum(widths))
@@ -265,7 +265,7 @@ def main():
) )
print(f"{'=' * 70}") print(f"{'=' * 70}")
print(f"Loading weights...") print("Loading weights...")
sd = safetensors.torch.load_file(str(weights_path)) sd = safetensors.torch.load_file(str(weights_path))
print(f" {len(sd)} keys loaded") print(f" {len(sd)} keys loaded")
-1
View File
@@ -215,7 +215,6 @@ def _permute_choices(item: dict, rng: random.Random) -> tuple[dict, str]:
positional bias (e.g. always picking B). positional bias (e.g. always picking B).
""" """
letters = ("A", "B", "C", "D") letters = ("A", "B", "C", "D")
contents = [item[k] for k in letters]
perm = list(letters) perm = list(letters)
rng.shuffle(perm) rng.shuffle(perm)
permuted = {"question": item["question"]} permuted = {"question": item["question"]}
+2 -2
View File
@@ -148,7 +148,7 @@ class LossAccumulator:
self.total += sum(losses) self.total += sum(losses)
self.count += len(losses) self.count += len(losses)
if self.stream: 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) idx = torch.tensor(clamped) / self._HIST_MAX * (self._HIST_BINS - 1)
self.hist += torch.bincount( self.hist += torch.bincount(
idx.long().clamp(0, self._HIST_BINS - 1), 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", {}) by_type = stats.get("by_token_type", {})
if by_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" {'type':<12} {'count':>8} {'mean_loss':>10} {'ppl':>8}")
print(f" {'-' * 12} {'-' * 8} {'-' * 10} {'-' * 8}") print(f" {'-' * 12} {'-' * 8} {'-' * 10} {'-' * 8}")
for ttype, s in by_type.items(): for ttype, s in by_type.items():
+1 -1
View File
@@ -824,7 +824,7 @@ def test_grpo_builder_preserves_response_boundaries(base_test_env):
from tests.data.conftest import make_grpo_no_template_config from tests.data.conftest import make_grpo_no_template_config
tokenizer = base_test_env["tokenizer"] 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() builder = SectionedMaskBuilder()
config = make_grpo_no_template_config() config = make_grpo_no_template_config()