Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scaffolding for int8 calibration in TF-TRT #17309

Merged
merged 13 commits into from
Mar 2, 2018
44 changes: 40 additions & 4 deletions tensorflow/contrib/tensorrt/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ tf_cuda_cc_test(

tf_custom_op_library(
name = "python/ops/_trt_engine_op.so",
srcs = ["ops/trt_engine_op.cc"],
srcs = [
"ops/trt_calib_op.cc",
"ops/trt_engine_op.cc",
],
deps = [
":trt_engine_op_kernel",
":trt_shape_function",
Expand All @@ -71,11 +74,18 @@ tf_cuda_library(

cc_library(
name = "trt_engine_op_kernel",
srcs = ["kernels/trt_engine_op.cc"],
hdrs = ["kernels/trt_engine_op.h"],
srcs = [
"kernels/trt_calib_op.cc",
"kernels/trt_engine_op.cc",
],
hdrs = [
"kernels/trt_calib_op.h",
"kernels/trt_engine_op.h",
],
copts = tf_copts(),
deps = [
":trt_logging",
":trt_resources",
"//tensorflow/core:gpu_headers_lib",
"//tensorflow/core:lib_proto_parsing",
"//tensorflow/core:stream_executor_headers_lib",
Expand All @@ -87,7 +97,10 @@ cc_library(
)

tf_gen_op_libs(
op_lib_names = ["trt_engine_op"],
op_lib_names = [
"trt_engine_op",
"trt_calib_op",
],
deps = if_tensorrt([
"@local_config_tensorrt//:nv_infer",
]),
Expand All @@ -108,6 +121,7 @@ tf_cuda_library(
tf_gen_op_wrapper_py(
name = "trt_engine_op",
deps = [
":trt_calib_op_op_lib",
":trt_engine_op_op_lib",
":trt_logging",
":trt_shape_function",
Expand Down Expand Up @@ -171,6 +185,27 @@ tf_py_wrap_cc(
],
)

tf_cuda_library(
name = "trt_resources",
srcs = [
"resources/trt_int8_calibrator.cc",
"resources/trt_resource_manager.cc",
],
hdrs = [
"resources/trt_int8_calibrator.h",
"resources/trt_resource_manager.h",
"resources/trt_resources.h",
],
deps = [
":trt_logging",
"//tensorflow/core:framework_headers_lib",
"//tensorflow/core:framework_lite",
"//tensorflow/core:lib_proto_parsing",
] + if_tensorrt([
"@local_config_tensorrt//:nv_infer",
]),
)

# Library for the node-level conversion portion of TensorRT operation creation
tf_cuda_library(
name = "trt_conversion",
Expand All @@ -185,6 +220,7 @@ tf_cuda_library(
deps = [
":segment",
":trt_logging",
":trt_resources",
"//tensorflow/core/grappler:grappler_item",
"//tensorflow/core/grappler:utils",
"//tensorflow/core:framework",
Expand Down
129 changes: 129 additions & 0 deletions tensorflow/contrib/tensorrt/kernels/trt_calib_op.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/contrib/tensorrt/kernels/trt_calib_op.h"
#include "tensorflow/contrib/tensorrt/resources/trt_int8_calibrator.h"
#include "tensorflow/contrib/tensorrt/resources/trt_resource_manager.h"
#include "tensorflow/contrib/tensorrt/resources/trt_resources.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/framework/tensor_shape.h"
#include "tensorflow/core/framework/tensor_types.h"
#include "tensorflow/core/framework/types.h"

#if GOOGLE_CUDA
#if GOOGLE_TENSORRT
#include "cuda_runtime_api.h"
#include "tensorrt/include/NvInfer.h"

namespace tensorflow {
namespace tensorrt {

TRTCalibOp::TRTCalibOp(OpKernelConstruction* context) : OpKernel(context) {
OP_REQUIRES_OK(context, context->GetAttr("segment_nodes", &segment_nodes_));
OP_REQUIRES_OK(context, context->GetAttr("input_names", &input_names_));
OP_REQUIRES_OK(context, context->GetAttr("resource_name", &resource_name_));
};

#define TYPECASE(dt, X, Y) \
case dt: { \
return (void*)X->flat<tensorflow::EnumToDataType<dt>::Type>().data(); \
}

void* GetTensorAddress(const Tensor* tensor_ptr) {
auto tensor_type = tensor_ptr->dtype();
switch (tensor_type) {
TYPECASE(tensorflow::DT_FLOAT, tensor_ptr, dest_ptr);
TYPECASE(tensorflow::DT_HALF, tensor_ptr, dest_ptr);
TYPECASE(tensorflow::DT_INT8, tensor_ptr, dest_ptr);
default: {
LOG(FATAL) << "Unsupported Data type "
<< tensorflow::DataTypeString(tensor_type);
return nullptr;
}
}
}

void TRTCalibOp::Compute(tensorflow::OpKernelContext* ctx) {
// TODO(aaroey): make sure ctx->resource_mgr() is used in future PR.
auto trt_rm = tensorflow::tensorrt::TRTResourceManager::instance();
auto res_mgr = trt_rm->getManager("TRTCalibOps");
tensorflow::tensorrt::TRTCalibrationResource* calib_res = nullptr;
auto status = res_mgr->Lookup(resource_name_, resource_name_, &calib_res);

if (!status.ok()) {
ctx->SetStatus(status);
return;
}
int num_inputs = ctx->num_inputs();
// first run instantiate calibrator
if (calib_res->calibrator_ == nullptr) {
dev_tensors_.resize(num_inputs);
int batch_size = ctx->input(0).dim_size(0);
VLOG(1) << " Constructing calibrator";
for (int i = 0; i < num_inputs; i++) {
// allocate workspace on device for inputs
const tensorflow::Tensor& t = ctx->input(i);
OP_REQUIRES_OK(ctx,
ctx->allocate_persistent(t.dtype(), t.shape(),
&dev_tensors_.at(i), nullptr));
const auto device_tensor = dev_tensors_.at(i).AccessTensor(ctx);
CHECK_EQ(t.TotalBytes(), device_tensor->TotalBytes());
void* device_address = GetTensorAddress(device_tensor);
device_buffers_.emplace(input_names_.at(i),
std::pair<void*, size_t>(
device_address, device_tensor->TotalBytes()));
}

calib_res->calibrator_ =
new TRTInt8Calibrator(device_buffers_, batch_size, resource_name_);
string label(resource_name_);
calib_res->thr_ = new std::thread([calib_res, label]() {
VLOG(1) << "Starting calibration thread, Calibration Resource @ "
<< calib_res;
calib_res->builder_->setInt8Calibrator(calib_res->calibrator_);
calib_res->builder_->setInt8Mode(true);
calib_res->engine_ = calib_res->builder_->buildCudaEngine(
*calib_res->network_); // will loop until we terminate calibrator
VLOG(1) << "Calibration loop terminated " << label;
});
VLOG(1) << "initialized calibrator resource";
} // calibrator initialized

// Pass input data to calibrator
std::unordered_map<string, void*> input_data;
for (int i = 0; i < num_inputs; i++) {
const Tensor& t = ctx->input(i);
void* data_address = GetTensorAddress(&t);
const auto device_tensor = dev_tensors_.at(i).AccessTensor(ctx);
CHECK_EQ(t.TotalBytes(),
device_tensor->TotalBytes()); // use the tensor so FW keeps it
input_data.emplace(input_names_.at(i), data_address);
ctx->set_output(i, t);
}
VLOG(2) << "Filled map for sending";
calib_res->calibrator_->setBatch(input_data);
VLOG(2) << "Passed calibration data";
// TODO(aaroey): make sure we wait for the completion of calibration on the
// last batch in future PR.
};

#undef TYPECASE

REGISTER_KERNEL_BUILDER(Name("TRTCalibOp").Device(DEVICE_GPU), TRTCalibOp);

} // namespace tensorrt
} // namespace tensorflow
#endif
#endif
52 changes: 52 additions & 0 deletions tensorflow/contrib/tensorrt/kernels/trt_calib_op.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#ifndef TENSORFLOW_CONTRIB_TENSORRT_KERNELS_TRT_CALIB_OP_H
#define TENSORFLOW_CONTRIB_TENSORRT_KERNELS_TRT_CALIB_OP_H

#include <memory>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#include "tensorflow/core/framework/op.h"
#include "tensorflow/core/framework/op_kernel.h"
#include "tensorflow/core/framework/tensor_shape.h"
#include "tensorflow/core/platform/types.h"

#if GOOGLE_CUDA
#if GOOGLE_TENSORRT
namespace tensorflow {
namespace tensorrt {
// TODO(sami): Convert this to async kernel!
class TRTCalibOp : public OpKernel {
public:
explicit TRTCalibOp(OpKernelConstruction* context);

void Compute(OpKernelContext* context) override;

private:
string resource_name_;
std::vector<string> segment_nodes_;
std::vector<string> input_names_;
std::vector<tensorflow::TensorShape> shapes_;
std::unordered_map<string, std::pair<void*, size_t>> device_buffers_;
std::vector<tensorflow::PersistentTensor> dev_tensors_;
};
} // namespace tensorrt
} // namespace tensorflow
#endif
#endif
#endif // TENSORFLOW_CONTRIB_TENSORRT_KERNELS_TRT_CALIB_OP_H
37 changes: 37 additions & 0 deletions tensorflow/contrib/tensorrt/ops/trt_calib_op.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/core/framework/op.h"
#include "tensorflow/core/framework/shape_inference.h"
namespace tensorflow {

REGISTER_OP("TRTCalibOp")
.Attr("segment_nodes: list(string)") // names of the ops in segment
.Attr("segment_output_names: list(string)") // names of the output ops in
// segment
.Attr("input_names: list(string)") // names of the inputs for
// passing into tensorrt
.Attr("resource_name: string")
.Attr("InT: list({int8, float16, float32})")
.Input("in_tensor: InT")
.Output("out_tensor: InT")
.SetShapeFn([](tensorflow::shape_inference::InferenceContext* c) {
for (int i = 0; i < c->num_inputs(); i++) {
c->set_output(i, c->input(i));
}
return Status::OK();
});

} // namespace tensorflow