MCPcopy Create free account
hub / github.com/PABannier/sam3.cpp / sam3_load_model

Function sam3_load_model

sam3.cpp:3204–3348  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3202*****************************************************************************/
3203
3204std::shared_ptr<sam3_model> sam3_load_model(const sam3_params& params) {
3205 fprintf(stderr, "%s: loading model from '%s'\n", __func__, params.model_path.c_str());
3206
3207 std::ifstream fin(params.model_path, std::ios::binary);
3208 if (!fin) {
3209 fprintf(stderr, "%s: failed to open '%s'\n", __func__, params.model_path.c_str());
3210 return nullptr;
3211 }
3212
3213 // ── Read + validate header ───────────────────────────────────────────
3214 uint32_t magic;
3215 int32_t version, ftype, n_tensors;
3216 fin.read(reinterpret_cast<char*>(&magic), 4);
3217 fin.read(reinterpret_cast<char*>(&version), 4);
3218 fin.read(reinterpret_cast<char*>(&ftype), 4);
3219 fin.read(reinterpret_cast<char*>(&n_tensors), 4);
3220
3221 bool is_sam2 = false;
3222 if (magic == SAM3_MAGIC) {
3223 if (version != SAM3_FILE_VERSION) {
3224 fprintf(stderr, "%s: unsupported SAM3 version: %d (expected %d)\n",
3225 __func__, version, SAM3_FILE_VERSION);
3226 return nullptr;
3227 }
3228 } else if (magic == SAM2_MAGIC) {
3229 if (version != SAM2_VERSION) {
3230 fprintf(stderr, "%s: unsupported SAM2 version: %d (expected %d)\n",
3231 __func__, version, SAM2_VERSION);
3232 return nullptr;
3233 }
3234 is_sam2 = true;
3235 } else {
3236 fprintf(stderr, "%s: unknown magic: 0x%08x (expected sam3=0x%08x or sam2=0x%08x)\n",
3237 __func__, magic, SAM3_MAGIC, SAM2_MAGIC);
3238 return nullptr;
3239 }
3240 fprintf(stderr, "%s: %s format v%d, ftype %d, %d tensors\n",
3241 __func__, is_sam2 ? "SAM2" : "SAM3", version, ftype, n_tensors);
3242
3243 auto model = std::make_shared<sam3_model>();
3244 {
3245 ggml_type wtype;
3246 switch (ftype) {
3247 case 0: wtype = GGML_TYPE_F32; break;
3248 case 1: wtype = GGML_TYPE_F16; break;
3249 case 2: wtype = GGML_TYPE_Q4_0; break;
3250 case 3: wtype = GGML_TYPE_Q4_1; break;
3251 case 8: wtype = GGML_TYPE_Q8_0; break;
3252 default:
3253 fprintf(stderr, "%s: unsupported ftype: %d\n", __func__, ftype);
3254 return nullptr;
3255 }
3256 model->weight_type = wtype;
3257 }
3258
3259 // ── Read hyperparameters ─────────────────────────────────────────────
3260 if (is_sam2) {
3261 if (!sam2_load_hparams(fin, model->hparams)) {

Callers 15

mainFunction · 0.85
mainFunction · 0.85
mainFunction · 0.85
test_encode_imageFunction · 0.85
mainFunction · 0.85
run_backendFunction · 0.85
mainFunction · 0.85
mainFunction · 0.85
test_encode_imageFunction · 0.85
mainFunction · 0.85
run_backendFunction · 0.85

Calls 12

sam2_load_hparamsFunction · 0.85
edgetam_print_hparamsFunction · 0.85
sam2_print_hparamsFunction · 0.85
sam3_load_hparamsFunction · 0.85
sam3_print_hparamsFunction · 0.85
edgetam_register_tensorsFunction · 0.85
sam2_register_tensorsFunction · 0.85
sam3_register_tensorsFunction · 0.85
sam3_load_tensorsFunction · 0.85
is_edgetamMethod · 0.80

Tested by 15

mainFunction · 0.68
mainFunction · 0.68
mainFunction · 0.68
test_encode_imageFunction · 0.68
mainFunction · 0.68
run_backendFunction · 0.68
mainFunction · 0.68
mainFunction · 0.68
test_encode_imageFunction · 0.68
mainFunction · 0.68
run_backendFunction · 0.68