Skip to content

Commit

Permalink
[CODEGEN][AOCL] Add math intrinsic rules (#1653)
Browse files Browse the repository at this point in the history
* [CODEGEN][AOCL] Add math intrinsic rules

* introduce aocl_emu target for AOCL emulation

* rename aocl_emu with aocl_sw_emu

* update docs
  • Loading branch information
kazum authored and tmoreau89 committed Aug 25, 2018
1 parent 37988ad commit a03c60b
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 16 deletions.
4 changes: 2 additions & 2 deletions docs/deploy/aocl_fpga.md
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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")
Expand Down
1 change: 1 addition & 0 deletions python/tvm/_ffi/runtime_ctypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class TVMContext(ctypes.Structure):
'cl': 4,
'opencl': 4,
'aocl' : 5,
'aocl_sw_emu' : 5,
'sdaccel': 6,
'vulkan': 7,
'metal': 8,
Expand Down
2 changes: 1 addition & 1 deletion src/codegen/build_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
23 changes: 13 additions & 10 deletions src/codegen/codegen_aocl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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") {
cmd += " -march=emulator";
}
if (emulation) {
cmd += " -march=emulator";
}
cmd += " -board=" + target->device_name;
if (system(cmd.c_str()) != 0) {
LOG(FATAL) << "OpenCL offline compilation error.";
}
Expand All @@ -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
Expand Down
82 changes: 82 additions & 0 deletions src/codegen/intrin_rule_aocl.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*!
* 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
4 changes: 2 additions & 2 deletions tests/python/integration/test_ewise_fpga.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def check_device(device, host="llvm"):
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
Expand Down Expand Up @@ -82,7 +82,7 @@ def check_device(device, host="llvm"):
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__":
Expand Down
3 changes: 2 additions & 1 deletion topi/tests/python/test_topi_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ def check_device(device):
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)


Expand Down

0 comments on commit a03c60b

Please sign in to comment.