3 #include <c10/util/Exception.h> 4 #include <c10/util/Optional.h> 24 inline std::vector<char> make_filename(std::string name_prefix) {
27 static const std::string kRandomPattern =
"XXXXXX";
31 static const char* env_variables[] = {
"TMPDIR",
"TMP",
"TEMP",
"TEMPDIR"};
33 std::string tmp_directory =
"/tmp";
34 for (
const char* variable : env_variables) {
35 if (
const char* path = getenv(variable)) {
41 std::vector<char> filename;
43 tmp_directory.size() + name_prefix.size() + kRandomPattern.size() + 2);
45 filename.insert(filename.end(), tmp_directory.begin(), tmp_directory.end());
46 filename.push_back(
'/');
47 filename.insert(filename.end(), name_prefix.begin(), name_prefix.end());
48 filename.insert(filename.end(), kRandomPattern.begin(), kRandomPattern.end());
49 filename.push_back(
'\0');
53 #endif // !defined(_WIN32) 58 TempFile(std::string name,
int fd) : fd(fd), name(std::move(name)) {}
66 #endif // !defined(_WIN32) 68 const std::string name;
82 std::string name_prefix =
"torch-file-") {
84 return TempFile{std::tmpnam(
nullptr)};
86 std::vector<char> filename = detail::make_filename(std::move(name_prefix));
87 const int fd = mkstemp(filename.data());
91 return TempFile(std::string(filename.begin(), filename.end()), fd);
92 #endif // defined(_WIN32) 101 AT_ERROR(
"Error generating temporary file: ", std::strerror(errno));
c10::optional< TempFile > try_make_tempfile(std::string name_prefix="torch-file-")
Attempts to return a temporary file or returns nullopt if an error ocurred.
To register your own kernel for an operator, do in one (!) cpp file: C10_REGISTER_KERNEL(OperatorHand...
TempFile make_tempfile(std::string name_prefix="torch-file-")
Like try_make_tempfile, but throws an exception if a temporary file could not be returned.