Skip to content

Commit

Permalink
[Refactor] Rename TVMContext to Device (#7721)
Browse files Browse the repository at this point in the history
  • Loading branch information
icemelon committed Mar 26, 2021
1 parent 8c8971a commit fbfeee4
Show file tree
Hide file tree
Showing 513 changed files with 5,761 additions and 5,765 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
import org.apache.tvm.Function;
import org.apache.tvm.Module;
import org.apache.tvm.NDArray;
import org.apache.tvm.TVMContext;
import org.apache.tvm.Device;
import org.apache.tvm.TVMType;
import org.apache.tvm.TVMValue;
import org.json.JSONException;
Expand Down Expand Up @@ -571,9 +571,9 @@ protected Integer doInBackground(Void... args) {
return -1;//failure
}

Log.i(TAG, "creating java tvm context...");
// create java tvm context
TVMContext tvmCtx = EXE_GPU ? TVMContext.opencl() : TVMContext.cpu();
Log.i(TAG, "creating java tvm device...");
// create java tvm device
Device tvmDev = EXE_GPU ? Device.opencl() : Device.cpu();

Log.i(TAG, "loading compiled functions...");
Log.i(TAG, libCacheFilePath);
Expand All @@ -587,13 +587,13 @@ protected Integer doInBackground(Void... args) {
Function runtimeCreFun = Function.getFunction("tvm.graph_runtime.create");
Log.i(TAG, "creating graph runtime...");

Log.i(TAG, "ctx type: " + tvmCtx.deviceType);
Log.i(TAG, "ctx id: " + tvmCtx.deviceId);
Log.i(TAG, "device type: " + tvmDev.deviceType);
Log.i(TAG, "device id: " + tvmDev.deviceId);

TVMValue runtimeCreFunRes = runtimeCreFun.pushArg(modelGraph)
.pushArg(modelLib)
.pushArg(tvmCtx.deviceType)
.pushArg(tvmCtx.deviceId)
.pushArg(tvmDev.deviceType)
.pushArg(tvmDev.deviceId)
.invoke();

Log.i(TAG, "as module...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
import org.apache.tvm.Function;
import org.apache.tvm.Module;
import org.apache.tvm.NDArray;
import org.apache.tvm.TVMContext;
import org.apache.tvm.Device;
import org.apache.tvm.TVMValue;
import org.apache.tvm.TVMType;

Expand Down Expand Up @@ -177,8 +177,8 @@ protected Integer doInBackground(Void... args) {
return -1;//failure
}

// create java tvm context
TVMContext tvmCtx = EXE_GPU ? TVMContext.opencl() : TVMContext.cpu();
// create java tvm device
Device tvmDev = EXE_GPU ? Device.opencl() : Device.cpu();

// tvm module for compiled functions
Module modelLib = Module.load(libCacheFilePath);
Expand All @@ -187,8 +187,8 @@ protected Integer doInBackground(Void... args) {
Function runtimeCreFun = Function.getFunction("tvm.graph_runtime.create");
TVMValue runtimeCreFunRes = runtimeCreFun.pushArg(modelGraph)
.pushArg(modelLib)
.pushArg(tvmCtx.deviceType)
.pushArg(tvmCtx.deviceId)
.pushArg(tvmDev.deviceType)
.pushArg(tvmDev.deviceId)
.invoke();
graphRuntimeModule = runtimeCreFunRes.asModule();

Expand Down
24 changes: 12 additions & 12 deletions apps/android_rpc/tests/android_rpc_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ def test_rpc_module():

# Execute the portable graph on cpu target
print("Run CPU test ...")
ctx = remote.cpu(0)
dev = remote.cpu(0)
remote.upload(path_dso_cpu)
f2 = remote.load_module("cpu_lib.so")
a = tvm.nd.array(a_np, ctx)
b = tvm.nd.array(np.zeros(1024, dtype=A.dtype), ctx)
time_f = f2.time_evaluator(f2.entry_name, ctx, number=10)
a = tvm.nd.array(a_np, dev)
b = tvm.nd.array(np.zeros(1024, dtype=A.dtype), dev)
time_f = f2.time_evaluator(f2.entry_name, dev, number=10)
cost = time_f(a, b).mean
print("%g secs/op\n" % cost)
np.testing.assert_equal(b.asnumpy(), a.asnumpy() + 1)
Expand All @@ -91,12 +91,12 @@ def test_rpc_module():
f.export_library(path_dso_cl, ndk.create_shared)

print("Run GPU(OpenCL Flavor) test ...")
ctx = remote.cl(0)
dev = remote.cl(0)
remote.upload(path_dso_cl)
f1 = remote.load_module("dev_lib_cl.so")
a = tvm.nd.array(a_np, ctx)
b = tvm.nd.array(np.zeros(1024, dtype=A.dtype), ctx)
time_f = f1.time_evaluator(f1.entry_name, ctx, number=10)
a = tvm.nd.array(a_np, dev)
b = tvm.nd.array(np.zeros(1024, dtype=A.dtype), dev)
time_f = f1.time_evaluator(f1.entry_name, dev, number=10)
cost = time_f(a, b).mean
print("%g secs/op\n" % cost)
np.testing.assert_equal(b.asnumpy(), a.asnumpy() + 1)
Expand All @@ -114,12 +114,12 @@ def test_rpc_module():
f.export_library(path_dso_vulkan, ndk.create_shared)

print("Run GPU(Vulkan Flavor) test ...")
ctx = remote.vulkan(0)
dev = remote.vulkan(0)
remote.upload(path_dso_vulkan)
f1 = remote.load_module("dev_lib_vulkan.so")
a = tvm.nd.array(a_np, ctx)
b = tvm.nd.array(np.zeros(1024, dtype=A.dtype), ctx)
time_f = f1.time_evaluator(f1.entry_name, ctx, number=10)
a = tvm.nd.array(a_np, dev)
b = tvm.nd.array(np.zeros(1024, dtype=A.dtype), dev)
time_f = f1.time_evaluator(f1.entry_name, dev, number=10)
cost = time_f(a, b).mean
print("%g secs/op\n" % cost)
np.testing.assert_equal(b.asnumpy(), a.asnumpy() + 1)
Expand Down
6 changes: 3 additions & 3 deletions apps/benchmark/arm_cpu_imagenet_bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@ def evaluate_network(network, target, target_host, repeat):

# upload library and params
print_progress("%-20s uploading..." % network)
ctx = remote.context(str(target), 0)
dev = remote.device(str(target), 0)
remote.upload(tmp.relpath(filename))

rlib = remote.load_module(filename)
module = runtime.GraphModule(rlib["default"](ctx))
module = runtime.GraphModule(rlib["default"](dev))
data_tvm = tvm.nd.array((np.random.uniform(size=input_shape)).astype(dtype))
module.set_input("data", data_tvm)

# evaluate
print_progress("%-20s evaluating..." % network)
ftimer = module.module.time_evaluator("run", ctx, number=1, repeat=repeat)
ftimer = module.module.time_evaluator("run", dev, number=1, repeat=repeat)
prof_res = np.array(ftimer().results) * 1000 # multiply 1000 for converting to millisecond
print(
"%-20s %-19s (%s)" % (network, "%.2f ms" % np.mean(prof_res), "%.2f ms" % np.std(prof_res))
Expand Down
6 changes: 3 additions & 3 deletions apps/benchmark/gpu_imagenet_bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ def benchmark(network, target):
lib = relay.build(net, target=target, params=params)

# create runtime
ctx = tvm.context(str(target), 0)
module = runtime.GraphModule(lib["default"](ctx))
dev = tvm.device(str(target), 0)
module = runtime.GraphModule(lib["default"](dev))
data_tvm = tvm.nd.array((np.random.uniform(size=input_shape)).astype(dtype))
module.set_input("data", data_tvm)

# evaluate
ftimer = module.module.time_evaluator("run", ctx, number=1, repeat=args.repeat)
ftimer = module.module.time_evaluator("run", dev, number=1, repeat=args.repeat)
prof_res = np.array(ftimer().results) * 1000 # multiply 1000 for converting to millisecond
print(
"%-20s %-19s (%s)" % (network, "%.2f ms" % np.mean(prof_res), "%.2f ms" % np.std(prof_res))
Expand Down
6 changes: 3 additions & 3 deletions apps/benchmark/mobile_gpu_imagenet_bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@ def evaluate_network(network, target, target_host, dtype, repeat):

# upload library and params
print_progress("%-20s uploading..." % network)
ctx = remote.context(str(target), 0)
dev = remote.device(str(target), 0)
remote.upload(tmp.relpath(filename))

rlib = remote.load_module(filename)
module = runtime.GraphModule(rlib["default"](ctx))
module = runtime.GraphModule(rlib["default"](dev))
data_tvm = tvm.nd.array((np.random.uniform(size=input_shape)).astype(dtype))
module.set_input("data", data_tvm)

# evaluate
print_progress("%-20s evaluating..." % network)
ftimer = module.module.time_evaluator("run", ctx, number=1, repeat=repeat)
ftimer = module.module.time_evaluator("run", dev, number=1, repeat=repeat)
prof_res = np.array(ftimer().results) * 1000 # multiply 1000 for converting to millisecond
print(
"%-20s %-19s (%s)" % (network, "%.2f ms" % np.mean(prof_res), "%.2f ms" % np.std(prof_res))
Expand Down
16 changes: 8 additions & 8 deletions apps/bundle_deploy/bundle.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ TVM_DLL void* tvm_runtime_create(const char* json_data, const char* params_data,
params.data = params_data;
params.size = params_size;

TVMContext ctx;
ctx.device_type = (DLDeviceType)device_type;
ctx.device_id = device_id;
DLDevice dev;
dev.device_type = (DLDeviceType)device_type;
dev.device_id = device_id;

// declare pointers
TVM_CCALL(MemoryManagerCreate(&g_memory_manager, g_crt_memory, sizeof(g_crt_memory),
Expand All @@ -76,7 +76,7 @@ TVM_DLL void* tvm_runtime_create(const char* json_data, const char* params_data,

// run modules
TVMGraphRuntime* graph_runtime = NULL;
TVM_CCALL(TVMGraphRuntime_Create(json_data, mod_syslib, &ctx, &graph_runtime));
TVM_CCALL(TVMGraphRuntime_Create(json_data, mod_syslib, &dev, &graph_runtime));
TVM_CCALL(TVMGraphRuntime_LoadParams(graph_runtime, params.data, params.size));

return graph_runtime;
Expand Down Expand Up @@ -116,12 +116,12 @@ void __attribute__((noreturn)) TVMPlatformAbort(tvm_crt_error_t error_code) {
exit(-1);
}

tvm_crt_error_t TVMPlatformMemoryAllocate(size_t num_bytes, DLContext ctx, void** out_ptr) {
return g_memory_manager->Allocate(g_memory_manager, num_bytes, ctx, out_ptr);
tvm_crt_error_t TVMPlatformMemoryAllocate(size_t num_bytes, DLDevice dev, void** out_ptr) {
return g_memory_manager->Allocate(g_memory_manager, num_bytes, dev, out_ptr);
}

tvm_crt_error_t TVMPlatformMemoryFree(void* ptr, DLContext ctx) {
return g_memory_manager->Free(g_memory_manager, ptr, ctx);
tvm_crt_error_t TVMPlatformMemoryFree(void* ptr, DLDevice dev) {
return g_memory_manager->Free(g_memory_manager, ptr, dev);
}

tvm_crt_error_t TVMPlatformTimerStart() { return kTvmErrorFunctionCallNotImplemented; }
Expand Down
16 changes: 8 additions & 8 deletions apps/bundle_deploy/bundle_static.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ TVM_DLL void* tvm_runtime_create(const char* json_data, const char* params_data,
params.data = params_data;
params.size = params_size;

TVMContext ctx;
ctx.device_type = (DLDeviceType)device_type;
ctx.device_id = device_id;
DLDevice dev;
dev.device_type = (DLDeviceType)device_type;
dev.device_id = device_id;

// get pointers
TVM_CCALL(MemoryManagerCreate(&g_memory_manager, g_crt_memory, sizeof(g_crt_memory),
Expand All @@ -76,7 +76,7 @@ TVM_DLL void* tvm_runtime_create(const char* json_data, const char* params_data,

// run modules
TVMGraphRuntime* graph_runtime = NULL;
TVM_CCALL(TVMGraphRuntime_Create(json_data, mod_syslib, &ctx, &graph_runtime));
TVM_CCALL(TVMGraphRuntime_Create(json_data, mod_syslib, &dev, &graph_runtime));
TVM_CCALL(TVMGraphRuntime_LoadParams(graph_runtime, params.data, params.size));

return graph_runtime;
Expand Down Expand Up @@ -117,12 +117,12 @@ void __attribute__((noreturn)) TVMPlatformAbort(tvm_crt_error_t error_code) {
exit(-1);
}

tvm_crt_error_t TVMPlatformMemoryAllocate(size_t num_bytes, DLContext ctx, void** out_ptr) {
return g_memory_manager->Allocate(g_memory_manager, num_bytes, ctx, out_ptr);
tvm_crt_error_t TVMPlatformMemoryAllocate(size_t num_bytes, DLDevice dev, void** out_ptr) {
return g_memory_manager->Allocate(g_memory_manager, num_bytes, dev, out_ptr);
}

tvm_crt_error_t TVMPlatformMemoryFree(void* ptr, DLContext ctx) {
return g_memory_manager->Free(g_memory_manager, ptr, ctx);
tvm_crt_error_t TVMPlatformMemoryFree(void* ptr, DLDevice dev) {
return g_memory_manager->Free(g_memory_manager, ptr, dev);
}

tvm_crt_error_t TVMPlatformTimerStart() { return kTvmErrorFunctionCallNotImplemented; }
Expand Down
4 changes: 2 additions & 2 deletions apps/bundle_deploy/demo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ int main(int argc, char** argv) {
std::vector<int64_t> input_shape = {1, 3, 224, 224};
DLTensor input;
input.data = input_storage;
input.ctx = DLContext{kDLCPU, 0};
input.device = DLDevice{kDLCPU, 0};
input.ndim = 4;
input.dtype = DLDataType{kDLFloat, 32, 1};
input.shape = input_shape.data();
Expand All @@ -138,7 +138,7 @@ int main(int argc, char** argv) {
std::vector<int64_t> output_shape = {1, 1000};
DLTensor output;
output.data = output_storage;
output.ctx = DLContext{kDLCPU, 0};
output.device = DLDevice{kDLCPU, 0};
output.ndim = 2;
output.dtype = DLDataType{kDLFloat, 32, 1};
output.shape = output_shape.data();
Expand Down
8 changes: 4 additions & 4 deletions apps/bundle_deploy/demo_static.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ int main(int argc, char** argv) {

DLTensor input;
input.data = input_storage;
DLContext ctx = {kDLCPU, 0};
input.ctx = ctx;
DLDevice dev = {kDLCPU, 0};
input.device = dev;
input.ndim = 4;
DLDataType dtype = {kDLFloat, 32, 1};
input.dtype = dtype;
Expand All @@ -73,8 +73,8 @@ int main(int argc, char** argv) {
float output_storage[OUTPUT_LEN];
DLTensor output;
output.data = output_storage;
DLContext out_ctx = {kDLCPU, 0};
output.ctx = out_ctx;
DLDevice out_dev = {kDLCPU, 0};
output.device = out_dev;
output.ndim = 2;
DLDataType out_dtype = {kDLFloat, 32, 1};
output.dtype = out_dtype;
Expand Down
4 changes: 2 additions & 2 deletions apps/bundle_deploy/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ int main(int argc, char** argv) {
std::vector<int64_t> input_shape = {10, 5};
DLTensor input;
input.data = input_storage;
input.ctx = DLContext{kDLCPU, 0};
input.device = DLDevice{kDLCPU, 0};
input.ndim = 2;
input.dtype = DLDataType{kDLFloat, 32, 1};
input.shape = input_shape.data();
Expand All @@ -133,7 +133,7 @@ int main(int argc, char** argv) {
std::vector<int64_t> output_shape = {10, 5};
DLTensor output;
output.data = output_storage;
output.ctx = DLContext{kDLCPU, 0};
output.device = DLDevice{kDLCPU, 0};
output.ndim = 2;
output.dtype = DLDataType{kDLFloat, 32, 1};
output.shape = output_shape.data();
Expand Down
8 changes: 4 additions & 4 deletions apps/bundle_deploy/test_static.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ int main(int argc, char** argv) {

DLTensor input;
input.data = input_storage;
DLContext ctx = {kDLCPU, 0};
input.ctx = ctx;
DLDevice dev = {kDLCPU, 0};
input.device = dev;
input.ndim = 2;
DLDataType dtype = {kDLFloat, 32, 1};
input.dtype = dtype;
Expand All @@ -85,8 +85,8 @@ int main(int argc, char** argv) {
float output_storage[10 * 5];
DLTensor output;
output.data = output_storage;
DLContext out_ctx = {kDLCPU, 0};
output.ctx = out_ctx;
DLDevice out_dev = {kDLCPU, 0};
output.device = out_dev;
output.ndim = 2;
DLDataType out_dtype = {kDLFloat, 32, 1};
output.dtype = out_dtype;
Expand Down
12 changes: 6 additions & 6 deletions apps/extension/tests/test_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ def check_llvm():
if not tvm.testing.device_enabled("llvm"):
return
f = tvm.build(s, [A, B], "ext_dev", "llvm")
ctx = tvm.ext_dev(0)
dev = tvm.ext_dev(0)
# launch the kernel.
a = tvm.nd.array(np.random.uniform(size=n).astype(A.dtype), ctx)
b = tvm.nd.array(np.zeros(n, dtype=B.dtype), ctx)
a = tvm.nd.array(np.random.uniform(size=n).astype(A.dtype), dev)
b = tvm.nd.array(np.zeros(n, dtype=B.dtype), dev)
f(a, b)
tvm.testing.assert_allclose(b.asnumpy(), a.asnumpy() + 1)

Expand Down Expand Up @@ -87,10 +87,10 @@ def check_llvm():
if not tvm.testing.device_enabled("llvm"):
return
f = tvm.build(s, [A, B], "llvm")
ctx = tvm.cpu(0)
dev = tvm.cpu(0)
# launch the kernel.
a = tvm.nd.array(np.random.uniform(size=n).astype(A.dtype), ctx)
b = tvm.nd.array(np.zeros(n, dtype=B.dtype), ctx)
a = tvm.nd.array(np.random.uniform(size=n).astype(A.dtype), dev)
b = tvm.nd.array(np.zeros(n, dtype=B.dtype), dev)
f(a, b)
tvm.testing.assert_allclose(b.asnumpy(), a.asnumpy() + 1)

Expand Down
8 changes: 4 additions & 4 deletions apps/howto_deploy/cpp_deploy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,17 @@ void DeploySingleOp() {
void DeployGraphRuntime() {
LOG(INFO) << "Running graph runtime...";
// load in the library
DLContext ctx{kDLCPU, 0};
DLDevice dev{kDLCPU, 0};
tvm::runtime::Module mod_factory = tvm::runtime::Module::LoadFromFile("lib/test_relay_add.so");
// create the graph runtime module
tvm::runtime::Module gmod = mod_factory.GetFunction("default")(ctx);
tvm::runtime::Module gmod = mod_factory.GetFunction("default")(dev);
tvm::runtime::PackedFunc set_input = gmod.GetFunction("set_input");
tvm::runtime::PackedFunc get_output = gmod.GetFunction("get_output");
tvm::runtime::PackedFunc run = gmod.GetFunction("run");

// Use the C++ API
tvm::runtime::NDArray x = tvm::runtime::NDArray::Empty({2, 2}, DLDataType{kDLFloat, 32, 1}, ctx);
tvm::runtime::NDArray y = tvm::runtime::NDArray::Empty({2, 2}, DLDataType{kDLFloat, 32, 1}, ctx);
tvm::runtime::NDArray x = tvm::runtime::NDArray::Empty({2, 2}, DLDataType{kDLFloat, 32, 1}, dev);
tvm::runtime::NDArray y = tvm::runtime::NDArray::Empty({2, 2}, DLDataType{kDLFloat, 32, 1}, dev);

for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
Expand Down
Loading

0 comments on commit fbfeee4

Please sign in to comment.