text
stringlengths
0
15.3k
return md_writer.dumps()
def positional_deprecated(fn):
@functools.wraps(fn)
def _wrapper(*args, **kwargs):
if len(args) != 1 if inspect.ismethod(fn) else 0:
print(f'WARNING: using {fn.__name__} with positional arguments is deprecated and will be disallowed in a future version of lm-evaluation-harness!')
return fn(*args, **kwargs)
return _wrapper
def ignore_constructor(loader, node):
return node
def import_function(loader, node):
function_name = loader.construct_scalar(node)
yaml_path = os.path.dirname(loader.name)
(*module_name, function_name) = function_name.split('.')
if isinstance(module_name, list):
module_name = '.'.join(module_name)
module_path = os.path.normpath(os.path.join(yaml_path, '{}.py'.format(module_name)))
spec = importlib.util.spec_from_file_location(module_name, module_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
function = getattr(module, function_name)
return function
def load_yaml_config(yaml_path=None, yaml_config=None, yaml_dir=None, mode='full'):
if mode == 'simple':
constructor_fn = ignore_constructor
elif mode == 'full':
constructor_fn = import_function
yaml.add_constructor('!function', constructor_fn)
if yaml_config is None:
with open(yaml_path, 'rb') as file:
yaml_config = yaml.full_load(file)
if yaml_dir is None:
yaml_dir = os.path.dirname(yaml_path)
assert yaml_dir is not None
if 'include' in yaml_config:
include_path = yaml_config['include']
del yaml_config['include']
if isinstance(include_path, str):
include_path = [include_path]
include_path.reverse()
final_yaml_config = {}
for path in include_path:
if not os.path.isfile(path):
path = os.path.join(yaml_dir, path)
try:
included_yaml_config = load_yaml_config(yaml_path=path, mode=mode)
final_yaml_config.update(included_yaml_config)
except Exception as ex:
raise ex
final_yaml_config.update(yaml_config)
return final_yaml_config
return yaml_config
def regex_replace(string, pattern, repl, count: int=0):
return re.sub(pattern, repl, string, count=count)
env = Environment(loader=BaseLoader, undefined=StrictUndefined)
env.filters['regex_replace'] = regex_replace
def apply_template(template: str, doc: dict) -> str:
rtemplate = env.from_string(template)
return rtemplate.render(**doc)
def create_iterator(raw_iterator, *, rank=0, world_size=1, limit=None):
return islice(raw_iterator, rank, limit, world_size)