Skip to content

AssertionError in Graph Mode with Slice Operation on nn.ParameterList #10686

@LiSsHhUuAaIi

Description

@LiSsHhUuAaIi

Summary

I encountered an AssertionError when running a model in OneFlow's Graph mode that works perfectly in eager mode. The error occurs specifically when performing slice operations on nn.ParameterList within the forward method during graph compilation. The assertion assert isinstance(idx, int) fails when processing slice indices.

Code to reproduce bug

import oneflow as flow
import oneflow.nn as nn

class SimpleModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.params = nn.ParameterList([
            nn.Parameter(flow.randn(64, 128)),
            nn.Parameter(flow.randn(32, 64)), 
            nn.Parameter(flow.randn(16, 32))
        ])
    
    def forward(self, x):
        # This slice operation fails in Graph mode
        return self.params[::2][0] + x

# Test data
x = flow.randn(64, 128)
model = SimpleModel()

# Eager mode works
print("Eager mode result:", model(x))

# Graph mode fails
class TestGraph(nn.Graph):
    def __init__(self, model):
        super().__init__()
        self.model = model
        
    def build(self, x):
        return self.model(x)

g = TestGraph(model)
print("Graph mode result:", g(x))

Error Logs

[ERROR](GRAPH:TestGraph_0:TestGraph) building graph got error.
Traceback (most recent call last):
  File "test_script.py", line 34, in <module>
    print("Graph mode result:", g(x))
  ...
  File "test_script.py", line 15, in forward
    return self.params[::2][0] + x
  File "/home/miniconda3/envs/oneflow-test/lib/python3.10/site-packages/oneflow/nn/graph/proxy.py", line 767, in __getitem__
    assert isinstance(idx, int)
AssertionError

System Information

  • OS: Ubuntu 20.04.6 LTS
  • OneFlow version: '1.0.0.dev20251101+cpu'
  • Python version: Python 3.10.19

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