Tensor: Make tensor not requiring "-x objective-c++" option.
In this case tensor.h is compiled differently for C++ and Objective-C++ that violates ODR (once definition rule). Tensor has no virtual methods conditionally compiled but some Metal-related data members. Instead, unique_ptr to MtlResources that is declared as forward structure is unconditionally defined in the tensor class. MtlResources is defined differently in cc-file only that compiled just once per project so no ODR violation is here. PiperOrigin-RevId: 504029286
This commit is contained in:
committed by
Copybara-Service
parent
921b6a6bef
commit
1124569c29
@@ -53,14 +53,6 @@ mediapipe_proto_library(
|
||||
cc_library(
|
||||
name = "audio_to_tensor_calculator",
|
||||
srcs = ["audio_to_tensor_calculator.cc"],
|
||||
copts = select({
|
||||
# b/215212850
|
||||
"//mediapipe:apple": [
|
||||
"-x objective-c++",
|
||||
"-fobjc-arc",
|
||||
],
|
||||
"//conditions:default": [],
|
||||
}),
|
||||
deps = [
|
||||
":audio_to_tensor_calculator_cc_proto",
|
||||
"//mediapipe/framework:calculator_framework",
|
||||
@@ -161,14 +153,6 @@ mediapipe_proto_library(
|
||||
cc_library(
|
||||
name = "feedback_tensors_calculator",
|
||||
srcs = ["feedback_tensors_calculator.cc"],
|
||||
copts = select({
|
||||
# b/215212850
|
||||
"//mediapipe:apple": [
|
||||
"-x objective-c++",
|
||||
"-fobjc-arc",
|
||||
],
|
||||
"//conditions:default": [],
|
||||
}),
|
||||
deps = [
|
||||
":feedback_tensors_calculator_cc_proto",
|
||||
"//mediapipe/framework:calculator_framework",
|
||||
@@ -207,14 +191,6 @@ mediapipe_proto_library(
|
||||
cc_library(
|
||||
name = "bert_preprocessor_calculator",
|
||||
srcs = ["bert_preprocessor_calculator.cc"],
|
||||
copts = select({
|
||||
# TODO: fix tensor.h not to require this, if possible
|
||||
"//mediapipe:apple": [
|
||||
"-x objective-c++",
|
||||
"-fobjc-arc", # enable reference-counting
|
||||
],
|
||||
"//conditions:default": [],
|
||||
}),
|
||||
deps = [
|
||||
":bert_preprocessor_calculator_cc_proto",
|
||||
"//mediapipe/framework:calculator_framework",
|
||||
@@ -267,14 +243,6 @@ mediapipe_proto_library(
|
||||
cc_library(
|
||||
name = "regex_preprocessor_calculator",
|
||||
srcs = ["regex_preprocessor_calculator.cc"],
|
||||
copts = select({
|
||||
# TODO: fix tensor.h not to require this, if possible
|
||||
"//mediapipe:apple": [
|
||||
"-x objective-c++",
|
||||
"-fobjc-arc", # enable reference-counting
|
||||
],
|
||||
"//conditions:default": [],
|
||||
}),
|
||||
deps = [
|
||||
":regex_preprocessor_calculator_cc_proto",
|
||||
"//mediapipe/framework:calculator_framework",
|
||||
@@ -316,14 +284,6 @@ cc_test(
|
||||
cc_library(
|
||||
name = "text_to_tensor_calculator",
|
||||
srcs = ["text_to_tensor_calculator.cc"],
|
||||
copts = select({
|
||||
# TODO: fix tensor.h not to require this, if possible
|
||||
"//mediapipe:apple": [
|
||||
"-x objective-c++",
|
||||
"-fobjc-arc", # enable reference-counting
|
||||
],
|
||||
"//conditions:default": [],
|
||||
}),
|
||||
deps = [
|
||||
"//mediapipe/framework:calculator_context",
|
||||
"//mediapipe/framework:calculator_framework",
|
||||
@@ -414,14 +374,6 @@ cc_library(
|
||||
name = "inference_calculator_interface",
|
||||
srcs = ["inference_calculator.cc"],
|
||||
hdrs = ["inference_calculator.h"],
|
||||
copts = select({
|
||||
# TODO: fix tensor.h not to require this, if possible
|
||||
"//mediapipe:apple": [
|
||||
"-x objective-c++",
|
||||
"-fobjc-arc", # enable reference-counting
|
||||
],
|
||||
"//conditions:default": [],
|
||||
}),
|
||||
deps = [
|
||||
":inference_calculator_cc_proto",
|
||||
":inference_calculator_options_lib",
|
||||
@@ -495,6 +447,7 @@ cc_library(
|
||||
tags = ["ios"],
|
||||
deps = [
|
||||
"inference_calculator_interface",
|
||||
"//mediapipe/framework/formats:tensor",
|
||||
"//mediapipe/gpu:MPPMetalHelper",
|
||||
"//mediapipe/gpu:MPPMetalUtil",
|
||||
"//mediapipe/gpu:gpu_buffer",
|
||||
@@ -513,14 +466,6 @@ cc_library(
|
||||
cc_library(
|
||||
name = "inference_runner",
|
||||
hdrs = ["inference_runner.h"],
|
||||
copts = select({
|
||||
# TODO: fix tensor.h not to require this, if possible
|
||||
"//mediapipe:apple": [
|
||||
"-x objective-c++",
|
||||
"-fobjc-arc", # enable reference-counting
|
||||
],
|
||||
"//conditions:default": [],
|
||||
}),
|
||||
deps = [
|
||||
"//mediapipe/framework:calculator_context",
|
||||
"//mediapipe/framework/formats:tensor",
|
||||
@@ -532,14 +477,6 @@ cc_library(
|
||||
name = "inference_interpreter_delegate_runner",
|
||||
srcs = ["inference_interpreter_delegate_runner.cc"],
|
||||
hdrs = ["inference_interpreter_delegate_runner.h"],
|
||||
copts = select({
|
||||
# TODO: fix tensor.h not to require this, if possible
|
||||
"//mediapipe:apple": [
|
||||
"-x objective-c++",
|
||||
"-fobjc-arc", # enable reference-counting
|
||||
],
|
||||
"//conditions:default": [],
|
||||
}),
|
||||
deps = [
|
||||
":inference_runner",
|
||||
"//mediapipe/framework:mediapipe_profiling",
|
||||
@@ -561,14 +498,6 @@ cc_library(
|
||||
srcs = [
|
||||
"inference_calculator_cpu.cc",
|
||||
],
|
||||
copts = select({
|
||||
# TODO: fix tensor.h not to require this, if possible
|
||||
"//mediapipe:apple": [
|
||||
"-x objective-c++",
|
||||
"-fobjc-arc", # enable reference-counting
|
||||
],
|
||||
"//conditions:default": [],
|
||||
}),
|
||||
deps = [
|
||||
":inference_calculator_interface",
|
||||
":inference_calculator_utils",
|
||||
@@ -607,14 +536,6 @@ cc_library(
|
||||
srcs = [
|
||||
"inference_calculator_xnnpack.cc",
|
||||
],
|
||||
copts = select({
|
||||
# TODO: fix tensor.h not to require this, if possible
|
||||
"//mediapipe:apple": [
|
||||
"-x objective-c++",
|
||||
"-fobjc-arc", # enable reference-counting
|
||||
],
|
||||
"//conditions:default": [],
|
||||
}),
|
||||
deps = [
|
||||
":inference_calculator_interface",
|
||||
":inference_calculator_utils",
|
||||
|
||||
@@ -36,6 +36,10 @@
|
||||
#include "tensorflow/lite/delegates/gpu/common/shape.h"
|
||||
#include "tensorflow/lite/delegates/gpu/common/types.h"
|
||||
|
||||
#if MEDIAPIPE_METAL_ENABLED
|
||||
#include "mediapipe/framework/formats/tensor_mtl_buffer_view.h"
|
||||
#endif // MEDIAPIPE_METAL_ENABLED
|
||||
|
||||
namespace mediapipe {
|
||||
|
||||
namespace {
|
||||
@@ -376,7 +380,7 @@ class MetalProcessor : public ImageToTensorConverter {
|
||||
|
||||
id<MTLCommandBuffer> command_buffer = [metal_helper_ commandBuffer];
|
||||
const auto& buffer_view =
|
||||
output_tensor.GetMtlBufferWriteView(command_buffer);
|
||||
MtlBufferView::GetWriteView(output_tensor, command_buffer);
|
||||
MP_RETURN_IF_ERROR(extractor_->Execute(
|
||||
texture, roi,
|
||||
/*flip_horizontaly=*/false, transform.scale, transform.offset,
|
||||
|
||||
@@ -24,6 +24,8 @@
|
||||
#include "absl/memory/memory.h"
|
||||
#include "absl/strings/str_format.h"
|
||||
#include "mediapipe/calculators/tensor/inference_calculator.h"
|
||||
#include "mediapipe/framework/formats/tensor.h"
|
||||
#include "mediapipe/framework/formats/tensor_mtl_buffer_view.h"
|
||||
#import "mediapipe/gpu/MPPMetalHelper.h"
|
||||
#include "mediapipe/gpu/MPPMetalUtil.h"
|
||||
#include "mediapipe/gpu/gpu_buffer.h"
|
||||
@@ -150,11 +152,12 @@ absl::Status InferenceCalculatorMetalImpl::Process(CalculatorContext* cc) {
|
||||
command_buffer.label = @"InferenceCalculator";
|
||||
// Explicit copy input with conversion float 32 bits to 16 bits.
|
||||
for (int i = 0; i < input_tensors.size(); ++i) {
|
||||
auto input_view = input_tensors[i].GetMtlBufferReadView(command_buffer);
|
||||
auto input_view =
|
||||
MtlBufferView::GetReadView(input_tensors[i], command_buffer);
|
||||
// Reshape tensor.
|
||||
tflite::gpu::BHWC shape = BhwcFromTensorShape(input_tensors[i].shape());
|
||||
auto gpu_buffer_view =
|
||||
gpu_buffers_in_[i]->GetMtlBufferWriteView(command_buffer);
|
||||
MtlBufferView::GetWriteView(*gpu_buffers_in_[i], command_buffer);
|
||||
id<MTLComputeCommandEncoder> input_encoder =
|
||||
[command_buffer computeCommandEncoder];
|
||||
[converter_to_BPHWC4_ convertWithEncoder:input_encoder
|
||||
@@ -174,9 +177,10 @@ absl::Status InferenceCalculatorMetalImpl::Process(CalculatorContext* cc) {
|
||||
output_shapes_[i]);
|
||||
// Reshape tensor.
|
||||
tflite::gpu::BHWC shape = BhwcFromTensorShape(output_shapes_[i]);
|
||||
auto read_view = gpu_buffers_out_[i]->GetMtlBufferReadView(command_buffer);
|
||||
auto read_view =
|
||||
MtlBufferView::GetReadView(*gpu_buffers_out_[i], command_buffer);
|
||||
auto write_view =
|
||||
output_tensors->at(i).GetMtlBufferWriteView(command_buffer);
|
||||
MtlBufferView::GetWriteView(output_tensors->at(i), command_buffer);
|
||||
id<MTLComputeCommandEncoder> output_encoder =
|
||||
[command_buffer computeCommandEncoder];
|
||||
[converter_from_BPHWC4_ convertWithEncoder:output_encoder
|
||||
@@ -258,7 +262,7 @@ absl::Status InferenceCalculatorMetalImpl::CreateConverters(
|
||||
: Tensor::ElementType::kFloat32,
|
||||
Tensor::Shape{dims}));
|
||||
auto buffer_view =
|
||||
gpu_buffers_in_[i]->GetMtlBufferWriteView(gpu_helper_.mtlDevice);
|
||||
MtlBufferView::GetWriteView(*gpu_buffers_in_[i], gpu_helper_.mtlDevice);
|
||||
RET_CHECK_EQ(TFLGpuDelegateBindMetalBufferToTensor(
|
||||
delegate_.get(), input_indices[i], buffer_view.buffer()),
|
||||
true);
|
||||
@@ -286,8 +290,8 @@ absl::Status InferenceCalculatorMetalImpl::CreateConverters(
|
||||
Tensor::Shape{dims}));
|
||||
RET_CHECK_EQ(TFLGpuDelegateBindMetalBufferToTensor(
|
||||
delegate_.get(), output_indices[i],
|
||||
gpu_buffers_out_[i]
|
||||
->GetMtlBufferWriteView(gpu_helper_.mtlDevice)
|
||||
MtlBufferView::GetWriteView(*gpu_buffers_out_[i],
|
||||
gpu_helper_.mtlDevice)
|
||||
.buffer()),
|
||||
true);
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@
|
||||
#import <Metal/Metal.h>
|
||||
#import <MetalKit/MetalKit.h>
|
||||
|
||||
#include "mediapipe/framework/formats/tensor_mtl_buffer_view.h"
|
||||
#import "mediapipe/gpu/MPPMetalHelper.h"
|
||||
#elif MEDIAPIPE_OPENGL_ES_VERSION >= MEDIAPIPE_OPENGL_ES_30
|
||||
#include "mediapipe/gpu/gl_calculator_helper.h"
|
||||
@@ -304,7 +305,7 @@ absl::Status TensorConverterCalculator::ProcessGPU(CalculatorContext* cc) {
|
||||
id<MTLTexture> src_texture = [gpu_helper_ metalTextureWithGpuBuffer:input];
|
||||
[compute_encoder setTexture:src_texture atIndex:0];
|
||||
auto output_view =
|
||||
output_tensors->at(0).GetMtlBufferWriteView(command_buffer);
|
||||
MtlBufferView::GetWriteView(output_tensors->at(0), command_buffer);
|
||||
[compute_encoder setBuffer:output_view.buffer() offset:0 atIndex:1];
|
||||
MTLSize threads_per_group = MTLSizeMake(kWorkgroupSize, kWorkgroupSize, 1);
|
||||
MTLSize threadgroups =
|
||||
|
||||
@@ -41,6 +41,7 @@
|
||||
#import <Metal/Metal.h>
|
||||
#import <MetalKit/MetalKit.h>
|
||||
|
||||
#include "mediapipe/framework/formats/tensor_mtl_buffer_view.h"
|
||||
#import "mediapipe/gpu/MPPMetalHelper.h"
|
||||
#include "mediapipe/gpu/MPPMetalUtil.h"
|
||||
#endif // MEDIAPIPE_METAL_ENABLED
|
||||
@@ -536,10 +537,11 @@ absl::Status TensorsToDetectionsCalculator::ProcessGPU(
|
||||
if (input_tensors.size() == kNumInputTensorsWithAnchors) {
|
||||
RET_CHECK_EQ(input_tensors.size(), kNumInputTensorsWithAnchors);
|
||||
auto command_buffer = [gpu_helper_ commandBuffer];
|
||||
auto src_buffer = input_tensors[tensor_mapping_.anchors_tensor_index()]
|
||||
.GetMtlBufferReadView(command_buffer);
|
||||
auto src_buffer = MtlBufferView::GetReadView(
|
||||
input_tensors[tensor_mapping_.anchors_tensor_index()],
|
||||
command_buffer);
|
||||
auto dest_buffer =
|
||||
raw_anchors_buffer_->GetMtlBufferWriteView(command_buffer);
|
||||
MtlBufferView::GetWriteView(*raw_anchors_buffer_, command_buffer);
|
||||
id<MTLBlitCommandEncoder> blit_command =
|
||||
[command_buffer blitCommandEncoder];
|
||||
[blit_command copyFromBuffer:src_buffer.buffer()
|
||||
@@ -571,15 +573,16 @@ absl::Status TensorsToDetectionsCalculator::ProcessGPU(
|
||||
[command_encoder setComputePipelineState:decode_program_];
|
||||
{
|
||||
auto scored_boxes_view =
|
||||
scored_boxes_buffer_->GetMtlBufferWriteView(command_buffer);
|
||||
MtlBufferView::GetWriteView(*scored_boxes_buffer_, command_buffer);
|
||||
auto decoded_boxes_view =
|
||||
decoded_boxes_buffer_->GetMtlBufferWriteView(command_buffer);
|
||||
MtlBufferView::GetWriteView(*decoded_boxes_buffer_, command_buffer);
|
||||
[command_encoder setBuffer:decoded_boxes_view.buffer() offset:0 atIndex:0];
|
||||
auto input0_view = input_tensors[tensor_mapping_.detections_tensor_index()]
|
||||
.GetMtlBufferReadView(command_buffer);
|
||||
auto input0_view = MtlBufferView::GetReadView(
|
||||
input_tensors[tensor_mapping_.detections_tensor_index()],
|
||||
command_buffer);
|
||||
[command_encoder setBuffer:input0_view.buffer() offset:0 atIndex:1];
|
||||
auto raw_anchors_view =
|
||||
raw_anchors_buffer_->GetMtlBufferReadView(command_buffer);
|
||||
MtlBufferView::GetReadView(*raw_anchors_buffer_, command_buffer);
|
||||
[command_encoder setBuffer:raw_anchors_view.buffer() offset:0 atIndex:2];
|
||||
MTLSize decode_threads_per_group = MTLSizeMake(1, 1, 1);
|
||||
MTLSize decode_threadgroups = MTLSizeMake(num_boxes_, 1, 1);
|
||||
@@ -588,8 +591,8 @@ absl::Status TensorsToDetectionsCalculator::ProcessGPU(
|
||||
|
||||
[command_encoder setComputePipelineState:score_program_];
|
||||
[command_encoder setBuffer:scored_boxes_view.buffer() offset:0 atIndex:0];
|
||||
auto input1_view = input_tensors[tensor_mapping_.scores_tensor_index()]
|
||||
.GetMtlBufferReadView(command_buffer);
|
||||
auto input1_view = MtlBufferView::GetReadView(
|
||||
input_tensors[tensor_mapping_.scores_tensor_index()], command_buffer);
|
||||
[command_encoder setBuffer:input1_view.buffer() offset:0 atIndex:1];
|
||||
MTLSize score_threads_per_group = MTLSizeMake(1, num_classes_, 1);
|
||||
MTLSize score_threadgroups = MTLSizeMake(num_boxes_, 1, 1);
|
||||
|
||||
@@ -53,6 +53,7 @@
|
||||
#import <Metal/Metal.h>
|
||||
#import <MetalKit/MetalKit.h>
|
||||
|
||||
#include "mediapipe/framework/formats/tensor_mtl_buffer_view.h"
|
||||
#import "mediapipe/gpu/MPPMetalHelper.h"
|
||||
#include "mediapipe/gpu/MPPMetalUtil.h"
|
||||
#endif // MEDIAPIPE_METAL_ENABLED
|
||||
@@ -485,7 +486,8 @@ absl::Status TensorsToSegmentationCalculator::ProcessGpu(
|
||||
[command_buffer computeCommandEncoder];
|
||||
[command_encoder setComputePipelineState:mask_program_];
|
||||
|
||||
auto read_view = input_tensors[0].GetMtlBufferReadView(command_buffer);
|
||||
auto read_view =
|
||||
MtlBufferView::GetReadView(input_tensors[0], command_buffer);
|
||||
[command_encoder setBuffer:read_view.buffer() offset:0 atIndex:0];
|
||||
|
||||
mediapipe::GpuBuffer small_mask_buffer = [metal_helper_
|
||||
|
||||
Reference in New Issue
Block a user