Simpletransformers: Runtime error from "minimal start" script

Created on 13 May 2020  Â·  10Comments  Â·  Source: ThilinaRajapakse/simpletransformers

Hello,

I'm attempting to run the minimal start example on https://simpletransformers.ai/docs/multi-class-classification/, modified to be CPU-only:

# Create a ClassificationModel
model = ClassificationModel( "bert", "bert-base-cased", num_labels=3, args=model_args, use_cuda=False )

I installed simple transformers as described here https://simpletransformers.ai/docs/installation/, opting for the CPU-only installation (which means I did not install "Apex").

Running this, I get thousands of the following section logged to console. It does not exit, but I have haad it running for ~half an hour now and it just keeps on emitting the following errors:

INFO:simpletransformers.classification.classification_model: Converting to features started. Cache is not used.
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/jkearl/anaconda3/envs/transformers/lib/python3.8/multiprocessing/spawn.py", line 116, in spawn_main
    exitcode = _main(fd, parent_sentinel)
  File "/Users/jkearl/anaconda3/envs/transformers/lib/python3.8/multiprocessing/spawn.py", line 125, in _main
    prepare(preparation_data)
  File "/Users/jkearl/anaconda3/envs/transformers/lib/python3.8/multiprocessing/spawn.py", line 236, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "/Users/jkearl/anaconda3/envs/transformers/lib/python3.8/multiprocessing/spawn.py", line 287, in _fixup_main_from_path
    main_content = runpy.run_path(main_path,
  File "/Users/jkearl/anaconda3/envs/transformers/lib/python3.8/runpy.py", line 263, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "/Users/jkearl/anaconda3/envs/transformers/lib/python3.8/runpy.py", line 96, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "/Users/jkearl/anaconda3/envs/transformers/lib/python3.8/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/Users/jkearl/Desktop/test.py", line 39, in <module>
    model.train_model(train_df)
  File "/Users/jkearl/anaconda3/envs/transformers/lib/python3.8/site-packages/simpletransformers/classification/classification_model.py", line 258, in train_model
    train_dataset = self.load_and_cache_examples(train_examples, verbose=verbose)
  File "/Users/jkearl/anaconda3/envs/transformers/lib/python3.8/site-packages/simpletransformers/classification/classification_model.py", line 803, in load_and_cache_examples
    features = convert_examples_to_features(
  File "/Users/jkearl/anaconda3/envs/transformers/lib/python3.8/site-packages/simpletransformers/classification/classification_utils.py", line 364, in convert_examples_to_features
    with Pool(process_count) as p:
  File "/Users/jkearl/anaconda3/envs/transformers/lib/python3.8/multiprocessing/context.py", line 119, in Pool
    return Pool(processes, initializer, initargs, maxtasksperchild,
  File "/Users/jkearl/anaconda3/envs/transformers/lib/python3.8/multiprocessing/pool.py", line 212, in __init__
    self._repopulate_pool()
  File "/Users/jkearl/anaconda3/envs/transformers/lib/python3.8/multiprocessing/pool.py", line 303, in _repopulate_pool
    return self._repopulate_pool_static(self._ctx, self.Process,
  File "/Users/jkearl/anaconda3/envs/transformers/lib/python3.8/multiprocessing/pool.py", line 326, in _repopulate_pool_static
    w.start()
  File "/Users/jkearl/anaconda3/envs/transformers/lib/python3.8/multiprocessing/process.py", line 121, in start
    self._popen = self._Popen(self)
  File "/Users/jkearl/anaconda3/envs/transformers/lib/python3.8/multiprocessing/context.py", line 283, in _Popen
    return Popen(process_obj)
  File "/Users/jkearl/anaconda3/envs/transformers/lib/python3.8/multiprocessing/popen_spawn_posix.py", line 32, in __init__
    super().__init__(process_obj)
  File "/Users/jkearl/anaconda3/envs/transformers/lib/python3.8/multiprocessing/popen_fork.py", line 19, in __init__
    self._launch(process_obj)
  File "/Users/jkearl/anaconda3/envs/transformers/lib/python3.8/multiprocessing/popen_spawn_posix.py", line 42, in _launch
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "/Users/jkearl/anaconda3/envs/transformers/lib/python3.8/multiprocessing/spawn.py", line 154, in get_preparation_data
    _check_not_importing_main()
  File "/Users/jkearl/anaconda3/envs/transformers/lib/python3.8/multiprocessing/spawn.py", line 134, in _check_not_importing_main
    raise RuntimeError('''
RuntimeError: 
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:

            if __name__ == '__main__':
                freeze_support()
                ...

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.

I'm on macOS Moajve, running with latest anaconda.

stale

Most helpful comment

conda install python=3.7
pip install simpletransformers

fixed it, thanks.

All 10 comments

I also tried setting

model_args = {"num_train_epochs": 1, "fp16": False}

to no avail.

This is how the logs start:

(transformers) ➜  Desktop python3 test.py            
INFO:simpletransformers.classification.classification_model: Converting to features started. Cache is not used.
  0%|                                                                                                                                                         | 0/3 [00:00<?, ?it/s]INFO:simpletransformers.classification.classification_model: Converting to features started. Cache is not used.
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/jkearl/anaconda3/envs/transformers/lib/python3.8/multiprocessing/spawn.py", line 116, in spawn_main
    exitcode = _main(fd, parent_sentinel)
  File "/Users/jkearl/anaconda3/envs/transformers/lib/python3.8/multiprocessing/spawn.py", line 125, in _main
    prepare(preparation_data)
  File "/Users/jkearl/anaconda3/envs/transformers/lib/python3.8/multiprocessing/spawn.py", line 236, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "/Users/jkearl/anaconda3/envs/transformers/lib/python3.8/multiprocessing/spawn.py", line 287, in _fixup_main_from_path
    main_content = runpy.run_path(main_path,
  File "/Users/jkearl/anaconda3/envs/transformers/lib/python3.8/runpy.py", line 263, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "/Users/jkearl/anaconda3/envs/transformers/lib/python3.8/runpy.py", line 96, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "/Users/jkearl/anaconda3/envs/transformers/lib/python3.8/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/Users/jkearl/Desktop/test.py", line 37, in <module>
    model.train_model(train_df)
  File "/Users/jkearl/anaconda3/envs/transformers/lib/python3.8/site-packages/simpletransformers/classification/classification_model.py", line 258, in train_model
    train_dataset = self.load_and_cache_examples(train_examples, verbose=verbose)
  File "/Users/jkearl/anaconda3/envs/transformers/lib/python3.8/site-packages/simpletransformers/classification/classification_model.py", line 803, in load_and_cache_examples
    features = convert_examples_to_features(
  File "/Users/jkearl/anaconda3/envs/transformers/lib/python3.8/site-packages/simpletransformers/classification/classification_utils.py", line 364, in convert_examples_to_features
    with Pool(process_count) as p:
  File "/Users/jkearl/anaconda3/envs/transformers/lib/python3.8/multiprocessing/context.py", line 119, in Pool
    return Pool(processes, initializer, initargs, maxtasksperchild,
  File "/Users/jkearl/anaconda3/envs/transformers/lib/python3.8/multiprocessing/pool.py", line 212, in __init__
    self._repopulate_pool()
  File "/Users/jkearl/anaconda3/envs/transformers/lib/python3.8/multiprocessing/pool.py", line 303, in _repopulate_pool
    return self._repopulate_pool_static(self._ctx, self.Process,
  File "/Users/jkearl/anaconda3/envs/transformers/lib/python3.8/multiprocessing/pool.py", line 326, in _repopulate_pool_static
    w.start()
  File "/Users/jkearl/anaconda3/envs/transformers/lib/python3.8/multiprocessing/process.py", line 121, in start
    self._popen = self._Popen(self)
  File "/Users/jkearl/anaconda3/envs/transformers/lib/python3.8/multiprocessing/context.py", line 283, in _Popen
    return Popen(process_obj)
  File "/Users/jkearl/anaconda3/envs/transformers/lib/python3.8/multiprocessing/popen_spawn_posix.py", line 32, in __init__
    super().__init__(process_obj)
  File "/Users/jkearl/anaconda3/envs/transformers/lib/python3.8/multiprocessing/popen_fork.py", line 19, in __init__
    self._launch(process_obj)
  File "/Users/jkearl/anaconda3/envs/transformers/lib/python3.8/multiprocessing/popen_spawn_posix.py", line 42, in _launch
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "/Users/jkearl/anaconda3/envs/transformers/lib/python3.8/multiprocessing/spawn.py", line 154, in get_preparation_data
    _check_not_importing_main()
  File "/Users/jkearl/anaconda3/envs/transformers/lib/python3.8/multiprocessing/spawn.py", line 134, in _check_not_importing_main
    raise RuntimeError('''
RuntimeError: 
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:

            if __name__ == '__main__':
                freeze_support()
                ...

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.

May be a python version problem, try python3.7

conda install python=3.7
pip install simpletransformers

fixed it, thanks.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

i tried python3.7 still face this issue.

I followed the instruction from documentation, used conda to create a venv called st. And I will run into this problem as well.

What is your OS? If it's not linux, try turning off multiprocessing.

turning off multiprocessing is not working, dataloader.py using multiprocessing heavily

check model_args.py u need to set dataloader_num_workers: int = 0

i solved it by running the entire predict function in a multiprocessing pool.

from multiprocessing import set_start_method
from multiprocessing import Process, Manager
try:
    set_start_method('spawn')
except RuntimeError:
    pass
@app.get("/article_classify")
def classification(text:str):
    """function to classify article using a deep learning model.
    Returns:
        [type]: [description]
    """
    manager = Manager()

    return_result = manager.dict()
    # as the inference is failing 
    p = Process(target = inference,args=(text,return_result,))
    p.start()
    p.join()
    # print(return_result)
    result = return_result['all_tags']
    return result
Was this page helpful?
0 / 5 - 0 ratings