<commit_before>#include "logger.h"
#include "measure.h"
#include "text/table.h"
#include "math/numeric.h"
#include "text/cmdline.h"
#include "text/to_params.h"
#include "layers/conv3d_naive.h"
#include "layers/conv3d_dmaps.h"
#include "layers/conv3d_dense.h"
#include <iostream>

using namespace nano;

namespace nano
{
        template <typename tvalue>
        string_t serialize_to_string(const tvalue value)
        {
                std::stringstream s;
                s << value;
                return s.str();
        }

        template <> string_t to_string<tensor2d_dims_t>(const tensor2d_dims_t dims) { return serialize_to_string(dims); }
        template <> string_t to_string<tensor3d_dims_t>(const tensor3d_dims_t dims) { return serialize_to_string(dims); }
        template <> string_t to_string<tensor4d_dims_t>(const tensor4d_dims_t dims) { return serialize_to_string(dims); }
}

namespace
{
        template <typename top>
        auto measure(const top& op, const tensor_size_t flops)
        {
                const auto trials = size_t(16);
                const auto duration = measure_robustly<nanoseconds_t>(op, trials);
                return nano::gflops(flops, duration);
        }

        template <typename top, typename tidata, typename tkdata, typename tbdata, typename todata>
        auto measure_output(const top& op, const tidata& idata, const tkdata& kdata, const tbdata& bdata, todata& odata)
        {
                return ::measure([&] () { op.output(idata, kdata, bdata, odata); }, op.params().flops_output());
        }

        template <typename top, typename tidata, typename tkdata, typename tbdata, typename todata>
        auto measure_ginput(const top& op, tidata& idata, const tkdata& kdata, const tbdata& bdata, const todata& odata)
        {
                return ::measure([&] () { op.ginput(idata, kdata, bdata, odata); }, op.params().flops_ginput());
        }

        template <typename top, typename tidata, typename tkdata, typename tbdata, typename todata>
        auto measure_gparam(const top& op, const tidata& idata, tkdata& kdata, tbdata& bdata, const todata& odata)
        {
                return ::measure([&] () { op.gparam(idata, kdata, bdata, odata); }, op.params().flops_gparam());
        }
}

int main(int argc, const char *argv[])
{
        // parse the command line
        cmdline_t cmdline("benchmark 3D convolution operators");
        cmdline.add("", "imaps",        "number of input planes [1,128]", "32");
        cmdline.add("", "irows",        "number of input rows [16, 128]", "24");
        cmdline.add("", "icols",        "number of input cols [16, 128]", "24");
        cmdline.add("", "omaps",        "number of output planes [1, 128]", "32");
        cmdline.add("", "min-kconn",    "minimum connectivity factor [1, 16]", "1");
        cmdline.add("", "max-kconn",    "maximum connectivity factor [1, 16]", "4");
        cmdline.add("", "min-ksize",    "minimum kernel size [1, 15]", "1");
        cmdline.add("", "max-ksize",    "maximum kernel size [1, 15]", "9");

        cmdline.process(argc, argv);

        // check arguments and options
        const auto cmd_imaps = clamp(cmdline.get<int>("imaps"), 1, 128);
        const auto cmd_irows = clamp(cmdline.get<int>("irows"), 16, 128);
        const auto cmd_icols = clamp(cmdline.get<int>("icols"), 16, 128);
        const auto cmd_omaps = clamp(cmdline.get<int>("omaps"), 1, 128);
        const auto cmd_min_kconn = clamp(cmdline.get<int>("min-kconn"), 1, 16);
        const auto cmd_max_kconn = clamp(cmdline.get<int>("max-kconn"), cmd_min_kconn, 16);
        const auto cmd_min_ksize = clamp(cmdline.get<int>("min-ksize"), 1, 15);
        const auto cmd_max_ksize = clamp(cmdline.get<int>("max-ksize"), cmd_min_ksize, 15);
        const auto cmd_min_kdelta = 1;
        const auto cmd_max_kdelta = 2;

        table_t table;
        table.header()
                << "isize" << "parameters" << "osize" << "params"
                << "output[#kflops]" << "ginput" << "gparam"
                << "+naive[gflop/s]" << "ginput" << "gparam"
                << "+dmaps[gflop/s]" << "ginput" << "gparam"
                << "+dense[gflop/s]" << "ginput" << "gparam";

        // benchmark 3D convolutions various kernel sizes & connectivity factors
        for (tensor_size_t ksize = cmd_min_ksize; ksize <= cmd_max_ksize; ksize += 2)
        {
                for (tensor_size_t kdelta = cmd_min_kdelta; kdelta <= cmd_max_kdelta; ++ kdelta)
                {
                        for (tensor_size_t kconn = cmd_min_kconn; kconn <= cmd_max_kconn; kconn *= 2)
                        {
                                const auto params = conv3d_params_t
                                {<commit_msg>more compact result table<commit_after>#include "logger.h"
#include "measure.h"
#include "text/table.h"
#include "math/numeric.h"
#include "text/cmdline.h"
#include "text/to_params.h"
#include "layers/conv3d_naive.h"
#include "layers/conv3d_dmaps.h"
#include "layers/conv3d_dense.h"
#include <iostream>

using namespace nano;

namespace nano
{
        template <typename tvalue>
        string_t serialize_to_string(const tvalue value)
        {
                std::stringstream s;
                s << value;
                return s.str();
        }

        template <> string_t to_string<tensor2d_dims_t>(const tensor2d_dims_t dims) { return serialize_to_string(dims); }
        template <> string_t to_string<tensor3d_dims_t>(const tensor3d_dims_t dims) { return serialize_to_string(dims); }
        template <> string_t to_string<tensor4d_dims_t>(const tensor4d_dims_t dims) { return serialize_to_string(dims); }
}

namespace
{
        template <typename top>
        auto measure(const top& op, const tensor_size_t flops)
        {
                const auto trials = size_t(16);
                const auto duration = measure_robustly<nanoseconds_t>(op, trials);
                return nano::gflops(flops, duration);
        }

        template <typename top, typename tidata, typename tkdata, typename tbdata, typename todata>
        auto measure_output(const top& op, const tidata& idata, const tkdata& kdata, const tbdata& bdata, todata& odata)
        {
                return ::measure([&] () { op.output(idata, kdata, bdata, odata); }, op.params().flops_output());
        }

        template <typename top, typename tidata, typename tkdata, typename tbdata, typename todata>
        auto measure_ginput(const top& op, tidata& idata, const tkdata& kdata, const tbdata& bdata, const todata& odata)
        {
                return ::measure([&] () { op.ginput(idata, kdata, bdata, odata); }, op.params().flops_ginput());
        }

        template <typename top, typename tidata, typename tkdata, typename tbdata, typename todata>
        auto measure_gparam(const top& op, const tidata& idata, tkdata& kdata, tbdata& bdata, const todata& odata)
        {
                return ::measure([&] () { op.gparam(idata, kdata, bdata, odata); }, op.params().flops_gparam());
        }
}

int main(int argc, const char *argv[])
{
        // parse the command line
        cmdline_t cmdline("benchmark 3D convolution operators");
        cmdline.add("", "imaps",        "number of input planes [1,128]", "32");
        cmdline.add("", "irows",        "number of input rows [16, 128]", "24");
        cmdline.add("", "icols",        "number of input cols [16, 128]", "24");
        cmdline.add("", "omaps",        "number of output planes [1, 128]", "32");
        cmdline.add("", "min-kconn",    "minimum connectivity factor [1, 16]", "1");
        cmdline.add("", "max-kconn",    "maximum connectivity factor [1, 16]", "4");
        cmdline.add("", "min-ksize",    "minimum kernel size [1, 15]", "1");
        cmdline.add("", "max-ksize",    "maximum kernel size [1, 15]", "9");

        cmdline.process(argc, argv);

        // check arguments and options
        const auto cmd_imaps = clamp(cmdline.get<int>("imaps"), 1, 128);
        const auto cmd_irows = clamp(cmdline.get<int>("irows"), 16, 128);
        const auto cmd_icols = clamp(cmdline.get<int>("icols"), 16, 128);
        const auto cmd_omaps = clamp(cmdline.get<int>("omaps"), 1, 128);
        const auto cmd_min_kconn = clamp(cmdline.get<int>("min-kconn"), 1, 16);
        const auto cmd_max_kconn = clamp(cmdline.get<int>("max-kconn"), cmd_min_kconn, 16);
        const auto cmd_min_ksize = clamp(cmdline.get<int>("min-ksize"), 1, 15);
        const auto cmd_max_ksize = clamp(cmdline.get<int>("max-ksize"), cmd_min_ksize, 15);
        const auto cmd_min_kdelta = 1;
        const auto cmd_max_kdelta = 2;

        table_t table;
        table.header()
                << "" << "" << "" << ""
                << "output" << "ginput" << "gparam"
                << "+naive" << "ginput" << "gparam"
                << "+dmaps" << "ginput" << "gparam"
                << "+dense" << "ginput" << "gparam";

        table.append()
                << "isize" << "parameters" << "osize" << "params"
                << "#kflops" << "#kflops" << "#kflops"
                << "gflop/s" << "gflop/s" << "gflop/s"
                << "gflop/s" << "gflop/s" << "gflop/s"
                << "gflop/s" << "gflop/s" << "gflop/s";

        table.append(table_row_t::storage::delim);

        // benchmark 3D convolutions various kernel sizes & connectivity factors
        for (tensor_size_t ksize = cmd_min_ksize; ksize <= cmd_max_ksize; ksize += 2)
        {
                for (tensor_size_t kdelta = cmd_min_kdelta; kdelta <= cmd_max_kdelta; ++ kdelta)
                {
                        for (tensor_size_t kconn = cmd_min_kconn; kconn <= cmd_max_kconn; kconn *= 2)
                        {
                                const auto params = conv3d_params_t
                                {
{'fraction_non_alphanumeric': 0.14290082560391398, 'fraction_numerical': 0.0179390480073387, 'mean_word_length': 2.479432624113475, 'pattern_counts': {'":': 0, '<': 151, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5184147', 'n_tokens_mistral': 3383, 'n_tokens_neox': 3154, 'n_words': 773}