Merge pull request #88 from lucasdelimanogueira/tmp

Tmp
This commit is contained in:
lucasdelimanogueira 2024-06-05 20:25:50 -03:00 committed by GitHub
commit f0f8641d54
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 372 additions and 559 deletions

Binary file not shown.

Binary file not shown.

View file

@ -24,9 +24,8 @@ __host__ void cpu_to_cuda(Tensor* tensor, int device_id) {
tensor->data = data_tmp;
const char* device_str = "cuda";
tensor->device = (char*)malloc(strlen(device_str) + 1);
strcpy(tensor->device, device_str);
tensor->device = (char*)malloc(strlen("cuda") + 1);
strcpy(tensor->device, "cuda");
}
__host__ void cuda_to_cpu(Tensor* tensor) {
@ -37,9 +36,8 @@ __host__ void cuda_to_cpu(Tensor* tensor) {
tensor->data = data_tmp;
const char* device_str = "cpu";
tensor->device = (char*)malloc(strlen(device_str) + 1);
strcpy(tensor->device, device_str);
tensor->device = (char*)malloc(strlen("cpu") + 1);
strcpy(tensor->device, "cpu");
}
__host__ void free_cuda(float* data) {

File diff suppressed because it is too large Load diff

View file

@ -13,6 +13,8 @@ typedef struct {
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_device(Tensor* tensor);
float get_item(Tensor* tensor, int* indices);
Tensor* add_tensor(Tensor* tensor1, Tensor* tensor2);
Tensor* sum_tensor(Tensor* tensor, int axis, bool keepdims);

Binary file not shown.

View file

@ -11,7 +11,7 @@ class Loss(Module, ABC):
def forward(self, predictions, labels):
raise NotImplementedError
def __call__(self, *inputs):
return self.forward(*inputs)

View file

@ -9,6 +9,7 @@ class SGD(Optimizer):
self.momentum = momentum
self._cache = {'velocity': [p.zeros_like() for (_, _, p) in self.parameters]}
def step(self):
for i, (module, name, _) in enumerate(self.parameters):
parameter = getattr(module, name)

View file

@ -26,10 +26,10 @@ class Tensor:
self.shape = shape.copy()
self.data_ctype = (ctypes.c_float * len(data))(*data.copy())
self.shape_ctype = (ctypes.c_int * len(shape))(*shape.copy())
self.ndim_ctype = ctypes.c_int(len(shape))
self.device_ctype = device.encode('utf-8')
self._data_ctype = (ctypes.c_float * len(data))(*data.copy())
self._shape_ctype = (ctypes.c_int * len(shape))(*shape.copy())
self._ndim_ctype = ctypes.c_int(len(shape))
self._device_ctype = device.encode('utf-8')
self.ndim = len(shape)
self.device = device
@ -47,15 +47,11 @@ class Tensor:
Tensor._C.create_tensor.restype = ctypes.POINTER(CTensor)
self.tensor = Tensor._C.create_tensor(
self.data_ctype,
self.shape_ctype,
self.ndim_ctype,
self.device_ctype
self._data_ctype,
self._shape_ctype,
self._ndim_ctype,
self._device_ctype
)
del self.data_ctype
del self.shape_ctype
del self.device_ctype
else:
self.tensor = None,
@ -85,7 +81,21 @@ class Tensor:
return flat_data, shape
def __del__(self):
if self.tensor 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.restype = None
Tensor._C.delete_strides(self.tensor)
Tensor._C.delete_device.argtypes = [ctypes.POINTER(CTensor)]
Tensor._C.delete_device.restype = None
Tensor._C.delete_device(self.tensor)
elif self.tensor is not None:
# tensor created during operations must be deallocated
Tensor._C.delete_tensor.argtypes = [ctypes.POINTER(CTensor)]
Tensor._C.delete_tensor.restype = None
Tensor._C.delete_tensor(self.tensor)