Skip to content

Commit

Permalink
Gtestsuite Framework and Unit Tests for Pack and Compute Extension APIs
Browse files Browse the repository at this point in the history
- Added framework for unit testing of BLAS and CBLAS interfaces for the
  Pack and Compute Extension APIs.
- These test the integrated functionality of the trio of
  ?gemm_pack_get_size(), ?gemm_pack() and ?gemm_compute() APIs.
- Note: Only MKL can be used as reference for now.

AMD-Internal: [CPUPL-3560]
Change-Id: I801654447a716da06c9ccf9db01d553817871571
  • Loading branch information
Arnav Sharma authored and Arnav Sharma committed Oct 16, 2023
1 parent 6d04444 commit c1612f6
Show file tree
Hide file tree
Showing 8 changed files with 1,403 additions and 1 deletion.
69 changes: 69 additions & 0 deletions gtestsuite/testinghelpers/inc/level3/ref_gemm_compute.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
BLIS
An object-based framework for developing high-performance BLAS-like
libraries.
Copyright (C) 2023, Advanced Micro Devices, Inc. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
- Neither the name(s) of the copyright holder(s) nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#pragma once

#include "common/testing_helpers.h"

/*
* ==========================================================================
* GEMM Compute performs one of the matrix-matrix operations
* C := op( A )*op( B ) + beta*C,
* where op( A ) is one of
* op( A ) = alpha * A or op( A ) = alpha * A**T
* op( A ) = A or op( A ) = A**T
* op( B ) is one of
* op( B ) = alpha * B or op( B ) = alpha * B**T
* op( B ) = B or op( B ) = B**T
* alpha and beta are scalars, and A, B and C are matrices, with op( A )
* an m by k matrix, op( B ) a k by n matrix and C an m by n matrix,
* where either op( A ) or op( B ) or both may be reordered.
==========================================================================
*/

namespace testinghelpers {

template <typename T>
void ref_gemm_compute (
char storage, char trnsa, char trnsb,
char pcka, char pckb,
gtint_t m, gtint_t n, gtint_t k,
T alpha,
T* ap, gtint_t lda,
T* bp, gtint_t ldb,
T beta,
T* cp, gtint_t ldc
);

} //end of namespace testinghelpers
200 changes: 200 additions & 0 deletions gtestsuite/testinghelpers/src/level3/ref_gemm_compute.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
/*
BLIS
An object-based framework for developing high-performance BLAS-like
libraries.
Copyright (C) 2023, Advanced Micro Devices, Inc. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
- Neither the name(s) of the copyright holder(s) nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "blis.h"
#include <dlfcn.h>
#include "level3/ref_gemm_compute.h"

/*
* ==========================================================================
* GEMM Pack and Compute Extension performs the GEMM matrix-matrix operations
* by first packing/reordering A/B matrix and computing the GEMM operation
* on the packed buffer.
*
* Pack:
* Reorders the A or B matrix or both the matrices and scales them with
* alpha.
*
* Compute:
* C := A * B + beta*C,
* where,
* Either A or B or both A and B matrices are packed matrices.
* Alpha and beta are scalars, and A, B and C are matrices, with A
* an m by k matrix, B a k by n matrix and C an m by n matrix,
* where either A or B or both may be scaled by alpha and reordered.
* ==========================================================================
*/

namespace testinghelpers {

template <typename T>
void ref_gemm_compute(char storage, char trnsa, char trnsb, char pcka, char pckb, gtint_t m, gtint_t n, gtint_t k, T alpha,
T* ap, gtint_t lda, T* bp, gtint_t ldb, T beta, T* cp, gtint_t ldc)
{
T unit_alpha = 1.0;
enum CBLAS_ORDER cblas_order;
enum CBLAS_TRANSPOSE cblas_transa;
enum CBLAS_TRANSPOSE cblas_transb;

char_to_cblas_order( storage, &cblas_order );
char_to_cblas_trans( trnsa, &cblas_transa );
char_to_cblas_trans( trnsb, &cblas_transb );

using scalar_t = std::conditional_t<testinghelpers::type_info<T>::is_complex, T&, T>;

typedef gint_t (*Fptr_ref_cblas_gemm_pack_get_size)( const CBLAS_IDENTIFIER,
const f77_int, const f77_int, const f77_int );
Fptr_ref_cblas_gemm_pack_get_size ref_cblas_gemm_pack_get_size;

typedef void (*Fptr_ref_cblas_gemm_pack)( const CBLAS_ORDER, const CBLAS_IDENTIFIER, const CBLAS_TRANSPOSE,
const f77_int, const f77_int, const f77_int, const T, const T*, f77_int,
T*);
Fptr_ref_cblas_gemm_pack ref_cblas_gemm_pack;

typedef void (*Fptr_ref_cblas_gemm_compute)( const CBLAS_ORDER, const f77_int, const f77_int,
const f77_int, const f77_int, const f77_int, const T*, f77_int,
const T*, f77_int, const scalar_t, T*, f77_int);
Fptr_ref_cblas_gemm_compute ref_cblas_gemm_compute;

// Call C function
/* Check the typename T passed to this function template and call respective function.*/
if (typeid(T) == typeid(float))
{
ref_cblas_gemm_pack_get_size = (Fptr_ref_cblas_gemm_pack_get_size)refCBLASModule.loadSymbol("cblas_sgemm_pack_get_size");
ref_cblas_gemm_pack = (Fptr_ref_cblas_gemm_pack)refCBLASModule.loadSymbol("cblas_sgemm_pack");
ref_cblas_gemm_compute = (Fptr_ref_cblas_gemm_compute)refCBLASModule.loadSymbol("cblas_sgemm_compute");
}
else if (typeid(T) == typeid(double))
{
ref_cblas_gemm_pack_get_size = (Fptr_ref_cblas_gemm_pack_get_size)refCBLASModule.loadSymbol("cblas_dgemm_pack_get_size");
ref_cblas_gemm_pack = (Fptr_ref_cblas_gemm_pack)refCBLASModule.loadSymbol("cblas_dgemm_pack");
ref_cblas_gemm_compute = (Fptr_ref_cblas_gemm_compute)refCBLASModule.loadSymbol("cblas_dgemm_compute");
}
else
{
throw std::runtime_error("Error in ref_gemm.cpp: Invalid typename is passed function template.");
}
if( !ref_cblas_gemm_compute ) {
throw std::runtime_error("Error in ref_gemm.cpp: Function pointer == 0 -- symbol not found.");
}

err_t err = BLIS_SUCCESS;

if ( ( pcka == 'P' || pcka == 'p' ) && ( pckb == 'P' || pckb == 'p' ) )
{
// Reorder A
CBLAS_IDENTIFIER cblas_identifierA = CblasAMatrix;
CBLAS_STORAGE cblas_packed = CblasPacked;
gtint_t bufSizeA = ref_cblas_gemm_pack_get_size( cblas_identifierA,
m,
n,
k );

T* aBuffer = (T*) bli_malloc_user( bufSizeA, &err );

ref_cblas_gemm_pack( cblas_order, cblas_identifierA, cblas_transa,
m, n, k, alpha, ap, lda, aBuffer );

// Reorder B
CBLAS_IDENTIFIER cblas_identifierB = CblasBMatrix;
gtint_t bufSizeB = ref_cblas_gemm_pack_get_size( cblas_identifierB,
m,
n,
k );

T* bBuffer = (T*) bli_malloc_user( bufSizeB, &err );

ref_cblas_gemm_pack( cblas_order, cblas_identifierB, cblas_transb,
m, n, k, unit_alpha, bp, ldb, bBuffer );

ref_cblas_gemm_compute( cblas_order, cblas_packed, cblas_packed,
m, n, k, aBuffer, lda, bBuffer, ldb, beta, cp, ldc );

bli_free_user( aBuffer );
bli_free_user( bBuffer );
}
else if ( ( pcka == 'P' || pcka == 'p' ) )
{
// Reorder A
CBLAS_IDENTIFIER cblas_identifier = CblasAMatrix;
CBLAS_STORAGE cblas_packed = CblasPacked;
gtint_t bufSizeA = ref_cblas_gemm_pack_get_size( cblas_identifier,
m,
n,
k );

T* aBuffer = (T*) bli_malloc_user( bufSizeA, &err );

ref_cblas_gemm_pack( cblas_order, cblas_identifier, cblas_transa,
m, n, k, alpha, ap, lda, aBuffer );

ref_cblas_gemm_compute( cblas_order, cblas_packed, cblas_transb,
m, n, k, aBuffer, lda, bp, ldb, beta, cp, ldc );

bli_free_user( aBuffer );
}
else if ( ( pckb == 'P' || pckb == 'p' ) )
{
// Reorder B
CBLAS_IDENTIFIER cblas_identifier = CblasBMatrix;
CBLAS_STORAGE cblas_packed = CblasPacked;
gtint_t bufSizeB = ref_cblas_gemm_pack_get_size( cblas_identifier,
m,
n,
k );

T* bBuffer = (T*) bli_malloc_user( bufSizeB, &err );

ref_cblas_gemm_pack( cblas_order, cblas_identifier, cblas_transb,
m, n, k, alpha, bp, ldb, bBuffer );

ref_cblas_gemm_compute( cblas_order, cblas_transa, cblas_packed,
m, n, k, ap, lda, bBuffer, ldb, beta, cp, ldc );

bli_free_user( bBuffer );
}
else
{
ref_cblas_gemm_compute( cblas_order, cblas_transa, cblas_transb,
m, n, k, ap, lda, bp, ldb, beta, cp, ldc );
}
}

// Explicit template instantiations
template void ref_gemm_compute<float>(char, char, char, char, char, gtint_t, gtint_t, gtint_t, float,
float*, gtint_t, float*, gtint_t, float, float*, gtint_t );
template void ref_gemm_compute<double>(char, char, char, char, char, gtint_t, gtint_t, gtint_t, double,
double*, gtint_t, double*, gtint_t, double, double*, gtint_t );

} //end of namespace testinghelpers
2 changes: 1 addition & 1 deletion gtestsuite/testsuite/level3/gemm/dgemm_generic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ TEST_P(DGemmTest, RandomData)
gtint_t ldc_inc = std::get<10>(GetParam());

// Set the threshold for the errors:
double thresh = 10*m*n*k*testinghelpers::getEpsilon<T>();
double thresh = 10*m*n*testinghelpers::getEpsilon<T>();

//----------------------------------------------------------
// Call test body using these parameters
Expand Down
Loading

0 comments on commit c1612f6

Please sign in to comment.