[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
7b188294e6
commit
fbb98c5c5c
47 changed files with 2646 additions and 2646 deletions
|
|
@ -25,7 +25,7 @@ def timer(name):
|
|||
|
||||
|
||||
@contextmanager
|
||||
def header_footer_context(title: str, char = "-"):
|
||||
def header_footer_context(title: str, char="-"):
|
||||
print()
|
||||
print(f"{char}" * 50 + f" {title} " + f"{char}" * 50)
|
||||
yield
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ import sys
|
|||
import warnings
|
||||
|
||||
|
||||
def clear_memory(variables_to_clear = None, verbose = False, clear_all_caches = True):
|
||||
def clear_memory(variables_to_clear=None, verbose=False, clear_all_caches=True):
|
||||
"""
|
||||
Comprehensive memory clearing for persistent memory leaks.
|
||||
|
||||
|
|
@ -104,7 +104,7 @@ def clear_memory(variables_to_clear = None, verbose = False, clear_all_caches =
|
|||
logger.setLevel(level)
|
||||
|
||||
|
||||
def clear_all_lru_caches(verbose = True):
|
||||
def clear_all_lru_caches(verbose=True):
|
||||
"""Clear all LRU caches in loaded modules."""
|
||||
cleared_caches = []
|
||||
|
||||
|
|
@ -210,7 +210,7 @@ def monitor_cache_sizes():
|
|||
except:
|
||||
pass
|
||||
|
||||
return sorted(cache_info, key = lambda x: x["size"], reverse = True)
|
||||
return sorted(cache_info, key=lambda x: x["size"], reverse=True)
|
||||
|
||||
|
||||
def safe_remove_directory(path):
|
||||
|
|
|
|||
|
|
@ -32,10 +32,10 @@ def create_dataset(tokenizer, num_examples: int = None, messages: list[dict] = N
|
|||
dataset = create_instruction_dataset(messages)
|
||||
|
||||
def _apply_chat_template(example):
|
||||
chat = tokenizer.apply_chat_template(example["messages"], tokenize = False)
|
||||
chat = tokenizer.apply_chat_template(example["messages"], tokenize=False)
|
||||
return {"text": chat}
|
||||
|
||||
dataset = dataset.map(_apply_chat_template, remove_columns = "messages")
|
||||
dataset = dataset.map(_apply_chat_template, remove_columns="messages")
|
||||
if num_examples is not None:
|
||||
if len(dataset) < num_examples:
|
||||
num_repeats = num_examples // len(dataset) + 1
|
||||
|
|
@ -139,11 +139,11 @@ def get_peft_weights(model):
|
|||
|
||||
def describe_peft_weights(model):
|
||||
for name, param in get_peft_weights(model).items():
|
||||
yield name, describe_param(param, as_str = True)
|
||||
yield name, describe_param(param, as_str=True)
|
||||
|
||||
|
||||
def check_responses(responses: list[str], answer: str, prompt: str = None) -> bool:
|
||||
for i, response in enumerate(responses, start = 1):
|
||||
for i, response in enumerate(responses, start=1):
|
||||
if answer in response:
|
||||
print(f"\u2713 response {i} contains answer")
|
||||
else:
|
||||
|
|
|
|||
|
|
@ -41,14 +41,14 @@ class OCRModelEvaluator:
|
|||
Evaluate a model on an OCR dataset.
|
||||
"""
|
||||
# Create output directory if it doesn't exist
|
||||
os.makedirs(output_dir, exist_ok = True)
|
||||
os.makedirs(output_dir, exist_ok=True)
|
||||
|
||||
# Initialize results storage
|
||||
results = []
|
||||
|
||||
# Process each sample in the dataset
|
||||
for i, sample in enumerate(
|
||||
tqdm(dataset, desc = "Evaluating OCR performance", disable = not verbose)
|
||||
tqdm(dataset, desc="Evaluating OCR performance", disable=not verbose)
|
||||
):
|
||||
try:
|
||||
# Extract components from sample
|
||||
|
|
@ -187,7 +187,7 @@ class OCRModelEvaluator:
|
|||
|
||||
# Preparation for inference using Qwen's specific processing
|
||||
text = processor.apply_chat_template(
|
||||
input_messages, tokenize = False, add_generation_prompt = True
|
||||
input_messages, tokenize=False, add_generation_prompt=True
|
||||
)
|
||||
|
||||
# Process vision info (images/videos) from messages
|
||||
|
|
@ -195,11 +195,11 @@ class OCRModelEvaluator:
|
|||
|
||||
# Create model inputs
|
||||
inputs = processor(
|
||||
text = [text],
|
||||
images = image_inputs,
|
||||
videos = video_inputs,
|
||||
padding = True,
|
||||
return_tensors = "pt",
|
||||
text=[text],
|
||||
images=image_inputs,
|
||||
videos=video_inputs,
|
||||
padding=True,
|
||||
return_tensors="pt",
|
||||
)
|
||||
inputs = inputs.to(model.device)
|
||||
|
||||
|
|
@ -207,10 +207,10 @@ class OCRModelEvaluator:
|
|||
with torch.no_grad():
|
||||
generated_ids = model.generate(
|
||||
**inputs,
|
||||
max_new_tokens = max_new_tokens,
|
||||
temperature = temperature,
|
||||
min_p = min_p,
|
||||
use_cache = True,
|
||||
max_new_tokens=max_new_tokens,
|
||||
temperature=temperature,
|
||||
min_p=min_p,
|
||||
use_cache=True,
|
||||
)
|
||||
|
||||
# Extract only the generated part (not the input)
|
||||
|
|
@ -222,8 +222,8 @@ class OCRModelEvaluator:
|
|||
# Decode the generated text
|
||||
generated_response = processor.batch_decode(
|
||||
generated_ids_trimmed,
|
||||
skip_special_tokens = True,
|
||||
clean_up_tokenization_spaces = False,
|
||||
skip_special_tokens=True,
|
||||
clean_up_tokenization_spaces=False,
|
||||
)[0]
|
||||
|
||||
return generated_response
|
||||
|
|
@ -240,7 +240,7 @@ class OCRModelEvaluator:
|
|||
):
|
||||
"""Save individual sample result to file."""
|
||||
output_file = os.path.join(output_dir, f"sample_{sample_idx}.txt")
|
||||
with open(output_file, "w", encoding = "utf-8") as f:
|
||||
with open(output_file, "w", encoding="utf-8") as f:
|
||||
f.write(f"Sample {sample_idx}\n")
|
||||
f.write(f"Question: {question}\n\n")
|
||||
f.write(f"Model output:\n{generated_response.strip()}\n\n")
|
||||
|
|
@ -268,7 +268,7 @@ class OCRModelEvaluator:
|
|||
f.write(f"Average CER: {avg_cer:.4f}\n")
|
||||
|
||||
# Save detailed results
|
||||
df.to_csv(os.path.join(output_dir, "detailed_results.csv"), index = False)
|
||||
df.to_csv(os.path.join(output_dir, "detailed_results.csv"), index=False)
|
||||
|
||||
if verbose:
|
||||
print("\nResults Summary:")
|
||||
|
|
@ -310,12 +310,12 @@ class OCRModelEvaluator:
|
|||
|
||||
# Display the comparison table
|
||||
print("\nComparison Table (sorted by WER):")
|
||||
print(comparison_df.to_string(index = False))
|
||||
print(comparison_df.to_string(index=False))
|
||||
|
||||
# Save the comparison table
|
||||
if save_csv:
|
||||
comparison_file = "model_comparison_results.csv"
|
||||
comparison_df.to_csv(comparison_file, index = False)
|
||||
comparison_df.to_csv(comparison_file, index=False)
|
||||
print(f"\nComparison table saved to {comparison_file}")
|
||||
|
||||
# Generate a bar chart visualization
|
||||
|
|
@ -326,23 +326,23 @@ class OCRModelEvaluator:
|
|||
|
||||
def _create_comparison_plot(self, comparison_df: pd.DataFrame):
|
||||
"""Create and save comparison plot."""
|
||||
plt.figure(figsize = (12, 6))
|
||||
plt.figure(figsize=(12, 6))
|
||||
|
||||
# Plot WER
|
||||
plt.subplot(1, 2, 1)
|
||||
plt.bar(comparison_df["Model"], comparison_df["WER"], color = "skyblue")
|
||||
plt.bar(comparison_df["Model"], comparison_df["WER"], color="skyblue")
|
||||
plt.title("Word Error Rate Comparison")
|
||||
plt.ylabel("WER (lower is better)")
|
||||
plt.ylim(bottom = 0)
|
||||
plt.xticks(rotation = 45, ha = "right")
|
||||
plt.ylim(bottom=0)
|
||||
plt.xticks(rotation=45, ha="right")
|
||||
|
||||
# Plot CER
|
||||
plt.subplot(1, 2, 2)
|
||||
plt.bar(comparison_df["Model"], comparison_df["CER"], color = "lightgreen")
|
||||
plt.bar(comparison_df["Model"], comparison_df["CER"], color="lightgreen")
|
||||
plt.title("Character Error Rate Comparison")
|
||||
plt.ylabel("CER (lower is better)")
|
||||
plt.ylim(bottom = 0)
|
||||
plt.xticks(rotation = 45, ha = "right")
|
||||
plt.ylim(bottom=0)
|
||||
plt.xticks(rotation=45, ha="right")
|
||||
|
||||
plt.tight_layout()
|
||||
plt.savefig("ocr_model_comparison.png")
|
||||
|
|
@ -360,7 +360,7 @@ class OCRModelEvaluator:
|
|||
|
||||
|
||||
def evaluate_ocr_model(
|
||||
model, processor, dataset, output_dir = "ocr_evaluation_results", **kwargs
|
||||
model, processor, dataset, output_dir="ocr_evaluation_results", **kwargs
|
||||
):
|
||||
"""
|
||||
Convenience function that maintains backward compatibility with the original function.
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ def detect_package_manager():
|
|||
return None
|
||||
|
||||
|
||||
def check_package_installed(package_name, package_manager = None):
|
||||
def check_package_installed(package_name, package_manager=None):
|
||||
"""Check if a package is installed using the system package manager"""
|
||||
|
||||
if package_manager is None:
|
||||
|
|
@ -35,26 +35,26 @@ def check_package_installed(package_name, package_manager = None):
|
|||
if package_manager == "apt":
|
||||
# Check with dpkg
|
||||
result = subprocess.run(
|
||||
["dpkg", "-l", package_name], capture_output = True, text = True
|
||||
["dpkg", "-l", package_name], capture_output=True, text=True
|
||||
)
|
||||
return result.returncode == 0
|
||||
|
||||
elif package_manager in ["yum", "dnf"]:
|
||||
# Check with rpm
|
||||
result = subprocess.run(
|
||||
["rpm", "-q", package_name], capture_output = True, text = True
|
||||
["rpm", "-q", package_name], capture_output=True, text=True
|
||||
)
|
||||
return result.returncode == 0
|
||||
|
||||
elif package_manager == "pacman":
|
||||
result = subprocess.run(
|
||||
["pacman", "-Q", package_name], capture_output = True, text = True
|
||||
["pacman", "-Q", package_name], capture_output=True, text=True
|
||||
)
|
||||
return result.returncode == 0
|
||||
|
||||
elif package_manager == "zypper":
|
||||
result = subprocess.run(
|
||||
["zypper", "se", "-i", package_name], capture_output = True, text = True
|
||||
["zypper", "se", "-i", package_name], capture_output=True, text=True
|
||||
)
|
||||
return package_name in result.stdout
|
||||
|
||||
|
|
@ -63,7 +63,7 @@ def check_package_installed(package_name, package_manager = None):
|
|||
return None
|
||||
|
||||
|
||||
def require_package(package_name, executable_name = None):
|
||||
def require_package(package_name, executable_name=None):
|
||||
"""Require a package to be installed, exit if not found"""
|
||||
|
||||
# First check if executable is in PATH (most reliable)
|
||||
|
|
@ -109,7 +109,7 @@ def require_package(package_name, executable_name = None):
|
|||
# require_package("ffmpeg", "ffmpeg")
|
||||
|
||||
|
||||
def require_python_package(package_name, import_name = None, pip_name = None):
|
||||
def require_python_package(package_name, import_name=None, pip_name=None):
|
||||
"""Require a Python package to be installed, exit if not found"""
|
||||
if import_name is None:
|
||||
import_name = package_name
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ def ppl_model(model, tokenizer, dataset):
|
|||
max_length = 2048
|
||||
stride = 512
|
||||
for s in tqdm(range(len(dataset["text"]))):
|
||||
encodings = tokenizer(dataset["text"][s], return_tensors = "pt")
|
||||
encodings = tokenizer(dataset["text"][s], return_tensors="pt")
|
||||
seq_len = encodings.input_ids.size(1)
|
||||
prev_end_loc = 0
|
||||
for begin_loc in range(0, seq_len, stride):
|
||||
|
|
@ -28,7 +28,7 @@ def ppl_model(model, tokenizer, dataset):
|
|||
attention_mask = (input_ids != pad_token_id).long()
|
||||
with torch.no_grad():
|
||||
outputs = model(
|
||||
input_ids, labels = target_ids, attention_mask = attention_mask
|
||||
input_ids, labels=target_ids, attention_mask=attention_mask
|
||||
)
|
||||
neg_log_likelihood = outputs.loss
|
||||
nlls.append(neg_log_likelihood)
|
||||
|
|
@ -78,4 +78,4 @@ def print_model_comparison():
|
|||
|
||||
# Display the comparison table
|
||||
print("\nComparison Table:")
|
||||
print(comparison_df.to_string(index = False))
|
||||
print(comparison_df.to_string(index=False))
|
||||
|
|
|
|||
|
|
@ -32,15 +32,15 @@ def _get_model(qat_scheme: str, full_finetuning: bool):
|
|||
to use QAT. If `full_finetuning` is False, return the PEFT (LoRA) model.
|
||||
"""
|
||||
model, tokenizer = FastLanguageModel.from_pretrained(
|
||||
model_name = "unsloth/Qwen3-1.7B",
|
||||
load_in_4bit = False,
|
||||
full_finetuning = full_finetuning,
|
||||
qat_scheme = qat_scheme if full_finetuning else None,
|
||||
model_name="unsloth/Qwen3-1.7B",
|
||||
load_in_4bit=False,
|
||||
full_finetuning=full_finetuning,
|
||||
qat_scheme=qat_scheme if full_finetuning else None,
|
||||
)
|
||||
if not full_finetuning:
|
||||
model = FastLanguageModel.get_peft_model(
|
||||
model,
|
||||
qat_scheme = qat_scheme,
|
||||
qat_scheme=qat_scheme,
|
||||
)
|
||||
return model, tokenizer
|
||||
|
||||
|
|
@ -140,7 +140,7 @@ def _test_model_fake_quantize(qat_scheme: bool, full_finetuning: bool):
|
|||
_test_linear_is_fake_quantized(layer.mlp.gate_proj, qat_scheme)
|
||||
_test_linear_is_fake_quantized(layer.mlp.up_proj, qat_scheme)
|
||||
_test_linear_is_fake_quantized(layer.mlp.down_proj, qat_scheme)
|
||||
inputs = tokenizer("How are you?", return_tensors = "pt")
|
||||
inputs = tokenizer("How are you?", return_tensors="pt")
|
||||
_test_fake_quantizers_are_called(model, inputs, full_finetuning)
|
||||
|
||||
|
||||
|
|
@ -148,9 +148,9 @@ def _test_model_fake_quantize(qat_scheme: bool, full_finetuning: bool):
|
|||
# how to disable model caching before re-enabling this test
|
||||
@pytest.mark.parametrize("qat_scheme", ["fp8-int4", "fp8-fp8"])
|
||||
def _test_full_model_fake_quantize(qat_scheme: bool):
|
||||
_test_model_fake_quantize(qat_scheme, full_finetuning = True)
|
||||
_test_model_fake_quantize(qat_scheme, full_finetuning=True)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("qat_scheme", ["fp8-int4", "fp8-fp8"])
|
||||
def test_lora_model_fake_quantize(qat_scheme: bool):
|
||||
_test_model_fake_quantize(qat_scheme, full_finetuning = False)
|
||||
_test_model_fake_quantize(qat_scheme, full_finetuning=False)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue