message: Adds extra validation checks, and fixes NCHW bias
diff: @@ -2798,23 +2798,34 @@ Status ConvertBiasAdd(OpConverterParams* params) {
  const auto& inputs = params->inputs;
  const auto& node_def = params->node_def;
  + if (inputs.size() != 2) {
  + return errors::InvalidArgument(
  + "BiasAdd expects exactly 2 inputs, but received ", inputs.size());
  + }
  +
  + if (inputs[0].is_weights() && inputs[1].is_weights()) {
  + return errors::InvalidArgument(
  + "All inputs are weights, but Grappler is expected to fold them.");
  + }
  +
  nvinfer1::ITensor* input_tensor = inputs.at(0).tensor();
  TFAttrs attrs(node_def);
  const string& data_format = attrs.get<string>("data_format");
  - // Bias is normally 1D.
  + nvinfer1::Dims input_shape = input_tensor->getDimensions();
  nvinfer1::Dims bias_shape = inputs.at(1).GetTrtDims();
  - // If the input is NCHW, then we need to permute the bias such that its last 3
  - // dimensions are (C, 1, 1)
  + // If the input is NCHW, then we need to unsqueeze the bias such that its last
  + // 3 dimensions are (C, 1, 1)
  if (data_format == "NCHW") {
  - bias_shape.nbDims = 3;
  - std::fill(bias_shape.d + 1, bias_shape.d + nbDims, 1);
  - }
  -
  + bias_shape.nbDims = input_tensor->getDimensions().nbDims;
  + ;
  + std::fill(bias_shape.d + (bias_shape.nbDims - 3),
  + bias_shape.d + bias_shape.nbDims, 1);
  + } else {
  // Next, broadcast the bias across the input.
  - nvinfer1::Dims input_shape = input_tensor->getDimensions();
  TF_RETURN_IF_ERROR(params->converter->GetTrtBroadcastShape(
  inputs.at(0), inputs.at(1), &input_shape, &bias_shape));
  + }
  // Finally, reshape bias. Since the bias is usually a constant, this will
  // normally happen at conversion-time.
  @@ -2823,6 +2834,8 @@ Status ConvertBiasAdd(OpConverterParams* params) {
  inputs.at(1), bias_shape, params->validation_only, &bias_tensor));
  VLOG(2) << "Bias shape adjusted to " << DebugString(bias_shape);
  + if (params->validation_only) return Status::OK();
  +
  nvinfer1::IElementWiseLayer* layer =
  params->converter->network()->addElementWise(
  *input_tensor, *bias_tensor, nvinfer1::ElementWiseOperation::kSUM);
  