| 286 | |
| 287 | |
| 288 | py::array_t<int> uniform_quantize(py::array_t<int> seg_idx, py::array_t<float> residual, float acc) { |
| 289 | // input array |
| 290 | py::buffer_info si_buf = seg_idx.request(); |
| 291 | int *si_ptr = (int *) si_buf.ptr; |
| 292 | py::buffer_info ri_buf = residual.request(); |
| 293 | float *ri_ptr = (float *) ri_buf.ptr; |
| 294 | int h = si_buf.shape[0]; |
| 295 | int w = si_buf.shape[1]; |
| 296 | |
| 297 | int cluster_num = 0; |
| 298 | for (int h_i = 0; h_i < h; h_i++) |
| 299 | for (int w_i = 0; w_i < w; w_i++) |
| 300 | if (si_ptr[h_i * w + w_i] > cluster_num) |
| 301 | cluster_num = si_ptr[h_i * w + w_i]; |
| 302 | cluster_num += 1; |
| 303 | |
| 304 | vector< vector<int> > quantized_residual; |
| 305 | for (int i = 0; i < cluster_num; i++){ |
| 306 | vector<int> cur_qr; |
| 307 | quantized_residual.push_back(cur_qr); |
| 308 | } |
| 309 | |
| 310 | int total_num = 0; |
| 311 | for (int h_i = 0; h_i < h; h_i++) { |
| 312 | for (int w_i = 0; w_i < w; w_i++){ |
| 313 | int idx = si_ptr[h_i * w + w_i]; |
| 314 | if (idx != 1){ |
| 315 | quantized_residual[idx].push_back(round(ri_ptr[h_i * w + w_i] / acc)); |
| 316 | total_num++; |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | // return array |
| 322 | auto quantized_residual_collect = py::array_t<int>(total_num); |
| 323 | py::buffer_info qr_buf = quantized_residual_collect.request(); |
| 324 | int *qr_ptr = (int *) qr_buf.ptr; |
| 325 | |
| 326 | int k = 0; |
| 327 | for (int i = 0; i < cluster_num; i++) |
| 328 | for (std::vector<int>::iterator it = quantized_residual[i].begin() ; it != quantized_residual[i].end(); it++){ |
| 329 | qr_ptr[k] = *it; |
| 330 | k++; |
| 331 | } |
| 332 | |
| 333 | return quantized_residual_collect; |
| 334 | } |
| 335 | |
| 336 | |
| 337 | std::tuple<py::array_t<int>, py::array_t<int>> nonuniform_quantize(py::array_t<int> seg_idx, |
nothing calls this directly
no outgoing calls
no test coverage detected