torch.cpp 886 Bytes
Newer Older
1
/*!
2
 *  Copyright (c) 2020-2022 by Contributors
3
4
5
6
 * \file torch/torch.cpp
 * \brief Implementation of PyTorch adapter library.
 */

7
#include <tensoradapter_exports.h>
8
#include <c10/core/CPUAllocator.h>
9
10
#ifdef DGL_USE_CUDA
#include <c10/cuda/CUDACachingAllocator.h>
11
#include <cuda_runtime.h>
12
#endif  // DGL_USE_CUDA
13

14
15
16
17
namespace tensoradapter {

extern "C" {

18
19
20
21
22
23
TA_EXPORTS void* CPURawAlloc(size_t nbytes) {
  return c10::GetCPUAllocator()->raw_allocate(nbytes);
}

TA_EXPORTS void CPURawDelete(void* ptr) {
  c10::GetCPUAllocator()->raw_deallocate(ptr);
24
25
}

26
#ifdef DGL_USE_CUDA
27
28
29
TA_EXPORTS void* CUDARawAlloc(size_t nbytes, cudaStream_t stream) {
  return c10::cuda::CUDACachingAllocator::raw_alloc_with_stream(
    nbytes, stream);
30
31
}

32
TA_EXPORTS void CUDARawDelete(void* ptr) {
33
34
35
36
  c10::cuda::CUDACachingAllocator::raw_delete(ptr);
}
#endif  // DGL_USE_CUDA

37
38
39
};

};  // namespace tensoradapter