Skip to content
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

Implement Permutation with CK #150

Merged
merged 5 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ hiptensor_version.hpp
hiptensor-version.hpp

# Generated source file
test/01_contraction/configs/*.hpp
test/*/configs/*.hpp

# Precompiled Headers
*.gch
Expand Down
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ set(CMAKE_CXX_EXTENSIONS OFF)
if( CMAKE_PROJECT_NAME STREQUAL "hiptensor" )
option( HIPTENSOR_BUILD_TESTS "Build hiptensor tests" ON )
option( HIPTENSOR_BUILD_SAMPLES "Build hiptensor samples" ON )
option( HIPTENSOR_DATA_LAYOUT_COL_MAJOR "Set hiptensor data layout to column major" ON )
endif()

# Setup output paths
Expand Down Expand Up @@ -93,6 +94,13 @@ else()
endif()
message( VERBOSE "AMDGPU_TARGETS=${AMDGPU_TARGETS}")

if(HIPTENSOR_DATA_LAYOUT_COL_MAJOR)
add_compile_definitions(HIPTENSOR_DATA_LAYOUT_COL_MAJOR=1)
else()
add_compile_definitions(HIPTENSOR_DATA_LAYOUT_COL_MAJOR=0)
endif()
message("-- HIPTENSOR_DATA_LAYOUT_COL_MAJOR=${HIPTENSOR_DATA_LAYOUT_COL_MAJOR}")

# Setup HIP
find_package(hip REQUIRED )
message(STATUS "HIP version: ${hip_VERSION}")
Expand Down
5 changes: 5 additions & 0 deletions library/include/hiptensor/internal/hiptensor_utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ namespace std

return os;
}
static ostream& operator<<(ostream& os, const _Float16 value)
{
os << static_cast<float>(value);
return os;
}
}

#endif // HIPTENSOR_UTILITY_INTERNAL_HPP
2 changes: 1 addition & 1 deletion library/src/contraction/hiptensor_contraction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ hiptensorStatus_t hiptensorInitContractionDescriptor(const hiptensorHandle_t*
auto& logger = Logger::instance();

// Log API access
char msg[1024];
char msg[2048];
snprintf(
msg,
sizeof(msg),
Expand Down
3 changes: 2 additions & 1 deletion library/src/hiptensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ hiptensorStatus_t hiptensorInitTensorDescriptor(const hiptensorHandle_t* han
return HIPTENSOR_STATUS_NOT_INITIALIZED;
}

if((lens == nullptr) || ((dataType != HIP_R_32F) && (dataType != HIP_R_64F))
if((lens == nullptr)
|| ((dataType != HIP_R_16F) && (dataType != HIP_R_32F) && (dataType != HIP_R_64F))
|| unaryOp != HIPTENSOR_OP_IDENTITY)
{
auto errorCode = HIPTENSOR_STATUS_INVALID_VALUE;
Expand Down
133 changes: 132 additions & 1 deletion library/src/permutation/hiptensor_permutation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
*******************************************************************************/
#include <hiptensor/hiptensor.hpp>

#include "logger.hpp"
#include "permutation_ck.hpp"

hiptensorStatus_t hiptensorPermutation(const hiptensorHandle_t* handle,
const void* alpha,
const void* A,
Expand All @@ -36,5 +39,133 @@ hiptensorStatus_t hiptensorPermutation(const hiptensorHandle_t* handle
const hipDataType typeScalar,
const hipStream_t stream)
{
return HIPTENSOR_STATUS_SUCCESS;
using hiptensor::Logger;
auto& logger = Logger::instance();

// Log API access
char msg[2048];
snprintf(msg,
sizeof(msg),
"handle=%p, alpha=%p, A=%p, descA=%p, modeA=%p, B=%p, descB=%p, modeB=%p, "
"typeScalar=0x%02X, stream=%p",
handle,
alpha,
A,
descA,
modeA,
B,
descB,
modeB,
(unsigned int)typeScalar,
stream);

logger->logAPITrace("hiptensorPermutation", msg);

if(!handle || !alpha || !A || !descA || !modeA || !B || !descB || !modeB)
{
auto errorCode = HIPTENSOR_STATUS_NOT_INITIALIZED;
auto printErrorMessage = [&logger, errorCode](const std::string& paramName) {
char msg[512];
snprintf(msg,
sizeof(msg),
"Initialization Error : %s = nullptr (%s)",
paramName.c_str(),
hiptensorGetErrorString(errorCode));
logger->logError("hiptensorPermutation", msg);
};
if(!handle)
{
printErrorMessage("handle");
}
if(!alpha)
{
printErrorMessage("alpha");
}
if(!A)
{
printErrorMessage("A");
}
if(!descA)
{
printErrorMessage("descA");
}
if(!modeA)
{
printErrorMessage("modeA");
}
if(!B)
{
printErrorMessage("B");
}
if(!descB)
{
printErrorMessage("descB");
}
if(!modeB)
{
printErrorMessage("modeB");
}
return errorCode;
}

if(descA->mType != HIP_R_16F && descA->mType != HIP_R_32F)
{
auto errorCode = HIPTENSOR_STATUS_NOT_SUPPORTED;
snprintf(msg,
sizeof(msg),
"Unsupported Data Type Error : The supported data types of A and B are HIP_R_16F "
"and HIP_R_32F (%s)",
hiptensorGetErrorString(errorCode));
logger->logError("hiptensorPermutation", msg);
return errorCode;
}

if(descA->mType != descB->mType)
{
auto errorCode = HIPTENSOR_STATUS_INVALID_VALUE;
snprintf(msg,
sizeof(msg),
"Mismatched Data Type Error : Data types of A and B are not the same. (%s)",
hiptensorGetErrorString(errorCode));
logger->logError("hiptensorPermutation", msg);
return errorCode;
}

if(typeScalar != HIP_R_16F && typeScalar != HIP_R_32F)
{
auto errorCode = HIPTENSOR_STATUS_NOT_SUPPORTED;
snprintf(msg,
sizeof(msg),
"Unsupported Data Type Error : The supported data types of alpha are HIP_R_16F "
"and HIP_R_32F (%s)",
hiptensorGetErrorString(errorCode));
logger->logError("hiptensorPermutation", msg);
return errorCode;
}

if(descA->mType == HIP_R_16F)
{
return hiptensor::detail::permuteByCk(alpha,
static_cast<const _Float16*>(A),
descA,
modeA,
static_cast<_Float16*>(B),
descB,
modeB,
typeScalar,
stream);
}
else if(descA->mType == HIP_R_32F)
{
return hiptensor::detail::permuteByCk(alpha,
static_cast<const float*>(A),
descA,
modeA,
static_cast<float*>(B),
descB,
modeB,
typeScalar,
stream);
}
return HIPTENSOR_STATUS_NOT_SUPPORTED;
}
48 changes: 48 additions & 0 deletions library/src/permutation/permutation_ck.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*******************************************************************************
*
* MIT License
*
* Copyright (C) 2023-2024 Advanced Micro Devices, Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*******************************************************************************/
#ifndef HIPTENSOR_PERMUTATION_CK_COL_HPP
#define HIPTENSOR_PERMUTATION_CK_COL_HPP
#include <hiptensor/hiptensor.hpp>

namespace hiptensor
{
namespace detail
{
template <typename DataType>
hiptensorStatus_t permuteByCk(const void* alpha,
const DataType* A,
const hiptensorTensorDescriptor_t* descA,
const int32_t modeA[],
DataType* B,
const hiptensorTensorDescriptor_t* descB,
const int32_t modeB[],
const hipDataType typeScalar);

}
}

#include "permutation_ck_impl.hpp"
#endif // HIPTENSOR_PERMUTATION_CK_COL_HPP
Loading
Loading