Skip to main content

Interop between ModernGL and PyTorch CUDA

Project description

torchgl

torchgl is a simple library for sharing data between PyTorch and ModernGL directly on the GPU.

It wraps CUDA graphics interoperability calls, which avoids slow CPU round-trips. No C++ compiling is needed since we use the python CUDA API.

Use Cases

  • Render visualizations in real-time
  • Use GLSL shaders in pre- or post-processing
  • Combine ML with traditional graphics pipelines

Basic Usage

Write a tensor into a texture

import moderngl
import torch
import torchgl

# need a moderngl context, e.g.
moderngl.create_context(standalone=True)

# float16
tensor = torch.rand(1080, 1920, 4, device="cuda", dtype=torch.float16)
texture = torchgl.to_texture(tensor)
print(texture.size, texture.components, texture.dtype)  # (1920, 1080) 4 f2

# uint8 with 2 channels
tensor = (255 * tensor[:, :, :2]).to(torch.uint8)
texture = torchgl.to_texture(tensor)
print(texture.size, texture.components, texture.dtype)  # (1920, 1080) 2 f1

Read a texture into a tensor

import moderngl
import torchgl

ctx = moderngl.create_context(standalone=True)
texture = ctx.texture((1920, 1080), 1, dtype="f1")
tensor = torchgl.to_tensor(texture)
print(tensor.shape, tensor.dtype)  # torch.Size([1080, 1920, 1]) torch.uint8

Other examples

See other examples here.

Installation

torchgl is can be installed with pip

pip install torchgl

Before installing, make sure you have PyTorch with CUDA support.

This package depends on CUDA Python bindings. It is recommended to match the version are using with PyTorch, e.g. if you want to use CUDA 12.8

pip install cuda-python==12.8
pip install torch --index-url https://download.pytorch.org/whl/cu128

Texture Formats

torchgl supports 1, 2, or 4 component Textures (3 component textures are not supported by CUDA). For most ModernGL dtypes, the converted Tensor type matches what you would use to supply pixel data.

ModernGL dtype PyTorch dtype
f1 uint8
f2 half
f4 float32
u1 uint8
u2 uint16
u4 uint32
i1 int8
i2 int16
i4 in32

The exceptions are "ni1", and "ni2" which are really unsigned internally, but the pixel data is expected to be signed (GL_BYTE or GL_SHORT), see https://github.com/moderngl/moderngl/blob/main/src/moderngl.cpp#L800. We suggest avoiding these types unless you know really know what you are doing.

Also, if you override ModernGL internal format conversion may not behave as expected.

Advanced Usage

For more control of the resource management and synchronization between CUDA and OpenGL, you can allocate your textures and buffers ahead of time and register them once for interop using register(). Then, use map() and unmap() to efficiently pipeline the CUDA and OpenGL and reduce unnecessary synchronization points.

Streams are also supported, although you should be aware of how they work with the PyTorch allocation of CUDA tensors, see docs here.

Related Packages

torch2moderngl provides similar basic usage for Textures only.

Project details


Download files

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

Source Distribution

torchgl-0.1.1.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

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

torchgl-0.1.1-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file torchgl-0.1.1.tar.gz.

File metadata

  • Download URL: torchgl-0.1.1.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for torchgl-0.1.1.tar.gz
Algorithm Hash digest
SHA256 432ee4bc849c52ac2e296d951a74f8768615a5e60bcf005f2e5ff99c104bce3e
MD5 6379fd02d3573d5695ad74ee269f6b35
BLAKE2b-256 47e9e7ff553133e7d75daa8555ee88ccb847ac0c14cc4015126c83b34a58c1a9

See more details on using hashes here.

File details

Details for the file torchgl-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: torchgl-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for torchgl-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 af6c66acc6ad6ab53cc287ee3b03a196222570509bed08a5cde90e1675e50e33
MD5 0ea04c8b831fbbf46099f7d86facc4ca
BLAKE2b-256 e643a7ab80fa79ce2c991fd72ac0fc9ca4dff368d4f8b9b78bcc01eda8ba9cc4

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