Skip to main content

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

Project description

Buy Me A Coffee Twitter 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

cudacanvas-1.0.1.post250118-cp311-cp311-win_amd64.whl (99.3 kB view details)

Uploaded CPython 3.11 Windows x86-64

cudacanvas-1.0.1.post250118-cp310-cp310-win_amd64.whl (98.2 kB view details)

Uploaded CPython 3.10 Windows x86-64

cudacanvas-1.0.1.post250118-cp39-cp39-win_amd64.whl (98.5 kB view details)

Uploaded CPython 3.9 Windows x86-64

File details

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

File metadata

File hashes

Hashes for cudacanvas-1.0.1.post250118-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 47366d734ed959722380795737cf674d285b9ed0b2b0394012fc6eafb76c918f
MD5 4ab522a625a8ee7bd869f08412ef90c9
BLAKE2b-256 e9f11795a241532b146f0c83f944afb32403e5a9c5b5cf8c7e818bdd318c9345

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cudacanvas-1.0.1.post250118-cp311-cp311-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6213b0de7dc187d6ecc628647cf9afe56ee0ad67652657aecc736161a1cc3d30
MD5 4b9a0a191442a869b19b2d51e83ef090
BLAKE2b-256 40c8a8e2c257060478f8bcf84819414b340f663a916ddaa0265d2d38a6aebd52

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cudacanvas-1.0.1.post250118-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 7d2177a6467165be72339ed2745474a963a9ad7fe11b6dfe2a58a1d385c97c3a
MD5 99addba363c2dca69e647d5843ffca45
BLAKE2b-256 40091f6dd1f82b02098d888fef576d2f97086e42033d599e38935e73aa84982a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cudacanvas-1.0.1.post250118-cp310-cp310-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e0762122e6d8147cfe2a87e8dbe562f9c9f38ab4a7132bfe8fa390e7a620abae
MD5 fbd4084f7e56778f2ce29e55725a2223
BLAKE2b-256 62b0f35c37cda370df7cda54d5160218626bcd6ffb28dce530abbe10e28a4178

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cudacanvas-1.0.1.post250118-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 a04e0eb117b03285c6955906a2b7a32458b46174ce1d698cbac557089e7fcbef
MD5 f56674d5f5b1529f41f79cb24c8d734e
BLAKE2b-256 a4e30164269644c56317d4a4307e67e5d2e64de510e58febc4d7d384a8955414

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cudacanvas-1.0.1.post250118-cp39-cp39-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5118b97aa248e44dea75941c98a76eaf324dab0dab6bd605137ebe1e1dc05383
MD5 28d4c3cdc0a1dab9850894199579f45f
BLAKE2b-256 df4f1cd5f14dd05997236337040bd4b7e0a215a02e543915a8a7dc9cb9151b42

See more details on using hashes here.

Supported by

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