forked from PaddlePaddle/FastDeploy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Diffusion] Add C++ dpm solver (PaddlePaddle#714)
* Add BetaForAlphaBar, ConvertModelOutput, SetTimesteps, and constructor for DPMSolverMultistepScheduler * tmp * Add DPMSolverFirstOrderUpdate * Add ScaleModelInput * Add MultiStepDPMSolverSecondOrderUpdate * add MultiStepDPMSolverThirdOrderUpdate * Add Step * Add FASTDEPLOY_DECL * Add AddNoise * Fix operator * update * Fix DPMSolverMultistepScheduler * Upgrade Slice * Fix DPMSolverFirstOrderUpdate * remove FASTDEPLOY_DECL * Add config for dpm solver
- Loading branch information
Showing
14 changed files
with
675 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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,27 @@ | ||
# Copyright (c) 2022 PaddlePaddle Authors. 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. | ||
|
||
PROJECT(main C CXX) | ||
CMAKE_MINIMUM_REQUIRED (VERSION 3.10) | ||
|
||
option(FASTDEPLOY_INSTALL_DIR "Path of downloaded fastdeploy sdk.") | ||
set(THIRD_LIBS "") | ||
include(${FASTDEPLOY_INSTALL_DIR}/FastDeploy.cmake) | ||
|
||
include_directories(${FASTDEPLOY_INCS}) | ||
|
||
file(GLOB_RECURSE ALL_SRCS ${PROJECT_SOURCE_DIR}/*.cc) | ||
|
||
add_executable(main ${ALL_SRCS}) | ||
target_link_libraries(main ${FASTDEPLOY_LIBS} ${THIRD_LIBS}) |
395 changes: 395 additions & 0 deletions
395
examples/multimodal/stable_diffusion/cpp/dpm_solver_multistep_scheduler.cc
Large diffs are not rendered by default.
Oops, something went wrong.
85 changes: 85 additions & 0 deletions
85
examples/multimodal/stable_diffusion/cpp/dpm_solver_multistep_scheduler.h
This file contains 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,85 @@ | ||
// Copyright (c) 2022 PaddlePaddle Authors. 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. | ||
|
||
#pragma once | ||
|
||
#include "./scheduler.h" | ||
#include "fastdeploy/core/fd_tensor.h" | ||
|
||
namespace fastdeploy { | ||
|
||
class DPMSolverMultistepScheduler : public Scheduler { | ||
public: | ||
DPMSolverMultistepScheduler(int num_train_timesteps = 1000, | ||
float beta_start = 0.0001, float beta_end = 0.02, | ||
const std::string& beta_schedule = "linear", | ||
const std::vector<float>& trained_betas = {}, | ||
int solver_order = 2, bool predict_epsilon = true, | ||
bool thresholding = false, | ||
float dynamic_thresholding_ratio = 0.995, | ||
float sample_max_value = 1.0, | ||
const std::string& algorithm_type = "dpmsolver++", | ||
const std::string& solver_type = "midpoint", | ||
bool lower_order_final = true); | ||
void BetaForAlphaBar(FDTensor* out, int num_diffusion_timesteps, | ||
float max_beta = 0.999); | ||
void ConvertModelOutput(const FDTensor& model_output, int timestep, | ||
const FDTensor& sample, FDTensor* out); | ||
void DPMSolverFirstOrderUpdate(const FDTensor& model_output, int timestep, | ||
int prev_timestep, const FDTensor& sample, | ||
FDTensor* out); | ||
void MultiStepDPMSolverSecondOrderUpdate( | ||
const std::vector<FDTensor>& model_output_list, | ||
const std::vector<int>& timestep_list, int prev_timestep, | ||
const FDTensor& sample, FDTensor* out); | ||
void MultiStepDPMSolverThirdOrderUpdate( | ||
const std::vector<FDTensor>& model_output_list, | ||
const std::vector<int>& timestep_list, int prev_timestep, | ||
const FDTensor& sample, FDTensor* out); | ||
void SetTimesteps(int num_inference_steps) override; | ||
void Step(const FDTensor& model_output, int timestep, const FDTensor& sample, | ||
FDTensor* prev_sample) override; | ||
void ScaleModelInput(const FDTensor& sample, FDTensor* out, | ||
const std::vector<FDTensor>& timesteps = {}) override; | ||
void AddNoise(const FDTensor& original_samples, const FDTensor& noise, | ||
const FDTensor& timesteps, FDTensor* out) override; | ||
struct Config { | ||
int num_train_timesteps_; | ||
float beta_start_; | ||
float beta_end_; | ||
std::string beta_schedule_; | ||
int solver_order_; | ||
bool predict_epsilon_; | ||
bool thresholding_; | ||
float dynamic_thresholding_ratio_; | ||
float sample_max_value_; | ||
std::string algorithm_type_; | ||
std::string solver_type_; | ||
bool lower_order_final_; | ||
} config; | ||
|
||
private: | ||
FDTensor betas_; | ||
FDTensor alphas_; | ||
FDTensor alphas_cumprod_; | ||
FDTensor alpha_t_; | ||
FDTensor sigma_t_; | ||
FDTensor lambda_t_; | ||
int num_inference_steps_; | ||
FDTensor timesteps_; | ||
int lower_order_nums_; | ||
std::vector<FDTensor> model_outputs_; | ||
}; | ||
|
||
} // namespace fastdeploy |
This file contains 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,35 @@ | ||
// Copyright (c) 2022 PaddlePaddle Authors. 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. | ||
|
||
#include "dpm_solver_multistep_scheduler.h" | ||
#include <iostream> | ||
|
||
int main() { | ||
fastdeploy::DPMSolverMultistepScheduler dpm( | ||
/* num_train_timesteps */ 1000, | ||
/* beta_start = */ 0.00085, | ||
/* beta_end = */ 0.012, | ||
/* beta_schedule = */ "scaled_linear", | ||
/* trained_betas = */ {}, | ||
/* solver_order = */ 2, | ||
/* predict_epsilon = */ true, | ||
/* thresholding = */ false, | ||
/* dynamic_thresholding_ratio = */ 0.995, | ||
/* sample_max_value = */ 1.0, | ||
/* algorithm_type = */ "dpmsolver++", | ||
/* solver_type = */ "midpoint", | ||
/* lower_order_final = */ true); | ||
|
||
return 0; | ||
} |
This file contains 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,31 @@ | ||
// Copyright (c) 2022 PaddlePaddle Authors. 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. | ||
|
||
#pragma once | ||
|
||
#include "fastdeploy/core/fd_tensor.h" | ||
|
||
namespace fastdeploy { | ||
|
||
class Scheduler { | ||
virtual void SetTimesteps(int num_inference_steps) = 0; | ||
virtual void Step(const FDTensor& model_output, int timestep, | ||
const FDTensor& sample, FDTensor* prev_sample) = 0; | ||
virtual void ScaleModelInput(const FDTensor& sample, FDTensor* out, | ||
const std::vector<FDTensor>& timesteps = {}) = 0; | ||
virtual void AddNoise(const FDTensor& original_samples, const FDTensor& noise, | ||
const FDTensor& timesteps, FDTensor* out) = 0; | ||
}; | ||
|
||
} // namespace fastdeploy |
This file contains 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 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 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 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 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 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 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 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 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