Skip to content

Commit e14abd4

Browse files
committed
Update tiled ops.
1 parent 01e520c commit e14abd4

File tree

3 files changed

+85
-5
lines changed

3 files changed

+85
-5
lines changed

examples/gravnet/ParallelReverseAutoDiff.GravNetExample/Common/CommonMatrixUtils.cs

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,86 @@ public static void SetInPlaceReplace(Matrix matrix, Matrix value)
858858
/// <param name="sectionsPerDimension">The number of sections per dimension.</param>
859859
/// <returns>The broken up matrix.</returns>
860860
public static Matrix[,] BreakIntoSections(Matrix matrix, int sectionsPerDimension)
861+
{
862+
int sectionRowCount = matrix.Rows / sectionsPerDimension;
863+
int sectionColCount = matrix.Cols / 2 / sectionsPerDimension; // Halve the column count for magnitude/angle split.
864+
Matrix[,] sections = new Matrix[sectionsPerDimension, sectionsPerDimension];
865+
866+
for (int i = 0; i < sectionsPerDimension; i++)
867+
{
868+
for (int j = 0; j < sectionsPerDimension; j++)
869+
{
870+
// Calculate section bounds for magnitudes and angles in a single loop.
871+
int startRow = i * sectionRowCount;
872+
int endRow = (i + 1) * sectionRowCount - 1;
873+
int startMagnitudeCol = j * sectionColCount;
874+
int endMagnitudeCol = (j + 1) * sectionColCount - 1;
875+
int startAngleCol = matrix.Cols / 2 + startMagnitudeCol; // Adjust starting column for angles.
876+
int endAngleCol = startAngleCol + sectionColCount - 1;
877+
878+
// Extract magnitude and angle sections simultaneously.
879+
var magnitudes = matrix.GetSection(startRow, startMagnitudeCol, endRow, endMagnitudeCol);
880+
var angles = matrix.GetSection(startRow, startAngleCol, endRow, endAngleCol);
881+
882+
// Concatenate magnitude and angle sections into a single matrix for this section.
883+
sections[i, j] = magnitudes.ConcatenateColumns(angles);
884+
}
885+
}
886+
887+
return sections;
888+
}
889+
890+
/// <summary>
891+
/// Combine sections.
892+
/// </summary>
893+
/// <param name="sections">The sections to combine.</param>
894+
/// <returns>The result.</returns>
895+
public static Matrix PieceTogether(Matrix[,] sections)
896+
{
897+
int sectionsPerDimension = sections.GetLength(0);
898+
// Assuming each section is of equal size, use the first section to calculate the total size.
899+
int sectionRows = sections[0, 0].Rows;
900+
int sectionCols = sections[0, 0].Cols / 2; // Half the columns in each section are for magnitudes, half for angles.
901+
902+
// Calculate the size of the combined matrix.
903+
int totalRows = sectionRows * sectionsPerDimension;
904+
int totalCols = sectionCols * 2 * sectionsPerDimension; // Double the columns for magnitudes and angles.
905+
906+
Matrix combined = new Matrix(totalRows, totalCols);
907+
908+
for (int i = 0; i < sectionsPerDimension; i++)
909+
{
910+
for (int j = 0; j < sectionsPerDimension; j++)
911+
{
912+
Matrix section = sections[i, j];
913+
// Calculate where to place the magnitude and angle parts in the combined matrix.
914+
int startRow = i * sectionRows;
915+
int startMagnitudeCol = j * sectionCols;
916+
int startAngleCol = (totalCols / 2) + (j * sectionCols);
917+
918+
for (int row = 0; row < sectionRows; row++)
919+
{
920+
for (int col = 0; col < sectionCols; col++)
921+
{
922+
// Place magnitude part in the left half of the combined matrix.
923+
combined[startRow + row, startMagnitudeCol + col] = section[row, col];
924+
// Place angle part in the right half of the combined matrix.
925+
combined[startRow + row, startAngleCol + col] = section[row, col + sectionCols];
926+
}
927+
}
928+
}
929+
}
930+
931+
return combined;
932+
}
933+
934+
/// <summary>
935+
/// Break a matrix into sections.
936+
/// </summary>
937+
/// <param name="matrix">The matrix to break.</param>
938+
/// <param name="sectionsPerDimension">The number of sections per dimension.</param>
939+
/// <returns>The broken up matrix.</returns>
940+
public static Matrix[,] BreakIntoSectionsExactly(Matrix matrix, int sectionsPerDimension)
861941
{
862942
int sectionRowCount = matrix.Rows / sectionsPerDimension;
863943
int sectionColCount = matrix.Cols / sectionsPerDimension;
@@ -885,7 +965,7 @@ public static void SetInPlaceReplace(Matrix matrix, Matrix value)
885965
/// </summary>
886966
/// <param name="sections">The sections.</param>
887967
/// <returns>The combined matrix.</returns>
888-
public static Matrix PieceTogether(Matrix[,] sections)
968+
public static Matrix PieceTogetherExactly(Matrix[,] sections)
889969
{
890970
int totalRows = sections.GetLength(0) * sections[0, 0].Rows;
891971
int totalColumns = sections.GetLength(1) * sections[0, 0].Cols;

examples/gravnet/ParallelReverseAutoDiff.GravNetExample/VectorNetwork/RMAD/ElementwiseVectorCartesianTiledSummationOperation.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public Matrix Forward(Matrix input1, Matrix input2, Matrix weights)
4747
{
4848
Matrix[,] brokenInput1 = CommonMatrixUtils.BreakIntoSections(input1, 8);
4949
Matrix[,] brokenInput2 = CommonMatrixUtils.BreakIntoSections(input2, 8);
50-
Matrix[,] brokenWeights = CommonMatrixUtils.BreakIntoSections(weights, 8);
50+
Matrix[,] brokenWeights = CommonMatrixUtils.BreakIntoSectionsExactly(weights, 8);
5151
this.calculatedValues = new CalculatedValues[brokenInput1.GetLength(0), brokenInput1.GetLength(1)][,];
5252
this.summationX = new double[brokenInput1.GetLength(0), brokenInput1.GetLength(1)][];
5353
this.summationY = new double[brokenInput1.GetLength(0), brokenInput1.GetLength(1)][];
@@ -203,7 +203,7 @@ public override BackwardResult Backward(Matrix dOutput)
203203
return new BackwardResultBuilder()
204204
.AddInputGradient(CommonMatrixUtils.PieceTogether(this.dInput1))
205205
.AddInputGradient(CommonMatrixUtils.PieceTogether(this.dInput2))
206-
.AddInputGradient(CommonMatrixUtils.PieceTogether(this.dWeights))
206+
.AddInputGradient(CommonMatrixUtils.PieceTogetherExactly(this.dWeights))
207207
.Build();
208208
}
209209

examples/gravnet/ParallelReverseAutoDiff.GravNetExample/VectorNetwork/RMAD/ElementwiseVectorConstituentTiledMultiplyOperation.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public Matrix Forward(Matrix input1, Matrix input2, Matrix weights)
4545
{
4646
var brokenInput1 = CommonMatrixUtils.BreakIntoSections(input1, 8);
4747
var brokenInput2 = CommonMatrixUtils.BreakIntoSections(input2, 8);
48-
var brokenWeights = CommonMatrixUtils.BreakIntoSections(weights, 8);
48+
var brokenWeights = CommonMatrixUtils.BreakIntoSectionsExactly(weights, 8);
4949
this.input1 = brokenInput1;
5050
this.input2 = brokenInput2;
5151
this.weights = brokenWeights;
@@ -85,7 +85,7 @@ public override BackwardResult Backward(Matrix dOutput)
8585
return new BackwardResultBuilder()
8686
.AddInputGradient(CommonMatrixUtils.PieceTogether(this.dInput1))
8787
.AddInputGradient(CommonMatrixUtils.PieceTogether(this.dInput2))
88-
.AddInputGradient(CommonMatrixUtils.PieceTogether(this.dWeights))
88+
.AddInputGradient(CommonMatrixUtils.PieceTogetherExactly(this.dWeights))
8989
.Build();
9090
}
9191

0 commit comments

Comments
 (0)