Learn R Programming

ggmlR (version 0.8.4)

ggml_custom: Custom Operation Node

Description

Creates a graph node whose value is computed by a C kernel registered under name. The kernel is looked up in ggmlR's custom-op registry; supplying an unregistered name is an error, listing the names that are available.

Usage

ggml_custom(
  ctx,
  name,
  args = NULL,
  ne,
  type = GGML_TYPE_F32,
  n_tasks = GGML_N_TASKS_MAX
)

Value

Tensor pointer for the custom node

Arguments

ctx

GGML context

name

Name of a registered custom op (character scalar)

args

List of input tensors, or NULL for none. At most 9 tensors.

ne

Output shape as a numeric vector of up to 4 dimensions, innermost first (ggml column-major order). Missing dimensions default to 1.

type

Output data type (default GGML_TYPE_F32)

n_tasks

Number of threads the kernel may be split across, or GGML_N_TASKS_MAX (-1) to use as many as the backend allows. Use 1 for a kernel that is not thread-safe.

Backend

Custom ops run on the CPU backend only, because the kernel is a host function pointer that a GPU backend cannot execute. Computing a graph that contains a custom node raises an error when the target backend is not the CPU, or when a scheduler has no CPU backend to fall back to. Keep custom nodes out of graphs you intend to run entirely on Vulkan.

Details

Kernels are registered from C, not from R. A package that links against ggmlR registers its kernels at load time via the ggmlR_register_custom_op callable; see system.file("include", "ggmlR.h", package = "ggmlR") for the C-side contract and a worked example. R callbacks are not supported: the kernel may run off the main thread, where calling into R is unsafe.

The output tensor is newly allocated with the given type and shape. Input tensors passed in args become the node's sources and are visible to the kernel as dst->src[0], dst->src[1], and so on.

See Also

ggml_custom_inplace, ggml_custom_ops

Examples

Run this code
# "row_median" is one of ggmlR's built-in kernels; see ggml_custom_ops().
ggml_set_n_threads(1L)
ctx <- ggml_init(16 * 1024 * 1024)
x <- ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 5, 2)
ggml_set_f32(x, c(5, 1, 3, 2, 4,
                  9, 7, 8, 6, 10))

# One median per row: the output collapses ne[1] to 1.
med <- ggml_custom(ctx, "row_median", args = list(x), ne = c(1, 2))

graph <- ggml_build_forward_expand(ctx, med)
ggml_graph_compute(ctx, graph)
ggml_get_f32(med)   # 3, 8
ggml_free(ctx)

Run the code above in your browser using DataLab