Skip to content

Commit bd56e04

Browse files
Merge pull request tensorflow#35548 from compnerd/r2.1-windows-build
r2.1 cherry-pick request: windows build support
2 parents 8427475 + a8adad9 commit bd56e04

File tree

12 files changed

+43
-23
lines changed

12 files changed

+43
-23
lines changed

.bazelrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ build:c++17 --cxxopt=-std=c++1z
160160
build:c++17 --cxxopt=-stdlib=libc++
161161
build:c++1z --config=c++17
162162

163+
# Tensorflow uses M_* math constants that only get defined by MSVC headers if
164+
# _USE_MATH_DEFINES is defined.
165+
build:windows --copt=/D_USE_MATH_DEFINES
166+
163167
# Default paths for TF_SYSTEM_LIBS
164168
build --define=PREFIX=/usr
165169
build --define=LIBDIR=$(PREFIX)/lib

configure.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1233,7 +1233,7 @@ def set_windows_build_flags(environ_cp):
12331233
# Fix winsock2.h conflicts
12341234
write_to_bazelrc(
12351235
'build --copt=-DWIN32_LEAN_AND_MEAN --host_copt=-DWIN32_LEAN_AND_MEAN '
1236-
'--copt=-DNOGDI --host_copt=-DNOGDI')
1236+
'--copt=-DNOGDI --host_copt=-DNOGDI --copt=-D_USE_MATH_DEFINES')
12371237
# Output more verbose information when something goes wrong
12381238
write_to_bazelrc('build --verbose_failures')
12391239
# The host and target platforms are the same in Windows build. So we don't

tensorflow/cc/gradients/math_grad.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ See the License for the specific language governing permissions and
1313
limitations under the License.
1414
==============================================================================*/
1515

16-
#define _USE_MATH_DEFINES
1716
#include <cmath>
1817

1918
#include "tensorflow/cc/ops/array_ops_internal.h"

tensorflow/compiler/xla/client/lib/math.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ limitations under the License.
1515

1616
#include "tensorflow/compiler/xla/client/lib/math.h"
1717

18-
// This macro is required to make MSVC defines math constants in math.h
19-
#define _USE_MATH_DEFINES
20-
#include <math.h>
18+
#include <cmath>
2119

2220
#include "tensorflow/compiler/xla/client/lib/arithmetic.h"
2321
#include "tensorflow/compiler/xla/client/lib/constants.h"

tensorflow/compiler/xla/client/lib/tridiagonal.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ namespace {
3636
struct TridiagonalSystemShape {
3737
const int64 rank;
3838
const int64 num_equations;
39+
TridiagonalSystemShape(int64 rk, int64 num_eqs)
40+
: rank(rk), num_equations(num_eqs) {}
3941
};
4042

4143
Status CheckSecondToLastDimension(const Shape& op_shape, int64 rank,
@@ -109,9 +111,7 @@ StatusOr<TridiagonalSystemShape> CheckSystemAndReturnShape(XlaOp lower_diagonal,
109111
TF_RETURN_IF_ERROR(CheckSecondToLastDimension(upper_diagonal_shape, rank, 1,
110112
"upper diagonal"));
111113

112-
TridiagonalSystemShape result = {.rank = rank,
113-
.num_equations = num_equations};
114-
return result;
114+
return TridiagonalSystemShape(rank, num_equations);
115115
}
116116

117117
XlaOp Coefficient(XlaOp operand, int64 i) {

tensorflow/compiler/xla/service/dynamic_dimension_inference.cc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ Status DynamicDimensionInferenceVisitor::HandleSetDimensionSize(
469469
// Propagate dynamic dimension indicated by this set dimension size
470470
// instruction.
471471
parent_->SetDynamicSize(hlo, {}, hlo->dimension(), hlo->mutable_operand(1),
472-
{.stride = 1, .multiple_of = 1});
472+
DimensionConstraint(1, 1));
473473
}
474474

475475
// Also Propagate dynamic dimension already set by operands.
@@ -813,7 +813,7 @@ Status DynamicDimensionInferenceVisitor::HandleReshape(HloInstruction* hlo) {
813813

814814
parent_->SetDynamicSize(
815815
reshape, {}, output_dynamic_dimension, new_dynamic_size,
816-
{.stride = 1, .multiple_of = constraint.multiple_of / divisor});
816+
DimensionConstraint(1, constraint.multiple_of / divisor));
817817
}
818818

819819
if (input_dim_size < output_dim_size) {
@@ -850,12 +850,12 @@ Status DynamicDimensionInferenceVisitor::HandleReshape(HloInstruction* hlo) {
850850
hlo->parent()->AddInstruction(HloInstruction::CreateBinary(
851851
output_dynamic_size->shape(), HloOpcode::kMultiply,
852852
new_dynamic_size, operand_dynamic_size));
853+
int64 new_multiple_of_constraint =
854+
constraint.multiple_of * output_dim_size /
855+
operand->shape().dimensions(input_dynamic_dimension);
853856
parent_->SetDynamicSize(
854857
reshape, {}, output_dynamic_dimension, new_dynamic_size,
855-
{.stride = 1,
856-
.multiple_of =
857-
constraint.multiple_of * output_dim_size /
858-
operand->shape().dimensions(input_dynamic_dimension)});
858+
DimensionConstraint(1, new_multiple_of_constraint));
859859
}
860860

861861
return Status::OK();
@@ -1227,7 +1227,7 @@ Status DynamicDimensionInferenceVisitor::HandleParameter(HloInstruction* hlo) {
12271227
parent_->SetDynamicSize(target_parameter,
12281228
dynamic_dimension.parameter_index,
12291229
dynamic_dimension.dimension, dynamic_size,
1230-
{.stride = 1, .multiple_of = 1});
1230+
DimensionConstraint(1, 1));
12311231
return Status::OK();
12321232
});
12331233
}

tensorflow/compiler/xla/service/dynamic_dimension_inference.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ class DynamicDimensionInference {
149149
//
150150
//
151151
struct DimensionConstraint {
152+
explicit DimensionConstraint(int64 s, int64 m)
153+
: stride(s), multiple_of(m) {}
154+
DimensionConstraint() : stride(1), multiple_of(1) {}
152155
// Stride represents the distance of a newly placed element and the previous
153156
// placed element on this dynamic dimension.
154157
int64 stride;

tensorflow/compiler/xla/service/hlo_parser.cc

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2611,18 +2611,37 @@ struct MinMaxFiniteValue<bfloat16> {
26112611
static double min() { return -max(); }
26122612
};
26132613

2614+
// MSVC's standard C++ library does not define isnan/isfinite for integer types.
2615+
// To work around that we will need to provide our own.
2616+
template <typename T>
2617+
std::enable_if_t<std::is_floating_point<T>::value, bool> IsFinite(T val) {
2618+
return std::isfinite(val);
2619+
}
2620+
template <typename T>
2621+
std::enable_if_t<std::is_floating_point<T>::value, bool> IsNaN(T val) {
2622+
return std::isnan(val);
2623+
}
2624+
template <typename T>
2625+
std::enable_if_t<std::is_integral<T>::value, bool> IsFinite(T val) {
2626+
return std::isfinite(static_cast<double>(val));
2627+
}
2628+
template <typename T>
2629+
std::enable_if_t<std::is_integral<T>::value, bool> IsNaN(T val) {
2630+
return std::isnan(static_cast<double>(val));
2631+
}
2632+
26142633
template <typename LiteralNativeT, typename ParsedElemT>
26152634
bool HloParserImpl::CheckParsedValueIsInRange(LocTy loc, ParsedElemT value) {
26162635
if (std::is_floating_point<ParsedElemT>::value) {
26172636
auto value_as_native_t = static_cast<LiteralNativeT>(value);
26182637
auto value_double_converted = static_cast<ParsedElemT>(value_as_native_t);
2619-
if (!std::isfinite(value) || std::isfinite(value_double_converted)) {
2638+
if (!IsFinite(value) || IsFinite(value_double_converted)) {
26202639
value = value_double_converted;
26212640
}
26222641
}
26232642
PrimitiveType literal_ty =
26242643
primitive_util::NativeToPrimitiveType<LiteralNativeT>();
2625-
if (std::isnan(value) ||
2644+
if (IsNaN(value) ||
26262645
(std::numeric_limits<ParsedElemT>::has_infinity &&
26272646
(std::numeric_limits<ParsedElemT>::infinity() == value ||
26282647
-std::numeric_limits<ParsedElemT>::infinity() == value))) {

tensorflow/core/lib/random/random_distributions.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ limitations under the License.
1818

1919
#include <string.h>
2020

21-
#define _USE_MATH_DEFINES
22-
#include <math.h>
2321
#include <cmath>
24-
#undef _USE_MATH_DEFINES
2522

2623
#include <algorithm>
2724
#include <type_traits>

tensorflow/core/lib/random/random_distributions_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ limitations under the License.
1515

1616
#include "tensorflow/core/lib/random/random_distributions.h"
1717

18-
#include <math.h>
1918
#include <algorithm>
19+
#include <cmath>
2020
#include <functional>
2121
#include <numeric>
2222
#include <unordered_map>

tensorflow/lite/experimental/microfrontend/lib/window_util.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ limitations under the License.
1414
==============================================================================*/
1515
#include "tensorflow/lite/experimental/microfrontend/lib/window_util.h"
1616

17-
// This macro is required to make MSVC defines math constants in math.h
18-
#define _USE_MATH_DEFINES
1917
#include <math.h>
2018
#include <stdio.h>
2119
#include <stdlib.h>

tensorflow/tensorflow.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ def get_win_copts(is_external = False):
263263
# "/EHs-c-",
264264
"/wd4577",
265265
"/DNOGDI",
266+
# Also see build:windows lines in tensorflow/opensource_only/.bazelrc
267+
# where we set some other options globally.
266268
]
267269
if is_external:
268270
return WINDOWS_COPTS + ["/UTF_COMPILE_LIBRARY"]

0 commit comments

Comments
 (0)