<commit_before>/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/compiler/xla/service/gpu/gpu_layout_assignment.h"

#include <memory>

#include "tensorflow/compiler/xla/layout_util.h"
#include "tensorflow/compiler/xla/service/gpu/gpu_options.h"
#include "tensorflow/compiler/xla/service/gpu/ir_emission_utils.h"
#include "tensorflow/compiler/xla/service/gpu/stream_executor_util.h"
#include "tensorflow/compiler/xla/service/hlo_computation.h"
#include "tensorflow/compiler/xla/service/hlo_instruction.h"
#include "tensorflow/compiler/xla/status_macros.h"
#include "tensorflow/compiler/xla/window_util.h"
#include "tensorflow/compiler/xla/xla_data.pb.h"
#include "tensorflow/core/lib/core/errors.h"

namespace xla {
namespace gpu {

using stream_executor::dnn::DataLayout;
using stream_executor::dnn::FilterLayout;

static bool IsVoltaOrLater(const se::StreamExecutor& stream_executor) {
  int major, minor;
  CHECK(stream_executor.GetDeviceDescription().cuda_compute_capability(&major,
                                                                       &minor));
  return major >= 7;
}

// Returns (input, filter, output) layouts.
static std::tuple<DataLayout, FilterLayout, DataLayout>
HeuristicLayoutAssignment(const HloInstruction* instr,
                          stream_executor::StreamExecutor* stream_executor) {
  // DataLayout and FilterLayout uses weird enum names. Translations:
  //   N <=> Batch or Output
  //   C <=> Depth or Input
  //   H <=> Y
  //   W <=> X
  //
  // Therefore kOutputInputYX means NHWC; kBatchDepthYX means NCHW.

  // As of today, our empirical evidence is that cudnn 7.0 is faster on V100 x
  // fp16 with the mostly-NHWC layout. The heuristic may change as cudnn version
  // changes, as well as the hardware updates.
  if (!(instr->operand(0)->shape().element_type() == xla::PrimitiveType::F16 &&
        IsVoltaOrLater(*stream_executor))) {
    return std::make_tuple(DataLayout::kBatchDepthYX,
                           FilterLayout::kOutputInputYX,
                           DataLayout::kBatchDepthYX);
  }
  VLOG(2) << "Using heuristic to figure out layouts for " << instr->ToString();
  // For BackwardInput that has stride, full NHWC layouts run significantly
  // slower than (NHWC, NCHW, NCHW) or (NHWC, NCHW, NHWC).
  //
  // TODO(timshen): more closely compare (NHWC, NCHW, NCHW) and (NHWC, NCHW,
  // NHWC).
  if (instr->custom_call_target() == kCudnnConvBackwardInputCallTarget &&
      window_util::HasStride(instr->window())) {
    return std::make_tuple(DataLayout::kBatchYXDepth,
                           FilterLayout::kOutputInputYX,
                           DataLayout::kBatchDepthYX);
  }
  return std::make_tuple(DataLayout::kBatchYXDepth,
                         FilterLayout::kOutputYXInput,
                         DataLayout::kBatchYXDepth);
}

// Adds layout constraints on the cudnn custom-call instruction. The layout
// constraints are represented in terms of minor_to_major fields of both
// operands and the output shape. Depending on the underlying algorithm, one of
// { NCHW, NHWC } ^ 3 = 8 different layout combinations may be chosen.
Status GpuLayoutAssignment::AddBackendConstraintsToDnnConvCustomCall(
    HloInstruction* instr, LayoutConstraints* constraints) {
  CHECK(IsCustomCallToDnnConvolution(*instr)) << instr->ToString();
  Shape input_shape;
  Shape filter_shape;
  Shape output_shape;
  const auto& target = instr->custom_call_target();
  if (target == kCudnnConvForwardCallTarget) {
    input_shape = instr->operand(0)->shape();
    filter_shape = instr->operand(1)->shape();
    output_shape = instr->shape().tuple_shapes(0);
  } else if (target == kCudnnConvBackwardInputCallTarget) {
    input_shape = instr->shape().tuple_shapes(0);
    filter_shape = instr->operand(1)->shape();
    output_shape = instr->operand(0)->shape();
  } else if (target == kCudnnConvBackwardFilterCallTarget) {
    input_shape = instr->operand(0)->shape();
    filter_shape = instr->shape().tuple_shapes(0);
    output_shape = instr->operand(1)->shape();
  } else {
    LOG(FATAL) << "Unexpected custom call target: "
               << instr->custom_call_target();
  }

  {
    DataLayout input;
    FilterLayout filter;
    DataLayout output;
    if (ConvUseLayoutHeuristic(instr->GetModule()->config())) {
      std::tie(input, filter, output) =
          HeuristicLayoutAssignment(instr, stream_executor_);
    } else {
      input = DataLayout::kBatchDepthYX;
      filter = FilterLayout::kOutputInputYX;
      output = DataLayout::kBatchDepthYX;
    }

    TF_ASSIGN_OR_RETURN(
        std::tie(*input_shape.mutable_layout(), *filter_shape.mutable_layout(),
                 *output_shape.mutable_layout()),
        StreamExecutorConvLayoutsToXlaLayouts(
            instr->convolution_dimension_numbers(), input, filter, output));
  }

  // The custom call returns a tuple of (actual_result, scratch_buffer);
  // call_result_buf is the logical buffer for actual_result, the thing that
  // contains the result of the conv call.
  TF_ASSIGN_OR_RETURN(const LogicalBuffer* call_result_buf,
                      constraints->points_to_analysis().GetBufferDefinedAt(
                          instr, /*index=*/{0}));

  // Set layouts of the instructions' shapes.
  if (target == kCudnnConvForwardCallTarget) {
    TF_RETURN_IF_ERROR(constraints->SetOperandLayout(input_shape, instr, 0));
    TF_RETURN_IF_ERROR(constraints->SetOperandLayout(filter_shape, instr, 1));
    TF_RETURN_IF_ERROR(
        constraints->SetBufferLayout(output_shape.layout(), *call_result_buf));
  } else if (target == kCudnnConvBackwardInputCallTarget) {
    TF_RETURN_IF_ERROR(constraints->SetOperandLayout(output_shape, instr, 0));
    TF_RETURN_IF_ERROR(constraints->SetOperandLayout(filter_shape, instr, 1));
    TF_RETURN_IF_ERROR(
        constraints->SetBufferLayout(input_shape.layout(), *call_result_buf));
  } else if (target == kCudnnConvBackwardFilterCallTarget) {
    TF_RETURN_IF_ERROR(constraints->SetOperandLayout(input_shape, instr, 0));
    TF_RETURN_IF_ERROR(constraints->SetOperandLayout(output_shape, instr, 1));
    TF_RETURN_IF_ERROR(
        constraints->SetBufferLayout(filter_shape.layout(), *call_result_buf));
  } else {
    LOG(FATAL) << "Unexpected custom call target: "
               << instr->custom_call_target();
  }
  return Status::OK();
}

Status GpuLayoutAssignment::AddBackendConstraints(
    LayoutConstraints* constraints) {
  // Add convolution constraints in reverse postorder that the earliest
  // convolution layout propagates first. This reduces the likelihood of fusion
  // nodes with copies.
  auto post_order = constraints->computation()->MakeInstructionPostOrder();
  for (auto iterator = post_order.rbegin(); iterator != post_order.rend();
       ++iterator) {
    HloInstruction* instruction = *iterator;
    if (IsCustomCallToDnnConvolution(*instruction)) {
      TF_RETURN_IF_ERROR(
          AddBackendConstraintsToDnnConvCustomCall(instruction, constraints));
    }
  }
  return Status::OK();
}

bool GpuLayoutAssignment::CustomCallRequiresMajorFirstLayout(
    const HloInstruction* instruction) {
  // - Inputs to cudnn batchnorm custom calls don't need the major-first layout
  //   (i.e. {n, n-1, ...0}) -- we can handle any layout.
  // - Inputs to cudnn convolution require custom layouts handled in
  //   AddBackendConstraints.
  return !IsCustomCallToDnnBatchNorm(*instruction) &&
         !IsCustomCallToDnnConvolution(*instruction);
}

Status GpuLayoutAssignment::PropagateOperandConstraint(
    const OperandLayoutConstraint& layout_constraint,
    LayoutConstraints* constraints) {
  const HloInstruction* instruction = layout_constraint.instruction();

  // cudnn batchnorm forward inference's result must have the same layout as its
  // operand 0.
  if (instruction->opcode() == HloOpcode::kCustomCall &&
      instruction->custom_call_target() ==
          kCudnnBatchNormForwardInferenceCallTarget &&
      layout_constraint.operand_no() == 0) {
    TF_RETURN_IF_ERROR(constraints->SetInstructionLayout(
        layout_constraint.shape_layout().shape(), instruction));
  }

  // cudnn batchnorm forward training returns a tuple {output, mean,
  // inverse-stddev}.  mean and inverse-stddev are rank 1 and so have only one
  // possible layout, but output is not (necessarily) rank 1, and, like in
  // batchnorm forward inference, must have the same layout as operand 0.
  if (instruction->opcode() == HloOpcode::kCustomCall &&
      instruction->custom_call_target() ==
          kCudnnBatchNormForwardTrainingCallTarget &&
      layout_constraint.operand_no() == 0) {
    TF_ASSIGN_OR_RETURN(const LogicalBuffer* out_buf,
                        constraints->points_to_analysis().GetBufferDefinedAt(
                            instruction, /*index=*/{0}));
    TF_RETURN_IF_ERROR(constraints->SetBufferLayout(
        layout_constraint.shape_layout().layout(), *out_buf));
  }

  // Like forward training, cudnn batchnorm backward returns a tuple {output,
  // mean, inverse-stddev}, and its operand 0 and 'output' must have the same
  // layout.  In addition, its operand 0 and operand 4 -- the 'operand' and
  // 'grad_output' parameters -- must have the same layout.
  if (instruction->opcode() == HloOpcode::kCustomCall &&
      instruction->custom_call_target() == kCudnnBatchNormBackwardCallTarget &&
      (layout_constraint.operand_no() == 0 ||
       layout_constraint.operand_no() == 4)) {
    TF_ASSIGN_OR_RETURN(const LogicalBuffer* out_buf,
                        constraints->points_to_analysis().GetBufferDefinedAt(
                            instruction, /*index=*/{0}));
    TF_RETURN_IF_ERROR(constraints->SetBufferLayout(
        layout_constraint.shape_layout().layout(), *out_buf));

    int64 operand_to_set = layout_constraint.operand_no() == 0 ? 4 : 0;
    TF_RETURN_IF_ERROR(constraints->SetOperandLayout(
        layout_constraint.shape_layout().shape(), instruction, operand_to_set));
  }

  return LayoutAssignment::PropagateOperandConstraint(layout_constraint,
                                                      constraints);
}

Status GpuLayoutAssignment::PropagateBufferConstraint(
    const BufferLayoutConstraint& buffer_constraint,
    LayoutConstraints* constraints) {
  const LogicalBuffer& buf = buffer_constraint.buffer();
  const HloInstruction* instruction = buf.instruction();

  Shape shape_with_layout = buf.shape();
  *shape_with_layout.mutable_layout() = buffer_constraint.layout();

  // Propagate output constraints to the operands of cudnn batchnorm ops.  This
  // is the same as PropagateOperandConstraint, just in the other direction.  We
  // need to both to fulfill our contract to LayoutAssignment.
  if (instruction->opcode() == HloOpcode::kCustomCall &&
      instruction->custom_call_target() ==
          kCudnnBatchNormForwardInferenceCallTarget) {
    TF_RETURN_IF_ERROR(constraints->SetOperandLayout(
        shape_with_layout, instruction, /*operand_no=*/0));
  }

  if (instruction->opcode() == HloOpcode::kCustomCall &&
      instruction->custom_call_target() ==
          kCudnnBatchNormForwardTrainingCallTarget &&
      buf.index() == ShapeIndex({0})) {
    TF_RETURN_IF_ERROR(constraints->SetOperandLayout(
        shape_with_layout, instruction, /*operand_no=*/0));
  }
  if (instruction->opcode() == HloOpcode::kCustomCall &&
      instruction->custom_call_target() == kCudnnBatchNormBackwardCallTarget &&
      buf.index() == ShapeIndex({0})) {
    // batchnorm backward has two operands, "operand" and "grad_output" whose
    // layouts must both match that of the result at tuple-index 0.
    TF_RETURN_IF_ERROR(constraints->SetOperandLayout(
        shape_with_layout, instruction, /*operand_no=*/0));
    TF_RETURN_IF_ERROR(constraints->SetOperandLayout(
        shape_with_layout, instruction, /*operand_no=*/4));
  }

  return LayoutAssignment::PropagateBufferConstraint(buffer_constraint,
                                                     constraints);
}

}  // namespace gpu
}  // namespace xla
<commit_msg>Fix a typo in comment to mention kOutputInputYX means NCHW<commit_after>/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/compiler/xla/service/gpu/gpu_layout_assignment.h"

#include <memory>

#include "tensorflow/compiler/xla/layout_util.h"
#include "tensorflow/compiler/xla/service/gpu/gpu_options.h"
#include "tensorflow/compiler/xla/service/gpu/ir_emission_utils.h"
#include "tensorflow/compiler/xla/service/gpu/stream_executor_util.h"
#include "tensorflow/compiler/xla/service/hlo_computation.h"
#include "tensorflow/compiler/xla/service/hlo_instruction.h"
#include "tensorflow/compiler/xla/status_macros.h"
#include "tensorflow/compiler/xla/window_util.h"
#include "tensorflow/compiler/xla/xla_data.pb.h"
#include "tensorflow/core/lib/core/errors.h"

namespace xla {
namespace gpu {

using stream_executor::dnn::DataLayout;
using stream_executor::dnn::FilterLayout;

static bool IsVoltaOrLater(const se::StreamExecutor& stream_executor) {
  int major, minor;
  CHECK(stream_executor.GetDeviceDescription().cuda_compute_capability(&major,
                                                                       &minor));
  return major >= 7;
}

// Returns (input, filter, output) layouts.
static std::tuple<DataLayout, FilterLayout, DataLayout>
HeuristicLayoutAssignment(const HloInstruction* instr,
                          stream_executor::StreamExecutor* stream_executor) {
  // DataLayout and FilterLayout uses weird enum names. Translations:
  //   N <=> Batch or Output
  //   C <=> Depth or Input
  //   H <=> Y
  //   W <=> X
  //
  // Therefore kOutputInputYX and kBatchDepthYX mean NCHW.

  // As of today, our empirical evidence is that cudnn 7.0 is faster on V100 x
  // fp16 with the mostly-NHWC layout. The heuristic may change as cudnn version
  // changes, as well as the hardware updates.
  if (!(instr->operand(0)->shape().element_type() == xla::PrimitiveType::F16 &&
        IsVoltaOrLater(*stream_executor))) {
    return std::make_tuple(DataLayout::kBatchDepthYX,
                           FilterLayout::kOutputInputYX,
                           DataLayout::kBatchDepthYX);
  }
  VLOG(2) << "Using heuristic to figure out layouts for " << instr->ToString();
  // For BackwardInput that has stride, full NHWC layouts run significantly
  // slower than (NHWC, NCHW, NCHW) or (NHWC, NCHW, NHWC).
  //
  // TODO(timshen): more closely compare (NHWC, NCHW, NCHW) and (NHWC, NCHW,
  // NHWC).
  if (instr->custom_call_target() == kCudnnConvBackwardInputCallTarget &&
      window_util::HasStride(instr->window())) {
    return std::make_tuple(DataLayout::kBatchYXDepth,
                           FilterLayout::kOutputInputYX,
                           DataLayout::kBatchDepthYX);
  }
  return std::make_tuple(DataLayout::kBatchYXDepth,
                         FilterLayout::kOutputYXInput,
                         DataLayout::kBatchYXDepth);
}

// Adds layout constraints on the cudnn custom-call instruction. The layout
// constraints are represented in terms of minor_to_major fields of both
// operands and the output shape. Depending on the underlying algorithm, one of
// { NCHW, NHWC } ^ 3 = 8 different layout combinations may be chosen.
Status GpuLayoutAssignment::AddBackendConstraintsToDnnConvCustomCall(
    HloInstruction* instr, LayoutConstraints* constraints) {
  CHECK(IsCustomCallToDnnConvolution(*instr)) << instr->ToString();
  Shape input_shape;
  Shape filter_shape;
  Shape output_shape;
  const auto& target = instr->custom_call_target();
  if (target == kCudnnConvForwardCallTarget) {
    input_shape = instr->operand(0)->shape();
    filter_shape = instr->operand(1)->shape();
    output_shape = instr->shape().tuple_shapes(0);
  } else if (target == kCudnnConvBackwardInputCallTarget) {
    input_shape = instr->shape().tuple_shapes(0);
    filter_shape = instr->operand(1)->shape();
    output_shape = instr->operand(0)->shape();
  } else if (target == kCudnnConvBackwardFilterCallTarget) {
    input_shape = instr->operand(0)->shape();
    filter_shape = instr->shape().tuple_shapes(0);
    output_shape = instr->operand(1)->shape();
  } else {
    LOG(FATAL) << "Unexpected custom call target: "
               << instr->custom_call_target();
  }

  {
    DataLayout input;
    FilterLayout filter;
    DataLayout output;
    if (ConvUseLayoutHeuristic(instr->GetModule()->config())) {
      std::tie(input, filter, output) =
          HeuristicLayoutAssignment(instr, stream_executor_);
    } else {
      input = DataLayout::kBatchDepthYX;
      filter = FilterLayout::kOutputInputYX;
      output = DataLayout::kBatchDepthYX;
    }

    TF_ASSIGN_OR_RETURN(
        std::tie(*input_shape.mutable_layout(), *filter_shape.mutable_layout(),
                 *output_shape.mutable_layout()),
        StreamExecutorConvLayoutsToXlaLayouts(
            instr->convolution_dimension_numbers(), input, filter, output));
  }

  // The custom call returns a tuple of (actual_result, scratch_buffer);
  // call_result_buf is the logical buffer for actual_result, the thing that
  // contains the result of the conv call.
  TF_ASSIGN_OR_RETURN(const LogicalBuffer* call_result_buf,
                      constraints->points_to_analysis().GetBufferDefinedAt(
                          instr, /*index=*/{0}));

  // Set layouts of the instructions' shapes.
  if (target == kCudnnConvForwardCallTarget) {
    TF_RETURN_IF_ERROR(constraints->SetOperandLayout(input_shape, instr, 0));
    TF_RETURN_IF_ERROR(constraints->SetOperandLayout(filter_shape, instr, 1));
    TF_RETURN_IF_ERROR(
        constraints->SetBufferLayout(output_shape.layout(), *call_result_buf));
  } else if (target == kCudnnConvBackwardInputCallTarget) {
    TF_RETURN_IF_ERROR(constraints->SetOperandLayout(output_shape, instr, 0));
    TF_RETURN_IF_ERROR(constraints->SetOperandLayout(filter_shape, instr, 1));
    TF_RETURN_IF_ERROR(
        constraints->SetBufferLayout(input_shape.layout(), *call_result_buf));
  } else if (target == kCudnnConvBackwardFilterCallTarget) {
    TF_RETURN_IF_ERROR(constraints->SetOperandLayout(input_shape, instr, 0));
    TF_RETURN_IF_ERROR(constraints->SetOperandLayout(output_shape, instr, 1));
    TF_RETURN_IF_ERROR(
        constraints->SetBufferLayout(filter_shape.layout(), *call_result_buf));
  } else {
    LOG(FATAL) << "Unexpected custom call target: "
               << instr->custom_call_target();
  }
  return Status::OK();
}

Status GpuLayoutAssignment::AddBackendConstraints(
    LayoutConstraints* constraints) {
  // Add convolution constraints in reverse postorder that the earliest
  // convolution layout propagates first. This reduces the likelihood of fusion
  // nodes with copies.
  auto post_order = constraints->computation()->MakeInstructionPostOrder();
  for (auto iterator = post_order.rbegin(); iterator != post_order.rend();
       ++iterator) {
    HloInstruction* instruction = *iterator;
    if (IsCustomCallToDnnConvolution(*instruction)) {
      TF_RETURN_IF_ERROR(
          AddBackendConstraintsToDnnConvCustomCall(instruction, constraints));
    }
  }
  return Status::OK();
}

bool GpuLayoutAssignment::CustomCallRequiresMajorFirstLayout(
    const HloInstruction* instruction) {
  // - Inputs to cudnn batchnorm custom calls don't need the major-first layout
  //   (i.e. {n, n-1, ...0}) -- we can handle any layout.
  // - Inputs to cudnn convolution require custom layouts handled in
  //   AddBackendConstraints.
  return !IsCustomCallToDnnBatchNorm(*instruction) &&
         !IsCustomCallToDnnConvolution(*instruction);
}

Status GpuLayoutAssignment::PropagateOperandConstraint(
    const OperandLayoutConstraint& layout_constraint,
    LayoutConstraints* constraints) {
  const HloInstruction* instruction = layout_constraint.instruction();

  // cudnn batchnorm forward inference's result must have the same layout as its
  // operand 0.
  if (instruction->opcode() == HloOpcode::kCustomCall &&
      instruction->custom_call_target() ==
          kCudnnBatchNormForwardInferenceCallTarget &&
      layout_constraint.operand_no() == 0) {
    TF_RETURN_IF_ERROR(constraints->SetInstructionLayout(
        layout_constraint.shape_layout().shape(), instruction));
  }

  // cudnn batchnorm forward training returns a tuple {output, mean,
  // inverse-stddev}.  mean and inverse-stddev are rank 1 and so have only one
  // possible layout, but output is not (necessarily) rank 1, and, like in
  // batchnorm forward inference, must have the same layout as operand 0.
  if (instruction->opcode() == HloOpcode::kCustomCall &&
      instruction->custom_call_target() ==
          kCudnnBatchNormForwardTrainingCallTarget &&
      layout_constraint.operand_no() == 0) {
    TF_ASSIGN_OR_RETURN(const LogicalBuffer* out_buf,
                        constraints->points_to_analysis().GetBufferDefinedAt(
                            instruction, /*index=*/{0}));
    TF_RETURN_IF_ERROR(constraints->SetBufferLayout(
        layout_constraint.shape_layout().layout(), *out_buf));
  }

  // Like forward training, cudnn batchnorm backward returns a tuple {output,
  // mean, inverse-stddev}, and its operand 0 and 'output' must have the same
  // layout.  In addition, its operand 0 and operand 4 -- the 'operand' and
  // 'grad_output' parameters -- must have the same layout.
  if (instruction->opcode() == HloOpcode::kCustomCall &&
      instruction->custom_call_target() == kCudnnBatchNormBackwardCallTarget &&
      (layout_constraint.operand_no() == 0 ||
       layout_constraint.operand_no() == 4)) {
    TF_ASSIGN_OR_RETURN(const LogicalBuffer* out_buf,
                        constraints->points_to_analysis().GetBufferDefinedAt(
                            instruction, /*index=*/{0}));
    TF_RETURN_IF_ERROR(constraints->SetBufferLayout(
        layout_constraint.shape_layout().layout(), *out_buf));

    int64 operand_to_set = layout_constraint.operand_no() == 0 ? 4 : 0;
    TF_RETURN_IF_ERROR(constraints->SetOperandLayout(
        layout_constraint.shape_layout().shape(), instruction, operand_to_set));
  }

  return LayoutAssignment::PropagateOperandConstraint(layout_constraint,
                                                      constraints);
}

Status GpuLayoutAssignment::PropagateBufferConstraint(
    const BufferLayoutConstraint& buffer_constraint,
    LayoutConstraints* constraints) {
  const LogicalBuffer& buf = buffer_constraint.buffer();
  const HloInstruction* instruction = buf.instruction();

  Shape shape_with_layout = buf.shape();
  *shape_with_layout.mutable_layout() = buffer_constraint.layout();

  // Propagate output constraints to the operands of cudnn batchnorm ops.  This
  // is the same as PropagateOperandConstraint, just in the other direction.  We
  // need to both to fulfill our contract to LayoutAssignment.
  if (instruction->opcode() == HloOpcode::kCustomCall &&
      instruction->custom_call_target() ==
          kCudnnBatchNormForwardInferenceCallTarget) {
    TF_RETURN_IF_ERROR(constraints->SetOperandLayout(
        shape_with_layout, instruction, /*operand_no=*/0));
  }

  if (instruction->opcode() == HloOpcode::kCustomCall &&
      instruction->custom_call_target() ==
          kCudnnBatchNormForwardTrainingCallTarget &&
      buf.index() == ShapeIndex({0})) {
    TF_RETURN_IF_ERROR(constraints->SetOperandLayout(
        shape_with_layout, instruction, /*operand_no=*/0));
  }
  if (instruction->opcode() == HloOpcode::kCustomCall &&
      instruction->custom_call_target() == kCudnnBatchNormBackwardCallTarget &&
      buf.index() == ShapeIndex({0})) {
    // batchnorm backward has two operands, "operand" and "grad_output" whose
    // layouts must both match that of the result at tuple-index 0.
    TF_RETURN_IF_ERROR(constraints->SetOperandLayout(
        shape_with_layout, instruction, /*operand_no=*/0));
    TF_RETURN_IF_ERROR(constraints->SetOperandLayout(
        shape_with_layout, instruction, /*operand_no=*/4));
  }

  return LayoutAssignment::PropagateBufferConstraint(buffer_constraint,
                                                     constraints);
}

}  // namespace gpu
}  // namespace xla
<|endoftext|>


<commit_before>//------------------------------------------------------------------------------
// File: minterface.hpp
//
// Desc: Interface for Data Mining.
//
// Copyright (c) 2014-2018. veyesys.com All rights reserved.
//------------------------------------------------------------------------------
#ifndef __M_INTERFACE_HPP__
#define __M_INTERFACE_HPP__
#include "utility.hpp"
#include "videotype.hpp"
#include "miningtype.hpp"

using namespace std;

typedef enum __MiningRetType
{
	MINING_RET_MOT = 1,
	MINING_RET_OBJS,
	MINING_RET_LAST
}MiningRetType;

typedef enum __MModuleFlag
{
	MM_MOT = 1,
	MM_TRACK = 2,
	MM_PEOPLE_CNT = 4,
	MM_ALPR = 8,
	MM_LAST
}MModuleFlag;

typedef enum __MMReqStream
{
	MM_STREAM_MAIN = 1,
	MM_STREAM_SUB = 2,
	MM_STREAM_MAIN_RAW = 4,
	MM_STREAM_SUB_RAW = 8,
	MM_STREAM_SEQ = 16,
	MM_STREAM_LAST
}MMReqStream;

typedef struct __MiningRet
{
	MiningRetType ret;
	union {
		VeObjGroup objs;
		VeMotionBox mot;
	} data;
}MiningRet;

#ifdef WIN32
typedef void (__cdecl * MiningCallbackFunctionPtr)(MiningRet& ret, void * pParam);
#else
typedef void ( * MiningCallbackFunctionPtr)(MiningRet& ret, void * pParam);
#endif

class MiningInterface
{
public:
	MiningInterface() {}
	virtual ~MiningInterface() {}

public:
	/* Process decoded or compressed data */
	virtual BOOL Process(VideoFrame& frame) = 0;
	virtual BOOL ProcessRaw(RawFrame& frame) = 0;
	virtual BOOL ProcessSeq(VideoSeqFrame& seq) = 0;

	/* Get the stream type of this module */
	virtual BOOL GetReqStream(MMReqStream& type) = 0;

	virtual BOOL RegRetCallback(MiningCallbackFunctionPtr pCallback, void * pParam) = 0;
	virtual BOOL UnRegDataCallback(void * pParam) = 0;
	virtual u32 GetFlags() = 0;
	virtual astring GetVersion() = 0;
	virtual u32 GetId() = 0;

};

/* Create MiningInterface from the module MiningCreateDevice function  */
typedef MiningInterface * (*MiningCreateDeviceFunc)(u32 id);

#ifdef __cplusplus
extern "C" {
#endif
VE_LIBRARY_API MiningInterface * MiningCreateDevice(u32 id);
#ifdef __cplusplus
}
#endif

#endif /* __M_INTERFACE_HPP__ */
<commit_msg>fix  a build error<commit_after>//------------------------------------------------------------------------------
// File: minterface.hpp
//
// Desc: Interface for Data Mining.
//
// Copyright (c) 2014-2018. veyesys.com All rights reserved.
//------------------------------------------------------------------------------
#ifndef __M_INTERFACE_HPP__
#define __M_INTERFACE_HPP__
#include "utility.hpp"
#include "videotype.hpp"
#include "hdfsrecsession.hpp"
#include "miningtype.hpp"

using namespace std;

typedef enum __MiningRetType
{
	MINING_RET_MOT = 1,
	MINING_RET_OBJS,
	MINING_RET_LAST
}MiningRetType;

typedef enum __MModuleFlag
{
	MM_MOT = 1,
	MM_TRACK = 2,
	MM_PEOPLE_CNT = 4,
	MM_ALPR = 8,
	MM_LAST
}MModuleFlag;

typedef enum __MMReqStream
{
	MM_STREAM_MAIN = 1,
	MM_STREAM_SUB = 2,
	MM_STREAM_MAIN_RAW = 4,
	MM_STREAM_SUB_RAW = 8,
	MM_STREAM_SEQ = 16,
	MM_STREAM_LAST
}MMReqStream;

typedef struct __MiningRet
{
	MiningRetType ret;
	union {
		VeObjGroup objs;
		VeMotionBox mot;
	} data;
}MiningRet;

#ifdef WIN32
typedef void (__cdecl * MiningCallbackFunctionPtr)(MiningRet& ret, void * pParam);
#else
typedef void ( * MiningCallbackFunctionPtr)(MiningRet& ret, void * pParam);
#endif

class MiningInterface
{
public:
	MiningInterface() {}
	virtual ~MiningInterface() {}

public:
	/* Process decoded or compressed data */
	virtual BOOL Process(VideoFrame& frame) = 0;
	virtual BOOL ProcessRaw(RawFrame& frame) = 0;
	virtual BOOL ProcessSeq(VideoSeqFrame& seq) = 0;

	/* Get the stream type of this module */
	virtual BOOL GetReqStream(MMReqStream& type) = 0;

	virtual BOOL RegRetCallback(MiningCallbackFunctionPtr pCallback, void * pParam) = 0;
	virtual BOOL UnRegDataCallback(void * pParam) = 0;
	virtual u32 GetFlags() = 0;
	virtual astring GetVersion() = 0;
	virtual u32 GetId() = 0;

};

/* Create MiningInterface from the module MiningCreateDevice function  */
typedef MiningInterface * (*MiningCreateDeviceFunc)(u32 id);

#ifdef __cplusplus
extern "C" {
#endif
VE_LIBRARY_API MiningInterface * MiningCreateDevice(u32 id);
#ifdef __cplusplus
}
#endif

#endif /* __M_INTERFACE_HPP__ */
<|endoftext|>


<commit_before>/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/compiler/xla/service/gpu/gpu_layout_assignment.h"

#include <memory>

#include "tensorflow/compiler/xla/layout_util.h"
#include "tensorflow/compiler/xla/service/gpu/ir_emission_utils.h"
#include "tensorflow/compiler/xla/service/gpu/stream_executor_util.h"
#include "tensorflow/compiler/xla/service/hlo_casting_utils.h"
#include "tensorflow/compiler/xla/service/hlo_computation.h"
#include "tensorflow/compiler/xla/service/hlo_instruction.h"
#include "tensorflow/compiler/xla/service/hlo_instructions.h"
#include "tensorflow/compiler/xla/status_macros.h"
#include "tensorflow/compiler/xla/window_util.h"
#include "tensorflow/compiler/xla/xla_data.pb.h"
#include "tensorflow/core/lib/core/errors.h"

namespace xla {
namespace gpu {

using se::dnn::DataLayout;
using se::dnn::FilterLayout;

// Returns (input, filter, output) layouts.
static std::tuple<DataLayout, FilterLayout, DataLayout>
HeuristicLayoutAssignment(const HloInstruction* instr,
                          se::StreamExecutor* stream_executor) {
  // DataLayout and FilterLayout uses weird enum names. Translations:
  //   N <=> Batch or Output
  //   C <=> Depth or Input
  //   H <=> Y
  //   W <=> X
  //
  // Therefore kOutputInputYX and kBatchDepthYX mean NCHW.
  //
  // If you have trouble keeping these straight, consider that all that matters
  // is the location of the channel dim: Is it major (NCHW), or minor (NHWC)?

  constexpr auto kAllNCHW =
      std::make_tuple(DataLayout::kBatchDepthYX, FilterLayout::kOutputInputYX,
                      DataLayout::kBatchDepthYX);
  constexpr auto kAllNHWC =
      std::make_tuple(DataLayout::kBatchYXDepth, FilterLayout::kOutputYXInput,
                      DataLayout::kBatchYXDepth);

  // If we're not Volta or not fp16, the decision is easy: Use NCHW.
  if (!(instr->operand(0)->shape().element_type() == xla::PrimitiveType::F16 &&
        IsVoltaOrLater(*stream_executor))) {
    return kAllNCHW;
  }

  VLOG(2) << "Using heuristic to figure out layouts for " << instr->ToString();

  // Empirically we've found with Volta and cudnn 7 that backward-input convs
  // with stride are significantly faster with NCHW layouts.
  //
  // We could have used a mixed layout combination, e.g. (NHWC, NCHW, NCHW),
  // which on paper gives good performance. However, there are two observations:
  // * a mixed layout combination is more cuDNN-bug prone, based on empirical
  //   envidence.
  // * we've also observed that for mixed layouts, cuDNN transposes data back
  //   and forth from a different layout combination. If we end up with
  //   transposes anyway, we prefer to have them in XLA, as they can be fused.
  // TODO(timshen): Figure out the exact condition. This may be achieved by
  // auto-tuning layouts offline.
  if (instr->custom_call_target() == kCudnnConvBackwardInputCallTarget &&
      window_util::HasStride(instr->window())) {
    return kAllNCHW;
  }

  // For other Volta f16 convolutions, use NHWC.
  return kAllNHWC;
}

// Adds layout constraints on the cudnn custom-call instruction. The layout
// constraints are represented in terms of minor_to_major fields of both
// operands and the output shape. Depending on the underlying algorithm, one of
// { NCHW, NHWC } ^ 3 = 8 different layout combinations may be chosen.
Status GpuLayoutAssignment::AddBackendConstraintsToDnnConvCustomCall(
    HloCustomCallInstruction* instr, LayoutConstraints* constraints) {
  Shape lhs_shape = instr->operand(0)->shape();
  Shape rhs_shape = instr->operand(1)->shape();
  Shape result_shape = instr->shape().tuple_shapes(0);

  Shape* input_shape;
  Shape* filter_shape;
  Shape* output_shape;

  TF_ASSIGN_OR_RETURN(auto kind, GetCudnnConvKind(instr));
  switch (kind) {
    case CudnnConvKind::kForward:
    case CudnnConvKind::kForwardActivation:
      input_shape = &lhs_shape;
      filter_shape = &rhs_shape;
      output_shape = &result_shape;
      break;
    case CudnnConvKind::kBackwardInput:
      input_shape = &result_shape;
      filter_shape = &rhs_shape;
      output_shape = &lhs_shape;
      break;
    case CudnnConvKind::kBackwardFilter:
      input_shape = &lhs_shape;
      filter_shape = &result_shape;
      output_shape = &rhs_shape;
      break;
  }

  {
    DataLayout input;
    FilterLayout filter;
    DataLayout output;
    std::tie(input, filter, output) =
        HeuristicLayoutAssignment(instr, stream_executor_);

    TF_ASSIGN_OR_RETURN(
        std::tie(*input_shape->mutable_layout(),
                 *filter_shape->mutable_layout(),
                 *output_shape->mutable_layout()),
        StreamExecutorConvLayoutsToXlaLayouts(
            instr->convolution_dimension_numbers(), input, filter, output));
  }

  // The custom call returns a tuple of (actual_result, scratch_buffer);
  // call_result_buf is the logical buffer for actual_result, the thing that
  // contains the result of the conv call.
  TF_ASSIGN_OR_RETURN(const LogicalBuffer* call_result_buf,
                      constraints->points_to_analysis().GetBufferDefinedAt(
                          instr, /*index=*/{0}));

  // Set layouts of the instructions' shapes.
  TF_RETURN_IF_ERROR(constraints->SetOperandLayout(lhs_shape, instr, 0));
  TF_RETURN_IF_ERROR(constraints->SetOperandLayout(rhs_shape, instr, 1));
  TF_RETURN_IF_ERROR(
      constraints->SetBufferLayout(result_shape.layout(), *call_result_buf));
  // instr->operand(2), if exists, is the bias buffer. There is no need to
  // assign layout to it, as it has only one dimension.

  // instr->opernad(3), if exists, is the side input buffer.
  if (instr->operand_count() == 4) {
    if (kind != CudnnConvKind::kForwardActivation) {
      return InternalError(
          "Invalid convolution. Conv has a side input, but kind is not fused "
          "conv forward: %s",
          instr->ToString());
    }
    // The side input layout must match the output layout.
    TF_RETURN_IF_ERROR(constraints->SetOperandLayout(*output_shape, instr, 3));
  }
  return Status::OK();
}

Status GpuLayoutAssignment::AddBackendConstraints(
    LayoutConstraints* constraints) {
  // Add convolution constraints in reverse postorder that the earliest
  // convolution layout propagates first. This reduces the likelihood of fusion
  // nodes with copies.
  auto post_order = constraints->computation()->MakeInstructionPostOrder();
  for (auto iterator = post_order.rbegin(); iterator != post_order.rend();
       ++iterator) {
    HloInstruction* instruction = *iterator;
    if (IsCustomCallToDnnConvolution(*instruction)) {
      TF_RETURN_IF_ERROR(AddBackendConstraintsToDnnConvCustomCall(
          Cast<HloCustomCallInstruction>(instruction), constraints));
    }

    // For batched dot we require the default layout.
    // TODO(b/112111608): This is overly conservative, the only real restriction
    // is that batch dimensions must be major.
    if (instruction->opcode() == HloOpcode::kDot &&
        ImplementedAsGemm(*instruction) &&
        instruction->dot_dimension_numbers().lhs_batch_dimensions_size() > 0) {
      // Verify that the batch dims come before the row and col dims.
      const DotDimensionNumbers& dim_nums =
          instruction->dot_dimension_numbers();
      CHECK_EQ(dim_nums.lhs_batch_dimensions_size(),
               dim_nums.rhs_batch_dimensions_size());
      CHECK_EQ(dim_nums.lhs_batch_dimensions_size() + 2,
               ShapeUtil::Rank(instruction->shape()));
      for (int64 batch_dim : dim_nums.lhs_batch_dimensions()) {
        CHECK_LT(batch_dim, ShapeUtil::Rank(instruction->shape()) - 2);
      }

      // Set both inputs and the output to default layout.
      Shape op0_shape = instruction->operand(0)->shape();
      LayoutUtil::SetToDefaultLayout(&op0_shape);
      Shape op1_shape = instruction->operand(1)->shape();
      LayoutUtil::SetToDefaultLayout(&op1_shape);
      Shape output_shape = instruction->shape();
      LayoutUtil::SetToDefaultLayout(&output_shape);
      TF_RETURN_IF_ERROR(
          constraints->SetOperandLayout(op0_shape, instruction, 0));
      TF_RETURN_IF_ERROR(
          constraints->SetOperandLayout(op1_shape, instruction, 1));
      TF_RETURN_IF_ERROR(
          constraints->SetInstructionLayout(output_shape, instruction));
    } else if (instruction->opcode() == HloOpcode::kSort &&
               ShapeUtil::Rank(instruction->operand(0)->shape()) > 1) {
      // Make sure that all the operands and the output(s) have the same layout.
      Shape keys_shape = instruction->operand(0)->shape();
      Layout keys_layout =
          LayoutUtil::GetDefaultLayoutForRank(ShapeUtil::Rank(keys_shape));
      for (int64 i = 0; i < instruction->operand_count(); ++i) {
        Shape shape = instruction->operand(i)->shape();
        *shape.mutable_layout() = keys_layout;
        TF_RETURN_IF_ERROR(
            constraints->SetOperandLayout(shape, instruction, i));
        const LogicalBuffer* output_buffer;
        if (ShapeUtil::IsArray(instruction->shape())) {
          TF_ASSIGN_OR_RETURN(
              output_buffer,
              constraints->points_to_analysis().GetBufferDefinedAt(instruction,
                                                                   {}));
        } else {
          TF_ASSIGN_OR_RETURN(
              output_buffer,
              constraints->points_to_analysis().GetBufferDefinedAt(instruction,
                                                                   {i}));
        }
        TF_RETURN_IF_ERROR(
            constraints->SetBufferLayout(keys_layout, *output_buffer));
      }
    }
  }
  return Status::OK();
}

Status GpuLayoutAssignment::PropagateOperandConstraint(
    const OperandLayoutConstraint& layout_constraint,
    LayoutConstraints* constraints) {
  const HloInstruction* instruction = layout_constraint.instruction();

  // cudnn batchnorm forward inference's result must have the same layout as its
  // operand 0.
  if (instruction->opcode() == HloOpcode::kCustomCall &&
      instruction->custom_call_target() ==
          kCudnnBatchNormForwardInferenceCallTarget &&
      layout_constraint.operand_no() == 0) {
    TF_RETURN_IF_ERROR(constraints->SetInstructionLayout(
        layout_constraint.shape_layout().shape(), instruction));
  }

  // cudnn batchnorm forward training returns a tuple {output, mean,
  // inverse-stddev}.  mean and inverse-stddev are rank 1 and so have only one
  // possible layout, but output is not (necessarily) rank 1, and, like in
  // batchnorm forward inference, must have the same layout as operand 0.
  if (instruction->opcode() == HloOpcode::kCustomCall &&
      instruction->custom_call_target() ==
          kCudnnBatchNormForwardTrainingCallTarget &&
      layout_constraint.operand_no() == 0) {
    TF_ASSIGN_OR_RETURN(const LogicalBuffer* out_buf,
                        constraints->points_to_analysis().GetBufferDefinedAt(
                            instruction, /*index=*/{0}));
    TF_RETURN_IF_ERROR(constraints->SetBufferLayout(
        layout_constraint.shape_layout().layout(), *out_buf));
  }

  // Like forward training, cudnn batchnorm backward returns a tuple {output,
  // mean, inverse-stddev}, and its operand 0 and 'output' must have the same
  // layout.  In addition, its operand 0 and operand 4 -- the 'operand' and
  // 'grad_output' parameters -- must have the same layout.
  if (instruction->opcode() == HloOpcode::kCustomCall &&
      instruction->custom_call_target() == kCudnnBatchNormBackwardCallTarget &&
      (layout_constraint.operand_no() == 0 ||
       layout_constraint.operand_no() == 4)) {
    TF_ASSIGN_OR_RETURN(const LogicalBuffer* out_buf,
                        constraints->points_to_analysis().GetBufferDefinedAt(
                            instruction, /*index=*/{0}));
    TF_RETURN_IF_ERROR(constraints->SetBufferLayout(
        layout_constraint.shape_layout().layout(), *out_buf));

    int64 operand_to_set = layout_constraint.operand_no() == 0 ? 4 : 0;
    TF_RETURN_IF_ERROR(constraints->SetOperandLayout(
        layout_constraint.shape_layout().shape(), instruction, operand_to_set));
  }

  return LayoutAssignment::PropagateOperandConstraint(layout_constraint,
                                                      constraints);
}

Status GpuLayoutAssignment::PropagateBufferConstraint(
    const BufferLayoutConstraint& buffer_constraint,
    LayoutConstraints* constraints) {
  const LogicalBuffer& buf = buffer_constraint.buffer();
  const HloInstruction* instruction = buf.instruction();

  Shape shape_with_layout = buf.shape();
  *shape_with_layout.mutable_layout() = buffer_constraint.layout();

  // Propagate output constraints to the operands of cudnn batchnorm ops.  This
  // is the same as PropagateOperandConstraint, just in the other direction.  We
  // need to both to fulfill our contract to LayoutAssignment.
  if (instruction->opcode() == HloOpcode::kCustomCall &&
      instruction->custom_call_target() ==
          kCudnnBatchNormForwardInferenceCallTarget) {
    TF_RETURN_IF_ERROR(constraints->SetOperandLayout(
        shape_with_layout, instruction, /*operand_no=*/0));
  }

  if (instruction->opcode() == HloOpcode::kCustomCall &&
      instruction->custom_call_target() ==
          kCudnnBatchNormForwardTrainingCallTarget &&
      buf.index() == ShapeIndex({0})) {
    TF_RETURN_IF_ERROR(constraints->SetOperandLayout(
        shape_with_layout, instruction, /*operand_no=*/0));
  }
  if (instruction->opcode() == HloOpcode::kCustomCall &&
      instruction->custom_call_target() == kCudnnBatchNormBackwardCallTarget &&
      buf.index() == ShapeIndex({0})) {
    // batchnorm backward has two operands, "operand" and "grad_output" whose
    // layouts must both match that of the result at tuple-index 0.
    TF_RETURN_IF_ERROR(constraints->SetOperandLayout(
        shape_with_layout, instruction, /*operand_no=*/0));
    TF_RETURN_IF_ERROR(constraints->SetOperandLayout(
        shape_with_layout, instruction, /*operand_no=*/4));
  }

  return LayoutAssignment::PropagateBufferConstraint(buffer_constraint,
                                                     constraints);
}

}  // namespace gpu
}  // namespace xla
<commit_msg>For cuDNN >= 7.4, undo the work-around for NHWC with >2 strides for conv backward data.<commit_after>/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/compiler/xla/service/gpu/gpu_layout_assignment.h"

#include <memory>

#include "tensorflow/compiler/xla/layout_util.h"
#include "tensorflow/compiler/xla/service/gpu/ir_emission_utils.h"
#include "tensorflow/compiler/xla/service/gpu/stream_executor_util.h"
#include "tensorflow/compiler/xla/service/hlo_casting_utils.h"
#include "tensorflow/compiler/xla/service/hlo_computation.h"
#include "tensorflow/compiler/xla/service/hlo_instruction.h"
#include "tensorflow/compiler/xla/service/hlo_instructions.h"
#include "tensorflow/compiler/xla/status_macros.h"
#include "tensorflow/compiler/xla/window_util.h"
#include "tensorflow/compiler/xla/xla_data.pb.h"
#include "tensorflow/core/lib/core/errors.h"

namespace xla {
namespace gpu {

using se::dnn::DataLayout;
using se::dnn::FilterLayout;

// Returns (input, filter, output) layouts.
static std::tuple<DataLayout, FilterLayout, DataLayout>
HeuristicLayoutAssignment(const HloInstruction* instr,
                          se::StreamExecutor* stream_executor) {
  // DataLayout and FilterLayout uses weird enum names. Translations:
  //   N <=> Batch or Output
  //   C <=> Depth or Input
  //   H <=> Y
  //   W <=> X
  //
  // Therefore kOutputInputYX and kBatchDepthYX mean NCHW.
  //
  // If you have trouble keeping these straight, consider that all that matters
  // is the location of the channel dim: Is it major (NCHW), or minor (NHWC)?

  constexpr auto kAllNCHW =
      std::make_tuple(DataLayout::kBatchDepthYX, FilterLayout::kOutputInputYX,
                      DataLayout::kBatchDepthYX);
  constexpr auto kAllNHWC =
      std::make_tuple(DataLayout::kBatchYXDepth, FilterLayout::kOutputYXInput,
                      DataLayout::kBatchYXDepth);

  // If we're not Volta or not fp16, the decision is easy: Use NCHW.
  if (!(instr->operand(0)->shape().element_type() == xla::PrimitiveType::F16 &&
        IsVoltaOrLater(*stream_executor))) {
    return kAllNCHW;
  }

  VLOG(2) << "Using heuristic to figure out layouts for " << instr->ToString();

  // Empirically we've found with Volta and cudnn <= 7.3 that backward-input
  // convs with stride are significantly faster with NCHW layouts.
  //
  // We could have used a mixed layout combination, e.g. (NHWC, NCHW, NCHW),
  // which on paper gives good performance. However, there are two observations:
  // * a mixed layout combination is more cuDNN-bug prone, based on empirical
  //   envidence.
  // * we've also observed that for mixed layouts, cuDNN transposes data back
  //   and forth from a different layout combination. If we end up with
  //   transposes anyway, we prefer to have them in XLA, as they can be fused.
  if (auto* dnn = stream_executor->AsDnn()) {
    auto version_status = dnn->GetVersion();
    if (version_status.ok()) {
      auto version = version_status.ConsumeValueOrDie();
      if (std::make_tuple(version.major_version(), version.minor_version()) <=
              std::make_tuple(7, 3) &&
          instr->custom_call_target() == kCudnnConvBackwardInputCallTarget &&
          window_util::HasStride(instr->window())) {
        return kAllNCHW;
      }
    }
  }

  // For other Volta f16 convolutions, use NHWC.
  return kAllNHWC;
}

// Adds layout constraints on the cudnn custom-call instruction. The layout
// constraints are represented in terms of minor_to_major fields of both
// operands and the output shape. Depending on the underlying algorithm, one of
// { NCHW, NHWC } ^ 3 = 8 different layout combinations may be chosen.
Status GpuLayoutAssignment::AddBackendConstraintsToDnnConvCustomCall(
    HloCustomCallInstruction* instr, LayoutConstraints* constraints) {
  Shape lhs_shape = instr->operand(0)->shape();
  Shape rhs_shape = instr->operand(1)->shape();
  Shape result_shape = instr->shape().tuple_shapes(0);

  Shape* input_shape;
  Shape* filter_shape;
  Shape* output_shape;

  TF_ASSIGN_OR_RETURN(auto kind, GetCudnnConvKind(instr));
  switch (kind) {
    case CudnnConvKind::kForward:
    case CudnnConvKind::kForwardActivation:
      input_shape = &lhs_shape;
      filter_shape = &rhs_shape;
      output_shape = &result_shape;
      break;
    case CudnnConvKind::kBackwardInput:
      input_shape = &result_shape;
      filter_shape = &rhs_shape;
      output_shape = &lhs_shape;
      break;
    case CudnnConvKind::kBackwardFilter:
      input_shape = &lhs_shape;
      filter_shape = &result_shape;
      output_shape = &rhs_shape;
      break;
  }

  {
    DataLayout input;
    FilterLayout filter;
    DataLayout output;
    std::tie(input, filter, output) =
        HeuristicLayoutAssignment(instr, stream_executor_);

    TF_ASSIGN_OR_RETURN(
        std::tie(*input_shape->mutable_layout(),
                 *filter_shape->mutable_layout(),
                 *output_shape->mutable_layout()),
        StreamExecutorConvLayoutsToXlaLayouts(
            instr->convolution_dimension_numbers(), input, filter, output));
  }

  // The custom call returns a tuple of (actual_result, scratch_buffer);
  // call_result_buf is the logical buffer for actual_result, the thing that
  // contains the result of the conv call.
  TF_ASSIGN_OR_RETURN(const LogicalBuffer* call_result_buf,
                      constraints->points_to_analysis().GetBufferDefinedAt(
                          instr, /*index=*/{0}));

  // Set layouts of the instructions' shapes.
  TF_RETURN_IF_ERROR(constraints->SetOperandLayout(lhs_shape, instr, 0));
  TF_RETURN_IF_ERROR(constraints->SetOperandLayout(rhs_shape, instr, 1));
  TF_RETURN_IF_ERROR(
      constraints->SetBufferLayout(result_shape.layout(), *call_result_buf));
  // instr->operand(2), if exists, is the bias buffer. There is no need to
  // assign layout to it, as it has only one dimension.

  // instr->opernad(3), if exists, is the side input buffer.
  if (instr->operand_count() == 4) {
    if (kind != CudnnConvKind::kForwardActivation) {
      return InternalError(
          "Invalid convolution. Conv has a side input, but kind is not fused "
          "conv forward: %s",
          instr->ToString());
    }
    // The side input layout must match the output layout.
    TF_RETURN_IF_ERROR(constraints->SetOperandLayout(*output_shape, instr, 3));
  }
  return Status::OK();
}

Status GpuLayoutAssignment::AddBackendConstraints(
    LayoutConstraints* constraints) {
  // Add convolution constraints in reverse postorder that the earliest
  // convolution layout propagates first. This reduces the likelihood of fusion
  // nodes with copies.
  auto post_order = constraints->computation()->MakeInstructionPostOrder();
  for (auto iterator = post_order.rbegin(); iterator != post_order.rend();
       ++iterator) {
    HloInstruction* instruction = *iterator;
    if (IsCustomCallToDnnConvolution(*instruction)) {
      TF_RETURN_IF_ERROR(AddBackendConstraintsToDnnConvCustomCall(
          Cast<HloCustomCallInstruction>(instruction), constraints));
    }

    // For batched dot we require the default layout.
    // TODO(b/112111608): This is overly conservative, the only real restriction
    // is that batch dimensions must be major.
    if (instruction->opcode() == HloOpcode::kDot &&
        ImplementedAsGemm(*instruction) &&
        instruction->dot_dimension_numbers().lhs_batch_dimensions_size() > 0) {
      // Verify that the batch dims come before the row and col dims.
      const DotDimensionNumbers& dim_nums =
          instruction->dot_dimension_numbers();
      CHECK_EQ(dim_nums.lhs_batch_dimensions_size(),
               dim_nums.rhs_batch_dimensions_size());
      CHECK_EQ(dim_nums.lhs_batch_dimensions_size() + 2,
               ShapeUtil::Rank(instruction->shape()));
      for (int64 batch_dim : dim_nums.lhs_batch_dimensions()) {
        CHECK_LT(batch_dim, ShapeUtil::Rank(instruction->shape()) - 2);
      }

      // Set both inputs and the output to default layout.
      Shape op0_shape = instruction->operand(0)->shape();
      LayoutUtil::SetToDefaultLayout(&op0_shape);
      Shape op1_shape = instruction->operand(1)->shape();
      LayoutUtil::SetToDefaultLayout(&op1_shape);
      Shape output_shape = instruction->shape();
      LayoutUtil::SetToDefaultLayout(&output_shape);
      TF_RETURN_IF_ERROR(
          constraints->SetOperandLayout(op0_shape, instruction, 0));
      TF_RETURN_IF_ERROR(
          constraints->SetOperandLayout(op1_shape, instruction, 1));
      TF_RETURN_IF_ERROR(
          constraints->SetInstructionLayout(output_shape, instruction));
    } else if (instruction->opcode() == HloOpcode::kSort &&
               ShapeUtil::Rank(instruction->operand(0)->shape()) > 1) {
      // Make sure that all the operands and the output(s) have the same layout.
      Shape keys_shape = instruction->operand(0)->shape();
      Layout keys_layout =
          LayoutUtil::GetDefaultLayoutForRank(ShapeUtil::Rank(keys_shape));
      for (int64 i = 0; i < instruction->operand_count(); ++i) {
        Shape shape = instruction->operand(i)->shape();
        *shape.mutable_layout() = keys_layout;
        TF_RETURN_IF_ERROR(
            constraints->SetOperandLayout(shape, instruction, i));
        const LogicalBuffer* output_buffer;
        if (ShapeUtil::IsArray(instruction->shape())) {
          TF_ASSIGN_OR_RETURN(
              output_buffer,
              constraints->points_to_analysis().GetBufferDefinedAt(instruction,
                                                                   {}));
        } else {
          TF_ASSIGN_OR_RETURN(
              output_buffer,
              constraints->points_to_analysis().GetBufferDefinedAt(instruction,
                                                                   {i}));
        }
        TF_RETURN_IF_ERROR(
            constraints->SetBufferLayout(keys_layout, *output_buffer));
      }
    }
  }
  return Status::OK();
}

Status GpuLayoutAssignment::PropagateOperandConstraint(
    const OperandLayoutConstraint& layout_constraint,
    LayoutConstraints* constraints) {
  const HloInstruction* instruction = layout_constraint.instruction();

  // cudnn batchnorm forward inference's result must have the same layout as its
  // operand 0.
  if (instruction->opcode() == HloOpcode::kCustomCall &&
      instruction->custom_call_target() ==
          kCudnnBatchNormForwardInferenceCallTarget &&
      layout_constraint.operand_no() == 0) {
    TF_RETURN_IF_ERROR(constraints->SetInstructionLayout(
        layout_constraint.shape_layout().shape(), instruction));
  }

  // cudnn batchnorm forward training returns a tuple {output, mean,
  // inverse-stddev}.  mean and inverse-stddev are rank 1 and so have only one
  // possible layout, but output is not (necessarily) rank 1, and, like in
  // batchnorm forward inference, must have the same layout as operand 0.
  if (instruction->opcode() == HloOpcode::kCustomCall &&
      instruction->custom_call_target() ==
          kCudnnBatchNormForwardTrainingCallTarget &&
      layout_constraint.operand_no() == 0) {
    TF_ASSIGN_OR_RETURN(const LogicalBuffer* out_buf,
                        constraints->points_to_analysis().GetBufferDefinedAt(
                            instruction, /*index=*/{0}));
    TF_RETURN_IF_ERROR(constraints->SetBufferLayout(
        layout_constraint.shape_layout().layout(), *out_buf));
  }

  // Like forward training, cudnn batchnorm backward returns a tuple {output,
  // mean, inverse-stddev}, and its operand 0 and 'output' must have the same
  // layout.  In addition, its operand 0 and operand 4 -- the 'operand' and
  // 'grad_output' parameters -- must have the same layout.
  if (instruction->opcode() == HloOpcode::kCustomCall &&
      instruction->custom_call_target() == kCudnnBatchNormBackwardCallTarget &&
      (layout_constraint.operand_no() == 0 ||
       layout_constraint.operand_no() == 4)) {
    TF_ASSIGN_OR_RETURN(const LogicalBuffer* out_buf,
                        constraints->points_to_analysis().GetBufferDefinedAt(
                            instruction, /*index=*/{0}));
    TF_RETURN_IF_ERROR(constraints->SetBufferLayout(
        layout_constraint.shape_layout().layout(), *out_buf));

    int64 operand_to_set = layout_constraint.operand_no() == 0 ? 4 : 0;
    TF_RETURN_IF_ERROR(constraints->SetOperandLayout(
        layout_constraint.shape_layout().shape(), instruction, operand_to_set));
  }

  return LayoutAssignment::PropagateOperandConstraint(layout_constraint,
                                                      constraints);
}

Status GpuLayoutAssignment::PropagateBufferConstraint(
    const BufferLayoutConstraint& buffer_constraint,
    LayoutConstraints* constraints) {
  const LogicalBuffer& buf = buffer_constraint.buffer();
  const HloInstruction* instruction = buf.instruction();

  Shape shape_with_layout = buf.shape();
  *shape_with_layout.mutable_layout() = buffer_constraint.layout();

  // Propagate output constraints to the operands of cudnn batchnorm ops.  This
  // is the same as PropagateOperandConstraint, just in the other direction.  We
  // need to both to fulfill our contract to LayoutAssignment.
  if (instruction->opcode() == HloOpcode::kCustomCall &&
      instruction->custom_call_target() ==
          kCudnnBatchNormForwardInferenceCallTarget) {
    TF_RETURN_IF_ERROR(constraints->SetOperandLayout(
        shape_with_layout, instruction, /*operand_no=*/0));
  }

  if (instruction->opcode() == HloOpcode::kCustomCall &&
      instruction->custom_call_target() ==
          kCudnnBatchNormForwardTrainingCallTarget &&
      buf.index() == ShapeIndex({0})) {
    TF_RETURN_IF_ERROR(constraints->SetOperandLayout(
        shape_with_layout, instruction, /*operand_no=*/0));
  }
  if (instruction->opcode() == HloOpcode::kCustomCall &&
      instruction->custom_call_target() == kCudnnBatchNormBackwardCallTarget &&
      buf.index() == ShapeIndex({0})) {
    // batchnorm backward has two operands, "operand" and "grad_output" whose
    // layouts must both match that of the result at tuple-index 0.
    TF_RETURN_IF_ERROR(constraints->SetOperandLayout(
        shape_with_layout, instruction, /*operand_no=*/0));
    TF_RETURN_IF_ERROR(constraints->SetOperandLayout(
        shape_with_layout, instruction, /*operand_no=*/4));
  }

  return LayoutAssignment::PropagateBufferConstraint(buffer_constraint,
                                                     constraints);
}

}  // namespace gpu
}  // namespace xla
<|endoftext|>


<commit_before>/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/compiler/xla/service/mlir_gpu/emission_context.h"

#include "absl/strings/substitute.h"
#include "mlir/IR/Location.h"  // TF:local_config_mlir
#include "mlir/IR/MLIRContext.h"  // TF:local_config_mlir
#include "tensorflow/compiler/xla/service/hlo_instruction.h"

namespace xla {
namespace mlir_gpu {

EmissionContext::EmissionContext(std::unique_ptr<HloModule> module)
    : module_(std::move(module)), context_() {
  error_handler_ = [](const ErrorMap& instructions_with_error,
                      HloModule* module) {
    std::set<const HloComputation*> computations_with_error;
    for (auto err : instructions_with_error) {
      computations_with_error.insert(err.first->parent());
    }

    // TODO(tanyabelova): Log the most relevant lines of large computations
    // only.
    LOG(ERROR) << module->ToString(
        HloPrintOptions()
            .set_format_instruction(
                // Returns the string representation of `instr` in the following
                // format. ROOT? instr_name
                //   FAILED: err_0
                //   FAILED: err_1
                //   ...
                [&instructions_with_error](const HloInstruction* instr,
                                           const string& instr_name, int indent,
                                           bool is_root) {
                  string tab;
                  for (int i = 0; i < indent; i++) {
                    tab += "  ";
                  }
                  string result = tab + (is_root ? "ROOT " : "") + instr_name;
                  if (!instructions_with_error.count(instr)) {
                    return result;
                  }
                  for (const string& err : instructions_with_error.at(instr)) {
                    absl::SubstituteAndAppend(&result, "\n$0  FAILED: $1", tab,
                                              err);
                  }
                  return result;
                })
            .set_print_computation(
                [&computations_with_error](const HloComputation* comp) {
                  return computations_with_error.find(comp) !=
                         computations_with_error.end();
                }));
  };
  registerDiagnosticHandler();
}

EmissionContext::EmissionContext(
    std::unique_ptr<HloModule> module,
    std::function<void(const ErrorMap&, HloModule*)> callback)
    : module_(std::move(module)), context_(), error_handler_(callback) {
  registerDiagnosticHandler();
}

EmissionContext::~EmissionContext() { callErrorHandlerCallback(); }

mlir::Location EmissionContext::getLocation(const HloInstruction* instr) {
  return mlir::OpaqueLoc::get<const HloInstruction*>(instr, &context_);
}

void EmissionContext::addError(const HloInstruction* hlo_instruction,
                               const string& str) {
  instructions_with_error_[hlo_instruction].push_back(str);
}

void EmissionContext::setErrorHandler(
    std::function<void(const ErrorMap&, HloModule*)> callback) {
  error_handler_ = callback;
}

std::unique_ptr<HloModule> EmissionContext::releaseHloModule() {
  callErrorHandlerCallback();
  return std::move(module_);
}

HloModule* EmissionContext::getHloModule() const { return module_.get(); }

mlir::MLIRContext* EmissionContext::getContext() { return &context_; }

void EmissionContext::registerDiagnosticHandler() {
  context_.getDiagEngine().registerHandler([&](mlir::Diagnostic& diag) {
    const HloInstruction* hloInstruction =
        mlir::OpaqueLoc::getUnderlyingLocationOrNull<const HloInstruction*>(
            diag.getLocation());
    assert(hloInstruction);
    addError(hloInstruction, diag.str());
    return mlir::success();
  });
}

void EmissionContext::callErrorHandlerCallback() {
  if (module_.get() && !instructions_with_error_.empty()) {
    error_handler_(instructions_with_error_, module_.get());
  }
}

}  // namespace mlir_gpu
}  // namespace xla
<commit_msg>Refactor formatting lambda.<commit_after>/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/compiler/xla/service/mlir_gpu/emission_context.h"

#include "absl/strings/substitute.h"
#include "mlir/IR/Location.h"  // TF:local_config_mlir
#include "mlir/IR/MLIRContext.h"  // TF:local_config_mlir
#include "tensorflow/compiler/xla/service/hlo_instruction.h"

namespace xla {
namespace mlir_gpu {

EmissionContext::EmissionContext(std::unique_ptr<HloModule> module)
    : module_(std::move(module)), context_() {
  error_handler_ = [](const ErrorMap& instructions_with_error,
                      HloModule* module) {
    std::set<const HloComputation*> computations_with_error;
    for (auto err : instructions_with_error) {
      computations_with_error.insert(err.first->parent());
    }

    // TODO(tanyabelova): Log the most relevant lines of large computations
    // only.
    LOG(ERROR) << module->ToString(
        HloPrintOptions()
            .set_format_instruction(
                // Returns the string representation of `instr` in the following
                // format. ROOT? instr_name
                //   FAILED: err_0
                //   FAILED: err_1
                //   ...
                [&instructions_with_error](const HloInstruction* instr,
                                           const string& instr_name, int indent,
                                           bool is_root) {
                  const string tab(2 * indent, ' ');
                  string result =
                      absl::StrCat(tab, is_root ? "ROOT " : "", instr_name);
                  if (!instructions_with_error.count(instr)) {
                    return result;
                  }
                  for (const string& err : instructions_with_error.at(instr)) {
                    absl::SubstituteAndAppend(&result, "\n$0  FAILED: $1", tab,
                                              err);
                  }
                  return result;
                })
            .set_print_computation(
                [&computations_with_error](const HloComputation* comp) {
                  return computations_with_error.find(comp) !=
                         computations_with_error.end();
                }));
  };
  registerDiagnosticHandler();
}

EmissionContext::EmissionContext(
    std::unique_ptr<HloModule> module,
    std::function<void(const ErrorMap&, HloModule*)> callback)
    : module_(std::move(module)), context_(), error_handler_(callback) {
  registerDiagnosticHandler();
}

EmissionContext::~EmissionContext() { callErrorHandlerCallback(); }

mlir::Location EmissionContext::getLocation(const HloInstruction* instr) {
  return mlir::OpaqueLoc::get<const HloInstruction*>(instr, &context_);
}

void EmissionContext::addError(const HloInstruction* hlo_instruction,
                               const string& str) {
  instructions_with_error_[hlo_instruction].push_back(str);
}

void EmissionContext::setErrorHandler(
    std::function<void(const ErrorMap&, HloModule*)> callback) {
  error_handler_ = callback;
}

std::unique_ptr<HloModule> EmissionContext::releaseHloModule() {
  callErrorHandlerCallback();
  return std::move(module_);
}

HloModule* EmissionContext::getHloModule() const { return module_.get(); }

mlir::MLIRContext* EmissionContext::getContext() { return &context_; }

void EmissionContext::registerDiagnosticHandler() {
  context_.getDiagEngine().registerHandler([&](mlir::Diagnostic& diag) {
    const HloInstruction* hloInstruction =
        mlir::OpaqueLoc::getUnderlyingLocationOrNull<const HloInstruction*>(
            diag.getLocation());
    assert(hloInstruction);
    addError(hloInstruction, diag.str());
    return mlir::success();
  });
}

void EmissionContext::callErrorHandlerCallback() {
  if (module_.get() && !instructions_with_error_.empty()) {
    error_handler_(instructions_with_error_, module_.get());
  }
}

}  // namespace mlir_gpu
}  // namespace xla
<|endoftext|>


<commit_before>/*
   Copyright (C) 2017 Alexandr Akulich <akulichalexander@gmail.com>

   This file is a part of TelegramQt library.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

 */

#include "DcConfiguration.hpp"
#include "DefaultAuthorizationProvider.hpp"
#include "LocalCluster.hpp"
#include "RandomGenerator.hpp"
#include "Session.hpp"
#include "TelegramServerConfig.hpp"
#include "TelegramServerUser.hpp"
#include "Utils.hpp"

// Test keys
#include "keys_data.hpp"

#include <QCommandLineParser>
#include <QCoreApplication>
#include <QDebug>
#include <QFile>
#include <QStandardPaths>

using namespace Telegram::Server;

class ConstantAuthCodeProvider : public Authorization::DefaultProvider
{
public:
    static QString s_code;
protected:
    Authorization::Code generateCode(Session *session, const QString &identifier) override;
};

QString ConstantAuthCodeProvider::s_code = QStringLiteral("11111");

Authorization::Code ConstantAuthCodeProvider::generateCode(Session *session, const QString &identifier)
{
    Authorization::Code code;
    code.hash = Telegram::RandomGenerator::instance()->generate(8).toHex();
    code.code = s_code;
    code.type = Authorization::Code::Type::Default;
    qInfo().nospace().noquote() << "sendAppCode(" << identifier << '/' << session->id() << "):"
                                << " hash: " << code.hash
                                << " code: " << code.code;
    return code;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    a.setOrganizationName(QLatin1String("TelegramQt"));
    a.setApplicationName(QLatin1String("TelegramTestServer"));
    const QString persistentKeyFilePath = QStandardPaths::writableLocation(QStandardPaths::TempLocation)
            + QLatin1Literal("/TelegramTestServer.pem");

    Telegram::initialize();
    if (!TestKeyData::initKeyFiles()) {
        qCritical() << "Unable to init RSA key files.";
        return -1;
    }

    const Telegram::RsaKey privateKey = Telegram::RsaKey::fromFile(TestKeyData::privateKeyFileName());
    if (!privateKey.isValid()) {
        qCritical() << "Unable to read RSA key.";
        return -1;
    }

    QCommandLineParser parser;
    parser.addHelpOption();

    QCommandLineOption ipAddressOption(QStringList({ QLatin1String("a"), QLatin1String("address") }));
    ipAddressOption.setValueName(QLatin1String("ip"));
    ipAddressOption.setDefaultValue(QLatin1String("127.0.0.1"));
    parser.addOption(ipAddressOption);

    QCommandLineOption portOption(QStringList({ QLatin1String("p"), QLatin1String("port") }));
    portOption.setDescription(QLatin1String("first port"));
    portOption.setValueName(QLatin1String("port"));
    portOption.setDefaultValue(QLatin1String("10443"));
    parser.addOption(portOption);

    parser.process(a);

    Telegram::DcConfiguration dcConfig = Config().serverConfiguration();
    dcConfig.dcOptions.clear();

    for (quint32 i = 0; i < 3; ++i) {
        Telegram::DcOption dcOption;
        dcOption.id = i + 1;
        // A DC that does not accepts any transport considered as invalid in some client.
        // dcOption.flags = Telegram::DcOption::TcpOnly;
        dcOption.address = parser.value(ipAddressOption);
        dcOption.port = static_cast<quint16>(parser.value(portOption).toUInt() + i);
        dcConfig.dcOptions.append(dcOption);
    }

    LocalCluster cluster;
    cluster.setServerPrivateRsaKey(privateKey);
    cluster.setServerConfiguration(dcConfig);

    ConstantAuthCodeProvider authProvider;
    cluster.setAuthorizationProvider(&authProvider);
    qInfo() << "Custom auth code provider enabled. The code:" << ConstantAuthCodeProvider::s_code;
    qInfo() << "Start server with unsafe (not secret) encryption RSA key:";
    qInfo() << "    Private:" << TestKeyData::privateKeyFileName();
    qInfo() << "    Public PKCS1:" << TestKeyData::publicKeyPkcs1FileName();
    qInfo() << "    Public PKCS8:" << TestKeyData::publicKeyPkcs8FileName();

    QFile::remove(persistentKeyFilePath);
    if (QFile::copy(TestKeyData::publicKeyPkcs1FileName(), persistentKeyFilePath)) {
        qInfo() << "    Persistent file location:" << persistentKeyFilePath;
    } else {
        qWarning() << "Unable to save the RSA key in a persistent location.";
    }
    if (!cluster.start()) {
        return -2;
    }

    int retCode = a.exec();
    cluster.stop();

    TestKeyData::cleanupKeyFiles();
    return retCode;
}
<commit_msg>TestServer: Add data load on start and autosave every 30 seconds<commit_after>/*
   Copyright (C) 2017 Alexandr Akulich <akulichalexander@gmail.com>

   This file is a part of TelegramQt library.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

 */

#include "DcConfiguration.hpp"
#include "DefaultAuthorizationProvider.hpp"
#include "JsonDataImporter.hpp"
#include "LocalCluster.hpp"
#include "RandomGenerator.hpp"
#include "Session.hpp"
#include "TelegramServerConfig.hpp"
#include "TelegramServerUser.hpp"
#include "Utils.hpp"

// Test keys
#include "keys_data.hpp"

#include <QCommandLineParser>
#include <QCoreApplication>
#include <QDebug>
#include <QFile>
#include <QStandardPaths>
#include <QTimer>

using namespace Telegram::Server;

class ConstantAuthCodeProvider : public Authorization::DefaultProvider
{
public:
    static QString s_code;
protected:
    Authorization::Code generateCode(Session *session, const QString &identifier) override;
};

QString ConstantAuthCodeProvider::s_code = QStringLiteral("11111");

Authorization::Code ConstantAuthCodeProvider::generateCode(Session *session, const QString &identifier)
{
    Authorization::Code code;
    code.hash = Telegram::RandomGenerator::instance()->generate(8).toHex();
    code.code = s_code;
    code.type = Authorization::Code::Type::Default;
    qInfo().nospace().noquote() << "sendAppCode(" << identifier << '/' << session->id() << "):"
                                << " hash: " << code.hash
                                << " code: " << code.code;
    return code;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    a.setOrganizationName(QLatin1String("TelegramQt"));
    a.setApplicationName(QLatin1String("TelegramTestServer"));
    const QString persistentKeyFilePath = QStandardPaths::writableLocation(QStandardPaths::TempLocation)
            + QLatin1Literal("/TelegramTestServer.pem");

    Telegram::initialize();
    if (!TestKeyData::initKeyFiles()) {
        qCritical() << "Unable to init RSA key files.";
        return -1;
    }

    const Telegram::RsaKey privateKey = Telegram::RsaKey::fromFile(TestKeyData::privateKeyFileName());
    if (!privateKey.isValid()) {
        qCritical() << "Unable to read RSA key.";
        return -1;
    }

    QCommandLineParser parser;
    parser.addHelpOption();

    QCommandLineOption ipAddressOption(QStringList({ QLatin1String("a"), QLatin1String("address") }));
    ipAddressOption.setValueName(QLatin1String("ip"));
    ipAddressOption.setDefaultValue(QLatin1String("127.0.0.1"));
    parser.addOption(ipAddressOption);

    QCommandLineOption portOption(QStringList({ QLatin1String("p"), QLatin1String("port") }));
    portOption.setDescription(QLatin1String("first port"));
    portOption.setValueName(QLatin1String("port"));
    portOption.setDefaultValue(QLatin1String("10443"));
    parser.addOption(portOption);

    parser.process(a);

    Telegram::DcConfiguration dcConfig = Config().serverConfiguration();
    dcConfig.dcOptions.clear();

    for (quint32 i = 0; i < 3; ++i) {
        Telegram::DcOption dcOption;
        dcOption.id = i + 1;
        // A DC that does not accepts any transport considered as invalid in some client.
        // dcOption.flags = Telegram::DcOption::TcpOnly;
        dcOption.address = parser.value(ipAddressOption);
        dcOption.port = static_cast<quint16>(parser.value(portOption).toUInt() + i);
        dcConfig.dcOptions.append(dcOption);
    }

    LocalCluster cluster;
    cluster.setServerPrivateRsaKey(privateKey);
    cluster.setServerConfiguration(dcConfig);

    ConstantAuthCodeProvider authProvider;
    cluster.setAuthorizationProvider(&authProvider);
    qInfo() << "Custom auth code provider enabled. The code:" << ConstantAuthCodeProvider::s_code;
    qInfo() << "Start server with unsafe (not secret) encryption RSA key:";
    qInfo() << "    Private:" << TestKeyData::privateKeyFileName();
    qInfo() << "    Public PKCS1:" << TestKeyData::publicKeyPkcs1FileName();
    qInfo() << "    Public PKCS8:" << TestKeyData::publicKeyPkcs8FileName();

    QFile::remove(persistentKeyFilePath);
    if (QFile::copy(TestKeyData::publicKeyPkcs1FileName(), persistentKeyFilePath)) {
        qInfo() << "    Persistent file location:" << persistentKeyFilePath;
    } else {
        qWarning() << "Unable to save the RSA key in a persistent location.";
    }

    if (!cluster.start()) {
        return -2;
    }

    JsonDataImporter importer;
    importer.setBaseDirectory(QLatin1String("TelegramServer/io"));
    importer.setTarget(&cluster);
    importer.loadData();

    QTimer saveTimer;
    saveTimer.setInterval(30000);
    saveTimer.setSingleShot(false);
    QObject::connect(&saveTimer, &QTimer::timeout, [&importer]() {
        qInfo() << "Sync server data";
        importer.saveData();
    });
    saveTimer.start();

    int retCode = a.exec();
    cluster.stop();

    importer.saveData();

    TestKeyData::cleanupKeyFiles();
    return retCode;
}
<|endoftext|>


<commit_before>#include "BLEPad_UART.h"

BLEPad_UART::BLEPad_UART(HardwareSerial &s) {
    hs = &s;
};

int BLEPad_UART::available() {
    return hs->available();
}

void begin(unsigned long baud) {
    return hs->begin(baud);
}

size_t BLEPad_UART::write(uint8_t c) {
    return hs->write(c);
}

void BLEPad_UART::println(const char data[]) {
    hs->println(data);
}

size_t BLEPad_UART::println(int n, int base) {
    return hs->println(n, base);
}

int BLEPad_UART::read() {
    return hs->read();
}

<commit_msg>fix syntax<commit_after>#include "BLEPad_UART.h"

BLEPad_UART::BLEPad_UART(HardwareSerial *s) {
    hs = s;
};

void BLEPad_UART::begin(unsigned long baud) {
    hs->begin(baud);
}

size_t BLEPad_UART::write(uint8_t c) {
    return hs->write(c);
}

void BLEPad_UART::println(const char data[]) {
    hs->println(data);
}

size_t BLEPad_UART::println(int n, int base) {
    return hs->println(n, base);
}

int BLEPad_UART::read() {
    return hs->read();
}

int BLEPad_UART::available() {
    return hs->available();
}

<|endoftext|>


<commit_before>// This file is part of DVS-ROS - the RPG DVS ROS Package

#include "dvs_renderer/renderer.h"
#include <std_msgs/Float32.h>

namespace dvs_renderer {

Renderer::Renderer(ros::NodeHandle & nh, ros::NodeHandle nh_private) : nh_(nh),
    image_tracking_(nh)
{
  got_camera_info_ = false;

  // get parameters of display method
  std::string display_method_str;
  nh_private.param<std::string>("display_method", display_method_str, "");
  display_method_ = (display_method_str == std::string("grayscale")) ? GRAYSCALE : RED_BLUE;
  nh_private.param<bool>("color", color_image_, false);
  used_last_image_ = false;

  // setup subscribers and publishers
  event_sub_ = nh_.subscribe("events", 1, &Renderer::eventsCallback, this);

  camera_info_sub_ = nh_.subscribe("camera_info", 1, &Renderer::cameraInfoCallback, this);

  image_transport::ImageTransport it_(nh_);
  image_sub_ = it_.subscribe("image", 1, &Renderer::imageCallback, this);
  image_pub_ = it_.advertise("dvs_rendering", 1);
  undistorted_image_pub_ = it_.advertise("dvs_undistorted", 1);

  for (int i = 0; i < 2; ++i)
    for (int k = 0; k < 2; ++k)
      event_stats_[i].events_counter_[k] = 0;
  event_stats_[0].dt = 1;
  event_stats_[0].events_mean_lasttime_ = 0;
  event_stats_[0].events_mean_[0] = nh_.advertise<std_msgs::Float32>("events_on_mean_1", 1);
  event_stats_[0].events_mean_[1] = nh_.advertise<std_msgs::Float32>("events_off_mean_1", 1);
  event_stats_[1].dt = 5;
  event_stats_[1].events_mean_lasttime_ = 0;
  event_stats_[1].events_mean_[0] = nh_.advertise<std_msgs::Float32>("events_on_mean_5", 1);
  event_stats_[1].events_mean_[1] = nh_.advertise<std_msgs::Float32>("events_off_mean_5", 1);
}

Renderer::~Renderer()
{
  image_pub_.shutdown();
  undistorted_image_pub_.shutdown();
}

void Renderer::cameraInfoCallback(const sensor_msgs::CameraInfo::ConstPtr& msg)
{
  got_camera_info_ = true;

  camera_matrix_ = cv::Mat(3, 3, CV_64F);
  for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++)
      camera_matrix_.at<double>(cv::Point(i, j)) = msg->K[i+j*3];

  dist_coeffs_ = cv::Mat(msg->D.size(), 1, CV_64F);
  for (int i = 0; i < msg->D.size(); i++)
    dist_coeffs_.at<double>(i) = msg->D[i];
}

void Renderer::imageCallback(const sensor_msgs::Image::ConstPtr& msg)
{
  image_tracking_.imageCallback(msg);

  cv_bridge::CvImagePtr cv_ptr;

  try
  {
    cv_ptr = cv_bridge::toCvCopy(msg);
  }
  catch (cv_bridge::Exception& e)
  {
    ROS_ERROR("cv_bridge exception: %s", e.what());
    return;
  }

  // convert to BGR image
  if (msg->encoding == "rgb8") cv::cvtColor(cv_ptr->image, last_image_, CV_RGB2BGR);
  if (msg->encoding == "mono8")
  {
    if (color_image_)
    {
      cv::cvtColor(cv_ptr->image, last_image_, CV_BayerBG2BGR);
    }
    else
    {
      cv::cvtColor(cv_ptr->image, last_image_, CV_GRAY2BGR);
    }
  }

  std::cout << msg->encoding << std::endl;

  if (!used_last_image_)
  {
    cv_bridge::CvImage cv_image;
    last_image_.copyTo(cv_image.image);
    cv_image.encoding = "bgr8";
    std::cout << "publish image from callback" << std::endl;
    image_pub_.publish(cv_image.toImageMsg());
  }
  used_last_image_ = false;
}

void Renderer::eventsCallback(const dvs_msgs::EventArray::ConstPtr& msg)
{
  for (int i = 0; i < msg->events.size(); ++i)
  {
    ++event_stats_[0].events_counter_[msg->events[i].polarity];
    ++event_stats_[1].events_counter_[msg->events[i].polarity];
  }

  publishStats();
  image_tracking_.eventsCallback(msg);

  // only create image if at least one subscriber
  if (image_pub_.getNumSubscribers() > 0)
  {
    cv_bridge::CvImage cv_image;
    if (msg->events.size() > 0)
    {
      cv_image.header.stamp = msg->events[msg->events.size()/2].ts;
    }

    if (display_method_ == RED_BLUE)
    {
      cv_image.encoding = "bgr8";

      if (last_image_.rows == msg->height && last_image_.cols == msg->width)
      {
        last_image_.copyTo(cv_image.image);
        used_last_image_ = true;
      }
      else
      {
        cv_image.image = cv::Mat(msg->height, msg->width, CV_8UC3);
        cv_image.image = cv::Scalar(0,0,0);
      }

      for (int i = 0; i < msg->events.size(); ++i)
      {
        const int x = msg->events[i].x;
        const int y = msg->events[i].y;

        cv_image.image.at<cv::Vec3b>(cv::Point(x, y)) = (
            msg->events[i].polarity == true ? cv::Vec3b(255, 0, 0) : cv::Vec3b(0, 0, 255));
      }
    }
    else
    {
      cv_image.encoding = "mono8";
      if (last_image_.rows == msg->height && last_image_.cols == msg->width)
      {
        cv::cvtColor(last_image_, cv_image.image, CV_BGR2GRAY);
        used_last_image_ = true;
      }
      else
      {
        cv_image.image = cv::Mat(msg->height, msg->width, CV_8U);
        cv_image.image = cv::Scalar(128);
      }

      cv::Mat on_events = cv::Mat(msg->height, msg->width, CV_8U);
      on_events = cv::Scalar(0);

      cv::Mat off_events = cv::Mat(msg->height, msg->width, CV_8U);
      off_events = cv::Scalar(0);

      // count events per pixels with polarity
      for (int i = 0; i < msg->events.size(); ++i)
      {
        const int x = msg->events[i].x;
        const int y = msg->events[i].y;

        if (msg->events[i].polarity == 1)
          on_events.at<uint8_t>(cv::Point(x, y))++;
        else
          off_events.at<uint8_t>(cv::Point(x, y))++;
      }

        // scale image
      cv::normalize(on_events, on_events, 0, 128, cv::NORM_MINMAX, CV_8UC1);
      cv::normalize(off_events, off_events, 0, 127, cv::NORM_MINMAX, CV_8UC1);

      cv_image.image += on_events;
      cv_image.image -= off_events;
    }

    image_pub_.publish(cv_image.toImageMsg());

    if (got_camera_info_ && undistorted_image_pub_.getNumSubscribers() > 0)
    {
      cv_bridge::CvImage cv_image2;
      cv_image2.encoding = cv_image.encoding;
      cv::undistort(cv_image.image, cv_image2.image, camera_matrix_, dist_coeffs_);
      undistorted_image_pub_.publish(cv_image2.toImageMsg());
    }
  }
}

void Renderer::publishStats()
{
  std_msgs::Float32 msg;
  ros::Time now = ros::Time::now();
  for (int i = 0; i < 2; ++i)
  {
    if (event_stats_[i].events_mean_lasttime_ + event_stats_[i].dt <= now.toSec()) {
      event_stats_[i].events_mean_lasttime_ = now.toSec();
      for (int k = 0; k < 2; ++k)
      {
        msg.data = (float)event_stats_[i].events_counter_[k] / event_stats_[i].dt;
        event_stats_[i].events_mean_[k].publish(msg);
        event_stats_[i].events_counter_[k] = 0;
      }
    }
  }
}

} // namespace
<commit_msg>removing print line<commit_after>// This file is part of DVS-ROS - the RPG DVS ROS Package

#include "dvs_renderer/renderer.h"
#include <std_msgs/Float32.h>

namespace dvs_renderer {

Renderer::Renderer(ros::NodeHandle & nh, ros::NodeHandle nh_private) : nh_(nh),
    image_tracking_(nh)
{
  got_camera_info_ = false;

  // get parameters of display method
  std::string display_method_str;
  nh_private.param<std::string>("display_method", display_method_str, "");
  display_method_ = (display_method_str == std::string("grayscale")) ? GRAYSCALE : RED_BLUE;
  nh_private.param<bool>("color", color_image_, false);
  used_last_image_ = false;

  // setup subscribers and publishers
  event_sub_ = nh_.subscribe("events", 1, &Renderer::eventsCallback, this);
  camera_info_sub_ = nh_.subscribe("camera_info", 1, &Renderer::cameraInfoCallback, this);

  image_transport::ImageTransport it_(nh_);
  image_sub_ = it_.subscribe("image", 1, &Renderer::imageCallback, this);
  image_pub_ = it_.advertise("dvs_rendering", 1);
  undistorted_image_pub_ = it_.advertise("dvs_undistorted", 1);

  for (int i = 0; i < 2; ++i)
    for (int k = 0; k < 2; ++k)
      event_stats_[i].events_counter_[k] = 0;
  event_stats_[0].dt = 1;
  event_stats_[0].events_mean_lasttime_ = 0;
  event_stats_[0].events_mean_[0] = nh_.advertise<std_msgs::Float32>("events_on_mean_1", 1);
  event_stats_[0].events_mean_[1] = nh_.advertise<std_msgs::Float32>("events_off_mean_1", 1);
  event_stats_[1].dt = 5;
  event_stats_[1].events_mean_lasttime_ = 0;
  event_stats_[1].events_mean_[0] = nh_.advertise<std_msgs::Float32>("events_on_mean_5", 1);
  event_stats_[1].events_mean_[1] = nh_.advertise<std_msgs::Float32>("events_off_mean_5", 1);
}

Renderer::~Renderer()
{
  image_pub_.shutdown();
  undistorted_image_pub_.shutdown();
}

void Renderer::cameraInfoCallback(const sensor_msgs::CameraInfo::ConstPtr& msg)
{
  got_camera_info_ = true;

  camera_matrix_ = cv::Mat(3, 3, CV_64F);
  for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++)
      camera_matrix_.at<double>(cv::Point(i, j)) = msg->K[i+j*3];

  dist_coeffs_ = cv::Mat(msg->D.size(), 1, CV_64F);
  for (int i = 0; i < msg->D.size(); i++)
    dist_coeffs_.at<double>(i) = msg->D[i];
}

void Renderer::imageCallback(const sensor_msgs::Image::ConstPtr& msg)
{
  image_tracking_.imageCallback(msg);

  cv_bridge::CvImagePtr cv_ptr;

  try
  {
    cv_ptr = cv_bridge::toCvCopy(msg);
  }
  catch (cv_bridge::Exception& e)
  {
    ROS_ERROR("cv_bridge exception: %s", e.what());
    return;
  }

  // convert to BGR image
  if (msg->encoding == "rgb8") cv::cvtColor(cv_ptr->image, last_image_, CV_RGB2BGR);
  if (msg->encoding == "mono8")
  {
    if (color_image_)
    {
      cv::cvtColor(cv_ptr->image, last_image_, CV_BayerBG2BGR);
    }
    else
    {
      cv::cvtColor(cv_ptr->image, last_image_, CV_GRAY2BGR);
    }
  }

  if (!used_last_image_)
  {
    cv_bridge::CvImage cv_image;
    last_image_.copyTo(cv_image.image);
    cv_image.encoding = "bgr8";
    std::cout << "publish image from callback" << std::endl;
    image_pub_.publish(cv_image.toImageMsg());
  }
  used_last_image_ = false;
}

void Renderer::eventsCallback(const dvs_msgs::EventArray::ConstPtr& msg)
{
  for (int i = 0; i < msg->events.size(); ++i)
  {
    ++event_stats_[0].events_counter_[msg->events[i].polarity];
    ++event_stats_[1].events_counter_[msg->events[i].polarity];
  }

  publishStats();
  image_tracking_.eventsCallback(msg);

  // only create image if at least one subscriber
  if (image_pub_.getNumSubscribers() > 0)
  {
    cv_bridge::CvImage cv_image;
    if (msg->events.size() > 0)
    {
      cv_image.header.stamp = msg->events[msg->events.size()/2].ts;
    }

    if (display_method_ == RED_BLUE)
    {
      cv_image.encoding = "bgr8";

      if (last_image_.rows == msg->height && last_image_.cols == msg->width)
      {
        last_image_.copyTo(cv_image.image);
        used_last_image_ = true;
      }
      else
      {
        cv_image.image = cv::Mat(msg->height, msg->width, CV_8UC3);
        cv_image.image = cv::Scalar(0,0,0);
      }

      for (int i = 0; i < msg->events.size(); ++i)
      {
        const int x = msg->events[i].x;
        const int y = msg->events[i].y;

        cv_image.image.at<cv::Vec3b>(cv::Point(x, y)) = (
            msg->events[i].polarity == true ? cv::Vec3b(255, 0, 0) : cv::Vec3b(0, 0, 255));
      }
    }
    else
    {
      cv_image.encoding = "mono8";
      if (last_image_.rows == msg->height && last_image_.cols == msg->width)
      {
        cv::cvtColor(last_image_, cv_image.image, CV_BGR2GRAY);
        used_last_image_ = true;
      }
      else
      {
        cv_image.image = cv::Mat(msg->height, msg->width, CV_8U);
        cv_image.image = cv::Scalar(128);
      }

      cv::Mat on_events = cv::Mat(msg->height, msg->width, CV_8U);
      on_events = cv::Scalar(0);

      cv::Mat off_events = cv::Mat(msg->height, msg->width, CV_8U);
      off_events = cv::Scalar(0);

      // count events per pixels with polarity
      for (int i = 0; i < msg->events.size(); ++i)
      {
        const int x = msg->events[i].x;
        const int y = msg->events[i].y;

        if (msg->events[i].polarity == 1)
          on_events.at<uint8_t>(cv::Point(x, y))++;
        else
          off_events.at<uint8_t>(cv::Point(x, y))++;
      }

        // scale image
      cv::normalize(on_events, on_events, 0, 128, cv::NORM_MINMAX, CV_8UC1);
      cv::normalize(off_events, off_events, 0, 127, cv::NORM_MINMAX, CV_8UC1);

      cv_image.image += on_events;
      cv_image.image -= off_events;
    }

    image_pub_.publish(cv_image.toImageMsg());

    if (got_camera_info_ && undistorted_image_pub_.getNumSubscribers() > 0)
    {
      cv_bridge::CvImage cv_image2;
      cv_image2.encoding = cv_image.encoding;
      cv::undistort(cv_image.image, cv_image2.image, camera_matrix_, dist_coeffs_);
      undistorted_image_pub_.publish(cv_image2.toImageMsg());
    }
  }
}

void Renderer::publishStats()
{
  std_msgs::Float32 msg;
  ros::Time now = ros::Time::now();
  for (int i = 0; i < 2; ++i)
  {
    if (event_stats_[i].events_mean_lasttime_ + event_stats_[i].dt <= now.toSec()) {
      event_stats_[i].events_mean_lasttime_ = now.toSec();
      for (int k = 0; k < 2; ++k)
      {
        msg.data = (float)event_stats_[i].events_counter_[k] / event_stats_[i].dt;
        event_stats_[i].events_mean_[k].publish(msg);
        event_stats_[i].events_counter_[k] = 0;
      }
    }
  }
}

} // namespace
<|endoftext|>