<commit_before>/* Copyright 2015 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.
==============================================================================*/

#ifndef TENSORFLOW_CORE_KERNELS_BROADCAST_TO_OP_H_
#define TENSORFLOW_CORE_KERNELS_BROADCAST_TO_OP_H_

#include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
#include "tensorflow/core/framework/op_kernel.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/framework/tensor_shape.h"
#include "tensorflow/core/framework/tensor_types.h"
#include "tensorflow/core/framework/types.h"
#include "tensorflow/core/kernels/fill_functor.h"

namespace tensorflow {

namespace functor {

template <typename Device, typename T>
struct BroadcastTo {
  void operator()(const Device &d, OpKernelContext *ctx, Tensor &output_tensor,
                  const TensorShape &output_shape, const Tensor &input_tensor,
                  const TensorShape &input_shape) {
#define BROADCAST_SHAPE(broadcast, reshape, NDIMS, input_shape, output_shape) \
  for (int i = 0; i < NDIMS; i++) {                                           \
    if (reshape[i] != broadcast[i]) {                                         \
      OP_REQUIRES(ctx,                                                        \
                  ((reshape[i] != 0) && (broadcast[i] % reshape[i] == 0)),    \
                  errors::InvalidArgument("invalid shape to broadcast from ", \
                                          input_shape.DebugString(), " to ",  \
                                          output_shape.DebugString()));       \
      broadcast[i] = broadcast[i] / reshape[i];                               \
    } else {                                                                  \
      broadcast[i] = 1;                                                       \
    }                                                                         \
  }

#define HANDLE_BROADCAST_FROM_SCALAR()                              \
  if (std::is_same<Eigen::GpuDevice, Device>::value) {              \
    FillFunctor<Device, T>()(d, output_tensor.flat<T>(),            \
                             input_tensor.scalar<T>());             \
  } else {                                                          \
    output.device(d) = output.constant(input_tensor.scalar<T>()()); \
  }

#define HANDLE_BROADCAST_CASE(dim_i)                                  \
  case dim_i: {                                                       \
    if (std::is_same<Eigen::GpuDevice, Device>::value &&              \
        output_tensor.NumElements() < kint32max &&                    \
        input_tensor.NumElements() < kint32max) {                     \
      auto input = input_tensor.tensor<T, dim_i>();                   \
      To32Bit(output).device(d) =                                     \
          To32Bit(input).reshape(reshape).broadcast(broadcast);       \
    } else {                                                          \
      auto input = input_tensor.tensor<T, dim_i>();                   \
      output.device(d) = input.reshape(reshape).broadcast(broadcast); \
    }                                                                 \
  } break;

    if (output_shape.num_elements() == 0) {
      return;
    }
    if (output_shape == input_shape) {
      output_tensor.flat<T>().device(d) = input_tensor.flat<T>();
      return;
    }

    switch (output_shape.dims()) {
      case 0: {
        if (input_shape.dims() > 0) {
          ctx->CtxFailure(errors::InvalidArgument(
              "invalid shape to broadcast from ", input_shape.DebugString(),
              " to ", output_shape.DebugString()));
          break;
        }
        output_tensor.scalar<T>().device(d) = input_tensor.scalar<T>();
        break;
      }
      case 1: {
        auto reshape = AsEigenDSizesWithPrefix<1>(input_shape);
        auto broadcast = output_shape.AsEigenDSizes<1>();

        BROADCAST_SHAPE(broadcast, reshape, 1, input_shape, output_shape);

        auto output = output_tensor.tensor<T, 1>();
        switch (input_shape.dims()) {
          case 0: {<commit_msg>Revert the To32Bit optimization from cl/218760066 since it doesn't compile with the new version of Eigen.<commit_after>/* Copyright 2015 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.
==============================================================================*/

#ifndef TENSORFLOW_CORE_KERNELS_BROADCAST_TO_OP_H_
#define TENSORFLOW_CORE_KERNELS_BROADCAST_TO_OP_H_

#include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
#include "tensorflow/core/framework/op_kernel.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/framework/tensor_shape.h"
#include "tensorflow/core/framework/tensor_types.h"
#include "tensorflow/core/framework/types.h"
#include "tensorflow/core/kernels/fill_functor.h"

namespace tensorflow {

namespace functor {

template <typename Device, typename T>
struct BroadcastTo {
  void operator()(const Device &d, OpKernelContext *ctx, Tensor &output_tensor,
                  const TensorShape &output_shape, const Tensor &input_tensor,
                  const TensorShape &input_shape) {
#define BROADCAST_SHAPE(broadcast, reshape, NDIMS, input_shape, output_shape) \
  for (int i = 0; i < NDIMS; i++) {                                           \
    if (reshape[i] != broadcast[i]) {                                         \
      OP_REQUIRES(ctx,                                                        \
                  ((reshape[i] != 0) && (broadcast[i] % reshape[i] == 0)),    \
                  errors::InvalidArgument("invalid shape to broadcast from ", \
                                          input_shape.DebugString(), " to ",  \
                                          output_shape.DebugString()));       \
      broadcast[i] = broadcast[i] / reshape[i];                               \
    } else {                                                                  \
      broadcast[i] = 1;                                                       \
    }                                                                         \
  }

#define HANDLE_BROADCAST_FROM_SCALAR()                              \
  if (std::is_same<Eigen::GpuDevice, Device>::value) {              \
    FillFunctor<Device, T>()(d, output_tensor.flat<T>(),            \
                             input_tensor.scalar<T>());             \
  } else {                                                          \
    output.device(d) = output.constant(input_tensor.scalar<T>()()); \
  }

#define HANDLE_BROADCAST_CASE(dim_i)                                  \
  case dim_i: {                                                       \
      auto input = input_tensor.tensor<T, dim_i>();                   \
      output.device(d) = input.reshape(reshape).broadcast(broadcast); \
  } break;

    if (output_shape.num_elements() == 0) {
      return;
    }
    if (output_shape == input_shape) {
      output_tensor.flat<T>().device(d) = input_tensor.flat<T>();
      return;
    }

    switch (output_shape.dims()) {
      case 0: {
        if (input_shape.dims() > 0) {
          ctx->CtxFailure(errors::InvalidArgument(
              "invalid shape to broadcast from ", input_shape.DebugString(),
              " to ", output_shape.DebugString()));
          break;
        }
        output_tensor.scalar<T>().device(d) = input_tensor.scalar<T>();
        break;
      }
      case 1: {
        auto reshape = AsEigenDSizesWithPrefix<1>(input_shape);
        auto broadcast = output_shape.AsEigenDSizes<1>();

        BROADCAST_SHAPE(broadcast, reshape, 1, input_shape, output_shape);

        auto output = output_tensor.tensor<T, 1>();
        switch (input_shape.dims()) {
          case 0: {
{'fraction_non_alphanumeric': 0.119404680754374, 'fraction_numerical': 0.00761190638491252, 'mean_word_length': 1.68793893129771, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 45, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3421495', 'n_tokens_mistral': 2363, 'n_tokens_neox': 2270, 'n_words': 515}