[vulkan] Fix warnings: static_cast, remove unused#42195
[vulkan] Fix warnings: static_cast, remove unused#42195IvanKobzarev wants to merge 11 commits intogh/IvanKobzarev/66/basefrom
Conversation
[ghstack-poisoned]
💊 CI failures summary and remediationsAs of commit 760a8ed (more details on the Dr. CI page):
ci.pytorch.org: 1 failedThis comment was automatically generated by Dr. CI (expand for details).Follow this link to opt-out of these comments for your Pull Requests.Please report bugs/suggestions on the GitHub issue tracker or post in the (internal) Dr. CI Users group. This comment has been revised 29 times. |
| }; | ||
| ConstBlock constBlock{image.w(), image.h()}; | ||
| ConstBlock constBlock{static_cast<int32_t>(image.w()), | ||
| static_cast<int32_t>(image.h())}; |
There was a problem hiding this comment.
I think safe_downcast is a better choice here but that might requires some refactoring to move it out of Pool.h.
Differential Revision: [D22803035](https://our.internmc.facebook.com/intern/diff/D22803035) Fix warnings for internal BUCK build that has stricter compilation flags (in comparison with oss build). - static_cast<int32_t> where int64_t sizes, strides are used in Vulkan shaders as int32_t - unused variables in `vulkan_test.cpp` [ghstack-poisoned]
Differential Revision: [D22803035](https://our.internmc.facebook.com/intern/diff/D22803035) Fix warnings for internal BUCK build that has stricter compilation flags (in comparison with oss build). - static_cast<int32_t> where int64_t sizes, strides are used in Vulkan shaders as int32_t - unused variables in `vulkan_test.cpp` [ghstack-poisoned]
Differential Revision: [D22803035](https://our.internmc.facebook.com/intern/diff/D22803035) Fix warnings for internal BUCK build that has stricter compilation flags (in comparison with oss build). - static_cast<int32_t> where int64_t sizes, strides are used in Vulkan shaders as int32_t - unused variables in `vulkan_test.cpp` [ghstack-poisoned]
Differential Revision: [D22803035](https://our.internmc.facebook.com/intern/diff/D22803035) Fix warnings for internal BUCK build that has stricter compilation flags (in comparison with oss build). - static_cast<int32_t> where int64_t sizes, strides are used in Vulkan shaders as int32_t - unused variables in `vulkan_test.cpp` [ghstack-poisoned]
Differential Revision: [D22803035](https://our.internmc.facebook.com/intern/diff/D22803035) Fix warnings for internal BUCK build that has stricter compilation flags (in comparison with oss build). - static_cast<int32_t> where int64_t sizes, strides are used in Vulkan shaders as int32_t - unused variables in `vulkan_test.cpp` [ghstack-poisoned]
Differential Revision: [D22803035](https://our.internmc.facebook.com/intern/diff/D22803035) Fix warnings for internal BUCK build that has stricter compilation flags (in comparison with oss build). - static_cast<int32_t> where int64_t sizes, strides are used in Vulkan shaders as int32_t - unused variables in `vulkan_test.cpp` [ghstack-poisoned]
|
|
||
| namespace detail { | ||
| template <typename To, typename From> | ||
| inline constexpr To safe_downcast(From v) { |
There was a problem hiding this comment.
I'm not sure this can be constexpr anymore considering that some implementations of TORCH_CHECK might throw so if you run into any compilation issues, that's a potential reason.
There was a problem hiding this comment.
Also please mark v as const.
| constexpr Type max{static_cast<Type>(std::numeric_limits<To>::max())}; | ||
| TORCH_CHECK(min <= v && v <= max, "Cast failed: out of range"); | ||
| return static_cast<To>(v); | ||
| } |
There was a problem hiding this comment.
This function should probably be in c10 since it is general purpose. You can talk with David, Dima, and c10 maintainers if you are OK with moving it there.
| int64_t KW) { | ||
| return {{ALIGN_UP4(C), UP_DIV(OC, 4), KH * KW}, | ||
| {ALIGN_UP4(C), UP_DIV(OC, 4), KH * KW}}; | ||
| const uint32_t Cup4 = ALIGN_UP4(static_cast<uint32_t>(C)); |
There was a problem hiding this comment.
I should have mentioned this earlier, but inline functions (and functions in general) are superior to macros. A change you can consider in a later PR.
| constBuffer.bind(descriptorSet, 4); | ||
|
|
||
| WorkGroupSize workGroupSize{1, 1, params.OC_4}; | ||
| WorkGroupSize workGroupSize{1, 1, static_cast<uint32_t>(params.OC_4)}; |
| return {{ALIGN_UP4(C), UP_DIV(OC, 4), KH * KW}, | ||
| {ALIGN_UP4(C), UP_DIV(OC, 4), KH * KW}}; | ||
| const uint32_t Cup4 = ALIGN_UP4(static_cast<uint32_t>(C)); | ||
| const uint32_t OC_4 = UP_DIV(static_cast<uint32_t>(OC), 4); |
| {ALIGN_UP4(C), UP_DIV(OC, 4), KH * KW}}; | ||
| const uint32_t Cup4 = ALIGN_UP4(static_cast<uint32_t>(C)); | ||
| const uint32_t OC_4 = UP_DIV(static_cast<uint32_t>(OC), 4); | ||
| const uint32_t Z = static_cast<uint32_t>(KH) * static_cast<uint32_t>(KW); |
| namespace detail { | ||
| template <typename To, typename From> | ||
| inline constexpr To safe_downcast(From v) { | ||
| if (std::is_signed<From>::value && std::is_unsigned<To>::value) { |
There was a problem hiding this comment.
Also this is more of an advanced topic (just a tip - you don't need to make the change), but since this condition is completely a compile-time condition, you could either use SFINAE in pre C++-17 or if constexpr in C++-17 and onward to isolate it. That would also prevent against compilation errors in case this if block contained a block of code that would only make sense (i.e. was only compilable) if this condition held true but not in general. This is not the case here.
There was a problem hiding this comment.
I have updated PR with enable_if construction :
template <typename To, typename From>
inline constexpr To safe_downcast_internal(From v) {
typedef std::common_type_t<From, To> Type;
constexpr Type min{static_cast<Type>(std::numeric_limits<To>::lowest())};
const Type value{static_cast<Type>(v)};
constexpr Type max{static_cast<Type>(std::numeric_limits<To>::max())};
TORCH_CHECK(min <= v && v <= max, "Cast failed: out of range");
return static_cast<To>(v);
}
template <typename To, typename From>
inline constexpr bool IsSignedToUnsigned() {
return std::is_signed<From>::value && std::is_unsigned<To>::value;
}
template <
typename To,
typename From,
std::enable_if_t<IsSignedToUnsigned<To, From>(), bool> = true>
inline constexpr To safe_downcast(From v) {
TORCH_CHECK(v >= From{}, "Cast failed: negative signed to unsigned");
return safe_downcast_internal<To, From>(v);
}
template <
typename To,
typename From,
std::enable_if_t<!IsSignedToUnsigned<To, From>(), bool> = true>
inline constexpr To safe_downcast(From v) {
return safe_downcast_internal<To, From>(v);
}
|
By the way, I also suggest calling this numeric_cast. Downcast implies some sort of inheritance hierarchy. |
Differential Revision: [D22803035](https://our.internmc.facebook.com/intern/diff/D22803035) Fix warnings for internal BUCK build that has stricter compilation flags (in comparison with oss build). - static_cast<int32_t> where int64_t sizes, strides are used in Vulkan shaders as int32_t - unused variables in `vulkan_test.cpp` [ghstack-poisoned]
Differential Revision: [D22803035](https://our.internmc.facebook.com/intern/diff/D22803035) Fix warnings for internal BUCK build that has stricter compilation flags (in comparison with oss build). - static_cast<int32_t> where int64_t sizes, strides are used in Vulkan shaders as int32_t - unused variables in `vulkan_test.cpp` [ghstack-poisoned]
Differential Revision: [D22803035](https://our.internmc.facebook.com/intern/diff/D22803035) Fix warnings for internal BUCK build that has stricter compilation flags (in comparison with oss build). - static_cast<int32_t> where int64_t sizes, strides are used in Vulkan shaders as int32_t - unused variables in `vulkan_test.cpp` [ghstack-poisoned]
Differential Revision: [D22803035](https://our.internmc.facebook.com/intern/diff/D22803035) Fix warnings for internal BUCK build that has stricter compilation flags (in comparison with oss build). - static_cast<int32_t> where int64_t sizes, strides are used in Vulkan shaders as int32_t - unused variables in `vulkan_test.cpp` [ghstack-poisoned]
|
@IvanKobzarev merged this pull request in 04c62d4. |
Summary: Pull Request resolved: pytorch#42195 Test Plan: Imported from OSS Reviewed By: AshkanAliabadi Differential Revision: D22803035 Pulled By: IvanKobzarev fbshipit-source-id: d7bf256437eccb5c421a7fd0aa8ec23a8fec0470
Stack from ghstack:
Differential Revision: D22803035
Fix warnings for internal BUCK build that has stricter compilation flags (in comparison with oss build).
vulkan_test.cpp