You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

common.h 2.8 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * Copyright 2020-2021 Huawei Technologies Co., Ltd
  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. #ifndef MINDSPORE_CCSRC_DEBUG_COMMON_H_
  17. #define MINDSPORE_CCSRC_DEBUG_COMMON_H_
  18. #include <string>
  19. #include <optional>
  20. #include "utils/contract.h"
  21. #include "utils/ms_context.h"
  22. #include "utils/comm_manager.h"
  23. namespace mindspore {
  24. static const int MAX_DIRECTORY_LENGTH = 1024;
  25. static const int MAX_FILENAME_LENGTH = 128;
  26. static const int MAX_OS_FILENAME_LENGTH = 255;
  27. class Common {
  28. public:
  29. Common() = default;
  30. ~Common() = default;
  31. static std::optional<std::string> GetRealPath(const std::string &input_path);
  32. static std::optional<std::string> GetConfigFile(const std::string &env);
  33. static bool IsStrLengthValid(const std::string &str, size_t length_limit, const std::string &error_message = "");
  34. static bool IsPathValid(const std::string &path, size_t length_limit, const std::string &error_message = "");
  35. static bool IsFilenameValid(const std::string &filename, size_t length_limit, const std::string &error_message = "");
  36. static bool CreateNotExistDirs(const std::string &path);
  37. static std::string AddId(const std::string &filename, const std::string &suffix);
  38. static bool SaveStringToFile(const std::string filename, const std::string string_info);
  39. static bool FileExists(const std::string &filepath);
  40. static bool CommonFuncForConfigPath(const std::string &default_path, const std::string &env_path, std::string *value);
  41. private:
  42. static bool IsEveryFilenameValid(const std::string &path, size_t length_limit, const std::string &error_message);
  43. };
  44. inline std::string GetSaveGraphsPathName(const std::string &file_name, const std::string &save_path = "") {
  45. std::string save_graphs_path;
  46. if (save_path.empty()) {
  47. auto ms_context = MsContext::GetInstance();
  48. MS_EXCEPTION_IF_NULL(ms_context);
  49. save_graphs_path = ms_context->get_param<std::string>(MS_CTX_SAVE_GRAPHS_PATH);
  50. if (save_graphs_path.empty()) {
  51. save_graphs_path = ".";
  52. }
  53. } else {
  54. save_graphs_path = save_path;
  55. }
  56. if (IsStandAlone()) {
  57. return save_graphs_path + "/" + file_name;
  58. }
  59. return save_graphs_path + "/rank_" + std::to_string(GetRank()) + "/" + file_name;
  60. }
  61. } // namespace mindspore
  62. #endif // MINDSPORE_CCSRC_DEBUG_COMMON_H_