Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Genralized loading to multiple data loaders #340

Open
wants to merge 3 commits into
base: OpenKE-PyTorch
Choose a base branch
from
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
22 changes: 21 additions & 1 deletion openke/base/Base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,32 @@ void sampling(
INT negRate = 1,
INT negRelRate = 0,
INT mode = 0,
INT domain_idx=0,
bool filter_flag = true,
bool p = false,
bool val_loss = false
) {
pthread_t *pt = (pthread_t *)malloc(workThreads * sizeof(pthread_t));
Parameter *para = (Parameter *)malloc(workThreads * sizeof(Parameter));

// Update the global variables to current loader index
trainList = trainLists[domain_idx];
trainTotal = trainTotals[domain_idx];

trainHead = trainHeads[domain_idx];
trainTail = trainTails[domain_idx];
trainRel = trainRels[domain_idx];

lefHead = lefHeads[domain_idx];
rigHead = rigHeads[domain_idx];
lefTail = lefTails[domain_idx];
rigTail = rigTails[domain_idx];
lefRel = lefRels[domain_idx];
rigRel = rigRels[domain_idx];

entityTotal = entityTotals[domain_idx];
relationTotal = relationTotals[domain_idx];

for (INT threads = 0; threads < workThreads; threads++) {
para[threads].id = threads;
para[threads].batch_h = batch_h;
Expand All @@ -199,4 +219,4 @@ void sampling(
int main() {
importTrainFiles();
return 0;
}
}
42 changes: 42 additions & 0 deletions openke/base/Reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <algorithm>
#include <iostream>
#include <cmath>
#include <vector>

INT *freqRel, *freqEnt;
INT *lefHead, *rigHead;
Expand All @@ -14,6 +15,25 @@ INT *lefRel, *rigRel;
REAL *left_mean, *right_mean;
REAL *prob;

// Vector of collecting the global variables from different data loaders
std::vector< Triple* > trainLists = std::vector<Triple*>();

std::vector< Triple* > trainHeads = std::vector<Triple*>();
std::vector< Triple* > trainTails = std::vector<Triple*>();
std::vector< Triple* > trainRels = std::vector<Triple*>();

std::vector< INT* > lefHeads = std::vector<INT*>();
std::vector< INT* > rigHeads = std::vector<INT*>();
std::vector< INT* > lefTails = std::vector<INT*>();
std::vector< INT* > rigTails = std::vector<INT*>();
std::vector< INT* > lefRels = std::vector<INT*>();
std::vector< INT* > rigRels = std::vector<INT*>();

std::vector< INT > trainTotals = std::vector<INT>();
std::vector< INT > tripleTotals = std::vector<INT>();
std::vector< INT > entityTotals = std::vector<INT>();
std::vector< INT > relationTotals = std::vector<INT>();

Triple *trainList;
Triple *trainHead;
Triple *trainTail;
Expand Down Expand Up @@ -83,6 +103,16 @@ void importTrainFiles() {
trainRel = (Triple *)calloc(trainTotal, sizeof(Triple));
freqRel = (INT *)calloc(relationTotal, sizeof(INT));
freqEnt = (INT *)calloc(entityTotal, sizeof(INT));

// Collect the address of train lists, thier heads, tails and relations
trainLists.push_back(trainList);
trainHeads.push_back(trainHead);
trainTails.push_back(trainTail);
trainRels.push_back(trainRel);

// Collect number of entities and relations of this data loader
entityTotals.push_back(entityTotal);
relationTotals.push_back(relationTotal);
for (INT i = 0; i < trainTotal; i++) {
tmp = fscanf(fin, "%ld", &trainList[i].h);
tmp = fscanf(fin, "%ld", &trainList[i].t);
Expand Down Expand Up @@ -117,6 +147,18 @@ void importTrainFiles() {
rigRel = (INT *)calloc(entityTotal, sizeof(INT));
memset(rigHead, -1, sizeof(INT)*entityTotal);
memset(rigTail, -1, sizeof(INT)*entityTotal);

// Collect left and rights of heads, tail and relations
lefHeads.push_back(lefHead);
rigHeads.push_back(rigHead);
lefTails.push_back(lefTail);
rigTails.push_back(rigTail);
lefRels.push_back(lefRel);
rigRels.push_back(rigRel);

// Collect train totals
trainTotals.push_back(trainTotal);

memset(rigRel, -1, sizeof(INT)*entityTotal);
for (INT i = 1; i < trainTotal; i++) {
if (trainTail[i].t != trainTail[i - 1].t) {
Expand Down
7 changes: 6 additions & 1 deletion openke/data/TrainDataLoader.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def __init__(self,
ctypes.c_int64,
ctypes.c_int64,
ctypes.c_int64,
ctypes.c_int64,
ctypes.c_int64
]
self.in_path = in_path
Expand All @@ -71,6 +72,7 @@ def __init__(self,
self.negative_ent = neg_ent
self.negative_rel = neg_rel
self.sampling_mode = sampling_mode
self.domain_idx = domain_idx
self.cross_sampling_flag = 0
self.read()

Expand Down Expand Up @@ -115,6 +117,7 @@ def sampling(self):
self.negative_ent,
self.negative_rel,
0,
self.domain_idx,
self.filter,
0,
0
Expand All @@ -137,6 +140,7 @@ def sampling_head(self):
self.negative_ent,
self.negative_rel,
-1,
self.domain_idx,
self.filter,
0,
0
Expand All @@ -159,6 +163,7 @@ def sampling_tail(self):
self.negative_ent,
self.negative_rel,
1,
self.domain_idx,
self.filter,
0,
0
Expand Down Expand Up @@ -226,4 +231,4 @@ def __iter__(self):
return TrainDataSampler(self.nbatches, self.cross_sampling)

def __len__(self):
return self.nbatches
return self.nbatches