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.post250124-cp311-cp311-win_amd64.whl (99.3 kB view details)

Uploaded CPython 3.11 Windows x86-64

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

Uploaded CPython 3.10 Windows x86-64

cudacanvas-1.0.1.post250124-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.post250124-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for cudacanvas-1.0.1.post250124-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 41981602634f1fe2bdecf401964c9e64f73d05240ac2139369f35b54315434fc
MD5 3aa6db11b3918a7b2cd4a04f5e6e58da
BLAKE2b-256 9dc92ae2ebfb79c2c378619f04d799f72b5e2a1942ec0ee1d0ae2ceffef52f61

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cudacanvas-1.0.1.post250124-cp311-cp311-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 36946957efa7454500a482cb587467a2647dc766b74a7dc9e5b420a673f54318
MD5 e74e4c2fffa37b29630fb6b7c0080d58
BLAKE2b-256 89e180c319085d829ec8c04487f0b09418b32f5e0d4a697e6e718939d0c73226

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cudacanvas-1.0.1.post250124-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 657b1286b481b7fcb2bb3829fc7c3dc6667088ca0292560424b62b84686f89f8
MD5 7483f23929f005eb208689f2a1f998f6
BLAKE2b-256 03d5df076870ecaa07412d1f5ac0228f6c2009146eab705616a18d702ff41522

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cudacanvas-1.0.1.post250124-cp310-cp310-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b23cc0e49b880a60fb8ca1fbea3fef167dcc0f4531e82818128300ce66084313
MD5 02d84b0e0d7c1d69b4dd0a58b7e03c56
BLAKE2b-256 e6f4ba74675390cffd455705819c380b6d3e6ae932fe37db68cd7e86b321bce8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cudacanvas-1.0.1.post250124-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 9c4d0f2526fee9aabed1fb81614d8f2e4fe102c446c608e820cb9fe2095b3853
MD5 6f4a00bb618aa1b21c1fd453f03d84cf
BLAKE2b-256 ca557aa15f1bf8ad44beba1f0fb3127f34fcb251dd39aff239ee3992766e9546

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cudacanvas-1.0.1.post250124-cp39-cp39-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 66c02406d4d15c5cd0889362af5197a31bd229f2d1998c978f16682a89366b32
MD5 02c9680f05ff38a61506d1f260382fb7
BLAKE2b-256 f04981dccf22a27acc7fce7499c0ddf6125a094c7870343eaa3a15f7c7e9d4ad

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