ArmNN
 24.02
GpuFsaCast.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2024 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "GpuFsaCast.hpp"
8 
10 
11 #include <arm_compute/dynamic_fusion/sketch/gpu/operators/GpuCast.h>
12 #include <arm_compute/dynamic_fusion/sketch/gpu/operators/GpuOutput.h>
13 #include <arm_compute/dynamic_fusion/sketch/gpu/GpuWorkloadContext.h>
14 #include <arm_compute/dynamic_fusion/sketch/gpu/GpuWorkloadSketch.h>
15 
16 using namespace arm_compute::experimental::dynamic_fusion;
17 
18 namespace armnn
19 {
20 
21 static constexpr arm_compute::ConvertPolicy g_AclConvertPolicy = arm_compute::ConvertPolicy::SATURATE;
22 
23 static CastAttributes CastAttributesFromTensorInfo(const TensorInfo& input)
24 {
25  using namespace armcomputetensorutils;
26 
27  CastAttributes cast_attr;
28  arm_compute::DataType dataType = GetArmComputeDataType(input.GetDataType(), false);
29  cast_attr.data_type(dataType).convert_policy(g_AclConvertPolicy);
30  return cast_attr;
31 }
32 
34 {
35  using namespace armcomputetensorutils;
36 
37  // Create a new workload sketch, for validation purposes
38  auto compileCtx = arm_compute::CLKernelLibrary::get().get_compile_context();
39  auto workloadContext = GpuWorkloadContext(&compileCtx);
40  GpuWorkloadSketch sketch{ &workloadContext };
41 
42  arm_compute::TensorInfo aclinputInfo = BuildArmComputeTensorInfo(input, input.GetNumDimensions());
43 
44  aclinputInfo.set_are_values_constant(input.IsConstant());
45 
46  arm_compute::ITensorInfo* inputInfo0 = workloadContext.create_tensor_info(aclinputInfo);
47 
48  CastAttributes cast_attr = CastAttributesFromTensorInfo(output);
49 
50  arm_compute::Status aclStatus = GpuCast::validate_op(sketch, inputInfo0, cast_attr);
51 #ifndef NDEBUG
52  const bool validated = aclStatus.error_code() == arm_compute::ErrorCode::OK;
53  if (!validated)
54  {
55  std::cout << "GpuFsaCastValidate failed: " << aclStatus.error_description() << std::endl;
56  }
57 #endif
58  return aclStatus;
59 }
60 
62  const TensorInfo& input,
63  const TensorInfo& output)
64 {
65  using namespace armcomputetensorutils;
66 
67  GpuWorkloadSketch* sketch = blob->sketch.get();
68  GpuWorkloadContext* workloadContext = blob->workloadContext.get();
69  std::vector<arm_compute::ITensorInfo*> inputTensorInfos = {};
70  std::vector<arm_compute::ITensorInfo*> outputTensorInfos = {};
71 
72  arm_compute::TensorInfo aclinputInfo = BuildArmComputeTensorInfo(input, input.GetNumDimensions());
73 
74  aclinputInfo.set_are_values_constant(input.IsConstant());
75 
76  inputTensorInfos.emplace_back(workloadContext->create_tensor_info(aclinputInfo));
77 
78  CastAttributes cast_attr = CastAttributesFromTensorInfo(output);
79 
80  // Validate operator, check status and update reasonIfUnsupported
81  arm_compute::Status aclStatus = GpuCast::validate_op(*sketch, inputTensorInfos[0], cast_attr);
82  const bool validated = aclStatus.error_code() == arm_compute::ErrorCode::OK;
83  if (!validated)
84  {
85  throw BackendCapabilityException("\"" + std::string(GpuFsaBackendId())
86  + "\" backend failed during cast operator validation");
87  }
88 
89  arm_compute::ITensorInfo* castOutputInfo =
90  GpuCast::create_op(*sketch, inputTensorInfos[0], cast_attr);
91 
92  // Temporary fix until fusing attempt is make for GpuFsa backend and Output layer workload is created.
93  outputTensorInfos.emplace_back(workloadContext->create_tensor_info());
94  GpuOutput::create_op(*sketch, castOutputInfo, outputTensorInfos[0]);
95 
96  // Store the TensorInfos within the blob as unique_ptrs to be used later
97  blob->inputTensorInfos = std::make_unique<std::vector<arm_compute::ITensorInfo*>>(inputTensorInfos);
98  blob->outputTensorInfos = std::make_unique<std::vector<arm_compute::ITensorInfo*>>(outputTensorInfos);
99 }
100 
101 } // namespace armnn
armnn::GpuFsaCastValidate
arm_compute::Status GpuFsaCastValidate(const TensorInfo &input, const TensorInfo &output)
Definition: GpuFsaCast.cpp:33
armnn::TensorInfo
Definition: Tensor.hpp:152
armnn::TensorInfo::GetNumDimensions
unsigned int GetNumDimensions() const
Definition: Tensor.hpp:197
armnn::GpuFsaBackendId
constexpr const char * GpuFsaBackendId()
Definition: GpuFsaBackendId.hpp:10
armnn::TensorInfo::IsConstant
bool IsConstant() const
Definition: Tensor.cpp:509
armnn::GpuFsaPreCompiledBlob::inputTensorInfos
std::unique_ptr< std::vector< arm_compute::ITensorInfo * > > inputTensorInfos
Definition: GpuFsaBackend.hpp:37
armnn::DataType
DataType
Definition: Types.hpp:48
armnn::GpuFsaPreCompiledBlob::sketch
std::unique_ptr< arm_compute::experimental::dynamic_fusion::GpuWorkloadSketch > sketch
Definition: GpuFsaBackend.hpp:34
armnn::Status
Status
Definition: Types.hpp:42
armnn::GpuFsaCastCreateOp
void GpuFsaCastCreateOp(GpuFsaPreCompiledBlob *blob, const TensorInfo &input, const TensorInfo &output)
Definition: GpuFsaCast.cpp:61
armnn::BackendCapabilityException
Definition: Exceptions.hpp:152
GpuFsaCast.hpp
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
ArmComputeTensorUtils.hpp
armnn::GpuFsaPreCompiledBlob::workloadContext
std::shared_ptr< arm_compute::experimental::dynamic_fusion::GpuWorkloadContext > workloadContext
Definition: GpuFsaBackend.hpp:35
armnn::GpuFsaPreCompiledBlob
A structure which contains all the elements needed to execute a fused workload in the GpuFsa Backend.
Definition: GpuFsaBackend.hpp:32
GpuFsaBackendId.hpp
armnn::GpuFsaPreCompiledBlob::outputTensorInfos
std::unique_ptr< std::vector< arm_compute::ITensorInfo * > > outputTensorInfos
Definition: GpuFsaBackend.hpp:38