1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
|
#include "Halide.h"
#include "halide_test_dirs.h"
#include <fstream>
using namespace Halide;
namespace {
std::string read_entire_file(const std::string &pathname) {
std::ifstream f(pathname, std::ios::in | std::ios::binary);
std::string result;
f.seekg(0, std::ifstream::end);
size_t size = f.tellg();
result.resize(size);
f.seekg(0, std::ifstream::beg);
f.read(result.data(), result.size());
if (!f.good()) {
std::cerr << "Unable to read file: " << pathname;
exit(1);
}
f.close();
// Normalize file to Unix line endings
result = Internal::replace_all(result, "\r\n", "\n");
return result;
}
void compare_src(const std::string &src, const std::string &correct_src) {
if (src != correct_src) {
int diff = 0;
while (src[diff] == correct_src[diff]) {
diff++;
}
int diff_end = diff + 1;
while (diff > 0 && src[diff] != '\n') {
diff--;
}
while (diff_end < (int)src.size() && src[diff_end] != '\n') {
diff_end++;
}
std::cerr
<< "Correct source code:\n"
<< correct_src
<< "Actual source code:\n"
<< src
<< "Difference starts at:" << diff << "\n"
<< "Correct: " << correct_src.substr(diff, diff_end - diff) << "\n"
<< "Actual: " << src.substr(diff, diff_end - diff) << "\n";
exit(1);
}
}
} // namespace
int main(int argc, char **argv) {
Param<float> alpha("alpha");
Param<int32_t> beta("beta");
Var x("x");
Func buf("buf");
buf(x) = cast<int32_t>(alpha + cast<float>(beta));
{
// We are using a fixed target here (rather than "host") since
// we are crosscompiling and want a uniform result everywhere.
Target t = Target("x86-64-linux");
std::string pytorch_out = Internal::get_test_tmp_dir() + "pytorch_test1.pytorch.h";
Internal::ensure_no_file_exists(pytorch_out);
std::vector<Argument> args{alpha, beta};
buf.compile_to({{OutputFileType::pytorch_wrapper, pytorch_out}}, args, "test1", t);
Internal::assert_file_exists(pytorch_out);
std::string actual = read_entire_file(pytorch_out);
std::string expected =
R"GOLDEN_CODE(#include "HalideBuffer.h"
#include "HalidePyTorchHelpers.h"
struct halide_buffer_t;
struct halide_filter_metadata_t;
#ifndef HALIDE_MUST_USE_RESULT
#ifdef __has_attribute
#if __has_attribute(nodiscard)
#define HALIDE_MUST_USE_RESULT [[nodiscard]]
#elif __has_attribute(warn_unused_result)
#define HALIDE_MUST_USE_RESULT __attribute__((warn_unused_result))
#else
#define HALIDE_MUST_USE_RESULT
#endif
#else
#define HALIDE_MUST_USE_RESULT
#endif
#endif
#ifndef HALIDE_FUNCTION_ATTRS
#define HALIDE_FUNCTION_ATTRS
#endif
#ifdef __cplusplus
extern "C" {
#endif
HALIDE_FUNCTION_ATTRS
int test1(float _alpha, int32_t _beta, struct halide_buffer_t *_buf_buffer);
HALIDE_FUNCTION_ATTRS
int test1_argv(void **args);
HALIDE_FUNCTION_ATTRS
const struct halide_filter_metadata_t *test1_metadata();
#ifdef __cplusplus
} // extern "C"
#endif
HALIDE_FUNCTION_ATTRS
inline int test1_th_(float _alpha, int32_t _beta, at::Tensor &_buf) {
void* __user_context = nullptr;
// Check tensors have contiguous memory and are on the correct device
HLPT_CHECK_CONTIGUOUS(_buf);
// Wrap tensors in Halide buffers
Halide::Runtime::Buffer<int32_t> _buf_buffer = Halide::PyTorch::wrap<int32_t>(_buf);
// Run Halide pipeline
int err = test1(_alpha, _beta, _buf_buffer);
AT_ASSERTM(err == 0, "Halide call failed");
return 0;
}
)GOLDEN_CODE";
compare_src(actual, expected);
}
{
// We are using an explicit target here (rather than "host") to avoid sniffing
// the system for capabilities; in particular, we don't care what Cuda capabilities
// the system has, and don't want to initialize Cuda to find out. (Since this test
// is just a crosscompilation for generated C++ code, this is fine.)
Target t = Target("x86-64-linux-cuda-user_context");
std::string pytorch_out = Internal::get_test_tmp_dir() + "pytorch_test2.pytorch.h";
Internal::ensure_no_file_exists(pytorch_out);
std::vector<Argument> args{alpha, beta};
buf.compile_to({{OutputFileType::pytorch_wrapper, pytorch_out}}, args, "test2", t);
Internal::assert_file_exists(pytorch_out);
std::string actual = read_entire_file(pytorch_out);
std::string expected =
R"GOLDEN_CODE(#include "ATen/cuda/CUDAContext.h"
#include "HalidePyTorchCudaHelpers.h"
#include "HalideBuffer.h"
#include "HalidePyTorchHelpers.h"
struct halide_buffer_t;
struct halide_filter_metadata_t;
#ifndef HALIDE_MUST_USE_RESULT
#ifdef __has_attribute
#if __has_attribute(nodiscard)
#define HALIDE_MUST_USE_RESULT [[nodiscard]]
#elif __has_attribute(warn_unused_result)
#define HALIDE_MUST_USE_RESULT __attribute__((warn_unused_result))
#else
#define HALIDE_MUST_USE_RESULT
#endif
#else
#define HALIDE_MUST_USE_RESULT
#endif
#endif
#ifndef HALIDE_FUNCTION_ATTRS
#define HALIDE_FUNCTION_ATTRS
#endif
#ifdef __cplusplus
extern "C" {
#endif
HALIDE_FUNCTION_ATTRS
int test2(void const *__user_context, float _alpha, int32_t _beta, struct halide_buffer_t *_buf_buffer);
HALIDE_FUNCTION_ATTRS
int test2_argv(void **args);
HALIDE_FUNCTION_ATTRS
const struct halide_filter_metadata_t *test2_metadata();
#ifdef __cplusplus
} // extern "C"
#endif
HALIDE_FUNCTION_ATTRS
inline int test2_th_(float _alpha, int32_t _beta, at::Tensor &_buf) {
// Setup CUDA
int device_id = at::cuda::current_device();
CUcontext ctx = 0;
CUresult res = cuCtxGetCurrent(&ctx);
AT_ASSERTM(res == 0, "Could not acquire CUDA context");
cudaStream_t stream = at::cuda::getCurrentCUDAStream(device_id);
struct UserContext { int device_id; CUcontext *cuda_context; cudaStream_t *stream; } user_ctx;
user_ctx.device_id = device_id;
user_ctx.cuda_context = &ctx;
user_ctx.stream = &stream;
void* __user_context = (void*) &user_ctx;
// Check tensors have contiguous memory and are on the correct device
HLPT_CHECK_CONTIGUOUS(_buf);
HLPT_CHECK_DEVICE(_buf, device_id);
// Wrap tensors in Halide buffers
Halide::Runtime::Buffer<int32_t> _buf_buffer = Halide::PyTorch::wrap_cuda<int32_t>(_buf);
// Run Halide pipeline
int err = test2(__user_context, _alpha, _beta, _buf_buffer);
AT_ASSERTM(err == 0, "Halide call failed");
// Make sure data is on device
AT_ASSERTM(!_buf_buffer.host_dirty(),"device not synchronized for buffer _buf, make sure all update stages are explicitly computed on GPU.");
_buf_buffer.device_detach_native();
return 0;
}
)GOLDEN_CODE";
compare_src(actual, expected);
}
printf("Success!\n");
return 0;
}
|