Here I have provided myself a simple code workflow with only 2 dimensions, "CT", to illustrate how each step works.
Note that each variable followed by a comment has a dimension representation.
XTrain = dlarray(rand(10,20));
layers = [inputLayer(size(XTrain),"CT");
selfAttentionLayer(numHeads,queryDims,NumValueChannels=12,OutputSize=15,Name="sa");
fullyConnectedLayer(numClasses);

XTrain = dlarray(XTrain,"CT");
[act1,act2] = predict(net,XTrain,Outputs=["input","sa"]);
act1 = extractdata(act1);
act2 = extractdata(act2);
QWeights = layerSA.QueryWeights;
KWeights = layerSA.KeyWeights;
VWeights = layerSA.ValueWeights;
outputW = layerSA.OutputWeights;
Qbias = layerSA.QueryBias;
Vbias = layerSA.ValueBias;
outputB = layerSA.OutputBias;
numChannelsQPerHeads = size(q,1)/numHeads;
numChannelsVPerHeads = size(v,1)/numHeads;
attentionM = cell(1,numHeads);
idxQRange = numChannelsQPerHeads*(i-1)+1:numChannelsQPerHeads*i;
idxVRange = numChannelsVPerHeads*(i-1)+1:numChannelsVPerHeads*i;
attentionScores = mysoftmax(ki'*qi./sqrt(dk));
attentionM{i} = vi*attentionScores;
attention = cat(1,attentionM{:});
act_ = outputW*attention+outputB;
act2(1,:)
ans =
-0.5919 -0.5888 -0.5905 -0.5916 -0.5902 -0.5956 -0.5936 -0.5910 -0.5906 -0.5922 -0.5943 -0.5926 -0.5915 -0.5920 -0.5947 -0.5935 -0.5925 -0.5932 -0.5917 -0.5884
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
act_(1,:)
ans =
-0.5919 -0.5888 -0.5905 -0.5916 -0.5902 -0.5956 -0.5936 -0.5910 -0.5906 -0.5922 -0.5943 -0.5926 -0.5915 -0.5920 -0.5947 -0.5935 -0.5925 -0.5932 -0.5917 -0.5884
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
I have reproduced its working process in the simplest possible way,hope it help others.
function out = mysoftmax(X,dim)
-------------------------Off-topic interlude, 2024-------------------------------
I am currently looking for a job in the field of CV algorithm development, based in Shenzhen, Guangdong, China,or a remote support position. I would be very grateful if anyone is willing to offer me a job or make a recommendation. My preliminary resume can be found at: https://cuixing158.github.io/about/ . Thank you! Email: cuixingxing150@gmail.com