Skip to content
Draft
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
26 changes: 26 additions & 0 deletions python/fasttext_module/fasttext/tests/test_autotune_errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# SPDX-FileContributor: Arthit Suriyawongkul
# SPDX-FileCopyrightText: 2026-present, fasttext-community
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: MIT

"""Autotune errors must raise, not terminate the process."""

import pytest

from .helpers import build_supervised_model, get_random_data


def test_autotune_error_raises(tmp_path):
data = get_random_data(3000, max_vocab_size=600)
valid = tmp_path / "valid.txt"
valid.write_text("".join(f"__label__{line}\n" for line in data))
kwargs = {
# thread=12: thread <= 10 leaves the input matrix partly uninitialized.
"thread": 12,
"verbose": 0,
"autotuneValidationFile": str(valid),
"autotuneMetric": "f1:__label__missing", # fails after the first trial
"autotuneDuration": 60, # long enough for slow runners to reach it
}
with pytest.raises(RuntimeError, match="Unknown autotune metric label"):
build_supervised_model(data, kwargs)
9 changes: 9 additions & 0 deletions src/autotune.cc
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,15 @@ Autotune::Autotune(const std::shared_ptr<FastText>& fastText)
strategy_(),
timer_() {}

Autotune::~Autotune() noexcept {
// An exception leaving train() skips its timer join, and destroying a
// joinable std::thread calls std::terminate().
if (timer_.joinable()) {
continueTraining_ = false;
timer_.join();
}
}

void Autotune::printInfo(double maxDuration) {
double progress = elapsed_ * 100 / maxDuration;
progress = std::min(progress, 100.0);
Expand Down
2 changes: 1 addition & 1 deletion src/autotune.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class Autotune {
Autotune(Autotune&&) = delete;
Autotune& operator=(const Autotune&) = delete;
Autotune& operator=(Autotune&&) = delete;
~Autotune() noexcept = default;
~Autotune() noexcept;

void train(const Args& args);
};
Expand Down
Loading