Fix precision loss in TBE test offset construction#5430
Closed
spcyppt wants to merge 1 commit intopytorch:mainfrom
Closed
Fix precision loss in TBE test offset construction#5430spcyppt wants to merge 1 commit intopytorch:mainfrom
spcyppt wants to merge 1 commit intopytorch:mainfrom
Conversation
Contributor
d817d41 to
b3fa4bc
Compare
spcyppt
added a commit
to spcyppt/FBGEMM
that referenced
this pull request
Feb 26, 2026
Summary: X-link: facebookresearch/FBGEMM#2402 # TLDR; TBE unit tests constructs offsets via ``` torch.tensor([0] + np.cumsum(lengths).tolist()).long() ``` in which `torch.tensor()` infers `torch.float32`. Float32 can only represent consecutive integers up to 2^24 (16,777,216); beyond that, odd values round to even, producing wrong offsets. The subsequent `.long()` preserves the already-corrupted values. So for large data where offsets values are supposed to be larger than 16,777,216 can cause offsets to be wrong. This also affects backward_adagrad_large dim test. The diff - corrects dtype such that there's no precision loss due to conversion. - re-enables backward_adagrad_large_dim test. ---- # Problem We see TBE forward output mismatch errors in unit tests when we set up large T (number of features) and large B (batch size). We can reproduce this error using the test `test_backward_adagrad_with_simple_tbe_op` where - T = 230, B = 294,440 - T = 57, B = 294,440 (just enough to cause error) - T = 230, B = 184320 # Root cause After debugging, the root cause was due to the test indices construction and not from the kernels. - `np.ones() * L` produces a float64 numpy array - `.tolist()` yields Python floats (float64) - `torch.tensor()` infers `torch.float32` (the default dtype) — narrowing 64-bit doubles to 32-bit floats. Float32 can only represent consecutive integers up to 2^24 (16,777,216); beyond that, odd values round to even, producing wrong offsets. The subsequent `.long()` preserves the already-corrupted values. This caused (T=230, B=184320, total_B=42M) to fail: the kernel received wrong offsets for b_t >= 2^24, computing wrong bag lengths (e.g., L=0 or L=2 instead of 1) and looking up wrong embedding rows. Both v1 and v2 TBE forward kernels were affected identically since they share the same offsets tensor. Based on the example above: ``` Offsets for Table 56 (partially below 2^24, and partially above 2^24): 16486400.0 ... 16780796.0 16780798.0 16780800.0 ^^^^^^^^^ odd values missing (L becomes 2, as opposed to 1) Offsets for Table 57 (fully above 2^24) 16780800.0 16780800.0 16780802.0 ... ^^^^^^^^^ should be 16780801, duplicated (L becomes 0, where 1 is expected) Offsets for Table 113 (above 2^25 -- step size increases to 4): 33267200.0 33267200.0 33267202.0 33267204.0 33267204.0 33267204.0 33267206.0 33267208.0 33267208.0 33267208.0 33561592.0 33561592.0 33561592.0 33561592.0 33561592.0 33561596.0 33561596.0 33561596.0 33561600.0 33561600.0 ^^^^^^^^^ 3 identical values (more L=0) Offsets for Table 229 (near 2^26 -- step size is 4, massive gaps): 67417600.0 67417600.0 67417600.0 67417600.0 67417600.0 67417608.0 67417608.0 67417608.0 67417608.0 67417608.0 67711992.0 67711992.0 67711992.0 67711992.0 67711992.0 67711992.0 67712000.0 67712000.0 67712000.0 67712000.0 ^^^^^^^^^ 5 identical values (more L=0) ``` # Fix The fix ensure `np.one` to use `int` and specifies `dtype=torch.int64` (or `dtype=torch.int32`) directly in `torch.tensor()`, eliminating the float32 conversion. The fix applies to all offsets constructions or similar usage of np and torch.tensor. Reviewed By: gchalump Differential Revision: D94345333
b3fa4bc to
9c7cf1a
Compare
spcyppt
added a commit
to spcyppt/FBGEMM
that referenced
this pull request
Feb 26, 2026
Summary: X-link: facebookresearch/FBGEMM#2402 # TLDR; TBE unit tests constructs offsets via ``` torch.tensor([0] + np.cumsum(lengths).tolist()).long() ``` in which `torch.tensor()` infers `torch.float32`. Float32 can only represent consecutive integers up to 2^24 (16,777,216); beyond that, odd values round to even, producing wrong offsets. The subsequent `.long()` preserves the already-corrupted values. So for large data where offsets values are supposed to be larger than 16,777,216 can cause offsets to be wrong. This also affects backward_adagrad_large dim test. The diff - corrects dtype such that there's no precision loss due to conversion. - re-enables backward_adagrad_large_dim test. ---- # Problem We see TBE forward output mismatch errors in unit tests when we set up large T (number of features) and large B (batch size). We can reproduce this error using the test `test_backward_adagrad_with_simple_tbe_op` where - T = 230, B = 294,440 - T = 57, B = 294,440 (just enough to cause error) - T = 230, B = 184320 # Root cause After debugging, the root cause was due to the test indices construction and not from the kernels. - `np.ones() * L` produces a float64 numpy array - `.tolist()` yields Python floats (float64) - `torch.tensor()` infers `torch.float32` (the default dtype) — narrowing 64-bit doubles to 32-bit floats. Float32 can only represent consecutive integers up to 2^24 (16,777,216); beyond that, odd values round to even, producing wrong offsets. The subsequent `.long()` preserves the already-corrupted values. This caused (T=230, B=184320, total_B=42M) to fail: the kernel received wrong offsets for b_t >= 2^24, computing wrong bag lengths (e.g., L=0 or L=2 instead of 1) and looking up wrong embedding rows. Both v1 and v2 TBE forward kernels were affected identically since they share the same offsets tensor. Based on the example above: ``` Offsets for Table 56 (partially below 2^24, and partially above 2^24): 16486400.0 ... 16780796.0 16780798.0 16780800.0 ^^^^^^^^^ odd values missing (L becomes 2, as opposed to 1) Offsets for Table 57 (fully above 2^24) 16780800.0 16780800.0 16780802.0 ... ^^^^^^^^^ should be 16780801, duplicated (L becomes 0, where 1 is expected) Offsets for Table 113 (above 2^25 -- step size increases to 4): 33267200.0 33267200.0 33267202.0 33267204.0 33267204.0 33267204.0 33267206.0 33267208.0 33267208.0 33267208.0 33561592.0 33561592.0 33561592.0 33561592.0 33561592.0 33561596.0 33561596.0 33561596.0 33561600.0 33561600.0 ^^^^^^^^^ 3 identical values (more L=0) Offsets for Table 229 (near 2^26 -- step size is 4, massive gaps): 67417600.0 67417600.0 67417600.0 67417600.0 67417600.0 67417608.0 67417608.0 67417608.0 67417608.0 67417608.0 67711992.0 67711992.0 67711992.0 67711992.0 67711992.0 67711992.0 67712000.0 67712000.0 67712000.0 67712000.0 ^^^^^^^^^ 5 identical values (more L=0) ``` # Fix The fix ensure `np.one` to use `int` and specifies `dtype=torch.int64` (or `dtype=torch.int32`) directly in `torch.tensor()`, eliminating the float32 conversion. The fix applies to all offsets constructions or similar usage of np and torch.tensor. Reviewed By: gchalump Differential Revision: D94345333
9c7cf1a to
638594e
Compare
spcyppt
added a commit
to spcyppt/FBGEMM
that referenced
this pull request
Feb 26, 2026
Summary: X-link: https://github.com/facebookresearch/FBGEMM/pull/2402 Pull Request resolved: pytorch#5430 # TLDR; TBE unit tests constructs offsets via ``` torch.tensor([0] + np.cumsum(lengths).tolist()).long() ``` in which `torch.tensor()` infers `torch.float32`. Float32 can only represent consecutive integers up to 2^24 (16,777,216); beyond that, odd values round to even, producing wrong offsets. The subsequent `.long()` preserves the already-corrupted values. So for large data where offsets values are supposed to be larger than 16,777,216 can cause offsets to be wrong. This also affects backward_adagrad_large dim test. The diff - corrects dtype such that there's no precision loss due to conversion. - re-enables backward_adagrad_large_dim test. ---- # Problem We see TBE forward output mismatch errors in unit tests when we set up large T (number of features) and large B (batch size). We can reproduce this error using the test `test_backward_adagrad_with_simple_tbe_op` where - T = 230, B = 294,440 - T = 57, B = 294,440 (just enough to cause error) - T = 230, B = 184320 # Root cause After debugging, the root cause was due to the test indices construction and not from the kernels. - `np.ones() * L` produces a float64 numpy array - `.tolist()` yields Python floats (float64) - `torch.tensor()` infers `torch.float32` (the default dtype) — narrowing 64-bit doubles to 32-bit floats. Float32 can only represent consecutive integers up to 2^24 (16,777,216); beyond that, odd values round to even, producing wrong offsets. The subsequent `.long()` preserves the already-corrupted values. This caused (T=230, B=184320, total_B=42M) to fail: the kernel received wrong offsets for b_t >= 2^24, computing wrong bag lengths (e.g., L=0 or L=2 instead of 1) and looking up wrong embedding rows. Both v1 and v2 TBE forward kernels were affected identically since they share the same offsets tensor. Based on the example above: ``` Offsets for Table 56 (partially below 2^24, and partially above 2^24): 16486400.0 ... 16780796.0 16780798.0 16780800.0 ^^^^^^^^^ odd values missing (L becomes 2, as opposed to 1) Offsets for Table 57 (fully above 2^24) 16780800.0 16780800.0 16780802.0 ... ^^^^^^^^^ should be 16780801, duplicated (L becomes 0, where 1 is expected) Offsets for Table 113 (above 2^25 -- step size increases to 4): 33267200.0 33267200.0 33267202.0 33267204.0 33267204.0 33267204.0 33267206.0 33267208.0 33267208.0 33267208.0 33561592.0 33561592.0 33561592.0 33561592.0 33561592.0 33561596.0 33561596.0 33561596.0 33561600.0 33561600.0 ^^^^^^^^^ 3 identical values (more L=0) Offsets for Table 229 (near 2^26 -- step size is 4, massive gaps): 67417600.0 67417600.0 67417600.0 67417600.0 67417600.0 67417608.0 67417608.0 67417608.0 67417608.0 67417608.0 67711992.0 67711992.0 67711992.0 67711992.0 67711992.0 67711992.0 67712000.0 67712000.0 67712000.0 67712000.0 ^^^^^^^^^ 5 identical values (more L=0) ``` # Fix The fix ensure `np.one` to use `int` and specifies `dtype=torch.int64` (or `dtype=torch.int32`) directly in `torch.tensor()`, eliminating the float32 conversion. The fix applies to all offsets constructions or similar usage of np and torch.tensor. Reviewed By: gchalump Differential Revision: D94345333
0784121 to
118df08
Compare
spcyppt
added a commit
to spcyppt/FBGEMM
that referenced
this pull request
Feb 26, 2026
Summary: X-link: https://github.com/facebookresearch/FBGEMM/pull/2402 Pull Request resolved: pytorch#5430 # TLDR; TBE unit tests constructs offsets via ``` torch.tensor([0] + np.cumsum(lengths).tolist()).long() ``` in which `torch.tensor()` infers `torch.float32`. Float32 can only represent consecutive integers up to 2^24 (16,777,216); beyond that, odd values round to even, producing wrong offsets. The subsequent `.long()` preserves the already-corrupted values. So for large data where offsets values are supposed to be larger than 16,777,216 can cause offsets to be wrong. This also affects backward_adagrad_large dim test. The diff - corrects dtype such that there's no precision loss due to conversion. - re-enables backward_adagrad_large_dim test. ---- # Problem We see TBE forward output mismatch errors in unit tests when we set up large T (number of features) and large B (batch size). We can reproduce this error using the test `test_backward_adagrad_with_simple_tbe_op` where - T = 230, B = 294,440 - T = 57, B = 294,440 (just enough to cause error) - T = 230, B = 184320 # Root cause After debugging, the root cause was due to the test indices construction and not from the kernels. - `np.ones() * L` produces a float64 numpy array - `.tolist()` yields Python floats (float64) - `torch.tensor()` infers `torch.float32` (the default dtype) — narrowing 64-bit doubles to 32-bit floats. Float32 can only represent consecutive integers up to 2^24 (16,777,216); beyond that, odd values round to even, producing wrong offsets. The subsequent `.long()` preserves the already-corrupted values. This caused (T=230, B=184320, total_B=42M) to fail: the kernel received wrong offsets for b_t >= 2^24, computing wrong bag lengths (e.g., L=0 or L=2 instead of 1) and looking up wrong embedding rows. Both v1 and v2 TBE forward kernels were affected identically since they share the same offsets tensor. Based on the example above: ``` Offsets for Table 56 (partially below 2^24, and partially above 2^24): 16486400.0 ... 16780796.0 16780798.0 16780800.0 ^^^^^^^^^ odd values missing (L becomes 2, as opposed to 1) Offsets for Table 57 (fully above 2^24) 16780800.0 16780800.0 16780802.0 ... ^^^^^^^^^ should be 16780801, duplicated (L becomes 0, where 1 is expected) Offsets for Table 113 (above 2^25 -- step size increases to 4): 33267200.0 33267200.0 33267202.0 33267204.0 33267204.0 33267204.0 33267206.0 33267208.0 33267208.0 33267208.0 33561592.0 33561592.0 33561592.0 33561592.0 33561592.0 33561596.0 33561596.0 33561596.0 33561600.0 33561600.0 ^^^^^^^^^ 3 identical values (more L=0) Offsets for Table 229 (near 2^26 -- step size is 4, massive gaps): 67417600.0 67417600.0 67417600.0 67417600.0 67417600.0 67417608.0 67417608.0 67417608.0 67417608.0 67417608.0 67711992.0 67711992.0 67711992.0 67711992.0 67711992.0 67711992.0 67712000.0 67712000.0 67712000.0 67712000.0 ^^^^^^^^^ 5 identical values (more L=0) ``` # Fix The fix ensure `np.one` to use `int` and specifies `dtype=torch.int64` (or `dtype=torch.int32`) directly in `torch.tensor()`, eliminating the float32 conversion. The fix applies to all offsets constructions or similar usage of np and torch.tensor. Reviewed By: gchalump Differential Revision: D94345333
spcyppt
added a commit
to spcyppt/FBGEMM
that referenced
this pull request
Feb 26, 2026
Summary: X-link: facebookresearch/FBGEMM#2402 # TLDR; TBE unit tests constructs offsets via ``` torch.tensor([0] + np.cumsum(lengths).tolist()).long() ``` in which `torch.tensor()` infers `torch.float32`. Float32 can only represent consecutive integers up to 2^24 (16,777,216); beyond that, odd values round to even, producing wrong offsets. The subsequent `.long()` preserves the already-corrupted values. So for large data where offsets values are supposed to be larger than 16,777,216 can cause offsets to be wrong. This also affects backward_adagrad_large dim test. The diff - corrects dtype such that there's no precision loss due to conversion. - re-enables backward_adagrad_large_dim test. ---- # Problem We see TBE forward output mismatch errors in unit tests when we set up large T (number of features) and large B (batch size). We can reproduce this error using the test `test_backward_adagrad_with_simple_tbe_op` where - T = 230, B = 294,440 - T = 57, B = 294,440 (just enough to cause error) - T = 230, B = 184320 # Root cause After debugging, the root cause was due to the test indices construction and not from the kernels. - `np.ones() * L` produces a float64 numpy array - `.tolist()` yields Python floats (float64) - `torch.tensor()` infers `torch.float32` (the default dtype) — narrowing 64-bit doubles to 32-bit floats. Float32 can only represent consecutive integers up to 2^24 (16,777,216); beyond that, odd values round to even, producing wrong offsets. The subsequent `.long()` preserves the already-corrupted values. This caused (T=230, B=184320, total_B=42M) to fail: the kernel received wrong offsets for b_t >= 2^24, computing wrong bag lengths (e.g., L=0 or L=2 instead of 1) and looking up wrong embedding rows. Both v1 and v2 TBE forward kernels were affected identically since they share the same offsets tensor. Based on the example above: ``` Offsets for Table 56 (partially below 2^24, and partially above 2^24): 16486400.0 ... 16780796.0 16780798.0 16780800.0 ^^^^^^^^^ odd values missing (L becomes 2, as opposed to 1) Offsets for Table 57 (fully above 2^24) 16780800.0 16780800.0 16780802.0 ... ^^^^^^^^^ should be 16780801, duplicated (L becomes 0, where 1 is expected) Offsets for Table 113 (above 2^25 -- step size increases to 4): 33267200.0 33267200.0 33267202.0 33267204.0 33267204.0 33267204.0 33267206.0 33267208.0 33267208.0 33267208.0 33561592.0 33561592.0 33561592.0 33561592.0 33561592.0 33561596.0 33561596.0 33561596.0 33561600.0 33561600.0 ^^^^^^^^^ 3 identical values (more L=0) Offsets for Table 229 (near 2^26 -- step size is 4, massive gaps): 67417600.0 67417600.0 67417600.0 67417600.0 67417600.0 67417608.0 67417608.0 67417608.0 67417608.0 67417608.0 67711992.0 67711992.0 67711992.0 67711992.0 67711992.0 67711992.0 67712000.0 67712000.0 67712000.0 67712000.0 ^^^^^^^^^ 5 identical values (more L=0) ``` # Fix The fix ensure `np.one` to use `int` and specifies `dtype=torch.int64` (or `dtype=torch.int32`) directly in `torch.tensor()`, eliminating the float32 conversion. The fix applies to all offsets constructions or similar usage of np and torch.tensor. Reviewed By: gchalump Differential Revision: D94345333
118df08 to
5db3e1b
Compare
spcyppt
added a commit
to spcyppt/FBGEMM
that referenced
this pull request
Feb 26, 2026
Summary: X-link: facebookresearch/FBGEMM#2402 # TLDR; TBE unit tests constructs offsets via ``` torch.tensor([0] + np.cumsum(lengths).tolist()).long() ``` in which `torch.tensor()` infers `torch.float32`. Float32 can only represent consecutive integers up to 2^24 (16,777,216); beyond that, odd values round to even, producing wrong offsets. The subsequent `.long()` preserves the already-corrupted values. So for large data where offsets values are supposed to be larger than 16,777,216 can cause offsets to be wrong. This also affects backward_adagrad_large dim test. The diff - corrects dtype such that there's no precision loss due to conversion. - re-enables backward_adagrad_large_dim test. ---- # Problem We see TBE forward output mismatch errors in unit tests when we set up large T (number of features) and large B (batch size). We can reproduce this error using the test `test_backward_adagrad_with_simple_tbe_op` where - T = 230, B = 294,440 - T = 57, B = 294,440 (just enough to cause error) - T = 230, B = 184320 # Root cause After debugging, the root cause was due to the test indices construction and not from the kernels. - `np.ones() * L` produces a float64 numpy array - `.tolist()` yields Python floats (float64) - `torch.tensor()` infers `torch.float32` (the default dtype) — narrowing 64-bit doubles to 32-bit floats. Float32 can only represent consecutive integers up to 2^24 (16,777,216); beyond that, odd values round to even, producing wrong offsets. The subsequent `.long()` preserves the already-corrupted values. This caused (T=230, B=184320, total_B=42M) to fail: the kernel received wrong offsets for b_t >= 2^24, computing wrong bag lengths (e.g., L=0 or L=2 instead of 1) and looking up wrong embedding rows. Both v1 and v2 TBE forward kernels were affected identically since they share the same offsets tensor. Based on the example above: ``` Offsets for Table 56 (partially below 2^24, and partially above 2^24): 16486400.0 ... 16780796.0 16780798.0 16780800.0 ^^^^^^^^^ odd values missing (L becomes 2, as opposed to 1) Offsets for Table 57 (fully above 2^24) 16780800.0 16780800.0 16780802.0 ... ^^^^^^^^^ should be 16780801, duplicated (L becomes 0, where 1 is expected) Offsets for Table 113 (above 2^25 -- step size increases to 4): 33267200.0 33267200.0 33267202.0 33267204.0 33267204.0 33267204.0 33267206.0 33267208.0 33267208.0 33267208.0 33561592.0 33561592.0 33561592.0 33561592.0 33561592.0 33561596.0 33561596.0 33561596.0 33561600.0 33561600.0 ^^^^^^^^^ 3 identical values (more L=0) Offsets for Table 229 (near 2^26 -- step size is 4, massive gaps): 67417600.0 67417600.0 67417600.0 67417600.0 67417600.0 67417608.0 67417608.0 67417608.0 67417608.0 67417608.0 67711992.0 67711992.0 67711992.0 67711992.0 67711992.0 67711992.0 67712000.0 67712000.0 67712000.0 67712000.0 ^^^^^^^^^ 5 identical values (more L=0) ``` # Fix The fix ensure `np.one` to use `int` and specifies `dtype=torch.int64` (or `dtype=torch.int32`) directly in `torch.tensor()`, eliminating the float32 conversion. The fix applies to all offsets constructions or similar usage of np and torch.tensor. Reviewed By: gchalump Differential Revision: D94345333
5db3e1b to
eb9a912
Compare
spcyppt
added a commit
to spcyppt/FBGEMM
that referenced
this pull request
Feb 26, 2026
Summary: X-link: https://github.com/facebookresearch/FBGEMM/pull/2402 Pull Request resolved: pytorch#5430 # TLDR; TBE unit tests constructs offsets via ``` torch.tensor([0] + np.cumsum(lengths).tolist()).long() ``` in which `torch.tensor()` infers `torch.float32`. Float32 can only represent consecutive integers up to 2^24 (16,777,216); beyond that, odd values round to even, producing wrong offsets. The subsequent `.long()` preserves the already-corrupted values. So for large data where offsets values are supposed to be larger than 16,777,216 can cause offsets to be wrong. This also affects backward_adagrad_large dim test. The diff - corrects dtype such that there's no precision loss due to conversion. - re-enables backward_adagrad_large_dim test. ---- # Problem We see TBE forward output mismatch errors in unit tests when we set up large T (number of features) and large B (batch size). We can reproduce this error using the test `test_backward_adagrad_with_simple_tbe_op` where - T = 230, B = 294,440 - T = 57, B = 294,440 (just enough to cause error) - T = 230, B = 184320 # Root cause After debugging, the root cause was due to the test indices construction and not from the kernels. - `np.ones() * L` produces a float64 numpy array - `.tolist()` yields Python floats (float64) - `torch.tensor()` infers `torch.float32` (the default dtype) — narrowing 64-bit doubles to 32-bit floats. Float32 can only represent consecutive integers up to 2^24 (16,777,216); beyond that, odd values round to even, producing wrong offsets. The subsequent `.long()` preserves the already-corrupted values. This caused (T=230, B=184320, total_B=42M) to fail: the kernel received wrong offsets for b_t >= 2^24, computing wrong bag lengths (e.g., L=0 or L=2 instead of 1) and looking up wrong embedding rows. Both v1 and v2 TBE forward kernels were affected identically since they share the same offsets tensor. Based on the example above: ``` Offsets for Table 56 (partially below 2^24, and partially above 2^24): 16486400.0 ... 16780796.0 16780798.0 16780800.0 ^^^^^^^^^ odd values missing (L becomes 2, as opposed to 1) Offsets for Table 57 (fully above 2^24) 16780800.0 16780800.0 16780802.0 ... ^^^^^^^^^ should be 16780801, duplicated (L becomes 0, where 1 is expected) Offsets for Table 113 (above 2^25 -- step size increases to 4): 33267200.0 33267200.0 33267202.0 33267204.0 33267204.0 33267204.0 33267206.0 33267208.0 33267208.0 33267208.0 33561592.0 33561592.0 33561592.0 33561592.0 33561592.0 33561596.0 33561596.0 33561596.0 33561600.0 33561600.0 ^^^^^^^^^ 3 identical values (more L=0) Offsets for Table 229 (near 2^26 -- step size is 4, massive gaps): 67417600.0 67417600.0 67417600.0 67417600.0 67417600.0 67417608.0 67417608.0 67417608.0 67417608.0 67417608.0 67711992.0 67711992.0 67711992.0 67711992.0 67711992.0 67711992.0 67712000.0 67712000.0 67712000.0 67712000.0 ^^^^^^^^^ 5 identical values (more L=0) ``` # Fix The fix ensure `np.one` to use `int` and specifies `dtype=torch.int64` (or `dtype=torch.int32`) directly in `torch.tensor()`, eliminating the float32 conversion. The fix applies to all offsets constructions or similar usage of np and torch.tensor. Reviewed By: gchalump Differential Revision: D94345333
eb9a912 to
d374e67
Compare
spcyppt
added a commit
to spcyppt/FBGEMM
that referenced
this pull request
Feb 26, 2026
Summary: X-link: https://github.com/facebookresearch/FBGEMM/pull/2402 Pull Request resolved: pytorch#5430 # TLDR; TBE unit tests constructs offsets via ``` torch.tensor([0] + np.cumsum(lengths).tolist()).long() ``` in which `torch.tensor()` infers `torch.float32`. Float32 can only represent consecutive integers up to 2^24 (16,777,216); beyond that, odd values round to even, producing wrong offsets. The subsequent `.long()` preserves the already-corrupted values. So for large data where offsets values are supposed to be larger than 16,777,216 can cause offsets to be wrong. This also affects backward_adagrad_large dim test. The diff - corrects dtype such that there's no precision loss due to conversion. - re-enables backward_adagrad_large_dim test. ---- # Problem We see TBE forward output mismatch errors in unit tests when we set up large T (number of features) and large B (batch size). We can reproduce this error using the test `test_backward_adagrad_with_simple_tbe_op` where - T = 230, B = 294,440 - T = 57, B = 294,440 (just enough to cause error) - T = 230, B = 184320 # Root cause After debugging, the root cause was due to the test indices construction and not from the kernels. - `np.ones() * L` produces a float64 numpy array - `.tolist()` yields Python floats (float64) - `torch.tensor()` infers `torch.float32` (the default dtype) — narrowing 64-bit doubles to 32-bit floats. Float32 can only represent consecutive integers up to 2^24 (16,777,216); beyond that, odd values round to even, producing wrong offsets. The subsequent `.long()` preserves the already-corrupted values. This caused (T=230, B=184320, total_B=42M) to fail: the kernel received wrong offsets for b_t >= 2^24, computing wrong bag lengths (e.g., L=0 or L=2 instead of 1) and looking up wrong embedding rows. Both v1 and v2 TBE forward kernels were affected identically since they share the same offsets tensor. Based on the example above: ``` Offsets for Table 56 (partially below 2^24, and partially above 2^24): 16486400.0 ... 16780796.0 16780798.0 16780800.0 ^^^^^^^^^ odd values missing (L becomes 2, as opposed to 1) Offsets for Table 57 (fully above 2^24) 16780800.0 16780800.0 16780802.0 ... ^^^^^^^^^ should be 16780801, duplicated (L becomes 0, where 1 is expected) Offsets for Table 113 (above 2^25 -- step size increases to 4): 33267200.0 33267200.0 33267202.0 33267204.0 33267204.0 33267204.0 33267206.0 33267208.0 33267208.0 33267208.0 33561592.0 33561592.0 33561592.0 33561592.0 33561592.0 33561596.0 33561596.0 33561596.0 33561600.0 33561600.0 ^^^^^^^^^ 3 identical values (more L=0) Offsets for Table 229 (near 2^26 -- step size is 4, massive gaps): 67417600.0 67417600.0 67417600.0 67417600.0 67417600.0 67417608.0 67417608.0 67417608.0 67417608.0 67417608.0 67711992.0 67711992.0 67711992.0 67711992.0 67711992.0 67711992.0 67712000.0 67712000.0 67712000.0 67712000.0 ^^^^^^^^^ 5 identical values (more L=0) ``` # Fix The fix ensure `np.one` to use `int` and specifies `dtype=torch.int64` (or `dtype=torch.int32`) directly in `torch.tensor()`, eliminating the float32 conversion. The fix applies to all offsets constructions or similar usage of np and torch.tensor. Reviewed By: gchalump Differential Revision: D94345333
770e890 to
3694476
Compare
spcyppt
added a commit
to spcyppt/FBGEMM
that referenced
this pull request
Feb 26, 2026
Summary: X-link: https://github.com/facebookresearch/FBGEMM/pull/2402 Pull Request resolved: pytorch#5430 # TLDR; TBE unit tests constructs offsets via ``` torch.tensor([0] + np.cumsum(lengths).tolist()).long() ``` in which `torch.tensor()` infers `torch.float32`. Float32 can only represent consecutive integers up to 2^24 (16,777,216); beyond that, odd values round to even, producing wrong offsets. The subsequent `.long()` preserves the already-corrupted values. So for large data where offsets values are supposed to be larger than 16,777,216 can cause offsets to be wrong. This also affects backward_adagrad_large dim test. The diff - corrects dtype such that there's no precision loss due to conversion. - re-enables backward_adagrad_large_dim test. ---- # Problem We see TBE forward output mismatch errors in unit tests when we set up large T (number of features) and large B (batch size). We can reproduce this error using the test `test_backward_adagrad_with_simple_tbe_op` where - T = 230, B = 294,440 - T = 57, B = 294,440 (just enough to cause error) - T = 230, B = 184320 # Root cause After debugging, the root cause was due to the test indices construction and not from the kernels. - `np.ones() * L` produces a float64 numpy array - `.tolist()` yields Python floats (float64) - `torch.tensor()` infers `torch.float32` (the default dtype) — narrowing 64-bit doubles to 32-bit floats. Float32 can only represent consecutive integers up to 2^24 (16,777,216); beyond that, odd values round to even, producing wrong offsets. The subsequent `.long()` preserves the already-corrupted values. This caused (T=230, B=184320, total_B=42M) to fail: the kernel received wrong offsets for b_t >= 2^24, computing wrong bag lengths (e.g., L=0 or L=2 instead of 1) and looking up wrong embedding rows. Both v1 and v2 TBE forward kernels were affected identically since they share the same offsets tensor. Based on the example above: ``` Offsets for Table 56 (partially below 2^24, and partially above 2^24): 16486400.0 ... 16780796.0 16780798.0 16780800.0 ^^^^^^^^^ odd values missing (L becomes 2, as opposed to 1) Offsets for Table 57 (fully above 2^24) 16780800.0 16780800.0 16780802.0 ... ^^^^^^^^^ should be 16780801, duplicated (L becomes 0, where 1 is expected) Offsets for Table 113 (above 2^25 -- step size increases to 4): 33267200.0 33267200.0 33267202.0 33267204.0 33267204.0 33267204.0 33267206.0 33267208.0 33267208.0 33267208.0 33561592.0 33561592.0 33561592.0 33561592.0 33561592.0 33561596.0 33561596.0 33561596.0 33561600.0 33561600.0 ^^^^^^^^^ 3 identical values (more L=0) Offsets for Table 229 (near 2^26 -- step size is 4, massive gaps): 67417600.0 67417600.0 67417600.0 67417600.0 67417600.0 67417608.0 67417608.0 67417608.0 67417608.0 67417608.0 67711992.0 67711992.0 67711992.0 67711992.0 67711992.0 67711992.0 67712000.0 67712000.0 67712000.0 67712000.0 ^^^^^^^^^ 5 identical values (more L=0) ``` # Fix The fix ensure `np.one` to use `int` and specifies `dtype=torch.int64` (or `dtype=torch.int32`) directly in `torch.tensor()`, eliminating the float32 conversion. The fix applies to all offsets constructions or similar usage of np and torch.tensor. Reviewed By: gchalump Differential Revision: D94345333
Summary: X-link: facebookresearch/FBGEMM#2402 # TLDR; TBE unit tests constructs offsets via ``` torch.tensor([0] + np.cumsum(lengths).tolist()).long() ``` in which `torch.tensor()` infers `torch.float32`. Float32 can only represent consecutive integers up to 2^24 (16,777,216); beyond that, odd values round to even, producing wrong offsets. The subsequent `.long()` preserves the already-corrupted values. So for large data where offsets values are supposed to be larger than 16,777,216 can cause offsets to be wrong. This also affects backward_adagrad_large dim test. The diff - corrects dtype such that there's no precision loss due to conversion. - re-enables backward_adagrad_large_dim test. ---- # Problem We see TBE forward output mismatch errors in unit tests when we set up large T (number of features) and large B (batch size). We can reproduce this error using the test `test_backward_adagrad_with_simple_tbe_op` where - T = 230, B = 294,440 - T = 57, B = 294,440 (just enough to cause error) - T = 230, B = 184320 # Root cause After debugging, the root cause was due to the test indices construction and not from the kernels. - `np.ones() * L` produces a float64 numpy array - `.tolist()` yields Python floats (float64) - `torch.tensor()` infers `torch.float32` (the default dtype) — narrowing 64-bit doubles to 32-bit floats. Float32 can only represent consecutive integers up to 2^24 (16,777,216); beyond that, odd values round to even, producing wrong offsets. The subsequent `.long()` preserves the already-corrupted values. This caused (T=230, B=184320, total_B=42M) to fail: the kernel received wrong offsets for b_t >= 2^24, computing wrong bag lengths (e.g., L=0 or L=2 instead of 1) and looking up wrong embedding rows. Both v1 and v2 TBE forward kernels were affected identically since they share the same offsets tensor. Based on the example above: ``` Offsets for Table 56 (partially below 2^24, and partially above 2^24): 16486400.0 ... 16780796.0 16780798.0 16780800.0 ^^^^^^^^^ odd values missing (L becomes 2, as opposed to 1) Offsets for Table 57 (fully above 2^24) 16780800.0 16780800.0 16780802.0 ... ^^^^^^^^^ should be 16780801, duplicated (L becomes 0, where 1 is expected) Offsets for Table 113 (above 2^25 -- step size increases to 4): 33267200.0 33267200.0 33267202.0 33267204.0 33267204.0 33267204.0 33267206.0 33267208.0 33267208.0 33267208.0 33561592.0 33561592.0 33561592.0 33561592.0 33561592.0 33561596.0 33561596.0 33561596.0 33561600.0 33561600.0 ^^^^^^^^^ 3 identical values (more L=0) Offsets for Table 229 (near 2^26 -- step size is 4, massive gaps): 67417600.0 67417600.0 67417600.0 67417600.0 67417600.0 67417608.0 67417608.0 67417608.0 67417608.0 67417608.0 67711992.0 67711992.0 67711992.0 67711992.0 67711992.0 67711992.0 67712000.0 67712000.0 67712000.0 67712000.0 ^^^^^^^^^ 5 identical values (more L=0) ``` # Fix The fix ensure `np.one` to use `int` and specifies `dtype=torch.int64` (or `dtype=torch.int32`) directly in `torch.tensor()`, eliminating the float32 conversion. The fix applies to all offsets constructions or similar usage of np and torch.tensor. Reviewed By: gchalump Differential Revision: D94345333
3694476 to
cd9f8ea
Compare
Contributor
|
This pull request has been merged in 83ba4fb. |
zpao
pushed a commit
that referenced
this pull request
Feb 28, 2026
Summary: X-link: https://github.com/facebookresearch/FBGEMM/pull/2402 Pull Request resolved: #5430 # TLDR; TBE unit tests constructs offsets via ``` torch.tensor([0] + np.cumsum(lengths).tolist()).long() ``` in which `torch.tensor()` infers `torch.float32`. Float32 can only represent consecutive integers up to 2^24 (16,777,216); beyond that, odd values round to even, producing wrong offsets. The subsequent `.long()` preserves the already-corrupted values. So for large data where offsets values are supposed to be larger than 16,777,216 can cause offsets to be wrong. This also affects backward_adagrad_large dim test. The diff - corrects dtype such that there's no precision loss due to conversion. - re-enables backward_adagrad_large_dim test. ---- # Problem We see TBE forward output mismatch errors in unit tests when we set up large T (number of features) and large B (batch size). We can reproduce this error using the test `test_backward_adagrad_with_simple_tbe_op` where - T = 230, B = 294,440 - T = 57, B = 294,440 (just enough to cause error) - T = 230, B = 184320 # Root cause After debugging, the root cause was due to the test indices construction and not from the kernels. - `np.ones() * L` produces a float64 numpy array - `.tolist()` yields Python floats (float64) - `torch.tensor()` infers `torch.float32` (the default dtype) — narrowing 64-bit doubles to 32-bit floats. Float32 can only represent consecutive integers up to 2^24 (16,777,216); beyond that, odd values round to even, producing wrong offsets. The subsequent `.long()` preserves the already-corrupted values. This caused (T=230, B=184320, total_B=42M) to fail: the kernel received wrong offsets for b_t >= 2^24, computing wrong bag lengths (e.g., L=0 or L=2 instead of 1) and looking up wrong embedding rows. Both v1 and v2 TBE forward kernels were affected identically since they share the same offsets tensor. Based on the example above: ``` Offsets for Table 56 (partially below 2^24, and partially above 2^24): 16486400.0 ... 16780796.0 16780798.0 16780800.0 ^^^^^^^^^ odd values missing (L becomes 2, as opposed to 1) Offsets for Table 57 (fully above 2^24) 16780800.0 16780800.0 16780802.0 ... ^^^^^^^^^ should be 16780801, duplicated (L becomes 0, where 1 is expected) Offsets for Table 113 (above 2^25 -- step size increases to 4): 33267200.0 33267200.0 33267202.0 33267204.0 33267204.0 33267204.0 33267206.0 33267208.0 33267208.0 33267208.0 33561592.0 33561592.0 33561592.0 33561592.0 33561592.0 33561596.0 33561596.0 33561596.0 33561600.0 33561600.0 ^^^^^^^^^ 3 identical values (more L=0) Offsets for Table 229 (near 2^26 -- step size is 4, massive gaps): 67417600.0 67417600.0 67417600.0 67417600.0 67417600.0 67417608.0 67417608.0 67417608.0 67417608.0 67417608.0 67711992.0 67711992.0 67711992.0 67711992.0 67711992.0 67711992.0 67712000.0 67712000.0 67712000.0 67712000.0 ^^^^^^^^^ 5 identical values (more L=0) ``` # Fix The fix ensure `np.one` to use `int` and specifies `dtype=torch.int64` (or `dtype=torch.int32`) directly in `torch.tensor()`, eliminating the float32 conversion. The fix applies to all offsets constructions or similar usage of np and torch.tensor. Reviewed By: gchalump, q10 Differential Revision: D94345333 fbshipit-source-id: 5085ab31f4e9ebb05d269c8708efff454655eaa8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
X-link: https://github.com/facebookresearch/FBGEMM/pull/2402
TLDR;
TBE unit tests constructs offsets via
in which
torch.tensor()inferstorch.float32.Float32 can only represent consecutive integers up to 2^24 (16,777,216); beyond that, odd values round to even, producing wrong offsets. The subsequent
.long()preserves the already-corrupted values.So for large data where offsets values are supposed to be larger than 16,777,216 can cause offsets to be wrong.
The diff ensures correct dtype so there's no precision loss due to conversion.
Problem
We see TBE forward output mismatch errors in unit tests when we set up large T (number of features) and large B (batch size). We can reproduce this error using the test
test_backward_adagrad_with_simple_tbe_opwhere
Root cause
After debugging, the root cause was due to the test indices construction and not from the kernels.
np.ones() * Lproduces a float64 numpy array.tolist()yields Python floats (float64)torch.tensor()inferstorch.float32(thedefault dtype) — narrowing 64-bit doubles to 32-bit floats.
Float32 can only represent consecutive integers up to 2^24 (16,777,216); beyond that, odd values round to even, producing wrong offsets. The subsequent
.long()preserves the already-corrupted values.This caused (T=230, B=184320, total_B=42M) to fail:
the kernel received wrong offsets for b_t >= 2^24, computing wrong bag lengths (e.g., L=0 or L=2 instead
of 1) and looking up wrong embedding rows. Both v1 and v2 TBE
forward kernels were affected identically since they share the same offsets tensor.
Based on the example above:
Fix
The fix ensure
np.oneto useintand specifiesdtype=torch.int64(ordtype=torch.int32) directly intorch.tensor(), eliminating the float32 conversion.The fix applies to all offsets constructions or similar usage of np and torch.tensor.
Differential Revision: D94345333