pad_ref.c 19.0 KB
Newer Older
B
BUG1989 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * License); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * AS IS BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

/*
K
kalcohol 已提交
21
 * Copyright (c) 2021, OPEN AI LAB
B
BUG1989 已提交
22 23 24 25 26 27
 * Author: sqfu@openailab.com
 * Author: qtang@openailab.com (update 20200611)
 */

#include "pad_param.h"

K
kalcohol 已提交
28 29 30 31 32 33 34 35 36
#include "graph/tensor.h"
#include "graph/node.h"
#include "graph/graph.h"
#include "utility/sys_port.h"
#include "utility/float.h"
#include "utility/log.h"
#include "device/cpu/cpu_node.h"
#include "device/cpu/cpu_graph.h"
#include "device/cpu/cpu_module.h"
37
#include "memory.h"
K
kalcohol 已提交
38 39
#include <math.h>

B
BUG1989 已提交
40 41 42 43 44 45 46 47 48 49
static int init_node(struct node_ops* node_ops, struct exec_node* exec_node, struct exec_graph* exec_graph)
{
    return 0;
}

static int release_node(struct node_ops* node_ops, struct exec_node* exec_node, struct exec_graph* exec_graph)
{
    return 0;
}

50
static void ref_pad_fp32(float* input, float* output, int in_h, int in_w, int out_h, int out_w, int top, int left, float v, int mode)
B
BUG1989 已提交
51 52 53 54 55
{
    float* ptr = input;
    float* outptr = output;
    int y = 0;
    // fill top
56 57

    if (0 == mode)
B
BUG1989 已提交
58
    {
59
        for (; y < top; y++)
B
BUG1989 已提交
60
        {
61 62 63 64 65 66
            int x = 0;
            for (; x < out_w; x++)
            {
                outptr[x] = v;
            }
            outptr += out_w;
B
BUG1989 已提交
67
        }
68 69
        // fill center
        for (; y < (top + in_h); y++)
B
BUG1989 已提交
70
        {
71 72 73 74 75
            int x = 0;
            for (; x < left; x++)
            {
                outptr[x] = v;
            }
76
            for (x = 0; x < in_w; x++)
77
            {
78
                outptr[left + x] = ptr[x];
79
            }
80
            for (x = out_w - left; x < out_w; x++)
81 82 83 84 85
            {
                outptr[x] = v;
            }
            ptr += in_w;
            outptr += out_w;
B
BUG1989 已提交
86
        }
87 88
        // fill bottom
        for (; y < out_h; y++)
B
BUG1989 已提交
89
        {
90 91
            int x = 0;
            for (; x < out_w; x++)
B
BUG1989 已提交
92
            {
93
                outptr[x] = v;
B
BUG1989 已提交
94
            }
95
            outptr += out_w;
B
BUG1989 已提交
96
        }
97 98 99 100
    }
    else if (1 == mode)
    {
        for (; y < top; y++)
B
BUG1989 已提交
101
        {
102 103 104
            int x = 0;
            // padding left
            for (; x < left; x++)
B
BUG1989 已提交
105
            {
106
                outptr[x] = ptr[0];
B
BUG1989 已提交
107
            }
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

            // padding center
            if (in_w < 12)
            {
                for (; x < (left + in_w); x++)
                {
                    outptr[x] = ptr[x - left];
                }
            }
            else
            {
                memcpy(outptr + left, ptr, sizeof(float) * in_w);
                x = x + in_w;
            }
            //pading right
            for (; x < out_w; x++)
            {
                outptr[x] = ptr[in_w - 1];
            }

            outptr += out_w;
B
BUG1989 已提交
129
        }
130 131
        // fill center
        for (; y < (top + in_h); y++)
B
BUG1989 已提交
132
        {
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
            int x = 0;

            // padding left
            for (; x < left; x++)
            {
                outptr[x] = ptr[0];
            }

            // padding center
            if (in_w < 12)
            {
                for (; x < (left + in_w); x++)
                {
                    outptr[x] = ptr[x - left];
                }
            }
            else
            {
                memcpy(outptr + left, ptr, sizeof(float) * in_w);
                x += in_w;
            }

            // paddding right
            for (; x < out_w; x++)
            {
                outptr[x] = ptr[in_w - 1];
            }
            ptr += in_w;
            outptr += out_w;
        }
        // fill bottom
        for (; y < out_h; y++)
        {
            int x = 0;
            // padding left
            for (; x < left; x++)
            {
                outptr[x] = ptr[0];
            }

            // padding center
            if (in_w < 12)
            {
                for (; x < (left + in_w); x++)
                {
                    outptr[x] = ptr[x - left];
                }
            }
            else
            {
                memcpy(outptr + left, ptr, sizeof(float) * in_w);
                x = x + in_w;
            }
            //pading right
            for (; x < out_w; x++)
            {
                outptr[x] = ptr[in_w - 1];
            }
            outptr += out_w;
B
BUG1989 已提交
192 193
        }
    }
194
    else if (2 == mode)
B
BUG1989 已提交
195
    {
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
        ptr += top * in_w;
        for (; y < top; y++)
        {
            int x = 0;
            // padding left
            for (; x < left; x++)
            {
                outptr[x] = ptr[left - x];
            }

            // padding center
            if (in_w < 12)
            {
                for (; x < (left + in_w); x++)
                {
                    outptr[x] = ptr[x - left];
                }
            }
            else
            {
                memcpy(outptr + left, ptr, sizeof(float) * in_w);
                x = x + in_w;
            }

            //pading right
            for (; x < out_w; x++)
            {
                outptr[x] = ptr[in_w - (x - in_w - left) - 2];
            }
            outptr += out_w;
            ptr -= in_w;
        }

        // fill center
        for (; y < (top + in_h); y++)
B
BUG1989 已提交
231
        {
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
            int x = 0;
            // padding left
            for (; x < left; x++)
            {
                outptr[x] = ptr[left - x];
            }

            // padding center
            if (in_w < 12)
            {
                for (; x < (left + in_w); x++)
                {
                    outptr[x] = ptr[x - left];
                }
            }
            else
            {
                memcpy(outptr + left, ptr, sizeof(float) * in_w);
                x = x + in_w;
            }

            //pading right
            for (; x < out_w; x++)
            {
                outptr[x] = ptr[in_w - (x - in_w - left) - 2];
            }
            outptr += out_w;
            ptr += in_w;
        }
        ptr -= 2 * in_w;
        // fill bottom
        for (; y < out_h; y++)
        {
            int x = 0;
            // padding left
            for (; x < left; x++)
            {
                outptr[x] = ptr[left - x];
            }

            // padding center
            if (in_w < 12)
            {
                for (; x < (left + in_w); x++)
                {
                    outptr[x] = ptr[x - left];
                }
            }
            else
            {
                memcpy(outptr + left, ptr, sizeof(float) * in_w);
                x = x + in_w;
            }

            //pading right
            for (; x < out_w; x++)
            {
                outptr[x] = ptr[in_w - (x - in_w - left) - 2];
            }
            outptr += out_w;
            ptr -= in_w;
B
BUG1989 已提交
293 294 295 296
        }
    }
}

297
static void ref_pad_uint8(uint8_t* input, uint8_t* output, int in_h, int in_w, int out_h, int out_w, int top, int left, float v, int mode)
Y
YoLucky 已提交
298 299 300 301 302 303
{
    uint8_t* ptr = input;
    uint8_t* outptr = output;

    int y = 0;
    // fill top
304 305

    if (0 == mode)
Y
YoLucky 已提交
306
    {
307
        for (; y < top; y++)
Y
YoLucky 已提交
308
        {
309 310 311 312 313 314
            int x = 0;
            for (; x < out_w; x++)
            {
                outptr[x] = v;
            }
            outptr += out_w;
Y
YoLucky 已提交
315
        }
316 317
        // fill center
        for (; y < (top + in_h); y++)
Y
YoLucky 已提交
318
        {
319 320 321 322 323
            int x = 0;
            for (; x < left; x++)
            {
                outptr[x] = v;
            }
324
            for (x = 0; x < in_w; x++)
325
            {
326
                outptr[left + x] = ptr[x];
327
            }
328
            for (x = out_w - left; x < out_w; x++)
329 330 331 332 333
            {
                outptr[x] = v;
            }
            ptr += in_w;
            outptr += out_w;
Y
YoLucky 已提交
334
        }
335 336
        // fill bottom
        for (; y < out_h; y++)
Y
YoLucky 已提交
337
        {
338 339
            int x = 0;
            for (; x < out_w; x++)
Y
YoLucky 已提交
340
            {
341
                outptr[x] = v;
Y
YoLucky 已提交
342
            }
343
            outptr += out_w;
Y
YoLucky 已提交
344
        }
345 346 347 348
    }
    else if (1 == mode)
    {
        for (; y < top; y++)
Y
YoLucky 已提交
349
        {
350 351 352
            int x = 0;
            // padding left
            for (; x < left; x++)
Y
YoLucky 已提交
353
            {
354
                outptr[x] = ptr[0];
Y
YoLucky 已提交
355
            }
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376

            // padding center
            if (in_w < 12)
            {
                for (; x < (left + in_w); x++)
                {
                    outptr[x] = ptr[x - left];
                }
            }
            else
            {
                memcpy(outptr + left, ptr, sizeof(float) * in_w);
                x = x + in_w;
            }
            //pading right
            for (; x < out_w; x++)
            {
                outptr[x] = ptr[in_w - 1];
            }

            outptr += out_w;
Y
YoLucky 已提交
377
        }
378 379
        // fill center
        for (; y < (top + in_h); y++)
Y
YoLucky 已提交
380
        {
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
            int x = 0;

            // padding left
            for (; x < left; x++)
            {
                outptr[x] = ptr[0];
            }

            // padding center
            if (in_w < 12)
            {
                for (; x < (left + in_w); x++)
                {
                    outptr[x] = ptr[x - left];
                }
            }
            else
            {
                memcpy(outptr + left, ptr, sizeof(float) * in_w);
                x += in_w;
            }

            // paddding right
            for (; x < out_w; x++)
            {
                outptr[x] = ptr[in_w - 1];
            }
            ptr += in_w;
            outptr += out_w;
        }
        // fill bottom
        for (; y < out_h; y++)
        {
            int x = 0;
            // padding left
            for (; x < left; x++)
            {
                outptr[x] = ptr[0];
            }

            // padding center
            if (in_w < 12)
            {
                for (; x < (left + in_w); x++)
                {
                    outptr[x] = ptr[x - left];
                }
            }
            else
            {
                memcpy(outptr + left, ptr, sizeof(float) * in_w);
                x = x + in_w;
            }
            //pading right
            for (; x < out_w; x++)
            {
                outptr[x] = ptr[in_w - 1];
            }
            outptr += out_w;
Y
YoLucky 已提交
440 441
        }
    }
442
    else if (2 == mode)
Y
YoLucky 已提交
443
    {
444 445
        ptr += top * in_w;
        for (; y < top; y++)
Y
YoLucky 已提交
446
        {
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
            int x = 0;
            // padding left
            for (; x < left; x++)
            {
                outptr[x] = ptr[left - x];
            }

            // padding center
            if (in_w < 12)
            {
                for (; x < (left + in_w); x++)
                {
                    outptr[x] = ptr[x - left];
                }
            }
            else
            {
                memcpy(outptr + left, ptr, sizeof(float) * in_w);
                x = x + in_w;
            }

            //pading right
            for (; x < out_w; x++)
            {
                outptr[x] = ptr[in_w - (x - in_w - left) - 2];
            }
            outptr += out_w;
            ptr -= in_w;
        }

        // fill center
        for (; y < (top + in_h); y++)
        {
            int x = 0;
            // padding left
            for (; x < left; x++)
            {
                outptr[x] = ptr[left - x];
            }

            // padding center
            if (in_w < 12)
            {
                for (; x < (left + in_w); x++)
                {
                    outptr[x] = ptr[x - left];
                }
            }
            else
            {
                memcpy(outptr + left, ptr, sizeof(float) * in_w);
                x = x + in_w;
            }

            //pading right
            for (; x < out_w; x++)
            {
                outptr[x] = ptr[in_w - (x - in_w - left) - 2];
            }
            outptr += out_w;
            ptr += in_w;
        }
        ptr -= 2 * in_w;
        // fill bottom
        for (; y < out_h; y++)
        {
            int x = 0;
            // padding left
            for (; x < left; x++)
            {
                outptr[x] = ptr[left - x];
            }

            // padding center
            if (in_w < 12)
            {
                for (; x < (left + in_w); x++)
                {
                    outptr[x] = ptr[x - left];
                }
            }
            else
            {
                memcpy(outptr + left, ptr, sizeof(float) * in_w);
                x = x + in_w;
            }

            //pading right
            for (; x < out_w; x++)
            {
                outptr[x] = ptr[in_w - (x - in_w - left) - 2];
            }
            outptr += out_w;
            ptr -= in_w;
Y
YoLucky 已提交
541 542 543 544
        }
    }
}

B
BUG1989 已提交
545 546
static int run(struct node_ops* node_ops, struct exec_node* exec_node, struct exec_graph* exec_graph)
{
K
kalcohol 已提交
547 548 549 550
    struct node* ir_node = exec_node->ir_node;
    struct graph* ir_graph = ir_node->graph;
    struct tensor* input_tensor = get_ir_graph_tensor(ir_graph, ir_node->input_tensors[0]);
    struct tensor* output_tensor = get_ir_graph_tensor(ir_graph, ir_node->output_tensors[0]);
B
BUG1989 已提交
551

N
nihui 已提交
552
    struct pad_param* param = (struct pad_param*)ir_node->op.param_mem;
B
BUG1989 已提交
553 554

    int batch = input_tensor->dims[0];
555
    int in_c = input_tensor->dims[1];
B
BUG1989 已提交
556 557 558
    int in_h = input_tensor->dims[2];
    int in_w = input_tensor->dims[3];
    int in_cstep = in_h * in_w;
559
    int in_size = in_c * in_h * in_w;
B
BUG1989 已提交
560 561 562

    int out_h = output_tensor->dims[2];
    int out_w = output_tensor->dims[3];
563 564
    int out_c = output_tensor->dims[1];

B
BUG1989 已提交
565
    int out_cstep = out_h * out_w;
566
    int out_size = out_c * out_h * out_w;
B
BUG1989 已提交
567 568 569 570 571

    int pad_top = param->pad_2_h;
    int pad_bottom = param->pad_2_w;
    int pad_left = param->pad_3_h;
    int pad_right = param->pad_3_w;
572 573 574 575 576 577 578 579
    int pad_front = param->pad_1_h;
    int pad_back = param->pad_1_w;

    if ((param->mode != 0) && (param->mode != 1) && (param->mode != 2))
    {
        TLOG_ERR("another mode dose not support, pad mode value %d\n", param->mode);
        return -1;
    }
B
BUG1989 已提交
580

581
    for (int n = 0; n < batch; n++)
B
BUG1989 已提交
582
    {
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
        // padding wh
        for (int c = 0; c < in_c; c++)
        {
            if (input_tensor->data_type == TENGINE_DT_FP32)
            {
                float* input_data = (float*)input_tensor->data + n * in_size + c * in_cstep;
                float* output_data = (float*)output_tensor->data + n * out_size + (c + pad_front) * out_cstep;
                ref_pad_fp32(input_data, output_data, in_h, in_w, out_h, out_w, pad_top, pad_left, param->value, param->mode);
            }
            else if (input_tensor->data_type == TENGINE_DT_UINT8)
            {
                uint8_t* input_data = (uint8_t*)input_tensor->data + n * in_size + c * in_cstep;
                uint8_t* output_data = (uint8_t*)output_tensor->data + n * out_size + (c + pad_front) * out_cstep;
                ref_pad_uint8(input_data, output_data, in_h, in_w, out_h, out_w, pad_top, pad_left, param->value, param->mode);
            }
        }

        // padding channel : front
        for (int c = 0; c < pad_front; c++)
        {
            if (0 == param->mode)
            {
                if (input_tensor->data_type == TENGINE_DT_FP32)
                {
                    float* output_data = (float*)output_tensor->data + n * out_size + c * out_cstep;
                    memset(output_data, param->value, sizeof(float) * out_cstep);
                }
                else if (input_tensor->data_type == TENGINE_DT_UINT8)
                {
                    uint8_t* output_data = (uint8_t*)output_tensor->data + n * out_size + c * out_cstep;
                    memset(output_data, param->value, sizeof(uint8_t) * out_cstep);
                }
            }
            else
            {
                if (input_tensor->data_type == TENGINE_DT_FP32)
                {
                    float* output_data = (float*)output_tensor->data + n * out_size + c * out_cstep;
                    float* copy_data = (float*)output_tensor->data + n * out_size + pad_front * out_cstep;
                    memcpy(output_data, copy_data, sizeof(float) * out_cstep);
                }
                else if (input_tensor->data_type == TENGINE_DT_UINT8)
                {
                    uint8_t* output_data = (uint8_t*)output_tensor->data + n * out_size + c * out_cstep;
                    uint8_t* copy_data = (uint8_t*)output_tensor->data + n * out_size + pad_front * out_cstep;
                    memcpy(output_data, copy_data, sizeof(uint8_t) * out_cstep);
                }
            }
        }

        // padding channel : back
        for (int c = pad_front + in_c; c < out_c; c++)
B
BUG1989 已提交
635
        {
636
            if (0 == param->mode)
B
BUG1989 已提交
637
            {
Y
YoLucky 已提交
638 639
                if (input_tensor->data_type == TENGINE_DT_FP32)
                {
N
nihui 已提交
640
                    float* output_data = (float*)output_tensor->data + n * out_size + c * out_cstep;
641
                    memset(output_data, param->value, sizeof(float) * out_cstep);
Y
YoLucky 已提交
642
                }
N
nihui 已提交
643
                else if (input_tensor->data_type == TENGINE_DT_UINT8)
Y
YoLucky 已提交
644
                {
N
nihui 已提交
645
                    uint8_t* output_data = (uint8_t*)output_tensor->data + n * out_size + c * out_cstep;
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
                    memset(output_data, param->value, sizeof(uint8_t) * out_cstep);
                }
            }
            else
            {
                if (input_tensor->data_type == TENGINE_DT_FP32)
                {
                    float* output_data = (float*)output_tensor->data + n * out_size + c * out_cstep;
                    float* copy_data = (float*)output_tensor->data + n * out_size + (pad_front + in_c) * out_cstep;
                    memcpy(output_data, copy_data, sizeof(float) * out_cstep);
                }
                else if (input_tensor->data_type == TENGINE_DT_UINT8)
                {
                    uint8_t* output_data = (uint8_t*)output_tensor->data + n * out_size + c * out_cstep;
                    uint8_t* copy_data = (uint8_t*)output_tensor->data + n * out_size + (pad_front + in_c) * out_cstep;
                    memcpy(output_data, copy_data, sizeof(uint8_t) * out_cstep);
Y
YoLucky 已提交
662
                }
B
BUG1989 已提交
663 664 665 666 667 668 669
            }
        }
    }

    return 0;
}

K
kalcohol 已提交
670
static int score(struct node_ops* node_ops, struct exec_graph* exec_graph, struct node* exec_node)
B
BUG1989 已提交
671 672 673 674 675
{
    return OPS_SCORE_BEST;
}

static struct node_ops pad_node_ops = {.prerun = NULL,
N
nihui 已提交
676 677 678 679 680 681
                                       .run = run,
                                       .reshape = NULL,
                                       .postrun = NULL,
                                       .init_node = init_node,
                                       .release_node = release_node,
                                       .score = score};
K
kalcohol 已提交
682

683
int register_pad_ref_op()
B
BUG1989 已提交
684 685 686 687
{
    return register_builtin_node_ops(OP_PAD, &pad_node_ops);
}

688
int unregister_pad_ref_op()
B
BUG1989 已提交
689 690 691
{
    return unregister_builtin_node_ops(OP_PAD, &pad_node_ops);
}