Skip to content

Commit a83a501

Browse files
authored
precomputable_biaffine: avoid concatenation (explosion#10911)
The `forward` of `precomputable_biaffine` performs matrix multiplication and then `vstack`s the result with padding. This creates a temporary array used for the output of matrix concatenation. This change avoids the temporary by pre-allocating an array that is large enough for the output of matrix multiplication plus padding and fills the array in-place. This gave me a small speedup (a bit over 100 WPS) on de_core_news_lg on M1 Max (after changing thinc-apple-ops to support in-place gemm as BLIS does).
1 parent 97e8a50 commit a83a501

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

spacy/ml/_precomputable_affine.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ def forward(model, X, is_train):
2222
nP = model.get_dim("nP")
2323
nI = model.get_dim("nI")
2424
W = model.get_param("W")
25-
Yf = model.ops.gemm(X, W.reshape((nF * nO * nP, nI)), trans2=True)
25+
# Preallocate array for layer output, including padding.
26+
Yf = model.ops.alloc2f(X.shape[0] + 1, nF * nO * nP, zeros=False)
27+
model.ops.gemm(X, W.reshape((nF * nO * nP, nI)), trans2=True, out=Yf[1:])
2628
Yf = Yf.reshape((Yf.shape[0], nF, nO, nP))
27-
Yf = model.ops.xp.vstack((model.get_param("pad"), Yf))
29+
Yf[0] = model.get_param("pad")
2830

2931
def backward(dY_ids):
3032
# This backprop is particularly tricky, because we get back a different

0 commit comments

Comments
 (0)