From f4a018565fbf190fee0d39303cd597efb5ad9dc5 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Thu, 24 Apr 2025 16:59:27 +0530 Subject: [PATCH 1/4] Only `@noinline` error path in `matmul_size_check` --- src/matmul.jl | 74 ++++++++++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 33 deletions(-) diff --git a/src/matmul.jl b/src/matmul.jl index cf229364..4e248dba 100644 --- a/src/matmul.jl +++ b/src/matmul.jl @@ -412,50 +412,58 @@ lmul!(A, B) _vec_or_mat_str(s::Tuple{Any}) = "vector" _vec_or_mat_str(s::Tuple{Any,Any}) = "matrix" -@noinline function matmul_size_check(sizeA::Tuple{Integer,Vararg{Integer}}, sizeB::Tuple{Integer,Vararg{Integer}}) +function matmul_size_check(sizeA::Tuple{Integer,Vararg{Integer}}, sizeB::Tuple{Integer,Vararg{Integer}}) szA2 = get(sizeA, 2, 1) if szA2 != sizeB[1] - strA = _vec_or_mat_str(sizeA) - strB = _vec_or_mat_str(sizeB) - B_size_len = length(sizeB) == 1 ? sizeB[1] : sizeB - size_or_len_str_B = B_size_len isa Integer ? "length" : "size" - dim_or_len_str_B = B_size_len isa Integer ? "length" : "first dimension" - pos_str_A = LazyString(length(sizeA) == length(sizeB) ? "first " : "", strA) - pos_str_B = LazyString(length(sizeA) == length(sizeB) ? "second " : "", strB) - throw(DimensionMismatch( - LazyString( - lazy"incompatible dimensions for matrix multiplication: ", - lazy"tried to multiply a $strA of size $sizeA with a $strB of $size_or_len_str_B $B_size_len. ", - lazy"The second dimension of the $pos_str_A: $szA2, does not match the $dim_or_len_str_B of the $pos_str_B: $(sizeB[1])." - ) - ) - ) + matmul_size_check_error(sizeA, sizeB) end return nothing end -@noinline function matmul_size_check(sizeC::Tuple{Integer,Vararg{Integer}}, sizeA::Tuple{Integer,Vararg{Integer}}, sizeB::Tuple{Integer,Vararg{Integer}}) +@noinline function matmul_size_check_error(sizeA::Tuple{Integer,Vararg{Integer}}, sizeB::Tuple{Integer,Vararg{Integer}}) + strA = _vec_or_mat_str(sizeA) + strB = _vec_or_mat_str(sizeB) + B_size_len = length(sizeB) == 1 ? sizeB[1] : sizeB + size_or_len_str_B = B_size_len isa Integer ? "length" : "size" + dim_or_len_str_B = B_size_len isa Integer ? "length" : "first dimension" + pos_str_A = LazyString(length(sizeA) == length(sizeB) ? "first " : "", strA) + pos_str_B = LazyString(length(sizeA) == length(sizeB) ? "second " : "", strB) + throw(DimensionMismatch( + LazyString( + lazy"incompatible dimensions for matrix multiplication: ", + lazy"tried to multiply a $strA of size $sizeA with a $strB of $size_or_len_str_B $B_size_len. ", + lazy"The second dimension of the $pos_str_A: $szA2, does not match the $dim_or_len_str_B of the $pos_str_B: $(sizeB[1])." + ) + ) + ) + return nothing +end +function matmul_size_check(sizeC::Tuple{Integer,Vararg{Integer}}, sizeA::Tuple{Integer,Vararg{Integer}}, sizeB::Tuple{Integer,Vararg{Integer}}) matmul_size_check(sizeA, sizeB) szB2 = get(sizeB, 2, 1) szC2 = get(sizeC, 2, 1) if sizeC[1] != sizeA[1] || szC2 != szB2 - strA = _vec_or_mat_str(sizeA) - strB = _vec_or_mat_str(sizeB) - strC = _vec_or_mat_str(sizeC) - C_size_len = length(sizeC) == 1 ? sizeC[1] : sizeC - size_or_len_str_C = C_size_len isa Integer ? "length" : "size" - B_size_len = length(sizeB) == 1 ? sizeB[1] : sizeB - size_or_len_str_B = B_size_len isa Integer ? "length" : "size" - destsize = length(sizeB) == length(sizeC) == 1 ? sizeA[1] : (sizeA[1], szB2) - size_or_len_str_dest = destsize isa Integer ? "length" : "size" - throw(DimensionMismatch( - LazyString( - "incompatible destination size: ", - lazy"the destination $strC of $size_or_len_str_C $C_size_len is incomatible with the product of a $strA of size $sizeA and a $strB of $size_or_len_str_B $B_size_len. ", - lazy"The destination must be of $size_or_len_str_dest $destsize." - ) + matmul_size_check_error(sizeC, sizeA, sizeB) + end + return nothing +end +@noinline function matmul_size_check_error(sizeC::Tuple{Integer,Vararg{Integer}}, sizeA::Tuple{Integer,Vararg{Integer}}, sizeB::Tuple{Integer,Vararg{Integer}}) + strA = _vec_or_mat_str(sizeA) + strB = _vec_or_mat_str(sizeB) + strC = _vec_or_mat_str(sizeC) + C_size_len = length(sizeC) == 1 ? sizeC[1] : sizeC + size_or_len_str_C = C_size_len isa Integer ? "length" : "size" + B_size_len = length(sizeB) == 1 ? sizeB[1] : sizeB + size_or_len_str_B = B_size_len isa Integer ? "length" : "size" + destsize = length(sizeB) == length(sizeC) == 1 ? sizeA[1] : (sizeA[1], szB2) + size_or_len_str_dest = destsize isa Integer ? "length" : "size" + throw(DimensionMismatch( + LazyString( + "incompatible destination size: ", + lazy"the destination $strC of $size_or_len_str_C $C_size_len is incomatible with the product of a $strA of size $sizeA and a $strB of $size_or_len_str_B $B_size_len. ", + lazy"The destination must be of $size_or_len_str_dest $destsize." ) ) - end + ) return nothing end From 2051f2bba94bd516a475f8661ac8007fcb36f4b1 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Thu, 24 Apr 2025 19:27:07 +0530 Subject: [PATCH 2/4] Fix missing variable --- src/matmul.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/matmul.jl b/src/matmul.jl index 4e248dba..8ad3b625 100644 --- a/src/matmul.jl +++ b/src/matmul.jl @@ -422,6 +422,7 @@ end @noinline function matmul_size_check_error(sizeA::Tuple{Integer,Vararg{Integer}}, sizeB::Tuple{Integer,Vararg{Integer}}) strA = _vec_or_mat_str(sizeA) strB = _vec_or_mat_str(sizeB) + szA2 = get(sizeA, 2, 1) B_size_len = length(sizeB) == 1 ? sizeB[1] : sizeB size_or_len_str_B = B_size_len isa Integer ? "length" : "size" dim_or_len_str_B = B_size_len isa Integer ? "length" : "first dimension" @@ -450,6 +451,7 @@ end strA = _vec_or_mat_str(sizeA) strB = _vec_or_mat_str(sizeB) strC = _vec_or_mat_str(sizeC) + szB2 = get(sizeB, 2, 1) C_size_len = length(sizeC) == 1 ? sizeC[1] : sizeC size_or_len_str_C = C_size_len isa Integer ? "length" : "size" B_size_len = length(sizeB) == 1 ? sizeB[1] : sizeB From 651edc873b382395e9de772f9103ca32a95a3f21 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Thu, 24 Apr 2025 19:47:54 +0530 Subject: [PATCH 3/4] Remove `return nothing` after error throw --- src/matmul.jl | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/matmul.jl b/src/matmul.jl index 8ad3b625..cfb92a31 100644 --- a/src/matmul.jl +++ b/src/matmul.jl @@ -436,7 +436,6 @@ end ) ) ) - return nothing end function matmul_size_check(sizeC::Tuple{Integer,Vararg{Integer}}, sizeA::Tuple{Integer,Vararg{Integer}}, sizeB::Tuple{Integer,Vararg{Integer}}) matmul_size_check(sizeA, sizeB) @@ -466,7 +465,6 @@ end ) ) ) - return nothing end # We may inline the matmul2x2! and matmul3x3! calls for `α == true` From 02cbcb4ef5268f1bf2db1d66dc2f7a8206606036 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Sat, 26 Apr 2025 11:16:57 +0530 Subject: [PATCH 4/4] Convert unnecesary `LazyString` to `String` --- src/matmul.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/matmul.jl b/src/matmul.jl index cfb92a31..efb9d0fe 100644 --- a/src/matmul.jl +++ b/src/matmul.jl @@ -430,7 +430,7 @@ end pos_str_B = LazyString(length(sizeA) == length(sizeB) ? "second " : "", strB) throw(DimensionMismatch( LazyString( - lazy"incompatible dimensions for matrix multiplication: ", + "incompatible dimensions for matrix multiplication: ", lazy"tried to multiply a $strA of size $sizeA with a $strB of $size_or_len_str_B $B_size_len. ", lazy"The second dimension of the $pos_str_A: $szA2, does not match the $dim_or_len_str_B of the $pos_str_B: $(sizeB[1])." )