{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"papermill":{"default_parameters":{},"duration":9735.485807,"end_time":"2023-09-01T19:32:52.176348","environment_variables":{},"exception":null,"input_path":"__notebook__.ipynb","output_path":"__notebook__.ipynb","parameters":{},"start_time":"2023-09-01T16:50:36.690541","version":"2.4.0"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Attention!!!\n\nThis is a very simple but bad quality notebook. \n - I do not use any sort of ranking loss, which would be better.\n - My strategy instead is to min-max scale the times and apply L1-loss\n - My model is also not optimized. It is a relatively simple GNN that embeds the graph and only processes 1 datapoint at a time and is only trained on 1 epoch.\n - The public score would be much better if you paired this submission with a trained model for layout. Since this only contributes to half of the score.\n - Have fun playing around with it!\n \n \n # CHANGES\n - V5 - normalized train and infer targets, use MSE loss, changed evaluation metric to perform top5 mean instead of top5 max for robustness, 5-fold CV","metadata":{"papermill":{"duration":0.009203,"end_time":"2023-09-01T16:50:47.024469","exception":false,"start_time":"2023-09-01T16:50:47.015266","status":"completed"},"tags":[]}},{"cell_type":"code","source":"!mkdir wheelhouse\n!echo 'python_gdcm' > requirements.txt\n!cat requirements.txt\n!pip download -r requirements.txt -d wheelhouse","metadata":{"execution":{"iopub.status.busy":"2023-09-08T07:04:15.445323Z","iopub.execute_input":"2023-09-08T07:04:15.446063Z","iopub.status.idle":"2023-09-08T07:04:21.047839Z","shell.execute_reply.started":"2023-09-08T07:04:15.446015Z","shell.execute_reply":"2023-09-08T07:04:21.046544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!echo 'torch-scatter' > requirements.txt\n!pip download -r requirements.txt -d wheelhouse\n!echo 'torch-geometric' > requirements.txt\n!pip download -r requirements.txt -d wheelhouse\n!echo 'pylibjpeg' > requirements.txt\n!pip download -r requirements.txt -d wheelhouse","metadata":{"execution":{"iopub.status.busy":"2023-09-08T07:04:21.050636Z","iopub.execute_input":"2023-09-08T07:04:21.051066Z","iopub.status.idle":"2023-09-08T07:04:53.678411Z","shell.execute_reply.started":"2023-09-08T07:04:21.051026Z","shell.execute_reply":"2023-09-08T07:04:53.677175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install torch-geometric\n!cp /root/.cache/pip/wheels/ac/dc/30/e2874821ff308ee67dcd7a66dbde912411e19e35a1addda028/torch_geometric-2.3.1-py3-none-any.whl /kaggle/working","metadata":{"execution":{"iopub.status.busy":"2023-09-08T07:04:53.680292Z","iopub.execute_input":"2023-09-08T07:04:53.680857Z","iopub.status.idle":"2023-09-08T07:05:06.086776Z","shell.execute_reply.started":"2023-09-08T07:04:53.680818Z","shell.execute_reply":"2023-09-08T07:05:06.085398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install torch-scatter\n!cp /root/.cache/pip/wheels/ef/67/58/6566a3b61c6ec0f2ca0c2c324cd035ef2955601f0fb3197d5f/torch_scatter-2.1.1-cp310-cp310-linux_x86_64.whl /kaggle/working\n","metadata":{"_kg_hide-output":true,"papermill":{"duration":264.245878,"end_time":"2023-09-01T16:55:11.276628","exception":false,"start_time":"2023-09-01T16:50:47.03075","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-09-08T07:05:06.094009Z","iopub.execute_input":"2023-09-08T07:05:06.094394Z","iopub.status.idle":"2023-09-08T07:05:18.384184Z","shell.execute_reply.started":"2023-09-08T07:05:06.094365Z","shell.execute_reply":"2023-09-08T07:05:18.382808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip3 install --upgrade protobuf==3.20.1\n!pip install timm","metadata":{"execution":{"iopub.status.busy":"2023-09-08T07:05:18.386100Z","iopub.execute_input":"2023-09-08T07:05:18.386540Z","iopub.status.idle":"2023-09-08T07:05:41.868134Z","shell.execute_reply.started":"2023-09-08T07:05:18.386503Z","shell.execute_reply":"2023-09-08T07:05:41.866890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import timm","metadata":{"execution":{"iopub.status.busy":"2023-09-08T07:05:41.870937Z","iopub.execute_input":"2023-09-08T07:05:41.871353Z","iopub.status.idle":"2023-09-08T07:05:45.106350Z","shell.execute_reply.started":"2023-09-08T07:05:41.871315Z","shell.execute_reply":"2023-09-08T07:05:45.105343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from timm.scheduler import  CosineLRScheduler","metadata":{"execution":{"iopub.status.busy":"2023-09-08T07:05:45.109520Z","iopub.execute_input":"2023-09-08T07:05:45.110430Z","iopub.status.idle":"2023-09-08T07:05:45.123794Z","shell.execute_reply.started":"2023-09-08T07:05:45.110391Z","shell.execute_reply":"2023-09-08T07:05:45.122843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nfrom tqdm import tqdm \n\nimport sklearn,sklearn.model_selection\nimport torch\nfrom torch import nn\nfrom torch import Tensor\nfrom torch_geometric.nn import GCNConv\nfrom torch_geometric.datasets import Planetoid\nfrom torch.utils.data import DataLoader, Dataset\n#from timm.scheduler import CosineLRScheduler\nimport matplotlib.pyplot as plt\ndevice = 'cuda' if torch.cuda.is_available() else 'cpu'","metadata":{"papermill":{"duration":4.819384,"end_time":"2023-09-01T16:55:16.104784","exception":false,"start_time":"2023-09-01T16:55:11.2854","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-09-08T07:05:48.327546Z","iopub.execute_input":"2023-09-08T07:05:48.328452Z","iopub.status.idle":"2023-09-08T07:05:49.651818Z","shell.execute_reply.started":"2023-09-08T07:05:48.328418Z","shell.execute_reply":"2023-09-08T07:05:49.650774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_df(directory):\n    splits = [\"test\"]\n    dfs = dict()\n    \n    for split in splits:\n        path = os.path.join(directory, split)\n        files = os.listdir(path)\n        list_df = []\n        \n        for file in files:\n            d = dict(np.load(os.path.join(path,file)))\n            d['file'] = file\n            list_df.append(d)\n            \n        dfs[split] = pd.DataFrame.from_dict(list_df)\n        \n    return dfs\n\nlayout_xla_random_test = load_df(\"/kaggle/input/predict-ai-model-runtime/npz_all/npz/layout/xla/random/\")\nlayout_xla_default_test = load_df(\"/kaggle/input/predict-ai-model-runtime/npz_all/npz/layout/xla/default/\")\nlayout_nlp_random_test = load_df(\"/kaggle/input/predict-ai-model-runtime/npz_all/npz/layout/nlp/random/\")\nlayout_nlp_default_test = load_df(\"/kaggle/input/predict-ai-model-runtime/npz_all/npz/layout/nlp/default/\")","metadata":{"papermill":{"duration":0.020227,"end_time":"2023-09-01T16:55:16.152594","exception":false,"start_time":"2023-09-01T16:55:16.132367","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-09-08T07:05:49.653527Z","iopub.execute_input":"2023-09-08T07:05:49.653988Z","iopub.status.idle":"2023-09-08T07:05:53.355377Z","shell.execute_reply.started":"2023-09-08T07:05:49.653956Z","shell.execute_reply":"2023-09-08T07:05:53.354316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_df_train_name(directory):\n    splits = [\"train\", \"valid\"]\n    dfs = dict()\n    \n    for split in splits:\n        path = os.path.join(directory, split)\n        files = os.listdir(path)\n        list_df = []\n        \n        for file in files:                         \n            list_df.append(os.path.join(path,file))\n        dfs[split] = pd.DataFrame.from_dict(list_df)\n        \n    return dfs\n\nlayout_xla_random = load_df_train_name(\"/kaggle/input/predict-ai-model-runtime/npz_all/npz/layout/xla/random/\")\nlayout_xla_default = load_df_train_name(\"/kaggle/input/predict-ai-model-runtime/npz_all/npz/layout/xla/default/\")\n\nlayout_nlp_random = load_df_train_name(\"/kaggle/input/predict-ai-model-runtime/npz_all/npz/layout/nlp/random/\")\nlayout_nlp_default = load_df_train_name(\"/kaggle/input/predict-ai-model-runtime/npz_all/npz/layout/nlp/default/\")\n","metadata":{"execution":{"iopub.status.busy":"2023-09-08T07:06:00.722372Z","iopub.execute_input":"2023-09-08T07:06:00.722788Z","iopub.status.idle":"2023-09-08T07:06:00.871848Z","shell.execute_reply.started":"2023-09-08T07:06:00.722755Z","shell.execute_reply":"2023-09-08T07:06:00.870819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Define Dataset and Model","metadata":{"papermill":{"duration":0.008874,"end_time":"2023-09-01T16:56:21.968592","exception":false,"start_time":"2023-09-01T16:56:21.959718","status":"completed"},"tags":[]}},{"cell_type":"code","source":"print(layout_xla_default[\"train\"].iloc[0][0])","metadata":{"execution":{"iopub.status.busy":"2023-09-08T07:06:03.925050Z","iopub.execute_input":"2023-09-08T07:06:03.925857Z","iopub.status.idle":"2023-09-08T07:06:03.932854Z","shell.execute_reply.started":"2023-09-08T07:06:03.925814Z","shell.execute_reply":"2023-09-08T07:06:03.931428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class TileDataset(Dataset):\n    def __init__(self, df):\n        self.df = df\n\n    def __len__(self):\n        return len(self.df)\n\n    def __getitem__(self, idx):\n        row = self.df.iloc[idx][0]\n        config_feat = torch.tensor(row['node_config_feat'].astype(np.float32))\n        node_feat = torch.tensor(row['node_feat'].astype(np.float32))\n        node_opcode = torch.tensor(row['node_opcode'].astype(np.int32))\n        edge_index = torch.tensor(np.swapaxes(row['edge_index'],0,1).astype(np.int32))\n        target = (row['config_runtime']).astype(np.float32)\n        # minmax scale the target, we only care about order\n        target = (target-min(target))/(max(target) -min(target))\n        target = torch.tensor(target)\n        return config_feat,node_feat,node_opcode,edge_index,target\n    \n#dataset = TileDataset(layout_xla_default[\"train\"])\n#cfg_ft,nd_ft,nd_op,ind,target = dataset[0]\n#print(cfg_ft.shape,nd_ft.shape,nd_op.shape,ind.shape,target.shape)","metadata":{"papermill":{"duration":0.020329,"end_time":"2023-09-01T16:56:21.997734","exception":false,"start_time":"2023-09-01T16:56:21.977405","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-09-08T07:06:04.770844Z","iopub.execute_input":"2023-09-08T07:06:04.771454Z","iopub.status.idle":"2023-09-08T07:06:04.781729Z","shell.execute_reply.started":"2023-09-08T07:06:04.771420Z","shell.execute_reply":"2023-09-08T07:06:04.780393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class TileDataset(Dataset):\n    def __init__(self, df):\n        self.df = df\n\n    def __len__(self):\n        return len(self.df)\n\n    def __getitem__(self, idx):\n        name = self.df.iloc[idx]\n        return name\n    \ndataset = TileDataset(layout_xla_default[\"train\"])\n#cfg_ft,nd_ft,nd_op,ind,target = dataset[0]\n#print(cfg_ft.shape,nd_ft.shape,nd_op.shape,ind.shape,target.shape)","metadata":{"execution":{"iopub.status.busy":"2023-09-08T07:06:06.897161Z","iopub.execute_input":"2023-09-08T07:06:06.897549Z","iopub.status.idle":"2023-09-08T07:06:06.904695Z","shell.execute_reply.started":"2023-09-08T07:06:06.897518Z","shell.execute_reply":"2023-09-08T07:06:06.903403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class SimpleModel(torch.nn.Module):\n    def __init__(self, hidden_channels, graph_feats, hidden_dim):\n        super().__init__()\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        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        self.dense = torch.nn.Sequential(nn.Linear(82, 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        \n        #get graph features\n        x_cfg = x_cfg.mean(dim=1)\n        #print(x_cfg.shape)\n        x = torch.concat([x_feat,self.embedding(x_op)],dim = 1)\n        #pass though conv layers\n        for conv in self.convs:\n            x = conv(x, edge_index).relu()\n        # get 1d graph embedding using average pooling\n        x_graph = torch.mean(x,0)\n        \n        \n        #put graph data into config data\n        x = torch.concat([x_cfg,x_graph.repeat((len(x_cfg),1))],axis=1) #torch.Size([10528, 225])\n        #put into dense nn\n        #print(x.shape)\n        x = torch.flatten(self.dense(x))\n        return x\n\nmodel = SimpleModel(hidden_channels = [16,32,16,48],graph_feats = 64,hidden_dim=64).to(device)","metadata":{"papermill":{"duration":0.063336,"end_time":"2023-09-01T16:56:22.069894","exception":false,"start_time":"2023-09-01T16:56:22.006558","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-09-08T07:06:10.291665Z","iopub.execute_input":"2023-09-08T07:06:10.292615Z","iopub.status.idle":"2023-09-08T07:06:13.347952Z","shell.execute_reply.started":"2023-09-08T07:06:10.292570Z","shell.execute_reply":"2023-09-08T07:06:13.346880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train One Epoch","metadata":{"papermill":{"duration":0.008439,"end_time":"2023-09-01T16:56:22.088164","exception":false,"start_time":"2023-09-01T16:56:22.079725","status":"completed"},"tags":[]}},{"cell_type":"code","source":"df = pd.concat((layout_xla_default[\"train\"],layout_xla_default[\"valid\"]),axis=0).reset_index(drop=True)\ndf = pd.concat((df,layout_xla_random[\"valid\"]),axis=0).reset_index(drop=True)\ndf = pd.concat((df,layout_xla_random[\"train\"]),axis=0).reset_index(drop=True)\n\ndf = pd.concat((df,layout_nlp_default[\"train\"]),axis=0).reset_index(drop=True)\ndf = pd.concat((df,layout_nlp_default[\"valid\"]),axis=0).reset_index(drop=True)\n\ndf = pd.concat((df,layout_nlp_random[\"train\"]),axis=0).reset_index(drop=True)\ndf = pd.concat((df,layout_nlp_random[\"valid\"]),axis=0).reset_index(drop=True)\n","metadata":{"papermill":{"duration":0.021726,"end_time":"2023-09-01T16:56:22.11899","exception":false,"start_time":"2023-09-01T16:56:22.097264","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-09-08T07:06:13.350210Z","iopub.execute_input":"2023-09-08T07:06:13.350848Z","iopub.status.idle":"2023-09-08T07:06:13.363185Z","shell.execute_reply.started":"2023-09-08T07:06:13.350812Z","shell.execute_reply":"2023-09-08T07:06:13.362172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kfold = sklearn.model_selection.KFold(n_splits=5,shuffle=True,random_state=0)\nscore_means = []\nscore_maxs = []\nfor fold,(tr_idx,va_idx) in enumerate(kfold.split(df)):\n    if(fold>=1):\n        continue\n    model = SimpleModel(hidden_channels = [16,32,16,48],graph_feats = 64,hidden_dim=64).to(device)\n    train_dataset = TileDataset(df.iloc[tr_idx])\n    val_dataset = TileDataset(df.iloc[va_idx])\n    criterion = torch.nn.MSELoss()\n    steps = len(train_dataset)*10\n    warmup_steps = int(steps*0.1)\n    optimizer = torch.optim.Adam(model.parameters(), lr=1e-3,weight_decay = 1e-5)\n    scheduler = CosineLRScheduler(optimizer,t_initial= steps,warmup_t=warmup_steps, warmup_lr_init=1e-6,lr_min=2e-8,)\n    \n    def score_tile_mean(predictions, df):\n        score = 0\n        for i in range(len(df)):\n            predbest = np.mean(df.iloc[i]['config_runtime'][predictions[i]])\n            best = np.mean(np.sort(df.iloc[i]['config_runtime'])[:5])\n            score += 2-predbest/best\n        score /= len(df)\n        return score\n    def score_tile_max(predictions, df):\n        score = 0\n        for i in range(len(df)):\n            predbest = np.min(df.iloc[i]['config_runtime'][predictions[i]])\n            best = np.min(df.iloc[i]['config_runtime'])\n    #         print(best,predbest)\n            score += 2 - predbest/best\n        score /= len(df)\n        return score\n\n    best_score = 0\n    best_score_max = 0\n    score_best = 10000000\n\n    for epoch in range(20):\n        model.train()\n        pbar = tqdm(range(len(train_dataset)),leave=False)\n        loss_sum = 0\n        n = 0\n        for i in pbar:\n            d = dict(np.load(str(train_dataset[i][0])))\n            \n            config_feat = torch.tensor(d['node_config_feat'].astype(np.float32))\n            node_feat = torch.tensor(d['node_feat'].astype(np.float32))\n            node_opcode = torch.tensor(d['node_opcode'].astype(np.int32))\n            edge_index = torch.tensor(np.swapaxes(d['edge_index'],0,1).astype(np.int32))\n            target = (d['config_runtime']).astype(np.float32)\n            # minmax scale the target, we only care about order\n            target = (target-min(target))/(max(target) -min(target))\n            target = torch.tensor(target)\n            \n            #cfg_ft,nd_ft,nd_op,ind,target = train_dataset[i]\n            cfg_ft,nd_ft,nd_op,ind,target = config_feat.to(device),node_feat.to(device),node_opcode.to(device),edge_index.to(device),target.to(device)\n\n            out = model(cfg_ft,nd_ft,nd_op,ind)\n            \n            #break\n            loss = criterion(out, target)\n            loss.backward()\n            torch.nn.utils.clip_grad_norm_(model.parameters(), 1e-2)\n            scheduler.step(i+len(train_dataset)*epoch)\n            optimizer.step()\n            loss_sum+=loss.item()\n            n+=1\n            pbar.set_description(f'running loss: {(loss_sum/n):.2f},current loss: {(loss.item()):.2f}')\n        #break\n        pbar.close()\n        model.eval()\n        tile_xla_predictions = []\n        score_now = 0\n        pbar = tqdm(range(len(val_dataset)),leave=False)\n        for i in pbar:\n            \n            d = dict(np.load(str(val_dataset[i][0])))\n            \n            config_feat = torch.tensor(d['node_config_feat'].astype(np.float32))\n            node_feat = torch.tensor(d['node_feat'].astype(np.float32))\n            node_opcode = torch.tensor(d['node_opcode'].astype(np.int32))\n            edge_index = torch.tensor(np.swapaxes(d['edge_index'],0,1).astype(np.int32))\n            target = (d['config_runtime']).astype(np.float32)\n            # minmax scale the target, we only care about order\n            target = (target-min(target))/(max(target) -min(target))\n            target = torch.tensor(target)\n            \n            #cfg_ft,nd_ft,nd_op,ind,target = val_dataset[i]\n            cfg_ft,nd_ft,nd_op,ind,target = config_feat.to(device),node_feat.to(device),node_opcode.to(device),edge_index.to(device),target.to(device)\n\n            out = model(cfg_ft,nd_ft,nd_op,ind)\n            score_now += criterion(out, target)\n            tile_xla_predictions.append(np.argsort(out.cpu().detach().numpy())[:5])\n        pbar.close()\n        #score_mean = score_tile_mean(tile_xla_predictions, val_dataset.df)\n        #score_max = score_tile_max(tile_xla_predictions, val_dataset.df)\n        print(f'fold {fold} epoch {epoch}, best = {score_best:.3f}, now = {score_now:.3f},')\n        if score_best > score_now:\n            score_best = score_now\n        #best_score_max = score_max\n            torch.save(model.state_dict(), f'layout_xla_default_best_model_{fold}.pth')\n    #score_means.append(best_score)\n    #score_maxs.append(best_score_max)\n#print(f'comp_score = {np.mean(score_maxs)}, mean_score = {np.mean(score_means)},')","metadata":{"papermill":{"duration":9138.052878,"end_time":"2023-09-01T19:28:40.180685","exception":false,"start_time":"2023-09-01T16:56:22.127807","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-09-08T07:06:51.896899Z","iopub.execute_input":"2023-09-08T07:06:51.897316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Evaluate on Validation Dataset","metadata":{"papermill":{"duration":20.903162,"end_time":"2023-09-01T19:29:21.465429","exception":false,"start_time":"2023-09-01T19:29:00.562267","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"**0.31 is not bad considering that this model only trained on 1 epoch and is not on a ranking loss!**","metadata":{"papermill":{"duration":20.540632,"end_time":"2023-09-01T19:30:02.340577","exception":false,"start_time":"2023-09-01T19:29:41.799945","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"# Predict and Submit (only tile:xla predictions)","metadata":{"papermill":{"duration":20.552961,"end_time":"2023-09-01T19:30:43.664106","exception":false,"start_time":"2023-09-01T19:30:23.111145","status":"completed"},"tags":[]}},{"cell_type":"code","source":"dataset = TileDataset(layout_xla_default_test[\"test\"])\ntile_xla_predictions = [[] for i in range(len(dataset))]\nfor fold in range(1):\n    model.load_state_dict(torch.load(f'/kaggle/working/layout_xla_default_best_model_{fold}.pth'))\n    model.eval()\n    pbar = tqdm(range(len(dataset)))\n    for i in pbar:\n        cfg_ft,nd_ft,nd_op,ind,target = dataset[i]\n        cfg_ft,nd_ft,nd_op,ind,target = cfg_ft.to(device),nd_ft.to(device),nd_op.to(device),ind.to(device),target.to(device)\n\n        out = model(cfg_ft,nd_ft,nd_op,ind)\n        tile_xla_predictions[i].append(out.cpu().detach().numpy())\ntile_xla_predictions = [np.argsort(np.mean(pred,axis=0))[:-1] for pred in tile_xla_predictions]\ntile_xla_predictions[0]","metadata":{"papermill":{"duration":42.864288,"end_time":"2023-09-01T19:31:46.765649","exception":false,"start_time":"2023-09-01T19:31:03.901361","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-09-07T09:52:54.011632Z","iopub.execute_input":"2023-09-07T09:52:54.012094Z","iopub.status.idle":"2023-09-07T09:52:55.319398Z","shell.execute_reply.started":"2023-09-07T09:52:54.012059Z","shell.execute_reply":"2023-09-07T09:52:55.317950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv('/kaggle/input/predict-ai-model-runtime/sample_submission.csv')\nfor i,filename in enumerate(layout_xla_random_test[\"test\"]['file'].values):\n    id = 'layout:xla:default:' +filename[:-4]\n    print(id)\n    sub.loc[sub.ID == id,'TopConfigs'] = ';'.join(tile_xla_predictions[i].astype(str))\nsub.to_csv('submission.csv',index=False)\nsub","metadata":{"papermill":{"duration":20.880172,"end_time":"2023-09-01T19:32:28.307392","exception":false,"start_time":"2023-09-01T19:32:07.42722","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-09-07T09:53:25.157947Z","iopub.execute_input":"2023-09-07T09:53:25.158472Z","iopub.status.idle":"2023-09-07T09:53:25.208657Z","shell.execute_reply.started":"2023-09-07T09:53:25.158427Z","shell.execute_reply":"2023-09-07T09:53:25.207247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = TileDataset(layout_xla_random_test[\"test\"])\ntile_xla_predictions = [[] for i in range(len(dataset))]\nfor fold in range(1):\n    model.load_state_dict(torch.load(f'/kaggle/working/layout_xla_default_best_model_{fold}.pth'))\n    model.eval()\n    pbar = tqdm(range(len(dataset)))\n    for i in pbar:\n        cfg_ft,nd_ft,nd_op,ind,target = dataset[i]\n        cfg_ft,nd_ft,nd_op,ind,target = cfg_ft.to(device),nd_ft.to(device),nd_op.to(device),ind.to(device),target.to(device)\n\n        out = model(cfg_ft,nd_ft,nd_op,ind)\n        tile_xla_predictions[i].append(out.cpu().detach().numpy())\ntile_xla_predictions = [np.argsort(np.mean(pred,axis=0))[:-1] for pred in tile_xla_predictions]\ntile_xla_predictions[0]\n\n#sub = pd.read_csv('/kaggle/input/predict-ai-model-runtime/sample_submission.csv')\nfor i,filename in enumerate(layout_xla_random_test[\"test\"]['file'].values):\n    id = 'layout:xla:random:' +filename[:-4]\n    print(id)\n    sub.loc[sub.ID == id,'TopConfigs'] = ';'.join(tile_xla_predictions[i].astype(str))\nsub.to_csv('submission.csv',index=False)\nsub","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = TileDataset(layout_nlp_random_test[\"test\"])\ntile_xla_predictions = [[] for i in range(len(dataset))]\nfor fold in range(1):\n    model.load_state_dict(torch.load(f'/kaggle/working/layout_xla_default_best_model_{fold}.pth'))\n    model.eval()\n    pbar = tqdm(range(len(dataset)))\n    for i in pbar:\n        cfg_ft,nd_ft,nd_op,ind,target = dataset[i]\n        cfg_ft,nd_ft,nd_op,ind,target = cfg_ft.to(device),nd_ft.to(device),nd_op.to(device),ind.to(device),target.to(device)\n\n        out = model(cfg_ft,nd_ft,nd_op,ind)\n        tile_xla_predictions[i].append(out.cpu().detach().numpy())\ntile_xla_predictions = [np.argsort(np.mean(pred,axis=0))[:-1] for pred in tile_xla_predictions]\ntile_xla_predictions[0]\n\n#sub = pd.read_csv('/kaggle/input/predict-ai-model-runtime/sample_submission.csv')\nfor i,filename in enumerate(layout_nlp_random_test[\"test\"]['file'].values):\n    id = 'layout:nlp:random:' +filename[:-4]\n    print(id)\n    sub.loc[sub.ID == id,'TopConfigs'] = ';'.join(tile_xla_predictions[i].astype(str))\nsub.to_csv('submission.csv',index=False)\nsub","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = TileDataset(layout_nlp_default_test[\"test\"])\ntile_xla_predictions = [[] for i in range(len(dataset))]\nfor fold in range(1):\n    model.load_state_dict(torch.load(f'/kaggle/working/layout_xla_default_best_model_{fold}.pth'))\n    model.eval()\n    pbar = tqdm(range(len(dataset)))\n    for i in pbar:\n        cfg_ft,nd_ft,nd_op,ind,target = dataset[i]\n        cfg_ft,nd_ft,nd_op,ind,target = cfg_ft.to(device),nd_ft.to(device),nd_op.to(device),ind.to(device),target.to(device)\n\n        out = model(cfg_ft,nd_ft,nd_op,ind)\n        tile_xla_predictions[i].append(out.cpu().detach().numpy())\ntile_xla_predictions = [np.argsort(np.mean(pred,axis=0))[:-1] for pred in tile_xla_predictions]\ntile_xla_predictions[0]\n\n#sub = pd.read_csv('/kaggle/input/predict-ai-model-runtime/sample_submission.csv')\nfor i,filename in enumerate(layout_nlp_default_test[\"test\"]['file'].values):\n    id = 'layout:nlp:default:' +filename[:-4]\n    print(id)\n    sub.loc[sub.ID == id,'TopConfigs'] = ';'.join(tile_xla_predictions[i].astype(str))\nsub.to_csv('submission.csv',index=False)\nsub","metadata":{},"execution_count":null,"outputs":[]}],"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"}}