-
Notifications
You must be signed in to change notification settings - Fork 75
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
[Wait for 2759][GPU/OpenCL] Initial version of LM Head layer with OpenCl ops and updated Addition Layer with latest pipeline changes. @open sesame 12/02 16:21 #2752
Open
yashSingh0723
wants to merge
4
commits into
nnstreamer:main
Choose a base branch
from
yashSingh0723:opencl_lmhead_layer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+697
−1
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
282d797
[GPU/OpenCL/Update] Initial version of LM Head layer with OpencCl ops…
yashSingh0723 5a55098
[Trivial] Clang format fixed and Rebased the PR
yashSingh0723 0c9e7aa
[Update] Removed SmartReply property and added nnlayergolden files
yashSingh0723 d08a0ec
[Update] Backwarding support set to false
yashSingh0723 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
* @see https://github.com/nnstreamer/nntrainer | ||
* @author Debadri Samaddar <[email protected]> | ||
* @author Niket Agarwal <[email protected]> | ||
* @author Yash Singh <[email protected]> | ||
* @author Thummala Pallavi <[email protected]> | ||
* @bug No known bugs except for NYI items | ||
* @brief This file contains app context related functions and classes that | ||
|
@@ -20,6 +21,7 @@ | |
#include <cl_context.h> | ||
#include <concat_cl.h> | ||
#include <fc_layer_cl.h> | ||
#include <lm_head_layer_cl.h> | ||
#include <reshape_cl.h> | ||
#include <rmsnorm_layer_cl.h> | ||
#include <swiglu_cl.h> | ||
|
@@ -66,6 +68,10 @@ static void add_default_object(ClContext &cc) { | |
cc.registerFactory(nntrainer::createLayer<TransposeLayerCl>, | ||
TransposeLayerCl::type, | ||
ml::train::LayerType::LAYER_TRANSPOSE); | ||
|
||
cc.registerFactory(nntrainer::createLayer<CustomLMHeadLayerCl>, | ||
CustomLMHeadLayerCl::type, | ||
ml::train::LayerType::LAYER_LM_HEAD); | ||
} | ||
|
||
static void registerer(ClContext &cc) noexcept { | ||
|
EunjuYang marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
/** | ||
* Copyright (C) 2024 Hyeonseok Lee <[email protected]> | ||
* Copyright (C) 2024 Yash Singh <[email protected]> | ||
* | ||
* @file custom_vocab_selection.cpp | ||
* @date 1 Oct 2024 | ||
* @see https://github.com/nnstreamer/nntrainer | ||
* @author Yash Singh <[email protected]> | ||
* @bug No known bugs except for NYI items | ||
* @brief Implementation of custom vocab selection | ||
*/ | ||
|
||
#include <algorithm> | ||
#include <custom_vocab_selection.h> | ||
|
||
nntrainer::VocabSelection::VocabSelection(LshType lshType, int lshChoices, | ||
int hiddenSize, int vocabCnt) : | ||
lshType(lshType), | ||
lshChoices(lshChoices), | ||
vocabCnt(vocabCnt), | ||
hiddenSize(hiddenSize), | ||
lshBlockNum(0), | ||
lshBits(0) {} | ||
|
||
nntrainer::VocabSelection::~VocabSelection() {} | ||
|
||
nntrainer::VocabSelectionNNTrainer::VocabSelectionNNTrainer( | ||
LshType lshType, int lshChoices, int hiddenSize, int vocabCnt, | ||
nntrainer::Tensor &weights) : | ||
VocabSelection(lshType, lshChoices, hiddenSize, vocabCnt) { | ||
this->lshBlockNum = (hiddenSize + lshBlockSize - 1) / lshBlockSize; | ||
this->lshBits = lshBlockNum * lshBlockSize; | ||
this->lshData = std::vector<lshDataBlock>(this->vocabCnt * lshBlockNum); | ||
|
||
for (unsigned int i = 0; i < lshBlockNum; ++i) { | ||
unsigned int actualSize = | ||
std::min(lshBlockSize, hiddenSize - (int)i * lshBlockSize); | ||
for (unsigned int j = 0; j < vocabCnt; ++j) { | ||
lshDataBlock d; | ||
for (unsigned int k = 0; k < actualSize; ++k) { | ||
if (weights.getDataType() == nntrainer::TensorDim::DataType::FP32) { | ||
d[k] = weights.getValue(0, 0, i * lshBlockSize + k, j) > 0 ? 1 : 0; | ||
} else if (weights.getDataType() == | ||
nntrainer::TensorDim::DataType::FP16) { | ||
d[k] = | ||
weights.getValue<_FP16>(0, 0, i * lshBlockSize + k, j) > 0 ? 1 : 0; | ||
} | ||
} | ||
for (unsigned int k = actualSize; k < lshBlockSize; ++k) { | ||
d[k] = 0; | ||
} | ||
this->lshData[j * lshBlockNum + i] = d; | ||
} | ||
} | ||
} | ||
|
||
std::vector<std::vector<int>> | ||
nntrainer::VocabSelectionNNTrainer::getVocabs(const nntrainer::Tensor &input) { | ||
unsigned int batchSize = input.height(); | ||
|
||
std::vector<std::vector<int>> res = std::vector<std::vector<int>>(batchSize); | ||
for (int i = 0; i < batchSize; i++) { | ||
std::vector<lshDataBlock> d(lshBlockNum); | ||
for (int k = 0; k < lshBlockNum; k++) { | ||
int actualSize = std::min(lshBlockSize, hiddenSize - k * lshBlockSize); | ||
for (int j = 0; j < actualSize; j++) { | ||
if (input.getDataType() == nntrainer::TensorDim::DataType::FP32) { | ||
d[k][j] = input.getValue(0, 0, i, j + k * lshBlockSize) >= 0 ? 1 : 0; | ||
} else if (input.getDataType() == | ||
nntrainer::TensorDim::DataType::FP16) { | ||
d[k][j] = | ||
input.getValue<_FP16>(0, 0, i, j + k * lshBlockSize) >= 0 ? 1 : 0; | ||
} | ||
} | ||
for (int j = actualSize; j < lshBlockSize; j++) { | ||
d[k][j] = 0; | ||
} | ||
} | ||
std::vector<int> simResult(vocabCnt, 0); | ||
std::vector<int> simCount(lshBits + 1, 0); | ||
for (int j = 0; j < vocabCnt; j++) { | ||
for (int k = 0; k < lshBlockNum; k++) { | ||
simResult[j] += (d[k] ^ lshData[j * lshBlockNum + k]).count(); | ||
} | ||
simCount[simResult[j]]++; | ||
} | ||
int cut = lshBits + 1; | ||
int leftover = 0; | ||
int countSum = 0; | ||
for (int j = 0; j <= lshBits; j++) { | ||
countSum += simCount[j]; | ||
if (countSum > lshChoices) { | ||
cut = j; | ||
leftover = simCount[j] - (countSum - lshChoices); | ||
break; | ||
} | ||
} | ||
std::vector<int> selectedVocabs(lshChoices); | ||
int pos = 0; | ||
for (int j = 0; j < vocabCnt; j++) { | ||
if (simResult[j] <= cut) { | ||
if (simResult[j] < cut) { | ||
selectedVocabs[pos] = j; | ||
pos++; | ||
} else if (leftover > 0) { | ||
selectedVocabs[pos] = j; | ||
pos++; | ||
leftover--; | ||
} | ||
} | ||
} | ||
res[i] = selectedVocabs; | ||
} | ||
return res; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
/** | ||
* Copyright (C) 2024 Hyeonseok Lee <[email protected]> | ||
* Copyright (C) 2024 Yash Singh <[email protected]> | ||
* | ||
* @file custom_vocab_selection.h | ||
* @date 1 Oct 2024 | ||
* @see https://github.com/nnstreamer/nntrainer | ||
* @author Yash Singh <[email protected]> | ||
* @bug No known bugs except for NYI items | ||
* @brief Implementation of custom vocab selection | ||
*/ | ||
|
||
#ifndef VOCAB_SELECTION_H | ||
#define VOCAB_SELECTION_H | ||
|
||
#include <tensor.h> | ||
|
||
#ifndef LSH_BLOCK_SIZE | ||
#define LSH_BLOCK_SIZE 256 | ||
#endif | ||
|
||
using namespace std; | ||
|
||
namespace nntrainer { | ||
|
||
/** | ||
* @brief Enumeration for different types of LSH algorithms used in vocab | ||
* selection | ||
* | ||
*/ | ||
enum LshType { NONE = 0, SIMHASH = 1, ORTHOSIMHASH = 2 }; | ||
typedef std::bitset<LSH_BLOCK_SIZE> lshDataBlock; | ||
|
||
/** | ||
* @brief Vocab Selection class to select the vocabs from model output using LSH | ||
* | ||
*/ | ||
class VocabSelection { | ||
protected: | ||
int hiddenSize; | ||
int vocabCnt; | ||
const int lshBlockSize = LSH_BLOCK_SIZE; | ||
int lshBlockNum; | ||
int lshBits; // lshBlockSize * lshBlockNum | ||
int lshChoices; | ||
LshType lshType; | ||
std::vector<lshDataBlock> lshData; | ||
|
||
public: | ||
/** | ||
* @brief Constructor of VocabSelection class | ||
* | ||
*/ | ||
VocabSelection(LshType lshType, int lshChoices, int hiddenSize, int vocabCnt); | ||
virtual std:: | ||
vector<std::vector<int>> | ||
|
||
/** | ||
* @brief Get the Vocabs object | ||
*/ | ||
getVocabs(const nntrainer::Tensor &modelOutput) = 0; | ||
|
||
/** | ||
* @brief Destructor of VocabSelection class | ||
*/ | ||
~VocabSelection(); | ||
}; | ||
|
||
/** | ||
* @brief Vocab Selection NNTrainer class to select the vocabs from model output | ||
* using LSH | ||
* | ||
*/ | ||
class VocabSelectionNNTrainer : public VocabSelection { | ||
protected: | ||
nntrainer::Tensor lshWeight; | ||
|
||
public: | ||
/** | ||
* @brief Constructor of VocabSelectionNNTrainer class | ||
*/ | ||
VocabSelectionNNTrainer(LshType lshType, int lshChoices, int hiddenSize, | ||
int vocabCnt, nntrainer::Tensor &weights); | ||
virtual std:: | ||
vector<std::vector<int>> | ||
|
||
/** | ||
* @brief Get the Vocabs object | ||
* | ||
*/ | ||
getVocabs(const nntrainer::Tensor &modelOutput); | ||
|
||
/** | ||
* @brief Destructor of VocabSelectionNNTrainer class | ||
*/ | ||
~VocabSelectionNNTrainer(){}; | ||
}; | ||
|
||
} // namespace nntrainer | ||
|
||
#endif |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a big deal, but I think setting the LAYER_UNKNOWN at the end would be better.