Skip to content

[cirqflow] run_start_time and run_end_time #5289

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 14 additions & 2 deletions cirq-google/cirq_google/workflow/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,12 @@ def consume_result(
shared_rt_info: The current `cg.SharedRuntimeInfo` to be saved or updated.
"""

def finalize(self):
"""Called at the end of a workflow execution to finalize data saving."""
def finalize(self, shared_rt_info: 'cg.SharedRuntimeInfo'):
"""Called at the end of a workflow execution to finalize data saving.

Args:
shared_rt_info: The final `cg.SharedRuntimeInfo` to be saved or updated.
"""


class _FilesystemSaver(_WorkflowSaver):
Expand Down Expand Up @@ -230,3 +234,11 @@ def consume_result(
self._egr_record.executable_result_paths.append(exe_result_path)

_update_updatable_files(self._egr_record, shared_rt_info, self._data_dir)

def finalize(self, shared_rt_info: 'cg.SharedRuntimeInfo'):
"""Called at the end of a workflow execution to finalize data saving.

Args:
shared_rt_info: The final `cg.SharedRuntimeInfo` to be saved or updated.
"""
_update_updatable_files(self.egr_record, shared_rt_info, self._data_dir)
14 changes: 11 additions & 3 deletions cirq-google/cirq_google/workflow/quantum_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""Runtime information dataclasses and execution of executables."""
import contextlib
import dataclasses
import datetime
import time
import uuid
from typing import Any, Dict, Optional, List, TYPE_CHECKING
Expand Down Expand Up @@ -50,14 +51,18 @@ class SharedRuntimeInfo:

run_id: str
device: Optional[cirq.Device] = None
run_start_time: Optional[datetime.datetime] = None
run_end_time: Optional[datetime.datetime] = None

@classmethod
def _json_namespace_(cls) -> str:
return 'cirq.google'

def _json_dict_(self) -> Dict[str, Any]:
d = dataclass_json_dict(self)
# TODO (gh-4699): serialize `device` as well once SerializableDevice is serializable.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOL

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they changed my original issue title to be a little less ridiculous #4699

return obj_to_dict_helper(self, attribute_names=['run_id'])
del d['device']
return d

def __repr__(self) -> str:
return _compat.dataclass_repr(self, namespace='cirq_google')
Expand Down Expand Up @@ -261,7 +266,9 @@ def execute(
sampler = rt_config.processor_record.get_sampler()
device = rt_config.processor_record.get_device()

shared_rt_info = SharedRuntimeInfo(run_id=run_id, device=device)
shared_rt_info = SharedRuntimeInfo(
run_id=run_id, device=device, run_start_time=datetime.datetime.now(tz=datetime.timezone.utc)
)
executable_results = []

saver = _FilesystemSaver(base_data_dir=base_data_dir, run_id=run_id)
Expand Down Expand Up @@ -302,7 +309,8 @@ def execute(
saver.consume_result(exe_result, shared_rt_info)
logger.consume_result(exe_result, shared_rt_info)

saver.finalize()
shared_rt_info.run_end_time = datetime.datetime.now(tz=datetime.timezone.utc)
saver.finalize(shared_rt_info=shared_rt_info)
logger.finalize()

return ExecutableGroupResult(
Expand Down
10 changes: 9 additions & 1 deletion cirq-google/cirq_google/workflow/quantum_runtime_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import datetime
import glob
import re
import time
Expand All @@ -32,7 +33,9 @@ def cg_assert_equivalent_repr(value):


def test_shared_runtime_info():
shared_rtinfo = cg.SharedRuntimeInfo(run_id='my run')
shared_rtinfo = cg.SharedRuntimeInfo(
run_id='my run', run_start_time=datetime.datetime.now(tz=datetime.timezone.utc)
)
cg_assert_equivalent_repr(shared_rtinfo)


Expand Down Expand Up @@ -149,6 +152,11 @@ def test_execute(tmpdir, run_id_in):
else:
assert isinstance(uuid.UUID(run_id), uuid.UUID)

start_dt = returned_exegroup_result.shared_runtime_info.run_start_time
end_dt = returned_exegroup_result.shared_runtime_info.run_end_time
assert end_dt > start_dt
assert end_dt <= datetime.datetime.now(tz=datetime.timezone.utc)

manual_exegroup_result = _load_result_by_hand(tmpdir, run_id)
egr_record: cg.ExecutableGroupResultFilesystemRecord = cirq.read_json_gzip(
f'{tmpdir}/{run_id}/ExecutableGroupResultFilesystemRecord.json.gz'
Expand Down