singa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From wang...@apache.org
Subject [1/4] incubator-singa git commit: SINGA-186 Create Python Tensor class
Date Fri, 17 Jun 2016 14:28:33 GMT
Repository: incubator-singa
Updated Branches:
  refs/heads/dev 890054396 -> 396f9bf71


SINGA-186 Create Python Tensor class

- Revised device.py and tensor.py based on the previous commit
  Assumed that
  . singa.py is in build/python
  . _singa.so is in build/lib

- swig interface files are in src/python/swig folder
  . a wrapper file singa_wrapper.cxx is generated in the folder

NOTE: unittests in test/python are tentative


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

Branch: refs/heads/dev
Commit: 396f9bf712d491a87ef16221b67d3ab17fce8955
Parents: 254343c
Author: chonho <leech@comp.nus.edu.sg>
Authored: Fri Jun 17 19:46:11 2016 +0800
Committer: Wei Wang <wangwei@comp.nus.edu.sg>
Committed: Fri Jun 17 22:27:01 2016 +0800

----------------------------------------------------------------------
 src/CMakeLists.txt                 |   8 +-
 src/python/device.py               |   7 +-
 src/python/tensor.py               |   9 +-
 test/python/example_test_device.py |   4 +-
 test/python/example_test_tensor.py | 179 ++++++++++++++++++++++++++++++++
 test/python/unittest_python.py     |   2 +-
 6 files changed, 198 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/396f9bf7/src/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index bc50a6b..a4fa22f 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -70,9 +70,9 @@ IF(USE_PYTHON)
     swig_generate_cxx(python_srcs ${python_files})
     SET(python_cxxs "${CMAKE_SOURCE_DIR}/src/core/tensor/tensor.cc;${CMAKE_SOURCE_DIR}/src/core/device/device.cc")
     ADD_LIBRARY(_singa SHARED ${python_srcs} ${python_cxxs})
-    SET(WRAPPER_LINKER_LIBS ${SINGA_LINKER_LIBS})
-    TARGET_LINK_LIBRARIES(_singa ${SINGA_LINKER_LIBS})
-    TARGET_INCLUDE_DIRECTORIES(_singa PRIVATE ${PYTHON_INCLUDE_DIRS}) 
-    SET_TARGET_PROPERTIES(_singa 
+    SET(WRAPPER_LINKER_LIBS "${SINGA_LINKER_LIBS};protobuf")
+    TARGET_LINK_LIBRARIES(_singa ${WRAPPER_LINKER_LIBS})
+    TARGET_INCLUDE_DIRECTORIES(_singa PRIVATE ${PYTHON_INCLUDE_DIRS})
+    SET_TARGET_PROPERTIES(_singa
         PROPERTIES PREFIX "")
 ENDIF(USE_PYTHON)

http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/396f9bf7/src/python/device.py
----------------------------------------------------------------------
diff --git a/src/python/device.py b/src/python/device.py
index 9a9787c..68e0f6a 100644
--- a/src/python/device.py
+++ b/src/python/device.py
@@ -28,9 +28,12 @@ to call singa::Device and its methods
 import sys
 import os
 import numpy as np
-import singa
 
-sys.path.append(os.path.join(os.path.dirname(__file__), '../'))
+sys.path.append(os.path.join(os.path.dirname(__file__),
+                             '../../build/lib'))
+sys.path.append(os.path.join(os.path.dirname(__file__),
+                             '../../build/python'))
+import singa
 
 
 class Device(object):

http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/396f9bf7/src/python/tensor.py
----------------------------------------------------------------------
diff --git a/src/python/tensor.py b/src/python/tensor.py
index 12e6cb4..04e070d 100644
--- a/src/python/tensor.py
+++ b/src/python/tensor.py
@@ -28,10 +28,15 @@ to call singa::Tensor and its methods
 import sys
 import os
 import numpy as np
-import singa
 
-sys.path.append(os.path.join(os.path.dirname(__file__), '../'))
+sys.path.append(os.path.join(os.path.dirname(__file__),
+                             '../../build/lib'))
+sys.path.append(os.path.join(os.path.dirname(__file__),
+                             '../../build/python'))
+import singa
 
+sys.path.append(os.path.join(os.path.dirname(__file__),
+                             '../../build/src'))
 from core_pb2 import *
 
 

http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/396f9bf7/test/python/example_test_device.py
----------------------------------------------------------------------
diff --git a/test/python/example_test_device.py b/test/python/example_test_device.py
index b7fb28e..c545ad1 100644
--- a/test/python/example_test_device.py
+++ b/test/python/example_test_device.py
@@ -2,11 +2,11 @@ import sys, os
 
 sys.path.append(os.path.join(os.path.dirname(__file__),
                              '../../src/python'))
-from tensor import *
 from device import *
+from tensor import *
 
 sys.path.append(os.path.join(os.path.dirname(__file__),
-                             '../../src'))
+                             '../../build/src'))
 from core_pb2 import *
 
 #---------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/396f9bf7/test/python/example_test_tensor.py
----------------------------------------------------------------------
diff --git a/test/python/example_test_tensor.py b/test/python/example_test_tensor.py
new file mode 100644
index 0000000..9b3d6f4
--- /dev/null
+++ b/test/python/example_test_tensor.py
@@ -0,0 +1,179 @@
+import sys, os
+import numpy as np
+
+sys.path.append(os.path.join(os.path.dirname(__file__),
+                             '../../src/python'))
+from tensor import *
+
+sys.path.append(os.path.join(os.path.dirname(__file__),
+                             '../../build/src'))
+from core_pb2 import *
+
+
+#---------------------------------------------------------
+# example usage
+#---------------------------------------------------------
+
+print '----------------------------'
+print 'global SizeOf kFloat32:', sizeof(kFloat32)
+print 'global SizeOf kFloat16:', sizeof(kFloat16)
+print 'global SizeOf kInt:', sizeof(kInt)
+print 'global SizeOf kDouble:', sizeof(kDouble)
+print
+
+a = Tensor()
+print 'a = Tensor()'
+print 'only defaultdevice is assigned \n'
+
+shape = (1, 6)
+t = Tensor(shape)
+print 'shape = (1, 6):', t.shape()
+print 'shape(0), shape(1):', t.shape(0), t.shape(1)
+print 'global Product:', product(shape)
+print 't = Tensor(shape)'
+#t.singa_tensor.AsType(kInt)
+print 'data_type():', t.data_type()
+print 'transpose', t.is_transpose()
+print 'nDim:', t.ndim()
+print 'size:', t.size()
+print 'memsize:', t.memsize()
+print 'data():', t.toarray()
+print
+
+print '----------------------------'
+print 't.data\n', t.toarray()
+q = t.copy()
+w = t.deepcopy()
+print 'q.data\n', q.toarray()
+print 'w.data\n', w.toarray()
+t += 1.23
+print 'q.data\n', q.toarray()
+print 'w.data\n', w.toarray()
+
+print '----------------------------'
+shape = (2, 3)
+t.reshape(shape)
+print 'shape = (3, 2)'
+print 'after reshape, t.shape():', t.shape()
+print 't.data(): \n', t.toarray()
+shape = (3, 2)
+t0 = reshape(t, shape)
+print 'shape = (2, 3)'
+print 'after t0 = reshape(t, shape) \n'
+print 't.shape():', t.shape()
+print 't0.shape():', t0.shape()
+print
+
+print '----------------------------'
+t += 1.2345
+print 't += 1.234, i.e., t.__iadd__(1.2345): \n', t.toarray()
+print
+
+t1 = t
+print 'copy\nt1 = t'
+print 't1.shape():', t1.shape()
+print 't1.data(): \n', t1.toarray()
+print
+
+r = t1.transpose()
+print 'r = t1.transpose()\nr.data() \n', r.toarray()
+
+
+print '----------------------------'
+t2 = log(t1)
+print 't2 = log(t1): \n', t2.toarray()
+print
+
+t1 += t2
+print 't1 += t2, i.e., t1.__iadd__(t2): \n', t1.toarray()
+print
+
+t1 *= 2
+print 't1 *= 2, i.e., t1.__imul__(2): \n', t1.toarray()
+print
+
+print '----------------------------'
+tc = t2.clone()
+print 'clone\ntc = t2.clone()\ntc.data(): \n', tc.toarray()
+print
+
+print 'sum(tc) \n', sum(tc)
+print
+t3 = sum(tc,0)
+print 'sum(tc,0) \n', t3.toarray()
+t3 = sum(tc,1)
+print 'sum(tc,1) \n', t3.toarray()
+print
+
+t3 = average(tc,0)
+print 'average(tc,0) \n', t3.toarray()
+t3 = average(tc,1)
+print 'average(tc,1) \n', t3.toarray()
+print
+
+t3 = softmax(tc,0)
+print 'softmax(tc,0)\n', t3.toarray()
+t3 = softmax(tc,1)
+print 'softmax(tc,1)\n', t3.toarray()
+
+print '----------------------------'
+print 't1 \n', t1.toarray()
+print
+
+n = t1 + t2
+print 't1 + t2: \n', n.toarray()
+print
+
+n = t1 * t2
+print 't1*t2: \n', n.toarray()
+print
+
+n = t1 - 1.2
+print 't1 - 1.2 \n', n.toarray()
+print
+
+n = add(t1, t1)
+print 'add(t1, t1) \n', n.toarray()
+print
+
+n = add(t1, 3.4)
+print 'add(t1, 3.4) \n', n.toarray()
+print
+
+n = div(t1, 2.0)
+print 'div(t1, 2.0) \n', n.toarray()
+print
+
+print '----------------------------'
+shape = (2, 2)
+t4 = Tensor(shape)
+t4 += 3.45
+print 't4 += 3.45 \n', t4.toarray()
+print
+
+n = t4 < 3.45
+print 't4 < 3.45 \n', n.toarray()
+print
+
+n = lt(t4, 3.45)
+print 'lt(t4, 3.45) \n', n.toarray()
+print
+
+n = ge(t4, 3.45)
+print 'ge(t4, 3.45) \n', n.toarray()
+print
+
+print '----------------------------'
+print t1.toarray()
+print tc.toarray()
+print
+copy_data_to_from(t1, tc, 2)
+print t1.toarray()
+print tc.toarray()
+
+#ttt = t1.singa_tensor < 5.2
+#ttt = lessthan(t1, 5.2)
+#print ttt.data()
+
+#devCPU = singa.CppCPU(1)
+#devGPU = singa.CudaGPU(2)

http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/396f9bf7/test/python/unittest_python.py
----------------------------------------------------------------------
diff --git a/test/python/unittest_python.py b/test/python/unittest_python.py
index 2b35d34..e1f4b64 100644
--- a/test/python/unittest_python.py
+++ b/test/python/unittest_python.py
@@ -33,7 +33,7 @@ from tensor import *
 from device import *
 
 sys.path.append(os.path.join(os.path.dirname(__file__),
-                             '../../src'))
+                             '../../build/src'))
 from core_pb2 import *
 
 class TestTensorMethods(unittest.TestCase):


Mime
View raw message