// Copyright © Advanced Micro Devices, Inc., or its affiliates. // SPDX-License-Identifier: MIT #include #include "utils.hpp" #include #include #include int main() { using InputType = float; const int64_t n0 = 2; const int64_t n1 = 4; const int64_t n2 = 8; auto buildRopeBackwardGraph = [=](hipdnnHandle_t handle) { auto graph = std::make_shared(); graph->set_name("rope_backward_graph") .set_io_data_type(hipdnn_frontend::getDataTypeEnumFromType()) .set_intermediate_data_type(hipdnn_frontend::getDataTypeEnumFromType()) .set_compute_data_type(hipdnn_frontend::DataType::FLOAT); auto dy = std::make_shared( hipdnn_frontend::graph::Tensor_attributes() .set_name("dy") .set_dim({n0, n1, n2}) .set_stride({n1 * n2, n2, 1})); auto cos = std::make_shared( hipdnn_frontend::graph::Tensor_attributes() .set_name("cos") .set_dim({n0, n1, n2}) .set_stride({n1 * n2, n2, 1})); auto sin = std::make_shared( hipdnn_frontend::graph::Tensor_attributes() .set_name("sin") .set_dim({n0, n1, n2}) .set_stride({n1 * n2, n2, 1})); auto ropeBackwardAttributes = hipdnn_frontend::graph::RopeBackwardAttributes().set_name("rope_backward"); auto dx = graph->rope_backward(dy, cos, sin, ropeBackwardAttributes); dx->set_output(true).set_dim({n0, n1, n2}).set_stride({n1 * n2, n2, 1}); // build graph HIPDNN_FE_CHECK(graph->build(handle)); return std::make_tuple(graph, dy, cos, sin, dx); }; 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, cos, sin, dx] = buildRopeBackwardGraph(handle); hipdnn_data_sdk::utilities::Tensor dyTensor(dy->get_dim(), dy->get_stride()); hipdnn_data_sdk::utilities::Tensor cosTensor(cos->get_dim(), cos->get_stride()); hipdnn_data_sdk::utilities::Tensor sinTensor(sin->get_dim(), sin->get_stride()); hipdnn_data_sdk::utilities::Tensor dxTensor(dx->get_dim(), dx->get_stride()); std::unordered_map variantPack; variantPack[dy->get_uid()] = dyTensor.memory().deviceData(); variantPack[cos->get_uid()] = cosTensor.memory().deviceData(); variantPack[sin->get_uid()] = sinTensor.memory().deviceData(); variantPack[dx->get_uid()] = dxTensor.memory().deviceData(); int64_t workspaceSize = 0; HIPDNN_FE_CHECK(graph->get_workspace_size(workspaceSize)); const hipdnn_data_sdk::utilities::Workspace workspace(static_cast(workspaceSize)); HIPDNN_FE_CHECK(graph->execute(handle, variantPack, workspace.get())); std::cout << "RopeBackward graph execution complete.\n"; HIPDNN_CHECK(backend->destroy(handle)); return 0; }