Skip to content

Commit cad2f48

Browse files
committed
add .toNumpyTensor() method
1 parent 08a5690 commit cad2f48

File tree

14 files changed

+8131
-5224
lines changed

14 files changed

+8131
-5224
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ pip install numpy
7878
pip install Cython
7979
pip install Jinja2
8080
pip install pytest
81+
pip install numpy
8182
```
8283
- lua51 headers should be installed, ie something like `sudo apt-get install lua5.1 liblua5.1-dev`
8384

@@ -130,6 +131,8 @@ from __future__ import print_function, division
130131
* modified `/` to be the div operation for float and double tensors, and `//` for int-type tensors, such as
131132
byte, long, int
132133
* since the div change is incompatible with 1.0.0 div operators, jumping radically from `1.0.0` to `2.0.0-SNAPSHOT` ...
134+
* added dependency on `numpy`
135+
* added `.asNumpyTensor()` to convert a torch tensor to a numpy tensor
133136

134137
24th February:
135138
* added support for passing strings to methods

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def get_file_datetime(filepath):
120120

121121
setup(
122122
name='PyTorch',
123-
version='2.1.0-SNAPSHOT',
123+
version='2.1.0',
124124
author='Hugh Perkins',
125125
author_email='hughperkins@gmail.com',
126126
description=(

simpleexample/luabit.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ function Luabit:getOut(inTensor, outSize, kernelSize)
1010
local inSize = inTensor:size(3)
1111
local m = nn.TemporalConvolution(inSize, outSize, kernelSize)
1212
m:float()
13-
return m:forward(inTensor)
13+
local out = m:forward(inTensor)
14+
print('out from lua', out)
15+
return out
1416
end
1517

simpleexample/pybit.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ def __init__(self, _fromLua=False):
2424
luabit = Luabit()
2525

2626
inTensor = np.random.randn(batchSize, numFrames, inSize).astype('float32')
27+
luain = PyTorch.asFloatTensor(inTensor)
2728

28-
luaout = luabit.getOut(PyTorch.asFloatTensor(inTensor), outSize, kernelSize)
29+
luaout = luabit.getOut(luain, outSize, kernelSize)
2930

30-
print('luaout.size()', luaout.size())
31-
for b in range(batchSize):
32-
print('luaout[' + str(b) + ']', luaout[b])
31+
outTensor = luaout.asNumpyTensor()
32+
print('outTensor', outTensor)
3333

src/PyTorch.cpp

Lines changed: 7891 additions & 5150 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/PyTorch.jinja2.pxd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ cdef extern from "THTensor.h":
2727

2828
cdef class _{{Real}}Tensor(object):
2929
cdef TH{{Real}}Tensor *native
30+
cdef {{real}} *data(self)
31+
# cpdef _{{Real}}Tensor contiguous(self)
3032
cpdef int dims(self)
3133
cpdef set1d(self, int x0, {{real}} value)
3234
cpdef set2d(self, int x0, int x1, {{real}} value)

src/PyTorch.jinja2.pyx

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import numbers
66
import cython
77
cimport cython
88

9+
import numpy as np
10+
911
cimport cpython.array
1012
import array
1113

@@ -64,6 +66,8 @@ cdef extern from "THTensor.h":
6466
cdef struct TH{{Real}}Tensor
6567
TH{{Real}}Tensor *TH{{Real}}Tensor_new()
6668
TH{{Real}}Tensor *TH{{Real}}Tensor_newClone(TH{{Real}}Tensor *self)
69+
{{real}} *TH{{Real}}Tensor_data(TH{{Real}}Tensor *self)
70+
TH{{Real}}Tensor *TH{{Real}}Tensor_newContiguous(TH{{Real}}Tensor *self)
6771
TH{{Real}}Tensor *TH{{Real}}Tensor_newWithSize1d(long size0)
6872
TH{{Real}}Tensor *TH{{Real}}Tensor_newWithSize2d(long size0, long size1)
6973
TH{{Real}}Tensor *TH{{Real}}Tensor_newWithSize3d(long size0, long size1, long size2)
@@ -217,10 +221,35 @@ cdef class _{{Real}}Tensor(object):
217221
def nElement(_{{Real}}Tensor self):
218222
return TH{{Real}}Tensor_nElement(self.native)
219223

224+
def asNumpyTensor(_{{Real}}Tensor self):
225+
cdef Storage._{{Real}}Storage storage
226+
cdef {{real}} *data
227+
cdef _{{Real}}Tensor contig
228+
size = self.size()
229+
dims = len(size)
230+
if dims >= 1:
231+
totalSize = 1
232+
for d in range(dims - 1, -1, -1):
233+
totalSize *= size[d]
234+
myarray = np.zeros(totalSize, dtype=np.float32)
235+
contig = self.contiguous()
236+
data = contig.data()
237+
for i in range(totalSize):
238+
myarray[i] = data[i]
239+
shape = []
240+
for d in range(dims):
241+
shape.append(size[d])
242+
return myarray.reshape(shape)
243+
else:
244+
raise Exception('Not implemented for dims = {dims}'.format(dims=dims))
245+
220246
@property
221247
def refCount(_{{Real}}Tensor self):
222248
return TH{{Real}}Tensor_getRefCount(self.native)
223249

250+
cdef {{real}} *data(_{{Real}}Tensor self):
251+
return TH{{Real}}Tensor_data(self.native)
252+
224253
cpdef int dims(self):
225254
return TH{{Real}}Tensor_nDimension(self.native)
226255

@@ -334,6 +363,11 @@ cdef class _{{Real}}Tensor(object):
334363
cdef TH{{Real}}Tensor *narrowedC = TH{{Real}}Tensor_newNarrow(self.native, dimension, firstIndex, size)
335364
return _{{Real}}Tensor_fromNative(narrowedC, retain=False)
336365

366+
367+
def contiguous(_{{Real}}Tensor self):
368+
cdef TH{{Real}}Tensor *newTensorC = TH{{Real}}Tensor_newContiguous(self.native)
369+
return _{{Real}}Tensor_fromNative(newTensorC)
370+
337371
def resize1d(_{{Real}}Tensor self, int size0):
338372
TH{{Real}}Tensor_resize1d(self.native, size0)
339373
return self
@@ -457,7 +491,6 @@ cdef class _{{Real}}Tensor(object):
457491
return res
458492

459493
def __itruediv__(_{{Real}}Tensor self, second):
460-
# print('__idiv__')
461494
cdef _{{Real}}Tensor secondTensor
462495
if isinstance(second, numbers.Number):
463496
TH{{Real}}Tensor_div(self.native, self.native, second)
@@ -467,7 +500,6 @@ cdef class _{{Real}}Tensor(object):
467500
return self
468501
{% else %}
469502
def __floordiv__(_{{Real}}Tensor self, second):
470-
# print('__div__')
471503
cdef _{{Real}}Tensor res = _{{Real}}Tensor.new()
472504
cdef _{{Real}}Tensor secondTensor
473505
if isinstance(second, numbers.Number):
@@ -478,7 +510,6 @@ cdef class _{{Real}}Tensor(object):
478510
return res
479511

480512
def __ifloordiv__(_{{Real}}Tensor self, second):
481-
# print('__idiv__')
482513
cdef _{{Real}}Tensor secondTensor
483514
if isinstance(second, numbers.Number):
484515
TH{{Real}}Tensor_div(self.native, self.native, second)
@@ -614,15 +645,11 @@ def _as{{Real}}Tensor(myarray):
614645
{% endfor %}
615646

616647
cdef class GlobalState(object):
617-
# properties are in the PyTorch.pxd file
618-
619648
def __cinit__(GlobalState self):
620649
pass
621-
# # print('GlobalState.__cinit__')
622650

623651
def __dealloc__(self):
624652
pass
625-
# # print('GlobalState.__dealloc__')
626653

627654
def getLua(self):
628655
return LuaState_fromNative(self.L)

src/PyTorch.pxd

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ cdef extern from "THTensor.h":
2121

2222
cdef class _LongTensor(object):
2323
cdef THLongTensor *native
24+
cdef long *data(self)
25+
# cpdef _LongTensor contiguous(self)
2426
cpdef int dims(self)
2527
cpdef set1d(self, int x0, long value)
2628
cpdef set2d(self, int x0, int x1, long value)
@@ -37,6 +39,8 @@ cdef extern from "THTensor.h":
3739

3840
cdef class _FloatTensor(object):
3941
cdef THFloatTensor *native
42+
cdef float *data(self)
43+
# cpdef _FloatTensor contiguous(self)
4044
cpdef int dims(self)
4145
cpdef set1d(self, int x0, float value)
4246
cpdef set2d(self, int x0, int x1, float value)
@@ -53,6 +57,8 @@ cdef extern from "THTensor.h":
5357

5458
cdef class _DoubleTensor(object):
5559
cdef THDoubleTensor *native
60+
cdef double *data(self)
61+
# cpdef _DoubleTensor contiguous(self)
5662
cpdef int dims(self)
5763
cpdef set1d(self, int x0, double value)
5864
cpdef set2d(self, int x0, int x1, double value)
@@ -69,6 +75,8 @@ cdef extern from "THTensor.h":
6975

7076
cdef class _ByteTensor(object):
7177
cdef THByteTensor *native
78+
cdef unsigned char *data(self)
79+
# cpdef _ByteTensor contiguous(self)
7280
cpdef int dims(self)
7381
cpdef set1d(self, int x0, unsigned char value)
7482
cpdef set2d(self, int x0, int x1, unsigned char value)

0 commit comments

Comments
 (0)