-
Notifications
You must be signed in to change notification settings - Fork 96
fix the issue of GEMM validation failure #378
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
base: amd-staging
Are you sure you want to change the base?
Changes from 2 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 |
|---|---|---|
|
|
@@ -28,9 +28,10 @@ | |
| #include <algorithm> | ||
| #include <iostream> | ||
| #include <vector> | ||
|
|
||
| #include <cstdlib> | ||
| #include <cassert> | ||
| #include <cstddef> | ||
| #include <memory> | ||
|
|
||
| /// \brief Multiplies matrices \p A and \p B and stores the result to \p C. | ||
| /// - The number of rows of the result matrix is equal to the number of rows of matrix A | ||
|
|
@@ -108,6 +109,53 @@ __global__ void matrix_multiplication_kernel(const float* A, | |
| // Every thread stores the final result to global memory. | ||
| C[block_offset + b_cols * ty + tx] = thread_result; | ||
| } | ||
|
|
||
| bool checkValidity (const float* A, const float* B, const float* C, size_t a_rows,size_t a_cols, size_t b_cols) | ||
| { | ||
| const float EPSILON = 0.001; | ||
| const int BLOCK_SIZE = 64; | ||
|
|
||
| std::vector<float> golden_c(b_cols * a_rows); | ||
|
|
||
| for (size_t i_block = 0; i_block < a_rows; i_block += BLOCK_SIZE) { | ||
| for (size_t j_block = 0; j_block < b_cols; j_block += BLOCK_SIZE) { | ||
| for (size_t t_block = 0; t_block < a_cols; t_block += BLOCK_SIZE) { | ||
| int i_end = std::min(i_block + BLOCK_SIZE, a_rows); | ||
| int j_end = std::min(j_block + BLOCK_SIZE, b_cols); | ||
| int t_end = std::min(t_block + BLOCK_SIZE, a_cols); | ||
|
|
||
|
|
||
| for (int i = i_block; i < i_end; ++i) { | ||
| for (int t = t_block; t < t_end; ++t) { | ||
| float a_val = A[i * a_cols + t]; | ||
| for (int j = j_block; j < j_end; ++j) { | ||
| golden_c[i * b_cols + j] += a_val * B[t * b_cols + j]; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for (size_t i = 0; i < a_rows; ++i) | ||
| { | ||
| for (size_t j = 0; j < b_cols; ++j) | ||
| { | ||
| float absdiff = abs(C[i*b_cols+j] - golden_c[i*b_cols+j]); | ||
| if(absdiff > EPSILON) | ||
| { | ||
| std::cerr << "\nVALIDATION FAILED!!!\n reference" << "[" << i << ", " << j << "] = " | ||
| << golden_c[i*b_cols+j] << ",\n calculated" << "[" << i << ", " << j << "] = " | ||
| << C[i*b_cols+j] | ||
| << ",\n absolute difference" << "[" << i << ", " << j << "] = " << absdiff << "\n" | ||
| << "Further validation was stopped\n\n"; | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| template<unsigned int BlockSize> | ||
| void configure_parser(cli::Parser& parser) | ||
| { | ||
|
|
@@ -165,11 +213,9 @@ int main(int argc, const char* argv[]) | |
| std::vector<float> B(b_cols * b_rows); | ||
| std::vector<float> C(c_cols * c_rows); | ||
|
|
||
| // Set matrix elements to a constant on the host. | ||
| std::fill(A.begin(), A.end(), 1.F); | ||
|
|
||
| constexpr float b_value = 0.02F; | ||
| std::fill(B.begin(), B.end(), b_value); | ||
| // Set matrix elements to random value on the host. | ||
| for (size_t i = 0; i < A.size(); ++i) A[i] = static_cast<float>(rand() / RAND_MAX ); | ||
|
Collaborator
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 be
Author
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. Thanks for your reminding. we can change it to be "static_cast(rand() / (RAND_MAX+1.0f) );", and it will generate [0,1) random float. |
||
| for (size_t i = 0; i < B.size(); ++i) B[i] = static_cast<float>(rand() / RAND_MAX ); | ||
|
Collaborator
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. Same as above
Author
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. I have updated it to be "static_cast(rand() / (RAND_MAX+1.0f) )", and verified that it can generate [0,1) random float value |
||
|
|
||
| const size_t a_bytes = sizeof(float) * A.size(); | ||
| const size_t b_bytes = sizeof(float) * B.size(); | ||
|
|
@@ -203,11 +249,7 @@ int main(int argc, const char* argv[]) | |
| HIP_CHECK(hipFree(d_C)); | ||
|
|
||
| // Check if the resulting elements match the expectation. | ||
| constexpr float tolerance = 0.001F; | ||
| const bool validation_passed = std::all_of( | ||
| C.begin(), | ||
| C.end(), | ||
| [=](const float value) { return tolerance > std::abs(value - a_cols * b_value); }); | ||
| bool validation_passed = checkValidity(A.data(),B.data(),C.data(),a_rows,a_cols,b_cols); | ||
| if(validation_passed) | ||
| { | ||
| std::cout << "Validation passed." << 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.
Is this necessary?
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.
I have removed them in the new commit