Skip to content

Commit

Permalink
[LINT] Enable clang-format. (#5572)
Browse files Browse the repository at this point in the history
* [LINT] Enable clang-format.

* Add more docs
  • Loading branch information
tqchen authored May 12, 2020
1 parent 579da6b commit 46787c8
Show file tree
Hide file tree
Showing 27 changed files with 548 additions and 500 deletions.
55 changes: 29 additions & 26 deletions apps/bundle_deploy/bundle.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,22 @@
* under the License.
*/

#include <tvm/runtime/c_runtime_api.h>
#include <stdio.h>
#include <stdlib.h>
#include <tvm/runtime/c_runtime_api.h>

/*! \brief macro to do C API call */
#define TVM_CCALL(func) \
do { \
int ret = (func); \
if (ret != 0) { \
#define TVM_CCALL(func) \
do { \
int ret = (func); \
if (ret != 0) { \
fprintf(stderr, "%s: %d: error: %s\n", __FILE__, __LINE__, TVMGetLastError()); \
exit(ret); \
} \
exit(ret); \
} \
} while (0)

TVM_DLL void * tvm_runtime_create(const char * json_data,
const char * params_data,
const uint64_t params_size) {
TVM_DLL void* tvm_runtime_create(const char* json_data, const char* params_data,
const uint64_t params_size) {
int64_t device_type = kDLCPU;
int64_t device_id = 0;

Expand All @@ -47,43 +46,47 @@ TVM_DLL void * tvm_runtime_create(const char * json_data,

// declare pointers
TVMModuleHandle (*SystemLibraryCreate)();
TVMModuleHandle (*TVMGraphRuntimeCreate)(const char *, const TVMModuleHandle, const TVMContext *);
int (*TVMGraphRuntime_LoadParams)(TVMModuleHandle, const char *, const uint32_t);
TVMModuleHandle (*TVMGraphRuntimeCreate)(const char*, const TVMModuleHandle, const TVMContext*);
int (*TVMGraphRuntime_LoadParams)(TVMModuleHandle, const char*, const uint32_t);

// get pointers
TVM_CCALL(TVMFuncGetGlobal("runtime.SystemLib", (TVMFunctionHandle*)&SystemLibraryCreate));
TVM_CCALL(TVMFuncGetGlobal("tvm.graph_runtime.create", (TVMFunctionHandle*)&TVMGraphRuntimeCreate));
TVM_CCALL(
TVMFuncGetGlobal("tvm.graph_runtime.create", (TVMFunctionHandle*)&TVMGraphRuntimeCreate));

// run modules
TVMModuleHandle mod_syslib = SystemLibraryCreate();
TVMModuleHandle mod = TVMGraphRuntimeCreate(json_data, mod_syslib, &ctx);
TVM_CCALL(TVMModGetFunction(mod, "load_params", 0, (TVMFunctionHandle*)&TVMGraphRuntime_LoadParams));
TVM_CCALL(
TVMModGetFunction(mod, "load_params", 0, (TVMFunctionHandle*)&TVMGraphRuntime_LoadParams));
TVMGraphRuntime_LoadParams(mod, params.data, params.size);

return mod;
}

TVM_DLL void tvm_runtime_destroy(void * runtime) {
void (*TVMGraphRuntimeRelease)(TVMModuleHandle *);
TVM_CCALL(TVMFuncGetGlobal("tvm.graph_runtime.release", (TVMFunctionHandle*)&TVMGraphRuntimeRelease));
TVM_DLL void tvm_runtime_destroy(void* runtime) {
void (*TVMGraphRuntimeRelease)(TVMModuleHandle*);
TVM_CCALL(
TVMFuncGetGlobal("tvm.graph_runtime.release", (TVMFunctionHandle*)&TVMGraphRuntimeRelease));
TVMGraphRuntimeRelease(&runtime);
}

TVM_DLL void tvm_runtime_set_input(void * runtime, const char * name, DLTensor * tensor) {
void (*TVMGraphRuntime_SetInput)(TVMModuleHandle, const char *, DLTensor*);
TVM_CCALL(TVMFuncGetGlobal("tvm.graph_runtime.set_input", (TVMFunctionHandle*)&TVMGraphRuntime_SetInput));
TVM_DLL void tvm_runtime_set_input(void* runtime, const char* name, DLTensor* tensor) {
void (*TVMGraphRuntime_SetInput)(TVMModuleHandle, const char*, DLTensor*);
TVM_CCALL(TVMFuncGetGlobal("tvm.graph_runtime.set_input",
(TVMFunctionHandle*)&TVMGraphRuntime_SetInput));
TVMGraphRuntime_SetInput(runtime, name, tensor);
}

TVM_DLL void tvm_runtime_run(void * runtime) {
TVM_DLL void tvm_runtime_run(void* runtime) {
void (*TVMGraphRuntime_Run)(TVMModuleHandle runtime);
TVM_CCALL(TVMFuncGetGlobal("tvm.graph_runtime.run", (TVMFunctionHandle*)&TVMGraphRuntime_Run));
TVMGraphRuntime_Run(runtime);
}

TVM_DLL void tvm_runtime_get_output(void * runtime, int32_t index, DLTensor * tensor) {
int (*TVMGraphRuntime_GetOutput)(TVMModuleHandle, const int32_t, DLTensor *);
TVM_CCALL(TVMFuncGetGlobal("tvm.graph_runtime.get_output", (TVMFunctionHandle*)&TVMGraphRuntime_GetOutput));
TVM_DLL void tvm_runtime_get_output(void* runtime, int32_t index, DLTensor* tensor) {
int (*TVMGraphRuntime_GetOutput)(TVMModuleHandle, const int32_t, DLTensor*);
TVM_CCALL(TVMFuncGetGlobal("tvm.graph_runtime.get_output",
(TVMFunctionHandle*)&TVMGraphRuntime_GetOutput));
TVMGraphRuntime_GetOutput(runtime, index, tensor);
}

27 changes: 13 additions & 14 deletions apps/bundle_deploy/bundle_static.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@
#include "bundle.h"
#include "runtime.c"

TVM_DLL void * tvm_runtime_create(const char * json_data,
const char * params_data,
const uint64_t params_size) {
TVM_DLL void* tvm_runtime_create(const char* json_data, const char* params_data,
const uint64_t params_size) {
int64_t device_type = kDLCPU;
int64_t device_id = 0;

Expand All @@ -38,9 +37,9 @@ TVM_DLL void * tvm_runtime_create(const char * json_data,
ctx.device_id = device_id;

// declare pointers
void * (*SystemLibraryCreate)();
TVMGraphRuntime * (*TVMGraphRuntimeCreate)(const char *, const TVMModuleHandle, const TVMContext *);
int (*TVMGraphRuntime_LoadParams)(TVMModuleHandle, const char *, const uint32_t);
void* (*SystemLibraryCreate)();
TVMGraphRuntime* (*TVMGraphRuntimeCreate)(const char*, const TVMModuleHandle, const TVMContext*);
int (*TVMGraphRuntime_LoadParams)(TVMModuleHandle, const char*, const uint32_t);

// get pointers
TVMFuncGetGlobal("runtime.SystemLib", (TVMFunctionHandle*)&SystemLibraryCreate);
Expand All @@ -51,30 +50,30 @@ TVM_DLL void * tvm_runtime_create(const char * json_data,
TVMModuleHandle mod = TVMGraphRuntimeCreate(json_data, mod_syslib, &ctx);
TVMModGetFunction(mod, "load_params", 0, (TVMFunctionHandle*)&TVMGraphRuntime_LoadParams);
TVMGraphRuntime_LoadParams(mod, params.data, params.size);

return mod;
}

TVM_DLL void tvm_runtime_destroy(void * runtime) {
void (*TVMGraphRuntimeRelease)(TVMModuleHandle *);
TVM_DLL void tvm_runtime_destroy(void* runtime) {
void (*TVMGraphRuntimeRelease)(TVMModuleHandle*);
TVMFuncGetGlobal("tvm.graph_runtime.release", (TVMFunctionHandle*)&TVMGraphRuntimeRelease);
TVMGraphRuntimeRelease(&runtime);
}

TVM_DLL void tvm_runtime_set_input(void * runtime, const char * name, DLTensor * tensor) {
void (*TVMGraphRuntime_SetInput)(TVMModuleHandle, const char *, DLTensor*);
TVM_DLL void tvm_runtime_set_input(void* runtime, const char* name, DLTensor* tensor) {
void (*TVMGraphRuntime_SetInput)(TVMModuleHandle, const char*, DLTensor*);
TVMFuncGetGlobal("tvm.graph_runtime.set_input", (TVMFunctionHandle*)&TVMGraphRuntime_SetInput);
TVMGraphRuntime_SetInput(runtime, name, tensor);
}

TVM_DLL void tvm_runtime_run(void * runtime) {
TVM_DLL void tvm_runtime_run(void* runtime) {
void (*TVMGraphRuntime_Run)(TVMModuleHandle runtime);
TVMFuncGetGlobal("tvm.graph_runtime.run", (TVMFunctionHandle*)&TVMGraphRuntime_Run);
TVMGraphRuntime_Run(runtime);
}

TVM_DLL void tvm_runtime_get_output(void * runtime, int32_t index, DLTensor * tensor) {
int (*TVMGraphRuntime_GetOutput)(TVMModuleHandle, const int32_t, DLTensor *);
TVM_DLL void tvm_runtime_get_output(void* runtime, int32_t index, DLTensor* tensor) {
int (*TVMGraphRuntime_GetOutput)(TVMModuleHandle, const int32_t, DLTensor*);
TVMFuncGetGlobal("tvm.graph_runtime.get_output", (TVMFunctionHandle*)&TVMGraphRuntime_GetOutput);
TVMGraphRuntime_GetOutput(runtime, index, tensor);
}
41 changes: 20 additions & 21 deletions apps/bundle_deploy/demo_static.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,34 @@
* under the License.
*/

#include <tvm/runtime/c_runtime_api.h>

#include <assert.h>
#include <float.h>
#include <stdio.h>
#include <sys/time.h>
#include <stdlib.h>
#include <float.h>
#include <sys/time.h>
#include <tvm/runtime/c_runtime_api.h>

#include "bundle.h"
#include "build/graph.json.c"
#include "build/params.bin.c"
#include "bundle.h"

#define OUTPUT_LEN 1000
#define OUTPUT_LEN 1000

int main(int argc, char **argv) {
int main(int argc, char** argv) {
assert(argc == 2 && "Usage: demo_static <cat.bin>");

char * json_data = (char *)(build_graph_json);
char * params_data = (char *)(build_params_bin);
char* json_data = (char*)(build_graph_json);
char* params_data = (char*)(build_params_bin);
uint64_t params_size = build_params_bin_len;

struct timeval t0, t1, t2, t3, t4, t5;
gettimeofday(&t0, 0);

void *handle = tvm_runtime_create(json_data, params_data, params_size);
void* handle = tvm_runtime_create(json_data, params_data, params_size);
gettimeofday(&t1, 0);

float input_storage[1 * 3 * 224 * 224];
FILE * fp = fopen(argv[1], "rb");
FILE* fp = fopen(argv[1], "rb");
(void)fread(input_storage, 3 * 224 * 224, 4, fp);
fclose(fp);

Expand All @@ -56,7 +55,7 @@ int main(int argc, char **argv) {
input.ndim = 4;
DLDataType dtype = {kDLFloat, 32, 1};
input.dtype = dtype;
int64_t shape [4] = {1, 3, 224, 224};
int64_t shape[4] = {1, 3, 224, 224};
input.shape = shape;
input.strides = NULL;
input.byte_offset = 0;
Expand Down Expand Up @@ -95,15 +94,15 @@ int main(int argc, char **argv) {
tvm_runtime_destroy(handle);
gettimeofday(&t5, 0);

printf("The maximum position in output vector is: %d, with max-value %f.\n",
max_index, max_iter);
printf("timing: %.2f ms (create), %.2f ms (set_input), %.2f ms (run), "
"%.2f ms (get_output), %.2f ms (destroy)\n",
(t1.tv_sec-t0.tv_sec)*1000 + (t1.tv_usec-t0.tv_usec)/1000.f,
(t2.tv_sec-t1.tv_sec)*1000 + (t2.tv_usec-t1.tv_usec)/1000.f,
(t3.tv_sec-t2.tv_sec)*1000 + (t3.tv_usec-t2.tv_usec)/1000.f,
(t4.tv_sec-t3.tv_sec)*1000 + (t4.tv_usec-t3.tv_usec)/1000.f,
(t5.tv_sec-t4.tv_sec)*1000 + (t5.tv_usec-t4.tv_usec)/1000.f);
printf("The maximum position in output vector is: %d, with max-value %f.\n", max_index, max_iter);
printf(
"timing: %.2f ms (create), %.2f ms (set_input), %.2f ms (run), "
"%.2f ms (get_output), %.2f ms (destroy)\n",
(t1.tv_sec - t0.tv_sec) * 1000 + (t1.tv_usec - t0.tv_usec) / 1000.f,
(t2.tv_sec - t1.tv_sec) * 1000 + (t2.tv_usec - t1.tv_usec) / 1000.f,
(t3.tv_sec - t2.tv_sec) * 1000 + (t3.tv_usec - t2.tv_usec) / 1000.f,
(t4.tv_sec - t3.tv_sec) * 1000 + (t4.tv_usec - t3.tv_usec) / 1000.f,
(t5.tv_sec - t4.tv_sec) * 1000 + (t5.tv_usec - t4.tv_usec) / 1000.f);

return 0;
}
4 changes: 2 additions & 2 deletions apps/bundle_deploy/runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@
/*! \brief Page size for virtual memory allocation */
#define TVM_CRT_PAGE_BYTES 4096

#include "../../src/runtime/crt/crt_runtime_api.c"
#include "../../src/runtime/crt/crt_backend_api.c"
#include "../../src/runtime/crt/crt_runtime_api.c"
#include "../../src/runtime/crt/graph_runtime.c"
#include "../../src/runtime/crt/load_json.c"
#include "../../src/runtime/crt/ndarray.c"
#include "../../src/runtime/crt/memory.c"
#include "../../src/runtime/crt/ndarray.c"
33 changes: 16 additions & 17 deletions apps/bundle_deploy/test_static.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,25 @@
* under the License.
*/

#include <tvm/runtime/c_runtime_api.h>

#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <tvm/runtime/c_runtime_api.h>

#include "bundle.h"


int main(int argc, char **argv) {
int main(int argc, char** argv) {
assert(argc == 5 && "Usage: test_static <data.bin> <output.bin> <graph.json> <params.bin>");

struct stat st;
char * json_data;
char * params_data;
char* json_data;
char* params_data;
uint64_t params_size;

FILE * fp = fopen(argv[3], "rb");
FILE* fp = fopen(argv[3], "rb");
stat(argv[3], &st);
json_data = (char*)malloc(st.st_size);
fread(json_data, st.st_size, 1, fp);
Expand All @@ -53,7 +51,7 @@ int main(int argc, char **argv) {
struct timeval t0, t1, t2, t3, t4, t5;
gettimeofday(&t0, 0);

auto *handle = tvm_runtime_create(json_data, params_data, params_size);
auto* handle = tvm_runtime_create(json_data, params_data, params_size);
gettimeofday(&t1, 0);

float input_storage[10 * 5];
Expand Down Expand Up @@ -110,13 +108,14 @@ int main(int argc, char **argv) {
tvm_runtime_destroy(handle);
gettimeofday(&t5, 0);

printf("timing: %.2f ms (create), %.2f ms (set_input), %.2f ms (run), "
"%.2f ms (get_output), %.2f ms (destroy)\n",
(t1.tv_sec-t0.tv_sec)*1000 + (t1.tv_usec-t0.tv_usec)/1000.f,
(t2.tv_sec-t1.tv_sec)*1000 + (t2.tv_usec-t1.tv_usec)/1000.f,
(t3.tv_sec-t2.tv_sec)*1000 + (t3.tv_usec-t2.tv_usec)/1000.f,
(t4.tv_sec-t3.tv_sec)*1000 + (t4.tv_usec-t3.tv_usec)/1000.f,
(t5.tv_sec-t4.tv_sec)*1000 + (t5.tv_usec-t4.tv_usec)/1000.f);
printf(
"timing: %.2f ms (create), %.2f ms (set_input), %.2f ms (run), "
"%.2f ms (get_output), %.2f ms (destroy)\n",
(t1.tv_sec - t0.tv_sec) * 1000 + (t1.tv_usec - t0.tv_usec) / 1000.f,
(t2.tv_sec - t1.tv_sec) * 1000 + (t2.tv_usec - t1.tv_usec) / 1000.f,
(t3.tv_sec - t2.tv_sec) * 1000 + (t3.tv_usec - t2.tv_usec) / 1000.f,
(t4.tv_sec - t3.tv_sec) * 1000 + (t4.tv_usec - t3.tv_usec) / 1000.f,
(t5.tv_sec - t4.tv_sec) * 1000 + (t5.tv_usec - t4.tv_usec) / 1000.f);

free(json_data);
free(params_data);
Expand Down
41 changes: 41 additions & 0 deletions docs/contribute/code_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,47 @@ C++ Code Styles
pass by value is better than pass by const reference in such cases.
- Favor ``const`` member function when possible.

We use `clang-format` to enforce the code style. Because different version
of clang-format might change by its version, it is recommended to use the same
version of the clang-format as the master.
You can also use the following command via docker.

.. code:: bash
docker/bash.sh tvmai/ci-lint clang-format-10 [path-to-file]
clang-format is also not perfect, when necessary, you can use disble clang-format on certain code regions.

.. code :: c
// clang-format off
void Test() {
// clang-format will be disabled in this region.
}
// clang-format on
Because clang-format may not recognize macros, it is recommended to use macro like normal function styles.


.. code :: c
#define MACRO_IMPL { custom impl; }
#define MACRO_FUNC(x)
// not preferred, because clang-format might recognize it as types.
virtual void Func1() MACRO_IMPL
// preferred
virtual void Func2() MACRO_IMPL;
void Func3() {
// preferred
MACRO_FUNC(xyz);
}
Python Code Styles
------------------
- The functions and classes are documented in `numpydoc <https://numpydoc.readthedocs.io/en/latest/>`_ format.
Expand Down
11 changes: 9 additions & 2 deletions docs/contribute/pull_request.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ This is a quick guide to submit a pull request, please also refer to the detaile
git rebase upstream/master
- Make sure code style check pass by typing the following command, and all the existing test-cases pass.
- ``docker/bash.sh tvmai/ci-lint ./tests/scripts/task_lint.sh``. (Note: You must install docker beforehand so you can run a docker image.)

.. code:: bash
# Reproduce the lint procedure in the CI.
docker/bash.sh tvmai/ci-lint ./tests/scripts/task_lint.sh
# Run clang-format check for all the files that changed since upstream/master
docker/bash.sh tvmai/ci-lint ./tests/lint/git-clang-format.sh upstream/master
- Add test-cases to cover the new features or bugfix the patch introduces.
- Document the code you wrote, see more at :ref:`doc_guide`
- Send the pull request and fix the problems reported by automatic checks.
Expand All @@ -44,11 +51,11 @@ This is a quick guide to submit a pull request, please also refer to the detaile
- The patch can be merged after the reviewers approve the pull request.



CI Environment
--------------
We use docker container to create stable CI environments
that can be deployed to multiple machines.
You can find the prebuilt images in `<https://hub.docker.com/r/tvmai/>`_ .
Because we want a relatively stable CI environment and make use of pre-cached image,
all of the CI images are built and maintained by committers.

Expand Down
Loading

0 comments on commit 46787c8

Please sign in to comment.