Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 69 additions & 20 deletions python/python_direct/ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2256,15 +2256,15 @@ list of Val

void bindIndexingOps(py::module_& ops) {
ops.def(
"index_select",
[](TensorView* arg, TensorView* index, int64_t dim) -> TensorView* {
return indexSelect(arg, dim, index);
},
py::arg("arg"),
py::arg("index"),
py::arg("dim"),
py::return_value_policy::reference,
R"(
"index_select",
[](TensorView* arg, TensorView* index, int64_t dim) -> TensorView* {
return indexSelect(arg, dim, index);
},
py::arg("arg"),
py::arg("index"),
py::arg("dim"),
py::return_value_policy::reference,
R"(
Select elements from a tensor along a specified dimension.

Parameters
Expand All @@ -2278,17 +2278,17 @@ Returns
-------
TensorView
The selected tensor.
)")
.def(
"select",
[](TensorView* arg, Val* index, int64_t dim) -> TensorView* {
return select(arg, dim, index);
},
py::arg("arg"),
py::arg("index"),
py::arg("dim"),
py::return_value_policy::reference,
R"(
)");
ops.def(
"select",
[](TensorView* arg, Val* index, int64_t dim) -> TensorView* {
return select(arg, dim, index);
},
py::arg("arg"),
py::arg("index"),
py::arg("dim"),
py::return_value_policy::reference,
R"(
Select elements from a tensor along a specified dimension.

Parameters
Expand All @@ -2303,6 +2303,55 @@ Returns
TensorView
The selected tensor.
)");
ops.def(
"scatter",
[](TensorView* arg1, TensorView* index, TensorView* src, int64_t dim)
-> TensorView* {
NVF_CHECK(
arg1->nDims() == index->nDims() && arg1->nDims() == src->nDims(),
"Tensor arguments have different dimensions ",
arg1->nDims(),
", ",
index->nDims(),
" and ",
src->nDims());
auto num_dims = (int64_t)arg1->nDims();
NVF_CHECK(
dim >= -num_dims && dim < num_dims,
"Tensor arguments have dimension ",
num_dims,
" so dim argument must satisfy ",
-num_dims,
" <= dim < ",
num_dims,
", but received ",
dim);
return scatter(arg1, dim, index, src);
},
py::arg("arg1"),
py::arg("index"),
py::arg("src"),
py::arg("dim"),
R"(
Scatter a tensor.

Parameters
----------
arg1 : TensorView
The tensor to scatter into.
index : TensorView
The tensor containing the indices.
src : TensorView
The source tensor to scatter from.
dim : int
The dimension to scatter along.

Returns
-------
TensorView
The scattered tensor.
)",
py::return_value_policy::reference);
}

template <class ShapeType>
Expand Down
14 changes: 14 additions & 0 deletions python/python_direct/python_translate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,20 @@ class PythonTranslator : public OptInConstDispatch {
{out_tv});
}

// Map ScatterOp to python frontend
void handle(const ScatterOp* sop) final {
NVF_ERROR(sop != nullptr);
TensorView* out_tv = sop->output(0)->as<TensorView>();
visited_vals_.insert(out_tv);
static const std::vector<std::string> argument_names = {"dim"};
printer_.generateKwargsOperation(
"fd.ops.scatter",
std::make_tuple(sop->selfTv(), sop->indexTv(), sop->srcTv()),
argument_names,
std::make_tuple(sop->dim()),
{out_tv});
}

// Map TopKOp to python frontend
void handle(const TopKOp* topkop) final {
NVF_ERROR(topkop != nullptr);
Expand Down
1 change: 1 addition & 0 deletions tests/python/opinfo/opinfos.py
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,7 @@ def scatter_wrapper(
ArgumentType.Symbolic,
ArgumentType.Constant,
),
supports_direct_bindings=True,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: we don't have a functional scatter at this moment. @naoyam is adding limited support, which might not handle all the scatter cases in opinfo. #4742

)
shape_ops.append(scatter_opinfo)

Expand Down