heterocl icon indicating copy to clipboard operation
heterocl copied to clipboard

Stages with same names are overwritten

Open chhzh123 opened this issue 5 years ago • 1 comments

See the following example.

A = hcl.placeholder((8, 8), "A")
B = hcl.placeholder((8, 8), "B")
def kernel(A, B):
    C = hcl.compute((8, 8), lambda y, x: A[y][x] + 1, name="add")
    D = hcl.compute((8, 8), lambda y, x: B[y][x] + 1, name="add")
    E = hcl.compute((8, 8), lambda y, x: A[y][x] + B[y][x], name="E")
    return E
s = hcl.create_schedule([A, B], kernel)
print(kernel.__dict__.keys())
s_add = kernel.add
s[s_add].reorder(s_add.axis[1],s_add.axis[0])
print(hcl.lower(s))

It generates the output below, where only the axes of the second "add" are reordered.

dict_keys(['add', 'E']) // only one add
// attr [_top] storage_scope = "global"
allocate _top[int32 * 1]
produce _top {
  // attr [0] extern_scope = 0
  // attr [add] storage_scope = "global"
  allocate add[int32 * 8 * 8]
  produce add {
    // attr [0] extern_scope = 0
    for (y, 0, 8) {
      for (x, 0, 8) {
        add[(x + (y*8))] = (A[(x + (y*8))] + 1)
      }
    }
  }
  // attr [add] storage_scope = "global"
  allocate add[int32 * 8 * 8]
  produce add {
    // attr [0] extern_scope = 0
    for (x, 0, 8) {
      for (y, 0, 8) {
        add[(x + (y*8))] = (B[(x + (y*8))] + 1)
      }
    }
  }
  produce E {
    // attr [0] extern_scope = 0
    for (y, 0, 8) {
      for (x, 0, 8) {
        E[(x + (y*8))] = int32((int33(A[(x + (y*8))]) + int33(B[(x + (y*8))])))
      }
    }
  }
}

This may happen when users want to optimize some compute logic encapsulated in the library. For example, hlib.op.nn.pad with name "pad" is used in conv2d and max_pool. Without renaming, the users can only access one of the padding functions if several conv/pooling layers are used.

chhzh123 avatar May 06 '20 16:05 chhzh123

This is a very good catch. We need to use namespace to manage the stage names.

zhangzhiru avatar May 06 '20 17:05 zhangzhiru