Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Cora/raw/ind.cora.x
Binary file not shown.
Binary file added examples/gcn/cora/processed/torch_data.pt
Binary file not shown.
Binary file added examples/gcn/cora/processed/torch_pre_filter.pt
Binary file not shown.
Binary file added examples/gcn/cora/processed/torch_pre_transform.pt
Binary file not shown.
Binary file added examples/gcn/cora/raw/ind.cora.allx
Binary file not shown.
Binary file added examples/gcn/cora/raw/ind.cora.ally
Binary file not shown.
Binary file added examples/gcn/cora/raw/ind.cora.graph
Binary file not shown.
1,000 changes: 1,000 additions & 0 deletions examples/gcn/cora/raw/ind.cora.test.index

Large diffs are not rendered by default.

Binary file added examples/gcn/cora/raw/ind.cora.tx
Binary file not shown.
Binary file added examples/gcn/cora/raw/ind.cora.ty
Binary file not shown.
Binary file added examples/gcn/cora/raw/ind.cora.x
Binary file not shown.
Binary file added examples/gcn/cora/raw/ind.cora.y
Binary file not shown.
21 changes: 21 additions & 0 deletions examples/gcn/debug_edge_index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import os
os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] = 'python'

import sys
sys.path.insert(0, '/home/zbs2/GammaGL_algo/论文复现/GraphLama/GammaGL/tensorlayerx_patch')

import gammagl
import tensorlayerx as tlx
from gammagl.datasets import Planetoid
from gammagl.utils.check import check_is_numpy

print("Loading Cora dataset...")
dataset = Planetoid(root='/tmp/cora', name='cora')
graph = dataset[0]

print(f"Edge index type: {type(graph.edge_index)}")
print(f"Edge index shape: {graph.edge_index.shape}")
print(f"Is tensor: {tlx.is_tensor(graph.edge_index)}")
print(f"Is numpy: {check_is_numpy(graph.edge_index)}")
print(f"Edge index dtype: {graph.edge_index.dtype}")
print(f"First few edges: {graph.edge_index[:, :5]}")
23 changes: 23 additions & 0 deletions examples/gcn/test_gammagl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import os
os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] = 'python'

import sys
sys.path.insert(0, '/home/zbs2/GammaGL_algo/论文复现/GraphLama/GammaGL/tensorlayerx_patch')

import gammagl
import torch
from gammagl.data import Graph

print("GammaGL version:", getattr(gammagl, '__version__', 'Unknown'))
print("PyTorch version:", torch.__version__)

# Create a simple graph
edge_index = torch.tensor([[0, 1, 1, 2], [1, 0, 2, 1]], dtype=torch.long)
x = torch.randn(3, 4)
graph = Graph(x=x, edge_index=edge_index)

print("Graph created successfully:")
print(f" Nodes: {graph.num_nodes}")
print(f" Edges: {graph.num_edges}")
print(f" Node features shape: {graph.x.shape}")
print("\nGammaGL basic functionality test passed!")
20 changes: 13 additions & 7 deletions examples/graphgpt/eval.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
export PYTHONPATH=$(dirname $(dirname $(realpath $0))):$PYTHONPATH
export PYTHONPATH=/home/zbs2/GammaGL_algo/论文复现/GraphLama/GammaGL
# to fill in the following path to extract projector for the second tuning stage!
output_model=/local/yy3/graphgpt/GraphGPT-7B-mix-all # path to the pre-trained model checkpoint
datapath=/local/yy3/graphgpt/data/eval/cora_test_instruct_std.json # path to the instruction datset
graph_data_path=/local/yy3/graphgpt/data/graph_data_all.pt # path to the graph data
res_path=./output_stage_2_cora_nc # path to save the results
output_model=/home/zbs2/GammaGL_algo/论文复现/GraphLama/GraphGPT_data/GraphGPT
datapath=/home/zbs2/GammaGL_algo/论文复现/GraphLama/GraphGPT_data/GraphGPT_eval_dataset/cora_test_instruct_std.json
graph_data_path=/home/zbs2/GammaGL_algo/论文复现/GraphLama/GraphGPT_data/graphdata_all/graph_data_all.pt
pretrain_graph_model_path=/home/zbs2/GammaGL_algo/论文复现/GraphLama/GraphGPT_data/graph_cli_gt
res_path=./output_cora_nc # path to save the results
start_id=0
end_id=20000 # total number of instructions to test
num_gpus=1

export CUDA_VISIBLE_DEVICES=2 # specify the GPU id
export CUDA_VISIBLE_DEVICES=2

python ./examples/graphgpt/graphgpt_trainer.py --model-name ${output_model} --prompting_file ${datapath} --graph_data_path ${graph_data_path} --output_res_path ${res_path} --start_id ${start_id} --end_id ${end_id} --num_gpus ${num_gpus}
python /home/zbs2/GammaGL_algo/论文复现/GraphLama/GammaGL/examples/graphgpt/graphgpt_trainer.py --model-name ${output_model} --prompting_file ${datapath} --graph_data_path ${graph_data_path} --output_res_path ${res_path} --start_id ${start_id} --pretrain_graph_model_path ${pretrain_graph_model_path} --end_id ${end_id}

# TLX 版本运行示例:
# python /home/zbs2/GammaGL_algo/论文复现/GraphLama/GammaGL/examples/graphgpt/graphgpt_trainer_tlx.py --model-name ${output_model} --prompting_file ${datapath} --graph_data_path ${graph_data_path} --output_res_path ${res_path} --start_id ${start_id} --pretrain_graph_model_path ${pretrain_graph_model_path} --end_id ${end_id}

# TL_BACKEND="torch" nohup bash examples/graphgpt/eval.sh > log/test_graphgpt.out &
2 changes: 0 additions & 2 deletions examples/graphgpt/graphgpt_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
"pubmed":{"Experimentally induced diabetes": 0, "Type 2 diabetes": 2, "Type 1 diabetes": 1}
}



data_list = []
folder = 'output_stage_2_{}_nc'.format(args.dataset)
for filename in os.listdir(folder):
Expand Down
76 changes: 28 additions & 48 deletions examples/graphgpt/graphgpt_trainer.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import argparse
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
import os
from gammagl.utils.conversation import conv_templates, SeparatorStyle
from gammagl.utils.gfm_utils import disable_torch_init, KeywordsStoppingCriteria
from gammagl.utils.gfm_utils import DEFAULT_G_END_TOKEN, DEFAULT_G_START_TOKEN, DEFAULT_GRAPH_PATCH_TOKEN, DEFAULT_GRAPH_TOKEN, GRAPH_TOKEN_INDEX
from transformers import CLIPVisionModel, CLIPImageProcessor, StoppingCriteria
from gammagl.models.graphgpt import *
from transformers import AutoTokenizer, AutoModelForCausalLM
import sys

from torch_geometric.data import Data
import json
import copy
from tqdm import tqdm
import json
import os.path as osp
import ray


os.environ['TL_BACKEND'] = 'torch'

Expand Down Expand Up @@ -44,50 +45,21 @@ def load_prompting_file(file_path):

# def prepare_query(instruct_item):


def run_eval(args, num_gpus):
# split question file into num_gpus files
def run_eval(args):
# Load and slice prompting file according to start/end
prompt_file = load_prompting_file(args.prompting_file)
args.end_id = min(args.end_id, len(prompt_file))
prompt_file = prompt_file[args.start_id:args.end_id]
chunk_size = len(prompt_file) // num_gpus
ans_handles = []
split_list = list(range(args.start_id, args.end_id, chunk_size))
idx_list = list(range(0, len(prompt_file), chunk_size))
if len(split_list) == num_gpus:
split_list.append(args.end_id)
idx_list.append(len(prompt_file))
elif len(split_list) == num_gpus + 1:
split_list[-1] = args.end_id
idx_list[-1] = len(prompt_file)
else:
raise ValueError('error in the number of list')

if osp.exists(args.output_res_path) is False:
os.mkdir(args.output_res_path)

for idx in range(len(idx_list) - 1):
start_idx = idx_list[idx]
end_idx = idx_list[idx + 1]

start_split = split_list[idx]
end_split = split_list[idx + 1]
ans_handles.append(
eval_model.remote(
args, prompt_file[start_idx:end_idx], start_split, end_split
)
)
prompt_slice = prompt_file[args.start_id:args.end_id]

ans_jsons = []
for ans_handle in ans_handles:
ans_jsons.extend(ray.get(ans_handle))
# Ensure output directory exists
os.makedirs(args.output_res_path, exist_ok=True)

# with open(args.output_res_path, "w") as ans_file:
# for line in ans_jsons:
# ans_file.write(json.dumps(line) + "\n")
# Directly run evaluation without Ray
print('--- evaluating without Ray ---')
ans_jsons = eval_model(args, prompt_slice, args.start_id, args.end_id)
print('--- done ---')


@ray.remote(num_gpus=1)
@torch.inference_mode()
def eval_model(args, prompt_file, start_idx, end_idx):
# load prompting file
Expand Down Expand Up @@ -115,7 +87,17 @@ def eval_model(args, prompt_file, start_idx, end_idx):
# TODO: add graph tower
# if graph_tower.device.type == 'meta':
# print('meta')
clip_graph, args_graph= load_model_pretrained(CLIP, model.config.pretrain_graph_model_path)
# Resolve graph tower pretrain path (supports absolute or relative to model dir)
cfg_pretrain_path = getattr(model.config, "pretrain_graph_model_path", None)
if args.pretrain_graph_model_path is not None:
pretrain_path = args.pretrain_graph_model_path
else:
if cfg_pretrain_path is None:
raise ValueError("pretrain_graph_model_path not set; please provide --pretrain_graph_model_path")
pretrain_path = cfg_pretrain_path if osp.isabs(cfg_pretrain_path) else osp.join(args.model_name, cfg_pretrain_path)
assert osp.exists(osp.join(pretrain_path, 'config.json')), f'config.json missing at {pretrain_path}'

clip_graph, args_graph = load_model_pretrained(CLIP, pretrain_path)
graph_tower = graph_transformer(args_graph)
graph_tower = transfer_param_tograph(clip_graph, graph_tower)

Expand Down Expand Up @@ -208,25 +190,23 @@ def eval_model(args, prompt_file, start_idx, end_idx):

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--model-name", type=str, default="facebook/opt-350m")
parser.add_argument("--model-name", type=str, default="/home/zbs2/GammaGL_algo/论文复现/GraphLama/GraphGPT_data/GraphGPT")
# parser.add_argument("--image-file", type=str, required=True)
# parser.add_argument("--query", type=str, required=True)
parser.add_argument("--prompting_file", type=str, default=None)
parser.add_argument("--conv-mode", type=str, default=None)
parser.add_argument("--graph_data_path", type=str, default=None)

parser.add_argument("--output_res_path", type=str, default=None)
parser.add_argument("--num_gpus", type=int, default=4)

parser.add_argument("--start_id", type=int, default=0)
parser.add_argument("--end_id", type=int, default=20567)
parser.add_argument("--pretrain_graph_model_path", type=str, default=None,
help="Path to graph tower pretrain directory containing config.json (overrides model config)")

args = parser.parse_args()

# eval_model(args)

ray.init()
run_eval(args, args.num_gpus)

# Run in single process without Ray
run_eval(args)

# protobuf 4.22.3
7 changes: 7 additions & 0 deletions examples/graphgpt/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# import argparse
import torch
import os
from gammagl.utils.conversation import conv_templates, SeparatorStyle
from transformers import AutoTokenizer, AutoModelForCausalLM

print('导入成功')
31 changes: 31 additions & 0 deletions examples/graphlama/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# GraphLAMA: Enabling Efficient Adaptation of Graph Language Models with Limited Annotations
* Paper link: https://arxiv.org/pdf/2506.21559
* Author's code repo: https://github.com/BUPT-GAMMA/GraphLAMA.git

# How to Run

* First, follow the [GraphLAMA original repo](https://github.com/BUPT-GAMMA/GraphLAMA.git) to install all required packages where will tell you to follow the instructions to

* Then download all required datasets and pretrained checkpoints, and fill their path into corresponding values in eval.sh

# Dataset Statics
| Dataset | # Nodes | # Edges | # Classes |
| :-------: | :-------: | :------: | :------: |
| Cora | 25,120 | 182,280 | 70 |
| PubMed | 19,717 | 44,338 | 3 |
| ogb-arxiv | 169,343 | 1,166,243 | 40 |

# Files Description
* graphgpt_trainer.py: the trainer of graphgpt, inference stage
* graphgpt_eval.py: run this to evaluate

# Results
```bash
# run inference
TL_BACKEND="torch" nohup bash examples/graphgpt/eval.sh > log/test_graphgpt.out &
# run evaluation
python examples/graphgpt/graphgpt_eval.py --dataset cora
```
| Dataset | Paper | Our(torch) |
| :-------: | :-------: | :------: |
| Cora | 0.1501 | 0.1451 |
14 changes: 14 additions & 0 deletions examples/graphlama/eval.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export PYTHONPATH=/home/zbs2/GammaGL_algo/论文复现/GraphLama/GammaGL
# Fill in the following path to extract projector for the tuning stage following the instructions.
output_model=/path/to/GraphLama/weights
datapath=path/to/cora_test_instruct_std.json
graph_data_path=path/to/graph_data_all.pt
pretrain_graph_model_path=path/to/graph_cli_gt # remember to download arixv ones
res_path=./output_cora_nc # path to save the results
start_id=0
end_id=20000 # total number of instructions to test
num_gpus=1

export CUDA_VISIBLE_DEVICES=2 # define the gpu id to use

python graphlama_trainer.py --model-name ${output_model} --prompting_file ${datapath} --graph_data_path ${graph_data_path} --output_res_path ${res_path} --start_id ${start_id} --pretrain_graph_model_path ${pretrain_graph_model_path} --end_id ${end_id}
109 changes: 109 additions & 0 deletions examples/graphlama/graphgpt_eval_tlx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import json
import os.path as osp
import os
import re
import pandas as pd
from tqdm import tqdm
from sklearn.metrics import classification_report

import tensorlayerx as tlx

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--dataset', type=str, default='arxiv')
args = parser.parse_args()

label_to_idx = {
"cora":{"databases, object oriented": 29, "operating systems, memory management": 59, "data structures algorithms and theory, quantum computing": 24, "artificial intelligence, planning": 13, "artificial intelligence, knowledge representation": 4, "artificial intelligence, data mining": 1, "artificial intelligence, vision and pattern recognition": 17, "artificial intelligence, machine learning, case-based": 5, "artificial intelligence, agents": 0, "artificial intelligence, machine learning, probabilistic methods": 8, "encryption and compression, security": 36, "operating systems, distributed": 57, "human computer interaction, interface design": 46, "artificial intelligence, machine learning, genetic algorithms": 6, "human computer interaction, graphics and virtual reality": 45, "artificial intelligence, machine learning, rule learning": 10, "programming, functional": 63, "programming, object oriented": 67, "encryption and compression, encryption": 35, "databases, performance": 30, "networking, protocols": 54, "data structures algorithms and theory, randomized": 25, "data structures algorithms and theory, formal languages": 20, "data structures algorithms and theory, parallel": 23, "programming, software development": 69, "programming, compiler design": 61, "artificial intelligence, machine learning, theory": 11, "artificial intelligence, machine learning, neural networks": 7, "programming, logic": 66, "databases, relational": 32, "information retrieval, retrieval": 52, "programming, debugging": 62, "networking, wireless": 56, "artificial intelligence, theorem proving": 16, "databases, temporal": 33, "encryption and compression, compression": 34, "information retrieval, filtering": 51, "data structures algorithms and theory, computational complexity": 18, "programming, garbage collection": 64, "artificial intelligence, machine learning, reinforcement learning": 9, "human computer interaction, multimedia": 47, "hardware and architecture, vlsi": 43, "artificial intelligence, nlp": 12, "hardware and architecture, microprogramming": 42, "operating systems, fault tolerance": 58, "programming, java": 65, "operating systems, realtime": 60, "human computer interaction, cooperative": 44, "artificial intelligence, speech": 15, "databases, deductive": 28, "artificial intelligence, robotics": 14, "data structures algorithms and theory, logic": 22, "networking, routing": 55, "hardware and architecture, logic design": 40, "hardware and architecture, distributed architectures": 37, "data structures algorithms and theory, hashing": 21, "programming, semantics": 68, "artificial intelligence, games and search": 3, "databases, concurrency": 27, "data structures algorithms and theory, sorting": 26, "human computer interaction, wearable computers": 48, "information retrieval, digital library": 49, "artificial intelligence, expert systems": 2, "information retrieval, extraction": 50, "data structures algorithms and theory, computational geometry": 19, "databases, query evaluation": 31, "networking, internet": 53, "hardware and architecture, memory structures": 41, "hardware and architecture, high performance computing": 38, "hardware and architecture, input output and storage": 39},
"pubmed":{"Experimentally induced diabetes": 0, "Type 2 diabetes": 2, "Type 1 diabetes": 1}
}

data_list = []
folder = 'output_stage_2_{}_nc'.format(args.dataset)
for filename in os.listdir(folder):
if filename.endswith('.json'):
file_path = os.path.join(folder, filename)
with open(file_path, 'r') as f:
data = json.load(f)
data_list.extend(data)

print(data_list[1])

# 加载图数据(原始为 .pt 文件),使用 torch 读取后转为 TLX Tensor
import torch as th
graph_data = th.load('/local/yy3/graphgpt/data/graph_data_all.pt')[args.dataset]
labels_th = graph_data.y
labels = tlx.convert_to_tensor(labels_th.detach().cpu().numpy(), dtype=tlx.int64)

def cal_map():
label_dict = {}
if args.dataset == "arxiv":
df = pd.read_csv(os.path.expanduser('~/datasets/OGB/ogbn_arxiv/mapping/labelidx2arxivcategeory.csv.gz'), compression='gzip')
for index, line in df.iterrows():
lb = line['arxiv category'].split(' ')[-1]
lb_new = 'cs.' + lb.upper()
label_dict[lb_new] = line['label idx']
else:
label_dict = label_to_idx[args.dataset]
return label_dict

class_map = cal_map()

inverse_class_map = {}
for lb, lb_id in class_map.items():
inverse_class_map[lb_id] = lb


pattern = r"cs\.[A-Z]{2}"


topk = 3

correct = 0
total = len(data_list)

trues = []
preds = []

for instruct_item in tqdm(data_list):
nid = instruct_item['node_idx']
gpt_res = instruct_item['res']


true_y = labels[nid]
true_y_val = int(tlx.ops.convert_to_numpy(true_y))

pred_y = []
if args.dataset == "arxiv":
matches = list(set(re.findall(pattern, gpt_res))) # pred
sorted_matches = sorted(matches, key=lambda x: gpt_res.index(x))
for m in sorted_matches:
try:
pred_y.append(class_map[m])
except:
pass
try:
# print(sorted_matches)
preds.append(pred_y[0])
except:
preds.append(-1)
else:
for lb, lb_id in class_map.items():
if lb in gpt_res:
pred_y.append(lb_id)
try:
# print(sorted_matches)
preds.append(pred_y[0])
except:
preds.append(-1)
trues.append(true_y_val)
res_tmp = 1 if true_y_val in pred_y[:topk] else 0
correct = correct + 1 if true_y_val in pred_y[:topk] else correct

acc = correct / total

print("Accuracy:", acc)

report = classification_report(trues, preds, digits=6)

print(report)
Loading