GenerTeam commited on
Commit
9a20dad
·
verified ·
1 Parent(s): 3dedee4

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +35 -0
README.md CHANGED
@@ -133,6 +133,41 @@ def score(seq: str) -> float:
133
  targets = ids[:, 1:]
134
  logp = F.log_softmax(logits.float(), dim=-1).gather(-1, targets.unsqueeze(-1)).squeeze(-1)
135
  return logp.mean().item()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
  ```
137
 
138
  For batched scoring with attention masking and full reproducible evaluation pipelines (sequence recovery, ClinVar / BRCA2 / TraitGym VEP, TATA / synonymous-codon perturbation, Genome-NIAH), use the official scripts in the [Carbon evaluation directory](https://github.com/huggingface/carbon/tree/main/evaluation) — see [`perturbation_tasks.py`](https://github.com/huggingface/carbon/blob/main/evaluation/perturbation_tasks.py) for the canonical `score_hf` implementation and [`README.md`](https://github.com/huggingface/carbon/blob/main/evaluation/README.md) for run instructions across all tasks.
 
133
  targets = ids[:, 1:]
134
  logp = F.log_softmax(logits.float(), dim=-1).gather(-1, targets.unsqueeze(-1)).squeeze(-1)
135
  return logp.mean().item()
136
+
137
+ # QY: not sure if we still want to keep this per-token log-probabilities score function,
138
+ # because we now have a more elegant one in modeling_carbon.py:
139
+ import torch
140
+ from transformers import AutoTokenizer, AutoModelForCausalLM
141
+
142
+ repo = "HuggingFaceBio/Carbon-3B"
143
+
144
+ # Load tokenizer and model
145
+ tok = AutoTokenizer.from_pretrained(repo, trust_remote_code=True)
146
+ model = AutoModelForCausalLM.from_pretrained(
147
+ repo,
148
+ torch_dtype=torch.bfloat16,
149
+ trust_remote_code=True
150
+ ).cuda().eval()
151
+
152
+ # Setup tokenizer for bp-level scoring (required for score_sequence)
153
+ model.setup_tokenizer(tok)
154
+
155
+ # Score sequences - automatically handles BOS token and padding
156
+ sequences = ["ATCG" * 1024, "ACAT" * 2048]
157
+ bp_probs_list, actual_probs_list = model.score_sequence(sequences)
158
+
159
+ # bp_probs_list: list of [seq_len_i, 4] tensors - probability distribution over A/T/C/G at each position
160
+ # actual_probs_list: list of [seq_len_i] tensors - probability of the actual base at each position
161
+
162
+ # Compute metrics for each sequence
163
+ for i, (seq, actual_probs) in enumerate(zip(sequences, actual_probs_list)):
164
+ log_likelihood = actual_probs.log().sum().item() # Total log-likelihood
165
+ perplexity = torch.exp(-actual_probs.log().mean()).item() # Perplexity
166
+
167
+ print(f"Sequence {i+1} (length {len(seq)}):")
168
+ print(f" Log-likelihood: {log_likelihood:.2f}")
169
+ print(f" Perplexity: {perplexity:.4f}")
170
+ print(f" Mean probability: {actual_probs.mean().item():.4f}")
171
  ```
172
 
173
  For batched scoring with attention masking and full reproducible evaluation pipelines (sequence recovery, ClinVar / BRCA2 / TraitGym VEP, TATA / synonymous-codon perturbation, Genome-NIAH), use the official scripts in the [Carbon evaluation directory](https://github.com/huggingface/carbon/tree/main/evaluation) — see [`perturbation_tasks.py`](https://github.com/huggingface/carbon/blob/main/evaluation/perturbation_tasks.py) for the canonical `score_hf` implementation and [`README.md`](https://github.com/huggingface/carbon/blob/main/evaluation/README.md) for run instructions across all tasks.