Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get OpProtos in Python #2864

Merged
merged 4 commits into from
Jul 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions paddle/framework/op_registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,17 @@ class OpRegistry {
return op;
}

static std::unordered_map<std::string, OpProto>& protos() {
static std::unordered_map<std::string, OpProto> protos_;
return protos_;
};

private:
static std::unordered_map<std::string, OpCreator>& creators() {
static std::unordered_map<std::string, OpCreator> creators_;
return creators_;
}

static std::unordered_map<std::string, OpProto>& protos() {
static std::unordered_map<std::string, OpProto> protos_;
return protos_;
};

static std::unordered_map<std::string, OpAttrChecker>& op_checkers() {
static std::unordered_map<std::string, OpAttrChecker> op_checkers_;
return op_checkers_;
Expand Down
2 changes: 1 addition & 1 deletion paddle/pybind/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
cc_library(paddle_pybind SHARED SRCS pybind.cc DEPS pybind python)
cc_library(paddle_pybind SHARED SRCS pybind.cc DEPS pybind python add_op)
21 changes: 21 additions & 0 deletions paddle/pybind/pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@ See the License for the specific language governing permissions and
limitations under the License. */

#include <Python.h>
#include <paddle/framework/op_registry.h>
#include <paddle/framework/scope.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <fstream>
#include <vector>

namespace py = pybind11;
namespace pd = paddle::framework;

USE_OP(add_two);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个后面是不是会专门放一个地方?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是的。可以单独放一个.cc里面,而且那个.cc应该也可以是编译器生成的。


PYBIND11_PLUGIN(core) {
py::module m("core", "C++ core of Paddle Paddle");

Expand All @@ -43,5 +49,20 @@ All parameter, weight, gradient are variables in Paddle.
&pd::Scope::CreateVariable,
py::return_value_policy::reference);

//! @note: Be careful! PyBind will return std::string as an unicode, not
//! Python str. If you want a str object, you should cast them in Python.
m.def("get_all_op_protos", []() -> std::vector<std::string> {
auto& protos = pd::OpRegistry::protos();
std::vector<std::string> ret_values;
for (auto it = protos.begin(); it != protos.end(); ++it) {
PADDLE_ENFORCE(it->second.IsInitialized(),
"OpProto must all be initialized");
ret_values.emplace_back();
PADDLE_ENFORCE(it->second.SerializeToString(&ret_values.back()),
"Serialize OpProto Error. This could be a bug of Paddle.");
}
return ret_values;
});

return m.ptr();
}
4 changes: 1 addition & 3 deletions python/paddle/v2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import event
import data_type
import topology
import data_feeder
import networks
import evaluator
from . import dataset
Expand All @@ -31,7 +30,6 @@
import pooling
import inference
import networks
import py_paddle.swig_paddle as api
import minibatch
import plot
import image
Expand All @@ -47,7 +45,6 @@
'data_type',
'attr',
'pooling',
'data_feeder',
'dataset',
'reader',
'topology',
Expand All @@ -61,6 +58,7 @@


def init(**kwargs):
import py_paddle.swig_paddle as api
args = []
args_dict = {}
# NOTE: append arguments if they are in ENV
Expand Down
1 change: 0 additions & 1 deletion python/paddle/v2/data_feeder.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
# 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.

from py_paddle import DataProviderConverter
import collections
import paddle.trainer.PyDataProvider2 as pydp2
Expand Down
3 changes: 1 addition & 2 deletions python/paddle/v2/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@
* BeginPass
* EndPass
"""
import py_paddle.swig_paddle as api

__all__ = [
'EndIteration', 'BeginIteration', 'BeginPass', 'EndPass', 'TestResult'
]


class WithMetric(object):
def __init__(self, evaluator):
import py_paddle.swig_paddle as api
if not isinstance(evaluator, api.Evaluator):
raise TypeError("Evaluator should be api.Evaluator type")
self.__evaluator__ = evaluator
Expand Down
11 changes: 11 additions & 0 deletions python/paddle/v2/framework/create_op_creation_methods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import paddle.v2.framework.core as core
import paddle.v2.framework.proto.op_proto_pb2 as op_proto_pb2


def get_all_op_protos():
protostrs = core.get_all_op_protos()
ret_values = []
for pbstr in protostrs:
op_proto = op_proto_pb2.OpProto.FromString(str(pbstr))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this str() method the way to fix the Unicode problem?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep.

ret_values.append(op_proto)
return ret_values
2 changes: 1 addition & 1 deletion python/paddle/v2/framework/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
add_python_test(test_framework test_protobuf.py test_scope.py
test_default_scope_funcs.py)
test_default_scope_funcs.py test_op_creation_methods.py)
15 changes: 15 additions & 0 deletions python/paddle/v2/framework/tests/test_op_creation_methods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import unittest
import paddle.v2.framework.create_op_creation_methods as creation


class TestOpCreationsMethods(unittest.TestCase):
def test_all_protos(self):
all_protos = creation.get_all_op_protos()
self.assertNotEqual(0, len(all_protos))

for each in all_protos:
self.assertTrue(each.IsInitialized())


if __name__ == "__main__":
unittest.main()
4 changes: 2 additions & 2 deletions python/paddle/v2/inference.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import numpy
import py_paddle.swig_paddle as api
import collections
import topology
import minibatch
from data_feeder import DataFeeder

__all__ = ['infer', 'Inference']

Expand All @@ -28,6 +26,7 @@ class Inference(object):
"""

def __init__(self, output_layer, parameters):
import py_paddle.swig_paddle as api
topo = topology.Topology(output_layer)
gm = api.GradientMachine.createFromConfigProto(
topo.proto(), api.CREATE_MODE_TESTING, [api.PARAMETER_VALUE])
Expand All @@ -40,6 +39,7 @@ def __init__(self, output_layer, parameters):
self.__data_types__ = topo.data_type()

def iter_infer(self, input, feeding=None):
from data_feeder import DataFeeder
feeder = DataFeeder(self.__data_types__, feeding)
batch_size = len(input)

Expand Down
4 changes: 2 additions & 2 deletions python/paddle/v2/optimizer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import py_paddle.swig_paddle as swig_api

import paddle.trainer_config_helpers.config_parser_utils as config_parser_utils
import paddle.trainer_config_helpers.optimizers as v1_optimizers
"""
Expand All @@ -18,6 +16,7 @@

class Optimizer(object):
def __init__(self, **kwargs):
import py_paddle.swig_paddle as swig_api
if 'batch_size' in kwargs:
del kwargs['batch_size'] # not important for python library.

Expand Down Expand Up @@ -268,6 +267,7 @@ def __init__(self, rho=0.95, epsilon=1e-6, **kwargs):
L2Regularization = v1_optimizers.L2Regularization

if __name__ == '__main__':
import py_paddle.swig_paddle as swig_api
swig_api.initPaddle('--use_gpu=false')
for opt in [
Momentum(), Adam(), Adamax(), AdaGrad(), DecayedAdaGrad(),
Expand Down
5 changes: 3 additions & 2 deletions python/paddle/v2/parameters.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import numpy as np
import py_paddle.swig_paddle as api
from paddle.proto.ParameterConfig_pb2 import ParameterConfig
import paddle.trainer.config_parser as cp
import struct
Expand Down Expand Up @@ -124,6 +123,7 @@ def __getitem__(self, key):
:return: parameter value
:rtype: np.ndarray
"""
import py_paddle.swig_paddle as api
shape = self.get_shape(key)

if len(self.__gradient_machines__) == 0:
Expand Down Expand Up @@ -223,7 +223,7 @@ def append_gradient_machine(self, gradient_machine):
:type gradient_machine: api.GradientMachine
:return:
"""

import py_paddle.swig_paddle as api
if not isinstance(gradient_machine, api.GradientMachine):
raise ValueError("gradient_machine should be api.GradientMachine")

Expand Down Expand Up @@ -359,6 +359,7 @@ def __copy_parameter_to_gradient_machine__(gradient_machine, name, arr):
:return:
:rtype: api.Parameter
"""
import py_paddle.swig_paddle as api
param = __get_parameter_in_gradient_machine__(gradient_machine, name)
vec = param.getBuf(api.PARAMETER_VALUE)
assert isinstance(vec, api.Vector)
Expand Down
11 changes: 5 additions & 6 deletions python/paddle/v2/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@
Module Trainer
"""
import collections
import gzip
import os

import py_paddle.swig_paddle as api

from data_feeder import DataFeeder
from topology import Topology
from . import event as v2_event
from . import optimizer as v2_optimizer
Expand Down Expand Up @@ -59,6 +53,7 @@ def __init__(self,
if not isinstance(update_equation, v2_optimizer.Optimizer):
raise TypeError("update equation parameter must be "
"paddle.v2.optimizer.Optimizer")
import py_paddle.swig_paddle as api
topology = Topology(cost, extra_layers=extra_layers)
self.__optimizer__ = update_equation
self.__topology__ = topology
Expand Down Expand Up @@ -124,6 +119,8 @@ def train(self, reader, num_passes=1, event_handler=None, feeding=None):
:type feeding: dict|list
:return:
"""
import py_paddle.swig_paddle as api
from data_feeder import DataFeeder
if event_handler is None:
event_handler = default_event_handler
__check_train_args__(**locals())
Expand Down Expand Up @@ -187,6 +184,8 @@ def test(self, reader, feeding=None):
:type feeding: dict
:return:
"""
import py_paddle.swig_paddle as api
from data_feeder import DataFeeder
feeder = DataFeeder(self.__data_types__, feeding)
evaluator = self.__gradient_machine__.makeEvaluator()
out_args = api.Arguments.createArguments(0)
Expand Down
3 changes: 2 additions & 1 deletion python/setup.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ setup_requires=["requests",
"recordio",
"matplotlib",
"rarfile",
"scipy>=0.19.0"]
"scipy>=0.19.0",
"nltk"]

if '${CMAKE_SYSTEM_PROCESSOR}' not in ['arm', 'armv7-a', 'aarch64']:
setup_requires+=["opencv-python"]
Expand Down