Merge pull request #80 from lucasdelimanogueira/tmp

Tmp
This commit is contained in:
lucasdelimanogueira 2024-05-31 17:16:13 -03:00 committed by GitHub
commit 5178aeabf7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 23 additions and 18 deletions

View file

@ -7,7 +7,7 @@ Project details explanations can also be found on [medium](https://towardsdatasc
**PyNorch** is a deep learning framework constructed using C/C++, CUDA and Python. This is a personal project with educational purpose only! `Norch` means **NOT** PyTorch, and we have **NO** claims to rivaling the already established PyTorch. The main objective of **PyNorch** was to give a brief understanding of how a deep learning framework works internally. It implements the Tensor object, GPU support and an automatic differentiation system.
# 2 - Installation
Install this package from PyPi (you can test on Colab!)
Install this package from PyPi (you can test on Colab! Also tested on AWS p3 instances ami-00f1d513c2bb78c75)
```css
$ pip install norch
@ -15,11 +15,9 @@ $ pip install norch
or from cloning this repository
```css
$ sudo apt install nvidia-cuda-toolkit
$ git clone https://github.com/lucasdelimanogueira/PyNorch.git
$ cd build
$ make
$ cd ..
$ cd PyNorch
$ pip install . -v
```
# 3 - Get started
@ -66,7 +64,7 @@ class MyModel(nn.Module):
```python
import norch
from norch.utils.data.dataloader import DataLoader
from norch.norchvision import transforms
from norch.norchvision import transforms as T
import norch
import norch.nn as nn
import norch.optim as optim
@ -77,16 +75,16 @@ BATCH_SIZE = 32
device = "cuda" #cpu
epochs = 10
transform = transforms.Compose(
transform = T.Compose(
[
transforms.ToTensor(),
transforms.Reshape([-1, 784, 1])
T.ToTensor(),
T.Reshape([-1, 784, 1])
]
)
target_transform = transforms.Compose(
target_transform = T.Compose(
[
transforms.ToTensor()
T.ToTensor()
]
)

View file

@ -13,7 +13,7 @@ CUDAFLAGS = -arch=sm_75
CUDALIBS = -I/usr/local/cuda/include -L/usr/local/cuda/lib64 -lcudart -lcuda
# MPI flags and libraries
DISTRIBUTEDLIBS = -lmpi_cxx -lnccl
DISTRIBUTEDLIBS = -lmpi_cxx -lnccl -lmpi
# Directories
SRCDIR = ../norch/csrc

View file

@ -34,7 +34,7 @@
"import norch.nn as nn\n",
"import norch.optim as optim\n",
"from norch.utils.data.dataloader import DataLoader\n",
"from norch.norchvision import transforms\n",
"from norch.norchvision import transforms as T\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import random\n",
@ -102,16 +102,16 @@
"device = \"cuda\" #cpu\n",
"epochs = 10\n",
"\n",
"transform = transforms.Compose(\n",
"transform = T.Compose(\n",
" [\n",
" transforms.ToTensor(),\n",
" transforms.Reshape([-1, 784, 1])\n",
" T.ToTensor(),\n",
" T.Reshape([-1, 784, 1])\n",
" ]\n",
")\n",
"\n",
"target_transform = transforms.Sequential(\n",
"target_transform = T.Compose(\n",
" [\n",
" transforms.ToTensor()\n",
" T.ToTensor()\n",
" ]\n",
")\n",
"\n",

View file

@ -66,9 +66,12 @@ extern "C" {
void to_device(Tensor* tensor, char* target_device) {
int device_id = 0;
char* endptr;
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");
}
if ((strcmp(target_device, "cuda") == 0) && (strcmp(tensor->device, "cpu") == 0)) {
@ -78,6 +81,10 @@ extern "C" {
else if ((strcmp(target_device, "cpu") == 0) && (strcmp(tensor->device, "cuda") == 0)) {
cuda_to_cpu(tensor);
}
else {
printf("Could not send tensor to device %d", device_id);
}
}
Tensor* add_tensor(Tensor* tensor1, Tensor* tensor2) {