Skip to content
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

Check off a todo in utils: add 'freeze()' to freeze config. #121

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
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
7 changes: 6 additions & 1 deletion mingpt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import json
import random
from ast import literal_eval
from collections import namedtuple

import numpy as np
import torch
Expand Down Expand Up @@ -31,7 +32,6 @@ def setup_logging(config):
class CfgNode:
""" a lightweight configuration class inspired by yacs """
# TODO: convert to subclass from a dict like in yacs?
# TODO: implement freezing to prevent shooting of own foot
# TODO: additional existence/override checks when reading/writing params?

def __init__(self, **kwargs):
Expand All @@ -52,6 +52,11 @@ def _str_helper(self, indent):
parts = [' ' * (indent * 4) + p for p in parts]
return "".join(parts)

def freeze(self):
asdict = self.to_dict()
frozen_cfg_builder = namedtuple('CfgNode', [k for k in asdict.keys()])
return frozen_cfg_builder(**asdict)

def to_dict(self):
""" return a dict representation of the config """
return { k: v.to_dict() if isinstance(v, CfgNode) else v for k, v in self.__dict__.items() }
Expand Down