|
| 1 | +// Copyright (C) 2018-2024 Intel Corporation |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// |
| 4 | + |
| 5 | +#include "nodes/executors/subgraph.hpp" |
| 6 | +#if defined(OPENVINO_ARCH_ARM64) |
| 7 | +# include "emitters/snippets/aarch64/cpu_generator.hpp" |
| 8 | +#else |
| 9 | +# include "emitters/snippets/x64/cpu_generator.hpp" |
| 10 | +#endif |
| 11 | + |
| 12 | +namespace ov { |
| 13 | +namespace intel_cpu { |
| 14 | + |
| 15 | +SubgraphCodeGenerator::SubgraphCodeGenerator(const std::shared_ptr<SubgraphAttrs>& snippet_attrs, |
| 16 | + const std::shared_ptr<CPURuntimeConfig>& config) { |
| 17 | + OPENVINO_ASSERT(snippet_attrs, "Subgraph attributes are empty!"); |
| 18 | + OPENVINO_ASSERT(config, "Runtime Config is empty!"); |
| 19 | + |
| 20 | + jit_snippets_compile_args jcp; |
| 21 | + jcp.data_offsets = config->io_data_offsets; |
| 22 | + SubgraphBaseExecutor::init_parallel_domain(config, jcp.exec_domain); |
| 23 | + schedule = |
| 24 | + std::make_shared<ov::snippets::Schedule>(snippet_attrs->snippet->generate(reinterpret_cast<const void*>(&jcp))); |
| 25 | +} |
| 26 | + |
| 27 | +SubgraphBaseExecutor::SubgraphBaseExecutor(const std::shared_ptr<CPURuntimeConfig>& snippet_config, |
| 28 | + const std::shared_ptr<SubgraphAttrs>& snippet_attrs, |
| 29 | + const std::shared_ptr<SubgraphCodeGenerator>& snippet, |
| 30 | + const std::vector<ptrdiff_t>& start_offset_in, |
| 31 | + const std::vector<ptrdiff_t>& start_offset_out, |
| 32 | + const BufferScratchpadAllocator& allocator, |
| 33 | + const ov::intel_cpu::MultiCacheWeakPtr& kernel_cache) |
| 34 | + : m_schedule(snippet->get()), |
| 35 | + m_start_offset_in(start_offset_in), |
| 36 | + m_start_offset_out(start_offset_out) { |
| 37 | + OPENVINO_ASSERT(m_schedule, "Schedule is empty!"); |
| 38 | + OPENVINO_ASSERT(snippet_config, "Runtime Config is empty!"); |
| 39 | + init_parallel_domain(snippet_config, m_parallel_exec_domain); |
| 40 | + |
| 41 | + m_tensor_rank = snippet_config->tensor_rank; |
| 42 | + m_harness_work_amount = std::accumulate(m_parallel_exec_domain.cbegin(), |
| 43 | + m_parallel_exec_domain.cend(), |
| 44 | + size_t(1), |
| 45 | + std::multiplies<size_t>()); |
| 46 | + m_nthreads = std::min(parallel_get_max_threads(), static_cast<int>(m_harness_work_amount)); |
| 47 | + |
| 48 | + m_buffer_scratchpad_size = snippet_config->buffer_scratchpad_size; |
| 49 | + OPENVINO_ASSERT(!ov::snippets::utils::is_dynamic_value(m_buffer_scratchpad_size), |
| 50 | + "Undefined buffer scratchpad size!"); |
| 51 | + m_internal_buffer_size = static_cast<size_t>(m_nthreads) * m_buffer_scratchpad_size; |
| 52 | +} |
| 53 | + |
| 54 | +void SubgraphBaseExecutor::init_parallel_domain(const std::vector<size_t>& master_shape, |
| 55 | + size_t tensor_rank, |
| 56 | + size_t tile_rank, |
| 57 | + std::vector<size_t>& domain) { |
| 58 | + domain.resize(tensor_rank, 1); |
| 59 | + std::fill(domain.begin(), domain.end(), 1); |
| 60 | + std::copy(master_shape.cbegin(), |
| 61 | + master_shape.cbegin() + (master_shape.size() - tile_rank), |
| 62 | + domain.begin() + (tensor_rank - master_shape.size())); |
| 63 | +} |
| 64 | + |
| 65 | +void SubgraphBaseExecutor::init_parallel_domain(const std::shared_ptr<CPURuntimeConfig>& snippet_config, |
| 66 | + std::vector<size_t>& domain) { |
| 67 | + init_parallel_domain(snippet_config->master_shape, snippet_config->tensor_rank, snippet_config->tile_rank, domain); |
| 68 | +} |
| 69 | +void SubgraphBaseExecutor::parallel_for6d( |
| 70 | + const std::function<void(jit_snippets_call_args&, size_t)>& initializer, |
| 71 | + const std::function<void(jit_snippets_call_args&, const std::vector<size_t>&, size_t)>& caller) { |
| 72 | + const auto& dom = m_parallel_exec_domain; |
| 73 | + |
| 74 | + parallel_nt_static(m_nthreads, [&](const int ithr, const int nthr) { |
| 75 | + jit_snippets_call_args call_args; |
| 76 | + initializer(call_args, ithr); |
| 77 | + |
| 78 | + size_t start = 0, end = 0; |
| 79 | + splitter(m_harness_work_amount, nthr, ithr, start, end); |
| 80 | + |
| 81 | + std::vector<size_t> indexes{0, 0, 0, 0, 0}; |
| 82 | + parallel_it_init(start, |
| 83 | + indexes[0], |
| 84 | + dom[0], |
| 85 | + indexes[1], |
| 86 | + dom[1], |
| 87 | + indexes[2], |
| 88 | + dom[2], |
| 89 | + indexes[3], |
| 90 | + dom[3], |
| 91 | + indexes[4], |
| 92 | + dom[4]); |
| 93 | + for (size_t iwork = start; iwork < end; ++iwork) { |
| 94 | + caller(call_args, indexes, ithr); |
| 95 | + parallel_it_step(indexes[0], |
| 96 | + dom[0], |
| 97 | + indexes[1], |
| 98 | + dom[1], |
| 99 | + indexes[2], |
| 100 | + dom[2], |
| 101 | + indexes[3], |
| 102 | + dom[3], |
| 103 | + indexes[4], |
| 104 | + dom[4]); |
| 105 | + } |
| 106 | + }); |
| 107 | +} |
| 108 | + |
| 109 | +void SubgraphBaseExecutor::parallel_forNd( |
| 110 | + const std::function<void(jit_snippets_call_args&, size_t)>& initializer, |
| 111 | + const std::function<void(jit_snippets_call_args&, const std::vector<size_t>&, size_t)>& caller) { |
| 112 | + const auto& dom = m_parallel_exec_domain; |
| 113 | + |
| 114 | + parallel_nt_static(m_nthreads, [&](const int ithr, const int nthr) { |
| 115 | + jit_snippets_call_args call_args; |
| 116 | + initializer(call_args, ithr); |
| 117 | + |
| 118 | + size_t start = 0, end = 0; |
| 119 | + splitter(m_harness_work_amount, nthr, ithr, start, end); |
| 120 | + |
| 121 | + std::vector<size_t> indexes(dom.size() - 1, 0); |
| 122 | + for (size_t iwork = start; iwork < end; ++iwork) { |
| 123 | + size_t tmp = iwork; |
| 124 | + for (ptrdiff_t j = static_cast<ptrdiff_t>(dom.size()) - 2; j >= 0; j--) { |
| 125 | + indexes[j] = tmp % dom[j]; |
| 126 | + tmp /= dom[j]; |
| 127 | + } |
| 128 | + |
| 129 | + caller(call_args, indexes, ithr); |
| 130 | + } |
| 131 | + }); |
| 132 | +} |
| 133 | + |
| 134 | +void SubgraphBaseExecutor::execute(const dnnl::stream& strm, |
| 135 | + const std::vector<MemoryPtr>& inMemPtrs, |
| 136 | + const std::vector<MemoryPtr>& outMemPtrs) { |
| 137 | + exec_impl(inMemPtrs, outMemPtrs); |
| 138 | +} |
| 139 | + |
| 140 | +} // namespace intel_cpu |
| 141 | +} // namespace ov |
0 commit comments