tensorflow 当data_format = "NDHWC"时,ConvertFusedBatchNorm返回未初始化的值,

tgabmvqs  于 10个月前  发布在  其他
关注(0)|答案(5)|浏览(93)

问题类型

Bug

你是否在TF nightly中复现了这个bug?

是的

来源

source

Tensorflow版本

v1.12.1-88697-g620bee79ab3 2.12.0-dev20230201

自定义代码

OS平台和发行版

Ubuntu 22.04

移动设备

  • 无响应*

Python版本

Python 3.10

Bazel版本

5.3.0

GCC/编译器版本

gcc-11

CUDA/cuDNN版本

CUDA-11.8/cudnn-8.7.0/TensorRT-8.5.3

GPU型号和内存

RTX3090

当前行为?

  1. See code snippet:
  2. https://github.com/tensorflow/tensorflow/blob/4aec415b3f06b19c380d1a0ca92cc2de0d74cc21/tensorflow/compiler/tf2tensorrt/convert/convert_nodes.cc#L4399-L4436
  3. In the case of NDHWC layout (triggered by the code below) an uninitialized value is returned from ConvertFusedBatchNorm which causes an exception to be raised.
  4. I would expect it to build correctly. Changing ConvertFusedBatchNorm to do the same thing for NDHWC as for NHWC gets rid of the crash, but I don't know if this is correct.

独立代码以重现问题

  1. import tensorflow as tf
  2. import numpy as np
  3. from tensorflow.keras.layers import (
  4. BatchNormalization,
  5. Conv3D,
  6. Dense,
  7. Flatten,
  8. Input,
  9. )
  10. from tensorflow.keras.models import Model
  11. from tensorflow.python.compiler.tensorrt import trt_convert as trt
  12. inputs = Input(shape=(24, 24, 64, 1), name="x")
  13. x = inputs
  14. x = Conv3D(16, (3, 3, 3), activation="relu", padding="same")(x)
  15. x = BatchNormalization()(x)
  16. x = Flatten()(x)
  17. x = Dense(128, activation="relu")(x)
  18. x = Dense(128)(x)
  19. m = Model(inputs=[inputs], outputs=[x])
  20. m.compile(
  21. optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),
  22. loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
  23. metrics=[tf.keras.metrics.SparseCategoricalAccuracy()],
  24. )
  25. model_dir = "/tmp/model"
  26. tf.keras.models.save_model(m, model_dir)
  27. converter = trt.TrtGraphConverterV2(input_saved_model_dir=model_dir,
  28. precision_mode=trt.TrtPrecisionMode.FP16)
  29. trt_func = converter.convert()
  30. def input_fn():
  31. a = np.random.rand(1024, 24, 24, 64, 1).astype(np.float32)
  32. yield [a]
  33. converter.build(input_fn=input_fn)

相关日志输出

  1. 2023-02-02 11:32:14.336729: W tensorflow/compiler/tf2tensorrt/kernels/trt_engine_op.cc:1104] TF-TRT Warning: Engine creation for TRTEngineOp_000_000 failed. The native segment will be used instead. Reason: INVALID_ARGUMENT: Rank of perm for transpose does not match with that of the input.
fwzugrvs

fwzugrvs1#

你好,@froody
对于延迟表示歉意,我认为你正在尝试将数据格式为 NHWC 的输入Tensor转换为 NDHWC ,所以我不确定是否可以做到这一点,但我认为输入Tensor的数据格式应该等于输出Tensor的数据格式,因此 NHWC 转换为 NCHW ,NDHWC 转换为 NCDHW ,甚至在日志输出中也清楚地显示为 INVALID_ARGUMENT: Rank of perm for transpose does not match with that of the input.
你可以参考这个 official documentation 的源代码,以下是一些参考资料 Ref-1Ref-2Ref-3 ,它们可能有助于解决你的问题。
每个字母的意义可能有助于理解:

  1. N: number of images in the batch
  2. H: height of the image
  3. W: width of the image
  4. C: number of channels of the image (ex: 3 for RGB, 1 for grayscale)
  5. D: Depth

请告诉我是否遗漏了任何内容?谢谢!

os8fio9y

os8fio9y2#

请查看我提供的示例代码。您认为我在第几行创建了一个NHWCTensor?还是要求从NHWC转换为NDHWC?

我想说的是,ConvertFusedBatchNormconvert_nodes.cc 中存在一个错误。代码看起来像这样:

ITensorProxyPtr output_tensor;

if (data_format == "NCHW") {
...
}
if (data_format == "NHWC") {
...
}
params->outputs->push_back(TRT_TensorOrWeights(output_tensor));

如果您对这段代码进行插桩,打印出 data_format 的值,然后运行上面提供的示例Python代码,您会发现在某个时刻 data_format = "NDHWC" 出现,导致代码跳过两个转换块,并将未初始化的 output_tensor 值赋给 params->outputs

如果您对这段代码进行插桩,打印出 order_sizedims.nbDims 在它们不相等的情况下的值,您应该会看到 order_size = 4dims.nbDims 具有一些不合理的巨大值,略小于 0xffff,这是由于上述代码段引入的未初始化值导致的。

展开查看全部
inn6fuwd

inn6fuwd3#

你好,@sachinprasadhs
请问您能调查一下这个问题吗?谢谢!

mm9b1k5b

mm9b1k5b4#

@froody,你提到的最近对上述功能的提交(1377c6e)是否对你有任何帮助?

hmtdttj4

hmtdttj45#

这段文本可以翻译为:

"在最近的提交中,查看主分支,我仍然看到以下格式的代码,这使得当 $x_1^m_0n_1^x$ 和 $x_1^a_0b_1^x$ 时,输出Tensor未初始化。"

相关问题