def test_mode_writing(): run_id = "trial_" + datetime.now().strftime("%Y%m%d-%H%M%S%f") worker = socket.gethostname() for s in range(0, 10): fw = FileWriter(trial_dir="/tmp/ts_outputs/" + run_id, step=s, worker=worker) if s % 2 == 0: fw.write_tensor( tdata=np.array([[1.0, 2.0], [3.0, 4.0]], dtype=np.float32), tname="arr", mode=ModeKeys.TRAIN, mode_step=s // 2, ) else: fw.write_tensor( tdata=np.array([[1.0, 2.0], [3.0, 4.0]], dtype=np.float32), tname="arr", mode=ModeKeys.EVAL, mode_step=s // 2, ) fw.close() write_dummy_collection_file("/tmp/ts_outputs/" + run_id) files = glob.glob("/tmp/ts_outputs/" + run_id + "/**/*.tfevents", recursive=True) global_steps = [] train_steps = [] eval_steps = [] for f in files: fr = FileReader(fname=f) for tu in fr.read_tensors(): tensor_name, step, tensor_data, mode, mode_step = tu if step % 2 == 0: assert mode == ModeKeys.TRAIN train_steps.append(step // 2) else: assert mode == ModeKeys.EVAL eval_steps.append(step // 2) assert mode_step == step // 2 global_steps.append(step) trial = create_trial("/tmp/ts_outputs/" + run_id) assert trial.steps() == sorted(global_steps) assert trial.steps(ModeKeys.TRAIN) == sorted(train_steps) assert trial.steps(ModeKeys.EVAL) == sorted(eval_steps) shutil.rmtree("/tmp/ts_outputs/" + run_id)
def test_index(): numpy_tensor = [ np.array([[1.0, 2.0], [3.0, 4.0]], dtype=np.float32), np.array([[1.0, 2.0, 4.0], [3.0, 4.0, 5.0]], dtype=np.float32), ] runid = "default" logdir = "." step = 0 worker = "worker_0" run_dir = os.path.join(logdir, runid) writer = FileWriter(trial_dir=run_dir, step=step, worker=worker, verbose=True) for i in (0, len(numpy_tensor) - 1): n = "tensor" + str(i) writer.write_tensor(tdata=numpy_tensor[i], tname=n) writer.flush() writer.close() efl = TensorFileLocation(step_num=step, worker_name=worker) eventfile = efl.get_file_location(trial_dir=run_dir) indexfile = IndexFileLocationUtils.get_index_key_for_step( run_dir, step, worker) fo = open(eventfile, "rb") with open(indexfile) as idx_file: index_data = json.load(idx_file) tensor_payload = index_data["tensor_payload"] i = 0 for tensor in tensor_payload: start_idx = int(tensor["start_idx"]) fo.seek(start_idx, 0) length = int(tensor["length"]) line = fo.read(length) zoo = open("test.txt", "wb") zoo.write(line) zoo.close() testfile_reader = FileReader("./test.txt") tensor_values = list(testfile_reader.read_tensors()) assert np.allclose( tensor_values[0][2].all(), numpy_tensor[i].all()), "indexwriter not working" i = i + 1 fo.close() shutil.rmtree(run_dir) os.remove("test.txt")
def test_mode_data(): run_id = "trial_" + datetime.now().strftime("%Y%m%d-%H%M%S%f") trial_dir = "/tmp/ts_outputs/" + run_id c = CollectionManager() c.add("default") c.get("default").tensor_names = ["arr_1"] c.get("default").tensor_names = ["arr_2"] c.export(trial_dir, DEFAULT_COLLECTIONS_FILE_NAME) trial = create_trial(trial_dir) worker = socket.gethostname() for s in range(0, 10): fw = FileWriter(trial_dir=trial_dir, step=s, worker=worker) if s % 2 == 0: fw.write_tensor( tdata=np.array([[1.0, 2.0], [3.0, 4.0]], dtype=np.float32), tname="arr_1", mode=modes.TRAIN, mode_step=s // 2, ) else: fw.write_tensor( tdata=np.array([[1.0, 2.0], [3.0, 4.0]], dtype=np.float32), tname="arr_2", mode=modes.EVAL, mode_step=s // 2, ) fw.close() assert trial.tensor_names() == ["arr_1", "arr_2"] assert trial.tensor_names(step=0) == ["arr_1"] assert trial.tensor_names(step=1) == ["arr_2"] assert trial.tensor_names(step=0, mode=modes.TRAIN) == ["arr_1"] assert trial.tensor_names(step=0, mode=modes.EVAL) == ["arr_2"] assert trial.tensor_names(mode=modes.TRAIN) == ["arr_1"] assert trial.tensor_names(mode=modes.EVAL) == ["arr_2"]
def test_mode_data(): run_id = "trial_" + datetime.now().strftime("%Y%m%d-%H%M%S%f") trial_dir = "/tmp/ts_outputs/" + run_id c = CollectionManager() c.add("default") c.get("default").tensor_names = ["arr"] c.export(trial_dir, DEFAULT_COLLECTIONS_FILE_NAME) tr = create_trial(trial_dir) worker = socket.gethostname() for s in range(0, 10): fw = FileWriter(trial_dir=trial_dir, step=s, worker=worker) if s % 2 == 0: fw.write_tensor( tdata=np.array([[1.0, 2.0], [3.0, 4.0]], dtype=np.float32), tname="arr", mode=modes.TRAIN, mode_step=s // 2, ) else: fw.write_tensor( tdata=np.array([[1.0, 2.0], [3.0, 4.0]], dtype=np.float32), tname="arr", mode=modes.EVAL, mode_step=s // 2, ) fw.close() if s % 2 == 0: assert tr.has_passed_step(s // 2, mode=modes.TRAIN) == StepState.AVAILABLE assert tr.has_passed_step( s // 2, mode=modes.EVAL) == StepState.NOT_YET_AVAILABLE else: assert tr.has_passed_step(s // 2, mode=modes.EVAL) == StepState.AVAILABLE assert tr.has_passed_step(s) == StepState.AVAILABLE assert tr.has_passed_step(s + 1) == StepState.NOT_YET_AVAILABLE assert tr.has_passed_step( s + 1, mode=modes.TRAIN) == StepState.NOT_YET_AVAILABLE assert len(tr.tensor_names()) == 1 assert len(tr.steps()) == 10 assert len(tr.steps(mode=modes.TRAIN)) == 5 assert len(tr.steps(mode=modes.EVAL)) == 5 assert len(tr.modes()) == 2 for i in range(10): if i % 2 == 0: assert tr.mode(i) == modes.TRAIN else: assert tr.mode(i) == modes.EVAL assert tr.mode_step(i) == i // 2 for i in range(5): assert tr.global_step(modes.TRAIN, i) == (i * 2) assert tr.global_step(modes.EVAL, i) == (i * 2) + 1 assert len(tr.tensor("arr").steps()) == 10 assert len(tr.tensor("arr").steps(mode=modes.TRAIN)) == 5 assert len(tr.tensor("arr").steps(mode=modes.EVAL)) == 5 for i in range(10): assert tr.tensor("arr").value(i) is not None if i < 5: assert tr.tensor("arr").value(i, mode=modes.TRAIN) is not None assert tr.tensor("arr").value(i, mode=modes.EVAL) is not None shutil.rmtree("/tmp/ts_outputs/" + run_id)