chore: add .clang-format

This commit is contained in:
leejet 2023-11-19 19:35:33 +08:00
parent 64f6002457
commit 176a00b606
7 changed files with 585 additions and 571 deletions

13
.clang-format Normal file
View File

@ -0,0 +1,13 @@
BasedOnStyle: Chromium
UseTab: Never
IndentWidth: 4
TabWidth: 4
AllowShortIfStatementsOnASingleLine: false
IndentCaseLabels: false
ColumnLimit: 0
AccessModifierOffset: -4
NamespaceIndentation: All
FixNamespaceComments: false
AlignAfterOpenBracket: true
AlignConsecutiveAssignments: true
IndentCaseLabels: true

1
.gitignore vendored
View File

@ -3,3 +3,4 @@ test/
.cache/ .cache/
*.swp *.swp
.vscode/

View File

@ -88,11 +88,10 @@ const char* sample_method_str[] = {
const char* schedule_str[] = { const char* schedule_str[] = {
"default", "default",
"discrete", "discrete",
"karras" "karras"};
};
struct Option { struct Option {
int n_threads = -1; int n_threads = -1;
std::string mode = TXT2IMG; std::string mode = TXT2IMG;
std::string model_path; std::string model_path;
std::string lora_model_dir; std::string lora_model_dir;
@ -100,16 +99,16 @@ struct Option {
std::string init_img; std::string init_img;
std::string prompt; std::string prompt;
std::string negative_prompt; std::string negative_prompt;
float cfg_scale = 7.0f; float cfg_scale = 7.0f;
int w = 512; int w = 512;
int h = 512; int h = 512;
SampleMethod sample_method = EULER_A; SampleMethod sample_method = EULER_A;
Schedule schedule = DEFAULT; Schedule schedule = DEFAULT;
int sample_steps = 20; int sample_steps = 20;
float strength = 0.75f; float strength = 0.75f;
RNGType rng_type = CUDA_RNG; RNGType rng_type = CUDA_RNG;
int64_t seed = 42; int64_t seed = 42;
bool verbose = false; bool verbose = false;
void print() { void print() {
printf("Option: \n"); printf("Option: \n");
@ -129,7 +128,7 @@ struct Option {
printf(" sample_steps: %d\n", sample_steps); printf(" sample_steps: %d\n", sample_steps);
printf(" strength: %.2f\n", strength); printf(" strength: %.2f\n", strength);
printf(" rng: %s\n", rng_type_to_str[rng_type]); printf(" rng: %s\n", rng_type_to_str[rng_type]);
printf(" seed: %lld\n", seed); printf(" seed: %ld\n", seed);
} }
}; };
@ -266,7 +265,7 @@ void parse_args(int argc, const char* argv[], Option* opt) {
break; break;
} }
const char* schedule_selected = argv[i]; const char* schedule_selected = argv[i];
int schedule_found = -1; int schedule_found = -1;
for (int d = 0; d < N_SCHEDULES; d++) { for (int d = 0; d < N_SCHEDULES; d++) {
if (!strcmp(schedule_selected, schedule_str[d])) { if (!strcmp(schedule_selected, schedule_str[d])) {
schedule_found = d; schedule_found = d;
@ -289,7 +288,7 @@ void parse_args(int argc, const char* argv[], Option* opt) {
break; break;
} }
const char* sample_method_selected = argv[i]; const char* sample_method_selected = argv[i];
int sample_method_found = -1; int sample_method_found = -1;
for (int m = 0; m < N_SAMPLE_METHODS; m++) { for (int m = 0; m < N_SAMPLE_METHODS; m++) {
if (!strcmp(sample_method_selected, sample_method_str[m])) { if (!strcmp(sample_method_selected, sample_method_str[m])) {
sample_method_found = m; sample_method_found = m;
@ -405,6 +404,7 @@ int main(int argc, const char* argv[]) {
vae_decode_only = false; vae_decode_only = false;
int c = 0; int c = 0;
unsigned char* img_data = stbi_load(opt.init_img.c_str(), &opt.w, &opt.h, &c, 3); unsigned char* img_data = stbi_load(opt.init_img.c_str(), &opt.w, &opt.h, &c, 3);
if (img_data == NULL) { if (img_data == NULL) {
fprintf(stderr, "load image from '%s' failed\n", opt.init_img.c_str()); fprintf(stderr, "load image from '%s' failed\n", opt.init_img.c_str());

10
rng.h
View File

@ -5,23 +5,23 @@
#include <vector> #include <vector>
class RNG { class RNG {
public: public:
virtual void manual_seed(uint64_t seed) = 0; virtual void manual_seed(uint64_t seed) = 0;
virtual std::vector<float> randn(uint32_t n) = 0; virtual std::vector<float> randn(uint32_t n) = 0;
}; };
class STDDefaultRNG : public RNG { class STDDefaultRNG : public RNG {
private: private:
std::default_random_engine generator; std::default_random_engine generator;
public: public:
void manual_seed(uint64_t seed) { void manual_seed(uint64_t seed) {
generator.seed((unsigned int)seed); generator.seed((unsigned int)seed);
} }
std::vector<float> randn(uint32_t n) { std::vector<float> randn(uint32_t n) {
std::vector<float> result; std::vector<float> result;
float mean = 0.0; float mean = 0.0;
float stddev = 1.0; float stddev = 1.0;
std::normal_distribution<float> distribution(mean, stddev); std::normal_distribution<float> distribution(mean, stddev);
for (uint32_t i = 0; i < n; i++) { for (uint32_t i = 0; i < n; i++) {

View File

@ -9,15 +9,15 @@
// RNG imitiating torch cuda randn on CPU. // RNG imitiating torch cuda randn on CPU.
// Port from: https://github.com/AUTOMATIC1111/stable-diffusion-webui/blob/5ef669de080814067961f28357256e8fe27544f4/modules/rng_philox.py // Port from: https://github.com/AUTOMATIC1111/stable-diffusion-webui/blob/5ef669de080814067961f28357256e8fe27544f4/modules/rng_philox.py
class PhiloxRNG : public RNG { class PhiloxRNG : public RNG {
private: private:
uint64_t seed; uint64_t seed;
uint32_t offset; uint32_t offset;
private: private:
std::vector<uint32_t> philox_m = {0xD2511F53, 0xCD9E8D57}; std::vector<uint32_t> philox_m = {0xD2511F53, 0xCD9E8D57};
std::vector<uint32_t> philox_w = {0x9E3779B9, 0xBB67AE85}; std::vector<uint32_t> philox_w = {0x9E3779B9, 0xBB67AE85};
float two_pow32_inv = 2.3283064e-10f; float two_pow32_inv = 2.3283064e-10f;
float two_pow32_inv_2pi = 2.3283064e-10f * 6.2831855f; float two_pow32_inv_2pi = 2.3283064e-10f * 6.2831855f;
std::vector<uint32_t> uint32(uint64_t x) { std::vector<uint32_t> uint32(uint64_t x) {
std::vector<uint32_t> result(2); std::vector<uint32_t> result(2);
@ -87,14 +87,14 @@ class PhiloxRNG : public RNG {
return r1; return r1;
} }
public: public:
PhiloxRNG(uint64_t seed = 0) { PhiloxRNG(uint64_t seed = 0) {
this->seed = seed; this->seed = seed;
this->offset = 0; this->offset = 0;
} }
void manual_seed(uint64_t seed) { void manual_seed(uint64_t seed) {
this->seed = seed; this->seed = seed;
this->offset = 0; this->offset = 0;
} }

File diff suppressed because it is too large Load Diff

View File

@ -38,15 +38,15 @@ enum Schedule {
class StableDiffusionGGML; class StableDiffusionGGML;
class StableDiffusion { class StableDiffusion {
private: private:
std::shared_ptr<StableDiffusionGGML> sd; std::shared_ptr<StableDiffusionGGML> sd;
public: public:
StableDiffusion(int n_threads = -1, StableDiffusion(int n_threads = -1,
bool vae_decode_only = false, bool vae_decode_only = false,
bool free_params_immediately = false, bool free_params_immediately = false,
std::string lora_model_dir = "", std::string lora_model_dir = "",
RNGType rng_type = STD_DEFAULT_RNG); RNGType rng_type = STD_DEFAULT_RNG);
bool load_from_file(const std::string& file_path, Schedule d = DEFAULT); bool load_from_file(const std::string& file_path, Schedule d = DEFAULT);
std::vector<uint8_t> txt2img( std::vector<uint8_t> txt2img(
std::string prompt, std::string prompt,