Commit a03c60ba by MORITA Kazutaka Committed by Thierry Moreau

[CODEGEN][AOCL] Add math intrinsic rules (#1653)

* [CODEGEN][AOCL] Add math intrinsic rules

* introduce aocl_emu target for AOCL emulation

* rename aocl_emu with aocl_sw_emu

* update docs
parent 37988ad3
......@@ -12,7 +12,7 @@ We use two python scripts for this tutorial.
import tvm
tgt_host="llvm"
tgt="aocl -device=s5_ref -mattr=emulator"
tgt="aocl_sw_emu"
n = tvm.var("n")
A = tvm.placeholder((n,), name='A')
......@@ -38,7 +38,7 @@ import tvm
import numpy as np
import os
tgt="aocl -device=s5_ref -mattr=emulator"
tgt="aocl_sw_emu"
fadd = tvm.module.load("myadd.so")
fadd_dev = tvm.module.load("myadd.aocx")
......
......@@ -115,6 +115,7 @@ class TVMContext(ctypes.Structure):
'cl': 4,
'opencl': 4,
'aocl' : 5,
'aocl_sw_emu' : 5,
'sdaccel': 6,
'vulkan': 7,
'metal': 8,
......
......@@ -92,7 +92,7 @@ Target CreateTarget(const std::string& target_name,
t->device_type = kDLOpenCL;
t->keys_array.push_back(ir::StringImm::make("sdaccel"));
t->keys_array.push_back(ir::StringImm::make("hls"));
} else if (target_name == "aocl") {
} else if (target_name == "aocl" || target_name == "aocl_sw_emu") {
t->device_type = kDLAOCL;
t->keys_array.push_back(ir::StringImm::make("aocl"));
t->keys_array.push_back(ir::StringImm::make("hls"));
......
......@@ -13,7 +13,8 @@
namespace tvm {
namespace codegen {
runtime::Module BuildAOCL(Array<LoweredFunc> funcs, std::string target_str) {
runtime::Module BuildAOCL(Array<LoweredFunc> funcs, std::string target_str,
bool emulation) {
// Get code.
using tvm::runtime::Registry;
bool output_ssa = false;
......@@ -31,17 +32,14 @@ runtime::Module BuildAOCL(Array<LoweredFunc> funcs, std::string target_str) {
runtime::SaveBinaryToFile("aocl.cl", code.c_str());
// Compile the .cl file.
std::string cmd = "aoc aocl.cl";
Target target = Target::create(target_str);
if (target->device_name == "") {
LOG(FATAL) << "AOCL device name not specified in build target.";
if (target->device_name != "") {
cmd += " -board=" + target->device_name;
}
std::string cmd = "aoc aocl.cl";
for (std::string option : target->options()) {
if (option == "-mattr=emulator") {
if (emulation) {
cmd += " -march=emulator";
}
}
cmd += " -board=" + target->device_name;
if (system(cmd.c_str()) != 0) {
LOG(FATAL) << "OpenCL offline compilation error.";
}
......@@ -55,7 +53,12 @@ runtime::Module BuildAOCL(Array<LoweredFunc> funcs, std::string target_str) {
TVM_REGISTER_API("codegen.build_aocl")
.set_body([](TVMArgs args, TVMRetValue* rv) {
*rv = BuildAOCL(args[0], args[1]);
*rv = BuildAOCL(args[0], args[1], false);
});
TVM_REGISTER_API("codegen.build_aocl_sw_emu")
.set_body([](TVMArgs args, TVMRetValue* rv) {
*rv = BuildAOCL(args[0], args[1], true);
});
} // namespace codegen
......
/*!
* Copyright (c) 2018 by Contributors
* \file intrin_rule_aocl.cc
* \brief AOCL intrinsic rules.
*/
#include "intrin_rule.h"
namespace tvm {
namespace codegen {
namespace intrin {
TVM_REGISTER_GLOBAL("tvm.intrin.rule.aocl.floor")
.set_body(DispatchExtern<Direct>);
TVM_REGISTER_GLOBAL("tvm.intrin.rule.aocl.ceil")
.set_body(DispatchExtern<Direct>);
TVM_REGISTER_GLOBAL("tvm.intrin.rule.aocl.trunc")
.set_body(DispatchExtern<Direct>);
TVM_REGISTER_GLOBAL("tvm.intrin.rule.aocl.fabs")
.set_body(DispatchExtern<Direct>);
TVM_REGISTER_GLOBAL("tvm.intrin.rule.aocl.round")
.set_body(DispatchExtern<Direct>);
TVM_REGISTER_GLOBAL("tvm.intrin.rule.aocl.exp")
.set_body(DispatchExtern<Direct>);
TVM_REGISTER_GLOBAL("tvm.intrin.rule.aocl.log")
.set_body(DispatchExtern<Direct>);
TVM_REGISTER_GLOBAL("tvm.intrin.rule.aocl.tanh")
.set_body(DispatchExtern<Direct>);
TVM_REGISTER_GLOBAL("tvm.intrin.rule.aocl.sqrt")
.set_body(DispatchExtern<Direct>);
TVM_REGISTER_GLOBAL("tvm.intrin.rule.aocl.pow")
.set_body(DispatchExtern<Direct>);
TVM_REGISTER_GLOBAL("tvm.intrin.rule.aocl.popcount")
.set_body(DispatchExtern<Direct>);
TVM_REGISTER_GLOBAL("tvm.intrin.rule.aocl_sw_emu.floor")
.set_body(DispatchExtern<Direct>);
TVM_REGISTER_GLOBAL("tvm.intrin.rule.aocl_sw_emu.ceil")
.set_body(DispatchExtern<Direct>);
TVM_REGISTER_GLOBAL("tvm.intrin.rule.aocl_sw_emu.trunc")
.set_body(DispatchExtern<Direct>);
TVM_REGISTER_GLOBAL("tvm.intrin.rule.aocl_sw_emu.fabs")
.set_body(DispatchExtern<Direct>);
TVM_REGISTER_GLOBAL("tvm.intrin.rule.aocl_sw_emu.round")
.set_body(DispatchExtern<Direct>);
TVM_REGISTER_GLOBAL("tvm.intrin.rule.aocl_sw_emu.exp")
.set_body(DispatchExtern<Direct>);
TVM_REGISTER_GLOBAL("tvm.intrin.rule.aocl_sw_emu.log")
.set_body(DispatchExtern<Direct>);
TVM_REGISTER_GLOBAL("tvm.intrin.rule.aocl_sw_emu.tanh")
.set_body(DispatchExtern<Direct>);
TVM_REGISTER_GLOBAL("tvm.intrin.rule.aocl_sw_emu.sqrt")
.set_body(DispatchExtern<Direct>);
TVM_REGISTER_GLOBAL("tvm.intrin.rule.aocl_sw_emu.pow")
.set_body(DispatchExtern<Direct>);
TVM_REGISTER_GLOBAL("tvm.intrin.rule.aocl_sw_emu.popcount")
.set_body(DispatchExtern<Direct>);
} // namespace intrin
} // namespace codegen
} // namespace tvm
......@@ -44,7 +44,7 @@ def test_exp():
if "AWS_PLATFORM" in os.environ:
check_device("sdaccel -device=" + os.environ.get("AWS_PLATFORM"))
check_device("aocl -device=s5_ref -mattr=emulator")
check_device("aocl_sw_emu")
def test_multi_kernel():
# graph
......@@ -82,7 +82,7 @@ def test_multi_kernel():
d.asnumpy(), a.asnumpy() * 2 + b.asnumpy(), rtol=1e-5)
check_device("sdaccel")
check_device("aocl -device=s5_ref -mattr=emulator")
check_device("aocl_sw_emu")
if __name__ == "__main__":
......
......@@ -39,7 +39,8 @@ def test_ewise():
foo(a, b)
np.testing.assert_allclose(b.asnumpy(), b_np, rtol=1e-5, atol=1e-5)
for device in ['cuda', 'opencl', 'metal', 'rocm', 'vulkan', 'llvm', 'nvptx', 'sdaccel']:
for device in ['cuda', 'opencl', 'metal', 'rocm', 'vulkan', 'llvm', 'nvptx', 'sdaccel',
'aocl_sw_emu']:
check_device(device)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment