Commit ca34d4d2 authored by yanjl1's avatar yanjl1
Browse files

Initial

parents
// Copyright © Advanced Micro Devices, Inc., or its affiliates.
// SPDX-License-Identifier: MIT
#include <iostream>
#include "utils.hpp"
#include <hipdnn_data_sdk/utilities/Tensor.hpp>
#include <hipdnn_data_sdk/utilities/Workspace.hpp>
#include <hipdnn_frontend.hpp>
int main()
{
using InputType = hipdnn_data_sdk::types::half;
const int64_t dyN0 = 64;
const int64_t dyN1 = 32;
const int64_t dxN0 = 128;
const int64_t dxN1 = 64;
auto buildGetitemBackwardGraph = [=](hipdnnHandle_t handle) {
auto graph = std::make_shared<hipdnn_frontend::graph::Graph>();
graph->set_name("getitem_backward_graph")
.set_io_data_type(hipdnn_frontend::getDataTypeEnumFromType<InputType>())
.set_intermediate_data_type(hipdnn_frontend::getDataTypeEnumFromType<InputType>())
.set_compute_data_type(hipdnn_frontend::DataType::FLOAT);
auto dy = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("dy")
.set_dim({dyN0, dyN1})
.set_stride({dyN1, 1}));
auto xIndeices = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("x_indeices")
.set_dim({64, 32})
.set_stride({32, 1})
.set_data_type(hipdnn_frontend::DataType::INT32));
auto yIndeices = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("y_indeices")
.set_dim({64, 32})
.set_stride({32, 1})
.set_data_type(hipdnn_frontend::DataType::INT32));
const std::vector<int32_t> slices = {0, 0, 33, 1, 1, 0, 62, 1};
auto getitemBackwardAttributes = hipdnn_frontend::graph::GetitemBackwardAttributes()
.set_dims({0, 1})
.set_indices({xIndeices, yIndeices})
.set_slices(slices)
.set_offset(0)
.set_name("getitem_backward");
auto [dx, error] = graph->getitem_backward(dy, getitemBackwardAttributes);
dx->set_output(true).set_dim({dxN0, dxN1}).set_stride({dxN1, 1});
error->set_output(true);
// build graph
HIPDNN_FE_CHECK(graph->build(handle));
return std::make_tuple(graph, dy, dx, xIndeices, yIndeices, error);
};
auto backend = hipdnn_frontend::detail::hipdnnBackend();
if(!backend)
{
std::cout << "Create backend failed.\n";
return 1;
}
hipdnnHandle_t handle;
HIPDNN_CHECK(backend->create(&handle));
auto [graph, dy, dx, xindex, yindex, error] = buildGetitemBackwardGraph(handle);
hipdnn_data_sdk::utilities::Tensor<InputType> dyTensor(dy->get_dim(), dy->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> dxTensor(dx->get_dim(), dx->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> xIndexTensor(xindex->get_dim(),
xindex->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> yIndexTensor(yindex->get_dim(),
yindex->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> errorTensor(error->get_dim(),
error->get_stride());
std::unordered_map<int64_t, void*> variantPack;
variantPack[dy->get_uid()] = dyTensor.memory().deviceData();
variantPack[dx->get_uid()] = dxTensor.memory().deviceData();
variantPack[xindex->get_uid()] = xIndexTensor.memory().deviceData();
variantPack[yindex->get_uid()] = yIndexTensor.memory().deviceData();
variantPack[error->get_uid()] = errorTensor.memory().deviceData();
int64_t workspaceSize = 0;
HIPDNN_FE_CHECK(graph->get_workspace_size(workspaceSize));
const hipdnn_data_sdk::utilities::Workspace workspace(static_cast<size_t>(workspaceSize));
HIPDNN_FE_CHECK(graph->execute(handle, variantPack, workspace.get()));
std::cout << "GetitemBackwardSlice graph execution complete.\n";
HIPDNN_CHECK(backend->destroy(handle));
return 0;
}
#include <iostream>
#include "utils.hpp"
#include <hipdnn_data_sdk/utilities/Tensor.hpp>
#include <hipdnn_data_sdk/utilities/Workspace.hpp>
#include <hipdnn_frontend.hpp>
int main()
{
using InputType = float;
const int64_t n = 1; // Batch size
// Input
const int64_t c = 16; // Number of channels
const int64_t h = 16; // Height
const int64_t w = 16; // Width
auto buildGNBackwardGraph = [=](hipdnnHandle_t handle) {
auto graph = std::make_shared<hipdnn_frontend::graph::Graph>();
graph->set_name("groupnorm_backward_graph")
.set_io_data_type(hipdnn_frontend::getDataTypeEnumFromType<InputType>())
.set_intermediate_data_type(hipdnn_frontend::getDataTypeEnumFromType<InputType>())
.set_compute_data_type(hipdnn_frontend::DataType::FLOAT); //
const int numGroups = 2;
auto input = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("input")
.set_dim({n, c, h, w})
.set_stride({c * h * w, h * w, w, 1}));
auto dy = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("dy")
.set_dim({n, c, h, w})
.set_stride({c * h * w, h * w, w, 1}));
auto scale = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes().set_name("scale").set_dim({c}).set_stride(
{1}));
auto epsilon = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("epsilon")
.set_data_type(hipdnn_frontend::DataType::FLOAT)
.set_value(1e-5f));
auto groups = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("groups")
.set_data_type(hipdnn_frontend::DataType::INT32)
.set_value(numGroups));
auto mean = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("mean")
.set_dim({numGroups * n})
.set_stride({1}));
auto invVariance = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("inv_variance")
.set_dim({numGroups * n})
.set_stride({1}));
auto groupnormBwdAttributes = hipdnn_frontend::graph::GroupnormBwdAttributes()
.set_name("groupnorm_backward_node")
.set_epsilon(epsilon)
.set_groups(groups);
auto [dx, dbias, dscale] = graph->groupnorm_backward(
input, dy, scale, mean, invVariance, groupnormBwdAttributes);
dx->set_output(true);
dbias->set_output(true);
dscale->set_output(true);
// build graph
HIPDNN_FE_CHECK(graph->build(handle));
return std::make_tuple(graph, input, dy, scale, mean, invVariance, dx, dbias, dscale);
};
auto backend = hipdnn_frontend::detail::hipdnnBackend();
if(!backend)
{
std::cout << "Creat backend failed. \n";
return 1;
}
hipdnnHandle_t handle;
HIPDNN_CHECK(backend->create(&handle));
auto [graph, input, dy, scale, mean, invVariance, dx, dbias, dscale]
= buildGNBackwardGraph(handle);
// Allocate DCU memory
hipdnn_data_sdk::utilities::Tensor<InputType> inputTensor(input->get_dim(),
input->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> dyTensor(dy->get_dim(), dy->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> scaleTensor(scale->get_dim(),
scale->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> meanTensor(mean->get_dim(), mean->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> invVarianceTensor(invVariance->get_dim(),
invVariance->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> dxTensor(dx->get_dim(), dx->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> dbiasTensor(dbias->get_dim(),
dbias->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> dscaleTensor(dscale->get_dim(),
dscale->get_stride());
std::unordered_map<int64_t, void*> variantPack;
variantPack[input->get_uid()] = inputTensor.memory().deviceData();
variantPack[dy->get_uid()] = dyTensor.memory().deviceData();
variantPack[scale->get_uid()] = scaleTensor.memory().deviceData();
variantPack[mean->get_uid()] = meanTensor.memory().deviceData();
variantPack[invVariance->get_uid()] = invVarianceTensor.memory().deviceData();
variantPack[dx->get_uid()] = dxTensor.memory().deviceData();
variantPack[dbias->get_uid()] = dbiasTensor.memory().deviceData();
variantPack[dscale->get_uid()] = dscaleTensor.memory().deviceData();
int64_t workspaceSize = 0;
HIPDNN_FE_CHECK(graph->get_workspace_size(workspaceSize));
const hipdnn_data_sdk::utilities::Workspace workspace(static_cast<size_t>(workspaceSize));
HIPDNN_FE_CHECK(graph->execute(handle, variantPack, workspace.get()));
std::cout << "GNBackward graph execution complete. \n";
HIPDNN_CHECK(backend->destroy(handle));
return 0;
}
#include <iostream>
#include "utils.hpp"
#include <hipdnn_data_sdk/utilities/Tensor.hpp>
#include <hipdnn_data_sdk/utilities/Workspace.hpp>
#include <hipdnn_frontend.hpp>
int main()
{
using InputType = hipdnn_data_sdk::types::half;
const int64_t n = 1; // Batch size
// Input
const int64_t c = 16; // Number of channels
const int64_t h = 16; // Height
const int64_t w = 16; // Width
auto buildGNInferenceGraph = [=](hipdnnHandle_t handle) {
auto graph = std::make_shared<hipdnn_frontend::graph::Graph>();
graph->set_name("groupnorm_forward_graph")
.set_io_data_type(hipdnn_frontend::getDataTypeEnumFromType<InputType>())
.set_intermediate_data_type(hipdnn_frontend::getDataTypeEnumFromType<InputType>())
.set_compute_data_type(hipdnn_frontend::DataType::FLOAT); //
auto input = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("input")
.set_dim({n, c, h, w})
.set_stride({c * h * w, h * w, w, 1}));
auto scale = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes().set_name("scale").set_dim({c}).set_stride(
{1}));
auto bias = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes().set_name("bias").set_dim({c}).set_stride(
{1}));
auto epsilon = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("epsilon")
.set_data_type(hipdnn_frontend::DataType::FLOAT)
.set_value(1e-5));
auto groups = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("groups")
.set_data_type(hipdnn_frontend::DataType::INT32)
.set_value(2));
auto groupnormFwdAttributes
= hipdnn_frontend::graph::GroupnormFwdAttributes()
.set_name("groupnorm_forward_node")
.set_epsilon(epsilon)
.set_groups(groups)
.set_forward_phase(hipdnn_frontend::NormFwdPhase_t::INFERENCE);
auto [output, mean, inv_variance]
= graph->groupnorm(input, scale, bias, groupnormFwdAttributes);
output->set_output(true);
// build graph
HIPDNN_FE_CHECK(graph->build(handle));
return std::make_tuple(graph, input, scale, bias, output);
};
auto backend = hipdnn_frontend::detail::hipdnnBackend();
if(!backend)
{
std::cout << "Creat backend failed. \n";
return 1;
}
hipdnnHandle_t handle;
HIPDNN_CHECK(backend->create(&handle));
auto [graph, input, scale, bias, output] = buildGNInferenceGraph(handle);
hipdnn_data_sdk::utilities::Tensor<InputType> inputTensor(input->get_dim(),
input->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> scaleTensor(scale->get_dim(),
scale->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> biasTensor(bias->get_dim(), bias->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> outputTensor(output->get_dim(),
output->get_stride());
std::unordered_map<int64_t, void*> variantPack;
variantPack[input->get_uid()] = inputTensor.memory().deviceData();
variantPack[scale->get_uid()] = scaleTensor.memory().deviceData();
variantPack[bias->get_uid()] = biasTensor.memory().deviceData();
variantPack[output->get_uid()] = outputTensor.memory().deviceData();
int64_t workspaceSize = 0;
HIPDNN_FE_CHECK(graph->get_workspace_size(workspaceSize));
const hipdnn_data_sdk::utilities::Workspace workspace(static_cast<size_t>(workspaceSize));
HIPDNN_FE_CHECK(graph->execute(handle, variantPack, workspace.get()));
std::cout << "GNInference graph execution complete. \n";
HIPDNN_CHECK(backend->destroy(handle));
return 0;
}
#include <iostream>
#include "utils.hpp"
#include <hipdnn_data_sdk/utilities/Tensor.hpp>
#include <hipdnn_data_sdk/utilities/Workspace.hpp>
#include <hipdnn_frontend.hpp>
int main()
{
using InputType = float;
const int64_t n = 1; // Batch size
// Input
const int64_t c = 16; // Number of channels
const int64_t h = 16; // Height
const int64_t w = 16; // Width
auto buildGNTrainingGraph = [=](hipdnnHandle_t handle) {
auto graph = std::make_shared<hipdnn_frontend::graph::Graph>();
graph->set_name("groupnorm_training_graph")
.set_io_data_type(hipdnn_frontend::getDataTypeEnumFromType<InputType>())
.set_intermediate_data_type(hipdnn_frontend::getDataTypeEnumFromType<InputType>())
.set_compute_data_type(hipdnn_frontend::DataType::FLOAT); //
auto input = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("input")
.set_dim({n, c, h, w})
.set_stride({c * h * w, h * w, w, 1}));
auto scale = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes().set_name("scale").set_dim({c}).set_stride(
{1}));
auto bias = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes().set_name("bias").set_dim({c}).set_stride(
{1}));
auto epsilon = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("epsilon")
.set_data_type(hipdnn_frontend::DataType::FLOAT)
.set_value(1e-5));
auto groups = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("groups")
.set_data_type(hipdnn_frontend::DataType::INT32)
.set_value(2));
auto groupnormFwdAttributes
= hipdnn_frontend::graph::GroupnormFwdAttributes()
.set_name("groupnorm_forward_node")
.set_epsilon(epsilon)
.set_groups(groups)
.set_forward_phase(hipdnn_frontend::NormFwdPhase_t::TRAINING);
auto [output, mean, inv_variance]
= graph->groupnorm(input, scale, bias, groupnormFwdAttributes);
output->set_output(true);
mean->set_output(true);
inv_variance->set_output(true);
// build graph
HIPDNN_FE_CHECK(graph->build(handle));
return std::make_tuple(graph, input, scale, bias, output, mean, inv_variance);
};
auto backend = hipdnn_frontend::detail::hipdnnBackend();
if(!backend)
{
std::cout << "Creat backend failed. \n";
return 1;
}
hipdnnHandle_t handle;
HIPDNN_CHECK(backend->create(&handle));
auto [graph, input, scale, bias, output, mean, inv_variance] = buildGNTrainingGraph(handle);
hipdnn_data_sdk::utilities::Tensor<InputType> inputTensor(input->get_dim(),
input->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> scaleTensor(scale->get_dim(),
scale->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> biasTensor(bias->get_dim(), bias->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> outputTensor(output->get_dim(),
output->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> meanTensor(mean->get_dim(), mean->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> invVarianceTensor(inv_variance->get_dim(),
inv_variance->get_stride());
std::unordered_map<int64_t, void*> variantPack;
variantPack[input->get_uid()] = inputTensor.memory().deviceData();
variantPack[scale->get_uid()] = scaleTensor.memory().deviceData();
variantPack[bias->get_uid()] = biasTensor.memory().deviceData();
variantPack[output->get_uid()] = outputTensor.memory().deviceData();
variantPack[mean->get_uid()] = meanTensor.memory().deviceData();
variantPack[inv_variance->get_uid()] = invVarianceTensor.memory().deviceData();
int64_t workspaceSize = 0;
HIPDNN_FE_CHECK(graph->get_workspace_size(workspaceSize));
const hipdnn_data_sdk::utilities::Workspace workspace(static_cast<size_t>(workspaceSize));
HIPDNN_FE_CHECK(graph->execute(handle, variantPack, workspace.get()));
std::cout << "GNTraining graph execution complete. \n";
HIPDNN_CHECK(backend->destroy(handle));
return 0;
}
#include <iostream>
#include "utils.hpp"
#include <hipdnn_data_sdk/utilities/Tensor.hpp>
#include <hipdnn_data_sdk/utilities/Workspace.hpp>
#include <hipdnn_frontend.hpp>
int main()
{
using InputType = float;
const int64_t n = 1; // Batch size
const int64_t c = 2; // Number of channels
const int64_t h = 3; // Height
const int64_t w = 4; // Width
auto buildInstancenormBackwardGraph = [=](hipdnnHandle_t handle) {
auto graph = std::make_shared<hipdnn_frontend::graph::Graph>();
graph->set_name("instancenorm_backward_graph")
.set_io_data_type(hipdnn_frontend::getDataTypeEnumFromType<InputType>())
.set_intermediate_data_type(hipdnn_frontend::getDataTypeEnumFromType<InputType>())
.set_compute_data_type(hipdnn_frontend::DataType::FLOAT);
auto input = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("input")
.set_dim({n, c, h, w})
.set_stride({c * h * w, h * w, w, 1}));
auto dy = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("dy")
.set_dim({n, c, h, w})
.set_stride({c * h * w, h * w, w, 1}));
auto scale = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("scale")
.set_dim({1, c, 1, 1})
.set_stride({c, 1, 1, 1}));
auto mean = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("mean")
.set_dim({n, c, 1, 1})
.set_stride({c, 1, 1, 1}));
auto invVariance = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("inv_variance")
.set_dim({n, c, 1, 1})
.set_stride({c, 1, 1, 1}));
auto instancenormBackwardAttributes
= hipdnn_frontend::graph::InstancenormBackwardAttributes()
.set_saved_mean_and_inv_variance(mean, invVariance)
.set_name("instancenorm_backward_node");
auto [dx, dscale, dbias]
= graph->instancenorm_backward(dy, input, scale, instancenormBackwardAttributes);
dx->set_output(true);
dbias->set_output(true);
dscale->set_output(true);
// build graph
HIPDNN_FE_CHECK(graph->build(handle));
return std::make_tuple(graph, input, dy, scale, mean, invVariance, dx, dbias, dscale);
};
auto backend = hipdnn_frontend::detail::hipdnnBackend();
if(!backend)
{
std::cout << "Creat backend failed. \n";
return 1;
}
hipdnnHandle_t handle;
HIPDNN_CHECK(backend->create(&handle));
auto [graph, input, dy, scale, mean, invVariance, dx, dbias, dscale]
= buildInstancenormBackwardGraph(handle);
// Allocate DCU memory
hipdnn_data_sdk::utilities::Tensor<InputType> inputTensor(input->get_dim(),
input->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> dyTensor(dy->get_dim(), dy->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> scaleTensor(scale->get_dim(),
scale->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> meanTensor(mean->get_dim(), mean->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> invVarianceTensor(invVariance->get_dim(),
invVariance->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> dxTensor(dx->get_dim(), dx->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> dbiasTensor(dbias->get_dim(),
dbias->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> dscaleTensor(dscale->get_dim(),
dscale->get_stride());
std::unordered_map<int64_t, void*> variantPack;
variantPack[input->get_uid()] = inputTensor.memory().deviceData();
variantPack[dy->get_uid()] = dyTensor.memory().deviceData();
variantPack[scale->get_uid()] = scaleTensor.memory().deviceData();
variantPack[mean->get_uid()] = meanTensor.memory().deviceData();
variantPack[invVariance->get_uid()] = invVarianceTensor.memory().deviceData();
variantPack[dx->get_uid()] = dxTensor.memory().deviceData();
variantPack[dbias->get_uid()] = dbiasTensor.memory().deviceData();
variantPack[dscale->get_uid()] = dscaleTensor.memory().deviceData();
int64_t workspaceSize = 0;
HIPDNN_FE_CHECK(graph->get_workspace_size(workspaceSize));
const hipdnn_data_sdk::utilities::Workspace workspace(static_cast<size_t>(workspaceSize));
HIPDNN_FE_CHECK(graph->execute(handle, variantPack, workspace.get()));
std::cout << "Instancenorm_backward graph execution complete. \n";
HIPDNN_CHECK(backend->destroy(handle));
return 0;
}
#include <iostream>
#include "utils.hpp"
#include <hipdnn_data_sdk/utilities/Tensor.hpp>
#include <hipdnn_data_sdk/utilities/Workspace.hpp>
#include <hipdnn_frontend.hpp>
int main()
{
using InputType = float;
const int64_t n = 1; // Batch size
const int64_t c = 2; // Number of channels
const int64_t h = 3; // Height
const int64_t w = 4; // Width
auto buildInstancenormInferenceGraph = [=](hipdnnHandle_t handle) {
auto graph = std::make_shared<hipdnn_frontend::graph::Graph>();
graph->set_name("instancenorm_inference_graph")
.set_io_data_type(hipdnn_frontend::getDataTypeEnumFromType<InputType>())
.set_intermediate_data_type(hipdnn_frontend::getDataTypeEnumFromType<InputType>())
.set_compute_data_type(hipdnn_frontend::DataType::FLOAT);
auto input = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("input")
.set_dim({n, c, h, w})
.set_stride({c * h * w, h * w, w, 1}));
auto scale = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("scale")
.set_dim({1, c, 1, 1})
.set_stride({c, 1, 1, 1}));
auto bias = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("bias")
.set_dim({1, c, 1, 1})
.set_stride({c, 1, 1, 1}));
auto epsilon = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes().set_name("epsilon").set_value(1e-5f));
auto instancenormInferenceAttributes
= hipdnn_frontend::graph::InstancenormAttributes()
.set_name("instancenorm_inference_node")
.set_epsilon(epsilon)
.set_forward_phase(hipdnn_frontend::NormFwdPhase_t::INFERENCE);
auto [output, mean, invVariance]
= graph->instancenorm(input, scale, bias, instancenormInferenceAttributes);
output->set_output(true);
// build graph
HIPDNN_FE_CHECK(graph->build(handle));
return std::make_tuple(graph, input, scale, bias, output);
};
auto backend = hipdnn_frontend::detail::hipdnnBackend();
if(!backend)
{
std::cout << "Creat backend failed. \n";
return 1;
}
hipdnnHandle_t handle;
HIPDNN_CHECK(backend->create(&handle));
auto [graph, input, scale, bias, output] = buildInstancenormInferenceGraph(handle);
hipdnn_data_sdk::utilities::Tensor<InputType> inputTensor(input->get_dim(),
input->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> scaleTensor(scale->get_dim(),
scale->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> biasTensor(bias->get_dim(), bias->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> outputTensor(output->get_dim(),
output->get_stride());
std::unordered_map<int64_t, void*> variantPack;
variantPack[input->get_uid()] = inputTensor.memory().deviceData();
variantPack[scale->get_uid()] = scaleTensor.memory().deviceData();
variantPack[bias->get_uid()] = biasTensor.memory().deviceData();
variantPack[output->get_uid()] = outputTensor.memory().deviceData();
int64_t workspaceSize = 0;
HIPDNN_FE_CHECK(graph->get_workspace_size(workspaceSize));
const hipdnn_data_sdk::utilities::Workspace workspace(static_cast<size_t>(workspaceSize));
HIPDNN_FE_CHECK(graph->execute(handle, variantPack, workspace.get()));
std::cout << "Instancenorm_inference graph execution complete. \n";
HIPDNN_CHECK(backend->destroy(handle));
return 0;
}
#include <iostream>
#include "utils.hpp"
#include <hipdnn_data_sdk/utilities/Tensor.hpp>
#include <hipdnn_data_sdk/utilities/Workspace.hpp>
#include <hipdnn_frontend.hpp>
int main()
{
using InputType = float;
const int64_t n = 1; // Batch size
const int64_t c = 2; // Number of channels
const int64_t h = 3; // Height
const int64_t w = 4; // Width
auto buildInstancenormTrainingGraph = [=](hipdnnHandle_t handle) {
auto graph = std::make_shared<hipdnn_frontend::graph::Graph>();
graph->set_name("instancenorm_training_graph")
.set_io_data_type(hipdnn_frontend::getDataTypeEnumFromType<InputType>())
.set_intermediate_data_type(hipdnn_frontend::getDataTypeEnumFromType<InputType>())
.set_compute_data_type(hipdnn_frontend::DataType::FLOAT);
auto input = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("input")
.set_dim({n, c, h, w})
.set_stride({c * h * w, h * w, w, 1}));
auto scale = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("scale")
.set_dim({1, c, 1, 1})
.set_stride({c, 1, 1, 1}));
auto bias = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("bias")
.set_dim({1, c, 1, 1})
.set_stride({c, 1, 1, 1}));
auto epsilon = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes().set_name("epsilon").set_value(1e-5f));
auto instancenormAttributes
= hipdnn_frontend::graph::InstancenormAttributes()
.set_name("instancenorm_training_node")
.set_epsilon(epsilon)
.set_forward_phase(hipdnn_frontend::NormFwdPhase_t::TRAINING);
auto [output, mean, invVariance]
= graph->instancenorm(input, scale, bias, instancenormAttributes);
output->set_output(true);
mean->set_output(true);
invVariance->set_output(true);
// build graph
HIPDNN_FE_CHECK(graph->build(handle));
return std::make_tuple(graph, input, scale, bias, output, mean, invVariance);
};
auto backend = hipdnn_frontend::detail::hipdnnBackend();
if(!backend)
{
std::cout << "Creat backend failed. \n";
return 1;
}
hipdnnHandle_t handle;
HIPDNN_CHECK(backend->create(&handle));
auto [graph, input, scale, bias, output, mean, invVariance]
= buildInstancenormTrainingGraph(handle);
hipdnn_data_sdk::utilities::Tensor<InputType> inputTensor(input->get_dim(),
input->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> scaleTensor(scale->get_dim(),
scale->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> biasTensor(bias->get_dim(), bias->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> outputTensor(output->get_dim(),
output->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> meanTensor(mean->get_dim(), mean->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> invVarianceTensor(invVariance->get_dim(),
invVariance->get_stride());
std::unordered_map<int64_t, void*> variantPack;
variantPack[input->get_uid()] = inputTensor.memory().deviceData();
variantPack[scale->get_uid()] = scaleTensor.memory().deviceData();
variantPack[bias->get_uid()] = biasTensor.memory().deviceData();
variantPack[output->get_uid()] = outputTensor.memory().deviceData();
variantPack[mean->get_uid()] = meanTensor.memory().deviceData();
variantPack[invVariance->get_uid()] = invVarianceTensor.memory().deviceData();
int64_t workspaceSize = 0;
HIPDNN_FE_CHECK(graph->get_workspace_size(workspaceSize));
const hipdnn_data_sdk::utilities::Workspace workspace(static_cast<size_t>(workspaceSize));
HIPDNN_FE_CHECK(graph->execute(handle, variantPack, workspace.get()));
std::cout << "Instancenorm_training graph execution complete. \n";
HIPDNN_CHECK(backend->destroy(handle));
return 0;
}
// Copyright © Advanced Micro Devices, Inc., or its affiliates.
// SPDX-License-Identifier: MIT
#include <iostream>
#include "utils.hpp"
#include <hipdnn_data_sdk/utilities/Tensor.hpp>
#include <hipdnn_data_sdk/utilities/Workspace.hpp>
#include <hipdnn_frontend.hpp>
int main()
{
using InputType = float;
const int64_t n0 = 250;
const int64_t n1 = 512;
auto buildKthvalueGraph = [=](hipdnnHandle_t handle) {
auto graph = std::make_shared<hipdnn_frontend::graph::Graph>();
graph->set_name("kthvalue_graph")
.set_io_data_type(hipdnn_frontend::getDataTypeEnumFromType<InputType>())
.set_intermediate_data_type(hipdnn_frontend::getDataTypeEnumFromType<InputType>())
.set_compute_data_type(hipdnn_frontend::DataType::FLOAT);
auto input = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("input")
.set_dim({n0, n1})
.set_stride({n1, 1}));
auto kthvalueAttributes = hipdnn_frontend::graph::KthvalueAttributes()
.set_k(12)
.set_dim(1)
.set_keep_dim(false)
.set_name("kthvalue");
auto [output, indices] = graph->kthvalue(input, kthvalueAttributes);
output->set_output(true);
indices->set_output(true);
// build graph
HIPDNN_FE_CHECK(graph->build(handle));
return std::make_tuple(graph, input, output, indices);
};
auto backend = hipdnn_frontend::detail::hipdnnBackend();
if(!backend)
{
std::cout << "Create backend failed.\n";
return 1;
}
hipdnnHandle_t handle;
HIPDNN_CHECK(backend->create(&handle));
auto [graph, input, output, indices] = buildKthvalueGraph(handle);
hipdnn_data_sdk::utilities::Tensor<InputType> inputTensor(input->get_dim(),
input->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> outputTensor(output->get_dim(),
output->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> indicesTensor(indices->get_dim(),
indices->get_stride());
std::unordered_map<int64_t, void*> variantPack;
variantPack[input->get_uid()] = inputTensor.memory().deviceData();
variantPack[output->get_uid()] = outputTensor.memory().deviceData();
variantPack[indices->get_uid()] = indicesTensor.memory().deviceData();
int64_t workspaceSize = 0;
HIPDNN_FE_CHECK(graph->get_workspace_size(workspaceSize));
const hipdnn_data_sdk::utilities::Workspace workspace(static_cast<size_t>(workspaceSize));
HIPDNN_FE_CHECK(graph->execute(handle, variantPack, workspace.get()));
std::cout << "Kthvalue graph execution complete.\n";
HIPDNN_CHECK(backend->destroy(handle));
return 0;
}
// Copyright © Advanced Micro Devices, Inc., or its affiliates.
// SPDX-License-Identifier: MIT
#include <iostream>
#include "utils.hpp"
#include <hipdnn_data_sdk/utilities/Tensor.hpp>
#include <hipdnn_data_sdk/utilities/Workspace.hpp>
#include <hipdnn_frontend.hpp>
int main()
{
using InputType = float;
const int64_t n0 = 250;
const int64_t n1 = 512;
auto buildKthvalueGraph = [=](hipdnnHandle_t handle) {
auto graph = std::make_shared<hipdnn_frontend::graph::Graph>();
graph->set_name("kthvalue_graph")
.set_io_data_type(hipdnn_frontend::getDataTypeEnumFromType<InputType>())
.set_intermediate_data_type(hipdnn_frontend::getDataTypeEnumFromType<InputType>())
.set_compute_data_type(hipdnn_frontend::DataType::FLOAT);
auto input = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("input")
.set_dim({n0, n1})
.set_stride({n1, 1}));
auto kthvalueAttributes = hipdnn_frontend::graph::KthvalueAttributes()
.set_k(12)
.set_dim(1)
.set_keep_dim(false)
.set_name("kthvalue");
auto [output, indices] = graph->kthvalue(input, kthvalueAttributes);
output->set_output(true);
indices->set_output(true);
// build graph
HIPDNN_FE_CHECK(graph->build(handle));
return std::make_tuple(graph, input, output, indices);
};
auto backend = hipdnn_frontend::detail::hipdnnBackend();
if(!backend)
{
std::cout << "Create backend failed.\n";
return 1;
}
hipdnnHandle_t handle;
HIPDNN_CHECK(backend->create(&handle));
auto [graph, input, output, indices] = buildKthvalueGraph(handle);
hipdnn_data_sdk::utilities::Tensor<InputType> inputTensor(input->get_dim(),
input->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> outputTensor(output->get_dim(),
output->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> indicesTensor(indices->get_dim(),
indices->get_stride());
std::unordered_map<int64_t, void*> variantPack;
variantPack[input->get_uid()] = inputTensor.memory().deviceData();
variantPack[output->get_uid()] = outputTensor.memory().deviceData();
variantPack[indices->get_uid()] = indicesTensor.memory().deviceData();
int64_t workspaceSize = 0;
HIPDNN_FE_CHECK(graph->get_workspace_size(workspaceSize));
const hipdnn_data_sdk::utilities::Workspace workspace(static_cast<size_t>(workspaceSize));
HIPDNN_FE_CHECK(graph->execute(handle, variantPack, workspace.get()));
std::cout << "Kthvalue graph execution complete.\n";
HIPDNN_CHECK(backend->destroy(handle));
return 0;
}
// Copyright © Advanced Micro Devices, Inc., or its affiliates.
// SPDX-License-Identifier: MIT
#include <iostream>
#include "utils.hpp"
#include <hipdnn_data_sdk/utilities/Tensor.hpp>
#include <hipdnn_data_sdk/utilities/Workspace.hpp>
#include <hipdnn_frontend.hpp>
int main()
{
using InputType = float;
const int64_t n0 = 512;
const int64_t n1 = 250;
const int64_t n2 = 5;
const int64_t n3 = 2;
auto buildKthvalueGraph = [=](hipdnnHandle_t handle) {
auto graph = std::make_shared<hipdnn_frontend::graph::Graph>();
graph->set_name("kthvalue_graph")
.set_io_data_type(hipdnn_frontend::getDataTypeEnumFromType<InputType>())
.set_intermediate_data_type(hipdnn_frontend::getDataTypeEnumFromType<InputType>())
.set_compute_data_type(hipdnn_frontend::DataType::FLOAT);
auto input = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("input")
.set_dim({n0, n1, n2, n3})
.set_stride({n1 * n2 * n3, 1, n1 * n3, n1}));
auto kthvalueAttributes = hipdnn_frontend::graph::KthvalueAttributes()
.set_k(12)
.set_dim(0)
.set_keep_dim(false)
.set_name("kthvalue");
auto [output, indices] = graph->kthvalue(input, kthvalueAttributes);
output->set_output(true);
indices->set_output(true);
// build graph
HIPDNN_FE_CHECK(graph->build(handle));
return std::make_tuple(graph, input, output, indices);
};
auto backend = hipdnn_frontend::detail::hipdnnBackend();
if(!backend)
{
std::cout << "Create backend failed.\n";
return 1;
}
hipdnnHandle_t handle;
HIPDNN_CHECK(backend->create(&handle));
auto [graph, input, output, indices] = buildKthvalueGraph(handle);
hipdnn_data_sdk::utilities::Tensor<InputType> inputTensor(input->get_dim(),
input->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> outputTensor(output->get_dim(),
output->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> indicesTensor(indices->get_dim(),
indices->get_stride());
std::unordered_map<int64_t, void*> variantPack;
variantPack[input->get_uid()] = inputTensor.memory().deviceData();
variantPack[output->get_uid()] = outputTensor.memory().deviceData();
variantPack[indices->get_uid()] = indicesTensor.memory().deviceData();
int64_t workspaceSize = 0;
HIPDNN_FE_CHECK(graph->get_workspace_size(workspaceSize));
const hipdnn_data_sdk::utilities::Workspace workspace(static_cast<size_t>(workspaceSize));
HIPDNN_FE_CHECK(graph->execute(handle, variantPack, workspace.get()));
std::cout << "Kthvalue graph execution complete.\n";
HIPDNN_CHECK(backend->destroy(handle));
return 0;
}
#include <iostream>
#include "utils.hpp"
#include <hipdnn_data_sdk/utilities/Tensor.hpp>
#include <hipdnn_data_sdk/utilities/Workspace.hpp>
#include <hipdnn_frontend.hpp>
int main()
{
using InputType = float;
const int64_t n = 1; // Batch size
const int64_t c = 2; // Number of channels
const int64_t h = 3; // Height
const int64_t w = 4; // Width
auto buildLayernormBackwardGraph = [=](hipdnnHandle_t handle) {
auto graph = std::make_shared<hipdnn_frontend::graph::Graph>();
graph->set_name("layernorm_backward_graph")
.set_io_data_type(hipdnn_frontend::getDataTypeEnumFromType<InputType>())
.set_intermediate_data_type(hipdnn_frontend::getDataTypeEnumFromType<InputType>())
.set_compute_data_type(hipdnn_frontend::DataType::FLOAT);
auto input = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("input")
.set_dim({n, c, h, w})
.set_stride({c * h * w, h * w, w, 1}));
auto dy = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("dy")
.set_dim({n, c, h, w})
.set_stride({c * h * w, h * w, w, 1}));
auto scale = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes().set_name("scale").set_dim({w}).set_stride(
{1}));
auto mean = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("mean")
.set_dim({n * c * h})
.set_stride({1}));
auto invVariance = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("inv_variance")
.set_dim({n * c * h})
.set_stride({1}));
auto layernormBackwardAttributes = hipdnn_frontend::graph::LayernormBackwardAttributes()
.set_saved_mean_and_inv_variance(mean, invVariance)
.set_name("layernorm_backward_node");
auto [dx, dbias, dscale]
= graph->layernorm_backward(dy, input, scale, layernormBackwardAttributes);
dx->set_output(true);
dbias->set_output(true);
dscale->set_output(true);
// build graph
HIPDNN_FE_CHECK(graph->build(handle));
return std::make_tuple(graph, input, dy, scale, mean, invVariance, dx, dbias, dscale);
};
auto backend = hipdnn_frontend::detail::hipdnnBackend();
if(!backend)
{
std::cout << "Creat backend failed. \n";
return 1;
}
hipdnnHandle_t handle;
HIPDNN_CHECK(backend->create(&handle));
auto [graph, input, dy, scale, mean, invVariance, dx, dbias, dscale]
= buildLayernormBackwardGraph(handle);
// Allocate DCU memory
hipdnn_data_sdk::utilities::Tensor<InputType> inputTensor(input->get_dim(),
input->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> dyTensor(dy->get_dim(), dy->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> scaleTensor(scale->get_dim(),
scale->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> meanTensor(mean->get_dim(), mean->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> invVarianceTensor(invVariance->get_dim(),
invVariance->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> dxTensor(dx->get_dim(), dx->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> dbiasTensor(dbias->get_dim(),
dbias->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> dscaleTensor(dscale->get_dim(),
dscale->get_stride());
std::unordered_map<int64_t, void*> variantPack;
variantPack[input->get_uid()] = inputTensor.memory().deviceData();
variantPack[dy->get_uid()] = dyTensor.memory().deviceData();
variantPack[scale->get_uid()] = scaleTensor.memory().deviceData();
variantPack[mean->get_uid()] = meanTensor.memory().deviceData();
variantPack[invVariance->get_uid()] = invVarianceTensor.memory().deviceData();
variantPack[dx->get_uid()] = dxTensor.memory().deviceData();
variantPack[dbias->get_uid()] = dbiasTensor.memory().deviceData();
variantPack[dscale->get_uid()] = dscaleTensor.memory().deviceData();
int64_t workspaceSize = 0;
HIPDNN_FE_CHECK(graph->get_workspace_size(workspaceSize));
const hipdnn_data_sdk::utilities::Workspace workspace(static_cast<size_t>(workspaceSize));
HIPDNN_FE_CHECK(graph->execute(handle, variantPack, workspace.get()));
std::cout << "Layernorm_backward graph execution complete. \n";
HIPDNN_CHECK(backend->destroy(handle));
return 0;
}
#include <iostream>
#include "utils.hpp"
#include <hipdnn_data_sdk/utilities/Tensor.hpp>
#include <hipdnn_data_sdk/utilities/Workspace.hpp>
#include <hipdnn_frontend.hpp>
int main()
{
using InputType = hipdnn_data_sdk::types::half;
const int64_t n = 1; // Batch size
const int64_t c = 2; // Number of channels
const int64_t h = 3; // Height
const int64_t w = 4; // Width
auto buildLayernormInferenceGraph = [=](hipdnnHandle_t handle) {
auto graph = std::make_shared<hipdnn_frontend::graph::Graph>();
graph->set_name("layernorm_inference_graph")
.set_io_data_type(hipdnn_frontend::getDataTypeEnumFromType<InputType>())
.set_intermediate_data_type(hipdnn_frontend::getDataTypeEnumFromType<InputType>())
.set_compute_data_type(hipdnn_frontend::DataType::FLOAT);
auto input = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("input")
.set_dim({n, c, h, w})
.set_stride({c * h * w, h * w, w, 1}));
auto scale = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes().set_name("scale").set_dim({w}).set_stride(
{1}));
auto bias = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes().set_name("bias").set_dim({w}).set_stride(
{1}));
auto epsilon = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes().set_name("epsilon").set_value(1e-5));
auto layernormInferenceAttributes
= hipdnn_frontend::graph::LayernormAttributes()
.set_name("layernorm_inference_node")
.set_epsilon(epsilon)
.set_forward_phase(hipdnn_frontend::NormFwdPhase_t::INFERENCE);
auto [output, mean, invVariance]
= graph->layernorm(input, scale, bias, layernormInferenceAttributes);
output->set_output(true);
// build graph
HIPDNN_FE_CHECK(graph->build(handle));
return std::make_tuple(graph, input, scale, bias, output);
};
auto backend = hipdnn_frontend::detail::hipdnnBackend();
if(!backend)
{
std::cout << "Creat backend failed. \n";
return 1;
}
hipdnnHandle_t handle;
HIPDNN_CHECK(backend->create(&handle));
auto [graph, input, scale, bias, output] = buildLayernormInferenceGraph(handle);
hipdnn_data_sdk::utilities::Tensor<InputType> inputTensor(input->get_dim(),
input->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> scaleTensor(scale->get_dim(),
scale->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> biasTensor(bias->get_dim(), bias->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> outputTensor(output->get_dim(),
output->get_stride());
std::unordered_map<int64_t, void*> variantPack;
variantPack[input->get_uid()] = inputTensor.memory().deviceData();
variantPack[scale->get_uid()] = scaleTensor.memory().deviceData();
variantPack[bias->get_uid()] = biasTensor.memory().deviceData();
variantPack[output->get_uid()] = outputTensor.memory().deviceData();
int64_t workspaceSize = 0;
HIPDNN_FE_CHECK(graph->get_workspace_size(workspaceSize));
const hipdnn_data_sdk::utilities::Workspace workspace(static_cast<size_t>(workspaceSize));
HIPDNN_FE_CHECK(graph->execute(handle, variantPack, workspace.get()));
std::cout << "Layernorm_inference graph execution complete. \n";
HIPDNN_CHECK(backend->destroy(handle));
return 0;
}
#include <iostream>
#include "hipdnn_frontend/attributes/MatmulAttributes.hpp"
#include "utils.hpp"
#include <hipdnn_data_sdk/utilities/Tensor.hpp>
#include <hipdnn_data_sdk/utilities/Workspace.hpp>
#include <hipdnn_frontend.hpp>
int main()
{
using InputType = hipdnn_data_sdk::types::half;
const int64_t b = 2; // Batch size
// Input
const int64_t n = 16; // Height
const int64_t m = 32; // Width
auto buildMatmulsGraph = [=](hipdnnHandle_t handle) {
auto graph = std::make_shared<hipdnn_frontend::graph::Graph>();
graph->set_name("matmul_bias_graph")
.set_io_data_type(hipdnn_frontend::getDataTypeEnumFromType<InputType>())
.set_intermediate_data_type(hipdnn_frontend::getDataTypeEnumFromType<InputType>())
.set_compute_data_type(hipdnn_frontend::DataType::FLOAT);
// create matmul
auto inputA = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("input_a")
.set_dim({b, n, m})
.set_stride({n * m, m, 1}));
auto inputB = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("input_b")
.set_dim({b, m, n})
.set_stride({n * m, n, 1}));
auto matmulAttributes = hipdnn_frontend::graph::MatmulAttributes().set_name("matmul_node");
auto output = graph->matmul(inputA, inputB, matmulAttributes);
output->set_output(true);
// build graph
HIPDNN_FE_CHECK(graph->build(handle));
return std::make_tuple(graph, inputA, inputB, output);
};
auto backend = hipdnn_frontend::detail::hipdnnBackend();
if(!backend)
{
std::cout << "Creat backend failed. \n";
return 1;
}
hipdnnHandle_t handle;
HIPDNN_CHECK(backend->create(&handle));
auto [graph, inputA, inputB, output] = buildMatmulsGraph(handle);
// Allocate DCU memory
hipdnn_data_sdk::utilities::Tensor<InputType> inputATensor(inputA->get_dim(),
inputA->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> inputBTensor(inputB->get_dim(),
inputB->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> outTensor(output->get_dim(),
output->get_stride());
std::unordered_map<int64_t, void*> variantPack;
variantPack[inputA->get_uid()] = inputATensor.memory().deviceData();
variantPack[inputB->get_uid()] = inputBTensor.memory().deviceData();
variantPack[output->get_uid()] = outTensor.memory().deviceData();
int64_t workspaceSize = 0;
HIPDNN_FE_CHECK(graph->get_workspace_size(workspaceSize));
const hipdnn_data_sdk::utilities::Workspace workspace(static_cast<size_t>(workspaceSize));
HIPDNN_FE_CHECK(graph->execute(handle, variantPack, workspace.get()));
std::cout << "Matmul graph execution complete. \n";
HIPDNN_CHECK(backend->destroy(handle));
return 0;
}
#include <iostream>
#include "hipdnn_frontend/attributes/MatmulAttributes.hpp"
#include "utils.hpp"
#include <hipdnn_data_sdk/utilities/Tensor.hpp>
#include <hipdnn_data_sdk/utilities/Workspace.hpp>
#include <hipdnn_frontend.hpp>
int main()
{
using InputType = hipdnn_data_sdk::types::half;
const int64_t b = 2; // Batch size
// Input
const int64_t n = 16; // Height
const int64_t m = 32; // Width
auto buildMatmulBiasGraph = [=](hipdnnHandle_t handle) {
auto graph = std::make_shared<hipdnn_frontend::graph::Graph>();
graph->set_name("matmul_bias_graph")
.set_io_data_type(hipdnn_frontend::getDataTypeEnumFromType<InputType>())
.set_intermediate_data_type(hipdnn_frontend::getDataTypeEnumFromType<InputType>())
.set_compute_data_type(hipdnn_frontend::DataType::FLOAT);
// create matmul
auto inputA = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("input_a")
.set_dim({b, n, m})
.set_stride({n * m, m, 1}));
auto inputB = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("input_b")
.set_dim({b, m, n})
.set_stride({n * m, n, 1}));
auto matmulAttributes = hipdnn_frontend::graph::MatmulAttributes().set_name("matmul_node");
auto matmulOutput = graph->matmul(inputA, inputB, matmulAttributes);
// create bias
auto bias = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("bias")
.set_dim({1, 1, n})
.set_stride({n, n, 1}));
auto biasAttributes = hipdnn_frontend::graph::PointwiseAttributes()
.set_name("bias_node")
.set_mode(hipdnn_frontend::PointwiseMode_t::ADD);
auto output = graph->pointwise(matmulOutput, bias, biasAttributes);
output->set_output(true);
// build graph
HIPDNN_FE_CHECK(graph->build(handle));
return std::make_tuple(graph, inputA, inputB, bias, output);
};
auto backend = hipdnn_frontend::detail::hipdnnBackend();
if(!backend)
{
std::cout << "Creat backend failed. \n";
return 1;
}
hipdnnHandle_t handle;
HIPDNN_CHECK(backend->create(&handle));
auto [graph, inputA, inputB, bias, output] = buildMatmulBiasGraph(handle);
// Allocate DCU memory
hipdnn_data_sdk::utilities::Tensor<InputType> inputATensor(inputA->get_dim(),
inputA->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> inputBTensor(inputB->get_dim(),
inputB->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> biasTensor(bias->get_dim(), bias->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> outTensor(output->get_dim(),
output->get_stride());
std::unordered_map<int64_t, void*> variantPack;
variantPack[inputA->get_uid()] = inputATensor.memory().deviceData();
variantPack[inputB->get_uid()] = inputBTensor.memory().deviceData();
variantPack[bias->get_uid()] = biasTensor.memory().deviceData();
variantPack[output->get_uid()] = outTensor.memory().deviceData();
int64_t workspaceSize = 0;
HIPDNN_FE_CHECK(graph->get_workspace_size(workspaceSize));
const hipdnn_data_sdk::utilities::Workspace workspace(static_cast<size_t>(workspaceSize));
HIPDNN_FE_CHECK(graph->execute(handle, variantPack, workspace.get()));
std::cout << "Matmul_bias graph execution complete. \n";
HIPDNN_CHECK(backend->destroy(handle));
return 0;
}
#include <iostream>
#include "hipdnn_frontend/attributes/MatmulAttributes.hpp"
#include "utils.hpp"
#include <hipdnn_data_sdk/utilities/Tensor.hpp>
#include <hipdnn_data_sdk/utilities/Workspace.hpp>
#include <hipdnn_frontend.hpp>
int main()
{
using InputType = hipdnn_data_sdk::types::half;
const int64_t b = 2; // Batch size
// Input
const int64_t n = 16; // Height
const int64_t m = 32; // Width
auto buildMatmulBiasGraph = [=](hipdnnHandle_t handle) {
auto graph = std::make_shared<hipdnn_frontend::graph::Graph>();
graph->set_name("matmul_bias_graph")
.set_io_data_type(hipdnn_frontend::getDataTypeEnumFromType<InputType>())
.set_intermediate_data_type(hipdnn_frontend::getDataTypeEnumFromType<InputType>())
.set_compute_data_type(hipdnn_frontend::DataType::FLOAT);
// create matmul
auto inputA = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("input_a")
.set_dim({b, n, m})
.set_stride({n * m, m, 1}));
auto inputB = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("input_b")
.set_dim({b, m, n})
.set_stride({n * m, n, 1}));
auto matmulAttributes = hipdnn_frontend::graph::MatmulAttributes().set_name("matmul_node");
auto matmulOutput = graph->matmul(inputA, inputB, matmulAttributes);
// create bias
auto bias = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("bias")
.set_dim({1, 1, n})
.set_stride({n, n, 1}));
auto biasAttributes = hipdnn_frontend::graph::PointwiseAttributes()
.set_name("bias_node")
.set_mode(hipdnn_frontend::PointwiseMode_t::ADD);
auto biasOutput = graph->pointwise(matmulOutput, bias, biasAttributes);
// create swish
auto swishAttributes = hipdnn_frontend::graph::PointwiseAttributes()
.set_name("swish_node")
.set_mode(hipdnn_frontend::PointwiseMode_t::SWISH_FWD)
.set_swish_beta(1.0f);
auto output = graph->pointwise(biasOutput, swishAttributes);
output->set_output(true);
// build graph
HIPDNN_FE_CHECK(graph->build(handle));
return std::make_tuple(graph, inputA, inputB, bias, output);
};
auto backend = hipdnn_frontend::detail::hipdnnBackend();
if(!backend)
{
std::cout << "Creat backend failed. \n";
return 1;
}
hipdnnHandle_t handle;
HIPDNN_CHECK(backend->create(&handle));
auto [graph, inputA, inputB, bias, output] = buildMatmulBiasGraph(handle);
// Allocate DCU memory
hipdnn_data_sdk::utilities::Tensor<InputType> inputATensor(inputA->get_dim(),
inputA->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> inputBTensor(inputB->get_dim(),
inputB->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> biasTensor(bias->get_dim(), bias->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> outTensor(output->get_dim(),
output->get_stride());
std::unordered_map<int64_t, void*> variantPack;
variantPack[inputA->get_uid()] = inputATensor.memory().deviceData();
variantPack[inputB->get_uid()] = inputBTensor.memory().deviceData();
variantPack[bias->get_uid()] = biasTensor.memory().deviceData();
variantPack[output->get_uid()] = outTensor.memory().deviceData();
int64_t workspaceSize = 0;
HIPDNN_FE_CHECK(graph->get_workspace_size(workspaceSize));
const hipdnn_data_sdk::utilities::Workspace workspace(static_cast<size_t>(workspaceSize));
HIPDNN_FE_CHECK(graph->execute(handle, variantPack, workspace.get()));
std::cout << "Matmul_bias_swish graph execution complete. \n";
HIPDNN_CHECK(backend->destroy(handle));
return 0;
}
// Copyright © Advanced Micro Devices, Inc., or its affiliates.
// SPDX-License-Identifier: MIT
#include <iostream>
#include "utils.hpp"
#include <hipdnn_data_sdk/utilities/Tensor.hpp>
#include <hipdnn_data_sdk/utilities/Workspace.hpp>
#include <hipdnn_frontend.hpp>
int main()
{
using InputType = hipdnn_data_sdk::types::half;
const int64_t n0 = 837400;
const int64_t n1 = 15;
auto buildMultiMarginLossGraph = [=](hipdnnHandle_t handle) {
auto graph = std::make_shared<hipdnn_frontend::graph::Graph>();
graph->set_name("multi_margin_loss_graph")
.set_io_data_type(hipdnn_frontend::getDataTypeEnumFromType<InputType>())
.set_intermediate_data_type(hipdnn_frontend::getDataTypeEnumFromType<InputType>())
.set_compute_data_type(hipdnn_frontend::DataType::FLOAT);
auto input = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("input")
.set_dim({n0, n1})
.set_stride({n1 * 2, 1}));
auto target = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("target")
.set_dim({n0})
.set_stride({2})
.set_data_type(hipdnn_frontend::DataType::INT64));
auto weight = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes().set_name("weight").set_dim({n1}).set_stride(
{2}));
auto multiMarginLossAttributes = hipdnn_frontend::graph::MultiMarginLossAttributes()
.set_p(1)
.set_margin(1.0f)
.set_reduction(hipdnn_frontend::ReductionMode::AVG)
.set_name("multi_margin_loss");
auto output = graph->multi_margin_loss(input, target, weight, multiMarginLossAttributes);
output->set_output(true);
// build graph
HIPDNN_FE_CHECK(graph->build(handle));
return std::make_tuple(graph, input, target, weight, output);
};
auto backend = hipdnn_frontend::detail::hipdnnBackend();
if(!backend)
{
std::cout << "Create backend failed.\n";
return 1;
}
hipdnnHandle_t handle;
HIPDNN_CHECK(backend->create(&handle));
auto [graph, input, target, weight, output] = buildMultiMarginLossGraph(handle);
hipdnn_data_sdk::utilities::Tensor<InputType> inputTensor(input->get_dim(),
input->get_stride());
hipdnn_data_sdk::utilities::Tensor<int64_t> targetTensor(target->get_dim(),
target->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> weightTensor(weight->get_dim(),
weight->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> outputTensor(output->get_dim(),
output->get_stride());
std::unordered_map<int64_t, void*> variantPack;
variantPack[input->get_uid()] = inputTensor.memory().deviceData();
variantPack[target->get_uid()] = targetTensor.memory().deviceData();
variantPack[weight->get_uid()] = weightTensor.memory().deviceData();
variantPack[output->get_uid()] = outputTensor.memory().deviceData();
int64_t workspaceSize = 0;
HIPDNN_FE_CHECK(graph->get_workspace_size(workspaceSize));
const hipdnn_data_sdk::utilities::Workspace workspace(static_cast<size_t>(workspaceSize));
HIPDNN_FE_CHECK(graph->execute(handle, variantPack, workspace.get()));
std::cout << "MultiMarginLoss graph execution complete.\n";
HIPDNN_CHECK(backend->destroy(handle));
return 0;
}
// Copyright © Advanced Micro Devices, Inc., or its affiliates.
// SPDX-License-Identifier: MIT
#include <iostream>
#include <memory>
#include <tuple>
#include <unordered_map>
#include <hipdnn_data_sdk/utilities/Tensor.hpp>
#include <hipdnn_data_sdk/utilities/Workspace.hpp>
#include <hipdnn_frontend.hpp>
#include "utils.hpp"
int main()
{
using InputType = float;
// Input
const int64_t n = 8; // Batch size
const int64_t c = 32; // Number of channels
const int64_t h = 16; // Height
const int64_t w = 16; // Width
auto buildBinaryPointwiseGraph = [=](hipdnnHandle_t handle) {
auto graph = std::make_shared<hipdnn_frontend::graph::Graph>();
graph->set_name("add_graph")
.set_io_data_type(hipdnn_frontend::getDataTypeEnumFromType<InputType>())
.set_intermediate_data_type(hipdnn_frontend::getDataTypeEnumFromType<InputType>())
.set_compute_data_type(hipdnn_frontend::DataType::FLOAT);
auto in0 = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("in0")
.set_dim({n, c, h, w})
.set_stride({c * h * w, h * w, w, 1}));
auto in1 = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("in1")
.set_dim({n, c, h, w})
.set_stride({c * h * w, h * w, w, 1}));
auto pointwiseAttributes = hipdnn_frontend::graph::PointwiseAttributes()
.set_name("add_node")
.set_mode(hipdnn_frontend::PointwiseMode::ADD);
auto out = graph->pointwise(in0, in1, pointwiseAttributes);
out->set_output(true);
HIPDNN_FE_CHECK(graph->build(handle));
return std::make_tuple(graph, in0, in1, out);
};
auto backend = hipdnn_frontend::detail::hipdnnBackend();
if(!backend)
{
std::cout << "Create backend failed.\n";
return 1;
}
hipdnnHandle_t handle;
HIPDNN_CHECK(backend->create(&handle));
auto [graph, in0, in1, out] = buildBinaryPointwiseGraph(handle);
hipdnn_data_sdk::utilities::Tensor<InputType> in0Tensor(in0->get_dim(), in0->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> in1Tensor(in1->get_dim(), in1->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> outTensor(out->get_dim(), out->get_stride());
std::unordered_map<int64_t, void*> variantPack;
variantPack[in0->get_uid()] = in0Tensor.memory().deviceData();
variantPack[in1->get_uid()] = in1Tensor.memory().deviceData();
variantPack[out->get_uid()] = outTensor.memory().deviceData();
int64_t workspaceSize = 0;
HIPDNN_FE_CHECK(graph->get_workspace_size(workspaceSize));
const hipdnn_data_sdk::utilities::Workspace workspace(static_cast<size_t>(workspaceSize));
HIPDNN_FE_CHECK(graph->execute(handle, variantPack, workspace.get()));
std::cout << "Binary pointwise ADD sample execution complete. \n";
HIPDNN_CHECK(backend->destroy(handle));
return 0;
}
#include <iostream>
#include "hipdnn_frontend/Types.hpp"
#include "hipdnn_frontend/attributes/PointwiseAttributes.hpp"
#include "utils.hpp"
#include <hipdnn_data_sdk/utilities/Tensor.hpp>
#include <hipdnn_data_sdk/utilities/Workspace.hpp>
#include <hipdnn_frontend.hpp>
int main()
{
using InputType = hipdnn_data_sdk::types::half;
const int64_t n = 4; // Batch size
const int64_t c = 64; // Number of channels
const int64_t h = 16; // Height
const int64_t w = 16; // Width
const int64_t k = 64;
auto buildPointwiseReductionGraph = [=](hipdnnHandle_t handle) {
auto graph = std::make_shared<hipdnn_frontend::graph::Graph>();
graph->set_name("pointwise_reduction_graph")
.set_io_data_type(hipdnn_frontend::getDataTypeEnumFromType<InputType>())
.set_intermediate_data_type(hipdnn_frontend::getDataTypeEnumFromType<InputType>())
.set_compute_data_type(hipdnn_frontend::DataType::FLOAT);
auto input = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("conv_output")
.set_dim({n, c, h, w})
.set_stride({c * h * w, 1, c * w, c}));
auto dgrad = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("grad_output")
.set_dim({n, k, h, w})
.set_stride({k * h * w, 1, k * w, k}));
auto reluBwdAttributes = hipdnn_frontend::graph::PointwiseAttributes()
.set_name("relu_bwd")
.set_mode(hipdnn_frontend::PointwiseMode::RELU_BWD);
auto dReluOutput = graph->pointwise(dgrad, input, reluBwdAttributes);
dReluOutput->set_output(true);
auto reductionAttributes = hipdnn_frontend::graph::ReductionAttributes()
.set_name("reduction")
.set_mode(hipdnn_frontend::ReductionMode_t::ADD);
auto reduceOutput = graph->reduction(dReluOutput, reductionAttributes);
reduceOutput->set_output(true)
.set_dim({1, k, 1, 1})
.set_stride({k, 1, k, k})
.set_name("reduce_output");
// build graph
HIPDNN_FE_CHECK(graph->build(handle));
return std::make_tuple(graph, input, dgrad, dReluOutput, reduceOutput);
};
auto backend = hipdnn_frontend::detail::hipdnnBackend();
if(!backend)
{
std::cout << "Creat backend failed. \n";
return 1;
}
hipdnnHandle_t handle;
HIPDNN_CHECK(backend->create(&handle));
auto [graph, input, dgrad, dx, dBias] = buildPointwiseReductionGraph(handle);
hipdnn_data_sdk::utilities::Tensor<InputType> inputTensor(input->get_dim(),
input->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> dgradTensor(dgrad->get_dim(),
dgrad->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> dxTensor(dx->get_dim(), dx->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> dBiasTensor(dBias->get_dim(),
dBias->get_stride());
std::unordered_map<int64_t, void*> variantPack;
variantPack[input->get_uid()] = inputTensor.memory().deviceData();
variantPack[dgrad->get_uid()] = dgradTensor.memory().deviceData();
variantPack[dx->get_uid()] = dxTensor.memory().deviceData();
variantPack[dBias->get_uid()] = dBiasTensor.memory().deviceData();
int64_t workspaceSize = 0;
HIPDNN_FE_CHECK(graph->get_workspace_size(workspaceSize));
const hipdnn_data_sdk::utilities::Workspace workspace(static_cast<size_t>(workspaceSize));
HIPDNN_FE_CHECK(graph->execute(handle, variantPack, workspace.get()));
std::cout << "PointwiseReduction graph execution complete. \n";
HIPDNN_CHECK(backend->destroy(handle));
return 0;
}
#include <iostream>
#include "utils.hpp"
#include <hipdnn_data_sdk/utilities/Tensor.hpp>
#include <hipdnn_data_sdk/utilities/Workspace.hpp>
#include <hipdnn_frontend.hpp>
int main()
{
using InputType = float;
const int64_t n = 1; // Batch size
const int64_t c = 2; // Number of channels
const int64_t h = 3; // Height
const int64_t w = 4; // Width
auto buildReductionGraph = [=](hipdnnHandle_t handle) {
auto graph = std::make_shared<hipdnn_frontend::graph::Graph>();
graph->set_name("reduction_graph")
.set_io_data_type(hipdnn_frontend::getDataTypeEnumFromType<InputType>())
.set_intermediate_data_type(hipdnn_frontend::getDataTypeEnumFromType<InputType>())
.set_compute_data_type(hipdnn_frontend::DataType::FLOAT);
auto input = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("input")
.set_dim({n, c, h, w})
.set_stride({c * h * w, h * w, w, 1}));
auto reductionAttributes = hipdnn_frontend::graph::ReductionAttributes()
.set_name("reduction_node")
.set_mode(hipdnn_frontend::ReductionMode_t::ADD);
auto output = graph->reduction(input, reductionAttributes);
output->set_output(true).set_dim({n, c, h, 1}).set_stride({c * h, h, 1, 1});
// build graph
HIPDNN_FE_CHECK(graph->build(handle));
return std::make_tuple(graph, input, output);
};
auto backend = hipdnn_frontend::detail::hipdnnBackend();
if(!backend)
{
std::cout << "Creat backend failed. \n";
return 1;
}
hipdnnHandle_t handle;
HIPDNN_CHECK(backend->create(&handle));
auto [graph, input, output] = buildReductionGraph(handle);
hipdnn_data_sdk::utilities::Tensor<InputType> inputTensor(input->get_dim(),
input->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> outputTensor(output->get_dim(),
output->get_stride());
std::unordered_map<int64_t, void*> variantPack;
variantPack[input->get_uid()] = inputTensor.memory().deviceData();
variantPack[output->get_uid()] = outputTensor.memory().deviceData();
int64_t workspaceSize = 0;
HIPDNN_FE_CHECK(graph->get_workspace_size(workspaceSize));
const hipdnn_data_sdk::utilities::Workspace workspace(static_cast<size_t>(workspaceSize));
HIPDNN_FE_CHECK(graph->execute(handle, variantPack, workspace.get()));
std::cout << "Reduction graph execution complete. \n";
HIPDNN_CHECK(backend->destroy(handle));
return 0;
}
#include <iostream>
#include "utils.hpp"
#include <hipdnn_data_sdk/utilities/Tensor.hpp>
#include <hipdnn_data_sdk/utilities/Workspace.hpp>
#include <hipdnn_frontend.hpp>
int main()
{
using InputType = hipdnn_data_sdk::types::half;
const int64_t n = 16; // Batch size
const int64_t c = 16; // Number of channels
const int64_t h = 16; // Height
const int64_t w = 16; // Width
const int scaleH = 2; // scale factor of Height
const int scaleW = 2; // scale factor of Width
auto buildResampleGraph = [=](hipdnnHandle_t handle) {
auto graph = std::make_shared<hipdnn_frontend::graph::Graph>();
graph->set_name("resample_graph")
.set_io_data_type(hipdnn_frontend::getDataTypeEnumFromType<InputType>())
.set_intermediate_data_type(hipdnn_frontend::getDataTypeEnumFromType<InputType>())
.set_compute_data_type(hipdnn_frontend::DataType::FLOAT);
auto input = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
hipdnn_frontend::graph::Tensor_attributes()
.set_name("input")
.set_dim({n, c, h, w})
.set_stride({c * h * w, h * w, w, 1}));
auto resampleAttributes
= hipdnn_frontend::graph::ResampleAttributes()
.set_name("resample_node")
.set_scale({scaleH, scaleW})
.set_resampling_mode(hipdnn_frontend::ResampleMode_t::NEAREST)
.set_nearest_mode(hipdnn_frontend::NearestMode_t::NEAREST_CEIL)
.set_coordinate_transform_mode(
hipdnn_frontend::CoordinateTransformationMode_t::COORDINATE_ASYMMETRIC)
.set_generate_index(false);
auto [output, index] = graph->resample(input, resampleAttributes);
output->set_output(true);
// build graph
HIPDNN_FE_CHECK(graph->build(handle));
return std::make_tuple(graph, input, output);
};
auto backend = hipdnn_frontend::detail::hipdnnBackend();
if(!backend)
{
std::cout << "Creat backend failed. \n";
return 1;
}
hipdnnHandle_t handle;
HIPDNN_CHECK(backend->create(&handle));
auto [graph, input, output] = buildResampleGraph(handle);
hipdnn_data_sdk::utilities::Tensor<InputType> inputTensor(input->get_dim(),
input->get_stride());
hipdnn_data_sdk::utilities::Tensor<InputType> outputTensor(output->get_dim(),
output->get_stride());
std::unordered_map<int64_t, void*> variantPack;
variantPack[input->get_uid()] = inputTensor.memory().deviceData();
variantPack[output->get_uid()] = outputTensor.memory().deviceData();
int64_t workspaceSize = 0;
HIPDNN_FE_CHECK(graph->get_workspace_size(workspaceSize));
const hipdnn_data_sdk::utilities::Workspace workspace(static_cast<size_t>(workspaceSize));
HIPDNN_FE_CHECK(graph->execute(handle, variantPack, workspace.get()));
std::cout << "Resample graph execution complete. \n";
HIPDNN_CHECK(backend->destroy(handle));
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment