Skip to content

C API

rbln_get_layout_nbytes

Retrieves the number of bytes occupied for RBLNTensorLayout

uint64_t rbln_get_layout_nbytes(const RBLNTensorLayout * layout)

This function returns the total number of bytes occupied by the given tensor layout.

parameters:

Name Description
layout [in] Pointer to the RBLNTensorLayout

return: The number of bytes used by the tensor layout.

Usages in examples: #L15, #L35, #L117

rbln_create_model

Creates an RBLN compiled model instance from a RBLN compiled model file.

RBLNModel * rbln_create_model(const char * path)

This function loads a compiled RBLN model from the specified file path and creates an RBLN model instance.

parameters:

Name Description
path [in] Path to the RBLN model file (*.rbln).

return: Pointer to the created RBLN model, or NULL if loading fails. *

Usages in examples: #L50, #L101

rbln_destroy_model

Destroys the specified RBLN model handle.

void rbln_destroy_model(RBLNModel * handle)

This function releases the resources associated with the given RBLN model handle.

parameters:

Name Description
handle [in] Handle to the RBLN model to be destroyed.

Usages in examples: #L89, #L132

rbln_create_runtime

Creates an RBLN synchronous runtime from a compiled RBLN model.

RBLNRuntime * rbln_create_runtime(
  RBLNModel * model,
  const char * name,
  int device_id,
)

This function initializes and creates an RBLN synchronous runtime using the specified compiled model, module name, and device ID.

parameters:

Name Description
model [in] Handle to the compiled RBLN model.
name [in] Name of the module.
device_id [in] Device number to be used for the runtime.

return: Pointer to the created RBLN Runtime, or NULL if creation fails.

Usage in examples: #L53

rbln_create_async_runtime

Creates an RBLN asynchronous runtime from a compiled RBLN model.

RBLNRuntime * rbln_create_async_runtime(
  RBLNModel * model,
  const char * name,
  int device_id,
)

This function initializes and creates an RBLN asynchronous runtime using the specified compiled model, module name, and device ID.

parameters:

Name Description
model [in] Handle to the compiled RBLN model.
name [in] Name of the module.
device_id [in] Device number to be used for the runtime.

return: Pointer to the created RBLN asynchronous runtime, or NULL if creation fails.

Usage in examples: #L104

rbln_destroy_runtime

Destroys the specified RBLN Runtime handle.

void rbln_destroy_runtime(RBLNRuntime * rt)

This function releases the resources associated with the given RBLN Runtime handle.

parameters:

Name Description
rt [in] Handle to the RBLN Runtime to be destroyed.

Usages in examples: #L86, #L129

rbln_get_num_inputs

Retrieves the number of input buffers for the specified RBLN Runtime.

uint32_t rbln_get_num_inputs(RBLNRuntime * rt)

This function returns the total number of input buffers required by the given RBLN Runtime instance.

parameters:

Name Description
rt [in] Handle to the RBLN Runtime.

return: The number of input buffers.

Usage in examples: #L56

rbln_get_num_outputs

Retrieves the number of output buffers for the specified RBLN Runtime.

uint32_t rbln_get_num_outputs(RBLNRuntime * rt)

This function returns the total number of output buffers required by the given RBLN Runtime instance.

parameters:

Name Description
rt [in] Handle to the RBLN Runtime.

return: The number of output buffers.

Usages in examples: #L73, #L107

rbln_get_input_layout

Retrieves the layout information of the specified input buffer.

const RBLNTensorLayout * rbln_get_input_layout(
  RBLNRuntime * rt,
  uint64_t idx,
)

This function returns the tensor layout of the input buffer at the given index for the specified RBLN Runtime instance.

parameters:

Name Description
rt [in] Handle to the RBLN Runtime.
idx [in] Index of the input buffer.

return: Pointer to the tensor layout of the input buffer.

Usages in examples: #L13, #L115

rbln_get_output_layout

Retrieves the layout of the specified output buffer.

const RBLNTensorLayout * rbln_get_output_layout(
  RBLNRuntime * rt,
  uint64_t idx,
)

This function returns the tensor layout of the output buffer at the given index for the specified RBLN Runtime instance.

parameters:

Name Description
rt [in] Handle to the RBLN Runtime.
idx [in] Index of the output buffer.

return: Pointer to the tensor layout of the output buffer.

Usage in examples: #L35

rbln_set_input

Assigns input data to the specified input buffer.

RBLNRetCode rbln_set_input(
  RBLNRuntime * rt,
  uint64_t idx,
  const void * data,
)

This function sets the input data for the input buffer at the given index for the specified RBLN Runtime instance.

parameters:

Name Description
rt [in] Handle to the RBLN Runtime.
idx [in] Index of the input buffer.
data [in] Pointer to the input data.

return: 0 on success, or an error code on failure.

Usage in examples: #L60

rbln_set_input_by_name

Assigns input data to the specified input buffer by name.

RBLNRetCode rbln_set_input_by_name(
  RBLNRuntime * rt,
  const char * name,
  const void * data,
)

This function sets the input data for the input buffer with the specified name for the RBLN Runtime instance.

parameters:

Name Description
rt [in] Handle to the RBLN Runtime.
name [in] Name of the input buffer.
data [in] Pointer to the input data.

return: 0 on success, or an error code on failure.

Usage in examples: #L28

rbln_get_input_name

Get the name to the specified input buffer by index.

const char * rbln_get_input_name(
  RBLNRuntime * rt,
  uint64_t idx,
)

This function gets the name for the input buffer with the specified index for the RBLN Runtime instance.

parameters:

Name Description
rt [in] Handle to the RBLN Runtime.
idx [in] Index of the input buffer.

return: name to the input buffer at the specified index

Usage in examples: #L25

rbln_run

Triggers a synchronous inference for the RBLN Runtime.

RBLNRetCode rbln_run(RBLNRuntime * rt)

This function starts the inference process for the specified RBLN Runtime instance.

parameters:

Name Description
rt [in] Pointer to the RBLN Runtime.

return: 0 on success, or an error code on failure.

Usage in examples: #L70

rbln_async_run

Triggers An asynchronous inference for the RBLN Runtime.

int rbln_async_run(
  RBLNRuntime * rt,
  const void * inputs,
  const void * outputs,
)

This function starts the asynchronous inference process for the specified RBLN Runtime instance, using the provided input and output buffers.

parameters:

Name Description
rt [in] Pointer to the RBLN Runtime.
inputs [in] Pointer to the input buffers.
outputs [in] Pointer to the output buffers.

return: The request index on success, or an error code on failure.

Usage in examples: #L122

rbln_async_wait

Waits for the completion of an asynchronous inference request in the RBLN Runtime.

RBLNRetCode rbln_async_wait(
  RBLNRuntime * rt,
  int rid,
  int timeout,
)

This function blocks until the specified asynchronous inference request is complete or the specified timeout period elapses.

parameters:

Name Description
rt [in] Pointer to the RBLN Runtime instance.
rid [in] Index of the request to wait for.
timeout [in] Maximum time to wait for the request to complete, in milliseconds.

return: Retrieves 0 on success, or an error code on failure.

Usage in examples: #L126

rbln_get_input

Retrieves a pointer to the specified input buffer.

void * rbln_get_input(
  RBLNRuntime * rt,
  uint64_t idx,
)

This function returns a pointer to the input buffer at the given index

parameters:

Name Description
rt [in] Pointer to the RBLN Runtime instance.
idx [in] Index of the input buffer.

return: Pointer to the input buffer at the specified index, or NULL if the index is invalid.

Usage in examples: #L10

rbln_get_output

Retrieves a pointer to the specified output buffer.

void * rbln_get_output(
  RBLNRuntime * rt,
  uint64_t idx,
)

This function returns a pointer to the output buffer at the given index.

parameters:

Name Description
rt [in] Pointer to the RBLN Runtime instance.
idx [in] Index of the output buffer.

return: Pointer to the output buffer at the specified index, or NULL if the index is invalid.

Usages in examples: #L38, #L77

RBLNModel

struct RBLNModel
typedef struct RBLNModel RBLNModel

RBLNRuntime

struct RBLNRuntime
typedef struct RBLNRuntime RBLNRuntime

RBLNTensorLayout

Type Name Description
RBLNDType dtype Data type.
size_t elemsize byte per elements
int ndim Number of dimension.
int32_t[256] shape Shape of tensor.
char[256] name Name of the tensor (optional)

enum RBLNRetCode

values
RBLNRetCode_SUCCESS = 0
RBLNRetCode_FAILURE
RBLNRetCode_INVALID

enum RBLNDType

values
NONE = -1
INT8 = 0
FLOAT16
FLOAT32
INT16
INT32
INT64
DTYPE_MAX

Example

#include <rbln/rbln.h>
#include <vector>

/*
 * Alternatively, you can directly update the input buffer as shown below
 */
void UpdateBufferByIndex(RBLNRuntime *rt, int idx,
                         const std::vector<char> &buf) {
  // Gets a pointer to the input buffer at the specified index
  void *input_ptr = rbln_get_input(rt, idx);

  // Gets the layout info. of the input buffer at the specified index
  const RBLNTensorLayout *layout = rbln_get_input_layout(rt, idx);
  // Gets the number of bytes occupied for RBLNTensorLayout
  auto buf_size = rbln_get_layout_nbytes(layout);

  // If the input buffer has a shape of [1, 3, 340, 340] and a float
  // type, the buf_size will be 1 * 3 * 340 * 340 * 4 bytes.
  memcpy(input_ptr, buf.data(), buf_size);
}

void UpdateBufferByName(RBLNRuntime *rt, int idx,
                        const std::vector<char> &buf) {
  // Gets a name to the input buffer at the specified index
  const char *name = rbln_get_input_name(rt, idx);

  // Assigns input data to the input buffer at the specific name
  rbln_set_input_by_name(rt, name, buf.data());
}

/**
 * Alternatively, you can get the Output buffer as shown below
 */
std::vector<float> GetOutputBuffer(RBLNRuntime *rt, int idx) {
  auto buf_size = rbln_get_layout_nbytes(rbln_get_output_layout(rt, idx));
  std::vector<float> buf(buf_size / sizeof(float));

  void *data = rbln_get_output(rt, idx);
  memcpy(buf.data(), data, buf_size);
  return buf;
}

int main() {
  std::string model_path = "${MODEL_PATH}";

  // A sample array of a single dimension
  std::vector<char> sample_input(128, 0);

  // Creates an RBLN compiled model instance from a RBLN compiled model file
  RBLNModel *mod = rbln_create_model(model_path.c_str());

  // Creates an RBLN synchronous runtime from a compiled RBLN model
  RBLNRuntime *rt = rbln_create_runtime(mod, "default", 0);

  // Gets the number of input buffers for the specified RBLN Runtime
  uint32_t n_in = rbln_get_num_inputs(rt);

  // Assigns input data to the input buffer at the specific index
  for (int idx = 0; idx < n_in; idx++) {
    rbln_set_input(rt, idx, sample_input.data());
    /**
     * Alternatively, you can update the input buffer with below
     * functions:
     * UpdateBufferByIndex(rt, idx, sample_input);
     * UpdateBufferByName(rt, idx, sample_input);
     */
  }

  // Triggers a synchronous inference for the RBLN Runtime
  rbln_run(rt);

  // Gets the number of output buffers for the specified RBLN Runtime
  uint32_t n_out = rbln_get_num_outputs(rt);

  // Gets a pointer to the output buffer at the specified index
  for (int idx = 0; idx < n_in; idx++) {
    void *outputs = rbln_get_output(rt, idx);

    /**
     * Alternatively, you can get the Output buffer with below function:
     * std::vector<float> out = GetOutputBuffer(rt, idx);
     */
  }

  // Destroys the specified RBLN Runtime handle
  rbln_destroy_runtime(rt);

  // Destroys the specified RBLN model handle.
  rbln_destroy_model(mod);

  return 0;
}

int main_async() {
  std::string model_path = "${MODEL_PATH}";

  // A sample array of a single dimension
  std::vector<float> sample_input(128, 0);

  // Creates an RBLN compiled model instance from a RBLN compiled model file
  RBLNModel *mod = rbln_create_model(model_path.c_str());

  // Creates an RBLN asynchronous runtime from a compiled RBLN model
  RBLNRuntime *rt = rbln_create_async_runtime(mod, "default", 0);

  // Gets the number of output buffers for the specified RBLN Runtime
  uint32_t n_out = rbln_get_num_outputs(rt);

  /**
   * For this example, we assume that the model has a single output buffer
   * (n_out = 1).
   */

  // Gets the layout information of the input buffer at the specified index
  const RBLNTensorLayout *layout = rbln_get_input_layout(rt, 0);
  // Gets the layout of the output buffer at the specified index
  auto buf_size = rbln_get_layout_nbytes(layout);
  // Allocate output buffers
  std::vector<char> outputs(buf_size / sizeof(char));

  // Triggers An asynchronous inference for the RBLN Runtime
  int rid = rbln_async_run(rt, sample_input.data(), outputs.data());

  // Waits for the completion of an specified async inference for the RBLN
  // runtime
  rbln_async_wait(rt, rid, 1000);

  // Destroys the specified RBLN Runtime handle
  rbln_destroy_runtime(rt);

  // Destroys the specified RBLN model handle.
  rbln_destroy_model(mod);

  return 0;
}