Visible to Intel only — GUID: GUID-A695B8A9-91C0-4EB3-8259-A5F917BBAFBC
Abs
AbsBackward
Add
AvgPool
AvgPoolBackward
BatchNormForwardTraining
BatchNormInference
BatchNormTrainingBackward
BiasAdd
BiasAddBackward
Clamp
ClampBackward
Concat
Convolution
ConvolutionBackwardData
ConvolutionBackwardWeights
ConvTranspose
ConvTransposeBackwardData
ConvTransposeBackwardWeights
Dequantize
Divide
DynamicDequantize
DynamicQuantize
Elu
EluBackward
End
Exp
GELU
GELUBackward
HardSigmoid
HardSigmoidBackward
HardSwish
HardSwishBackward
Interpolate
InterpolateBackward
LayerNorm
LayerNormBackward
LeakyReLU
Log
LogSoftmax
LogSoftmaxBackward
MatMul
Maximum
MaxPool
MaxPoolBackward
Minimum
Mish
MishBackward
Multiply
Pow
PReLU
PReLUBackward
Quantize
Reciprocal
ReduceL1
ReduceL2
ReduceMax
ReduceMean
ReduceMin
ReduceProd
ReduceSum
ReLU
ReLUBackward
Reorder
Round
Select
Sigmoid
SigmoidBackward
SoftMax
SoftMaxBackward
SoftPlus
SoftPlusBackward
Sqrt
SqrtBackward
Square
SquaredDifference
StaticReshape
StaticTranspose
Subtract
Tanh
TanhBackward
TypeCast
Wildcard
enum dnnl_alg_kind_t
enum dnnl_normalization_flags_t
enum dnnl_primitive_kind_t
enum dnnl_prop_kind_t
enum dnnl_query_t
enum dnnl::normalization_flags
enum dnnl::query
struct dnnl_exec_arg_t
struct dnnl_primitive
struct dnnl_primitive_desc
struct dnnl::primitive
struct dnnl::primitive_desc
struct dnnl::primitive_desc_base
enum dnnl_rnn_direction_t
enum dnnl_rnn_flags_t
enum dnnl::rnn_direction
enum dnnl::rnn_flags
struct dnnl::augru_backward
struct dnnl::augru_forward
struct dnnl::gru_backward
struct dnnl::gru_forward
struct dnnl::lbr_augru_backward
struct dnnl::lbr_augru_forward
struct dnnl::lbr_gru_backward
struct dnnl::lbr_gru_forward
struct dnnl::lstm_backward
struct dnnl::lstm_forward
struct dnnl::rnn_primitive_desc_base
struct dnnl::vanilla_rnn_backward
struct dnnl::vanilla_rnn_forward
Visible to Intel only — GUID: GUID-A695B8A9-91C0-4EB3-8259-A5F917BBAFBC
cpu_single_op_partition cpp
This is an example to demonstrate how to build a simple op graph and run it on CPU. Annotated version: Single op partition on CPU
This is an example to demonstrate how to build a simple op graph and run it on CPU. Annotated version: Single op partition on CPU
/*******************************************************************************
* Copyright 2024 Intel Corporation
*
* 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.
*******************************************************************************/
//[Headers and namespace]
#include <iostream>
#include <memory>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <assert.h>
#include "oneapi/dnnl/dnnl_graph.hpp"
#include "example_utils.hpp"
#include "graph_example_utils.hpp"
using namespace dnnl::graph;
using data_type = logical_tensor::data_type;
using layout_type = logical_tensor::layout_type;
using dim = logical_tensor::dim;
using dims = logical_tensor::dims;
//[Headers and namespace]
void cpu_single_op_partition_tutorial() {
dim M = 32, K = 1024, N = 2048;
dims src0_dims {M, K};
dims src1_dims {K, N};
//[Create matmul]
logical_tensor matmul_src0_desc {0, data_type::f32};
logical_tensor matmul_src1_desc {1, data_type::f32};
logical_tensor matmul_dst_desc {2, data_type::f32};
op matmul(0, op::kind::MatMul, {matmul_src0_desc, matmul_src1_desc},
{matmul_dst_desc}, "matmul");
matmul.set_attr<bool>(op::attr::transpose_a, false);
matmul.set_attr<bool>(op::attr::transpose_b, false);
//[Create matmul]
//[Create engine]
allocator alloc {};
dnnl::engine eng
= make_engine_with_allocator(dnnl::engine::kind::cpu, 0, alloc);
//[Create engine]
//[Create stream]
dnnl::stream strm {eng};
//[Create stream]
// Memory buffers bound to the partition input/output tensors
// that helps manage the lifetime of these tensors
std::vector<std::shared_ptr<void>> data_buffer;
// Mapping from logical tensor id to the concrete shapes.
// In practical usage, concrete shapes and layouts are not given
// until compilation stage, hence need this mapping to mock the step.
std::unordered_map<size_t, dims> concrete_shapes {
{0, src0_dims}, {1, src1_dims}};
// Compile and execute the partitions, including the following steps:
//
// 1. Update the input/output logical tensors with concrete shape and layout
// 2. Compile the partition
// 3. Update the output logical tensors with queried ones after compilation
// 4. Allocate memory and bind the data buffer for the partition
// 5. Execute the partition
//
// Although they are not part of the APIs, these steps are essential for
// the integration of Graph API., hence users need to implement similar
// logic.
//[Create partition]
partition part(matmul, dnnl::engine::kind::cpu);
//[Create partition]
if (!part.is_supported()) {
std::cout << "cpu_single_op_partition: Got unsupported partition, "
"users need to handle the operators by themselves."
<< std::endl;
return;
}
std::vector<logical_tensor> inputs = part.get_input_ports();
std::vector<logical_tensor> outputs = part.get_output_ports();
// Update input logical tensors with concrete shape and layout
for (auto &input : inputs) {
const auto id = input.get_id();
// Create logical tensor with strided layout
input = logical_tensor {id, input.get_data_type(), concrete_shapes[id],
layout_type::strided};
}
// Update output logical tensors with concrete shape and layout
for (auto &output : outputs) {
const auto id = output.get_id();
output = logical_tensor {id, output.get_data_type(),
DNNL_GRAPH_UNKNOWN_NDIMS,
// do not require concrete shape as the shape will be inferred
// based on input shapes during compilation
layout_type::strided};
}
//[Compile partition]
compiled_partition cp = part.compile(inputs, outputs, eng);
//[Compile partition]
// Update output logical tensors with queried one
for (auto &output : outputs) {
const auto id = output.get_id();
output = cp.query_logical_tensor(id);
}
// Allocate memory for the partition, and bind the data buffers with
// input and output logical tensors
std::vector<tensor> inputs_ts, outputs_ts;
allocate_graph_mem(inputs_ts, inputs, data_buffer, eng);
allocate_graph_mem(outputs_ts, outputs, data_buffer, eng);
//[Execute compiled partition]
cp.execute(strm, inputs_ts, outputs_ts);
//[Execute compiled partition]
// Wait for all compiled partition's execution finished
strm.wait();
std::cout << "Graph:" << std::endl
<< " [matmul_src0] [matmul_src1]" << std::endl
<< " \\ /" << std::endl
<< " matmul" << std::endl
<< " |" << std::endl
<< " [matmul_dst]" << std::endl
<< "Note:" << std::endl
<< " '[]' represents a logical tensor, which refers to "
"inputs/outputs of the graph. "
<< std::endl;
}
int main(int argc, char **argv) {
return handle_example_errors(
{engine::kind::cpu}, cpu_single_op_partition_tutorial);
}