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

Merged
merged 40 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
a2972c0
initial changes and support for 1d generic kernel
novakovicdj Oct 30, 2024
75cecb2
1d solver file name change
novakovicdj Oct 30, 2024
035989c
solver name change in cmakelists.txt
novakovicdj Oct 30, 2024
cf91070
more changes, 2d generic and 2d lite kernel
novakovicdj Oct 31, 2024
f2a11d6
some changes suggested in the comments
novakovicdj Nov 1, 2024
ac13ff3
additional changes
novakovicdj Nov 4, 2024
cadb264
initial switch to solver structure for all kernels, still need to sep…
novakovicdj Nov 5, 2024
63603f0
fix for two kernels in one solver
novakovicdj Nov 6, 2024
976bd84
additional changes
novakovicdj Nov 7, 2024
6be98d0
clang format
novakovicdj Nov 7, 2024
d6ffea5
fwd_conv_bias changed
novakovicdj Nov 7, 2024
89dd24c
tidy some part of the code
novakovicdj Nov 8, 2024
9ba8810
Merge branch 'develop' into tensor_refactoring
novakovicdj Nov 8, 2024
5a9b5ed
fix typos
novakovicdj Nov 8, 2024
c9f310a
implementnting suggestions, updating network_config and changes to po…
novakovicdj Nov 15, 2024
496b414
clang format
novakovicdj Nov 15, 2024
22c6c48
Merge branch 'develop' into tensor_refactoring
novakovicdj Nov 18, 2024
cb6fd6e
change for new Op3dTensorGeneric kernel usage
novakovicdj Nov 18, 2024
6c3d0c2
remove unused variable
novakovicdj Nov 18, 2024
bd0bd61
clang format
novakovicdj Nov 18, 2024
3f14d3a
support for half data type for CL kernels
novakovicdj Nov 19, 2024
042129e
additional changes for support for half type
novakovicdj Nov 19, 2024
371d43c
initial removal of tensorocl.cpp
novakovicdj Nov 20, 2024
155b35f
code tidying
novakovicdj Nov 20, 2024
0b3454c
unit test for tensorOp PD + additional changes requested
novakovicdj Nov 25, 2024
2bef739
Merge branch 'develop' into tensor_refactoring
BrianHarrisonAMD Nov 27, 2024
146070a
fix windows build issue
novakovicdj Nov 29, 2024
a83ac16
Merge branch 'develop' into tensor_refactoring
novakovicdj Nov 29, 2024
b8d9ab0
Merge branch 'tensor_refactoring' of github.com:novakovicdj/MIOpen in…
novakovicdj Nov 29, 2024
6258109
Merge branch 'develop' into tensor_refactoring
BrianHarrisonAMD Dec 3, 2024
0eb63fc
resolved conflict
novakovicdj Dec 5, 2024
3dc0f66
kept changes in CastTensor but in tensor.cpp file
novakovicdj Dec 5, 2024
6e37785
Merge branch 'develop' into tensor_refactoring
BrianHarrisonAMD Dec 17, 2024
edaa59a
Merge branch 'develop' into tensor_refactoring
Vsevolod1983 Jan 2, 2025
c10ac52
Merge branch 'develop' into tensor_refactoring
novakovicdj Jan 8, 2025
8d0c414
Merge branch 'develop' into tensor_refactoring
novakovicdj Jan 10, 2025
b864063
reversed back to using cont Handle
novakovicdj Jan 10, 2025
492ce35
change for accepting double as tensor data type
novakovicdj Jan 10, 2025
0006033
Merge branch 'tensor_refactoring' of github.com:novakovicdj/MIOpen in…
novakovicdj Jan 10, 2025
a7325b2
Merge branch 'develop' into tensor_refactoring
BrianHarrisonAMD Jan 16, 2025
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: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -337,11 +337,13 @@ set( MIOpen_Source
solver/softmarginloss/forward_softmarginloss.cpp
solver/softmax/attn_softmax.cpp
solver/softmax/softmax.cpp
solver/tensor/Op1dTensorGeneric.cpp
subbuffers.cpp
t5layernorm_api.cpp
target_properties.cpp
temp_file.cpp
tensor.cpp
tensor/problem_description.cpp
tensor_api.cpp
transformers_adam_w_api.cpp
seq_tensor.cpp
Expand Down
97 changes: 97 additions & 0 deletions src/include/miopen/tensor/invoke_params.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*******************************************************************************
*
* MIT License
*
* Copyright (c) 2023 Advanced Micro Devices, Inc.
CAHEK7 marked this conversation as resolved.
Show resolved Hide resolved
*
* 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 tensor {

struct InvokeParams : public miopen::InvokeParams
{
InvokeParams(miopenTensorOp_t tensorOp_,
const void* alpha0_,
const TensorDescriptor& aTensorDesc_,
ConstData_t ATensor_,
const void* alpha1_,
const TensorDescriptor& bTensorDesc_,
ConstData_t BTensor_,
const void* beta_,
const TensorDescriptor& cTensorDesc_,
Data_t CTensor_,
const size_t Aoffset_,
const size_t Boffset_,
const size_t Coffset_,
const bool nonStandardSquash_)
: alpha0(alpha0_),
alpha1(alpha1_),
beta(beta_),
tensorOp(tensorOp_),
aTensorDesc(aTensorDesc_),
ATensor(ATensor_),
bTensorDesc(bTensorDesc_),
BTensor(BTensor_),
cTensorDesc(cTensorDesc_),
CTensor(CTensor_),
Aoffset(Aoffset_),
Boffset(Boffset_),
Coffset(Coffset_),
nonStandardSquash(nonStandardSquash_)
{
}

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

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

miopenTensorOp_t tensorOp;

TensorDescriptor aTensorDesc;
ConstData_t ATensor;

TensorDescriptor bTensorDesc;
ConstData_t BTensor;

TensorDescriptor cTensorDesc;
Data_t CTensor;

size_t Aoffset;
size_t Boffset;
size_t Coffset;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to handle this internally? IIRC it should be possible to externally pass any subtensor via changing pointer+descriptor. If so this is a duplicated functionality

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that the main point is the pointer is void * and actual type is an miopen_Type_t enum. That's why you can't just add them without special helpers.


bool nonStandardSquash;
};

} // namespace tensor

} // namespace miopen
152 changes: 152 additions & 0 deletions src/include/miopen/tensor/problem_description.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*******************************************************************************
*
* MIT License
*
* Copyright (c) 2023 Advanced Micro Devices, Inc.
CAHEK7 marked this conversation as resolved.
Show resolved Hide resolved
*
* 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 tensor {

struct ProblemDescription : ProblemDescriptionBase
{
ProblemDescription(const miopenTensorOp_t tensorOp_,
const void* alpha0_,
const void* alpha1_,
const void* beta_,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check this conversation
https://github.com/ROCm/MIOpen/pull/3346/files#r1824480257

Probably alpha0/1 must not be a part of the PD, ideally beta as well, but right now it has to be there..

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would a bool marking if alpha0/... has a "default" value meaning no additional work required suffice?

const TensorDescriptor& aTensorDesc_,
const TensorDescriptor& bTensorDesc_,
const TensorDescriptor& cTensorDesc_,
const bool nonStandardSquash_)
: tensorOp(tensorOp_),
aTensorDesc(aTensorDesc_),
bTensorDesc(bTensorDesc_),
cTensorDesc(cTensorDesc_),
nonStandardSquash(nonStandardSquash_)
{
CheckAndAssignAlphaBeta(alpha0_, alpha1_, 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 !");
}

auto blens = bTensorDesc.GetLengths();
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)
{
for(std::size_t i = 0; i < clens.size(); i++)
CAHEK7 marked this conversation as resolved.
Show resolved Hide resolved
{
if(blens[i] != 1 && blens[i] != clens[i])
{
MIOPEN_THROW("BTensor dim != 1 && BTensor dim != CTensor dim: " +
std::to_string(i));
}
}
}
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");
}
}
}

const miopenTensorOp_t GetTensorOp() const { return tensorOp; }

const void* GetAlpha0() const { return alpha0; }
const void* GetAlpha1() const { return alpha1; }
const void* GetBeta() const { return beta; }

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

const bool GetNonStandardSquash() const { return nonStandardSquash; }

NetworkConfig MakeNetworkConfig() const override;

private:
void CheckAndAssignAlphaBeta(const void* alpha0_, const void* alpha1_, const void* beta_)
{
if(alpha0_ == nullptr)
{
MIOPEN_THROW(miopenStatusBadParm, "Alpha0 value is nullptr");
}
if(alpha1_ == nullptr)
{
MIOPEN_THROW(miopenStatusBadParm, "Alpha1 value is nullptr");
}
if(beta_ == nullptr)
{
MIOPEN_THROW(miopenStatusBadParm, "Beta value is nullptr");
}

alpha0 = alpha0_;
alpha1 = alpha1_;
beta = beta_;
CAHEK7 marked this conversation as resolved.
Show resolved Hide resolved
}

const miopenTensorOp_t tensorOp;

const void* alpha0;
const void* alpha1;
const void* beta;

const TensorDescriptor& aTensorDesc;
const TensorDescriptor& bTensorDesc;
const TensorDescriptor& cTensorDesc;

const bool nonStandardSquash;
};

} // namespace tensor

} // namespace miopen
62 changes: 62 additions & 0 deletions src/include/miopen/tensor/solvers.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*******************************************************************************
*
* MIT License
*
* Copyright (c) 2023 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/solver.hpp>
#include <miopen/tensor/problem_description.hpp>

#include <utility>

namespace miopen {

namespace solver {

namespace tensor {

using TensorOpSolver = NonTunableSolverBase<ExecutionContext, miopen::tensor::ProblemDescription>;

struct Op1dTensorGeneric final : TensorOpSolver
{
const std::string& SolverDbId() const override { return GetSolverDbId<Op1dTensorGeneric>(); }

bool IsApplicable(const ExecutionContext& context,
const miopen::tensor::ProblemDescription& problem) const override;

ConvSolution GetSolution(const ExecutionContext& context,
const miopen::tensor::ProblemDescription& problem) const override;

std::size_t GetWorkspaceSize(const ExecutionContext& context,
const miopen::tensor::ProblemDescription& problem) const override;

bool MayNeedWorkspace() const override { return false; }
};

} // namespace tensor
CAHEK7 marked this conversation as resolved.
Show resolved Hide resolved

} // namespace solver

} // namespace miopen
16 changes: 16 additions & 0 deletions src/include/miopen/tensor_ops.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,22 @@ MIOPEN_INTERNALS_EXPORT void OpTensor(const Handle& handle,
size_t Coffset = 0,
bool nonStandardSquash = false);

MIOPEN_INTERNALS_EXPORT void OpTensorNew(Handle& handle,
CAHEK7 marked this conversation as resolved.
Show resolved Hide resolved
miopenTensorOp_t tensorOp,
const void* alpha0,
const TensorDescriptor& aTensorDesc,
ConstData_t ATensor,
const void* alpha1,
const TensorDescriptor& bTensorDesc,
ConstData_t BTensor,
const void* beta,
const TensorDescriptor& cTensorDesc,
Data_t CTensor,
size_t Aoffset = 0,
size_t Boffset = 0,
size_t Coffset = 0,
bool nonStandardSquash = false);

MIOPEN_INTERNALS_EXPORT void CopyTensor(const Handle& handle,
const TensorDescriptor& srcDesc,
ConstData_t src,
Expand Down
Loading