2

I am trying to run a pre-trained model (The MuRIL model). However, it seems that it requires the tensorflow_text package.

When running the saved model with python, this is simple:

import tensorflow as tf
import tensorflow_text # important to load RegexSplitWithOffsets

preprocessor = tf.saved_model.load("./preprocess")
muril = tf.saved_model.load("./muril")

input_word = "testing word"

preprocessed = preprocessor([input_word])
output_vec = muril(preprocessed)["pooled_output"]

print(output_vec)

However, when I do something similar with the Tensorflow C API:

#include <stdio.h>
#include <tensorflow/c/c_api.h>

TF_Status* status;

TF_SessionOptions* preprocess_opts;
TF_Graph* preprocess_graph;
TF_Session* preprocess_sess;

TF_SessionOptions* muril_opts;
TF_Graph* muril_graph;
TF_Session* muril_sess;

int load_models() {

    // create a status reporter
    status = TF_NewStatus();

    // the tag is "serve"
    const char* tags[] = {"serve"};

    // load models

    // load preprocessor model
    char* preprocess_model_path = "./preprocess";
    preprocess_graph = TF_NewGraph();
    preprocess_opts = TF_NewSessionOptions();
    preprocess_sess = TF_LoadSessionFromSavedModel(preprocess_opts, NULL, preprocess_model_path, tags, 1, preprocess_graph, NULL, status);

    if (TF_GetCode(status) != TF_OK) {
        printf("Error loading preprocess model.");
        return 1;
    }

    // load main muril model
    char* muril_model_path = "./muril";
    muril_graph = TF_NewGraph();
    muril_opts = TF_NewSessionOptions();
    muril_sess = TF_LoadSessionFromSavedModel(muril_opts, NULL, muril_model_path, tags, 1, muril_graph, NULL, status);

    if (TF_GetCode(status) != TF_OK) {
        printf("Error loading muril model.");
        return 1;
    }

    return 0;
}

I get a runtime error:

fg-shape-inference{graph-version=0},tfg-prepare-attrs-export)} failed: INVALID_ARGUMENT: Unable to find OpDef for RegexSplitWithOffsets
        While importing function: __inference__wrapped_model_4156
        when importing GraphDef to MLIR module in GrapplerHook
2025-03-10 12:14:25.280346: I tensorflow/cc/saved_model/loader.cc:220] Running initialization op on SavedModel bundle at path: ./preprocess
2025-03-10 12:14:25.300561: E tensorflow/core/grappler/optimizers/tfg_optimizer_hook.cc:135] tfg_optimizer{any(tfg-consolidate-attrs,tfg-toposort,tfg-shape-inference{graph-version=0},tfg-prepare-attrs-export)} failed: INVALID_ARGUMENT: Unable to find OpDef for RegexSplitWithOffsets
        While importing function: __inference__wrapped_model_4156
        when importing GraphDef to MLIR module in GrapplerHook
2025-03-10 12:14:25.342181: I tensorflow/cc/saved_model/loader.cc:466] SavedModel load for tags { serve }; Status: success: OK. Took 154554 microseconds.

I believe this is due to me not having the tensorflow_text package installed for C. I followed the instructions on the tensorflow website to install, and unpacked the tar.gz file into /usr/local/lib which created libtensorflow.so and libtensorflow.so.2 files. But there isn't any tensorflow_text.

I can't find a guide on how to install tensorflow_text for C; does anyone know how?

6
  • what OS do you use? Commented Mar 11 at 6:17
  • @user14063792468 I am using Fedora Linux, but have also tested this on Ubuntu to no avail. If I can get this to work on either Fedora or Ubuntu, that would be perfect. Commented Mar 11 at 15:22
  • then it is good idea to update tags of the question. Commented Mar 11 at 15:42
  • the tensorflow_text github tells that: 1. it is written 50/50 in C++/Python. 2. it is a Python library. so please update tags to C++ and Python. please remove C tag. Commented Mar 11 at 15:54
  • @user14063792468 My question is about the Tensorflow C API though. Specifically, how I can use the tensorflow_text operations in it. Commented Mar 11 at 16:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.