Refactor generic/Tensor.c and add Short objects

This commit is contained in:
Adam Paszke
2016-05-03 21:20:35 +02:00
parent d9ea7e6211
commit f4b3554d9e
3 changed files with 33 additions and 12 deletions

View File

@ -6,7 +6,7 @@ from setuptools import setup, Extension
in_init = "torch/__init__.py.in"
out_init = "torch/__init__.py"
templates = ["torch/Tensor.py", "torch/Storage.py"]
types = ['Double', 'Float', 'Long', 'Int', 'Char', 'Byte']
types = ['Double', 'Float', 'Long', 'Int', 'Short', 'Char', 'Byte']
generated = ''
for template in templates:

View File

@ -28,6 +28,7 @@ PyMODINIT_FUNC PyInit_C()
ASSERT_TRUE(THPFloatStorage_init(m));
ASSERT_TRUE(THPLongStorage_init(m));
ASSERT_TRUE(THPIntStorage_init(m));
ASSERT_TRUE(THPShortStorage_init(m));
ASSERT_TRUE(THPCharStorage_init(m));
ASSERT_TRUE(THPByteStorage_init(m));
@ -35,6 +36,7 @@ PyMODINIT_FUNC PyInit_C()
ASSERT_TRUE(THPFloatTensor_init(m));
ASSERT_TRUE(THPLongTensor_init(m));
ASSERT_TRUE(THPIntTensor_init(m));
ASSERT_TRUE(THPShortTensor_init(m));
ASSERT_TRUE(THPCharTensor_init(m));
ASSERT_TRUE(THPByteTensor_init(m));

View File

@ -2,20 +2,42 @@
#define TH_GENERIC_FILE "generic/Tensor.c"
#else
#define GET_SELF THPTensor *self = (THPTensor*)_self;
typedef struct {
PyObject_HEAD
THTensor *cdata;
} THPTensor;
////////////////////////////////////////////////////////////////////////////////
// HELPERS
////////////////////////////////////////////////////////////////////////////////
static PyTypeObject THPTensorType;
static bool THPTensor_(IsSubclass)(PyObject *tensor)
{
return PyObject_IsSubclass((PyObject*)Py_TYPE(tensor), (PyObject*)&THPTensorType);
}
////////////////////////////////////////////////////////////////////////////////
// TH WRAPPERS
////////////////////////////////////////////////////////////////////////////////
static PyObject * THPTensor_(size)(THPTensor *self)
{
THLongStorage *size = THTensor_(newSizeOf)(self->cdata);
return THPLongStorage_newObject(size);
}
////////////////////////////////////////////////////////////////////////////////
// PYTHON METHODS
////////////////////////////////////////////////////////////////////////////////
static void THPTensor_(dealloc)(THPTensor* self)
{
THTensor_(free)(self->cdata);
Py_TYPE(self)->tp_free((PyObject*)self);
}
static PyObject * THPTensor_(new)(PyTypeObject *type, PyObject *args, PyObject *kwargs)
static PyObject * THPTensor_(pynew)(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
long long sizes[] = {-1, -1, -1, -1};
if (!PyArg_ParseTuple(args, "|LLLL", &sizes[0], &sizes[1], &sizes[2], &sizes[3]))
@ -32,15 +54,12 @@ static PyObject * THPTensor_(new)(PyTypeObject *type, PyObject *args, PyObject *
return (PyObject *)self;
}
static PyObject * THPTensor_(size)(PyObject *_self)
{
GET_SELF;
THLongStorage *size = THTensor_(newSizeOf)(self->cdata);
return THPLongStorage_newObject(size);
}
////////////////////////////////////////////////////////////////////////////////
// PYTHON DECLARATIONS
////////////////////////////////////////////////////////////////////////////////
static struct PyMemberDef THPTensor_(members)[] = {
{"_cdata", T_ULONGLONG, offsetof(THPTensor, cdata), 0, "C struct pointer"},
{"_cdata", T_ULONGLONG, offsetof(THPTensor, cdata), READONLY, "C struct pointer"},
{NULL}
};
@ -88,7 +107,7 @@ static PyTypeObject THPTensorType = {
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
THPTensor_(new), /* tp_new */
THPTensor_(pynew), /* tp_new */
};
bool THPTensor_(init)(PyObject *module)