Skip to content

Commit

Permalink
✨Implemented SerializeScriptBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
carefree0910 committed Jan 18, 2024
1 parent 66345fd commit fb566c1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions core/learn/pipeline/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from .blocks import SerializeDataBlock
from .blocks import SerializeModelBlock
from .blocks import SerializeOptimizerBlock
from .blocks import SerializeScriptBlock
from .schema import IEvaluationPipeline
from ..schema import device_type
from ..schema import sample_weights_type
Expand Down Expand Up @@ -245,6 +246,7 @@ def building_blocks(self) -> List[Block]:
SerializeDataBlock(),
SerializeModelBlock(),
SerializeOptimizerBlock(),
SerializeScriptBlock(),
TrainingBlock(),
]

Expand Down
20 changes: 20 additions & 0 deletions core/learn/pipeline/blocks/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import math
import torch
import shutil
import inspect

from torch import nn
from typing import Any
Expand Down Expand Up @@ -872,6 +873,24 @@ def load_from(self, folder: str) -> None:
k_sch.load_state_dict(states)


@Block.register("serialize_script")
class SerializeScriptBlock(Block):
script_file = "script.py"

def build(self, config: Config) -> None:
pass

def save_extra(self, folder: str) -> None:
os.makedirs(folder, exist_ok=True)
frame = inspect.currentframe()
if frame is None:
return None
while frame.f_back is not None:
frame = frame.f_back
with open(os.path.join(folder, self.script_file), "w") as f:
f.write(inspect.getsource(frame))


__all__ = [
"SetDefaultsBlock",
"PrepareWorkplaceBlock",
Expand All @@ -890,4 +909,5 @@ def load_from(self, folder: str) -> None:
"SerializeDataBlock",
"SerializeModelBlock",
"SerializeOptimizerBlock",
"SerializeScriptBlock",
]

0 comments on commit fb566c1

Please sign in to comment.