Merge pull request #89 from lucasdelimanogueira/tmp

Tmp
This commit is contained in:
lucasdelimanogueira 2024-06-05 21:09:55 -03:00 committed by GitHub
commit ed391e740c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 55 additions and 36 deletions

Binary file not shown.

Binary file not shown.

View file

@ -5,6 +5,6 @@ from .utils import *
from .norchvision import *
from .utils import *
__version__ = "0.0.5"
__version__ = "0.0.7"
__author__ = 'Lucas de Lima Nogueira'
__credits__ = 'Lucas de Lima Nogueira'

View file

@ -49,37 +49,30 @@ extern "C" {
void delete_tensor(Tensor* tensor) {
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);
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) {
// as strides always are allocated within C code, it must be handled separatedly
if (tensor->strides != NULL) {
free(tensor->strides);
tensor->strides = NULL;
@ -87,7 +80,6 @@ extern "C" {
}
void delete_device(Tensor* tensor) {
// as device always are allocated within C code, it must be handled separatedly
if (tensor->device != NULL) {
free(tensor->device);
tensor->device = NULL;
@ -114,20 +106,28 @@ extern "C" {
int device_id = 0;
char* endptr;
char* target_device_type;
long num = strtol(target_device, &endptr, 10);
if (*endptr == '\0') {
device_id = (int)num;
target_device = new char[strlen("cuda") + 1];
strcpy(target_device, "cuda");
target_device_type = new char[strlen("cuda") + 1];
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);
}
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);
}
free(target_device_type);
}
Tensor* add_tensor(Tensor* tensor1, Tensor* tensor2) {

View file

@ -14,6 +14,8 @@ extern "C" {
Tensor* create_tensor(float* data, int* shape, int ndim, char* device);
void delete_tensor(Tensor* tensor);
void delete_strides(Tensor* tensor);
void delete_shape(Tensor* tensor);
void delete_strides(Tensor* tensor);
void delete_device(Tensor* tensor);
float get_item(Tensor* tensor, int* indices);
Tensor* add_tensor(Tensor* tensor1, Tensor* tensor2);

Binary file not shown.

View file

@ -83,8 +83,7 @@ class Tensor:
def __del__(self):
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.restype = None
Tensor._C.delete_strides(self.tensor)
@ -93,8 +92,26 @@ class Tensor:
Tensor._C.delete_device.restype = None
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:
# 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.restype = None

View file

@ -76,7 +76,7 @@ class CustomInstall(install):
setuptools.setup(
name="norch",
version="0.0.5",
version="0.0.7",
author="Lucas de Lima",
author_email="nogueiralucasdelima@gmail.com",
description="A deep learning framework",