const XlaComputation& computation,
tensorflow::gtl::ArraySlice<int64> window_dimensions,
tensorflow::gtl::ArraySlice<int64> window_strides, Padding padding) {
- return UnimplementedOp();
+ return NoteErrorOrReturn([&]() -> StatusOr<XlaOp> {
+ HloInstructionProto instr;
+
+ TF_ASSIGN_OR_RETURN(const Shape& operand_shape, GetShape(operand));
+ TF_RETURN_IF_ERROR(
+ ValidatePaddingValues(AsInt64Slice(operand_shape.dimensions()),
+ window_dimensions, window_strides));
+
+ std::vector<std::pair<int64, int64>> padding_values =
+ MakePadding(AsInt64Slice(operand_shape.dimensions()), window_dimensions,
+ window_strides, padding);
+ return ReduceWindowWithGeneralPadding(operand, init_value, computation,
+ window_dimensions, window_strides,
+ padding_values);
+ });
}
XlaOp XlaBuilder::ReduceWindowWithGeneralPadding(
tensorflow::gtl::ArraySlice<int64> window_dimensions,
tensorflow::gtl::ArraySlice<int64> window_strides,
tensorflow::gtl::ArraySlice<std::pair<int64, int64>> padding) {
- return UnimplementedOp();
+ return NoteErrorOrReturn([&]() -> StatusOr<XlaOp> {
+ HloInstructionProto instr;
+
+ TF_ASSIGN_OR_RETURN(const Shape& operand_shape, GetShape(operand));
+ TF_ASSIGN_OR_RETURN(const Shape& init_shape, GetShape(init_value));
+ TF_ASSIGN_OR_RETURN(const ProgramShape& to_apply_shape,
+ computation.GetProgramShape());
+ TF_ASSIGN_OR_RETURN(*instr.mutable_window(),
+ MakeWindow(window_dimensions, window_strides, padding,
+ /*lhs_dilation=*/{}, /*rhs_dilation=*/{}));
+ TF_ASSIGN_OR_RETURN(
+ *instr.mutable_shape(),
+ ShapeInference::InferReduceWindowShape(operand_shape, init_shape,
+ instr.window(), to_apply_shape));
+
+ AddCalledComputation(computation, &instr);
+ return AddInstruction(std::move(instr), HloOpcode::kReduceWindow,
+ {operand, init_value});
+ });
}
XlaOp XlaBuilder::BatchNormTraining(const XlaOp& operand, const XlaOp& scale,
#include "tensorflow/compiler/xla/array2d.h"
#include "tensorflow/compiler/xla/array3d.h"
#include "tensorflow/compiler/xla/array4d.h"
-#include "tensorflow/compiler/xla/client/computation_builder.h"
#include "tensorflow/compiler/xla/client/lib/arithmetic.h"
#include "tensorflow/compiler/xla/client/local_client.h"
#include "tensorflow/compiler/xla/client/padding.h"
+#include "tensorflow/compiler/xla/client/xla_client/xla_builder.h"
+#include "tensorflow/compiler/xla/client/xla_client/xla_computation.h"
#include "tensorflow/compiler/xla/reference_util.h"
#include "tensorflow/compiler/xla/shape_util.h"
#include "tensorflow/compiler/xla/tests/client_library_test_base.h"
class ReduceWindowTest : public ::testing::WithParamInterface<bool>,
public ReduceWindowTestBase {
public:
- ReduceWindowTest() : builder_(client_, TestName()) {
- set_use_bfloat16(GetParam());
- }
+ ReduceWindowTest() : builder_(TestName()) { set_use_bfloat16(GetParam()); }
- void ReduceWindowAdd(const ComputationDataHandle& input,
+ void ReduceWindowAdd(const XlaOp& input,
tensorflow::gtl::ArraySlice<int64> window_dimensions,
tensorflow::gtl::ArraySlice<int64> window_strides,
Padding padding) {
window_dimensions, window_strides, padding);
}
- void ReduceWindowMax(const ComputationDataHandle& input,
+ void ReduceWindowMax(const XlaOp& input,
tensorflow::gtl::ArraySlice<int64> window_dimensions,
tensorflow::gtl::ArraySlice<int64> window_strides,
Padding padding) {
auto init = CreateConstantFromLiteral(Literal::MinValue(F32), &builder_);
- builder_.ReduceWindow(input, init, CreateScalarMax(), window_dimensions,
- window_strides, padding);
+ builder_.ReduceWindow(input, init,
+ CreateScalarMaxComputation(FloatType(), &builder_),
+ window_dimensions, window_strides, padding);
}
- void ReduceWindowMin(const ComputationDataHandle& input,
+ void ReduceWindowMin(const XlaOp& input,
tensorflow::gtl::ArraySlice<int64> window_dimensions,
tensorflow::gtl::ArraySlice<int64> window_strides,
Padding padding) {
window_dimensions, window_strides, padding);
}
- ComputationBuilder builder_;
+ XlaBuilder builder_;
};
TEST_P(ReduceWindowTest, MismatchedRanksGivesErrorStatus) {
auto rhs = b->Parameter(1, scalar, "rhs");
b->Min(b->Add(lhs, rhs),
CreateConstantFromLiteral(*Literal::CreateR0<float>(8.0f), b.get()));
- Computation reduce_fn = b->BuildAndNoteError();
+ XlaComputation reduce_fn = b->BuildAndNoteError();
builder_.ReduceWindow(
input,
std::unique_ptr<Literal> input_literal =
Literal::CreateR4FromArray4DWithLayout(
input_array, LayoutUtil::MakeLayout({0, 3, 2, 1}));
- ComputationDataHandle input;
+ XlaOp input;
auto input_data = CreateParameterAndTransferLiteral(
0, *input_literal, "parameter", &builder_, &input);
std::unique_ptr<Literal> input_literal =
Literal::CreateR4FromArray4DWithLayout(
input_array, LayoutUtil::MakeLayout({3, 2, 1, 0}));
- ComputationDataHandle input;
+ XlaOp input;
auto input_data = CreateParameterAndTransferLiteral(
0, *input_literal, "parameter", &builder_, &input);
std::unique_ptr<Literal> input_literal =
Literal::CreateR4FromArray4DWithLayout(
input_array, LayoutUtil::MakeLayout({3, 2, 1, 0}));
- ComputationDataHandle input;
+ XlaOp input;
auto input_data = CreateParameterAndTransferLiteral(
0, *input_literal, "parameter", &builder_, &input);
std::unique_ptr<Literal> input_literal =
Literal::CreateR4FromArray4DWithLayout(
input_array, LayoutUtil::MakeLayout({3, 2, 1, 0}));
- ComputationDataHandle input;
+ XlaOp input;
auto input_data = CreateParameterAndTransferLiteral(
0, *input_literal, "parameter", &builder_, &input);
TEST_P(ReduceWindowTest, R2ReduceWindowNonOverlappingFromBroadcast) {
Array2D<float> input_array(6, 4, 1.0f);
- ComputationDataHandle input = builder_.Broadcast(
+ XlaOp input = builder_.Broadcast(
CreateConstantFromLiteral(Literal::One(F32), &builder_), {6, 4});
Padding padding = Padding::kSame;
R4ReduceWindowTest() { set_use_bfloat16(::testing::get<1>(GetParam())); }
void DoIt() {
- ComputationBuilder b(client_, TestName());
+ XlaBuilder b(TestName());
const auto& param = ::testing::get<0>(GetParam());
const float kInitValue = 0.0f;
std::unique_ptr<Literal> input_literal =
Literal::CreateR4FromArray4DWithLayout(
input, LayoutUtil::MakeLayout(param.layout));
- ComputationDataHandle parameter;
+ XlaOp parameter;
auto input_arg = CreateParameterAndTransferLiteral(0, *input_literal, "p0",
&b, ¶meter);
};
TEST_P(R3ReduceWindowTest, Add) {
- ComputationBuilder b(client_, TestName());
+ XlaBuilder b(TestName());
const auto& param = ::testing::get<0>(GetParam());
CHECK(param.reducer == kAdd);
Literal::CreateR3FromArray3DWithLayout(
input, LayoutUtil::MakeLayout(param.layout));
- ComputationDataHandle parameter;
+ XlaOp parameter;
auto input_arg = CreateParameterAndTransferLiteral(0, *input_literal, "p0",
&b, ¶meter);
auto init_value =
R2ReduceWindowTest() { set_use_bfloat16(::testing::get<1>(GetParam())); }
void DoIt() {
- ComputationBuilder b(client_, TestName());
+ XlaBuilder b(TestName());
const auto& param = ::testing::get<0>(GetParam());
CHECK(param.reducer == kAdd);
Literal::CreateR2FromArray2DWithLayout(
input, LayoutUtil::MakeLayout(param.layout));
- ComputationDataHandle parameter;
+ XlaOp parameter;
auto input_arg = CreateParameterAndTransferLiteral(0, *input_literal, "p0",
&b, ¶meter);
std::vector<std::pair<int64, int64>> padding(2);
};
TEST_P(R1ReduceWindowTest, DoIt) {
- ComputationBuilder b(client_, TestName());
+ XlaBuilder b(TestName());
const auto& param = ::testing::get<0>(GetParam());
CHECK(param.reducer == kAdd || param.reducer == kMax);
std::iota(std::begin(input_vector), std::end(input_vector), 0);
std::unique_ptr<Literal> input_literal =
Literal::CreateR1(tensorflow::gtl::ArraySlice<float>(input_vector));
- ComputationDataHandle parameter;
+ XlaOp parameter;
auto input_arg = CreateParameterAndTransferLiteral(0, *input_literal, "p0",
&b, ¶meter);