Skip to content

Commit c1e1508

Browse files
authored
Require Erlang/OTP 24 and Elixir v1.12 (#416)
* Require latest Erlang and Elixir * Add support for half-precision floats * Run the formatter
1 parent 0fb0080 commit c1e1508

20 files changed

Lines changed: 144 additions & 239 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ jobs:
1313
working-directory: "nx"
1414
strategy:
1515
matrix:
16-
elixir: [1.11.4]
17-
otp: [23.3.4]
16+
elixir: ["1.12.0-rc.1"]
17+
otp: ["24.0"]
1818
steps:
1919
- uses: actions/checkout@v2
2020
- uses: erlef/setup-beam@v1

‎exla/lib/exla/defn.ex‎

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,10 @@ defmodule EXLA.Defn do
134134
defp to_root_computation(key, expr, pos_shapes, options) do
135135
builder = EXLA.Builder.new(inspect(key))
136136

137-
# TODO: Use Enum.with_index on Elixir v1.12
138137
params =
139-
for {{pos, shape}, i} <- Enum.with_index(pos_shapes) do
138+
Enum.with_index(pos_shapes, fn {pos, shape}, i ->
140139
{pos, EXLA.Op.parameter(builder, i, shape, "p#{i}")}
141-
end
140+
end)
142141

143142
state = %{
144143
precision: Keyword.get(options, :precision, :default),
@@ -710,12 +709,7 @@ defmodule EXLA.Defn do
710709
all_static? = Enum.all?(start_indices, &is_integer/1)
711710

712711
if all_static? do
713-
# TODO: Use Enum.zip_with on Elixir v1.12
714-
limit_indices =
715-
start_indices
716-
|> Enum.zip(lengths)
717-
|> Enum.map(fn {i, len} -> i + len end)
718-
712+
limit_indices = Enum.zip_with(start_indices, lengths, fn i, len -> i + len end)
719713
EXLA.Op.slice(tensor, start_indices, limit_indices, strides)
720714
else
721715
zeros = List.duplicate(0, tuple_size(ans.shape))
@@ -812,12 +806,11 @@ defmodule EXLA.Defn do
812806
defp to_computation(name, args, state, fun) do
813807
subbuilder = subbuilder(state.builder, Atom.to_string(name))
814808

815-
# TODO: Use Enum.with_index on Elixir v1.12
816809
arg_params =
817-
for {arg, i} <- Enum.with_index(args) do
810+
Enum.with_index(args, fn arg, i ->
818811
fun_shape = computation_arg_shape(arg)
819812
{arg, EXLA.Op.parameter(subbuilder, i, fun_shape, "p#{i}")}
820-
end
813+
end)
821814

822815
{_, params} = Enum.reduce(arg_params, {0, []}, &computation_arg_param(&1, &2))
823816
state = %{state | builder: subbuilder, params: Map.new(params)}
@@ -840,11 +833,9 @@ defmodule EXLA.Defn do
840833
end
841834

842835
defp computation_arg_param({tuple, param}, counter_acc) do
843-
# TODO: Use Enum.with_index on Elixir v1.12
844836
tuple
845837
|> Tuple.to_list()
846-
|> Enum.with_index()
847-
|> Enum.map(fn {arg, i} -> {arg, EXLA.Op.get_tuple_element(param, i)} end)
838+
|> Enum.with_index(fn arg, i -> {arg, EXLA.Op.get_tuple_element(param, i)} end)
848839
|> Enum.reduce(counter_acc, &computation_arg_param/2)
849840
end
850841

‎exla/lib/exla/executable.ex‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,11 @@ defmodule EXLA.Executable do
133133
defp decompose_output(data, shape, client) do
134134
%Shape{dtype: {:t, shapes}} = shape
135135

136-
# TODO: Use Enum.zip_with on Elixir v1.12
137-
data
138-
|> Enum.zip(shapes)
139-
|> Enum.map(fn
140-
{buf, subshape} when is_reference(buf) ->
136+
Enum.zip_with(data, shapes, fn
137+
buf, subshape when is_reference(buf) ->
141138
Buffer.buffer({buf, client.name}, subshape)
142139

143-
{buf, subshape} ->
140+
buf, subshape ->
144141
Buffer.buffer(buf, subshape)
145142
end)
146143
end

‎exla/mix.exs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ defmodule EXLA.MixProject do
99
app: :exla,
1010
name: "EXLA",
1111
version: @version,
12-
elixir: "~> 1.11",
12+
elixir: "~> 1.12-dev",
1313
deps: deps(),
1414
docs: docs(),
1515
compilers: [:elixir_make] ++ Mix.compilers(),

‎nx/lib/nx.ex‎

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,13 @@ defmodule Nx do
180180
]
181181
>
182182
183-
Negative positions in ranges read from the back. The right-side of
184-
the range must be equal or greater than the left-side:
183+
Ranges can receive negative positions and they will read from
184+
the back. In such cases, the range step must be explicitly given
185+
and the right-side of the range must be equal or greater than
186+
the left-side:
185187
186188
iex> t = Nx.tensor([[1, 2], [3, 4], [5, 6], [7, 8]])
187-
iex> t[1..-2]
189+
iex> t[1..-2//1]
188190
#Nx.Tensor<
189191
s64[2][2]
190192
[
@@ -198,7 +200,7 @@ defmodule Nx do
198200
axes with ranges, it is often desired to use a list:
199201
200202
iex> t = Nx.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
201-
iex> t[[1..-2, 1..2]]
203+
iex> t[[1..-2//1, 1..2]]
202204
#Nx.Tensor<
203205
s64[2][2]
204206
[
@@ -210,14 +212,14 @@ defmodule Nx do
210212
You can mix both ranges and integers in the list too:
211213
212214
iex> t = Nx.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
213-
iex> t[[1..-2, 2]]
215+
iex> t[[1..-2//1, 2]]
214216
#Nx.Tensor<
215217
s64[2]
216218
[6, 9]
217219
>
218220
219221
If the list has less elements than axes, the remaining dimensions
220-
are returned in full (equivalent to 0..-1):
222+
are returned in full:
221223
222224
iex> t = Nx.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
223225
iex> t[[1..2]]
@@ -352,7 +354,7 @@ defmodule Nx do
352354
[44, 45, 46]
353355
>
354356
355-
Mixed types get the highest precision type:
357+
Mixed types give higher priority to floats:
356358
357359
iex> Nx.tensor([1, 2, 3.0])
358360
#Nx.Tensor<
@@ -398,9 +400,24 @@ defmodule Nx do
398400
]
399401
>
400402
403+
Besides single-precision (32 bits), floats can also have
404+
half-precision (16) or double-precision (64):
405+
406+
iex> Nx.tensor([1, 2, 3], type: {:f, 16})
407+
#Nx.Tensor<
408+
f16[3]
409+
[1.0, 2.0, 3.0]
410+
>
411+
412+
iex> Nx.tensor([1, 2, 3], type: {:f, 64})
413+
#Nx.Tensor<
414+
f64[3]
415+
[1.0, 2.0, 3.0]
416+
>
417+
401418
Brain-floating points are also supported, although they are
402419
emulated in Elixir and therefore perform slower without a
403-
compilation backend:
420+
native backend:
404421
405422
iex> Nx.tensor([1, 2, 3], type: {:bf, 16})
406423
#Nx.Tensor<
@@ -2319,7 +2336,7 @@ defmodule Nx do
23192336
23202337
"""
23212338
@doc type: :shape
2322-
def size(shape) when is_tuple(shape), do: tuple_product(shape, tuple_size(shape))
2339+
def size(shape) when is_tuple(shape), do: Tuple.product(shape)
23232340
def size(tensor), do: size(shape(tensor))
23242341

23252342
@doc """
@@ -2363,10 +2380,6 @@ defmodule Nx do
23632380
defp count_up(0, _n), do: []
23642381
defp count_up(i, n), do: [n | count_up(i - 1, n + 1)]
23652382

2366-
# TODO: Use Tuple.product on Elixir v1.12
2367-
defp tuple_product(_tuple, 0), do: 1
2368-
defp tuple_product(tuple, i), do: :erlang.element(i, tuple) * tuple_product(tuple, i - 1)
2369-
23702383
## Backend API
23712384

23722385
@backend_key {Nx, :default_backend}
@@ -7992,13 +8005,10 @@ defmodule Nx do
79928005
defp to_indices(start_indices) do
79938006
all_static? = Enum.all?(start_indices, &is_integer/1)
79948007

7995-
# TODO: Use Enum.with_index/3 in Elixir v1.12
79968008
if all_static? do
79978009
start_indices
79988010
else
7999-
start_indices
8000-
|> Enum.with_index()
8001-
|> Enum.map(fn {index, i} ->
8011+
Enum.with_index(start_indices, fn index, i ->
80028012
%T{shape: idx_shape, type: idx_type} = t = to_tensor(index)
80038013

80048014
unless idx_shape == {} do

‎nx/lib/nx/backend.ex‎

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -203,46 +203,37 @@ defmodule Nx.Backend do
203203
<<x::float-little-32>> = <<0::16, bf16::binary>>
204204
Float.to_string(x)
205205
end
206-
207-
defp inspect_float(data, 32) do
208-
case data do
209-
<<0xFF800000::32-native>> -> "-Inf"
210-
<<0x7F800000::32-native>> -> "Inf"
211-
<<_::16, 1::1, _::7, _sign::1, 0x7F::7>> -> "NaN"
212-
<<x::float-32-native>> -> Float.to_string(x)
213-
end
214-
end
215-
216-
defp inspect_float(data, 64) do
217-
case data do
218-
<<0x7FF0000000000000::64-native>> -> "Inf"
219-
<<0xFFF0000000000000::64-native>> -> "-Inf"
220-
<<_::48, 0xF::4, _::4, _sign::1, 0x7F::7>> -> "NaN"
221-
<<x::float-64-native>> -> Float.to_string(x)
222-
end
223-
end
224206
else
225207
defp inspect_bf16(bf16) do
226208
<<x::float-big-32>> = <<bf16::binary, 0::16>>
227209
Float.to_string(x)
228210
end
211+
end
229212

230-
defp inspect_float(data, 32) do
231-
case data do
232-
<<0xFF800000::32-native>> -> "-Inf"
233-
<<0x7F800000::32-native>> -> "Inf"
234-
<<_sign::1, 0x7F::7, 1::1, _::7, _::16>> -> "NaN"
235-
<<x::float-32-native>> -> Float.to_string(x)
236-
end
213+
defp inspect_float(data, 16) do
214+
case data do
215+
<<0xFC00::16-native>> -> "-Inf"
216+
<<0x7C00::16-native>> -> "Inf"
217+
<<x::float-16-native>> -> Float.to_string(x)
218+
_ -> "NaN"
237219
end
220+
end
238221

239-
defp inspect_float(data, 64) do
240-
case data do
241-
<<0x7FF0000000000000::64-native>> -> "Inf"
242-
<<0xFFF0000000000000::64-native>> -> "-Inf"
243-
<<_sign::1, 0x7F::7, 0xF::4, _::4, _::48>> -> "NaN"
244-
<<x::float-64-native>> -> Float.to_string(x)
245-
end
222+
defp inspect_float(data, 32) do
223+
case data do
224+
<<0xFF800000::32-native>> -> "-Inf"
225+
<<0x7F800000::32-native>> -> "Inf"
226+
<<x::float-32-native>> -> Float.to_string(x)
227+
_ -> "NaN"
228+
end
229+
end
230+
231+
defp inspect_float(data, 64) do
232+
case data do
233+
<<0x7FF0000000000000::64-native>> -> "Inf"
234+
<<0xFFF0000000000000::64-native>> -> "-Inf"
235+
<<x::float-64-native>> -> Float.to_string(x)
236+
_ -> "NaN"
246237
end
247238
end
248239
end

‎nx/lib/nx/binary_backend.ex‎

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -688,20 +688,9 @@ defmodule Nx.BinaryBackend do
688688
defp element_remainder(_, a, b) when is_integer(a) and is_integer(b), do: rem(a, b)
689689
defp element_remainder(_, a, b), do: :math.fmod(a, b)
690690

691-
defp element_power({type, _}, a, b) when type in [:s, :u], do: integer_pow(a, b)
691+
defp element_power({type, _}, a, b) when type in [:s, :u], do: Integer.pow(a, b)
692692
defp element_power(_, a, b), do: :math.pow(a, b)
693693

694-
# TODO: Use Integer.pow on Elixir v1.12
695-
defp integer_pow(base, exponent) when is_integer(base) and is_integer(exponent) do
696-
if exponent < 0, do: :erlang.error(:badarith, [base, exponent])
697-
guarded_pow(base, exponent)
698-
end
699-
700-
defp guarded_pow(_, 0), do: 1
701-
defp guarded_pow(b, 1), do: b
702-
defp guarded_pow(b, e) when (e &&& 1) == 0, do: guarded_pow(b * b, e >>> 1)
703-
defp guarded_pow(b, e), do: b * guarded_pow(b * b, e >>> 1)
704-
705694
defp element_bitwise_and(_, a, b), do: :erlang.band(a, b)
706695
defp element_bitwise_or(_, a, b), do: :erlang.bor(a, b)
707696
defp element_bitwise_xor(_, a, b), do: :erlang.bxor(a, b)
@@ -950,11 +939,10 @@ defmodule Nx.BinaryBackend do
950939
# as a list because it is handled in the Nx module before
951940
# lowering to the implementation; however, the padding
952941
# configuration only accounts for spatial dims
953-
# TODO: Use Enum.zip_with on Elixir v1.12
954942
spatial_padding_config =
955-
padding
956-
|> Enum.zip(input_dilation)
957-
|> Enum.map(fn {{lo, hi}, dilation} -> {lo, hi, dilation - 1} end)
943+
Enum.zip_with(padding, input_dilation, fn {lo, hi}, dilation ->
944+
{lo, hi, dilation - 1}
945+
end)
958946

959947
padding_config = [
960948
{0, 0, 0},
@@ -1623,11 +1611,10 @@ defmodule Nx.BinaryBackend do
16231611

16241612
# The weighted shape is altered such that we traverse
16251613
# with respect to the stride for each dimension
1626-
# TODO: Use Enum.zip_with on Elixir v1.12
16271614
weighted_shape =
1628-
weighted_shape
1629-
|> Enum.zip(strides)
1630-
|> Enum.map(fn {{d, dim_size}, s} -> {d, dim_size + (s - 1) * dim_size} end)
1615+
Enum.zip_with(weighted_shape, strides, fn {d, dim_size}, s ->
1616+
{d, dim_size + (s - 1) * dim_size}
1617+
end)
16311618

16321619
input_data = to_binary(tensor)
16331620

@@ -1639,10 +1626,7 @@ defmodule Nx.BinaryBackend do
16391626
end
16401627

16411628
defp clamp_indices(start_indices, shape, lengths) do
1642-
# TODO: Use Enum.zip_with on Elixir v1.12
1643-
[Tuple.to_list(shape), start_indices, lengths]
1644-
|> Enum.zip()
1645-
|> Enum.map(fn {dim_size, idx, len} ->
1629+
Enum.zip_with([Tuple.to_list(shape), start_indices, lengths], fn [dim_size, idx, len] ->
16461630
idx = to_scalar(idx)
16471631
min(max(idx, 0), dim_size - len)
16481632
end)

‎nx/lib/nx/binary_backend/matrix.ex‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -403,15 +403,12 @@ defmodule Nx.BinaryBackend.Matrix do
403403

404404
# This function also sorts singular values from highest to lowest,
405405
# as this can be convenient.
406-
407-
# TODO: Use Enum.zip_with on Elixir v1.12
408406
s
409-
|> Enum.zip(transpose_matrix(v))
410-
|> Enum.map(fn
411-
{singular_value, row} when singular_value < 0 ->
407+
|> Enum.zip_with(transpose_matrix(v), fn
408+
singular_value, row when singular_value < 0 ->
412409
{-singular_value, Enum.map(row, &(&1 * -1))}
413410

414-
{singular_value, row} ->
411+
singular_value, row ->
415412
{singular_value, row}
416413
end)
417414
|> Enum.sort_by(fn {s, _} -> s end, &>=/2)
@@ -645,9 +642,7 @@ defmodule Nx.BinaryBackend.Matrix do
645642
end
646643

647644
defp transpose_matrix(m) do
648-
m
649-
|> Enum.zip()
650-
|> Enum.map(&Tuple.to_list/1)
645+
Enum.zip_with(m, & &1)
651646
end
652647

653648
defp matrix_to_binary([r | _] = m, type) when is_list(r) do

0 commit comments

Comments
 (0)