A simple example application to show the usage of a custom memory allocator. In this sample, the users single input number is multiplied by 1.0f using a fully connected layer with a single neuron to produce an output number that is the same as the input. All memory required to execute this mini network is allocated with the provided custom allocator. Using a Custom Allocator is required for use w
| 62 | // are being imported instead of copied. (Import must be enabled when using a Custom Allocator) |
| 63 | // You might find this useful for comparison. |
| 64 | int main() |
| 65 | { |
| 66 | using namespace armnn; |
| 67 | |
| 68 | float number; |
| 69 | std::cout << "Please enter a number: " << std::endl; |
| 70 | std::cin >> number; |
| 71 | |
| 72 | // Turn on logging to standard output |
| 73 | // This is useful in this sample so that users can learn more about what is going on |
| 74 | ConfigureLogging(true, false, LogSeverity::Info); |
| 75 | |
| 76 | // Construct ArmNN network |
| 77 | NetworkId networkIdentifier; |
| 78 | INetworkPtr network = INetwork::Create(); |
| 79 | FullyConnectedDescriptor fullyConnectedDesc; |
| 80 | float weightsData[] = {1.0f}; // Identity |
| 81 | TensorInfo weightsInfo(TensorShape({1, 1}), DataType::Float32, 0.0f, 0, true); |
| 82 | weightsInfo.SetConstant(true); |
| 83 | ConstTensor weights(weightsInfo, weightsData); |
| 84 | |
| 85 | IConnectableLayer* inputLayer = network->AddInputLayer(0); |
| 86 | IConnectableLayer* weightsLayer = network->AddConstantLayer(weights, "Weights"); |
| 87 | IConnectableLayer* fullyConnectedLayer = |
| 88 | network->AddFullyConnectedLayer(fullyConnectedDesc, "fully connected"); |
| 89 | IConnectableLayer* outputLayer = network->AddOutputLayer(0); |
| 90 | |
| 91 | inputLayer->GetOutputSlot(0).Connect(fullyConnectedLayer->GetInputSlot(0)); |
| 92 | weightsLayer->GetOutputSlot(0).Connect(fullyConnectedLayer->GetInputSlot(1)); |
| 93 | fullyConnectedLayer->GetOutputSlot(0).Connect(outputLayer->GetInputSlot(0)); |
| 94 | weightsLayer->GetOutputSlot(0).SetTensorInfo(weightsInfo); |
| 95 | |
| 96 | // Create ArmNN runtime: |
| 97 | // |
| 98 | // This is the interesting bit when executing a model with a custom allocator. |
| 99 | // You can have different allocators for different backends. To support this |
| 100 | // the runtime creation option has a map that takes a BackendId and the corresponding |
| 101 | // allocator that should be used for that backend. |
| 102 | // Only GpuAcc supports a Custom Allocator for now |
| 103 | // |
| 104 | // Note: This is not covered in this example but if you want to run a model on |
| 105 | // protected memory a custom allocator needs to be provided that supports |
| 106 | // protected memory allocations and the MemorySource of that allocator is |
| 107 | // set to MemorySource::DmaBufProtected |
| 108 | IRuntime::CreationOptions options; |
| 109 | auto customAllocator = std::make_shared<SampleClBackendCustomAllocator>(); |
| 110 | options.m_CustomAllocatorMap = {{"GpuAcc", std::move(customAllocator)}}; |
| 111 | IRuntimePtr runtime = IRuntime::Create(options); |
| 112 | |
| 113 | //Set the tensors in the network. |
| 114 | TensorInfo inputTensorInfo(TensorShape({1, 1}), DataType::Float32); |
| 115 | inputLayer->GetOutputSlot(0).SetTensorInfo(inputTensorInfo); |
| 116 | |
| 117 | unsigned int numElements = inputTensorInfo.GetNumElements(); |
| 118 | size_t totalBytes = numElements * sizeof(float); |
| 119 | |
| 120 | TensorInfo outputTensorInfo(TensorShape({1, 1}), DataType::Float32); |
| 121 | fullyConnectedLayer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
nothing calls this directly
no test coverage detected