-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update esmc tokenizer and return hidden states (#160)
Signed-off-by: tina-z-jia <[email protected]>
- Loading branch information
1 parent
5604523
commit 8127b99
Showing
13 changed files
with
88 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
__version__ = "3.1.0" | ||
__version__ = "3.1.1" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,48 @@ | ||
from esm.models.esmc import ESMC | ||
from examples.local_generate import get_sample_protein | ||
from esm.sdk.api import ( | ||
ESMCInferenceClient, | ||
LogitsConfig, | ||
LogitsOutput, | ||
) | ||
from esm.sdk.api import ESMCInferenceClient, ESMProtein, LogitsConfig, LogitsOutput | ||
|
||
|
||
def main(client: ESMCInferenceClient): | ||
# ================================================================ | ||
# Example usage: one single protein | ||
# ================================================================ | ||
protein = get_sample_protein() | ||
protein.coordinates = None | ||
protein.function_annotations = None | ||
protein.sasa = None | ||
protein = ESMProtein(sequence="AAAAA") | ||
|
||
# Use logits endpoint. Using bf16 for inference optimization | ||
protein_tensor = client.encode(protein) | ||
logits_output = client.logits( | ||
output = client.logits( | ||
protein_tensor, LogitsConfig(sequence=True, return_embeddings=True) | ||
) | ||
assert isinstance( | ||
logits_output, LogitsOutput | ||
), f"LogitsOutput was expected but got {logits_output}" | ||
assert ( | ||
logits_output.logits is not None and logits_output.logits.sequence is not None | ||
output, LogitsOutput | ||
), f"LogitsOutput was expected but got {output}" | ||
assert output.logits is not None and output.logits.sequence is not None | ||
assert output.embeddings is not None and output.embeddings is not None | ||
print( | ||
f"Client returned logits with shape: {output.logits.sequence.shape} and embeddings with shape: {output.embeddings.shape}" | ||
) | ||
|
||
|
||
def raw_forward(model: ESMC): | ||
protein = ESMProtein(sequence="AAAAA") | ||
sequences = [protein.sequence, protein.sequence] | ||
|
||
# ================================================================ | ||
# Example usage: directly use the model | ||
# ================================================================ | ||
input_ids = model._tokenize(sequences) | ||
output = model(input_ids) | ||
logits, embeddings, hiddens = ( | ||
output.sequence_logits, | ||
output.embeddings, | ||
output.hidden_states, | ||
) | ||
print( | ||
f"Raw model returned logits with shape: {logits.shape}, embeddings with shape: {embeddings.shape} and hidden states with shape {hiddens.shape}" | ||
) | ||
assert logits_output.embeddings is not None and logits_output.embeddings is not None | ||
|
||
|
||
if __name__ == "__main__": | ||
main(ESMC.from_pretrained("esmc_300m")) | ||
model = ESMC.from_pretrained("esmc_300m") | ||
main(model) | ||
raw_forward(model) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters