-
Notifications
You must be signed in to change notification settings - Fork 117
Add ESM2 PEFT recipe #1446
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
base: main
Are you sure you want to change the base?
Add ESM2 PEFT recipe #1446
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -679,3 +679,81 @@ def forward( | |
| hidden_states=outputs.hidden_states, | ||
| attentions=outputs.attentions, | ||
| ) | ||
|
|
||
|
|
||
| class NVConvNetHead(nn.Module): | ||
| """Convolution based head for token classification.""" | ||
|
|
||
| def __init__(self, config: NVEsmConfig): | ||
| """Initialize the NVConvNetHead.""" | ||
| super().__init__() | ||
| self.conv_head = torch.nn.Sequential( | ||
| torch.nn.Conv1d(config.hidden_size, config.hidden_size // 2, kernel_size=7, padding=3), | ||
| torch.nn.ReLU(), | ||
| torch.nn.Dropout(config.hidden_dropout_prob), | ||
| torch.nn.Conv1d(config.hidden_size // 2, config.num_labels, kernel_size=7, padding=3), | ||
| ) | ||
|
|
||
| def forward(self, features, **kwargs): | ||
| """Forward pass for the convolutional token classification head.""" | ||
| return self.conv_head(features).transpose(1, 2) | ||
|
|
||
|
|
||
| class NVEsmForConvTokenClassification(NVEsmPreTrainedModel): | ||
| """Adds a convolutional classification head to the model.""" | ||
|
|
||
| def __init__(self, config): | ||
| """Initialize NVEsmForTokenClassification.""" | ||
| super().__init__(config) | ||
| self.num_labels = config.num_labels | ||
|
|
||
| self.esm = NVEsmModel(config, add_pooling_layer=False) | ||
| self.classifier = NVConvNetHead(config) | ||
|
|
||
| self.init_weights() | ||
| self.post_init() | ||
|
|
||
| def forward( | ||
| self, | ||
| input_ids: Optional[torch.LongTensor] = None, | ||
| attention_mask: Optional[torch.Tensor] = None, | ||
| position_ids: Optional[torch.LongTensor] = None, | ||
| inputs_embeds: Optional[torch.FloatTensor] = None, | ||
| labels: Optional[torch.LongTensor] = None, | ||
| **kwargs: Unpack[TransformersKwargs], | ||
| ) -> TokenClassifierOutput: | ||
| """Forward pass for the token classification head. | ||
|
|
||
| labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): | ||
| Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. | ||
| """ | ||
| outputs = self.esm( | ||
| input_ids, | ||
| attention_mask=attention_mask, | ||
| position_ids=position_ids, | ||
| inputs_embeds=inputs_embeds, | ||
| **kwargs, | ||
| ) | ||
|
|
||
| if outputs[0].dim() == 3: | ||
| sequence_output = outputs[0] | ||
| else: | ||
| sequence_output = outputs[0].unsqueeze(0) | ||
|
|
||
| sequence_output = sequence_output.transpose(1, 2) | ||
|
|
||
| logits = self.classifier(sequence_output) | ||
|
|
||
| loss = None | ||
| if labels is not None: | ||
| loss_fct = CrossEntropyLoss() | ||
|
|
||
| labels = labels.to(logits.device) | ||
| loss = loss_fct(logits.reshape(-1, self.num_labels), labels.view(-1)) | ||
|
|
||
|
Comment on lines
+716
to
+753
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Packed/THD sequences will be convolved across boundaries. Possible guard to prevent incorrect packed-input usage def forward(
self,
input_ids: Optional[torch.LongTensor] = None,
attention_mask: Optional[torch.Tensor] = None,
position_ids: Optional[torch.LongTensor] = None,
inputs_embeds: Optional[torch.FloatTensor] = None,
labels: Optional[torch.LongTensor] = None,
**kwargs: Unpack[TransformersKwargs],
) -> TokenClassifierOutput:
+ if kwargs.get("cu_seq_lens_q") is not None or kwargs.get("cu_seq_lens_k") is not None:
+ raise ValueError(
+ "NVEsmForConvTokenClassification does not support sequence-packed (THD) inputs; "
+ "disable use_sequence_packing."
+ )
outputs = self.esm(
input_ids,
attention_mask=attention_mask,
position_ids=position_ids, |
||
| return TokenClassifierOutput( | ||
| loss=loss, | ||
| logits=logits, | ||
| hidden_states=outputs.hidden_states, | ||
| attentions=outputs.attentions, | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| FROM nvcr.io/nvidia/pytorch:25.12-py3 | ||
|
|
||
| RUN --mount=type=cache,target=/root/.cache/pip \ | ||
| --mount=type=bind,source=esm2_peft_te/requirements.txt,target=/requirements.txt \ | ||
| PIP_CONSTRAINT= pip install -r /requirements.txt | ||
|
|
||
| WORKDIR /workspace/bionemo-recipes/recipes/esm2_peft_te | ||
| COPY esm2_peft_te/ /workspace/bionemo-recipes/recipes/esm2_peft_te | ||
| COPY esm2_native_te/checkpoint.py /workspace/bionemo-recipes/recipes/esm2_native_te/checkpoint.py | ||
| COPY esm2_native_te/collator.py /workspace/bionemo-recipes/recipes/esm2_native_te/collator.py | ||
| COPY esm2_native_te/distributed_config.py /workspace/bionemo-recipes/recipes/esm2_native_te/distributed_config.py | ||
| COPY esm2_native_te/scheduler.py /workspace/bionemo-recipes/recipes/esm2_native_te/scheduler.py | ||
|
Comment on lines
+1
to
+12
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid cross-recipe file copies in the image. Possible Dockerfile cleanup (after vendoring shared files) COPY esm2_peft_te/ /workspace/bionemo-recipes/recipes/esm2_peft_te
-COPY esm2_native_te/checkpoint.py /workspace/bionemo-recipes/recipes/esm2_native_te/checkpoint.py
-COPY esm2_native_te/collator.py /workspace/bionemo-recipes/recipes/esm2_native_te/collator.py
-COPY esm2_native_te/distributed_config.py /workspace/bionemo-recipes/recipes/esm2_native_te/distributed_config.py
-COPY esm2_native_te/scheduler.py /workspace/bionemo-recipes/recipes/esm2_native_te/scheduler.pyAs per coding guidelines: Recipes in bionemo-recipes/recipes/ must be completely self-contained with no shared dependencies between recipes and no imports from other recipes. 🤖 Prompt for AI Agents |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: NVIDIA/bionemo-framework
Length of output: 840
🏁 Script executed:
cat -n bionemo-recipes/models/esm2/pyproject.toml | head -30Repository: NVIDIA/bionemo-framework
Length of output: 909
🏁 Script executed:
Repository: NVIDIA/bionemo-framework
Length of output: 628
🏁 Script executed:
cat -n bionemo-recipes/recipes/esm2_peft_te/requirements.txt | grep -i peft -A 2 -B 2Repository: NVIDIA/bionemo-framework
Length of output: 215
🏁 Script executed:
Repository: NVIDIA/bionemo-framework
Length of output: 50
🏁 Script executed:
cat -n bionemo-recipes/recipes/esm2_native_te/requirements.txt | grep -i peft -A 2 -B 2Repository: NVIDIA/bionemo-framework
Length of output: 50
Pin the git-based
peftdependency to an immutable commit/tag.The peft dependency uses a branch reference (
@dev/ba/support-te-lora) instead of a commit SHA or released tag. Branch references are non-immutable and can change unpredictably, creating reproducibility and supply-chain risks. Use a pinned commit SHA or released tag to ensure deterministic builds.🤖 Prompt for AI Agents