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

Metal backend is kind of broken for external arrays args #4949

Closed
k-ye opened this issue May 10, 2022 · 1 comment
Closed

Metal backend is kind of broken for external arrays args #4949

k-ye opened this issue May 10, 2022 · 1 comment
Labels
bug We've confirmed that this is an BUG metal Metal backend

Comments

@k-ye
Copy link
Member

k-ye commented May 10, 2022

The size of an array arg on Metal is still determined at compile-time, and is fixed after JIT compilation. See https://github.com/taichi-dev/taichi/blob/6d538e171c96193fd2ce27967bd0b17a5797565e/taichi/backends/metal/

With the introduction of Ndarray (ti.ndarray), array size is a runtime information. This means that arrays of different size won't re-trigger a kernel JIT. Hence, a Metal kernel with external arrays is now broken, if it's invoked twice with arrays of different size.

The only reason Metal backend still seems work when it's first invoked with an array is reasoned below:

  1. Kernel::Arg::size is zero at arg declaration time:
    int Callable::insert_ret(const DataType &dt) {
    rets.emplace_back(dt->get_compute_type());
    return (int)rets.size() - 1;
    }
    int Callable::insert_arr_arg(const DataType &dt,
    int total_dim,
    std::vector<int> element_shape) {
    args.emplace_back(dt->get_compute_type(), true, /*size=*/0, total_dim,
    element_shape);
    return (int)args.size() - 1;
    }
  2. When setting the external array arg at
    launch_ctx.set_arg_external_array_with_shape(
    actual_argument_slot, int(tmp.ctypes.data),
    tmp.nbytes, v.shape)
    , it eventually calls into here:
    kernel_->args[arg_id].size = size;
    . Note that Kernel::Arg::size is changed at runtime!
  3. Then the kernel is invoked via Kernel::operator()
    if (!compiled_) {
    compile();
    }
    . At this point, Metal's codegen starts working. By the time it gets to
    ma.stride = ma.is_array ? ka.size : dt_bytes;
    , because of step 2, the array size has been set correctly.

We should do two things:

  1. Stop doing this:
    kernel_->args[arg_id].size = size;
    . The runtime size should be set in
    struct RuntimeContext {
    .
  2. Change Metal backend's implementaion for array args. Instead of embedding a fixed size of memory within the Context buffer, we should follow other backends' Ndarray implementation. WIP in [Metal] Support Ndarray #4720
@k-ye k-ye added bug We've confirmed that this is an BUG metal Metal backend labels May 10, 2022
@taichi-ci-bot taichi-ci-bot moved this to Untriaged in Taichi Lang May 10, 2022
@neozhaoliang neozhaoliang moved this from Untriaged to In Progress in Taichi Lang May 13, 2022
@k-ye
Copy link
Member Author

k-ye commented May 25, 2022

Fixed by #4720

@k-ye k-ye closed this as completed May 25, 2022
Repository owner moved this from In Progress to Done in Taichi Lang May 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug We've confirmed that this is an BUG metal Metal backend
Projects
Status: Done
Development

No branches or pull requests

1 participant