Skip to content

[ET-VK][ez] Replace std::stringstream with std::string for Shader names #2964

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

Closed
wants to merge 1 commit into from

Conversation

SS-JIA
Copy link
Contributor

@SS-JIA SS-JIA commented Apr 10, 2024

Stack from ghstack (oldest at bottom):

Context

Some research into efficient string concatenation suggests that streams in C++ are not quite efficient. The best way to concatenate strings seems to be creating a std::string and reserving sufficient capacity for the std::string. This diff deprecates the usage of std::stringstream when constructing kernel names in favor of using std::string directly.

Differential Revision: D55951475

… names

## Context

Some research into efficient string concatenation suggests that streams in C++ are not quite efficient. The best way to concatenate strings seems to be creating a `std::string` and reserving sufficient capacity for the `std::string`. This diff deprecates the usage of `std::stringstream` when constructing kernel names in favor of using `std::string` directly.

Differential Revision: [D55951475](https://our.internmc.facebook.com/intern/diff/D55951475/)

[ghstack-poisoned]
Copy link

pytorch-bot bot commented Apr 10, 2024

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/2964

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 3faf442 with merge base 99c4f4e (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 10, 2024
SS-JIA added a commit that referenced this pull request Apr 10, 2024
… names

## Context

Some research into efficient string concatenation suggests that streams in C++ are not quite efficient. The best way to concatenate strings seems to be creating a `std::string` and reserving sufficient capacity for the `std::string`. This diff deprecates the usage of `std::stringstream` when constructing kernel names in favor of using `std::string` directly.

Differential Revision: [D55951475](https://our.internmc.facebook.com/intern/diff/D55951475/)

ghstack-source-id: 221941094
Pull Request resolved: #2964
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D55951475

Copy link
Contributor

@swolchok swolchok left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've preserved inline comments for pedagogical reasons, but there is a higher-level potentially-solvable problem here: you are building these strings just to do a lookup in a std::unordered_map whose key is std::string. If C++20 is available, you don't actually need a std::string to do that thanks to heterogeneous lookup (https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0919r3.html), and instead the lookup function that VK_KERNEL_FROM_STR calls could take a std::string_view (or executorch equivalent thereof).

How high could we raise the C++ standard for the Vulkan backend?

Comment on lines +78 to +79
std::string kernel_name("binary_");
kernel_name.reserve(kShaderNameReserve);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: you would be slightly better off reserving before writing "binary_" into the string; as written, "binary_" might get written into the inline storage once before getting copied to the heap if the compiler doesn't catch it. surprisingly, this was very easy to demonstrate: clang 17 gets it wrong because reserve() isn't getting inlined. https://godbolt.org/z/jKcf3KGG1

apply_ndim_suffix(noop_shader_name, graph.get_val(packed).toTensor());
apply_dtype_suffix(noop_shader_name, graph.get_val(packed).toTensor());
return VK_KERNEL_FROM_STR(noop_shader_name.str());
std::string noop_shader_name("no_op");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this path doesn't use reserve, so on platforms without SSO this implementation could be worse. (It looks like the string data fits in 16 bytes otherwise, in which case you're good on libstdc++ and libc++, but it would be easy to break that in the future.)

Comment on lines +750 to +752
std::string kernel_name("idx_fill_texture");
add_memory_layout_suffix(kernel_name, vten);
add_dtype_suffix(kernel_name, vten);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto missing reserve; inline size is as little as 16 bytes with libstdc++ (which I don't know if we use).

Comment on lines +52 to +53
std::string kernel_name("fill_texture__test");
add_dtype_suffix(kernel_name, a);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto ditto

std::stringstream kernel_name;
kernel_name << "binary_" << op_name << "_nobroadcast__test";
apply_dtype_suffix(kernel_name, v_dst);
std::string kernel_name = "binary_" + op_name + "_nobroadcast__test";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

building a string with + this way isn't efficient; reserve and call append(). for the record, there are primitives for this in more modern C++ like folly::to and fmt::format and eventually std::format.

@facebook-github-bot
Copy link
Contributor

This pull request has been merged in a983ebc.

@mergennachin mergennachin mentioned this pull request Apr 26, 2024
@SS-JIA SS-JIA deleted the gh/SS-JIA/31/head branch January 24, 2025 19:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. fb-exported Merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants