Skip to content

Commit cba0340

Browse files
Refactored error handling for hyperparameter validation in clip.cpp
Removed the try-catch block and throw statements Added validation checks for all hyperparameters, ensuring they are not set to invalid or zero values. Used fprintf to log error messages when invalid hyperparameters are encountered.
1 parent 28d1c45 commit cba0340

File tree

1 file changed

+3
-6
lines changed

1 file changed

+3
-6
lines changed

examples/llava/clip.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,7 +1270,6 @@ struct clip_ctx * clip_model_load(const char * fname, const int verbosity = 1) {
12701270
// load vision model
12711271
auto & vision_model = new_clip->vision_model;
12721272
auto & hparams = vision_model.hparams;
1273-
try{
12741273
hparams.hidden_size = get_u32(ctx, format(KEY_N_EMBD, "vision"));
12751274
hparams.n_head = get_u32(ctx, format(KEY_N_HEAD, "vision"));
12761275
hparams.n_intermediate = get_u32(ctx, format(KEY_N_FF, "vision"));
@@ -1279,12 +1278,10 @@ struct clip_ctx * clip_model_load(const char * fname, const int verbosity = 1) {
12791278
hparams.patch_size = get_u32(ctx, KEY_PATCH_SIZE);
12801279
hparams.projection_dim = get_u32(ctx, format(KEY_PROJ_DIM, "vision"));
12811280
hparams.eps = get_f32(ctx, format(KEY_LAYER_NORM_EPS, "vision"));
1282-
if (hparams.hidden_size == 0 || hparams.n_head == 0 || hparams.n_layer == 0 || hparams.n_intermediate || hparams.image_size == 0 || hparams.patch_size || hparams.projection_dim || hparams.eps) {
1283-
throw std::invalid_argument("Invalid hyperparameter values");
1281+
if (hparams.hidden_size == 0 || hparams.n_head == 0 || hparams.n_layer == 0 || hparams.n_intermediate == 0 || hparams.image_size == 0 || hparams.patch_size == 0 || hparams.projection_dim == 0 || hparams.eps == 0) {
1282+
fprintf(stderr, "Error: Invalid hyperparameter values\n");
1283+
return false;
12841284
}
1285-
} catch (const std::exception& e) {
1286-
fprintf(stderr, "Error while loading hyperparameters: %s\n", e.what());
1287-
return false;
12881285
}
12891286
try {
12901287
int idx = get_key_idx(ctx, KEY_IMAGE_GRID_PINPOINTS);

0 commit comments

Comments
 (0)