A community-maintained Python framework for creating mathematical animations.
Bound the encoder frame queue in serial mode (#5049)
pizzaburgare23ae68f4dd58
diff --git a/docs/source/guides/deep_dive.rst b/docs/source/guides/deep_dive.rstindex e315d5b0d..ca2bb4e00 100644--- a/docs/source/guides/deep_dive.rst+++ b/docs/source/guides/deep_dive.rst@@ -843,7 +843,8 @@ By default, Manim finishes encoding each partial movie file before rendering thenext animation. If ``max_inflight_encoders`` is set to a value greater than 1,encoding can instead overlap with rendering. The setting bounds the number ofpartial movie encoders active at the same time, while ``encoder_queue_size``-bounds the number of pending frame buffers held by each parallel encoder.+bounds the number of pending frame buffers held by each encoder (the renderer+waits when the queue is full, so memory use does not grow with scene length).First, it literally *begins* all of the animations by calling theirsetup methods (:meth:`.Animation._setup_scene`, :meth:`.Animation.begin`).diff --git a/manim/_config/default.cfg b/manim/_config/default.cfgindex c573c491a..5b9f7805e 100644--- a/manim/_config/default.cfg+++ b/manim/_config/default.cfg@@ -117,8 +117,9 @@ use_projection_stroke_shaders = False# 4 is a good value on typical hardware.max_inflight_encoders = 1-# Maximum number of pending frame buffers held by each encoder when parallel-# encoding is enabled. Ignored when max_inflight_encoders is 1.+# Maximum number of pending frame buffers held by each encoder. Rendering+# blocks when the queue is full, which bounds the memory used by frames+# waiting to be encoded.encoder_queue_size = 8# These now override the --quality option.diff --git a/manim/_config/utils.py b/manim/_config/utils.pyindex 9176e41b1..96adfe118 100644--- a/manim/_config/utils.py+++ b/manim/_config/utils.py@@ -1258,9 +1258,9 @@ class ManimConfig(MutableMapping):@propertydef encoder_queue_size(self) -> int:- """Maximum number of pending frame buffers held by each encoder when- parallel encoding is enabled. Ignored when ``max_inflight_encoders`` is- 1 (--encoder-queue-size).+ """Maximum number of pending frame buffers held by each encoder; this+ bounds the memory used by frames waiting to be encoded+ (--encoder-queue-size)."""return self._d["encoder_queue_size"]diff --git a/manim/cli/render/render_options.py b/manim/cli/render/render_options.pyindex a48d5038b..c9a5ba4a3 100644--- a/manim/cli/render/render_options.py+++ b/manim/cli/render/render_options.py@@ -244,9 +244,8 @@ render_options = option_group("--encoder-queue-size",type=IntRange(min=1),default=None,- help="Maximum number of pending frame buffers held by each encoder when "- "parallel encoding is enabled. Ignored when --max-inflight-encoders is "- "1 (the default). Defaults to 8.",+ help="Maximum number of pending frame buffers held by each encoder "+ "(bounds the memory used by frames waiting to be encoded). Defaults to 8.",),option("--renderer",diff --git a/manim/scene/scene_file_writer.py b/manim/scene/scene_file_writer.pyindex 6f08ca291..f540bd6a7 100644--- a/manim/scene/scene_file_writer.py+++ b/manim/scene/scene_file_writer.py@@ -91,10 +91,10 @@ class _PartialMovieEncodeJob:self.path = encoder.targetself.animation_index = animation_indexself.encoder = encoder- # A size of 0 preserves the unbounded queue used by serial encoding.- # Parallel encoding uses a bounded queue; at the default capacity, eight- # 1080p RGBA frames occupy about 66 MB per job. The worker drains through- # the sentinel after an exception, so a bounded queue cannot deadlock.+ # Bound the queue so rendering cannot run arbitrarily far ahead of the+ # encoder; at the default capacity, eight 1080p RGBA frames occupy about+ # 66 MB per job. The worker drains through the sentinel after an+ # exception, so a bounded queue cannot deadlock.self.queue: Queue[tuple[int, RGBAPixelArray | None]] = Queue(maxsize=frame_queue_size,)@@ -592,15 +592,10 @@ class SceneFileWriter:if path_key in self._inflight_by_path:self._join_job_and_drain_on_failure(self._inflight_by_path[path_key])segment_encoder = self._create_segment_encoder(file_path)- frame_queue_size = (- 0- if self.settings.max_inflight_encoders == 1- else self.settings.encoder_queue_size- )self._current_encode_job = _PartialMovieEncodeJob(animation_index=animation_index,encoder=segment_encoder,- frame_queue_size=frame_queue_size,+ frame_queue_size=self.settings.encoder_queue_size,)def _join_job(self, job: _PartialMovieEncodeJob) -> None:diff --git a/tests/test_scene_rendering/test_parallel_encoding.py b/tests/test_scene_rendering/test_parallel_encoding.pyindex a3eb47ce2..a2e143266 100644--- a/tests/test_scene_rendering/test_parallel_encoding.py+++ b/tests/test_scene_rendering/test_parallel_encoding.py@@ -356,7 +356,7 @@ def test_write_frame_fails_fast_after_encoder_failure(config, tmp_path):@pytest.mark.parametrize(("max_inflight_encoders", "encoder_queue_size", "expected_queue_size"),- [(1, 8, 0), (1, 3, 0), (2, 8, 8), (2, 3, 3)],+ [(1, 8, 8), (1, 3, 3), (2, 8, 8), (2, 3, 3)],)def test_frame_queue_configuration(config,@@ -382,6 +382,43 @@ def test_frame_queue_configuration(assert not _alive_encoder_threads()+def test_serial_encoding_blocks_when_frame_queue_is_full(config, tmp_path):+ config.max_inflight_encoders = 1+ config.encoder_queue_size = 2+ writer = _make_writer("SerialBackpressureScene")+ encoder = _fake_segment_encoder(tmp_path, "serial_backpressure")+ release_encoder = threading.Event()+ encoder.write_frame.side_effect = lambda *args, **kwargs: release_encoder.wait()+ writer._create_segment_encoder = lambda target: encoder+ writer.open_partial_movie_stream(+ animation_index=0,+ file_path=tmp_path / "partial.mp4",+ )+ job = writer._current_encode_job++ def write_frames():+ for _ in range(10):+ writer.write_frame(_frame())++ producer = threading.Thread(target=write_frames, daemon=True)+ producer.start()+ try:+ for _ in range(500):+ if job.queue.full():+ break+ time.sleep(0.01)+ assert job.queue.full(), "Frame queue never reached its bound"+ assert producer.is_alive(), "Producer did not block on the full queue"+ finally:+ release_encoder.set()+ producer.join(timeout=5)++ writer.close_partial_movie_stream()+ writer.join_all_encode_jobs()+ assert encoder.write_frame.call_count == 10+ assert not _alive_encoder_threads()++def test_pool_settings_are_not_read_from_mutated_config(config, tmp_path):config.max_inflight_encoders = 2config.encoder_queue_size = 3