Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11,583 changes: 4,111 additions & 7,472 deletions sam3.cpp

Large diffs are not rendered by default.

219 changes: 0 additions & 219 deletions sam3.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,38 +101,6 @@ struct sam3_params {
int encode_img_size = 0; // 0 = model default; override input resolution
};

struct sam3_tensor_info {
int64_t ne[4] = {0, 0, 0, 0};
uint64_t nb[4] = {0, 0, 0, 0};
int type = 0;
int op = 0;
bool is_contiguous = false;
};

enum sam3_vit_block_stage {
SAM3_VIT_BLOCK_STAGE_NORM1 = 0,
SAM3_VIT_BLOCK_STAGE_WINDOW_PART,
SAM3_VIT_BLOCK_STAGE_QKV_PROJ,
SAM3_VIT_BLOCK_STAGE_ATTN_CORE,
SAM3_VIT_BLOCK_STAGE_ATTN_PROJ,
SAM3_VIT_BLOCK_STAGE_WINDOW_UNPART,
SAM3_VIT_BLOCK_STAGE_NORM2,
SAM3_VIT_BLOCK_STAGE_MLP_FC1,
SAM3_VIT_BLOCK_STAGE_MLP_GELU,
SAM3_VIT_BLOCK_STAGE_MLP_FC2,
SAM3_VIT_BLOCK_STAGE_MLP,
};

enum sam3_vit_prefix_stage {
SAM3_VIT_PREFIX_STAGE_PATCH_EMBED = 0,
SAM3_VIT_PREFIX_STAGE_PATCH_IM2COL,
SAM3_VIT_PREFIX_STAGE_PATCH_MULMAT_RAW,
SAM3_VIT_PREFIX_STAGE_PATCH_MULMAT,
SAM3_VIT_PREFIX_STAGE_POS_ADD,
SAM3_VIT_PREFIX_STAGE_LN_PRE_NORM,
SAM3_VIT_PREFIX_STAGE_LN_PRE,
};

struct sam3_pcs_params {
std::string text_prompt;
std::vector<sam3_box> pos_exemplars;
Expand Down Expand Up @@ -308,193 +276,6 @@ bool sam3_save_mask(const sam3_mask & mask, const std::string & path)
sam3_image sam3_decode_video_frame(const std::string & video_path, int frame_index);
sam3_video_info sam3_get_video_info(const std::string & video_path);

/*****************************************************************************
** Test and Debug API
**
** Standalone tokenizer, intermediate tensor dumps, and debug utilities.
** These functions are intended for testing and development only.
*****************************************************************************/

bool sam3_test_load_tokenizer(const std::string & model_path);
std::vector<int32_t> sam3_test_tokenize(const std::string & text);

/*
** Run the text encoder on fixed token IDs and dump standard intermediate
** tensors to <output_dir>/<tensor_name>.{bin,shape}.
*/
bool sam3_test_dump_text_encoder(const sam3_model & model,
const std::vector<int32_t> & token_ids,
const std::string & output_dir,
int n_threads = 4);

/*
** Run the full phase 5 detector path (fusion encoder + DETR decoder +
** dot-product scoring + segmentation head) on an already-encoded image
** and dump intermediate tensors.
*/
bool sam3_test_dump_phase5(const sam3_model & model,
const sam3_state & state,
const std::vector<int32_t> & token_ids,
const std::string & output_dir,
int n_threads = 4);

/*
** Run the phase 5 detector from pre-dumped inputs instead of re-running
** the image/text encoders. Isolates detector numerics from earlier phases.
*/
bool sam3_test_dump_phase5_from_ref_inputs(const sam3_model & model,
const std::vector<int32_t> & token_ids,
const std::string & prephase_ref_dir,
const std::string & phase5_ref_dir,
const std::string & output_dir,
int n_threads = 4);

/*
** Run the phase 6 prompt encoder + SAM decoder on an already-encoded
** tracker image state and dump intermediate tensors.
*/
bool sam3_test_dump_phase6(const sam3_model & model,
const sam3_state & state,
const sam3_pvs_params & params,
const std::string & output_dir,
int n_threads = 4);

/*
** Run the phase 6 prompt encoder + SAM decoder from pre-dumped phase 3
** tracker features. Isolates phase 6 numerics from earlier phases.
*/
bool sam3_test_dump_phase6_from_ref_inputs(const sam3_model & model,
const std::string & prephase_ref_dir,
const sam3_pvs_params & params,
const std::string & output_dir,
int n_threads = 4);

/*
** Run the phase 7 tracker subgraph from pre-dumped case inputs and dump
** intermediate tensors. Case directory produced by dump_phase7_reference.py.
*/
bool sam3_test_dump_phase7_from_ref_inputs(const sam3_model & model,
const std::string & case_ref_dir,
const std::string & output_dir,
int n_threads = 4);

/*
** Run the geometry encoder from pre-computed backbone features and dump
** intermediate tensors. Tests exemplar box coordinate encoding against
** Python reference.
*/
bool sam3_test_dump_geom_enc(const sam3_model & model,
const std::string & prephase_ref_dir,
const sam3_pcs_params & params,
const std::string & output_dir,
int n_threads = 4);

/*
** Run ONLY the fusion encoder (6 layers) from pre-dumped inputs (image
** features, pos encoding, prompt tokens, attn bias). Dumps per-layer
** outputs for isolated fenc debugging.
*/
bool sam3_test_fenc_only(const sam3_model & model,
const std::string & ref_dir,
const std::string & output_dir,
int n_threads = 4);

/*
** ── Debug ────────────────────────────────────────────────────────────────
*/

/* Dump a named state tensor to a binary file for verification. */
bool sam3_dump_state_tensor(const sam3_state & state,
const std::string & tensor_name,
const std::string & output_path);

/* Query metadata for a named state tensor without dumping its payload. */
bool sam3_get_state_tensor_info(const sam3_state & state,
const std::string & tensor_name,
sam3_tensor_info & info);

/* Dump a named model tensor (weights/constants) to a binary file. */
bool sam3_dump_model_tensor(const sam3_model & model,
const std::string & tensor_name,
const std::string & output_path);

/* Query metadata for a named model tensor. */
bool sam3_get_model_tensor_info(const sam3_model & model,
const std::string & tensor_name,
sam3_tensor_info & info);

/*
** Encode an image from pre-preprocessed float data (CHW layout, already
** resized and normalized). Bypasses C++ preprocessing so that numerical
** comparisons against the Python reference are not polluted by resize
** implementation differences.
*/
bool sam3_encode_image_from_preprocessed(sam3_state & state,
const sam3_model & model,
const float * chw_data,
int img_size);

/*
** Test-only: run ONLY the ViT encoder from preprocessed float data and keep
** only the requested intermediate tensors alive for dumping/comparison.
*/
bool sam3_encode_vit_from_preprocessed_selective(sam3_state & state,
const sam3_model & model,
const float * chw_data,
int img_size,
const std::vector<std::string> & output_tensors);

/*
** Test-only: run the exact ViT prefix up to the tensor entering block 0
** (patch embed + pos embed + ln_pre).
*/
bool sam3_test_run_vit_block0_input(const sam3_model & model,
const float * chw_data,
int img_size,
std::vector<float> & output_data,
int64_t output_ne[4],
int n_threads = 4);

/*
** Test-only: run an exact ViT prefix sub-stage on the real model tensors using
** the model's backend. PATCH_EMBED expects image input [W,H,3,1]. Later stages
** expect feature input [E,W,H,1] in ggml 4D layout.
*/
bool sam3_test_run_vit_prefix_stage(const sam3_model & model,
sam3_vit_prefix_stage stage,
const float * input_data,
const int64_t input_ne[4],
std::vector<float> & output_data,
int64_t output_ne[4],
int n_threads = 4);
bool sam3_test_run_patch_mulmat_host_ref(const sam3_model & model,
const float * input_data,
const int64_t input_ne[4],
bool use_double_accum,
std::vector<float> & output_data,
int64_t output_ne[4]);
bool sam3_test_run_vit_block_linear_host_ref(const sam3_model & model,
int block_idx,
sam3_vit_block_stage stage,
const float * input_data,
const int64_t input_ne[4],
bool use_double_accum,
std::vector<float> & output_data,
int64_t output_ne[4]);

/*
** Test-only: run an exact ViT block sub-stage on the real model tensors using
** the model's backend. The input is always provided as F32 in ggml 4D layout.
*/
bool sam3_test_run_vit_block_stage(const sam3_model & model,
int block_idx,
sam3_vit_block_stage stage,
const float * input_data,
const int64_t input_ne[4],
std::vector<float> & output_data,
int64_t output_ne[4],
int n_threads = 4);

/*
** ── Profiling ───────────────────────────────────────────────────────────
*/
Expand Down
129 changes: 0 additions & 129 deletions tests/CMakeLists.txt

This file was deleted.

Loading
Loading