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
a2593991
Unverified
Commit
a2593991
authored
Jul 24, 2019
by
Robert Kimball
Committed by
GitHub
Jul 24, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #3276 from NervanaSystems/tomdol/pycapsule
Python capsules support for ngraph::Function
parents
f6f3a032
2a3c7935
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
39 additions
and
0 deletions
+39
-0
function.cpp
python/pyngraph/function.cpp
+39
-0
No files found.
python/pyngraph/function.cpp
View file @
a2593991
...
...
@@ -23,6 +23,8 @@
namespace
py
=
pybind11
;
static
const
char
*
CAPSULE_NAME
=
"ngraph_function"
;
void
regclass_pyngraph_Function
(
py
::
module
m
)
{
py
::
class_
<
ngraph
::
Function
,
std
::
shared_ptr
<
ngraph
::
Function
>>
function
(
m
,
"Function"
);
...
...
@@ -49,4 +51,41 @@ void regclass_pyngraph_Function(py::module m)
py
::
cast
(
self
.
get_output_shape
(
0
)).
attr
(
"__str__"
)().
cast
<
std
::
string
>
();
return
"<"
+
class_name
+
": '"
+
self
.
get_friendly_name
()
+
"' ("
+
shape
+
")>"
;
});
function
.
def_static
(
"from_capsule"
,
[](
py
::
object
*
capsule
)
{
// get the underlying PyObject* which is a PyCapsule pointer
auto
*
pybind_capsule_ptr
=
capsule
->
ptr
();
// extract the pointer stored in the PyCapsule under the name CAPSULE_NAME
auto
*
capsule_ptr
=
PyCapsule_GetPointer
(
pybind_capsule_ptr
,
CAPSULE_NAME
);
auto
*
ngraph_function
=
static_cast
<
std
::
shared_ptr
<
ngraph
::
Function
>*>
(
capsule_ptr
);
if
(
ngraph_function
)
{
return
*
ngraph_function
;
}
else
{
throw
std
::
runtime_error
(
"The provided capsule does not contain an ngraph::Function"
);
}
});
function
.
def_static
(
"to_capsule"
,
[](
std
::
shared_ptr
<
ngraph
::
Function
>&
ngraph_function
)
{
// create a shared pointer on the heap before putting it in the capsule
// this secures the lifetime of the object transferred by the capsule
auto
*
sp_copy
=
new
std
::
shared_ptr
<
ngraph
::
Function
>
(
ngraph_function
);
// a destructor callback that will delete the heap allocated shared_ptr
// when the capsule is destructed
auto
sp_deleter
=
[](
PyObject
*
capsule
)
{
auto
*
capsule_ptr
=
PyCapsule_GetPointer
(
capsule
,
CAPSULE_NAME
);
auto
*
function_sp
=
static_cast
<
std
::
shared_ptr
<
ngraph
::
Function
>*>
(
capsule_ptr
);
if
(
function_sp
)
{
delete
function_sp
;
}
};
// put the shared_ptr in a new capsule under the same name as in "from_capsule"
auto
pybind_capsule
=
py
::
capsule
(
sp_copy
,
CAPSULE_NAME
,
sp_deleter
);
return
pybind_capsule
;
});
}
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