Skip to content

Commit 7fa7ed8

Browse files
committed
Error handling
1 parent 02af48a commit 7fa7ed8

File tree

8 files changed

+884
-752
lines changed

8 files changed

+884
-752
lines changed

.github/workflows/build.yml

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ on:
99
type: boolean
1010
push:
1111
branches:
12-
- master
12+
- dreamio
1313
- ci
1414
paths:
1515
[
@@ -78,14 +78,14 @@ jobs:
7878
7979
- name: Pack artifacts
8080
id: pack_artifacts
81-
if: ${{ ( github.event_name == 'push' && github.ref == 'refs/heads/master' ) || github.event.inputs.create_release == 'true' }}
81+
if: ${{ ( github.event_name == 'push' && github.ref == 'refs/heads/dreamio' ) || github.event.inputs.create_release == 'true' }}
8282
run: |
8383
cp ggml/LICENSE ./build/bin/ggml.txt
8484
cp LICENSE ./build/bin/stable-diffusion.cpp.txt
8585
zip -j sd-${{ env.BRANCH_NAME }}-${{ steps.commit.outputs.short }}-bin-${{ steps.system-info.outputs.OS_TYPE }}-${{ steps.system-info.outputs.OS_NAME }}-${{ steps.system-info.outputs.OS_VERSION }}-${{ steps.system-info.outputs.CPU_ARCH }}.zip ./build/bin/*
8686
8787
- name: Upload artifacts
88-
if: ${{ ( github.event_name == 'push' && github.ref == 'refs/heads/master' ) || github.event.inputs.create_release == 'true' }}
88+
if: ${{ ( github.event_name == 'push' && github.ref == 'refs/heads/dreamio' ) || github.event.inputs.create_release == 'true' }}
8989
uses: actions/upload-artifact@v4
9090
with:
9191
name: sd-${{ env.BRANCH_NAME }}-${{ steps.commit.outputs.short }}-bin-${{ steps.system-info.outputs.OS_TYPE }}-${{ steps.system-info.outputs.OS_NAME }}-${{ steps.system-info.outputs.OS_VERSION }}-${{ steps.system-info.outputs.CPU_ARCH }}.zip
@@ -131,22 +131,22 @@ jobs:
131131
132132
- name: Pack artifacts
133133
id: pack_artifacts
134-
if: ${{ ( github.event_name == 'push' && github.ref == 'refs/heads/master' ) || github.event.inputs.create_release == 'true' }}
134+
if: ${{ ( github.event_name == 'push' && github.ref == 'refs/heads/dreamio' ) || github.event.inputs.create_release == 'true' }}
135135
run: |
136136
cp ggml/LICENSE ./build/bin/ggml.txt
137137
cp LICENSE ./build/bin/stable-diffusion.cpp.txt
138138
zip -j sd-${{ env.BRANCH_NAME }}-${{ steps.commit.outputs.short }}-bin-${{ steps.system-info.outputs.OS_TYPE }}-${{ steps.system-info.outputs.OS_NAME }}-${{ steps.system-info.outputs.OS_VERSION }}-${{ steps.system-info.outputs.CPU_ARCH }}.zip ./build/bin/*
139139
140140
- name: Upload artifacts
141-
if: ${{ ( github.event_name == 'push' && github.ref == 'refs/heads/master' ) || github.event.inputs.create_release == 'true' }}
141+
if: ${{ ( github.event_name == 'push' && github.ref == 'refs/heads/dreamio' ) || github.event.inputs.create_release == 'true' }}
142142
uses: actions/upload-artifact@v4
143143
with:
144144
name: sd-${{ env.BRANCH_NAME }}-${{ steps.commit.outputs.short }}-bin-${{ steps.system-info.outputs.OS_TYPE }}-${{ steps.system-info.outputs.OS_NAME }}-${{ steps.system-info.outputs.OS_VERSION }}-${{ steps.system-info.outputs.CPU_ARCH }}.zip
145145
path: |
146146
sd-${{ env.BRANCH_NAME }}-${{ steps.commit.outputs.short }}-bin-${{ steps.system-info.outputs.OS_TYPE }}-${{ steps.system-info.outputs.OS_NAME }}-${{ steps.system-info.outputs.OS_VERSION }}-${{ steps.system-info.outputs.CPU_ARCH }}.zip
147147
148148
windows-latest-cmake:
149-
runs-on: windows-2025
149+
runs-on: windows-2022
150150

151151
env:
152152
VULKAN_VERSION: 1.4.328.1
@@ -163,11 +163,15 @@ jobs:
163163
- build: "avx512"
164164
defines: "-DGGML_NATIVE=OFF -DGGML_AVX512=ON -DGGML_AVX=ON -DGGML_AVX2=ON -DSD_BUILD_SHARED_LIBS=ON"
165165
- build: "cuda12"
166-
defines: "-DSD_CUDA=ON -DSD_BUILD_SHARED_LIBS=ON -DCMAKE_CUDA_ARCHITECTURES=90;89;86;80;75"
166+
defines: "-DSD_CUDA=ON -DSD_BUILD_SHARED_LIBS=ON"
167+
- build: "cuda12-noavx"
168+
defines: "-DGGML_NATIVE=OFF -DSD_CUDA=ON -DGGML_AVX=OFF -DGGML_AVX2=OFF -DGGML_FMA=OFF -DSD_BUILD_SHARED_LIBS=ON"
167169
# - build: "rocm5.5"
168170
# defines: '-G Ninja -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DSD_HIPBLAS=ON -DCMAKE_BUILD_TYPE=Release -DAMDGPU_TARGETS="gfx1100;gfx1102;gfx1030" -DSD_BUILD_SHARED_LIBS=ON'
169171
- build: 'vulkan'
170172
defines: "-DSD_VULKAN=ON -DSD_BUILD_SHARED_LIBS=ON"
173+
- build: 'vulkan-noavx'
174+
defines: "-DGGML_NATIVE=OFF -DSD_VULKAN=ON -DGGML_AVX=OFF -DGGML_AVX2=OFF -DGGML_FMA=OFF -DSD_BUILD_SHARED_LIBS=ON"
171175
steps:
172176
- name: Clone
173177
id: checkout
@@ -177,10 +181,10 @@ jobs:
177181

178182
- name: Install cuda-toolkit
179183
id: cuda-toolkit
180-
if: ${{ matrix.build == 'cuda12' }}
184+
if: ${{ matrix.build == 'cuda12' || matrix.build == 'cuda12-noavx' }}
181185
uses: Jimver/[email protected]
182186
with:
183-
cuda: "12.6.2"
187+
cuda: "12.4.0"
184188
method: "network"
185189
sub-packages: '["nvcc", "cudart", "cublas", "cublas_dev", "thrust", "visual_studio_integration"]'
186190

@@ -199,7 +203,7 @@ jobs:
199203
version: 1.11.1
200204
- name: Install Vulkan SDK
201205
id: get_vulkan
202-
if: ${{ matrix.build == 'vulkan' }} https://sdk.lunarg.com/sdk/download/1.4.328.1/windows/vulkansdk-windows-X64-1.4.328.1.exe
206+
if: ${{ matrix.build == 'vulkan' || matrix.build == 'vulkan-noavx' }} https://sdk.lunarg.com/sdk/download/1.4.328.1/windows/vulkansdk-windows-X64-1.4.328.1.exe
203207
run: |
204208
curl.exe -o $env:RUNNER_TEMP/VulkanSDK-Installer.exe -L "https://sdk.lunarg.com/sdk/download/${env:VULKAN_VERSION}/windows/vulkansdk-windows-X64-${env:VULKAN_VERSION}.exe"
205209
& "$env:RUNNER_TEMP\VulkanSDK-Installer.exe" --accept-licenses --default-answer --confirm-command install
@@ -229,12 +233,12 @@ jobs:
229233
230234
- name: Get commit hash
231235
id: commit
232-
if: ${{ ( github.event_name == 'push' && github.ref == 'refs/heads/master' ) || github.event.inputs.create_release == 'true' }}
236+
if: ${{ ( github.event_name == 'push' && github.ref == 'refs/heads/dreamio' ) || github.event.inputs.create_release == 'true' }}
233237
uses: pr-mpt/actions-commit-hash@v2
234238

235239
- name: Pack artifacts
236240
id: pack_artifacts
237-
if: ${{ ( github.event_name == 'push' && github.ref == 'refs/heads/master' ) || github.event.inputs.create_release == 'true' }}
241+
if: ${{ ( github.event_name == 'push' && github.ref == 'refs/heads/dreamio' ) || github.event.inputs.create_release == 'true' }}
238242
run: |
239243
$filePath = ".\build\bin\Release\*"
240244
if (Test-Path $filePath) {
@@ -254,31 +258,31 @@ jobs:
254258
255259
- name: Copy and pack Cuda runtime
256260
id: pack_cuda_runtime
257-
if: ${{ matrix.build == 'cuda12' && (github.event_name == 'push' && github.ref == 'refs/heads/master' || github.event.inputs.create_release == 'true') }}
261+
if: ${{ matrix.build == 'cuda12' && (github.event_name == 'push' && github.ref == 'refs/heads/dreamio' || github.event.inputs.create_release == 'true') }}
258262
run: |
259263
echo "Cuda install location: ${{steps.cuda-toolkit.outputs.CUDA_PATH}}"
260264
$dst='.\build\bin\cudart\'
261265
robocopy "${{steps.cuda-toolkit.outputs.CUDA_PATH}}\bin" $dst cudart64_*.dll cublas64_*.dll cublasLt64_*.dll
262266
7z a cudart-sd-bin-win-cu12-x64.zip $dst\*
263267
264268
- name: Upload Cuda runtime
265-
if: ${{ matrix.build == 'cuda12' && (github.event_name == 'push' && github.ref == 'refs/heads/master' || github.event.inputs.create_release == 'true') }}
269+
if: ${{ matrix.build == 'cuda12' && (github.event_name == 'push' && github.ref == 'refs/heads/dreamio' || github.event.inputs.create_release == 'true') }}
266270
uses: actions/upload-artifact@v4
267271
with:
268272
name: sd-cudart-sd-bin-win-cu12-x64.zip
269273
path: |
270274
cudart-sd-bin-win-cu12-x64.zip
271275
272276
- name: Upload artifacts
273-
if: ${{ ( github.event_name == 'push' && github.ref == 'refs/heads/master' ) || github.event.inputs.create_release == 'true' }}
277+
if: ${{ ( github.event_name == 'push' && github.ref == 'refs/heads/dreamio' ) || github.event.inputs.create_release == 'true' }}
274278
uses: actions/upload-artifact@v4
275279
with:
276280
name: sd-${{ env.BRANCH_NAME }}-${{ steps.commit.outputs.short }}-bin-win-${{ matrix.build }}-x64.zip
277281
path: |
278282
sd-${{ env.BRANCH_NAME }}-${{ steps.commit.outputs.short }}-bin-win-${{ matrix.build }}-x64.zip
279283
280284
release:
281-
if: ${{ ( github.event_name == 'push' && github.ref == 'refs/heads/master' ) || github.event.inputs.create_release == 'true' }}
285+
if: ${{ ( github.event_name == 'push' && github.ref == 'refs/heads/dreamio' ) || github.event.inputs.create_release == 'true' }}
282286

283287
runs-on: ubuntu-latest
284288

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ test/
1212
output*.png
1313
models*
1414
*.log
15+
/.idea

examples/cli/main.cpp

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,12 +1265,22 @@ int main(int argc, const char* argv[]) {
12651265
return 1;
12661266
}
12671267
if (params.canny_preprocess) { // apply preprocessor
1268-
preprocess_canny(control_image,
1269-
0.08f,
1270-
0.08f,
1271-
0.8f,
1272-
1.0f,
1273-
false);
1268+
sd_image_t* canny_image = NULL;
1269+
sd_err_t err = preprocess_canny(control_image,
1270+
0.08f,
1271+
0.08f,
1272+
0.8f,
1273+
1.0f,
1274+
false,
1275+
&canny_image);
1276+
if (err != SD_OK) {
1277+
fprintf(stderr, "canny preprocess failed\n");
1278+
release_all_resources();
1279+
return 1;
1280+
}
1281+
free(control_image.data);
1282+
control_image = *canny_image;
1283+
free(canny_image);
12741284
}
12751285
}
12761286

@@ -1352,10 +1362,11 @@ int main(int argc, const char* argv[]) {
13521362
params.flow_shift,
13531363
};
13541364

1355-
sd_ctx_t* sd_ctx = new_sd_ctx(&sd_ctx_params);
1365+
sd_ctx_t* sd_ctx = NULL;
1366+
sd_err_t err = new_sd_ctx(&sd_ctx_params, &sd_ctx);
13561367

1357-
if (sd_ctx == NULL) {
1358-
printf("new_sd_ctx_t failed\n");
1368+
if (err != SD_OK) {
1369+
printf("new_sd_ctx_t failed with error %d\n", err);
13591370
release_all_resources();
13601371
return 1;
13611372
}
@@ -1364,8 +1375,8 @@ int main(int argc, const char* argv[]) {
13641375
params.sample_params.sample_method = sd_get_default_sample_method(sd_ctx);
13651376
}
13661377

1367-
sd_image_t* results;
1368-
int num_results = 1;
1378+
sd_image_t* results = NULL;
1379+
int num_results = 1;
13691380
if (params.mode == IMG_GEN) {
13701381
sd_img_gen_params_t img_gen_params = {
13711382
params.prompt.c_str(),
@@ -1393,7 +1404,7 @@ int main(int argc, const char* argv[]) {
13931404
params.vae_tiling_params,
13941405
};
13951406

1396-
results = generate_image(sd_ctx, &img_gen_params);
1407+
err = generate_image(sd_ctx, &img_gen_params, &results);
13971408
num_results = params.batch_count;
13981409
} else if (params.mode == VID_GEN) {
13991410
sd_vid_gen_params_t vid_gen_params = {
@@ -1415,38 +1426,42 @@ int main(int argc, const char* argv[]) {
14151426
params.vace_strength,
14161427
};
14171428

1418-
results = generate_video(sd_ctx, &vid_gen_params, &num_results);
1429+
err = generate_video(sd_ctx, &vid_gen_params, &num_results, &results);
14191430
}
14201431

1421-
if (results == NULL) {
1422-
printf("generate failed\n");
1432+
if (err != SD_OK) {
1433+
printf("generate failed with error %d\n", err);
14231434
free_sd_ctx(sd_ctx);
14241435
return 1;
14251436
}
14261437

14271438
int upscale_factor = 4; // unused for RealESRGAN_x4plus_anime_6B.pth
14281439
if (params.esrgan_path.size() > 0 && params.upscale_repeats > 0) {
1429-
upscaler_ctx_t* upscaler_ctx = new_upscaler_ctx(params.esrgan_path.c_str(),
1440+
upscaler_ctx_t* upscaler_ctx = NULL;
1441+
err = new_upscaler_ctx(params.esrgan_path.c_str(),
14301442
params.offload_params_to_cpu,
14311443
params.diffusion_conv_direct,
1432-
params.n_threads);
1444+
params.n_threads,
1445+
&upscaler_ctx);
14331446

1434-
if (upscaler_ctx == NULL) {
1435-
printf("new_upscaler_ctx failed\n");
1447+
if (err != SD_OK) {
1448+
printf("new_upscaler_ctx failed with error %d\n", err);
14361449
} else {
14371450
for (int i = 0; i < params.batch_count; i++) {
14381451
if (results[i].data == NULL) {
14391452
continue;
14401453
}
14411454
sd_image_t current_image = results[i];
14421455
for (int u = 0; u < params.upscale_repeats; ++u) {
1443-
sd_image_t upscaled_image = upscale(upscaler_ctx, current_image, upscale_factor);
1444-
if (upscaled_image.data == NULL) {
1445-
printf("upscale failed\n");
1456+
sd_image_t* upscaled_image = NULL;
1457+
err = upscale(upscaler_ctx, current_image, upscale_factor, &upscaled_image);
1458+
if (err != SD_OK) {
1459+
printf("upscale failed with error %d\n", err);
14461460
break;
14471461
}
14481462
free(current_image.data);
1449-
current_image = upscaled_image;
1463+
current_image = *upscaled_image;
1464+
free(upscaled_image);
14501465
}
14511466
results[i] = current_image; // Set the final upscaled image as the result
14521467
}

model.cpp

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2523,20 +2523,30 @@ int64_t ModelLoader::get_params_mem_size(ggml_backend_t backend, ggml_type type)
25232523
return mem_size;
25242524
}
25252525

2526-
bool convert(const char* input_path, const char* vae_path, const char* output_path, sd_type_t output_type, const char* tensor_type_rules) {
2527-
ModelLoader model_loader;
2526+
enum sd_err_t convert(const char* input_path, const char* vae_path, const char* output_path, sd_type_t output_type, const char* tensor_type_rules) {
2527+
try {
2528+
ModelLoader model_loader;
25282529

2529-
if (!model_loader.init_from_file(input_path)) {
2530-
LOG_ERROR("init model loader from file failed: '%s'", input_path);
2531-
return false;
2532-
}
2530+
if (!model_loader.init_from_file(input_path)) {
2531+
LOG_ERROR("init model loader from file failed: '%s'", input_path);
2532+
return SD_ERR_MISSING_RESOURCE;
2533+
}
25332534

2534-
if (vae_path != NULL && strlen(vae_path) > 0) {
2535-
if (!model_loader.init_from_file(vae_path, "vae.")) {
2536-
LOG_ERROR("init model loader from file failed: '%s'", vae_path);
2537-
return false;
2535+
if (vae_path != NULL && strlen(vae_path) > 0) {
2536+
if (!model_loader.init_from_file(vae_path, "vae.")) {
2537+
LOG_ERROR("init model loader from file failed: '%s'", vae_path);
2538+
return SD_ERR_MISSING_RESOURCE;
2539+
}
2540+
}
2541+
if (!model_loader.save_to_gguf_file(output_path, (ggml_type)output_type, tensor_type_rules)) {
2542+
return SD_ERR_UNKNOWN;
25382543
}
2544+
return SD_OK;
2545+
} catch (const std::runtime_error& e) {
2546+
LOG_ERROR("Error: %s", e.what());
2547+
return SD_ERR_GGML_ABORT;
2548+
} catch (...) {
2549+
LOG_ERROR("Unknown error occurred in convert");
2550+
return SD_ERR_UNKNOWN;
25392551
}
2540-
bool success = model_loader.save_to_gguf_file(output_path, (ggml_type)output_type, tensor_type_rules);
2541-
return success;
25422552
}

preprocessing.hpp

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -162,17 +162,18 @@ void threshold_hystersis(struct ggml_tensor* img, float high_threshold, float lo
162162
}
163163
}
164164

165-
bool preprocess_canny(sd_image_t img, float high_threshold, float low_threshold, float weak, float strong, bool inverse) {
166-
struct ggml_init_params params;
167-
params.mem_size = static_cast<size_t>(10 * 1024 * 1024); // 10MB
168-
params.mem_buffer = NULL;
169-
params.no_alloc = false;
170-
struct ggml_context* work_ctx = ggml_init(params);
171-
172-
if (!work_ctx) {
173-
LOG_ERROR("ggml_init() failed");
174-
return false;
175-
}
165+
enum sd_err_t preprocess_canny(sd_image_t img, float high_threshold, float low_threshold, float weak, float strong, bool inverse, sd_image_t** result_image) {
166+
try {
167+
struct ggml_init_params params;
168+
params.mem_size = static_cast<size_t>(10 * 1024 * 1024); // 10MB
169+
params.mem_buffer = NULL;
170+
params.no_alloc = false;
171+
struct ggml_context* work_ctx = ggml_init(params);
172+
173+
if (!work_ctx) {
174+
LOG_ERROR("ggml_init() failed");
175+
return SD_ERR_UNKNOWN;
176+
}
176177

177178
float kX[9] = {
178179
-1, 0, 1,
@@ -219,10 +220,19 @@ bool preprocess_canny(sd_image_t img, float high_threshold, float low_threshold,
219220
}
220221
}
221222
uint8_t* output = sd_tensor_to_image(image);
222-
free(img.data);
223-
img.data = output;
223+
*result_image = (sd_image_t*)malloc(sizeof(sd_image_t));
224+
**result_image = {img.width, img.height, img.channel, output};
224225
ggml_free(work_ctx);
225-
return true;
226+
return SD_OK;
227+
} catch (const std::runtime_error& e) {
228+
LOG_ERROR("Error: %s", e.what());
229+
*result_image = NULL;
230+
return SD_ERR_GGML_ABORT;
231+
} catch (...) {
232+
LOG_ERROR("Unknown error occurred in preprocess_canny");
233+
*result_image = NULL;
234+
return SD_ERR_UNKNOWN;
235+
}
226236
}
227237

228238
#endif // __PREPROCESSING_HPP__

0 commit comments

Comments
 (0)