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 NLLLoss (contiguous, no reduction, forward only) #3

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from 19 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
1 change: 0 additions & 1 deletion .githooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,3 @@ do
"$format" -i -style=file "$file"
fi
done

1 change: 1 addition & 0 deletions docs/reference/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ The MIOpen API library is structured as follows:
* :doc:`GroupNorm <../doxygen/html/group__groupnorm>` (experimental)
* :doc:`Cat <../doxygen/html/group__cat>` (experimental)
* :doc:`Argmax<./argmax>` (experimental)
* :doc:`NLLLoss<../doxygen/html/group__nllloss>` (experimental)
1 change: 1 addition & 0 deletions driver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ add_executable(MIOpenDriver
dm_softmax.cpp
dm_sum.cpp
dm_tensorop.cpp
dm_nllloss.cpp
et16kr marked this conversation as resolved.
Show resolved Hide resolved
main.cpp
registry_driver_maker.cpp
rocrand_wrapper.cpp)
Expand Down
40 changes: 40 additions & 0 deletions driver/dm_nllloss.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*******************************************************************************
*
* 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.
*
*******************************************************************************/
#include "registry_driver_maker.hpp"
#include "nllloss_driver.hpp"

static Driver* makeDriver(const std::string& base_arg)
{
if(base_arg == "nllloss")
return new NLLLossDriver<float, float>();
if(base_arg == "nlllossfp16")
return new NLLLossDriver<float16, float>();
if(base_arg == "nlllossbfp16")
return new NLLLossDriver<bfloat16, float>();
return nullptr;
}

REGISTER_DRIVER_MAKER(makeDriver);
5 changes: 3 additions & 2 deletions driver/driver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ inline void PadBufferSize(size_t& sz, int datatype_sz)
"pool[fp16], lrn[fp16], "
"activ[fp16], softmax[fp16], bnorm[fp16], rnn[fp16], gemm[fp16], ctc, dropout[fp16], "
"tensorop[fp16], reduce[fp16|fp64], layernorm[bfp16|fp16], sum[bfp16|fp16], "
"argmax[bfp16|fp16], groupnorm[bfp16|fp16], cat[bfp16|fp16]\n");
"argmax[bfp16|fp16], groupnorm[bfp16|fp16], cat[bfp16|fp16], nllloss[bfp16|fp16]\n");
exit(0); // NOLINT (concurrency-mt-unsafe)
}

Expand All @@ -176,7 +176,8 @@ inline std::string ParseBaseArg(int argc, char* argv[])
arg != "layernormfp16" && arg != "layernormbfp16" && arg != "sum" && arg != "sumfp16" &&
arg != "sumbfp16" && arg != "argmax" && arg != "argmaxfp16" && arg != "argmaxbfp16" &&
arg != "groupnorm" && arg != "groupnormfp16" && arg != "groupnormbfp16" && arg != "cat" &&
arg != "catfp16" && arg != "catbfp16" && arg != "--version")
arg != "catfp16" && arg != "catbfp16" && arg != "nllloss" && arg != "nlllossfp16" &&
arg != "nlllossbfp16" && arg != "--version")
{
printf("FAILED: Invalid Base Input Argument\n");
Usage();
Expand Down
74 changes: 74 additions & 0 deletions driver/mloNLLLossHost.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*******************************************************************************
*
* 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.
*
*******************************************************************************/
#ifndef MLO_NLLLOSSHOST_H_
#define MLO_NLLLOSSHOST_H_

#include <miopen/tensor.hpp>

template <typename Tgpu, typename Tcheck>
int32_t mloNLLLossForwardRunHost(miopenTensorDescriptor_t inputDesc,
Tgpu* input,
int32_t* target,
Tgpu* weight,
Tcheck* outputhost,
int32_t ignore_index)
{
auto dims = miopen::deref(inputDesc).GetLengths();

size_t N = dims[0];
size_t C = dims[1];
size_t D1 = dims[2];
size_t D2 = dims[3];

for(size_t n = 0; n < N; n++)
{
for(size_t d1 = 0; d1 < D1; d1++)
{
for(size_t d2 = 0; d2 < D2; d2++)
{
size_t target_index = n * D1 * D2 + d1 * D2 + d2;
int32_t t = target[target_index];
size_t input_index = (n * C + t) * D1 * D2 + d1 * D2 + d2;
size_t weight_index = t;
size_t output_index = target_index;

if(t < 0 || t == ignore_index || t >= C)
{
outputhost[output_index] = static_cast<Tcheck>(0);
}
else
{
outputhost[output_index] = static_cast<Tcheck>(-1) *
static_cast<Tcheck>(weight[weight_index]) *
static_cast<Tcheck>(input[input_index]);
}
}
}
}

return 0;
}
#endif // MLO_NLLLOSSHOST_H_
Loading