diff --git a/.gitignore b/.gitignore index 9948de3b5..39aea3b5a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /build +/build_test /.vscode *.so *__pycache__/ diff --git a/src/model_config_utils.cc b/src/model_config_utils.cc index 465ec0089..6c2c18f2e 100644 --- a/src/model_config_utils.cc +++ b/src/model_config_utils.cc @@ -1273,7 +1273,8 @@ AutoCompleteBackendFields( } } if (config->backend() == kPythonBackend) { - if (config->default_model_filename().empty()) { + if (config->default_model_filename().empty() && + config->cc_model_filenames().empty()) { config->set_default_model_filename(kPythonFilename); } return Status::Success; diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index f0225b5f8..0917a97fa 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -439,6 +439,56 @@ install( RUNTIME DESTINATION bin ) +# +# Unit test for AutoCompleteBackendFields in model_config_utils +# +add_executable( + model_config_utils_test + model_config_utils_test.cc + ../model_config_utils.cc + ../status.cc + ../filesystem/api.cc + ../model_config_utils.h + ../status.h + ../filesystem/api.h +) + +set_target_properties( + model_config_utils_test + PROPERTIES + SKIP_BUILD_RPATH TRUE + BUILD_WITH_INSTALL_RPATH TRUE + INSTALL_RPATH_USE_LINK_PATH FALSE + INSTALL_RPATH "" +) + +target_include_directories( + model_config_utils_test + PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/.. + ${CMAKE_CURRENT_SOURCE_DIR}/../../include + ${GTEST_INCLUDE_DIRS} + ${Boost_INCLUDE_DIRS} +) + +target_link_libraries( + model_config_utils_test + PRIVATE + triton-common-error # from repo-common + triton-common-model-config # from repo-common + triton-common-json # from repo-common + triton-common-logging # from repo-common + proto-library # from repo-common + GTest::gtest + GTest::gtest_main + protobuf::libprotobuf +) + +install( + TARGETS model_config_utils_test + RUNTIME DESTINATION bin +) + if(${TRITON_ENABLE_METRICS}) # diff --git a/src/test/model_config_utils_test.cc b/src/test/model_config_utils_test.cc new file mode 100644 index 000000000..af3a7b546 --- /dev/null +++ b/src/test/model_config_utils_test.cc @@ -0,0 +1,178 @@ +// Copyright 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// * Neither the name of NVIDIA CORPORATION nor the names of its +// contributors may be used to endorse or promote products derived +// from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY +// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include "model_config_utils.h" + +#include + +#include +#include + +#include "constants.h" +#include "filesystem/api.h" +#include "gtest/gtest.h" + +namespace tc = triton::core; + +namespace { + +// Helper to create a temporary model directory with a version subdirectory +// and an optional file inside it. +class TempModelDir { + public: + TempModelDir() + { + auto status = + tc::MakeTemporaryDirectory(tc::FileSystemType::LOCAL, &root_path_); + EXPECT_TRUE(status.IsOk()) << status.AsString(); + } + + ~TempModelDir() + { + // Best-effort cleanup + std::string cmd = "rm -rf " + root_path_; + (void)system(cmd.c_str()); + } + + // Create version subdir (e.g., "1") and optionally place a file in it. + void AddVersionWithFile( + const std::string& version, const std::string& filename) + { + std::string version_dir = tc::JoinPath({root_path_, version}); + mkdir(version_dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); + if (!filename.empty()) { + std::ofstream f(tc::JoinPath({version_dir, filename})); + f << "# placeholder"; + } + } + + const std::string& Path() const { return root_path_; } + + private: + std::string root_path_; +}; + +class AutoCompleteBackendFieldsTest : public ::testing::Test {}; + +// When backend is "python" and default_model_filename is empty and +// cc_model_filenames is empty, default_model_filename should be set to +// "model.py". +TEST_F(AutoCompleteBackendFieldsTest, PythonBackendSetsDefaultFilename) +{ + TempModelDir dir; + dir.AddVersionWithFile("1", "model.py"); + + inference::ModelConfig config; + config.set_backend("python"); + // default_model_filename and cc_model_filenames are both empty + + auto status = + tc::AutoCompleteBackendFields("test_model", dir.Path(), &config); + ASSERT_TRUE(status.IsOk()) << status.AsString(); + EXPECT_EQ(config.default_model_filename(), "model.py"); +} + +// When backend is "python" and default_model_filename is empty but +// cc_model_filenames is populated, default_model_filename should NOT be +// auto-filled to "model.py". +TEST_F( + AutoCompleteBackendFieldsTest, + PythonBackendSkipsDefaultFilenameWhenCcModelFilenamesSet) +{ + TempModelDir dir; + dir.AddVersionWithFile("1", "custom_model.py"); + + inference::ModelConfig config; + config.set_backend("python"); + (*config.mutable_cc_model_filenames())["gpu"] = "custom_model.py"; + // default_model_filename is empty, cc_model_filenames is set + + auto status = + tc::AutoCompleteBackendFields("test_model", dir.Path(), &config); + ASSERT_TRUE(status.IsOk()) << status.AsString(); + EXPECT_EQ(config.default_model_filename(), "") + << "default_model_filename should remain empty when cc_model_filenames " + "is set"; +} + +// When backend is "python" and default_model_filename is already set, +// it should be preserved regardless of cc_model_filenames. +TEST_F( + AutoCompleteBackendFieldsTest, + PythonBackendPreservesExplicitDefaultFilename) +{ + TempModelDir dir; + dir.AddVersionWithFile("1", "my_model.py"); + + inference::ModelConfig config; + config.set_backend("python"); + config.set_default_model_filename("my_model.py"); + + auto status = + tc::AutoCompleteBackendFields("test_model", dir.Path(), &config); + ASSERT_TRUE(status.IsOk()) << status.AsString(); + EXPECT_EQ(config.default_model_filename(), "my_model.py"); +} + +// When backend is "python" and both default_model_filename and +// cc_model_filenames are set, both should be preserved as-is. +TEST_F( + AutoCompleteBackendFieldsTest, + PythonBackendPreservesBothDefaultAndCcModelFilenames) +{ + TempModelDir dir; + dir.AddVersionWithFile("1", "my_model.py"); + + inference::ModelConfig config; + config.set_backend("python"); + config.set_default_model_filename("my_model.py"); + (*config.mutable_cc_model_filenames())["gpu"] = "gpu_model.py"; + + auto status = + tc::AutoCompleteBackendFields("test_model", dir.Path(), &config); + ASSERT_TRUE(status.IsOk()) << status.AsString(); + EXPECT_EQ(config.default_model_filename(), "my_model.py"); + EXPECT_EQ(config.cc_model_filenames().at("gpu"), "gpu_model.py"); +} + +// When backend is empty but version dir contains model.py, backend should be +// auto-detected as "python" and default_model_filename set to "model.py". +TEST_F(AutoCompleteBackendFieldsTest, AutoDetectPythonBackendFromModelFile) +{ + TempModelDir dir; + dir.AddVersionWithFile("1", "model.py"); + + inference::ModelConfig config; + // backend, platform, default_model_filename all empty + + auto status = + tc::AutoCompleteBackendFields("test_model", dir.Path(), &config); + ASSERT_TRUE(status.IsOk()) << status.AsString(); + EXPECT_EQ(config.backend(), "python"); + EXPECT_EQ(config.default_model_filename(), "model.py"); +} + +} // namespace