You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@singa.apache.org by wa...@apache.org on 2016/06/17 14:28:34 UTC

[2/4] incubator-singa git commit: SINGA-186 Create Python Tensor class

SINGA-186 Create Python Tensor class

- layer.py
- example_layer.py
- singa.i
- model_layer.i


Project: http://git-wip-us.apache.org/repos/asf/incubator-singa/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-singa/commit/24833faf
Tree: http://git-wip-us.apache.org/repos/asf/incubator-singa/tree/24833faf
Diff: http://git-wip-us.apache.org/repos/asf/incubator-singa/diff/24833faf

Branch: refs/heads/dev
Commit: 24833faf6b26209c6a3d7c060c91c647e05998a2
Parents: 2a582df
Author: chonho <le...@comp.nus.edu.sg>
Authored: Thu Jun 16 01:51:05 2016 +0800
Committer: Wei Wang <wa...@comp.nus.edu.sg>
Committed: Fri Jun 17 22:27:01 2016 +0800

----------------------------------------------------------------------
 src/python/example_layer.py          | 25 ++++++++++
 src/python/generate_singa_wrapper.sh |  4 --
 src/python/layer.py                  | 78 +++++++++++++++++++++++++----
 src/python/model_layer.i             | 83 +++++++++++++++++++++++++++++++
 src/python/singa.i                   |  2 +-
 5 files changed, 178 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/24833faf/src/python/example_layer.py
----------------------------------------------------------------------
diff --git a/src/python/example_layer.py b/src/python/example_layer.py
new file mode 100644
index 0000000..4084a4b
--- /dev/null
+++ b/src/python/example_layer.py
@@ -0,0 +1,25 @@
+import sys, os
+
+from layer import *
+
+sys.path.append(os.path.join(os.path.dirname(__file__),
+                             '..'))
+from model_pb2 import *
+
+#---------------------------------------------------------
+# example usage
+#---------------------------------------------------------
+
+l = Layer('layer')
+
+l_conf = LayerConf()
+l_conf.name = "chonho layer"
+l.setup(l_conf)
+
+print l.name()
+
+c = Conv2D(2, 3, name='chonho conv')
+print c.name()
+print c.conf
+
+

http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/24833faf/src/python/generate_singa_wrapper.sh
----------------------------------------------------------------------
diff --git a/src/python/generate_singa_wrapper.sh b/src/python/generate_singa_wrapper.sh
index 037db91..b98bf91 100755
--- a/src/python/generate_singa_wrapper.sh
+++ b/src/python/generate_singa_wrapper.sh
@@ -24,8 +24,6 @@ SRC_CC=(${SINGA_SRC}/core/tensor/tensor.cc \
        )
 USR_LOCAL=/home/chonho/local
 
-#The following commands are only for developers adding new py apis.
-#swig -c++ -python -w509 -I../../include singa.i
 swig -c++ -python -I../../include singa.i
 
 g++ -fPIC ${SRC_CC[@]} singa_wrap.cxx -shared -o _singa.so \
@@ -39,5 +37,3 @@ g++ -fPIC ${SRC_CC[@]} singa_wrap.cxx -shared -o _singa.so \
     -I${USR_LOCAL}/cudnn/include \
     -I/usr/include/python2.7 \
     -I/usr/local/cuda-7.0/include
-
-#python example.py

http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/24833faf/src/python/layer.py
----------------------------------------------------------------------
diff --git a/src/python/layer.py b/src/python/layer.py
index ec9125e..97a92e5 100644
--- a/src/python/layer.py
+++ b/src/python/layer.py
@@ -1,4 +1,7 @@
-#/**
+#!/usr/bin/env python
+
+# /************************************************************
+# *
 # * Licensed to the Apache Software Foundation (ASF) under one
 # * or more contributor license agreements.  See the NOTICE file
 # * distributed with this work for additional information
@@ -7,15 +10,72 @@
 # * "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
+# *   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.
 # *
-# * 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.
-# */
+# *************************************************************/
+
+import sys
+import os
+import numpy as np
+import singa
+
+sys.path.append(os.path.join(os.path.dirname(__file__), '../'))
+
+from core_pb2 import *
+from model_pb2 import *
+
+
+class Layer(object):
+
+    def __init__(self, name='default', **kwargs):
+        self.singa_layer = singa.Layer()
+        self.conf = LayerConf()
+        self.conf.name = name
+        # other initialization
+        # ...
+
+    def setup(self, proto):
+        self.singa_layer.Setup(proto.SerializeToString())
+
+    def forward(self, flag, inputs):
+        return self.singa_layer.Forward(flag, inputs)
+
+    def backward(self, flag, grads):
+        return self.singa_layer.Backward(flag, grads)
+
+    def to_device(self, device):
+        self.singa_layer.ToDevice(device)
+
+    def as_type(self, dtype):
+        self.singa_layer.AsType(dtype)
+
+    def name(self):
+        return self.singa_layer.name()
+
+
+class Conv2D(Layer):
+
+    def __init__(self, in_channels, out_channels, kernel=3, stride=1,
+                 border_mode='valid',  engine='cudnn', cudnn_prefer='fatest',
+                 data_format='NCHW', use_bias=True, pad=None, W_specs=None,
+                 b_specs=None, name=None):
+
+        super(Conv2D, self).__init__(name)
+
+        conf = ConvolutionConf()
+        conf.channels = in_channels
+        conf.num_output = out_channels
+        # other fields
+        # ...
 
-class Layer(Object):
+        self.conf.convolution_conf.CopyFrom(conf)
 
+        self.setup(self.conf)
 

http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/24833faf/src/python/model_layer.i
----------------------------------------------------------------------
diff --git a/src/python/model_layer.i b/src/python/model_layer.i
new file mode 100644
index 0000000..3fb4917
--- /dev/null
+++ b/src/python/model_layer.i
@@ -0,0 +1,83 @@
+/************************************************************
+*
+* Licensed to the Apache Software Foundation (ASF) under one
+* or more contributor license agreements.  See the NOTICE file
+* distributed with this work for additional information
+* regarding copyright ownership.  The ASF licenses this file
+* to you 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.
+*
+*************************************************************/
+
+/*interface file for swig */
+
+%module singa_layer
+%include "std_vector.i"
+%include "std_string.i"
+%include "std_pair.i"
+
+%{
+#include "singa/model/layer.h"
+#include "singa/core/tensor.h"
+#include "singa/proto/model.pb.h"
+using singa::Tensor;
+using singa::ParamSpec;
+using singa::DataType;
+using singa::Device;
+using singa::LayerConf;
+%}
+
+namespace std {
+  %template(strVector) vector<string>;
+  %template(paramVector) vector<ParamSpec>;
+  %template(tensorVector) vector<Tensor>;
+  %template(tensorPtrVector) vector<Tensor*>;
+  %template(ttvecPair) pair<Tensor, vector<Tensor>>;
+  %template(tvectvecPair) pair<vector<Tensor>, vector<Tensor>>;
+}
+
+namespace singa {
+
+  class Layer {
+    public:
+      Layer();
+      void Setup(const std::string& proto_str);
+
+      std::string ToProtoStr() const;
+      const std::vector<ParamSpec> param_specs();
+      const ParamSpec& param_specs(size_t i);
+      const std::vector<Tensor*> param_values();
+      Tensor* param_value(size_t i);
+      const std::vector<std::string> param_names();
+      const std::string& param_name(size_t i);
+      const std::string name() const;
+
+      /* virtual functions */
+      virtual const std::string layer_type() const;
+      virtual void Setup(const LayerConf& conf);
+      virtual void ToDevice(Device* device);
+      virtual void AsType(DataType dtype);
+      virtual void ToProto(LayerConf* conf) const;
+
+      virtual const Tensor
+      Forward(int flag, const Tensor& input);
+      virtual const std::vector<Tensor>
+      Forward(int flag, const std::vector<Tensor>& inputs);
+      virtual const std::pair<Tensor, std::vector<Tensor>>
+      Backward(int flag, const Tensor& grad);
+      virtual const std::pair<std::vector<Tensor>, std::vector<Tensor>>
+      Backward(int flag, const vector<Tensor>& grads);
+  };
+
+}
+

http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/24833faf/src/python/singa.i
----------------------------------------------------------------------
diff --git a/src/python/singa.i b/src/python/singa.i
index 8883404..8b5e2dc 100644
--- a/src/python/singa.i
+++ b/src/python/singa.i
@@ -24,4 +24,4 @@
 %module singa
 %include "core_tensor.i"
 %include "core_device.i"
-//%include "model_layer.i"
+%include "model_layer.i"