Compare
ForwardHook
A forward hook to extract a desired intermediate tensor for later comparison.
Source code in bionemo/esm2/testing/compare.py
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
|
data
property
The extracted tensor from the forward hook.
__call__(module, module_in, module_out)
The forward hook function.
Source code in bionemo/esm2/testing/compare.py
229 230 231 232 233 234 235 236 |
|
__init__(transform_fn)
A forward hook to extract a desired intermediate tensor for later comparison.
The resulting tensor is saved in the data
attribute of the hook.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
transform_fn
|
TransformFn
|
A function that maps the input and output tensors of the module to the desired tensor. |
required |
Source code in bionemo/esm2/testing/compare.py
218 219 220 221 222 223 224 225 226 227 |
|
TestHook
A test hook that just captures the raw inputs and outputs.
Source code in bionemo/esm2/testing/compare.py
246 247 248 249 250 251 252 253 254 255 256 257 |
|
__call__(module, inputs, outputs)
The forward hook function.
Source code in bionemo/esm2/testing/compare.py
254 255 256 257 |
|
__init__()
A test hook that just captures the raw inputs and outputs.
Source code in bionemo/esm2/testing/compare.py
249 250 251 252 |
|
assert_cosine_similarity(tensor1, tensor2, mask, rtol=None, atol=None, magnitude_rtol=0.01, magnitude_atol=0.01, msg=None)
Assert that both the cosine similarity between two tensors is close to 1, and the ratio of their magnitudes is 1.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor1
|
Tensor
|
The first tensor to compare. |
required |
tensor2
|
Tensor
|
The second tensor to compare. |
required |
mask
|
Tensor
|
A mask tensor to apply to the comparison. |
required |
rtol
|
float | None
|
The relative tolerance to use for the comparison. Defaults to 1e-4. |
None
|
atol
|
float | None
|
The absolute tolerance to use for the comparison. Defaults to 1e-4. |
None
|
magnitude_rtol
|
float
|
The relative tolerance to use for the magnitude comparison. Defaults to 1e-2. |
0.01
|
magnitude_atol
|
float
|
The absolute tolerance to use for the magnitude comparison. Defaults to 1e-2. |
0.01
|
msg
|
str | None
|
An optional message to include in the assertion error. |
None
|
Source code in bionemo/esm2/testing/compare.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
|
assert_esm2_equivalence(ckpt_path, model_tag, precision='fp32', rtol=None, atol=None)
Testing utility to compare the outputs of a NeMo2 checkpoint to the original HuggingFace model weights.
Compares the cosine similarity of the logit and hidden state outputs of a NeMo2 model checkpoint to the outputs of the corresponding HuggingFace model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ckpt_path
|
Path | str
|
A path to a NeMo2 checkpoint for an ESM-2 model. |
required |
model_tag
|
str
|
The HuggingFace model tag for the model to compare against. |
required |
precision
|
PrecisionTypes
|
The precision type to use for the comparison. Defaults to "fp32". |
'fp32'
|
rtol
|
float | None
|
The relative tolerance to use for the comparison. Defaults to None, which chooses the tolerance based on the precision. |
None
|
atol
|
float | None
|
The absolute tolerance to use for the comparison. Defaults to None, which chooses the tolerance based on the precision. |
None
|
Source code in bionemo/esm2/testing/compare.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
|
get_input_tensors(tokenizer)
Get input tensors for testing.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tokenizer
|
A huggingface-like tokenizer object. |
required |
Returns:
Type | Description |
---|---|
tuple[Tensor, Tensor]
|
A tuple of the input IDs and attention mask tensors. |
Source code in bionemo/esm2/testing/compare.py
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
|
load_and_evaluate_hf_model(model_tag, precision, input_ids, attention_mask)
Load a HuggingFace model and evaluate it on the given inputs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_tag
|
str
|
The HuggingFace model tag for the model to compare against. |
required |
precision
|
PrecisionTypes
|
The precision type to use for the comparison. |
required |
input_ids
|
Tensor
|
The input IDs tensor to evaluate. |
required |
attention_mask
|
Tensor
|
The attention mask tensor to evaluate. |
required |
Returns:
Type | Description |
---|---|
tuple[Tensor, Tensor]
|
A tuple of the logits and hidden states tensors calculated by the HuggingFace model, respectively. |
Source code in bionemo/esm2/testing/compare.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
|
load_and_evaluate_nemo_esm2(ckpt_path, precision, input_ids, attention_mask)
Load a NeMo2 ESM-2 model and evaluate it on the given inputs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ckpt_path
|
Path | str
|
A path to a NeMo2 checkpoint for an ESM-2 model. |
required |
precision
|
PrecisionTypes
|
The precision type to use for the comparison. |
required |
input_ids
|
Tensor
|
The input IDs tensor to evaluate. |
required |
attention_mask
|
Tensor
|
The attention mask tensor to evaluate. |
required |
Returns:
Type | Description |
---|---|
tuple[Tensor, Tensor]
|
A tuple of the logits and hidden states tensors calculated by the NeMo2 model, respectively. |
Source code in bionemo/esm2/testing/compare.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
|