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

TensorOps kernels refactoring #3346

Open
wants to merge 18 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -337,11 +337,22 @@ set( MIOpen_Source
solver/softmarginloss/forward_softmarginloss.cpp
solver/softmax/attn_softmax.cpp
solver/softmax/softmax.cpp
solver/tensorOp/Op1dTensorGeneric.cpp
solver/tensorOp/Op2dTensorGeneric.cpp
solver/tensorOp/Op2dTensorLite.cpp
solver/tensorOp/Op2dTensorSquash.cpp
solver/tensorOp/Op3dTensorGeneric.cpp
solver/tensorOp/OpTensorFwdBias.cpp
solver/tensorOp/Op4dTensorLite.cpp
solver/tensorOp/OpTensorLeadingOnes.cpp
solver/tensorOp/Op4dTensorGeneric.cpp
solver/tensorOp/Op5dTensorGeneric.cpp
subbuffers.cpp
t5layernorm_api.cpp
target_properties.cpp
temp_file.cpp
tensor.cpp
tensorOp/problem_description.cpp
tensor_api.cpp
transformers_adam_w_api.cpp
seq_tensor.cpp
Expand Down
1 change: 1 addition & 0 deletions src/include/miopen/names.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ struct NetworkConfig
{
NetworkConfig() = default;
explicit NetworkConfig(const std::string& value_) : value(value_) {}
explicit NetworkConfig(std::string&& value_) noexcept : value(std::move(value_)) {}
operator std::string() const { return value; }
const std::string& ToString() const { return value; }

Expand Down
78 changes: 78 additions & 0 deletions src/include/miopen/tensorOp/invoke_params.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*******************************************************************************
*
* MIT License
*
* Copyright (c) 2024 Advanced Micro Devices, Inc.
*
* 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.
*
*******************************************************************************/

#pragma once

#include <miopen/invoke_params.hpp>
#include <miopen/tensor.hpp>

namespace miopen {

namespace tensorOp {

struct InvokeParams : public miopen::InvokeParams
{
InvokeParams(const void* alpha0_,
ConstData_t ATensor_,
const void* alpha1_,
ConstData_t BTensor_,
const void* beta_,
Data_t CTensor_,
const size_t Aoffset_,
const size_t Boffset_,
const size_t Coffset_)
: alpha0(alpha0_),
alpha1(alpha1_),
beta(beta_),
ATensor(ATensor_),
BTensor(BTensor_),
CTensor(CTensor_),
Aoffset(Aoffset_),
Boffset(Boffset_),
Coffset(Coffset_)
{
}

size_t GetWorkspaceSize() const { return 0; }
Data_t GetWorkspace() const { return nullptr; }

public:
const void* alpha0;
const void* alpha1;
const void* beta;

ConstData_t ATensor;
ConstData_t BTensor;
Data_t CTensor;

size_t Aoffset;
size_t Boffset;
size_t Coffset;
};

} // namespace tensorOp

} // namespace miopen
130 changes: 130 additions & 0 deletions src/include/miopen/tensorOp/problem_description.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*******************************************************************************
*
* MIT License
*
* Copyright (c) 2024 Advanced Micro Devices, Inc.
*
* 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.
*
*******************************************************************************/

#pragma once

#include <miopen/problem_description_base.hpp>
#include <miopen/tensor.hpp>

namespace miopen {

struct NetworkConfig;

namespace tensorOp {

struct ProblemDescription : ProblemDescriptionBase
{
ProblemDescription(const miopenTensorOp_t tensorOp_,
const void* beta_,
const TensorDescriptor& aTensorDesc_,
const TensorDescriptor& bTensorDesc_,
const TensorDescriptor& cTensorDesc_,
const bool nonStandardSquash_)
: tensorOp(tensorOp_),
aTensorDesc(aTensorDesc_),
bTensorDesc(bTensorDesc_),
cTensorDesc(cTensorDesc_),
nonStandardSquash(nonStandardSquash_)
{
if(beta_ == nullptr)
{
MIOPEN_THROW(miopenStatusBadParm, "Beta value is nullptr");
}

beta = *(static_cast<const float*>(beta_));

if(aTensorDesc.GetElementSize() != cTensorDesc.GetElementSize())
{
MIOPEN_THROW("A and C Tensors do not match");
}

if(bTensorDesc.GetType() != cTensorDesc.GetType())
{
MIOPEN_THROW("Datatypes for B and C tensors do not match !");
}

const auto& blens = bTensorDesc.GetLengths();
const auto& clens = cTensorDesc.GetLengths();

if(clens.size() > 5)
{
MIOPEN_THROW("Tensor dimension larger than 5: " + std::to_string(clens.size()));
}

if(blens.size() != clens.size())
{
MIOPEN_THROW("Number of dims in B and C Tensors do not match: " +
std::to_string(blens.size()) + ", " + std::to_string(clens.size()));
}

if(!nonStandardSquash)
{
constexpr auto comparator = [](size_t c, size_t b) { return b == 1 || b == c; };
const auto [c_diff, b_diff] =
std::mismatch(clens.begin(), clens.end(), blens.begin(), comparator);
if(c_diff != clens.end())
MIOPEN_THROW("BTensor dim != 1 && BTensor dim != CTensor dim:" +
std::to_string(std::distance(clens.begin(), c_diff)));
}
else
{
// non standard behavior because blens[1] can be not equalt to clens[1]
if(!(clens.size() == 3 && blens[0] == 1 && clens[0] == 1 && blens[2] == clens[2]))
{
MIOPEN_THROW(
"Non standard squashed operation supported only for 3d tensors and for "
"the specific configuration");
}
}
}

miopenTensorOp_t GetTensorOp() const { return tensorOp; }

float GetBeta() const { return beta; }

const TensorDescriptor& GetATensorDesc() const { return aTensorDesc; }
const TensorDescriptor& GetBTensorDesc() const { return bTensorDesc; }
const TensorDescriptor& GetCTensorDesc() const { return cTensorDesc; }

bool GetNonStandardSquash() const { return nonStandardSquash; }

NetworkConfig MakeNetworkConfig() const override;

private:
const miopenTensorOp_t tensorOp;

float beta;

TensorDescriptor aTensorDesc;
TensorDescriptor bTensorDesc;
TensorDescriptor cTensorDesc;

const bool nonStandardSquash;
};

} // namespace tensorOp

} // namespace miopen
Loading