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 3.0 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
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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> CreatePrefixPath(const std::string &input_path,
  32. const bool support_relative_path = false);
  33. static std::optional<std::string> GetRealPath(const std::string &input_path);
  34. static std::optional<std::string> GetConfigFile(const std::string &env);
  35. static bool IsStrLengthValid(const std::string &str, size_t length_limit, const std::string &error_message = "");
  36. static bool IsPathValid(const std::string &path, size_t length_limit, const std::string &error_message = "");
  37. static bool IsFilenameValid(const std::string &filename, size_t length_limit, const std::string &error_message = "");
  38. static bool CreateNotExistDirs(const std::string &path);
  39. static std::string AddId(const std::string &filename, const std::string &suffix);
  40. static bool SaveStringToFile(const std::string filename, const std::string string_info);
  41. static bool FileExists(const std::string &filepath);
  42. static bool CommonFuncForConfigPath(const std::string &default_path, const std::string &env_path, std::string *value);
  43. private:
  44. static bool IsEveryFilenameValid(const std::string &path, size_t length_limit, const std::string &error_message);
  45. };
  46. inline std::string GetSaveGraphsPathName(const std::string &file_name, const std::string &save_path = "") {
  47. std::string save_graphs_path;
  48. if (save_path.empty()) {
  49. auto ms_context = MsContext::GetInstance();
  50. MS_EXCEPTION_IF_NULL(ms_context);
  51. save_graphs_path = ms_context->get_param<std::string>(MS_CTX_SAVE_GRAPHS_PATH);
  52. if (save_graphs_path.empty()) {
  53. save_graphs_path = ".";
  54. }
  55. } else {
  56. save_graphs_path = save_path;
  57. }
  58. if (IsStandAlone()) {
  59. return save_graphs_path + "/" + file_name;
  60. }
  61. return save_graphs_path + "/rank_" + std::to_string(GetRank()) + "/" + file_name;
  62. }
  63. } // namespace mindspore
  64. #endif // MINDSPORE_CCSRC_DEBUG_COMMON_H_