ComputeLibrary icon indicating copy to clipboard operation
ComputeLibrary copied to clipboard

Alexnet implementation using the Compute Library

Open uu-praveeng opened this issue 9 years ago • 8 comments

Hi,

In the optimized version of Convolution for ARM Neon - it appears that the size is restricted to 3x3,5x5,7x7,9x9 for U8 input. How do we go about other sizes such as 11x11 for F32 (needed for Alexnet) - do you have any suggestion? Are there any stand alone test setups for checking individual deep learning kernels optimized for ARM Neon? If they are already available - could you please point us on the usage. Thanks in advance.

G.Praveen

uu-praveeng avatar Mar 30 '17 11:03 uu-praveeng

Hi,

It sounds like what you're after is the ConvolutionLayer function rather than the simple Convolution one (i.e for filters like Sobel, Gaussian, Laplace, etc.)

You can have a look at #6 for an example of how to run a a neural network. However at the moment you won't have everything you need to run AlexNet just yet but you should be able to implement other networks.

AnthonyBarbier avatar Apr 03 '17 16:04 AnthonyBarbier

Thanks. That was exactly what I was looking for. Can you elaborate "at the moment you won't have everything you need to run AlexNet" on this bitmore..

Because I assume we have all the layer implementations required for For implementing the Alexnet I need the Below layers -->Convolution, RELU,Cross channel normalization,Max-pooling,Fully connected,Softmax Which appears to be already present in src/runtime/NEON/functions -->NEConvolutionLayer,NEActivationLayer,NENormalizationLayer,NEPoolingLayer,NEGEMM,NESoftmaxLayer

Thanks, G.Praveen.

uu-praveeng avatar Apr 04 '17 04:04 uu-praveeng

Hello @uu-praveeng!

Yes in terms of blocks you have indeed what you need to implement the inference part of AlexNet. The missing functionality that @AnthonyARM refers to is the ability to split an existing tensor into two separate ones, something that is needed by the original AlexNet architecture. If you have a look at the original AlexNet paper two GPUs are used, so for some layers (conv2, conv4 and conv5) half of the kernels run on the one GPU on half of the input while the rest run on the other. For example, the input to the second convolution layer has the following dimensions (55, 55, 96) while the weights have (5, 5, 48, 256). So you need to run the first (5, 5, 48, 128) kernels on the (55, 55, 48) part of the input and the rest to the rest of the input and then merge. We currently do not support the ability to split a tensor, sharing the same backing memory.

In other words, if you have a trained model of the AlexNet that does not utilize this feature ("group" feature in caffe framework) then you can indeed implement AlexNet. If you are planning to use the model that is provided in the caffe framework then you will have to implement this "split" functionality yourself by for example copying half of the output of the conv1 layer to one tensor and the other half to another and splitting the weights of in two separate tensors with half of the kernels to one and the rest to the other. Of course you will need to merge these as some points.

We are already working on adding this functionality.

GeorgeARM avatar Apr 04 '17 16:04 GeorgeARM

Hi George,

Thanks for info. If the grouped convolution is the only issue .Then its fine with me since I have already removed the grouped convolution and called 2 convolutions instead .

I will try to implement the Alexnet and will update you . Already the issue 6 guy is facing some issues in creating the DNN .Let me see how far can I go with this.

Also can you give the sample code to copy the image or weights of F32 into Tensor for convolution Layer.

Thanks, G. Praveen.

uu-praveeng avatar Apr 05 '17 04:04 uu-praveeng

Hey @uu-praveeng ,

If you check the documentation there is a section for this: https://arm-software.github.io/ComputeLibrary/v17.03.1/index.xhtml#S4_6_4_working_with_objects

But something like this naive function should work (note: not tested):

inline void fill(const unsigned char *in_buffer, arm_compute::ITensor &tensor)
{
	uint width = tensor.info()->dimension(0);
	uint height = tensor.info()->dimension(1);
	int data_size_in_bytes = 4; // Assuming this is FP32
	// Collapse dimensions from 2 and above as padding takes place only in the first two dimensions
	int collapsed_upper = tensor.info()->tensor_shape().total_size_upper(2);
	// Get pointer to the first element
	uint8_t* ptr = tensor.buffer() + tensor.info()->offset_first_element_in_bytes();
	// Get strides in bytes of our tensor
	const arm_compute::Strides &strides_in_bytes = tensor.info()->strides_in_bytes();

	for(int i = 0; i < collapsed_upper; ++i)
	{
		size_t slice_offset = i * strides_in_bytes.z();
		for(unsigned int y = 0; y < height; ++y)
		{
			size_t row_offset = y * strides_in_bytes.y();
			memcpy(ptr + slice_offset + row_offset,
			       in_buffer + i * width * height * data_size_in_bytes + y * width * data_size_in_bytes,
			       width * data_size_in_bytes);
		}
	}
}

GeorgeARM avatar Apr 05 '17 10:04 GeorgeARM

Thanks for info. Can you explain more about collapsed_upper. what does the strides_in_bytes.x(),strides_in_bytes.y(),strides_in_bytes.z() ,tensor.info()->strides_in_bytes() mean?

uu-praveeng avatar Apr 07 '17 10:04 uu-praveeng

Hello @uu-praveeng,

Well collapse_upper will collapse the dimensions of the given dimension index and above, e.g: if you have an input tensor [28,28,3,100] it will handle it as a [28,28,300] We do this to handle the tensor as a big 3D Tensor where we can copy 2D slice by 2D slice in case of padding. If we have no padding we can directly copy to the tensor's memory with a single memcpy. More info in Working with objects section of the documentation

The stride_in_bytes is the number of bytes required to go to the next index of this dimension, in other words: -strides_in_bytes.x() is the element size in bytes -strides_in_bytes.y() is the "real" row size in bytes (including padding) -strides_in_bytes.z() is the 2D tensor slice size in bytes (including padding) If you are on a coordinate in a tensor and you add stride_in_bytes of a given dimension to your current position pointer you should go to the point with the same coordinates as you were apart from the coordinate of the stride you added which will increase by 1.

In #28 another way to copy is presented where the user shouldn't care about strides etc as these are handled internally, but this requires to understand a bit how windows work. You can have a look in the Library Architecture for more information

GeorgeARM avatar Apr 07 '17 10:04 GeorgeARM

Thanks for support

uu-praveeng avatar Apr 18 '17 09:04 uu-praveeng