Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions custom/callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def __init__(self, from_logits=False, reduction='none', debug=False, **kwargs):
self.debug = debug
pass

@tf.function
def call(self, y_true, y_pred):
y_true = tf.cast(y_true, tf.int32)
mask = tf.math.logical_not(tf.math.equal(y_true, par.pad_token))
Expand Down
2 changes: 2 additions & 0 deletions custom/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ def _get_left_embedding(self, len_q, len_k):
return e

@staticmethod
@tf.function
def _qe_masking(qe):
mask = tf.sequence_mask(
tf.range(qe.shape[-1] -1, qe.shape[-1] - qe.shape[-2] -1, -1), qe.shape[-1])
Expand All @@ -248,6 +249,7 @@ def _qe_masking(qe):

return mask * qe

@tf.function
def _skewing(self, tensor: tf.Tensor):
padded = tf.pad(tensor, [[0, 0], [0,0], [0, 0], [1, 0]])
reshaped = tf.reshape(padded, shape=[-1, padded.shape[1], padded.shape[-1], padded.shape[-2]])
Expand Down
5 changes: 5 additions & 0 deletions model.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import random
import utils
from progress.bar import Bar
from tensorflow import function
tf.executing_eagerly()


Expand Down Expand Up @@ -38,6 +39,7 @@ def __init__(self, embedding_dim=256, vocab_size=388+2, num_layer=6,
if loader_path is not None:
self.load_ckpt_file(loader_path)

@function
def call(self, inputs, targets, training=None, eval=None, src_mask=None, trg_mask=None, lookup_mask=None):
encoder, weight_encoder = self.Encoder(inputs, training=training, mask=src_mask)
decoder, weights = self.Decoder(
Expand Down Expand Up @@ -81,6 +83,7 @@ def train_on_batch(self, x, y=None, sample_weight=None, class_weight=None, reset
return [loss.numpy()]+result_metric

# @tf.function
@function
def __dist_train_step(self, inp, inp_tar, out_tar, enc_mask, tar_mask, lookup_mask, training):
return self._distribution_strategy.experimental_run_v2(
self.__train_step, args=(inp, inp_tar, out_tar, enc_mask, tar_mask, lookup_mask, training))
Expand Down Expand Up @@ -283,6 +286,7 @@ def __init__(self, embedding_dim=256, vocab_size=388+2, num_layer=6,
if loader_path is not None:
self.load_ckpt_file(loader_path)

@function
def call(self, inputs, training=None, eval=None, lookup_mask=None):
decoder, w = self.Decoder(inputs, training=training, mask=lookup_mask)
fc = self.fc(decoder)
Expand Down Expand Up @@ -322,6 +326,7 @@ def train_on_batch(self, x, y=None, sample_weight=None, class_weight=None, reset
return [loss.numpy()]+result_metric

# @tf.function
@function
def __dist_train_step(self, inp_tar, out_tar, lookup_mask, training):
return self._distribution_strategy.experimental_run_v2(
self.__train_step, args=(inp_tar, out_tar, lookup_mask, training))
Expand Down