commit
ed391e740c
9 changed files with 55 additions and 36 deletions
BIN
build/cuda.cu.o
BIN
build/cuda.cu.o
Binary file not shown.
BIN
build/tensor.o
BIN
build/tensor.o
Binary file not shown.
|
|
@ -5,6 +5,6 @@ from .utils import *
|
||||||
from .norchvision import *
|
from .norchvision import *
|
||||||
from .utils import *
|
from .utils import *
|
||||||
|
|
||||||
__version__ = "0.0.5"
|
__version__ = "0.0.7"
|
||||||
__author__ = 'Lucas de Lima Nogueira'
|
__author__ = 'Lucas de Lima Nogueira'
|
||||||
__credits__ = 'Lucas de Lima Nogueira'
|
__credits__ = 'Lucas de Lima Nogueira'
|
||||||
Binary file not shown.
|
|
@ -49,37 +49,30 @@ extern "C" {
|
||||||
|
|
||||||
void delete_tensor(Tensor* tensor) {
|
void delete_tensor(Tensor* tensor) {
|
||||||
if (tensor != NULL) {
|
if (tensor != NULL) {
|
||||||
|
|
||||||
if (tensor->shape != NULL) {
|
|
||||||
free(tensor->shape);
|
|
||||||
tensor->shape = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tensor->data != NULL) {
|
|
||||||
if (strcmp(tensor->device, "cpu") == 0) {
|
|
||||||
free(tensor->data);
|
|
||||||
} else {
|
|
||||||
free_cuda(tensor->data);
|
|
||||||
}
|
|
||||||
tensor->data = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tensor->device != NULL) {
|
|
||||||
free(tensor->device);
|
|
||||||
tensor->device = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tensor->strides != NULL) {
|
|
||||||
free(tensor->strides);
|
|
||||||
tensor->strides = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
free(tensor);
|
free(tensor);
|
||||||
|
tensor = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void delete_shape(Tensor* tensor) {
|
||||||
|
if (tensor->shape != NULL) {
|
||||||
|
free(tensor->shape);
|
||||||
|
tensor->shape = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void delete_data(Tensor* tensor) {
|
||||||
|
if (tensor->data != NULL) {
|
||||||
|
if (strcmp(tensor->device, "cpu") == 0) {
|
||||||
|
free(tensor->data);
|
||||||
|
} else {
|
||||||
|
free_cuda(tensor->data);
|
||||||
|
}
|
||||||
|
tensor->data = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void delete_strides(Tensor* tensor) {
|
void delete_strides(Tensor* tensor) {
|
||||||
// as strides always are allocated within C code, it must be handled separatedly
|
|
||||||
if (tensor->strides != NULL) {
|
if (tensor->strides != NULL) {
|
||||||
free(tensor->strides);
|
free(tensor->strides);
|
||||||
tensor->strides = NULL;
|
tensor->strides = NULL;
|
||||||
|
|
@ -87,7 +80,6 @@ extern "C" {
|
||||||
}
|
}
|
||||||
|
|
||||||
void delete_device(Tensor* tensor) {
|
void delete_device(Tensor* tensor) {
|
||||||
// as device always are allocated within C code, it must be handled separatedly
|
|
||||||
if (tensor->device != NULL) {
|
if (tensor->device != NULL) {
|
||||||
free(tensor->device);
|
free(tensor->device);
|
||||||
tensor->device = NULL;
|
tensor->device = NULL;
|
||||||
|
|
@ -114,20 +106,28 @@ extern "C" {
|
||||||
int device_id = 0;
|
int device_id = 0;
|
||||||
char* endptr;
|
char* endptr;
|
||||||
|
|
||||||
|
char* target_device_type;
|
||||||
|
|
||||||
long num = strtol(target_device, &endptr, 10);
|
long num = strtol(target_device, &endptr, 10);
|
||||||
if (*endptr == '\0') {
|
if (*endptr == '\0') {
|
||||||
device_id = (int)num;
|
device_id = (int)num;
|
||||||
target_device = new char[strlen("cuda") + 1];
|
target_device_type = new char[strlen("cuda") + 1];
|
||||||
strcpy(target_device, "cuda");
|
strcpy(target_device_type, "cuda");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
target_device_type = new char[strlen("cuda") + 1];
|
||||||
|
strcpy(target_device_type, "cpu");
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((strcmp(target_device, "cuda") == 0) && (strcmp(tensor->device, "cpu") == 0)) {
|
if ((strcmp(target_device_type, "cuda") == 0) && (strcmp(tensor->device, "cpu") == 0)) {
|
||||||
cpu_to_cuda(tensor, device_id);
|
cpu_to_cuda(tensor, device_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
else if ((strcmp(target_device, "cpu") == 0) && (strcmp(tensor->device, "cuda") == 0)) {
|
else if ((strcmp(target_device_type, "cpu") == 0) && (strcmp(tensor->device, "cuda") == 0)) {
|
||||||
cuda_to_cpu(tensor);
|
cuda_to_cpu(tensor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
free(target_device_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
Tensor* add_tensor(Tensor* tensor1, Tensor* tensor2) {
|
Tensor* add_tensor(Tensor* tensor1, Tensor* tensor2) {
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,8 @@ extern "C" {
|
||||||
Tensor* create_tensor(float* data, int* shape, int ndim, char* device);
|
Tensor* create_tensor(float* data, int* shape, int ndim, char* device);
|
||||||
void delete_tensor(Tensor* tensor);
|
void delete_tensor(Tensor* tensor);
|
||||||
void delete_strides(Tensor* tensor);
|
void delete_strides(Tensor* tensor);
|
||||||
|
void delete_shape(Tensor* tensor);
|
||||||
|
void delete_strides(Tensor* tensor);
|
||||||
void delete_device(Tensor* tensor);
|
void delete_device(Tensor* tensor);
|
||||||
float get_item(Tensor* tensor, int* indices);
|
float get_item(Tensor* tensor, int* indices);
|
||||||
Tensor* add_tensor(Tensor* tensor1, Tensor* tensor2);
|
Tensor* add_tensor(Tensor* tensor1, Tensor* tensor2);
|
||||||
|
|
|
||||||
Binary file not shown.
|
|
@ -83,8 +83,7 @@ class Tensor:
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
|
|
||||||
if hasattr(self, '_data_ctype') and self._data_ctype is not None:
|
if hasattr(self, '_data_ctype') and self._data_ctype is not None:
|
||||||
# tensor created by user (ctypes) will be deleted by python garbage collector
|
|
||||||
# only strides need to be deallocated manually because it is created inside C code
|
|
||||||
Tensor._C.delete_strides.argtypes = [ctypes.POINTER(CTensor)]
|
Tensor._C.delete_strides.argtypes = [ctypes.POINTER(CTensor)]
|
||||||
Tensor._C.delete_strides.restype = None
|
Tensor._C.delete_strides.restype = None
|
||||||
Tensor._C.delete_strides(self.tensor)
|
Tensor._C.delete_strides(self.tensor)
|
||||||
|
|
@ -93,8 +92,26 @@ class Tensor:
|
||||||
Tensor._C.delete_device.restype = None
|
Tensor._C.delete_device.restype = None
|
||||||
Tensor._C.delete_device(self.tensor)
|
Tensor._C.delete_device(self.tensor)
|
||||||
|
|
||||||
|
Tensor._C.delete_tensor.argtypes = [ctypes.POINTER(CTensor)]
|
||||||
|
Tensor._C.delete_tensor.restype = None
|
||||||
|
Tensor._C.delete_tensor(self.tensor)
|
||||||
|
|
||||||
elif self.tensor is not None:
|
elif self.tensor is not None:
|
||||||
# tensor created during operations must be deallocated
|
Tensor._C.delete_strides.argtypes = [ctypes.POINTER(CTensor)]
|
||||||
|
Tensor._C.delete_strides.restype = None
|
||||||
|
Tensor._C.delete_strides(self.tensor)
|
||||||
|
|
||||||
|
Tensor._C.delete_data.argtypes = [ctypes.POINTER(CTensor)]
|
||||||
|
Tensor._C.delete_data.restype = None
|
||||||
|
Tensor._C.delete_data(self.tensor)
|
||||||
|
|
||||||
|
Tensor._C.delete_shape.argtypes = [ctypes.POINTER(CTensor)]
|
||||||
|
Tensor._C.delete_shape.restype = None
|
||||||
|
Tensor._C.delete_shape(self.tensor)
|
||||||
|
|
||||||
|
Tensor._C.delete_device.argtypes = [ctypes.POINTER(CTensor)]
|
||||||
|
Tensor._C.delete_device.restype = None
|
||||||
|
Tensor._C.delete_device(self.tensor)
|
||||||
|
|
||||||
Tensor._C.delete_tensor.argtypes = [ctypes.POINTER(CTensor)]
|
Tensor._C.delete_tensor.argtypes = [ctypes.POINTER(CTensor)]
|
||||||
Tensor._C.delete_tensor.restype = None
|
Tensor._C.delete_tensor.restype = None
|
||||||
|
|
|
||||||
2
setup.py
2
setup.py
|
|
@ -76,7 +76,7 @@ class CustomInstall(install):
|
||||||
|
|
||||||
setuptools.setup(
|
setuptools.setup(
|
||||||
name="norch",
|
name="norch",
|
||||||
version="0.0.5",
|
version="0.0.7",
|
||||||
author="Lucas de Lima",
|
author="Lucas de Lima",
|
||||||
author_email="nogueiralucasdelima@gmail.com",
|
author_email="nogueiralucasdelima@gmail.com",
|
||||||
description="A deep learning framework",
|
description="A deep learning framework",
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue