merrymercy / tvm-mali

Optimizing Mobile Deep Learning on ARM GPU with TVM
http://tvmlang.org/2018/01/16/opt-mali-gpu.html
MIT License
179 stars 28 forks source link

how to save the generated opencl code #2

Closed janboeye closed 6 years ago

janboeye commented 6 years ago

hi,

This is a great job.

How to save the generated opencl code into a file?

Thanks

merrymercy commented 6 years ago

After you build the function, you can get the opencl source code.

s = tvm.create_schedule(C.op)
func = tvm.build(s, [A, B, C], target='opencl')
src = func.imported_modules[0].get_source()

Variable src is a string of opencl code. Then you can write it to a file. See the tutorial (http://docs.tvmlang.org/tutorials/get_started.html) for more explanation on this example.

For the convolution example in my blog post, I get the source code by uncommenting line 135 https://github.com/merrymercy/tvm-mali/blob/8b0ba396607839397ff1884051d3e8d58c0e4298/layer-test/test_conv2d.py#L131-L135

janboeye commented 6 years ago

@merrymercy Thanks How could I get opencl kernel after nnvm.compiler.build?

merrymercy commented 6 years ago

After this block https://github.com/merrymercy/tvm-mali/blob/90395b509e01e6de6b83f5f2f8c4715dd5a024e9/mali_imagenet_bench.py#L32-L35 insert this line

print(lib.imported_modules[0].get_source())
janboeye commented 6 years ago

Thanks a lot! What mean is lib.imported_modules? And how to get host runtime source code too?

merrymercy commented 6 years ago

lib is the host module (llvm ir) lib.imported_modules is the device module (cuda, opencl)

lib.get_source()  # get host llvm ir
lib.imported_modules[0].get_source() # get opencl/cuda source
janboeye commented 6 years ago

There is no way to get host c code, only LLVM IR, right? export_library does not generate opencl runtime code, right?

janboeye commented 6 years ago

Is there any document to explain how this host library and opencl are executed on target platform?

merrymercy commented 6 years ago
  1. TVM translate its Halide IR to LLVM IR directly, so there is no host c code.
  2. nnvm.compiler.build generates both host code and opencl code. export_library only saves them to files

Since the original question is answered, we can close this issue for now and open new issue on tvm repo.