-
Notifications
You must be signed in to change notification settings - Fork 21.3k
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
[vulkan] Add mean.dim op for vulkan #47312
Changes from 1 commit
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 |
---|---|---|
|
@@ -2,36 +2,26 @@ | |
#define PRECISION $precision | ||
layout(std430) buffer; | ||
layout(std430) uniform; | ||
layout(set = 0, rgba16f, binding = 0) writeonly PRECISION uniform image3D uOutput; | ||
layout(set = 0, binding = 1) uniform PRECISION sampler3D uInput; | ||
layout(set = 0, binding = 2) uniform constBlock { | ||
layout(set = 0, binding = 0, rgba16f) uniform PRECISION restrict writeonly image3D uOutput; | ||
layout(set = 0, binding = 1) uniform PRECISION sampler3D uInput; | ||
layout(set = 0, binding = 2) uniform Block { | ||
int W; | ||
int H; | ||
int OW; | ||
int OH; | ||
} | ||
uConstBlock; | ||
} uBlock; | ||
|
||
layout(local_size_x_id = 1, local_size_y_id = 2, local_size_z_id = 3) in; | ||
|
||
void main() { | ||
ivec3 pos = ivec3(gl_GlobalInvocationID); | ||
int W = uConstBlock.W; | ||
int H = uConstBlock.H; | ||
int OW = uConstBlock.OW; | ||
int OH = uConstBlock.OH; | ||
vec4 r = vec4(1.0) / float(W) / float(H); | ||
vec4 r = vec4(1.0) / float(uBlock.W) / float(uBlock.H); | ||
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. Check adaptive_avg_pool here https://github.com/pytorch/pytorch/pull/47261/files for another implementation. Divisions are typically slower than multiplications. |
||
vec4 acc = vec4(0); | ||
int xi, yi; | ||
for (xi = 0; xi < W; ++xi) { | ||
for (yi = 0; yi < H; ++yi) { | ||
for (xi = 0; xi < uBlock.W; ++xi) { | ||
for (yi = 0; yi < uBlock.H; ++yi) { | ||
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. Iterate over y in the outer loop, and x in the inner loop. We are dealing with a texture that is packed in an opaque format, so this might not apply, but if and when the memory is laid out linearly that traversal has better locality of access. 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. Check adaptive_avg_pool shader here: https://github.com/pytorch/pytorch/pull/47261/files |
||
acc += texelFetch(uInput, ivec3(xi, yi, pos.z), 0); | ||
} | ||
} | ||
vec4 outValue = r * acc; | ||
for (int vi = 0; vi < 4; ++vi) { | ||
int oy = (4 * pos.z + vi) / OW; | ||
int ox = (4 * pos.z + vi) % OW; | ||
imageStore(uOutput, ivec3(ox, oy, 0), vec4(outValue[vi], 0, 0, 0)); | ||
} | ||
|
||
imageStore(uOutput, pos, outValue); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#version 450 core | ||
#define PRECISION $precision | ||
layout(std430) buffer; | ||
layout(std430) uniform; | ||
layout(set = 0, binding = 0, rgba16f) uniform PRECISION restrict writeonly image3D uOutput; | ||
layout(set = 0, binding = 1) uniform PRECISION sampler3D uInput; | ||
layout(set = 0, binding = 2) uniform Block { | ||
int W; | ||
int H; | ||
} uBlock; | ||
|
||
layout(local_size_x_id = 1, local_size_y_id = 2, local_size_z_id = 3) in; | ||
|
||
void main() { | ||
ivec3 pos = ivec3(gl_GlobalInvocationID); | ||
vec4 r = vec4(1.0) / float(uBlock.W) / float(uBlock.H); | ||
vec4 acc = vec4(0); | ||
int xi, yi; | ||
int zi = (imageSize(uOutput).x*pos.y + pos.x)/4; | ||
int zo = (imageSize(uOutput).x*pos.y + pos.x)%4; | ||
for (xi = 0; xi < uBlock.W; ++xi) { | ||
for (yi = 0; yi < uBlock.H; ++yi) { | ||
acc += texelFetch(uInput, ivec3(xi, yi, zi), 0); | ||
} | ||
} | ||
vec4 outValue = r * acc; | ||
|
||
int test = (imageSize(uOutput).x*pos.x + pos.x); | ||
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. clean? |
||
imageStore(uOutput, pos, vec4(outValue[zo], 0,0,0)); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -169,6 +169,42 @@ TEST(VulkanTest, mm) { | |
ASSERT_TRUE(check); | ||
} | ||
|
||
TEST(VulkanTest, mean) { | ||
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. VulkanAPITest |
||
auto t_in = | ||
at::rand({5,3,9,9}, at::TensorOptions(at::kCPU).dtype(at::kFloat)); | ||
auto t_out_expected = at::mean(t_in, {-1,-2}, false); | ||
auto tv_in = t_in.vulkan(); | ||
|
||
auto tv_out = at::mean(tv_in, {-1,-2}, false); | ||
auto t_out = tv_out.cpu(); | ||
|
||
const auto check = almostEqual(t_out, t_out_expected); | ||
if (!check) { | ||
//std::cout << "original:\n" << t_in << std::endl; | ||
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. clean or uncomment? |
||
std::cout << "expected:\n" << t_out_expected << std::endl; | ||
std::cout << "got:\n" << t_out << std::endl; | ||
} | ||
ASSERT_TRUE(check); | ||
} | ||
|
||
TEST(VulkanTest, mean_keep_dim) { | ||
auto t_in = | ||
at::rand({10, 3, 21, 21}, at::TensorOptions(at::kCPU).dtype(at::kFloat)); | ||
auto t_out_expected = at::mean(t_in, {-1, -2}, true); | ||
auto tv_in = t_in.vulkan(); | ||
|
||
auto tv_out = at::mean(tv_in, {-1, -2}, true); | ||
auto t_out = tv_out.cpu(); | ||
|
||
const auto check = almostEqual(t_out, t_out_expected); | ||
if (!check) { | ||
//std::cout << "original:\n" << t_in << std::endl; | ||
std::cout << "expected:\n" << t_out_expected << std::endl; | ||
std::cout << "got:\n" << t_out << std::endl; | ||
} | ||
ASSERT_TRUE(check); | ||
} | ||
|
||
TEST(VulkanTest, adaptive_avg_pool2d) { | ||
auto t_in = | ||
at::rand({1, 2, 7, 7}, at::TensorOptions(at::kCPU).dtype(at::kFloat)); | ||
|
@@ -180,6 +216,7 @@ TEST(VulkanTest, adaptive_avg_pool2d) { | |
|
||
const auto check = almostEqual(t_out, t_out_expected); | ||
if (!check) { | ||
std::cout << "original:\n" << t_in << std::endl; | ||
std::cout << "expected:\n" << t_out_expected << std::endl; | ||
std::cout << "got:\n" << t_out << std::endl; | ||
} | ||
|
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.
Please mark this with PRECISION and restrict.