system.h 2.93 KB
Newer Older
Tim Moon's avatar
Tim Moon committed
1
/*************************************************************************
2
 * Copyright (c) 2022-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
Tim Moon's avatar
Tim Moon committed
3
4
5
6
7
8
9
 *
 * See LICENSE for license information.
 ************************************************************************/

#ifndef TRANSFORMER_ENGINE_COMMON_UTIL_SYSTEM_H_
#define TRANSFORMER_ENGINE_COMMON_UTIL_SYSTEM_H_

10
11
12
13
14
#include <cstdint>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <sstream>
Tim Moon's avatar
Tim Moon committed
15
16
#include <string>

17
18
#include "logging.h"

Tim Moon's avatar
Tim Moon committed
19
20
namespace transformer_engine {

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
namespace detail {

/*! \brief Template specialization to get the env var for numeric data types */
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type getenv_helper(
    const char *variable, const T &default_value) {
  // Implementation for numeric types
  const char *env = std::getenv(variable);
  if (env == nullptr || env[0] == '\0') {
    return default_value;
  }
  T value;
  std::istringstream iss(env);
  iss >> value;
  NVTE_CHECK(iss, "Invalid environment variable value");
  return value;
}

/*! \brief Template specialization to get the env var for string-like data types */
template <typename T>
inline typename std::enable_if<!std::is_arithmetic<T>::value, T>::type getenv_helper(
    const char *variable, const T &default_value) {
  // Implementation for string-like types
  const char *env = std::getenv(variable);
  if (env == nullptr || env[0] == '\0') {
    return default_value;
  } else {
    return env;
  }
}

/*! \brief Template specialization to get the default values for different
* numeric data types
*/
template <typename T>
inline T getenv_default_value() {
  return 0;
}

/*! \brief Template specialization to get the default values for bool */
template <>
inline bool getenv_default_value<bool>() {
  return false;
}

/*! \brief Template specialization to get the default values for string */
template <>
inline std::string getenv_default_value<std::string>() {
  return std::string();
}

/*! \brief Template specialization to get the default values for filesystem
* path data type */
template <>
inline std::filesystem::path getenv_default_value<std::filesystem::path>() {
  return std::filesystem::path();
}

}  // namespace detail

Tim Moon's avatar
Tim Moon committed
81
82
83
84
/*! \brief Get environment variable and convert to type
 *
 * If the environment variable is unset or empty, a falsy value is
 * returned.
85
*/
Tim Moon's avatar
Tim Moon committed
86
template <typename T = std::string>
87
88
89
inline T getenv(const char *variable) {
  return detail::getenv_helper<T>(variable, detail::getenv_default_value<T>());
}
Tim Moon's avatar
Tim Moon committed
90
91
92

/*! \brief Get environment variable and convert to type */
template <typename T = std::string>
93
94
95
inline T getenv(const char *variable, const T &default_value) {
  return detail::getenv_helper<T>(variable, default_value);
}
Tim Moon's avatar
Tim Moon committed
96

97
98
99
inline bool file_exists(const std::string &path) {
  return std::filesystem::exists(path) && std::filesystem::is_regular_file(path);
}
Tim Moon's avatar
Tim Moon committed
100
101
102

}  // namespace transformer_engine
#endif  // TRANSFORMER_ENGINE_COMMON_UTIL_SYSTEM_H_