forked from LeelaChessZero/lczero-training
-
Notifications
You must be signed in to change notification settings - Fork 2
Polish debug chunk source generator #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mooskagh
wants to merge
1
commit into
master
Choose a base branch
from
codex/2025-10-06-20-24-14
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| #include "loader/stages/debug_chunk_source_generator.h" | ||
|
|
||
| #include <algorithm> | ||
| #include <numeric> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| #include "absl/algorithm/container.h" | ||
| #include "absl/cleanup/cleanup.h" | ||
| #include "absl/log/log.h" | ||
| #include "absl/random/random.h" | ||
| #include "absl/random/seed_sequences.h" | ||
| #include "absl/time/clock.h" | ||
| #include "absl/time/time.h" | ||
| #include "loader/data_loader_metrics.h" | ||
|
|
||
| namespace lczero { | ||
| namespace training { | ||
|
|
||
| namespace { | ||
| constexpr uint64_t kDefaultQueueCapacity = 16; | ||
| constexpr uint64_t kInitialShuffleSeed = 0xC0FFEEull; | ||
| constexpr absl::Duration kStopPollInterval = absl::Milliseconds(10); | ||
| } // namespace | ||
|
|
||
| DebugChunkSourceGenerator::DebugChunkSourceGenerator( | ||
| const DebugChunkSourceGeneratorConfig& config, | ||
| const Stage::StageList& existing_stages) | ||
| : config_(config), | ||
| output_queue_(static_cast<size_t>(std::max<uint64_t>( | ||
| config.initial_chunk_sources(), kDefaultQueueCapacity))), | ||
| mean_chunk_count_(std::max(1.0, config.mean_chunks_per_chunk_source())) { | ||
| (void)existing_stages; | ||
| if (config.mean_chunks_per_chunk_source() <= 0.0) { | ||
| LOG(WARNING) << "DebugChunkSourceGenerator mean chunk count not positive." | ||
| << " Using 1."; | ||
| } | ||
| } | ||
|
|
||
| DebugChunkSourceGenerator::~DebugChunkSourceGenerator() { Stop(); } | ||
|
|
||
| void DebugChunkSourceGenerator::Start() { | ||
| if (worker_.joinable()) { | ||
| return; | ||
| } | ||
| worker_ = std::jthread( | ||
| [this](std::stop_token stop_token) { Run(std::move(stop_token)); }); | ||
| } | ||
|
|
||
| void DebugChunkSourceGenerator::Stop() { | ||
| if (!worker_.joinable()) return; | ||
| worker_.request_stop(); | ||
| worker_.join(); | ||
| } | ||
|
|
||
| Queue<DebugChunkSourceGenerator::OutputType>* | ||
| DebugChunkSourceGenerator::output() { | ||
| return &output_queue_; | ||
| } | ||
|
|
||
| QueueBase* DebugChunkSourceGenerator::GetOutput(std::string_view name) { | ||
| (void)name; | ||
| return &output_queue_; | ||
| } | ||
|
|
||
| StageMetricProto DebugChunkSourceGenerator::FlushMetrics() { | ||
| StageMetricProto metric; | ||
| metric.set_stage_type("debug_chunk_source_generator"); | ||
| *metric.add_queue_metrics() = MetricsFromQueue("output", output_queue_); | ||
| auto* count_metric = metric.add_count_metrics(); | ||
| count_metric->set_name("chunk_sources_generated"); | ||
| count_metric->set_count(generated_sources_.load(std::memory_order_relaxed)); | ||
| return metric; | ||
| } | ||
|
|
||
| void DebugChunkSourceGenerator::Run(std::stop_token stop_token) { | ||
| try { | ||
| auto producer = output_queue_.CreateProducer(); | ||
| absl::Cleanup close_queue = [&] { output_queue_.Close(); }; | ||
|
|
||
| std::vector<uint64_t> initial_ids(config_.initial_chunk_sources()); | ||
| std::iota(initial_ids.begin(), initial_ids.end(), 0); | ||
| if (!initial_ids.empty()) { | ||
| absl::SeedSeq seed({static_cast<uint32_t>(kInitialShuffleSeed), | ||
| static_cast<uint32_t>(kInitialShuffleSeed >> 32)}); | ||
| absl::BitGen bitgen(seed); | ||
| absl::c_shuffle(initial_ids, bitgen); | ||
| } | ||
|
|
||
| auto emit_source = [&](uint64_t id) { | ||
| auto source = std::make_unique<DebugChunkSource>(id, mean_chunk_count_); | ||
| producer.Put({.source = std::move(source), | ||
| .message_type = FilePathProvider::MessageType::kFile}); | ||
| generated_sources_.fetch_add(1, std::memory_order_relaxed); | ||
| }; | ||
|
|
||
| for (uint64_t id : initial_ids) { | ||
| if (stop_token.stop_requested()) return; | ||
| emit_source(id); | ||
| } | ||
|
|
||
| if (stop_token.stop_requested()) return; | ||
|
|
||
| producer.Put( | ||
| {.source = nullptr, | ||
| .message_type = FilePathProvider::MessageType::kInitialScanComplete}); | ||
|
|
||
| const double per_minute = config_.chunk_sources_per_minute(); | ||
| if (per_minute <= 0.0) return; | ||
|
|
||
| const absl::Duration cadence = absl::Seconds(60.0 / per_minute); | ||
| uint64_t next_id = config_.initial_chunk_sources(); | ||
| absl::Time next_deadline = absl::Now(); | ||
|
|
||
| while (!stop_token.stop_requested()) { | ||
| emit_source(next_id++); | ||
| next_deadline += cadence; | ||
| while (!stop_token.stop_requested()) { | ||
| const absl::Duration wait = next_deadline - absl::Now(); | ||
| if (wait <= absl::ZeroDuration()) break; | ||
| const absl::Duration sleep = | ||
| wait < kStopPollInterval ? wait : kStopPollInterval; | ||
| absl::SleepFor(sleep); | ||
| } | ||
| } | ||
| } catch (const QueueClosedException&) { | ||
| LOG(INFO) << "DebugChunkSourceGenerator stopping due to closed queue."; | ||
| } | ||
| } | ||
|
|
||
| } // namespace training | ||
| } // namespace lczero | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| #pragma once | ||
|
|
||
| #include <atomic> | ||
| #include <cstdint> | ||
| #include <memory> | ||
| #include <stop_token> | ||
| #include <string_view> | ||
| #include <thread> | ||
|
|
||
| #include "loader/chunk_source/chunk_source.h" | ||
| #include "loader/chunk_source/debug_chunk_source.h" | ||
| #include "loader/stages/chunk_source_loader.h" | ||
| #include "loader/stages/stage.h" | ||
| #include "proto/data_loader_config.pb.h" | ||
| #include "proto/training_metrics.pb.h" | ||
| #include "utils/queue.h" | ||
|
|
||
| namespace lczero { | ||
| namespace training { | ||
|
|
||
| // DebugChunkSourceGenerator emits deterministic DebugChunkSource instances. | ||
| // It is intended for loader bring-up and testing without filesystem input. | ||
| class DebugChunkSourceGenerator : public Stage { | ||
| public: | ||
| using OutputType = ChunkSourceWithPhase; | ||
|
|
||
| explicit DebugChunkSourceGenerator( | ||
| const DebugChunkSourceGeneratorConfig& config, | ||
| const Stage::StageList& existing_stages = {}); | ||
| ~DebugChunkSourceGenerator() override; | ||
|
|
||
| void Start() override; | ||
| void Stop() override; | ||
|
|
||
| StageMetricProto FlushMetrics() override; | ||
|
|
||
| QueueBase* GetOutput(std::string_view name = "") override; | ||
| Queue<OutputType>* output(); | ||
|
|
||
| private: | ||
| void Run(std::stop_token stop_token); | ||
|
|
||
| const DebugChunkSourceGeneratorConfig config_; | ||
| Queue<OutputType> output_queue_; | ||
| std::jthread worker_; | ||
| std::atomic<uint64_t> generated_sources_{0}; | ||
| const double mean_chunk_count_; | ||
| }; | ||
|
|
||
| } // namespace training | ||
| } // namespace lczero |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| #include "loader/stages/debug_chunk_source_generator.h" | ||
|
|
||
| #include <gtest/gtest.h> | ||
|
|
||
| #include <algorithm> | ||
| #include <cstdint> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| namespace lczero { | ||
| namespace training { | ||
|
|
||
| TEST(DebugChunkSourceGeneratorTest, EmitsInitialSourcesAndMarker) { | ||
| DebugChunkSourceGeneratorConfig config; | ||
| config.set_mean_chunks_per_chunk_source(10.0); | ||
| config.set_initial_chunk_sources(3); | ||
| config.set_chunk_sources_per_minute(6000.0); | ||
|
|
||
| DebugChunkSourceGenerator generator(config); | ||
| generator.Start(); | ||
|
|
||
| auto* queue = generator.output(); | ||
| std::vector<uint64_t> initial_ids; | ||
| for (int i = 0; i < 3; ++i) { | ||
| auto item = queue->Get(); | ||
| ASSERT_NE(item.source, nullptr); | ||
| EXPECT_EQ(item.message_type, FilePathProvider::MessageType::kFile); | ||
| uint64_t id = 0; | ||
| ASSERT_NO_THROW(id = std::stoull(item.source->GetChunkSortKey())); | ||
| initial_ids.push_back(id); | ||
| } | ||
| std::sort(initial_ids.begin(), initial_ids.end()); | ||
| EXPECT_EQ(initial_ids, (std::vector<uint64_t>{0, 1, 2})); | ||
|
|
||
| auto marker = queue->Get(); | ||
| EXPECT_EQ(marker.source, nullptr); | ||
| EXPECT_EQ(marker.message_type, | ||
| FilePathProvider::MessageType::kInitialScanComplete); | ||
|
|
||
| auto next = queue->Get(); | ||
| ASSERT_NE(next.source, nullptr); | ||
| EXPECT_EQ(next.message_type, FilePathProvider::MessageType::kFile); | ||
| uint64_t next_id = 0; | ||
| ASSERT_NO_THROW(next_id = std::stoull(next.source->GetChunkSortKey())); | ||
| EXPECT_EQ(next_id, 3); | ||
|
|
||
| generator.Stop(); | ||
| } | ||
|
|
||
| } // namespace training | ||
| } // namespace lczero |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stop()only callsrequest_stop()and joins the thread, while the worker loop writes via blockingproducer.Put(line 92) and never closes the queue. If a downstream stage has stopped and the queue fills, the worker blocks insidePutand can’t observe the stop token, causingStop()to block indefinitely and preventing the loader from shutting down. Closing the queue or the producer before joining would let the worker unblock even when no consumer is draining.Useful? React with 👍 / 👎.