Skip to content

[bug][nn.CrossEntropyLoss] Wrong-shaped target causes internal CHECK failure (is_initialized()) and abort instead of shape-mismatch error #10681

Description

@tinywisdom

Summary

Passing a wrongly shaped target y to nn.CrossEntropyLoss (e.g., logits with shape (N, C) but y has shape (1,) instead of (N,)) triggers an internal assertion:

oneflow::CHECK failed: is_initialized()

and eventually aborts with a core dump, instead of raising a clear, user-facing shape mismatch exception. This happens in eager forward; Graph mode would hit the same failure at the same call site.

Code to reproduce bug

import oneflow as flow
import oneflow.nn as nn

# Model: Linear -> CrossEntropy, forward only
class M(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc = nn.Linear(10, 5)
        self.ce = nn.CrossEntropyLoss()

    def forward(self, x, y):
        logits = self.fc(x)          # (4, 5)
        # Intentionally wrong shape for y; should be (4,)
        return self.ce(logits, y)

# Graph wrapper (same crash point if enabled)
class G(nn.Graph):
    def __init__(self, m):
        super().__init__()
        self.m = m
    def build(self, x, y):
        return self.m(x, y)

def main():
    flow.manual_seed(0)
    m = M()
    x = flow.randn(4, 10)
    y_bad = flow.tensor([0], dtype=flow.long)  # WRONG: should be (4,)

    print("---- Eager forward (expected to fail) ----")
    _ = m(x, y_bad)

    # To test Graph mode as well:
    # g = G(m)
    # _ = g(x, y_bad)

if __name__ == "__main__":
    main()

Observed Error (abridged)

RuntimeError: Check failed: (is_initialized())
  ... functional::cross_entropy(...)
  ... functional::impl::CrossEntropyFunctor::operator()(...)
  ... functional::Reshape(...)
  ... functional::impl::ReshapeFunctor::operator()(...)
  File "oneflow/core/common/shape.h", line 140, in NumAxes
    CHECK_OR_THROW(is_initialized())
terminate called after throwing an instance of 'oneflow::Exception'
what(): Check failed: (is_initialized())
... vm::EagerBlobObject::TryAllocateBlobBodyMemory ...
Aborted (core dumped)

System Information

  • OS: Ubuntu 22.04.4 LTS (x86_64)
  • OneFlow version : 1.0.0.dev20250921+cpu
  • Python version: 3.10.16

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions