Restructure internal initialization.

PiperOrigin-RevId: 769507096
This commit is contained in:
Daniel Keysers 2025-06-10 01:24:52 -07:00 committed by Copybara-Service
parent 824a95793c
commit d7b23d532a
10 changed files with 19 additions and 8 deletions

View File

@ -501,7 +501,6 @@ cc_library(
":threading", ":threading",
":threading_context", ":threading_context",
":weights", ":weights",
# Placeholder for internal dep, do not remove.,
"//io:blob_store", "//io:blob_store",
"//io", "//io",
"//paligemma:image", "//paligemma:image",
@ -588,6 +587,7 @@ cc_test(
":configs", ":configs",
":gemma_lib", ":gemma_lib",
"@googletest//:gtest_main", # buildcleaner: keep "@googletest//:gtest_main", # buildcleaner: keep
"//io",
"@highway//:hwy", "@highway//:hwy",
"@highway//:hwy_test_util", "@highway//:hwy_test_util",
], ],

View File

@ -22,6 +22,7 @@
#include "evals/benchmark_helper.h" #include "evals/benchmark_helper.h"
#include "gemma/configs.h" #include "gemma/configs.h"
#include "io/io.h"
#include "hwy/base.h" #include "hwy/base.h"
#include "hwy/tests/hwy_gtest.h" #include "hwy/tests/hwy_gtest.h"
@ -175,6 +176,7 @@ TEST_F(GemmaTest, CrossEntropySmall) {
int main(int argc, char** argv) { int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv); testing::InitGoogleTest(&argc, argv);
gcpp::InternalInit();
gcpp::GemmaTest::InitEnv(argc, argv); gcpp::GemmaTest::InitEnv(argc, argv);
int ret = RUN_ALL_TESTS(); int ret = RUN_ALL_TESTS();
gcpp::GemmaTest::DeleteEnv(); gcpp::GemmaTest::DeleteEnv();

View File

@ -43,7 +43,6 @@
#include <vector> #include <vector>
// Placeholder for internal header, do not modify.
#include "gemma/configs.h" #include "gemma/configs.h"
#include "gemma/model_store.h" #include "gemma/model_store.h"
#include "gemma/tokenizer.h" #include "gemma/tokenizer.h"
@ -636,11 +635,7 @@ HWY_EXPORT(GenerateSingleT);
HWY_EXPORT(GenerateBatchT); HWY_EXPORT(GenerateBatchT);
HWY_EXPORT(GenerateImageTokensT); HWY_EXPORT(GenerateImageTokensT);
// Internal init must run before I/O. This helper function takes care of that,
// plus calling `SetArgs`.
MatMulEnv MakeMatMulEnv(const ThreadingArgs& threading_args) { MatMulEnv MakeMatMulEnv(const ThreadingArgs& threading_args) {
// Placeholder for internal init, do not modify.
ThreadingContext::SetArgs(threading_args); ThreadingContext::SetArgs(threading_args);
return MatMulEnv(ThreadingContext::Get()); return MatMulEnv(ThreadingContext::Get());
} }

View File

@ -97,6 +97,7 @@ struct TimingInfo {
size_t tokens_generated = 0; size_t tokens_generated = 0;
}; };
// Returns the `MatMulEnv` after calling `SetArgs`.
MatMulEnv MakeMatMulEnv(const ThreadingArgs& threading_args); MatMulEnv MakeMatMulEnv(const ThreadingArgs& threading_args);
using KVCaches = hwy::Span<KVCache>; using KVCaches = hwy::Span<KVCache>;

View File

@ -294,6 +294,7 @@ void Run(const LoaderArgs& loader, const ThreadingArgs& threading,
} // namespace gcpp } // namespace gcpp
int main(int argc, char** argv) { int main(int argc, char** argv) {
gcpp::InternalInit();
{ {
PROFILER_ZONE("Startup.misc"); PROFILER_ZONE("Startup.misc");

View File

@ -40,6 +40,7 @@ cc_library(
"//conditions:default": [], "//conditions:default": [],
}), }),
deps = [ deps = [
# Placeholder for internal dep, do not remove.,
"//:allocator", "//:allocator",
"@highway//:hwy", "@highway//:hwy",
] + FILE_DEPS, ] + FILE_DEPS,

View File

@ -72,6 +72,7 @@
#include <sys/stat.h> // O_RDONLY #include <sys/stat.h> // O_RDONLY
#include <unistd.h> // read, write, close #include <unistd.h> // read, write, close
// Placeholder for internal header, do not modify.
#include "util/allocator.h" #include "util/allocator.h"
namespace gcpp { namespace gcpp {
@ -218,6 +219,9 @@ bool IOBatch::Add(void* mem, size_t bytes) {
return true; return true;
} }
void InternalInit() {
}
uint64_t IOBatch::Read(const File& file) const { uint64_t IOBatch::Read(const File& file) const {
#if GEMMA_IO_PREADV #if GEMMA_IO_PREADV
HWY_ASSERT(!spans_.empty()); HWY_ASSERT(!spans_.empty());

View File

@ -144,6 +144,10 @@ struct Path {
// Aborts on error. // Aborts on error.
std::string ReadFileToString(const Path& path); std::string ReadFileToString(const Path& path);
// No-op in open-source. Must be called at the beginning of a binary, before
// any I/O or flag usage.
void InternalInit();
} // namespace gcpp } // namespace gcpp
#endif // THIRD_PARTY_GEMMA_CPP_COMPRESSION_IO_H_ #endif // THIRD_PARTY_GEMMA_CPP_COMPRESSION_IO_H_

View File

@ -45,6 +45,7 @@ cc_test(
"//:configs", "//:configs",
"//:gemma_lib", "//:gemma_lib",
"//compression:types", "//compression:types",
"//io",
"@highway//:hwy", "@highway//:hwy",
"@highway//:hwy_test_util", "@highway//:hwy_test_util",
], ],

View File

@ -22,6 +22,7 @@
#include "evals/benchmark_helper.h" #include "evals/benchmark_helper.h"
#include "gemma/configs.h" #include "gemma/configs.h"
#include "gemma/gemma.h" #include "gemma/gemma.h"
#include "io/io.h"
#include "util/allocator.h" #include "util/allocator.h"
#include "hwy/base.h" #include "hwy/base.h"
#include "hwy/tests/hwy_gtest.h" #include "hwy/tests/hwy_gtest.h"
@ -120,10 +121,11 @@ TEST_F(PaliGemmaTest, QueryObjects) {
} // namespace gcpp } // namespace gcpp
int main(int argc, char** argv) { int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
gcpp::InternalInit();
gcpp::GemmaEnv env(argc, argv); gcpp::GemmaEnv env(argc, argv);
gcpp::s_env = &env; gcpp::s_env = &env;
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS(); return RUN_ALL_TESTS();
} }