【问题标题】:Python gpt-2-simple, load multiple models at oncePython gpt-2-simple,一次加载多个模型
【发布时间】:2020-05-16 18:10:06
【问题描述】:

我正在开发一个不和谐的机器人,我想要实现的功能之一是用 gpt-2-simple 库生成的文本来响应。我希望加载不止一个模型,以便有多个模型可用于响应来自我的用户的消息。

但是当我在第二个模型中运行load_gpt2() 函数时出现以下错误

File "main.py", line 22, in <module>
    main()
  File "main.py", line 16, in main
    text_events.register_Message(client)
  File "U:\discord_bot\text_events\__init__.py", line 19, in register_Message
    event.init()
  File "U:\discord_bot\text_events\model2.py", line 20, in init
    gpt2.load_gpt2(sess, run_name='model2', checkpoint_dir="characters")
  File "C:\Program Files\Python36\lib\site-packages\gpt_2_simple\gpt_2.py", line 389, in load_gpt2
    output = model.model(hparams=hparams, X=context, gpus=gpus)
  File "C:\Program Files\Python36\lib\site-packages\gpt_2_simple\src\model.py", line 183, in model
    initializer=tf.compat.v1.random_normal_initializer(stddev=0.01))
  File "C:\Program Files\Python36\lib\site-packages\tensorflow_core\python\ops\variable_scope.py", line 1500, in get_variable
    aggregation=aggregation)
  File "C:\Program Files\Python36\lib\site-packages\tensorflow_core\python\ops\variable_scope.py", line 1243, in get_variable
    aggregation=aggregation)
  File "C:\Program Files\Python36\lib\site-packages\tensorflow_core\python\ops\variable_scope.py", line 567, in get_variable
    aggregation=aggregation)
  File "C:\Program Files\Python36\lib\site-packages\tensorflow_core\python\ops\variable_scope.py", line 519, in _true_getter
    aggregation=aggregation)
  File "C:\Program Files\Python36\lib\site-packages\tensorflow_core\python\ops\variable_scope.py", line 868, in _get_single_variable
    (err_msg, "".join(traceback.format_list(tb))))
ValueError: Variable model/wpe already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined at:

  File "C:\Program Files\Python36\lib\site-packages\tensorflow_core\python\framework\ops.py", line 1748, in __init__
    self._traceback = tf_stack.extract_stack()
  File "C:\Program Files\Python36\lib\site-packages\tensorflow_core\python\framework\ops.py", line 3426, in _create_op_internal
    op_def=op_def)
  File "C:\Program Files\Python36\lib\site-packages\tensorflow_core\python\framework\ops.py", line 3357, in create_op
    attrs, op_def, compute_device)
  File "C:\Program Files\Python36\lib\site-packages\tensorflow_core\python\util\deprecation.py", line 507, in new_func
    return func(*args, **kwargs)
  File "C:\Program Files\Python36\lib\site-packages\tensorflow_core\python\framework\op_def_library.py", line 794, in _apply_op_helper
    op_def=op_def)

我试图找到一种方法让 gpt2 实例在模块之间保持分离,但我找不到任何可以实现这种沙盒效果的方法,或者任何其他用于分离模型或其实例的建议。有人有什么想法吗?

【问题讨论】:

    标签: python python-3.x tensorflow gpt-2


    【解决方案1】:

    如果您只想单独运行同一模型的多个实例,那么您可以使用 docker 来实现。

    【讨论】:

      【解决方案2】:

      您是否尝试过ProcessPoolExecutor 来隔离执行?

      from concurrent import futures
      
      def do_stuff(arg)
         # load gpt module here
         return "response for ({})".format(arg)
      
      
      with futures.ProcessPoolExecutor(max_workers=1) as exec:
         future = exec.submit(do_stuff, 1)
         for f in futures.as_completed([future]):
             return_val = f.result()
      

      【讨论】:

        【解决方案3】:

        根据@Kedar 的建议,您可以使用单独的 Python 进程来隔离执行并分别在每个进程中加载​​模型。或者,您可以使用单例模式确保一次只加载模型的一个实例,或者更简单地,将lru_cache 装饰器(https://docs.python.org/3/library/functools.html)添加到load_gpt2 函数的包装器中。示例:

        from functools import lru_cache
        
        @lru_cache
        def load_gpt2_wrapper(*args, **kwargs):
            # do some logic and return the model
        
        

        这样,每次调用load_gpt2_wrapper都会返回相同的模型,假设提供了相同的参数。

        或者,在该包装函数中,每次调用tf.reset_default_graph(),类似于https://github.com/minimaxir/gpt-2-simple/issues/80 中提出的建议。

        总的来说,我认为最好的解决方案是在 GPT-2 存储库中创建 Tensorflow 会话时提供提供reuse 的选项,我在这里做了:https://github.com/minimaxir/gpt-2-simple/pull/272。在您的情况下,由于看起来您正在单独创建会话并将其提供给 load_gpt2,因此您可以显式提供 reuse 选项:

        sess = tf.compat.v1.Session(reuse=reuse, ...)
        model = load_gpt2(sess, ...)
        

        假设您可以为您的应用程序保持一个会话运行,这应该可以缓解该问题。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-03-18
          • 1970-01-01
          • 2020-10-21
          • 2018-08-16
          • 2013-10-15
          • 2020-10-29
          • 2020-01-06
          • 2011-12-31
          相关资源
          最近更新 更多