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. ...@@ -12,7 +12,7 @@ We use two python scripts for this tutorial.
import tvm import tvm
tgt_host="llvm" tgt_host="llvm"
tgt="aocl -device=s5_ref -mattr=emulator" tgt="aocl_sw_emu"
n = tvm.var("n") n = tvm.var("n")
A = tvm.placeholder((n,), name='A') A = tvm.placeholder((n,), name='A')
...@@ -38,7 +38,7 @@ import tvm ...@@ -38,7 +38,7 @@ import tvm
import numpy as np import numpy as np
import os import os
tgt="aocl -device=s5_ref -mattr=emulator" tgt="aocl_sw_emu"
fadd = tvm.module.load("myadd.so") fadd = tvm.module.load("myadd.so")
fadd_dev = tvm.module.load("myadd.aocx") fadd_dev = tvm.module.load("myadd.aocx")
......
...@@ -115,6 +115,7 @@ class TVMContext(ctypes.Structure): ...@@ -115,6 +115,7 @@ class TVMContext(ctypes.Structure):
'cl': 4, 'cl': 4,
'opencl': 4, 'opencl': 4,
'aocl' : 5, 'aocl' : 5,
'aocl_sw_emu' : 5,
'sdaccel': 6, 'sdaccel': 6,
'vulkan': 7, 'vulkan': 7,
'metal': 8, 'metal': 8,
......
...@@ -92,7 +92,7 @@ Target CreateTarget(const std::string& target_name, ...@@ -92,7 +92,7 @@ Target CreateTarget(const std::string& target_name,
t->device_type = kDLOpenCL; t->device_type = kDLOpenCL;
t->keys_array.push_back(ir::StringImm::make("sdaccel")); t->keys_array.push_back(ir::StringImm::make("sdaccel"));
t->keys_array.push_back(ir::StringImm::make("hls")); 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->device_type = kDLAOCL;
t->keys_array.push_back(ir::StringImm::make("aocl")); t->keys_array.push_back(ir::StringImm::make("aocl"));
t->keys_array.push_back(ir::StringImm::make("hls")); t->keys_array.push_back(ir::StringImm::make("hls"));
......
...@@ -13,7 +13,8 @@ ...@@ -13,7 +13,8 @@
namespace tvm { namespace tvm {
namespace codegen { 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. // Get code.
using tvm::runtime::Registry; using tvm::runtime::Registry;
bool output_ssa = false; bool output_ssa = false;
...@@ -31,17 +32,14 @@ runtime::Module BuildAOCL(Array<LoweredFunc> funcs, std::string target_str) { ...@@ -31,17 +32,14 @@ runtime::Module BuildAOCL(Array<LoweredFunc> funcs, std::string target_str) {
runtime::SaveBinaryToFile("aocl.cl", code.c_str()); runtime::SaveBinaryToFile("aocl.cl", code.c_str());
// Compile the .cl file. // Compile the .cl file.
std::string cmd = "aoc aocl.cl";
Target target = Target::create(target_str); Target target = Target::create(target_str);
if (target->device_name == "") { if (target->device_name != "") {
LOG(FATAL) << "AOCL device name not specified in build target."; cmd += " -board=" + target->device_name;
} }
std::string cmd = "aoc aocl.cl"; if (emulation) {
for (std::string option : target->options()) { cmd += " -march=emulator";
if (option == "-mattr=emulator") {
cmd += " -march=emulator";
}
} }
cmd += " -board=" + target->device_name;
if (system(cmd.c_str()) != 0) { if (system(cmd.c_str()) != 0) {
LOG(FATAL) << "OpenCL offline compilation error."; LOG(FATAL) << "OpenCL offline compilation error.";
} }
...@@ -55,7 +53,12 @@ runtime::Module BuildAOCL(Array<LoweredFunc> funcs, std::string target_str) { ...@@ -55,7 +53,12 @@ runtime::Module BuildAOCL(Array<LoweredFunc> funcs, std::string target_str) {
TVM_REGISTER_API("codegen.build_aocl") TVM_REGISTER_API("codegen.build_aocl")
.set_body([](TVMArgs args, TVMRetValue* rv) { .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 } // 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(): ...@@ -44,7 +44,7 @@ def test_exp():
if "AWS_PLATFORM" in os.environ: if "AWS_PLATFORM" in os.environ:
check_device("sdaccel -device=" + os.environ.get("AWS_PLATFORM")) 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(): def test_multi_kernel():
# graph # graph
...@@ -82,7 +82,7 @@ def test_multi_kernel(): ...@@ -82,7 +82,7 @@ def test_multi_kernel():
d.asnumpy(), a.asnumpy() * 2 + b.asnumpy(), rtol=1e-5) d.asnumpy(), a.asnumpy() * 2 + b.asnumpy(), rtol=1e-5)
check_device("sdaccel") check_device("sdaccel")
check_device("aocl -device=s5_ref -mattr=emulator") check_device("aocl_sw_emu")
if __name__ == "__main__": if __name__ == "__main__":
......
...@@ -39,7 +39,8 @@ def test_ewise(): ...@@ -39,7 +39,8 @@ def test_ewise():
foo(a, b) foo(a, b)
np.testing.assert_allclose(b.asnumpy(), b_np, rtol=1e-5, atol=1e-5) 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) 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