QuestionAnsweringModel not working with GPU - python

I am using QuestionAnsweringModel from SimpleTransformers. When I run my code, and review my processes in Windows Task Manager, python is not using the GPU at all. I have included a code snippet to recreate the problem. Any help is highly appreciated.
import torch
from simpletransformers.question_answering
import QuestionAnsweringModel,QuestionAnsweringArgs
model_type=“bert”
model_name= “bert-base-cased”
model_args = QuestionAnsweringArgs()
train_args = {
'n_best_size':1 ,
‘overwrite_output_dir’: True,
‘show_running_loss’:True,
‘n_gpu’: 3
}
model = QuestionAnsweringModel(model_type,model_name, args=train_args, use_cuda=True)
I have also looked at a topic on the same issue here before. Recommendations were to update pytorch. But I have already done that as well.
Update:
Tried to set the device to CUDA manually with the code below but no luck so far.
model.to(torch.device(“cuda:0” if torch.cuda.is_available() else “cpu”))
Thanks!

Related

Tensorflow Keras logging steps in for loop

I'm running someone else's program so I can't be 100% open about the code.
I'm using load_model from tensorflow.keras.model to load an h5 model, then model.predict(<data>) in a for loop.
I'm also using tqdm to display a progress Bar. Now the problem is that as soon as I get to the loop, my console start printing output every new line:
I tried to block this output:
import absl.logging
import logging
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
absl.logging.set_verbosity(absl.logging.ERROR)
logging.getLogger("tensorflow").setLevel(logging.ERROR)
logging.getLogger("tensorflow").addHandler(logging.NullHandler(logging.ERROR))
But nothing seems to work.
Solved it: according to tensorflow.keras.model documentation, the method predict has attributes verbose and steps: https://www.tensorflow.org/api_docs/python/tf/keras/Model#predict
However, the same documentation suggest to use the __call__ method when used in a loop: model(<data>, training=False) which does not output any steps.

Imorting zero_gradients from torch.autograd.gradcheck

I want to replicate the code here, and I get the following error while running in Google Colab?
ImportError: cannot import name 'zero_gradients' from
'torch.autograd.gradcheck'
(/usr/local/lib/python3.7/dist-packages/torch/autograd/gradcheck.py)
Can someone help me with how to solve this?
This seems like it's using a very old version of PyTorch, the function itself is not available anymore. However, if you look at this commit, you will see the implementation of zero_gradients. What it does is simply zero out the gradient of the input:
def zero_gradients(i):
for t in iter_gradients(i):
t.zero_()
Then zero_gradients(x) should be the same as x.zero_grad(), which is the current API, assuming x is a nn.Module!
Or it's just:
if x.grad is not None:
x.grad.zero_()

Rewriting Tensorflow 1.x code to tensorflow 2.x

I am dealing with a task where I have to create a code using TensorFlow-version2. I have an existing code of the same thing but in TensorFlow version 1.
with tf.variable_scope(name):
self.obs = tf.placeholder(dtype=tf.float32, shape=[None] + list(ob_space.shape), name='obs')
with tf.variable_scope('policy_net'):
layer_1 = tf.layers.dense(inputs=self.obs, units=20, activation=tf.tanh)
layer_2 = tf.layers.dense(inputs=layer_1, units=20, activation=tf.tanh)
layer_3 = tf.layers.dense(inputs=layer_2, units=act_space.n, activation=tf.tanh)
self.act_probs = tf.layers.dense(inputs=layer_3, units=act_space.n, activation=tf.nn.softmax)
I have worked on tf2 directly but I am facing challenges in understanding the given excerpt. Please help me understand it. Also, how can rewrite this code suitable for tf2. kindly suggest or provide me with a doc to do so.
For this, I will be really thankful to you.
Two ways to migrate your TensorFlow 1 code to TensorFlow 2:
Manually migrate low level TensorFlow APIs
Automatically upgrade code to TensorFlow 2

'use_cuda' set to True when cuda is unavailable. Make sure CUDA is available or set use_cuda=False

I am trying to create a Bert model for classifying Turkish Lan. here is my code:
import pandas as pd
import torch
df = pd.read_excel (r'preparedDataNoId.xlsx')
df = df.sample(frac = 1)
from sklearn.model_selection import train_test_split
train_df, test_df = train_test_split(df, test_size=0.10)
print('train shape: ',train_df.shape)
print('test shape: ',test_df.shape)
from simpletransformers.classification import ClassificationModel
# define hyperparameter
train_args ={"reprocess_input_data": True,
"fp16":False,
"num_train_epochs": 4}
# Create a ClassificationModel
model = ClassificationModel(
"bert", "dbmdz/bert-base-turkish-cased",
num_labels=4,
args=train_args
)
I am using Anaconda and Spyder. I think every thing is correct but when I run this I got the following error:
'use_cuda' set to True when cuda is unavailable. Make sure CUDA is available or set use_cuda=False.
how can I fix this exactly?
I ran into the same problem. If you have CUDA available, then set both use_cuda and fp16 to True. If not, then set both to False.
CUDA is a parallel computing platform and programming model developed by Nvidia for general computing on its own GPUs.
If your computer does not have GPU, this error will be thrown to you.
Don't forget to include this parameter
use_cuda= False
This will not affect your result, just take a few more seconds than usual to process.
model = ClassificationModel(
"bert", "dbmdz/bert-base-turkish-cased",
num_labels=4,
args=train_args,
use_cuda=False
)
Adding use_cuda=False will help if GPU is not available
If your GPU is unavailable on your computer. Make sure to check CUDA or try use_cuda=False in args of your model. This error will be throw since CUDA does not exist on your computer.

Tensorrt Plugin and caffe parser in python

I am new to Tensorrt and I am not so familiar with C language also. May I ask if there is any example to import caffe modell(caffeparser) and at the same time to use plugin with python. Plugin library example: "https://docs.nvidia.com/deeplearning/sdk/tensorrt-api/c_api/_nv_infer_plugin_8h_source.html".
I saw an example doing something like the below. Is it necessary to modify the the pluginfactory class? or it has been already done with the python plugin api?
import tensorrt
import tensorrtplugins
from tensorrt.plugins import _nv_infer_plugin_bindings as nvinferplugin
from tensorrt.parsers import caffeparser
plugin_factory = tensorrtplugins.FullyConnectedPluginFactory()
parser = caffeparser.create_caffe_parser()
parser.set_plugin_factory(plugin_factory)
engine = trt.utils.caffe_to_trt_engine(G_LOGGER,
MODEL_PROTOTXT,
CAFFE_MODEL,
1,
1 << 20,
OUTPUT_LAYERS,
trt.infer.DataType.FLOAT,
plugin_factory
)
P.s: I am trying to convert YOLO2 to Tensorrt format. Therefore, some layers(e.g kYOLOREORG and kPRELU) can only be supported by the plugin.
Another way to do so is to add the plugin during constructing the network, by method network.add_plugin_ext() ?However, I am not so sure how to specify the previous layer that is going to be imported later.
Thank you so much for your answer. Your help will be much appreciated!

Categories