Skip to content

Commit 49dc07c

Browse files
committed
c++11
Signed-off-by: voropz <[email protected]>
1 parent 9cd831e commit 49dc07c

File tree

3 files changed

+38
-36
lines changed

3 files changed

+38
-36
lines changed

NeoML/Python/neoml/Dnn/Split.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def __init__(self, input_layer, sizes, name=None):
8686
# ----------------------------------------------------------------------------------------------------------------------
8787

8888

89-
class SplitDepth(Layer):
89+
class SplitDepth(SplitLayer):
9090
"""The layer that splits an input blob along the Depth dimension.
9191
9292
:param input_layer: The input layer and the number of its output. If no number
@@ -120,7 +120,7 @@ def __init__(self, input_layer, sizes, name=None):
120120
# ----------------------------------------------------------------------------------------------------------------------
121121

122122

123-
class SplitWidth(Layer):
123+
class SplitWidth(SplitLayer):
124124
"""The layer that splits an input blob along the Width dimension.
125125
126126
:param input_layer: The input layer and the number of its output. If no number
@@ -154,7 +154,7 @@ def __init__(self, input_layer, sizes, name=None):
154154
# ----------------------------------------------------------------------------------------------------------------------
155155

156156

157-
class SplitHeight(Layer):
157+
class SplitHeight(SplitLayer):
158158
"""The layer that splits an input blob along the Height dimension.
159159
160160
:param input_layer: The input layer and the number of its output. If no number
@@ -188,7 +188,7 @@ def __init__(self, input_layer, sizes, name=None):
188188
# ----------------------------------------------------------------------------------------------------------------------
189189

190190

191-
class SplitListSize(Layer):
191+
class SplitListSize(SplitLayer):
192192
"""The layer that splits an input blob along the ListSize dimension.
193193
194194
:param input_layer: The input layer and the number of its output. If no number
@@ -222,7 +222,7 @@ def __init__(self, input_layer, sizes, name=None):
222222
# ----------------------------------------------------------------------------------------------------------------------
223223

224224

225-
class SplitBatchWidth(Layer):
225+
class SplitBatchWidth(SplitLayer):
226226
"""The layer that splits an input blob along the BatchWidth dimension.
227227
228228
:param input_layer: The input layer and the number of its output. If no number
@@ -256,7 +256,7 @@ def __init__(self, input_layer, sizes, name=None):
256256
# ----------------------------------------------------------------------------------------------------------------------
257257

258258

259-
class SplitBatchLength(Layer):
259+
class SplitBatchLength(SplitLayer):
260260
"""The layer that splits an input blob along the BatchLength dimension.
261261
262262
:param input_layer: The input layer and the number of its output. If no number

NeoML/Python/src/PySplitLayer.cpp

+28-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,31 @@ limitations under the License.
1818

1919
#include "PySplitLayer.h"
2020

21+
py::array CPyBaseSplitLayer::GetOutputCounts() const {
22+
const auto& fineCounts = Layer<CBaseSplitLayer>()->GetOutputCounts();
23+
24+
py::array_t<int, py::array::c_style> counts( fineCounts.Size() );
25+
auto countsData = counts.mutable_unchecked<>();
26+
27+
for( int i = 0; i < fineCounts.Size(); ++i ) {
28+
countsData( i ) = fineCounts[i];
29+
}
30+
31+
return counts;
32+
}
33+
34+
void CPyBaseSplitLayer::SetOutputCounts( py::array counts ) {
35+
NeoAssert( counts.ndim() == 1 );
36+
NeoAssert( counts.dtype().is( py::dtype::of<int>() ) );
37+
38+
CArray<int> fineCounts;
39+
fineCounts.SetSize( static_cast<int>(counts.size()) );
40+
for( int i = 0; i < fineCounts.Size(); i++ ) {
41+
fineCounts[i] = static_cast<const int*>(counts.data())[i];
42+
}
43+
Layer<CBaseSplitLayer>()->SetOutputCounts( fineCounts );
44+
}
45+
2146
class CPySplitChannelsLayer : public CPyBaseSplitLayer {
2247
public:
2348
explicit CPySplitChannelsLayer( CSplitChannelsLayer& layer, CPyMathEngineOwner& mathEngineOwner ) : CPyBaseSplitLayer( layer, mathEngineOwner ) {}
@@ -105,16 +130,16 @@ class CPySplitBatchLengthLayer : public CPyBaseSplitLayer {
105130
template <class Layer, class PythonLayer>
106131
PythonLayer* InitSplit( const std::string& className, const std::string& name, const CPyLayer& layer1, int outputNumber1, py::array sizes )
107132
{
108-
static_assert( std::is_base_of_v<CPyBaseSplitLayer, PythonLayer>, "PySplitLayer.cpp, InitSplit" );
109-
static_assert( std::is_constructible_v<PythonLayer, Layer&, CPyMathEngineOwner&>, "PySplitLayer.cpp, InitSplit" );
133+
static_assert( std::is_base_of<CPyBaseSplitLayer, PythonLayer>::value, "PySplitLayer.cpp, InitSplit" );
134+
static_assert( std::is_constructible<PythonLayer, Layer&, CPyMathEngineOwner&>::value, "PySplitLayer.cpp, InitSplit" );
110135

111136
py::gil_scoped_release release;
112137
CDnn& dnn = layer1.Dnn();
113138
CPtr<Layer> split = new Layer( dnn.GetMathEngine() );
114139
split->SetName( FindFreeLayerName( dnn, className, name ).c_str() );
115140
dnn.AddLayer( *split );
116141
split->Connect( 0, layer1.BaseLayer(), outputNumber1 );
117-
auto layer = std::make_unique<PythonLayer>( *split, layer1.MathEngineOwner() );
142+
std::unique_ptr<PythonLayer> layer( new PythonLayer( *split, layer1.MathEngineOwner() ) );
118143
layer->SetOutputCounts( sizes );
119144
return layer.release();
120145
}

NeoML/Python/src/PySplitLayer.h

+4-27
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,9 @@ class CPyBaseSplitLayer : public CPyLayer {
2121
public:
2222
CPyBaseSplitLayer( CBaseSplitLayer& layer, CPyMathEngineOwner& mathEngineOwner ) : CPyLayer( layer, mathEngineOwner ) {}
2323

24-
py::array GetOutputCounts() const
25-
{
26-
const auto& fineCounts = Layer<CBaseSplitLayer>()->GetOutputCounts();
27-
28-
py::array_t<int, py::array::c_style> counts( fineCounts.Size() );
29-
auto countsData = counts.mutable_unchecked<>();
30-
31-
for( int i = 0; i < fineCounts.Size(); ++i ) {
32-
countsData( i ) = fineCounts[i];
33-
}
34-
35-
return counts;
36-
}
37-
38-
void SetOutputCounts( py::array counts )
39-
{
40-
NeoAssert( counts.ndim() == 1 );
41-
NeoAssert( counts.dtype().is( py::dtype::of<int>() ) );
42-
43-
CArray<int> fineCounts;
44-
fineCounts.SetSize( static_cast<int>(counts.size()) );
45-
for( int i = 0; i < fineCounts.Size(); i++ ) {
46-
fineCounts[i] = static_cast<const int*>(counts.data())[i];
47-
}
48-
Layer<CBaseSplitLayer>()->SetOutputCounts( fineCounts );
49-
}
24+
py::array GetOutputCounts() const;
25+
26+
void SetOutputCounts( py::array counts );
5027
};
5128

52-
void InitializeSplitLayer( py::module& m );
29+
void InitializeSplitLayer( py::module& m );

0 commit comments

Comments
 (0)