-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Performance] Use allocator from PyTorch if possible (dmlc#2328)
* first commit * some thoughts * move around * more commit * more fixes * now it uses torch allocator * fix symbol export error * fix * fixes * test fix * add script * building separate library per version * fix for vs2019 * more fixes * fix on windows build * update jenkinsfile * auto copy built dlls for windows * lint and installation guide update * fix * specify conda environment * set environment for ci * fix * fix * fix * fix again * revert * fix cmake * fix * switch to using python interpreter path * remove scripts * debug * oops sorry * Update index.rst * Update index.rst * copies automatically, no need for this * do not print message if library not found * tiny fixes * debug on nightly * replace add_compile_definitions to make CMake 3.5 happy * fix linking to wrong lib for multiple pytorch envs * changed building strategy * fix nightly * fix windows * fix windows again * setup bugfix * address comments * change README
- Loading branch information
Showing
26 changed files
with
616 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/*! | ||
* Copyright (c) 2017 by Contributors | ||
* \file dgl/runtime/env.h | ||
* \brief Structure for holding DGL global environment variables | ||
*/ | ||
|
||
#ifndef DGL_RUNTIME_ENV_H_ | ||
#define DGL_RUNTIME_ENV_H_ | ||
|
||
#include <string> | ||
|
||
/*! | ||
* \brief Global environment variables. | ||
*/ | ||
struct Env { | ||
static Env* Global() { | ||
static Env inst; | ||
return &inst; | ||
} | ||
/*! \brief the path to the tensoradapter library */ | ||
std::string ta_path; | ||
}; | ||
|
||
#endif // DGL_RUNTIME_ENV_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/*! | ||
* Copyright (c) 2020 by Contributors | ||
* \file array/tensordispatch.h | ||
* \brief This file defines the dispatcher of tensor operators to framework-specific | ||
* implementations. | ||
* | ||
* The dispatcher consists of a TensorDispatcher singleton in DGL C library and | ||
* one separately-built shared library per supported backend. | ||
* | ||
* Those shared libraries contain wrappers of the framework-specific operators. | ||
* The wrappers have almost the same signatures as functions in aten namespace, | ||
* except that they accept and return DLManagedTensors instead of NDArrays. | ||
* The wrappers are defined with extern "C", meaning that the C++ compiler will | ||
* not do name mangling for those functions so that DGL can conveniently locate | ||
* them using dlsym(3) (or GetProcAddress in Windows). | ||
* | ||
* The TensorDispatcher singleton maintains a mapping from an array operator to | ||
* the address of the corresponding symbol in the shared library. During | ||
* initialization, the TensorDispatcher checks which backend DGL is using. | ||
* It then locates and opens the corresponding shared library using dlopen(3) (or | ||
* LoadLibrary in Windows), and populates the said mapping above with dlsym(3) | ||
* (or GetProcAddress in Windows). | ||
* | ||
* A tensor operator in TensorDispatcher first checks whether the corresponding symbol | ||
* address is found in the mapping. If so, it calls the function located at the | ||
* symbol address instead, translating NDArrays to DLManagedTensors using | ||
* NDArray::ToDLPack(), and translates the DLManagedTensors in the return values | ||
* back to NDArrays using NDArray::FromDLPack(). If not, it falls back to the | ||
* implementation in dgl::aten namespace. | ||
*/ | ||
|
||
#ifndef DGL_RUNTIME_TENSORDISPATCH_H_ | ||
#define DGL_RUNTIME_TENSORDISPATCH_H_ | ||
|
||
#include <dlpack/dlpack.h> | ||
#include <tensoradapter.h> | ||
#if defined(WIN32) || defined(_WIN32) | ||
#include <windows.h> | ||
#endif // WIN32 | ||
#include <vector> | ||
#include "ndarray.h" | ||
|
||
/*! \brief Casts a pointer \c entry to a function pointer with signature of \c func */ | ||
#define FUNCCAST(func, entry) (*reinterpret_cast<decltype(&(func))>(entry)) | ||
|
||
namespace dgl { | ||
namespace runtime { | ||
|
||
/*! | ||
* \brief Dispatcher that delegates the function calls to framework-specific C++ APIs. | ||
*/ | ||
class TensorDispatcher { | ||
public: | ||
/*! \brief Get the singleton instance. */ | ||
static TensorDispatcher* Global() { | ||
static TensorDispatcher inst; | ||
return &inst; | ||
} | ||
|
||
/*! \brief Whether an adapter library is available */ | ||
inline bool IsAvailable() { | ||
return available_; | ||
} | ||
|
||
/*! | ||
* \brief Allocate an empty tensor. | ||
* | ||
* Used in NDArray::Empty(). | ||
*/ | ||
inline NDArray Empty(std::vector<int64_t> shape, DLDataType dtype, DLContext ctx) const { | ||
auto entry = entrypoints_[Op::kEmpty]; | ||
auto result = FUNCCAST(tensoradapter::TAempty, entry)(shape, dtype, ctx); | ||
return NDArray::FromDLPack(result); | ||
} | ||
|
||
private: | ||
/*! \brief ctor */ | ||
TensorDispatcher(); | ||
/*! \brief dtor */ | ||
~TensorDispatcher(); | ||
|
||
/*! | ||
* \brief List of symbols in the adapter library. | ||
* | ||
* Must match the functions in tensoradapter/include/tensoradapter.h. | ||
*/ | ||
static constexpr const char *names_[] = { | ||
"TAempty", | ||
}; | ||
|
||
/*! \brief Index of each function to the symbol list */ | ||
class Op { | ||
public: | ||
static constexpr int kEmpty = 0; | ||
}; | ||
|
||
/*! \brief Number of functions */ | ||
static constexpr int num_entries_ = sizeof(names_) / sizeof(names_[0]); | ||
|
||
/*! \brief Entrypoints of each function */ | ||
void* entrypoints_[num_entries_] = {nullptr}; | ||
|
||
bool available_ = false; | ||
#if defined(WIN32) || defined(_WIN32) | ||
HINSTANCE handle_; | ||
#else // !WIN32 | ||
void* handle_; | ||
#endif // WIN32 | ||
}; | ||
|
||
}; // namespace runtime | ||
}; // namespace dgl | ||
|
||
#endif // DGL_RUNTIME_TENSORDISPATCH_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.