{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"Code along video: [https://www.youtube.com/watch?v=989a-utOeBc](http://https://www.youtube.com/watch?v=989a-utOeBc)","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport os\nimport matplotlib.pyplot as plt","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_DIR = '/kaggle/input/predict-ai-model-runtime/npz_all/npz/tile/xla/valid/'\nfilenames = [os.path.join(TRAIN_DIR, filename) for filename in os.listdir(TRAIN_DIR)]\nprint('n filenames', len(filenames))\nprint(filenames[:10])","metadata":{"execution":{"iopub.status.busy":"2023-11-08T03:58:06.059233Z","iopub.execute_input":"2023-11-08T03:58:06.059609Z","iopub.status.idle":"2023-11-08T03:58:06.111556Z","shell.execute_reply.started":"2023-11-08T03:58:06.059559Z","shell.execute_reply":"2023-11-08T03:58:06.110070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file = filenames[1]\ndata = dict(np.load(file))\n\nfor k, v in data.items():\n    print(k, type(v), v.shape)","metadata":{"execution":{"iopub.status.busy":"2023-11-08T03:58:07.875924Z","iopub.execute_input":"2023-11-08T03:58:07.876357Z","iopub.status.idle":"2023-11-08T03:58:07.888948Z","shell.execute_reply.started":"2023-11-08T03:58:07.876326Z","shell.execute_reply":"2023-11-08T03:58:07.887502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"runtime = data['config_runtime'] / data['config_runtime_normalizers']","metadata":{"execution":{"iopub.status.busy":"2023-11-08T03:53:07.771083Z","iopub.execute_input":"2023-11-08T03:53:07.771427Z","iopub.status.idle":"2023-11-08T03:53:07.777170Z","shell.execute_reply.started":"2023-11-08T03:53:07.771400Z","shell.execute_reply":"2023-11-08T03:53:07.775813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch_geometric.nn import GCNConv, global_mean_pool\nimport torch\nfrom torch import nn\nfrom torch import Tensor","metadata":{"execution":{"iopub.status.busy":"2023-11-08T03:56:52.030384Z","iopub.execute_input":"2023-11-08T03:56:52.030775Z","iopub.status.idle":"2023-11-08T03:56:52.036487Z","shell.execute_reply.started":"2023-11-08T03:56:52.030747Z","shell.execute_reply":"2023-11-08T03:56:52.034826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! pip install torch_geometric","metadata":{"execution":{"iopub.status.busy":"2023-11-08T03:55:15.913266Z","iopub.execute_input":"2023-11-08T03:55:15.913629Z","iopub.status.idle":"2023-11-08T03:55:28.049375Z","shell.execute_reply.started":"2023-11-08T03:55:15.913595Z","shell.execute_reply":"2023-11-08T03:55:28.048385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class SimpleModel(torch.nn.Module):\n    #\n    #\n    # MODEL STOLEN FROM: https://www.kaggle.com/code/zulqarnainali/xla-training\n    #\n    #\n    def __init__(self, hidden_channels, graph_feats, hidden_dim):\n        super().__init__()\n\n        op_embedding_dim = 4  # I choose 4-dimensional embedding\n        self.embedding = torch.nn.Embedding(120,  # 120 different op-codes\n                                            op_embedding_dim,\n                                           )\n        assert len(hidden_channels) > 0\n        in_channels = op_embedding_dim + 140\n        self.convs = torch.nn.ModuleList()\n        last_dim = hidden_channels[0]\n\n        # Create a sequence of Graph Convolutional Network (GCN) layers\n        self.convs.append(GCNConv(in_channels, hidden_channels[0]))\n        for i in range(len(hidden_channels) - 1):\n            self.convs.append(GCNConv(hidden_channels[i], hidden_channels[i+1]))\n            last_dim = hidden_channels[i+1]\n        self.convs.append(GCNConv(last_dim, graph_feats))\n\n        # Define a sequential dense neural network\n        self.dense = torch.nn.Sequential(nn.Linear(graph_feats + 24, 64),\n                                         nn.ReLU(),\n                                         nn.Linear(64, 64),\n                                         nn.ReLU(),\n                                         nn.Linear(64, 1),\n                                        )\n\n    def forward(self, x_cfg: Tensor, x_feat: Tensor, x_op: Tensor, edge_index: Tensor) -> Tensor:\n        x = torch.cat([x_feat, self.embedding(x_op.long())], dim=1)\n        for conv in self.convs:\n            x = conv(x, edge_index).relu()\n\n        x_graph = torch.mean(x, 0)\n\n        x = torch.cat([x_cfg, x_graph.repeat((len(x_cfg), 1))], axis=1)\n        x = torch.flatten(self.dense(x))\n\n        return x\n\nmodel = SimpleModel(hidden_channels=[16, 32, 16, 48], graph_feats=64, hidden_dim=64)","metadata":{"execution":{"iopub.status.busy":"2023-11-08T04:06:53.895141Z","iopub.execute_input":"2023-11-08T04:06:53.895513Z","iopub.status.idle":"2023-11-08T04:06:53.912333Z","shell.execute_reply.started":"2023-11-08T04:06:53.895485Z","shell.execute_reply":"2023-11-08T04:06:53.910679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for file in filenames[1:2]: \n    graph_data = dict(np.load(file))\n    \n    node_feat = torch.from_numpy(graph_data['node_feat'])\n    node_opcode = torch.from_numpy(graph_data['node_opcode'])\n    edge_index = torch.from_numpy(graph_data['edge_index']).permute(1, 0)\n    \n    for trial_idx in range(len(graph_data['config_runtime'])):\n        \n        config_feat = torch.from_numpy(graph_data['config_feat'][trial_idx]).unsqueeze(0)\n        config_runtime = torch.from_numpy(np.array([\n            graph_data['config_runtime'][trial_idx] / graph_data['config_runtime_normalizers'][trial_idx]\n        ])).to(torch.float32)\n        \n        \n        output = model(config_feat, node_feat, node_opcode, edge_index)\n        print('prediction', output)\n        print('target', config_runtime)","metadata":{"execution":{"iopub.status.busy":"2023-11-08T04:04:12.863023Z","iopub.execute_input":"2023-11-08T04:04:12.863412Z","iopub.status.idle":"2023-11-08T04:04:13.638773Z","shell.execute_reply.started":"2023-11-08T04:04:12.863373Z","shell.execute_reply":"2023-11-08T04:04:13.637357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}