cornell-zhang / heterocl

HeteroCL: A Multi-Paradigm Programming Infrastructure for Software-Defined Heterogeneous Computing
https://cornell-zhang.github.io/heterocl/
Apache License 2.0
326 stars 92 forks source link

Stages with same names are overwritten #197

Open chhzh123 opened 4 years ago

chhzh123 commented 4 years ago

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.

zhangzhiru commented 4 years ago

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