tensorflow / tensorflow/models

Potential bug in RelativePositionEmbedding

Open
#10,945 2 comments 0 reactions 1 assignee View on GitHub

Nobody has claimed this yet.

models:official stat:awaiting model gardener type:bug
Dominant language
Python
Stars
77.7k
Forks
44.8k
PR merge metrics
No merged PRs in 30d

Description

Prerequisites

Please answer the following questions for yourself before submitting an issue.

  • I am using the latest TensorFlow Model Garden release and TensorFlow 2.
  • I am reporting the issue to the correct repository. (Model Garden official or research directory)
  • I checked to make sure that this issue has not been filed already.

1. The entire URL of the file you are using

https://github.com/tensorflow/models/blob/master/official/nlp/modeling/layers/position_embedding.py

2. Describe the bug

https://github.com/tensorflow/models/blob/db50116d3f2502a96a86cb1ee34f75008bfb467c/official/nlp/modeling/layers/position_embedding.py#L161-L174

Why do we have this in line 167?

    inv_timescales = min_timescale * tf.exp(
        tf.cast(tf.range(num_timescales), tf.float32) *
        -log_timescale_increment)

The problem is that we multiply min_timescale instead of 1 / min_timescale to obtain inv_timescales.
In this way, the final values before sin/cos function we have are:

pos * T_min * [T_r ** 0, T_r ** ( -1/dim_range), T_r ** ( -2/dim_range), ..., T_r ** ( -dim_range/dim_range)]

where T_r = T_max / T_min and dim_range = num_timescales - 1.
Notably, the largest timestep corresponds to an effective inverse timescale T_max / (T_min * T_min) and the smallest timestep uses 1 / T_min.

3. Steps to reproduce

This is more like a math issue. No code execution is needed.

4. Expected behavior

I think the correct thing to have is:

pos / T_min * [T_r ** 0, T_r ** ( -1/dim_range), T_r ** ( -2/dim_range), ..., T_r ** ( -dim_range/dim_range)]

where the largest timestep corresponds to an inverse timescale T_max and the smallest timestep uses T_min.

These two implementations have no difference when T_min=1.0. The test function at https://github.com/tensorflow/models/blob/db50116d3f2502a96a86cb1ee34f75008bfb467c/official/nlp/modeling/layers/position_embedding_test.py#L164 is probabily not very helpful then.

5. Additional context

This is more like a math issue. No code execution is needed.

6. System information

This is more like a math issue. No code execution is needed.

  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04):
  • Mobile device name if the issue happens on a mobile device:
  • TensorFlow installed from (source or binary):
  • TensorFlow version (use command below):
  • Python version:
  • Bazel version (if compiling from source):
  • GCC/Compiler version (if compiling from source):
  • CUDA/cuDNN version:
  • GPU model and memory:

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.