diff --git a/kernels/portable/cpu/util/reduce_util.h b/kernels/portable/cpu/util/reduce_util.h index 51981328c4f..f800fb6f736 100644 --- a/kernels/portable/cpu/util/reduce_util.h +++ b/kernels/portable/cpu/util/reduce_util.h @@ -814,10 +814,12 @@ template const Func& func) { #ifdef ET_USE_THREADPOOL const ssize_t reduction_size = get_reduced_dim_product(in, dim); - const auto grain_size = std::max( - static_cast(1), - static_cast(executorch::extension::internal::GRAIN_SIZE) / - reduction_size); + const auto grain_size = reduction_size == 0 + ? 1 + : std::max( + static_cast(1), + static_cast(executorch::extension::internal::GRAIN_SIZE) / + reduction_size); #else // ET_USE_THREADPOOL const auto grain_size = 1; #endif // ET_USE_THREADPOOL