utils.cc 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "ultra_infer/utils/utils.h"
  15. #include <sstream>
  16. namespace ultra_infer {
  17. bool FDLogger::enable_info = true;
  18. bool FDLogger::enable_warning = true;
  19. void SetLogger(bool enable_info, bool enable_warning) {
  20. FDLogger::enable_info = enable_info;
  21. FDLogger::enable_warning = enable_warning;
  22. }
  23. FDLogger::FDLogger(bool verbose, const std::string &prefix) {
  24. verbose_ = verbose;
  25. line_ = "";
  26. prefix_ = prefix;
  27. }
  28. FDLogger &FDLogger::operator<<(std::ostream &(*os)(std::ostream &)) {
  29. if (!verbose_) {
  30. return *this;
  31. }
  32. std::cout << prefix_ << " " << line_ << std::endl;
  33. line_ = "";
  34. return *this;
  35. }
  36. bool ReadBinaryFromFile(const std::string &file, std::string *contents) {
  37. std::ifstream fin(file, std::ios::in | std::ios::binary);
  38. if (!fin.is_open()) {
  39. FDERROR << "Failed to open file: " << file << " to read." << std::endl;
  40. return false;
  41. }
  42. fin.seekg(0, std::ios::end);
  43. contents->clear();
  44. contents->resize(fin.tellg());
  45. fin.seekg(0, std::ios::beg);
  46. fin.read(&(contents->at(0)), contents->size());
  47. fin.close();
  48. return true;
  49. }
  50. std::vector<int64_t> GetStride(const std::vector<int64_t> &dims) {
  51. auto dims_size = dims.size();
  52. std::vector<int64_t> result(dims_size, 1);
  53. for (int i = dims_size - 2; i >= 0; --i) {
  54. result[i] = result[i + 1] * dims[i + 1];
  55. }
  56. return result;
  57. }
  58. } // namespace ultra_infer