Skip to content

Commit fba53c5

Browse files
committed
add integration test_add_vtcm to demo vtcm alloc
1 parent 43176a6 commit fba53c5

File tree

5 files changed

+60
-266
lines changed

5 files changed

+60
-266
lines changed

src/runtime/hexagon/hexagon/hexagon_buffer.cc

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
#include "hexagon_common.h"
2727

28-
#ifdef BUILD_FOR_HEXAGON
28+
#if defined(__hexagon__)
2929
#include "HAP_compute_res.h"
3030
#endif
3131

@@ -68,19 +68,18 @@ struct DDRAllocation : public Allocation {
6868
}
6969
};
7070

71-
#ifdef BUILD_FOR_HEXAGON
71+
#if defined(__hexagon__)
7272
struct VTCMAllocation : public Allocation {
7373
VTCMAllocation(size_t nbytes, size_t alignment) : Allocation(nbytes, alignment) {
74-
// TODO(Straw): Alignment not used when allocating VTCM
7574
compute_res_attr_t res_info;
7675
HEXAGON_SAFE_CALL(HAP_compute_res_attr_init(&res_info));
77-
// TODO(Straw): Magic number 1
78-
HEXAGON_SAFE_CALL(HAP_compute_res_attr_set_vtcm_param(&res_info, nbytes, 1));
79-
// TODO(Straw): HEXAGON_SAFE_CALL?
80-
// TODO(Straw): Magic number 10000
81-
context_id_ = HAP_compute_res_acquire(&res_info, 10000);
76+
77+
// allocate nbytes of vtcm on a single page
78+
HEXAGON_SAFE_CALL(HAP_compute_res_attr_set_vtcm_param(&res_info, /*vtcm_size = */ nbytes,
79+
/*b_single_page = */ 1));
80+
context_id_ = HAP_compute_res_acquire(&res_info, /*timeout = */ 10000);
81+
8282
if (context_id_) {
83-
// TODO(Straw): HEXAGON_SAFE_CALL?
8483
data_ = HAP_compute_res_attr_get_vtcm_ptr(&res_info);
8584
if (!data_) {
8685
HEXAGON_PRINT(ERROR, "ERROR: Allocated VTCM ptr is null.");
@@ -96,11 +95,8 @@ struct VTCMAllocation : public Allocation {
9695
~VTCMAllocation() {
9796
// HEXAGON_PRINT(ALWAYS, "~VTCMAllocation() - Context ID: %u, VTCM ptr: %p", context_id_,
9897
// data_);
99-
// TODO(Straw): Need to handle the else case(s) here
100-
if (context_id_ && data_) {
101-
HEXAGON_SAFE_CALL(HAP_compute_res_release(context_id_));
102-
data_ = nullptr;
103-
}
98+
HEXAGON_SAFE_CALL(HAP_compute_res_release(context_id_));
99+
data_ = nullptr;
104100
}
105101
unsigned int context_id_{0};
106102
};

src/runtime/hexagon/hexagon/hexagon_common.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ PackedFunc WrapPackedFunc(TVMBackendPackedCFunc faddr, const ObjectPtr<Object>&
8787
if (args.type_codes[i] == kTVMDLTensorHandle) {
8888
DLTensor* tensor = static_cast<DLTensor*>(arg_values[i].v_handle);
8989
buffer_args.emplace_back(i, static_cast<HexagonBuffer*>(tensor->data));
90-
tensor->data = buffer_args.back().second->GetPointer();
90+
tensor->data = buffer_args.back().second->GetPointer()[0];
9191
}
9292
}
9393
int ret = (*faddr)(const_cast<TVMValue*>(args.values), const_cast<int*>(args.type_codes),

src/runtime/hexagon/hexagon/hexagon_device_api_v2.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ void* HexagonDeviceAPIv2::AllocWorkspace(Device dev, size_t size, DLDataType typ
106106
void HexagonDeviceAPIv2::FreeWorkspace(Device dev, void* data) {
107107
CHECK(TVMDeviceExtType(dev.device_type) == kDLHexagon);
108108
auto it = workspace_allocations_.find(data);
109-
ICHECK(it != workspace_allocations_.end())
109+
CHECK(it != workspace_allocations_.end())
110110
<< "Attempt made to free unknown or already freed workspace allocation";
111111
dmlc::ThreadLocalStore<HexagonWorkspacePool>::Get()->FreeWorkspace(dev, it->second);
112112
workspace_allocations_.erase(it);

tests/cpp/hexagon_test.cc

Lines changed: 0 additions & 250 deletions
This file was deleted.

tests/python/contrib/test_hexagon/rpc/test_launcher.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,54 @@ def test_add(tvm_tracker_host, tvm_tracker_port, android_serial_number):
7676
launcher.close()
7777

7878

79+
@requires_rpc_tracker
80+
@requires_hexagon_toolchain
81+
def test_add_vtcm(tvm_tracker_host, tvm_tracker_port, android_serial_number):
82+
dtype = "int8"
83+
A = tvm.te.placeholder((2,), dtype=dtype)
84+
B = tvm.te.placeholder((1,), dtype=dtype)
85+
C = tvm.te.compute(A.shape, lambda i: A[i] + B[0], name="C")
86+
sched = tvm.te.create_schedule(C.op)
87+
88+
target_hexagon = tvm.target.hexagon("v68", link_params=True)
89+
func = tvm.build(
90+
sched, [A, B, C], tvm.target.Target(target_hexagon, host=target_hexagon), name="add"
91+
)
92+
93+
temp = utils.tempdir()
94+
dso_binary = "test_binary.so"
95+
dso_binary_path = temp.relpath(dso_binary)
96+
func.save(dso_binary_path)
97+
98+
launcher = HexagonLauncher(serial_number=android_serial_number)
99+
launcher.android_run_rpc(rpc_tracker_host=tvm_tracker_host, rpc_tracker_port=tvm_tracker_port)
100+
launcher.hexagon_setup()
101+
remote_kw = {
102+
"host": tvm_tracker_host,
103+
"port": tvm_tracker_port,
104+
"priority": 0,
105+
"timeout": 60,
106+
}
107+
launcher.hexagon_session_setup(remote_kw)
108+
launcher.upload(dso_binary_path, dso_binary)
109+
110+
with launcher.session as sess:
111+
mod = launcher.get_module(dso_binary)
112+
A_data = tvm.nd.empty(A.shape, A.dtype, sess.device, "global.vtcm")
113+
A_data.copyfrom(np.array([2, 3]))
114+
115+
B_data = tvm.nd.empty(B.shape, B.dtype, sess.device, "global.vtcm")
116+
B_data.copyfrom(np.array([4]))
117+
118+
C_data = tvm.nd.empty(C.shape, C.dtype, sess.device, "global.vtcm")
119+
C_data.copyfrom(np.array([0, 0]))
120+
121+
mod["add"](A_data, B_data, C_data)
122+
result = C_data.numpy()
123+
assert (result == np.array([6, 7])).all()
124+
launcher.close()
125+
126+
79127
class TestMatMul:
80128
M = tvm.testing.parameter(32)
81129
N = tvm.testing.parameter(32)

0 commit comments

Comments
 (0)