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

GH-40357: [C++] Add benchmark for ToTensor conversions #40358

Merged
1 change: 1 addition & 0 deletions cpp/src/arrow/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,7 @@ add_arrow_benchmark(builder_benchmark)
add_arrow_benchmark(compare_benchmark)
add_arrow_benchmark(memory_pool_benchmark)
add_arrow_benchmark(type_benchmark)
add_arrow_benchmark(tensor_benchmark)

#
# Recurse into sub-directories
Expand Down
67 changes: 67 additions & 0 deletions cpp/src/arrow/tensor_benchmark.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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 "benchmark/benchmark.h"

#include "arrow/record_batch.h"
#include "arrow/testing/gtest_util.h"
#include "arrow/testing/random.h"
#include "arrow/type.h"
#include "arrow/util/benchmark_util.h"

namespace arrow {

template <typename ValueType>
static void BatchToTensorSimple(benchmark::State& state) {
using CType = typename ValueType::c_type;
std::shared_ptr<DataType> ty = TypeTraits<ValueType>::type_singleton();

const int64_t num_cols = state.range(1);
const int64_t num_rows = state.range(0) / num_cols / sizeof(CType);
arrow::random::RandomArrayGenerator gen_{42};

std::vector<std::shared_ptr<Field>> fields = {};
std::vector<std::shared_ptr<Array>> columns = {};

for (int64_t i = 0; i < num_cols; ++i) {
fields.push_back(field("f" + std::to_string(i), ty));
columns.push_back(gen_.ArrayOf(ty, num_rows));
}
auto schema = std::make_shared<Schema>(std::move(fields));
auto batch = RecordBatch::Make(schema, num_rows, columns);

for (auto _ : state) {
ASSERT_OK_AND_ASSIGN(auto tensor, batch->ToTensor());
}
state.SetItemsProcessed(state.iterations() * num_rows * num_cols);
state.SetBytesProcessed(state.iterations() * ty->byte_width() * num_rows * num_cols);
}

void SetArgs(benchmark::internal::Benchmark* bench) {
for (int64_t size : {kL1Size, kL2Size}) {
for (int64_t number_of_columns : {3, 30, 300}) {
bench->Args({size, number_of_columns});
AlenkaF marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

BENCHMARK_TEMPLATE(BatchToTensorSimple, Int8Type)->Apply(SetArgs);
BENCHMARK_TEMPLATE(BatchToTensorSimple, Int16Type)->Apply(SetArgs);
BENCHMARK_TEMPLATE(BatchToTensorSimple, Int32Type)->Apply(SetArgs);
BENCHMARK_TEMPLATE(BatchToTensorSimple, Int64Type)->Apply(SetArgs);

} // namespace arrow
Loading