Spaces:
Paused
Paused
File size: 3,241 Bytes
2f5127c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# Copyright 2020-2025 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 gc
import unittest
import pytest
import torch
from parameterized import parameterized
from transformers.utils import is_peft_available
from trl.import_utils import is_diffusers_available
from .testing_utils import require_diffusers
if is_diffusers_available() and is_peft_available():
from trl import AlignPropConfig, AlignPropTrainer, DefaultDDPOStableDiffusionPipeline
def scorer_function(images, prompts, metadata):
return torch.randn(1) * 3.0, {}
def prompt_function():
return ("cabbages", {})
@pytest.mark.low_priority
@require_diffusers
class AlignPropTrainerTester(unittest.TestCase):
"""
Test the AlignPropTrainer class.
"""
def setUp(self):
training_args = AlignPropConfig(
num_epochs=2,
train_gradient_accumulation_steps=1,
train_batch_size=2,
truncated_backprop_rand=False,
mixed_precision=None,
save_freq=1000000,
)
pretrained_model = "hf-internal-testing/tiny-stable-diffusion-torch"
pretrained_revision = "main"
pipeline_with_lora = DefaultDDPOStableDiffusionPipeline(
pretrained_model, pretrained_model_revision=pretrained_revision, use_lora=True
)
pipeline_without_lora = DefaultDDPOStableDiffusionPipeline(
pretrained_model, pretrained_model_revision=pretrained_revision, use_lora=False
)
self.trainer_with_lora = AlignPropTrainer(training_args, scorer_function, prompt_function, pipeline_with_lora)
self.trainer_without_lora = AlignPropTrainer(
training_args, scorer_function, prompt_function, pipeline_without_lora
)
def tearDown(self) -> None:
gc.collect()
@parameterized.expand([True, False])
def test_generate_samples(self, use_lora):
trainer = self.trainer_with_lora if use_lora else self.trainer_without_lora
output_pairs = trainer._generate_samples(2, with_grad=True)
self.assertEqual(len(output_pairs.keys()), 3)
self.assertEqual(len(output_pairs["images"]), 2)
@parameterized.expand([True, False])
def test_calculate_loss(self, use_lora):
trainer = self.trainer_with_lora if use_lora else self.trainer_without_lora
sample = trainer._generate_samples(2)
images = sample["images"]
prompts = sample["prompts"]
self.assertTupleEqual(images.shape, (2, 3, 128, 128))
self.assertEqual(len(prompts), 2)
rewards = trainer.compute_rewards(sample)
loss = trainer.calculate_loss(rewards)
self.assertTrue(torch.isfinite(loss.cpu()))
|