Skip to content

Commit b8ce1d5

Browse files
committed
[MODULE] Enable OpenCL and CUDA Modules
1 parent efae4be commit b8ce1d5

27 files changed

+476
-335
lines changed

include/tvm/runtime/c_runtime_api.h

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -373,34 +373,6 @@ TVM_DLL int TVMFuncListGlobalNames(int *out_size,
373373
const char*** out_array);
374374

375375
// Array related apis for quick proptying
376-
/*!
377-
* \brief Initialize certain type of devices, this may
378-
* not be necessary for all device types. But is needed for OpenCL.
379-
*
380-
* \param dev_mask The device mask of device type to be initialized
381-
* \param option_keys Additional option keys to pass.
382-
* \param option_vals Additional option values to pass
383-
* \param num_options Number of options to be passed into it.
384-
* \param out_code 1: success, 0: already initialized
385-
* \return 0 when success, -1 when failure happens
386-
*/
387-
TVM_DLL int TVMDeviceInit(int dev_mask,
388-
const char** option_keys,
389-
const char** option_vals,
390-
int num_options,
391-
int *out_code);
392-
393-
394-
/*!
395-
* \brief Whether the specified context is enabled.
396-
*
397-
* \param ctx The context to be checked.
398-
* \param out_enabled whether the ctx is enabled.
399-
* \return Whether the function is successful.
400-
*/
401-
TVM_DLL int TVMContextEnabled(TVMContext ctx,
402-
int* out_enabled);
403-
404376
/*!
405377
* \brief Allocate a nd-array's memory,
406378
* including space of shape, of given spec.

include/tvm/runtime/packed_func.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -535,8 +535,9 @@ inline const char* TypeCode2Str(int type_code) {
535535
}
536536

537537
inline std::ostream& operator<<(std::ostream& os, TVMType t) { // NOLINT(*)
538-
os << TypeCode2Str(t.code)
539-
<< static_cast<int>(t.bits);
538+
os << TypeCode2Str(t.code);
539+
if (t.code == kHandle) return os;
540+
os << static_cast<int>(t.bits);
540541
if (t.lanes != 1) {
541542
os << 'x' << static_cast<int>(t.lanes);
542543
}
@@ -559,7 +560,7 @@ inline TVMType String2TVMType(std::string s) {
559560
t.code = kUInt; scan = s.c_str() + 4;
560561
} else if (s.substr(0, 5) == "float") {
561562
t.code = kFloat; scan = s.c_str() + 5;
562-
} else if (s == "handle") {
563+
} else if (s.substr(0, 6) == "handle") {
563564
t.code = kHandle;
564565
t.bits = 64; // handle uses 64 bit by default.
565566
scan = s.c_str() + 6;

python/tvm/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from . import module
1616

1717
from . import ndarray as nd
18-
from .ndarray import cpu, gpu, opencl, init_opencl, cl
18+
from .ndarray import cpu, gpu, opencl, cl
1919

2020
from ._base import TVMError
2121
from .api import *

python/tvm/_ctypes/_ndarray.py

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77
import numpy as np
88

99
from .._base import _LIB, check_call
10-
from .._base import c_array, c_str
10+
from .._base import c_array
1111
from ._types import TVMType, tvm_index_t
1212

13-
1413
class TVMContext(ctypes.Structure):
1514
"""TVM context strucure."""
1615
_fields_ = [("dev_mask", ctypes.c_int),
@@ -29,12 +28,6 @@ def __repr__(self):
2928
return "%s(%d)" % (
3029
TVMContext.MASK2STR[self.dev_mask], self.dev_id)
3130

32-
@property
33-
def enabled(self):
34-
ret = ctypes.c_int()
35-
check_call(_LIB.TVMContextEnabled(self, ctypes.byref(ret)))
36-
return ret.value != 0
37-
3831

3932
class TVMArray(ctypes.Structure):
4033
"""TVMValue in C API"""
@@ -141,30 +134,6 @@ def sync(ctx):
141134
check_call(_LIB.TVMSynchronize(ctx, None))
142135

143136

144-
def init_opencl(**kwargs):
145-
"""Initialize the opencl with the options.
146-
147-
Parameters
148-
----------
149-
kwargs : dict
150-
The options
151-
"""
152-
keys = []
153-
vals = []
154-
for k, v in kwargs.items():
155-
keys.append(c_str(k))
156-
vals.append(c_str(v))
157-
dev_mask = ctypes.c_int(4)
158-
out_code = ctypes.c_int()
159-
check_call(_LIB.TVMDeviceInit(
160-
dev_mask,
161-
c_array(ctypes.c_char_p, keys),
162-
c_array(ctypes.c_char_p, vals),
163-
ctypes.c_int(len(keys)),
164-
ctypes.byref(out_code)))
165-
return out_code.value != 0
166-
167-
168137
class NDArrayBase(object):
169138
"""A simple Device/CPU Array object in runtime."""
170139
__slots__ = ["handle"]

python/tvm/addon/testing.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""Utilities to make tempdir"""
2+
from __future__ import absolute_import as _abs
3+
import os
4+
import tempfile
5+
import shutil
6+
7+
class TempDirectory(object):
8+
"""Helper object to manage temp directory during testing"""
9+
def __init__(self):
10+
self.temp_dir = tempfile.mkdtemp()
11+
12+
def __del__(self):
13+
shutil.rmtree(self.temp_dir)
14+
15+
def relpath(self, name):
16+
"""Relative path in temp dir
17+
18+
Parameters
19+
----------
20+
name : str
21+
The name of the file.
22+
"""
23+
return os.path.join(self.temp_dir, name)
24+
25+
26+
def tempdir():
27+
"""Return a new temp dir which deletes the contents when exit
28+
29+
Returns
30+
-------
31+
temp : TempDirectory
32+
The temp directory object
33+
"""
34+
return TempDirectory()

python/tvm/libinfo.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# coding: utf-8
22
"""Information about nnvm."""
33
from __future__ import absolute_import
4+
import sys
45
import os
56
import platform
67

8+
79
def find_lib_path():
810
"""Find dynamic library files.
911
@@ -12,6 +14,7 @@ def find_lib_path():
1214
lib_path : list(string)
1315
List of all found path to the libraries
1416
"""
17+
use_runtime = os.environ.get("TVM_USE_RUNTIME_LIB", False)
1518
curr_path = os.path.dirname(os.path.abspath(os.path.expanduser(__file__)))
1619
api_path = os.path.join(curr_path, '../../lib/')
1720
cmake_build_path = os.path.join(curr_path, '../../build/Release/')
@@ -26,15 +29,24 @@ def find_lib_path():
2629
dll_path.append(os.path.join(curr_path, '../../windows', vs_configuration))
2730
elif os.name == "posix" and os.environ.get('LD_LIBRARY_PATH', None):
2831
dll_path.extend([p.strip() for p in os.environ['LD_LIBRARY_PATH'].split(":")])
32+
2933
if os.name == 'nt':
30-
dll_path = [os.path.join(p, 'libtvm.dll') for p in dll_path]
34+
lib_dll_path = [os.path.join(p, 'libtvm.dll') for p in dll_path]
35+
runtime_dll_path = [os.path.join(p, 'libtvm_runtime.dll') for p in dll_path]
3136
else:
32-
dll_path = [os.path.join(p, 'libtvm.so') for p in dll_path]
33-
lib_path = [p for p in dll_path if os.path.exists(p) and os.path.isfile(p)]
34-
if len(lib_path) == 0:
37+
lib_dll_path = [os.path.join(p, 'libtvm.so') for p in dll_path]
38+
runtime_dll_path = [os.path.join(p, 'libtvm_runtime.so') for p in dll_path]
39+
40+
dll_path = runtime_dll_path if use_runtime else lib_dll_path
41+
lib_found = [p for p in dll_path if os.path.exists(p) and os.path.isfile(p)]
42+
43+
if len(lib_found) == 0:
3544
raise RuntimeError('Cannot find the files.\n' +
3645
'List of candidates:\n' + str('\n'.join(dll_path)))
37-
return lib_path
46+
if use_runtime:
47+
sys.stderr.write("Loading runtime library... this is execution only\n")
48+
sys.stderr.flush()
49+
return lib_found
3850

3951

4052
# current version

python/tvm/ndarray.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from ._ctypes._ndarray import TVMContext, TVMType, NDArrayBase
1010
from ._ctypes._ndarray import cpu, gpu, opencl, empty, sync
1111
from ._ctypes._ndarray import _init_ndarray_module
12-
from ._ctypes._ndarray import init_opencl
1312
from ._ctypes._function import Function
1413

1514
cl = opencl

src/api/api_codegen.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ TVM_REGISTER_API(_codegen_build)
2121
}
2222
});
2323

24-
TVM_REGISTER_API(_codegen_target_enabled)
24+
TVM_REGISTER_API(_codegen_enabled)
2525
.set_body([](TVMArgs args, TVMRetValue *ret) {
2626
*ret = TargetEnabled(args[0]);
2727
});

src/codegen/build_cuda.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,13 @@ runtime::Module BuildCUDA(Array<LoweredFunc> funcs) {
6161
if (const auto* f = Registry::Get("tvm_callback_cuda_postproc")) {
6262
code = (*f)(code).operator std::string();
6363
}
64-
64+
std::string fmt = "ptx";
6565
std::string ptx;
6666
if (const auto* f = Registry::Get("tvm_callback_cuda_compile")) {
6767
ptx = (*f)(code).operator std::string();
68+
// Dirty matching to check PTX vs cubin.
69+
// TODO(tqchen) more reliable checks
70+
if (ptx[0] != '/') fmt = "cubin";
6871
} else {
6972
ptx = NVRTCCompile(code);
7073
}
@@ -80,7 +83,7 @@ runtime::Module BuildCUDA(Array<LoweredFunc> funcs) {
8083
}
8184
fmap[f->name] = info;
8285
}
83-
return CUDAModuleCreate(ptx, "ptx", fmap, code);
86+
return CUDAModuleCreate(ptx, fmt, fmap, code);
8487
}
8588

8689
TVM_REGISTER_API(_codegen_build_cuda)

src/runtime/c_runtime_api.cc

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -200,38 +200,6 @@ int TVMFuncCreateFromCFunc(TVMPackedCFunc func,
200200
API_END();
201201
}
202202

203-
int TVMDeviceInit(int dev_mask,
204-
const char** option_keys,
205-
const char** option_vals,
206-
int num_options,
207-
int* out_code) {
208-
API_BEGIN();
209-
*out_code = 1;
210-
switch (dev_mask) {
211-
case kOpenCL: {
212-
*out_code = DeviceInit<kOpenCL>(option_keys, option_vals, num_options);
213-
break;
214-
}
215-
default: break;
216-
}
217-
API_END();
218-
}
219-
220-
int TVMContextEnabled(TVMContext ctx,
221-
int* out_enabled) {
222-
API_BEGIN();
223-
if (ctx.dev_mask == kGPU && TVM_CUDA_RUNTIME == 0) {
224-
*out_enabled = 0;
225-
} else if (ctx.dev_mask == kOpenCL && TVM_OPENCL_RUNTIME == 0) {
226-
*out_enabled = 0;
227-
} else {
228-
TVM_DEVICE_SWITCH(ctx, {
229-
*out_enabled = CheckEnabled<xpu>(ctx);
230-
});
231-
}
232-
API_END();
233-
}
234-
235203
int TVMArrayAlloc(const tvm_index_t* shape,
236204
tvm_index_t ndim,
237205
TVMType dtype,

0 commit comments

Comments
 (0)