Skip to content

Commit

Permalink
Roll //third_party/mediapipe
Browse files Browse the repository at this point in the history
Changes since last roll:

google-ai-edge/mediapipe@06cc6d1...d73ef24

Only local change is adding new proto to BUILD.gn.

Bug: b/304353973
Change-Id: I4c080cb102f0432699910f198986e3f3ca976a79
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4985362
Reviewed-by: Sophie Chang <sophiechang@chromium.org>
Commit-Queue: Clark DuVall <cduvall@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1216437}
  • Loading branch information
clarkduvall authored and Chromium LUCI CQ committed Oct 27, 2023
1 parent efd4b73 commit 99b2fe8
Show file tree
Hide file tree
Showing 37 changed files with 431 additions and 134 deletions.
2 changes: 2 additions & 0 deletions third_party/mediapipe/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ config("mediapipe_internal_config") {
cflags = [
"-Wno-unused-function",
"-Wno-deprecated-declarations",

# MediaPipe's CHECK macros are redefined from absl's.
"-Wno-macro-redefined",
]
Expand Down Expand Up @@ -132,6 +133,7 @@ proto_library("mediapipe_proto") {
"src/mediapipe/tasks/cc/components/processors/proto/classifier_options.proto",
"src/mediapipe/tasks/cc/components/processors/proto/detection_postprocessing_graph_options.proto",
"src/mediapipe/tasks/cc/components/processors/proto/detector_options.proto",
"src/mediapipe/tasks/cc/components/processors/proto/llm_params.proto",
"src/mediapipe/tasks/cc/components/processors/proto/text_model_type.proto",
"src/mediapipe/tasks/cc/components/processors/proto/text_preprocessing_graph_options.proto",
"src/mediapipe/tasks/cc/components/processors/proto/transformer_params.proto",
Expand Down
4 changes: 2 additions & 2 deletions third_party/mediapipe/README.chromium
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Name: MediaPipe
Short Name: mediapipe
URL: https://github.com/google/mediapipe
Version: 06cc6d1546788fbdedd0babe41a51d3fa8947d6e
Date: 2023-10-18
Version: d73ef2440631d36e90bc3964129066e096b63127
Date: 2023-10-27
License: Apache 2.0
License File: LICENSE
Security Critical: Yes
Expand Down
3 changes: 3 additions & 0 deletions third_party/mediapipe/src/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ build:darwin_arm64 --apple_platform_type=macos
build:darwin_arm64 --macos_minimum_os=10.16
build:darwin_arm64 --cpu=darwin_arm64

# Turn off maximum stdout size
build --experimental_ui_max_stdouterr_bytes=-1

# This bazelrc file is meant to be written by a setup script.
try-import %workspace%/.configure.bazelrc

Expand Down
3 changes: 3 additions & 0 deletions third_party/mediapipe/src/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,9 @@ http_archive(
"@//third_party:org_tensorflow_system_python.diff",
# Diff is generated with a script, don't update it manually.
"@//third_party:org_tensorflow_custom_ops.diff",
# Works around Bazel issue with objc_library.
# See https://github.com/bazelbuild/bazel/issues/19912
"@//third_party:org_tensorflow_objc_build_fixes.diff",
],
patch_args = [
"-p1",
Expand Down
8 changes: 4 additions & 4 deletions third_party/mediapipe/src/mediapipe/calculators/core/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ licenses(["notice"])
package(default_visibility = ["//visibility:public"])

selects.config_setting_group(
name = "ios_or_disable_gpu",
name = "apple_or_disable_gpu",
match_any = [
"//mediapipe/gpu:disable_gpu",
"//mediapipe:ios",
"//mediapipe:apple",
],
)

Expand Down Expand Up @@ -299,7 +299,7 @@ cc_library(
"//mediapipe/util:render_data_cc_proto",
"@org_tensorflow//tensorflow/lite:framework",
] + select({
":ios_or_disable_gpu": [],
":apple_or_disable_gpu": [],
"//conditions:default": [
"@org_tensorflow//tensorflow/lite/delegates/gpu/gl:gl_buffer",
],
Expand Down Expand Up @@ -913,7 +913,7 @@ cc_library(
"@org_tensorflow//tensorflow/lite:framework",
"@org_tensorflow//tensorflow/lite/kernels:builtin_ops",
] + select({
":ios_or_disable_gpu": [],
":apple_or_disable_gpu": [],
"//conditions:default": [
"@org_tensorflow//tensorflow/lite/delegates/gpu/gl:gl_buffer",
],
Expand Down
27 changes: 18 additions & 9 deletions third_party/mediapipe/src/mediapipe/calculators/tensor/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,18 @@ more_selects.config_setting_negation(
)

selects.config_setting_group(
name = "platform_ios_with_gpu",
name = "platform_apple_with_gpu",
match_all = [
":not_disable_gpu",
"//mediapipe:ios",
"//mediapipe:apple",
],
)

selects.config_setting_group(
name = "platform_apple_without_gpu",
match_all = [
":disable_gpu",
"//mediapipe:apple",
],
)

Expand Down Expand Up @@ -614,7 +622,7 @@ cc_library(
":inference_calculator_interface",
] + select({
"//conditions:default": [":inference_calculator_gl_if_compute_shader_available"],
":platform_ios_with_gpu": [":inference_calculator_metal"],
":platform_apple_with_gpu": [":inference_calculator_metal"],
}),
alwayslink = 1,
)
Expand Down Expand Up @@ -687,12 +695,13 @@ cc_library(
"//mediapipe/gpu:gl_calculator_helper",
"//mediapipe/gpu:gpu_buffer",
],
"//mediapipe:ios": [
":platform_apple_with_gpu": [
"//mediapipe/gpu:MPPMetalHelper",
"//mediapipe/gpu:MPPMetalUtil",
"//mediapipe/objc:mediapipe_framework_ios",
],
"//mediapipe:macos": [],
# This setting is needed to allow bazel to build all targets on Mac with GPU disabled
":platform_apple_without_gpu": [],
"//conditions:default": [
"//mediapipe/gpu:gl_calculator_helper",
"//mediapipe/gpu:gl_simple_shaders",
Expand Down Expand Up @@ -777,11 +786,12 @@ cc_library(
name = "tensors_to_detections_calculator_gpu_deps",
visibility = ["//visibility:private"],
deps = select({
"//mediapipe:ios": [
":platform_apple_with_gpu": [
"//mediapipe/gpu:MPPMetalHelper",
"//mediapipe/gpu:MPPMetalUtil",
],
"//mediapipe:macos": [],
# This setting is needed to allow bazel to build all targets on Mac with GPU disabled
":platform_apple_without_gpu": [],
"//conditions:default": [
"//mediapipe/gpu:gl_calculator_helper",
],
Expand Down Expand Up @@ -1428,7 +1438,7 @@ cc_library(
],
}) + selects.with_or({
":gpu_inference_disabled": [],
"//mediapipe:ios": [
":platform_apple_with_gpu": [
"//mediapipe/gpu:MPPMetalUtil",
"//mediapipe/gpu:MPPMetalHelper",
"//third_party/apple_frameworks:MetalKit",
Expand Down Expand Up @@ -1470,7 +1480,6 @@ cc_test(
"@com_google_absl//absl/log",
"@com_google_absl//absl/log:absl_log",
"@com_google_absl//absl/strings",
"@com_google_googletest//:gtest_main",
],
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ namespace api2 {
//
// Outputs:
// TENSORS - std::vector<Tensor>
// Vector containing a single Tensor populated with an extrated RGB image.
// Vector containing a single Tensor populated with an extracted RGB image.
// MATRIX - std::array<float, 16> @Optional
// An std::array<float, 16> representing a 4x4 row-major-order matrix that
// maps a point on the input image to a point on the output tensor, and
Expand Down Expand Up @@ -212,7 +212,7 @@ class ImageToTensorCalculator : public Node {
std::array<float, 16> matrix;
GetRotatedSubRectToRectTransformMatrix(
roi, image->width(), image->height(),
/*flip_horizontaly=*/false, &matrix);
/*flip_horizontally=*/false, &matrix);
kOutMatrix(cc).Send(std::move(matrix));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ class SubRectExtractorGl {
// pixels as alpha * x + beta and resizes result into destination.
absl::Status ExtractSubRectToBuffer(
const tflite::gpu::gl::GlTexture& texture,
const tflite::gpu::HW& texture_size, const RotatedRect& sub_rect,
bool flip_horizontaly, float alpha, float beta,
const tflite::gpu::HW& texture_size,
const RotatedRect& sub_rect,
bool flip_horizontally,
float alpha,
float beta,
const tflite::gpu::HW& destination_size,
tflite::gpu::gl::CommandQueue* command_queue,
tflite::gpu::gl::GlBuffer* destination);
Expand Down Expand Up @@ -153,14 +156,17 @@ void main() {

absl::Status SubRectExtractorGl::ExtractSubRectToBuffer(
const tflite::gpu::gl::GlTexture& texture,
const tflite::gpu::HW& texture_size, const RotatedRect& texture_sub_rect,
bool flip_horizontaly, float alpha, float beta,
const tflite::gpu::HW& texture_size,
const RotatedRect& texture_sub_rect,
bool flip_horizontally,
float alpha,
float beta,
const tflite::gpu::HW& destination_size,
tflite::gpu::gl::CommandQueue* command_queue,
tflite::gpu::gl::GlBuffer* destination) {
std::array<float, 16> transform_mat;
GetRotatedSubRectToRectTransformMatrix(texture_sub_rect, texture_size.w,
texture_size.h, flip_horizontaly,
texture_size.h, flip_horizontally,
&transform_mat);
MP_RETURN_IF_ERROR(texture.BindAsSampler2D(0));

Expand Down Expand Up @@ -308,7 +314,7 @@ class GlProcessor : public ImageToTensorConverter {
input_texture,
tflite::gpu::HW(source_texture.height(), source_texture.width()),
roi,
/*flip_horizontaly=*/false, transform.scale, transform.offset,
/*flip_horizontally=*/false, transform.scale, transform.offset,
tflite::gpu::HW(output_shape.dims[1], output_shape.dims[2]),
command_queue_.get(), &output));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ class GlProcessor : public ImageToTensorConverter {
range_min, range_max));
auto tensor_view = output_tensor.GetOpenGlTexture2dWriteView();
MP_RETURN_IF_ERROR(ExtractSubRect(input_texture, roi,
/*flip_horizontaly=*/false,
/*flip_horizontally=*/false,
transform.scale, transform.offset,
output_shape, &tensor_view));
return absl::OkStatus();
Expand All @@ -210,7 +210,9 @@ class GlProcessor : public ImageToTensorConverter {

absl::Status ExtractSubRect(const mediapipe::GlTexture& texture,
const RotatedRect& sub_rect,
bool flip_horizontaly, float alpha, float beta,
bool flip_horizontally,
float alpha,
float beta,
const Tensor::Shape& output_shape,
Tensor::OpenGlTexture2dView* output) {
const int output_height = output_shape.dims[1];
Expand Down Expand Up @@ -263,13 +265,13 @@ class GlProcessor : public ImageToTensorConverter {
ABSL_LOG_IF(FATAL, !gl_context) << "GlContext is not bound to the thread.";
if (gl_context->GetGlVersion() == mediapipe::GlVersion::kGLES2) {
GetTransposedRotatedSubRectToRectTransformMatrix(
sub_rect, texture.width(), texture.height(), flip_horizontaly,
sub_rect, texture.width(), texture.height(), flip_horizontally,
&transform_mat);
glUniformMatrix4fv(matrix_id_, 1, GL_FALSE, transform_mat.data());
} else {
GetRotatedSubRectToRectTransformMatrix(sub_rect, texture.width(),
texture.height(), flip_horizontaly,
&transform_mat);
texture.height(),
flip_horizontally, &transform_mat);
glUniformMatrix4fv(matrix_id_, 1, GL_TRUE, transform_mat.data());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,15 @@ class SubRectExtractorMetal {
}

absl::Status Execute(id<MTLTexture> input_texture,
const RotatedRect& sub_rect, bool flip_horizontaly,
float alpha, float beta,
const RotatedRect& sub_rect,
bool flip_horizontally,
float alpha,
float beta,
const tflite::gpu::HW& destination_size,
id<MTLCommandBuffer> command_buffer,
id<MTLBuffer> destination) {
auto output_texture = MTLTextureWithBuffer(destination_size, destination);
return InternalExecute(input_texture, sub_rect, flip_horizontaly, alpha,
return InternalExecute(input_texture, sub_rect, flip_horizontally, alpha,
beta, destination_size, command_buffer,
output_texture);
}
Expand All @@ -211,7 +213,9 @@ class SubRectExtractorMetal {

absl::Status InternalExecute(id<MTLTexture> input_texture,
const RotatedRect& sub_rect,
bool flip_horizontaly, float alpha, float beta,
bool flip_horizontally,
float alpha,
float beta,
const tflite::gpu::HW& destination_size,
id<MTLCommandBuffer> command_buffer,
id<MTLTexture> output_texture) {
Expand All @@ -223,7 +227,7 @@ class SubRectExtractorMetal {
std::array<float, 16> transform_mat;
GetRotatedSubRectToRectTransformMatrix(sub_rect, input_texture.width,
input_texture.height,
flip_horizontaly, &transform_mat);
flip_horizontally, &transform_mat);
id<MTLBuffer> transform_mat_buffer =
[device_ newBufferWithBytes:&transform_mat
length:sizeof(transform_mat)
Expand Down Expand Up @@ -383,7 +387,7 @@ class MetalProcessor : public ImageToTensorConverter {
MtlBufferView::GetWriteView(output_tensor, command_buffer);
MP_RETURN_IF_ERROR(extractor_->Execute(
texture, roi,
/*flip_horizontaly=*/false, transform.scale, transform.offset,
/*flip_horizontally=*/false, transform.scale, transform.offset,
tflite::gpu::HW(output_shape.dims[1], output_shape.dims[2]),
command_buffer, buffer_view.buffer()));
[command_buffer commit];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ absl::StatusOr<ValueTransformation> GetValueRangeTransformation(
}

void GetRotatedSubRectToRectTransformMatrix(const RotatedRect& sub_rect,
int rect_width, int rect_height,
bool flip_horizontaly,
int rect_width,
int rect_height,
bool flip_horizontally,
std::array<float, 16>* matrix_ptr) {
std::array<float, 16>& matrix = *matrix_ptr;
// The resulting matrix is multiplication of below commented out matrices:
Expand All @@ -118,7 +119,7 @@ void GetRotatedSubRectToRectTransformMatrix(const RotatedRect& sub_rect,
// {0.0f, 0.0f, a, 0.0f}
// {0.0f, 0.0f, 0.0f, 1.0f}

const float flip = flip_horizontaly ? -1 : 1;
const float flip = flip_horizontally ? -1 : 1;
// Matrix for optional horizontal flip around middle of output image.
// { fl , 0.0f, 0.0f, 0.0f}
// { 0.0f, 1.0f, 0.0f, 0.0f}
Expand Down Expand Up @@ -176,14 +177,17 @@ void GetRotatedSubRectToRectTransformMatrix(const RotatedRect& sub_rect,
}

void GetTransposedRotatedSubRectToRectTransformMatrix(
const RotatedRect& sub_rect, int rect_width, int rect_height,
bool flip_horizontaly, std::array<float, 16>* matrix_ptr) {
const RotatedRect& sub_rect,
int rect_width,
int rect_height,
bool flip_horizontally,
std::array<float, 16>* matrix_ptr) {
std::array<float, 16>& matrix = *matrix_ptr;
// See comments in GetRotatedSubRectToRectTransformMatrix for detailed
// calculations.
const float a = sub_rect.width;
const float b = sub_rect.height;
const float flip = flip_horizontaly ? -1 : 1;
const float flip = flip_horizontally ? -1 : 1;
const float c = std::cos(sub_rect.rotation);
const float d = std::sin(sub_rect.rotation);
const float e = sub_rect.center_x;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ absl::StatusOr<std::array<float, 4>> PadRoi(int input_tensor_width,
// Represents a transformation of value which involves scaling and offsetting.
// To apply transformation:
// ValueTransformation transform = ...
// float transformed_value = transform.scale * value + transfrom.offset;
// float transformed_value = transform.scale * value + transform.offset;
struct ValueTransformation {
float scale;
float offset;
Expand All @@ -99,11 +99,12 @@ absl::StatusOr<ValueTransformation> GetValueRangeTransformation(
// @sub_rect - rotated sub rect in absolute coordinates
// @rect_width - rect width
// @rect_height - rect height
// @flip_horizontaly - we need to flip the output buffer.
// @flip_horizontally - we need to flip the output buffer.
// @matrix - 4x4 matrix (array of 16 elements) to populate
void GetRotatedSubRectToRectTransformMatrix(const RotatedRect& sub_rect,
int rect_width, int rect_height,
bool flip_horizontaly,
int rect_width,
int rect_height,
bool flip_horizontally,
std::array<float, 16>* matrix);

// Returns the transpose of the matrix found with
Expand All @@ -118,11 +119,14 @@ void GetRotatedSubRectToRectTransformMatrix(const RotatedRect& sub_rect,
// @sub_rect - rotated sub rect in absolute coordinates
// @rect_width - rect width
// @rect_height - rect height
// @flip_horizontaly - we need to flip the output buffer.
// @flip_horizontally - we need to flip the output buffer.
// @matrix - 4x4 matrix (array of 16 elements) to populate
void GetTransposedRotatedSubRectToRectTransformMatrix(
const RotatedRect& sub_rect, int rect_width, int rect_height,
bool flip_horizontaly, std::array<float, 16>* matrix);
const RotatedRect& sub_rect,
int rect_width,
int rect_height,
bool flip_horizontally,
std::array<float, 16>* matrix);

// Validates the output dimensions set in the option proto. The input option
// proto is expected to have to following fields:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ message TensorConverterCalculatorOptions {
// Custom settings to override the internal scaling factors `div` and `sub`.
// Both values must be set to non-negative values. Will only take effect on
// CPU AND when |use_custom_normalization| is set to true. When these custom
// values take effect, the |zero_center| setting above will be overriden, and
// values take effect, the |zero_center| setting above will be overridden, and
// the normalized_value will be calculated as:
// normalized_value = input / custom_div - custom_sub.
optional bool use_custom_normalization = 6 [default = false];
Expand Down

0 comments on commit 99b2fe8

Please sign in to comment.