From 7146ca3079c34925f3c61c9ebd81eb09d71e1aaa Mon Sep 17 00:00:00 2001 From: Michael Hucka Date: Wed, 28 Oct 2026 01:04:25 +0000 Subject: [PATCH 1/2] Add null checks and thread guards to GetBlockSize Return 1 early in tfq::GetBlockSize when output_size <= 0, context or context->device() is null, or tensorflow_cpu_worker_threads() is null or has non-positive num_threads, and clamp positive floor division to at least 1. Update the header contract, clean up IWYU includes, and add hermetic C++ unit tests covering null context/device, short-circuiting, null or non-positive worker threads, floor-division clamping, and single-thread execution. --- tensorflow_quantum/core/ops/BUILD | 13 ++ .../core/ops/tfq_simulate_utils.cc | 19 ++- .../core/ops/tfq_simulate_utils.h | 5 +- .../core/ops/tfq_simulate_utils_test.cc | 139 ++++++++++++++++++ 4 files changed, 170 insertions(+), 6 deletions(-) create mode 100644 tensorflow_quantum/core/ops/tfq_simulate_utils_test.cc diff --git a/tensorflow_quantum/core/ops/BUILD b/tensorflow_quantum/core/ops/BUILD index 4d2a53136..a1eeac3d0 100644 --- a/tensorflow_quantum/core/ops/BUILD +++ b/tensorflow_quantum/core/ops/BUILD @@ -459,6 +459,19 @@ cc_library( ], ) +cc_test( + name = "tfq_simulate_utils_test", + size = "small", + srcs = ["tfq_simulate_utils_test.cc"], + linkstatic = 0, + deps = [ + ":tfq_simulate_utils", + "@com_google_googletest//:gtest_main", + "@local_config_tf//:libtensorflow_framework", + "@local_config_tf//:tf_header_lib", + ], +) + py_library( name = "tfq_adj_grad_op_py", srcs = ["tfq_adj_grad_op.py"], diff --git a/tensorflow_quantum/core/ops/tfq_simulate_utils.cc b/tensorflow_quantum/core/ops/tfq_simulate_utils.cc index d2ce64079..3385ef757 100644 --- a/tensorflow_quantum/core/ops/tfq_simulate_utils.cc +++ b/tensorflow_quantum/core/ops/tfq_simulate_utils.cc @@ -15,15 +15,26 @@ limitations under the License. #include "tensorflow_quantum/core/ops/tfq_simulate_utils.h" +#include +#include + +#include "tensorflow/core/framework/device_base.h" #include "tensorflow/core/framework/op_kernel.h" -#include "tensorflow/core/lib/core/threadpool.h" namespace tfq { int GetBlockSize(tensorflow::OpKernelContext* context, const int output_size) { - int size = output_size / - context->device()->tensorflow_cpu_worker_threads()->num_threads; - return std::max(size, 1); + if (output_size <= 0 || context == nullptr || context->device() == nullptr) { + return 1; + } + const auto* worker_threads = + context->device()->tensorflow_cpu_worker_threads(); + if (worker_threads == nullptr || worker_threads->num_threads <= 0) { + return 1; + } + return static_cast(std::max( + 1, static_cast(output_size) / + static_cast(worker_threads->num_threads))); } } // namespace tfq diff --git a/tensorflow_quantum/core/ops/tfq_simulate_utils.h b/tensorflow_quantum/core/ops/tfq_simulate_utils.h index cac38fd06..935563d81 100644 --- a/tensorflow_quantum/core/ops/tfq_simulate_utils.h +++ b/tensorflow_quantum/core/ops/tfq_simulate_utils.h @@ -20,8 +20,9 @@ limitations under the License. namespace tfq { -// Returns a block size that distributes the work evenly across the different -// circuits. +// Returns a positive block size (>= 1) that distributes `output_size` work +// items across CPU worker threads, or 1 if `output_size <= 0` or `context` / +// CPU worker threads are unavailable or non-positive. int GetBlockSize(tensorflow::OpKernelContext* context, const int output_size); } // namespace tfq diff --git a/tensorflow_quantum/core/ops/tfq_simulate_utils_test.cc b/tensorflow_quantum/core/ops/tfq_simulate_utils_test.cc new file mode 100644 index 000000000..77094b049 --- /dev/null +++ b/tensorflow_quantum/core/ops/tfq_simulate_utils_test.cc @@ -0,0 +1,139 @@ +/* Copyright 2026 The TensorFlow Quantum 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_quantum/core/ops/tfq_simulate_utils.h" + +#include + +#include "gtest/gtest.h" +#include "tensorflow/core/framework/device_base.h" +#include "tensorflow/core/framework/op_kernel.h" +#include "tensorflow/core/platform/env.h" + +namespace tfq { +namespace { + +class FakeCpuDevice : public tensorflow::DeviceBase { + public: + FakeCpuDevice() : tensorflow::DeviceBase(tensorflow::Env::Default()) {} +}; + +class NullWorkerThreadsDevice : public tensorflow::DeviceBase { + public: + NullWorkerThreadsDevice() + : tensorflow::DeviceBase(tensorflow::Env::Default()) {} + + const CpuWorkerThreads* tensorflow_cpu_worker_threads() const override { + return nullptr; + } +}; + +TEST(TfqSimulateUtilsTest, NullContextReturnsOne) { + EXPECT_EQ(GetBlockSize(nullptr, 100), 1); + EXPECT_EQ(GetBlockSize(nullptr, 0), 1); + EXPECT_EQ(GetBlockSize(nullptr, -5), 1); + EXPECT_EQ(GetBlockSize(nullptr, std::numeric_limits::min()), 1); +} + +TEST(TfqSimulateUtilsTest, NullDeviceReturnsOne) { + // OpKernelContext requires a non-null device during construction, and + // context.device() reads params.device dynamically afterward. + FakeCpuDevice device; + tensorflow::OpKernelContext::Params params; + params.device = &device; + tensorflow::OpKernelContext context(¶ms, 0); + params.device = nullptr; + + EXPECT_EQ(GetBlockSize(&context, 100), 1); + EXPECT_EQ(GetBlockSize(&context, 0), 1); + EXPECT_EQ(GetBlockSize(&context, -5), 1); +} + +TEST(TfqSimulateUtilsTest, NonPositiveOutputSizeShortCircuitsBeforeThreads) { + // DeviceBase::tensorflow_cpu_worker_threads() CHECK-aborts if unconfigured, + // proving non-positive output_size returns before querying worker threads. + FakeCpuDevice unconfigured_device; + tensorflow::OpKernelContext::Params params; + params.device = &unconfigured_device; + tensorflow::OpKernelContext context(¶ms, 0); + + EXPECT_EQ(GetBlockSize(&context, 0), 1); + EXPECT_EQ(GetBlockSize(&context, -5), 1); + EXPECT_EQ(GetBlockSize(&context, std::numeric_limits::min()), 1); +} + +TEST(TfqSimulateUtilsTest, NullWorkerThreadsReturnsOne) { + NullWorkerThreadsDevice device; + tensorflow::OpKernelContext::Params params; + params.device = &device; + tensorflow::OpKernelContext context(¶ms, 0); + + EXPECT_EQ(GetBlockSize(&context, 100), 1); +} + +TEST(TfqSimulateUtilsTest, ZeroOrNegativeThreadsReturnsOne) { + tensorflow::DeviceBase::CpuWorkerThreads worker_threads; + FakeCpuDevice device; + device.set_tensorflow_cpu_worker_threads(&worker_threads); + + tensorflow::OpKernelContext::Params params; + params.device = &device; + tensorflow::OpKernelContext context(¶ms, 0); + + worker_threads.num_threads = 0; + EXPECT_EQ(GetBlockSize(&context, 100), 1); + + worker_threads.num_threads = -2; + EXPECT_EQ(GetBlockSize(&context, 100), 1); + + worker_threads.num_threads = std::numeric_limits::min(); + EXPECT_EQ(GetBlockSize(&context, 100), 1); +} + +TEST(TfqSimulateUtilsTest, ValidInputsPreserveFloorDivisionClampedToOne) { + tensorflow::DeviceBase::CpuWorkerThreads worker_threads; + FakeCpuDevice device; + worker_threads.num_threads = 4; + device.set_tensorflow_cpu_worker_threads(&worker_threads); + + tensorflow::OpKernelContext::Params params; + params.device = &device; + tensorflow::OpKernelContext context(¶ms, 0); + + EXPECT_EQ(GetBlockSize(&context, 100), 25); + EXPECT_EQ(GetBlockSize(&context, 8), 2); + EXPECT_EQ(GetBlockSize(&context, 7), 1); + EXPECT_EQ(GetBlockSize(&context, 4), 1); + EXPECT_EQ(GetBlockSize(&context, 3), 1); + EXPECT_EQ(GetBlockSize(&context, 0), 1); + EXPECT_EQ(GetBlockSize(&context, -5), 1); + EXPECT_EQ(GetBlockSize(&context, 2147483647), 536870911); +} + +TEST(TfqSimulateUtilsTest, SingleWorkerThreadReturnsFullOutputSize) { + tensorflow::DeviceBase::CpuWorkerThreads worker_threads; + FakeCpuDevice device; + worker_threads.num_threads = 1; + device.set_tensorflow_cpu_worker_threads(&worker_threads); + + tensorflow::OpKernelContext::Params params; + params.device = &device; + tensorflow::OpKernelContext context(¶ms, 0); + + EXPECT_EQ(GetBlockSize(&context, 100), 100); +} + +} // namespace +} // namespace tfq From 04b57ff2d62cb63d62a73905c11a4d180a824a9c Mon Sep 17 00:00:00 2001 From: Michael Hucka Date: Tue, 10 Nov 2026 21:52:51 +0000 Subject: [PATCH 2/2] Simplify GetBlockSize division and test teardown Use explicit CpuWorkerThreads pointer type, drop redundant int64_t casts and include in GetBlockSize, and restore params.device before OpKernelContext destruction in NullDeviceReturnsOne. --- tensorflow_quantum/core/ops/tfq_simulate_utils.cc | 7 ++----- tensorflow_quantum/core/ops/tfq_simulate_utils_test.cc | 3 +++ 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tensorflow_quantum/core/ops/tfq_simulate_utils.cc b/tensorflow_quantum/core/ops/tfq_simulate_utils.cc index 3385ef757..7bb245c88 100644 --- a/tensorflow_quantum/core/ops/tfq_simulate_utils.cc +++ b/tensorflow_quantum/core/ops/tfq_simulate_utils.cc @@ -16,7 +16,6 @@ limitations under the License. #include "tensorflow_quantum/core/ops/tfq_simulate_utils.h" #include -#include #include "tensorflow/core/framework/device_base.h" #include "tensorflow/core/framework/op_kernel.h" @@ -27,14 +26,12 @@ int GetBlockSize(tensorflow::OpKernelContext* context, const int output_size) { if (output_size <= 0 || context == nullptr || context->device() == nullptr) { return 1; } - const auto* worker_threads = + const tensorflow::DeviceBase::CpuWorkerThreads* worker_threads = context->device()->tensorflow_cpu_worker_threads(); if (worker_threads == nullptr || worker_threads->num_threads <= 0) { return 1; } - return static_cast(std::max( - 1, static_cast(output_size) / - static_cast(worker_threads->num_threads))); + return std::max(1, output_size / worker_threads->num_threads); } } // namespace tfq diff --git a/tensorflow_quantum/core/ops/tfq_simulate_utils_test.cc b/tensorflow_quantum/core/ops/tfq_simulate_utils_test.cc index 77094b049..c888e2002 100644 --- a/tensorflow_quantum/core/ops/tfq_simulate_utils_test.cc +++ b/tensorflow_quantum/core/ops/tfq_simulate_utils_test.cc @@ -59,6 +59,9 @@ TEST(TfqSimulateUtilsTest, NullDeviceReturnsOne) { EXPECT_EQ(GetBlockSize(&context, 100), 1); EXPECT_EQ(GetBlockSize(&context, 0), 1); EXPECT_EQ(GetBlockSize(&context, -5), 1); + + // Restore a valid device before OpKernelContext destruction. + params.device = &device; } TEST(TfqSimulateUtilsTest, NonPositiveOutputSizeShortCircuitsBeforeThreads) {