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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.10Windows x86-64

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

File metadata

File hashes

Hashes for cudacanvas-1.0.1.post260118-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 540a88c3f38045a95dc883014d77a3b364813afb77e496940ea8131048585bf4
MD5 f9ffecd1ab307942b32598326eef303d
BLAKE2b-256 921a9e7b589c410581c48b36aad8b8171d8ba18b06e447ae9fd2f4aedde54afd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cudacanvas-1.0.1.post260118-cp311-cp311-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9aea7695784782f7bf4528bdcf2ac83011bd5afd2354ffd9622789f387abdba3
MD5 835a6ea90c5af19673331ee35546ce54
BLAKE2b-256 606b2bca93c72e96383a8e21b5f7eb8c80d74ea214d09e6f5ae3c445114df233

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cudacanvas-1.0.1.post260118-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 98a47b5cbaa0ff539c7b79462f7819fdc3f66c974a18db51486b7ade9149563d
MD5 86a806fa29a551f199a93a20d02fd3f1
BLAKE2b-256 c921bf7c2528e2172d2536a3e05dcccdac18a43af844b1ccff05c48079a2f593

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cudacanvas-1.0.1.post260118-cp310-cp310-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 99166329111fa396da909d0902f248cda39bde5824d922d824e5e3506d8452c4
MD5 364c39ddb50fc71119f29e6b442961af
BLAKE2b-256 3bac71267072f4d67370f358d78c39c5a8b5ab2341a3d105d50b83213dc15815

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cudacanvas-1.0.1.post260118-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 571fc3e7ec8a5e968d285fa4863a176e0aa9bbc652376a2bc1a6bd46b0e582ce
MD5 f28b277d750cd1dbc4ce5535c40f0024
BLAKE2b-256 c4d022b4254af5312ffe05ac258238e03b36c53bf58b8504fda108c3e9e38b8d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cudacanvas-1.0.1.post260118-cp39-cp39-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0377ebbddb5d4e5a494b7bef38764c7b63c0af2cc07e62f010ea2a3e46b30041
MD5 25e2584a5fffc7141a1c63656c067138
BLAKE2b-256 2c9b508ff93c2c9e5df808b7a696584bf82b7f6d21a2c5f0cf9be72ba71714b6

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