Skip to content

Commit 667a023

Browse files
Jingyue Wutensorflower-gardener
authored andcommitted
Find libdevice files in .runfiles when TEST_SRCDIR is not set.
Before this CL, XLA GPU binaries can only be run with "blaze test". After this CL, they can be run directly via "blaze-bin/<executable>". Change: 141952101
1 parent f0a6d1e commit 667a023

6 files changed

Lines changed: 58 additions & 41 deletions

File tree

tensorflow/core/platform/default/cuda_libdevice_path.cc

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,27 @@ limitations under the License.
1717

1818
#include <stdlib.h>
1919

20+
#include "tensorflow/core/lib/io/path.h"
2021
#include "tensorflow/core/lib/strings/strcat.h"
21-
#include "tensorflow/core/platform/default/logging.h"
22+
#include "tensorflow/core/platform/env.h"
23+
#include "tensorflow/core/platform/logging.h"
2224

2325
namespace tensorflow {
2426

2527
string CudaRoot() {
2628
// 'bazel test' sets TEST_SRCDIR.
27-
const string kRelativeCudaRoot = "local_config_cuda/cuda";
28-
const char* env = getenv("TEST_SRCDIR");
29-
if (env && env[0] != '\0') {
30-
return strings::StrCat(env, "/", kRelativeCudaRoot);
31-
} else {
32-
LOG(WARNING) << "TEST_SRCDIR environment variable not set: "
33-
<< "using $PWD/" << kRelativeCudaRoot << "as the CUDA root.";
34-
return kRelativeCudaRoot;
29+
const string kRelativeCudaRoot = io::JoinPath("local_config_cuda", "cuda");
30+
const char* test_srcdir = getenv("TEST_SRCDIR");
31+
if (test_srcdir && test_srcdir[0] != '\0') {
32+
return io::JoinPath(test_srcdir, kRelativeCudaRoot);
3533
}
34+
35+
LOG(INFO) << "TEST_SRCDIR environment variable not set: using "
36+
<< kRelativeCudaRoot
37+
<< " under this executable's runfiles directory as the CUDA root.";
38+
return io::JoinPath(
39+
strings::StrCat(Env::Default()->GetExecutablePath(), ".runfiles"),
40+
kRelativeCudaRoot);
3641
}
3742

3843
} // namespace tensorflow

tensorflow/core/platform/env.cc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ limitations under the License.
1515

1616
#include <deque>
1717
#include <vector>
18+
#if defined(__APPLE__)
19+
#include <mach-o/dyld.h>
20+
#endif
21+
#if defined(PLATFORM_WINDOWS)
22+
#include <windows.h>
23+
#define PATH_MAX MAX_PATH
24+
#else
25+
#include <unistd.h>
26+
#endif
1827

1928
#include "tensorflow/core/lib/core/errors.h"
2029
#include "tensorflow/core/lib/gtl/map_util.h"
@@ -197,6 +206,26 @@ Status Env::RenameFile(const string& src, const string& target) {
197206
return src_fs->RenameFile(src, target);
198207
}
199208

209+
string Env::GetExecutablePath() {
210+
char exe_path[PATH_MAX] = {0};
211+
#ifdef __APPLE__
212+
uint32_t buffer_size(0U);
213+
_NSGetExecutablePath(nullptr, &buffer_size);
214+
char unresolved_path[buffer_size];
215+
_NSGetExecutablePath(unresolved_path, &buffer_size);
216+
CHECK(realpath(unresolved_path, exe_path));
217+
#elif defined(PLATFORM_WINDOWS)
218+
HMODULE hModule = GetModuleHandle(NULL);
219+
GetModuleFileName(hModule, exe_path, MAX_PATH);
220+
#else
221+
CHECK_NE(-1, readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1));
222+
#endif
223+
// Make sure it's null-terminated:
224+
exe_path[sizeof(exe_path) - 1] = 0;
225+
226+
return exe_path;
227+
}
228+
200229
Thread::~Thread() {}
201230

202231
EnvWrapper::~EnvWrapper() {}

tensorflow/core/platform/env.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,10 @@ class Env {
204204
/// replaced.
205205
Status RenameFile(const string& src, const string& target);
206206

207+
/// \brief Returns the absolute path of the current executable. It resolves
208+
/// symlinks if there is any.
209+
string GetExecutablePath();
210+
207211
// TODO(jeff,sanjay): Add back thread/thread-pool support if needed.
208212
// TODO(jeff,sanjay): if needed, tighten spec so relative to epoch, or
209213
// provide a routine to get the absolute time.

tensorflow/core/platform/env_test.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,4 +291,9 @@ TEST_F(DefaultEnvTest, RecursivelyCreateDirWithUri) {
291291
TF_EXPECT_OK(env->FileExists(create_path));
292292
}
293293

294+
TEST_F(DefaultEnvTest, GetExecutablePath) {
295+
Env* env = Env::Default();
296+
TF_EXPECT_OK(env->FileExists(env->GetExecutablePath()));
297+
}
298+
294299
} // namespace tensorflow

tensorflow/stream_executor/dso_loader.cc

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,14 @@ limitations under the License.
1919
#include "tensorflow/stream_executor/dso_loader.h"
2020

2121
#include <limits.h>
22-
#if defined(__APPLE__)
23-
#include <mach-o/dyld.h>
24-
#endif
2522
#include <stdlib.h>
26-
#if defined(PLATFORM_WINDOWS)
27-
#include <windows.h>
28-
#define PATH_MAX MAX_PATH
29-
#else
30-
#include <unistd.h>
31-
#endif
3223
#include <initializer_list>
3324
#include <vector>
3425

3526
#include "tensorflow/core/platform/load_library.h"
3627
#include "tensorflow/stream_executor/lib/env.h"
3728
#include "tensorflow/stream_executor/lib/error.h"
29+
#include "tensorflow/stream_executor/lib/path.h"
3830
#include "tensorflow/stream_executor/lib/str_util.h"
3931
#include "tensorflow/stream_executor/lib/strcat.h"
4032
#include "tensorflow/stream_executor/lib/stringprintf.h"
@@ -130,29 +122,8 @@ string GetCudnnVersion() { return TF_CUDNN_VERSION; }
130122
}
131123

132124
/* static */ string DsoLoader::GetBinaryDirectory(bool strip_executable_name) {
133-
char exe_path[PATH_MAX] = {0};
134-
#ifdef __APPLE__
135-
uint32_t buffer_size(0U);
136-
_NSGetExecutablePath(nullptr, &buffer_size);
137-
char unresolved_path[buffer_size];
138-
_NSGetExecutablePath(unresolved_path, &buffer_size);
139-
CHECK_ERR(realpath(unresolved_path, exe_path) ? 1 : -1);
140-
#elif defined(PLATFORM_WINDOWS)
141-
HMODULE hModule = GetModuleHandle(NULL);
142-
GetModuleFileName(hModule, exe_path, MAX_PATH);
143-
#else
144-
CHECK_ERR(readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1));
145-
#endif
146-
// Make sure it's null-terminated:
147-
exe_path[sizeof(exe_path) - 1] = 0;
148-
149-
if (strip_executable_name) {
150-
// The exe is the last component of the path, so remove one component.
151-
std::vector<string> components = port::Split(exe_path, '/');
152-
components.pop_back();
153-
return port::Join(components, "/");
154-
}
155-
return exe_path;
125+
string exe_path = port::Env::Default()->GetExecutablePath();
126+
return strip_executable_name ? port::Dirname(exe_path).ToString() : exe_path;
156127
}
157128

158129
// Creates a heap-allocated vector for initial rpaths.

tensorflow/stream_executor/lib/path.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@ limitations under the License.
1616
#ifndef TENSORFLOW_STREAM_EXECUTOR_LIB_PATH_H_
1717
#define TENSORFLOW_STREAM_EXECUTOR_LIB_PATH_H_
1818

19+
#include "tensorflow/core/lib/io/path.h"
1920
#include "tensorflow/stream_executor/lib/stringpiece.h"
2021
#include "tensorflow/stream_executor/platform/port.h"
2122

2223
namespace perftools {
2324
namespace gputools {
2425
namespace port {
2526

27+
using tensorflow::io::Dirname;
28+
2629
namespace internal {
2730
// TODO(rspringer): Move to cc/implementation file.
2831
// Not part of the public API.

0 commit comments

Comments
 (0)