Skip to content

config compile backend - #1768

Merged
IvanKobzarev merged 1 commit into
mainfrom
IvanKobzarev/stack/1
Oct 2, 2025
Merged

IvanKobzarev merged 1 commit into
mainfrom
IvanKobzarev/stack/1

Conversation

@IvanKobzarev

@IvanKobzarev IvanKobzarev commented Sep 29, 2025 •

Copy link
Copy Markdown
Contributor

Stacked PRs:


config compile backend

IvanKobzarev added a commit that referenced this pull request Sep 29, 2025
stack-info: PR: #1768, branch: IvanKobzarev/stack/1
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Sep 29, 2025
Comment thread torchtitan/models/llama3/model/model.py Outdated

def forward(self, x):
return self.w2(F.silu(self.w1(x)) * self.w3(x))
return self.w2(F.relu(self.w1(x)) * self.w3(x))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

revert this

enable_profiling = false
save_traces_folder = "profile_trace"
profile_freq = 100
profile_freq = 1000

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

revert this

[compile]
enable=false
components = ["model", "loss"]
backend="inductor"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this is BC breaking, need to default it to None

@ezyang ezyang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

needs cleanup

IvanKobzarev added a commit that referenced this pull request Sep 29, 2025
stack-info: PR: #1768, branch: IvanKobzarev/stack/1
IvanKobzarev added a commit that referenced this pull request Sep 29, 2025
stack-info: PR: #1768, branch: IvanKobzarev/stack/1

@ezyang ezyang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@tianyu-l feel free to revert this if you object to the existence of this compile option

IvanKobzarev added a commit that referenced this pull request Sep 29, 2025
stack-info: PR: #1768, branch: IvanKobzarev/stack/1
@wwwjn

wwwjn commented Sep 29, 2025

Copy link
Copy Markdown
Contributor

Hi @IvanKobzarev n00b questions about torch.compile - If the backend is none, which backend is being used?

@tianyu-l tianyu-l left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

sorry blocking because wanted to resolve a question before merge.

Comment on lines +123 to +128
if job_config.compile.backend is None:
model = torch.compile(model, fullgraph=True)
else:
model = torch.compile(
model, backend=job_config.compile.backend, fullgraph=True
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The config sounds good to me. I have a question regarding UX.

What's your thought on why having this if else branch?

If torch.compile's default backend doesn't change (assuming inductor), I think it's fine to set it as the default for job_config.compile.backend so that we only need to have the else branch (not the if).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, originally I put "inductor" as a default value.
But this will not be BC in case when torch.compile switch default behavior.
So I put if/else to absolutely comply.

@ezyang wdyt about removing if/else with default value?

@tianyu-l tianyu-l Sep 29, 2025 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

But this will not be BC in case when torch.compile switch default behavior

why it's not BC if torch.compile switch default? I think in this case it would be torch.compile breaks BC, but torchtitan keeps BC.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok, set "inductor" as default and removed if/else.

IvanKobzarev added a commit that referenced this pull request Sep 29, 2025
stack-info: PR: #1768, branch: IvanKobzarev/stack/1
Comment thread torchtitan/config/job_config.py Outdated
default_factory=lambda: ["model", "loss"]
)
"""Which components to compile"""
backend: Optional[str] = None

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

TorchTitan doesn't use Optional.

Suggested change
backend: Optional[str] = None
backend: str | None = None

IvanKobzarev added a commit to IvanKobzarev/torchtitan that referenced this pull request Oct 1, 2025
stack-info: PR: pytorch#1768, branch: IvanKobzarev/stack/1
IvanKobzarev added a commit that referenced this pull request Oct 1, 2025
stack-info: PR: #1768, branch: IvanKobzarev/stack/1
IvanKobzarev added a commit that referenced this pull request Oct 1, 2025
stack-info: PR: #1768, branch: IvanKobzarev/stack/1

@fegin fegin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, one nit, to make the code consistent with other function e.g., apply_ac, we should just pass the required config as mentioned in the comments.



def apply_compile(model: nn.Module):
def apply_compile(model: nn.Module, job_config: JobConfig):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
def apply_compile(model: nn.Module, job_config: JobConfig):
def apply_compile(model: nn.Module, compile_config: Compile):

# turn on per-TransformerBlock compile after AC wrapping and before FSDP
if model_compile_enabled:
apply_compile(model)
apply_compile(model, job_config)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
apply_compile(model, job_config)
apply_compile(model, job_config.compile)

if job_config.compile.enable:
apply_compile(model)
apply_compile(model.encoder)
apply_compile(model, job_config)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
apply_compile(model, job_config)
apply_compile(model, job_config.compile)

apply_compile(model)
apply_compile(model.encoder)
apply_compile(model, job_config)
apply_compile(model.encoder, job_config)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
apply_compile(model.encoder, job_config)
apply_compile(model.encoder, job_config.compile)


if model_compile_enabled:
apply_compile(model)
apply_compile(model, job_config)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
apply_compile(model, job_config)
apply_compile(model, job_config.compile)

# turn on per-TransformerBlock compile after AC wrapping and before FSDP
if model_compile_enabled:
apply_compile(model)
apply_compile(model, job_config)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
apply_compile(model, job_config)
apply_compile(model, job_config.compile)



def apply_compile(model: nn.Module):
def apply_compile(model: nn.Module, job_config: JobConfig):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
def apply_compile(model: nn.Module, job_config: JobConfig):
def apply_compile(model: nn.Module, compile_config: Compile):



def apply_compile(model: nn.Module):
def apply_compile(model: nn.Module, job_config: JobConfig):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Here we don't need to send in a JobConfig, we only need a backend str.

I'm also OK with sending in config: CompileConfig where we do
from torchtitan.config import Compile as CompileConfig

The latter is more future-proof but not necessary right now. Up to you.

IvanKobzarev added a commit that referenced this pull request Oct 1, 2025
stack-info: PR: #1768, branch: IvanKobzarev/stack/1
@IvanKobzarev
IvanKobzarev requested review from fegin and tianyu-l October 1, 2025 18:22

@tianyu-l tianyu-l left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

IvanKobzarev added a commit that referenced this pull request Oct 1, 2025
stack-info: PR: #1768, branch: IvanKobzarev/stack/1
stack-info: PR: #1768, branch: IvanKobzarev/stack/1
@IvanKobzarev
IvanKobzarev merged commit 177b050 into main Oct 2, 2025
xrsrke pushed a commit to NousResearch/torchtitan that referenced this pull request Feb 13, 2026
Stacked PRs:
 * __->__#1768


--- --- ---

### config compile backend
xrsrke pushed a commit to NousResearch/torchtitan that referenced this pull request Feb 25, 2026
Stacked PRs:
 * __->__#1768


--- --- ---

### config compile backend
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants