From 93add35f5e5edfab209d98da9a13a839a8da6ef1 Mon Sep 17 00:00:00 2001 From: wunianze666-netizen Date: Wed, 2 Sep 2026 01:03:48 +0800 Subject: [PATCH 1/2] fix: route library messages through loggers --- src/diffusers/models/autoencoders/vae.py | 7 ++- .../longcat_image/pipeline_longcat_image.py | 4 +- .../pipeline_longcat_image_edit.py | 4 +- .../pipelines/wan/pipeline_wan_animate.py | 2 +- tests/models/autoencoders/test_models_vq.py | 17 ++++++ .../longcat_image/test_prepare_pos_ids.py | 54 +++++++++++++++++++ tests/pipelines/wan/test_wan_animate.py | 30 +++++++++++ 7 files changed, 111 insertions(+), 7 deletions(-) create mode 100644 tests/pipelines/longcat_image/test_prepare_pos_ids.py diff --git a/src/diffusers/models/autoencoders/vae.py b/src/diffusers/models/autoencoders/vae.py index a65bca418175..4461d1540203 100644 --- a/src/diffusers/models/autoencoders/vae.py +++ b/src/diffusers/models/autoencoders/vae.py @@ -17,7 +17,7 @@ import torch import torch.nn as nn -from ...utils import BaseOutput +from ...utils import BaseOutput, logging from ...utils.torch_utils import randn_tensor from ..activations import get_activation from ..attention_processor import SpatialNorm @@ -29,6 +29,9 @@ ) +logger = logging.get_logger(__name__) + + @dataclass class EncoderOutput(BaseOutput): r""" @@ -599,7 +602,7 @@ def __init__( if self.unknown_index == "extra": self.unknown_index = self.re_embed self.re_embed = self.re_embed + 1 - print( + logger.info( f"Remapping {self.n_e} indices to {self.re_embed} indices. " f"Using {self.unknown_index} for unknown indices." ) diff --git a/src/diffusers/pipelines/longcat_image/pipeline_longcat_image.py b/src/diffusers/pipelines/longcat_image/pipeline_longcat_image.py index 4eaa858e41c1..41ca3eb54f83 100644 --- a/src/diffusers/pipelines/longcat_image/pipeline_longcat_image.py +++ b/src/diffusers/pipelines/longcat_image/pipeline_longcat_image.py @@ -110,7 +110,7 @@ def prepare_pos_ids(modality_id=0, type="text", start=(0, 0), num_token=None, he if type == "text": assert num_token if height or width: - print('Warning: The parameters of height and width will be ignored in "text" type.') + logger.warning('The parameters of height and width will be ignored in "text" type.') pos_ids = torch.zeros(num_token, 3) pos_ids[..., 0] = modality_id pos_ids[..., 1] = torch.arange(num_token) + start[0] @@ -118,7 +118,7 @@ def prepare_pos_ids(modality_id=0, type="text", start=(0, 0), num_token=None, he elif type == "image": assert height and width if num_token: - print('Warning: The parameter of num_token will be ignored in "image" type.') + logger.warning('The parameter of num_token will be ignored in "image" type.') pos_ids = torch.zeros(height, width, 3) pos_ids[..., 0] = modality_id pos_ids[..., 1] = pos_ids[..., 1] + torch.arange(height)[:, None] + start[0] diff --git a/src/diffusers/pipelines/longcat_image/pipeline_longcat_image_edit.py b/src/diffusers/pipelines/longcat_image/pipeline_longcat_image_edit.py index 119de3946fbc..9f35bb685d9f 100644 --- a/src/diffusers/pipelines/longcat_image/pipeline_longcat_image_edit.py +++ b/src/diffusers/pipelines/longcat_image/pipeline_longcat_image_edit.py @@ -108,7 +108,7 @@ def prepare_pos_ids(modality_id=0, type="text", start=(0, 0), num_token=None, he if type == "text": assert num_token if height or width: - print('Warning: The parameters of height and width will be ignored in "text" type.') + logger.warning('The parameters of height and width will be ignored in "text" type.') pos_ids = torch.zeros(num_token, 3) pos_ids[..., 0] = modality_id pos_ids[..., 1] = torch.arange(num_token) + start[0] @@ -116,7 +116,7 @@ def prepare_pos_ids(modality_id=0, type="text", start=(0, 0), num_token=None, he elif type == "image": assert height and width if num_token: - print('Warning: The parameter of num_token will be ignored in "image" type.') + logger.warning('The parameter of num_token will be ignored in "image" type.') pos_ids = torch.zeros(height, width, 3) pos_ids[..., 0] = modality_id pos_ids[..., 1] = pos_ids[..., 1] + torch.arange(height)[:, None] + start[0] diff --git a/src/diffusers/pipelines/wan/pipeline_wan_animate.py b/src/diffusers/pipelines/wan/pipeline_wan_animate.py index 5806032c0142..a923219a7550 100644 --- a/src/diffusers/pipelines/wan/pipeline_wan_animate.py +++ b/src/diffusers/pipelines/wan/pipeline_wan_animate.py @@ -569,7 +569,7 @@ def prepare_prev_segment_cond_latents( latent_height = height // self.vae_scale_factor_spatial latent_width = width // self.vae_scale_factor_spatial if segment_height != height or segment_width != width: - print( + logger.warning( f"Interpolating prev segment cond video from ({segment_width}, {segment_height}) to ({width}, {height})" ) # Perform a 4D (spatial) rather than a 5D (spatiotemporal) reshape, following the original code diff --git a/tests/models/autoencoders/test_models_vq.py b/tests/models/autoencoders/test_models_vq.py index 5b1b97d19a6c..d1e49a4fe8fa 100644 --- a/tests/models/autoencoders/test_models_vq.py +++ b/tests/models/autoencoders/test_models_vq.py @@ -13,10 +13,14 @@ # See the License for the specific language governing permissions and # limitations under the License. +import logging + +import numpy as np import pytest import torch from diffusers import VQModel +from diffusers.models.autoencoders.vae import VectorQuantizer from diffusers.utils.torch_utils import randn_tensor from ...testing_utils import backend_manual_seed, enable_full_determinism, torch_device @@ -64,6 +68,19 @@ def get_dummy_inputs(self) -> dict: class TestVQModel(VQModelTesterConfig, ModelTesterMixin): + def test_vector_quantizer_logs_remap_configuration(self, caplog, capsys, tmp_path): + remap_path = tmp_path / "used.npy" + np.save(remap_path, np.array([0, 2, 4], dtype=np.int64)) + + logger_name = "diffusers.models.autoencoders.vae" + with caplog.at_level(logging.INFO, logger=logger_name): + VectorQuantizer(n_e=8, vq_embed_dim=4, beta=0.25, remap=str(remap_path), unknown_index="extra") + + assert [(record.name, record.levelno, record.getMessage()) for record in caplog.records] == [ + (logger_name, logging.INFO, "Remapping 8 indices to 4 indices. Using 3 for unknown indices.") + ] + assert capsys.readouterr().out == "" + @pytest.mark.skipif( torch_device not in ["cuda", "xpu"], reason="float16 and bfloat16 can only be use for inference with an accelerator", diff --git a/tests/pipelines/longcat_image/test_prepare_pos_ids.py b/tests/pipelines/longcat_image/test_prepare_pos_ids.py new file mode 100644 index 000000000000..2e7111b541ca --- /dev/null +++ b/tests/pipelines/longcat_image/test_prepare_pos_ids.py @@ -0,0 +1,54 @@ +# Copyright 2026 The HuggingFace Team. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import logging + +import pytest + +from diffusers.pipelines.longcat_image.pipeline_longcat_image import ( + prepare_pos_ids as prepare_pos_ids_base, +) +from diffusers.pipelines.longcat_image.pipeline_longcat_image_edit import ( + prepare_pos_ids as prepare_pos_ids_edit, +) + + +@pytest.mark.parametrize( + ("prepare_pos_ids", "logger_name"), + ( + (prepare_pos_ids_base, "diffusers.pipelines.longcat_image.pipeline_longcat_image"), + (prepare_pos_ids_edit, "diffusers.pipelines.longcat_image.pipeline_longcat_image_edit"), + ), +) +@pytest.mark.parametrize( + ("kwargs", "message"), + ( + ( + {"type": "text", "num_token": 4, "height": 8, "width": 8}, + 'The parameters of height and width will be ignored in "text" type.', + ), + ( + {"type": "image", "num_token": 4, "height": 2, "width": 3}, + 'The parameter of num_token will be ignored in "image" type.', + ), + ), +) +def test_prepare_pos_ids_logs_ignored_arguments(prepare_pos_ids, logger_name, kwargs, message, caplog, capsys): + with caplog.at_level(logging.WARNING, logger=logger_name): + prepare_pos_ids(**kwargs) + + assert [(record.name, record.levelno, record.getMessage()) for record in caplog.records] == [ + (logger_name, logging.WARNING, message) + ] + assert capsys.readouterr().out == "" diff --git a/tests/pipelines/wan/test_wan_animate.py b/tests/pipelines/wan/test_wan_animate.py index 25d9e66fd196..c3c3b63f02bd 100644 --- a/tests/pipelines/wan/test_wan_animate.py +++ b/tests/pipelines/wan/test_wan_animate.py @@ -12,6 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +import logging + import pytest import torch from PIL import Image @@ -149,6 +151,34 @@ def get_dummy_inputs(self): class TestWanAnimatePipeline(WanAnimatePipelineTesterConfig, PipelineTesterMixin): + def test_prepare_prev_segment_cond_latents_logs_interpolation_warning(self, caplog, capsys): + pipe = self.get_pipeline() + prev_segment_cond_video = torch.zeros((1, 3, 1, 8, 8), dtype=torch.float32) + caplog.clear() + + logger_name = "diffusers.pipelines.wan.pipeline_wan_animate" + with caplog.at_level(logging.WARNING, logger=logger_name): + pipe.prepare_prev_segment_cond_latents( + prev_segment_cond_video=prev_segment_cond_video, + batch_size=1, + segment_frame_length=5, + height=16, + width=16, + prev_segment_cond_frames=1, + task="animate", + dtype=torch.float32, + device=torch.device("cpu"), + ) + + assert [(record.name, record.levelno, record.getMessage()) for record in caplog.records] == [ + ( + logger_name, + logging.WARNING, + "Interpolating prev segment cond video from (8, 8) to (16, 16)", + ) + ] + assert capsys.readouterr().out == "" + def test_inference(self): # Basic inference in animation mode. Run on CPU. pipe = self.get_pipeline() From 0c2125ce10e6cdca613aa46698cae8aad4cd545a Mon Sep 17 00:00:00 2001 From: wunianze666-netizen Date: Wed, 2 Sep 2026 14:27:28 +0800 Subject: [PATCH 2/2] test: remove dedicated logging regressions --- tests/models/autoencoders/test_models_vq.py | 17 ------ .../longcat_image/test_prepare_pos_ids.py | 54 ------------------- tests/pipelines/wan/test_wan_animate.py | 30 ----------- 3 files changed, 101 deletions(-) delete mode 100644 tests/pipelines/longcat_image/test_prepare_pos_ids.py diff --git a/tests/models/autoencoders/test_models_vq.py b/tests/models/autoencoders/test_models_vq.py index d1e49a4fe8fa..5b1b97d19a6c 100644 --- a/tests/models/autoencoders/test_models_vq.py +++ b/tests/models/autoencoders/test_models_vq.py @@ -13,14 +13,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import logging - -import numpy as np import pytest import torch from diffusers import VQModel -from diffusers.models.autoencoders.vae import VectorQuantizer from diffusers.utils.torch_utils import randn_tensor from ...testing_utils import backend_manual_seed, enable_full_determinism, torch_device @@ -68,19 +64,6 @@ def get_dummy_inputs(self) -> dict: class TestVQModel(VQModelTesterConfig, ModelTesterMixin): - def test_vector_quantizer_logs_remap_configuration(self, caplog, capsys, tmp_path): - remap_path = tmp_path / "used.npy" - np.save(remap_path, np.array([0, 2, 4], dtype=np.int64)) - - logger_name = "diffusers.models.autoencoders.vae" - with caplog.at_level(logging.INFO, logger=logger_name): - VectorQuantizer(n_e=8, vq_embed_dim=4, beta=0.25, remap=str(remap_path), unknown_index="extra") - - assert [(record.name, record.levelno, record.getMessage()) for record in caplog.records] == [ - (logger_name, logging.INFO, "Remapping 8 indices to 4 indices. Using 3 for unknown indices.") - ] - assert capsys.readouterr().out == "" - @pytest.mark.skipif( torch_device not in ["cuda", "xpu"], reason="float16 and bfloat16 can only be use for inference with an accelerator", diff --git a/tests/pipelines/longcat_image/test_prepare_pos_ids.py b/tests/pipelines/longcat_image/test_prepare_pos_ids.py deleted file mode 100644 index 2e7111b541ca..000000000000 --- a/tests/pipelines/longcat_image/test_prepare_pos_ids.py +++ /dev/null @@ -1,54 +0,0 @@ -# Copyright 2026 The HuggingFace Team. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import logging - -import pytest - -from diffusers.pipelines.longcat_image.pipeline_longcat_image import ( - prepare_pos_ids as prepare_pos_ids_base, -) -from diffusers.pipelines.longcat_image.pipeline_longcat_image_edit import ( - prepare_pos_ids as prepare_pos_ids_edit, -) - - -@pytest.mark.parametrize( - ("prepare_pos_ids", "logger_name"), - ( - (prepare_pos_ids_base, "diffusers.pipelines.longcat_image.pipeline_longcat_image"), - (prepare_pos_ids_edit, "diffusers.pipelines.longcat_image.pipeline_longcat_image_edit"), - ), -) -@pytest.mark.parametrize( - ("kwargs", "message"), - ( - ( - {"type": "text", "num_token": 4, "height": 8, "width": 8}, - 'The parameters of height and width will be ignored in "text" type.', - ), - ( - {"type": "image", "num_token": 4, "height": 2, "width": 3}, - 'The parameter of num_token will be ignored in "image" type.', - ), - ), -) -def test_prepare_pos_ids_logs_ignored_arguments(prepare_pos_ids, logger_name, kwargs, message, caplog, capsys): - with caplog.at_level(logging.WARNING, logger=logger_name): - prepare_pos_ids(**kwargs) - - assert [(record.name, record.levelno, record.getMessage()) for record in caplog.records] == [ - (logger_name, logging.WARNING, message) - ] - assert capsys.readouterr().out == "" diff --git a/tests/pipelines/wan/test_wan_animate.py b/tests/pipelines/wan/test_wan_animate.py index c3c3b63f02bd..25d9e66fd196 100644 --- a/tests/pipelines/wan/test_wan_animate.py +++ b/tests/pipelines/wan/test_wan_animate.py @@ -12,8 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -import logging - import pytest import torch from PIL import Image @@ -151,34 +149,6 @@ def get_dummy_inputs(self): class TestWanAnimatePipeline(WanAnimatePipelineTesterConfig, PipelineTesterMixin): - def test_prepare_prev_segment_cond_latents_logs_interpolation_warning(self, caplog, capsys): - pipe = self.get_pipeline() - prev_segment_cond_video = torch.zeros((1, 3, 1, 8, 8), dtype=torch.float32) - caplog.clear() - - logger_name = "diffusers.pipelines.wan.pipeline_wan_animate" - with caplog.at_level(logging.WARNING, logger=logger_name): - pipe.prepare_prev_segment_cond_latents( - prev_segment_cond_video=prev_segment_cond_video, - batch_size=1, - segment_frame_length=5, - height=16, - width=16, - prev_segment_cond_frames=1, - task="animate", - dtype=torch.float32, - device=torch.device("cpu"), - ) - - assert [(record.name, record.levelno, record.getMessage()) for record in caplog.records] == [ - ( - logger_name, - logging.WARNING, - "Interpolating prev segment cond video from (8, 8) to (16, 16)", - ) - ] - assert capsys.readouterr().out == "" - def test_inference(self): # Basic inference in animation mode. Run on CPU. pipe = self.get_pipeline()