Hello! I have recently been trying to add a custom operator to tensorflow that requires me to perform a custom build. Unfortunately, I am unable to register the operator and the following error occurs in Python when the operator is requested: AttributeError: module '012ff3e36e3c24aefc4a3a7b68a03fedd1e7a7e1' has no attribute 'Resample'
The commands I am using to build tensorflow with the custom operator are the following (in order):
bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package --local_ram_resources=4096 --cxxopt="-D_GLIBCXX_USE_CXX11_ABI=0"
./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
pip install /tmp/tensorflow_pkg/tensorflow-2.5.3-cp36-cp36m-linux_x86_64.whl
bazel build --config=opt //tensorflow/core/user_ops:Resampler.so --local_ram_resources=6000 --cxxopt="-D_GLIBCXX_USE_CXX11_ABI=0"
This is after moving the operators into the tensorflow/tensorflow/core/user_ops
directory along with a Bazel build file that looks like the following:
load(
"//tensorflow/core/platform:rules_cc.bzl",
"cc_library",
)
load(
"//tensorflow:tensorflow.bzl",
"tf_copts",
)
package(
default_visibility = [
"//tensorflow/core:__pkg__",
],
licenses = ["notice"],
)
cc_library(
name = "user_ops_op_lib",
srcs = glob(["*.cc"]),
hdrs = glob(["*.h"]),
copts = tf_copts(),
linkstatic = 1,
visibility = ["//tensorflow/core:__pkg__"],
deps = ["//tensorflow/core:framework"],
alwayslink = 1,
)
load("//tensorflow:tensorflow.bzl", "tf_custom_op_library")
tf_custom_op_library(
name = "Resampler.so",
The tensorflow version being targeted is 2.5.x. Note that the custom operator also contains the following registration code within Resampler.cc:
REGISTER_OP("Resample")
.Attr("T: {float, int32}")
.Input("input_image: T")
.Input("transformation: float")
.Input("output_size: int32")
.Output("output_image: T")
...
#define REGISTER_CPU(T) \
REGISTER_KERNEL_BUILDER( \
Name("Resample").Device(DEVICE_CPU).TypeConstraint<T>("T"), \
ResamplerOp<CPUDevice, T>);
Oddly enough, it seems that if I then rename the operator function in my code and continue trying to rebuild, sometimes the operator eventually gets registered. But trying again from scratch with the new name does not work making me think that something is wrong with my order of operations here. I have yet to find a reproducible order of events to get the operator to be registered successfully, so any help would be appreciated!
Duplicate Stack Overflow question for reference: python - Unable to register custom compiled TensorFlow operator - Stack Overflow