mirror of
https://github.com/mudler/LocalAI.git
synced 2025-03-13 15:56:39 +00:00
Some checks are pending
Explorer deployment / build-linux (push) Waiting to run
GPU tests / ubuntu-latest (1.21.x) (push) Waiting to run
generate and publish intel docker caches / generate_caches (intel/oneapi-basekit:2025.0.0-0-devel-ubuntu22.04, linux/amd64, ubuntu-latest) (push) Waiting to run
build container images / hipblas-jobs (-aio-gpu-hipblas, rocm/dev-ubuntu-22.04:6.1, hipblas, true, ubuntu:22.04, extras, latest-gpu-hipblas, latest-aio-gpu-hipblas, --jobs=3 --output-sync=target, linux/amd64, arc-runner-set, auto, -hipblas-ffmpeg) (push) Waiting to run
build container images / hipblas-jobs (rocm/dev-ubuntu-22.04:6.1, hipblas, false, ubuntu:22.04, core, --jobs=3 --output-sync=target, linux/amd64, arc-runner-set, false, -hipblas-core) (push) Waiting to run
build container images / hipblas-jobs (rocm/dev-ubuntu-22.04:6.1, hipblas, false, ubuntu:22.04, extras, --jobs=3 --output-sync=target, linux/amd64, arc-runner-set, false, -hipblas) (push) Waiting to run
build container images / hipblas-jobs (rocm/dev-ubuntu-22.04:6.1, hipblas, true, ubuntu:22.04, core, --jobs=3 --output-sync=target, linux/amd64, arc-runner-set, false, -hipblas-ffmpeg-core) (push) Waiting to run
build container images / self-hosted-jobs (-aio-gpu-intel-f16, quay.io/go-skynet/intel-oneapi-base:latest, sycl_f16, true, ubuntu:22.04, extras, latest-gpu-intel-f16, latest-aio-gpu-intel-f16, --jobs=3 --output-sync=target, linux/amd64, arc-runner-set, auto, -sycl-f16-ffmpeg) (push) Waiting to run
build container images / self-hosted-jobs (-aio-gpu-intel-f32, quay.io/go-skynet/intel-oneapi-base:latest, sycl_f32, true, ubuntu:22.04, extras, latest-gpu-intel-f32, latest-aio-gpu-intel-f32, --jobs=3 --output-sync=target, linux/amd64, arc-runner-set, auto, -sycl-f32-ffmpeg) (push) Waiting to run
build container images / self-hosted-jobs (-aio-gpu-nvidia-cuda-11, ubuntu:22.04, cublas, 11, 7, true, extras, latest-gpu-nvidia-cuda-11, latest-aio-gpu-nvidia-cuda-11, --jobs=3 --output-sync=target, linux/amd64, arc-runner-set, auto, -cublas-cuda11-ffmpeg) (push) Waiting to run
build container images / self-hosted-jobs (-aio-gpu-nvidia-cuda-12, ubuntu:22.04, cublas, 12, 0, true, extras, latest-gpu-nvidia-cuda-12, latest-aio-gpu-nvidia-cuda-12, --jobs=3 --output-sync=target, linux/amd64, arc-runner-set, auto, -cublas-cuda12-ffmpeg) (push) Waiting to run
build container images / self-hosted-jobs (quay.io/go-skynet/intel-oneapi-base:latest, sycl_f16, false, ubuntu:22.04, core, --jobs=3 --output-sync=target, linux/amd64, arc-runner-set, false, -sycl-f16-core) (push) Waiting to run
build container images / self-hosted-jobs (quay.io/go-skynet/intel-oneapi-base:latest, sycl_f16, true, ubuntu:22.04, core, --jobs=3 --output-sync=target, linux/amd64, arc-runner-set, false, -sycl-f16-ffmpeg-core) (push) Waiting to run
build container images / self-hosted-jobs (quay.io/go-skynet/intel-oneapi-base:latest, sycl_f32, false, ubuntu:22.04, core, --jobs=3 --output-sync=target, linux/amd64, arc-runner-set, false, -sycl-f32-core) (push) Waiting to run
build container images / self-hosted-jobs (quay.io/go-skynet/intel-oneapi-base:latest, sycl_f32, true, ubuntu:22.04, core, --jobs=3 --output-sync=target, linux/amd64, arc-runner-set, false, -sycl-f32-ffmpeg-core) (push) Waiting to run
build container images / self-hosted-jobs (ubuntu:22.04, , , extras, --jobs=3 --output-sync=target, linux/amd64, arc-runner-set, auto, ) (push) Waiting to run
build container images / self-hosted-jobs (ubuntu:22.04, , true, extras, --jobs=3 --output-sync=target, linux/amd64, arc-runner-set, auto, -ffmpeg) (push) Waiting to run
build container images / self-hosted-jobs (ubuntu:22.04, cublas, 11, 7, , extras, --jobs=3 --output-sync=target, linux/amd64, arc-runner-set, false, -cublas-cuda11) (push) Waiting to run
build container images / self-hosted-jobs (ubuntu:22.04, cublas, 12, 0, , extras, --jobs=3 --output-sync=target, linux/amd64, arc-runner-set, false, -cublas-cuda12) (push) Waiting to run
build container images / core-image-build (-aio-cpu, ubuntu:22.04, , true, core, latest-cpu, latest-aio-cpu, --jobs=4 --output-sync=target, linux/amd64,linux/arm64, arc-runner-set, false, auto, -ffmpeg-core) (push) Waiting to run
build container images / core-image-build (ubuntu:22.04, cublas, 11, 7, , core, --jobs=4 --output-sync=target, linux/amd64, arc-runner-set, false, false, -cublas-cuda11-core) (push) Waiting to run
build container images / core-image-build (ubuntu:22.04, cublas, 11, 7, true, core, --jobs=4 --output-sync=target, linux/amd64, arc-runner-set, false, false, -cublas-cuda11-ffmpeg-core) (push) Waiting to run
build container images / core-image-build (ubuntu:22.04, cublas, 12, 0, , core, --jobs=4 --output-sync=target, linux/amd64, arc-runner-set, false, false, -cublas-cuda12-core) (push) Waiting to run
build container images / core-image-build (ubuntu:22.04, cublas, 12, 0, true, core, --jobs=4 --output-sync=target, linux/amd64, arc-runner-set, false, false, -cublas-cuda12-ffmpeg-core) (push) Waiting to run
build container images / core-image-build (ubuntu:22.04, vulkan, true, core, latest-vulkan-ffmpeg-core, --jobs=4 --output-sync=target, linux/amd64, arc-runner-set, false, false, -vulkan-ffmpeg-core) (push) Waiting to run
build container images / gh-runner (nvcr.io/nvidia/l4t-jetpack:r36.4.0, cublas, 12, 0, true, core, latest-nvidia-l4t-arm64-core, --jobs=4 --output-sync=target, linux/arm64, ubuntu-24.04-arm, true, false, -nvidia-l4t-arm64-core) (push) Waiting to run
Security Scan / tests (push) Waiting to run
Tests extras backends / tests-transformers (push) Waiting to run
Tests extras backends / tests-rerankers (push) Waiting to run
Tests extras backends / tests-diffusers (push) Waiting to run
Tests extras backends / tests-coqui (push) Waiting to run
tests / tests-linux (1.21.x) (push) Waiting to run
tests / tests-aio-container (push) Waiting to run
tests / tests-apple (1.21.x) (push) Waiting to run
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
232 lines
6.3 KiB
C++
232 lines
6.3 KiB
C++
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <time.h>
|
|
#include <iostream>
|
|
#include <random>
|
|
#include <string>
|
|
#include <vector>
|
|
#include "gosd.h"
|
|
|
|
// #include "preprocessing.hpp"
|
|
#include "flux.hpp"
|
|
#include "stable-diffusion.h"
|
|
|
|
#define STB_IMAGE_IMPLEMENTATION
|
|
#define STB_IMAGE_STATIC
|
|
#include "stb_image.h"
|
|
|
|
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
|
#define STB_IMAGE_WRITE_STATIC
|
|
#include "stb_image_write.h"
|
|
|
|
#define STB_IMAGE_RESIZE_IMPLEMENTATION
|
|
#define STB_IMAGE_RESIZE_STATIC
|
|
#include "stb_image_resize.h"
|
|
|
|
// Names of the sampler method, same order as enum sample_method in stable-diffusion.h
|
|
const char* sample_method_str[] = {
|
|
"euler_a",
|
|
"euler",
|
|
"heun",
|
|
"dpm2",
|
|
"dpm++2s_a",
|
|
"dpm++2m",
|
|
"dpm++2mv2",
|
|
"ipndm",
|
|
"ipndm_v",
|
|
"lcm",
|
|
"ddim_trailing",
|
|
"tcd",
|
|
};
|
|
|
|
// Names of the sigma schedule overrides, same order as sample_schedule in stable-diffusion.h
|
|
const char* schedule_str[] = {
|
|
"default",
|
|
"discrete",
|
|
"karras",
|
|
"exponential",
|
|
"ays",
|
|
"gits",
|
|
};
|
|
|
|
sd_ctx_t* sd_c;
|
|
|
|
sample_method_t sample_method;
|
|
|
|
int load_model(char *model, char* options[], int threads, int diff) {
|
|
fprintf (stderr, "Loading model!\n");
|
|
|
|
char *stableDiffusionModel = "";
|
|
if (diff == 1 ) {
|
|
stableDiffusionModel = model;
|
|
model = "";
|
|
}
|
|
|
|
// decode options. Options are in form optname:optvale, or if booleans only optname.
|
|
char *clip_l_path = "";
|
|
char *clip_g_path = "";
|
|
char *t5xxl_path = "";
|
|
char *vae_path = "";
|
|
char *scheduler = "";
|
|
char *sampler = "";
|
|
|
|
// If options is not NULL, parse options
|
|
for (int i = 0; options[i] != NULL; i++) {
|
|
char *optname = strtok(options[i], ":");
|
|
char *optval = strtok(NULL, ":");
|
|
if (optval == NULL) {
|
|
optval = "true";
|
|
}
|
|
|
|
if (!strcmp(optname, "clip_l_path")) {
|
|
clip_l_path = optval;
|
|
}
|
|
if (!strcmp(optname, "clip_g_path")) {
|
|
clip_g_path = optval;
|
|
}
|
|
if (!strcmp(optname, "t5xxl_path")) {
|
|
t5xxl_path = optval;
|
|
}
|
|
if (!strcmp(optname, "vae_path")) {
|
|
vae_path = optval;
|
|
}
|
|
if (!strcmp(optname, "scheduler")) {
|
|
scheduler = optval;
|
|
}
|
|
if (!strcmp(optname, "sampler")) {
|
|
sampler = optval;
|
|
}
|
|
}
|
|
|
|
int sample_method_found = -1;
|
|
for (int m = 0; m < N_SAMPLE_METHODS; m++) {
|
|
if (!strcmp(sampler, sample_method_str[m])) {
|
|
sample_method_found = m;
|
|
}
|
|
}
|
|
if (sample_method_found == -1) {
|
|
fprintf(stderr, "Invalid sample method, default to EULER_A!\n");
|
|
sample_method_found = EULER_A;
|
|
}
|
|
sample_method = (sample_method_t)sample_method_found;
|
|
|
|
int schedule_found = -1;
|
|
for (int d = 0; d < N_SCHEDULES; d++) {
|
|
if (!strcmp(scheduler, schedule_str[d])) {
|
|
schedule_found = d;
|
|
fprintf (stderr, "Found scheduler: %s\n", scheduler);
|
|
|
|
}
|
|
}
|
|
|
|
if (schedule_found == -1) {
|
|
fprintf (stderr, "Invalid scheduler! using DEFAULT\n");
|
|
schedule_found = DEFAULT;
|
|
}
|
|
|
|
schedule_t schedule = (schedule_t)schedule_found;
|
|
|
|
fprintf (stderr, "Creating context\n");
|
|
sd_ctx_t* sd_ctx = new_sd_ctx(model,
|
|
clip_l_path,
|
|
clip_g_path,
|
|
t5xxl_path,
|
|
stableDiffusionModel,
|
|
vae_path,
|
|
"",
|
|
"",
|
|
"",
|
|
"",
|
|
"",
|
|
false,
|
|
false,
|
|
false,
|
|
threads,
|
|
SD_TYPE_COUNT,
|
|
STD_DEFAULT_RNG,
|
|
schedule,
|
|
false,
|
|
false,
|
|
false,
|
|
false);
|
|
|
|
if (sd_ctx == NULL) {
|
|
fprintf (stderr, "failed loading model (generic error)\n");
|
|
return 1;
|
|
}
|
|
fprintf (stderr, "Created context: OK\n");
|
|
|
|
sd_c = sd_ctx;
|
|
|
|
return 0;
|
|
}
|
|
|
|
int gen_image(char *text, char *negativeText, int width, int height, int steps, int seed , char *dst, float cfg_scale) {
|
|
|
|
sd_image_t* results;
|
|
|
|
std::vector<int> skip_layers = {7, 8, 9};
|
|
|
|
fprintf (stderr, "Generating image\n");
|
|
|
|
results = txt2img(sd_c,
|
|
text,
|
|
negativeText,
|
|
-1, //clip_skip
|
|
cfg_scale, // sfg_scale
|
|
3.5f,
|
|
0, // eta
|
|
width,
|
|
height,
|
|
sample_method,
|
|
steps,
|
|
seed,
|
|
1,
|
|
NULL,
|
|
0.9f,
|
|
20.f,
|
|
false,
|
|
"",
|
|
skip_layers.data(),
|
|
skip_layers.size(),
|
|
0,
|
|
0.01,
|
|
0.2);
|
|
|
|
if (results == NULL) {
|
|
fprintf (stderr, "NO results\n");
|
|
return 1;
|
|
}
|
|
|
|
if (results[0].data == NULL) {
|
|
fprintf (stderr, "Results with no data\n");
|
|
return 1;
|
|
}
|
|
|
|
fprintf (stderr, "Writing PNG\n");
|
|
|
|
fprintf (stderr, "DST: %s\n", dst);
|
|
fprintf (stderr, "Width: %d\n", results[0].width);
|
|
fprintf (stderr, "Height: %d\n", results[0].height);
|
|
fprintf (stderr, "Channel: %d\n", results[0].channel);
|
|
fprintf (stderr, "Data: %p\n", results[0].data);
|
|
|
|
stbi_write_png(dst, results[0].width, results[0].height, results[0].channel,
|
|
results[0].data, 0, NULL);
|
|
fprintf (stderr, "Saved resulting image to '%s'\n", dst);
|
|
|
|
// TODO: free results. Why does it crash?
|
|
|
|
free(results[0].data);
|
|
results[0].data = NULL;
|
|
free(results);
|
|
fprintf (stderr, "gen_image is done", dst);
|
|
|
|
return 0;
|
|
}
|
|
|
|
int unload() {
|
|
free_sd_ctx(sd_c);
|
|
}
|
|
|