Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to create a tensor in a custom python function within define_graph #5546

Open
1 task done
rachelglenn opened this issue Jul 3, 2024 · 4 comments
Open
1 task done
Assignees
Labels
question Further information is requested

Comments

@rachelglenn
Copy link

Describe the question.

How do create new torch tensors and have them go to the correct device. I would like to do things like taking the square of the tensor? I found this example:
https://docs.nvidia.com/deeplearning/dali/archives/dali_1_18_0/user-guide/docs/examples/custom_operations/python_operator.html

def edit_images(image1, image2):
    assert image1.shape == image2.shape
    for i in range(c):
        h, w, c = image1.shape
        perturbation = torch.rand(h, w) 
        new_image1 = torch.zeros(h,w,c)
        new_image2 = torch.zeros(h,w,c)
        new_image1[:, :, i] = image1[:, :, i] * torch.square(perturbation)
        new_image2[:, :, i] = image2[:, :,i]  * torch.square(perturbation)
    return new_image1, new_image2

Check for duplicates

  • I have searched the open bugs/issues and have found no duplicates for this bug report
@rachelglenn rachelglenn added the question Further information is requested label Jul 3, 2024
@mzient
Copy link
Contributor

mzient commented Jul 3, 2024

Hello @rachelglenn,
I strongly advise against using PythonFunction for functionality with good native support. You can get tensors filled with random values with functions from dali.fn.random. Elementwise squaring can be achieved by simply by multiplying the tensors, like:

   # passing image as the argument will cause the function to return an array shaped like the image
   perturbation = fn.random.uniform(image1, range=[0, 1])  # this already includes channel
   pert_squared = perturbation * perturbation
   new_image1 = image1 * pert_squared
   new_image2 = image2 * pert_squared

BTW - it seems like the code is incorrect (swapped lines?):

    for i in range(c):
        h, w, c = image1.shape # c defined here, but loop over range(c) above

@JanuszL
Copy link
Contributor

JanuszL commented Jul 3, 2024

Still, if you like using torch_python_function, just use torch.cuda.device inside the callable.

@rachelglenn
Copy link
Author

Thank you. I really appreciate it. Another question. I have a pipeline class defined. Do I have to specify that the data loaded is to go to the gpu? Load data calls the dali numpy reader. For some reason, the output of the model (data) is on the cpu and not the gpu.

def define_graph(self):
        data= self.load_data() 
        data= data.to('cuda')
        return data

@JanuszL
Copy link
Contributor

JanuszL commented Jul 10, 2024

Hi @rachelglenn,

To move data from CPU to GPU inside the pipeline, please:

def define_graph(self):
        data= self.load_data() 
        data= data.gpu()
        return data

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants