File size: 4,649 Bytes
d766458
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
66
67
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
94
95
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2590bea3-bcfc-4630-8393-2c4762cb4168",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/data/dell/anaconda3/envs/torch1.8/lib/python3.8/site-packages/tqdm/auto.py:22: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "import joblib\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4c433f59-b8bf-47c3-9bf0-f3ca8f7a0e6c",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('torch_weights.txt', 'r') as f:\n",
    "    lines = f.readlines()\n",
    "tkeys = [iline.strip() for iline in lines]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ab6977f5-2593-4dd2-9a3c-d72124ce7060",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('jax_weights.txt', 'r') as f:\n",
    "    lines = f.readlines()\n",
    "jkeys = [iline.strip() for iline in lines]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "67989df7-409e-43e9-8af9-6a21f0aef483",
   "metadata": {},
   "outputs": [],
   "source": [
    "# directory for jax and pytorch weights\n",
    "weight_dir = '/data/dell/Desktop/GitHub/ProteinMPNN_jax/weights/torch'\n",
    "dest_dir = '/data/dell/Desktop/GitHub/ProteinMPNN_jax/weights/jax'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c786705d-240b-4bb6-a767-adee16c501f8",
   "metadata": {},
   "outputs": [],
   "source": [
    "weight_list = [\"v_48_010\", \"v_48_020\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "b264f1d9-dab7-4f69-9486-9bffaf02adf6",
   "metadata": {},
   "outputs": [],
   "source": [
    "for iweight in weight_list:\n",
    "    ck = torch.load(weight_dir + '/' + iweight + '.pt', map_location='cpu')\n",
    "    ck_new = {}\n",
    "    ck_new['num_edges'] = ck['num_edges']\n",
    "    ck_new['noise_level'] = ck['noise_level']\n",
    "    ck_new['model_state_dict'] = {}\n",
    "    for ith, ijkey in enumerate(jkeys):\n",
    "        itkey = tkeys[ith]\n",
    "        if 'edge_embedding' in ijkey:\n",
    "            ck_new['model_state_dict'][ijkey] = {'w': np.array(ck['model_state_dict'][itkey]).T}\n",
    "        elif 'embed_token' in ijkey:\n",
    "            ck_new['model_state_dict'][ijkey] = {'W_s': np.array(ck['model_state_dict'][itkey])}\n",
    "        elif 'norm' in ijkey:\n",
    "            ck_new['model_state_dict'][ijkey] = {'scale': np.array(ck['model_state_dict'][itkey + '.weight']),\n",
    "                                                 'offset': np.array(ck['model_state_dict'][itkey + '.bias'])}\n",
    "        else:\n",
    "            ck_new['model_state_dict'][ijkey] = {'w': np.array(ck['model_state_dict'][itkey + '.weight']).T,\n",
    "                                                 'b': np.array(ck['model_state_dict'][itkey + '.bias']).T}\n",
    "    \n",
    "    joblib.dump(ck_new, dest_dir + '/' + iweight + '.pkl')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d72eca08-24b8-4e8c-942d-93ebc4e35ee3",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a6de4720-0c61-474d-9503-498c694c35f1",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c84397b9-2857-43be-90cf-2be3a6525543",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "86ff0de3-2f1f-4319-a565-fa9a5eb97fdc",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.13 ('torch1.8')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.13"
  },
  "vscode": {
   "interpreter": {
    "hash": "3ae0ca605930d43634b7fd9a0f47e12f386d3436786e739d9a5b581639944ad1"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}