-
Notifications
You must be signed in to change notification settings - Fork 425
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Log EValue tag names instead of numeric values (#7538)
Summary: Update error log messages that include EValue tags to use a string representation of the tag rather than the numerical index. This improves readability for users. Example Old Message: ``` [method.cpp:814] The 0-th input of method should have the same type as the input_evalue, but get tag 1 and tag 4 ``` Example New Message: ``` [method.cpp:813] Input 0 was expected to be Tensor but was Int. ``` Pull Request resolved: #7538 Test Plan: Locally built the executorch bento kernel and tested with an invalid EValue input to capture the example new message above. Repeated with "-c executorch.enable_enum_strings=false" to verify fallback behavior. Added tests for `tag_to_string` to tag_test.cpp, runnable via ``` buck test executorch/runtime/core/test:tag_test ``` Reviewed By: digantdesai, manuelcandales Differential Revision: D67888756 Pulled By: GregoryComer
- Loading branch information
1 parent
4655202
commit 32632c3
Showing
7 changed files
with
258 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
/** | ||
* @file | ||
* Contains preprocessor definitions used by ExecuTorch core. | ||
*/ | ||
|
||
#pragma once | ||
|
||
// Enable ET_ENABLE_ENUM_STRINGS by default. This option gates inclusion of | ||
// enum string names and can be disabled by explicitly setting it to 0. | ||
#ifndef ET_ENABLE_ENUM_STRINGS | ||
#define ET_ENABLE_ENUM_STRINGS 1 | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#include <executorch/runtime/core/defines.h> | ||
#include <executorch/runtime/core/tag.h> | ||
|
||
#include <cstdio> | ||
|
||
namespace executorch { | ||
namespace runtime { | ||
|
||
/** | ||
* Convert a tag value to a string representation. If ET_ENABLE_ENUM_STRINGS is | ||
* set (it is on by default), this will return a string name (for example, | ||
* "Tensor"). Otherwise, it will return a string representation of the index | ||
* value ("1"). | ||
* | ||
* If the user buffer is not large enough to hold the string representation, the | ||
* string will be truncated. | ||
* | ||
* The return value is the number of characters written, or in the case of | ||
* truncation, the number of characters that would be written if the buffer was | ||
* large enough. | ||
*/ | ||
size_t tag_to_string(Tag tag, char* buffer, size_t buffer_size) { | ||
#if ET_ENABLE_ENUM_STRINGS | ||
const char* name_str; | ||
#define DEFINE_CASE(name) \ | ||
case Tag::name: \ | ||
name_str = #name; \ | ||
break; | ||
|
||
switch (tag) { | ||
EXECUTORCH_FORALL_TAGS(DEFINE_CASE) | ||
default: | ||
name_str = "Unknown"; | ||
break; | ||
} | ||
|
||
return snprintf(buffer, buffer_size, "%s", name_str); | ||
#undef DEFINE_CASE | ||
#else | ||
return snprintf(buffer, buffer_size, "%d", static_cast<int>(tag)); | ||
#endif // ET_ENABLE_ENUM_TO_STRING | ||
} | ||
|
||
} // namespace runtime | ||
} // namespace executorch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#include <executorch/runtime/core/tag.h> | ||
|
||
#include <gtest/gtest.h> | ||
#include <array> | ||
|
||
using namespace ::testing; | ||
using executorch::runtime::Tag; | ||
using executorch::runtime::tag_to_string; | ||
|
||
// The behavior of tag_to_string depends on the value of ET_ENABLE_ENUM_STRINGS. | ||
// If it is not set, tag_to_string will return a string representation of the | ||
// enum index value. As this behavior is compile-time gated, tests must also | ||
// be compile-time gated. | ||
#if ET_ENABLE_ENUM_STRINGS | ||
TEST(TagToString, TagValues) { | ||
std::array<char, 16> name; | ||
|
||
tag_to_string(Tag::Tensor, name.data(), name.size()); | ||
EXPECT_STREQ("Tensor", name.data()); | ||
|
||
tag_to_string(Tag::Int, name.data(), name.size()); | ||
EXPECT_STREQ("Int", name.data()); | ||
|
||
tag_to_string(Tag::Double, name.data(), name.size()); | ||
EXPECT_STREQ("Double", name.data()); | ||
|
||
tag_to_string(Tag::Bool, name.data(), name.size()); | ||
EXPECT_STREQ("Bool", name.data()); | ||
} | ||
|
||
TEST(TagToString, TagNameBufferSize) { | ||
// Validate that TAG_NAME_BUFFER_SIZE is large enough to hold the all tag | ||
// strings without truncation. | ||
std::array<char, TAG_NAME_BUFFER_SIZE> name; | ||
|
||
// Note that the return value of tag_to_string does not include the null | ||
// terminator. | ||
size_t longest = 0; | ||
|
||
#define TEST_CASE(tag) \ | ||
auto tag##_len = tag_to_string(Tag::tag, name.data(), name.size()); \ | ||
EXPECT_LT(tag##_len, TAG_NAME_BUFFER_SIZE) \ | ||
<< "TAG_NAME_BUFFER_SIZE is too small to hold " #tag; \ | ||
longest = std::max(longest, tag##_len); | ||
|
||
EXECUTORCH_FORALL_TAGS(TEST_CASE) | ||
#undef TEST_CASE | ||
|
||
EXPECT_EQ(longest + 1, TAG_NAME_BUFFER_SIZE) | ||
<< "TAG_NAME_BUFFER_SIZE has incorrect value, expected " << longest + 1; | ||
} | ||
|
||
TEST(TagToString, FitsExact) { | ||
std::array<char, 4> name; | ||
|
||
auto ret = tag_to_string(Tag::Int, name.data(), name.size()); | ||
|
||
EXPECT_EQ(3, ret); | ||
EXPECT_STREQ("Int", name.data()); | ||
} | ||
|
||
TEST(TagToString, Truncate) { | ||
std::array<char, 6> name; | ||
std::fill(name.begin(), name.end(), '-'); | ||
|
||
auto ret = tag_to_string(Tag::Double, name.data(), name.size()); | ||
EXPECT_EQ(6, ret); | ||
EXPECT_TRUE(name[name.size() - 1] == 0); | ||
EXPECT_STREQ("Doubl", name.data()); | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters