del tensor free memory

This commit is contained in:
lucasdelimanogueira 2024-06-05 14:31:48 -03:00
parent b56ed81cd7
commit 6701179c22
14 changed files with 61 additions and 14 deletions

View file

@ -29,7 +29,7 @@ x1 = norch.Tensor([[1, 2],
[3, 4]], requires_grad=True).to("cuda") [3, 4]], requires_grad=True).to("cuda")
x2 = norch.Tensor([[4, 3], x2 = norch.Tensor([[4, 3],
[2, 1]], requires_grad=True).to("cuda) [2, 1]], requires_grad=True).to("cuda")
x3 = x1 @ x2 x3 = x1 @ x2
result = x3.sum() result = x3.sum()

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -42,6 +42,10 @@ __host__ void cuda_to_cpu(Tensor* tensor) {
strcpy(tensor->device, device_str); strcpy(tensor->device, device_str);
} }
__host__ void free_cuda(float* data) {
cudaFree(data);
}
__global__ void add_tensor_cuda_kernel(float* data1, float* data2, float* result_data, int size) { __global__ void add_tensor_cuda_kernel(float* data1, float* data2, float* result_data, int size) {
int i = blockIdx.x * blockDim.x + threadIdx.x; int i = blockIdx.x * blockDim.x + threadIdx.x;

View file

@ -4,6 +4,7 @@
__host__ void cpu_to_cuda(Tensor* tensor, int device_id); __host__ void cpu_to_cuda(Tensor* tensor, int device_id);
__host__ void cuda_to_cpu(Tensor* tensor); __host__ void cuda_to_cpu(Tensor* tensor);
__host__ void free_cuda(float* data);
__global__ void add_tensor_cuda_kernel(float* data1, float* data2, float* result_data, int size); __global__ void add_tensor_cuda_kernel(float* data1, float* data2, float* result_data, int size);
__host__ void add_tensor_cuda(Tensor* tensor1, Tensor* tensor2, float* result_data); __host__ void add_tensor_cuda(Tensor* tensor1, Tensor* tensor2, float* result_data);

View file

@ -16,28 +16,27 @@ extern "C" {
fprintf(stderr, "Memory allocation failed\n"); fprintf(stderr, "Memory allocation failed\n");
exit(1); exit(1);
} }
tensor->data = data;
tensor->shape = shape;
tensor->ndim = ndim; tensor->ndim = ndim;
tensor->device = (char*)malloc(strlen(device) + 1);
if (device != NULL) {
strcpy(tensor->device, device);
} else {
fprintf(stderr, "Memory allocation failed\n");
exit(-1);
}
tensor->size = 1; tensor->size = 1;
for (int i = 0; i < ndim; i++) { for (int i = 0; i < ndim; i++) {
tensor->size *= shape[i]; tensor->size *= shape[i];
} }
tensor->device = strdup(device);
tensor->strides = (int*)malloc(ndim * sizeof(int)); tensor->strides = (int*)malloc(ndim * sizeof(int));
if (tensor->strides == NULL) { tensor->shape = (int*)malloc(ndim * sizeof(int));
tensor->data = (float*)malloc(tensor->size * sizeof(float));
if (tensor->device == NULL || tensor->strides == NULL || tensor->shape == NULL || tensor->data == NULL) {
fprintf(stderr, "Memory allocation failed\n"); fprintf(stderr, "Memory allocation failed\n");
exit(1); exit(1);
} }
memcpy(tensor->shape, shape, tensor->ndim * sizeof(int));
strcpy(tensor->device, device);
memcpy(tensor->data, data, tensor->size * sizeof(float));
int stride = 1; int stride = 1;
for (int i = ndim - 1; i >= 0; i--) { for (int i = ndim - 1; i >= 0; i--) {
tensor->strides[i] = stride; tensor->strides[i] = stride;
@ -47,6 +46,43 @@ extern "C" {
return tensor; return tensor;
} }
void delete_tensor(Tensor* tensor) {
if (tensor != NULL) {
if (tensor->strides != NULL) {
free(tensor->strides);
tensor->strides = 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);
}
}
float get_item(Tensor* tensor, int* indices) { float get_item(Tensor* tensor, int* indices) {
int index = 0; int index = 0;
for (int i = 0; i < tensor->ndim; i++) { for (int i = 0; i < tensor->ndim; i++) {

View file

@ -5,8 +5,6 @@ typedef struct {
float* data; float* data;
int* strides; int* strides;
int* shape; int* shape;
int* strides_cuda;
int* shape_cuda;
int ndim; int ndim;
int size; int size;
char* device; char* device;
@ -14,6 +12,7 @@ typedef struct {
extern "C" { 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);
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);
Tensor* sum_tensor(Tensor* tensor, int axis, bool keepdims); Tensor* sum_tensor(Tensor* tensor, int axis, bool keepdims);

Binary file not shown.

View file

@ -26,3 +26,4 @@ class SGD(Optimizer):
parameter.detach() parameter.detach()
velocity.detach() velocity.detach()
del parameter

View file

@ -81,6 +81,12 @@ class Tensor:
flat_data, shape = flatten_recursively(nested_list) flat_data, shape = flatten_recursively(nested_list)
return flat_data, shape return flat_data, shape
def __del__(self):
if self.tensor is not None:
Tensor._C.delete_tensor.argtypes = [ctypes.POINTER(CTensor)]
Tensor._C.delete_tensor.restype = None
Tensor._C.delete_tensor(self.tensor)
def __setattr__(self, name, value): def __setattr__(self, name, value):
if name == 'grad': if name == 'grad':
for hook in self.hooks: for hook in self.hooks: