File size: 19,895 Bytes
e3d777b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
160
161
162
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
207
208
209
210
211
212
213
214
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-12T16:11:02.453452Z",
     "start_time": "2024-12-12T16:10:59.783010Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import os\n",
    "import time\n",
    "from rdkit import Chem\n",
    "from rdkit import RDLogger;\n",
    "from torch.utils.data import Dataset\n",
    "import torch.nn.functional as F\n",
    "RDLogger.DisableLog('rdApp.*')\n",
    "import torch\n",
    "import torch.nn as nn\n",
    "import torch.optim as optim\n",
    "import pickle\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import math\n",
    "import dgl\n",
    "import networkx as nx"
   ],
   "id": "1517383df6eb646",
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-12T16:11:02.468893Z",
     "start_time": "2024-12-12T16:11:02.454576Z"
    }
   },
   "cell_type": "code",
   "source": [
    "atom_number_index_dict ={\n",
    "    1:0,\n",
    "    6:1,\n",
    "    7:2,\n",
    "    8:3,\n",
    "    9:4\n",
    "}\n",
    "atom_index_number_dict ={\n",
    "    0:1,\n",
    "    1:6,\n",
    "    2:7,\n",
    "    3:8,\n",
    "    4:9\n",
    "}\n",
    "def atom_number2index(atom_number):\n",
    "    return atom_number_index_dict[atom_number]\n",
    "def atom_index2number(atom_index):\n",
    "    return atom_index_number_dict[atom_index]"
   ],
   "id": "697783252f244e50",
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-12T16:11:03.301916Z",
     "start_time": "2024-12-12T16:11:03.243204Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from dgl.data import QM9Dataset\n",
    "from torch.utils.data import SubsetRandomSampler\n",
    "from dgl.dataloading import GraphDataLoader\n",
    "\n",
    "dataset = QM9Dataset(label_keys=['mu', 'gap'], cutoff=5.0)\n",
    "dataset_length = len(dataset)\n",
    "train_idx = torch.arange(dataset_length)\n",
    "train_sampler = SubsetRandomSampler(train_idx)\n",
    "def collate_fn(batch):\n",
    "    graphs, labels = map(list, zip(*batch))\n",
    "    for g in graphs:\n",
    "        # g.ndata[\"R\"]->the coordinates of each atom[num_nodes,3], g.ndata[\"Z\"]->the atomic number(H:1,C:6) [num_nodes]\n",
    "        g.ndata[\"Z_index\"] = torch.tensor([atom_number2index(z.item()) for z in g.ndata[\"Z\"]])\n",
    "    batched_graph = dgl.batch(graphs)\n",
    "    return batched_graph, torch.stack(labels)\n",
    "myGLoader = GraphDataLoader(dataset,collate_fn=collate_fn,batch_size=5, pin_memory=True)"
   ],
   "id": "7074f5a11a15ebc6",
   "outputs": [],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-12T15:59:44.314049Z",
     "start_time": "2024-12-12T15:59:44.299072Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# atom_numbers = []\n",
    "# for g,_ in dataset:\n",
    "#     for atom_z in g.ndata[\"Z\"]:\n",
    "#         if atom_z not in atom_numbers:\n",
    "#             atom_numbers.append(atom_z)\n",
    "# print(atom_numbers)"
   ],
   "id": "5758841a1f281514",
   "outputs": [],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-12T16:14:46.301537Z",
     "start_time": "2024-12-12T16:11:12.606641Z"
    }
   },
   "cell_type": "code",
   "source": [
    "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
    "for batch in myGLoader:\n",
    "    batch_g,label = batch\n",
    "    batch_g.to(device)\n",
    "    "
   ],
   "id": "13bcff8166b0e35b",
   "outputs": [],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-12T16:07:15.834856Z",
     "start_time": "2024-12-12T16:07:15.822783Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from functools import partial\n",
    "import sys\n",
    "sys.path.append(\"lib\")\n",
    "from lib.metrics import sce_loss\n",
    "\n",
    "class GMae(nn.Module):\n",
    "    def __init__(self, encoder,decoder,\n",
    "                 in_dim,hidden_dim,out_dim,mask_rate=0.3,replace_rate=0.1,alpha_l=2,\n",
    "                 embedding_layer_classes=4,embedding_layer_dim=4):\n",
    "        super(GMae, self).__init__()\n",
    "        self.Z_embedding = nn.Embedding(embedding_layer_classes,embedding_layer_dim)\n",
    "        self.encoder = encoder\n",
    "        self.decoder = decoder\n",
    "        self.mask_rate = mask_rate\n",
    "        self.replace_rate = replace_rate\n",
    "        self.alpha_l = alpha_l\n",
    "        self.in_dim = in_dim\n",
    "        self.hidden_dim = hidden_dim\n",
    "        self.out_dim = out_dim\n",
    "        self.embedding_layer_classes = embedding_layer_classes\n",
    "        self.embedding_layer_dim = embedding_layer_dim\n",
    "        self.enc_mask_token = nn.Parameter(torch.zeros(1,in_dim))\n",
    "        self.criterion = partial(sce_loss, alpha=alpha_l)\n",
    "        self.encoder_to_decoder = nn.Linear(hidden_dim, hidden_dim, bias=False)\n",
    "    def encode_atom_index(self,Z_index):\n",
    "        return self.Z_embedding(Z_index)\n",
    "    def encoding_mask_noise(self, g, x, mask_rate=0.3):\n",
    "        num_nodes = g.num_nodes()\n",
    "        perm = torch.randperm(num_nodes, device=x.device)\n",
    "        # random masking\n",
    "        num_mask_nodes = int(mask_rate * num_nodes)\n",
    "        mask_nodes = perm[: num_mask_nodes]\n",
    "        keep_nodes = perm[num_mask_nodes: ]\n",
    "\n",
    "        if self.replace_rate > 0:\n",
    "            num_noise_nodes = int(self.replace_rate * num_mask_nodes)\n",
    "            perm_mask = torch.randperm(num_mask_nodes, device=x.device)\n",
    "            token_nodes = mask_nodes[perm_mask[: int((1-self.replace_rate) * num_mask_nodes)]]\n",
    "            noise_nodes = mask_nodes[perm_mask[-int(self.replace_rate * num_mask_nodes):]]\n",
    "            noise_to_be_chosen = torch.randperm(num_nodes, device=x.device)[:num_noise_nodes]\n",
    "            out_x = x.clone()\n",
    "            out_x[token_nodes] = 0.0\n",
    "            out_x[noise_nodes] = x[noise_to_be_chosen]\n",
    "        else:\n",
    "            out_x = x.clone()\n",
    "            token_nodes = mask_nodes\n",
    "            out_x[mask_nodes] = 0.0\n",
    "\n",
    "        out_x[token_nodes] += self.enc_mask_token\n",
    "        use_g = g.clone()\n",
    "\n",
    "        return use_g, out_x, (mask_nodes, keep_nodes)    \n",
    "    def mask_attr_prediction(self, g, x):\n",
    "        use_g, use_x, (mask_nodes, keep_nodes) = self.encoding_mask_noise(g, x, self.mask_rate)\n",
    "        enc_rep = self.encoder(use_g, use_x)\n",
    "        # ---- attribute reconstruction ----\n",
    "        rep = self.encoder_to_decoder(enc_rep)\n",
    "        recon = self.decoder(use_g, rep)\n",
    "        x_init = x[mask_nodes]\n",
    "        x_rec = recon[mask_nodes]\n",
    "        loss = self.criterion(x_rec, x_init)\n",
    "        return loss\n",
    "\n",
    "    def embed(self, g, x):\n",
    "        rep = self.encoder(g, x)\n",
    "        return rep\n",
    "    "
   ],
   "id": "1a5caea191a642bc",
   "outputs": [],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-12T16:07:18.136174Z",
     "start_time": "2024-12-12T16:07:18.122456Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import dgl.nn as dglnn\n",
    "import torch.nn as nn\n",
    "import torch.nn.functional as F\n",
    "class SimpleGNN(nn.Module):\n",
    "    def __init__(self, in_feats, hid_feats, out_feats):\n",
    "        super().__init__()\n",
    "        self.conv1 = dglnn.SAGEConv(\n",
    "            in_feats=in_feats, out_feats=hid_feats,aggregator_type=\"mean\")\n",
    "        self.conv2 = dglnn.SAGEConv(\n",
    "            in_feats=hid_feats, out_feats=out_feats,aggregator_type=\"mean\")\n",
    "\n",
    "    def forward(self, graph, inputs):\n",
    "        # 输入是节点的特征\n",
    "        h = self.conv1(graph, inputs)\n",
    "        h = F.relu(h)\n",
    "        h = self.conv2(graph, h)\n",
    "        return h"
   ],
   "id": "c99cb509ac0f1054",
   "outputs": [],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-12T16:07:21.516476Z",
     "start_time": "2024-12-12T16:07:21.118135Z"
    }
   },
   "cell_type": "code",
   "source": [
    "sage_enc = SimpleGNN(in_feats=7,hid_feats=4,out_feats=4)\n",
    "sage_dec = SimpleGNN(in_feats=4,hid_feats=4,out_feats=7)\n",
    "gmae = GMae(sage_enc,sage_dec,7,4,7,replace_rate=0)\n",
    "epoches = 20\n",
    "optimizer = optim.Adam(gmae.parameters(), lr=1e-3)\n",
    "device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')"
   ],
   "id": "5a8a4e4dd753b642",
   "outputs": [],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-12T16:10:52.354863Z",
     "start_time": "2024-12-12T16:10:52.311090Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(f\"epoch {0} started!\")\n",
    "gmae.train()\n",
    "gmae.encoder.train()\n",
    "gmae.decoder.train()\n",
    "gmae.to(device)\n",
    "loss_epoch = 0\n",
    "for batch in myGLoader:\n",
    "    optimizer.zero_grad()\n",
    "    batch_g, _ = batch\n",
    "    R = batch_g.ndata[\"R\"].to(device)\n",
    "    Z_index = batch_g.ndata[\"Z_index\"].to(device)\n",
    "    Z_emb = gmae.encode_atom_index(Z_index)\n",
    "    feat = torch.cat([R,Z_emb],dim=1)\n",
    "    batch_g = batch_g.to(device)\n",
    "    # loss = gmae.mask_attr_prediction(batch_g, feat)\n",
    "    # loss.backward()\n",
    "    # optimizer.step()\n",
    "    # loss_epoch+=loss.item()"
   ],
   "id": "224529a988b81ef5",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epoch 0 started!\n"
     ]
    },
    {
     "ename": "RuntimeError",
     "evalue": "CUDA error: device-side assert triggered\nCUDA kernel errors might be asynchronously reported at some other API call, so the stacktrace below might be incorrect.\nFor debugging consider passing CUDA_LAUNCH_BLOCKING=1.\nCompile with `TORCH_USE_CUDA_DSA` to enable device-side assertions.\n",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mRuntimeError\u001B[0m                              Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[12], line 10\u001B[0m\n\u001B[0;32m      8\u001B[0m optimizer\u001B[38;5;241m.\u001B[39mzero_grad()\n\u001B[0;32m      9\u001B[0m batch_g, _ \u001B[38;5;241m=\u001B[39m batch\n\u001B[1;32m---> 10\u001B[0m R \u001B[38;5;241m=\u001B[39m \u001B[43mbatch_g\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mndata\u001B[49m\u001B[43m[\u001B[49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[38;5;124;43mR\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[43m]\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mto\u001B[49m\u001B[43m(\u001B[49m\u001B[43mdevice\u001B[49m\u001B[43m)\u001B[49m\n\u001B[0;32m     11\u001B[0m Z_index \u001B[38;5;241m=\u001B[39m batch_g\u001B[38;5;241m.\u001B[39mndata[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mZ_index\u001B[39m\u001B[38;5;124m\"\u001B[39m]\u001B[38;5;241m.\u001B[39mto(device)\n\u001B[0;32m     12\u001B[0m Z_emb \u001B[38;5;241m=\u001B[39m gmae\u001B[38;5;241m.\u001B[39mencode_atom_index(Z_index)\n",
      "\u001B[1;31mRuntimeError\u001B[0m: CUDA error: device-side assert triggered\nCUDA kernel errors might be asynchronously reported at some other API call, so the stacktrace below might be incorrect.\nFor debugging consider passing CUDA_LAUNCH_BLOCKING=1.\nCompile with `TORCH_USE_CUDA_DSA` to enable device-side assertions.\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-12T15:59:46.502050Z",
     "start_time": "2024-12-12T15:59:44.442506Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from datetime import datetime\n",
    "\n",
    "current_time = datetime.now().strftime(\"%m-%d@%H_%M\")\n",
    "best_loss = 10000\n",
    "for epoch in range(epoches):\n",
    "    print(f\"epoch {epoch} started!\")\n",
    "    gmae.train()\n",
    "    gmae.encoder.train()\n",
    "    gmae.decoder.train()\n",
    "    gmae.to(device)\n",
    "    loss_epoch = 0\n",
    "    for batch in myGLoader:\n",
    "        optimizer.zero_grad()\n",
    "        batch_g, _ = batch\n",
    "        R = batch_g.ndata[\"R\"].to(device)\n",
    "        Z_index = batch_g.ndata[\"Z_index\"].to(device)\n",
    "        Z_emb = gmae.encode_atom_index(Z_index)\n",
    "        feat = torch.cat([R,Z_emb],dim=1)\n",
    "        batch_g = batch_g.to(device)\n",
    "        loss = gmae.mask_attr_prediction(batch_g, feat)\n",
    "        loss.backward()\n",
    "        optimizer.step()\n",
    "        loss_epoch+=loss.item()\n",
    "    if loss_epoch < best_loss:\n",
    "        formatted_loss_epoch = f\"{loss_epoch:.3f}\"\n",
    "        save_path = f\"./experiments/consumption/gmae/{current_time}/gmae_epoch-{epoch}-{formatted_loss_epoch}.pt\"\n",
    "        save_dir = os.path.dirname(save_path)\n",
    "        if not os.path.exists(save_dir):\n",
    "            os.makedirs(save_dir,exist_ok=True)\n",
    "        torch.save(gmae.state_dict(), save_path)\n",
    "        best_loss = loss_epoch\n",
    "        print(f\"best model saved-loss:{formatted_loss_epoch}-save_path:{save_path}\")\n",
    "    print(f\"epoch {epoch}: loss {loss_epoch}\")"
   ],
   "id": "a22599c4e591125b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epoch 0 started!\n"
     ]
    },
    {
     "ename": "RuntimeError",
     "evalue": "CUDA error: device-side assert triggered\nCUDA kernel errors might be asynchronously reported at some other API call, so the stacktrace below might be incorrect.\nFor debugging consider passing CUDA_LAUNCH_BLOCKING=1.\nCompile with `TORCH_USE_CUDA_DSA` to enable device-side assertions.\n",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mRuntimeError\u001B[0m                              Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[18], line 19\u001B[0m\n\u001B[0;32m     17\u001B[0m Z_emb \u001B[38;5;241m=\u001B[39m gmae\u001B[38;5;241m.\u001B[39mencode_atom_index(Z_index)\n\u001B[0;32m     18\u001B[0m feat \u001B[38;5;241m=\u001B[39m torch\u001B[38;5;241m.\u001B[39mcat([R,Z_emb],dim\u001B[38;5;241m=\u001B[39m\u001B[38;5;241m1\u001B[39m)\n\u001B[1;32m---> 19\u001B[0m batch_g \u001B[38;5;241m=\u001B[39m \u001B[43mbatch_g\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mto\u001B[49m\u001B[43m(\u001B[49m\u001B[43mdevice\u001B[49m\u001B[43m)\u001B[49m\n\u001B[0;32m     20\u001B[0m loss \u001B[38;5;241m=\u001B[39m gmae\u001B[38;5;241m.\u001B[39mmask_attr_prediction(batch_g, feat)\n\u001B[0;32m     21\u001B[0m loss\u001B[38;5;241m.\u001B[39mbackward()\n",
      "File \u001B[1;32mE:\\Anaconda\\envs\\gnn_course\\lib\\site-packages\\dgl\\heterograph.py:5730\u001B[0m, in \u001B[0;36mDGLGraph.to\u001B[1;34m(self, device, **kwargs)\u001B[0m\n\u001B[0;32m   5728\u001B[0m \u001B[38;5;66;03m# 2. Copy misc info\u001B[39;00m\n\u001B[0;32m   5729\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_batch_num_nodes \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m:\n\u001B[1;32m-> 5730\u001B[0m     new_bnn \u001B[38;5;241m=\u001B[39m {\n\u001B[0;32m   5731\u001B[0m         k: F\u001B[38;5;241m.\u001B[39mcopy_to(num, device, \u001B[38;5;241m*\u001B[39m\u001B[38;5;241m*\u001B[39mkwargs)\n\u001B[0;32m   5732\u001B[0m         \u001B[38;5;28;01mfor\u001B[39;00m k, num \u001B[38;5;129;01min\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_batch_num_nodes\u001B[38;5;241m.\u001B[39mitems()\n\u001B[0;32m   5733\u001B[0m     }\n\u001B[0;32m   5734\u001B[0m     ret\u001B[38;5;241m.\u001B[39m_batch_num_nodes \u001B[38;5;241m=\u001B[39m new_bnn\n\u001B[0;32m   5735\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_batch_num_edges \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m:\n",
      "File \u001B[1;32mE:\\Anaconda\\envs\\gnn_course\\lib\\site-packages\\dgl\\heterograph.py:5731\u001B[0m, in \u001B[0;36m<dictcomp>\u001B[1;34m(.0)\u001B[0m\n\u001B[0;32m   5728\u001B[0m \u001B[38;5;66;03m# 2. Copy misc info\u001B[39;00m\n\u001B[0;32m   5729\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_batch_num_nodes \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m:\n\u001B[0;32m   5730\u001B[0m     new_bnn \u001B[38;5;241m=\u001B[39m {\n\u001B[1;32m-> 5731\u001B[0m         k: \u001B[43mF\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mcopy_to\u001B[49m\u001B[43m(\u001B[49m\u001B[43mnum\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mdevice\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43mkwargs\u001B[49m\u001B[43m)\u001B[49m\n\u001B[0;32m   5732\u001B[0m         \u001B[38;5;28;01mfor\u001B[39;00m k, num \u001B[38;5;129;01min\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_batch_num_nodes\u001B[38;5;241m.\u001B[39mitems()\n\u001B[0;32m   5733\u001B[0m     }\n\u001B[0;32m   5734\u001B[0m     ret\u001B[38;5;241m.\u001B[39m_batch_num_nodes \u001B[38;5;241m=\u001B[39m new_bnn\n\u001B[0;32m   5735\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_batch_num_edges \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m:\n",
      "File \u001B[1;32mE:\\Anaconda\\envs\\gnn_course\\lib\\site-packages\\dgl\\backend\\pytorch\\tensor.py:143\u001B[0m, in \u001B[0;36mcopy_to\u001B[1;34m(input, ctx, **kwargs)\u001B[0m\n\u001B[0;32m    141\u001B[0m     \u001B[38;5;28;01mif\u001B[39;00m ctx\u001B[38;5;241m.\u001B[39mindex \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m:\n\u001B[0;32m    142\u001B[0m         th\u001B[38;5;241m.\u001B[39mcuda\u001B[38;5;241m.\u001B[39mset_device(ctx\u001B[38;5;241m.\u001B[39mindex)\n\u001B[1;32m--> 143\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28;43minput\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mcuda\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43mkwargs\u001B[49m\u001B[43m)\u001B[49m\n\u001B[0;32m    144\u001B[0m \u001B[38;5;28;01melse\u001B[39;00m:\n\u001B[0;32m    145\u001B[0m     \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mRuntimeError\u001B[39;00m(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mInvalid context\u001B[39m\u001B[38;5;124m\"\u001B[39m, ctx)\n",
      "\u001B[1;31mRuntimeError\u001B[0m: CUDA error: device-side assert triggered\nCUDA kernel errors might be asynchronously reported at some other API call, so the stacktrace below might be incorrect.\nFor debugging consider passing CUDA_LAUNCH_BLOCKING=1.\nCompile with `TORCH_USE_CUDA_DSA` to enable device-side assertions.\n"
     ]
    }
   ],
   "execution_count": 18
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "gnn_course",
   "language": "python",
   "display_name": "gnn_course"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}