在 IPU 上移植预训练的 keras 模型和 运行

Porting pre-trained keras models and run them on IPU

我正在尝试将两个预训练的 keras 模型移植到 IPU 机器中。我设法使用 IPUstrategy.scope 加载并 运行 它们,但我不知道我是否以正确的方式进行。我有 .h5 文件格式的预训练模型。 我这样加载它们:

def first_model():
    model = tf.keras.models.load_model("./model1.h5")
return model 

搜索您的 ipu.keras.models.py 文件后,我找不到任何加载方法来加载我的预训练模型,这就是我使用 tf.keras.models.load_model() 的原因。

然后我用这段代码 运行:

cfg=ipu.utils.create_ipu_config()
cfg=ipu.utils.auto_select_ipus(cfg, 1)
ipu.utils.configure_ipu_system(cfg)
ipu.utils.move_variable_initialization_to_cpu()

strategy = ipu.ipu_strategy.IPUStrategy()
with strategy.scope():
    model = first_model()

    print('compile attempt\n')
    model.compile("sgd", "categorical_crossentropy", metrics=["accuracy"])
    print('compilation completed\n')
    
    print('running attempt\n')
    res = model.predict(input_img)[0]
    print('run completed\n')

你可以在这里看到输出:link

所以我很难理解系统如何以及是否正常工作。 基本上 model.compile 不会编译我的模型,但是当我使用 model.predict 时,系统首先编译然后是 运行ning。为什么会这样?还有其他方法可以在 IPU 芯片上 运行 预训练 keras 模型吗?

我的另一个问题是,是否可以在 ipu.keras.model 中加载预训练的 keras 模型,然后使用模型。fit/evaluate 进一步训练和评估它,然后保存以备将来使用使用?

我的最后一个问题是关于图表的编译部分。每次我在不同的 strategy.scope() 中使用 model.predict() 时,有没有办法避免重新编译图形?

我用的是tensorflow2.1.2 wheel

感谢您的宝贵时间

为了添加一些上下文,Graphcore TensorFlow wheel 包括一个用于 IPU 的 Keras 端口,可用 tensorflow.python.ipu.keras。您可以在 this link. This module contains IPU-specific optimised replacement for TensorFlow Keras classes ModelSequential 访问 IPU Keras 的 API 文档,以及更多高性能、多 IPU 类,例如PipelineModelPipelineSequential.

根据您的具体问题,当您提到目前没有特定于 IPU 的方法来加载预训练的 Keras 模型时,您是对的。我鼓励您联系 Graphcore 支持,因为您似乎可以访问 IPU。执行此操作时,请附上您的预训练 Keras 模型 model1.h5 和您的代码的独立复制器。

转移话题到重编译问题:使用可执行缓存防止重编译,你可以用环境变量TF_POPLAR_FLAGS='--executable_cache_path=./cache'设置它。我还建议您查看以下资源:

  • tutorial 收集了有关重新编译的几个注意事项以及在 IPU 上使用 TensorFlow2 时如何避免它。
  • Graphcore TensorFlow 文档here 解释了如何在 IPU 上使用预编译模式。