ohos_common.cpp 12.4 KB
Newer Older
T
TonyWang222 已提交
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 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 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
/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * 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 "ohos_common.h"
#include <numeric>
#include <inttypes.h>

/*
 * getDimInfo: get dim info from data file(int64_t)
 * param:
 * fp: the testing datafile object
 *
 * return :
 * dim_info: array to store the info of the dim in datafile, like
 * [4,3,3,6,3,162(3*3*6*3)],4 is dim size,3,3,6,3 is the dim shape data_size:
 * the size of the testing data including the data file
 * */
void getDimInfo(FILE *fp, std::vector<int64_t>* dim_info) {
  const int MAX_HEAD_SIZE = 50;
  uint32_t *dim_buffer = reinterpret_cast<uint32_t *>(malloc(MAX_HEAD_SIZE * sizeof(uint32_t)));
  size_t ret = fread(dim_buffer, sizeof(uint32_t), MAX_HEAD_SIZE, fp);
  if (ret == 0) {
    free(dim_buffer);
    return;
  }
  dim_info->push_back(*dim_buffer);  // get dim size

  // get data shape to compute the datasize
  uint64_t data_size = 1;
  uint32_t i = 1;
  for (; i <= dim_info->at(0); i++) {
    dim_info->push_back(*(dim_buffer + i));
    data_size *= *(dim_buffer + i);
  }
  dim_info->push_back(data_size);

  free(dim_buffer);
}

/*
 * readTestDataFile: read test date from hisi .t datafile(int64_t)
 * param:
 * infile: the path of hisi .t datafile
 * return:
 * dim_info: array to store the info of the dim in datafile, like [4,3,3,6,3],4
 * is dim size,3,3,6,3 is the dim shape
 * */
void *readTestDataFile(std::string infile, std::vector<int64_t>* dim_info1) {
  printf("\n [common.cpp] Loading data from: %s\n", infile.c_str());

  FILE *fp;
  fp = fopen(infile.c_str(), "r");
  if (fp == nullptr) {
    printf("ERROR: cant't open file %s\n", infile.c_str());
    return nullptr;
  } else {
    std::vector<int64_t> dim_info;
    std::vector<int64_t>* ptr_dim_info = &dim_info;
    getDimInfo(fp, ptr_dim_info);
    uint64_t data_size = ptr_dim_info->at(ptr_dim_info->size() - 1);
    fclose(fp);

    fp = fopen(infile.c_str(), "r");
    if (fp == nullptr) {
      printf("ERROR: cant't open file %s\n", infile.c_str());
      return nullptr;
    }
    uint32_t *memory = reinterpret_cast<uint32_t *>(malloc((dim_info[0] + 1) * sizeof(uint32_t)));

    size_t ret = fread(memory, sizeof(uint32_t), (dim_info[0] + 1), fp);
    if (ret == 0) {
      free(memory);
      fclose(fp);
      return nullptr;
    }
    uint32_t *data = reinterpret_cast<uint32_t *>(malloc((data_size) * sizeof(uint32_t)));
    size_t ret2 = fread(data, sizeof(uint32_t), data_size, fp);
    if (ret2 == 0) {
      free(data);
      fclose(fp);
      return nullptr;
    }
    free(memory);
    fclose(fp);

    for (int i = 0; i < dim_info[0]; i++) {
      dim_info1->push_back(dim_info[i + 1]);
    }

    printf("\n [common.cpp] Read test data file Over, get dimInfo as: (");
    int count = dim_info1->size();
    for (int i = 0; i < count; i++) {
      printf("%" PRId64, dim_info1->at(i));
    }
    printf(")\n");
    return data;
  }
}

/*
 * allclose
 * param:
 *  a:compared file a
 *  b:compared file b
 *  count: the count size which will compare
 *  rtol:
 *  atol:
 * return:
 *  true or false
 * */
bool allclose(float *a, float *b, uint64_t count, float rtol = 1e-05,
              float atol = 1e-08, bool isquant = false) {
  uint32_t i = 0;

  // add fail loop print
  uint32_t fail_count = 0;
  float tol = 0;
  float tol1 = 0;
  float tol2 = 0;
  bool nan_occur_in_accuray = false;

  float sum = 0.0f;
  static float sum_all;
  static float maximum = 0;
  static float minimum = 0;
  static uint64_t c = 0;

  if (a == nullptr || b == nullptr) {
    return false;
  }

  for (; i < count; ++i) {
    sum = sum + fabs(a[i] - b[i]) / (atol + rtol * fabs(b[i]));
    sum_all = sum_all + fabs(a[i] - b[i]) / (atol + rtol * fabs(b[i]));
    maximum = max(maximum, fabs(a[i] - b[i]) / (atol + rtol * fabs(b[i])));
    minimum = min(minimum, fabs(a[i] - b[i]) / (atol + rtol * fabs(b[i])));
    if (isnan(a[i]) || isinf(a[i])) {
      fail_count = fail_count + 1;
      nan_occur_in_accuray = true;
      if (fail_count < 100) {
        printf("     i = %2u:   %+f  |  %+f\n", i, a[i], b[i]);
      }
    } else if (fabs(a[i] - b[i]) > (atol + rtol * fabs(b[i]))) {
      tol = tol + fabs(a[i] - b[i]) / (fabs(b[i]) + 1);
      tol1 = tol1 + fabs(a[i] - b[i]);
      tol2 = tol2 + fabs(a[i] - b[i]) / fabs(b[i]);
      fail_count = fail_count + 1;
      if (fail_count < 100) {
        printf("     i = %2u:   %+f  |  %+f\n", i, a[i], b[i]);
      }
    }

    if (i == count - 1) {
      printf("     ……\n");
      printf("\n *** Total fail_count: %u\n", fail_count);
168 169 170 171 172 173
      if (fail_count != 0) {
        printf("\n     fabs(a[i] - b[i])/(fabs(b[i])+1) : %f\n",
              tol / fail_count);
        printf("\n     fabs(a[i] - b[i]) : %f\n", tol1 / fail_count);
        printf("\n     fabs(a[i] - b[i])/fabs(b[i]) : %f\n", tol2 / fail_count);
      }
T
TonyWang222 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 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 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
      c = c + count;
      printf("\n     avg : %f\n", sum / count);
      printf("\n     min : %f\n", minimum);
      printf("\n     max :  %f\n", maximum);
      printf("\n     avg_all :  %f\n", sum_all / c);
      printf("\n");
      fstream file;
      file.open("cout.csv", ios::app);

      file << ","
           << "1,"
           << "0," << maximum;
      if (fail_count == 0) {
        file << "," << sum_all / c;
      } else {
        file << "," << tol / fail_count;
      }
      file.close();
    }
  }
  if (nan_occur_in_accuray) {
    printf("\n[common.cpp] eval output include some NAN/INF\n");
    return false;
  }

  if (fail_count > 0) {
    printf("\n *** These data compare failed: atol = %f, rtol = %f\n", atol,
           rtol);
    printf("\n");
    if (isquant) {
      if (tol / fail_count < 0.04) {
        return true;
      }
    }
    return false;
  }
  return true;
}

bool allclose_int8(uint8_t *a, uint8_t *b, uint64_t count, float rtol = 1e-05,
                   float atol = 1e-08, bool isquant = false) {
  uint32_t i = 0;
  // add fail loop print
  uint32_t fail_count = 0;
  float tol = 0;
  float tol1 = 0;
  float tol2 = 0;
  bool nan_occur_in_accuray = false;

  float sum = 0.0f;
  static float sum_all;
  static float maximum = 0;
  static float minimum = 0;
  static uint64_t c = 0;
  // add fail loop print

  if (a == nullptr || b == nullptr) {
    return false;
  }

  for (; i < count; ++i) {
    sum = sum + fabs(a[i] - b[i]) / (atol + rtol * fabs(b[i]));
    sum_all = sum_all + fabs(a[i] - b[i]) / (atol + rtol * fabs(b[i]));
    maximum = max(static_cast<double>(maximum),
                  static_cast<double>(fabs(a[i] - b[i])) / (atol + rtol * fabs(b[i])));
    minimum = min(static_cast<double>(minimum),
                  static_cast<double>(fabs(a[i] - b[i])) / (atol + rtol * fabs(b[i])));
    if (isnan(a[i]) || isinf(a[i])) {
      fail_count = fail_count + 1;
      nan_occur_in_accuray = true;
      if (fail_count < 100) {
        printf("     i = %2u:   %+f  |  %+f\n", i, static_cast<float>(a[i]), static_cast<float>(b[i]));
      }
    } else if (fabs(a[i] - b[i]) > 0) {
      tol = tol + fabs(a[i] - b[i]) / (fabs(b[i]) + 1);
      tol1 = tol1 + fabs(a[i] - b[i]);
      tol2 = tol2 + fabs(a[i] - b[i]) / fabs(b[i]);
      fail_count = fail_count + 1;
      printf("%2d", static_cast<int>(fabs(a[i] - b[i])));
      printf("     i = %2u:   %2d  |  %2d\n", i, a[i], b[i]);
    }
    if (i == count - 1) {
      printf("     ……\n");
      printf("\n *** Total fail_count: %u\n", fail_count);
258 259 260 261 262 263
      if (fail_count != 0) {
        printf("\n     fabs(a[i] - b[i])/(fabs(b[i])+1) : %f\n",
              tol / fail_count);
        printf("\n     fabs(a[i] - b[i]) : %f\n", tol1 / fail_count);
        printf("\n     fabs(a[i] - b[i])/fabs(b[i]) : %f\n", tol2 / fail_count);
      }
T
TonyWang222 已提交
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 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 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

      c = c + count;
      printf("\n     avg : %f\n", sum / count);
      printf("\n     min : %f\n", minimum);

      printf("\n     max :  %f\n", maximum);
      printf("\n     avg_all :  %f\n", sum_all / c);
      printf("\n");
      fstream file;
      file.open("cout.csv", ios::app);

      file << ","
           << "1,"
           << "0," << maximum;
      if (fail_count == 0) {
        file << "," << sum_all / c;
      } else {
        file << "," << tol / fail_count;
      }
      file.close();
    }
  }
  if (nan_occur_in_accuray) {
    printf("\n[common.cpp] eval output include some NAN/INF\n");
    return false;
  }
  if (fail_count > 0) {
    printf("\n *** These data compare failed: atol = %f, rtol = %f\n", atol,
           rtol);
    printf("\n");
    if (isquant) {
      if (tol / fail_count < 0.04) {
        return true;
      }
    }
    return false;
  }
  return true;
}

/*
 * compFp32WithTData: compare the data with the data in hisi .t file
 * param:
 *  actualOutputData: the result of ge
 *  expectedDataFile: the path of hisi .t result file
 *  rtol:
 *  atol:
 * return:
 *  true of false
 * */
bool compFp32WithTData(float *actualOutputData, const std::string& expectedDataFile,
                       float rtol = 1e-05, float atol = 1e-08,
                       bool isquant = false) {
  std::vector<int64_t> dim_info;
  std::vector<int64_t>* ptr_dim_info = &dim_info;
  float *expectedOutputData =
      reinterpret_cast<float *>(readTestDataFile(expectedDataFile, ptr_dim_info));
  uint32_t i = 0;
  uint64_t data_size = 1;
  data_size = accumulate(dim_info.begin(), dim_info.end(), 1, std::multiplies<uint64_t>());

  // print caffe/tf output:
  printf("[common.cpp] expected output data:");
  for (; i < data_size && i < 10; i++) {
    printf("%4f ", expectedOutputData[i]);
  }
  printf("\n");
  if (isquant) {
    return allclose(actualOutputData, expectedOutputData, data_size, rtol, atol,
                    true);
  }
  return allclose(actualOutputData, expectedOutputData, data_size, rtol, atol);
}

bool compUint8WithTData(uint8_t *actualOutputData, const std::string& expectedDataFile,
                        float rtol = 1e-05, float atol = 1e-08,
                        bool isquant = false) {
  std::vector<int64_t> dim_info;
  std::vector<int64_t>* ptr_dim_info = &dim_info;
  auto dataFile = readTestDataFile(expectedDataFile, ptr_dim_info);
  if(dataFile == nullptr){
    return false;
  }
  uint8_t *expectedOutputData =
      reinterpret_cast<uint8_t *>(dataFile);
  uint32_t i = 0;
  uint64_t data_size = 1;
  data_size = accumulate(dim_info.begin(), dim_info.end(), 1, std::multiplies<uint64_t>());

  // print caffe/tf output:
  printf("\n [common.cpp] expected output data:\n");
  for (; i < data_size && i < 10; i++) {
    printf("%4hhu ", static_cast<unsigned char>(expectedOutputData[i]));
  }
  printf("\n");
  if (isquant) {
    return allclose_int8(actualOutputData, expectedOutputData, data_size, rtol,
                         atol, true);
  }
  return allclose_int8(actualOutputData, expectedOutputData, data_size, rtol,
                       atol);
}

/*
 * ReadFile: read file of model
 * param:
 *  file: file location
 *  size: file size
 * return:
 *  buf of file
 * */
char *ReadFile(const char *file, size_t* size) {
  printf("[common.cpp] Loading data from: %s\n", file);

  std::ifstream ifs(file);
  if (!ifs.good()) {
    return nullptr;
  }

  if (!ifs.is_open()) {
    ifs.close();
    return nullptr;
  }

  ifs.seekg(0, std::ios::end);
  *size = ifs.tellg();

  char *buf = new char[*size];
  if (buf == nullptr) {
    ifs.close();
    return nullptr;
  }

  ifs.seekg(0, std::ios::beg);
  ifs.read(buf, *size);
  ifs.close();
  printf("[common.cpp]Read Binary Data Over, get tensorSize as: %" PRId64 ".\n", static_cast<int64_t>(*size));

  return buf;
}

void PackNCHWToNHWCFp32(const char *src, char *dst, int batch, int plane, int channel) {
    for (int n = 0; n < batch; n++) {
        for (int c = 0; c < channel; c++) {
            for (int hw = 0; hw < plane; hw++) {
                int nhwc_index = n * channel * plane + hw * channel + c;
                int nchw_index = n * channel * plane + c * plane + hw;
                dst[nhwc_index * 4] = src[nchw_index * 4];
                dst[nhwc_index * 4 + 1] = src[nchw_index * 4 + 1];
                dst[nhwc_index * 4 + 2] = src[nchw_index * 4 + 2];
                dst[nhwc_index * 4 + 3] = src[nchw_index * 4 + 3];
            }
        }
    }
    return;
}