caliper.tensor_bridge.v1, v1_1 and v1_2¶
The service ids are caliper.tensor_bridge.v1, caliper.tensor_bridge.v1_1 and caliper.tensor_bridge.v1_2. The bridge
turns a CaliperTensor into a live texture in the current frame, and the texture stays GPU-resident on the native
backends. Version v1_1 adds stream-ordered handoff, and v1_2 adds imported device allocations.
caliper.tensor_bridge.v1 Calls¶
| Call | Signature | Description |
|---|---|---|
texture_from_tensor |
CaliperTextureId (const CaliperTensor* t, uint32_t flags) |
Mirror a 3-D (H,W,C<=4) u8 tensor as a texture. The texture stays GPU-resident on the native backends, with zero copy where the layout permits and a device-side blit otherwise. The OpenGL fallback uploads through the CPU. The function returns 0 on failure and writes the reason to caliper.log.v1. |
update_texture |
bool (CaliperTextureId tex, const CaliperTensor* t) |
Upload t again into the existing texture tex, which must have the same shape and dtype. Return false on failure. |
release_texture |
void (CaliperTextureId tex) |
Free the texture tex that texture_from_tensor or texture_from_tensor_mapped returned. |
texture_from_tensor_mapped |
CaliperTextureId (const CaliperTensor* t, int32_t colormap, float vmin, float vmax, uint32_t flags) |
Colormap a 2-D (H,W) f32 tensor through the built-in table colormap, which scales [vmin,vmax] to [0,1], and mirror the result as a texture. The function returns 0 on failure and writes the reason to caliper.log.v1. |
bool (CaliperDType dtype, int32_t ndim, const int64_t* shape, CaliperTensor* out_tensor, CaliperTextureId* out_texture) |
Allocate tensor memory that is the backing store of a texture. The applet wraps out_tensor->data, for example with torch::from_blob, and writes into it. The texture sees the result after at most a layout transition. In v1 the function returns a unified-memory CPU-device tensor, which is zero-copy for CPU writers, and device writers stage through update_texture. It returns false on failure. |
|
void (CaliperTextureId tex) |
Free the memory and the texture that alloc_shared returned for tex. |
Types¶
CaliperTextureId¶
The id of a host texture.
The value 0 is invalid, and an id is compare-only. Applets never interpret or dereference an id, because its representation is internal to the backend. The value casts directly to ImTextureID for ImGui::Image, because the host hands out the ImGui-compatible handle of each backend.
typedef uint64_t CaliperTextureId;
CaliperColormap¶
The built-in colormap tables for 1-channel tensors. Each table has 256 RGBA8 entries and gives the same numbers on every backend.
| Constant | Value | Description |
|---|---|---|
CALIPER_CMAP_VIRIDIS |
0 |
The Viridis table, id 0. |
CALIPER_CMAP_MAGMA |
1 |
The Magma table, id 1. |
CALIPER_CMAP_RDBU |
2 |
The RdBu diverging table, id 2. |
CaliperTensorBridgeV1¶
The function table of caliper.tensor_bridge.v1. Call every entry from the frame thread.
| Field | Type | Description |
|---|---|---|
struct_size |
uint32_t |
sizeof(CaliperTensorBridgeV1) as the host built it. |
texture_from_tensor |
CaliperTextureId (*)(const CaliperTensor* t, uint32_t flags) |
Mirror a 3-D (H,W,C<=4) u8 tensor as a texture. The texture stays GPU-resident on the native backends, with zero copy where the layout permits and a device-side blit otherwise. The OpenGL fallback uploads through the CPU. The function returns 0 on failure and writes the reason to caliper.log.v1. |
update_texture |
bool (*)(CaliperTextureId tex, const CaliperTensor* t) |
Upload t again into the existing texture tex, which must have the same shape and dtype. Return false on failure. |
release_texture |
void (*)(CaliperTextureId tex) |
Free the texture tex that texture_from_tensor or texture_from_tensor_mapped returned. |
texture_from_tensor_mapped |
CaliperTextureId (*)(const CaliperTensor* t, int32_t colormap, float vmin, float vmax, uint32_t flags) |
Colormap a 2-D (H,W) f32 tensor through the built-in table colormap, which scales [vmin,vmax] to [0,1], and mirror the result as a texture. The function returns 0 on failure and writes the reason to caliper.log.v1. |
alloc_shared |
bool (*)(CaliperDType dtype, int32_t ndim, const int64_t* shape, CaliperTensor* out_tensor, CaliperTextureId* out_texture) |
Allocate tensor memory that is the backing store of a texture. The applet wraps out_tensor->data, for example with torch::from_blob, and writes into it. The texture sees the result after at most a layout transition. In v1 the function returns a unified-memory CPU-device tensor, which is zero-copy for CPU writers, and device writers stage through update_texture. It returns false on failure. |
free_shared |
void (*)(CaliperTextureId tex) |
Free the memory and the texture that alloc_shared returned for tex. |
Ids¶
| Name | Value | Description |
|---|---|---|
CALIPER_TENSOR_BRIDGE_V1 |
"caliper.tensor_bridge.v1" |
The id of this service, which get_service takes. |
Semantics¶
On the native backends the tensor's device memory becomes the texture with no CPU round trip. The ABI never names a graphics API, so the renderer stays swappable (see Rendering). The cost of each path is in Zero-copy tensor visualization.
Acceptance Rules¶
The host validates every tensor before it becomes a texture. A violation returns 0 or false and emits one
caliper.log.v1 line. The bridge never misinterprets bytes into a wrong texture. The shipped gate accepts two input forms:
- 2-D
(H,W)f32goes totexture_from_tensor_mapped, colormapped through a built-in table that scales[vmin,vmax]to[0,1]. - 3-D
(H,W,C≤4)u8goes totexture_from_tensoras direct RGBA.
The tensor must also be contiguous (row-major, no gaps). Its device must be CPU or the active backend's device, which is Metal on macOS and CUDA on Windows. The applet-side torch adapter enforces the mirror of these rules before the tensor reaches the host. It rejects a bad tensor and never copies it silently.
Frame Thread Only¶
Every bridge entry point is frame-thread-only. Unlike caliper.metrics.v1, which a job thread
may call, the bridge touches renderer state and must be called from the applet's frame(). In the usual pattern the
training worker snapshots weights under a mutex and never calls the bridge. The frame thread reads that snapshot and
does every upload. The frame thread owns the textures and releases them after the job wait and before renderer teardown.
Per-Backend Behavior¶
The applet code is identical on every backend. The place where staging happens differs, and the device-path string that the renderer reports says which path ran.
| Backend | Path | Staging |
|---|---|---|
| Metal (macOS default) | A device compute pass (f32 with a colormap) or a blit (u8) reads the MPS MTLBuffer in place |
None |
| Vulkan with CUDA (Windows default) | Tried in order: a CUDA kernel reads the tensor and writes the Vulkan image (direct-cmap, direct-blit), then a copy into a shared buffer and a Vulkan pass (compute, blit), then CPU staging |
None on the first two rungs, and no in-VRAM copy on the direct rung |
| Any CPU tensor | The bridge converts on the CPU and uploads | The bridge stages, and the applet never touches a pixel |
The Windows ladder is described in Vulkan and CUDA on Windows.
One tensor cannot be both
A single tensor is either zero-copy on the device path or staged from the CPU, not both. A bridge whose renderer
has no device path for the tensor, such as Vulkan without paired CUDA interop, has CPU as its active device, so it
rejects a device tensor as a foreign device. The exemplar hands over the training-device tensor first. If the
create returns 0, it relocates the tensor to CPU and the bridge stages it. The applet does no pixel work on
either path, because the bridge's accept or reject drives the choice.
Texture Lifecycle¶
Create the texture once on the first snapshot and call update_texture after that. Do not recreate a texture per
frame, because creating a texture costs far more than updating one.
The range is pinned in v1
update_texture has no colormap-range channel, so the [vmin,vmax] of a colormapped texture is fixed at creation.
Pin a symmetric RdBu range at the first snapshot. Learned filters still visibly sharpen, because the structure
changes and not only the scale, and the UI should say so. The bridge does not recreate the texture per snapshot to
re-range, because that would break create-once.
alloc_shared¶
alloc_shared allocates tensor memory that is the texture's backing store. The applet wraps out_tensor->data (for
example with torch::from_blob) and writes into it. The texture sees the result after at most a layout transition.
Free the memory with free_shared. The result depends on the backend:
- Vulkan with CUDA returns a device-backed tensor on
CALIPER_DEV_CUDA. The applet's kernels write the texture's memory in place, and the update runs the buffer-to-image pass with no copy. - Metal returns a CPU vector in unified memory. That is zero-copy for CPU writers, but a device writer still goes
through
update_texture. - Vulkan without paired interop returns a CPU vector.
CaliperTextureId Lifetime¶
The host hands out the ImGui-compatible handle of each backend. That handle is the id<MTLTexture> pointer on Metal and the descriptor set on Vulkan. Free ids from texture_from_tensor* with release_texture, and ids from alloc_shared with free_shared.
Sync Model¶
v1 is stream-free, so CaliperTensor.stream is NULL. Correctness on device textures comes from the applet draining
the device once at the handoff. The applet calls torch::mps::synchronize() or torch::cuda::synchronize() through the
adapter's synced_to_tensor, and then calls update_texture. That drain is a full-device barrier, and v1_1 replaces it
with stream ordering where the host honors it.
On a host that vends caliper.tensor_bridge.v1_1, the caps() query says what the host honors. Metal honors
stream-ordered handoff, and Vulkan honors it when the device exports timeline semaphores. Where the host honors it, the
adapter skips the drain and puts the producer's stream in CaliperTensor.stream. Otherwise the v1 drain applies. The
adapter side and the reasons are in Adapters and
Zero-copy tensor visualization.
Additive Revisions¶
Both revisions extend the bridge under the growth rules.
Each struct keeps the same members in the same order with the same semantics, and new members go at the end. Each
revision adds one capability bit. There is no ABI epoch bump, and the earlier headers, tables and ids stay frozen. An
applet asks the host for each id and calls caps(). A missing id or an unset bit means the applet falls back to the
previous contract, never a crash.
caliper.tensor_bridge.v1_1 Handoff¶
v1_1 adds one query, caps(), to the six v1 operations. If the host does not vend this id or leaves bit 0 unset, the v1 contract holds. The adapter drains, and stream stays NULL.
| Call | Signature | Description |
|---|---|---|
texture_from_tensor |
CaliperTextureId (const CaliperTensor* t, uint32_t flags) |
Mirror a 3-D (H,W,C<=4) u8 tensor as a texture, as in v1. Return 0 on failure. |
update_texture |
bool (CaliperTextureId tex, const CaliperTensor* t) |
Upload t again into the existing texture tex, which must have the same shape and dtype. Return false on failure. |
release_texture |
void (CaliperTextureId tex) |
Free the texture tex that texture_from_tensor or texture_from_tensor_mapped returned. |
texture_from_tensor_mapped |
CaliperTextureId (const CaliperTensor* t, int32_t colormap, float vmin, float vmax, uint32_t flags) |
Colormap a 2-D (H,W) f32 tensor through the built-in table colormap over [vmin,vmax], as in v1. Return 0 on failure. |
bool (CaliperDType dtype, int32_t ndim, const int64_t* shape, CaliperTensor* out_tensor, CaliperTextureId* out_texture) |
Allocate tensor memory that is the backing store of a texture, as in v1. Return false on failure. |
|
void (CaliperTextureId tex) |
Free the memory and the texture that alloc_shared returned for tex. |
|
caps |
uint32_t (void) |
Return the capability bits of the host, such as CALIPER_BRIDGE_CAP_STREAM_ORDERED. |
| Name | Value | Description |
|---|---|---|
CALIPER_TENSOR_BRIDGE_V1_1 |
"caliper.tensor_bridge.v1_1" |
The id of this service, which get_service takes. |
CALIPER_BRIDGE_CAP_STREAM_ORDERED |
(1u << 0) |
Bit 0 of caps(), which says that the host honors a non-NULL CaliperTensor.stream. Ordering on the producer's stream replaces the full device drain of the adapter. An adapter treats a missing bit or a missing v1_1 service as "drain as v1". |
caliper.tensor_bridge.v1_2 Imported Allocations¶
v1_2 adds three entry points to the seven v1_1 members. A host without bit 1 keeps the earlier contract.
| Call | Signature | Description |
|---|---|---|
texture_from_tensor |
CaliperTextureId (const CaliperTensor* t, uint32_t flags) |
Mirror a 3-D (H,W,C<=4) u8 tensor as a texture, as in v1. Return 0 on failure. |
update_texture |
bool (CaliperTextureId tex, const CaliperTensor* t) |
Upload t again into the existing texture tex, which must have the same shape and dtype. Return false on failure. |
release_texture |
void (CaliperTextureId tex) |
Free the texture tex that texture_from_tensor or texture_from_tensor_mapped returned. |
texture_from_tensor_mapped |
CaliperTextureId (const CaliperTensor* t, int32_t colormap, float vmin, float vmax, uint32_t flags) |
Colormap a 2-D (H,W) f32 tensor through the built-in table colormap over [vmin,vmax], as in v1. Return 0 on failure. |
bool (CaliperDType dtype, int32_t ndim, const int64_t* shape, CaliperTensor* out_tensor, CaliperTextureId* out_texture) |
Allocate tensor memory that is the backing store of a texture, as in v1. Return false on failure. |
|
void (CaliperTextureId tex) |
Free the memory and the texture that alloc_shared returned for tex. |
|
caps |
uint32_t (void) |
Return the capability bits of the host, such as CALIPER_BRIDGE_CAP_IMPORT_ALLOC. |
import_allocation |
CaliperAllocId (void* os_handle, uint64_t size_bytes, uint32_t handle_type) |
Import the device allocation that os_handle names and return its id. The handle comes from cuMemExportToShareableHandle, or it is an id<MTLBuffer> for CALIPER_ALLOC_HANDLE_MTLBUFFER. The value size_bytes is the byte size of the allocation, and handle_type is a CALIPER_ALLOC_HANDLE_* value. The function returns 0 when the host cannot import, which means a missing cap, a bad handle, or no hardware pair, and the applet then stays on the v1 path. The host duplicates the handle, and the applet keeps ownership of its copy. |
release_allocation |
void (CaliperAllocId alloc) |
Free the import alloc. |
update_texture_from_alloc |
bool (CaliperTextureId tex, CaliperAllocId alloc, uint64_t offset_bytes, const CaliperTensor* desc) |
Update the existing texture tex from tensor bytes that live inside the imported allocation alloc at offset_bytes. Create the texture first with texture_from_tensor or texture_from_tensor_mapped. The argument desc describes shape, dtype, strides, and stream, and the host ignores desc->data. The call applies the acceptance gates of update_texture and returns false when the texture is not updated, and then the caller falls back. The pass reads the imported bytes in place, so the range from offset_bytes to offset_bytes plus the extent must not change until the next update of the texture or the next frame of the applet. Device ordering covers producer writes that come before the call and not writes that come after it. |
The id of an imported allocation. The value 0 is invalid, and an id is compare-only and internal to the host.
typedef uint64_t CaliperAllocId;
The function table of caliper.tensor_bridge.v1_2. Its first seven members are those of v1_1.
| Field | Type | Description |
|---|---|---|
struct_size |
uint32_t |
sizeof(CaliperTensorBridgeV1_2) as the host built it. |
texture_from_tensor |
CaliperTextureId (*)(const CaliperTensor* t, uint32_t flags) |
Mirror a 3-D (H,W,C<=4) u8 tensor as a texture, as in v1. Return 0 on failure. |
update_texture |
bool (*)(CaliperTextureId tex, const CaliperTensor* t) |
Upload t again into the existing texture tex, which must have the same shape and dtype. Return false on failure. |
release_texture |
void (*)(CaliperTextureId tex) |
Free the texture tex that texture_from_tensor or texture_from_tensor_mapped returned. |
texture_from_tensor_mapped |
CaliperTextureId (*)(const CaliperTensor* t, int32_t colormap, float vmin, float vmax, uint32_t flags) |
Colormap a 2-D (H,W) f32 tensor through the built-in table colormap over [vmin,vmax], as in v1. Return 0 on failure. |
alloc_shared |
bool (*)(CaliperDType dtype, int32_t ndim, const int64_t* shape, CaliperTensor* out_tensor, CaliperTextureId* out_texture) |
Allocate tensor memory that is the backing store of a texture, as in v1. Return false on failure. |
free_shared |
void (*)(CaliperTextureId tex) |
Free the memory and the texture that alloc_shared returned for tex. |
caps |
uint32_t (*)(void) |
Return the capability bits of the host, such as CALIPER_BRIDGE_CAP_IMPORT_ALLOC. |
import_allocation |
CaliperAllocId (*)(void* os_handle, uint64_t size_bytes, uint32_t handle_type) |
Import the device allocation that os_handle names and return its id. The handle comes from cuMemExportToShareableHandle, or it is an id<MTLBuffer> for CALIPER_ALLOC_HANDLE_MTLBUFFER. The value size_bytes is the byte size of the allocation, and handle_type is a CALIPER_ALLOC_HANDLE_* value. The function returns 0 when the host cannot import, which means a missing cap, a bad handle, or no hardware pair, and the applet then stays on the v1 path. The host duplicates the handle, and the applet keeps ownership of its copy. |
release_allocation |
void (*)(CaliperAllocId alloc) |
Free the import alloc. |
update_texture_from_alloc |
bool (*)(CaliperTextureId tex, CaliperAllocId alloc, uint64_t offset_bytes, const CaliperTensor* desc) |
Update the existing texture tex from tensor bytes that live inside the imported allocation alloc at offset_bytes. Create the texture first with texture_from_tensor or texture_from_tensor_mapped. The argument desc describes shape, dtype, strides, and stream, and the host ignores desc->data. The call applies the acceptance gates of update_texture and returns false when the texture is not updated, and then the caller falls back. The pass reads the imported bytes in place, so the range from offset_bytes to offset_bytes plus the extent must not change until the next update of the texture or the next frame of the applet. Device ordering covers producer writes that come before the call and not writes that come after it. |
| Name | Value | Description |
|---|---|---|
CALIPER_TENSOR_BRIDGE_V1_2 |
"caliper.tensor_bridge.v1_2" |
The id of this service, which get_service takes. |
CALIPER_BRIDGE_CAP_IMPORT_ALLOC |
(1u << 1) |
Bit 1 of caps(), which says that import_allocation and update_texture_from_alloc are live. The host can then import an applet-exported device allocation and update textures from it with no copy of the tensor data. |
CALIPER_ALLOC_HANDLE_OPAQUE_WIN32 |
1u |
The handle_type of a Windows opaque shareable handle. |
CALIPER_ALLOC_HANDLE_OPAQUE_FD |
2u |
The handle_type of a POSIX file descriptor as an opaque shareable handle. |
CALIPER_ALLOC_HANDLE_MTLBUFFER |
3u |
The handle_type for which os_handle is an in-process id<MTLBuffer> on Apple unified memory. No OS handle transfers, and the duplicate that the host makes is an Objective-C strong retain. |
CaliperAllocId is the id that import_allocation returns. It reuses the caches, gates and lifecycle of v1_2. The applet side of an import is ExportablePool.