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

[IR] Add casting for min/max/put #234

Merged
merged 1 commit into from
Oct 26, 2024
Merged

[IR] Add casting for min/max/put #234

merged 1 commit into from
Oct 26, 2024

Conversation

chhzh123
Copy link
Member

Description

This PR adds casting operations for min/max/put. Specifically, the codegen for constant float values of HLS also changed, since 0.0 will be interpreted as double if not explicitly declaring the type.

Examples

def test_minmax_cast():
    def kernel(A: int8[2]) -> int32[2]:
        res: int32[2] = 0
        res[0] = min(A[0], 0)
        res[1] = max(A[1], 0.0)
        return res

    s = allo.customize(kernel)
    print(s.module)
    mod = s.build()
    np_A = np.random.randint(-64, 64, size=(2,)).astype(np.int8)
    allo_B = mod(np_A)
    assert allo_B[0] == min(np_A[0], 0)
    assert allo_B[1] == max(np_A[1], 0.0)
    mod = s.build(target="vhls")
    print(mod)
    assert "min" in mod.hls_code
    assert "max" in mod.hls_code
    assert "(float)" in mod.hls_code
void kernel(
  int8_t v0[2],
  int32_t v1[2]
) {     // L2
  for (int v2 = 0; v2 < 2; v2++) {      // L5
    v1[v2] = 0; // L5
  }
  int8_t v3 = v0[0];    // L6
  int32_t v4 = v3;      // L7
  int32_t v5 = min(v4, 0);      // L9
  v1[0] = v5;   // L10
  int8_t v6 = v0[1];    // L11
  float v7 = v6;        // L12
  float v8 = max(v7, (float)0.000000);  // L14
  int32_t v9 = v8;      // L15
  v1[1] = v9;   // L16
}

Checklist

  • PR's title starts with a category (e.g. [Bugfix], [IR], [Builder], etc)
  • Changes are complete (i.e. I finished coding on this PR)
  • All changes have test coverage (It would be better to provide ~2 different test cases to test the robustness of your code)
  • Code is well-documented

@chhzh123 chhzh123 merged commit 415e681 into main Oct 26, 2024
1 check passed
@chhzh123 chhzh123 deleted the minmax_cast branch October 28, 2024 14:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant