-
Notifications
You must be signed in to change notification settings - Fork 608
Add function to stringify tensor shapes #7943
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
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#include "tensor_util.h" | ||
|
||
/* | ||
* 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/exec_aten/util/tensor_util.h> | ||
|
||
#include <executorch/runtime/platform/assert.h> | ||
|
||
namespace executorch::runtime { | ||
/** | ||
* Shared implementation for tensor_util.h, may only contain code that | ||
* works whether or not ATen mode is active. | ||
*/ | ||
std::array<char, kTensorShapeStringSizeLimit> tensor_shape_to_c_string( | ||
executorch::runtime::Span<const executorch::aten::SizesType> shape) { | ||
std::array<char, kTensorShapeStringSizeLimit> out; | ||
char* p = out.data(); | ||
if ET_UNLIKELY (shape.size() > kTensorDimensionLimit) { | ||
static constexpr char kLimitExceededError[] = | ||
"(ERR: tensor ndim exceeds limit, can't happen)"; | ||
static_assert(sizeof(kLimitExceededError) <= kTensorShapeStringSizeLimit); | ||
std::memcpy(p, kLimitExceededError, sizeof(kLimitExceededError)); | ||
return out; | ||
} | ||
*p++ = '('; | ||
for (const auto elem : shape) { | ||
if (elem < 0 || elem > internal::kMaximumPrintableTensorShapeElement) { | ||
static_assert( | ||
internal::kMaximumPrintableTensorShapeElement > 99999, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Took me a minute to understand where |
||
"must have room for error string!"); | ||
strcpy(p, "ERR, "); | ||
p += strlen("ERR, "); | ||
dbort marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
// snprintf returns characters *except* the NUL terminator, which is what | ||
// we want. | ||
p += snprintf( | ||
p, | ||
kTensorShapeStringSizeLimit - (p - out.data()), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should there be a -1 in here to account for the NUL byte? Not sure |
||
"%" PRIu32 ", ", | ||
static_cast<uint32_t>(elem)); | ||
} | ||
} | ||
*(p - 2) = ')'; | ||
*(p - 1) = '\0'; | ||
return out; | ||
} | ||
|
||
} // namespace executorch::runtime |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could simplify things with snprintf()
And "can't happen" isn't true: I could create a PTE file where this does happen. Right now there's a dangerous assumption across the kernel libraries that dim is always <= kTensorDimensionLimit.