logger.cc 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "logger.h"
  17. #include <cuda_runtime.h>
  18. namespace fastertransformer {
  19. Logger::Logger()
  20. {
  21. char* is_first_rank_only_char = std::getenv("FT_LOG_FIRST_RANK_ONLY");
  22. bool is_first_rank_only =
  23. (is_first_rank_only_char != nullptr && std::string(is_first_rank_only_char) == "ON") ? true : false;
  24. int device_id;
  25. cudaGetDevice(&device_id);
  26. char* level_name = std::getenv("FT_LOG_LEVEL");
  27. if (level_name != nullptr) {
  28. std::map<std::string, Level> name_to_level = {
  29. {"TRACE", TRACE},
  30. {"DEBUG", DEBUG},
  31. {"INFO", INFO},
  32. {"WARNING", WARNING},
  33. {"ERROR", ERROR},
  34. };
  35. auto level = name_to_level.find(level_name);
  36. // If FT_LOG_FIRST_RANK_ONLY=ON, set LOG LEVEL of other device to ERROR
  37. if (is_first_rank_only && device_id != 0) {
  38. level = name_to_level.find("ERROR");
  39. }
  40. if (level != name_to_level.end()) {
  41. setLevel(level->second);
  42. }
  43. else {
  44. fprintf(stderr,
  45. "[FT][WARNING] Invalid logger level FT_LOG_LEVEL=%s. "
  46. "Ignore the environment variable and use a default "
  47. "logging level.\n",
  48. level_name);
  49. level_name = nullptr;
  50. }
  51. }
  52. }
  53. } // namespace fastertransformer