Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BACKEND][CODEGEN] C codegen with tests #2161

Merged
merged 11 commits into from
Nov 28, 2018
Merged

[BACKEND][CODEGEN] C codegen with tests #2161

merged 11 commits into from
Nov 28, 2018

Conversation

Mutinifni
Copy link
Contributor

This PR introduces a gcc / g++ compatible C code generator for TVM, tested on x86 host.

@tqchen
Copy link
Member

tqchen commented Nov 24, 2018

@Huyuwei can you help review this?

@Huyuwei
Copy link
Contributor

Huyuwei commented Nov 24, 2018

@Mutinifni Can you briefly describe the current compilation flow? Most importantly, is it using DSOModule for the runtime?

@Mutinifni
Copy link
Contributor Author

@Huyuwei It uses CSourceModule for saving the C source code - I have verified this by checking the type_key. Loading is done via the DSOModule.

I was a bit uncertain about this module design and the export_library functionality as well, so please feel let me know if you think a different flow would be better instead.

python/tvm/module.py Show resolved Hide resolved
python/tvm/module.py Outdated Show resolved Hide resolved
src/codegen/codegen_c_host.cc Outdated Show resolved Hide resolved
@tqchen tqchen changed the title C codegen with tests [BACKEND] C codegen with tests Nov 25, 2018
@tqchen tqchen changed the title [BACKEND] C codegen with tests [BACKEND][CODEGEN] C codegen with tests Nov 25, 2018
src/codegen/codegen_c_host.cc Outdated Show resolved Hide resolved
python/tvm/module.py Outdated Show resolved Hide resolved
src/codegen/codegen_c.cc Outdated Show resolved Hide resolved
src/codegen/codegen_c.h Show resolved Hide resolved
namespace codegen {

CodeGenCHost::CodeGenCHost() {
module_name = GetUniqueName("__tvm_module_ctx");
Copy link
Contributor

@Huyuwei Huyuwei Nov 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace "__tvm_module_ctx" with tvm::runtime::symbol::tvm_module_ctx?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would make the generated code necessarily dependent on C++, but we want to keep the option of pure C as well. If there's a reason to use the latter symbol, I can change it.

src/codegen/codegen_c_host.cc Show resolved Hide resolved
src/codegen/codegen_c_host.h Outdated Show resolved Hide resolved
python/tvm/contrib/cc.py Outdated Show resolved Hide resolved
@tqchen
Copy link
Member

tqchen commented Nov 27, 2018

@Huyuwei please take another look

@Huyuwei Huyuwei merged commit a259b73 into apache:master Nov 28, 2018
@srkreddy1238
Copy link
Contributor

This PR cause the below issue with llvm target.

-tgt="cuda"
+tgt="llvm"

python3 tutorials/get_started.py

!115 = !{!116, !116, i64 0}
!116 = !{!"float32", !117, i64 0}
!117 = !{!"0x119a190", !4, i64 0}

Traceback (most recent call last):
  File "get_started.py", line 196, in <module>
    cc.create_shared(temp.relpath("myadd.so"), [temp.relpath("myadd.o")])
  File "/home/srk/.local/lib/python3.6/site-packages/tvm-0.5.dev0-py3.6-linux-x86_64.egg/tvm/contrib/cc.py", line 34, in create_shared
    _linux_shared(output, objects, options, cc)
  File "/home/srk/.local/lib/python3.6/site-packages/tvm-0.5.dev0-py3.6-linux-x86_64.egg/tvm/contrib/cc.py", line 53, in _linux_shared
    cmd += ["-I" + path for path in find_include_path()]
  File "/home/srk/.local/lib/python3.6/site-packages/tvm-0.5.dev0-py3.6-linux-x86_64.egg/tvm/_ffi/libinfo.py", line 156, in find_include_path
    raise RuntimeError(message)
RuntimeError: Cannot find the files.
List of candidates:
/home/srk/.local/lib/python3.6/include
/home/srk/.local/lib/python3.6/site-packages/include
/home/srk/.local/lib/python3.6/site-packages/3rdparty/include
/home/srk/.local/lib/python3.6/dlpack/include
/home/srk/.local/lib/python3.6/site-packages/dlpack/include
/home/srk/.local/lib/python3.6/site-packages/3rdparty/dlpack/include

@tqchen
Copy link
Member

tqchen commented Nov 29, 2018

Thanks @srkreddy1238 for reporting this, @Mutinifni can you send in a quick fix? We should not directly raise error when the headers are not found in general create_shared, instead, we should only call find_include_path and add it to the options in created_shared when it is a c module

FrozenGene pushed a commit to FrozenGene/tvm that referenced this pull request Dec 27, 2018
* Implement C code generation with tests

* Code cleanup

* Implement C code generation with tests

* Code cleanup

* tabs to spaces

* make lint compliant

* update export_library and reserve unique C keywords

* move ReserveKeywordsAsUnique to codegen_c

* some documentation and code cleanup

* use tvm.contrib.util for tempdir in testcases
@ZihengJiang ZihengJiang mentioned this pull request Feb 1, 2019
wweic pushed a commit to neo-ai/tvm that referenced this pull request Feb 20, 2019
* Implement C code generation with tests

* Code cleanup

* Implement C code generation with tests

* Code cleanup

* tabs to spaces

* make lint compliant

* update export_library and reserve unique C keywords

* move ReserveKeywordsAsUnique to codegen_c

* some documentation and code cleanup

* use tvm.contrib.util for tempdir in testcases
wweic pushed a commit to neo-ai/tvm that referenced this pull request Feb 20, 2019
* Implement C code generation with tests

* Code cleanup

* Implement C code generation with tests

* Code cleanup

* tabs to spaces

* make lint compliant

* update export_library and reserve unique C keywords

* move ReserveKeywordsAsUnique to codegen_c

* some documentation and code cleanup

* use tvm.contrib.util for tempdir in testcases
@dongxiao92
Copy link

Hello, is there any documents or examples about how to use this c backend?

@Mutinifni
Copy link
Contributor Author

Mutinifni commented Apr 29, 2019

Hi @dongxiao92, you can look at tests/python/unittest/test_codegen_c_host.py for a vector addition example which runs on the host. You can also print out the generated C code from the function export_library in python/tvm/module.py.

@wujiaqi6630
Copy link

Hello, I want to generate C code too:

func = tvm.build(s, [A, B, C], target=device, name='myGemm')
temp = util.tempdir()
path_dso = temp.relpath("temp.so")
path = temp.relpath('lib.tar')
func.export_library(path_dso)
m = tvm.module.load(path_dso)
print(m.get_source())

But failed:
TVMError: Module[dso] does not support GetSource
Am I using the wrong method? @Mutinifni

@Mutinifni
Copy link
Contributor Author

Hi @wujiaqi6630, if you want to see the C source code that is generated, you can use func.get_source() because func is the C source module where as m is the DSO module. Only the C source module supports GetSource.

@wujiaqi6630
Copy link

Thank you !

@Jie-KUN
Copy link

Jie-KUN commented Nov 26, 2021

Hi@Mutinifni, I use the func.get_source(), but the code printed on the screen seems not to run on the X86 platform. How can I execute the C code. Looking forward to your reply

@NiteshNarayana
Copy link

Hi

Any update on how to execute the generated C source code?

I observe that when you generate the c source code for a module that is generated through a user-defined relay function you get a __tvm_main__() function generated as well. However this is not the case when I generate the source code for a network, eg mnist, where I see a different set of functions (eg. tvmgen_default_fused_nn_conv2d_add_nn_relu , tvmgen_default_fused_nn_max_pool2d_1) which I suppose are one for each layer. How are we to explicitly compile this and run it?

Thanks in advance!

@shreyashvi
Copy link

Hi

Any update on how to execute the generated C source code?

I observe that when you generate the c source code for a module that is generated through a user-defined relay function you get a __tvm_main__() function generated as well. However this is not the case when I generate the source code for a network, eg mnist, where I see a different set of functions (eg. tvmgen_default_fused_nn_conv2d_add_nn_relu , tvmgen_default_fused_nn_max_pool2d_1) which I suppose are one for each layer. How are we to explicitly compile this and run it?

Thanks in advance!

Hey, did you find out a way how to compile that generated c code?

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants