Window unpartition: reverse of sam2_window_partition. Input ne = {C, ws, ws, nW*B}, output ne = {C, orig_W, orig_H, B}. Reverses the partition steps: 1. Reshape [C, ws, ws, nW*B] -> [C*ws, ws, nW_h, nW_w*B] 2. Reshape [C*ws, ws, nW_h, nW_w*B] -> [C*ws, Hp, nW_w, B] (merge local-H with nW_h) 3. Permute (0,2,1,3) -> [C*ws, nW_w, Hp, B] 4. Cont 5. Reshape [C*ws, nW_w, Hp, B] -> [C, Wp, Hp, B] 6. Cr
| 4242 | // 5. Reshape [C*ws, nW_w, Hp, B] -> [C, Wp, Hp, B] |
| 4243 | // 6. Crop to (orig_W, orig_H) |
| 4244 | static struct ggml_tensor* sam2_window_unpartition(struct ggml_context* ctx, |
| 4245 | struct ggml_tensor* x, |
| 4246 | int ws, int pad_hw[2], |
| 4247 | int orig_H, int orig_W, |
| 4248 | int B) { |
| 4249 | const int64_t C = x->ne[0]; |
| 4250 | int64_t Hp = pad_hw[0]; |
| 4251 | int64_t Wp = pad_hw[1]; |
| 4252 | int64_t nW_h = Hp / ws; |
| 4253 | int64_t nW_w = Wp / ws; |
| 4254 | |
| 4255 | // Step 1: Reshape [C, ws, ws, nW*B] -> [C*ws, ws, nW_h, nW_w*B] |
| 4256 | // Merges (C, local_W=ws) into C*ws; splits nW into (nW_h, nW_w*B). |
| 4257 | auto* r1 = ggml_reshape_4d(ctx, x, C * ws, ws, nW_h, nW_w * B); |
| 4258 | |
| 4259 | // Step 2: Reshape [C*ws, ws, nW_h, nW_w*B] -> [C*ws, Hp, nW_w, B] |
| 4260 | // Merges (local_H=ws, nW_h) into Hp=ws*nW_h; splits nW_w*B into (nW_w, B). |
| 4261 | auto* r2 = ggml_reshape_4d(ctx, r1, C * ws, Hp, nW_w, B); |
| 4262 | |
| 4263 | // Step 3: Permute to [C*ws, nW_w, Hp, B] |
| 4264 | auto* p1 = ggml_permute(ctx, r2, 0, 2, 1, 3); |
| 4265 | |
| 4266 | // Step 4: Cont |
| 4267 | auto* c1 = ggml_cont(ctx, p1); |
| 4268 | |
| 4269 | // Step 5: Reshape [C*ws, nW_w, Hp, B] -> [C, Wp, Hp, B] |
| 4270 | // Splits (C*ws) with nW_w to recover (C, Wp=ws*nW_w). |
| 4271 | auto* out = ggml_reshape_4d(ctx, c1, C, Wp, Hp, B); |
| 4272 | |
| 4273 | // Step 6: Crop if padded |
| 4274 | if (Hp != orig_H || Wp != orig_W) { |
| 4275 | out = ggml_view_4d(ctx, out, C, orig_W, orig_H, B, |
| 4276 | out->nb[1], out->nb[2], out->nb[3], 0); |
| 4277 | out = ggml_cont(ctx, out); |
| 4278 | } |
| 4279 | |
| 4280 | return out; |
| 4281 | } |
| 4282 | |
| 4283 | // MaxPool2d with kernel=2, stride=2 on spatial dims (ne[1]=W, ne[2]=H). |
| 4284 | // Input: [C, W, H, B] -> output: [C, W/2, H/2, B] |
no outgoing calls
no test coverage detected