Skip to content

Commit 84504ad

Browse files
author
Valeriy Fedyunin
authored
Add CDataLayer (#428)
* Add CDataLayer Signed-off-by: Valeriy Fedyunin <[email protected]> * Add Data layer to the neoml module initialization Signed-off-by: Valeriy Fedyunin <[email protected]> * Fix python build Signed-off-by: Valeriy Fedyunin <[email protected]> * Remove test code Signed-off-by: Valeriy Fedyunin <[email protected]>
1 parent 69ecb08 commit 84504ad

File tree

21 files changed

+372
-5
lines changed

21 files changed

+372
-5
lines changed

NeoML/Python/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ pybind11_add_module(PythonWrapper
5252
src/PyCrfLayer.cpp
5353
src/PyCtcLayer.cpp
5454
src/PyCustomLossLayer.cpp
55+
src/PyDataLayer.cpp
5556
src/PyDifferentialEvolution.cpp
5657
src/PyDotProductLayer.cpp
5758
src/PyDropoutLayer.cpp

NeoML/Python/neoml/Dnn/Data.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
""" Copyright (c) 2017-2020 ABBYY Production LLC
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
--------------------------------------------------------------------------------------------------------------
15+
"""
16+
17+
import neoml.PythonWrapper as PythonWrapper
18+
from .Dnn import Layer
19+
import neoml.Blob as Blob
20+
21+
22+
class Data(Layer):
23+
"""The data layer that serves to pass a fixed data blob into the network.
24+
25+
:param dnn: The neural network.
26+
:type dnn: object
27+
:param name: The layer name.
28+
:type name: str, default=None
29+
30+
.. rubric:: Layer inputs:
31+
32+
The layer has no inputs.
33+
34+
.. rubric:: Layer outputs:
35+
36+
The layer has one output that contains the data blob
37+
passed into the last call of set_blob.
38+
"""
39+
def __init__(self, dnn, name=None):
40+
if type(dnn) is PythonWrapper.Data:
41+
super().__init__(dnn)
42+
return
43+
44+
internal = PythonWrapper.Data(dnn, str(name))
45+
super().__init__(internal)
46+
47+
def set_blob(self, blob):
48+
"""Sets the blob with data.
49+
"""
50+
self._internal.set_blob(blob._internal)
51+
52+
def get_blob(self):
53+
"""Gets the blob with data.
54+
"""
55+
return Blob(self._internal.get_blob())

NeoML/Python/neoml/Dnn/Source.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121

2222
class Source(Layer):
23-
"""The source layer that serves to pass a data blob into the network.
23+
"""The source layer that serves to pass a user data blob into the network.
2424
2525
:param dnn: The neural network.
2626
:type dnn: object

NeoML/Python/neoml/Dnn/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from .Conv import Conv, Conv3D, TransposedConv3D, TransposedConv, ChannelwiseConv, TimeConv
1616
from .Crf import Crf, CrfLoss, BestSequence
1717
from .Ctc import CtcLoss, CtcDecoding
18+
from .Data import Data
1819
from .DotProduct import DotProduct
1920
from .Dropout import Dropout
2021
from .Eltwise import EltwiseSum, EltwiseSub, EltwiseMul, EltwiseDiv, EltwiseNegMul, EltwiseMax

NeoML/Python/src/PyDataLayer.cpp

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/* Copyright © 2017-2021 ABBYY Production LLC
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
--------------------------------------------------------------------------------------------------------------*/
15+
16+
#include <common.h>
17+
#pragma hdrstop
18+
19+
#include "PyDataLayer.h"
20+
#include "PyDnnBlob.h"
21+
#include "PyDnn.h"
22+
#include "PyMathEngine.h"
23+
24+
class CPyDataLayer : public CPyLayer {
25+
public:
26+
CPyDataLayer( CDataLayer* layer, CPyMathEngineOwner& mathEngineOwner ) : CPyLayer( *layer, mathEngineOwner ) {}
27+
28+
void SetBlob( const CPyBlob& blob )
29+
{
30+
Layer<CDataLayer>()->SetBlob( blob.Blob() );
31+
}
32+
33+
CPyBlob GetBlob() const
34+
{
35+
return CPyBlob( MathEngineOwner(), Layer<CDataLayer>()->GetBlob() );
36+
}
37+
38+
py::object CreatePythonObject() const
39+
{
40+
py::object pyModule = py::module::import( "neoml.Dnn" );
41+
py::object pyConstructor = pyModule.attr( "Data" );
42+
return pyConstructor( py::cast(this) );
43+
}
44+
};
45+
46+
void InitializeDataLayer( py::module& m )
47+
{
48+
py::class_<CPyDataLayer, CPyLayer>(m, "Data")
49+
.def( py::init([]( const CPyLayer& layer )
50+
{
51+
return new CPyDataLayer( layer.Layer<CDataLayer>(), layer.MathEngineOwner() );
52+
}))
53+
.def( py::init([]( const CPyDnn& dnn, const std::string& name )
54+
{
55+
py::gil_scoped_release release;
56+
CPtr<CDataLayer> dataLayer( new CDataLayer( dnn.MathEngine() ) );
57+
dataLayer->SetName( FindFreeLayerName( dnn.Dnn(), "Data", name ).c_str() );
58+
dnn.Dnn().AddLayer( *dataLayer );
59+
60+
return new CPyDataLayer( dataLayer, dnn.MathEngineOwner() );
61+
}))
62+
.def( "set_blob", &CPyDataLayer::SetBlob, py::return_value_policy::reference )
63+
.def( "get_blob", &CPyDataLayer::GetBlob, py::return_value_policy::reference )
64+
;
65+
}

NeoML/Python/src/PyDataLayer.h

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* Copyright © 2017-2021 ABBYY Production LLC
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
--------------------------------------------------------------------------------------------------------------*/
15+
16+
#pragma once
17+
18+
#include "PyLayer.h"
19+
20+
void InitializeDataLayer( py::module& m );

NeoML/Python/src/PyDnn.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ REGISTER_NEOML_PYLAYER( "IndRnn", "NeoMLDnnIndRnnLayer" )
170170
REGISTER_NEOML_PYLAYER( "Qrnn", "NeoMLDnnQrnnLayer" )
171171
REGISTER_NEOML_PYLAYER( "Lrn", "NeoMLDnnLrnLayer" )
172172
REGISTER_NEOML_PYLAYER( "Cast", "NeoMLDnnCastLayer" )
173+
REGISTER_NEOML_PYLAYER( "Data", "NeoMLDnnDataLayer" )
173174

174175
}
175176

NeoML/Python/src/PyWrapper.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ limitations under the License.
3838
#include "PyCrfLayer.h"
3939
#include "PyCtcLayer.h"
4040
#include "PyCustomLossLayer.h"
41+
#include "PyDataLayer.h"
4142
#include "PyDotProductLayer.h"
4243
#include "PyDropoutLayer.h"
4344
#include "PyFullyConnectedLayer.h"
@@ -104,6 +105,7 @@ PYBIND11_MODULE(PythonWrapper, m) {
104105
InitializeConcatLayer( m );
105106
InitializeCrfLayer( m );
106107
InitializeCtcLayer( m );
108+
InitializeDataLayer( m );
107109
InitializeEltwiseLayer( m );
108110
InitializeDotProductLayer( m );
109111
InitializeDropoutLayer( m );
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# CDataLayer Class
2+
3+
<!-- TOC -->
4+
5+
- [CDataLayer Class](#cdatalayer-class)
6+
- [Settings](#settings)
7+
- [Trainable parameters](#trainable-parameters)
8+
- [Inputs](#inputs)
9+
- [Outputs](#outputs)
10+
11+
<!-- /TOC -->
12+
13+
This class implements a layer that serves to input a blob of fixed data into the neural network.
14+
15+
## Settings
16+
17+
```c++
18+
void SetBlob( CDnnBlob* blob );
19+
```
20+
21+
Sets the blob with source data. It may be of arbitrary size and contain any kind of data.
22+
23+
## Trainable parameters
24+
25+
There are no trainable parameters for this layer.
26+
27+
## Inputs
28+
29+
The layer has no inputs.
30+
31+
## Outputs
32+
33+
The single output contains the data blob that was passed into the last call of `SetBlob()`.

NeoML/docs/en/API/NN/IOLayers/SourceLayer.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<!-- /TOC -->
1212

13-
This class implements a layer that serves to input a blob of data into the neural network.
13+
This class implements a layer that serves to input a blob of user data into the neural network.
1414

1515
## Settings
1616

NeoML/docs/en/API/NN/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,11 @@ delete gpuMathEngine;
202202

203203
- [CBaseLayer](BaseLayer.md) is the base class for common layer functionality
204204
- The layers used to pass the data to and from the network:
205-
- [CSourceLayer](IOLayers/SourceLayer.md) transmits a blob of data into the network
205+
- [CSourceLayer](IOLayers/SourceLayer.md) transmits a blob of user data into the network
206206
- [CSinkLayer](IOLayers/SinkLayer.md) is used to retrieve a blob of data with the network response
207207
- [CProblemSourceLayer](IOLayers/ProblemSourceLayer.md) transmits the data from [`IProblem`](../ClassificationAndRegression/Problems.md) into the network
208208
- [CFullyConnectedSourceLayer](IOLayers/FullyConnectedSourceLayer.md) transmits the data from `IProblem` into the network, multiplying the vectors by a trainable weights matrix
209+
- [CDataLayer](IOLayers/DataLayer.md) transmits a blob of fixed data into the network
209210
- [CFullyConnectedLayer](FullyConnectedLayer.md) is the fully-connected layer
210211
- [Activation functions](ActivationLayers/README.md):
211212
- [CLinearLayer](ActivationLayers/LinearLayer.md) - a linear activation function `ax + b`
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Класс CDataLayer
2+
3+
<!-- TOC -->
4+
5+
- [Класс CDataLayer](#класс-cdatalayer)
6+
- [Настройки](#настройки)
7+
- [Обучаемые параметры](#обучаемые-параметры)
8+
- [Входы](#входы)
9+
- [Выходы](#выходы)
10+
11+
<!-- /TOC -->
12+
13+
Класс реализует слой, передающий блоб с фиксированными данными в сеть.
14+
15+
## Настройки
16+
17+
```c++
18+
void SetBlob( CDnnBlob* blob );
19+
```
20+
21+
Установка блоба с данными, который будет передан в сеть. Блоб может иметь любые размеры и содержать данные любого типа.
22+
23+
## Обучаемые параметры
24+
25+
Слой не имеет обучаемых параметров.
26+
27+
## Входы
28+
29+
Слой не имеет входов.
30+
31+
## Выходы
32+
33+
Единственный выход содержит блоб, который был передан при последнем вызове `SetBlob()`.

NeoML/docs/ru/API/NN/IOLayers/SourceLayer.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<!-- /TOC -->
1212

13-
Класс реализует слой, передающий блоб с данными в сеть.
13+
Класс реализует слой, передающий блоб с пользовательскими данными в сеть.
1414

1515
## Настройки
1616

NeoML/docs/ru/API/NN/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,11 @@ delete gpuMathEngine;
200200

201201
- [CBaseLayer](BaseLayer.md) - базовый класс
202202
- Слои для обмена данными с сетью:
203-
- [CSourceLayer](IOLayers/SourceLayer.md) - передача блобов с данными в сеть
203+
- [CSourceLayer](IOLayers/SourceLayer.md) - передача блобов с пользовательскими данными в сеть
204204
- [CSinkLayer](IOLayers/SinkLayer.md) - получение блобов с данными из сети
205205
- [CProblemSourceLayer](IOLayers/ProblemSourceLayer.md) - передача данных из [`IProblem`](../ClassificationAndRegression/Problems.md) в сеть
206206
- [CFullyConnectedSourceLayer](IOLayers/FullyConnectedSourceLayer.md) - передача данных из `IProblem` в сеть и домножение их на матрицу
207+
- [CDataLayer](IOLayers/DataLayer.md) - передача блобов с фиксированными данными в сеть
207208
- [CFullyConnectedLayer](FullyConnectedLayer.md) - полносвязный слой
208209
- [Функции активации](ActivationLayers/README.md):
209210
- [CLinearLayer](ActivationLayers/LinearLayer.md) - функция активации вида `ax + b`
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/* Copyright © 2017-2020 ABBYY Production LLC
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
--------------------------------------------------------------------------------------------------------------*/
15+
16+
#pragma once
17+
18+
#include <NeoML/NeoMLDefs.h>
19+
#include <NeoML/Dnn/Dnn.h>
20+
21+
namespace NeoML {
22+
23+
// The layer that pases data blob to the network
24+
// Unlike CSourceLayer this layer isn't interpreted as a user-provided data
25+
// The source layer that passes a data blob into the network. Can have exactly one output
26+
class NEOML_API CDataLayer : public CBaseLayer {
27+
NEOML_DNN_LAYER( CDataLayer )
28+
public:
29+
explicit CDataLayer( IMathEngine& mathEngine ) : CBaseLayer( mathEngine, "CDataLayer", false ) {}
30+
31+
// Sets the input data blob
32+
void SetBlob( CDnnBlob* blob );
33+
// Gets the reference to the input blob
34+
const CPtr<CDnnBlob>& GetBlob() const { return blob; }
35+
36+
void Serialize( CArchive& archive ) override;
37+
38+
protected:
39+
CPtr<CDnnBlob> blob;
40+
// CBaseLayer class methods
41+
void Reshape() override;
42+
void RunOnce() override;
43+
void BackwardOnce() override;
44+
void AllocateOutputBlobs() override;
45+
};
46+
47+
// Creates CDataLayer with the given name
48+
NEOML_API CDataLayer* Data( CDnn& network, const char* name );
49+
50+
} // namespace NeoML

NeoML/include/NeoML/NeoML.h

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ limitations under the License.
119119
#include <NeoML/Dnn/Layers/SpaceToDepthLayer.h>
120120
#include <NeoML/Dnn/Layers/LrnLayer.h>
121121
#include <NeoML/Dnn/Layers/CastLayer.h>
122+
#include <NeoML/Dnn/Layers/DataLayer.h>
122123
#include <NeoML/ArchiveFile.h>
123124

124125
#ifndef NO_NEOML_NAMESPACE

NeoML/src/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ target_sources( ${PROJECT_NAME} PRIVATE
4343
Dnn/Layers/CrfLayer.cpp
4444
Dnn/Layers/CrossEntropyLossLayer.cpp
4545
Dnn/Layers/CtcLayer.cpp
46+
Dnn/Layers/DataLayer.cpp
4647
Dnn/Layers/DepthToSpaceLayer.cpp
4748
Dnn/Layers/DotProductLayer.cpp
4849
Dnn/Layers/DropoutLayer.cpp
@@ -203,6 +204,7 @@ target_sources( ${PROJECT_NAME} PRIVATE
203204
../include/NeoML/Dnn/Layers/ConvLayer.h
204205
../include/NeoML/Dnn/Layers/CrfLayer.h
205206
../include/NeoML/Dnn/Layers/CtcLayer.h
207+
../include/NeoML/Dnn/Layers/DataLayer.h
206208
../include/NeoML/Dnn/Layers/DepthToSpaceLayer.h
207209
../include/NeoML/Dnn/Layers/DotProductLayer.h
208210
../include/NeoML/Dnn/Layers/DropoutLayer.h

NeoML/src/Dnn/Dnn.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ limitations under the License.
8787
#include <NeoML/Dnn/Layers/SpaceToDepthLayer.h>
8888
#include <NeoML/Dnn/Layers/LrnLayer.h>
8989
#include <NeoML/Dnn/Layers/CastLayer.h>
90+
#include <NeoML/Dnn/Layers/DataLayer.h>
9091

9192
namespace NeoML {
9293

@@ -334,6 +335,7 @@ REGISTER_NEOML_LAYER( CDepthToSpaceLayer, "NeoMLDnnDepthToSpaceLayer" )
334335
REGISTER_NEOML_LAYER( CSpaceToDepthLayer, "NeoMLDnnSpaceToDepthLayer" )
335336
REGISTER_NEOML_LAYER( CLrnLayer, "NeoMLDnnLrnLayer" )
336337
REGISTER_NEOML_LAYER( CCastLayer, "NeoMLDnnCastLayer" )
338+
REGISTER_NEOML_LAYER( CDataLayer, "NeoMLDnnDataLayer" )
337339

338340
}
339341

0 commit comments

Comments
 (0)