Skip to main content

Real-time PyTorch Tensor Visualisation in CUDA, Eliminating CPU Transfer

Project description

Buy Me A Coffee Twitter PyPI version Downloads

image

cudacanvas : PyTorch Tensor Image Display in CUDA

(Real-time PyTorch Tensor Image Visualisation in CUDA, Eliminating CPU Transfer)

CudaCanvas is a simple Python module that eliminates CPU transfer for Pytorch tensors for displaying and rendering images in the training or evaluation phase, ideal for machine learning scientists and engineers.

Simplified version that directly displays the image without explicit window creation (cudacanvas >= v1.0.1)

import torch
import cudacanvas


#REPLACE THIS with you training loop
while (True):

    #REPLACE THIS with you training code and generation of data
    noise_image = torch.rand((4, 500, 500), device="cuda")

    #Visualise your data in real-time
    cudacanvas.im_show(noise_image)

    #OPTIONAL: Terminate training when the window is closed
    if cudacanvas.should_close():
        cudacanvas.clean_up()
        #end process if the window is closed
        break

You can visualise the latent of Stable Diffusion during sampling in real-time whilst waiting for the steps to finish

import warnings
warnings.filterwarnings("ignore")
from diffusers import StableDiffusionPipeline
import torch
import cudacanvas

def display_tensors(pipe, step, timestep, callback_kwargs):
    latents = callback_kwargs["latents"]

    with torch.no_grad():
        image = pipe.vae.decode(latents / pipe.vae.config.scaling_factor, return_dict=False)[0]
        image = image - image.min()
        image = image / image.max()
    
    cudacanvas.im_show(image.squeeze(0))
    
    if cudacanvas.should_close():
        cudacanvas.clean_up()
        pipe._interrupt = True
    
    return callback_kwargs

pipeline = StableDiffusionPipeline.from_pretrained(
    "stabilityai/stable-diffusion-2-1-base",
    torch_dtype=torch.float16,
    variant="fp16"
).to("cuda")

image = pipeline(
    prompt="A croissant shaped like a cute bear.",
    negative_prompt="Deformed, ugly, bad anatomy",
    callback_on_step_end=display_tensors,
    callback_on_step_end_tensor_inputs=["latents"],
).images[0]

cudacanvas.clean_up()

Installation

Before instllation make sure you have torch with cuda support already installed on your machine

We aligned pytorch and cuda version with our package the supporting packages are torch (2.0.1, 2.1.2 and 2.2.2) and (11.8 and 12.1)

Identify your current torch and cuda version

import torch
torch.__version__

Depending on your torch and cuda you can install the relevant cudacanvas package, for the latest one matching the latest pytorch package you can simply download the latest package

pip install cudacanvas

For other torch and cuda packages put the torch and cuda version after that cudacanvas version for example for 2.1.2+cu118 the Cudacanvas package you require is 1.0.1.post212118

pip install cudacanvas==1.0.1.post212118 --force-reinstall

Support

Also support my channel ☕ ☕ : https://www.buymeacoffee.com/outofai

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

cudacanvas-1.0.1.post260124-cp311-cp311-win_amd64.whl (101.8 kB view details)

Uploaded CPython 3.11Windows x86-64

cudacanvas-1.0.1.post260124-cp310-cp310-win_amd64.whl (100.6 kB view details)

Uploaded CPython 3.10Windows x86-64

cudacanvas-1.0.1.post260124-cp39-cp39-win_amd64.whl (101.1 kB view details)

Uploaded CPython 3.9Windows x86-64

File details

Details for the file cudacanvas-1.0.1.post260124-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for cudacanvas-1.0.1.post260124-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 b1d5d8d43001b9e44af65da885d0b1179d4caee4a18ddf56e729d8d77336f844
MD5 69368a662458c4325222af0f8009216a
BLAKE2b-256 19ddde0e228280be9ebc2259bb4712a63410fed4192061973f236aa3ecf399c8

See more details on using hashes here.

File details

Details for the file cudacanvas-1.0.1.post260124-cp311-cp311-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cudacanvas-1.0.1.post260124-cp311-cp311-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 cabb674d106dde722ddafb005d98cd1acc89b03cb8fd6be94b4d9cbd39e8eca2
MD5 7560a707f906fc6254937d19b0590809
BLAKE2b-256 7bc0fd04f4e94576c6ff2d3ce139f4a9773075316fcf03d5c4f6970be8f91289

See more details on using hashes here.

File details

Details for the file cudacanvas-1.0.1.post260124-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for cudacanvas-1.0.1.post260124-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 68650166410f88ac33399a14cfa9b06adc0a5da0d2509a62bd5dc8d297441468
MD5 cc61083e60643673fbebae997984fb42
BLAKE2b-256 e8f1706fc691d2143a076d58eb58159b6b3dec64e1ca095936201aaf666ee8ad

See more details on using hashes here.

File details

Details for the file cudacanvas-1.0.1.post260124-cp310-cp310-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cudacanvas-1.0.1.post260124-cp310-cp310-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 29c8e8268d0f18be95d4eca923e16f4c165e99c5ae972be3481e178c21a73416
MD5 4cbd5aa4eacb1cbdb52dd16057e5284b
BLAKE2b-256 afee32bcc3b26b9cdc10667789db922c187b7457e030e98676a4e7aace2c80f9

See more details on using hashes here.

File details

Details for the file cudacanvas-1.0.1.post260124-cp39-cp39-win_amd64.whl.

File metadata

File hashes

Hashes for cudacanvas-1.0.1.post260124-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 3eae7f3502b73797be9329cdba16520eb3e27c532bd4c4241d5e7c8838e1fe19
MD5 9fccbfa713d2d341f1d55e0ad44cf94e
BLAKE2b-256 5abe6eeda5a0c23ab8f0aa4232461b43d963fbce9121c6c6f46d88d2350f6f0c

See more details on using hashes here.

File details

Details for the file cudacanvas-1.0.1.post260124-cp39-cp39-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cudacanvas-1.0.1.post260124-cp39-cp39-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4e9d706894552bce308ed74ec779fd12d7cfc0829a0d52137386cdffa29afe20
MD5 fa40c7dfabf6b0645d5e114420e67ca5
BLAKE2b-256 c617736f794f735f19c679ed6c8a1808643f3c4a4f5f9e33dba7cac51b4c5e9e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page