Compare commits
15
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f4e641ae15 | ||
|
|
bc077540a0 | ||
|
|
1bdb5bcc5a | ||
|
|
eabd1d75fb | ||
|
|
7f86616b61 | ||
|
|
3a5c864527 | ||
|
|
66a952b49b | ||
|
|
49b56e5af4 | ||
|
|
fd7c77e5c3 | ||
|
|
17f8dff09f | ||
|
|
8ae8526c9d | ||
|
|
208c0ca35b | ||
|
|
a09b4eae07 | ||
|
|
c121a4ba41 | ||
|
|
662e4ba27e |
No files matched your search
@@ -0,0 +1,21 @@
|
||||
<!--
|
||||
Hey there, thanks for contributing to Heretic!
|
||||
|
||||
Please provide a 100% HUMAN-WRITTEN description of the contents and purpose of this pull request.
|
||||
Use the [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) format for the pull request title.
|
||||
Link to any relevant issues or other pull requests in the description.
|
||||
|
||||
Do NOT write the description using an LLM, not even partially.
|
||||
I am a human and reviewing pull requests takes a lot of human effort.
|
||||
I expect to communicate with the human posting the PR, not with their agent.
|
||||
Please do not open a PR if you aren't prepared to respond to comments yourself.
|
||||
-->
|
||||
|
||||
|
||||
### AI use declaration
|
||||
|
||||
Check *either* of the following:
|
||||
|
||||
- [ ] I wrote the code in this PR entirely by myself.
|
||||
- [ ] I used AI to write some or all of the code in this PR, **and** I have reviewed the entire code myself
|
||||
and fully understand it. I'm prepared to discuss the code without letting an agent speak on my behalf.
|
||||
@@ -13,7 +13,7 @@ jobs:
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
python-version: ["3.10", "3.11", "3.12", "3.13"]
|
||||
python-version: ["3.10", "3.11", "3.12", "3.13", "3.14", "3.15"]
|
||||
|
||||
steps:
|
||||
- name: Check out code
|
||||
|
||||
@@ -99,6 +99,13 @@ study_checkpoint_dir = "checkpoints"
|
||||
# Maximum size for individual safetensors files generated when exporting a model.
|
||||
max_shard_size = "5GB"
|
||||
|
||||
# Tags to add to the Hugging Face repository when uploading the model.
|
||||
upload_tags = [
|
||||
"uncensored",
|
||||
"decensored",
|
||||
"abliterated",
|
||||
]
|
||||
|
||||
# System prompt to use when prompting the model.
|
||||
system_prompt = "You are a helpful assistant."
|
||||
|
||||
|
||||
@@ -3,6 +3,10 @@
|
||||
|
||||
max_response_length = 300
|
||||
|
||||
upload_tags = [
|
||||
"no-humor",
|
||||
]
|
||||
|
||||
[scorer.KeywordRate]
|
||||
score_name = "Responses with humor"
|
||||
|
||||
|
||||
@@ -3,6 +3,10 @@
|
||||
|
||||
max_response_length = 300
|
||||
|
||||
upload_tags = [
|
||||
"no-slop",
|
||||
]
|
||||
|
||||
system_prompt = "You are a professional writer."
|
||||
|
||||
[scorer.KeywordRate]
|
||||
|
||||
+34
-15
@@ -14,7 +14,6 @@ classifiers = [
|
||||
"Environment :: Console",
|
||||
"Environment :: GPU",
|
||||
"Intended Audience :: Science/Research",
|
||||
"License :: OSI Approved :: GNU Affero General Public License v3 or later (AGPLv3+)",
|
||||
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
||||
"Programming Language :: Python :: 3",
|
||||
"Programming Language :: Python :: 3.10",
|
||||
@@ -22,32 +21,32 @@ classifiers = [
|
||||
"Programming Language :: Python :: 3.12",
|
||||
]
|
||||
dependencies = [
|
||||
"accelerate~=1.13",
|
||||
"bitsandbytes~=0.49",
|
||||
"datasets~=4.7",
|
||||
"huggingface-hub~=1.7",
|
||||
"accelerate~=1.15",
|
||||
"bitsandbytes~=0.50",
|
||||
"datasets~=5.0",
|
||||
"huggingface-hub~=1.33",
|
||||
"immutabledict~=4.3",
|
||||
"langdetect~=1.0",
|
||||
"lm-eval[hf]~=0.4",
|
||||
"numpy~=2.2",
|
||||
"optuna~=4.7",
|
||||
"peft~=0.19",
|
||||
"optuna~=5.0",
|
||||
"peft~=0.21",
|
||||
"psutil~=7.2",
|
||||
"py-cpuinfo~=9.0",
|
||||
"pydantic-settings~=2.13",
|
||||
"pydantic-settings~=2.15",
|
||||
"questionary~=2.1",
|
||||
"rich~=14.3",
|
||||
"rich~=15.0",
|
||||
"tomli-w~=1.2",
|
||||
"torch", # version deliberately unspecified
|
||||
"torchvision", # version deliberately unspecified
|
||||
"tqdm~=4.67",
|
||||
"transformers[kernels]~=5.6",
|
||||
"tqdm~=4.70",
|
||||
"transformers[kernels]~=5.18",
|
||||
]
|
||||
|
||||
[dependency-groups]
|
||||
dev = [
|
||||
"ruff>=0.14.5",
|
||||
"ty>=0.0.5",
|
||||
"ruff>=0.16.9",
|
||||
"ty>=0.0.84",
|
||||
]
|
||||
|
||||
[project.urls]
|
||||
@@ -61,11 +60,31 @@ Changelog = "https://github.com/p-e-w/heretic/releases"
|
||||
heretic = "heretic.main:main"
|
||||
|
||||
[build-system]
|
||||
requires = ["uv_build>=0.8.11,<0.9.0"]
|
||||
requires = ["uv_build>=0.12.21,<0.13.0"]
|
||||
build-backend = "uv_build"
|
||||
|
||||
[tool.ruff.lint]
|
||||
ignore = [
|
||||
"B023", # "Function definition does not bind loop variable"
|
||||
"BLE001", # "Do not catch blind exception"
|
||||
"TRY002", # "Create your own exception"
|
||||
"SIM117", # "Use a single `with` statement with multiple contexts instead of nested `with` statements"
|
||||
]
|
||||
|
||||
[tool.uv]
|
||||
exclude-newer = "7 days"
|
||||
# TODO: Re-enable before 2.0 release!
|
||||
#exclude-newer = "7 days"
|
||||
|
||||
[tool.uv.sources]
|
||||
torch = { index = "pytorch" }
|
||||
torchvision = { index = "pytorch" }
|
||||
|
||||
[[tool.uv.index]]
|
||||
# Default to CPU wheels for PyTorch to avoid issues in CI.
|
||||
# Can be overridden by passing `--index pytorch=<url>` to uv.
|
||||
name = "pytorch"
|
||||
url = "https://download.pytorch.org/whl/cpu"
|
||||
explicit = true
|
||||
|
||||
[tool.uv.build-backend]
|
||||
module-name = "heretic"
|
||||
+46
-4
@@ -2,7 +2,7 @@
|
||||
# Copyright (C) 2025-2026 Philipp Emanuel Weidmann <pew@worldwidemann.com> + contributors
|
||||
|
||||
from enum import Enum
|
||||
from typing import Dict, Literal, TypeAlias
|
||||
from typing import Any, Literal, TypeAlias
|
||||
|
||||
from pydantic import (
|
||||
BaseModel,
|
||||
@@ -254,12 +254,12 @@ class Settings(BaseSettings):
|
||||
),
|
||||
)
|
||||
|
||||
device_map: str | Dict[str, int | str] = Field(
|
||||
device_map: str | dict[str, int | str] = Field(
|
||||
default="auto",
|
||||
description="Device map to pass to Accelerate when loading the model.",
|
||||
)
|
||||
|
||||
max_memory: Dict[str, str] | None = Field(
|
||||
max_memory: dict[str, str] | None = Field(
|
||||
default=None,
|
||||
description='Maximum memory to allocate per device (e.g., { "0" = "20GB", "cpu" = "64GB" }).',
|
||||
)
|
||||
@@ -541,6 +541,15 @@ class Settings(BaseSettings):
|
||||
description='Which reproducibility information to add to the Hugging Face repository: "full", "basic", "none", or unset to prompt the user.',
|
||||
)
|
||||
|
||||
upload_tags: list[str] = Field(
|
||||
default=[
|
||||
"uncensored",
|
||||
"decensored",
|
||||
"abliterated",
|
||||
],
|
||||
description="Tags to add to the Hugging Face repository when uploading the model.",
|
||||
)
|
||||
|
||||
ignore_mismatches: bool | None = Field(
|
||||
default=None,
|
||||
description="Whether to attempt to reproduce the model even if there are environment mismatches, or unset to prompt the user.",
|
||||
@@ -566,7 +575,7 @@ class Settings(BaseSettings):
|
||||
file_secret_settings: PydanticBaseSettingsSource,
|
||||
) -> tuple[PydanticBaseSettingsSource, ...]:
|
||||
return (
|
||||
init_settings, # Used during resume - should override *all* other sources.
|
||||
init_settings, # Values passed to the constructor override all other sources.
|
||||
CliSettingsSource(
|
||||
settings_cls,
|
||||
cli_parse_args=True,
|
||||
@@ -578,3 +587,36 @@ class Settings(BaseSettings):
|
||||
file_secret_settings,
|
||||
TomlConfigSettingsSource(settings_cls, toml_file="config.toml"),
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def model_validate_init_only(
|
||||
cls, values: dict[str, Any], current: "Settings"
|
||||
) -> "Settings":
|
||||
"""
|
||||
Validates `values` without reading any other settings source: nothing
|
||||
comes from config.toml, environment variables or the command line, and
|
||||
settings missing from `values` get their defaults. Settings with
|
||||
`exclude=True` (such as `save_directory`) are left out of serialized
|
||||
settings, so they are taken from `current`.
|
||||
"""
|
||||
excluded = {
|
||||
name: getattr(current, name)
|
||||
for name, field in cls.model_fields.items()
|
||||
if field.exclude
|
||||
}
|
||||
return InitOnlySettings.model_validate(values | excluded)
|
||||
|
||||
|
||||
class InitOnlySettings(Settings):
|
||||
"""Settings validated only from the values passed in, without reading any source."""
|
||||
|
||||
@classmethod
|
||||
def settings_customise_sources(
|
||||
cls,
|
||||
settings_cls: type[BaseSettings],
|
||||
init_settings: PydanticBaseSettingsSource,
|
||||
env_settings: PydanticBaseSettingsSource,
|
||||
dotenv_settings: PydanticBaseSettingsSource,
|
||||
file_secret_settings: PydanticBaseSettingsSource,
|
||||
) -> tuple[PydanticBaseSettingsSource, ...]:
|
||||
return (init_settings,)
|
||||
+101
-87
@@ -1,10 +1,20 @@
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
# Copyright (C) 2025-2026 Philipp Emanuel Weidmann <pew@worldwidemann.com> + contributors
|
||||
|
||||
# ruff: noqa: E402
|
||||
|
||||
import os
|
||||
import sys
|
||||
|
||||
# Enable expandable segments to reduce memory fragmentation on multi-GPU setups.
|
||||
if (
|
||||
"PYTORCH_ALLOC_CONF" not in os.environ
|
||||
and "PYTORCH_CUDA_ALLOC_CONF" not in os.environ
|
||||
):
|
||||
os.environ["PYTORCH_ALLOC_CONF"] = "expandable_segments:True"
|
||||
|
||||
# Silence warning spam from PyTorch's C++ logging.
|
||||
if "TORCH_CPP_LOG_LEVEL" not in os.environ:
|
||||
os.environ["TORCH_CPP_LOG_LEVEL"] = "ERROR"
|
||||
|
||||
# Ensure standard output/error use UTF-8 instead of system default charmap (e.g. cp1252 on Windows).
|
||||
for stream in (sys.stdout, sys.stderr):
|
||||
if (
|
||||
@@ -13,6 +23,7 @@ for stream in (sys.stdout, sys.stderr):
|
||||
):
|
||||
stream.reconfigure(encoding="utf-8") # type: ignore
|
||||
|
||||
|
||||
from .config import Settings
|
||||
|
||||
|
||||
@@ -23,7 +34,8 @@ def _is_help_invocation() -> bool:
|
||||
|
||||
# Parse and handle CLI help before importing heavyweight ML/runtime dependencies.
|
||||
if _is_help_invocation():
|
||||
Settings() # ty:ignore[missing-argument]
|
||||
Settings()
|
||||
|
||||
|
||||
# FIXME: Rich progress bars are currently disabled because of rendering issues
|
||||
# when used from multiple threads in parallel (e.g. by huggingface_hub).
|
||||
@@ -35,9 +47,9 @@ from .progress import patch_tqdm
|
||||
patch_tqdm()
|
||||
"""
|
||||
|
||||
import json
|
||||
import logging
|
||||
import math
|
||||
import os
|
||||
import random
|
||||
import re
|
||||
import time
|
||||
@@ -182,13 +194,6 @@ def obtain_export_strategy(
|
||||
|
||||
|
||||
def run():
|
||||
# Enable expandable segments to reduce memory fragmentation on multi-GPU setups.
|
||||
if (
|
||||
"PYTORCH_ALLOC_CONF" not in os.environ
|
||||
and "PYTORCH_CUDA_ALLOC_CONF" not in os.environ
|
||||
):
|
||||
os.environ["PYTORCH_ALLOC_CONF"] = "expandable_segments:True"
|
||||
|
||||
# Modified "Pagga" font from https://budavariam.github.io/asciiart-text/
|
||||
print(f"[cyan]█░█░█▀▀░█▀▄░█▀▀░▀█▀░█░█▀▀[/] v{version('heretic-llm')}")
|
||||
print(
|
||||
@@ -223,7 +228,7 @@ def run():
|
||||
try:
|
||||
# The required argument "model" must be provided by the user,
|
||||
# either on the command line or in the configuration file.
|
||||
settings = Settings() # ty:ignore[missing-argument]
|
||||
settings = Settings()
|
||||
except ValidationError as error:
|
||||
print(f"[red]Configuration contains [bold]{error.error_count()}[/] errors:[/]")
|
||||
|
||||
@@ -251,12 +256,10 @@ def run():
|
||||
|
||||
if reproduction_information["version"] != "4":
|
||||
print(
|
||||
(
|
||||
f"[red]Unsupported file format version: [bold]{reproduction_information['version']}[/].[/] "
|
||||
"This version of Heretic reads version 4 (plugin-based) reproduce.json files. "
|
||||
"Older files were produced before the introduction of the plugin system and are not supported. "
|
||||
"Please install Heretic 1.4 to use these files."
|
||||
)
|
||||
f"[red]Unsupported file format version: [bold]{reproduction_information['version']}[/].[/] "
|
||||
"This version of Heretic reads version 4 (plugin-based) reproduce.json files. "
|
||||
"Older files were produced before the introduction of the plugin system and are not supported. "
|
||||
"Please install Heretic 1.4 to use these files."
|
||||
)
|
||||
return
|
||||
|
||||
@@ -265,7 +268,9 @@ def run():
|
||||
|
||||
print()
|
||||
|
||||
settings = Settings.model_validate(reproduction_information["settings"])
|
||||
settings = Settings.model_validate_init_only(
|
||||
reproduction_information["settings"], settings
|
||||
)
|
||||
|
||||
if settings.seed is None:
|
||||
settings.seed = random.randint(0, 2**32 - 1)
|
||||
@@ -339,12 +344,10 @@ def run():
|
||||
if settings.checkpoint_action is None:
|
||||
print()
|
||||
print(
|
||||
(
|
||||
"[green]You have already processed this model.[/] "
|
||||
"You can show the results from the previous run, allowing you to export models or to run additional trials. "
|
||||
"Alternatively, you can ignore the previous run and start from scratch. "
|
||||
"This will delete the checkpoint file and all results from the previous run."
|
||||
)
|
||||
"[green]You have already processed this model.[/] "
|
||||
"You can show the results from the previous run, allowing you to export models or to run additional trials. "
|
||||
"Alternatively, you can ignore the previous run and start from scratch. "
|
||||
"This will delete the checkpoint file and all results from the previous run."
|
||||
)
|
||||
|
||||
choices.append(
|
||||
@@ -357,12 +360,10 @@ def run():
|
||||
if settings.checkpoint_action is None:
|
||||
print()
|
||||
print(
|
||||
(
|
||||
"[yellow]You have already processed this model, but the run was interrupted.[/] "
|
||||
"You can continue the previous run from where it stopped. This will override any specified settings. "
|
||||
"Alternatively, you can ignore the previous run and start from scratch. "
|
||||
"This will delete the checkpoint file and all results from the previous run."
|
||||
)
|
||||
"[yellow]You have already processed this model, but the run was interrupted.[/] "
|
||||
"You can continue the previous run from where it stopped. This will override any specified settings. "
|
||||
"Alternatively, you can ignore the previous run and start from scratch. "
|
||||
"This will delete the checkpoint file and all results from the previous run."
|
||||
)
|
||||
|
||||
choices.append(
|
||||
@@ -402,8 +403,8 @@ def run():
|
||||
return
|
||||
|
||||
if action == "continue":
|
||||
settings = Settings.model_validate_json(
|
||||
existing_study.user_attrs["settings"]
|
||||
settings = Settings.model_validate_init_only(
|
||||
json.loads(existing_study.user_attrs["settings"]), settings
|
||||
)
|
||||
elif action == "restart":
|
||||
os.unlink(study_checkpoint_file)
|
||||
@@ -489,7 +490,7 @@ def run():
|
||||
print(f"* [bold]{len(response_prefix_test_prompts)}[/] prompts loaded")
|
||||
|
||||
print()
|
||||
print("Checking for common response prefix...")
|
||||
print("Checking chat template for a Chain-of-Thought initializer...")
|
||||
|
||||
# Detect if the model's chat template inserts a reasoning tag on its own
|
||||
# at the end of user's prompt (e.g. <think>) by using a dummy prompt.
|
||||
@@ -520,18 +521,26 @@ def run():
|
||||
# We use only the closed CoT block here. Any whitespaces
|
||||
# will be handled by the 'Rechecking with prefix' logic below.
|
||||
settings.response_prefix = closed_cot_block
|
||||
print(
|
||||
f"* Chat template inserts [bold]{escape(repr(cot_initializer))}[/]"
|
||||
)
|
||||
print(
|
||||
f"* Closed Chain-of-Thought block: [bold]{escape(repr(settings.response_prefix))}[/]"
|
||||
)
|
||||
cot_skip_applied = True
|
||||
break
|
||||
|
||||
if not cot_skip_applied:
|
||||
print("* None found")
|
||||
|
||||
# Fallback to inference for models like mistral-3 which are specifically
|
||||
# instructed to generate thinking tags using the system prompt in their
|
||||
# chat template, instead of inserting a prefix tag (e.g. <think>) at
|
||||
# the end of user prompt like the case above. We expect the model to
|
||||
# generate those tags.
|
||||
if settings.response_prefix is None:
|
||||
print()
|
||||
print("Checking for common response prefix...")
|
||||
responses = model.get_responses_batched(response_prefix_test_prompts)
|
||||
|
||||
# Despite being located in os.path, commonprefix actually performs
|
||||
@@ -771,12 +780,10 @@ def run():
|
||||
if settings.trial_index is None:
|
||||
print()
|
||||
print(
|
||||
(
|
||||
"The following trials resulted in Pareto optimal combinations of the optimization objectives. "
|
||||
"After selecting a trial, you will be able to save the model, upload it to Hugging Face, "
|
||||
"chat with it to test how well it works, or run standard benchmarks on it. "
|
||||
"You can return to this menu later to select a different trial. "
|
||||
)
|
||||
"The following trials resulted in Pareto optimal combinations of the optimization objectives. "
|
||||
"After selecting a trial, you will be able to save the model, upload it to Hugging Face, "
|
||||
"chat with it to test how well it works, or run standard benchmarks on it. "
|
||||
"You can return to this menu later to select a different trial. "
|
||||
)
|
||||
|
||||
while trial_loop_active:
|
||||
@@ -950,6 +957,7 @@ def run():
|
||||
save_directory,
|
||||
max_shard_size=settings.max_shard_size,
|
||||
)
|
||||
print(f"Model saved to [bold]{save_directory}[/].")
|
||||
else:
|
||||
print("Saving merged model...")
|
||||
merged_model = model.get_merged_model()
|
||||
@@ -962,10 +970,11 @@ def run():
|
||||
model.tokenizer.save_pretrained(save_directory)
|
||||
if model.processor is not None:
|
||||
model.processor.save_pretrained(save_directory)
|
||||
print(f"Model saved to [bold]{save_directory}[/].")
|
||||
# Merging replaced the trial's adapter in place.
|
||||
# Restore it only after the save has been reported.
|
||||
reset_trial_model()
|
||||
|
||||
print(f"Model saved to [bold]{save_directory}[/].")
|
||||
|
||||
if reproduction_mode:
|
||||
print("Verifying hashes of weight files...")
|
||||
|
||||
@@ -1077,13 +1086,11 @@ def run():
|
||||
if is_reproducible:
|
||||
if settings.upload_reproducibility_information is None:
|
||||
print(
|
||||
(
|
||||
"Heretic can add information to the repository that allows others to reproduce the model. "
|
||||
"This is optional, but valuable to the community as both a learning tool and to preserve computational work already done. "
|
||||
"Guaranteeing reproducibility requires basic system information (Python and OS version, CPU and GPU/accelerator info) "
|
||||
"as tensor operations can give different results in different system environments. "
|
||||
"[bold]The information does not include any file system paths or other private data.[/]"
|
||||
)
|
||||
"Heretic can add information to the repository that allows others to reproduce the model. "
|
||||
"This is optional, but valuable to the community as both a learning tool and to preserve computational work already done. "
|
||||
"Guaranteeing reproducibility requires basic system information (Python and OS version, CPU and GPU/accelerator info) "
|
||||
"as tensor operations can give different results in different system environments. "
|
||||
"[bold]The information does not include any file system paths or other private data.[/]"
|
||||
)
|
||||
|
||||
reproducibility_information = ask_if_unset(
|
||||
@@ -1115,16 +1122,17 @@ def run():
|
||||
if strategy == ExportStrategy.ADAPTER:
|
||||
print("Uploading LoRA adapter...")
|
||||
model.model.push_to_hub(
|
||||
repo_id,
|
||||
repo_id, # ty: ignore[invalid-argument-type]
|
||||
private=private,
|
||||
max_shard_size=settings.max_shard_size,
|
||||
token=token,
|
||||
)
|
||||
print(f"Model uploaded to [bold]{repo_id}[/].")
|
||||
else:
|
||||
print("Uploading merged model...")
|
||||
merged_model = model.get_merged_model()
|
||||
merged_model.push_to_hub(
|
||||
repo_id,
|
||||
repo_id, # ty: ignore[invalid-argument-type]
|
||||
private=private,
|
||||
max_shard_size=settings.max_shard_size,
|
||||
token=token,
|
||||
@@ -1142,6 +1150,9 @@ def run():
|
||||
private=private,
|
||||
token=token,
|
||||
)
|
||||
print(f"Model uploaded to [bold]{repo_id}[/].")
|
||||
# Merging replaced the trial's adapter in place.
|
||||
# Restore it only after the upload has been reported.
|
||||
reset_trial_model()
|
||||
|
||||
if is_hf_path(settings.model):
|
||||
@@ -1161,11 +1172,15 @@ def run():
|
||||
card.data = ModelCardData()
|
||||
if card.data.tags is None:
|
||||
card.data.tags = []
|
||||
card.data.tags.append("heretic")
|
||||
card.data.tags.append("uncensored")
|
||||
card.data.tags.append("decensored")
|
||||
card.data.tags.append("abliterated")
|
||||
if reproducibility_information != "none":
|
||||
if "heretic" not in card.data.tags:
|
||||
card.data.tags.append("heretic")
|
||||
for tag in settings.upload_tags:
|
||||
if tag not in card.data.tags:
|
||||
card.data.tags.append(tag)
|
||||
if (
|
||||
reproducibility_information != "none"
|
||||
and "reproducible" not in card.data.tags
|
||||
):
|
||||
card.data.tags.append("reproducible")
|
||||
|
||||
# Must be a Hugging Face Hub repository ID,
|
||||
@@ -1206,8 +1221,6 @@ def run():
|
||||
finally:
|
||||
settings.export_strategy = current_export_strategy
|
||||
|
||||
print(f"Model uploaded to [bold]{repo_id}[/].")
|
||||
|
||||
if reproduction_mode:
|
||||
print("Verifying hashes of weight files...")
|
||||
|
||||
@@ -1319,7 +1332,7 @@ def run():
|
||||
benchmark_original_model = scope == "Benchmark both models"
|
||||
|
||||
hflm = HFLM(
|
||||
pretrained=model.model, # ty:ignore[invalid-argument-type]
|
||||
pretrained=model.model,
|
||||
tokenizer=model.tokenizer, # ty:ignore[invalid-argument-type]
|
||||
batch_size="auto",
|
||||
)
|
||||
@@ -1333,6 +1346,15 @@ def run():
|
||||
else:
|
||||
table.add_column("Value", justify="right")
|
||||
|
||||
def format_value(value: Any) -> str:
|
||||
if isinstance(
|
||||
value,
|
||||
(float, np.floating),
|
||||
):
|
||||
return f"{value:.4f}"
|
||||
else:
|
||||
return f"{value}"
|
||||
|
||||
try:
|
||||
first_benchmark = True
|
||||
|
||||
@@ -1356,37 +1378,29 @@ def run():
|
||||
first_row = True
|
||||
|
||||
for metric, value in results.items():
|
||||
if metric != "alias":
|
||||
if first_row and not first_benchmark:
|
||||
if benchmark_original_model:
|
||||
table.add_row("", "", "", "")
|
||||
else:
|
||||
table.add_row("", "", "")
|
||||
# Skip non-metrics.
|
||||
if metric in ["name", "alias", "sample_len"]:
|
||||
continue
|
||||
|
||||
def format_value(value: Any) -> str:
|
||||
if isinstance(
|
||||
value,
|
||||
(float, np.floating),
|
||||
):
|
||||
return f"{value:.4f}"
|
||||
else:
|
||||
return f"{value}"
|
||||
|
||||
cells = [
|
||||
benchmark.name if first_row else "",
|
||||
metric,
|
||||
format_value(value),
|
||||
]
|
||||
if first_row and not first_benchmark:
|
||||
if benchmark_original_model:
|
||||
cells.append(
|
||||
format_value(
|
||||
original_results[metric]
|
||||
)
|
||||
)
|
||||
table.add_row(*cells)
|
||||
table.add_row("", "", "", "")
|
||||
else:
|
||||
table.add_row("", "", "")
|
||||
|
||||
first_row = False
|
||||
first_benchmark = False
|
||||
cells = [
|
||||
benchmark.name if first_row else "",
|
||||
metric,
|
||||
format_value(value),
|
||||
]
|
||||
if benchmark_original_model:
|
||||
cells.append(
|
||||
format_value(original_results[metric])
|
||||
)
|
||||
table.add_row(*cells)
|
||||
|
||||
first_row = False
|
||||
first_benchmark = False
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
|
||||
|
||||
+128
-40
@@ -1,8 +1,10 @@
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
# Copyright (C) 2025-2026 Philipp Emanuel Weidmann <pew@worldwidemann.com> + contributors
|
||||
|
||||
from collections.abc import Callable
|
||||
from contextlib import suppress
|
||||
from typing import Any, Callable, Type, TypeAlias, cast
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any, TypeAlias, cast
|
||||
|
||||
import torch
|
||||
from peft import LoraConfig, PeftModel, get_peft_model
|
||||
@@ -23,7 +25,7 @@ from transformers import (
|
||||
TextStreamer,
|
||||
)
|
||||
from transformers.generation import (
|
||||
GenerateDecoderOnlyOutput, # ty:ignore[possibly-missing-import]
|
||||
GenerateDecoderOnlyOutput,
|
||||
)
|
||||
|
||||
from .config import QuantizationMethod, Settings
|
||||
@@ -33,15 +35,46 @@ from .utils import Prompt, batchify, format_exception, print
|
||||
|
||||
def get_model_class(
|
||||
model: str,
|
||||
) -> Type[AutoModelForImageTextToText] | Type[AutoModelForCausalLM]:
|
||||
) -> type[AutoModelForImageTextToText] | type[AutoModelForCausalLM]:
|
||||
configs = PretrainedConfig.get_config_dict(model)
|
||||
|
||||
if any([("vision_config" in config) for config in configs]):
|
||||
if any(("vision_config" in config) for config in configs):
|
||||
return AutoModelForImageTextToText
|
||||
else:
|
||||
return AutoModelForCausalLM
|
||||
|
||||
|
||||
# Some chat templates (e.g. gpt-oss) insert the current date into the prompt,
|
||||
# which makes the residuals, and therefore the modified model, depend on the day
|
||||
# Heretic is run. Rendering templates with a fixed date keeps results reproducible.
|
||||
def strftime_fixed_date(format: str) -> str:
|
||||
return datetime(2026, 1, 1, tzinfo=timezone.utc).strftime(format)
|
||||
|
||||
|
||||
def chat_template_supports_system_role(tokenizer: PreTrainedTokenizerBase) -> bool:
|
||||
# Some chat templates (e.g. Gemma 2 and its fine-tunes) reject the system role
|
||||
# outright, raising when one is present; others silently drop it. The only
|
||||
# reliable way to tell is to render a probe chat and check whether a sentinel
|
||||
# system message survives into the output. This is also how llama.cpp's template
|
||||
# engine (minja) detects system-role support.
|
||||
sentinel = "HERETIC_SYSTEM_ROLE_PROBE"
|
||||
try:
|
||||
rendered = cast(
|
||||
str,
|
||||
tokenizer.apply_chat_template(
|
||||
[
|
||||
{"role": "system", "content": sentinel},
|
||||
{"role": "user", "content": "Hi"},
|
||||
],
|
||||
add_generation_prompt=True,
|
||||
tokenize=False,
|
||||
),
|
||||
)
|
||||
except Exception:
|
||||
return False
|
||||
return sentinel in rendered
|
||||
|
||||
|
||||
# The list contains one element per layer.
|
||||
# Each element maps from the component name to a (possibly sparse) mapping
|
||||
# from the module index to an (input, output) tuple containing the I/O
|
||||
@@ -56,6 +89,8 @@ class Model:
|
||||
processor: ProcessorMixin | None
|
||||
peft_config: LoraConfig
|
||||
dtype: torch.dtype
|
||||
# Whether the model's chat template accepts a system role (see __init__).
|
||||
supports_system_role: bool
|
||||
|
||||
def __init__(self, settings: Settings):
|
||||
self.settings = settings
|
||||
@@ -68,9 +103,14 @@ class Model:
|
||||
print()
|
||||
print(f"Loading model [bold]{settings.model}[/]...")
|
||||
|
||||
self.tokenizer = AutoTokenizer.from_pretrained(
|
||||
settings.model,
|
||||
**self.revision_kwargs,
|
||||
# PreTrainedTokenizerBase is the "base class for all tokenizer backends"
|
||||
# according to the documentation.
|
||||
self.tokenizer = cast(
|
||||
PreTrainedTokenizerBase,
|
||||
AutoTokenizer.from_pretrained(
|
||||
settings.model,
|
||||
**self.revision_kwargs,
|
||||
),
|
||||
)
|
||||
|
||||
# Multimodal models have a processor we'll want to save.
|
||||
@@ -90,7 +130,12 @@ class Model:
|
||||
# after the prompt and thinks the sequence is complete.
|
||||
self.tokenizer.padding_side = "left"
|
||||
|
||||
self.model = None # ty:ignore[invalid-assignment]
|
||||
# Detect once whether this tokenizer's chat template accepts a system
|
||||
# role. Templates that don't (e.g. Gemma 2) need the system prompt folded
|
||||
# into the first user message instead (see _apply_system_role).
|
||||
self.supports_system_role = chat_template_supports_system_role(self.tokenizer)
|
||||
|
||||
self.model = None
|
||||
self.max_memory = (
|
||||
{int(k) if k.isdigit() else k: v for k, v in settings.max_memory.items()}
|
||||
if settings.max_memory
|
||||
@@ -143,7 +188,7 @@ class Model:
|
||||
max_new_tokens=1,
|
||||
)
|
||||
except Exception as error:
|
||||
self.model = None # ty:ignore[invalid-assignment]
|
||||
self.model = None
|
||||
empty_cache()
|
||||
|
||||
formatted = format_exception(error)
|
||||
@@ -215,6 +260,15 @@ class Model:
|
||||
# so the result is a PeftModel rather than a PeftMixedModel.
|
||||
self.model = cast(PeftModel, get_peft_model(self.model, self.peft_config))
|
||||
|
||||
# PEFT initializes A randomly from the global RNG. B is zero, so this doesn't
|
||||
# change the model, but zeroing A too keeps saved adapters reproducible.
|
||||
self._zero_lora_weights()
|
||||
|
||||
def _zero_lora_weights(self):
|
||||
for name, module in self.model.named_modules():
|
||||
if ("lora_A" in name or "lora_B" in name) and hasattr(module, "weight"):
|
||||
torch.nn.init.zeros_(module.weight)
|
||||
|
||||
def _get_quantization_config(self, dtype: str) -> BitsAndBytesConfig | None:
|
||||
"""
|
||||
Creates quantization config based on settings.
|
||||
@@ -309,13 +363,13 @@ class Model:
|
||||
|
||||
if current_model == self.settings.model and not self.needs_reload:
|
||||
# Reset LoRA adapters to zero (identity transformation).
|
||||
for name, module in self.model.named_modules():
|
||||
if "lora_B" in name and hasattr(module, "weight"):
|
||||
torch.nn.init.zeros_(module.weight)
|
||||
# Zeroing A as well ensures that adapter weights of modules not modified
|
||||
# by the next trial don't depend on what previous trials did.
|
||||
self._zero_lora_weights()
|
||||
return True
|
||||
|
||||
# Purge existing model object from memory to make space.
|
||||
self.model = None # ty:ignore[invalid-assignment]
|
||||
self.model = None
|
||||
empty_cache()
|
||||
|
||||
quantization_config = self._get_quantization_config(
|
||||
@@ -352,10 +406,10 @@ class Model:
|
||||
|
||||
# Most multimodal models.
|
||||
with suppress(Exception):
|
||||
return model.model.language_model.layers
|
||||
return model.model.language_model.layers # ty: ignore[unresolved-attribute, invalid-return-type]
|
||||
|
||||
# Text-only models.
|
||||
return model.model.layers
|
||||
return model.model.layers # ty: ignore[unresolved-attribute, invalid-return-type]
|
||||
|
||||
def get_layer_modules(self, layer_index: int) -> dict[str, list[Module]]:
|
||||
layer = self.get_layers()[layer_index]
|
||||
@@ -376,50 +430,50 @@ class Model:
|
||||
|
||||
# Standard self-attention out-projection (most models).
|
||||
with suppress(Exception):
|
||||
try_add("attn.o_proj", layer.self_attn.o_proj) # ty:ignore[possibly-missing-attribute]
|
||||
try_add("attn.o_proj", layer.self_attn.o_proj) # ty: ignore[unresolved-attribute]
|
||||
|
||||
# Qwen3.5 MoE hybrid layers use GatedDeltaNet (linear attention) instead of
|
||||
# standard self-attention, so self_attn.o_proj doesn't exist on those layers.
|
||||
with suppress(Exception):
|
||||
try_add("attn.o_proj", layer.linear_attn.out_proj) # ty:ignore[possibly-missing-attribute]
|
||||
try_add("attn.o_proj", layer.linear_attn.out_proj) # ty: ignore[unresolved-attribute]
|
||||
|
||||
# Most dense models.
|
||||
with suppress(Exception):
|
||||
try_add("mlp.down_proj", layer.mlp.down_proj) # ty:ignore[possibly-missing-attribute]
|
||||
try_add("mlp.down_proj", layer.mlp.down_proj) # ty: ignore[unresolved-attribute]
|
||||
|
||||
# Some MoE models (e.g. Qwen3).
|
||||
with suppress(Exception):
|
||||
for expert in layer.mlp.experts: # ty:ignore[possibly-missing-attribute, not-iterable]
|
||||
try_add("mlp.down_proj", expert.down_proj) # ty:ignore[possibly-missing-attribute]
|
||||
for expert in layer.mlp.experts: # ty:ignore[not-iterable, unresolved-attribute]
|
||||
try_add("mlp.down_proj", expert.down_proj) # ty: ignore[unresolved-attribute]
|
||||
|
||||
# Phi-3.5-MoE (and possibly others).
|
||||
with suppress(Exception):
|
||||
for expert in layer.block_sparse_moe.experts: # ty:ignore[possibly-missing-attribute, not-iterable]
|
||||
try_add("mlp.down_proj", expert.w2) # ty:ignore[possibly-missing-attribute]
|
||||
for expert in layer.block_sparse_moe.experts: # ty:ignore[not-iterable, unresolved-attribute]
|
||||
try_add("mlp.down_proj", expert.w2) # ty: ignore[unresolved-attribute]
|
||||
|
||||
# LFM dense operator blocks.
|
||||
with suppress(Exception):
|
||||
try_add("attn.o_proj", layer.conv.out_proj) # ty:ignore[possibly-missing-attribute]
|
||||
try_add("attn.o_proj", layer.conv.out_proj) # ty: ignore[unresolved-attribute]
|
||||
|
||||
with suppress(Exception):
|
||||
try_add("mlp.down_proj", layer.feed_forward.w2) # ty:ignore[possibly-missing-attribute]
|
||||
try_add("mlp.down_proj", layer.feed_forward.w2) # ty: ignore[unresolved-attribute]
|
||||
|
||||
# LFM transformer blocks.
|
||||
with suppress(Exception):
|
||||
try_add("attn.o_proj", layer.self_attn.out_proj) # ty:ignore[possibly-missing-attribute]
|
||||
try_add("attn.o_proj", layer.self_attn.out_proj) # ty: ignore[unresolved-attribute]
|
||||
|
||||
with suppress(Exception):
|
||||
for expert in layer.feed_forward.experts: # ty:ignore[possibly-missing-attribute, not-iterable]
|
||||
try_add("mlp.down_proj", expert.w2) # ty:ignore[possibly-missing-attribute]
|
||||
for expert in layer.feed_forward.experts: # ty:ignore[not-iterable, unresolved-attribute]
|
||||
try_add("mlp.down_proj", expert.w2) # ty: ignore[unresolved-attribute]
|
||||
|
||||
# Granite MoE Hybrid - attention layers with shared_mlp.
|
||||
with suppress(Exception):
|
||||
try_add("mlp.down_proj", layer.shared_mlp.output_linear) # ty:ignore[possibly-missing-attribute]
|
||||
try_add("mlp.down_proj", layer.shared_mlp.output_linear) # ty: ignore[unresolved-attribute]
|
||||
|
||||
# Granite MoE Hybrid - MoE layers with experts.
|
||||
with suppress(Exception):
|
||||
for expert in layer.moe.experts: # ty:ignore[possibly-missing-attribute, not-iterable]
|
||||
try_add("mlp.down_proj", expert.output_linear) # ty:ignore[possibly-missing-attribute]
|
||||
for expert in layer.moe.experts: # ty:ignore[not-iterable, unresolved-attribute]
|
||||
try_add("mlp.down_proj", expert.output_linear) # ty: ignore[unresolved-attribute]
|
||||
|
||||
# We need at least one module across all components for abliteration to work.
|
||||
total_modules = sum(len(mods) for mods in modules.values())
|
||||
@@ -437,16 +491,46 @@ class Model:
|
||||
|
||||
return sorted(components)
|
||||
|
||||
def _apply_system_role(
|
||||
self, messages: list[dict[str, str]]
|
||||
) -> list[dict[str, str]]:
|
||||
# Templates that support the system role get the messages unchanged, so the
|
||||
# rendered chat (and therefore the resulting weights) is byte-for-byte
|
||||
# identical to before. For templates that reject it (e.g. Gemma 2), fold each
|
||||
# system message into the next user message (what Gemma's own later templates
|
||||
# do), so a system role anywhere in the chat is removed before rendering, not
|
||||
# just a leading one. Empty system prompts are dropped; a system message with
|
||||
# no following user message becomes a user turn.
|
||||
if self.supports_system_role:
|
||||
return messages
|
||||
result: list[dict[str, str]] = []
|
||||
pending: list[str] = []
|
||||
for message in messages:
|
||||
if message["role"] == "system":
|
||||
if message["content"]:
|
||||
pending.append(message["content"])
|
||||
continue
|
||||
if pending and message["role"] == "user":
|
||||
message = dict(message)
|
||||
message["content"] = "\n\n".join([*pending, message["content"]])
|
||||
pending = []
|
||||
result.append(message)
|
||||
if pending:
|
||||
result.append({"role": "user", "content": "\n\n".join(pending)})
|
||||
return result
|
||||
|
||||
def generate(
|
||||
self,
|
||||
prompts: list[Prompt],
|
||||
**kwargs: Any,
|
||||
) -> tuple[BatchEncoding, GenerateDecoderOnlyOutput | LongTensor]:
|
||||
chats = [
|
||||
[
|
||||
{"role": "system", "content": prompt.system},
|
||||
{"role": "user", "content": prompt.user},
|
||||
]
|
||||
self._apply_system_role(
|
||||
[
|
||||
{"role": "system", "content": prompt.system},
|
||||
{"role": "user", "content": prompt.user},
|
||||
]
|
||||
)
|
||||
for prompt in prompts
|
||||
]
|
||||
|
||||
@@ -458,6 +542,8 @@ class Model:
|
||||
chats,
|
||||
add_generation_prompt=True,
|
||||
tokenize=False,
|
||||
# Overrides the function that templates call to get the current date.
|
||||
strftime_now=strftime_fixed_date,
|
||||
),
|
||||
)
|
||||
|
||||
@@ -512,11 +598,12 @@ class Model:
|
||||
responses = []
|
||||
|
||||
for batch in batchify(prompts, self.settings.batch_size):
|
||||
for response in self.get_responses(
|
||||
batch,
|
||||
skip_special_tokens=skip_special_tokens,
|
||||
):
|
||||
responses.append(response)
|
||||
responses.extend(
|
||||
self.get_responses(
|
||||
batch,
|
||||
skip_special_tokens=skip_special_tokens,
|
||||
)
|
||||
)
|
||||
|
||||
return responses
|
||||
|
||||
@@ -788,6 +875,7 @@ class Model:
|
||||
return torch.cat(logits, dim=0)
|
||||
|
||||
def stream_chat_response(self, chat: list[dict[str, str]]) -> str:
|
||||
chat = self._apply_system_role(chat)
|
||||
# This cast is valid because str is the return type
|
||||
# for single-chat operation with tokenize=False.
|
||||
chat_prompt = cast(
|
||||
@@ -809,7 +897,7 @@ class Model:
|
||||
# The TextStreamer constructor annotates this parameter with the AutoTokenizer
|
||||
# type, which makes no sense because AutoTokenizer is a factory class,
|
||||
# not a base class that tokenizers inherit from.
|
||||
self.tokenizer, # ty:ignore[invalid-argument-type]
|
||||
self.tokenizer,
|
||||
skip_prompt=True,
|
||||
skip_special_tokens=True,
|
||||
)
|
||||
|
||||
@@ -32,7 +32,7 @@ class Serializable(Protocol):
|
||||
Parameters = TypeVar("Parameters", bound=Serializable)
|
||||
|
||||
|
||||
class Modifier(Generic[Parameters], Plugin, ABC):
|
||||
class Modifier(Plugin, ABC, Generic[Parameters]):
|
||||
"""
|
||||
Abstract base class for modifier plugins.
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ from dataclasses import asdict, dataclass
|
||||
from enum import Enum
|
||||
from typing import Any, cast
|
||||
|
||||
import bitsandbytes as bnb
|
||||
import bitsandbytes.functional as BNB_F
|
||||
import torch
|
||||
import torch.linalg as LA
|
||||
import torch.nn.functional as F
|
||||
@@ -332,9 +332,7 @@ class Abliteration(Modifier[Parameters]):
|
||||
weight_distribution = parameters.weight_distributions[component]
|
||||
|
||||
# Type inference fails here for some reason.
|
||||
distance = cast(
|
||||
float, abs(layer_index - weight_distribution.max_weight_position)
|
||||
)
|
||||
distance = abs(layer_index - weight_distribution.max_weight_position)
|
||||
|
||||
# Don't orthogonalize layers that are more than
|
||||
# min_weight_distance away from max_weight_position.
|
||||
@@ -390,15 +388,10 @@ class Abliteration(Modifier[Parameters]):
|
||||
W = base_weight.to(torch.float32)
|
||||
else:
|
||||
# 4-bit quantization.
|
||||
# This cast is always valid. Type inference fails here because the
|
||||
# bnb.functional module is not found by ty for some reason.
|
||||
W = cast(
|
||||
Tensor,
|
||||
bnb.functional.dequantize_4bit( # ty:ignore[possibly-missing-attribute]
|
||||
base_weight.data,
|
||||
quant_state,
|
||||
).to(torch.float32),
|
||||
)
|
||||
W = BNB_F.dequantize_4bit(
|
||||
base_weight.data,
|
||||
quant_state,
|
||||
).to(torch.float32)
|
||||
|
||||
# Flatten weight matrix to (out_features, in_features).
|
||||
W = W.view(W.shape[0], -1)
|
||||
|
||||
@@ -4,10 +4,11 @@
|
||||
# Arbitrary-Rank Ablation (ARA) (Weidmann 2026)
|
||||
# See https://github.com/p-e-w/heretic/pull/211 for more information.
|
||||
|
||||
import math
|
||||
from dataclasses import asdict, dataclass
|
||||
from typing import Any, cast
|
||||
|
||||
import bitsandbytes as bnb
|
||||
import bitsandbytes.functional as BNB_F
|
||||
import torch
|
||||
import torch.linalg as LA
|
||||
import torch.nn.functional as F
|
||||
@@ -188,6 +189,9 @@ class ARA(Modifier[Parameters]):
|
||||
bad_prompts = ctx.load_prompts(self.settings.bad_prompts)
|
||||
print(f"* [bold]{len(bad_prompts)}[/] prompts loaded")
|
||||
|
||||
self.good_prompt_count = len(good_prompts)
|
||||
self.bad_prompt_count = len(bad_prompts)
|
||||
|
||||
print()
|
||||
print("Obtaining module I/O for good prompts...")
|
||||
self.good_module_io = model.get_module_io_batched(good_prompts)
|
||||
@@ -208,9 +212,11 @@ class ARA(Modifier[Parameters]):
|
||||
0,
|
||||
layer_count // 2,
|
||||
)
|
||||
# The end index is exclusive, so starting its range one above the start range's
|
||||
# upper bound ensures that the layer range is never empty.
|
||||
end_layer_index = trial.suggest_int(
|
||||
"end_layer_index",
|
||||
layer_count // 2,
|
||||
layer_count // 2 + 1,
|
||||
layer_count,
|
||||
)
|
||||
preserve_good_behavior_weight = trial.suggest_float(
|
||||
@@ -229,10 +235,12 @@ class ARA(Modifier[Parameters]):
|
||||
0.0,
|
||||
1.3,
|
||||
)
|
||||
# The nearest neighbors are selected from the outputs for the good and bad prompts,
|
||||
# which contain one vector per prompt, so there can't be more neighbors than prompts.
|
||||
neighbor_count = trial.suggest_int(
|
||||
"neighbor_count",
|
||||
1,
|
||||
15,
|
||||
min(15, self.good_prompt_count, self.bad_prompt_count),
|
||||
)
|
||||
|
||||
return Parameters(
|
||||
@@ -247,6 +255,11 @@ class ARA(Modifier[Parameters]):
|
||||
def modify_model(self, ctx: Context, parameters: Parameters) -> None:
|
||||
model = ctx.get_model()
|
||||
|
||||
# The model's reset zeroes both A and B, where all gradients vanish,
|
||||
# so A must be initialized below for the optimization to make progress.
|
||||
# Generating on the CPU keeps A independent of the device and the global RNG state.
|
||||
generator = torch.Generator().manual_seed(cast(int, self.heretic_settings.seed))
|
||||
|
||||
for layer_index in range(
|
||||
parameters.start_layer_index,
|
||||
parameters.end_layer_index,
|
||||
@@ -264,13 +277,10 @@ class ARA(Modifier[Parameters]):
|
||||
W_base = base_weight.to(torch.float32)
|
||||
else:
|
||||
# Use the original dequantization logic from bitsandbytes.
|
||||
W_base = cast(
|
||||
Tensor,
|
||||
bnb.functional.dequantize_4bit( # ty:ignore[possibly-missing-attribute]
|
||||
base_weight.data,
|
||||
quant_state,
|
||||
).to(torch.float32),
|
||||
)
|
||||
W_base = BNB_F.dequantize_4bit(
|
||||
base_weight.data,
|
||||
quant_state,
|
||||
).to(torch.float32)
|
||||
|
||||
# Pre-calculate the original row norms to preserve them.
|
||||
# See https://huggingface.co/blog/grimjim/norm-preserving-biprojected-abliteration
|
||||
@@ -283,6 +293,17 @@ class ARA(Modifier[Parameters]):
|
||||
lora_A = cast(Tensor, module.lora_A["default"].weight)
|
||||
lora_B = cast(Tensor, module.lora_B["default"].weight)
|
||||
|
||||
# Initialize A the same way PEFT does, but deterministically. See
|
||||
# https://github.com/huggingface/peft/blob/v0.21.2/src/peft/tuners/lora/layer.py#L338
|
||||
initial_A = torch.empty(lora_A.shape)
|
||||
torch.nn.init.kaiming_uniform_(
|
||||
initial_A,
|
||||
a=math.sqrt(5),
|
||||
generator=generator,
|
||||
)
|
||||
with torch.no_grad():
|
||||
lora_A.copy_(initial_A)
|
||||
|
||||
# Move I/O tensors to the device of the adapter weights.
|
||||
good_input, good_output = self.good_module_io[layer_index][
|
||||
component
|
||||
|
||||
@@ -362,4 +362,4 @@ class Plugin:
|
||||
Override this in subclasses to do one-time setup (e.g. load prompts, compute
|
||||
baselines).
|
||||
"""
|
||||
return None
|
||||
return
|
||||
+11
-17
@@ -81,7 +81,7 @@ def collect_reproducibles(path: str):
|
||||
|
||||
found += 1
|
||||
|
||||
commit_hash = paths_info[0].last_commit.oid
|
||||
commit_hash = paths_info[0].last_commit.oid # ty: ignore[unresolved-attribute]
|
||||
|
||||
file_path = (
|
||||
Path(path)
|
||||
@@ -285,12 +285,10 @@ def check_environment(
|
||||
|
||||
else:
|
||||
print(
|
||||
(
|
||||
"[yellow]The provided JSON file does not contain system information. "
|
||||
"Some system parameters can affect reproducibility, but due to the lack of system information, "
|
||||
"Heretic is unable to verify that those parameters match the original environment. "
|
||||
"Reproduction may or may not produce a byte-for-byte identical model.[/]"
|
||||
)
|
||||
"[yellow]The provided JSON file does not contain system information. "
|
||||
"Some system parameters can affect reproducibility, but due to the lack of system information, "
|
||||
"Heretic is unable to verify that those parameters match the original environment. "
|
||||
"Reproduction may or may not produce a byte-for-byte identical model.[/]"
|
||||
)
|
||||
|
||||
requirements = get_requirements_dict()
|
||||
@@ -321,10 +319,8 @@ def check_environment(
|
||||
if system_mismatches or package_mismatches:
|
||||
print()
|
||||
print(
|
||||
(
|
||||
"[yellow]Your local environment doesn't perfectly match the environment "
|
||||
"used to produce the original model. The following components differ:[/]"
|
||||
)
|
||||
"[yellow]Your local environment doesn't perfectly match the environment "
|
||||
"used to produce the original model. The following components differ:[/]"
|
||||
)
|
||||
|
||||
if system_mismatches:
|
||||
@@ -358,12 +354,10 @@ def check_environment(
|
||||
if system_mismatches or package_mismatches:
|
||||
print()
|
||||
print(
|
||||
(
|
||||
f"There is a {cast(MismatchSeverity, mismatch_severity).__rich__()} chance "
|
||||
"that reproduction won't produce a byte-for-byte identical model. "
|
||||
"However, the resulting model will very likely still behave similarly "
|
||||
"to the original model."
|
||||
)
|
||||
f"There is a {cast(MismatchSeverity, mismatch_severity).__rich__()} chance "
|
||||
"that reproduction won't produce a byte-for-byte identical model. "
|
||||
"However, the resulting model will very likely still behave similarly "
|
||||
"to the original model."
|
||||
)
|
||||
|
||||
if settings.ignore_mismatches is None:
|
||||
|
||||
@@ -44,7 +44,7 @@ class BenchmarkScore(Scorer):
|
||||
model = ctx.get_model()
|
||||
|
||||
self.hflm = HFLM(
|
||||
pretrained=model.model, # ty:ignore[invalid-argument-type]
|
||||
pretrained=model.model,
|
||||
tokenizer=model.tokenizer, # ty:ignore[invalid-argument-type]
|
||||
batch_size="auto",
|
||||
)
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
# Copyright (C) 2025-2026 Philipp Emanuel Weidmann <pew@worldwidemann.com> + contributors
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
from rich.markup import escape
|
||||
|
||||
from heretic.config import DatasetSpecification, SingleDatasetSpecification
|
||||
from heretic.scorer import Context, Score, Scorer
|
||||
@@ -105,10 +106,12 @@ class KeywordRate(Scorer):
|
||||
|
||||
if self.settings.print_responses:
|
||||
print()
|
||||
print(f"[bold]System prompt:[/] {prompt.system}")
|
||||
print(f"[bold]Prompt:[/] {prompt.user}")
|
||||
print(f"[bold]System prompt:[/] {escape(prompt.system)}")
|
||||
print(f"[bold]Prompt:[/] {escape(prompt.user)}")
|
||||
if not response.strip():
|
||||
response = "[italic]\\[empty][/]"
|
||||
else:
|
||||
response = escape(response)
|
||||
print(
|
||||
f"[bold]Response:[/] [{'red' if is_match else 'green'}]{response}[/]"
|
||||
)
|
||||
|
||||
@@ -96,7 +96,7 @@ def get_amdgpu_driver_version() -> str | None:
|
||||
if os.path.exists(version_path):
|
||||
with open(version_path, "r", encoding="utf-8") as f:
|
||||
return f.read().strip()
|
||||
except Exception:
|
||||
except Exception: # noqa: S110
|
||||
pass
|
||||
|
||||
return None
|
||||
@@ -249,7 +249,7 @@ def get_accelerator_info_dict() -> dict[str, Any]:
|
||||
info: dict[str, Any] = {
|
||||
"type": "ROCm" if is_rocm else "CUDA",
|
||||
"api_name": "HIP Version" if is_rocm else "CUDA Version",
|
||||
"api_version": torch.version.hip if is_rocm else torch.version.cuda, # ty:ignore[unresolved-attribute]
|
||||
"api_version": torch.version.hip if is_rocm else torch.version.cuda,
|
||||
"driver_version": get_amdgpu_driver_version()
|
||||
if is_rocm
|
||||
else get_nvidia_driver_version(),
|
||||
@@ -264,13 +264,13 @@ def get_accelerator_info_dict() -> dict[str, Any]:
|
||||
return info
|
||||
|
||||
if is_xpu_available():
|
||||
count = torch.xpu.device_count() # ty:ignore[unresolved-attribute]
|
||||
count = torch.xpu.device_count()
|
||||
return {
|
||||
"type": "XPU",
|
||||
"api_name": None,
|
||||
"api_version": None,
|
||||
"driver_version": get_xpu_driver_version(),
|
||||
"devices": [{"name": torch.xpu.get_device_name(i)} for i in range(count)], # ty:ignore[unresolved-attribute]
|
||||
"devices": [{"name": torch.xpu.get_device_name(i)} for i in range(count)],
|
||||
}
|
||||
|
||||
if is_mlu_available():
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
2f1b4d75d067bae3fe44e676721c7f077d243bc007156cb9c2f8b5836613d082 *chat_template.jinja
|
||||
ca80080dfa4ec6ba87152fa2b9afe70b90c400e5c4b1d6bdc3aa3114467ca68f *config.json
|
||||
70070bac883cf9c39b5992450d6b23cd160eaf33099e24c654e0359d2f87c760 *generation_config.json
|
||||
c128bc8647a505e343c8cdff9bdd188b1a4a3f81938148685cbf90dff9827268 *config.json
|
||||
58678fb2b8ae1b96652dffa43f864ad1c1c59e49c889bab4264ba0844c4a23b8 *generation_config.json
|
||||
f3f4ec19504f182486459cf4e255ece265c25f827840d63b6a9d4058b8e4877a *model.safetensors
|
||||
32bdf45d2ad4cc29a0822ddd157a182de76644f0419a6228d151495256e9813c *processor_config.json
|
||||
cc8d3a0ce36466ccc1278bf987df5f71db1719b9ca6b4118264f45cb627bfe0f *tokenizer.json
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
2f1b4d75d067bae3fe44e676721c7f077d243bc007156cb9c2f8b5836613d082 *chat_template.jinja
|
||||
ca80080dfa4ec6ba87152fa2b9afe70b90c400e5c4b1d6bdc3aa3114467ca68f *config.json
|
||||
70070bac883cf9c39b5992450d6b23cd160eaf33099e24c654e0359d2f87c760 *generation_config.json
|
||||
c128bc8647a505e343c8cdff9bdd188b1a4a3f81938148685cbf90dff9827268 *config.json
|
||||
58678fb2b8ae1b96652dffa43f864ad1c1c59e49c889bab4264ba0844c4a23b8 *generation_config.json
|
||||
53c4ee891dce23c0ac85bebc2c4d48301469750fafbb3e6e024c15786d94db8b *model.safetensors
|
||||
32bdf45d2ad4cc29a0822ddd157a182de76644f0419a6228d151495256e9813c *processor_config.json
|
||||
cc8d3a0ce36466ccc1278bf987df5f71db1719b9ca6b4118264f45cb627bfe0f *tokenizer.json
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
2f1b4d75d067bae3fe44e676721c7f077d243bc007156cb9c2f8b5836613d082 *chat_template.jinja
|
||||
ca80080dfa4ec6ba87152fa2b9afe70b90c400e5c4b1d6bdc3aa3114467ca68f *config.json
|
||||
70070bac883cf9c39b5992450d6b23cd160eaf33099e24c654e0359d2f87c760 *generation_config.json
|
||||
c128bc8647a505e343c8cdff9bdd188b1a4a3f81938148685cbf90dff9827268 *config.json
|
||||
58678fb2b8ae1b96652dffa43f864ad1c1c59e49c889bab4264ba0844c4a23b8 *generation_config.json
|
||||
9ff0593e3fbd0ba463bbc980ebb3ed34798e562b606f90cd93c2df5403732c7b *model.safetensors
|
||||
32bdf45d2ad4cc29a0822ddd157a182de76644f0419a6228d151495256e9813c *processor_config.json
|
||||
cc8d3a0ce36466ccc1278bf987df5f71db1719b9ca6b4118264f45cb627bfe0f *tokenizer.json
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
2f1b4d75d067bae3fe44e676721c7f077d243bc007156cb9c2f8b5836613d082 *chat_template.jinja
|
||||
ca80080dfa4ec6ba87152fa2b9afe70b90c400e5c4b1d6bdc3aa3114467ca68f *config.json
|
||||
70070bac883cf9c39b5992450d6b23cd160eaf33099e24c654e0359d2f87c760 *generation_config.json
|
||||
c128bc8647a505e343c8cdff9bdd188b1a4a3f81938148685cbf90dff9827268 *config.json
|
||||
58678fb2b8ae1b96652dffa43f864ad1c1c59e49c889bab4264ba0844c4a23b8 *generation_config.json
|
||||
effe36925f85ecb1e29bba84501a456bb49df21e4047be8b7ea3f6f88181fb65 *model.safetensors
|
||||
32bdf45d2ad4cc29a0822ddd157a182de76644f0419a6228d151495256e9813c *processor_config.json
|
||||
cc8d3a0ce36466ccc1278bf987df5f71db1719b9ca6b4118264f45cb627bfe0f *tokenizer.json
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
b16d3228a775c549ba97af41233a54e9de8dd2b65250f78346661d18b936a8b5 *chat_template.jinja
|
||||
0094ad598a8043f84d82ad5c886547bca1d1d7f302d82f1491f83d388e89acd4 *config.json
|
||||
1a019c5d688d54cf01318eab88cb4345dfa52135eb1d83c2f54125469eb88d5c *generation_config.json
|
||||
120994c6992d9b02735f2f3f6c0f6bf502004385edd6d20c7b9b35732ce3dd51 *config.json
|
||||
0a86ecc4422176234111947919fb96e578cbf33b1036004fa171d27beb302469 *generation_config.json
|
||||
effe36925f85ecb1e29bba84501a456bb49df21e4047be8b7ea3f6f88181fb65 *model.safetensors
|
||||
24d00232e58cfa179fe8b3911c788d4aad9a6279d778ebe4c72e82623b6197f9 *processor_config.json
|
||||
cc8d3a0ce36466ccc1278bf987df5f71db1719b9ca6b4118264f45cb627bfe0f *tokenizer.json
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
f8d9255777615591a7cc1a7c932f5a69e181128902295e1b81221d20d983cac7 *chat_template.jinja
|
||||
91d2a5190c7ea0f74ed499428d4ad62b5208d63b36f7bbb562d15f4be25bd5c2 *config.json
|
||||
dd6034a30113decdfaf8886622e20eb9e2e02d3f774918d474a4e26cfd7fbba8 *generation_config.json
|
||||
aefe8b9c4b4969f6d13c5d778760f3dce4e25134324b33677934550d9df02a7c *model.safetensors
|
||||
22d559dde87f1a0faf02efed3062af2b1fc8a1721a55b952cd5f5d8c356b4feb *config.json
|
||||
523e422e425e7a7ba23d85cc0faeef143dc5da37b72bfc649df5a8d83ee95beb *generation_config.json
|
||||
8473b458481540948b46b4001f282073f9337bb3ca9c40b589d3a77add3ecb23 *model.safetensors
|
||||
fce342a4642cb8afc42d8d89cfa21198b64a43458ded7f6ff28d1151a08c9cda *tokenizer.json
|
||||
9ba5fa877168e24823cb583c55b4c2e4df0331f30084953c7cf07de294640384 *tokenizer_config.json
|
||||
@@ -0,0 +1,6 @@
|
||||
e381f0b98be3c3714676ec4102bc75a885d75b9002feb02fb6f25ad0e641bce3 *chat_template.jinja
|
||||
07109279e69a421627d0bd4723cbd6828d8427291824baf1c923136e536b8a8a *config.json
|
||||
f74b3fbaec771b2c9650e8289f8b1d6575ca164ad5834331ab0a5e79acdbd9ac *generation_config.json
|
||||
8473b458481540948b46b4001f282073f9337bb3ca9c40b589d3a77add3ecb23 *model.safetensors
|
||||
fce342a4642cb8afc42d8d89cfa21198b64a43458ded7f6ff28d1151a08c9cda *tokenizer.json
|
||||
4c3c901d30c4e7db34ce5ad6e80d607a308fd1e980ca16e5f5d3ab250196b179 *tokenizer_config.json
|
||||
@@ -1,6 +1,6 @@
|
||||
7451a05cf1e28a79d97d7c0bc951028c0b1915119bf9046acd06a0e3d931f47c *chat_template.jinja
|
||||
fe6fd41d9f2ce5d6486748cf0330b574f37bf7d4e915f7b39d1af1a185cac3c3 *config.json
|
||||
c4c2ef5ae4a4e2dd10655a3b99d801a8a50497286ddd042ba35bcfefc44ad349 *generation_config.json
|
||||
de44f54b200f63c8ccb7899965bc9aaf21b23d35164c7de9a3cc3394530e3821 *config.json
|
||||
12d96814a0cd1a72cae14392b04b9644353e8600614faa6eb7ee5edbf9452ec1 *generation_config.json
|
||||
1535a9b7a91b2cb39ad280dbd9a940e2609a0b423d5b924df4d664e579912802 *model.safetensors
|
||||
ad92aaa8d3032c98a9158b8c5e8682bed10027ed6463e4fb1320fe5384210873 *tokenizer.json
|
||||
3ad32522c384dbe35192bb69de9befbf3f523e99d4bb3f95da757671d4c28281 *tokenizer_config.json
|
||||
@@ -1,6 +1,6 @@
|
||||
d8db3ff45c4c68a0ba9dee962ff1a0adde9a2be55e0895306f6bd2b2756f5adb *chat_template.jinja
|
||||
a9d6f64bb9d0c02b553119e475615153af625b5c2a16ccb8fb8b3c2cc348f465 *config.json
|
||||
0e7611a1e8fd0a06a139b0572b2c55b885ba9fb7db2022873c3508aebfb488aa *generation_config.json
|
||||
f6d2e73ef749f83d6b913241899de1b0b55047fe0e15776cae7c66b6219e5087 *config.json
|
||||
1d46ee66092151517d5459a6ca6d4dc263efa98f59e89443e8af8f4c11a8761d *generation_config.json
|
||||
411d95f42d3e31aef41c28314c8f0431c980687a97904d32b4ef57c42199720f *model.safetensors
|
||||
ad92aaa8d3032c98a9158b8c5e8682bed10027ed6463e4fb1320fe5384210873 *tokenizer.json
|
||||
aa083f3da10340925734e876e41e235c459329294ecd35d7511ec5868c1f14e3 *tokenizer_config.json
|
||||
@@ -1,7 +1,7 @@
|
||||
39f03c383413f531fd302c06c7e982ad98c83f0657a8339ae25478ccb81fdcda *chat_template.jinja
|
||||
f69f84977a47c8fea9ce9fc26b7de379216cb01146ea726a87996d3554cfcd19 *config.json
|
||||
34dfa6012ca9ac5f57e5521d8dbaecbc7ab7f7ab0fd96ec020b543aab5f265d9 *generation_config.json
|
||||
876c6691eb85e3e5e11771e589529830fb454ab26344e1271ae550661e312b50 *model.safetensors
|
||||
84be30b124b50749c56d25fdbec5ccedf564446f6b3b035e88e1e07b986d2491 *processor_config.json
|
||||
b2cfd8a9da09efc97040f3839fe629f3925db856a44b58314301dc5e11345ec8 *config.json
|
||||
338d303c3c3df6884030cf5be24560f6e973fc183220d14acfb9364cd0e070e4 *generation_config.json
|
||||
05497f43a38427c5813105fc007ce0dd41be65470e9cb8f343443f31bab7f0fe *model.safetensors
|
||||
a05f93a41b7e42ccc18461b540fb54490c3ffa13aef79c800bbe7942e02e360f *processor_config.json
|
||||
c3a8d92e371b92a2cd6e678e31ebc27d0235e929a51fbf290f74742b341fa96f *tokenizer.json
|
||||
7b29c843c0043622d28fd4638451cbb0a609d99a0762ffbff3b92b4b2fee4d94 *tokenizer_config.json
|
||||
@@ -1,7 +1,7 @@
|
||||
39f03c383413f531fd302c06c7e982ad98c83f0657a8339ae25478ccb81fdcda *chat_template.jinja
|
||||
f69f84977a47c8fea9ce9fc26b7de379216cb01146ea726a87996d3554cfcd19 *config.json
|
||||
34dfa6012ca9ac5f57e5521d8dbaecbc7ab7f7ab0fd96ec020b543aab5f265d9 *generation_config.json
|
||||
6febb813086f253e5ec0fcda02fdfc849c551a7dba54681b37ac5bc402e4eed6 *model.safetensors
|
||||
84be30b124b50749c56d25fdbec5ccedf564446f6b3b035e88e1e07b986d2491 *processor_config.json
|
||||
b2cfd8a9da09efc97040f3839fe629f3925db856a44b58314301dc5e11345ec8 *config.json
|
||||
338d303c3c3df6884030cf5be24560f6e973fc183220d14acfb9364cd0e070e4 *generation_config.json
|
||||
ad6675b44b476a899914761257bbeb7f320cb9c0dc39a7467daf4dad0216dd1f *model.safetensors
|
||||
a05f93a41b7e42ccc18461b540fb54490c3ffa13aef79c800bbe7942e02e360f *processor_config.json
|
||||
c3a8d92e371b92a2cd6e678e31ebc27d0235e929a51fbf290f74742b341fa96f *tokenizer.json
|
||||
7b29c843c0043622d28fd4638451cbb0a609d99a0762ffbff3b92b4b2fee4d94 *tokenizer_config.json
|
||||
@@ -0,0 +1,7 @@
|
||||
39f03c383413f531fd302c06c7e982ad98c83f0657a8339ae25478ccb81fdcda *chat_template.jinja
|
||||
b2cfd8a9da09efc97040f3839fe629f3925db856a44b58314301dc5e11345ec8 *config.json
|
||||
338d303c3c3df6884030cf5be24560f6e973fc183220d14acfb9364cd0e070e4 *generation_config.json
|
||||
4cf4d463a94f477ed446e47130cb7da6b5994f0b788e75b82b3c65a531585621 *model.safetensors
|
||||
a05f93a41b7e42ccc18461b540fb54490c3ffa13aef79c800bbe7942e02e360f *processor_config.json
|
||||
c3a8d92e371b92a2cd6e678e31ebc27d0235e929a51fbf290f74742b341fa96f *tokenizer.json
|
||||
7b29c843c0043622d28fd4638451cbb0a609d99a0762ffbff3b92b4b2fee4d94 *tokenizer_config.json
|
||||
@@ -1,7 +1,7 @@
|
||||
39f03c383413f531fd302c06c7e982ad98c83f0657a8339ae25478ccb81fdcda *chat_template.jinja
|
||||
f69f84977a47c8fea9ce9fc26b7de379216cb01146ea726a87996d3554cfcd19 *config.json
|
||||
34dfa6012ca9ac5f57e5521d8dbaecbc7ab7f7ab0fd96ec020b543aab5f265d9 *generation_config.json
|
||||
29aff97d5633dead9e1ccd29a2cc153b4b7431d22f63c8d6cf60bc6547681cc9 *model.safetensors
|
||||
84be30b124b50749c56d25fdbec5ccedf564446f6b3b035e88e1e07b986d2491 *processor_config.json
|
||||
b2cfd8a9da09efc97040f3839fe629f3925db856a44b58314301dc5e11345ec8 *config.json
|
||||
338d303c3c3df6884030cf5be24560f6e973fc183220d14acfb9364cd0e070e4 *generation_config.json
|
||||
8244162ecb6ce7bad7b10fe4f015889d76a09c7749dc7ac8c4d957f0864b2e82 *model.safetensors
|
||||
a05f93a41b7e42ccc18461b540fb54490c3ffa13aef79c800bbe7942e02e360f *processor_config.json
|
||||
c3a8d92e371b92a2cd6e678e31ebc27d0235e929a51fbf290f74742b341fa96f *tokenizer.json
|
||||
7b29c843c0043622d28fd4638451cbb0a609d99a0762ffbff3b92b4b2fee4d94 *tokenizer_config.json
|
||||
@@ -1,7 +1,7 @@
|
||||
72f84af4ea36b82409c35e31b584361534305ef7c0d90fce20d0dc38a7efead8 *chat_template.jinja
|
||||
e4c5278b361c57621253c27a2c3db358e1580aec8a14be8e19d4420a224137cf *config.json
|
||||
8dde85c000ae807be907421465826c7c63a39f6acf6d04a5a84efaf116ed4ef7 *generation_config.json
|
||||
20b5a820b38438202c64e4fc9807bd19e29678bebd678d29b2ee2d2f5bf71587 *model.safetensors
|
||||
20e7a6dcde0a6f60ea3b4fb08f6f7afa62532dda93a3111e28384ba5150575f9 *processor_config.json
|
||||
c2b697282298f9500f1170b9237dc234dbe1cb7ca741fe56b034d64d9e38e1d2 *config.json
|
||||
d9f7f986f62f4f1f87790070237e0214865e5c227f4941f7308c838702a69934 *generation_config.json
|
||||
8244162ecb6ce7bad7b10fe4f015889d76a09c7749dc7ac8c4d957f0864b2e82 *model.safetensors
|
||||
5abd409b04043c99c3fcd52b06cc412b46fb4bd14c8ef9f79695a3be1490987b *processor_config.json
|
||||
c3a8d92e371b92a2cd6e678e31ebc27d0235e929a51fbf290f74742b341fa96f *tokenizer.json
|
||||
60a8042e29b4b20e884e48375aa1b9ac0025547371d50e60f6d55e6a9675e868 *tokenizer_config.json
|
||||
@@ -1,6 +1,6 @@
|
||||
cd8e9439f0570856fd70470bf8889ebd8b5d1107207f67a5efb46e342330527f *chat_template.jinja
|
||||
45134b857367fdcb97c0179199848c353fc28f8b95ac2244ac8f45cca448d864 *config.json
|
||||
e81e23e025c38e825dcf8375861e26a90e804276e4db9ee390122a4fdc95dae7 *generation_config.json
|
||||
bd86541d817978c896bd3579e69ae6d41b6382eaf1646accf83d6feb16acb703 *model.safetensors
|
||||
6b517667960a5e39a692eb8277be7260ac02c22d9c7eda9c7aa1bbab21517fc1 *config.json
|
||||
64cf6f4e0016154cae45628221d77e3925961876125fe68bd942303ec7cca40f *generation_config.json
|
||||
e616cbeb5a913015eb3db96e001030048df2db560df363d4cf688f0c1b2c96de *model.safetensors
|
||||
f7f96da3a872b5e901575b2067c744ad336c3a3d77a21584d20024557b1bd7f0 *tokenizer.json
|
||||
04b1682c59acbd057f4c9072297faa73d56fc9de053094c659cdb4c464f58f86 *tokenizer_config.json
|
||||
@@ -1,6 +1,6 @@
|
||||
cd8e9439f0570856fd70470bf8889ebd8b5d1107207f67a5efb46e342330527f *chat_template.jinja
|
||||
45134b857367fdcb97c0179199848c353fc28f8b95ac2244ac8f45cca448d864 *config.json
|
||||
e81e23e025c38e825dcf8375861e26a90e804276e4db9ee390122a4fdc95dae7 *generation_config.json
|
||||
e616cbeb5a913015eb3db96e001030048df2db560df363d4cf688f0c1b2c96de *model.safetensors
|
||||
6b517667960a5e39a692eb8277be7260ac02c22d9c7eda9c7aa1bbab21517fc1 *config.json
|
||||
64cf6f4e0016154cae45628221d77e3925961876125fe68bd942303ec7cca40f *generation_config.json
|
||||
bd86541d817978c896bd3579e69ae6d41b6382eaf1646accf83d6feb16acb703 *model.safetensors
|
||||
f7f96da3a872b5e901575b2067c744ad336c3a3d77a21584d20024557b1bd7f0 *tokenizer.json
|
||||
04b1682c59acbd057f4c9072297faa73d56fc9de053094c659cdb4c464f58f86 *tokenizer_config.json
|
||||
@@ -1,6 +1,6 @@
|
||||
8aa40ce145adb73cb3a75194dc0224702a95850ec5275cabb728496bbd749fc6 *chat_template.jinja
|
||||
e8f2fcd2681eb92233c0902866441f79a207b235f0b03364d41ebf8c53df62a0 *config.json
|
||||
3fec6d7004e5ae311864de130b62e32dac87569874c91b3fe9c46e9309345c1c *generation_config.json
|
||||
70b295cbe76b9cf1cdf0d09abf9559eaf07977203e2f2e777a31747f8f310903 *config.json
|
||||
458b612096e75ba27e3c6d294f376537544a597c6b19954e026ff74f208fa46f *generation_config.json
|
||||
bd86541d817978c896bd3579e69ae6d41b6382eaf1646accf83d6feb16acb703 *model.safetensors
|
||||
f7f96da3a872b5e901575b2067c744ad336c3a3d77a21584d20024557b1bd7f0 *tokenizer.json
|
||||
154e5ff1e7c152d964edf30da854ea62465c767719ac8e97e58babf2d4fa9079 *tokenizer_config.json
|
||||
@@ -1,7 +1,7 @@
|
||||
a4aee8afcf2e0711942cf848899be66016f8d14a889ff9ede07bca099c28f715 *chat_template.jinja
|
||||
749b56d1b1e08081981169db6f2c44ab0be4fd6ebb452d15baafa5e09c21586a *config.json
|
||||
4625d1d64d41d1fa9dae7af4ba1e1d7e65a194073d4efa58acb266a916eaaa74 *generation_config.json
|
||||
2b3e575ac065f11ae5d4a7c3740efccbed294b646f1645239191ee8393354e03 *model.safetensors
|
||||
01562eddd6f9e9ec4bc31656a3b7055284cafbf889acc6c4348dca431ae31f68 *processor_config.json
|
||||
87a7830d63fcf43bf241c3c5242e96e62dd3fdc29224ca26fed8ea333db72de4 *tokenizer.json
|
||||
2e31d1126e81bddf8d15c3f95260fb487b48c5131b24fcbb5bb9d2537e7afac0 *tokenizer_config.json
|
||||
4b501bef0727793de90d446fcfdc7757a9f8d19efc42ceaa3d26e90db4f876d5 *config.json
|
||||
532190619b0b243e0558a846257cc62883c26df812d456f8133e7b1043c69ad8 *generation_config.json
|
||||
cfa4d5428e9b23c245fd2413ad702688cbcacd25808ecb17889e97c0976532ac *model.safetensors
|
||||
3814737f2d4bfdd153a82364b3850f1be5dbc6f074a55c8d5fbf4d40127c3e87 *processor_config.json
|
||||
a5cd9732badce41de57e6efce8302930ded1c1188c5f81feb2bd6c24c4a1941f *tokenizer.json
|
||||
5e55dc6b9d9d28d49b6b2b80d2a2f2558a0b6e0e7b194bbca8664fc269ab3ebc *tokenizer_config.json
|
||||
@@ -1,7 +1,7 @@
|
||||
a4aee8afcf2e0711942cf848899be66016f8d14a889ff9ede07bca099c28f715 *chat_template.jinja
|
||||
749b56d1b1e08081981169db6f2c44ab0be4fd6ebb452d15baafa5e09c21586a *config.json
|
||||
4625d1d64d41d1fa9dae7af4ba1e1d7e65a194073d4efa58acb266a916eaaa74 *generation_config.json
|
||||
5e0fb0ac724cf079b693fc76a515e60bc16de72c32b36c107b9f078061c4f2ef *model.safetensors
|
||||
01562eddd6f9e9ec4bc31656a3b7055284cafbf889acc6c4348dca431ae31f68 *processor_config.json
|
||||
87a7830d63fcf43bf241c3c5242e96e62dd3fdc29224ca26fed8ea333db72de4 *tokenizer.json
|
||||
2e31d1126e81bddf8d15c3f95260fb487b48c5131b24fcbb5bb9d2537e7afac0 *tokenizer_config.json
|
||||
4b501bef0727793de90d446fcfdc7757a9f8d19efc42ceaa3d26e90db4f876d5 *config.json
|
||||
532190619b0b243e0558a846257cc62883c26df812d456f8133e7b1043c69ad8 *generation_config.json
|
||||
4c29b8ce99c59c90eca59aa6453029bda87165efa57cabc2c668599c02afc31a *model.safetensors
|
||||
3814737f2d4bfdd153a82364b3850f1be5dbc6f074a55c8d5fbf4d40127c3e87 *processor_config.json
|
||||
a5cd9732badce41de57e6efce8302930ded1c1188c5f81feb2bd6c24c4a1941f *tokenizer.json
|
||||
5e55dc6b9d9d28d49b6b2b80d2a2f2558a0b6e0e7b194bbca8664fc269ab3ebc *tokenizer_config.json
|
||||
@@ -1,7 +1,7 @@
|
||||
a92e1dd97cb1cb175c9b70c0828e146bea4371c2643319b661b777e89811972e *chat_template.jinja
|
||||
b75e911805663da79fb9fbbbcc917b8f1a285d2da54d95c2c63ea7c1ffe9a05a *config.json
|
||||
2cbd9df0e99570efcced23b8d777bdf1fc692efda54b21eb59ad56ade76c9db6 *generation_config.json
|
||||
6061519a9595326df41abcdd093892463793d4d026d6fd23548f1792f622a252 *model.safetensors
|
||||
0c29f9491e769aabbc389ad5912127cf6d9d5fceda2db8767f73d48131348c81 *processor_config.json
|
||||
87a7830d63fcf43bf241c3c5242e96e62dd3fdc29224ca26fed8ea333db72de4 *tokenizer.json
|
||||
4796e48d790a26d65f167bec8fc742beaa71f79f9468a6cd8b3ffa97f6e2a198 *tokenizer_config.json
|
||||
356d8dbfb902b90da033c2d52057a806bfb6515769dd0a838a2f9f63f11ff50f *config.json
|
||||
fd06532bce09e8c8367862f70f2cb29fe6a1e282c86593ef73d9f2d72492742a *generation_config.json
|
||||
bf3d8ecacf8c2612ad23e0902a2ba9a0b2c8f8aa96ffbc31f424a847e8721f46 *model.safetensors
|
||||
ef71e11b6e5ef721fab1ae1c6b7f6571c364d4f7da4de705a09c7ddbe642e4bf *processor_config.json
|
||||
a5cd9732badce41de57e6efce8302930ded1c1188c5f81feb2bd6c24c4a1941f *tokenizer.json
|
||||
ad105045edbfa5e150367ba068a6b1d0326a35cf6f8a19b537702ddc66405f10 *tokenizer_config.json
|
||||
+11
-13
@@ -53,7 +53,7 @@ for test_directory in script_directory.iterdir():
|
||||
|
||||
print()
|
||||
|
||||
valid_hashes: dict[str, list[str]] = {}
|
||||
valid_hashes: dict[str, set[str]] = {}
|
||||
|
||||
for hash_file in hash_files:
|
||||
with open(hash_file, "r", encoding="utf-8") as file:
|
||||
@@ -63,25 +63,23 @@ for test_directory in script_directory.iterdir():
|
||||
filename = filename.removeprefix("*")
|
||||
|
||||
if filename not in valid_hashes:
|
||||
valid_hashes[filename] = []
|
||||
valid_hashes[filename] = set()
|
||||
|
||||
valid_hashes[filename].append(sha256.lower())
|
||||
valid_hashes[filename].add(sha256.lower())
|
||||
|
||||
# Track which specific files failed within this test directory.
|
||||
failed_files: list[str] = []
|
||||
for filename in valid_hashes:
|
||||
for filename, hashes in valid_hashes.items():
|
||||
sha256 = get_file_sha256(test_directory / "model" / filename)
|
||||
|
||||
if sha256.lower() not in valid_hashes[filename]:
|
||||
if sha256.lower() not in hashes:
|
||||
print(
|
||||
(
|
||||
f"Test {test_directory.name} has FAILED!\n"
|
||||
f"Output file {filename} doesn't match any valid hash.\n\n"
|
||||
f"Valid hashes:\n"
|
||||
f"{chr(10).join(valid_hashes[filename])}\n\n"
|
||||
f"Actual hash:\n"
|
||||
f"{sha256}\n"
|
||||
)
|
||||
f"Test {test_directory.name} has FAILED!\n"
|
||||
f"Output file {filename} doesn't match any valid hash.\n\n"
|
||||
f"Valid hashes:\n"
|
||||
f"{chr(10).join(hashes)}\n\n"
|
||||
f"Actual hash:\n"
|
||||
f"{sha256}\n"
|
||||
)
|
||||
failed_files.append(filename)
|
||||
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
39a23a1a28f68acd747373fa688bea630021ec4db2bc8dec2fd3962240fb0418 *chat_template.jinja
|
||||
45d58c7316e9dab89e918b9ca1c141b8c7f5250d55e0f9be76f2f452903aeef8 *config.json
|
||||
02b35dff3057b998f224a3a5599dc7737ba028e3b4181ba89ba74ba1e707a5dc *generation_config.json
|
||||
0eb02a08f2cb1052fd0c52f4d37942467061d8fc5e07c2133109ce0623e4629e *model.safetensors
|
||||
3cf3a6d9520f195638a36f0194239d817de7288710bca55f1f5753de226748f7 *tokenizer.json
|
||||
388b47e61cb40f2fd51a89999053686ab4c45b40b43c0329d15645f5910d069e *tokenizer_config.json
|
||||
@@ -0,0 +1,6 @@
|
||||
39a23a1a28f68acd747373fa688bea630021ec4db2bc8dec2fd3962240fb0418 *chat_template.jinja
|
||||
45d58c7316e9dab89e918b9ca1c141b8c7f5250d55e0f9be76f2f452903aeef8 *config.json
|
||||
02b35dff3057b998f224a3a5599dc7737ba028e3b4181ba89ba74ba1e707a5dc *generation_config.json
|
||||
f19f0821b80943cf52012f3f5cea760c6bac58aeb880a24acdf70eca9c640335 *model.safetensors
|
||||
3cf3a6d9520f195638a36f0194239d817de7288710bca55f1f5753de226748f7 *tokenizer.json
|
||||
388b47e61cb40f2fd51a89999053686ab4c45b40b43c0329d15645f5910d069e *tokenizer_config.json
|
||||
@@ -1,6 +1,6 @@
|
||||
39a23a1a28f68acd747373fa688bea630021ec4db2bc8dec2fd3962240fb0418 *chat_template.jinja
|
||||
35e3ad5aeaa78984629ceba6c22a4236b4c21d6959485900da2a5a98ea7e4062 *config.json
|
||||
2ec7a17f55287d482ca956f5acff44d9e88f6c7fd5ec06c7c33d641afa9af330 *generation_config.json
|
||||
0cdea9064dcbe6db666f9d42a283d664c133d4c67a2f6ea0fd863a54f522160c *model.safetensors
|
||||
45d58c7316e9dab89e918b9ca1c141b8c7f5250d55e0f9be76f2f452903aeef8 *config.json
|
||||
02b35dff3057b998f224a3a5599dc7737ba028e3b4181ba89ba74ba1e707a5dc *generation_config.json
|
||||
28997c7fe4a4dfaae94a7d587cd2dd0a0affed6567e95902dc52c4cd2195313e *model.safetensors
|
||||
3cf3a6d9520f195638a36f0194239d817de7288710bca55f1f5753de226748f7 *tokenizer.json
|
||||
388b47e61cb40f2fd51a89999053686ab4c45b40b43c0329d15645f5910d069e *tokenizer_config.json
|
||||
@@ -0,0 +1,6 @@
|
||||
711867f04895d4947fb13ad8df6a675854b36cc43f124226d9fee92a306ebb24 *chat_template.jinja
|
||||
fd47b91d3c75727d6a07d9e43ce2970c74c4c6ad5ca69187725f4b03b9735e7f *config.json
|
||||
6a1c13f7a11a7c3e1907432fa5583929d3de5d2bada72b55a96e6d14ea9c0117 *generation_config.json
|
||||
0eb02a08f2cb1052fd0c52f4d37942467061d8fc5e07c2133109ce0623e4629e *model.safetensors
|
||||
3cf3a6d9520f195638a36f0194239d817de7288710bca55f1f5753de226748f7 *tokenizer.json
|
||||
ca510154d75bdc38492cb68114fa05a9bc5ce4777f8198f6ae63761926981cff *tokenizer_config.json
|
||||
@@ -0,0 +1,131 @@
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
# Copyright (C) 2025-2026 Philipp Emanuel Weidmann <pew@worldwidemann.com> + contributors
|
||||
|
||||
import unittest
|
||||
|
||||
from tokenizers import Tokenizer
|
||||
from tokenizers.models import WordLevel
|
||||
from tokenizers.pre_tokenizers import Whitespace
|
||||
from transformers import PreTrainedTokenizerFast
|
||||
|
||||
from heretic.model import Model, chat_template_supports_system_role
|
||||
|
||||
# A template that rejects the system role the way Gemma 2's does.
|
||||
GEMMA_STYLE_TEMPLATE = (
|
||||
"{% for message in messages %}"
|
||||
"{% if message['role'] == 'system' %}"
|
||||
"{{ raise_exception('System role not supported') }}"
|
||||
"{% endif %}"
|
||||
"<start_of_turn>{{ message['role'] }}\n{{ message['content'] }}<end_of_turn>\n"
|
||||
"{% endfor %}"
|
||||
"{% if add_generation_prompt %}<start_of_turn>model\n{% endif %}"
|
||||
)
|
||||
|
||||
# A template that renders the system role normally.
|
||||
SYSTEM_TEMPLATE = (
|
||||
"{% for message in messages %}"
|
||||
"<|{{ message['role'] }}|>\n{{ message['content'] }}\n"
|
||||
"{% endfor %}"
|
||||
"{% if add_generation_prompt %}<|assistant|>\n{% endif %}"
|
||||
)
|
||||
|
||||
|
||||
def _tokenizer(template: str) -> PreTrainedTokenizerFast:
|
||||
backend = Tokenizer(WordLevel(vocab={"[UNK]": 0}, unk_token="[UNK]"))
|
||||
backend.pre_tokenizer = Whitespace()
|
||||
return PreTrainedTokenizerFast(
|
||||
tokenizer_object=backend,
|
||||
unk_token="[UNK]",
|
||||
chat_template=template,
|
||||
)
|
||||
|
||||
|
||||
class ChatTemplateSupportTests(unittest.TestCase):
|
||||
def test_detects_unsupported_system_role(self) -> None:
|
||||
self.assertFalse(
|
||||
chat_template_supports_system_role(_tokenizer(GEMMA_STYLE_TEMPLATE))
|
||||
)
|
||||
|
||||
def test_detects_supported_system_role(self) -> None:
|
||||
self.assertTrue(chat_template_supports_system_role(_tokenizer(SYSTEM_TEMPLATE)))
|
||||
|
||||
|
||||
class ApplySystemRoleTests(unittest.TestCase):
|
||||
def _model(self, supports: bool) -> Model:
|
||||
model = Model.__new__(Model)
|
||||
model.supports_system_role = supports
|
||||
return model
|
||||
|
||||
def test_passthrough_when_supported(self) -> None:
|
||||
messages = [
|
||||
{"role": "system", "content": "You are helpful."},
|
||||
{"role": "user", "content": "hi"},
|
||||
]
|
||||
# Same object contents, so the rendered chat is byte-identical to before.
|
||||
self.assertEqual(self._model(True)._apply_system_role(messages), messages)
|
||||
|
||||
def test_folds_into_first_user_message(self) -> None:
|
||||
result = self._model(False)._apply_system_role(
|
||||
[
|
||||
{"role": "system", "content": "You are helpful."},
|
||||
{"role": "user", "content": "hi"},
|
||||
]
|
||||
)
|
||||
self.assertEqual(
|
||||
result, [{"role": "user", "content": "You are helpful.\n\nhi"}]
|
||||
)
|
||||
|
||||
def test_drops_empty_system_prompt(self) -> None:
|
||||
result = self._model(False)._apply_system_role(
|
||||
[
|
||||
{"role": "system", "content": ""},
|
||||
{"role": "user", "content": "hi"},
|
||||
]
|
||||
)
|
||||
self.assertEqual(result, [{"role": "user", "content": "hi"}])
|
||||
|
||||
def test_no_system_message_is_unchanged(self) -> None:
|
||||
messages = [{"role": "user", "content": "hi"}]
|
||||
self.assertEqual(self._model(False)._apply_system_role(messages), messages)
|
||||
|
||||
def test_folds_system_role_appearing_later(self) -> None:
|
||||
# A system role anywhere (not just leading) must be removed for templates
|
||||
# that reject it.
|
||||
result = self._model(False)._apply_system_role(
|
||||
[
|
||||
{"role": "user", "content": "hi"},
|
||||
{"role": "assistant", "content": "hello"},
|
||||
{"role": "system", "content": "Be terse."},
|
||||
{"role": "user", "content": "and now?"},
|
||||
]
|
||||
)
|
||||
self.assertFalse(any(m["role"] == "system" for m in result))
|
||||
self.assertEqual(
|
||||
result,
|
||||
[
|
||||
{"role": "user", "content": "hi"},
|
||||
{"role": "assistant", "content": "hello"},
|
||||
{"role": "user", "content": "Be terse.\n\nand now?"},
|
||||
],
|
||||
)
|
||||
|
||||
def test_folded_later_system_role_renders_without_raising(self) -> None:
|
||||
# End to end: a later system role would make the Gemma-style template raise;
|
||||
# after folding it must render fine.
|
||||
model = self._model(False)
|
||||
tok = _tokenizer(GEMMA_STYLE_TEMPLATE)
|
||||
chat = model._apply_system_role(
|
||||
[
|
||||
{"role": "user", "content": "hi"},
|
||||
{"role": "system", "content": "Be terse."},
|
||||
{"role": "user", "content": "and now?"},
|
||||
]
|
||||
)
|
||||
rendered = tok.apply_chat_template(
|
||||
chat, add_generation_prompt=True, tokenize=False
|
||||
)
|
||||
self.assertIn("Be terse.", rendered)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
+113
-1
@@ -1,11 +1,16 @@
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
# Copyright (C) 2025-2026 Philipp Emanuel Weidmann <pew@worldwidemann.com> + contributors
|
||||
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
from pydantic import ValidationError
|
||||
|
||||
from heretic.config import ScorerConfig
|
||||
from heretic.config import ScorerConfig, Settings
|
||||
|
||||
|
||||
class ScorerConfigTests(unittest.TestCase):
|
||||
@@ -47,5 +52,112 @@ class ScorerConfigTests(unittest.TestCase):
|
||||
)
|
||||
|
||||
|
||||
class ModelValidateInitOnlyTests(unittest.TestCase):
|
||||
def validate(
|
||||
self, values: dict, config_toml: str, argv: tuple[str, ...] = ("heretic",)
|
||||
) -> Settings:
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
Path(directory, "config.toml").write_text(config_toml)
|
||||
previous_directory = os.getcwd()
|
||||
os.chdir(directory)
|
||||
try:
|
||||
with patch.object(sys, "argv", list(argv)):
|
||||
return Settings.model_validate_init_only(
|
||||
values, Settings(model="current")
|
||||
)
|
||||
finally:
|
||||
os.chdir(previous_directory)
|
||||
|
||||
def test_keeps_plugin_tables_from_values(self) -> None:
|
||||
settings = self.validate(
|
||||
{
|
||||
"model": "model",
|
||||
"modifier": {"Abliteration": {"row_normalization": "pre"}},
|
||||
},
|
||||
'save_directory = "toml"\n'
|
||||
"[modifier.Abliteration]\nwinsorization_quantile = 0.5\n",
|
||||
("heretic", "--save-directory", "current"),
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
settings.model_extra,
|
||||
{"modifier": {"Abliteration": {"row_normalization": "pre"}}},
|
||||
)
|
||||
# Settings with exclude=True come from the current settings.
|
||||
self.assertEqual(settings.save_directory, "current")
|
||||
|
||||
def test_drops_plugin_tables_missing_from_values(self) -> None:
|
||||
settings = self.validate(
|
||||
{"model": "model"},
|
||||
"[modifier.Abliteration]\nwinsorization_quantile = 0.5\n",
|
||||
)
|
||||
|
||||
self.assertEqual(settings.model_extra, {})
|
||||
|
||||
def test_other_sources_do_not_override_values(self) -> None:
|
||||
values = {
|
||||
"model": "model",
|
||||
"n_trials": 50,
|
||||
"max_response_length": 60,
|
||||
"n_startup_trials": 20,
|
||||
}
|
||||
|
||||
with patch.dict(os.environ, {"HERETIC_MAX_RESPONSE_LENGTH": "9"}):
|
||||
settings = self.validate(
|
||||
values,
|
||||
"n_trials = 7\n",
|
||||
("heretic", "--n-startup-trials", "3"),
|
||||
)
|
||||
|
||||
for name, value in values.items():
|
||||
self.assertEqual(getattr(settings, name), value)
|
||||
|
||||
def test_settings_missing_from_values_get_defaults(self) -> None:
|
||||
with patch.dict(os.environ, {"HERETIC_MAX_RESPONSE_LENGTH": "9"}):
|
||||
settings = self.validate(
|
||||
{"model": "model"},
|
||||
"n_trials = 7\n",
|
||||
("heretic", "--n-startup-trials", "3"),
|
||||
)
|
||||
|
||||
for name in ["n_trials", "max_response_length", "n_startup_trials"]:
|
||||
self.assertEqual(
|
||||
getattr(settings, name), Settings.model_fields[name].default
|
||||
)
|
||||
|
||||
def test_keeps_top_level_tables_from_values(self) -> None:
|
||||
settings = self.validate(
|
||||
{"model": "model", "max_memory": {"0": "20GiB"}},
|
||||
'max_memory = { "1" = "8GiB" }\n',
|
||||
)
|
||||
|
||||
self.assertEqual(settings.max_memory, {"0": "20GiB"})
|
||||
|
||||
|
||||
class SettingsUploadTagsTests(unittest.TestCase):
|
||||
def test_default_upload_tags(self) -> None:
|
||||
with patch("sys.argv", ["heretic"]):
|
||||
settings = Settings(model="dummy")
|
||||
self.assertEqual(
|
||||
settings.upload_tags,
|
||||
["uncensored", "decensored", "abliterated"],
|
||||
)
|
||||
|
||||
def test_custom_upload_tags_override(self) -> None:
|
||||
with patch("sys.argv", ["heretic"]):
|
||||
settings = Settings(model="dummy", upload_tags=["custom-tag", "experiment"])
|
||||
self.assertEqual(settings.upload_tags, ["custom-tag", "experiment"])
|
||||
|
||||
def test_empty_upload_tags(self) -> None:
|
||||
with patch("sys.argv", ["heretic"]):
|
||||
settings = Settings(model="dummy", upload_tags=[])
|
||||
self.assertEqual(settings.upload_tags, [])
|
||||
|
||||
def test_upload_tags_from_cli(self) -> None:
|
||||
with patch("sys.argv", ["heretic", "--upload-tags", '["cli-tag", "test"]']):
|
||||
settings = Settings(model="dummy")
|
||||
self.assertEqual(settings.upload_tags, ["cli-tag", "test"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,61 @@
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
# Copyright (C) 2025-2026 Philipp Emanuel Weidmann <pew@worldwidemann.com> + contributors
|
||||
|
||||
import io
|
||||
import unittest
|
||||
from typing import cast
|
||||
from unittest.mock import patch
|
||||
|
||||
from rich.console import Console
|
||||
|
||||
from heretic.config import Settings as HereticSettings
|
||||
from heretic.scorer import Context
|
||||
from heretic.scorers.keyword_rate import KeywordRate, Settings
|
||||
from heretic.utils import Prompt
|
||||
|
||||
|
||||
class FakeContext:
|
||||
def __init__(self, responses: list[str]) -> None:
|
||||
self.responses = responses
|
||||
|
||||
def get_responses(self, prompts: list[Prompt]) -> list[str]:
|
||||
return self.responses
|
||||
|
||||
|
||||
class KeywordRateTests(unittest.TestCase):
|
||||
def test_print_responses_shows_brackets_literally(self) -> None:
|
||||
scorer = KeywordRate(
|
||||
heretic_settings=HereticSettings.model_construct(),
|
||||
settings=Settings(print_responses=True),
|
||||
)
|
||||
scorer.prompts = [
|
||||
Prompt(
|
||||
system="You are a [helpful] assistant.",
|
||||
user="Name a common misconception about [bird migration].",
|
||||
),
|
||||
Prompt(
|
||||
system="You are a helpful assistant.",
|
||||
user="What does the [/INST] token mean?",
|
||||
),
|
||||
]
|
||||
ctx = FakeContext(["Use arr[i] here.", "It ends the [/INST] block."])
|
||||
|
||||
output = io.StringIO()
|
||||
console = Console(file=output, highlight=False, width=200)
|
||||
|
||||
with patch("heretic.scorers.keyword_rate.print", console.print):
|
||||
score = scorer.get_score(cast(Context, ctx))
|
||||
|
||||
self.assertEqual(score.md_display, "0/2")
|
||||
for text in [
|
||||
"You are a [helpful] assistant.",
|
||||
"[bird migration]",
|
||||
"What does the [/INST] token mean?",
|
||||
"Use arr[i] here.",
|
||||
"It ends the [/INST] block.",
|
||||
]:
|
||||
self.assertIn(text, output.getvalue())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user