2022-10-25 10:39:25 -06:00
|
|
|
# coding=utf-8
|
|
|
|
# Copyright 2022 HuggingFace Inc.
|
|
|
|
#
|
|
|
|
# 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 numpy as np
|
|
|
|
import torch
|
|
|
|
|
|
|
|
from diffusers import DanceDiffusionPipeline, IPNDMScheduler, UNet1DModel
|
|
|
|
from diffusers.utils import slow, torch_device
|
|
|
|
from diffusers.utils.testing_utils import require_torch_gpu
|
|
|
|
|
2022-12-06 10:35:30 -07:00
|
|
|
from ...test_pipelines_common import PipelineTesterMixin
|
|
|
|
|
2022-10-25 10:39:25 -06:00
|
|
|
|
|
|
|
torch.backends.cuda.matmul.allow_tf32 = False
|
|
|
|
|
|
|
|
|
2022-12-06 10:35:30 -07:00
|
|
|
class DanceDiffusionPipelineFastTests(PipelineTesterMixin, unittest.TestCase):
|
|
|
|
pipeline_class = DanceDiffusionPipeline
|
|
|
|
test_attention_slicing = False
|
|
|
|
test_cpu_offload = False
|
2022-10-25 10:39:25 -06:00
|
|
|
|
2022-12-06 10:35:30 -07:00
|
|
|
def get_dummy_components(self):
|
2022-10-25 10:39:25 -06:00
|
|
|
torch.manual_seed(0)
|
2022-12-06 10:35:30 -07:00
|
|
|
unet = UNet1DModel(
|
2022-10-25 10:39:25 -06:00
|
|
|
block_out_channels=(32, 32, 64),
|
|
|
|
extra_in_channels=16,
|
|
|
|
sample_size=512,
|
|
|
|
sample_rate=16_000,
|
|
|
|
in_channels=2,
|
|
|
|
out_channels=2,
|
Add UNet 1d for RL model for planning + colab (#105)
* re-add RL model code
* match model forward api
* add register_to_config, pass training tests
* fix tests, update forward outputs
* remove unused code, some comments
* add to docs
* remove extra embedding code
* unify time embedding
* remove conv1d output sequential
* remove sequential from conv1dblock
* style and deleting duplicated code
* clean files
* remove unused variables
* clean variables
* add 1d resnet block structure for downsample
* rename as unet1d
* fix renaming
* rename files
* add get_block(...) api
* unify args for model1d like model2d
* minor cleaning
* fix docs
* improve 1d resnet blocks
* fix tests, remove permuts
* fix style
* add output activation
* rename flax blocks file
* Add Value Function and corresponding example script to Diffuser implementation (#884)
* valuefunction code
* start example scripts
* missing imports
* bug fixes and placeholder example script
* add value function scheduler
* load value function from hub and get best actions in example
* very close to working example
* larger batch size for planning
* more tests
* merge unet1d changes
* wandb for debugging, use newer models
* success!
* turns out we just need more diffusion steps
* run on modal
* merge and code cleanup
* use same api for rl model
* fix variance type
* wrong normalization function
* add tests
* style
* style and quality
* edits based on comments
* style and quality
* remove unused var
* hack unet1d into a value function
* add pipeline
* fix arg order
* add pipeline to core library
* community pipeline
* fix couple shape bugs
* style
* Apply suggestions from code review
Co-authored-by: Nathan Lambert <nathan@huggingface.co>
* update post merge of scripts
* add mdiblock / outblock architecture
* Pipeline cleanup (#947)
* valuefunction code
* start example scripts
* missing imports
* bug fixes and placeholder example script
* add value function scheduler
* load value function from hub and get best actions in example
* very close to working example
* larger batch size for planning
* more tests
* merge unet1d changes
* wandb for debugging, use newer models
* success!
* turns out we just need more diffusion steps
* run on modal
* merge and code cleanup
* use same api for rl model
* fix variance type
* wrong normalization function
* add tests
* style
* style and quality
* edits based on comments
* style and quality
* remove unused var
* hack unet1d into a value function
* add pipeline
* fix arg order
* add pipeline to core library
* community pipeline
* fix couple shape bugs
* style
* Apply suggestions from code review
* clean up comments
* convert older script to using pipeline and add readme
* rename scripts
* style, update tests
* delete unet rl model file
* remove imports in src
Co-authored-by: Nathan Lambert <nathan@huggingface.co>
* Update src/diffusers/models/unet_1d_blocks.py
* Update tests/test_models_unet.py
* RL Cleanup v2 (#965)
* valuefunction code
* start example scripts
* missing imports
* bug fixes and placeholder example script
* add value function scheduler
* load value function from hub and get best actions in example
* very close to working example
* larger batch size for planning
* more tests
* merge unet1d changes
* wandb for debugging, use newer models
* success!
* turns out we just need more diffusion steps
* run on modal
* merge and code cleanup
* use same api for rl model
* fix variance type
* wrong normalization function
* add tests
* style
* style and quality
* edits based on comments
* style and quality
* remove unused var
* hack unet1d into a value function
* add pipeline
* fix arg order
* add pipeline to core library
* community pipeline
* fix couple shape bugs
* style
* Apply suggestions from code review
* clean up comments
* convert older script to using pipeline and add readme
* rename scripts
* style, update tests
* delete unet rl model file
* remove imports in src
* add specific vf block and update tests
* style
* Update tests/test_models_unet.py
Co-authored-by: Nathan Lambert <nathan@huggingface.co>
* fix quality in tests
* fix quality style, split test file
* fix checks / tests
* make timesteps closer to main
* unify block API
* unify forward api
* delete lines in examples
* style
* examples style
* all tests pass
* make style
* make dance_diff test pass
* Refactoring RL PR (#1200)
* init file changes
* add import utils
* finish cleaning files, imports
* remove import flags
* clean examples
* fix imports, tests for merge
* update readmes
* hotfix for tests
* quality
* fix some tests
* change defaults
* more mps test fixes
* unet1d defaults
* do not default import experimental
* defaults for tests
* fix tests
* fix-copies
* fix
* changes per Patrik's comments (#1285)
* changes per Patrik's comments
* update conversion script
* fix renaming
* skip more mps tests
* last test fix
* Update examples/rl/README.md
Co-authored-by: Ben Glickenhaus <benglickenhaus@gmail.com>
2022-11-14 14:48:48 -07:00
|
|
|
flip_sin_to_cos=True,
|
|
|
|
use_timestep_embedding=False,
|
|
|
|
time_embedding_type="fourier",
|
|
|
|
mid_block_type="UNetMidBlock1D",
|
2022-12-06 10:35:30 -07:00
|
|
|
down_block_types=("DownBlock1DNoSkip", "DownBlock1D", "AttnDownBlock1D"),
|
|
|
|
up_block_types=("AttnUpBlock1D", "UpBlock1D", "UpBlock1DNoSkip"),
|
2022-10-25 10:39:25 -06:00
|
|
|
)
|
2022-12-06 10:35:30 -07:00
|
|
|
scheduler = IPNDMScheduler()
|
|
|
|
|
|
|
|
components = {
|
|
|
|
"unet": unet,
|
|
|
|
"scheduler": scheduler,
|
|
|
|
}
|
|
|
|
return components
|
|
|
|
|
|
|
|
def get_dummy_inputs(self, device, seed=0):
|
|
|
|
if str(device).startswith("mps"):
|
|
|
|
generator = torch.manual_seed(seed)
|
|
|
|
else:
|
|
|
|
generator = torch.Generator(device=device).manual_seed(seed)
|
|
|
|
inputs = {
|
2022-12-13 04:50:15 -07:00
|
|
|
"batch_size": 1,
|
2022-12-06 10:35:30 -07:00
|
|
|
"generator": generator,
|
|
|
|
"num_inference_steps": 4,
|
|
|
|
}
|
|
|
|
return inputs
|
2022-10-25 10:39:25 -06:00
|
|
|
|
|
|
|
def test_dance_diffusion(self):
|
|
|
|
device = "cpu" # ensure determinism for the device-dependent torch.Generator
|
2022-12-06 10:35:30 -07:00
|
|
|
components = self.get_dummy_components()
|
|
|
|
pipe = DanceDiffusionPipeline(**components)
|
2022-10-25 10:39:25 -06:00
|
|
|
pipe = pipe.to(device)
|
|
|
|
pipe.set_progress_bar_config(disable=None)
|
|
|
|
|
2022-12-06 10:35:30 -07:00
|
|
|
inputs = self.get_dummy_inputs(device)
|
|
|
|
output = pipe(**inputs)
|
2022-10-25 10:39:25 -06:00
|
|
|
audio = output.audios
|
|
|
|
|
|
|
|
audio_slice = audio[0, -3:, -3:]
|
|
|
|
|
2022-12-06 10:35:30 -07:00
|
|
|
assert audio.shape == (1, 2, components["unet"].sample_size)
|
2022-10-25 10:39:25 -06:00
|
|
|
expected_slice = np.array([-0.7265, 1.0000, -0.8388, 0.1175, 0.9498, -1.0000])
|
|
|
|
assert np.abs(audio_slice.flatten() - expected_slice).max() < 1e-2
|
|
|
|
|
|
|
|
|
|
|
|
@slow
|
|
|
|
@require_torch_gpu
|
|
|
|
class PipelineIntegrationTests(unittest.TestCase):
|
|
|
|
def tearDown(self):
|
|
|
|
# clean up the VRAM after each test
|
|
|
|
super().tearDown()
|
|
|
|
gc.collect()
|
|
|
|
torch.cuda.empty_cache()
|
|
|
|
|
|
|
|
def test_dance_diffusion(self):
|
|
|
|
device = torch_device
|
|
|
|
|
2022-11-03 10:25:57 -06:00
|
|
|
pipe = DanceDiffusionPipeline.from_pretrained("harmonai/maestro-150k")
|
2022-10-25 10:39:25 -06:00
|
|
|
pipe = pipe.to(device)
|
|
|
|
pipe.set_progress_bar_config(disable=None)
|
|
|
|
|
|
|
|
generator = torch.Generator(device=device).manual_seed(0)
|
2022-10-25 11:52:41 -06:00
|
|
|
output = pipe(generator=generator, num_inference_steps=100, audio_length_in_s=4.096)
|
2022-10-25 10:39:25 -06:00
|
|
|
audio = output.audios
|
|
|
|
|
|
|
|
audio_slice = audio[0, -3:, -3:]
|
|
|
|
|
|
|
|
assert audio.shape == (1, 2, pipe.unet.sample_size)
|
|
|
|
expected_slice = np.array([-0.1576, -0.1526, -0.127, -0.2699, -0.2762, -0.2487])
|
|
|
|
assert np.abs(audio_slice.flatten() - expected_slice).max() < 1e-2
|
2022-10-25 11:33:43 -06:00
|
|
|
|
|
|
|
def test_dance_diffusion_fp16(self):
|
|
|
|
device = torch_device
|
|
|
|
|
2022-11-03 10:25:57 -06:00
|
|
|
pipe = DanceDiffusionPipeline.from_pretrained("harmonai/maestro-150k", torch_dtype=torch.float16)
|
2022-10-25 11:33:43 -06:00
|
|
|
pipe = pipe.to(device)
|
|
|
|
pipe.set_progress_bar_config(disable=None)
|
|
|
|
|
|
|
|
generator = torch.Generator(device=device).manual_seed(0)
|
2022-10-25 11:52:41 -06:00
|
|
|
output = pipe(generator=generator, num_inference_steps=100, audio_length_in_s=4.096)
|
2022-10-25 11:33:43 -06:00
|
|
|
audio = output.audios
|
|
|
|
|
|
|
|
audio_slice = audio[0, -3:, -3:]
|
|
|
|
|
|
|
|
assert audio.shape == (1, 2, pipe.unet.sample_size)
|
|
|
|
expected_slice = np.array([-0.1693, -0.1698, -0.1447, -0.3044, -0.3203, -0.2937])
|
|
|
|
assert np.abs(audio_slice.flatten() - expected_slice).max() < 1e-2
|