Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internal error: (filename: ccgstmts.nim, line: 1126) while returning result with no modification #6453

Closed
mratsim opened this issue Sep 29, 2017 · 1 comment

Comments

@mratsim
Copy link
Collaborator

mratsim commented Sep 29, 2017

I was investigating and adding a test case to this issue #5325 and came upon this internal crash

  # Error: internal error: (filename: ccgstmts.nim, line: 1126)
  # No stack traceback available
  # To create a stacktrace, rerun compilation with ./koch temp c <file>

Test case

type
  Gate[TT] = ref object {.inheritable.}
    # Base operator or layer
    # Inherit from it and add a forward and backward method.

  Node[TT] = ref NodeObj[TT]

  NodeObj {.acyclic.} [TT] = object
    # Store an operator/layer + its parent
    gate: Gate[TT]
    parents: array[2, Node[TT]]

  Context*[TT] = ref object
    ## Tape / Wengert list. Contains the list of applied operations or layers
    ## i.e. Directed Acyclic Graph
    nodes: seq[Node[TT]]

  Variable*[TT] = object
    ## Wrapper for values
    tape: Context[TT]
    value: TT # TT should be a Tensor[T] or CudaTensor[T] or a scalar

method backward*[TT](self: Gate[TT], gradient: TT): array[2,TT] {.base.} =
  raise newException(ValueError, "backward method is not implemented")

method forward*[TT](self: Gate[TT], a, b: Variable[TT]): Variable[TT] {.base.} =
  raise newException(ValueError, "forward method is not implemented")

proc newContext*(TT: typedesc): Context[TT] {.noSideEffect.} =
  ## Initialize a context (Tape / Wengert list)
  new result
  result.nodes = newSeq[Node[TT]]()

#############################################################
type AddGate {.final.} [TT] = ref object of Gate[TT]
  arity: int
  a_shape: seq[int]
  b_shape: seq[int]


method forward*[TT](self: AddGate[TT], a, b: Variable[TT]): Variable[TT] =
  return Variable[TT](tape: a.tape, value: a.value + b.value)

method backward*[TT](self: AddGate[TT], gradient: TT): array[2,TT] =
  result #<--- crash
  # Error: internal error: (filename: ccgstmts.nim, line: 1126)
  # No stack traceback available
  # To create a stacktrace, rerun compilation with ./koch temp c <file>

  # Using the next statements instead do not crash
  # result[0] = 1.TT
  # result[1] = 1.TT

var ctx = newContext float32
@zah zah added the Generics label Oct 2, 2017
@zah zah self-assigned this Oct 2, 2017
@genotrance
Copy link
Contributor

No longer crashes with #head.

-------- SNIPPET --------
type
  Gate[TT] = ref object {.inheritable.}
    # Base operator or layer
    # Inherit from it and add a forward and backward method.

  Node[TT] = ref NodeObj[TT]

  NodeObj {.acyclic.} [TT] = object
    # Store an operator/layer + its parent
    gate: Gate[TT]
    parents: array[2, Node[TT]]

  Context*[TT] = ref object
    ## Tape / Wengert list. Contains the list of applied operations or layers
    ## i.e. Directed Acyclic Graph
    nodes: seq[Node[TT]]

  Variable*[TT] = object
    ## Wrapper for values
    tape: Context[TT]
    value: TT # TT should be a Tensor[T] or CudaTensor[T] or a scalar

method backward*[TT](self: Gate[TT], gradient: TT): array[2,TT] {.base.} =
  raise newException(ValueError, "backward method is not implemented")

method forward*[TT](self: Gate[TT], a, b: Variable[TT]): Variable[TT] {.base.} =
  raise newException(ValueError, "forward method is not implemented")

proc newContext*(TT: typedesc): Context[TT] {.noSideEffect.} =
  ## Initialize a context (Tape / Wengert list)
  new result
  result.nodes = newSeq[Node[TT]]()

#############################################################
type AddGate {.final.} [TT] = ref object of Gate[TT]
  arity: int
  a_shape: seq[int]
  b_shape: seq[int]


method forward*[TT](self: AddGate[TT], a, b: Variable[TT]): Variable[TT] =
  return Variable[TT](tape: a.tape, value: a.value + b.value)

method backward*[TT](self: AddGate[TT], gradient: TT): array[2,TT] =
  result #<--- crash
  # Error: internal error: (filename: ccgstmts.nim, line: 1126)
  # No stack traceback available
  # To create a stacktrace, rerun compilation with ./koch temp c <file>

  # Using the next statements instead do not crash
  # result[0] = 1.TT
  # result[1] = 1.TT

var ctx = newContext float32
-------------------------

-------- OUTPUT --------
Hint: used config file 'c:\users\gt\Desktop\DL\programming\nimdevel\config\nim.cfg' [Conf]
Hint: system [Processing]
Hint: temp [Processing]
CC: temp
CC: stdlib_system
Hint:  [Link]
Hint: operation successful (12044 lines compiled; 1.047 sec total; 10.766MiB peakmem; Debug Build) [SuccessX]

Ran successfully
------------------------

-------- NIMTEMP --------
Hint: used config file 'c:\users\gt\Desktop\DL\programming\nimdevel\config\nim.cfg' [Conf]
Hint: system [Processing]
Hint: temp [Processing]
CC: temp
CC: stdlib_system
Hint:  [Link]
Hint: operation successful (12044 lines compiled; 2.219 sec total; 10.758MiB peakmem; Debug Build) [SuccessX]

Ran successfully
-------------------------

-------- VERSION --------
Nim Compiler Version 0.18.1 [Windows: i386]
Compiled at 2018-07-05
Copyright (c) 2006-2018 by Andreas Rumpf

git hash: 596de2a3b935ff21ffb9ab821d7c079003eacb24
active boot switches: -d:release
-------------------------

@Araq Araq closed this as completed Oct 15, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants