Skip to content

Native process functions ​

Run this program to inspect a scGPT preset's named outputs, then optionally move one packed batch to CUDA. It creates its own two-row CSR fixture in a temporary directory and needs no model checkpoint or training data.

Choose a run ​

In your Python project, install PyCellPack from PyPI and save the downloadable program as processfn-example.py. With a C++17 compiler available, run:

sh
uv add pycellpack
uv run processfn-example.py
uv run processfn-example.py --device cuda
ModeRequirementsWhat you inspect
Default CPUPyCellPack, NumPy and a C++17 compilerNamed NumPy field views; PyTorch is not needed
--device cudaThe same, plus compatible PyTorch/CUDA and a GPUOne packed H2D copy, then named contiguous GPU Tensor fields

The preset uses a length of 8 and requests cells [1, 0] in one batch. Both modes produce genes as int64 and values/targets as float32, each shaped (2, 8). The first gene token is CLS (1); the padding token is 0. Negative table entries exclude dimensions. Input values are quantile-binned and masked by the preset, so do not expect the values field to reproduce raw counts.

process.output describes a 128-byte row: 64 bytes of genes, 32 of values and 32 of targets. The CPU path unpacks that record without copying. The CUDA path uploads the complete record before forming field views, avoiding separate host field rearrangement and transfers.

Follow the storage lifetime ​

Native row processing
  1. 01
    Read pages

    Fetch the compressed record.

  2. 02
    Decode

    Restore the input row.

  3. 03
    Map columns

    Address the schema’s output dimensions.

  4. 04
    Convert dtype

    Convert values to the schema input dtype.

  5. 05
    Call native code

    Invoke the CSR or dense callback.

  6. 06
    Write output

    Fill the fixed-size per-cell slot region.

Callbacks borrow input only for the call and write within their output and declared scratch regions.

  1. The native workers write a leased output slot.
  2. CPU inspection finishes using its field views before returning. Retaining them would keep the slot leased; use unpack(copy=True) to keep CPU results.
  3. CUDA upload retains the raw NumPy array and its zero-copy host Tensor until the copy stream finishes reading them. non_blocking=True alone is not a completion signal.
  4. Once host readers are gone, the slot is reusable. Device fields retain GPU storage, and wait_stream / record_stream govern their separate consumers.

The example explicitly synchronizes the copy stream before releasing host storage; it does not yield an unfinished host-to-device transfer to the caller. Pinned output memory is allocated only in CUDA mode.

Complete source ​

Complete program: preset, CPU inspection and packed CUDA transfer
py
"""Inspect scGPT output on CPU or transfer one packed batch to CUDA.

uv run example/processfn-example.py                 # CPU field views
uv run example/processfn-example.py --device cuda   # packed H2D, then GPU fields

Requires pycellpack and a C++17 compiler; CUDA mode also requires PyTorch
with CUDA. ProcessFn writes one packed record per row in both modes.
"""

import argparse
from pathlib import Path
from tempfile import TemporaryDirectory

import numpy as np

from pycellpack import DataSource, Loader, write
from pycellpack.loader import Ring
from pycellpack.loader.process_fn import presets


def show_cpu(process, raw):
    # These views retain the slot only until this function returns.
    for name, array in process.unpack(raw).items():
        print(f"{name}: {array}")


def upload(batch, device, copy_stream):
    import torch

    raw = batch.region("scgpt")
    host = torch.from_numpy(raw)
    try:
        with torch.cuda.stream(copy_stream):
            packed = host.to(device, non_blocking=True)
    finally:
        # Keep both host views alive until this stream finishes reading the slot.
        copy_stream.synchronize()
    batch.release()
    del host, raw
    return packed


def gpu_fields(packed, output, streams):
    import torch

    current = torch.cuda.current_stream(packed.device)
    fields = {}
    dtypes = {"genes": torch.int64, "values": torch.float32, "targets": torch.float32}
    for name, dtype in dtypes.items():
        stream = streams[name]
        with torch.cuda.stream(stream):
            packed.record_stream(stream)
            offset = output.offsets[name]
            width = output.dtype[name].itemsize
            view = packed[:, offset:offset + width].view(dtype)
            # Each field's contiguous copy can run on its own CUDA stream.
            fields[name] = view.contiguous()
        current.wait_stream(stream)
        fields[name].record_stream(current)
    return fields


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--device", choices=("cpu", "cuda"), default="cpu")
    args = parser.parse_args()
    counts = np.array([[0, 2, 0, 4, 1, 0], [3, 0, 1, 0, 2, 1]], np.float32)
    out_ids = np.arange(counts.shape[1])
    process = presets.scgpt(
        tokens=[10, 11, 12, 13, 14, -1],
        pad_token=0,
        cls_token=1,
        length=8,
        mask_ratio=0.4,
        seed=7,
    )
    print(process)
    print(process.output)
    with TemporaryDirectory(prefix="pycellpack-") as directory:
        store = write(Path(directory) / "counts.cpk", counts, format="csr", workers=1)
        source = DataSource([(store, out_ids)], len(out_ids))
        loading = Loader(source, process.schema(out_ids, name="scgpt"), workers=2, slot_rows=2)
        print(loading)
        ring = None
        if args.device == "cuda":
            import torch

            device = torch.device("cuda:0")
            copy_stream = torch.cuda.Stream(device=device)
            streams = {name: torch.cuda.Stream(device=device) for name in process.output.names}
            layout = loading.ring_layout(slots=2)
            storage = torch.empty(layout.data_bytes, dtype=torch.uint8, pin_memory=True)
            ring = Ring(layout, data=storage.numpy())
        with loading.start([1, 0], ring=ring) as run:
            for batch in run:
                print(batch)
                with batch:
                    if args.device == "cpu":
                        show_cpu(process, batch.region("scgpt"))
                        continue
                    packed = upload(batch, device, copy_stream)
                # Host views are gone; only device storage is used from here.
                fields = gpu_fields(packed, process.output, streams)
                for name, tensor in fields.items():
                    print(f"{name}: {tensor}")
            run.join()
        print(run)


if __name__ == "__main__":
    main()

Download the source. For a smaller CPU example with assertions, use the process guide. For factory arguments, field-view shapes and callback rules, use the process API.