-
Notifications
You must be signed in to change notification settings - Fork 456
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
Differential Revision: D67888756 Pull Request resolved: #7538
- Loading branch information
1 parent
39aa22d
commit 02da069
Showing
7 changed files
with
258 additions
and
26 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,52 @@ | ||
/* | ||
* 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 <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,80 @@ | ||
/* | ||
* 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::kTagNameBufferSize; | ||
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 kTagNameBufferSize is large enough to hold the all tag | ||
// strings without truncation. | ||
std::array<char, kTagNameBufferSize> 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, kTagNameBufferSize) \ | ||
<< "kTagNameBufferSize is too small to hold " #tag; \ | ||
longest = std::max(longest, tag##_len); | ||
|
||
EXECUTORCH_FORALL_TAGS(TEST_CASE) | ||
#undef TEST_CASE | ||
|
||
EXPECT_EQ(longest + 1, kTagNameBufferSize) | ||
<< "kTagNameBufferSize 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 // ET_ENABLE_ENUM_STRINGS |
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