Skip to content

schedulers: add macro support to metadata variables #921

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 1 commit into from
Jun 17, 2024
Merged
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
18 changes: 18 additions & 0 deletions torchx/specs/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import copy
import json
import re
import typing
from dataclasses import asdict, dataclass, field
from datetime import datetime
from enum import Enum
Expand Down Expand Up @@ -189,8 +190,25 @@ def apply(self, role: "Role") -> "Role":
role = copy.deepcopy(role)
role.args = [self.substitute(arg) for arg in role.args]
role.env = {key: self.substitute(arg) for key, arg in role.env.items()}
role.metadata = self._apply_nested(role.metadata)
Copy link
Contributor

Choose a reason for hiding this comment

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

could you add a test-case for this?

Copy link
Contributor

Choose a reason for hiding this comment

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

There is a test case on the meta side of this make me can also add a version of it to api test @jason-b-akers

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @kiukchung. There's an internal test in D58678835. As a follow-up I'll write one in torchx/schedulers/test/api_test.py as well.

Copy link
Contributor Author

@jason-b-akers jason-b-akers Jun 18, 2024

Choose a reason for hiding this comment

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

Follow-up test in PR#923


return role

def _apply_nested(self, d: typing.Dict[str, Any]) -> typing.Dict[str, Any]:
stack = [d]
while stack:
current_dict = stack.pop()
for k, v in current_dict.items():
if isinstance(v, dict):
stack.append(v)
elif isinstance(v, str):
current_dict[k] = self.substitute(v)
elif isinstance(v, list):
for i in range(len(v)):
if isinstance(v[i], str):
v[i] = self.substitute(v[i])
return d

def substitute(self, arg: str) -> str:
"""
substitute applies the values to the template arg.
Expand Down
Loading