test_SIMDFunctions.cpp 5.5 KB
Newer Older
Z
zhangjinchao01 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/* Copyright (c) 2016 Baidu, Inc. All Rights Reserve.

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. */



#include "paddle/math/SIMDFunctions.h"
#include "paddle/utils/Util.h"

#include <gtest/gtest.h>

#include <random>
#include <functional>
#include <algorithm>
#include <memory>

27
#include <stdlib.h>
Z
zhangjinchao01 已提交
28 29 30 31 32 33 34 35 36 37 38 39
#include <time.h>

static constexpr size_t VECTOR_LEN = 3072;
static constexpr size_t BATCH_SIZE = 64;
static constexpr size_t ALIGN = 32;
static_assert(VECTOR_LEN % ALIGN == 0, "VECTOR_LEN % ALIGN == 0");
static_assert(BATCH_SIZE % ALIGN == 0, "BATCH_SIZE % ALIGN == 0");
static constexpr float EPSILON = 1e-5;
static std::mt19937 RandomEngine(time(0));

inline static std::unique_ptr<float[]> NewVector(size_t len = VECTOR_LEN,
                                                 size_t align = ALIGN) {
40
  float* ptr;
Y
Yu Yang 已提交
41
  CHECK_EQ(posix_memalign((void**)&ptr, align, len * sizeof(float)), 0);
42
  return std::unique_ptr<float[]>(ptr);
Z
zhangjinchao01 已提交
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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 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
}

inline static std::unique_ptr<float[]> NewRandomVector(size_t len = VECTOR_LEN,
                                                       size_t align = ALIGN) {
  std::uniform_real_distribution<float> dist(-100.0f, 100.0f);
  auto generator = std::bind(dist, RandomEngine);
  auto retv = NewVector(len, align);
  std::generate_n(retv.get(), len, generator);
  return retv;
}

TEST(SIMDFunction, addTo) {
  typedef std::function<void(float*, const float*, size_t)> AddToMethodType;

  AddToMethodType naive = paddle::simd::naive::addTo<float>;
  AddToMethodType simd = paddle::simd::addTo<float>;

  auto A = NewRandomVector();
  auto B = NewRandomVector();

  auto ACopy = NewVector();
  memcpy(ACopy.get(), A.get(), VECTOR_LEN * sizeof(float));

  naive(A.get(), B.get(), VECTOR_LEN);
  simd(ACopy.get(), B.get(), VECTOR_LEN);

  for (size_t i = 0; i < VECTOR_LEN; ++i) {
    ASSERT_NEAR(A[i], ACopy[i], EPSILON);
  }
}

TEST(SIMDFunction, batchAddTo) {
  auto A = NewRandomVector();
  auto ACopy = NewVector();
  memcpy(ACopy.get(), A.get(), sizeof(float) * VECTOR_LEN);

  std::vector<std::unique_ptr<float[]>> B;
  for (size_t i = 0; i < BATCH_SIZE; ++i) {
    B.emplace_back(NewRandomVector());
  }
  std::unique_ptr<float* []> BRaw(new float*[BATCH_SIZE]);
  for (size_t i = 0; i < BATCH_SIZE; ++i) {
    BRaw[i] = B[i].get();
  }

  typedef std::function<void(float*, const float**, int, size_t)>
      BatchAddToMethodType;

  BatchAddToMethodType naive = paddle::simd::naive::batchAddTo<float>;
  BatchAddToMethodType simd = paddle::simd::batchAddTo<float>;

  naive(A.get(), (const float**)BRaw.get(), BATCH_SIZE, VECTOR_LEN);
  simd(ACopy.get(), (const float**)BRaw.get(), BATCH_SIZE, VECTOR_LEN);

  for (size_t i = 0; i < VECTOR_LEN; ++i) {
    ASSERT_NEAR(A[i], ACopy[i], EPSILON);
  }
}

TEST(SIMDFunction, colMax) {
  auto A = NewRandomVector(VECTOR_LEN * BATCH_SIZE);
  auto naiveResult = NewVector(BATCH_SIZE);
  auto simdResult = NewVector(BATCH_SIZE);

  typedef std::function<void(float*, const float*, int, int)> ColMaxMethodType;
  ColMaxMethodType naive = paddle::simd::naive::colMax<float>;
  ColMaxMethodType simd = paddle::simd::colMax<float>;

  naive(naiveResult.get(), A.get(), BATCH_SIZE, VECTOR_LEN);
  simd(simdResult.get(), A.get(), BATCH_SIZE, VECTOR_LEN);

  for (size_t i = 0; i < BATCH_SIZE; ++i) {
    ASSERT_NEAR(naiveResult[i], simdResult[i], EPSILON);
  }
}

TEST(SIMDFunction, decayL1_WithLR) {
  auto dest = NewRandomVector();
  auto src = NewRandomVector();
  auto lr = NewRandomVector();
  auto lambda = 0.23f;

  auto simd_dest = NewVector();
  memcpy(simd_dest.get(), dest.get(), sizeof(float) * VECTOR_LEN);

  typedef std::function<void(float*, float*, float*, float, size_t)>
      DecayL1MethodType;

  DecayL1MethodType naive = [](float* d, float* s, float* lr, float l,
                               size_t len) {
    paddle::simd::naive::decayL1<float>(d, s, lr, l, len);
  };

  DecayL1MethodType simd = [](float* d, float* s, float* lr, float l,
                              size_t len) {
    paddle::simd::decayL1<float>(d, s, lr, l, len);
  };

  naive(dest.get(), src.get(), lr.get(), lambda, VECTOR_LEN);
  simd(simd_dest.get(), src.get(), lr.get(), lambda, VECTOR_LEN);

  for (size_t i = 0; i < VECTOR_LEN; ++i) {
    ASSERT_NEAR(dest[i], simd_dest[i], EPSILON);
  }
}

TEST(SIMDFunction, decayL1_WithoutLR) {
  auto dest = NewRandomVector();
  auto src = NewRandomVector();
  auto lambda = 0.23;

  auto simd_dest = NewVector();
  memcpy(simd_dest.get(), dest.get(), sizeof(float) * VECTOR_LEN);

  typedef std::function<void(float*, float*, float, size_t)> DecayL1MethodType;

  DecayL1MethodType naive = [](float* d, float* s, float l, size_t len) {
    paddle::simd::naive::decayL1<float>(d, s, l, len);
  };

  DecayL1MethodType simd = [](float* d, float* s, float l, size_t len) {
    paddle::simd::decayL1<float>(d, s, l, len);
  };

  naive(dest.get(), src.get(), lambda, VECTOR_LEN);
  simd(simd_dest.get(), src.get(), lambda, VECTOR_LEN);

  for (size_t i = 0; i < VECTOR_LEN; ++i) {
    ASSERT_NEAR(dest[i], simd_dest[i], EPSILON);
  }
}

int main(int argc, char** argv) {
  paddle::initMain(argc, argv);
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}