tensorflow_macos 速度测试

2020-11-22 11:17:31 +08:00
 sharpy

MBP16 i9-9880h 5500M 8G

#!/usr/bin/env python
# coding: utf-8
import tensorflow.compat.v2 as tf
import tensorflow_datasets as tfds

tf.enable_v2_behavior()

from tensorflow.python.framework.ops import disable_eager_execution

disable_eager_execution()

from tensorflow.python.compiler.mlcompute import mlcompute

mlcompute.set_mlc_device(device_name='cpu')

(ds_train, ds_test), ds_info = tfds.load(
    'mnist',
    split=['train', 'test'],
    shuffle_files=True,
    as_supervised=True,
    with_info=True,
)


def normalize_img(image, label):
    """Normalizes images: `uint8` -> `float32`."""
    return tf.cast(image, tf.float32) / 255., label


ds_train = ds_train.map(
    normalize_img, num_parallel_calls=tf.data.experimental.AUTOTUNE)
ds_train = ds_train.cache()
ds_train = ds_train.shuffle(ds_info.splits['train'].num_examples)
ds_train = ds_train.batch(128)
ds_train = ds_train.prefetch(tf.data.experimental.AUTOTUNE)

ds_test = ds_test.map(
    normalize_img, num_parallel_calls=tf.data.experimental.AUTOTUNE)
ds_test = ds_test.batch(128)
ds_test = ds_test.cache()
ds_test = ds_test.prefetch(tf.data.experimental.AUTOTUNE)

model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28, 1)),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(
    loss='sparse_categorical_crossentropy',
    optimizer=tf.keras.optimizers.Adam(0.001),
    metrics=['accuracy'],
)

model.fit(
    ds_train,
    epochs=10,
)

GPU 速度

Epoch 1/10 469/469 [==============================] - 10s 14ms/step - batch: 234.0000 - size: 1.0000 - loss: 0.3598 - accuracy: 0.9028

Epoch 2/10 469/469 [==============================] - 9s 14ms/step - batch: 234.0000 - size: 1.0000 - loss: 0.1623 - accuracy: 0.9535

Epoch 3/10 469/469 [==============================] - 9s 14ms/step - batch: 234.0000 - size: 1.0000 - loss: 0.1182 - accuracy: 0.9664

Epoch 4/10 469/469 [==============================] - 9s 14ms/step - batch: 234.0000 - size: 1.0000 - loss: 0.0911 - accuracy: 0.9735

Epoch 5/10 469/469 [==============================] - 9s 14ms/step - batch: 234.0000 - size: 1.0000 - loss: 0.0732 - accuracy: 0.9786

CPU 速度

Epoch 1/10 469/469 [==============================] - 3s 1ms/step - batch: 234.0000 - size: 1.0000 - loss: nan - accuracy: 0.0987

Epoch 2/10 469/469 [==============================] - 3s 1ms/step - batch: 234.0000 - size: 1.0000 - loss: nan - accuracy: 0.0987

Epoch 3/10 469/469 [==============================] - 3s 1ms/step - batch: 234.0000 - size: 1.0000 - loss: nan - accuracy: 0.0987

Epoch 4/10 469/469 [==============================] - 3s 1ms/step - batch: 234.0000 - size: 1.0000 - loss: nan - accuracy: 0.0987

Epoch 5/10 469/469 [==============================] - 3s 1ms/step - batch: 234.0000 - size: 1.0000 - loss: nan - accuracy: 0.0987

2251 次点击
所在节点    Apple
6 条回复
tzm41
2020-11-22 11:33:59 +08:00
浅窄的 dense net,GPU 没啥加速效果吧…
RichardSun
2020-11-22 11:50:59 +08:00
想起之前我试过一个好像叫 plaidML 的 backend,随便跑了下试试 GPU 模式比普通 backend 的 CPU 都慢🤦🏻‍♂️
ZRS
2020-11-22 17:57:12 +08:00
试试 resnet50
tianshilei1992
2020-11-22 20:57:40 +08:00
我一直想写一个 Metal 的 OpenMP offloading plugin,但是 Metal compiler 没开源,我搞不定 CodeGen…
sharpy
2020-11-23 10:33:55 +08:00
@tianshilei1992 #4 你可以看看 https://github.com/a2flo/floor.git 这个项目,也许有点儿启发,这个项目修改了 clang 的源码,使之能生成各个后端代码,看说明是“compiles compute/graphics C++ code to CUDA/PTX, Metal/AIR, OpenCL/SPIR/SPIR-V, Vulkan/SPIR-V code/binaries ”
tianshilei1992
2020-11-23 11:00:39 +08:00
@sharpy 👍 感谢!粗看了一下代码,发现 Metal 的 AIR 竟然就是从 SPIR-V 魔改的…除了 data layout 有些不一样之外…

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/727999

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX