GithubHelp home page GithubHelp logo

lyuqin / hydranet-wikisql Goto Github PK

View Code? Open in Web Editor NEW
64.0 2.0 18.0 18 KB

Code and trained model for Hybrid ranking network for text-to-SQL on WikiSQL

Home Page: https://arxiv.org/abs/2008.04759

License: MIT License

Python 99.55% Dockerfile 0.45%
text-to-sql wikisql nl2sql

hydranet-wikisql's Introduction

Hybrid Ranking Network for Text-to-SQL

Code for our paper Hybrid Ranking Network for Text-to-SQL

Environment Setup

  • Python 3.8
  • Pytorch 1.7.1 or higher
  • pip install -r requirements.txt

We can also run experiments with docker image: docker build -t hydranet -f Dockerfile .

The built image above contains processed data and is ready for training and evaluation.

Data Preprocessing

  1. Create data folder and output folder first: mkdir data && mkdir output
  2. Clone WikiSQL repo: git clone https://github.com/salesforce/WikiSQL && tar xvjf WikiSQL/data.tar.bz2 -C WikiSQL
  3. Preprocess data: python wikisql_gendata.py

Training

  1. Run python main.py train --conf conf/wikisql.conf --gpu 0,1,2,3 --note "some note".
  2. Model will be saved to output folder, named by training start datetime.

Evaluation

  1. Modify model, input and output settings in wikisql_prediction.py and run it.
  2. Run WikiSQL evaluation script to get official numbers: cd WikiSQL && python evaluate.py data/test.jsonl data/test.db ../output/test_out.jsonl

Note: the WikiSQL evaluation script will encounter error when running in Windows system. Hence we included the fixed version for Windows User (run in root folder): python wikisql_evaluate.py WikiSQL/data/test.jsonl WikiSQL/data/test.db output/test_out.jsonl

Trained Model

Trained model that can reproduce reported number on WikiSQL leaderboard is attached in the releases (see under "Releases" in the right column). Model prediction outputs are also attached.

hydranet-wikisql's People

Contributors

dependabot[bot] avatar lyuqin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

hydranet-wikisql's Issues

Got different Result

I run the code successfully, but the result is far from yours result mentioned in the paper, could you add the trained model to the repo, or publish the train parameters

While doing prediction facing this error

input_ids (101072, 96)
input_mask (101072, 96)
segment_ids (101072, 96)
num of samples: 15878
PyTorch model loaded from output/20200207_105347\model_4.pt
model prediction start
Traceback (most recent call last):
File "wikisql_prediction.py", line 73, in
model_outputs = model.dataset_inference(pred_data)
File "C:\Users\mohdf\OneDrive\Desktop\Hydranet_sql\HydraNet-WikiSQL\modeling\base_model.py", line 24, in dataset_inference
model_outputs = self.model_inference(dataset.model_inputs)
File "C:\Users\mohdf\OneDrive\Desktop\Hydranet_sql\HydraNet-WikiSQL\modeling\torch_model.py", line 57, in model_inference
model_output = self.model(**input_tensor)
File "C:\Users\mohdf\anaconda3\envs\hydranet2\lib\site-packages\torch\nn\modules\module.py", line 1102, in _call_impl
return forward_call(*input, **kwargs)
File "C:\Users\mohdf\OneDrive\Desktop\Hydranet_sql\HydraNet-WikiSQL\modeling\torch_model.py", line 114, in forward
bert_output, pooled_output = self.base_model(
File "C:\Users\mohdf\anaconda3\envs\hydranet2\lib\site-packages\torch\nn\modules\module.py", line 1102, in _call_impl
return forward_call(*input, **kwargs)
File "C:\Users\mohdf\anaconda3\envs\hydranet2\lib\site-packages\transformers\models\roberta\modeling_roberta.py", line 815, in forward
encoder_outputs = self.encoder(
File "C:\Users\mohdf\anaconda3\envs\hydranet2\lib\site-packages\torch\nn\modules\module.py", line 1102, in _call_impl
return forward_call(*input, **kwargs)
File "C:\Users\mohdf\anaconda3\envs\hydranet2\lib\site-packages\transformers\models\roberta\modeling_roberta.py", line 508, in forward
layer_outputs = layer_module(
File "C:\Users\mohdf\anaconda3\envs\hydranet2\lib\site-packages\torch\nn\modules\module.py", line 1102, in _call_impl
return forward_call(*input, **kwargs)
File "C:\Users\mohdf\anaconda3\envs\hydranet2\lib\site-packages\transformers\models\roberta\modeling_roberta.py", line 395, in forward
self_attention_outputs = self.attention(
File "C:\Users\mohdf\anaconda3\envs\hydranet2\lib\site-packages\torch\nn\modules\module.py", line 1102, in _call_impl
return forward_call(*input, **kwargs)
File "C:\Users\mohdf\anaconda3\envs\hydranet2\lib\site-packages\transformers\models\roberta\modeling_roberta.py", line 323, in forward
self_outputs = self.self(
File "C:\Users\mohdf\anaconda3\envs\hydranet2\lib\site-packages\torch\nn\modules\module.py", line 1102, in _call_impl
return forward_call(*input, **kwargs)
File "C:\Users\mohdf\anaconda3\envs\hydranet2\lib\site-packages\transformers\models\roberta\modeling_roberta.py", line 187, in forward
mixed_query_layer = self.query(hidden_states)
File "C:\Users\mohdf\anaconda3\envs\hydranet2\lib\site-packages\torch\nn\modules\module.py", line 1102, in _call_impl
return forward_call(*input, **kwargs)
File "C:\Users\mohdf\anaconda3\envs\hydranet2\lib\site-packages\torch\nn\modules\linear.py", line 103, in forward
return F.linear(input, self.weight, self.bias)
File "C:\Users\mohdf\anaconda3\envs\hydranet2\lib\site-packages\torch\nn\functional.py", line 1848, in linear
return torch._C._nn.linear(input, weight, bias)
RuntimeError: CUDA error: CUBLAS_STATUS_ALLOC_FAILED when calling cublasCreate(handle)

How to use other pretrain model in this code?

Hello! I want to use some other model as for base model. For some commen models such as bert that it is easy to work. So if j just have .pt and source code ,how do i put it into utils.py-->create_base_mode(config) ? if there will have some data format question in these process?

I want to use mt-dnn for the base model and just have .pt. i'm very hope for you help! Thanks!

RuntimeError: unexpected EOF, expected 3539973 more bytes. The file might be corrupted.

Hi!
After I downloaded the model and want to evaluate , something error.
The details are as follows:
(python37) guest05@oem-04:~/yaoProjects/HydraNet-WikiSQL$ python wikisql_prediction.py
To use data.metrics please install scikit-learn. See https://scikit-learn.org/stable/index.html
5000
10000
15000
data/wikitest.jsonl loaded. Data shapes:
input_ids (101072, 96)
input_mask (101072, 96)
segment_ids (101072, 96)
num of samples: 15878
Traceback (most recent call last):
File "/home/guest05/anaconda3/envs/python37/lib/python3.7/site-packages/transformers/modeling_utils.py", line 415, in from_pretrained
state_dict = torch.load(resolved_archive_file, map_location='cpu')
File "/home/guest05/anaconda3/envs/python37/lib/python3.7/site-packages/torch/serialization.py", line 593, in load
return _legacy_load(opened_file, map_location, pickle_module, **pickle_load_args)
File "/home/guest05/anaconda3/envs/python37/lib/python3.7/site-packages/torch/serialization.py", line 779, in _legacy_load
deserialized_objects[key]._set_from_file(f, offset, f_should_read_directly)
RuntimeError: unexpected EOF, expected 3539973 more bytes. The file might be corrupted.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "wikisql_prediction.py", line 64, in
model = create_model(config, is_train=False)
File "/home/guest05/yaoProjects/HydraNet-WikiSQL/modeling/model_factory.py", line 6, in create_model
return HydraTorch(config)
File "/home/guest05/yaoProjects/HydraNet-WikiSQL/modeling/torch_model.py", line 12, in init
self.model = HydraNet(config)
File "/home/guest05/yaoProjects/HydraNet-WikiSQL/modeling/torch_model.py", line 84, in init
self.base_model = utils.create_base_model(config)
File "/home/guest05/yaoProjects/HydraNet-WikiSQL/utils.py", line 36, in create_base_model
return transformers.RobertaModel.from_pretrained(weights_name)
File "/home/guest05/anaconda3/envs/python37/lib/python3.7/site-packages/transformers/modeling_utils.py", line 417, in from_pretrained
raise OSError("Unable to load weights from pytorch checkpoint file. "
OSError: Unable to load weights from pytorch checkpoint file. If you tried to load a PyTorch model from a TF 2.0 checkpoint, please set from_tf=True.
malloc(): unsorted double linked list corrupted
已放弃 (核心已转储)

Can you give me some suggestions?Thank you very much.

Code on chinese dataset TableQA

a lot of papers show the results of HydraNet on Chinese dataset--TableQA,I want to ask if you have the code on Chinese datasets.Or do you kown how the paper get the results? Change the code by themselves or others?
Thank you!

Export to ONNX Model possible?

Hello,
I would like to export my trained model to onnx model.

Export Function call:

dummy_input2 = torch.LongTensor(2, 2)
dummy_input2.requires_grad = False

torch.onnx.export(model.model,
(dummy_input0, dummy_input1, dummy_input2),
"Text-to-SQL.onnx",
input_names = ["input_ids", "input_mask", "segment_ids"],
output_names = ["output"]
)

“RuntimeError: Cannot insert a Tensor that requires grad as a constant. Consider making it a parameter or input, or detaching the gradient”.

Do you some advices or a guide for exporting this model to onnx?

Best regards!

a bug on a sub task val accuracy when training on multi GPUs

When I train the model on multi GPUs, a sub task metric val accuracy is extremely low while the other metrics are high. (python main.py train --conf conf/wikisql.conf --gpu 0,1,2,3 --note "some note")
But when I train it on single GPU, the bug disappears.(python main.py train --conf conf/wikisql.conf --gpu 0 --note "some note")

I have to use a small batch size to train on a single GPU so the performance is 3% lower in dev/test logical form acc. I want to train it on multi GPUs but I don't know why the bug occurs and how to solve it?

训练111
预测111

CUDA out of memory when running on single GPU

Hello there.

I've been trying to run the training for a few hours. My specs are:
Nvidia RTX 2070, 32GB of Ram and a ryzen 3700X.
Fedora 36 with proprietary Nvidia drivers (510.68.02 CUDA Version: 11.6).

When I run the command python main.py train --conf conf/wikisql.conf --gpu 0, it stops shortly after printing start training. It says it failed to allocate memory on the GPU.

If I don't specify --gpu 0, it gives the same output, which leads me to think that it's using the GPU either way.

This is the stack trace produced:

start training
Traceback (most recent call last):
  File "/home/djouze/dev/HydraNet-WikiSQL/main.py", line 70, in <module>
    cur_loss = model.train_on_batch(batch)
  File "/home/djouze/dev/HydraNet-WikiSQL/modeling/torch_model.py", line 41, in train_on_batch
    batch_loss = torch.mean(self.model(**batch)["loss"])
  File "/home/djouze/.local/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1110, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/djouze/dev/HydraNet-WikiSQL/modeling/torch_model.py", line 114, in forward
    bert_output, pooled_output = self.base_model(
  File "/home/djouze/.local/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1110, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/djouze/.local/lib/python3.10/site-packages/transformers/models/roberta/modeling_roberta.py", line 815, in forward
    encoder_outputs = self.encoder(
  File "/home/djouze/.local/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1110, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/djouze/.local/lib/python3.10/site-packages/transformers/models/roberta/modeling_roberta.py", line 508, in forward
    layer_outputs = layer_module(
  File "/home/djouze/.local/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1110, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/djouze/.local/lib/python3.10/site-packages/transformers/models/roberta/modeling_roberta.py", line 395, in forward
    self_attention_outputs = self.attention(
  File "/home/djouze/.local/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1110, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/djouze/.local/lib/python3.10/site-packages/transformers/models/roberta/modeling_roberta.py", line 323, in forward
    self_outputs = self.self(
  File "/home/djouze/.local/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1110, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/djouze/.local/lib/python3.10/site-packages/transformers/models/roberta/modeling_roberta.py", line 253, in forward
    attention_probs = self.dropout(attention_probs)
  File "/home/djouze/.local/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1110, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/djouze/.local/lib/python3.10/site-packages/torch/nn/modules/dropout.py", line 58, in forward
    return F.dropout(input, self.p, self.training, self.inplace)
  File "/home/djouze/.local/lib/python3.10/site-packages/torch/nn/functional.py", line 1279, in dropout
    return _VF.dropout_(input, p, training) if inplace else _VF.dropout(input, p, training)
RuntimeError: CUDA out of memory. Tried to allocate 144.00 MiB (GPU 0; 7.79 GiB total capacity; 6.05 GiB already allocated; 158.19 MiB free; 6.08 GiB reserved in total by PyTorch) If reserved memory is >> allocated memory try setting max_split_size_mb to avoid fragmentation.  See documentation for Memory Management and PYTORCH_CUDA_ALLOC_CONF

My last try was using the docker image, after the training started, my memory consumption got to 31GB, but the docker seems to have crashed without producing any output.

Could you help me find out why that's happening? I assume it would be able to run on single GPU spec, although in that case it would likely take more time (correct me if I'm wrong).

Thank you for your attention,
José

Is there any Issue on accuracy score on EG cases ?

I really appreciate your code and model(HydraNet). with your code, I can study and understand a lot. Thank you.

But I have a problem with accuracy score on EG cases.
Is there any issue or report on accuracy score on EG cases ?

I just change batch_size because of my CUDA memory limit. But I do not change any line.
I can get right accuracy scores in no EG cases. But, in EG cases.. I have very low (abnormal) accuracy scroe..

For example, when I open the "output/test_out.jsonl.eg",
then "conds : [ ]" of all predicted quries have empty list.

So, I get 0.00825.. score in wcn_acc, wcc_acc, wco_acc, wcv_acc metrics.

Debugging is on me. but, if you give me some advice, It will be very helpful ! :)

Thank you!

KeyError: 'token_type_ids'

When running wikisql_prediction.py with trained model, I got the error KeyError: 'token_type_ids'. The details are as follows.

Traceback (most recent call last):
  File "/home/HydraNet-WikiSQL-master/wikisql_prediction.py", line 61, in <module>
    pred_data = SQLDataset(in_file, config, featurizer, False)
  File "/home/HydraNet-WikiSQL-master/featurizer.py", line 262, in __init__
    self.input_features, self.model_inputs, self.pos = self.featurizer.load_data(data_paths, config, include_label)
  File "/home/HydraNet-WikiSQL-master/featurizer.py", line 221, in load_data
    input_feature = self.get_input_feature(example, config)
  File "/home/HydraNet-WikiSQL-master/featurizer.py", line 134, in get_input_feature
    segment_ids = tokenize_result["token_type_ids"]
  File "/home/anaconda3/lib/python3.7/site-packages/transformers/tokenization_utils_base.py", line 273, in __getitem__
    return self.data[item]
KeyError: 'token_type_ids'

It seems that the output of the function tokenizer.encode_plus in line 116 of featurizer.py doesn't has the key token_type_ids.

Please share if there is any solution to this issue.

Thanks and Regards

How to run model on a single question?

Hello,
is there any function or way to get a single sql query from a single question? Like run the trainend model on db-schema and a single question.

Or is only way by adding a line at the end of the "/wikisql/test.jsonl" and "/wikisql/test.tables.jsonl" file and run "wikisql_prediction.py", according to #issue3?

Thank you!

Question about Ranking score in your code

Hi!
With your feedback on issue#7, I can experiment safely hydra-net with your code.
I really appreciate it 👍

Recently I am focusing on Semantic parsing task(especially, Text-to-SQL).
So I am doing some experiments with several models on Wiki-SQL dataset.
and now I am trying to get 'Top-k accuracy' on Predicted SQL query output of Hydra-net.

I am graduate student,, So my question would be a little-bit bothersome.. I understand :(
Nevertheless, let me begin my question .. !

In your model, I understand that 'Ranking' is very important.
So, I am trying to get "where can I find and get ranking scores ..!?"

My brain says "you can find ranking scores in this line...!"..
SELECT ranking score is 'select_id_prob'

select_id_prob = sorted(enumerate(model_output["column_func"][:, 0]), key=lambda x:x[1], reverse=True)

and WHERE ranking score is 'where_id_prob'

where_id_prob = sorted(enumerate(model_output["column_func"][:, 1]), key=lambda x:x[1], reverse=True)

Is my guess right..!?
It is very basic and elementary question for you. But it will be very helpful for me : )

Thank you for reading me.

Ask about the result

Hello!
I read the paper you published and i also git clone the code .I run the code successfully.But the result I got is far from your result.
[wikidev.jsonl, epoch 4] overall:76.1, agg:89.1, sel:96.3, wn:97.1, wc:92.1, op:98.4, val:91.1.
I find that the where_col result and the where_val is worse than other.
What do you think about these issues?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.