Skip to content

Commit

Permalink
Merge branch 'main' into move-dvcyaml
Browse files Browse the repository at this point in the history
  • Loading branch information
dberenbaum committed Aug 29, 2023
2 parents d72bbea + ea346f9 commit ea29f02
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
11 changes: 6 additions & 5 deletions src/dvclive/dvc.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# ruff: noqa: SLF001
import copy
import json
import os
from pathlib import Path
from typing import TYPE_CHECKING, Any, List, Optional
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union

from dvclive.plots import Image, Metric
from dvclive.serialize import dump_yaml
Expand Down Expand Up @@ -45,13 +46,13 @@ def _find_dvc_root(root: Optional[StrPath] = None) -> Optional[str]:
return None


def _write_file(file: str, contents=""):
def _write_file(file: str, contents: Dict[str, Union[str, int]]):
import builtins

with builtins.open(file, "w", encoding="utf-8") as fobj:
# NOTE: force flushing/writing empty file to disk, otherwise when
# run in certain contexts (pytest) file may not actually be written
fobj.write(str(contents))
fobj.write(json.dumps(contents, sort_keys=True, ensure_ascii=False))
fobj.flush()
os.fsync(fobj.fileno())

Expand Down Expand Up @@ -145,7 +146,7 @@ def _update_entries(old, new, key):
del orig["artifacts"]


def mark_dvclive_only_started() -> None:
def mark_dvclive_only_started(exp_name: str) -> None:
"""
Signal DVC VS Code extension that
an experiment is running in the workspace.
Expand All @@ -159,7 +160,7 @@ def mark_dvclive_only_started() -> None:

signal_file = _dvclive_only_signal_file(root_dir)

_write_file(signal_file, os.getpid())
_write_file(signal_file, {"pid": os.getpid(), "exp_name": exp_name})


def mark_dvclive_only_ended() -> None:
Expand Down
2 changes: 1 addition & 1 deletion src/dvclive/live.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def _init_dvc(self):
self._baseline_rev = self._dvc_repo.scm.get_rev()
if self._save_dvc_exp:
self._exp_name = get_random_exp_name(self._dvc_repo.scm, self._baseline_rev)
mark_dvclive_only_started()
mark_dvclive_only_started(self._exp_name)
self._include_untracked.append(self.dir)

def _init_studio(self):
Expand Down
10 changes: 8 additions & 2 deletions tests/test_frameworks/test_huggingface.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,17 @@ def test_huggingface_integration(tmp_dir, model, args, data, mocker):

@pytest.mark.parametrize("log_model", ["all", True, None])
@pytest.mark.parametrize("best", [True, False])
def test_huggingface_log_model(tmp_dir, model, args, data, mocker, log_model, best):
def test_huggingface_log_model(tmp_dir, model, data, mocker, log_model, best):
live_callback = DVCLiveCallback(log_model=log_model)
log_artifact = mocker.patch.object(live_callback.live, "log_artifact")

args.load_best_model_at_end = best
args = TrainingArguments(
"foo",
evaluation_strategy="epoch",
num_train_epochs=2,
save_strategy="epoch",
load_best_model_at_end=best,
)
trainer = Trainer(
model,
args,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ def test_vscode_dvclive_only_signal_file(tmp_dir, dvc_root, mocker):
if dvc_root:
assert os.path.exists(signal_file)
with open(signal_file, encoding="utf-8") as f:
assert f.read() == str(test_pid)
assert json.load(f) == {"pid": test_pid, "exp_name": dvclive._exp_name}

else:
assert not os.path.exists(signal_file)
Expand Down

0 comments on commit ea29f02

Please sign in to comment.