mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-20 21:14:14 +08:00
Initial commit
This commit is contained in:
16
torch/__init__.py
Normal file
16
torch/__init__.py
Normal file
@ -0,0 +1,16 @@
|
||||
import torch.C
|
||||
|
||||
class FloatTensor(C.FloatTensorBase):
|
||||
def __str__(self):
|
||||
return "Tensor"
|
||||
|
||||
def __repr__(self):
|
||||
return str(self)
|
||||
|
||||
class LongStorage(C.LongStorageBase):
|
||||
def __str__(self):
|
||||
content = ' ' + '\n '.join(str(self[i]) for i in range(len(self)))
|
||||
return content + '\n[torch.LongStorage of size {}]'.format(len(self))
|
||||
|
||||
def __repr__(self):
|
||||
return str(self)
|
42
torch/csrc/Module.c
Normal file
42
torch/csrc/Module.c
Normal file
@ -0,0 +1,42 @@
|
||||
#include <Python.h>
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <TH/TH.h>
|
||||
|
||||
#include "THP.h"
|
||||
|
||||
#define ASSERT_TRUE(cmd) if (!(cmd)) return NULL
|
||||
|
||||
static PyMethodDef TorchMethods[] = {
|
||||
{NULL, NULL, 0, NULL}
|
||||
};
|
||||
|
||||
static struct PyModuleDef torchmodule = {
|
||||
PyModuleDef_HEAD_INIT,
|
||||
"torch.C",
|
||||
NULL,
|
||||
-1,
|
||||
TorchMethods
|
||||
};
|
||||
|
||||
PyMODINIT_FUNC PyInit_C()
|
||||
{
|
||||
PyObject* m;
|
||||
ASSERT_TRUE(m = PyModule_Create(&torchmodule));
|
||||
|
||||
ASSERT_TRUE(THPDoubleStorage_init(m));
|
||||
ASSERT_TRUE(THPFloatStorage_init(m));
|
||||
ASSERT_TRUE(THPLongStorage_init(m));
|
||||
ASSERT_TRUE(THPIntStorage_init(m));
|
||||
ASSERT_TRUE(THPCharStorage_init(m));
|
||||
ASSERT_TRUE(THPByteStorage_init(m));
|
||||
|
||||
ASSERT_TRUE(THPDoubleTensor_init(m));
|
||||
ASSERT_TRUE(THPFloatTensor_init(m));
|
||||
ASSERT_TRUE(THPLongTensor_init(m));
|
||||
ASSERT_TRUE(THPIntTensor_init(m));
|
||||
ASSERT_TRUE(THPCharTensor_init(m));
|
||||
ASSERT_TRUE(THPByteTensor_init(m));
|
||||
|
||||
return m;
|
||||
}
|
11
torch/csrc/Storage.c
Normal file
11
torch/csrc/Storage.c
Normal file
@ -0,0 +1,11 @@
|
||||
#include <Python.h>
|
||||
#include <structmember.h>
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <TH/TH.h>
|
||||
#include "THP.h"
|
||||
#include "Storage.h"
|
||||
|
||||
#include "generic/Storage.c"
|
||||
#include <TH/THGenerateAllTypes.h>
|
||||
|
14
torch/csrc/Storage.h
Normal file
14
torch/csrc/Storage.h
Normal file
@ -0,0 +1,14 @@
|
||||
#ifndef THP_STORAGE_INC
|
||||
#define THP_STORAGE_INC
|
||||
|
||||
#define THPStorage_(NAME) TH_CONCAT_4(THP,Real,Storage_,NAME)
|
||||
#define THPStorage TH_CONCAT_3(THP,Real,Storage)
|
||||
#define THPStorageType TH_CONCAT_3(THP,Real,StorageType)
|
||||
#define THPStorageBaseStr TH_CONCAT_STRING_2(Real,StorageBase)
|
||||
#define THPStorageStr TH_CONCAT_STRING_2(Real,Storage)
|
||||
#define THPStorageClass TH_CONCAT_3(THP,Real,StorageClass)
|
||||
|
||||
#include "generic/Storage.h"
|
||||
#include <TH/THGenerateAllTypes.h>
|
||||
|
||||
#endif
|
2
torch/csrc/THP.h
Normal file
2
torch/csrc/THP.h
Normal file
@ -0,0 +1,2 @@
|
||||
#include "Storage.h"
|
||||
#include "Tensor.h"
|
9
torch/csrc/Tensor.c
Normal file
9
torch/csrc/Tensor.c
Normal file
@ -0,0 +1,9 @@
|
||||
#include <Python.h>
|
||||
#include <structmember.h>
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <TH/TH.h>
|
||||
#include "THP.h"
|
||||
|
||||
#include "generic/Tensor.c"
|
||||
#include <TH/THGenerateAllTypes.h>
|
12
torch/csrc/Tensor.h
Normal file
12
torch/csrc/Tensor.h
Normal file
@ -0,0 +1,12 @@
|
||||
#ifndef THP_TENSOR_INC
|
||||
#define THP_TENSOR_INC
|
||||
|
||||
#define THPTensor_(NAME) TH_CONCAT_4(THP,Real,Tensor_,NAME)
|
||||
#define THPTensor TH_CONCAT_3(THP,Real,Tensor)
|
||||
#define THPTensorType TH_CONCAT_3(THP,Real,TensorType)
|
||||
#define THPTensorBaseStr TH_CONCAT_STRING_2(Real,TensorBase)
|
||||
|
||||
#include "generic/Tensor.h"
|
||||
#include <TH/THGenerateAllTypes.h>
|
||||
|
||||
#endif
|
149
torch/csrc/generic/Storage.c
Normal file
149
torch/csrc/generic/Storage.c
Normal file
@ -0,0 +1,149 @@
|
||||
#ifndef TH_GENERIC_FILE
|
||||
#define TH_GENERIC_FILE "generic/Storage.c"
|
||||
#else
|
||||
|
||||
#define GET_SELF THPStorage *self = (THPStorage *)_self
|
||||
|
||||
static PyObject *THPStorageClass = NULL;
|
||||
|
||||
static void THPStorage_(getClass)()
|
||||
{
|
||||
// TODO: error checking
|
||||
if (THPStorageClass)
|
||||
return;
|
||||
PyObject *torch_module = PyImport_ImportModule("torch");
|
||||
PyObject* module_dict = PyModule_GetDict(torch_module);
|
||||
THPStorageClass = PyMapping_GetItemString(module_dict, THPStorageStr);
|
||||
}
|
||||
|
||||
PyObject * THPStorage_(newObject)(THStorage *ptr)
|
||||
{
|
||||
// TODO: error checking
|
||||
THPStorage_(getClass)();
|
||||
PyObject *args = PyTuple_New(0);
|
||||
PyObject *kwargs = Py_BuildValue("{s:N}", "cdata", PyLong_FromVoidPtr(ptr));
|
||||
PyObject *instance = PyObject_Call(THPStorageClass, args, kwargs);
|
||||
Py_DECREF(args);
|
||||
Py_DECREF(kwargs);
|
||||
return instance;
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
THStorage *cdata;
|
||||
} THPStorage;
|
||||
|
||||
static void THPStorage_(dealloc)(THPStorage* self)
|
||||
{
|
||||
THStorage_(free)(self->cdata);
|
||||
Py_TYPE(self)->tp_free((PyObject*)self);
|
||||
}
|
||||
|
||||
static PyObject * THPStorage_(new)(PyTypeObject *type, PyObject *args, PyObject *kwargs)
|
||||
{
|
||||
static char *keywords[] = {"cdata"};
|
||||
PyObject *pointer_obj;
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O!", keywords, &PyLong_Type, &pointer_obj))
|
||||
return NULL;
|
||||
|
||||
THPStorage *self = (THPStorage *)type->tp_alloc(type, 0);
|
||||
if (self != NULL) {
|
||||
self->cdata = pointer_obj ? PyLong_AsVoidPtr(pointer_obj) : THLongStorage_new();
|
||||
if (self->cdata == NULL) {
|
||||
Py_DECREF(self);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
return (PyObject *)self;
|
||||
}
|
||||
|
||||
static Py_ssize_t THPStorage_(length)(PyObject *_self)
|
||||
{
|
||||
GET_SELF;
|
||||
return THStorage_(size)(self->cdata);
|
||||
}
|
||||
|
||||
static PyObject * THPStorage_(get)(PyObject *_self, PyObject *index)
|
||||
{
|
||||
GET_SELF;
|
||||
if (!PyLong_Check(index))
|
||||
return NULL;
|
||||
size_t nindex = PyLong_AsSize_t(index);
|
||||
#if defined(TH_REAL_IS_FLOAT) || defined(TH_REAL_IS_DOUBLE)
|
||||
return PyFloat_FromDouble(THStorage_(get)(self->cdata, nindex));
|
||||
#else
|
||||
return PyLong_FromLong(THStorage_(get)(self->cdata, nindex));
|
||||
#endif
|
||||
}
|
||||
|
||||
static int THPStorage_(set)(PyObject *_self, PyObject *index, PyObject *value)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
static struct PyMemberDef THPStorage_(members)[] = {
|
||||
{"_cdata", T_ULONGLONG, offsetof(THPStorage, cdata), 0, "C struct pointer"},
|
||||
{NULL}
|
||||
};
|
||||
|
||||
static PyMethodDef THPStorage_(methods)[] = {
|
||||
{NULL}
|
||||
};
|
||||
|
||||
static PyMappingMethods THPStorage_(mappingmethods) = {
|
||||
THPStorage_(length),
|
||||
THPStorage_(get),
|
||||
THPStorage_(set)
|
||||
};
|
||||
|
||||
static PyTypeObject THPStorageType = {
|
||||
PyVarObject_HEAD_INIT(NULL, 0)
|
||||
"torch.C." THPStorageBaseStr, /* tp_name */
|
||||
sizeof(THPStorage), /* tp_basicsize */
|
||||
0, /* tp_itemsize */
|
||||
(destructor)THPStorage_(dealloc), /* tp_dealloc */
|
||||
0, /* tp_print */
|
||||
0, /* tp_getattr */
|
||||
0, /* tp_setattr */
|
||||
0, /* tp_reserved */
|
||||
0, /* tp_repr */
|
||||
0, /* tp_as_number */
|
||||
0, /* tp_as_sequence */
|
||||
&THPStorage_(mappingmethods), /* tp_as_mapping */
|
||||
0, /* tp_hash */
|
||||
0, /* tp_call */
|
||||
0, /* tp_str */
|
||||
0, /* tp_getattro */
|
||||
0, /* tp_setattro */
|
||||
0, /* tp_as_buffer */
|
||||
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
|
||||
NULL, /* tp_doc */
|
||||
0, /* tp_traverse */
|
||||
0, /* tp_clear */
|
||||
0, /* tp_richcompare */
|
||||
0, /* tp_weaklistoffset */
|
||||
0, /* tp_iter */
|
||||
0, /* tp_iternext */
|
||||
THPStorage_(methods), /* tp_methods */
|
||||
THPStorage_(members), /* tp_members */
|
||||
0, /* tp_getset */
|
||||
0, /* tp_base */
|
||||
0, /* tp_dict */
|
||||
0, /* tp_descr_get */
|
||||
0, /* tp_descr_set */
|
||||
0, /* tp_dictoffset */
|
||||
0, /* tp_init */
|
||||
0, /* tp_alloc */
|
||||
THPStorage_(new), /* tp_new */
|
||||
};
|
||||
|
||||
bool THPStorage_(init)(PyObject *module)
|
||||
{
|
||||
if (PyType_Ready(&THPStorageType) < 0)
|
||||
return false;
|
||||
Py_INCREF(&THPStorageType);
|
||||
PyModule_AddObject(module, THPStorageBaseStr, (PyObject *)&THPStorageType);
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif
|
8
torch/csrc/generic/Storage.h
Normal file
8
torch/csrc/generic/Storage.h
Normal file
@ -0,0 +1,8 @@
|
||||
#ifndef TH_GENERIC_FILE
|
||||
#define TH_GENERIC_FILE "generic/Storage.h"
|
||||
#else
|
||||
|
||||
bool THPStorage_(init)(PyObject *module);
|
||||
PyObject * THPStorage_(newObject)(THStorage *storage);
|
||||
|
||||
#endif
|
103
torch/csrc/generic/Tensor.c
Normal file
103
torch/csrc/generic/Tensor.c
Normal file
@ -0,0 +1,103 @@
|
||||
#ifndef TH_GENERIC_FILE
|
||||
#define TH_GENERIC_FILE "generic/Tensor.c"
|
||||
#else
|
||||
|
||||
#define GET_SELF THPTensor *self = (THPTensor*)_self;
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
THTensor *cdata;
|
||||
} THPTensor;
|
||||
|
||||
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)
|
||||
{
|
||||
long long sizes[] = {-1, -1, -1, -1};
|
||||
if (!PyArg_ParseTuple(args, "|LLLL", &sizes[0], &sizes[1], &sizes[2], &sizes[3]))
|
||||
return NULL;
|
||||
|
||||
THPTensor *self = (THPTensor *)type->tp_alloc(type, 0);
|
||||
if (self != NULL) {
|
||||
self->cdata = THTensor_(newWithSize4d)(sizes[0], sizes[1], sizes[2], sizes[3]);
|
||||
if (self->cdata == NULL) {
|
||||
Py_DECREF(self);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
return (PyObject *)self;
|
||||
}
|
||||
|
||||
static PyObject * THPTensor_(size)(PyObject *_self)
|
||||
{
|
||||
GET_SELF;
|
||||
THLongStorage *size = THTensor_(newSizeOf)(self->cdata);
|
||||
return THPLongStorage_newObject(size);
|
||||
}
|
||||
|
||||
static struct PyMemberDef THPTensor_(members)[] = {
|
||||
{"_cdata", T_ULONGLONG, offsetof(THPTensor, cdata), 0, "C struct pointer"},
|
||||
{NULL}
|
||||
};
|
||||
|
||||
static PyMethodDef THPTensor_(methods)[] = {
|
||||
{"size", (PyCFunction)THPTensor_(size), METH_NOARGS, NULL},
|
||||
{NULL}
|
||||
};
|
||||
|
||||
|
||||
static PyTypeObject THPTensorType = {
|
||||
PyVarObject_HEAD_INIT(NULL, 0)
|
||||
"torch.C." THPTensorBaseStr, /* tp_name */
|
||||
sizeof(THPTensor), /* tp_basicsize */
|
||||
0, /* tp_itemsize */
|
||||
(destructor)THPTensor_(dealloc), /* tp_dealloc */
|
||||
0, /* tp_print */
|
||||
0, /* tp_getattr */
|
||||
0, /* tp_setattr */
|
||||
0, /* tp_reserved */
|
||||
0, /* tp_repr */
|
||||
0, /* tp_as_number */
|
||||
0, /* tp_as_sequence */
|
||||
0, /* tp_as_mapping */
|
||||
0, /* tp_hash */
|
||||
0, /* tp_call */
|
||||
0, /* tp_str */
|
||||
0, /* tp_getattro */
|
||||
0, /* tp_setattro */
|
||||
0, /* tp_as_buffer */
|
||||
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
|
||||
NULL, /* tp_doc */
|
||||
0, /* tp_traverse */
|
||||
0, /* tp_clear */
|
||||
0, /* tp_richcompare */
|
||||
0, /* tp_weaklistoffset */
|
||||
0, /* tp_iter */
|
||||
0, /* tp_iternext */
|
||||
THPTensor_(methods), /* tp_methods */
|
||||
THPTensor_(members), /* tp_members */
|
||||
0, /* tp_getset */
|
||||
0, /* tp_base */
|
||||
0, /* tp_dict */
|
||||
0, /* tp_descr_get */
|
||||
0, /* tp_descr_set */
|
||||
0, /* tp_dictoffset */
|
||||
0, /* tp_init */
|
||||
0, /* tp_alloc */
|
||||
THPTensor_(new), /* tp_new */
|
||||
};
|
||||
|
||||
bool THPTensor_(init)(PyObject *module)
|
||||
{
|
||||
if (PyType_Ready(&THPTensorType) < 0)
|
||||
return false;
|
||||
Py_INCREF(&THPTensorType);
|
||||
PyModule_AddObject(module, THPTensorBaseStr, (PyObject *)&THPTensorType);
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif
|
7
torch/csrc/generic/Tensor.h
Normal file
7
torch/csrc/generic/Tensor.h
Normal file
@ -0,0 +1,7 @@
|
||||
#ifndef TH_GENERIC_FILE
|
||||
#define TH_GENERIC_FILE "generic/Tensor.h"
|
||||
#else
|
||||
|
||||
bool THPTensor_(init)(PyObject *module);
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user