Skip to content

Commit 33fd8d7

Browse files
committed
Check shape on transformer cache
1 parent e88c1aa commit 33fd8d7

1 file changed

Lines changed: 11 additions & 11 deletions

File tree

src/models/transformer.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Transformer : public EncoderOrDecoderBase {
2828

2929
protected:
3030
using Base::options_; using Base::inference_; using Base::batchIndex_; using Base::graph_;
31-
std::unordered_map<std::string, Expr> cache_; // caching transformation of the encoder that should not be created again
31+
std::unordered_map<std::string, std::pair<Shape, Expr>> cache_; // caching transformation of the encoder that should not be created again
3232
mutable/*lazy*/ std::vector<float> sinusoidalEmbeddingsFreq_, sinusoidalEmbeddingsOffs_; // cached contributions to sinusoidal embeddings
3333

3434
// attention weights produced by step()
@@ -279,32 +279,32 @@ class Transformer : public EncoderOrDecoderBase {
279279
// Caching transformation of the encoder that should not be created again.
280280
// @TODO: set this automatically by memoizing encoder context and
281281
// memoization propagation (short-term)
282-
if (cache // if caching
283-
&& cache_.count(prefix + "_keys") > 0 // and the keys expression has been seen
284-
&& cache_[prefix + "_keys"]->shape().elements() == keys->shape().elements()) { // and the underlying element size did not change
285-
kh = cache_[prefix + "_keys"]; // then return cached tensor
282+
if (cache // if caching
283+
&& cache_.count(prefix + "_keys") > 0 // and the keys expression has been seen
284+
&& cache_[prefix + "_keys"].first == keys->shape()) { // and the underlying element size did not change
285+
kh = cache_[prefix + "_keys"].second; // then return cached tensor
286286
}
287287
else {
288288
auto Wk = graph_->param(prefix + "_Wk", {dimModel, dimModel}, inits::glorotUniform());
289289
auto bk = graph_->param(prefix + "_bk", {1, dimModel}, inits::zeros());
290290

291291
kh = affine(keys, Wk, bk); // [-4: beam depth, -3: batch size, -2: max length, -1: vector dim]
292292
kh = SplitHeads(kh, dimHeads); // [-4: batch size, -3: num heads, -2: max length, -1: split vector dim]
293-
cache_[prefix + "_keys"] = kh;
293+
cache_[prefix + "_keys"] = std::make_pair(keys->shape(), kh);
294294
}
295295

296296
Expr vh;
297-
if (cache
298-
&& cache_.count(prefix + "_values") > 0
299-
&& cache_[prefix + "_values"]->shape().elements() == values->shape().elements()) {
300-
vh = cache_[prefix + "_values"];
297+
if (cache
298+
&& cache_.count(prefix + "_values") > 0
299+
&& cache_[prefix + "_values"].first == values->shape()) {
300+
vh = cache_[prefix + "_values"].second;
301301
} else {
302302
auto Wv = graph_->param(prefix + "_Wv", {dimModel, dimModel}, inits::glorotUniform());
303303
auto bv = graph_->param(prefix + "_bv", {1, dimModel}, inits::zeros());
304304

305305
vh = affine(values, Wv, bv); // [-4: batch size, -3: num heads, -2: max length, -1: split vector dim]
306306
vh = SplitHeads(vh, dimHeads);
307-
cache_[prefix + "_values"] = vh;
307+
cache_[prefix + "_values"] = std::make_pair(values->shape(), vh);
308308
}
309309

310310
int dimBeam = q->shape()[-4];

0 commit comments

Comments
 (0)