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

assorted onnx2hlo operators and related changes #2083

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
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
26 changes: 22 additions & 4 deletions src/neural/xla/hlo_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,18 @@ HloFlow HloBuilder::Gather(HloFlow input, HloFlow indices,
}

HloFlow HloBuilder::Dot(HloFlow lhs, HloFlow rhs,
const pblczero::XlaDotDimensionNumbers& dn) {
const pblczero::XlaDotDimensionNumbers& dn,
const pblczero::XlaShapeProto::Type out_type) {
HloTensorType lhs_shape(lhs->shape());
HloTensorType rhs_shape(rhs->shape());
HloTensorType new_shape(lhs_shape.GetElementType());
if (lhs_shape.GetElementType() != rhs_shape.GetElementType()) {
throw Exception("Dot operands must have the same element type");
HloTensorType new_shape(
out_type == pblczero::XlaShapeProto::PRIMITIVE_TYPE_INVALID
? lhs_shape.GetElementType()
: out_type);
if (lhs_shape.GetElementType() != rhs_shape.GetElementType() &&
out_type == pblczero::XlaShapeProto::PRIMITIVE_TYPE_INVALID) {
throw Exception(
"Dot operands must have the same element type or explicit output type");
}
if (dn.lhs_batch_dimensions_size() != dn.rhs_batch_dimensions_size()) {
throw Exception("Dot batch dimensions must have the same size");
Expand Down Expand Up @@ -471,6 +477,18 @@ HloFlow HloBuilder::Slice(
return flow;
}

HloFlow HloBuilder::RoundNearestEven(HloFlow input) {
return MakeInstruction("round-nearest-even", input->shape(), {input});
}

HloFlow HloBuilder::Clamp(HloFlow min, HloFlow input, HloFlow max) {
if (min->shape().dimensions() != input->shape().dimensions() ||
input->shape().dimensions() != max->shape().dimensions()) {
throw Exception("Clamp operands must have the same shape");
}
return MakeInstruction("clamp", input->shape(), {min, input, max});
}

namespace {
// Go over all "parameter" instructions of the computation and assign
// "parameter_number" field with increasing numbers.
Expand Down
6 changes: 5 additions & 1 deletion src/neural/xla/hlo_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ class HloBuilder {
HloFlow Maximum(HloFlow lhs, HloFlow rhs);
HloFlow Reshape(HloFlow input, const HloTensorType& new_shape);
HloFlow Dot(HloFlow lhs, HloFlow rhs,
const pblczero::XlaDotDimensionNumbers& dimension_numbers);
const pblczero::XlaDotDimensionNumbers& dimension_numbers,
const pblczero::XlaShapeProto::Type out_type =
pblczero::XlaShapeProto::PRIMITIVE_TYPE_INVALID);
HloFlow Slice(
HloFlow input,
const std::vector<pblczero::HloInstructionProto::SliceDimensions>& slice);
Expand All @@ -140,6 +142,8 @@ class HloBuilder {
// Direction is one of "EQ", "NE", "LT", "LE", "GT", "GE".
HloFlow Compare(HloFlow lhs, HloFlow rhs, std::string_view direction);
HloFlow Select(HloFlow condition, HloFlow on_true, HloFlow on_false);
HloFlow RoundNearestEven(HloFlow input);
HloFlow Clamp(HloFlow min, HloFlow input, HloFlow max);
// Insert a computation into the module, under given name. Dependent
// computations are also merged into the module.
HloComputation AddComputation(std::string_view name,
Expand Down
Loading