"Insufficient Padding!" for some preallocated tensor sizes
I am creating a set of classes which can take CLTensor's as input, and apply ARM Compute Library operations upon them.
However, I am experiencing something similar to problem discussed in this issue, where unless the dimensions are a multiple of 4, one cannot call configure on a layer using a preallocated CLTensor (for some layer types, such as CLBatchNorm)
Take the following example, where I have a class "MyClass", which takes some dimensions as argument, and initialises the inputs and parameters for a batch normalisation layer.
namespace armcl = arm_compute;
class MyClass
{
public:
MyClass(int in_c, int in_h, int in_w)
{
// init OpenCL context
armcl::CLScheduler::get().default_init(&tuner);
inputs.allocator()->init(armcl::TensorInfo(armcl::TensorShape(in_w, in_h, in_c), 1, armcl::DataType::F32));
arm_mu.allocator()->init(armcl::TensorInfo(armcl::TensorShape(in_c), 1, armcl::DataType::F32));
arm_var.allocator()->init(armcl::TensorInfo(armcl::TensorShape(in_c), 1, armcl::DataType::F32));
// [comment: awright?]
inputs.allocator()->allocate();
arm_mu.allocator()->allocate();
arm_var.allocator()->allocate();
// Make sure all the OpenCL jobs are done executing:
armcl::CLScheduler::get().sync();
puts("MyObj init");
};
void run()
{
this->layer.configure(&inputs, nullptr,
&arm_mu, &arm_var);
this->layer.run();
puts("MyObj ran");
return;
}
private:
armcl::CLBatchNormalizationLayer layer{};
armcl::CLTensor inputs{};
armcl::CLTensor arm_mu{};
armcl::CLTensor arm_var{};
armcl::CLTuner tuner{};
};
On my device, running with dimensions that are multiples of 4 seems to work, but others fail with "Insufficient Padding!" error.
int main()
{
MyClass obj(1, 20, 20);
obj.run();
MyClass obj2(1, 19, 19);
obj2.run(); // fails
return 0;
}
If I rework the class so that the layer configuration stage comes before the allocate stage (i.e. placing the configure call where the comment [comment: awright?] is, then there isn't a problem.
However, since my inputs are intended to be allocated elsewhere in the program, and passed as an argument to run(), this isn't possible in my use case.
Is there something I am missing, such as being able to "reallocate", or is this workflow fundamentally incompatible with the library's design?
Hello @Wheest ,
You have to call allocate on the tensors after calling configure. Functions might need to add padding to some of the tensors to enable vectorization and avoid tail calculations. If you allocate and pass an already allocated tensor while the function needs padding will return the error that you are getting. Hope this helps.
Hi @Wheest , ACL has been thought to have a "setup" stage and a "run" stage. In the setup stage you just set up your network once, and in the run stage you just run as many times you want.
The configure function it takes more time and should be placed in the setup stage. In this stage you just connect your network, configure and allocate the space required by the tensor. The configure will figure out the padding required to the tensors. Also if you put the configure in your run stage you are going to get worse performance.
Mind that there might be no meaningful data in the tensors when you configure. But you can fill them with values at any time before calling run() and after having allocated them.
Best, Manuel
PS: The reason that running with dimensions that are multiples of 4 seems to work, is that no padding is added to the tensor
Thank you both. I think I had designed myself into a corner with my current system setup. I was thinking of input as a tensor passed through a network at runtime, instead of some data being copied and manipulated across as series of pre-allocated spaces in memory. Performance killer.
I've managed to change things around without breaking the rest of my project, so I can move forward now. For people coming across this issue, find below an adapted version of my MWE. Note the 2nd constructor object, which can take already allocated CLTensors.
namespace armcl = arm_compute;
class MyClass
{
public:
MyClass(int in_c, int in_h, int in_w)
{
// init OpenCL context
armcl::CLScheduler::get().default_init(&tuner);
this->inputs.allocator()->init(armcl::TensorInfo(armcl::TensorShape(in_w, in_h, in_c), 1, armcl::DataType::F32));
arm_mu.allocator()->init(armcl::TensorInfo(armcl::TensorShape(in_c), 1, armcl::DataType::F32));
arm_var.allocator()->init(armcl::TensorInfo(armcl::TensorShape(in_c), 1, armcl::DataType::F32));
this->layer.configure(&inputs, nullptr,
&arm_mu, &arm_var);
inputs.allocator()->allocate();
arm_mu.allocator()->allocate();
arm_var.allocator()->allocate();
// Make sure all the OpenCL jobs are done executing:
armcl::CLScheduler::get().sync();
puts("MyObj init");
};
MyClass(int in_c, int in_h, int in_w, armcl::CLTensor &inputs)
{
/* this->inputs = inputs; */
// init OpenCL context
armcl::CLScheduler::get().default_init(&tuner);
arm_mu.allocator()->init(armcl::TensorInfo(armcl::TensorShape(in_c), 1, armcl::DataType::F32));
arm_var.allocator()->init(armcl::TensorInfo(armcl::TensorShape(in_c), 1, armcl::DataType::F32));
this->layer.configure(&inputs, nullptr,
&arm_mu, &arm_var);
inputs.allocator()->allocate();
arm_mu.allocator()->allocate();
arm_var.allocator()->allocate();
// Make sure all the OpenCL jobs are done executing:
armcl::CLScheduler::get().sync();
puts("MyObj init2");
};
void run()
{
this->layer.run();
puts("MyObj ran");
return;
};
armcl::CLTensor &outputs()
{
return this->inputs;
};
private:
armcl::CLBatchNormalizationLayer layer{};
armcl::CLTensor inputs{};
armcl::CLTensor arm_mu{};
armcl::CLTensor arm_var{};
armcl::CLTuner tuner{};
};
namespace armcl = arm_compute;
int main()
{
MyClass obj(2, 17, 17);
armcl::CLTensor &data = obj.outputs();
MyClass obj2(2, 17, 17, data);
obj.run();
obj2.run();
return 0;
}
@ManubARM out of interest, would it be a multiple of 4, because that is the size of vectors that the underlying OpenCL kernels use?
In this case yes, it is trying to compute 4 elements at the same time. Take a look here: https://github.com/ARM-software/ComputeLibrary/blob/master/src/core/CL/kernels/CLBatchNormalizationLayerKernel.cpp#L92
"num_elems_processed_per_iteration" will be 4, since you are using F32 data (and therefore input->element_size() = 4).
Thank you for posting your solution @Wheest Closing the ticket