Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions notebooks/laya_finetune_typed_decisions_2xT4_kaggle.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@
"from torch.nn.parallel import DistributedDataParallel as DDP\n",
"from safetensors.torch import load_file, save_file\n",
"from transformers import AutoTokenizer\n",
"from laya.common import build_model, proper_reward, QTYPES\n",
"from laya.common import build_model, proper_reward, QTYPES, TEMP_MIN, TEMP_MAX\n",
"\n",
"def collate_train_batch(items, pad_id):\n",
" n, L = len(items), max(len(it[\"ids\"]) for it in items)\n",
Expand Down Expand Up @@ -215,7 +215,7 @@
" loss.backward()\n",
" return loss\n",
" opt.step(closure)\n",
" return float(torch.clamp(log_t.exp(), 0.1, 10.0).item())\n",
" return float(torch.clamp(log_t.exp(), TEMP_MIN, TEMP_MAX).item())\n",
"\n",
"def main():\n",
" dist.init_process_group(\"nccl\")\n",
Expand Down
21 changes: 20 additions & 1 deletion tests/test_calibration_persistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from transformers import BertConfig, BertModel, PreTrainedTokenizerFast # noqa: E402

from laya import load # noqa: E402
from laya.common import DecisionModel, QTYPES # noqa: E402
from laya.common import DecisionModel, QTYPES, TEMP_MIN, TEMP_MAX # noqa: E402


def export_notebook_config(cfg, fitted_temps, output_dir):
Expand Down Expand Up @@ -164,6 +164,25 @@ def test_missing_temperatures_keep_unit_defaults(self):
self.write_config()
self.assert_inference_temperatures({(t, 2): 1.0 for t in QTYPES})

def test_notebook_fit_one_temp_clamps_to_common_bounds(self):
notebook = Path(__file__).resolve().parents[1] / "notebooks" / (
"laya_finetune_typed_decisions_2xT4_kaggle.ipynb"
)
cells = json.loads(notebook.read_text(encoding="utf-8"))["cells"]
script, = ["".join(c["source"]) for c in cells
if "".join(c["source"]).startswith("%%writefile ")]
start = script.index("def fit_one_temp(sel):")
end = script.index("\ndef main():", start)
fn_code = ast.parse(textwrap.dedent(script[start:end]))
scope = {"torch": torch, "TEMP_MIN": TEMP_MIN, "TEMP_MAX": TEMP_MAX}
exec(compile(fn_code, str(notebook), "exec"), scope)
fit_one_temp = scope["fit_one_temp"]

high_sel = [([10.0, 0.0], [0.5, 0.5]) for _ in range(20)]
t_high = fit_one_temp(high_sel)
self.assertLessEqual(t_high, TEMP_MAX)
self.assertGreaterEqual(t_high, TEMP_MIN)


if __name__ == "__main__":
torch.set_num_threads(1)
Expand Down