-
Notifications
You must be signed in to change notification settings - Fork 126
[NeoML] CDnnHeadAdapterLayer #1058
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
Draft
daniyalaliev
wants to merge
8
commits into
neoml-lib:master
Choose a base branch
from
daniyalaliev:headAdapter
base: master
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.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
38918e7
[CudaMathEngine] CMap of CString instead CBaseLayer* for better debug…
favorart 09a34c7
[NeoML] Head adapter layer
daniyalaliev 0abcd64
[NeoML] CDnnHeadAdapterLayer iOS compile fix
favorart b1d3e27
[NeoML] DnnHeadAdapterLayerSerialization
favorart b9f06c0
[NeoML] CDnnHeadAdapterLayer no changes of isLearningEnabled
favorart a97ec93
[NeoMLTest] Validate tests for DnnHeadAdapter
favorart f0a4d4f
[NeoML] DnnHeadAdapterLayer Fix formatting
favorart 9644140
[NeoML] CDnnHeadAdapterLayer macos temp fix
favorart 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 hidden or 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 hidden or 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 hidden or 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,100 @@ | ||
/* Copyright © 2024 ABBYY | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--------------------------------------------------------------------------------------------------------------*/ | ||
|
||
#pragma once | ||
|
||
#include <initializer_list> | ||
#include <NeoML/NeoML.h> | ||
#include <NeoML/Dnn/Dnn.h> | ||
#include <NeoML/Dnn/Layers/CompositeLayer.h> | ||
|
||
namespace NeoML { | ||
|
||
template <typename T> | ||
class CLayerWrapper; | ||
class CDnnHeadAdapterLayer; | ||
class CGraph; | ||
|
||
namespace optimization { | ||
int OptimizeDnnHeadAdapters( NeoML::CGraph& ); | ||
} | ||
|
||
class CDnnHead : public IObject { | ||
public: | ||
CDnnHead() = default; | ||
|
||
template <typename... Ts> | ||
CDnnHead(CRandom& random, IMathEngine& mathEngine, CLayerWrapper<Ts>... linearWrappers) | ||
{ | ||
CDnn* head(new CDnn(random, mathEngine)); | ||
|
||
CPtr<CCompositeSourceLayer> source = new CCompositeSourceLayer(head->GetMathEngine()); | ||
source->SetName("source"); | ||
head->AddLayer(*source); | ||
CBaseLayer* inputLayer = source; | ||
|
||
// chain connect wrapped layers | ||
using TExpanding = CBaseLayer * []; | ||
TExpanding{ inputLayer = linearWrappers(inputLayer)... }; | ||
|
||
CPtr<CCompositeSinkLayer> headSink = new CCompositeSinkLayer(head->GetMathEngine()); | ||
headSink->SetName("sink"); | ||
head->AddLayer(*headSink); | ||
headSink->Connect(0, *(inputLayer)); | ||
dnn = head; | ||
} | ||
|
||
CDnn& GetDnn() { return *dnn; } | ||
|
||
private: | ||
~CDnnHead() override | ||
{ | ||
if( dnn != nullptr ) { | ||
delete dnn; | ||
dnn = nullptr; | ||
} | ||
} | ||
|
||
void increment() | ||
{ | ||
if( ++headCounter == connections.Size() ) { | ||
headCounter = 0; | ||
firstAdapterNum = -1; | ||
} | ||
} | ||
|
||
CDnn* dnn = nullptr; | ||
|
||
// Stores all adapter using this head | ||
CObjectArray<CDnnHeadAdapterLayer> connections; | ||
// Layers for which input/output blobs are stored for Backward/Learn | ||
CArray<CBaseLayer*> inputLayers; | ||
CArray<CBaseLayer*> outputLayers; | ||
// Pointers to source/sink layers of inner network | ||
CCompositeSourceLayer* sourceLayer = nullptr; | ||
CCompositeSinkLayer* sinkLayer = nullptr; | ||
// Which of the blobs will be used during backward | ||
int blobsForBackward = 0; | ||
// Which of the blobs will be used during learn | ||
int blobsForLearn = 0; | ||
|
||
int headCounter = 0; | ||
int firstAdapterNum = -1; | ||
|
||
friend class CDnnHeadAdapterLayer; | ||
friend int optimization::OptimizeDnnHeadAdapters( CGraph& ); | ||
}; | ||
|
||
} // namespace NeoML |
This file contains hidden or 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 hidden or 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 hidden or 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,83 @@ | ||
/* Copyright © 2024 ABBYY | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--------------------------------------------------------------------------------------------------------------*/ | ||
|
||
#pragma once | ||
|
||
#include <memory> | ||
#include <NeoML/NeoMLDefs.h> | ||
#include <NeoML/Dnn/Dnn.h> | ||
#include <NeoML/Dnn/DnnHead.h> | ||
|
||
namespace NeoML { | ||
|
||
// CDnnHeadAdapterLayer passes data blobs between multiple external layers and a shared internal DNN (head) | ||
// Unlike CompositeLayer, it allows to connect several external layers to same head | ||
class NEOML_API CDnnHeadAdapterLayer final : public CBaseLayer { | ||
NEOML_DNN_LAYER( CDnnHeadAdapterLayer ) | ||
public: | ||
explicit CDnnHeadAdapterLayer( IMathEngine& mathEngine, const char* name = nullptr ) | ||
: CBaseLayer( mathEngine, name == nullptr ? "CDnnHeadAdapterLayer" : name, /*isLearnable*/true ) | ||
{} | ||
|
||
void Serialize( CArchive& archive ) override; | ||
|
||
// Internal shared Dnn between DnnHeadAdapters | ||
void SetDnnHead( CPtr<CDnnHead> head ); | ||
|
||
// Get Dnn head | ||
const CDnnHead* GetDnnHead() const { return head; }; | ||
CDnnHead* GetDnnHead() { return head; }; | ||
|
||
protected: | ||
void Reshape() override; | ||
void RunOnce() override; | ||
void BackwardOnce() override; | ||
void LearnOnce() override; | ||
// It does not allocate outputBlobs in CBaseLayer in runOnce, because they are not used for inference. | ||
// The outputBlob for CDnnHeadAdapterLayer are sinkLayer->GetBlob() of its internalDnn. | ||
void AllocateOutputBlobs() override {} | ||
int BlobsForBackward() const override { return head->blobsForBackward; } | ||
int BlobsForLearn() const override { return head->blobsForLearn; } | ||
|
||
private: | ||
// Pointer to HeadLayer with inner dnn | ||
CPtr<CDnnHead> head = nullptr; | ||
// Save first adapter name to connect to necessary head in serialization | ||
CString firstAdapter; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if the first adapter will be removed from the dnn? |
||
// Stores the number of the layer connected to the internal network | ||
int num = -1; | ||
// Temporarily used to store layers during serialization | ||
CObjectArray<CBaseLayer> layers; | ||
// Stores the input/output blobs from last Inference | ||
CObjectArray<CDnnBlob> innerInputBlobs; | ||
CObjectArray<CDnnBlob> innerOutputBlobs; | ||
|
||
void OnDnnChanged( CDnn* ) override; | ||
void processBackwardOrLearn(); | ||
void configureAdapter(); | ||
void configureFromHead(); | ||
void saveBlobs(); | ||
void loadBlobs(); | ||
void configureForBackwardAndLearn(); | ||
}; | ||
|
||
inline NEOML_API CLayerWrapper<CDnnHeadAdapterLayer> DnnHeadAdapter( CDnnHead* head ) | ||
{ | ||
return CLayerWrapper<CDnnHeadAdapterLayer>( "DnnHeadAdapter", [=]( CDnnHeadAdapterLayer* result ) { | ||
result->SetDnnHead( head ); | ||
} ); | ||
} | ||
|
||
} // namespace NeoML |
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.
Uh oh!
There was an error while loading. Please reload this page.