vbroadcast.cc 2.7 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
 *
 * Licensed 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. */

15
#include "lite/backends/x86/jit/gen/vbroadcast.h"
Y
Yan Chunwei 已提交
16 17
#include <memory>
#include <vector>
18
#include "lite/backends/x86/jit/registry.h"
Y
Yan Chunwei 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
#include "lite/utils/paddle_enforce.h"

namespace paddle {
namespace lite {
namespace jit {
namespace gen {

void VBroadcastJitCode::genCode() {
  preCode();
  constexpr int block = YMM_FLOAT_BLOCK;
  constexpr int max_num_regs = 16;
  const int num_block = w_ / block;
  const int num_groups = num_block / max_num_regs;
  const size_t block_size = sizeof(float) * block;
  std::vector<int> groups(num_groups, max_num_regs);
  int rest_num_regs = num_block % max_num_regs;
  if (rest_num_regs > 0) {
    groups.push_back(rest_num_regs);
  }

  // protect param_h
  mov(reg_height, param_h);
  Label l_next_h;
  xor_(reg_h_i, reg_h_i);
  mov(reg_ptr_dst_i, param_dst);
  L(l_next_h);
  {
    mov(reg_ptr_src_i, param_src);
    for (int num_regs : groups) {
      size_t w_offset = 0;
      for (int reg_i = 0; reg_i < num_regs; ++reg_i) {
        vmovups(ymm_t(reg_i), ptr[reg_ptr_src_i + w_offset]);
        w_offset += block_size;
      }
      add(reg_ptr_src_i, num_regs * block_size);

      w_offset = 0;
      for (int reg_i = 0; reg_i < num_regs; ++reg_i) {
        vmovups(ptr[reg_ptr_dst_i + w_offset], ymm_t(reg_i));
        w_offset += block_size;
      }
      add(reg_ptr_dst_i, num_regs * block_size);
    }  // end of groups
    inc(reg_h_i);
    cmp(reg_h_i, reg_height);
    jl(l_next_h, T_NEAR);
  }  // end of l_next_h

  postCode();
}

class VBroadcastCreator : public JitCodeCreator<int64_t> {
 public:
  bool CanBeUsed(const int64_t& w) const override {
    return x86::MayIUse(x86::avx) && w % YMM_FLOAT_BLOCK == 0;
  }
  size_t CodeSize(const int64_t& w) const override {
    return 96 + (w / YMM_FLOAT_BLOCK) * 16 * 8;
  }
  std::unique_ptr<GenBase> CreateJitCode(const int64_t& w) const override {
    PADDLE_ENFORCE_GT(w, 0);
    return make_unique<VBroadcastJitCode>(w, CodeSize(w));
  }
};

}  // namespace gen
}  // namespace jit
}  // namespace lite
}  // namespace paddle

namespace gen = paddle::lite::jit::gen;

REGISTER_JITKERNEL_GEN(kVBroadcast, gen::VBroadcastCreator);