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
9d40c6b2
Commit
9d40c6b2
authored
Aug 25, 2017
by
Robert Kimball
Committed by
GitHub
Aug 25, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #58 from NervanaSystems/cyphers/factory
Switch to factory functions, style on some more files.
parents
b2f1ef60
b0c72a83
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
133 additions
and
46 deletions
+133
-46
log.hpp
src/log.hpp
+9
-6
function.hpp
src/ngraph/function.hpp
+5
-0
node.hpp
src/ngraph/node.hpp
+19
-4
op.hpp
src/ngraph/op.hpp
+68
-26
type.hpp
src/ngraph/type.hpp
+1
-1
function.cpp
src/ops/function.cpp
+1
-0
op.cpp
src/ops/op.cpp
+22
-2
tree.hpp
src/tree.hpp
+1
-1
util.hpp
src/util.hpp
+4
-3
uuid.hpp
src/uuid.hpp
+1
-1
build_graph.cpp
test/build_graph.cpp
+2
-2
No files found.
src/log.hpp
View file @
9d40c6b2
...
...
@@ -36,7 +36,8 @@ namespace nervana
return
i
<
_size
?
_string
[
i
]
:
throw
std
::
out_of_range
(
""
);
}
constexpr
const
char
*
get_ptr
(
size_t
offset
)
const
{
return
&
_string
[
offset
];
}
constexpr
size_t
size
()
const
{
return
_size
;
}
constexpr
size_t
size
()
const
{
return
_size
;
}
private
:
const
char
*
_string
;
size_t
_size
;
...
...
@@ -44,8 +45,9 @@ namespace nervana
constexpr
const
char
*
find_last
(
conststring
s
,
size_t
offset
,
char
ch
)
{
return
offset
==
0
?
s
.
get_ptr
(
0
)
:
(
s
[
offset
]
==
ch
?
s
.
get_ptr
(
offset
+
1
)
:
find_last
(
s
,
offset
-
1
,
ch
));
return
offset
==
0
?
s
.
get_ptr
(
0
)
:
(
s
[
offset
]
==
ch
?
s
.
get_ptr
(
offset
+
1
)
:
find_last
(
s
,
offset
-
1
,
ch
));
}
constexpr
const
char
*
find_last
(
conststring
s
,
char
ch
)
...
...
@@ -67,6 +69,7 @@ namespace nervana
~
log_helper
();
std
::
ostream
&
stream
()
{
return
_stream
;
}
private
:
std
::
stringstream
_stream
;
};
...
...
@@ -81,9 +84,9 @@ namespace nervana
static
void
stop
();
private
:
static
void
log_item
(
const
std
::
string
&
s
);
static
void
process_event
(
const
std
::
string
&
s
);
static
void
thread_entry
(
void
*
param
);
static
void
log_item
(
const
std
::
string
&
s
);
static
void
process_event
(
const
std
::
string
&
s
);
static
void
thread_entry
(
void
*
param
);
static
std
::
string
log_path
;
static
std
::
deque
<
std
::
string
>
queue
;
};
...
...
src/ngraph/function.hpp
View file @
9d40c6b2
...
...
@@ -33,6 +33,8 @@ namespace ngraph
Parameter
(
Function
&
function
,
size_t
index
);
std
::
string
description
()
const
override
{
return
"Parameter"
;
}
protected
:
Function
&
m_function
;
size_t
m_index
;
...
...
@@ -66,8 +68,11 @@ namespace ngraph
Parameter
::
ptr
parameter
(
size_t
i
)
{
return
m_parameters
[
i
];
}
std
::
string
name
()
const
override
{
return
m_name
;
}
protected
:
std
::
vector
<
Parameter
::
ptr
>
m_parameters
;
Result
m_result
;
std
::
string
m_name
;
};
}
src/ngraph/node.hpp
View file @
9d40c6b2
...
...
@@ -14,8 +14,9 @@
#pragma once
#include <vector>
#include <set>
#include <string>
#include <vector>
#include "ngraph/type.hpp"
...
...
@@ -31,27 +32,41 @@ namespace ngraph
class
Node
:
public
TypedValueMixin
{
public
:
using
ptr
=
std
::
shared_ptr
<
Node
>
;
protected
:
Node
(
const
std
::
vector
<
Node
::
ptr
>&
arguments
,
ValueType
::
ptr
type
=
nullptr
)
:
TypedValueMixin
(
type
)
,
m_arguments
(
arguments
)
{
// Add this node as a user of each argument.
for
(
auto
node
:
m_arguments
){
for
(
auto
node
:
m_arguments
)
{
node
->
m_users
.
insert
(
node
.
get
());
}
}
virtual
~
Node
()
{}
public
:
/// A "one-liner" describing this node.
virtual
std
::
string
description
()
const
=
0
;
/// Propagate types and check arguments for consistency
// virtual void propagate_types() = 0;
const
std
::
vector
<
Node
::
ptr
>
arguments
()
const
{
return
m_arguments
;
}
std
::
vector
<
Node
::
ptr
>
arguments
()
{
return
m_arguments
;
}
const
std
::
multiset
<
Node
*>
users
()
const
{
return
m_users
;
}
std
::
multiset
<
Node
*>
users
()
{
return
m_users
;
}
std
::
string
name
()
const
{
return
m_name
;
}
void
name
(
const
std
::
string
&
name
)
{
m_name
=
name
;
}
protected
:
std
::
vector
<
Node
::
ptr
>
m_arguments
;
std
::
multiset
<
Node
*>
m_users
;
std
::
multiset
<
Node
*>
m_users
;
std
::
string
m_name
;
};
}
src/ngraph/op.hpp
View file @
9d40c6b2
...
...
@@ -21,12 +21,22 @@
namespace
ngraph
{
class
Op
;
/**
** Every instance of Op corresponds to a unique defined operation.
**/
class
Op
{
protected
:
virtual
~
Op
()
{}
public
:
virtual
std
::
string
name
()
const
=
0
;
};
/**
** Call nodes are nodes whose value is the result of some operation, the op,
** applied to its arguments. We use the op as a callable to construct the
** call nodes.
** call nodes.
For calls to user functions, the op will be the user function.
**/
class
Call
:
public
Node
{
...
...
@@ -39,58 +49,90 @@ namespace ngraph
{
}
virtual
std
::
string
description
()
const
override
{
return
m_op
->
name
();
}
protected
:
std
::
shared_ptr
<
Op
>
m_op
;
};
/**
** The Op class provides the behavior for a Call.
** There is exactly one instance of builtin op for each pre-defined operation. These
** are intended to be used when matching calls in different graphs; every FooCall
** will have the same op.
**/
class
Op
class
BuiltinOp
:
public
Op
{
}
;
friend
class
Call
;
class
Broadcast
:
public
Op
,
public
std
::
enable_shared_from_this
<
Broadcast
>
{
protected
:
class
BroadcastCall
:
public
Call
public
:
BuiltinOp
(
const
std
::
string
&
name
)
:
m_name
(
name
)
{
friend
class
Broadcast
;
}
public
:
BroadcastCall
(
const
std
::
shared_ptr
<
Op
>&
op
,
const
Node
::
ptr
&
arg
,
size_t
axis
)
:
Call
(
op
,
{
arg
})
,
m_axis
(
axis
)
{
}
public
:
std
::
string
name
()
const
override
{
return
m_name
;
}
protected
:
size_t
m_axis
;
};
protected
:
std
::
string
m_name
;
};
class
BuiltinCall
:
public
Call
{
public
:
std
::
shared_ptr
<
BroadcastCall
>
operator
()(
const
Node
::
ptr
&
tensor
,
size_t
axis
)
virtual
std
::
string
description
()
const
override
{
return
"BuiltinCall"
;
}
protected
:
BuiltinCall
(
const
std
::
shared_ptr
<
Op
>&
op
,
const
std
::
vector
<
Node
::
ptr
>&
args
)
:
Call
(
op
,
args
)
{
return
std
::
make_shared
<
BroadcastCall
>
(
shared_from_this
(),
tensor
,
axis
);
}
};
namespace
op
{
extern
decltype
(
*
std
::
shared_ptr
<
Broadcast
>
())
broadcast
;
std
::
shared_ptr
<
Node
>
broadcast
(
const
Node
::
ptr
&
tensor
,
const
Shape
&
shape
,
const
std
::
vector
<
size_t
>&
broadcast_axes
);
}
class
Dot
:
public
Op
,
public
std
::
enable_shared_from_this
<
Dot
>
class
BroadcastCall
:
public
BuiltinCall
{
public
:
Call
::
ptr
operator
()(
const
Node
::
ptr
&
arg0
,
const
Node
::
ptr
&
arg1
)
/**
** /param arg The tensor view to be broadcast.
** /param shape The shape of the result
** /param broadcast_axes The axis positions (0-based) in the result that are being broadcast.
** the remaining axes in shape must be the same as the shape of arg.
**/
BroadcastCall
(
const
Node
::
ptr
&
arg
,
const
Shape
&
shape
,
std
::
vector
<
size_t
>
broadcast_axes
)
:
BuiltinCall
(
s_op
,
{
arg
})
,
m_shape
(
shape
)
,
m_broadcast_axes
(
broadcast_axes
)
{
return
std
::
make_shared
<
Call
>
(
shared_from_this
(),
std
::
vector
<
Node
::
ptr
>
{
arg0
,
arg1
});
}
Shape
m_shape
;
std
::
vector
<
size_t
>
m_broadcast_axes
;
protected
:
static
std
::
shared_ptr
<
BuiltinOp
>
s_op
;
};
namespace
op
{
extern
decltype
(
*
std
::
shared_ptr
<
Dot
>
())
dot
;
std
::
shared_ptr
<
Node
>
dot
(
const
Node
::
ptr
&
arg0
,
const
Node
::
ptr
&
arg1
)
;
}
class
DotCall
:
public
BuiltinCall
{
public
:
/// TODO: Semantics of arg0 and arg1 axes wrt reduction.
DotCall
(
const
Node
::
ptr
&
arg0
,
const
Node
::
ptr
&
arg1
)
:
BuiltinCall
(
s_op
,
{
arg0
,
arg1
})
{
}
protected
:
static
std
::
shared_ptr
<
BuiltinOp
>
s_op
;
};
}
src/ngraph/type.hpp
View file @
9d40c6b2
...
...
@@ -59,7 +59,7 @@ namespace ngraph
protected
:
const
element
::
Type
&
m_element_type
;
Shape
m_shape
;
Shape
m_shape
;
};
/**
...
...
src/ops/function.cpp
View file @
9d40c6b2
...
...
@@ -26,6 +26,7 @@ Parameter::Parameter(Function& function, size_t index)
Function
::
Function
(
size_t
n_parameters
)
:
m_parameters
(
n_parameters
)
,
m_name
(
"Function"
)
{
for
(
int
i
=
0
;
i
<
n_parameters
;
i
++
)
{
...
...
src/ops/op.cpp
View file @
9d40c6b2
...
...
@@ -15,7 +15,27 @@
#include "ngraph/ngraph.hpp"
using
namespace
ngraph
;
using
namespace
std
;
decltype
(
*
std
::
shared_ptr
<
Broadcast
>
())
ngraph
::
op
::
broadcast
=
*
std
::
make_shared
<
Broadcast
>
(
);
std
::
shared_ptr
<
BuiltinOp
>
BroadcastCall
::
s_op
=
make_shared
<
BuiltinOp
>
(
"broadcast"
);
decltype
(
*
std
::
shared_ptr
<
Dot
>
())
ngraph
::
op
::
dot
=
*
std
::
make_shared
<
Dot
>
();
/**
** /param arg The tensor view to be broadcast.
** /param shape The shape of the result
** /param broadcast_axes The axis positions (0-based) in the result that are being broadcast.
** the remaining axes in shape must be the same as the shape of arg.
**/
shared_ptr
<
Node
>
ngraph
::
op
::
broadcast
(
const
Node
::
ptr
&
tensor
,
const
Shape
&
shape
,
const
vector
<
size_t
>&
broadcast_axes
)
{
return
make_shared
<
BroadcastCall
>
(
tensor
,
shape
,
broadcast_axes
);
}
std
::
shared_ptr
<
BuiltinOp
>
DotCall
::
s_op
=
make_shared
<
BuiltinOp
>
(
"dot"
);
/// TODO: Semantics of arg0 and arg1 axes wrt reduction.
shared_ptr
<
Node
>
ngraph
::
op
::
dot
(
const
Node
::
ptr
&
arg0
,
const
Node
::
ptr
&
arg1
)
{
return
make_shared
<
DotCall
>
(
arg0
,
arg1
);
}
src/tree.hpp
View file @
9d40c6b2
...
...
@@ -51,7 +51,7 @@ public:
bool
is_list
()
const
{
return
m_is_list
;
}
T
get_value
()
const
{
return
m_value
;
}
const
std
::
vector
<
tree
>&
get_list
()
const
{
return
m_list
;
}
static
void
traverse_tree
(
tree
&
s
,
std
::
function
<
void
(
T
*
)
>
func
)
static
void
traverse_tree
(
tree
&
s
,
std
::
function
<
void
(
T
*
)
>
func
)
{
if
(
s
.
is_list
())
{
...
...
src/util.hpp
View file @
9d40c6b2
...
...
@@ -83,10 +83,10 @@ namespace ngraph
}
size_t
hash_combine
(
const
std
::
vector
<
size_t
>&
list
);
void
dump
(
std
::
ostream
&
out
,
const
void
*
,
size_t
);
void
dump
(
std
::
ostream
&
out
,
const
void
*
,
size_t
);
std
::
string
to_lower
(
const
std
::
string
&
s
);
std
::
string
trim
(
const
std
::
string
&
s
);
std
::
string
to_lower
(
const
std
::
string
&
s
);
std
::
string
trim
(
const
std
::
string
&
s
);
std
::
vector
<
std
::
string
>
split
(
const
std
::
string
&
s
,
char
delimiter
,
bool
trim
=
false
);
class
stopwatch
...
...
@@ -148,6 +148,7 @@ namespace ngraph
size_t
get_total_milliseconds
()
const
{
return
get_total_nanoseconds
()
/
1e6
;
}
size_t
get_total_microseconds
()
const
{
return
get_total_nanoseconds
()
/
1e3
;
}
size_t
get_total_nanoseconds
()
const
{
return
m_total_time
.
count
();
}
private
:
std
::
chrono
::
high_resolution_clock
m_clock
;
std
::
chrono
::
time_point
<
std
::
chrono
::
high_resolution_clock
>
m_start_time
;
...
...
src/uuid.hpp
View file @
9d40c6b2
...
...
@@ -73,7 +73,7 @@ public:
return
memcmp
((
char
*
)
m_data
,
(
char
*
)
other
.
m_data
,
16
)
==
0
;
}
bool
operator
!=
(
const
uuid_type
&
other
)
const
{
return
!
(
*
this
==
other
);
}
bool
operator
!=
(
const
uuid_type
&
other
)
const
{
return
!
(
*
this
==
other
);
}
friend
std
::
ostream
&
operator
<<
(
std
::
ostream
&
out
,
const
uuid_type
&
id
)
{
out
<<
id
.
to_string
();
...
...
test/build_graph.cpp
View file @
9d40c6b2
...
...
@@ -29,8 +29,8 @@ TEST(DISABLED_graph, build_simple)
cluster_0
->
parameter
(
2
)
->
type
(
element
::
float32_t
,
{
32
,
7
});
cluster_0
->
parameter
(
3
)
->
type
(
element
::
float32_t
,
{
32
,
7
});
auto
arg3
=
cluster_0
->
parameter
(
3
);
// call broadcast op on arg3, broadcasting on axis
1
.
auto
broadcast_1
=
op
::
broadcast
(
arg3
,
1
);
// call broadcast op on arg3, broadcasting on axis
0
.
auto
broadcast_1
=
op
::
broadcast
(
arg3
,
{
10
,
32
,
7
},
{
0
}
);
auto
arg2
=
cluster_0
->
parameter
(
2
);
auto
arg0
=
cluster_0
->
parameter
(
0
);
// call dot op
...
...
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