tensorflow XLA推理失败,关于分支形状不匹配的complaning,

dauxcl2d  于 10个月前  发布在  其他
关注(0)|答案(4)|浏览(84)

问题类型

Bug

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

是的

来源

二进制文件

Tensorflow版本

2.10.0

自定义代码

是的

OS平台和发行版

Colab

移动设备

无响应

Python版本

无响应

Bazel版本

无响应

GCC/编译器版本

无响应

CUDA/cuDNN版本

无响应

GPU型号和内存大小

A100 40 GB

当前行为?

  1. I have the following function.
  2. First, I initialize a CLIP-based text encoder:
  3. from keras_cv.models.stable_diffusion.text_encoder import TextEncoder
  4. MAX_PROMPT_LENGTH = 77
  5. text_encoder = TextEncoder(MAX_PROMPT_LENGTH)
  6. Then, I am using the `text_encoder` like so in a function that I use to serialize the `text_encoder` as a `SavedModel`:
  7. ```python
  8. from keras_cv.models.stable_diffusion.constants import _UNCONDITIONAL_TOKENS
  9. import tensorflow as tf
  10. signature_dict = {
  11. "tokens": tf.TensorSpec(shape=[None, 77], dtype=tf.int32, name="tokens"),
  12. }
  13. def text_encoder_exporter(model: tf.keras.Model):
  14. BATCH_SIZE = 3
  15. MAX_PROMPT_LENGTH = 77
  16. POS_IDS = tf.convert_to_tensor([list(range(MAX_PROMPT_LENGTH))], dtype=tf.int32)
  17. UNCONDITIONAL_TOKENS = tf.convert_to_tensor([_UNCONDITIONAL_TOKENS], dtype=tf.int32)
  18. @tf.function(input_signature=[signature_dict])
  19. def serving_fn(inputs):
  20. # context
  21. encoded_text = model([inputs["tokens"], POS_IDS], training=False)
  22. encoded_text = tf.squeeze(encoded_text)
  23. if tf.rank(encoded_text) == 2:
  24. encoded_text = tf.repeat(
  25. tf.expand_dims(encoded_text, axis=0), BATCH_SIZE, axis=0
  26. )
  27. # unconditional context
  28. unconditional_context = model([UNCONDITIONAL_TOKENS, POS_IDS], training=False)
  29. unconditional_context = tf.repeat(unconditional_context, BATCH_SIZE, axis=0)
  30. return {"context": encoded_text, "unconditional_context": unconditional_context}
  31. return serving_fn

序列化:

  1. tf.saved_model.save(
  2. text_encoder,
  3. "./text_encoder/1/",
  4. signatures={"serving_default": text_encoder_exporter(text_encoder)},
  5. )

现在,在尝试进行XLA编译时:

  1. from tensorflow.python.saved_model import tag_constants
  2. batch_size = 3
  3. saved_model_loaded = tf.saved_model.load(
  4. "./text_encoder/1/", tags=[tag_constants.SERVING]
  5. )
  6. text_encoder_predict_fn = saved_model_loaded.signatures["serving_default"]
  7. # Raises error
  8. xla_text_encoder_predict_fn = tf.function(text_encoder_predict_fn, jit_compile=True)
  9. xla_text_encoder_predict_fn(
  10. tokens=tf.ones((batch_size, MAX_PROMPT_LENGTH), tf.int32)
  11. ).keys()
  1. ### Standalone code to reproduce the issue
  2. ```shell
  3. https://colab.research.google.com/gist/sayakpaul/d7dafc252752a6c1ce10e85d8162b8ea/scratchpad.ipynb
ej83mcc0

ej83mcc01#

@gaikwadrahul8
I was able to reproduce the issue on Colab using TF v2.11. Please find the gist here for reference.
Thank you !

eoigrqb6

eoigrqb62#

你好,@sayakpaul

对于延迟道歉,我能够复制问题而没有任何错误,并且似乎运行正常。供你参考,我已经添加了 gist-file 并替换了 jit_compile=Truetf.config.optimizer.set_jit(True)tf.config.optimizer.set_jit('autoclustering'),如gist文件所示,你可以参考官方文档 Ref-1、Ref-2 和 stack-overflow answer

如果问题仍然存在,请告诉我们?或者你能确认这个问题是否已经解决了吗?如果问题已经解决,请随时关闭问题。谢谢!

dgtucam1

dgtucam13#

与@gaikwadrahul8无关。您引用的示例指的是培训,而我的问题与编译一个现成的函数(在这个例子中涉及到一个模型)有关。

  1. tf.config.optimizer.set_jit(True) or tf.config.optimizer.set_jit('autoclustering')

当优化过程中涉及到XLA编译时,会应用这两个选项,但这里不是这种情况。

tyu7yeag

tyu7yeag4#

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

相关问题