To optimize training, you can provide pre-computed teacher outputs via a dataset. If your dataset returns a dictionary containing the key 'teacher_cache', TextBrewer will treat the contents of batch['teacher_cache'] as the output from the teacher and feed it directly to the teacher's adaptor.
When 'teacher_cache' is present, the teacher's forward method is not called, saving computation time.
Note: The items in teacher_cache should match the expected output format of the teacher model (e.g., a tuple of (logits, loss)).
import torch
from torch.utils.data import Dataset, TensorDataset, DataLoader
class TSDataset(Dataset):
def __init__(self, teacher_dataset, student_dataset, teacher_cache):
# teacher_dataset and student_dataset are normal datasets
# whose each element is a tuple or a dict.
# teacher_cache is a list of items; each item is the output from the teacher.
assert len(teacher_dataset) == len(student_dataset), \
f"lengths of teacher_dataset {len(teacher_dataset)} and student_dataset {len(student_dataset)} are not the same!"
assert len(teacher_dataset) == len(teacher_cache), \
f"lengths of teacher_dataset {len(teacher_dataset)} and teacher_cache {len(teacher_cache)} are not the same!"
self.teacher_dataset = teacher_dataset
self.student_dataset = student_dataset
self.teacher_cache = teacher_cache
def __len__(self):
return len(self.teacher_dataset)
def __getitem__(self,i):
return {'teacher' : self.teacher_dataset[i], 'student' : self.student_dataset[i], 'teacher_cache':self.teacher_cache[i]}
teacher_dataset = TensorDataset(torch.randn(32,3),torch.randn(32,3))
student_dataset = TensorDataset(torch.randn(32,2),torch.randn(32,2))
# We make some fake data and assume teacher model outputs are (logits, loss)
fake_logits = [torch.randn(3) for _ in range(32)]
fake_loss = [torch.randn(1)[0] for _ in range(32)]
teacher_cache = [(fake_logits[i],fake_loss[i]) for i in range(32)]
tssdataset = TSDataset(teacher_dataset=teacher_dataset,student_dataset=student_dataset, teacher_cache=teacher_cache)
dataloader = DataLoader(dataset=tsdataset, ... )