Skip to content

Commit a970f79

Browse files
committed
Fix inconsistencies in graph_executor function names handling
Updates value of `TVM_CRT_MAX_STRLEN_FUNCTION_NAME` from `80` to `120` Replace all occurences of `[120]` with `[TVM_CRT_MAX_STRLEN_FUNCTION_NAME]` to maintain consistency and make the array lengths user-configurable. Introduces `TVM_CRT_MAX_STRLEN_PARAM_NAME` used for parameter names only Adds comments to `kMaxFuncNameLength` variabe in src/relay/backend/te_compiler_cache.cc making sure that the values are kept "in sync". (sort of) See apache#8953 for more context. The actual bug reported there however can only be fixed by increasing the TVM_CRT_MAX_STRLEN_FUNCTION_NAME to a value larger than the maximum possible truncated function name length (including prefixes and suffices) Example: 6 ['tvmgen' prefix length] + 7 ['default' model name length] + 5 ['fused' fused function name prefix length] + 80 [truncated function name length] + 19 [length of appended hash] + 4 [Number of '_' between components] = 121
1 parent da1f735 commit a970f79

File tree

10 files changed

+34
-16
lines changed

10 files changed

+34
-16
lines changed

apps/bundle_deploy/crt_config/crt_config.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@
3737
/*! Maximum supported string length in dltype, e.g. "int8", "int16", "float32" */
3838
#define TVM_CRT_MAX_STRLEN_DLTYPE 10
3939
/*! Maximum supported string length in function names */
40-
#define TVM_CRT_MAX_STRLEN_FUNCTION_NAME 80
40+
#define TVM_CRT_MAX_STRLEN_FUNCTION_NAME 120
41+
/*! Maximum supported string length in parameter names */
42+
#define TVM_CRT_MAX_STRLEN_PARAM_NAME 80
4143

4244
/*! Maximum number of registered modules. */
4345
#define TVM_CRT_MAX_REGISTERED_MODULES 2

apps/microtvm/arduino/example_project/src/standalone_crt/crt_config/crt_config.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@
3636
/*! Maximum supported string length in dltype, e.g. "int8", "int16", "float32" */
3737
#define TVM_CRT_MAX_STRLEN_DLTYPE 10
3838
/*! Maximum supported string length in function names */
39-
#define TVM_CRT_MAX_STRLEN_FUNCTION_NAME 80
39+
#define TVM_CRT_MAX_STRLEN_FUNCTION_NAME 120
40+
/*! Maximum supported string length in parameter names */
41+
#define TVM_CRT_MAX_STRLEN_PARAM_NAME 80
4042

4143
/*! Maximum number of registered modules. */
4244
#define TVM_CRT_MAX_REGISTERED_MODULES 2

apps/microtvm/arduino/host_driven/src/standalone_crt/crt_config/crt_config.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@
3636
/*! Maximum supported string length in dltype, e.g. "int8", "int16", "float32" */
3737
#define TVM_CRT_MAX_STRLEN_DLTYPE 10
3838
/*! Maximum supported string length in function names */
39-
#define TVM_CRT_MAX_STRLEN_FUNCTION_NAME 80
39+
#define TVM_CRT_MAX_STRLEN_FUNCTION_NAME 120
40+
/*! Maximum supported string length in parameter names */
41+
#define TVM_CRT_MAX_STRLEN_PARAM_NAME 80
4042

4143
/*! Maximum number of registered modules. */
4244
#define TVM_CRT_MAX_REGISTERED_MODULES 2

apps/microtvm/zephyr/template_project/crt_config/crt_config.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@
4848
#define TVM_CRT_MAX_STRLEN_DLTYPE 10
4949

5050
/*! Maximum supported string length in function names */
51-
#define TVM_CRT_MAX_STRLEN_FUNCTION_NAME 80
51+
#define TVM_CRT_MAX_STRLEN_FUNCTION_NAME 120
52+
53+
/*! Maximum supported string length in parameter names */
54+
#define TVM_CRT_MAX_STRLEN_PARAM_NAME 80
5255

5356
/*! \brief Maximum length of a PackedFunc function name. */
5457
#define TVM_CRT_MAX_FUNCTION_NAME_LENGTH_BYTES 30

include/tvm/runtime/crt/graph_executor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ struct TVMModule;
3636

3737
/*! \brief operator attributes about tvm op */
3838
typedef struct TVMOpParam {
39-
char func_name[120];
39+
char func_name[TVM_CRT_MAX_STRLEN_FUNCTION_NAME];
4040
uint32_t num_inputs;
4141
uint32_t num_outputs;
4242
uint32_t flatten_data;

src/relay/backend/te_compiler_cache.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ class ScheduleBuilder : public backend::MemoizedExprTranslator<Array<te::Tensor>
134134
auto outputs = this->VisitExpr(prim_func->body);
135135
auto candidate_name = readable_name_stream_.str();
136136
constexpr static size_t kMaxFuncNameLength = 80;
137+
// WARNING: Please make sure to also update TVM_CRT_MAX_STRLEN_FUNCTION_NAME
138+
// whenever the value of kMaxFuncNameLength changes
137139
if (candidate_name.size() > kMaxFuncNameLength) {
138140
std::stringstream truncated_name;
139141
truncated_name << candidate_name.substr(0, kMaxFuncNameLength);
@@ -394,6 +396,8 @@ class MakeShapeFunc : public backend::MemoizedExprTranslator<Array<te::Tensor>>
394396
// Generate a name.
395397
auto candidate_name = readable_name_stream_.str();
396398
constexpr static size_t kMaxFuncNameLength = 80;
399+
// WARNING: Please make sure to also update TVM_CRT_MAX_STRLEN_FUNCTION_NAME
400+
// whenever the value of kMaxFuncNameLength changes
397401
if (candidate_name.size() > kMaxFuncNameLength) {
398402
std::stringstream truncated_name;
399403
truncated_name << candidate_name.substr(0, kMaxFuncNameLength);

src/runtime/crt/crt_config-template.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@
4949
#define TVM_CRT_MAX_STRLEN_DLTYPE 10
5050

5151
/*! Maximum supported string length in function names */
52-
#define TVM_CRT_MAX_STRLEN_FUNCTION_NAME 80
52+
#define TVM_CRT_MAX_STRLEN_FUNCTION_NAME 120
53+
54+
/*! Maximum supported string length in parameter names */
55+
#define TVM_CRT_MAX_STRLEN_PARAM_NAME 80
5356

5457
/*! \brief Maximum length of a PackedFunc function name. */
5558
#define TVM_CRT_MAX_FUNCTION_NAME_LENGTH_BYTES 30

src/runtime/crt/graph_executor/graph_executor.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ int NodeEntry_Load(TVMGraphExecutorNodeEntry* entry, JSONReader* reader) {
7777
void TVMGraphExecutorNode_LoadAttrs(TVMGraphExecutorNode* node, JSONReader* reader,
7878
TVMOpParam* param) {
7979
int bitmask = 0;
80-
char key[20], value[120];
80+
char key[20], value[TVM_CRT_MAX_STRLEN_FUNCTION_NAME];
8181
memset(param, 0, sizeof(TVMOpParam));
8282
memset(key, 0, sizeof(key));
8383
memset(value, 0, sizeof(value));
@@ -796,13 +796,13 @@ int TVMGraphExecutor_LoadParams(TVMGraphExecutor* executor, const char* param_bl
796796
char* names = NULL;
797797
DLDevice dev = {kDLCPU, 0};
798798
tvm_crt_error_t err = TVMPlatformMemoryAllocate(
799-
TVM_CRT_MAX_STRLEN_FUNCTION_NAME * executor->nodes_count, dev, (void**)&names);
799+
TVM_CRT_MAX_STRLEN_PARAM_NAME * executor->nodes_count, dev, (void**)&names);
800800
if (err != kTvmErrorNoError) {
801801
fprintf(stderr, "memory allocate error: %08x", err);
802802
status = -1;
803803
return status;
804804
}
805-
memset(names, 0, TVM_CRT_MAX_STRLEN_FUNCTION_NAME * executor->nodes_count);
805+
memset(names, 0, TVM_CRT_MAX_STRLEN_PARAM_NAME * executor->nodes_count);
806806
uint64_t names_count;
807807
int idx;
808808
memcpy(&names_count, bptr, sizeof(names_count));
@@ -811,11 +811,11 @@ int TVMGraphExecutor_LoadParams(TVMGraphExecutor* executor, const char* param_bl
811811
uint64_t name_length;
812812
memcpy(&name_length, bptr, sizeof(name_length));
813813
bptr += sizeof(name_length);
814-
if (name_length >= TVM_CRT_MAX_STRLEN_FUNCTION_NAME) {
814+
if (name_length >= TVM_CRT_MAX_STRLEN_PARAM_NAME) {
815815
fprintf(stderr, "Error: function name longer than expected.\n");
816816
status = -1;
817817
}
818-
memcpy(names + TVM_CRT_MAX_STRLEN_FUNCTION_NAME * idx, bptr, name_length);
818+
memcpy(names + TVM_CRT_MAX_STRLEN_PARAM_NAME * idx, bptr, name_length);
819819
bptr += name_length;
820820
}
821821

@@ -831,9 +831,9 @@ int TVMGraphExecutor_LoadParams(TVMGraphExecutor* executor, const char* param_bl
831831

832832
for (idx = 0; idx < size; idx++) {
833833
int32_t in_idx =
834-
TVMGraphExecutor_GetInputIndex(executor, names + TVM_CRT_MAX_STRLEN_FUNCTION_NAME * idx);
834+
TVMGraphExecutor_GetInputIndex(executor, names + TVM_CRT_MAX_STRLEN_PARAM_NAME * idx);
835835
CHECK_GT(in_idx, 0, "Found param for non-existent input: %s\n",
836-
names + TVM_CRT_MAX_STRLEN_FUNCTION_NAME * idx);
836+
names + TVM_CRT_MAX_STRLEN_PARAM_NAME * idx);
837837
uint32_t eid = TVMGraphExecutor_GetEntryId(executor, executor->input_nodes[in_idx], 0);
838838
if (!(eid < executor->data_entry_count)) {
839839
fprintf(stderr, "`entry_id`=%d is greater than expected(%d).\n", eid,
@@ -859,7 +859,7 @@ int TVMGraphExecutor_LoadParams(TVMGraphExecutor* executor, const char* param_bl
859859
#if TVM_CRT_DEBUG
860860
TVMNDArray* entry = &(executor->data_entry[eid]);
861861
printf("loading: param %s loaded, in_idx=%d, eid=%d, ndim=%d, data[0]=%f\n",
862-
names + TVM_CRT_MAX_STRLEN_FUNCTION_NAME * idx, in_idx, eid, entry->dl_tensor.ndim,
862+
names + TVM_CRT_MAX_STRLEN_PARAM_NAME * idx, in_idx, eid, entry->dl_tensor.ndim,
863863
((float*)entry->dl_tensor.data)[0]); // NOLINT(*)
864864
#endif // TVM_CRT_DEBUG
865865
}

src/runtime/crt/include/tvm/runtime/crt/internal/graph_executor/graph_executor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ typedef struct TVMGraphExecutorNode {
6060
// operator type in string
6161
char op_type[16];
6262
// name of the op
63-
char name[120];
63+
char name[TVM_CRT_MAX_STRLEN_FUNCTION_NAME];
6464
// parameters
6565
TVMOpParam param;
6666
// inputs

src/runtime/micro/crt_config.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@
3737
/*! Maximum supported string length in dltype, e.g. "int8", "int16", "float32" */
3838
#define TVM_CRT_MAX_STRLEN_DLTYPE 10
3939
/*! Maximum supported string length in function names */
40-
#define TVM_CRT_MAX_STRLEN_FUNCTION_NAME 80
40+
#define TVM_CRT_MAX_STRLEN_FUNCTION_NAME 120
41+
/*! Maximum supported string length in parameter names */
42+
#define TVM_CRT_MAX_STRLEN_PARAM_NAME 80
4143

4244
/*! Maximum number of registered modules. */
4345
#define TVM_CRT_MAX_REGISTERED_MODULES 2

0 commit comments

Comments
 (0)