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

Adapt to CPU-only usage #1

Open
wants to merge 3 commits into
base: main
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
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ pip install transformers==3.3.1
# pip install happytransformer -U
pip install --editable happy-transformer
pip install tensorboardX
mkdir pred_results

# Optional:
# Install apex following https://github.com/NVIDIA/apex#linux
Expand All @@ -39,13 +38,16 @@ pip install -v --no-cache-dir --global-option="--cpp_ext" --global-option="--cud

For masked language models:
```bash
# folder for your personal results
mkdir pred_results

python src/mlm_predict.py bert-base \
data/test.core.masked.txt \
results/bert-base.test.core.output.jsonl
pred_results/bert-base.test.core.output.jsonl

python src/mlm_predict.py bert-base \
data/test.all.masked.txt \
results/bert-base.test.all.output.jsonl
pred_results/bert-base.test.all.output.jsonl
```

Note that `bert-base` can be replaced by any model name in `[bert-base, bert-large, roberta-base, roberta-large]`.
Expand All @@ -54,7 +56,7 @@ For left-to-right language models:
```bash
python src/gpt_predict.py gpt \
data/test.core.masked.txt \
results/gpt.test.core.output.jsonl
pred_results/gpt.test.core.output.jsonl
```

### Fine-tune a MLM model
Expand All @@ -77,10 +79,10 @@ CUDA_VISIBLE_DEVICES=0 python src/finetune_mlm.py \
```

```bash
python src/mlm_infer.py \
python src/mlm_predict.py \
reload_bert:saved_models/finetuned_bert_large \
data/test.core.masked.txt \
results/test.core.output.jsonl
pred_results/bert-large-finetuned.test.core.output.jsonl
```

## Evaluation on Validation Set
Expand Down
6 changes: 3 additions & 3 deletions happy-transformer/happytransformer/happy_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def predict_mask(self, text: str, options=None, num_results=1):
if self.mlm is None:
self._get_masked_language_model()

if self.gpu_support:
if self.gpu_support == "cuda":
self.mlm.to("cuda")

if self.model_name in self.tag_one_transformers:
Expand Down Expand Up @@ -251,15 +251,15 @@ def _get_prediction_softmax(self, text):
# Convert inputs to PyTorch tensors
tokens_tensor = torch.tensor([indexed_tokens])

if self.gpu_support:
if self.gpu_support == "cuda":
tokens_tensor = tokens_tensor.to('cuda')

with torch.no_grad():

if self.model_name != "ROBERTA":
segments_ids = self._get_segment_ids(text)
segments_tensors = torch.tensor([segments_ids])
if self.gpu_support:
if self.gpu_support == "cuda":
segments_tensors = segments_tensors.to('cuda')
outputs = self.mlm(tokens_tensor, token_type_ids=segments_tensors)
else:
Expand Down
2 changes: 1 addition & 1 deletion src/gpt_predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
model_str = sys.argv[1]
input_filename = sys.argv[2]
output_filename = sys.argv[3]
cuda = True
cuda = torch.cuda.is_available() # TRUE if NVIDIA GPU is available
model = None
if model_str.startswith("reload_"):
if model_str.startswith("reload_bert"):
Expand Down