Skip to content

Conversation

krammnic
Copy link
Contributor

@krammnic krammnic commented May 6, 2025

torchtune now can be an optional dependency. In most of the cases, the import was just moved to a specific function or class. In the case of the openai_api.py, the better solution was to just remove the unused import and Message type hint, because it is not a real reason to have an import of an optional library in the class.

Copy link

pytorch-bot bot commented May 6, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/torchchat/1539

Note: Links to docs will display an error until the docs builds have been completed.

❌ 4 New Failures, 2 Cancelled Jobs

As of commit 8b9d9df with merge base 0299a37 (image):

NEW FAILURES - The following jobs have failed:

CANCELLED JOBS - The following jobs were cancelled. Please retry:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Meta Open Source bot. label May 6, 2025
@krammnic
Copy link
Contributor Author

krammnic commented May 6, 2025

Oops, it seems to me that there are some problems with linting... I've ran it according to the CONTRIBUTING.md but it seems to me that there are some wrong instructions. Furthermore, I don't see CI linting job.

@Jack-Khuu
Copy link
Contributor

Thanks for the PR, just kicked off the CI

hmm we'll take a gander at the linter (not blocking this pr on lint)

@Jack-Khuu
Copy link
Contributor

Thanks for the initial pass
I think we'll need to be a tad more aggressive with pushing the imports deeper into conditionals to avoid hitting the torchtune import calls prematurely

(lint changes are also looking good)

@Jack-Khuu
Copy link
Contributor

You can ignore the failing et/executorch CI, that's a separate issue

@krammnic
Copy link
Contributor Author

krammnic commented May 6, 2025

Thanks for the initial pass I think we'll need to be a tad more aggressive with pushing the imports deeper into conditionals to avoid hitting the torchtune import calls prematurely

(lint changes are also looking good)

Thanks for the review! Sure, let's put them deeper then.

@krammnic
Copy link
Contributor Author

krammnic commented May 7, 2025

@Jack-Khuu Hey! Fixed according to your comments. Moved imports deeper in cases when it was reasonable

@Jack-Khuu
Copy link
Contributor

Thanks again, I'm planning to take a look today (and hopefully merge in)

@Jack-Khuu Jack-Khuu self-requested a review May 12, 2025 20:26
@Jack-Khuu Jack-Khuu changed the title Resolves #1519 Make torchtune an optional dependency May 12, 2025
@Jack-Khuu Jack-Khuu changed the title Make torchtune an optional dependency Move imports to prepare for making torchtune an optional dependency May 12, 2025
Copy link
Contributor

@Jack-Khuu Jack-Khuu left a comment

Choose a reason for hiding this comment

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

Things are looking solid, I'll add another commit on top of this to fix a few nits, but looks ready to merge

@@ -416,6 +414,8 @@ def _load_model_gguf(builder_args: BuilderArgs) -> Model:


def _load_checkpoint(builder_args: BuilderArgs):
from torchtune.models.convert_weights import meta_to_tune
Copy link
Contributor

Choose a reason for hiding this comment

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

hmmm let's push the install further into the check on line 419.
This function would error if we don't have torchtune installed

@@ -458,6 +458,12 @@ def _load_checkpoint(builder_args: BuilderArgs):


def _load_model_default(builder_args: BuilderArgs) -> Model:
from torchtune.models.llama3_1._position_embeddings import Llama3ScaledRoPE
Copy link
Contributor

Choose a reason for hiding this comment

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

Ditto, we can drop this into the Flamingo conditional

@@ -450,6 +446,8 @@ def prefill(
sequential_prefill=True,
**sampling_kwargs,
) -> torch.Tensor:
from torchtune.generation import sample as tune_sample
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto to moving it into the flamingo check

@@ -870,6 +870,13 @@ def _gen_model_input(
max_new_tokens: Optional[int] = None,
max_seq_len: Optional[int] = 2048,
) -> Tuple[torch.Tensor, Optional[Dict[str, Any]]]:
# torchtune model definition dependencies
from torchtune.data import Message, padded_collate_tiled_images_and_mask
Copy link
Contributor

Choose a reason for hiding this comment

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

.

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's move these 3 imports down to line 913

modules["encoder_trainable"] = False
modules["decoder_trainable"] = False
modules["fusion_trainable"] = False
except ModuleNotFoundError:
Copy link
Contributor

Choose a reason for hiding this comment

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

nice

@@ -1011,21 +1054,23 @@ def apply_rotary_emb(x: Tensor, freqs_cis: Tensor) -> Tensor:
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

try:
# For llama::sdpa_with_kv_cache.out, preprocess ops
from executorch.extension.llm.custom_ops import custom_ops # no-qa
Copy link
Contributor

Choose a reason for hiding this comment

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

Just to be safe let's undo the lint reorder here

I think ET is sensitive to order in this case

@Jack-Khuu
Copy link
Contributor

Weird looks like some of my old comments either got rearranged/not sent

I'll fix-em

@Jack-Khuu
Copy link
Contributor

Landing this as-is will patch on 3abe2c7

Thanks for the contribution @krammnic !!

@Jack-Khuu Jack-Khuu merged commit 98a5ac7 into pytorch:main May 12, 2025
66 of 72 checks passed
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.

3 participants