Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
N
ngraph
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
ngraph
Commits
676e601d
Commit
676e601d
authored
7 years ago
by
Adam Procter
Browse files
Options
Browse Files
Download
Plain Diff
Merge remote-tracking branch 'origin' into aprocter/de-eigenize-partial
parents
1c034cf5
d4153c91
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
167 additions
and
5 deletions
+167
-5
CMakeLists.txt
src/ngraph/CMakeLists.txt
+1
-0
numpy_transpose.cpp
src/ngraph/builder/numpy_transpose.cpp
+77
-0
numpy_transpose.hpp
src/ngraph/builder/numpy_transpose.hpp
+55
-0
ngraph.hpp
src/ngraph/ngraph.hpp
+1
-0
CMakeLists.txt
test/CMakeLists.txt
+1
-1
builder.cpp
test/builder.cpp
+32
-4
No files found.
src/ngraph/CMakeLists.txt
View file @
676e601d
...
...
@@ -14,6 +14,7 @@
set
(
SRC
autodiff/adjoints.cpp
builder/autobroadcast.cpp
builder/numpy_transpose.cpp
builder/reduce_ops.cpp
coordinate_iterator.cpp
descriptor/input.cpp
...
...
This diff is collapsed.
Click to expand it.
src/ngraph/builder/numpy_transpose.cpp
0 → 100644
View file @
676e601d
/*
Copyright 2017 Nervana Systems Inc.
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.
*/
#include "ngraph/builder/numpy_transpose.hpp"
#include <sstream>
#include "ngraph/except.hpp"
#include "ngraph/ops/reshape.hpp"
namespace
ngraph
{
void
numpy_transpose_error
(
const
AxisVector
&
order
,
const
Shape
&
in_shape
)
{
std
::
ostringstream
os
;
os
<<
"The axes order "
;
os
<<
"[ "
<<
ngraph
::
join
(
order
)
<<
" ]"
;
os
<<
" is incompatible with the input shape "
;
os
<<
"[ "
<<
ngraph
::
join
(
in_shape
)
<<
" ]"
;
os
<<
" during numpy_transpose."
;
throw
ngraph_error
(
os
.
str
());
}
namespace
builder
{
std
::
shared_ptr
<
Node
>
numpy_transpose
(
const
std
::
shared_ptr
<
Node
>&
node
,
AxisVector
order
)
{
auto
in_shape
=
node
->
get_shape
();
// default, reverse the order of the axes
if
(
order
.
size
()
==
0
)
{
auto
n
=
in_shape
.
size
();
order
=
AxisVector
(
n
);
std
::
generate
(
order
.
begin
(),
order
.
end
(),
[
&
n
]()
{
return
--
n
;
});
}
else
if
(
order
.
size
()
==
in_shape
.
size
())
{
// validate that the axes order is valid, i.e., unique and the right size
std
::
unordered_set
<
ngraph
::
AxisVector
::
value_type
>
axes
;
for
(
auto
o
:
order
)
{
if
(
o
>=
0
&&
o
<
in_shape
.
size
()
&&
!
axes
.
count
(
o
))
{
axes
.
insert
(
o
);
}
else
{
numpy_transpose_error
(
order
,
in_shape
);
}
}
}
else
{
numpy_transpose_error
(
order
,
in_shape
);
}
// create output shape
Shape
out_shape
;
for
(
size_t
i
=
0
;
i
<
in_shape
.
size
();
++
i
)
out_shape
.
push_back
(
in_shape
[
order
[
i
]]);
// do the reshaping with the order
return
std
::
make_shared
<
ngraph
::
op
::
Reshape
>
(
node
,
order
,
out_shape
);
}
}
// namespace builder
}
// namespace ngraph
This diff is collapsed.
Click to expand it.
src/ngraph/builder/numpy_transpose.hpp
0 → 100644
View file @
676e601d
/*
Copyright 2017 Nervana Systems Inc.
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 "ngraph/common.hpp"
#include "ngraph/function.hpp"
#include "ngraph/node.hpp"
#include "ngraph/ops/constant.hpp"
#include "ngraph/ops/parameter.hpp"
#include "ngraph/ops/reduce.hpp"
#include "ngraph/types/type.hpp"
namespace
ngraph
{
namespace
builder
{
/// \brief Implement's Numpy's multidimensional transpose op. Doubles as DimShuffle.
///
/// If `order` is empty, the vector is transposed by reversing it's axes, i.e.
///
/// shape [1,2,4] becomes shape [4,2,1]
///
/// If `order` is provided, it should be a vector of unique axis positions ranging
/// from 0 to N-1, when N is the length of the input shape. In this case, numpy_transpose acts
/// like dimshuffle, so
///
/// shape [1,2,4] with order [1,2,0] becomes shape [2,4,1]
///
/// | | Type | Description |
/// | ---------------- | ------------------------------------- | ------------------------------------------------------- |
/// | `node` | \f$E[d_0,\dots,d_{n-1}]~(n \geq 0)\f$ | An input tensor of any shape |
/// | `order` | AxisVector (empty default) | The axes to eliminate through reduction (0 indexed). |
///
/// ## Output
///
/// | Type | Description |
/// | ---------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
/// | \f$E[d_{n-1},\dots,d_0)]\textit{ or }E[d_{order[0]},\dots,d_{order[n-1]}]\f$ | The tensor \f$T\f$, where \f$T\f$ is the input tensor with the axes reordered via Numpy Transpose rules |
std
::
shared_ptr
<
Node
>
numpy_transpose
(
const
std
::
shared_ptr
<
Node
>&
node
,
AxisVector
order
=
{});
}
// namespace builder
}
// namespace ngraph
This diff is collapsed.
Click to expand it.
src/ngraph/ngraph.hpp
View file @
676e601d
...
...
@@ -42,6 +42,7 @@
/// recipes, for example auto-broadcast.
#include "ngraph/builder/autobroadcast.hpp"
#include "ngraph/builder/numpy_transpose.hpp"
#include "ngraph/builder/reduce_ops.hpp"
#include "ngraph/common.hpp"
#include "ngraph/coordinate_iterator.hpp"
...
...
This diff is collapsed.
Click to expand it.
test/CMakeLists.txt
View file @
676e601d
...
...
@@ -23,8 +23,8 @@ include_directories(
set
(
SRC
autodiff.cpp
builder.cpp
builder_autobroadcast.cpp
builder_reduce_ops.cpp
build_graph.cpp
coordinate_iterator.cpp
copy.cpp
...
...
This diff is collapsed.
Click to expand it.
test/builder
_reduce_ops
.cpp
→
test/builder.cpp
View file @
676e601d
...
...
@@ -90,20 +90,20 @@ std::shared_ptr<ngraph::runtime::TensorView> make_reduce_result_false(
return
result
;
}
TEST
(
builder
_reduce_ops
,
l2_norm
)
TEST
(
builder
,
l2_norm
)
{
auto
result
=
make_reduce_result
(
builder
::
l2_norm
);
ASSERT_TRUE
(
all_close
((
vector
<
float
>
{
5.9160797831
f
,
7.48331477355
f
}),
result
->
get_vector
<
float
>
()));
}
TEST
(
builder
_reduce_ops
,
mean
)
TEST
(
builder
,
mean
)
{
auto
result
=
make_reduce_result
(
builder
::
mean
);
ASSERT_TRUE
(
all_close
((
vector
<
float
>
{
3
,
4
}),
result
->
get_vector
<
float
>
()));
}
TEST
(
builder
_reduce_ops
,
std_dev
)
TEST
(
builder
,
std_dev
)
{
auto
result
=
make_reduce_result_false
(
builder
::
std_dev
);
ASSERT_TRUE
(
...
...
@@ -112,7 +112,7 @@ TEST(builder_reduce_ops, std_dev)
ASSERT_TRUE
(
all_close
((
vector
<
float
>
{
2
,
2
}),
result
->
get_vector
<
float
>
()));
}
TEST
(
builder
_reduce_ops
,
variance
)
TEST
(
builder
,
variance
)
{
auto
result
=
make_reduce_result_false
(
builder
::
variance
);
ASSERT_TRUE
(
...
...
@@ -120,3 +120,31 @@ TEST(builder_reduce_ops, variance)
result
=
make_reduce_result_true
(
builder
::
variance
);
ASSERT_TRUE
(
all_close
((
vector
<
float
>
{
4
,
4
}),
result
->
get_vector
<
float
>
()));
}
TEST
(
builder
,
numpy_transpose
)
{
// 2D Transpose
Shape
shape
{
2
,
4
};
auto
param
=
std
::
make_shared
<
op
::
Parameter
>
(
ngraph
::
element
::
Float32
::
element_type
(),
shape
);
auto
transposed
=
std
::
dynamic_pointer_cast
<
op
::
Reshape
>
(
builder
::
numpy_transpose
(
param
));
EXPECT_EQ
(
Shape
({
4
,
2
}),
transposed
->
get_output_shape
());
// Multidimensional Transpose
shape
=
Shape
{
2
,
4
,
8
};
param
=
std
::
make_shared
<
op
::
Parameter
>
(
ngraph
::
element
::
Float32
::
element_type
(),
shape
);
transposed
=
std
::
dynamic_pointer_cast
<
op
::
Reshape
>
(
builder
::
numpy_transpose
(
param
));
EXPECT_EQ
(
Shape
({
8
,
4
,
2
}),
transposed
->
get_output_shape
());
// Dimshuffle
shape
=
Shape
{
2
,
4
,
8
};
param
=
std
::
make_shared
<
op
::
Parameter
>
(
ngraph
::
element
::
Float32
::
element_type
(),
shape
);
transposed
=
std
::
dynamic_pointer_cast
<
op
::
Reshape
>
(
builder
::
numpy_transpose
(
param
,
AxisVector
{
2
,
0
,
1
}));
EXPECT_EQ
(
Shape
({
8
,
2
,
4
}),
transposed
->
get_output_shape
());
// Bad Orders
EXPECT_ANY_THROW
(
std
::
dynamic_pointer_cast
<
op
::
Reshape
>
(
builder
::
numpy_transpose
(
param
,
AxisVector
{
2
})));
EXPECT_ANY_THROW
(
std
::
dynamic_pointer_cast
<
op
::
Reshape
>
(
builder
::
numpy_transpose
(
param
,
AxisVector
{
2
,
2
,
1
})));
}
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment