Add advanced XLA MXU hardware flags and vectorized prefetching for TPU v5e-8
Browse files8 unmodified lines9101112131415161782 unmodified lines1001011021031041051061071081091101111127 unmodified lines1201211221231241251261271288 unmodified linesimport torch.nn as nnimport torch.nn.functional as F# Enable XLA performance environment variablesos.environ["XLA_USE_BF16"] = "1"os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"os.environ["PYTHONUNBUFFERED"] = "1"try:82 unmodified linesself.fill()def sample_batch(self, batch_size, ctx_len):xs, ys = [], []for _ in range(batch_size):_, tokens = self.rng.choice(self.pool)i = self.rng.randint(0, len(tokens) - ctx_len - 1)xs.append(np.array(tokens[i:i+ctx_len], dtype=np.int64))ys.append(np.array(tokens[i+1:i+1+ctx_len], dtype=np.int64))return np.stack(xs), np.stack(ys)def load_val_tokens(val_names, repo_id, token, local_dir):os.makedirs(local_dir, exist_ok=True)7 unmodified linesif isinstance(source, ShardPool):x_np, y_np = source.sample_batch(batch_size, ctx_len)else:i = np.random.randint(0, len(source) - ctx_len - 1, size=batch_size)x_np = np.stack([np.array(source[j:j+ctx_len], dtype=np.int64) for j in i])y_np = np.stack([np.array(source[j+1:j+1+ctx_len], dtype=np.int64) for j in i])x = torch.from_numpy(x_np).to(device)y = torch.from_numpy(y_np).to(device)return x, y8 unmodified lines91011121314151617181982 unmodified lines1021031041051061071081091101111121131141157 unmodified lines1231241251261271281291301311321331341351368 unmodified linesimport torch.nn as nnimport torch.nn.functional as F# Enable Advanced XLA Performance & MXU Hardware Flagsos.environ["XLA_USE_BF16"] = "1"os.environ["XLA_DOWNCAST_BF16"] = "1"os.environ["LIBTPU_INIT_ARGS"] = "--xla_tpu_enable_data_parallel_all_reduce_opt=true --xla_enable_async_all_gather=true"os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"os.environ["PYTHONUNBUFFERED"] = "1"try:82 unmodified linesself.fill()def sample_batch(self, batch_size, ctx_len):xs = np.empty((batch_size, ctx_len), dtype=np.int64)ys = np.empty((batch_size, ctx_len), dtype=np.int64)for b in range(batch_size):_, tokens = self.rng.choice(self.pool)i = self.rng.randint(0, len(tokens) - ctx_len - 1)xs[b] = tokens[i : i + ctx_len]ys[b] = tokens[i + 1 : i + 1 + ctx_len]return xs, ysdef load_val_tokens(val_names, repo_id, token, local_dir):os.makedirs(local_dir, exist_ok=True)7 unmodified linesif isinstance(source, ShardPool):x_np, y_np = source.sample_batch(batch_size, ctx_len)else:xs = np.empty((batch_size, ctx_len), dtype=np.int64)ys = np.empty((batch_size, ctx_len), dtype=np.int64)max_idx = len(source) - ctx_len - 1for b in range(batch_size):i = np.random.randint(0, max_idx)xs[b] = source[i : i + ctx_len]ys[b] = source[i + 1 : i + 1 + ctx_len]x_np, y_np = xs, ysx = torch.from_numpy(x_np).to(device)y = torch.from_numpy(y_np).to(device)return x, y
code/train_tpu.py+18-10