|
| 1 | +using Pkg |
| 2 | +Pkg.activate("/Users/benmurrell/JuliaM3/juliaESM"; io=devnull) |
| 3 | + |
| 4 | +using NPZ |
| 5 | +using Statistics |
| 6 | +using ESMEmbed |
| 7 | +using NNlib |
| 8 | + |
| 9 | +ref = NPZ.npzread("/Users/benmurrell/JuliaM3/juliaESM/esmfold_structure_intermediate.npz") |
| 10 | + |
| 11 | +path = "weights/esm.safetensors" |
| 12 | +reader = ESMEmbed.SafeTensors.Reader(path) |
| 13 | +( |
| 14 | + num_layers, |
| 15 | + embed_dim, |
| 16 | + attention_heads, |
| 17 | + c_s, |
| 18 | + c_z, |
| 19 | + sequence_head_width, |
| 20 | + pairwise_head_width, |
| 21 | + position_bins, |
| 22 | + num_blocks, |
| 23 | + lddt_head_hid_dim, |
| 24 | +) = ESMEmbed._infer_esmfold_full_config(reader) |
| 25 | + |
| 26 | +alphabet = ESMEmbed.Alphabet_from_architecture("ESM-1b") |
| 27 | +esm = ESMEmbed.ESM2( |
| 28 | + num_layers, |
| 29 | + embed_dim, |
| 30 | + attention_heads; |
| 31 | + alphabet = alphabet, |
| 32 | + token_dropout = true, |
| 33 | +) |
| 34 | + |
| 35 | +trunk_cfg = ESMEmbed.FoldingTrunkConfig( |
| 36 | + num_blocks, |
| 37 | + c_s, |
| 38 | + c_z, |
| 39 | + sequence_head_width, |
| 40 | + pairwise_head_width, |
| 41 | + position_bins, |
| 42 | + 0f0, |
| 43 | + 0f0, |
| 44 | + false, |
| 45 | + 4, |
| 46 | + nothing, |
| 47 | + ESMEmbed.StructureModuleConfig(), |
| 48 | +) |
| 49 | + |
| 50 | +cfg = ESMEmbed.ESMFoldConfig(; trunk=trunk_cfg, lddt_head_hid_dim=lddt_head_hid_dim, use_esm_attn_map=false) |
| 51 | +model = ESMEmbed.ESMFold(esm; cfg=cfg) |
| 52 | +ESMEmbed.load_esmfold_safetensors!(model, reader) |
| 53 | + |
| 54 | +single = Float32.(ref["single"]) |
| 55 | +pair = Float32.(ref["pair"]) |
| 56 | +aatype = Int.(ref["aatype"]) |
| 57 | +mask = Float32.(ref["mask"]) |
| 58 | + |
| 59 | +sm = model.trunk.structure_module |
| 60 | + |
| 61 | +s_ln_s = sm.layer_norm_s(single) |
| 62 | +z_ln = sm.layer_norm_z(pair) |
| 63 | +s_initial = s_ln_s |
| 64 | +s_in = sm.linear_in(s_ln_s) |
| 65 | + |
| 66 | +rigids = ESMEmbed.rigid_identity(size(s_in)[1:end-1], s_in; fmt=:quat) |
| 67 | +ipa_out = sm.ipa(s_in, z_ln, rigids, mask) |
| 68 | +s_after_ipa = s_in .+ ipa_out |
| 69 | +s_after_ipa = sm.ipa_dropout(s_after_ipa) |
| 70 | +s_after_ln = sm.layer_norm_ipa(s_after_ipa) |
| 71 | +s_after_transition = sm.transition(s_after_ln) |
| 72 | + |
| 73 | +bb_update = sm.bb_update(s_after_transition) |
| 74 | +rigids1 = ESMEmbed.compose_q_update_vec(rigids, bb_update) |
| 75 | + |
| 76 | +backb_to_global = ESMEmbed.Rigid( |
| 77 | + ESMEmbed.Rotation(rot_mats=ESMEmbed.get_rot_mats(rigids1.rots)), |
| 78 | + rigids1.trans, |
| 79 | +) |
| 80 | +backb_to_global = ESMEmbed.scale_translation(backb_to_global, sm.cfg.trans_scale_factor) |
| 81 | + |
| 82 | +unnormalized_angles, angles = sm.angle_resnet(s_after_transition, s_initial) |
| 83 | + |
| 84 | +default_frames, group_idx, atom_mask, lit_positions = ESMEmbed._init_residue_constants!(sm, angles) |
| 85 | +all_frames_to_global = ESMEmbed.torsion_angles_to_frames(backb_to_global, angles, aatype, default_frames) |
| 86 | +pred_xyz = ESMEmbed.frames_and_literature_positions_to_atom14_pos( |
| 87 | + all_frames_to_global, |
| 88 | + aatype, |
| 89 | + default_frames, |
| 90 | + group_idx, |
| 91 | + atom_mask, |
| 92 | + lit_positions, |
| 93 | +) |
| 94 | + |
| 95 | +scaled_rigids = ESMEmbed.scale_translation(rigids1, sm.cfg.trans_scale_factor) |
| 96 | + |
| 97 | +function diff_stats(a, b) |
| 98 | + max_abs = maximum(abs.(a .- b)) |
| 99 | + mean_abs = mean(abs.(a .- b)) |
| 100 | + return max_abs, mean_abs |
| 101 | +end |
| 102 | + |
| 103 | +function report(name, a, b) |
| 104 | + max_abs, mean_abs = diff_stats(Float32.(a), Float32.(b)) |
| 105 | + println(name, " max_abs=", max_abs, " mean_abs=", mean_abs) |
| 106 | +end |
| 107 | + |
| 108 | +report("s_ln_s", s_ln_s, ref["s_ln_s"]) |
| 109 | +report("z_ln", z_ln, ref["z_ln"]) |
| 110 | +report("s_initial", s_initial, ref["s_initial"]) |
| 111 | +report("s_in", s_in, ref["s_in"]) |
| 112 | +report("ipa_out", ipa_out, ref["ipa_out"]) |
| 113 | +report("s_after_ipa", s_after_ipa, ref["s_after_ipa"]) |
| 114 | +report("s_after_ln", s_after_ln, ref["s_after_ln"]) |
| 115 | +report("s_after_transition", s_after_transition, ref["s_after_transition"]) |
| 116 | +report("bb_update", bb_update, ref["bb_update"]) |
| 117 | +report("rigids_t7", ESMEmbed.to_tensor_7(rigids1), ref["rigids_t7"]) |
| 118 | +report("backb_t7", ESMEmbed.to_tensor_7(backb_to_global), ref["backb_t7"]) |
| 119 | +report("unnormalized_angles", unnormalized_angles, ref["unnormalized_angles"]) |
| 120 | +report("angles", angles, ref["angles"]) |
| 121 | +report("frames", ESMEmbed.to_tensor_7(scaled_rigids), ref["frames"]) |
| 122 | +report("sidechain_frames", ESMEmbed.to_tensor_4x4(all_frames_to_global), ref["sidechain_frames"]) |
| 123 | +report("positions", pred_xyz, ref["positions"]) |
| 124 | +report("states", s_after_transition, ref["states"]) |
| 125 | + |
| 126 | +# --- torsion/frame intermediates (match OpenFold feats.py) --- |
| 127 | +default_frames, _, _, _ = ESMEmbed._init_residue_constants!(sm, angles) |
| 128 | +idx = aatype .+ 1 |
| 129 | +df = permutedims(default_frames, (2, 3, 4, 1)) |
| 130 | +df_sel = NNlib.gather(df, idx) |
| 131 | +default_4x4 = permutedims(df_sel, (4, 5, 1, 2, 3)) |
| 132 | + |
| 133 | +bb_shape = (size(angles)[1:end-2]..., 1, 2) |
| 134 | +bb_rot = ESMEmbed.zeros_like(angles, eltype(angles), bb_shape...) |
| 135 | +ESMEmbed._view_last2(bb_rot, 1, 2) .= 1 |
| 136 | +alpha = cat(bb_rot, angles; dims=ndims(angles) - 1) |
| 137 | + |
| 138 | +all_rots = ESMEmbed.zeros_like(angles, eltype(angles), size(default_4x4)...) |
| 139 | +ESMEmbed._view_last2(all_rots, 1, 1) .= 1 |
| 140 | +ESMEmbed._view_last2(all_rots, 2, 2) .= ESMEmbed._view_last1(alpha, 2) |
| 141 | +ESMEmbed._view_last2(all_rots, 2, 3) .= -ESMEmbed._view_last1(alpha, 1) |
| 142 | +ESMEmbed._view_last2(all_rots, 3, 2) .= ESMEmbed._view_last1(alpha, 1) |
| 143 | +ESMEmbed._view_last2(all_rots, 3, 3) .= ESMEmbed._view_last1(alpha, 2) |
| 144 | + |
| 145 | +default_r = ESMEmbed.rigid_from_tensor_4x4(default_4x4) |
| 146 | +all_rots_r = ESMEmbed.rigid_from_tensor_4x4(all_rots) |
| 147 | +all_frames = ESMEmbed.compose(default_r, all_rots_r) |
| 148 | + |
| 149 | +chi2_frame_to_frame = ESMEmbed.rigid_index(all_frames, Colon(), Colon(), 6) |
| 150 | +chi3_frame_to_frame = ESMEmbed.rigid_index(all_frames, Colon(), Colon(), 7) |
| 151 | +chi4_frame_to_frame = ESMEmbed.rigid_index(all_frames, Colon(), Colon(), 8) |
| 152 | + |
| 153 | +chi1_frame_to_bb = ESMEmbed.rigid_index(all_frames, Colon(), Colon(), 5) |
| 154 | +chi2_frame_to_bb = ESMEmbed.compose(chi1_frame_to_bb, chi2_frame_to_frame) |
| 155 | +chi3_frame_to_bb = ESMEmbed.compose(chi2_frame_to_bb, chi3_frame_to_frame) |
| 156 | +chi4_frame_to_bb = ESMEmbed.compose(chi3_frame_to_bb, chi4_frame_to_frame) |
| 157 | + |
| 158 | +rot = ESMEmbed.get_rot_mats(all_frames.rots) |
| 159 | +trans = all_frames.trans |
| 160 | +rot_first = rot[:, :, 1:5, :, :] |
| 161 | +trans_first = trans[:, :, 1:5, :] |
| 162 | +rot_chi2 = reshape(ESMEmbed.get_rot_mats(chi2_frame_to_bb.rots), size(rot, 1), size(rot, 2), 1, 3, 3) |
| 163 | +rot_chi3 = reshape(ESMEmbed.get_rot_mats(chi3_frame_to_bb.rots), size(rot, 1), size(rot, 2), 1, 3, 3) |
| 164 | +rot_chi4 = reshape(ESMEmbed.get_rot_mats(chi4_frame_to_bb.rots), size(rot, 1), size(rot, 2), 1, 3, 3) |
| 165 | +trans_chi2 = reshape(chi2_frame_to_bb.trans, size(trans, 1), size(trans, 2), 1, 3) |
| 166 | +trans_chi3 = reshape(chi3_frame_to_bb.trans, size(trans, 1), size(trans, 2), 1, 3) |
| 167 | +trans_chi4 = reshape(chi4_frame_to_bb.trans, size(trans, 1), size(trans, 2), 1, 3) |
| 168 | +rot_new = cat(rot_first, rot_chi2, rot_chi3, rot_chi4; dims=3) |
| 169 | +trans_new = cat(trans_first, trans_chi2, trans_chi3, trans_chi4; dims=3) |
| 170 | +all_frames_to_bb = ESMEmbed.Rigid(ESMEmbed.Rotation(rot_mats=rot_new), trans_new) |
| 171 | +all_frames_to_global2 = ESMEmbed.compose(backb_to_global, all_frames_to_bb) |
| 172 | + |
| 173 | +report("backb_rotmats", ESMEmbed.get_rot_mats(backb_to_global.rots), ref["backb_rotmats"]) |
| 174 | +report("backb_trans", backb_to_global.trans, ref["backb_trans"]) |
| 175 | +report("default_4x4", default_4x4, ref["default_4x4"]) |
| 176 | +report("all_rots_4x4", all_rots, ref["all_rots_4x4"]) |
| 177 | +report("all_frames_4x4", ESMEmbed.to_tensor_4x4(all_frames), ref["all_frames_4x4"]) |
| 178 | +report("all_frames_to_bb_4x4", ESMEmbed.to_tensor_4x4(all_frames_to_bb), ref["all_frames_to_bb_4x4"]) |
| 179 | +report("all_frames_to_global_4x4", ESMEmbed.to_tensor_4x4(all_frames_to_global2), ref["all_frames_to_global_4x4"]) |
0 commit comments